Documentation
¶
Overview ¶
Package ip is a library for performing manipulations on IPv4 and IPv6 addresses and CIDR prefixes.
Index ¶
- func AddrFromIP(ip net.IP) netip.Addr
- func CoalescePrefixes(prefixes []netip.Prefix) []netip.Prefix
- func CompareUnmap(addr1, addr2 netip.Addr) int
- func CountIPsInCIDR(ipnet *net.IPNet) *big.Int
- func IPToPrefix(ip net.IP) *net.IPNet
- func IsPublicAddr(ip net.IP) bool
- func KeepUniqueAddrs(addrs []netip.Addr) []netip.Addr
- func LaminarCIDRsOverlap(c1, c2 netip.Prefix) bool
- func ListContainsIP(ipList []net.IP, ip net.IP) bool
- func ParsePrefixes(cidrs []string) ([]netip.Prefix, error)
- func ParseScope(scope string) (int, error)
- func PartitionCIDR(targetCIDR net.IPNet, excludeCIDR net.IPNet) ([]*net.IPNet, []*net.IPNet, []*net.IPNet)
- func PrefixCeil(numIPs int, multiple int) int
- func PrefixToIps(prefixCidr string, maxIPs int) ([]string, error)
- func PrefixesContains(prefixes []netip.Prefix, addr netip.Addr) bool
- func SortAddrList(ipList []netip.Addr)
- type Addr
- type Prefix
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddrFromIP ¶
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 ¶
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
CompareUnMap unmap 2 addresses before comparing
func CountIPsInCIDR ¶
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 ¶
IPToPrefix returns the corresponding IPNet for the given IP.
func IsPublicAddr ¶
IsPublicAddr returns whether a given global IP is from a public range.
func KeepUniqueAddrs ¶
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
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 ¶
ListContainsIP returns whether a list of IPs contains a given IP.
func ParsePrefixes ¶
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 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 ¶
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 ¶
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
PrefixesContains checks that any prefix in prefix *fully* contains addr.
func SortAddrList ¶ added in v1.15.0
Types ¶
type Addr ¶ added in v1.20.0
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 (*Addr) DeepCopyInto ¶ added in v1.20.0
DeepCopyInto is a manual copy: netip.Addr's hidden zone pointer references interned, immutable data, so a value copy is safe.
type Prefix ¶ added in v1.20.0
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
PrefixFrom wraps a netip.Prefix.
func (*Prefix) DeepCopyInto ¶ added in v1.20.0
func (Prefix) IsZero ¶ added in v1.20.0
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 `""`.