netaddr

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 4 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 netaddr turns source addresses into ban units and checks the prefixes a list may publish.

Index

Constants

This section is empty.

Variables

View Source
var DefaultLimits = Limits{IPv4MinPrefix: 24, IPv6MinPrefix: 48}

DefaultLimits allow nothing wider than an IPv4 /24 or an IPv6 /48.

Functions

func Coverage

func Coverage(p netip.Prefix) (ipv4Addresses, ipv6Networks uint64)

Coverage counts what a prefix covers: IPv4 addresses, or IPv6 /64 networks, where anything narrower than a /64 counts as one. An IPv6 /0, whose count does not fit, counts as math.MaxUint64.

func Kind

func Kind(p netip.Prefix, ipv6Prefix int) string

Kind names a prefix for metrics: ipv4 or ipv6 for a single source, range for anything wider than a unit.

func ParseAllow

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

ParseAllow parses an allowlist line: an address, or a prefix of any width.

func ParseEntry

func ParseEntry(s string, lim Limits) (netip.Prefix, error)

ParseEntry parses a prefix for a published list. It must be canonical (host bits zero), written as IPv4 when it is IPv4, and no wider than lim.

func Unit

func Unit(addr string, ipv6Prefix int) (netip.Prefix, error)

Unit returns the prefix a source is scored and banned as: an IPv4 address (IPv4-mapped IPv6 included) as a /32, and an IPv6 address as its network of length ipv6Prefix.

Types

type Limits

type Limits struct {
	IPv4MinPrefix int `json:"ipv4MinPrefix"`
	IPv6MinPrefix int `json:"ipv6MinPrefix"`
}

Limits are the widest prefixes a published list may carry.

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set is a list of prefixes. It stays small (an allowlist), so lookups scan.

func NewSet

func NewSet(ps ...netip.Prefix) *Set

NewSet returns a set holding ps.

func SpecialPurpose

func SpecialPurpose() *Set

SpecialPurpose returns a set of the special-purpose blocks.

func Union

func Union(sets ...*Set) *Set

Union returns a new set with the prefixes of every set.

func (*Set) Add

func (s *Set) Add(ps ...netip.Prefix)

Add adds prefixes to the set.

func (*Set) Covers

func (s *Set) Covers(p netip.Prefix) bool

Covers reports whether one prefix in the set holds all of p.

func (*Set) Len

func (s *Set) Len() int

Len returns the number of prefixes.

func (*Set) Overlaps

func (s *Set) Overlaps(p netip.Prefix) bool

Overlaps reports whether p shares an address with any prefix in the set, either way round: an allowed address inside a wider candidate counts.

Jump to

Keyboard shortcuts

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