subnetmath

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2019 License: MIT Imports: 5 Imported by: 4

README

subnetmath

func main() {
    aggregate := subnetmath.ParseNetworkCIDR("192.168.0.0/22")
    subnets := []*net.IPNet{
        subnetmath.ParseNetworkCIDR("192.168.1.0/24"),
        subnetmath.ParseNetworkCIDR("192.168.2.32/30"),
    }
    unused := subnetmath.FindUnusedSubnets(aggregate, subnets)
    // [ 192.168.0.0/24 192.168.2.0/27 192.168.2.36/30 192.168.2.40/29 192.168.2.48/28 192.168.2.64/26 192.168.2.128/25 192.168.3.0/24 ]
}
Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz

BenchmarkIntToAddr-8                      	30000000	        56.1 ns/op	      24 B/op	       2 allocs/op
BenchmarkAddrToInt-8                      	20000000	        80.5 ns/op	      80 B/op	       2 allocs/op
BenchmarkParseNetworkCIDR-8               	10000000	       167 ns/op	      72 B/op	       4 allocs/op
BenchmarkNetworkComesBefore-8             	50000000	        30.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkIPv4ClassfulNetwork-8            	20000000	        89.1 ns/op	      68 B/op	       3 allocs/op
BenchmarkNextAddr-8                       	10000000	       165 ns/op	     104 B/op	       4 allocs/op
BenchmarkShrinkNetwork-8                  	 2000000	       747 ns/op	     104 B/op	      26 allocs/op
BenchmarkNextNetwork-8                    	 5000000	       351 ns/op	     256 B/op	       9 allocs/op
BenchmarkFindInbetweenSubnets-8           	  200000	      7118 ns/op	    4336 B/op	     133 allocs/op
BenchmarkFindInbetweenSubnetsBuffered-8   	  300000	      4255 ns/op	     168 B/op	       9 allocs/op
BenchmarkFindUnusedSubnets-8              	   50000	     31529 ns/op	    6720 B/op	     480 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddrToInt

func AddrToInt(address net.IP) *big.Int

AddrToInt will return the *bit.Int of a given IPv6 address

func AddressComesBefore

func AddressComesBefore(firstIP, secondIP net.IP) bool

AddressComesBefore returns a bool with regards to numerical address order. Note that IPv4 addresses come before IPv6 addresses.

func BroadcastAddr

func BroadcastAddr(network *net.IPNet) net.IP

BroadcastAddr returns the broadcast address

func DuplicateAddr

func DuplicateAddr(addr net.IP) net.IP

DuplicateAddr creates a new copy of net.IP

func DuplicateNetwork

func DuplicateNetwork(network *net.IPNet) *net.IPNet

DuplicateNetwork returns a new copy of *net.IPNet

func FindInbetweenSubnets

func FindInbetweenSubnets(start, stop net.IP) []*net.IPNet

FindInbetweenSubnets returns a slice of subnets given a range of IP addresses. Note that the delimiter 'stop' is inclusive. In other words, it will be included in the result.

func FindUnusedSubnets

func FindUnusedSubnets(aggregate *net.IPNet, subnets ...*net.IPNet) (unused []*net.IPNet)

FindUnusedSubnets returns a slice of unused subnets given the aggregate and sibling subnets

func IPv4ClassfulNetwork

func IPv4ClassfulNetwork(address net.IP) *net.IPNet

IPv4ClassfulNetwork eithers return the classful network given an IPv4 address or returns nil if given a multicast address or IPv6 address

func IntToAddr

func IntToAddr(intAddress *big.Int) net.IP

IntToAddr will return the net.IP of the big.Int represented address

func NetworkComesBefore

func NetworkComesBefore(first, second *net.IPNet) bool

NetworkComesBefore returns a bool with regards to numerical network order. Note that IPv4 networks come before IPv6 networks.

func NetworkContainsSubnet

func NetworkContainsSubnet(network *net.IPNet, subnet *net.IPNet) bool

NetworkContainsSubnet validates that the network is a valid supernet

func NetworksAreIdentical

func NetworksAreIdentical(first, second *net.IPNet) bool

NetworksAreIdentical returns a bool with regards to the two networks being equal

func NextAddr

func NextAddr(addr net.IP) net.IP

NextAddr returns a new net.IP that is the next address

func NextNetwork

func NextNetwork(network *net.IPNet) *net.IPNet

NextNetwork returns the next network of the same size

func ParseNetworkCIDR

func ParseNetworkCIDR(cidr string) *net.IPNet

ParseNetworkCIDR is a convienence function that will return either the *net.IPNet or nil if the supplied cidr is invalid

func ShrinkNetwork

func ShrinkNetwork(network *net.IPNet) *net.IPNet

ShrinkNetwork increases the mask size by one

func SubnetZeroAddr

func SubnetZeroAddr(address net.IP, network *net.IPNet) net.IP

SubnetZeroAddr returns the subnet zero address

Types

type Buffer

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

func NewBuffer

func NewBuffer() *Buffer

func (*Buffer) AddressComesBefore

func (b *Buffer) AddressComesBefore(firstIP, secondIP net.IP) bool

AddressComesBefore returns a bool with regards to numerical address order. Note that IPv4 addresses come before IPv6 addresses.

func (*Buffer) FindInbetweenSubnets

func (b *Buffer) FindInbetweenSubnets(start, stop net.IP) []*net.IPNet

FindInbetweenSubnets returns a slice of subnets given a range of IP addresses. Note that the delimiter 'stop' is inclusive. In other words, it will be included in the result.

func (*Buffer) NetworkComesBefore

func (b *Buffer) NetworkComesBefore(first, second *net.IPNet) bool

NetworkComesBefore returns a bool with regards to numerical network order. Note that IPv4 networks come before IPv6 networks.

func (*Buffer) NetworkContainsSubnet

func (b *Buffer) NetworkContainsSubnet(network *net.IPNet, subnet *net.IPNet) bool

NetworkContainsSubnet validates that the network is a valid supernet

func (*Buffer) SubnetZeroAddr

func (b *Buffer) SubnetZeroAddr(address net.IP, network *net.IPNet) net.IP

SubnetZeroAddr returns the subnet zero address

Jump to

Keyboard shortcuts

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