Update
This commit is contained in:
1 parent
3530afb854
commit
6903fcff27
2146 files changed
+112256
-190703
No files matched your search
+25
-1
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) Tailscale Inc & AUTHORS
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
// Package tsaddr handles Tailscale-specific IPs and ranges.
|
||||
@@ -7,6 +7,7 @@ package tsaddr
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"iter"
|
||||
"net/netip"
|
||||
"slices"
|
||||
"sync"
|
||||
@@ -69,6 +70,7 @@ const (
|
||||
// IsTailscaleIP reports whether IP is an IP address in a range that
|
||||
// Tailscale assigns from.
|
||||
func IsTailscaleIP(ip netip.Addr) bool {
|
||||
ip = ip.Unmap()
|
||||
if ip.Is4() {
|
||||
return IsTailscaleIPv4(ip)
|
||||
}
|
||||
@@ -77,6 +79,7 @@ func IsTailscaleIP(ip netip.Addr) bool {
|
||||
|
||||
// IsTailscaleIPv4 reports whether an IPv4 IP is an IP address that
|
||||
// Tailscale assigns from.
|
||||
// It will always return false if ip is an "IPv4-mapped IPv6 address".
|
||||
func IsTailscaleIPv4(ip netip.Addr) bool {
|
||||
return CGNATRange().Contains(ip) && !ChromeOSVMRange().Contains(ip)
|
||||
}
|
||||
@@ -317,3 +320,24 @@ func MapVia(siteID uint32, v4 netip.Prefix) (via netip.Prefix, err error) {
|
||||
copy(a[12:], ip4a[:])
|
||||
return netip.PrefixFrom(netip.AddrFrom16(a), v4.Bits()+64+32), nil
|
||||
}
|
||||
|
||||
// FirstTailscaleAddrs returns the first Tailscale IPv4 address and
|
||||
// the first Tailscale IPv6 address among the addresses of addrs'
|
||||
// prefixes, if any. The addrs sequence is typically a node's own
|
||||
// address list, either a slice (via [slices.All]) or a view (via
|
||||
// [views.Slice.All]).
|
||||
func FirstTailscaleAddrs(addrs iter.Seq2[int, netip.Prefix]) (a4, a6 netip.Addr) {
|
||||
for _, pfx := range addrs {
|
||||
a := pfx.Addr()
|
||||
switch {
|
||||
case a.Is4() && !a4.IsValid() && IsTailscaleIP(a):
|
||||
a4 = a
|
||||
case a.Is6() && !a6.IsValid() && IsTailscaleIP(a):
|
||||
a6 = a
|
||||
}
|
||||
if a4.IsValid() && a6.IsValid() {
|
||||
break
|
||||
}
|
||||
}
|
||||
return a4, a6
|
||||
}
|
||||
Reference in new issue
Block a user