netaddr

package
v1.234.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package netaddr reproduces the inet.af/netaddr IP, IPPrefix and IPRange values that gomplate v3's net.ParseIP, net.ParseIPPrefix and net.ParseIPRange returned. Templates written for v3 call their methods directly and pass them to each other (for example `(net.ParseIPPrefix "10.0.0.0/8").Contains (net.ParseIP "10.1.2.3")`), so the compatibility shims return these types instead of net/netip values. The package name matches inet.af/netaddr so `printf "%T"` output is unchanged.

The values are backed by net/netip and go4.org/netipx; inet.af/netaddr is no longer maintained.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IP

type IP struct {
	// contains filtered or unexported fields
}

IP is an IPv4 or IPv6 address, possibly with an IPv6 zone. The zero value is not a valid address.

func FromAddr

func FromAddr(addr netip.Addr) IP

FromAddr wraps a net/netip address.

func (IP) AppendTo

func (ip IP) AppendTo(b []byte) []byte

AppendTo appends the text form of ip to b.

func (IP) As4

func (ip IP) As4() [net.IPv4len]byte

As4 returns an IPv4 or IPv4-mapped IPv6 address in its 4-byte representation.

func (IP) As16

func (ip IP) As16() [net.IPv6len]byte

As16 returns ip in its 16-byte representation.

func (IP) BitLen

func (ip IP) BitLen() uint8

BitLen returns 32 for IPv4, 128 for IPv6, and 0 for the zero value.

func (IP) Compare

func (ip IP) Compare(ip2 IP) int

Compare returns -1, 0 or 1 as ip sorts before, equal to or after ip2.

func (IP) IPAddr

func (ip IP) IPAddr() *net.IPAddr

IPAddr returns ip as a net.IPAddr.

func (IP) Is4

func (ip IP) Is4() bool

Is4 reports whether ip is an IPv4 address.

func (IP) Is4in6

func (ip IP) Is4in6() bool

Is4in6 reports whether ip is an IPv4-mapped IPv6 address.

func (IP) Is6

func (ip IP) Is6() bool

Is6 reports whether ip is an IPv6 address, including IPv4-mapped ones.

func (IP) IsGlobalUnicast

func (ip IP) IsGlobalUnicast() bool

IsGlobalUnicast reports whether ip is a global unicast address.

func (IP) IsInterfaceLocalMulticast

func (ip IP) IsInterfaceLocalMulticast() bool

IsInterfaceLocalMulticast reports whether ip is an IPv6 interface-local multicast address.

func (IP) IsLinkLocalMulticast

func (ip IP) IsLinkLocalMulticast() bool

IsLinkLocalMulticast reports whether ip is a link-local multicast address.

func (IP) IsLinkLocalUnicast

func (ip IP) IsLinkLocalUnicast() bool

IsLinkLocalUnicast reports whether ip is a link-local unicast address.

func (IP) IsLoopback

func (ip IP) IsLoopback() bool

IsLoopback reports whether ip is a loopback address.

func (IP) IsMulticast

func (ip IP) IsMulticast() bool

IsMulticast reports whether ip is a multicast address.

func (IP) IsPrivate

func (ip IP) IsPrivate() bool

IsPrivate reports whether ip is in a private address range (RFC 1918 or RFC 4193).

func (IP) IsUnspecified

func (ip IP) IsUnspecified() bool

IsUnspecified reports whether ip is 0.0.0.0 or ::.

func (IP) IsValid

func (ip IP) IsValid() bool

IsValid reports whether ip is a valid address (not the zero value).

func (IP) IsZero

func (ip IP) IsZero() bool

IsZero reports whether ip is the zero value.

func (IP) Less

func (ip IP) Less(ip2 IP) bool

Less reports whether ip sorts before ip2.

func (IP) MarshalBinary

func (ip IP) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler.

func (IP) MarshalText

func (ip IP) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler; the zero value encodes as "".

func (IP) Netmask

func (ip IP) Netmask(mask []byte) (IPPrefix, error)

Netmask returns the prefix of ip for a netmask in byte form.

func (IP) Next

func (ip IP) Next() IP

Next returns the address following ip, or the zero value at the end of the address space.

func (IP) Prefix

func (ip IP) Prefix(bits uint8) (IPPrefix, error)

Prefix returns the prefix of ip with the given length, with host bits cleared.

func (IP) Prior

func (ip IP) Prior() IP

Prior returns the address before ip, or the zero value at the start of the address space.

func (IP) String

func (ip IP) String() string

String returns ip in its canonical form, or "invalid IP" for the zero value.

func (IP) StringExpanded

func (ip IP) StringExpanded() string

StringExpanded is like String but writes IPv6 addresses in full.

func (IP) Unmap

func (ip IP) Unmap() IP

Unmap returns ip with any IPv4-mapped IPv6 prefix removed.

func (IP) WithZone

func (ip IP) WithZone(zone string) IP

WithZone returns ip with the given IPv6 zone; IPv4 addresses are unchanged.

func (IP) Zone

func (ip IP) Zone() string

Zone returns ip's IPv6 scoped addressing zone, if any.

type IPPrefix

type IPPrefix struct {
	// contains filtered or unexported fields
}

IPPrefix is an IP address and a prefix length (CIDR notation), with the address kept as written.

func FromPrefix

func FromPrefix(prefix netip.Prefix) IPPrefix

FromPrefix wraps a net/netip prefix, keeping its address as written.

func (IPPrefix) AppendTo

func (p IPPrefix) AppendTo(b []byte) []byte

AppendTo appends the text form of p to b.

func (IPPrefix) Bits

func (p IPPrefix) Bits() uint8

Bits returns the prefix length.

func (IPPrefix) Contains

func (p IPPrefix) Contains(ip IP) bool

Contains reports whether p contains ip.

func (IPPrefix) IP

func (p IPPrefix) IP() IP

IP returns the prefix's address as written, host bits included.

func (IPPrefix) IPNet

func (p IPPrefix) IPNet() *net.IPNet

IPNet returns p as a net.IPNet, keeping the address as written.

func (IPPrefix) IsSingleIP

func (p IPPrefix) IsSingleIP() bool

IsSingleIP reports whether p contains exactly one address.

func (IPPrefix) IsValid

func (p IPPrefix) IsValid() bool

IsValid reports whether p has a valid address and a length that fits it.

func (IPPrefix) IsZero

func (p IPPrefix) IsZero() bool

IsZero reports whether p is the zero value.

func (IPPrefix) MarshalText

func (p IPPrefix) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler; the zero value encodes as "".

func (IPPrefix) Masked

func (p IPPrefix) Masked() IPPrefix

Masked returns p with host bits cleared, or the zero value if p is invalid.

func (IPPrefix) Overlaps

func (p IPPrefix) Overlaps(o IPPrefix) bool

Overlaps reports whether p and o have any address in common.

func (IPPrefix) Range

func (p IPPrefix) Range() IPRange

Range returns the inclusive range of addresses in p.

func (IPPrefix) String

func (p IPPrefix) String() string

String returns p in CIDR notation, or "invalid IPPrefix" if p is invalid.

func (IPPrefix) Valid

func (p IPPrefix) Valid() bool

Valid is IsValid.

type IPRange

type IPRange struct {
	// contains filtered or unexported fields
}

IPRange is an inclusive range of IP addresses.

func FromRange

func FromRange(r netipx.IPRange) IPRange

FromRange wraps a go4.org/netipx range.

func (IPRange) AppendPrefixes

func (r IPRange) AppendPrefixes(dst []IPPrefix) []IPPrefix

AppendPrefixes appends the prefixes that cover r to dst.

func (IPRange) AppendTo

func (r IPRange) AppendTo(b []byte) []byte

AppendTo appends the text form of r to b.

func (IPRange) Contains

func (r IPRange) Contains(addr IP) bool

Contains reports whether r contains addr.

func (IPRange) From

func (r IPRange) From() IP

From returns the first address in r.

func (IPRange) IsValid

func (r IPRange) IsValid() bool

IsValid reports whether r has addresses of the same family with From not after To.

func (IPRange) IsZero

func (r IPRange) IsZero() bool

IsZero reports whether r is the zero value.

func (IPRange) MarshalText

func (r IPRange) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler; the zero value encodes as "".

func (IPRange) Overlaps

func (r IPRange) Overlaps(o IPRange) bool

Overlaps reports whether r and o have any address in common.

func (IPRange) Prefix

func (r IPRange) Prefix() (IPPrefix, bool)

Prefix returns r as a single prefix when it is exactly one.

func (IPRange) Prefixes

func (r IPRange) Prefixes() []IPPrefix

Prefixes returns the smallest set of prefixes that covers r.

func (IPRange) String

func (r IPRange) String() string

String returns r as "from-to", or "invalid IPRange" if r is invalid.

func (IPRange) To

func (r IPRange) To() IP

To returns the last address in r.

func (IPRange) Valid

func (r IPRange) Valid() bool

Valid is IsValid.

Jump to

Keyboard shortcuts

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