mirror of
https://github.com/tailscale/tailscale.git
synced 2024-12-01 14:05:39 +00:00
5f6abcfa6f
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>
51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Package netaddr is a transitional package while we finish migrating from inet.af/netaddr
|
|
// to Go 1.18's net/netip.
|
|
//
|
|
// TODO(bradfitz): delete this package eventually. Tracking bug is
|
|
// https://github.com/tailscale/tailscale/issues/5162
|
|
package netaddr
|
|
|
|
import (
|
|
"net"
|
|
"net/netip"
|
|
)
|
|
|
|
// IPv4 returns the IP of the IPv4 address a.b.c.d.
|
|
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) {
|
|
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.
|
|
return netip.Prefix{}, false
|
|
}
|
|
|
|
ones, bits := std.Mask.Size()
|
|
if ones == 0 && bits == 0 {
|
|
// IPPrefix does not support non-contiguous masks.
|
|
return netip.Prefix{}, false
|
|
}
|
|
|
|
return netip.PrefixFrom(ip, ones), true
|
|
}
|