mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-11 13:18:53 +00:00
all: migrate more code code to net/netip directly
Instead of going through the tailscale.com/net/netaddr transitional wrappers. Updates #5162 Change-Id: I3dafd1c2effa1a6caa9b7151ecf6edd1a3fda3dd Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:

committed by
Brad Fitzpatrick

parent
eb32847d85
commit
8725b14056
@@ -20,42 +20,14 @@ func IPv4(a, b, c, d uint8) netip.Addr {
|
||||
return netip.AddrFrom4([4]byte{a, b, c, d})
|
||||
}
|
||||
|
||||
// IPFrom16 returns the IP address given by the bytes in addr, unmapping any
|
||||
// v6-mapped IPv4 address.
|
||||
//
|
||||
// It is equivalent to calling IPv6Raw(addr).Unmap().
|
||||
func IPFrom16(a [16]byte) netip.Addr {
|
||||
return netip.AddrFrom16(a).Unmap()
|
||||
}
|
||||
|
||||
// FromStdIP returns an IP from the standard library's IP type.
|
||||
//
|
||||
// If std is invalid, ok is false.
|
||||
//
|
||||
// FromStdIP implicitly unmaps IPv6-mapped IPv4 addresses. That is, if
|
||||
// len(std) == 16 and contains an IPv4 address, only the IPv4 part is
|
||||
// returned, without the IPv6 wrapper. This is the common form returned by
|
||||
// the standard library's ParseIP: https://play.golang.org/p/qdjylUkKWxl.
|
||||
// To convert a standard library IP without the implicit unmapping, use
|
||||
// netip.AddrFromSlice.
|
||||
func FromStdIP(std net.IP) (ip netip.Addr, ok bool) {
|
||||
ret, ok := netip.AddrFromSlice(std)
|
||||
if !ok {
|
||||
return ret, false
|
||||
}
|
||||
if ret.Is4In6() {
|
||||
return ret.Unmap(), true
|
||||
}
|
||||
return ret, true
|
||||
}
|
||||
|
||||
// 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) {
|
||||
ip, ok := FromStdIP(std.IP)
|
||||
ip, ok := netip.AddrFromSlice(std.IP)
|
||||
if !ok {
|
||||
return netip.Prefix{}, false
|
||||
}
|
||||
ip = ip.Unmap()
|
||||
|
||||
if l := len(std.Mask); l != net.IPv4len && l != net.IPv6len {
|
||||
// Invalid mask.
|
||||
@@ -74,7 +46,7 @@ func FromStdIPNet(std *net.IPNet) (prefix netip.Prefix, ok bool) {
|
||||
// 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 := FromStdIP(stdIP)
|
||||
ip, ok := netip.AddrFromSlice(stdIP)
|
||||
if !ok || port < 0 || port > math.MaxUint16 {
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user