wgengine/magicsock, types/nettype, etc: finish ReadFromUDPAddrPort netip migration

So we're staying within the netip.Addr/AddrPort consistently and
avoiding allocs/conversions to the legacy net addr types.

Updates #5162

Change-Id: I59feba60d3de39f773e68292d759766bac98c917
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2023-04-15 13:08:16 -07:00
committed by Brad Fitzpatrick
parent 29f7df9d8f
commit 10f1c90f4d
8 changed files with 48 additions and 54 deletions
+1 -1
View File
@@ -521,7 +521,7 @@ func (f *forwarder) sendUDP(ctx context.Context, fq *forwardQuery, rr resolverAn
// The 1 extra byte is to detect packet truncation.
out := make([]byte, maxResponseBytes+1)
n, _, err := conn.ReadFrom(out)
n, _, err := conn.ReadFromUDPAddrPort(out)
if err != nil {
if err := ctx.Err(); err != nil {
return nil, err
+4 -9
View File
@@ -208,7 +208,7 @@ type Client struct {
// reusing an existing UDP connection.
type STUNConn interface {
WriteToUDPAddrPort([]byte, netip.AddrPort) (int, error)
ReadFrom([]byte) (int, net.Addr, error)
ReadFromUDPAddrPort([]byte) (int, netip.AddrPort, error)
}
func (c *Client) enoughRegions() int {
@@ -518,7 +518,7 @@ func nodeMight4(n *tailcfg.DERPNode) bool {
}
type packetReaderFromCloser interface {
ReadFrom([]byte) (int, net.Addr, error)
ReadFromUDPAddrPort([]byte) (int, netip.AddrPort, error)
io.Closer
}
@@ -538,7 +538,7 @@ func (c *Client) readPackets(ctx context.Context, pc packetReaderFromCloser) {
var buf [64 << 10]byte
for {
n, addr, err := pc.ReadFrom(buf[:])
n, addr, err := pc.ReadFromUDPAddrPort(buf[:])
if err != nil {
if ctx.Err() != nil {
return
@@ -546,16 +546,11 @@ func (c *Client) readPackets(ctx context.Context, pc packetReaderFromCloser) {
c.logf("ReadFrom: %v", err)
return
}
ua, ok := addr.(*net.UDPAddr)
if !ok {
c.logf("ReadFrom: unexpected addr %T", addr)
continue
}
pkt := buf[:n]
if !stun.Is(pkt) {
continue
}
if ap := netaddr.Unmap(ua.AddrPort()); ap.IsValid() {
if ap := netaddr.Unmap(addr); ap.IsValid() {
c.ReceiveSTUNPacket(pkt, ap)
}
}
+5 -10
View File
@@ -531,7 +531,7 @@ func (c *Client) createOrGetMapping(ctx context.Context) (external netip.AddrPor
res := make([]byte, 1500)
for {
n, srci, err := uc.ReadFrom(res)
n, src, err := uc.ReadFromUDPAddrPort(res)
if err != nil {
if ctx.Err() == context.Canceled {
return netip.AddrPort{}, err
@@ -542,8 +542,7 @@ func (c *Client) createOrGetMapping(ctx context.Context) (external netip.AddrPor
}
return netip.AddrPort{}, NoMappingError{ErrNoPortMappingServices}
}
srcu := srci.(*net.UDPAddr)
src := netaddr.Unmap(srcu.AddrPort())
src = netaddr.Unmap(src)
if !src.IsValid() {
continue
}
@@ -793,18 +792,14 @@ func (c *Client) Probe(ctx context.Context) (res ProbeResult, err error) {
// Nothing more to discover.
return res, nil
}
n, addr, err := uc.ReadFrom(buf)
n, src, err := uc.ReadFromUDPAddrPort(buf)
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
err = nil
}
return res, err
}
ip, ok := netip.AddrFromSlice(addr.(*net.UDPAddr).IP)
if !ok {
continue
}
ip = ip.Unmap()
ip := src.Addr().Unmap()
handleUPnPResponse := func() {
metricUPnPResponse.Add(1)
@@ -832,7 +827,7 @@ func (c *Client) Probe(ctx context.Context) (res ProbeResult, err error) {
c.mu.Unlock()
}
port := uint16(addr.(*net.UDPAddr).Port)
port := src.Port()
switch port {
case c.upnpPort():
if mem.Contains(mem.B(buf[:n]), mem.S(":InternetGatewayDevice:")) {
+10 -11
View File
@@ -6,14 +6,15 @@ package stuntest
import (
"context"
"errors"
"fmt"
"net"
"net/netip"
"strconv"
"strings"
"sync"
"testing"
"tailscale.com/net/netaddr"
"tailscale.com/net/stun"
"tailscale.com/tailcfg"
"tailscale.com/types/nettype"
@@ -44,28 +45,27 @@ func ServeWithPacketListener(t testing.TB, ln nettype.PacketListener) (addr *net
addr.IP = net.ParseIP("127.0.0.1")
}
doneCh := make(chan struct{})
go runSTUN(t, pc, &stats, doneCh)
go runSTUN(t, pc.(nettype.PacketConn), &stats, doneCh)
return addr, func() {
pc.Close()
<-doneCh
}
}
func runSTUN(t testing.TB, pc net.PacketConn, stats *stunStats, done chan<- struct{}) {
func runSTUN(t testing.TB, pc nettype.PacketConn, stats *stunStats, done chan<- struct{}) {
defer close(done)
var buf [64 << 10]byte
for {
n, addr, err := pc.ReadFrom(buf[:])
n, src, err := pc.ReadFromUDPAddrPort(buf[:])
if err != nil {
// TODO: when we switch to Go 1.16, replace this with errors.Is(err, net.ErrClosed)
if strings.Contains(err.Error(), "closed network connection") {
if errors.Is(err, net.ErrClosed) {
t.Logf("STUN server shutdown")
return
}
continue
}
ua := addr.(*net.UDPAddr)
src = netaddr.Unmap(src)
pkt := buf[:n]
if !stun.Is(pkt) {
continue
@@ -76,16 +76,15 @@ func runSTUN(t testing.TB, pc net.PacketConn, stats *stunStats, done chan<- stru
}
stats.mu.Lock()
if ua.IP.To4() != nil {
if src.Addr().Is4() {
stats.readIPv4++
} else {
stats.readIPv6++
}
stats.mu.Unlock()
nia, _ := netip.AddrFromSlice(ua.IP)
res := stun.Response(txid, netip.AddrPortFrom(nia, uint16(ua.Port)))
if _, err := pc.WriteTo(res, addr); err != nil {
res := stun.Response(txid, src)
if _, err := pc.WriteToUDPAddrPort(res, src); err != nil {
t.Logf("STUN server write failed: %v", err)
}
}