From 920145a836e8eace4697216be8a0f7e4755b08c8 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Wed, 12 May 2021 16:21:22 -0700 Subject: [PATCH] netaddr.NewIPPort --- disco/disco.go | 6 ++---- ipn/ipnlocal/local.go | 9 +++------ net/dns/manager.go | 2 +- net/dns/resolver/tsdns_test.go | 4 ++-- net/netcheck/netcheck.go | 6 +++--- net/packet/tsmp.go | 4 ++-- net/portmapper/portmapper.go | 12 ++++++------ tstest/natlab/nat.go | 5 +---- tstest/natlab/natlab.go | 6 +++--- tstest/natlab/natlab_test.go | 16 ++++++++-------- wgengine/filter/filter_test.go | 8 ++++---- wgengine/magicsock/magicsock.go | 11 ++++------- wgengine/netstack/netstack.go | 4 ++-- 13 files changed, 41 insertions(+), 52 deletions(-) diff --git a/disco/disco.go b/disco/disco.go index e43f6103b..9995f1a4a 100644 --- a/disco/disco.go +++ b/disco/disco.go @@ -164,10 +164,8 @@ func parseCallMeMaybe(ver uint8, p []byte) (m *CallMeMaybe, err error) { for len(p) > 0 { var a [16]byte copy(a[:], p) - m.MyNumber = append(m.MyNumber, netaddr.IPPort{ - IP: netaddr.IPFrom16(a), - Port: binary.BigEndian.Uint16(p[16:18]), - }) + port := binary.BigEndian.Uint16(p[16:18]) + m.MyNumber = append(m.MyNumber, netaddr.NewIPPort(netaddr.IPFrom16(a), port)) p = p[epLength:] } return m, nil diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 00065623a..01e458045 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -1796,10 +1796,7 @@ func parseResolver(cfg tailcfg.DNSResolver) (netaddr.IPPort, error) { if err != nil { return netaddr.IPPort{}, fmt.Errorf("[unexpected] non-IP resolver %q", cfg.Addr) } - return netaddr.IPPort{ - IP: ip, - Port: 53, - }, nil + return netaddr.NewIPPort(ip, 53), nil } // tailscaleVarRoot returns the root directory of Tailscale's writable @@ -2580,9 +2577,9 @@ func peerAPIBase(nm *netmap.NetworkMap, peer *tailcfg.Node) string { var ipp netaddr.IPPort switch { case have4 && p4 != 0: - ipp = netaddr.IPPort{IP: nodeIP(peer, netaddr.IP.Is4), Port: p4} + ipp = netaddr.NewIPPort(nodeIP(peer, netaddr.IP.Is4), p4) case have6 && p6 != 0: - ipp = netaddr.IPPort{IP: nodeIP(peer, netaddr.IP.Is6), Port: p6} + ipp = netaddr.NewIPPort(nodeIP(peer, netaddr.IP.Is6), p6) } if ipp.IP.IsZero() { return "" diff --git a/net/dns/manager.go b/net/dns/manager.go index e59842548..6286607f4 100644 --- a/net/dns/manager.go +++ b/net/dns/manager.go @@ -219,7 +219,7 @@ func toIPsOnly(ipps []netaddr.IPPort) (ret []netaddr.IP) { func toIPPorts(ips []netaddr.IP) (ret []netaddr.IPPort) { ret = make([]netaddr.IPPort, 0, len(ips)) for _, ip := range ips { - ret = append(ret, netaddr.IPPort{IP: ip, Port: 53}) + ret = append(ret, netaddr.NewIPPort(ip, 53)) } return ret } diff --git a/net/dns/resolver/tsdns_test.go b/net/dns/resolver/tsdns_test.go index 993d7411f..17b4e49c4 100644 --- a/net/dns/resolver/tsdns_test.go +++ b/net/dns/resolver/tsdns_test.go @@ -433,8 +433,8 @@ func TestDelegateCollision(t *testing.T) { qtype dns.Type addr netaddr.IPPort }{ - {"test.site.", dns.TypeA, netaddr.IPPort{IP: netaddr.IPv4(1, 1, 1, 1), Port: 1001}}, - {"test.site.", dns.TypeAAAA, netaddr.IPPort{IP: netaddr.IPv4(1, 1, 1, 1), Port: 1002}}, + {"test.site.", dns.TypeA, netaddr.NewIPPort(netaddr.IPv4(1, 1, 1, 1), 1001)}, + {"test.site.", dns.TypeAAAA, netaddr.NewIPPort(netaddr.IPv4(1, 1, 1, 1), 1002)}, } // packets will have the same dns txid. diff --git a/net/netcheck/netcheck.go b/net/netcheck/netcheck.go index 097b58704..8fe1eedab 100644 --- a/net/netcheck/netcheck.go +++ b/net/netcheck/netcheck.go @@ -1172,7 +1172,7 @@ func (c *Client) nodeAddr(ctx context.Context, n *tailcfg.DERPNode, proto probeP if proto == probeIPv6 && ip.Is4() { return nil } - return netaddr.IPPort{IP: ip, Port: uint16(port)}.UDPAddr() + return netaddr.NewIPPort(ip, uint16(port)).UDPAddr() } switch proto { @@ -1182,7 +1182,7 @@ func (c *Client) nodeAddr(ctx context.Context, n *tailcfg.DERPNode, proto probeP if !ip.Is4() { return nil } - return netaddr.IPPort{IP: ip, Port: uint16(port)}.UDPAddr() + return netaddr.NewIPPort(ip, uint16(port)).UDPAddr() } case probeIPv6: if n.IPv6 != "" { @@ -1190,7 +1190,7 @@ func (c *Client) nodeAddr(ctx context.Context, n *tailcfg.DERPNode, proto probeP if !ip.Is6() { return nil } - return netaddr.IPPort{IP: ip, Port: uint16(port)}.UDPAddr() + return netaddr.NewIPPort(ip, uint16(port)).UDPAddr() } default: return nil diff --git a/net/packet/tsmp.go b/net/packet/tsmp.go index 87475d033..b7e874312 100644 --- a/net/packet/tsmp.go +++ b/net/packet/tsmp.go @@ -192,8 +192,8 @@ func (pp *Parsed) AsTailscaleRejectedHeader() (h TailscaleRejectedHeader, ok boo Reason: TailscaleRejectReason(p[2]), IPSrc: pp.Src.IP, IPDst: pp.Dst.IP, - Src: netaddr.IPPort{IP: pp.Dst.IP, Port: binary.BigEndian.Uint16(p[3:5])}, - Dst: netaddr.IPPort{IP: pp.Src.IP, Port: binary.BigEndian.Uint16(p[5:7])}, + Src: netaddr.NewIPPort(pp.Dst.IP, binary.BigEndian.Uint16(p[3:5])), + Dst: netaddr.NewIPPort(pp.Src.IP, binary.BigEndian.Uint16(p[5:7])), } if len(p) > 7 { flags := p[7] diff --git a/net/portmapper/portmapper.go b/net/portmapper/portmapper.go index 0bf4e3028..74676ff20 100644 --- a/net/portmapper/portmapper.go +++ b/net/portmapper/portmapper.go @@ -95,7 +95,7 @@ func (m *pmpMapping) release() { } defer uc.Close() pkt := buildPMPRequestMappingPacket(m.internal.Port, m.external.Port, pmpMapLifetimeDelete) - uc.WriteTo(pkt, netaddr.IPPort{IP: m.gw, Port: pmpPort}.UDPAddr()) + uc.WriteTo(pkt, netaddr.NewIPPort(m.gw, pmpPort).UDPAddr()) } // NewClient returns a new portmapping client. @@ -256,7 +256,7 @@ func (c *Client) CreateOrGetMapping(ctx context.Context) (external netaddr.IPPor localPort := c.localPort m := &pmpMapping{ gw: gw, - internal: netaddr.IPPort{IP: myIP, Port: localPort}, + internal: netaddr.NewIPPort(myIP, localPort), } // prevPort is the port we had most previously, if any. We try @@ -297,7 +297,7 @@ func (c *Client) CreateOrGetMapping(ctx context.Context) (external netaddr.IPPor uc.SetReadDeadline(time.Now().Add(portMapServiceTimeout)) defer closeCloserOnContextDone(ctx, uc)() - pmpAddr := netaddr.IPPort{IP: gw, Port: pmpPort} + pmpAddr := netaddr.NewIPPort(gw, pmpPort) pmpAddru := pmpAddr.UDPAddr() // Ask for our external address if needed. @@ -468,9 +468,9 @@ func (c *Client) Probe(ctx context.Context) (res ProbeResult, err error) { defer cancel() defer closeCloserOnContextDone(ctx, uc)() - pcpAddr := netaddr.IPPort{IP: gw, Port: pcpPort}.UDPAddr() - pmpAddr := netaddr.IPPort{IP: gw, Port: pmpPort}.UDPAddr() - upnpAddr := netaddr.IPPort{IP: gw, Port: upnpPort}.UDPAddr() + pcpAddr := netaddr.NewIPPort(gw, pcpPort).UDPAddr() + pmpAddr := netaddr.NewIPPort(gw, pmpPort).UDPAddr() + upnpAddr := netaddr.NewIPPort(gw, upnpPort).UDPAddr() // Don't send probes to services that we recently learned (for // the same gw/myIP) are available. See diff --git a/tstest/natlab/nat.go b/tstest/natlab/nat.go index a4fbb9ee1..36c1d80d9 100644 --- a/tstest/natlab/nat.go +++ b/tstest/natlab/nat.go @@ -237,10 +237,7 @@ func (n *SNAT44) allocateMappedPort() (net.PacketConn, netaddr.IPPort) { if err != nil { panic(fmt.Sprintf("ran out of NAT ports: %v", err)) } - addr := netaddr.IPPort{ - IP: ip, - Port: uint16(pc.LocalAddr().(*net.UDPAddr).Port), - } + addr := netaddr.NewIPPort(ip, uint16(pc.LocalAddr().(*net.UDPAddr).Port)) return pc, addr } diff --git a/tstest/natlab/natlab.go b/tstest/natlab/natlab.go index 2d3f3ae70..ace415664 100644 --- a/tstest/natlab/natlab.go +++ b/tstest/natlab/natlab.go @@ -396,8 +396,8 @@ func (m *Machine) deliverLocalPacket(p *Packet, iface *Interface) { } possibleDsts := []netaddr.IPPort{ p.Dst, - netaddr.IPPort{IP: v6unspec, Port: p.Dst.Port}, - netaddr.IPPort{IP: v4unspec, Port: p.Dst.Port}, + netaddr.NewIPPort(v6unspec, p.Dst.Port), + netaddr.NewIPPort(v4unspec, p.Dst.Port), } for _, dest := range possibleDsts { c, ok := conns[dest] @@ -707,7 +707,7 @@ func (m *Machine) ListenPacket(ctx context.Context, network, address string) (ne return nil, nil } } - ipp := netaddr.IPPort{IP: ip, Port: port} + ipp := netaddr.NewIPPort(ip, port) c := &conn{ m: m, diff --git a/tstest/natlab/natlab_test.go b/tstest/natlab/natlab_test.go index 1119e804d..08372d1bd 100644 --- a/tstest/natlab/natlab_test.go +++ b/tstest/natlab/natlab_test.go @@ -49,8 +49,8 @@ func TestSendPacket(t *testing.T) { ifFoo := foo.Attach("eth0", internet) ifBar := bar.Attach("enp0s1", internet) - fooAddr := netaddr.IPPort{IP: ifFoo.V4(), Port: 123} - barAddr := netaddr.IPPort{IP: ifBar.V4(), Port: 456} + fooAddr := netaddr.NewIPPort(ifFoo.V4(), 123) + barAddr := netaddr.NewIPPort(ifBar.V4(), 456) ctx := context.Background() fooPC, err := foo.ListenPacket(ctx, "udp4", fooAddr.String()) @@ -111,10 +111,10 @@ func TestMultiNetwork(t *testing.T) { t.Fatal(err) } - clientAddr := netaddr.IPPort{IP: ifClient.V4(), Port: 123} - natLANAddr := netaddr.IPPort{IP: ifNATLAN.V4(), Port: 456} - natWANAddr := netaddr.IPPort{IP: ifNATWAN.V4(), Port: 456} - serverAddr := netaddr.IPPort{IP: ifServer.V4(), Port: 789} + clientAddr := netaddr.NewIPPort(ifClient.V4(), 123) + natLANAddr := netaddr.NewIPPort(ifNATLAN.V4(), 456) + natWANAddr := netaddr.NewIPPort(ifNATWAN.V4(), 456) + serverAddr := netaddr.NewIPPort(ifServer.V4(), 789) const msg1, msg2 = "hello", "world" if _, err := natPC.WriteTo([]byte(msg1), clientAddr.UDPAddr()); err != nil { @@ -216,7 +216,7 @@ func TestPacketHandler(t *testing.T) { } const msg = "some message" - serverAddr := netaddr.IPPort{IP: ifServer.V4(), Port: 456} + serverAddr := netaddr.NewIPPort(ifServer.V4(), 456) if _, err := clientPC.WriteTo([]byte(msg), serverAddr.UDPAddr()); err != nil { t.Fatal(err) } @@ -230,7 +230,7 @@ func TestPacketHandler(t *testing.T) { if string(buf) != msg { t.Errorf("read %q; want %q", buf, msg) } - mappedAddr := netaddr.IPPort{IP: ifNATWAN.V4(), Port: 123} + mappedAddr := netaddr.NewIPPort(ifNATWAN.V4(), 123) if addr.String() != mappedAddr.String() { t.Errorf("addr = %q; want %q", addr, mappedAddr) } diff --git a/wgengine/filter/filter_test.go b/wgengine/filter/filter_test.go index ca807a5fd..8a4fab6eb 100644 --- a/wgengine/filter/filter_test.go +++ b/wgengine/filter/filter_test.go @@ -425,10 +425,10 @@ func TestLoggingPrivacy(t *testing.T) { f.logIPs = logB.IPSet() var ( - ts4 = netaddr.IPPort{IP: tsaddr.CGNATRange().IP.Next(), Port: 1234} - internet4 = netaddr.IPPort{IP: netaddr.MustParseIP("8.8.8.8"), Port: 1234} - ts6 = netaddr.IPPort{IP: tsaddr.TailscaleULARange().IP.Next(), Port: 1234} - internet6 = netaddr.IPPort{IP: netaddr.MustParseIP("2001::1"), Port: 1234} + ts4 = netaddr.NewIPPort(tsaddr.CGNATRange().IP.Next(), 1234) + internet4 = netaddr.NewIPPort(netaddr.MustParseIP("8.8.8.8"), 1234) + ts6 = netaddr.NewIPPort(tsaddr.TailscaleULARange().IP.Next(), 1234) + internet6 = netaddr.NewIPPort(netaddr.MustParseIP("2001::1"), 1234) ) tests := []struct { diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index 4291e13d9..0ee7e2fe8 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -965,7 +965,7 @@ func (c *Conn) goDerpConnect(node int) { if node == 0 { return } - go c.derpWriteChanOfAddr(netaddr.IPPort{IP: derpMagicIPAddr, Port: uint16(node)}, key.Public{}) + go c.derpWriteChanOfAddr(netaddr.NewIPPort(derpMagicIPAddr, uint16(node)), key.Public{}) } // determineEndpoints returns the machine's endpoint addresses. It @@ -1037,7 +1037,7 @@ func (c *Conn) determineEndpoints(ctx context.Context) ([]tailcfg.Endpoint, erro ips = loopback } for _, ip := range ips { - addAddr(netaddr.IPPort{IP: ip, Port: uint16(localAddr.Port)}, tailcfg.EndpointLocal) + addAddr(netaddr.NewIPPort(ip, uint16(localAddr.Port)), tailcfg.EndpointLocal) } } else { // Our local endpoint is bound to a particular address. @@ -1676,7 +1676,7 @@ func (c *Conn) processDERPReadResult(dm derpReadResult, b []byte) (n int, ep con return 0, nil } - ipp := netaddr.IPPort{IP: derpMagicIPAddr, Port: uint16(regionID)} + ipp := netaddr.NewIPPort(derpMagicIPAddr, uint16(regionID)) if c.handleDiscoMessage(b[:n], ipp) { return 0, nil } @@ -3254,10 +3254,7 @@ func (de *discoEndpoint) initFakeUDPAddr() { addr[0] = 0xfd addr[1] = 0x00 binary.BigEndian.PutUint64(addr[2:], uint64(reflect.ValueOf(de).Pointer())) - de.fakeWGAddr = netaddr.IPPort{ - IP: netaddr.IPFrom16(addr), - Port: 12345, - } + de.fakeWGAddr = netaddr.NewIPPort(netaddr.IPFrom16(addr), 12345) } // isFirstRecvActivityInAwhile notes that receive activity has occured for this diff --git a/wgengine/netstack/netstack.go b/wgengine/netstack/netstack.go index a8c9aa6bd..d8607876c 100644 --- a/wgengine/netstack/netstack.go +++ b/wgengine/netstack/netstack.go @@ -316,7 +316,7 @@ func (m DNSMap) Resolve(ctx context.Context, addr string) (netaddr.IPPort, error // Try MagicDNS first, else otherwise a real DNS lookup. ip := m[host] if !ip.IsZero() { - return netaddr.IPPort{IP: ip, Port: uint16(port16)}, nil + return netaddr.NewIPPort(ip, uint16(port16)), nil } // No MagicDNS name so try real DNS. @@ -329,7 +329,7 @@ func (m DNSMap) Resolve(ctx context.Context, addr string) (netaddr.IPPort, error return netaddr.IPPort{}, fmt.Errorf("DNS lookup returned no results for %q", host) } ip, _ = netaddr.FromStdIP(ips[0]) - return netaddr.IPPort{IP: ip, Port: uint16(port16)}, nil + return netaddr.NewIPPort(ip, uint16(port16)), nil } func (ns *Impl) DialContextTCP(ctx context.Context, addr string) (*gonet.TCPConn, error) {