Documentation ¶
Overview ¶
Package net provides utility functions for working with IPs (net.IP).
Index ¶
- Constants
- Variables
- func NextIP(ip netip.Addr) netip.Addr
- func PreviousIP(ip netip.Addr) netip.Addr
- type IPVersion
- type Network
- type NetworkNumber
- func (n NetworkNumber) Bit(position uint) (uint32, error)
- func (n NetworkNumber) Equal(n1 NetworkNumber) bool
- func (n *NetworkNumber) FlipNthBit(position uint) error
- func (n NetworkNumber) LeastCommonBitPosition(n1 NetworkNumber) (uint, error)
- func (n NetworkNumber) Next() NetworkNumber
- func (n NetworkNumber) Previous() NetworkNumber
- func (n NetworkNumber) ToIP() netip.Addr
- func (n NetworkNumber) ToV4() NetworkNumber
- func (n NetworkNumber) ToV6() NetworkNumber
- type NetworkNumberMask
Constants ¶
const ( IPv4Uint32Count = 1 IPv6Uint32Count = 4 BitsPerUint32 = 32 BytePerUint32 = 4 IPv4 IPVersion = "IPv4" IPv6 IPVersion = "IPv6" )
Helper constants.
Variables ¶
var ErrInvalidBitPosition = fmt.Errorf("bit position not valid")
ErrInvalidBitPosition is returned when bits requested is not valid.
var ErrNoGreatestCommonBit = fmt.Errorf("no greatest common bit")
ErrNoGreatestCommonBit is an error returned when no greatest common bit exists for the cidr ranges.
var ErrVersionMismatch = fmt.Errorf("Network input version mismatch")
ErrVersionMismatch is returned upon mismatch in network input versions.
Functions ¶
Types ¶
type Network ¶
type Network struct { IPNet netip.Prefix Number NetworkNumber Mask NetworkNumberMask }
Network represents a block of network numbers, also known as CIDR.
func NewNetwork ¶
NewNetwork returns Network built using given net.IPNet.
func (Network) Contains ¶
func (n Network) Contains(nn NetworkNumber) bool
Contains returns true if NetworkNumber is in range of Network, false otherwise.
func (Network) LeastCommonBitPosition ¶
LeastCommonBitPosition returns the smallest position of the preceding common bits of the 2 networks, and returns an error ErrNoGreatestCommonBit if the two network number diverges from the first bit.
type NetworkNumber ¶
type NetworkNumber []uint32
NetworkNumber represents an IP address using uint32 as internal storage. IPv4 usings 1 uint32, while IPv6 uses 4 uint32.
func NewNetworkNumber ¶
func NewNetworkNumber(ip netip.Addr) NetworkNumber
NewNetworkNumber returns a equivalent NetworkNumber to given IP address, return nil if ip is neither IPv4 nor IPv6.
func (NetworkNumber) Bit ¶
func (n NetworkNumber) Bit(position uint) (uint32, error)
Bit returns uint32 representing the bit value at given position, e.g., "128.0.0.0" has bit value of 1 at position 31, and 0 for positions 30 to 0.
func (NetworkNumber) Equal ¶
func (n NetworkNumber) Equal(n1 NetworkNumber) bool
Equal is the equality test for 2 network numbers.
func (*NetworkNumber) FlipNthBit ¶ added in v1.0.4
func (n *NetworkNumber) FlipNthBit(position uint) error
FlipNthBit reverses the bit value at position. Position numbering is LSB 0.
func (NetworkNumber) LeastCommonBitPosition ¶
func (n NetworkNumber) LeastCommonBitPosition(n1 NetworkNumber) (uint, error)
LeastCommonBitPosition returns the smallest position of the preceding common bits of the 2 network numbers, and returns an error ErrNoGreatestCommonBit if the two network number diverges from the first bit. e.g., if the network number diverges after the 1st bit, it returns 131 for IPv6 and 31 for IPv4 .
func (NetworkNumber) Next ¶
func (n NetworkNumber) Next() NetworkNumber
Next returns the next logical network number.
func (NetworkNumber) Previous ¶
func (n NetworkNumber) Previous() NetworkNumber
Previous returns the previous logical network number.
func (NetworkNumber) ToIP ¶
func (n NetworkNumber) ToIP() netip.Addr
ToIP returns equivalent net.IP.
func (NetworkNumber) ToV4 ¶
func (n NetworkNumber) ToV4() NetworkNumber
ToV4 returns ip address if ip is IPv4, returns nil otherwise.
func (NetworkNumber) ToV6 ¶
func (n NetworkNumber) ToV6() NetworkNumber
ToV6 returns ip address if ip is IPv6, returns nil otherwise.
type NetworkNumberMask ¶
type NetworkNumberMask NetworkNumber
NetworkNumberMask is an IP address.
func (NetworkNumberMask) Mask ¶
func (m NetworkNumberMask) Mask(n NetworkNumber) (NetworkNumber, error)
Mask returns a new masked NetworkNumber from given NetworkNumber.