What version of Go are you using (go version)?
$ go version
go version go1.18 windows/amd64
Same behaviour on playground.
Does this issue reproduce with the latest release?
Yes.
What did you do?
https://go.dev/play/p/tohTC3rXoZt
cidrString := "::ffff:4.4.4.4/64"
fmt.Println(cidrString)
_, cidr, _ := net.ParseCIDR(cidrString)
fmt.Println(cidr) // == ::/64, seems like it could be the problem
ip := net.ParseIP("::ffff:4.4.4.4")
fmt.Println(ip)
fmt.Println(cidr.Contains(ip)) // == false, seems wrong
ip = net.ParseIP("4.4.4.4")
fmt.Println(ip)
fmt.Println(cidr.Contains(ip)) // == false, seems maybe wrong
fmt.Println("---")
cidrString = "::ffff:4.4.4.4/124"
fmt.Println(cidrString)
_, cidr, _ = net.ParseCIDR(cidrString)
fmt.Println(cidr)
ip = net.ParseIP("::ffff:4.4.4.4")
fmt.Println(ip)
fmt.Println(cidr.Contains(ip)) // == true, seems right
ip = net.ParseIP("4.4.4.4")
fmt.Println(ip)
fmt.Println(cidr.Contains(ip)) // == true, seems right
What did you expect to see?
That ::ffff:4.4.4.4/64 would contain ::ffff:4.4.4.4 and 4.4.4.4 (or at least one of them, like netip.Prefix).
What did you see instead?
It doesn't.
My guess is that the problem is that ::ffff:4.4.4.4/64 is turning into the IPv6 CIDR ::/64, but ::ffff:4.4.4.4 is turning into the IPv4 4.4.4.4. And the v4 IP will never be contained in the v6 CIDR. In contrast, ::ffff:4.4.4.4/124 turns into a v4 CIDR and contains the IPs.
What version of Go are you using (
go version)?Same behaviour on playground.
Does this issue reproduce with the latest release?
Yes.
What did you do?
https://go.dev/play/p/tohTC3rXoZt
What did you expect to see?
That
::ffff:4.4.4.4/64would contain::ffff:4.4.4.4and4.4.4.4(or at least one of them, likenetip.Prefix).What did you see instead?
It doesn't.
My guess is that the problem is that
::ffff:4.4.4.4/64is turning into the IPv6 CIDR::/64, but::ffff:4.4.4.4is turning into the IPv44.4.4.4. And the v4 IP will never be contained in the v6 CIDR. In contrast,::ffff:4.4.4.4/124turns into a v4 CIDR and contains the IPs.