mirror of
https://github.com/tailscale/tailscale.git
synced 2025-10-08 23:49:56 +00:00
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>
This commit is contained in:

committed by
Brad Fitzpatrick

parent
6a396731eb
commit
a12aad6b47
@@ -9,8 +9,6 @@ import (
|
||||
"net/netip"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"tailscale.com/net/netaddr"
|
||||
)
|
||||
|
||||
// FirewallType is the type of filtering a stateful firewall
|
||||
@@ -38,8 +36,8 @@ const (
|
||||
// some fields, so in practice the key is either a 2-tuple (src only),
|
||||
// 3-tuple (src ip+port and dst ip) or 4-tuple (src+dst ip+port).
|
||||
type fwKey struct {
|
||||
src netaddr.IPPort
|
||||
dst netaddr.IPPort
|
||||
src netip.AddrPort
|
||||
dst netip.AddrPort
|
||||
}
|
||||
|
||||
// key returns an fwKey for the given src and dst, trimmed according
|
||||
@@ -48,7 +46,7 @@ type fwKey struct {
|
||||
// world), it's the caller's responsibility to swap src and dst in the
|
||||
// call to key when processing packets inbound from the "untrusted"
|
||||
// world.
|
||||
func (s FirewallType) key(src, dst netaddr.IPPort) fwKey {
|
||||
func (s FirewallType) key(src, dst netip.AddrPort) fwKey {
|
||||
k := fwKey{src: src}
|
||||
switch s {
|
||||
case EndpointIndependentFirewall:
|
||||
|
@@ -11,15 +11,13 @@ import (
|
||||
"net/netip"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"tailscale.com/net/netaddr"
|
||||
)
|
||||
|
||||
// mapping is the state of an allocated NAT session.
|
||||
type mapping struct {
|
||||
lanSrc netaddr.IPPort
|
||||
lanDst netaddr.IPPort
|
||||
wanSrc netaddr.IPPort
|
||||
lanSrc netip.AddrPort
|
||||
lanDst netip.AddrPort
|
||||
wanSrc netip.AddrPort
|
||||
deadline time.Time
|
||||
|
||||
// pc is a PacketConn that reserves an outbound port on the NAT's
|
||||
@@ -55,10 +53,10 @@ const (
|
||||
// fields, so in practice the key is either a 2-tuple (src only),
|
||||
// 3-tuple (src ip+port and dst ip) or 4-tuple (src+dst ip+port).
|
||||
type natKey struct {
|
||||
src, dst netaddr.IPPort
|
||||
src, dst netip.AddrPort
|
||||
}
|
||||
|
||||
func (t NATType) key(src, dst netaddr.IPPort) natKey {
|
||||
func (t NATType) key(src, dst netip.AddrPort) natKey {
|
||||
k := natKey{src: src}
|
||||
switch t {
|
||||
case EndpointIndependentNAT:
|
||||
@@ -102,7 +100,7 @@ type SNAT44 struct {
|
||||
|
||||
mu sync.Mutex
|
||||
byLAN map[natKey]*mapping // lookup by outbound packet tuple
|
||||
byWAN map[netaddr.IPPort]*mapping // lookup by wan ip:port only
|
||||
byWAN map[netip.AddrPort]*mapping // lookup by wan ip:port only
|
||||
}
|
||||
|
||||
func (n *SNAT44) timeNow() time.Time {
|
||||
@@ -122,7 +120,7 @@ func (n *SNAT44) mappingTimeout() time.Duration {
|
||||
func (n *SNAT44) initLocked() {
|
||||
if n.byLAN == nil {
|
||||
n.byLAN = map[natKey]*mapping{}
|
||||
n.byWAN = map[netaddr.IPPort]*mapping{}
|
||||
n.byWAN = map[netip.AddrPort]*mapping{}
|
||||
}
|
||||
if n.ExternalInterface.Machine() != n.Machine {
|
||||
panic(fmt.Sprintf("NAT given interface %s that is not part of given machine %s", n.ExternalInterface, n.Machine.Name))
|
||||
@@ -228,7 +226,7 @@ func (n *SNAT44) HandleForward(p *Packet, iif, oif *Interface) *Packet {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *SNAT44) allocateMappedPort() (net.PacketConn, netaddr.IPPort) {
|
||||
func (n *SNAT44) allocateMappedPort() (net.PacketConn, netip.AddrPort) {
|
||||
// Clean up old entries before trying to allocate, to free up any
|
||||
// expired ports.
|
||||
n.gc()
|
||||
@@ -238,7 +236,7 @@ func (n *SNAT44) allocateMappedPort() (net.PacketConn, netaddr.IPPort) {
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("ran out of NAT ports: %v", err))
|
||||
}
|
||||
addr := netaddr.IPPortFrom(ip, uint16(pc.LocalAddr().(*net.UDPAddr).Port))
|
||||
addr := netip.AddrPortFrom(ip, uint16(pc.LocalAddr().(*net.UDPAddr).Port))
|
||||
return pc, addr
|
||||
}
|
||||
|
||||
|
@@ -31,7 +31,7 @@ var traceOn, _ = strconv.ParseBool(os.Getenv("NATLAB_TRACE"))
|
||||
|
||||
// Packet represents a UDP packet flowing through the virtual network.
|
||||
type Packet struct {
|
||||
Src, Dst netaddr.IPPort
|
||||
Src, Dst netip.AddrPort
|
||||
Payload []byte
|
||||
|
||||
// Prefix set by various internal methods of natlab, to locate
|
||||
@@ -80,7 +80,7 @@ func (p *Packet) setLocator(msg string, args ...any) {
|
||||
p.locator = fmt.Sprintf(" "+msg, args...)
|
||||
}
|
||||
|
||||
func mustPrefix(s string) netaddr.IPPrefix {
|
||||
func mustPrefix(s string) netip.Prefix {
|
||||
ipp, err := netip.ParsePrefix(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -100,14 +100,14 @@ func NewInternet() *Network {
|
||||
|
||||
type Network struct {
|
||||
Name string
|
||||
Prefix4 netaddr.IPPrefix
|
||||
Prefix6 netaddr.IPPrefix
|
||||
Prefix4 netip.Prefix
|
||||
Prefix6 netip.Prefix
|
||||
|
||||
mu sync.Mutex
|
||||
machine map[netaddr.IP]*Interface
|
||||
machine map[netip.Addr]*Interface
|
||||
defaultGW *Interface // optional
|
||||
lastV4 netaddr.IP
|
||||
lastV6 netaddr.IP
|
||||
lastV4 netip.Addr
|
||||
lastV6 netip.Addr
|
||||
}
|
||||
|
||||
func (n *Network) SetDefaultGateway(gwIf *Interface) {
|
||||
@@ -119,21 +119,21 @@ func (n *Network) SetDefaultGateway(gwIf *Interface) {
|
||||
n.defaultGW = gwIf
|
||||
}
|
||||
|
||||
func (n *Network) addMachineLocked(ip netaddr.IP, iface *Interface) {
|
||||
func (n *Network) addMachineLocked(ip netip.Addr, iface *Interface) {
|
||||
if iface == nil {
|
||||
return // for tests
|
||||
}
|
||||
if n.machine == nil {
|
||||
n.machine = map[netaddr.IP]*Interface{}
|
||||
n.machine = map[netip.Addr]*Interface{}
|
||||
}
|
||||
n.machine[ip] = iface
|
||||
}
|
||||
|
||||
func (n *Network) allocIPv4(iface *Interface) netaddr.IP {
|
||||
func (n *Network) allocIPv4(iface *Interface) netip.Addr {
|
||||
n.mu.Lock()
|
||||
defer n.mu.Unlock()
|
||||
if !n.Prefix4.IsValid() {
|
||||
return netaddr.IP{}
|
||||
return netip.Addr{}
|
||||
}
|
||||
if !n.lastV4.IsValid() {
|
||||
n.lastV4 = n.Prefix4.Addr()
|
||||
@@ -148,11 +148,11 @@ func (n *Network) allocIPv4(iface *Interface) netaddr.IP {
|
||||
return n.lastV4
|
||||
}
|
||||
|
||||
func (n *Network) allocIPv6(iface *Interface) netaddr.IP {
|
||||
func (n *Network) allocIPv6(iface *Interface) netip.Addr {
|
||||
n.mu.Lock()
|
||||
defer n.mu.Unlock()
|
||||
if !n.Prefix6.IsValid() {
|
||||
return netaddr.IP{}
|
||||
return netip.Addr{}
|
||||
}
|
||||
if !n.lastV6.IsValid() {
|
||||
n.lastV6 = n.Prefix6.Addr()
|
||||
@@ -212,7 +212,7 @@ type Interface struct {
|
||||
machine *Machine
|
||||
net *Network
|
||||
name string // optional
|
||||
ips []netaddr.IP // static; not mutated once created
|
||||
ips []netip.Addr // static; not mutated once created
|
||||
}
|
||||
|
||||
func (f *Interface) Machine() *Machine {
|
||||
@@ -224,18 +224,18 @@ func (f *Interface) Network() *Network {
|
||||
}
|
||||
|
||||
// V4 returns the machine's first IPv4 address, or the zero value if none.
|
||||
func (f *Interface) V4() netaddr.IP { return f.pickIP(netaddr.IP.Is4) }
|
||||
func (f *Interface) V4() netip.Addr { return f.pickIP(netip.Addr.Is4) }
|
||||
|
||||
// V6 returns the machine's first IPv6 address, or the zero value if none.
|
||||
func (f *Interface) V6() netaddr.IP { return f.pickIP(netaddr.IP.Is6) }
|
||||
func (f *Interface) V6() netip.Addr { return f.pickIP(netip.Addr.Is6) }
|
||||
|
||||
func (f *Interface) pickIP(pred func(netaddr.IP) bool) netaddr.IP {
|
||||
func (f *Interface) pickIP(pred func(netip.Addr) bool) netip.Addr {
|
||||
for _, ip := range f.ips {
|
||||
if pred(ip) {
|
||||
return ip
|
||||
}
|
||||
}
|
||||
return netaddr.IP{}
|
||||
return netip.Addr{}
|
||||
}
|
||||
|
||||
func (f *Interface) String() string {
|
||||
@@ -247,7 +247,7 @@ func (f *Interface) String() string {
|
||||
}
|
||||
|
||||
// Contains reports whether f contains ip as an IP.
|
||||
func (f *Interface) Contains(ip netaddr.IP) bool {
|
||||
func (f *Interface) Contains(ip netip.Addr) bool {
|
||||
for _, v := range f.ips {
|
||||
if ip == v {
|
||||
return true
|
||||
@@ -257,7 +257,7 @@ func (f *Interface) Contains(ip netaddr.IP) bool {
|
||||
}
|
||||
|
||||
type routeEntry struct {
|
||||
prefix netaddr.IPPrefix
|
||||
prefix netip.Prefix
|
||||
iface *Interface
|
||||
}
|
||||
|
||||
@@ -341,11 +341,11 @@ type Machine struct {
|
||||
interfaces []*Interface
|
||||
routes []routeEntry // sorted by longest prefix to shortest
|
||||
|
||||
conns4 map[netaddr.IPPort]*conn // conns that want IPv4 packets
|
||||
conns6 map[netaddr.IPPort]*conn // conns that want IPv6 packets
|
||||
conns4 map[netip.AddrPort]*conn // conns that want IPv4 packets
|
||||
conns6 map[netip.AddrPort]*conn // conns that want IPv6 packets
|
||||
}
|
||||
|
||||
func (m *Machine) isLocalIP(ip netaddr.IP) bool {
|
||||
func (m *Machine) isLocalIP(ip netip.Addr) bool {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for _, intf := range m.interfaces {
|
||||
@@ -392,10 +392,10 @@ func (m *Machine) deliverLocalPacket(p *Packet, iface *Interface) {
|
||||
if p.Dst.Addr().Is6() {
|
||||
conns = m.conns6
|
||||
}
|
||||
possibleDsts := []netaddr.IPPort{
|
||||
possibleDsts := []netip.AddrPort{
|
||||
p.Dst,
|
||||
netaddr.IPPortFrom(v6unspec, p.Dst.Port()),
|
||||
netaddr.IPPortFrom(v4unspec, p.Dst.Port()),
|
||||
netip.AddrPortFrom(v6unspec, p.Dst.Port()),
|
||||
netip.AddrPortFrom(v4unspec, p.Dst.Port()),
|
||||
}
|
||||
for _, dest := range possibleDsts {
|
||||
c, ok := conns[dest]
|
||||
@@ -443,7 +443,7 @@ func (m *Machine) forwardPacket(p *Packet, iif *Interface) {
|
||||
oif.net.write(p)
|
||||
}
|
||||
|
||||
func unspecOf(ip netaddr.IP) netaddr.IP {
|
||||
func unspecOf(ip netip.Addr) netip.Addr {
|
||||
if ip.Is4() {
|
||||
return v4unspec
|
||||
}
|
||||
@@ -562,7 +562,7 @@ func (m *Machine) writePacket(p *Packet) (n int, err error) {
|
||||
return iface.net.write(p)
|
||||
}
|
||||
|
||||
func (m *Machine) interfaceForIP(ip netaddr.IP) (*Interface, error) {
|
||||
func (m *Machine) interfaceForIP(ip netip.Addr) (*Interface, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for _, re := range m.routes {
|
||||
@@ -642,12 +642,12 @@ func (m *Machine) unregisterConn6(c *conn) {
|
||||
delete(m.conns6, c.ipp)
|
||||
}
|
||||
|
||||
func registerConn(conns *map[netaddr.IPPort]*conn, c *conn) error {
|
||||
func registerConn(conns *map[netip.AddrPort]*conn, c *conn) error {
|
||||
if _, ok := (*conns)[c.ipp]; ok {
|
||||
return fmt.Errorf("duplicate conn listening on %v", c.ipp)
|
||||
}
|
||||
if *conns == nil {
|
||||
*conns = map[netaddr.IPPort]*conn{}
|
||||
*conns = map[netip.AddrPort]*conn{}
|
||||
}
|
||||
(*conns)[c.ipp] = c
|
||||
return nil
|
||||
@@ -659,7 +659,7 @@ func (m *Machine) ListenPacket(ctx context.Context, network, address string) (ne
|
||||
// if udp4, udp6, etc... look at address IP vs unspec
|
||||
var (
|
||||
fam uint8
|
||||
ip netaddr.IP
|
||||
ip netip.Addr
|
||||
)
|
||||
switch network {
|
||||
default:
|
||||
@@ -705,7 +705,7 @@ func (m *Machine) ListenPacket(ctx context.Context, network, address string) (ne
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
ipp := netaddr.IPPortFrom(ip, port)
|
||||
ipp := netip.AddrPortFrom(ip, port)
|
||||
|
||||
c := &conn{
|
||||
m: m,
|
||||
@@ -738,7 +738,7 @@ func (m *Machine) ListenPacket(ctx context.Context, network, address string) (ne
|
||||
type conn struct {
|
||||
m *Machine
|
||||
fam uint8 // 0, 4, or 6
|
||||
ipp netaddr.IPPort
|
||||
ipp netip.AddrPort
|
||||
|
||||
mu sync.Mutex
|
||||
closed bool
|
||||
|
@@ -12,15 +12,14 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"tailscale.com/net/netaddr"
|
||||
"tailscale.com/tstest"
|
||||
)
|
||||
|
||||
func TestAllocIPs(t *testing.T) {
|
||||
n := NewInternet()
|
||||
saw := map[netaddr.IP]bool{}
|
||||
saw := map[netip.Addr]bool{}
|
||||
for i := 0; i < 255; i++ {
|
||||
for _, f := range []func(*Interface) netaddr.IP{n.allocIPv4, n.allocIPv6} {
|
||||
for _, f := range []func(*Interface) netip.Addr{n.allocIPv4, n.allocIPv6} {
|
||||
ip := f(nil)
|
||||
if saw[ip] {
|
||||
t.Fatalf("got duplicate %v", ip)
|
||||
@@ -51,8 +50,8 @@ func TestSendPacket(t *testing.T) {
|
||||
ifFoo := foo.Attach("eth0", internet)
|
||||
ifBar := bar.Attach("enp0s1", internet)
|
||||
|
||||
fooAddr := netaddr.IPPortFrom(ifFoo.V4(), 123)
|
||||
barAddr := netaddr.IPPortFrom(ifBar.V4(), 456)
|
||||
fooAddr := netip.AddrPortFrom(ifFoo.V4(), 123)
|
||||
barAddr := netip.AddrPortFrom(ifBar.V4(), 456)
|
||||
|
||||
ctx := context.Background()
|
||||
fooPC, err := foo.ListenPacket(ctx, "udp4", fooAddr.String())
|
||||
@@ -113,10 +112,10 @@ func TestMultiNetwork(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
clientAddr := netaddr.IPPortFrom(ifClient.V4(), 123)
|
||||
natLANAddr := netaddr.IPPortFrom(ifNATLAN.V4(), 456)
|
||||
natWANAddr := netaddr.IPPortFrom(ifNATWAN.V4(), 456)
|
||||
serverAddr := netaddr.IPPortFrom(ifServer.V4(), 789)
|
||||
clientAddr := netip.AddrPortFrom(ifClient.V4(), 123)
|
||||
natLANAddr := netip.AddrPortFrom(ifNATLAN.V4(), 456)
|
||||
natWANAddr := netip.AddrPortFrom(ifNATWAN.V4(), 456)
|
||||
serverAddr := netip.AddrPortFrom(ifServer.V4(), 789)
|
||||
|
||||
const msg1, msg2 = "hello", "world"
|
||||
if _, err := natPC.WriteTo([]byte(msg1), net.UDPAddrFromAddrPort(clientAddr)); err != nil {
|
||||
@@ -151,7 +150,7 @@ func TestMultiNetwork(t *testing.T) {
|
||||
}
|
||||
|
||||
type trivialNAT struct {
|
||||
clientIP netaddr.IP
|
||||
clientIP netip.Addr
|
||||
lanIf, wanIf *Interface
|
||||
}
|
||||
|
||||
@@ -218,7 +217,7 @@ func TestPacketHandler(t *testing.T) {
|
||||
}
|
||||
|
||||
const msg = "some message"
|
||||
serverAddr := netaddr.IPPortFrom(ifServer.V4(), 456)
|
||||
serverAddr := netip.AddrPortFrom(ifServer.V4(), 456)
|
||||
if _, err := clientPC.WriteTo([]byte(msg), net.UDPAddrFromAddrPort(serverAddr)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -232,7 +231,7 @@ func TestPacketHandler(t *testing.T) {
|
||||
if string(buf) != msg {
|
||||
t.Errorf("read %q; want %q", buf, msg)
|
||||
}
|
||||
mappedAddr := netaddr.IPPortFrom(ifNATWAN.V4(), 123)
|
||||
mappedAddr := netip.AddrPortFrom(ifNATWAN.V4(), 123)
|
||||
if addr.String() != mappedAddr.String() {
|
||||
t.Errorf("addr = %q; want %q", addr, mappedAddr)
|
||||
}
|
||||
@@ -318,7 +317,7 @@ func TestFirewall(t *testing.T) {
|
||||
|
||||
type fwTest struct {
|
||||
iif, oif *Interface
|
||||
src, dst netaddr.IPPort
|
||||
src, dst netip.AddrPort
|
||||
ok bool
|
||||
}
|
||||
|
||||
@@ -341,7 +340,7 @@ func testFirewall(t *testing.T, f *Firewall, tests []fwTest) {
|
||||
}
|
||||
}
|
||||
|
||||
func ipp(str string) netaddr.IPPort {
|
||||
func ipp(str string) netip.AddrPort {
|
||||
ipp, err := netip.ParseAddrPort(str)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -455,7 +454,7 @@ func TestNAT(t *testing.T) {
|
||||
}
|
||||
|
||||
type natTest struct {
|
||||
src, dst netaddr.IPPort
|
||||
src, dst netip.AddrPort
|
||||
wantNewMapping bool
|
||||
}
|
||||
|
||||
@@ -463,7 +462,7 @@ func testNAT(t *testing.T, n *SNAT44, lanIf, wanIf *Interface, tests []natTest)
|
||||
clock := &tstest.Clock{}
|
||||
n.TimeNow = clock.Now
|
||||
|
||||
mappings := map[netaddr.IPPort]bool{}
|
||||
mappings := map[netip.AddrPort]bool{}
|
||||
for _, test := range tests {
|
||||
clock.Advance(time.Second)
|
||||
p := &Packet{
|
||||
|
Reference in New Issue
Block a user