Involved Source Filesleaf_alts.go
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).
uint128.go
Package-Level Type Names (total 5, in which 3 are exported)
/* sort exporteds by: | */
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 ::.
addr is the hi and lo bits of an IPv6 address. If z==z4,
hi and lo contain the IPv4-mapped IPv6 address.
hi and lo are constructed by interpreting a 16-byte IPv6
address as a big-endian 128-bit number. The most significant
bits of that number go into hi, the rest into lo.
For example, 0011:2233:4455:6677:8899:aabb:ccdd:eeff is stored as:
addr.hi = 0x0011223344556677
addr.lo = 0x8899aabbccddeeff
We store IPs like this, rather than as [16]byte, because it
turns most operations on IPs into arithmetic and bit-twiddling
operations on 64-bit registers, which is much faster than
bytewise processing.
z is a combination of the address family and the IPv6 zone.
nil means invalid IP address (for a zero Addr).
z4 means an IPv4 address.
z6noz means an IPv6 address without a zone.
Otherwise it's the interned zone name string.
AppendTo appends a text encoding of ip,
as generated by MarshalText,
to b and returns the extended buffer.
As16 returns the IP address in its 16-byte representation.
IPv4 addresses are returned in their v6-mapped form.
IPv6 addresses with zones are returned without their zone (use the
Zone method to get it).
The ip zero value returns all zeroes.
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.
AsSlice returns an IPv4 or IPv6 address in its respective 4-byte or 16-byte representation.
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.
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.
Is4 reports whether ip is an IPv4 address.
It returns false for IP4-mapped IPv6 addresses. See IP.Unmap.
Is4In6 reports whether ip is an IPv4-mapped IPv6 address.
Is6 reports whether ip is an IPv6 address, including IPv4-mapped
IPv6 addresses.
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.
IsInterfaceLocalMulticast reports whether ip is an IPv6 interface-local
multicast address.
IsLinkLocalMulticast reports whether ip is a link-local multicast address.
IsLinkLocalUnicast reports whether ip is a link-local unicast address.
IsLoopback reports whether ip is a loopback address.
IsMulticast reports whether ip is a multicast address.
IsPrivate reports whether ip is a private address, according to RFC 1918
(IPv4 addresses) and RFC 4193 (IPv6 addresses). That is, it reports whether
ip is in 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, or fc00::/7. This is the
same as net.IP.IsPrivate.
IsUnspecified reports whether ip is an unspecified address, either the IPv4
address "0.0.0.0" or the IPv6 address "::".
Note that the zero Addr is not an unspecified address.
IsValid reports whether the Addr is an initialized address (not the zero Addr).
Note that "0.0.0.0" and "::" are both valid values.
Less reports whether ip sorts before ip2.
IP addresses sort first by length, then their address.
IPv6 addresses with zones sort just after the same address without a zone.
MarshalBinary implements the encoding.BinaryMarshaler interface.
It returns a zero-length slice for the zero Addr,
the 4-byte form for an IPv4 address,
and the 16-byte form with zone appended for an IPv6 address.
MarshalText implements the encoding.TextMarshaler interface,
The encoding is the same as returned by String, with one exception:
If ip is the zero Addr, the encoding is the empty string.
Next returns the address following ip.
If there is none, it returns the zero Addr.
Prefix keeps only the top b bits of IP, producing a Prefix
of the specified length.
If ip is a zero Addr, Prefix always returns a zero Prefix and a nil error.
Otherwise, if bits is less than zero or greater than ip.BitLen(),
Prefix returns an error.
Prev returns the IP before ip.
If there is none, it returns the IP zero value.
String returns the string form of the IP address ip.
It returns one of 5 forms:
- "invalid IP", if ip is the zero Addr
- IPv4 dotted decimal ("192.0.2.1")
- IPv6 ("2001:db8::1")
- "::ffff:1.2.3.4" (if Is4In6)
- IPv6 with zone ("fe80:db8::1%eth0")
Note that unlike package net's IP.String method,
IP4-mapped IPv6 addresses format with a "::ffff:"
prefix before the dotted quad.
StringExpanded is like String but IPv6 addresses are expanded with leading
zeroes and no "::" compression. For example, "2001:db8::1" becomes
"2001:0db8:0000:0000:0000:0000:0000:0001".
Unmap returns ip with any IPv4-mapped IPv6 address prefix removed.
That is, if ip is an IPv6 address wrapping an IPv4 adddress, it
returns the wrapped IPv4 address. Otherwise it returns ip unmodified.
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
It expects data in the form generated by MarshalBinary.
UnmarshalText implements the encoding.TextUnmarshaler interface.
The IP address is expected in a form accepted by ParseAddr.
If text is empty, UnmarshalText sets *ip to the zero Addr and
returns no error.
WithZone returns an IP that's the same as ip but with the provided
zone. If zone is empty, the zone is removed. If ip is an IPv4
address, WithZone is a no-op and returns ip unchanged.
Zone returns ip's IPv6 scoped addressing zone, if any.
( Addr) appendTo4(ret []byte) []byte( Addr) appendTo6(ret []byte) []byte
hasZone reports whether IP has an IPv6 zone.
isZero reports whether ip is the zero value of the IP type.
The zero value is not a valid IP address of any type.
Note that "0.0.0.0" and "::" are not the zero value. Use IsUnspecified to
check for these values instead.
( Addr) lessOrEq(ip2 Addr) bool( Addr) marshalBinaryWithTrailingBytes(trailingBytes int) []byte( Addr) string4() string
string6 formats ip in IPv6 textual representation. It follows the
guidelines in section 4 of RFC 5952
(https://tools.ietf.org/html/rfc5952#section-4): no unnecessary
zeros, use :: to elide the longest run of zeros, and don't use ::
to compact a single zero field.
v4 returns the i'th byte of ip. If ip is not an IPv4, v4 returns
unspecified garbage.
v6 returns the i'th byte of ip. If ip is an IPv4 address, this
accesses the IPv4-mapped IPv6 address form of the IP.
v6u16 returns the i'th 16-bit word of ip. If ip is an IPv4 address,
this accesses the IPv4-mapped IPv6 address form of the IP.
withoutZone unconditionally strips the zone from IP.
It's similar to WithZone, but small enough to be inlinable.
Addr : database/sql/driver.Validator
Addr : encoding.BinaryMarshaler
*Addr : encoding.BinaryUnmarshaler
Addr : encoding.TextMarshaler
*Addr : encoding.TextUnmarshaler
Addr : expvar.Var
Addr : fmt.Stringer
Addr : context.stringer
*Addr : crypto/hmac.marshalable
Addr : github.com/aws/smithy-go/middleware.stringer
Addr : runtime.stringer
func AddrFrom16(addr [16]byte) Addr
func AddrFrom4(addr [4]byte) Addr
func AddrFromSlice(slice []byte) (ip Addr, ok bool)
func IPv4Unspecified() Addr
func IPv6LinkLocalAllNodes() Addr
func IPv6Unspecified() Addr
func MustParseAddr(s string) Addr
func ParseAddr(s string) (Addr, error)
func Addr.Next() Addr
func Addr.Prev() Addr
func Addr.Unmap() Addr
func Addr.WithZone(zone string) Addr
func AddrPort.Addr() Addr
func Prefix.Addr() Addr
func net.(*Resolver).LookupNetIP(ctx context.Context, network, host string) ([]Addr, error)
func ipv6Slice(addr []byte) Addr
func parseIPv4(s string) (ip Addr, err error)
func parseIPv6(in string) (Addr, error)
func Addr.withoutZone() Addr
func AddrPortFrom(ip Addr, port uint16) AddrPort
func PrefixFrom(ip Addr, bits int) Prefix
func Addr.Compare(ip2 Addr) int
func Addr.Less(ip2 Addr) bool
func Prefix.Contains(ip Addr) bool
func Addr.lessOrEq(ip2 Addr) bool
Prefix is an IP address prefix (CIDR) representing an IP network.
The first Bits() of Addr() are specified. The remaining bits match any address.
The range of Bits() is [0,32] for IPv4 or [0,128] for IPv6.
bits is logically a uint8 (storing [0,128]) but also
encodes an "invalid" bit, currently represented by the
invalidPrefixBits sentinel value. It could be packed into
the uint8 more with more complicated expressions in the
accessors, but the extra byte (in padding anyway) doesn't
hurt and simplifies code below.
ipAddr
Addr returns p's IP address.
AppendTo appends a text encoding of p,
as generated by MarshalText,
to b and returns the extended buffer.
Bits returns p's prefix length.
It reports -1 if invalid.
Contains reports whether the network p includes ip.
An IPv4 address will not match an IPv6 prefix.
A v6-mapped IPv6 address will not match an IPv4 prefix.
A zero-value IP will not match any prefix.
If ip has an IPv6 zone, Contains returns false,
because Prefixes strip zones.
IsSingleIP reports whether p contains exactly one IP.
IsValid reports whether p.Bits() has a valid range for p.IP().
If p.Addr() is the zero Addr, IsValid returns false.
Note that if p is the zero Prefix, then p.IsValid() == false.
MarshalBinary implements the encoding.BinaryMarshaler interface.
It returns Addr.MarshalBinary with an additional byte appended
containing the prefix bits.
MarshalText implements the encoding.TextMarshaler interface,
The encoding is the same as returned by String, with one exception:
If p is the zero value, the encoding is the empty string.
Masked returns p in its canonical form, with all but the high
p.Bits() bits of p.Addr() masked off.
If p is zero or otherwise invalid, Masked returns the zero Prefix.
Overlaps reports whether p and o contain any IP addresses in common.
If p and o are of different address families or either have a zero
IP, it reports false. Like the Contains method, a prefix with a
v6-mapped IPv4 IP is still treated as an IPv6 mask.
String returns the CIDR notation of p: "<ip>/<bits>".
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
It expects data in the form generated by MarshalBinary.
UnmarshalText implements the encoding.TextUnmarshaler interface.
The IP address is expected in a form accepted by ParsePrefix
or generated by MarshalText.
( Prefix) isZero() bool
Prefix : database/sql/driver.Validator
Prefix : encoding.BinaryMarshaler
*Prefix : encoding.BinaryUnmarshaler
Prefix : encoding.TextMarshaler
*Prefix : encoding.TextUnmarshaler
Prefix : expvar.Var
Prefix : fmt.Stringer
Prefix : context.stringer
*Prefix : crypto/hmac.marshalable
Prefix : github.com/aws/smithy-go/middleware.stringer
Prefix : runtime.stringer
func MustParsePrefix(s string) Prefix
func ParsePrefix(s string) (Prefix, error)
func PrefixFrom(ip Addr, bits int) Prefix
func Addr.Prefix(b int) (Prefix, error)
func Prefix.Masked() Prefix
func Prefix.Overlaps(o Prefix) bool
// optionally, the unparsed portion of in at which the error occurred.
// the string given to ParseAddr
// an explanation of the parse failure
( parseAddrError) Error() string
parseAddrError : error
uint128 represents a uint128 using two uint64s.
When the methods below mention a bit number, bit 0 is the most
significant bit (in hi) and bit 127 is the lowest (lo&1).
hiuint64louint64
addOne returns u + 1.
and returns the bitwise AND of u and m (u&m).
bitsClearedFrom returns a copy of u with the given bit
and all subsequent ones cleared.
bitsSetFrom returns a copy of u with the given bit
and all subsequent ones set.
( uint128) commonPrefixLen(v uint128) (n uint8)
halves returns the two uint64 halves of the uint128.
Logically, think of it as returning two uint64s.
It only returns pointers for inlining reasons on 32-bit platforms.
isZero reports whether u == 0.
It's faster than u == (uint128{}) because the compiler (as of Go
1.15/1.16b1) doesn't do this trick and instead inserts a branch in
its eq alg's generated code.
not returns the bitwise NOT of u.
or returns the bitwise OR of u and m (u|m).
subOne returns u - 1.
xor returns the bitwise XOR of u and m (u^m).
func mask6(n int) uint128
Package-Level Functions (total 30, in which 14 are exported)
AddrFrom16 returns the IPv6 address given by the bytes in addr.
An IPv6-mapped IPv4 address is left as an IPv6 address.
(Use Unmap to convert them if needed.)
AddrFrom4 returns the address of the IPv4 address given by the bytes in addr.
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.
AddrPortFrom returns an AddrPort with the provided IP and port.
It does not allocate.
IPv4Unspecified returns the IPv4 unspecified address "0.0.0.0".
IPv6LinkLocalAllNodes returns the IPv6 link-local all nodes multicast
address ff02::1.
IPv6Unspecified returns the IPv6 unspecified address "::".
MustParseAddr calls ParseAddr(s) and panics on error.
It is intended for use in tests with hard-coded strings.
MustParseAddrPort calls ParseAddrPort(s) and panics on error.
It is intended for use in tests with hard-coded strings.
MustParsePrefix calls ParsePrefix(s) and panics on error.
It is intended for use in tests with hard-coded strings.
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").
ParseAddrPort parses s as an AddrPort.
It doesn't do any name resolution: both the address and the port
must be numeric.
ParsePrefix parses s as an IP address prefix.
The string can be in the form "192.168.1.0/24" or "2001:db8::/32",
the CIDR notation defined in RFC 4632 and RFC 4291.
Note that masked address bits are not zeroed. Use Masked for that.
PrefixFrom returns a Prefix with the provided IP address and bit
prefix length.
It does not allocate. Unlike Addr.Prefix, PrefixFrom does not mask
off the host bits of ip.
If bits is less than zero or greater than ip.BitLen, Prefix.Bits
will return an invalid value -1.
appendDecimal appends the decimal string representation of x to b.
appendHex appends the hex string representation of x to b.
appendHexPad appends the fully padded hex string representation of x to b.
mask6 returns a uint128 bitmask with the topmost n bits of a
128-bit number.
parseIPv4 parses s as an IPv4 address (in form "192.168.0.1").
parseIPv6 parses s as an IPv6 address (in form "2001:db8::68").
splitAddrPort splits s into an IP address string and a port
string. It splits strings shaped like "foo:bar" or "[foo]:bar",
without further validating the substrings. v6 indicates whether the
ip string should parse as an IPv6 address or an IPv4 address, in
order for s to be a valid ip:port string.