ipcalc

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ipcalc provides policy-agnostic subnet arithmetic. It knows nothing about tail-ipam's registry, CLI, or Tailscale-specific concepts.

Index

Constants

View Source
const MaxMaterializedChildren = 65536

Variables

View Source
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.

View Source
var ErrTooManyChildren = errors.New("ipcalc: split would produce too many children to materialize")

Functions

func Aggregate

func Aggregate(prefixes []netip.Prefix) []netip.Prefix

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

func NextFree(pool netip.Prefix, targetBits int, taken []netip.Prefix) (netip.Prefix, bool)

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

func Split(parent netip.Prefix, targetBits int) ([]netip.Prefix, *big.Int, error)

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

func Unmap4In6(prefix netip.Prefix) (netip.Prefix, bool)

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

func Info(prefix netip.Prefix) (BlockInfo, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL