netip

package standard library
go1.19.2 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2022 License: BSD-3-Clause Imports: 7 Imported by: 11,142

Documentation

Overview

Package netip defines an IP address type that's a small value type. Building on that Addr type, the package also defines AddrPort (an IP address and a port), and Prefix (an IP address and a bit length prefix).

Compared to the net.IP type, this package's Addr type takes less memory, is immutable, and is comparable (supports == and being a map key).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Addr

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

Addr represents an IPv4 or IPv6 address (with or without a scoped addressing zone), similar to net.IP or net.IPAddr.

Unlike net.IP or net.IPAddr, Addr is a comparable value type (it supports == and can be a map key) and is immutable.

The zero Addr is not a valid IP address. Addr{} is distinct from both 0.0.0.0 and ::.

func AddrFrom4

func AddrFrom4(addr [4]byte) Addr

AddrFrom4 returns the address of the IPv4 address given by the bytes in addr.

func AddrFrom16

func AddrFrom16(addr [16]byte) Addr

AddrFrom16 returns the IPv6 address given by the bytes in addr. An IPv4-mapped IPv6 address is left as an IPv6 address. (Use Unmap to convert them if needed.)

func AddrFromSlice

func AddrFromSlice(slice []byte) (ip Addr, ok bool)

AddrFromSlice parses the 4- or 16-byte byte slice as an IPv4 or IPv6 address. Note that a net.IP can be passed directly as the []byte argument. If slice's length is not 4 or 16, AddrFromSlice returns Addr{}, false.

func IPv4Unspecified

func IPv4Unspecified() Addr

IPv4Unspecified returns the IPv4 unspecified address "0.0.0.0".

func IPv6LinkLocalAllNodes

func IPv6LinkLocalAllNodes() Addr

IPv6LinkLocalAllNodes returns the IPv6 link-local all nodes multicast address ff02::1.

func IPv6Unspecified

func IPv6Unspecified() Addr

IPv6Unspecified returns the IPv6 unspecified address "::".

func MustParseAddr

func MustParseAddr(s string) Addr

MustParseAddr calls ParseAddr(s) and panics on error. It is intended for use in tests with hard-coded strings.

func ParseAddr

func ParseAddr(s string) (Addr, error)

ParseAddr parses s as an IP address, returning the result. The string s can be in dotted decimal ("192.0.2.1"), IPv6 ("2001:db8::68"), or IPv6 with a scoped addressing zone ("fe80::1cc0:3e8c:119f:c2e1%ens18").

func (Addr) AppendTo

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

AppendTo appends a text encoding of ip, as generated by MarshalText, to b and returns the extended buffer.

func (Addr) As4

func (ip Addr) As4() (a4 [4]byte)

As4 returns an IPv4 or IPv4-in-IPv6 address in its 4-byte representation. If ip is the zero Addr or an IPv6 address, As4 panics. Note that 0.0.0.0 is not the zero Addr.

func (Addr) As16

func (ip Addr) As16() (a16 [16]byte)

As16 returns the IP address in its 16-byte representation. IPv4 addresses are returned as IPv4-mapped IPv6 addresses. IPv6 addresses with zones are returned without their zone (use the Zone method to get it). The ip zero value returns all zeroes.

func (Addr) AsSlice

func (ip Addr) AsSlice() []byte

AsSlice returns an IPv4 or IPv6 address in its respective 4-byte or 16-byte representation.

func (Addr) BitLen

func (ip Addr) BitLen() int

BitLen returns the number of bits in the IP address: 128 for IPv6, 32 for IPv4, and 0 for the zero Addr.

Note that IPv4-mapped IPv6 addresses are considered IPv6 addresses and therefore have bit length 128.

func (Addr) Compare

func (ip Addr) Compare(ip2 Addr) int

Compare returns an integer comparing two IPs. The result will be 0 if ip == ip2, -1 if ip < ip2, and +1 if ip > ip2. The definition of "less than" is the same as the Less method.

func (Addr) Is4

func (ip Addr) Is4() bool

Is4 reports whether ip is an IPv4 address.

It returns false for IPv4-mapped IPv6 addresses. See Addr.Unmap.

func (Addr) Is4In6

func (ip Addr) Is4In6() bool

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

func (Addr) Is6

func (ip Addr) Is6() bool

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

func (Addr) IsGlobalUnicast

func (ip Addr) IsGlobalUnicast() bool

IsGlobalUnicast reports whether ip is a global unicast address.

It returns true for IPv6 addresses which fall outside of the current IANA-allocated 2000::/3 global unicast space, with the exception of the link-local address space. It also returns true even if ip is in the IPv4 private address space or IPv6 unique local address space. It returns false for the zero Addr.

For reference, see RFC 1122, RFC 4291, and RFC 4632.

func (Addr) IsInterfaceLocalMulticast

func (ip Addr) IsInterfaceLocalMulticast() bool

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

func (Addr) IsLinkLocalMulticast

func (ip Addr) IsLinkLocalMulticast() bool

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

func (Addr) IsLinkLocalUnicast

func (ip Addr) IsLinkLocalUnicast() bool

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

func (Addr) IsLoopback

func (ip Addr) IsLoopback() bool

IsLoopback reports whether ip is a loopback address.

func (Addr) IsMulticast

func (ip Addr) IsMulticast() bool

IsMulticast reports whether ip is a multicast address.

func (Addr)