@ -45,19 +45,19 @@ type Conn struct {
epUpdateCtx context . Context // endpoint updater context
epUpdateCtx context . Context // endpoint updater context
epUpdateCancel func ( ) // the func to cancel epUpdateCtx
epUpdateCancel func ( ) // the func to cancel epUpdateCtx
// indexedAddrs is a map of every remote ip:port to a priority
// addrsByUDP is a map of every remote ip:port to a priority
// list of endpoint addresses for a peer.
// list of endpoint addresses for a peer.
// The priority list is provided by wgengine configuration.
// The priority list is provided by wgengine configuration.
//
//
// Given a wgcfg describing:
// Given a wgcfg describing:
// machineA: 10.0.0.1:1, 10.0.0.2:2
// machineA: 10.0.0.1:1, 10.0.0.2:2
// machineB: 10.0.0.3:3
// machineB: 10.0.0.3:3
// the indexedAddrs map contains:
// the addrsByUDP map contains:
// 10.0.0.1:1 -> [10.0.0.1:1, 10.0.0.2:2] , index:0
// 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] , index:1
// 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] , index:0
// 10.0.0.3:3 -> [10.0.0.3:3]
indexedAddrsMu sync . Mutex
addrsMu sync . Mutex
indexedAddrs map [ udpAddr ] indexed AddrSet
addrsByUDP map [ udpAddr ] * AddrSet
// stunReceiveFunc holds the current STUN packet processing func.
// stunReceiveFunc holds the current STUN packet processing func.
// Its Loaded value is always non-nil.
// Its Loaded value is always non-nil.
@ -73,21 +73,13 @@ type Conn struct {
derpWriteCh map [ int ] chan <- derpWriteRequest
derpWriteCh map [ int ] chan <- derpWriteRequest
}
}
// udpAddr is the key in the indexedAddrs map.
// udpAddr is the key in the addrsByUDP map.
// It maps an ip:port onto an indexedAddr .
// It maps an ip:port onto an *AddrSet .
type udpAddr struct {
type udpAddr struct {
ip wgcfg . IP
ip wgcfg . IP
port uint16
port uint16
}
}
// indexedAddrSet is an AddrSet (a priority list of ip:ports for a peer and the
// current favored ip:port for communicating with the peer) and an index
// number saying which element of the priority list is this map entry.
type indexedAddrSet struct {
addr * AddrSet
index int // index of map key in addr.Addrs
}
// 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
@ -152,7 +144,7 @@ func Listen(opts Options) (*Conn, error) {
epUpdateCancel : epUpdateCancel ,
epUpdateCancel : epUpdateCancel ,
epFunc : opts . endpointsFunc ( ) ,
epFunc : opts . endpointsFunc ( ) ,
logf : log . Printf ,
logf : log . Printf ,
indexedAddrs: make ( map [ udpAddr ] indexed AddrSet) ,
addrsByUDP: make ( map [ udpAddr ] * AddrSet) ,
derpRecvCh : make ( chan derpReadResult ) ,
derpRecvCh : make ( chan derpReadResult ) ,
udpRecvCh : make ( chan udpReadResult ) ,
udpRecvCh : make ( chan udpReadResult ) ,
}
}
@ -655,19 +647,15 @@ func (c *Conn) runDerpWriter(ctx context.Context, derpFakeAddr *net.UDPAddr, dc
}
}
}
}
func ( c * Conn ) find Indexed AddrSet( addr * net . UDPAddr ) ( addrSet * AddrSet , index int ) {
func ( c * Conn ) find AddrSet( addr * net . UDPAddr ) * AddrSet {
var epAddr udpAddr
var epAddr udpAddr
copy ( epAddr . ip . Addr [ : ] , addr . IP . To16 ( ) )
copy ( epAddr . ip . Addr [ : ] , addr . IP . To16 ( ) )
epAddr . port = uint16 ( addr . Port )
epAddr . port = uint16 ( addr . Port )
c . indexedA ddrsMu. Lock ( )
c . a ddrsMu. Lock ( )
defer c . indexedA ddrsMu. Unlock ( )
defer c . a ddrsMu. Unlock ( )
indAddr := c . indexedAddrs [ epAddr ]
return c . addrsByUDP [ epAddr ]
if indAddr . addr == nil {
return nil , 0
}
return indAddr . addr , indAddr . index
}
}
type udpReadResult struct {
type udpReadResult struct {
@ -741,7 +729,7 @@ func (c *Conn) ReceiveIPv4(b []byte) (n int, ep conn.Endpoint, addr *net.UDPAddr
n , addr = um . n , um . addr
n , addr = um . n , um . addr
}
}
addrSet , _ := c . fin dIndexe dAddrSet( addr )
addrSet := c . fin dAddrSet( addr )
if addrSet == nil {
if addrSet == nil {
// The peer that sent this packet has roamed beyond the
// The peer that sent this packet has roamed beyond the
// knowledge provided by the control server.
// knowledge provided by the control server.
@ -1043,17 +1031,14 @@ func (c *Conn) CreateEndpoint(key [32]byte, addrs string) (conn.Endpoint, error)
}
}
}
}
c . indexedA ddrsMu. Lock ( )
c . a ddrsMu. Lock ( )
for i , addr := range a . addrs {
for _ , addr := range a . addrs {
var epAddr udpAddr
var epAddr udpAddr
copy ( epAddr . ip . Addr [ : ] , addr . IP . To16 ( ) )
copy ( epAddr . ip . Addr [ : ] , addr . IP . To16 ( ) )
epAddr . port = uint16 ( addr . Port )
epAddr . port = uint16 ( addr . Port )
c . indexedAddrs [ epAddr ] = indexedAddrSet {
c . addrsByUDP [ epAddr ] = a
addr : a ,
index : i ,
}
}
}
c . indexedA ddrsMu. Unlock ( )
c . addrsMu . Unlock ( )
return a , nil
return a , nil
}
}