netaddr.NewIPPort

This commit is contained in:
Josh Bleecher Snyder 2021-05-12 16:21:22 -07:00
parent ebcd7ab890
commit 920145a836
13 changed files with 41 additions and 52 deletions

View File

@ -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

View File

@ -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 ""

View File

@ -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
}

View File

@ -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.

View File

@ -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

View File

@ -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]

View File

@ -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

View File

@ -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
}

View File

@ -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,

View File

@ -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)
}

View File

@ -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 {

View File

@ -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

View File

@ -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) {