extnetip

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2024 License: MIT Imports: 4 Imported by: 7

README

package extnetip

Go Reference GitHub release (latest SemVer) CI Go Report Card Coverage Status Stand With Ukraine

Package extnetip is an extension to net/netip with a few missing but important auxiliary functions for converting IP-prefixes to IP-ranges and vice versa.

With these extensions to net/netip, third-party IP-range libraries become easily possible.

API

import "github.com/gaissmai/extnetip"

func Range(p netip.Prefix) (first, last netip.Addr)
func Prefix(first, last netip.Addr) (prefix netip.Prefix, ok bool)
func All(first, last netip.Addr) iter.Seq[netip.Prefix]

// Deprecated: func Prefixes(first, last netip.Addr) []netip.Prefix
// Deprecated: func PrefixesAppend(dst []netip.Prefix, first, last netip.Addr) []netip.Prefix

Future

Hopefully some day these needed helper functions are added to netip by the stdlib maintainers.

Documentation

Overview

Package extnetip is an extension to net/netip with a few missing but important auxiliary functions for converting IP-prefixes to IP-ranges and vice versa.

The functions are effectively performed in uint128 space, no conversions from/to bytes are performed.

With these extensions to net/netip, third-party IP-range libraries become easily possible.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v1.1.0

func All(first, last netip.Addr) iter.Seq[netip.Prefix]

All returns an iterator over the set of prefixes that covers the range from [first, last].

If first or last are not valid, in the wrong order or not of the same version, the set is empty.

Example
package main

import (
	"fmt"
	"net/netip"

	"github.com/gaissmai/extnetip"
)

func main() {
	first := netip.MustParseAddr("10.1.0.0")
	last := netip.MustParseAddr("10.1.13.233")

	fmt.Println("Prefixes:")
	for pfx := range extnetip.All(first, last) {
		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 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() {
	first := netip.MustParseAddr("fe80::")
	last := netip.MustParseAddr("fe80::7")

	pfx, ok := extnetip.Prefix(first, last)

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

	fmt.Println()

	first = netip.MustParseAddr("10.0.0.1")
	last = netip.MustParseAddr("10.0.0.19")

	pfx, ok = extnetip.Prefix(first, last)

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

}
Output:

OK:     true
Prefix: fe80::/125

OK:     false
Prefix: invalid Prefix

func Prefixes deprecated

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.

Deprecated: Prefixes is deprecated. Use the iterator version All instead.

func PrefixesAppend deprecated added in v0.3.0

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

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

Deprecated: PrefixesAppend is deprecated. Use the iterator version All instead.

func Range

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

Range returns the inclusive range of IP addresses [first, last] 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")
	first, last := extnetip.Range(pfx)

	fmt.Println("First:", first)
	fmt.Println("Last: ", last)
}
Output:

First: 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