func AddrIPNet(addr netip.Addr) *net.IPNet
AddrIPNet returns the net.IPNet representation of an netip.Addr with a mask corresponding to the addresses's bit length. The returned value is always non-nil. Any zone identifier is dropped in the conversion.
func AddrNext(ip netip.Addr) netip.Addr
AddrNext returns the IP following ip. If there is none, it returns the IP zero value.
Deprecated: use netip.Addr.Next instead.
func AddrPrior(ip netip.Addr) netip.Addr
AddrPrior returns the IP before ip. If there is none, it returns the IP zero value.
Deprecated: use netip.Addr.Prev instead.
func FromStdAddr(stdIP net.IP, port int, zone string) (_ netip.AddrPort, ok bool)
FromStdAddr maps the components of a standard library TCPAddr or UDPAddr into an IPPort.
func FromStdIP(std net.IP) (ip netip.Addr, ok bool)
FromStdIP returns an IP from the standard library's IP type.
If std is invalid, ok is false.
FromStdIP implicitly unmaps IPv6-mapped IPv4 addresses. That is, if len(std) == 16 and contains an IPv4 address, only the IPv4 part is returned, without the IPv6 wrapper. This is the common form returned by the standard library's ParseIP: https://play.golang.org/p/qdjylUkKWxl. To convert a standard library IP without the implicit unmapping, use netip.AddrFromSlice.
func FromStdIPNet(std *net.IPNet) (prefix netip.Prefix, ok bool)
FromStdIPNet returns an netip.Prefix from the standard library's IPNet type. If std is invalid, ok is false.
func FromStdIPRaw(std net.IP) (ip netip.Addr, ok bool)
FromStdIPRaw returns an IP from the standard library's IP type. If std is invalid, ok is false. Unlike FromStdIP, FromStdIPRaw does not do an implicit Unmap if len(std) == 16 and contains an IPv6-mapped IPv4 address.
Deprecated: use netip.AddrFromSlice instead.
func MustFromStdIP(std net.IP) netip.Addr
MustFromStdIP is like FromStdIP, but it panics if std is invalid.
func PrefixIPNet(p netip.Prefix) *net.IPNet
PrefixIPNet returns the net.IPNet representation of an netip.Prefix. The returned value is always non-nil. Any zone identifier is dropped in the conversion.
func PrefixLastIP(p netip.Prefix) netip.Addr
PrefixLastIP returns the last IP in the prefix.
IPRange represents an inclusive range of IP addresses from the same address family.
The From and To IPs are inclusive bounds, with both included in the range.
To be valid, the From and To values must be non-zero, have matching address families (IPv4 vs IPv6), and From must be less than or equal to To. IPv6 zones are stripped out and ignored. An invalid range may be ignored.
type IPRange struct {
// contains filtered or unexported fields
}
func IPRangeFrom(from, to netip.Addr) IPRange
IPRangeFrom returns an IPRange from from to to. It does not allocate.
func MustParseIPRange(s string) IPRange
MustParseIPRange calls ParseIPRange(s) and panics on error. It is intended for use in tests with hard-coded strings.
func ParseIPRange(s string) (IPRange, error)
ParseIPRange parses a range out of two IPs separated by a hyphen.
It returns an error if the range is not valid.
func RangeOfPrefix(p netip.Prefix) IPRange
RangeOfPrefix returns the inclusive range of IPs that p covers.
If p is zero or otherwise invalid, Range returns the zero value.
func (r IPRange) AppendPrefixes(dst []netip.Prefix) []netip.Prefix
AppendPrefixes is an append version of IPRange.Prefixes. It appends the netip.Prefix entries that cover r to dst.
func (r IPRange) AppendTo(b []byte) []byte
AppendTo appends a text encoding of r, as generated by MarshalText, to b and returns the extended buffer.
func (r IPRange) Contains(addr netip.Addr) bool
Contains reports whether the range r includes addr.
An invalid range always reports false.
If ip has an IPv6 zone, Contains returns false, because IPPrefixes strip zones.
func (r IPRange) From() netip.Addr
From returns the lower bound of r.
func (r IPRange) IsValid() bool
IsValid reports whether r.From() and r.To() are both non-zero and obey the documented requirements: address families match, and From is less than or equal to To.
func (r IPRange) IsZero() bool
IsZero reports whether r is the zero value of the IPRange type.
func (r IPRange) MarshalText() ([]byte, error)
MarshalText implements the encoding.TextMarshaler interface, The encoding is the same as returned by String, with one exception: If ip is the zero value, the encoding is the empty string.
func (r IPRange) Overlaps(o IPRange) bool
Overlaps reports whether p and o overlap at all.
If p and o are of different address families or either are invalid, it reports false.
func (r IPRange) Prefix() (p netip.Prefix, ok bool)
Prefix returns r as an IPPrefix, if it can be presented exactly as such. If r is not valid or is not exactly equal to one prefix, ok is false.
func (r IPRange) Prefixes() []netip.Prefix
Prefixes returns the set of IPPrefix entries that covers r.
If either of r's bounds are invalid, in the wrong order, or if they're of different address families, then Prefixes returns nil.
Prefixes necessarily allocates. See AppendPrefixes for a version that uses memory you provide.
func (r IPRange) String() string
String returns a string representation of the range.
For a valid range, the form is "From-To" with a single hyphen separating the IPs, the same format recognized by ParseIPRange.
func (r IPRange) To() netip.Addr
To returns the upper bound of r.
func (r *IPRange) UnmarshalText(text []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface. The IP range is expected in a form accepted by ParseIPRange. It returns an error if *r is not the IPRange zero value.
func (r IPRange) Valid() bool
Valid reports whether r.From() and r.To() are both non-zero and obey the documented requirements: address families match, and From is less than or equal to To.
Deprecated: use the correctly named and identical IsValid method instead.
IPSet represents a set of IP addresses.
IPSet is safe for concurrent use. The zero value is a valid value representing a set of no IPs. Use IPSetBuilder to construct IPSets.
type IPSet struct {
// contains filtered or unexported fields
}
▹ Example
func (s *IPSet) Contains(ip netip.Addr) bool
Contains reports whether ip is in s. If ip has an IPv6 zone, Contains returns false, because IPSets do not track zones.
func (s *IPSet) ContainsPrefix(p netip.Prefix) bool
ContainsPrefix reports whether all IPs in p are in s.
func (s *IPSet) ContainsRange(r IPRange) bool
ContainsRange reports whether all IPs in r are in s.
func (s *IPSet) Equal(o *IPSet) bool
Equal reports whether s and o represent the same set of IP addresses.
func (s *IPSet) Overlaps(b *IPSet) bool
Overlaps reports whether any IP in b is also in s.
func (s *IPSet) OverlapsPrefix(p netip.Prefix) bool
OverlapsPrefix reports whether any IP in p is also in s.
func (s *IPSet) OverlapsRange(r IPRange) bool
OverlapsRange reports whether any IP in r is also in s.
func (s *IPSet) Prefixes() []netip.Prefix
Prefixes returns the minimum and sorted set of IP prefixes that covers s.
func (s *IPSet) Ranges() []IPRange
Ranges returns the minimum and sorted set of IP ranges that covers s.
func (s *IPSet) RemoveFreePrefix(bitLen uint8) (p netip.Prefix, newSet *IPSet, ok bool)
RemoveFreePrefix splits s into a Prefix of length bitLen and a new IPSet with that prefix removed.
If no contiguous prefix of length bitLen exists in s, RemoveFreePrefix returns ok=false.
IPSetBuilder builds an immutable IPSet.
The zero value is a valid value representing a set of no IPs.
The Add and Remove methods add or remove IPs to/from the set. Removals only affect the current membership of the set, so in general Adds should be called first. Input ranges may overlap in any way.
Most IPSetBuilder methods do not return errors. Instead, errors are accumulated and reported by IPSetBuilder.IPSet.
type IPSetBuilder struct {
// contains filtered or unexported fields
}
func (s *IPSetBuilder) Add(ip netip.Addr)
Add adds ip to s.
func (s *IPSetBuilder) AddPrefix(p netip.Prefix)
AddPrefix adds all IPs in p to s.
func (s *IPSetBuilder) AddRange(r IPRange)
AddRange adds r to s. If r is not Valid, AddRange does nothing.
func (s *IPSetBuilder) AddSet(b *IPSet)
AddSet adds all IPs in b to s.
func (s *IPSetBuilder) Clone() *IPSetBuilder
Clone returns a copy of s that shares no memory with s.
func (s *IPSetBuilder) Complement()
Complement updates s to contain the complement of its current contents.
func (s *IPSetBuilder) IPSet() (*IPSet, error)
IPSet returns an immutable IPSet representing the current state of s.
Most IPSetBuilder methods do not return errors. Rather, the builder ignores any invalid inputs (such as an invalid IPPrefix), and accumulates a list of any such errors that it encountered.
IPSet also reports any such accumulated errors. Even if the returned error is non-nil, the returned IPSet is usable and contains all modifications made with valid inputs.
The builder remains usable after calling IPSet. Calling IPSet clears any accumulated errors.
func (s *IPSetBuilder) Intersect(b *IPSet)
Intersect updates s to the set intersection of s and b.
func (s *IPSetBuilder) Remove(ip netip.Addr)
Remove removes ip from s.
func (s *IPSetBuilder) RemovePrefix(p netip.Prefix)
RemovePrefix removes all IPs in p from s.
func (s *IPSetBuilder) RemoveRange(r IPRange)
RemoveRange removes all IPs in r from s.
func (s *IPSetBuilder) RemoveSet(b *IPSet)
RemoveSet removes all IPs in o from s.