iputils

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package iputils is small, allocation-aware helpers on top of net/netip for the patterns every service eventually re-implements: CIDR allowlists / denylists, classification (loopback, private, link-local), efficient many-prefix containment via a compiled Set.

Built on netip.Addr / netip.Prefix throughout — never the legacy net.IP. Set is the only stateful type; everything else is stateless functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(prefix netip.Prefix, addr netip.Addr) bool

Contains reports whether prefix contains addr. Cross-family inputs (an IPv4 addr against an IPv6 prefix or vice versa) return false.

func ContainsAny

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

ContainsAny reports whether any prefix in prefixes contains addr. For repeated lookups over a large list prefer Set.

func IsLinkLocal

func IsLinkLocal(addr netip.Addr) bool

IsLinkLocal reports whether addr is in 169.254.0.0/16 (v4) or fe80::/10 (v6).

func IsLoopback

func IsLoopback(addr netip.Addr) bool

IsLoopback reports whether addr is in 127.0.0.0/8 (v4) or ::1 (v6).

func IsMulticast

func IsMulticast(addr netip.Addr) bool

IsMulticast reports whether addr is multicast.

func IsPrivate

func IsPrivate(addr netip.Addr) bool

IsPrivate reports whether addr is in any RFC1918 / RFC4193 range. netip itself ships IsPrivate() since 1.20; this wrapper exists for API symmetry and stable scope across Go versions.

func IsPublic

func IsPublic(addr netip.Addr) bool

IsPublic reports whether addr is routable on the public internet. (Not loopback, not link-local, not multicast, not unspecified, not private.)

func IsUnspecified

func IsUnspecified(addr netip.Addr) bool

IsUnspecified reports whether addr is 0.0.0.0 / ::.

func MustParseAddr

func MustParseAddr(s string) netip.Addr

MustParseAddr panics on error. For test fixtures and init-time constants only.

func MustParsePrefix

func MustParsePrefix(s string) netip.Prefix

MustParsePrefix panics on error. For test fixtures and init-time constants only.

func ParseAddr

func ParseAddr(s string) (netip.Addr, error)

ParseAddr is netip.ParseAddr with a clearer error wrap.

func ParsePrefix

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

ParsePrefix is netip.ParsePrefix with a clearer error wrap.

Types

type Set

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

Set is a compiled multi-prefix container. Lookups are O(P) over the prefix count but with no allocations on the hot path; suitable for allowlist / denylist sizes up to a few thousand.

func NewSet

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

NewSet builds a Set from the given prefixes. Order is not preserved.

func (*Set) Add

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

Add registers an additional prefix. Safe to call concurrently with Contains.

func (*Set) Contains

func (s *Set) Contains(addr netip.Addr) bool

Contains reports whether any prefix in the set contains addr.

func (*Set) Len

func (s *Set) Len() int

Len returns the total prefix count.

func (*Set) String

func (s *Set) String() string

String returns a comma-joined prefix list, longest-match first.

Jump to

Keyboard shortcuts

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