ipset

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ipset answers one question: is this address inside any of these CIDRs?

It replaces a prefix-trie library that converted the address into a freshly allocated slice on every lookup. That allocation sat on the hottest path a resolver has — the access list runs before the cache, so every query paid it.

A prefix is a contiguous range of addresses, so a set of prefixes is a set of ranges, and "is this address in one of them" is the classic stabbing query: sort the ranges by where they start, remember the furthest end seen so far, and one binary search answers it. Overlaps and nesting need no special handling, lookups touch a flat slice, and nothing allocates.

A Set is built once at startup and read concurrently afterwards.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cidrs []string) (*Set, []BadEntry)

New compiles cidrs into a Set, returning the entries it could not parse. Host bits are allowed and masked away, as net.ParseCIDR has always accepted them.

Types

type BadEntry

type BadEntry struct {
	CIDR string
	Err  error
}

BadEntry is a CIDR that would not parse, reported rather than fatal: a typo in one entry should not knock out the rest of a config.

func (BadEntry) Error

func (b BadEntry) Error() string

type Set

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

Set is a compiled list of prefixes. The zero value is an empty set, which contains nothing.

func (*Set) Contains

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

Contains reports whether addr falls inside any prefix in the set.

An IPv4-mapped IPv6 address is answered as the IPv4 address it carries: that is what a client behind a dual-stack socket looks like, and an operator who wrote an IPv4 CIDR means it to match them.

func (*Set) ContainsIP

func (s *Set) ContainsIP(ip net.IP) bool

ContainsIP is Contains for callers holding a net.IP. The conversion is a value copy — nothing escapes, nothing allocates.

func (*Set) Len

func (s *Set) Len() int

Len returns how many prefixes the set holds.

Jump to

Keyboard shortcuts

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