net/tsaddr: add func ContainsExitRoutes

Change-Id: I772441a406083e2fe0f9374b2b23d89aac18928f
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/4068/head
Brad Fitzpatrick 3 years ago committed by Brad Fitzpatrick
parent 7ddf2e2fea
commit b493ef5b71

@ -18,6 +18,7 @@ import (
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/atomicfile" "tailscale.com/atomicfile"
"tailscale.com/net/tsaddr"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/types/persist" "tailscale.com/types/persist"
"tailscale.com/types/preftype" "tailscale.com/types/preftype"
@ -442,18 +443,7 @@ func (p *Prefs) AdvertisesExitNode() bool {
if p == nil { if p == nil {
return false return false
} }
var v4, v6 bool return tsaddr.ContainsExitRoutes(p.AdvertiseRoutes)
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
} }
// SetAdvertiseExitNode mutates p (if non-nil) to add or remove the two // SetAdvertiseExitNode mutates p (if non-nil) to add or remove the two

@ -213,3 +213,28 @@ func PrefixIs4(p netaddr.IPPrefix) bool { return p.IP().Is4() }
// PrefixIs6 reports whether p is an IPv6 prefix. // PrefixIs6 reports whether p is an IPv6 prefix.
func PrefixIs6(p netaddr.IPPrefix) bool { return p.IP().Is6() } 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 }

Loading…
Cancel
Save