netipx

package module
v0.0.0-...-fdeea32 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: BSD-3-Clause Imports: 10 Imported by: 151

README

netipx Test Status Go Reference

What

This is a package containing the bits of the old inet.af/netaddr package that didn't make it into Go 1.18's net/netip standard library package.

As background, see:

This package requires Go 1.18+ to use and complements the net/netip.

FAQ

Why's it no longer under inet.af? Since that joke started, that TLD is now under control of the Taliban. (Yes, we should've known better. We'd even previously scolded people for relying on questionable ccTLDs. Whoops.)

Will this stuff make it into the standard library? Maybe. We'll see.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddrIPNet

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 deprecated

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 deprecated

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 ComparePrefix

func ComparePrefix(a, b netip.Prefix) int

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

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

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

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 deprecated

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

func MustFromStdIP(std net.IP) netip.Addr

MustFromStdIP is like FromStdIP, but it panics if std is invalid.

func ParsePrefixOrAddr

func ParsePrefixOrAddr(s string) (netip.Addr, error)

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

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

func PrefixLastIP(p netip.Prefix) netip.Addr

PrefixLastIP returns the last IP in the prefix.

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

func IPRangeFrom(from, to netip.Addr) IPRange

IPRangeFrom returns an IPRange from from to to. It does not allocate.

func MustParseIPRange

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

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

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 (IPRange) AppendPrefixes

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 (IPRange) AppendTo

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 (IPRange) Contains

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 (IPRange) From

func (r IPRange) From() netip.Addr

From returns the lower bound of r.

func (IPRange) IsValid

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 (IPRange) IsZero

func (r IPRange) IsZero() bool

IsZero reports whether r is the zero value of the IPRange type.

func (IPRange) MarshalText

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 (IPRange) Overlaps

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 (IPRange) Prefix

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 (IPRange) Prefixes

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 (IPRange) String

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 (IPRange) To

func (r IPRange) To() netip.Addr

To returns the upper bound of r.

func (*IPRange) UnmarshalText

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 (IPRange) Valid deprecated

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.

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

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 (*IPSet) ContainsPrefix

func (s *IPSet) ContainsPrefix(p netip.Prefix) bool

ContainsPrefix reports whether all IPs in p are in s.

func (*IPSet) ContainsRange

func (s *IPSet) ContainsRange(r IPRange) bool

ContainsRange reports whether all IPs in r are in s.

func (*IPSet) Equal

func (s *IPSet) Equal(o *IPSet) bool

Equal reports whether s and o represent the same set of IP addresses.

func (*IPSet) Overlaps

func (s *IPSet) Overlaps(b *IPSet) bool

Overlaps reports whether any IP in b is also in s.

func (*IPSet) OverlapsPrefix

func (s *IPSet) OverlapsPrefix(p netip.Prefix) bool

OverlapsPrefix reports whether any IP in p is also in s.

func (*IPSet) OverlapsRange

func (s *IPSet) OverlapsRange(r IPRange) bool

OverlapsRange reports whether any IP in r is also in s.

func (*IPSet) Prefixes

func (s *IPSet) Prefixes() []netip.Prefix

Prefixes returns the minimum and sorted set of IP prefixes that covers s.

func (*IPSet) Ranges

func (s *IPSet) Ranges() []IPRange

Ranges returns the minimum and sorted set of IP ranges that covers s.

func (*IPSet) RemoveFreePrefix

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.

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) Add

func (s *IPSetBuilder) Add(ip netip.Addr)

Add adds ip to s.

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.

Jump to

Keyboard shortcuts

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