ipnet

package
v1.16.2 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package ipnet contains utility functions for IPv4 and IPv6 families.

Index

Constants

This section is empty.

Variables

View Source
var ErrRawEntryParse = errors.New("failed to parse as an IP address or an IP address in CIDR notation")

ErrRawEntryParse indicates that a string could not be parsed as an IP address or an IP address in CIDR notation.

Functions

func All added in v1.14.1

func All(yield func(Family) bool)

All enumerates IP4 and then IP6.

func Bindings added in v1.14.1

func Bindings[V any](m map[Family]V) iter.Seq2[Family, V]

Bindings enumerates the key IP4 and then IP6 for a map.

func DescribeBadAddress added in v1.16.0

func DescribeBadAddress(ip netip.Addr) (string, bool)

DescribeBadAddress reports whether the address is unsuitable as a DNS/WAF target. If unsuitable, it returns a predicate phrase suitable for "(subject) %s" (e.g., "is a loopback address") and true. The caller is responsible for formatting the full message with context.

func Emit4in6Hint added in v1.16.0

func Emit4in6Hint(ppfmt pp.PP, wants4in6Hint bool)

Emit4in6Hint emits the standard IPv4-mapped IPv6 hint message when wants4in6Hint is true. Safe to call unconditionally; it is a no-op otherwise.

func IsCloudflareIP added in v1.16.0

func IsCloudflareIP(ip netip.Addr) bool

IsCloudflareIP reports whether ip falls inside any of Cloudflare's published IP ranges. A detected IP inside these ranges is a Cloudflare egress IP, not a publishable client/public IP for DDNS.

func ParseAddrOrPrefix added in v1.16.0

func ParseAddrOrPrefix(ppfmt pp.PP, s string) (netip.Prefix, bool)

ParseAddrOrPrefix parses a network prefix or a bare IP address. This is used for parsing Cloudflare WAF list items, not raw detection data.

func ValidateAndNormalizeIP added in v1.16.0

func ValidateAndNormalizeIP(ipFamily Family, ip netip.Addr) (
	normalized netip.Addr, unmapped bool, issue string, wants4in6Hint bool, ok bool,
)

ValidateAndNormalizeIP validates ip for ipFamily.

  • On success, it returns the canonical normalized address, whether normalization unmapped an IPv4-mapped IPv6 address (unmapped), and ok=true.
  • On failure, it returns an issue phrase suitable for "(subject) %s" (for example, "is a loopback address") and ok=false. When wants4in6Hint is true, callers should also show pp.MessageIP4MappedIP6Address.

The function does not emit messages; callers report errors in their own context.

Types

type CloudflareIPRejecter added in v1.16.0

type CloudflareIPRejecter struct{}

CloudflareIPRejecter rejects IPs that fall inside Cloudflare's published IP ranges. Such IPs are Cloudflare egress/proxy IPs, not publishable client IPs for DDNS.

func (CloudflareIPRejecter) RejectRawIP added in v1.16.0

func (CloudflareIPRejecter) RejectRawIP(ip netip.Addr) (bool, string)

RejectRawIP rejects IPs inside Cloudflare's published ranges.

type Family added in v1.16.0

type Family int

Family identifies an IP family.

const (
	// IP4 is IP version 4.
	IP4 Family = 4

	// IP6 is IP version 6.
	IP6 Family = 6

	// FamilyCount is the number of IP families.
	FamilyCount = 2
)

func (Family) Describe added in v1.16.0

func (t Family) Describe() string

Describe returns a human-readable description of the IP family.

func (Family) Int added in v1.16.0

func (t Family) Int() int

Int returns the IP version. It is either 4 or 6.

func (Family) Matches added in v1.16.0

func (t Family) Matches(ip netip.Addr) bool

Matches reports whether an IP belongs to this family.

func (Family) NormalizeDetectedIPs added in v1.16.0

func (t Family) NormalizeDetectedIPs(ppfmt pp.PP, ips []netip.Addr) ([]netip.Addr, bool)

NormalizeDetectedIPs normalizes a list of detected IPs.

Behavior: - fail-fast: return false on the first invalid IP - preserve emptiness: empty input returns empty output - canonicalize set semantics: output is sorted and deduplicated.

func (Family) NormalizeDetectedRawEntries added in v1.16.0

func (t Family) NormalizeDetectedRawEntries(ppfmt pp.PP, entries []RawEntry) ([]RawEntry, bool)

NormalizeDetectedRawEntries normalizes a list of detected raw-data IP addresses with prefix lengths while preserving host bits in the address portion.

Behavior:

  • fail-fast: return false on the first invalid entry
  • preserve emptiness: empty input returns empty output
  • canonicalize set semantics: output is sorted and deduplicated.

func (Family) RecordType added in v1.16.0

func (t Family) RecordType() string

RecordType prints out the DNS record type for the IP family. For IPv4, it is A; for IPv6, it is AAAA.

func (Family) UDPNetwork added in v1.16.0

func (t Family) UDPNetwork() string

UDPNetwork returns the net.Dial network name for this IP family.

type RawEntry added in v1.16.0

type RawEntry netip.Prefix

RawEntry carries one detected IP address together with its prefix length.

Unlike netip.Prefix, the full address is significant: host bits are preserved and used downstream (e.g., DNS derivation extracts the address; WAF derivation explicitly masks via RawEntry.Masked). The prefix length rides alongside the address but does not imply that host bits are irrelevant.

Construction: use RawEntryFrom or LiftValidatedIPsToRawEntries.

func LiftValidatedIPsToRawEntries added in v1.16.0

func LiftValidatedIPsToRawEntries(ips []netip.Addr, prefixLen int) []RawEntry

LiftValidatedIPsToRawEntries preserves the observed address bits and applies the given prefix length to each already-validated address.

func NormalizeRawEntryIP added in v1.16.0

func NormalizeRawEntryIP(family Family, entry RawEntry) (
	normalized RawEntry, problem string, wants4in6Hint bool, ok bool,
)

NormalizeRawEntryIP adjusts the prefix length for IPv4-mapped IPv6 addresses and validates the IP for the given family. No messages are emitted; callers use the returned problem description and wants4in6Hint for their own diagnostics.

On success problem is empty. On failure problem is a predicate phrase suitable for "(subject) %s" (e.g., "is not a valid IPv4 address").

The prefix-length adjustment follows RFC 6887 PCP FILTER semantics: when an IPv4 prefix is encoded in the ::ffff:0:0/96 mapped form, the encoded prefix length is the IPv4 prefix length plus the fixed 96-bit mapping prefix.

func ParseRawEntry added in v1.16.0

func ParseRawEntry(s string, defaultPrefixLen int) (RawEntry, error)

ParseRawEntry parses s as a CIDR prefix or a bare IP address. Bare IPs receive defaultPrefixLen. CIDR notation preserves the stated prefix length and the full address (host bits included).

Zoned addresses (e.g. "fe80::1%eth0") are rejected because netip.PrefixFrom silently strips zones, which would lose information.

func RawEntryFrom added in v1.16.0

func RawEntryFrom(addr netip.Addr, prefixLen int) RawEntry

RawEntryFrom constructs a RawEntry from an address and prefix length.

func (RawEntry) Addr added in v1.16.0

func (r RawEntry) Addr() netip.Addr

Addr returns the IP address, including any host bits.

func (RawEntry) Compare added in v1.16.0

func (r RawEntry) Compare(other RawEntry) int

Compare returns an integer comparing two raw entries. The result is suitable for use with slices.SortFunc.

func (RawEntry) Describe added in v1.16.0

func (r RawEntry) Describe(defaultPrefixLen int) string

Describe returns a human-readable representation of the raw entry. The CIDR suffix is omitted only when the entry is a single host (prefix length == address bit length) AND that full-host length is also the effective default. This keeps output familiar: users whose effective default is /32 see bare "1.2.3.4", while users whose effective default is /24 always see the explicit "/24" or "/32" suffix so the distinction is never ambiguous.

func (RawEntry) IsValid added in v1.16.0

func (r RawEntry) IsValid() bool

IsValid reports whether the entry was constructed from valid inputs.

func (RawEntry) Masked added in v1.16.0

func (r RawEntry) Masked() netip.Prefix

Masked returns the network prefix with host bits zeroed. This is the explicit derivation step from raw entry to network prefix.

func (RawEntry) Prefix added in v1.16.0

func (r RawEntry) Prefix() netip.Prefix

Prefix converts back to netip.Prefix for stdlib or external API interop.

func (RawEntry) PrefixLen added in v1.16.0

func (r RawEntry) PrefixLen() int

PrefixLen returns the prefix length.

func (RawEntry) String added in v1.16.0

func (r RawEntry) String() string

String returns the CIDR notation representation (e.g. "1.2.3.4/32").

type RawIPRejecter added in v1.16.0

type RawIPRejecter interface {
	RejectRawIP(ip netip.Addr) (bool, string)
}

RawIPRejecter validates a detected raw IP address (before normalization). If the IP should be rejected, RejectRawIP returns (false, reason). Otherwise it returns (true, "").

Jump to

Keyboard shortcuts

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