[net/netip] About the check the ip version in netip.AddrFromSlice

185 views
Skip to first unread message

woolf ian

unread,
Oct 23, 2022, 1:17:09 PM10/23/22
to golang-dev
Hi

i notice some intresting in the issue #54826(https://github.com/golang/go/issues/54826). I need some advice and review on this issue and cl(https://go-review.googlesource.com/c/go/+/443196).


the netip.AddrFromSlice(https://cs.opensource.google/go/go/+/refs/tags/go1.19:src/net/netip/netip.go;l=345-353) only checks the length of the slice. when the length of the slice is 16, the slice will be treated as ipv6.

But the comment of  net.IP(https://cs.opensource.google/go/go/+/refs/tags/go1.19:src/net/ip.go;l=33states that the length is not proof to verify the ip version.

> IP address as an IPv4 address or an IPv6 address
> is a semantic property of the address, not just the
> length of the byte slice: a 16-byte slice can still
> be an IPv4 address.

so i think netip.AddrFromSlice should check the ip version when the param slice's length is 16. and i send a cl(https://go-review.googlesource.com/c/go/+/443196).

please feel free for the review the cl, or discuss in the issue.   
Thanks the submitter of the issue.

Eli Lindsey

unread,
Oct 23, 2022, 2:10:09 PM10/23/22
to woolf ian, golang-dev
so i think netip.AddrFromSlice should check the ip version when the param slice's length is 16. and i send a cl(https://go-review.googlesource.com/c/go/+/443196).

This would cause AddrFromSlice to convert IPv4-mapped addresses from their IPv6 form to their IPv4 form, which is a breaking API change.

IPv4-mapped IPv6 has a habit of complicating APIs. Generally the best thing to do is write your software IPv6-centric and let v4 be handled by mapped addresses, then you can seamlessly support both with a single codepath and fewer components needing to understand the difference (the APIs nicely handle this today).

If you don’t want to do that and your software does want to distinguish between the two, the easiest thing would be to call Unmap directly after the AddrFromSlice conversion - that will truncate any IPv4-mapped IPv6 addresses back to IPv4 such that Is4() will report true, but leave proper IPv6 addresses as-is (so is safe to call on all addresses).

-eli

--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-dev/f84172e5-7e56-4774-bb81-66d610f5af65n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages