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

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