ipaddr

package
v0.0.0-...-1dc08c0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const IPv4mappedIPv6prefix = uint64(0xFFFF) << 32

IPv4mappedIPv6prefix is the byte prefix for IPv4-mapped IPv6.

View Source
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.

View Source
const IPv4max = IPv4mask

IPv4max is used for overflows.

View Source
const IPv4size = net.IPv4len + 2

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

View Source
const IPv6size = net.IPv6len + 2

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

Variables

This section is empty.

Functions

func ParseINet

func ParseINet(s string, dest *IPAddr) error

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

Types

type Addr

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

func (ip Addr) Add(o uint64) Addr

Add wraps the Uint128 addition.

func (Addr) Compare

func (ip Addr) Compare(o Addr) int

Compare wraps the Uint128 equivilance.

func (Addr) Equal

func (ip Addr) Equal(o Addr) bool

Equal wraps the Uint128 equivilance.

func (Addr) String

func (ip Addr) String() string

String wraps net.IP.String().

func (Addr) Sub

func (ip Addr) Sub(o uint64) Addr

Sub wraps the Uint128 subtraction.

func (Addr) WriteIPv4Bytes

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

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

WriteIPv6Bytes gets the 16-byte IPv6 representation.

type IPAddr

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

func RandIPAddr(rng *rand.Rand) IPAddr

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

func (*IPAddr) Add

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

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

func (*IPAddr) And

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

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

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

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

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

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

func (IPAddr) ContainedByOrEquals

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

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

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

func (IPAddr) ContainsOrContainedBy

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

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

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

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

func (*IPAddr) FromBuffer

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

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

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

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

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

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

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

func (*IPAddr) SubIPAddr

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

SubIPAddr returns the difference between two IPAddrs.

func (*IPAddr) ToBuffer

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

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

type IPFamily

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
)

Jump to

Keyboard shortcuts

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