Documentation
¶
Overview ¶
Package ipcalc provides policy-agnostic subnet arithmetic. It knows nothing about tail-ipam's registry, CLI, or Tailscale-specific concepts.
Index ¶
- Constants
- Variables
- func Aggregate(prefixes []netip.Prefix) []netip.Prefix
- func NextFree(pool netip.Prefix, targetBits int, taken []netip.Prefix) (netip.Prefix, bool)
- func Split(parent netip.Prefix, targetBits int) ([]netip.Prefix, *big.Int, error)
- func Unmap4In6(prefix netip.Prefix) (netip.Prefix, bool)
- type BlockInfo
Constants ¶
const MaxMaterializedChildren = 65536
Variables ¶
var ErrIPv6NotSupported = errors.New("ipcalc: IPv6 prefixes are not supported")
ErrIPv6NotSupported is retained for source compatibility with callers that handled the IPv4-only behavior. Info no longer returns it because it now reports IPv6 block facts.
Deprecated: IPv6 prefixes are supported by Info.
var ErrTooManyChildren = errors.New("ipcalc: split would produce too many children to materialize")
Functions ¶
func Aggregate ¶
Aggregate returns the minimal CIDR cover of prefixes. It masks prefixes, drops prefixes already covered by a wider prefix, and repeatedly replaces adjacent sibling prefixes with their parent. The result is ordered by network address. Both address families may be supplied together: a prefix never merges with or is covered by a prefix of the other family, and every IPv4 result precedes every IPv6 result. Callers must provide valid prefixes.
func NextFree ¶
NextFree returns the lowest-address targetBits child of pool that does not overlap a valid same-family prefix in taken. It returns false when pool or targetBits is invalid, or when every target-sized child overlaps an exclusion. Prefixes outside pool and from the other address family are ignored.
NextFree searches the gaps between sorted, merged exclusion intervals. It does not materialize pool's possible children, so the search remains useful for large IPv4 and IPv6 pools.
func Split ¶
Split divides parent into equal-sized child prefixes of length targetBits. If targetBits is shorter than parent's own prefix length, or longer than the address family's bit length, Split returns an error. Split works for both IPv4 and IPv6 — family-specific restrictions belong to callers, not this function.
If the number of children would exceed MaxMaterializedChildren, Split returns (nil, count, ErrTooManyChildren) — count is still the exact number (as big.Int, since it can exceed uint64 range for IPv6 callers), so a caller can report it without enumerating.
func Unmap4In6 ¶
Unmap4In6 rewrites an IPv4-mapped IPv6 prefix (::ffff:a.b.c.d/n) to its equivalent plain IPv4 prefix. ok is false if prefix is not a 4-in-6 address, or if its bit length is too short to represent a valid IPv4 prefix once the 96-bit mapping prefix is removed. A caller that supports IPv6 can preserve the original prefix when ok is false.
Types ¶
type BlockInfo ¶
type BlockInfo struct {
// Network is the block's network address (the masked prefix address).
Network netip.Addr
// Broadcast is the IPv4 block's broadcast address. It is the zero Addr
// (IsValid() == false) for every IPv6 block and for IPv4 /31 and /32,
// which have none: a /31 is an RFC 3021 point-to-point link and a /32 is
// a host route.
Broadcast netip.Addr
// FirstUsable and LastUsable bound the usable host range. For /30 and
// shorter they exclude the network and broadcast addresses; for /31
// both addresses are usable (RFC 3021) and for /32 the single address
// is the whole range.
FirstUsable netip.Addr
LastUsable netip.Addr
// UsableHosts is the number of usable host addresses. It is a
// *big.Int, like [Split]'s count, so an IPv6 extension of this type
// can exceed uint64 range without forking the result shape.
UsableHosts *big.Int
}
BlockInfo reports classic ipcalc facts for an IPv4 or IPv6 block.
func Info ¶
Info reports the network address, broadcast address where applicable, usable host range, and usable-host count for an IPv4 or IPv6 prefix. Host bits in prefix are masked off first, so an address inside a block yields the facts for its containing block. IPv6 has no broadcast address and all addresses in its prefix are usable. Callers accepting 4-in-6 input can normalize it with Unmap4In6 before calling.