netaddr

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

README

netaddr

A library for network address manipulation, written in Go.

GitHub go.mod Go version of a Go module GoDoc reference example GoReportCard example

Documentation

Index

Constants

View Source
const (
	// IP address lengths (bytes).
	IPv4len = 4
	IPv6len = 16
)

Variables

View Source
var (
	IPv4 = &Version{
		number:    4,
		length:    4,
		bitLength: IPv4len * 8,

		max: &IPNumber{
			Int: (&big.Int{}).Sub((&big.Int{}).Exp(big.NewInt(2), big.NewInt(32), nil), big.NewInt(1)),
		},
	}

	IPv6 = &Version{
		number:    6,
		length:    16,
		bitLength: IPv6len * 8,

		max: &IPNumber{
			Int: (&big.Int{}).Sub((&big.Int{}).Exp(big.NewInt(2), big.NewInt(128), nil), big.NewInt(1)),
		},
	}
)
View Source
var (
	ErrorAddressOutOFBounds = fmt.Errorf("ip number out range of ip-version boundary")
)

Functions

func ValidIPV4

func ValidIPV4(ipBytes []byte) bool

ValidIPV4 returns true when the passed bytes are a valid IPV4.

Types

type ByIPRanges

type ByIPRanges []IPRange

Implements sort.Interface for sorting IPRange

func (ByIPRanges) Len

func (rs ByIPRanges) Len() int

func (ByIPRanges) Less

func (rs ByIPRanges) Less(i, j int) bool

func (ByIPRanges) Swap

func (rs ByIPRanges) Swap(i, j int)

type IPAddress

type IPAddress struct {
	*net.IP
	// contains filtered or unexported fields
}

func MinAddress

func MinAddress(addr1, addr2 *IPAddress) *IPAddress

func NewIP

func NewIP(ip string) *IPAddress

NewIP returns a new IPAddress object, initialised with the IP info parsed from ip.

func (*IPAddress) Equal

func (ip *IPAddress) Equal(other *IPAddress) bool

func (*IPAddress) GreaterThan

func (ip *IPAddress) GreaterThan(other *IPAddress) bool

func (*IPAddress) GreaterThanOrEqual

func (ip *IPAddress) GreaterThanOrEqual(other *IPAddress) bool

func (*IPAddress) Increment

func (ip *IPAddress) Increment(val *IPNumber) (*IPAddress, error)

Increment increments the IPAddress by an amount, val, which is of big.Int type.

func (*IPAddress) LessThan

func (ip *IPAddress) LessThan(other *IPAddress) bool

func (*IPAddress) LessThanOrEqual

func (ip *IPAddress) LessThanOrEqual(other *IPAddress) bool

func (*IPAddress) String

func (ip *IPAddress) String() string

String returns the string representation of address ip.

func (*IPAddress) ToInt

func (ip *IPAddress) ToInt() *IPNumber

ToInt returns the integer representation (IPNumber) for the given IPAddress.

func (*IPAddress) Version

func (ip *IPAddress) Version() *Version

Version returns the ip version for IPAddress, ip.

type IPMask

type IPMask struct {
	*net.IPMask
}

func NewMask

func NewMask(ones, bits int64) *IPMask

NewMask returns a new IPMask object with the passed ones and bits.

func (*IPMask) Equals

func (m *IPMask) Equals(other *IPMask) bool

func (*IPMask) Length

func (m *IPMask) Length() *IPNumber

returns the number of valid ip addresses in a subnet

func (*IPMask) LessThan

func (m *IPMask) LessThan(other *IPMask) bool

type IPNetwork

type IPNetwork struct {
	Mask *IPMask
	// contains filtered or unexported fields
}

IPNetwork defines an IPAddress network, including version and mask.

func IPRangeToCIDRS

func IPRangeToCIDRS(version *Version, start, end *IPAddress) ([]*IPNetwork, error)

func NewIPNetwork

func NewIPNetwork(cidr string) (*IPNetwork, error)

func (*IPNetwork) ContainsAddress

func (nw *IPNetwork) ContainsAddress(addr *IPAddress) bool

func (*IPNetwork) ContainsSubnetwork

func (nw *IPNetwork) ContainsSubnetwork(other *IPNetwork) bool

func (*IPNetwork) Equal

func (nw *IPNetwork) Equal(other *IPNetwork) bool

func (*IPNetwork) First

func (nw *IPNetwork) First() *IPAddress

func (*IPNetwork) Last

func (nw *IPNetwork) Last() *IPAddress

func (*IPNetwork) Length

func (nw *IPNetwork) Length() *IPNumber

func (*IPNetwork) LessThan

func (nw *IPNetwork) LessThan(other *IPNetwork) bool

func (*IPNetwork) Partition

func (nw *IPNetwork) Partition(exclude *IPNetwork) *Partition

func (*IPNetwork) PrefixLength

func (nw *IPNetwork) PrefixLength() *IPNumber

func (*IPNetwork) String

func (nw *IPNetwork) String() string

Returns the string representation of the netwrok - i.e. 127.0.0.1/8

func (*IPNetwork) Subnet

func (nw *IPNetwork) Subnet(newCIDRPrefix int) ([]*IPNetwork, error)

Divide a subnet into smaller subnets based on the provided CIDR prefix

type IPNumber

type IPNumber struct{ *big.Int }

IPNumber is the integer representation of an IP address

func NewIPNumber

func NewIPNumber(v int64) *IPNumber

NewIPNumber returns an IPNumber for the passed number.

func (*IPNumber) Add

func (num *IPNumber) Add(v *IPNumber) *IPNumber

func (*IPNumber) And

func (num *IPNumber) And(v *IPNumber) *IPNumber

func (*IPNumber) Equal

func (num *IPNumber) Equal(other *IPNumber) bool

func (*IPNumber) Exp

func (num *IPNumber) Exp(v *IPNumber) *IPNumber

func (*IPNumber) GreaterThan

func (num *IPNumber) GreaterThan(other *IPNumber) bool

GreaterThan compares two IPNumbers, returning true when num is greater than other.

func (*IPNumber) GreaterThanOrEqual

func (num *IPNumber) GreaterThanOrEqual(other *IPNumber) bool

GreaterThanOrEqual compares two IPNumbers, returning true when num is greater than or equal to other.

func (*IPNumber) LessThan

func (num *IPNumber) LessThan(other *IPNumber) bool

func (*IPNumber) LessThanOrEqual

func (num *IPNumber) LessThanOrEqual(other *IPNumber) bool

func (*IPNumber) Lsh

func (num *IPNumber) Lsh(v uint) *IPNumber

func (*IPNumber) Neg

func (num *IPNumber) Neg() *IPNumber

func (*IPNumber) Sub

func (num *IPNumber) Sub(v *IPNumber) *IPNumber

func (*IPNumber) ToIPAddress

func (num *IPNumber) ToIPAddress() *IPAddress

ToIPAddress convers the given IPNumber object to an IPAddress.

type IPRange

type IPRange struct {
	// contains filtered or unexported fields
}

type IPSet

type IPSet []*IPNetwork

IPSet represents an unordered collection of unique IP addresses and subnets. IPAddresses are represented here as iPNetworks with a mask of /32

func MergeCIDRs

func MergeCIDRs(cidrs []IPNetwork) IPSet

func (*IPSet) Add

func (set *IPSet) Add()

Add adds an IPAddress or IPNetwork to this IPSet.

IPAddresses are represented as IPNetworks with a /32 subnet mask, and where possible the IPAddresses and IPNetworks are merged with other members of the set to form more concise CIDR blocks.

func (*IPSet) Pop

func (set *IPSet) Pop()

Pop removes an arbitrary subnet from this IPSet

func (*IPSet) Remove

func (set *IPSet) Remove()

Remote Removes an IP address or subnet or IPRange from this IP set. Does nothing if it is not already a member.

type Partition

type Partition struct {
	Before    []*IPNetwork
	Partition *IPNetwork
	After     []*IPNetwork
}

type Version

type Version struct {
	// contains filtered or unexported fields
}

func (*Version) LessThan

func (v *Version) LessThan(other *Version) bool

Compares two IP Address versions, v and other. Returns true if v is less than other.

func (*Version) String

func (v *Version) String() string

String returns the string representation of version v.

Jump to

Keyboard shortcuts

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