ipmath

package
v1.0.1-0...-6e06715 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package ipmath provides IP/CIDR parsing, safety validation, deduplication, CIDR merging and allowlist subtraction. It is pure (no I/O) and is the only place address math happens.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(prefixes []netip.Prefix, addr netip.Addr) bool

Contains reports whether any prefix in prefixes covers addr. Used to warn when an allowlisted IP shows up in feeds.

func ParsePrefix

func ParsePrefix(s string) (netip.Prefix, error)

ParsePrefix parses a bare IP ("1.2.3.4", "2001:db8::1") or a CIDR ("1.2.3.0/24") into a normalized (masked) prefix.

Types

type Policy

type Policy struct {
	// AllowPrivate accepts RFC1918/ULA and other special-purpose ranges.
	AllowPrivate bool
	// MinV4Bits rejects IPv4 prefixes broader than this (default 8,
	// i.e. /7 and shorter are refused). 0 uses the default.
	MinV4Bits int
	// MinV6Bits is the IPv6 equivalent (default 16).
	MinV6Bits int
}

Policy controls which prefixes are accepted into a blocklist.

func (Policy) Check

func (p Policy) Check(pfx netip.Prefix) error

Check returns a non-nil error when the prefix must not enter a blocklist under this policy. The error message says why.

type Result

type Result struct {
	V4 []netip.Prefix
	V6 []netip.Prefix
}

Result is the merged, family-split outcome of a build.

func Merge

func Merge(block, allow []netip.Prefix) (Result, error)

Merge deduplicates and merges overlapping/adjacent prefixes, then subtracts every allow prefix, returning minimal per-family prefix lists. Allowlist subtraction happens after aggregation so an allow entry always wins regardless of which feed(s) contributed overlapping blocks.

func (Result) Total

func (r Result) Total() int

Total is the number of prefixes across both families.

Jump to

Keyboard shortcuts

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