Documentation ¶
Overview ¶
Package netipx contains code and types that were left behind when the old inet.af/netaddr package moved to the standard library in Go 1.18 as net/netip.
Index ¶
- func AddrIPNet(addr netip.Addr) *net.IPNet
- func AddrNext(ip netip.Addr) netip.Addrdeprecated
- func AddrPrior(ip netip.Addr) netip.Addrdeprecated
- func ComparePrefix(a, b netip.Prefix) int
- func FromStdAddr(stdIP net.IP, port int, zone string) (_ netip.AddrPort, ok bool)
- func FromStdIP(std net.IP) (ip netip.Addr, ok bool)
- func FromStdIPNet(std *net.IPNet) (prefix netip.Prefix, ok bool)
- func FromStdIPRaw(std net.IP) (ip netip.Addr, ok bool)deprecated
- func MustFromStdIP(std net.IP) netip.Addr
- func ParsePrefixOrAddr(s string) (netip.Addr, error)
- func PrefixIPNet(p netip.Prefix) *net.IPNet
- func PrefixLastIP(p netip.Prefix) netip.Addr
- type IPRange
- func (r IPRange) AppendPrefixes(dst []netip.Prefix) []netip.Prefix
- func (r IPRange) AppendTo(b []byte) []byte
- func (r IPRange) Contains(addr netip.Addr) bool
- func (r IPRange) From() netip.Addr
- func (r IPRange) IsValid() bool
- func (r IPRange) IsZero() bool
- func (r IPRange) MarshalText() ([]byte, error)
- func (r IPRange) Overlaps(o IPRange) bool
- func (r IPRange) Prefix() (p netip.Prefix, ok bool)
- func (r IPRange) Prefixes() []netip.Prefix
- func (r IPRange) String() string
- func (r IPRange) To() netip.Addr
- func (r *IPRange) UnmarshalText(text []byte) error
- func (r IPRange) Valid() booldeprecated
- type IPSet
- func (s *IPSet) Contains(ip netip.Addr) bool
- func (s *IPSet) ContainsPrefix(p netip.Prefix) bool
- func (s *IPSet) ContainsRange(r IPRange) bool
- func (s *IPSet) Equal(o *IPSet) bool
- func (s *IPSet) Overlaps(b *IPSet) bool
- func (s *IPSet) OverlapsPrefix(p netip.Prefix) bool
- func (s *IPSet) OverlapsRange(r IPRange) bool
- func (s *IPSet) Prefixes() []netip.Prefix
- func (s *IPSet) Ranges() []IPRange
- func (s *IPSet) RemoveFreePrefix(bitLen uint8) (p netip.Prefix, newSet *IPSet, ok bool)
- type IPSetBuilder
- func (s *IPSetBuilder) Add(ip netip.Addr)
- func (s *IPSetBuilder) AddPrefix(p netip.Prefix)
- func (s *IPSetBuilder) AddRange(r IPRange)
- func (s *IPSetBuilder) AddSet(b *IPSet)
- func (s *IPSetBuilder) Clone() *IPSetBuilder
- func (s *IPSetBuilder) Complement()
- func (s *IPSetBuilder) IPSet() (*IPSet, error)
- func (s *IPSetBuilder) Intersect(b *IPSet)
- func (s *IPSetBuilder) Remove(ip netip.Addr)
- func (s *IPSetBuilder) RemovePrefix(p netip.Prefix)
- func (s *IPSetBuilder) RemoveRange(r IPRange)
- func (s *IPSetBuilder) RemoveSet(b *IPSet)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddrIPNet ¶
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 ComparePrefix ¶
ComparePrefix is a compare function (returning -1, 0 or 1) sorting prefixes first by address family (IPv4 before IPv6), then by prefix length (smaller prefixes first), then by address.
func FromStdAddr ¶
FromStdAddr maps the components of a standard library TCPAddr or UDPAddr into an IPPort.
func FromStdIP ¶
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 ¶
FromStdIPNet returns an netip.Prefix from the standard library's IPNet type. If std is invalid, ok is false.
func FromStdIPRaw
deprecated
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 ¶
MustFromStdIP is like FromStdIP, but it panics if std is invalid.
func ParsePrefixOrAddr ¶
ParsePrefixOrAddr parses s as an IP address prefix or IP address. If s parses as an IP address prefix, its net/netip.Prefix.Addr is returned. The string s can be an IPv4 address ("192.0.2.1"), IPv6 address ("2001:db8::68"), IPv4 prefix ("192.0.2.1/32"), or IPv6 prefix ("2001:db:68/96").
func PrefixIPNet ¶
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.
Types ¶
type IPRange ¶
type IPRange struct {
// contains filtered or unexported fields
}
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.
func IPRangeFrom ¶
IPRangeFrom returns an IPRange from from to to. It does not allocate.
func MustParseIPRange ¶
MustParseIPRange calls ParseIPRange(s) and panics on error. It is intended for use in tests with hard-coded strings.
func ParseIPRange ¶
ParseIPRange parses a range out of two IPs separated by a hyphen.
It returns an error if the range is not valid.
func RangeOfPrefix ¶
RangeOfPrefix returns the inclusive range of IPs that p covers.
If p is zero or otherwise invalid, Range returns the zero value.
func (IPRange) AppendPrefixes ¶
AppendPrefixes is an append version of IPRange.Prefixes. It appends the netip.Prefix entries that cover r to dst.
func (IPRange) AppendTo ¶
AppendTo appends a text encoding of r, as generated by MarshalText, to b and returns the extended buffer.
func (IPRange) Contains ¶
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 (IPRange) IsValid ¶
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 (IPRange) MarshalText ¶
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 (IPRange) Overlaps ¶
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 (IPRange) Prefix ¶
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 (IPRange) Prefixes ¶
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 (IPRange) 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 (*IPRange) UnmarshalText ¶
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.
type IPSet ¶
type IPSet struct {
// contains filtered or unexported fields
}
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.
Example ¶
package main import ( "fmt" "net/netip" "go4.org/netipx" ) func main() { var b netipx.IPSetBuilder b.AddPrefix(netip.MustParsePrefix("10.0.0.0/8")) b.RemovePrefix(netip.MustParsePrefix("10.0.0.0/16")) b.AddRange(netipx.IPRangeFrom( netip.MustParseAddr("fed0::0400"), netip.MustParseAddr("fed0::04ff"), )) s, _ := b.IPSet() fmt.Println("Ranges:") for _, r := range s.Ranges() { fmt.Printf(" %s - %s\n", r.From(), r.To()) } fmt.Println("Prefixes:") for _, p := range s.Prefixes() { fmt.Printf(" %s\n", p) } }
Output: Ranges: 10.1.0.0 - 10.255.255.255 fed0::400 - fed0::4ff Prefixes: 10.1.0.0/16 10.2.0.0/15 10.4.0.0/14 10.8.0.0/13 10.16.0.0/12 10.32.0.0/11 10.64.0.0/10 10.128.0.0/9 fed0::400/120
func (*IPSet) Contains ¶
Contains reports whether ip is in s. If ip has an IPv6 zone, Contains returns false, because IPSets do not track zones.
func (*IPSet) ContainsPrefix ¶
ContainsPrefix reports whether all IPs in p are in s.
func (*IPSet) ContainsRange ¶
ContainsRange reports whether all IPs in r are in s.
func (*IPSet) OverlapsPrefix ¶
OverlapsPrefix reports whether any IP in p is also in s.
func (*IPSet) OverlapsRange ¶
OverlapsRange reports whether any IP in r is also in s.
func (*IPSet) RemoveFreePrefix ¶
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.
type IPSetBuilder ¶
type IPSetBuilder struct {
// contains filtered or unexported fields
}
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.
func (*IPSetBuilder) AddPrefix ¶
func (s *IPSetBuilder) AddPrefix(p netip.Prefix)
AddPrefix adds all IPs in p to s.
func (*IPSetBuilder) AddRange ¶
func (s *IPSetBuilder) AddRange(r IPRange)
AddRange adds r to s. If r is not Valid, AddRange does nothing.
func (*IPSetBuilder) AddSet ¶
func (s *IPSetBuilder) AddSet(b *IPSet)
AddSet adds all IPs in b to s.
func (*IPSetBuilder) Clone ¶
func (s *IPSetBuilder) Clone() *IPSetBuilder
Clone returns a copy of s that shares no memory with s.
func (*IPSetBuilder) Complement ¶
func (s *IPSetBuilder) Complement()
Complement updates s to contain the complement of its current contents.
func (*IPSetBuilder) IPSet ¶
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 (*IPSetBuilder) Intersect ¶
func (s *IPSetBuilder) Intersect(b *IPSet)
Intersect updates s to the set intersection of s and b.
func (*IPSetBuilder) Remove ¶
func (s *IPSetBuilder) Remove(ip netip.Addr)
Remove removes ip from s.
func (*IPSetBuilder) RemovePrefix ¶
func (s *IPSetBuilder) RemovePrefix(p netip.Prefix)
RemovePrefix removes all IPs in p from s.
func (*IPSetBuilder) RemoveRange ¶
func (s *IPSetBuilder) RemoveRange(r IPRange)
RemoveRange removes all IPs in r from s.
func (*IPSetBuilder) RemoveSet ¶
func (s *IPSetBuilder) RemoveSet(b *IPSet)
RemoveSet removes all IPs in o from s.