From eec74d42efe433ba15806aa87f00ccf4427463ba Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Wed, 12 May 2021 16:23:38 -0700 Subject: [PATCH] ip.WithPort --- 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 | 6 ++---- 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, 42 insertions(+), 52 deletions(-) diff --git a/disco/disco.go b/disco/disco.go index e43f6103b..89b65b15f 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.IPFrom16(a).WithPort(port)) p = p[epLength:] } return m, nil diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 00065623a..8aa12105d 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 ip.WithPort(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 = nodeIP(peer, netaddr.IP.Is4).WithPort(p4) case have6 && p6 != 0: - ipp = netaddr.IPPort{IP: nodeIP(peer, netaddr.IP.Is6), Port: p6} + ipp = nodeIP(peer, netaddr.IP.Is6).WithPort(p6) } if ipp.IP.IsZero() { return "" diff --git a/net/dns/manager.go b/net/dns/manager.go index e59842548..6ec15bf0e 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, ip.WithPort(53)) } return ret } diff --git a/net/dns/resolver/tsdns_test.go b/net/dns/resolver/tsdns_test.go index 993d7411f..40e8c7dfc 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.IPv4(1, 1, 1, 1).WithPort(1001)}, + {"test.site.", dns.TypeAAAA, netaddr.IPv4(1, 1, 1, 1).WithPort(1002)}, } // packets will have the same dns txid. diff --git a/net/netcheck/netcheck.go b/net/netcheck/netcheck.go index 097b58704..3d36bf7b2 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 ip.WithPort(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 ip.WithPort(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 ip.WithPort(uint16(port)).UDPAddr() } default: return nil diff --git a/net/packet/tsmp.go b/net/packet/tsmp.go index 87475d033..d662c9576 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: pp.Dst.IP.WithPort(binary.BigEndian.Uint16(p[3:5])), + Dst: pp.Src.IP.WithPort(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..a70f41cf3 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, m.gw.WithPort(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: myIP.WithPort(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 := gw.WithPort(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 := gw.WithPort(pcpPort).UDPAddr() + pmpAddr := gw.WithPort(pmpPort).UDPAddr() + upnpAddr := gw.WithPort(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..1f5c307ba 100644 --- a/tstest/natlab/nat.go +++ b/tstest/natlab/nat.go @@ -237,10 +237,8 @@ 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), - } + port := uint16(pc.LocalAddr().(*net.UDPAddr).Port) + addr := ip.WithPort(port) return pc, addr } diff --git a/tstest/natlab/natlab.go b/tstest/natlab/natlab.go index 2d3f3ae70..3cda25b32 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}, + v6unspec.WithPort(p.Dst.Port), + v4unspec.WithPort(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 := ip.WithPort(port) c := &conn{ m: m, diff --git a/tstest/natlab/natlab_test.go b/tstest/natlab/natlab_test.go index 1119e804d..eaac31e5e 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 := ifFoo.V4().WithPort(123) + barAddr := ifBar.V4().WithPort(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 := ifClient.V4().WithPort(123) + natLANAddr := ifNATLAN.V4().WithPort(456) + natWANAddr := ifNATWAN.V4().WithPort(456) + serverAddr := ifServer.V4().WithPort(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 := ifServer.V4().WithPort(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 := ifNATWAN.V4().WithPort(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..57b82e270 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 = tsaddr.CGNATRange().IP.Next().WithPort(1234) + internet4 = netaddr.MustParseIP("8.8.8.8").WithPort(1234) + ts6 = tsaddr.TailscaleULARange().IP.Next().WithPort(1234) + internet6 = netaddr.MustParseIP("2001::1").WithPort(1234) ) tests := []struct { diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index 4291e13d9..bf31d02fb 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(derpMagicIPAddr.WithPort(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(ip.WithPort(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 := derpMagicIPAddr.WithPort(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.IPFrom16(addr).WithPort(12345) } // isFirstRecvActivityInAwhile notes that receive activity has occured for this diff --git a/wgengine/netstack/netstack.go b/wgengine/netstack/netstack.go index a8c9aa6bd..bfe6f5490 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 ip.WithPort(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 ip.WithPort(uint16(port16)), nil } func (ns *Impl) DialContextTCP(ctx context.Context, addr string) (*gonet.TCPConn, error) {