wgengine/magicsock: start using inet.af/netaddr a bit

This commit is contained in:
Brad Fitzpatrick 2020-04-17 13:51:52 -07:00
parent 42e62a7eb2
commit 6fb30ff543
3 changed files with 19 additions and 18 deletions

1
go.mod
View File

@ -28,5 +28,6 @@ require (
golang.org/x/sys v0.0.0-20200317113312-5766fd39f98d
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
gortc.io/stun v1.22.1
inet.af/netaddr v0.0.0-20200417204647-17eccff2620c
rsc.io/goversion v1.2.0
)

5
go.sum
View File

@ -146,5 +146,10 @@ gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gortc.io/stun v1.22.1 h1:96mOdDATYRqhYB+TZdenWBg4CzL2Ye5kPyBXQ8KAB+8=
gortc.io/stun v1.22.1/go.mod h1:XD5lpONVyjvV3BgOyJFNo0iv6R2oZB4L+weMqxts+zg=
inet.af v0.0.0-20181218191229-53da77bc832c h1:U3RoiyEF5b3Y1SVL6NNvpkgqUz2qS3a0OJh9kpSCN04=
inet.af/netaddr v0.0.0-20200417144406-01f6d1b213c8 h1:flBh+hqli1lALDWbRAkX2Cs7QSHsJcg52YQAqWpzXQc=
inet.af/netaddr v0.0.0-20200417144406-01f6d1b213c8/go.mod h1:qqYzz/2whtrbWJvt+DNWQyvekNN4ePQZcg2xc2/Yjww=
inet.af/netaddr v0.0.0-20200417204647-17eccff2620c h1:MzkYVBmOE/xWhdLv/R4ycVJ2On0j00w5gtboXBIqnE8=
inet.af/netaddr v0.0.0-20200417204647-17eccff2620c/go.mod h1:qqYzz/2whtrbWJvt+DNWQyvekNN4ePQZcg2xc2/Yjww=
rsc.io/goversion v1.2.0 h1:SPn+NLTiAG7w30IRK/DKp1BjvpWabYgxlLp/+kx5J8w=
rsc.io/goversion v1.2.0/go.mod h1:Eih9y/uIBS3ulggl7KNJ09xGSLcuNaLgmvvqa07sgfo=

View File

@ -30,6 +30,7 @@
"github.com/tailscale/wireguard-go/device"
"github.com/tailscale/wireguard-go/wgcfg"
"golang.org/x/time/rate"
"inet.af/netaddr"
"tailscale.com/derp"
"tailscale.com/derp/derphttp"
"tailscale.com/derp/derpmap"
@ -92,7 +93,7 @@ type Conn struct {
// 10.0.0.1:1 -> [10.0.0.1:1, 10.0.0.2:2]
// 10.0.0.2:2 -> [10.0.0.1:1, 10.0.0.2:2]
// 10.0.0.3:3 -> [10.0.0.3:3]
addrsByUDP map[udpAddr]*AddrSet // TODO: clean up this map sometime?
addrsByUDP map[netaddr.IPPort]*AddrSet // TODO: clean up this map sometime?
// addsByKey maps from public keys (as seen by incoming DERP
// packets) to its AddrSet (the same values as in addrsByUDP).
@ -175,13 +176,6 @@ type activeDerp struct {
createTime time.Time
}
// udpAddr is the key in the addrsByUDP map.
// It maps an ip:port onto an *AddrSet.
type udpAddr struct {
ip wgcfg.IP
port uint16
}
// DefaultPort is the default port to listen on.
// The current default (zero) means to auto-select a random free port.
const DefaultPort = 0
@ -231,7 +225,7 @@ func Listen(opts Options) (*Conn, error) {
logf: opts.logf(),
epFunc: opts.endpointsFunc(),
sendLogLimit: rate.NewLimiter(rate.Every(1*time.Minute), 1),
addrsByUDP: make(map[udpAddr]*AddrSet),
addrsByUDP: make(map[netaddr.IPPort]*AddrSet),
addrsByKey: make(map[key.Public]*AddrSet),
wantDerp: true,
derpRecvCh: make(chan derpReadResult),
@ -1060,14 +1054,16 @@ func (c *Conn) findEndpoint(addr *net.UDPAddr) conn.Endpoint {
}
func (c *Conn) findAddrSet(addr *net.UDPAddr) *AddrSet {
var epAddr udpAddr
copy(epAddr.ip.Addr[:], addr.IP.To16())
epAddr.port = uint16(addr.Port)
ip, ok := netaddr.FromStdIP(addr.IP)
if !ok {
return nil
}
ipp := netaddr.IPPort{ip, uint16(addr.Port)}
c.mu.Lock()
defer c.mu.Unlock()
return c.addrsByUDP[epAddr]
return c.addrsByUDP[ipp]
}
type udpReadResult struct {
@ -1766,11 +1762,10 @@ func (c *Conn) CreateEndpoint(key [32]byte, addrs string) (conn.Endpoint, error)
if addr.IP.Equal(derpMagicIP) {
continue
}
var epAddr udpAddr
copy(epAddr.ip.Addr[:], addr.IP.To16())
epAddr.port = uint16(addr.Port)
c.addrsByUDP[epAddr] = a
if ip, ok := netaddr.FromStdIP(addr.IP); ok {
ipp := netaddr.IPPort{ip, uint16(addr.Port)}
c.addrsByUDP[ipp] = a
}
}
c.addrsByKey[key] = a
c.mu.Unlock()