-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: net: add func(IP) Unmap() IP to help when using net/netip #54234
Comments
And/or: package net
// Addr returns ip in its netip.Addr form, unmapped. ....
func (ip IP) Addr() netip.Addr { ... } Then people can get the unmapped form from /cc @rsc |
And then make the net.TCPAddr.AddrPort (and UDPAddr) method use .Addr to unmap the net.IP. |
This proposal has been added to the active column of the proposals project |
FWIW, IPv4 returning a 16-byte IP address dates to the very first commit in this package: https://go.googlesource.com/go/+/e8a02230f215efb075cccd4146b3d0d1ada4870e/src/lib/net/ip.go // IPv4 addresses are 4 bytes; IPv6 addresses are 16 bytes. At the time (2008) that seemed like the way of the future. |
The proposal is two pieces:
The method name Addr is confusing because net.Addr is something else. There are no good names for that method. So here's another possibility:
That will accomplish the same thing but will not have the confusing method name. We could do just the second too, if we didn't want to imply in package net's API that there is an important semantic difference within package net. But others will probably need Unmap to do the conversion, and there is a semantic difference when you're crossing the boundary into netip. We will need to document in the IP.Unmap method that package net will treat the input and output exactly the same in all cases. (But netip may not.) |
I don't think The IMO this behavior should be preserved, because this is how the underlying socket works. Those who wish to unmap IPv4-mapped IPv6 addresses can always do the unmapping themselves in their own code. |
And if there's no address family specified, as in |
Except it has always been like this. On most systems Unless being able to run on OpenBSD and DragonFly BSD is a requirement, one can usually safely assume that |
(Rather than fork this whole conversation in two threads, I've only replied in #53607 (comment)) |
Last week I wrote that there could be two parts:
#53607 seems to have taken on the question of whether to do the second. For this issue, let's decide whether to do the first. Given that we have one package (net) that almost always returns 16-byte addresses and another package (net/netip) that cares about 4-byte vs 16-byte, it makes sense to have a converter available. That would be IP.Unmap. Does anyone object to adding IP.Unmap? |
With this issue restricted to adding IP.Unmap, with a clear doc comment saying that it is only meaningful when using net/netip - net doesn't treat the input and output differently - the consensus seems to be that it makes sense to provide this functionality, specifically for crossing the net -> netip boundary. Do I have that right? Does anyone object to adding IP.Unmap? The discussion about TCPAddr / UDPAddr is now #53607. |
I don't object to that per se, but I don't think that it's that useful, since you can already do: newIP := netip.AddrFromSlice(oldIP).Unmap() In other words, it really makes no difference whether one unmaps I feel like the original API proposed in the OP is more of a convenience specifically for |
I think at the turn in the discussion at #54234 (comment), I didn't realize that we already had netip.AddrFromSlice(old).Unmap() when I proposed we add old.Unmap to use in netip.AddrFromSlice(old.Unmap()). The proposal started out as about TCPAddr and UDPAddr but that has moved to #53607, so maybe there's nothing left here. Closing this as a duplicate of #53607. |
This proposal is a duplicate of a previously discussed proposal, as noted above, |
net.TCPAddr.AddrPort
andnet.UDPAddr.AddrPort
can return anetip.AddrPort
containing a 4-in-6 IP. (See #53607 (comment))To "fix" those with a
netip.Addr
, we have https://pkg.go.dev/net/netip#Addr.UnmapBut with an AddrPort, it's verbose:
netip.AddrPortFrom(ap.Addr().Unmap(), ap.Port())
I propose we add:
/cc @rsc @josharian @maisem @ianlancetaylor
The text was updated successfully, but these errors were encountered: