subnet

package
v0.0.9-alpha Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Functions for working with subnets

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddressCount

func AddressCount[A ip.Address[A]](family ip.Family[A], bits int) *big.Int

Number of addresses in subnet with given bit mask size. Panics if mask bits exceeds width of family or is less than zero.

Example
package main

import (
	"fmt"

	"github.com/ipfreely-uk/go/ip"
	"github.com/ipfreely-uk/go/ip/subnet"
)

func main() {
	family4 := ip.V4()
	for mask := 0; mask <= family4.Width(); mask++ {
		count := subnet.AddressCount(family4, mask)
		msg := fmt.Sprintf("IPv%d /%d == %s", family4.Version(), mask, count.String())
		println(msg)
	}
	family6 := ip.V6()
	for mask := 0; mask <= family6.Width(); mask++ {
		count := subnet.AddressCount(family6, mask)
		msg := fmt.Sprintf("IPv%d /%d == %s", family6.Version(), mask, count.String())
		println(msg)
	}
}
Output:

func Mask

func Mask[A ip.Address[A]](family ip.Family[A], bits int) A

Mask of given bit size. Panics if mask bits exceeds ip.Family.Width or is less than zero.

Example
package main

import (
	"github.com/ipfreely-uk/go/ip"
	"github.com/ipfreely-uk/go/ip/subnet"
)

func main() {
	network := ip.V4().MustFromBytes(192, 168, 0, 0)

	maskBits := 24
	mask := subnet.Mask(ip.V4(), maskBits)

	println("First: %s", network.String())
	println("Last: %s", mask.Not().Or(network).String())
	println("Mask: %s", mask.String())
}
Output:

Example (Second)
package main

import (
	"fmt"

	"github.com/ipfreely-uk/go/ip"
	"github.com/ipfreely-uk/go/ip/subnet"
)

func main() {
	printAllMasks(ip.V4())
	printAllMasks(ip.V6())
}

func printAllMasks[A ip.Address[A]](f ip.Family[A]) {
	println(fmt.Sprintf("IPv%d", f.Version()))
	for bits := 0; bits <= f.Width(); bits++ {
		mask := subnet.Mask(f, bits)
		cidrTail := fmt.Sprintf("/%d", bits)
		println(mask.String(), "==", cidrTail)
	}
}
Output:

func MaskSize

func MaskSize[A ip.Address[A]](first, last A) int

Mask size in bits. Returns between 0 and ip.Family bit width inclusive if first and last form valid CIDR block. Returns -1 if first and last do not form valid CIDR block.

Example
package main

import (
	"github.com/ipfreely-uk/go/ip"
	"github.com/ipfreely-uk/go/ip/subnet"
)

func main() {
	family := ip.V4()
	first := family.MustFromBytes(192, 0, 2, 0)
	last := family.MustFromBytes(192, 0, 2, 255)

	maskBits := subnet.MaskSize(first, last)
	mask := subnet.Mask(family, maskBits)

	println(maskBits, "==", mask.String())
}
Output:

Example (Second)
package main

import (
	"fmt"

	"github.com/ipfreely-uk/go/ip"
	"github.com/ipfreely-uk/go/ip/subnet"
)

func main() {
	family := ip.V4()
	first := family.MustFromBytes(192, 0, 2, 0)
	last := family.MustFromBytes(192, 0, 2, 255)

	maskBits := subnet.MaskSize(first, last)
	if maskBits != -1 {
		cidrNotation := fmt.Sprintf("%s/%d", first.String(), maskBits)
		println(first.String(), "-", last.String(), " is valid subnet ", cidrNotation)
	} else {
		println(first.String(), "-", last.String(), " is not a valid subnet")
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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