extnetip

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2022 License: MIT Imports: 3 Imported by: 6

README

package extnetip

Go Reference

extnetip is an extension package to the stdlib net/netip.

Some missing math functions are added to the closed private internals of netip.Addr using unsafe.

No further types are defined, only helper functions on the existing types netip.Addr and netip.Prefix.

With these tiny extensions, third party IP-Range libraries, based on the stdlib net/netip, are now possible without further bytes/bits fumbling.

API

func Range(p netip.Prefix) (first, last netip.Addr)

func Prefix(first, last netip.Addr) (prefix netip.Prefix, ok bool)

func Prefixes(first, last netip.Addr) []netip.Prefix

func AppendPrefixes(dst []netip.Prefix, first, last netip.Addr) []netip.Prefix

Future

Hopefully some day these tiny helper functions are added to netip by the stdlib maintainers, currently (2022) they refused the proposal.

Importpath

import "github.com/gaissmai/extnetip"

Documentation

Overview

extnetip is an extension to net/netip.

Some missing math functions are added to the closed private internals of netip using unsafe.

No further types are defined, only helper functions on existing net/netip types.

With these tiny extensions, third party IP-Range libraries, based on the stdlib net/netip, are now possible without further bytes/bits fumbling.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendPrefixes

func AppendPrefixes(dst []netip.Prefix, first, last netip.Addr) []netip.Prefix

AppendPrefixes is an append version of Prefixes. It appends the netip.Prefix entries to dst that covers the IP range from first to last.

func Prefix

func Prefix(first, last netip.Addr) (prefix netip.Prefix, ok bool)

Prefix returns the netip.Prefix from first to last and ok=true, if it can be presented exactly as such.

If first or last are not valid, in the wrong order or not exactly equal to one prefix, ok is false.

Example
package main

import (
	"fmt"
	"net/netip"

	"github.com/gaissmai/extnetip"
)

func main() {
	start := netip.MustParseAddr("fe80::")
	last := netip.MustParseAddr("fe80::7")
	pfx, ok := extnetip.Prefix(start, last)

	fmt.Println("OK:    ", ok)
	fmt.Println("Prefix:", pfx)
}
Output:

OK:     true
Prefix: fe80::/125

func Prefixes

func Prefixes(first, last netip.Addr) []netip.Prefix

Prefixes returns the set of netip.Prefix entries that covers the IP range from first to last.

If first or last are invalid, in the wrong order, or if they're of different address families, then Prefixes returns nil.

Prefixes necessarily allocates. See AppendPrefixes for a version that uses memory you provide.

Example
package main

import (
	"fmt"
	"net/netip"

	"github.com/gaissmai/extnetip"
)

func main() {
	start := netip.MustParseAddr("10.1.0.0")
	last := netip.MustParseAddr("10.1.13.233")
	pfxs := extnetip.Prefixes(start, last)

	fmt.Println("Prefixes:")
	for _, pfx := range pfxs {
		fmt.Println(pfx)
	}
}
Output:

Prefixes:
10.1.0.0/21
10.1.8.0/22
10.1.12.0/24
10.1.13.0/25
10.1.13.128/26
10.1.13.192/27
10.1.13.224/29
10.1.13.232/31

func Range

func Range(p netip.Prefix) (first, last netip.Addr)

Range returns the inclusive range of IP addresses that p covers.

If p is invalid, Range returns the zero values.

Example
package main

import (
	"fmt"
	"net/netip"

	"github.com/gaissmai/extnetip"
)

func main() {
	pfx := netip.MustParsePrefix("fe80::/10")
	start, last := extnetip.Range(pfx)

	fmt.Println("Start:", start)
	fmt.Println("Last: ", last)
}
Output:

Start: fe80::
Last:  febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff

Types

This section is empty.

Jump to

Keyboard shortcuts

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