cidr

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: 8 Imported by: 0

Documentation

Overview

RFC-4632 Classless Inter-domain Routing notation handling https://www.rfc-editor.org/rfc/rfc4632

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Notation

func Notation[A ip.Address[A]](b network.Block[A]) string

Returns string form in CIDR notation

Example
package main

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

func main() {
	address, _ := ip.V4().FromBytes(192, 168, 0, 1)
	single := network.NewBlock(address, address.Family().Width())
	notation := cidr.Notation(single)
	println(notation)
}
Output:

func Parse

func Parse[A ip.Address[A]](f ip.Family[A], notation string) (network.Block[A], error)

Parses CIDR notation

Example
package main

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

func main() {
	reservedForDocumentation, _ := cidr.Parse(ip.V6(), "2001:db8::/32")
	printRangeDetails(reservedForDocumentation)
}

func printRangeDetails[A ip.Address[A]](addresses network.AddressRange[A]) {
	println("Start:", addresses.First().String())
	println("End:", addresses.Last().String())
	println("Addresses:", addresses.Size().String())
	println()
}
Output:

func ParseUnknown

func ParseUnknown(notation string) (any, error)

Parses CIDR notation where IP address family is unknown. Returns error if operand is not valid CIDR notation. Returns network.Block when oprand is valid.

Example
package main

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

func main() {
	reservedForDocumentation := []string{
		"192.0.2.0/24",
		"198.51.100.0/24",
		"203.0.113.0/24",
		"2001:db8::/32",
	}
	for _, notation := range reservedForDocumentation {
		block, err := cidr.ParseUnknown(notation)
		if err != nil {
			panic(err)
		}
		switch addresses := block.(type) {
		case network.Block[ip.Addr4]:
			printRangeDetails(addresses)
		case network.Block[ip.Addr6]:
			printRangeDetails(addresses)
		}
	}
}

func printRangeDetails[A ip.Address[A]](addresses network.AddressRange[A]) {
	println("Start:", addresses.First().String())
	println("End:", addresses.Last().String())
	println("Addresses:", addresses.Size().String())
	println()
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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