golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.88k stars 17.65k forks source link

net: apply ICANN/IANA-managed semantics to IP.IsGlobalUnicast #11772

Open mike-joseph opened 9 years ago

mike-joseph commented 9 years ago

net.IP.IsGlobalUnicast() applies incorrect address semantics. The current implementation returns True if the address is not Unspecified, Loopback, LinkLocal, or Multicast. However, a Global Unicast address, by definition, excludes more than just those categories. Class E space is to be excluded, as is all of RFC1918 & RFC6598.

This manifests in a few significant ways:

net.ParseIP("255.255.255.255").IsGlobalUnicast() currently returns True net.ParseIP("10.1.2.3").IsGlobalUnicast() currently returns True

I would propose the following changes:

Ideally, it would be even better to extend both further using the tables in RFC6890 as a guideline.

bradfitz commented 9 years ago

/cc @mikioh

mikioh commented 9 years ago

Go 1.5 includes the fix for #11585, for the case of IPv4 limited broadcast address.

rekby commented 7 years ago

False positive for net.ParseIP("FC00::1").IsGlobalUnicast() currently return true but https://tools.ietf.org/html/rfc4193 define prefix FC00::/7 as local unicast.

mikioh commented 7 years ago

@rekby,

The IsGlobalUnicast method of IP struct simply follows RFC 4291 when the IP is an IPv6 address. That means that the method just uses address type identification of https://tools.ietf.org/html/rfc4291#section-2.5.4 because it's hard to identify various IPv6 addresses without context; for example, in the case of constructing sort of IPv4-IPv6 translator applications using addresses defined in RFC 6052.

There are a few options: a) just updating documentation like "it just follows RFC 4291", b) making IsGlobalUnicast on unique local IPv6 unicast addresses return false, c) adding IsUniqueLocalUnicast method to IP, though I have no strong opinion about the latter two.

mikioh commented 7 years ago

though I have no strong opinion about the latter two.

To be honest, I'd like to see some package that provides both IPv4 and IPv6 address identification using IETF addressing architectures, IANA registries and application context as an external package.