bio-routing / bio-rd

bio routing is a project to create a versatile, fast and reliable routing daemon in Golang. bio = BGP + IS-IS + OSPF
Apache License 2.0
283 stars 44 forks source link

net.IPFromString misparses ::ffff:0.0.0.0 #477

Open taktv6 opened 1 month ago

taktv6 commented 1 month ago

Describe the bug When parsing ::ffff:0.0.0.0 net.IPFromString returns an all zero IPv4 address while IPv6 is expected.

Steps to Reproduce


import (
    "fmt"
    "net"

    bnet "github.com/bio-routing/bio-rd/net"
)

func main() {
    bio()
    upstream()
}

func upstream() {
    fmt.Printf("golang net:\n")
    ip := net.ParseIP("::ffff:0.0.0.0")
    v4 := ip.To4()
    if v4 != nil {
        fmt.Printf("is IPv4!\n")
    }

    v6 := ip.To16()
    if v6 != nil {
        fmt.Printf("Is IPv6!\n")
    }

    fmt.Printf("v4: %s\n", v4.String())
    fmt.Printf("v6: %s\n", v6.String())
}

func bio() {
    fmt.Printf("bio net:\n")
    ip, err := bnet.IPFromString("::ffff:0.0.0.0")
    if err != nil {
        panic(err)
    }

    fmt.Printf("Is IPv4: %v\n", ip.IsIPv4())

    fmt.Printf("Addr: %s\n", ip.String())
}

Is IPv4: true
Addr: 0.0.0.0
golang net:
is IPv4!
Is IPv6!
v4: 0.0.0.0
v6: 0.0.0.0```
**Expected behavior**
net.IPFromString should return `::ffff:0.0.0.0` or `::ffff:0:0`

**Additional context**
This behavior was inherited from upstream net.ParseIP function. It basically casts any IPv6 mapped IPv4 address into a pure IPv4 address from which it is not possible to detect that it actually was an IPv6 address.