mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-11 21:27:31 +00:00
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:

committed by
Brad Fitzpatrick

parent
29f7df9d8f
commit
10f1c90f4d
@@ -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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user