net/tsaddr: add NewContainsIPFunc (move from wgengine)

I want to use this from netstack but it's not exported.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2021-05-05 12:58:40 -07:00
committed by Brad Fitzpatrick
parent 78d4c561b5
commit 7629cd6120
3 changed files with 78 additions and 24 deletions

View File

@@ -243,7 +243,7 @@ func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error)
router: conf.Router,
pingers: make(map[wgkey.Key]*pinger),
}
e.isLocalAddr.Store(genLocalAddrFunc(nil))
e.isLocalAddr.Store(tsaddr.NewContainsIPFunc(nil))
if conf.LinkMonitor != nil {
e.linkMon = conf.LinkMonitor
@@ -936,28 +936,6 @@ func (e *userspaceEngine) updateActivityMapsLocked(trackDisco []tailcfg.DiscoKey
e.tundev.SetDestIPActivityFuncs(e.destIPActivityFuncs)
}
// genLocalAddrFunc returns a func that reports whether an IP is in addrs.
// addrs is assumed to be all /32 or /128 entries.
func genLocalAddrFunc(addrs []netaddr.IPPrefix) func(netaddr.IP) bool {
// Specialize the three common cases: no address, just IPv4
// (or just IPv6), and both IPv4 and IPv6.
if len(addrs) == 0 {
return func(netaddr.IP) bool { return false }
}
if len(addrs) == 1 {
return func(t netaddr.IP) bool { return t == addrs[0].IP }
}
if len(addrs) == 2 {
return func(t netaddr.IP) bool { return t == addrs[0].IP || t == addrs[1].IP }
}
// Otherwise, the general implementation: a map lookup.
m := map[netaddr.IP]bool{}
for _, a := range addrs {
m[a.IP] = true
}
return func(t netaddr.IP) bool { return m[t] }
}
func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config, dnsCfg *dns.Config) error {
if routerCfg == nil {
panic("routerCfg must not be nil")
@@ -966,7 +944,7 @@ func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config,
panic("dnsCfg must not be nil")
}
e.isLocalAddr.Store(genLocalAddrFunc(routerCfg.LocalAddrs))
e.isLocalAddr.Store(tsaddr.NewContainsIPFunc(routerCfg.LocalAddrs))
e.wgLock.Lock()
defer e.wgLock.Unlock()