cockroach: github.com/cockroachdb/cockroach/pkg/util/ipaddr Index | Files

package ipaddr

import "github.com/cockroachdb/cockroach/pkg/util/ipaddr"

Index

Package Files

ipaddr.go

Constants

const IPv4mappedIPv6prefix = uint64(0xFFFF) << 32

IPv4mappedIPv6prefix is the byte prefix for IPv4-mapped IPv6.

const IPv4mask = uint64(0xFFFFFFFF)

IPv4mask is used to select only the lower 32 bits of uint128. IPv4 addresses may not have the the upper 96 bits of Addr set to 0. IPv4 addresses mapped to IPv6 have prefix bits that should not change.

const IPv4max = IPv4mask

IPv4max is used for overflows.

const IPv4size = net.IPv4len + 2

IPv4size 1 byte for family, 1 byte for mask, 4 for IP.

const IPv6size = net.IPv6len + 2

IPv6size 1 byte for family, 1 byte for mask, 16 for IP.

func ParseINet Uses

func ParseINet(s string, dest *IPAddr) error

ParseINet parses postgres style INET types. See TestIPAddrParseINet for examples.

type Addr Uses

type Addr uint128.Uint128

Addr is the representation of the IP address. The Uint128 takes 16-bytes for both IPv4 and IPv6.

func (Addr) Add Uses

func (ip Addr) Add(o uint64) Addr

Add wraps the Uint128 addition.

func (Addr) Compare Uses

func (ip Addr) Compare(o Addr) int

Compare wraps the Uint128 equivilance.

func (Addr) Equal Uses

func (ip Addr) Equal(o Addr) bool

Equal wraps the Uint128 equivilance.

func (Addr) String Uses

func (ip Addr) String() string

String wraps net.IP.String().

func (Addr) Sub Uses

func (ip Addr) Sub(o uint64) Addr

Sub wraps the Uint128 subtraction.

func (Addr) WriteIPv4Bytes Uses

func (ip Addr) WriteIPv4Bytes(writer io.Writer) error

WriteIPv4Bytes writes the 4-byte IPv4 representation. If the IP is IPv6 then the first 12-bytes are truncated.

func (Addr) WriteIPv6Bytes Uses

func (ip Addr) WriteIPv6Bytes(writer io.Writer) error

WriteIPv6Bytes gets the 16-byte IPv6 representation.

type IPAddr Uses

type IPAddr struct {
    // Family denotes what type of IP the original IP was.
    Family IPFamily
    Mask   byte
    Addr   Addr
}

IPAddr stores an IP address's family, IP, and host mask. This was chosen over Go's "net" IP, as that struct doesn't work well for what we need to do.

	- It discards information when parsing IPv4, forcing it to be IPv6, and then
		assuming IPv4-mapped IPv6 addresses are purely IPv4 (only for printing).
		This is solved by having a Family field.
	- ParseIP and ParseCIDR are very strict, whereas postgres' INET and CIDR
	  have very relaxed constraints for parsing an IP.
 - Doing int64 operations is much more efficient than byte slice operations.

func RandIPAddr Uses

func RandIPAddr(rng *rand.Rand) IPAddr

RandIPAddr generates a random IPAddr. This includes random mask size and IP family.

func (*IPAddr) Add Uses

func (ipAddr *IPAddr) Add(o int64) (IPAddr, error)

Add returns a new IPAddr that is incremented by an int64.

func (*IPAddr) And Uses

func (ipAddr *IPAddr) And(other *IPAddr) (IPAddr, error)

And returns a new IPAddr which is the bitwise AND of two IPAddrs. Only the lower 32 bits are changed for IPv4.

func (*IPAddr) Broadcast Uses

func (ipAddr *IPAddr) Broadcast() IPAddr

Broadcast returns a new IPAddr where the host mask of the IP address is a full mask, i.e. 0xFF bytes.

func (IPAddr) Compare Uses

func (ipAddr IPAddr) Compare(other *IPAddr) int

Compare two IPAddrs. IPv4-mapped IPv6 addresses are not equal to their IPv4 mapping. The order of order importance goes Family > Mask > IP-bytes.

func (*IPAddr) Complement Uses

func (ipAddr *IPAddr) Complement() IPAddr

Complement returns a new IPAddr which is the bitwise complement of the original IP. Only the lower 32 bits are changed for IPv4.

func (IPAddr) ContainedBy Uses

func (ipAddr IPAddr) ContainedBy(other *IPAddr) bool

ContainedBy determines if one ipAddr is in the same subnet as another.

func (IPAddr) ContainedByOrEquals Uses

func (ipAddr IPAddr) ContainedByOrEquals(other *IPAddr) bool

ContainedByOrEquals determines if one ipAddr is in the same subnet as another or the addresses and subnets are equal.

func (IPAddr) Contains Uses

func (ipAddr IPAddr) Contains(other *IPAddr) bool

Contains determines if one ipAddr is in the same subnet as another.

func (IPAddr) ContainsOrContainedBy Uses

func (ipAddr IPAddr) ContainsOrContainedBy(other *IPAddr) bool

ContainsOrContainedBy determines if one ipAddr is in the same subnet as another or vice versa.

func (IPAddr) ContainsOrEquals Uses

func (ipAddr IPAddr) ContainsOrEquals(other *IPAddr) bool

ContainsOrEquals determines if one ipAddr is in the same subnet as another or the addresses and subnets are equal.

func (*IPAddr) Equal Uses

func (ipAddr *IPAddr) Equal(other *IPAddr) bool

Equal checks if the family, mask, and IP are equal.

func (*IPAddr) FromBuffer Uses

func (ipAddr *IPAddr) FromBuffer(data []byte) ([]byte, error)

FromBuffer populates an IPAddr with data from a byte slice, returning the remaining buffer or an error.

func (*IPAddr) Hostmask Uses

func (ipAddr *IPAddr) Hostmask() IPAddr

Hostmask returns the host masked IP. This is defined as the IP address bits that are not masked.

func (*IPAddr) Netmask Uses

func (ipAddr *IPAddr) Netmask() IPAddr

Netmask returns the network masked IP. This is defined as the IP address bits that are masked.

func (*IPAddr) Or Uses

func (ipAddr *IPAddr) Or(other *IPAddr) (IPAddr, error)

Or returns a new IPAddr which is the bitwise OR of two IPAddrs. Only the lower 32 bits are changed for IPv4.

func (IPAddr) String Uses

func (ipAddr IPAddr) String() string

String will convert the IP to the appropriate family formatted string representation. In order to retain postgres compatibility we ensure IPv4-mapped IPv6 stays in IPv6 format, unlike net.Addr.String().

func (*IPAddr) Sub Uses

func (ipAddr *IPAddr) Sub(o int64) (IPAddr, error)

Sub returns a new IPAddr that is decremented by an int64.

func (*IPAddr) SubIPAddr Uses

func (ipAddr *IPAddr) SubIPAddr(other *IPAddr) (int64, error)

SubIPAddr returns the difference between two IPAddrs.

func (*IPAddr) ToBuffer Uses

func (ipAddr *IPAddr) ToBuffer(appendTo []byte) []byte

ToBuffer appends the IPAddr encoding to a buffer and returns the final buffer.

type IPFamily Uses

type IPFamily byte

IPFamily denotes which classification the IP belongs to.

const (
    // IPv4family is for IPs in the IPv4 space.
    IPv4family IPFamily = iota
    // IPv6family is for IPs in the IPv6 space.
    IPv6family
)

Package ipaddr imports 12 packages (graph) and is imported by 24 packages. Updated 2019-06-27. Refresh now. Tools for package owners.