2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-07-25 03:08:42 +00:00
|
|
|
|
|
|
|
// 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.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func IPv4(a, b, c, d uint8) netip.Addr {
|
2022-07-25 03:08:42 +00:00
|
|
|
return netip.AddrFrom4([4]byte{a, b, c, d})
|
|
|
|
}
|
|
|
|
|
2022-08-03 04:48:56 +00:00
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
2022-07-25 03:08:42 +00:00
|
|
|
// FromStdIPNet returns an IPPrefix from the standard library's IPNet type.
|
|
|
|
// If std is invalid, ok is false.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func FromStdIPNet(std *net.IPNet) (prefix netip.Prefix, ok bool) {
|
2022-08-02 20:38:11 +00:00
|
|
|
ip, ok := netip.AddrFromSlice(std.IP)
|
2022-07-25 03:08:42 +00:00
|
|
|
if !ok {
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
return netip.Prefix{}, false
|
2022-07-25 03:08:42 +00:00
|
|
|
}
|
2022-08-02 20:38:11 +00:00
|
|
|
ip = ip.Unmap()
|
2022-07-25 03:08:42 +00:00
|
|
|
|
|
|
|
if l := len(std.Mask); l != net.IPv4len && l != net.IPv6len {
|
|
|
|
// Invalid mask.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
return netip.Prefix{}, false
|
2022-07-25 03:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ones, bits := std.Mask.Size()
|
|
|
|
if ones == 0 && bits == 0 {
|
|
|
|
// IPPrefix does not support non-contiguous masks.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
return netip.Prefix{}, false
|
2022-07-25 03:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return netip.PrefixFrom(ip, ones), true
|
|
|
|
}
|