wgengine/magicsock: document and enforce that peerInfo.ep is non-nil.

Signed-off-by: David Anderson <danderson@tailscale.com>
(cherry picked from commit 2aa5df7ac1d6cf35f91966644b78a81eb3b7d154)
This commit is contained in:
David Anderson 2021-10-18 10:22:56 -07:00 committed by Brad Fitzpatrick
parent ee02c95259
commit 5cf0619cb2

View File

@ -72,7 +72,7 @@ func useDerpRoute() bool {
// peerInfo is all the information magicsock tracks about a particular // peerInfo is all the information magicsock tracks about a particular
// peer. // peer.
type peerInfo struct { type peerInfo struct {
ep *endpoint // optional, if wireguard-go isn't currently talking to this peer. ep *endpoint // always non-nil.
// ipPorts is an inverted version of peerMap.byIPPort (below), so // ipPorts is an inverted version of peerMap.byIPPort (below), so
// that when we're deleting this node, we can rapidly find out the // that when we're deleting this node, we can rapidly find out the
// keys that need deleting from peerMap.byIPPort without having to // keys that need deleting from peerMap.byIPPort without having to
@ -80,8 +80,9 @@ type peerInfo struct {
ipPorts map[netaddr.IPPort]bool ipPorts map[netaddr.IPPort]bool
} }
func newPeerInfo() *peerInfo { func newPeerInfo(ep *endpoint) *peerInfo {
return &peerInfo{ return &peerInfo{
ep: ep,
ipPorts: map[netaddr.IPPort]bool{}, ipPorts: map[netaddr.IPPort]bool{},
} }
} }
@ -169,7 +170,7 @@ func (m *peerMap) forEachEndpointWithDiscoKey(dk tailcfg.DiscoKey, f func(ep *en
func (m *peerMap) upsertEndpoint(ep *endpoint) { func (m *peerMap) upsertEndpoint(ep *endpoint) {
pi := m.byNodeKey[ep.publicKey] pi := m.byNodeKey[ep.publicKey]
if pi == nil { if pi == nil {
pi = newPeerInfo() pi = newPeerInfo(ep)
m.byNodeKey[ep.publicKey] = pi m.byNodeKey[ep.publicKey] = pi
} }
old := pi.ep old := pi.ep