Help us shape the future of Go! Take the 2026 Go Developer Survey

ip

package
v1.21.0-pre.4 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 11 Imported by: 55

Documentation

Overview

Package ip is a library for performing manipulations on IPv4 and IPv6 addresses and CIDR prefixes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddrFromIP

func AddrFromIP(ip net.IP) netip.Addr

AddrFromIP converts a net.IP to netip.Addr, returning the zero value for nil. This is useful when the net.IP may be nil (e.g., from optional configuration).

func CoalescePrefixes

func CoalescePrefixes(prefixes []netip.Prefix) []netip.Prefix

CoalescePrefixes reduces the prefixes to the minimal equivalent set: prefixes contained in another one are dropped, and prefixes that together cover a contiguous range are merged into the shortest prefixes covering that range. The result is sorted by address family, IPv4 prefixes first.

Ranges of different address families are never contiguous, so a single set coalesces both families independently.

Invalid prefixes are dropped: netipx.IPSetBuilder only reports errors for the invalid inputs it was asked to add, so callers that need to reject them must validate before calling.

func CompareUnmap added in v1.19.0

func CompareUnmap(addr1, addr2 netip.Addr) int

CompareUnMap unmap 2 addresses before comparing

func CountIPsInCIDR

func CountIPsInCIDR(ipnet *net.IPNet) *big.Int

CountIPsInCIDR takes a RFC4632/RFC4291-formatted IPv4/IPv6 CIDR and determines how many IP addresses reside within that CIDR. The first and the last (base and broadcast) IPs are excluded.

Returns 0 if the input CIDR cannot be parsed.

func IPToPrefix

func IPToPrefix(ip net.IP) *net.IPNet

IPToPrefix returns the corresponding IPNet for the given IP.

func IsPublicAddr

func IsPublicAddr(ip net.IP) bool

IsPublicAddr returns whether a given global IP is from a public range.

func KeepUniqueAddrs

func KeepUniqueAddrs(addrs []netip.Addr) []netip.Addr

KeepUniqueAddrs transforms the provided multiset of IP addresses into a single set, lexicographically sorted via comparison of the addresses using netip.Addr.Compare (i.e. IPv4 addresses show up before IPv6). The slice is manipulated in-place destructively; it does not create a new slice.

func LaminarCIDRsOverlap added in v1.20.0

func LaminarCIDRsOverlap(c1, c2 netip.Prefix) bool

LaminarCIDRsOverlap reports whether c1 and c2 overlap, i.e. one is contained within the other. CIDRs are laminar: two prefixes are either nested or disjoint, never partially overlapping, so checking containment in either direction is equivalent to checking that the two ranges intersect.

func ListContainsIP

func ListContainsIP(ipList []net.IP, ip net.IP) bool

ListContainsIP returns whether a list of IPs contains a given IP.

func ParsePrefixes

func ParsePrefixes(cidrs []string) ([]netip.Prefix, error)

ParsePrefixes parses all CIDRs referred to by the specified slice and returns them as regular golang netip.Prefix objects. A CIDR may also be given in host format, in which case it is parsed as a single-address prefix.

func ParseScope

func ParseScope(scope string) (int, error)

func PartitionCIDR

func PartitionCIDR(targetCIDR net.IPNet, excludeCIDR net.IPNet) ([]*net.IPNet, []*net.IPNet, []*net.IPNet)

PartitionCIDR returns a list of IP Networks partitioned upon excludeCIDR. The first list contains the networks to the left of the excludeCIDR in the partition, the second is a list containing the excludeCIDR itself if it is contained within the targetCIDR (nil otherwise), and the third is a list containing the networks to the right of the excludeCIDR in the partition.

func PrefixCeil

func PrefixCeil(numIPs int, multiple int) int

PrefixCeil converts the given number of IPs to the minimum number of prefixes needed to host those IPs. multiple indicates the number of IPs in a single prefix.

func PrefixToIps

func PrefixToIps(prefixCidr string, maxIPs int) ([]string, error)

PrefixToIps converts the given prefix to an array containing IPs in the provided prefix/CIDR block. When maxIPs is set to 0, the returned array will contain all IPs in the given prefix. Otherwise, the returned array of IPs will be limited to the value of maxIPs starting at the first IP in the provided CIDR. For example, when providing 192.168.1.0/28 as a CIDR with 4 maxIPs, 192.168.1.0, 192.168.1.1, 192.168.1.2, 192.168.1.3 will be returned.

func PrefixesContains added in v1.18.0

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

PrefixesContains checks that any prefix in prefix *fully* contains addr.

func SortAddrList added in v1.15.0

func SortAddrList(ipList []netip.Addr)

Types

type Addr added in v1.20.0

type Addr struct {
	netip.Addr
}

Addr wraps netip.Addr so it can be used as a field in Kubernetes API types. Embedding (rather than a defined type) inherits MarshalText / UnmarshalText. encoding/json falls back to those when no MarshalJSON / UnmarshalJSON is defined, preserving the textual wire format. The hand-written DeepCopyInto / DeepCopy / DeepEqual methods are required because deepcopy-gen and deepequal-gen cannot synthesize them for an external type with unexported pointer fields.

Unlike Prefix, Addr carries no Format marker: the API server's CRD format registry only has `ipv4` and `ipv6` but no combined `ip` format, so we can't have format-based validation for a field that can be of either IP family.

+k8s:deepcopy-gen=false +deepequal-gen=false +kubebuilder:validation:Type=string

func AddrFrom added in v1.20.0

func AddrFrom(a netip.Addr) Addr

AddrFrom wraps a netip.Addr.

func (*Addr) DeepCopy added in v1.20.0

func (in *Addr) DeepCopy() *Addr

func (*Addr) DeepCopyInto added in v1.20.0

func (in *Addr) DeepCopyInto(out *Addr)

DeepCopyInto is a manual copy: netip.Addr's hidden zone pointer references interned, immutable data, so a value copy is safe.

func (*Addr) DeepEqual added in v1.20.0

func (in *Addr) DeepEqual(other *Addr) bool

func (Addr) Unwrap

func (a Addr) Unwrap() netip.Addr

Unwrap returns the wrapped netip.Addr.

type Prefix added in v1.20.0

type Prefix struct {
	netip.Prefix
}

Prefix wraps netip.Prefix, same rationale as Addr.

+k8s:deepcopy-gen=false +deepequal-gen=false +kubebuilder:validation:Type=string +kubebuilder:validation:Format=cidr

func PrefixFrom added in v1.20.0

func PrefixFrom(p netip.Prefix) Prefix

PrefixFrom wraps a netip.Prefix.

func (*Prefix) DeepCopy added in v1.20.0

func (in *Prefix) DeepCopy() *Prefix

func (*Prefix) DeepCopyInto added in v1.20.0

func (in *Prefix) DeepCopyInto(out *Prefix)

func (*Prefix) DeepEqual added in v1.20.0

func (in *Prefix) DeepEqual(other *Prefix) bool

func (Prefix) IsZero added in v1.20.0

func (p Prefix) IsZero() bool

IsZero reports whether p is unset or invalid, for encoding/json's `omitzero` tag option. A Prefix built from a valid Addr with an out-of-range bit count (e.g. PrefixFrom(addr, -1)) is non-zero by reflect's definition but invalid by IsValid(), and would otherwise be emitted as `""`.

func (Prefix) Unwrap

func (p Prefix) Unwrap() netip.Prefix

Unwrap returns the wrapped netip.Prefix.

Jump to

Keyboard shortcuts

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