Documentation ¶
Index ¶
- Constants
- func ParseINet(s string, dest *IPAddr) error
- type Addr
- type IP
- type IPAddr
- func (ipAddr *IPAddr) Add(o int64) (IPAddr, error)
- func (ipAddr *IPAddr) And(other *IPAddr) (IPAddr, error)
- func (ipAddr *IPAddr) Broadcast() IPAddr
- func (ipAddr IPAddr) Compare(other *IPAddr) int
- func (ipAddr *IPAddr) Complement() IPAddr
- func (ipAddr IPAddr) ContainedBy(other *IPAddr) bool
- func (ipAddr IPAddr) ContainedByOrEquals(other *IPAddr) bool
- func (ipAddr IPAddr) Contains(other *IPAddr) bool
- func (ipAddr IPAddr) ContainsOrContainedBy(other *IPAddr) bool
- func (ipAddr IPAddr) ContainsOrEquals(other *IPAddr) bool
- func (ipAddr *IPAddr) Equal(other *IPAddr) bool
- func (ipAddr *IPAddr) FromBuffer(data []byte) ([]byte, error)
- func (ipAddr *IPAddr) Hostmask() IPAddr
- func (ipAddr *IPAddr) Netmask() IPAddr
- func (ipAddr *IPAddr) Or(other *IPAddr) (IPAddr, error)
- func (ipAddr IPAddr) String() string
- func (ipAddr *IPAddr) Sub(o int64) (IPAddr, error)
- func (ipAddr *IPAddr) SubIPAddr(other *IPAddr) (int64, error)
- func (ipAddr *IPAddr) ToBuffer(appendTo []byte) []byte
- type IPFamily
Constants ¶
const ( IPv4len = 4 IPv6len = 16 )
IP address lengths (bytes).
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 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.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Addr ¶
Addr is the representation of the IP address. The Uint128 takes 16-bytes for both IPv4 and IPv6.
func (Addr) WriteIPv4Bytes ¶
WriteIPv4Bytes writes the 4-byte IPv4 representation. If the IP is IPv6 then the first 12-bytes are truncated.
type IP ¶
type IP []byte
An IP is a single IP address, a slice of bytes. Functions in this package accept either 4-byte (IPv4) or 16-byte (IPv6) slices as input.
Note that in this documentation, referring to an IP address as an IPv4 address or an IPv6 address is a semantic property of the address, not just the length of the byte slice: a 16-byte slice can still be an IPv4 address.
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 ¶
RandIPAddr generates a random IPAddr. This includes random mask size and IP family.
func (*IPAddr) And ¶
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 ¶
Broadcast returns a new IPAddr where the host mask of the IP address is a full mask, i.e. 0xFF bytes.
func (IPAddr) Compare ¶
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 ¶
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 ¶
ContainedBy determines if one ipAddr is in the same subnet as another.
func (IPAddr) ContainedByOrEquals ¶
ContainedByOrEquals determines if one ipAddr is in the same subnet as another or the addresses and subnets are equal.
func (IPAddr) ContainsOrContainedBy ¶
ContainsOrContainedBy determines if one ipAddr is in the same subnet as another or vice versa.
func (IPAddr) ContainsOrEquals ¶
ContainsOrEquals determines if one ipAddr is in the same subnet as another or the addresses and subnets are equal.
func (*IPAddr) FromBuffer ¶
FromBuffer populates an IPAddr with data from a byte slice, returning the remaining buffer or an error.
func (*IPAddr) Hostmask ¶
Hostmask returns the host masked IP. This is defined as the IP address bits that are not masked.
func (*IPAddr) Netmask ¶
Netmask returns the network masked IP. This is defined as the IP address bits that are masked.
func (*IPAddr) Or ¶
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 ¶
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().