all: migrate code from netaddr.FromStdAddr to Go 1.18

With caveat https://github.com/golang/go/issues/53607#issuecomment-1203466984
that then requires a new wrapper. But a simpler one at least.

Updates #5162

Change-Id: I0a5265065bfcd7f21e8dd65b2bd74cae90d76090
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2022-08-02 21:48:56 -07:00
committed by Brad Fitzpatrick
parent 7c7e23d87a
commit 5f6abcfa6f
7 changed files with 36 additions and 35 deletions

View File

@ -10,7 +10,6 @@
package netaddr
import (
"math"
"net"
"net/netip"
)
@ -20,6 +19,13 @@ func IPv4(a, b, c, d uint8) netip.Addr {
return netip.AddrFrom4([4]byte{a, b, c, d})
}
// Unmap returns the provided AddrPort with its Addr IP component Unmap'ed.
//
// See https://github.com/golang/go/issues/53607#issuecomment-1203466984
func Unmap(ap netip.AddrPort) netip.AddrPort {
return netip.AddrPortFrom(ap.Addr().Unmap(), ap.Port())
}
// FromStdIPNet returns an IPPrefix from the standard library's IPNet type.
// If std is invalid, ok is false.
func FromStdIPNet(std *net.IPNet) (prefix netip.Prefix, ok bool) {
@ -42,21 +48,3 @@ func FromStdIPNet(std *net.IPNet) (prefix netip.Prefix, ok bool) {
return netip.PrefixFrom(ip, ones), true
}
// FromStdAddr maps the components of a standard library TCPAddr or
// UDPAddr into an IPPort.
func FromStdAddr(stdIP net.IP, port int, zone string) (_ netip.AddrPort, ok bool) {
ip, ok := netip.AddrFromSlice(stdIP)
if !ok || port < 0 || port > math.MaxUint16 {
return
}
ip = ip.Unmap()
if zone != "" {
if ip.Is4() {
ok = false
return
}
ip = ip.WithZone(zone)
}
return netip.AddrPortFrom(ip, uint16(port)), true
}