cidr

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2020 License: MIT Imports: 3 Imported by: 472

Documentation

Overview

Package cidr is a collection of assorted utilities for computing network and host addresses within network ranges.

It expects a CIDR-type address structure where addresses are divided into some number of prefix bits representing the network and then the remaining suffix bits represent the host.

For example, it can help to calculate addresses for sub-networks of a parent network, or to calculate host addresses within a particular prefix.

At present this package is prioritizing simplicity of implementation and de-prioritizing speed and memory usage. Thus caution is advised before using this package in performance-critical applications or hot codepaths. Patches to improve the speed and memory usage may be accepted as long as they do not result in a significant increase in code complexity.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddressCount

func AddressCount(network *net.IPNet) uint64

AddressCount returns the number of distinct host addresses within the given CIDR range.

Since the result is a uint64, this function returns meaningful information only for IPv4 ranges and IPv6 ranges with a prefix size of at least 65.

func AddressRange

func AddressRange(network *net.IPNet) (net.IP, net.IP)

AddressRange returns the first and last addresses in the given CIDR range.

func Dec

func Dec(IP net.IP) net.IP

Dec decreases the IP by one this returns a new []byte for the IP

func Host

func Host(base *net.IPNet, num int) (net.IP, error)

Host takes a parent CIDR range and turns it into a host IP address with the given host number.

For example, 10.3.0.0/16 with a host number of 2 gives 10.3.0.2.

func HostBig added in v1.1.0

func HostBig(base *net.IPNet, num *big.Int) (net.IP, error)

HostBig takes a parent CIDR range and turns it into a host IP address with the given host number. It differs from Host in that it takes a *big.Int for the num, instead of an int.

For example, 10.3.0.0/16 with a host number of 2 gives 10.3.0.2.

func Inc

func Inc(IP net.IP) net.IP

Inc increases the IP by one this returns a new []byte for the IP

func NextSubnet

func NextSubnet(network *net.IPNet, prefixLen int) (*net.IPNet, bool)

NextSubnet returns the next available subnet of the desired mask size starting for the maximum IP of the offset subnet If the IP exceeds the maxium IP then the second return value is true

func PreviousSubnet

func PreviousSubnet(network *net.IPNet, prefixLen int) (*net.IPNet, bool)

PreviousSubnet returns the subnet of the desired mask in the IP space just lower than the start of IPNet provided. If the IP space rolls over then the second return value is true

func Subnet

func Subnet(base *net.IPNet, newBits int, num int) (*net.IPNet, error)

Subnet takes a parent CIDR range and creates a subnet within it with the given number of additional prefix bits and the given network number.

For example, 10.3.0.0/16, extended by 8 bits, with a network number of 5, becomes 10.3.5.0/24 .

func SubnetBig added in v1.1.0

func SubnetBig(base *net.IPNet, newBits int, num *big.Int) (*net.IPNet, error)

SubnetBig takes a parent CIDR range and creates a subnet within it with the given number of additional prefix bits and the given network number. It differs from Subnet in that it takes a *big.Int for the num, instead of an int.

For example, 10.3.0.0/16, extended by 8 bits, with a network number of 5, becomes 10.3.5.0/24 .

func VerifyNoOverlap

func VerifyNoOverlap(subnets []*net.IPNet, CIDRBlock *net.IPNet) error

VerifyNoOverlap takes a list subnets and supernet (CIDRBlock) and verifies none of the subnets overlap and all subnets are in the supernet it returns an error if any of those conditions are not satisfied

Types

This section is empty.

Jump to

Keyboard shortcuts

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