cidr

package module
v0.0.0-...-dd7a2bd Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotSorted   = errors.New("list is not sorted")
	ErrInvalidAddr = errors.New("addr is invalid")
)

Functions

This section is empty.

Types

type List

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

List is a list of netip.Prefix. It stores all netip.Prefix in one single slice and use binary search. It is suitable for large static cidr search. List converts IPv4 input netip.Prefix to IPv6 form. Therefore, all get functions that return netip.Prefix will always return a IPv6 form.

func NewList

func NewList() *List

NewList creates a *List.

func (*List) Append

func (list *List) Append(newPrefix netip.Prefix) bool

Append appends new netip.Prefix to the list. This modified the list. Caller must call List.Sort() before calling List.Contains() If newPrefix is invalid, Append returns false.

func (*List) Cap

func (list *List) Cap() int

func (*List) Contains

func (list *List) Contains(addr netip.Addr) (bool, error)

Contains reports whether the list includes the given netip.Addr. See also: GetPrefix.

func (*List) Copy

func (list *List) Copy() *List

func (*List) GetPrefix

func (list *List) GetPrefix(addr netip.Addr) (netip.Prefix, error)

GetPrefix get the netip.Prefix from list that includes the given netip.Addr. If list is not sorted or addr is not valid, an error will be returned. If no netip.Prefix was found, GetPrefix returns a zero netip.Prefix.

func (*List) GetPrefixes

func (list *List) GetPrefixes() []netip.Prefix

GetPrefixes returns a copy of list's inner []netip.Prefix.

func (*List) Len

func (list *List) Len() int

func (*List) Sort

func (list *List) Sort()

Sort sorts the list and removes overlapped prefixes.

func (*List) SortAndMerge

func (list *List) SortAndMerge()

SortAndMerge sorts the list, removes overlapped prefixes, and merges continuous prefixes.

Jump to

Keyboard shortcuts

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