net/tsaddr: add func ContainsExitRoutes

Change-Id: I772441a406083e2fe0f9374b2b23d89aac18928f
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2022-03-04 08:00:53 -08:00 committed by Brad Fitzpatrick
parent 7ddf2e2fea
commit b493ef5b71
2 changed files with 27 additions and 12 deletions

View File

@ -18,6 +18,7 @@
"inet.af/netaddr"
"tailscale.com/atomicfile"
"tailscale.com/net/tsaddr"
"tailscale.com/tailcfg"
"tailscale.com/types/persist"
"tailscale.com/types/preftype"
@ -442,18 +443,7 @@ func (p *Prefs) AdvertisesExitNode() bool {
if p == nil {
return false
}
var v4, v6 bool
for _, r := range p.AdvertiseRoutes {
if r.Bits() != 0 {
continue
}
if r.IP().Is4() {
v4 = true
} else if r.IP().Is6() {
v6 = true
}
}
return v4 && v6
return tsaddr.ContainsExitRoutes(p.AdvertiseRoutes)
}
// SetAdvertiseExitNode mutates p (if non-nil) to add or remove the two

View File

@ -213,3 +213,28 @@ func PrefixIs4(p netaddr.IPPrefix) bool { return p.IP().Is4() }
// PrefixIs6 reports whether p is an IPv6 prefix.
func PrefixIs6(p netaddr.IPPrefix) bool { return p.IP().Is6() }
// ContainsExitRoutes reports whether rr contains both the IPv4 and
// IPv6 /0 route.
func ContainsExitRoutes(rr []netaddr.IPPrefix) bool {
var v4, v6 bool
for _, r := range rr {
if r == allIPv4 {
v4 = true
} else if r == allIPv6 {
v6 = true
}
}
return v4 && v6
}
var (
allIPv4 = netaddr.MustParseIPPrefix("0.0.0.0/0")
allIPv6 = netaddr.MustParseIPPrefix("::/0")
)
// AllIPv4 returns 0.0.0.0/0.
func AllIPv4() netaddr.IPPrefix { return allIPv4 }
// AllIPv6 returns ::/0.
func AllIPv6() netaddr.IPPrefix { return allIPv6 }