net

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package net provides utility functions for working with IPs (net.IP).

Index

Constants

View Source
const (
	IPv4Uint32Count = 1
	IPv6Uint32Count = 4

	BitsPerUint32 = 32
	BytePerUint32 = 4

	IPv4 IPVersion = "IPv4"
	IPv6 IPVersion = "IPv6"
)

Helper constants.

Variables

View Source
var ErrInvalidBitPosition = fmt.Errorf("bit position not valid")

ErrInvalidBitPosition is returned when bits requested is not valid.

View Source
var ErrNoGreatestCommonBit = fmt.Errorf("no greatest common bit")

ErrNoGreatestCommonBit is an error returned when no greatest common bit exists for the cidr ranges.

View Source
var ErrVersionMismatch = fmt.Errorf("Network input version mismatch")

ErrVersionMismatch is returned upon mismatch in network input versions.

Functions

func NextIP

func NextIP(ip netip.Addr) netip.Addr

NextIP returns the next sequential ip.

func PreviousIP

func PreviousIP(ip netip.Addr) netip.Addr

PreviousIP returns the previous sequential ip.

Types

type IPVersion

type IPVersion string

IPVersion is version of IP address.

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

func NewNetwork(ipNet netip.Prefix) Network

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) Covers

func (n Network) Covers(o Network) bool

Covers returns true if Network covers o, false otherwise

func (Network) Equal

func (n Network) Equal(n1 Network) bool

Equal is the equality test for 2 networks.

func (Network) LeastCommonBitPosition

func (n Network) LeastCommonBitPosition(n1 Network) (max uint, err error)

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.

func (Network) Masked

func (n Network) Masked(ones int) Network

Masked returns a new network conforming to new mask.

func (Network) String

func (n Network) String() string

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

Mask returns a new masked NetworkNumber from given NetworkNumber.

Jump to

Keyboard shortcuts

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