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

pull/308/head
Brad Fitzpatrick 5 years ago
parent 42e62a7eb2
commit 6fb30ff543

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

@ -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= 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 h1:96mOdDATYRqhYB+TZdenWBg4CzL2Ye5kPyBXQ8KAB+8=
gortc.io/stun v1.22.1/go.mod h1:XD5lpONVyjvV3BgOyJFNo0iv6R2oZB4L+weMqxts+zg= 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 h1:SPn+NLTiAG7w30IRK/DKp1BjvpWabYgxlLp/+kx5J8w=
rsc.io/goversion v1.2.0/go.mod h1:Eih9y/uIBS3ulggl7KNJ09xGSLcuNaLgmvvqa07sgfo= rsc.io/goversion v1.2.0/go.mod h1:Eih9y/uIBS3ulggl7KNJ09xGSLcuNaLgmvvqa07sgfo=

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

Loading…
Cancel
Save