Documentation ¶
Overview ¶
Package cidranger provides utility to store CIDR blocks and perform ip inclusion tests against it.
To create a new instance of the path-compressed trie:
ranger := NewPCTrieRanger()
To insert or remove an entry (any object that satisfies the RangerEntry interface):
_, network, _ := net.ParseCIDR("192.168.0.0/24") ranger.Insert(NewBasicRangerEntry(*network)) ranger.Remove(network)
If you desire for any value to be attached to the entry, simply create custom struct that satisfies the RangerEntry interface:
type RangerEntry interface { Network() net.IPNet }
To test whether an IP is contained in the constructed networks ranger:
// returns bool, error containsBool, err := ranger.Contains(net.ParseIP("192.168.0.1"))
To get a list of CIDR blocks in constructed ranger that contains IP:
// returns []RangerEntry, error entries, err := ranger.ContainingNetworks(net.ParseIP("192.168.0.1"))
To get a list of all IPv4/IPv6 rangers respectively:
// returns []RangerEntry, error entries, err := ranger.CoveredNetworks(*AllIPv4) entries, err := ranger.CoveredNetworks(*AllIPv6)
Index ¶
Constants ¶
This section is empty.
Variables ¶
var AllIPv4 = parseCIDRUnsafe("0.0.0.0/0")
AllIPv4 is a IPv4 CIDR that contains all networks
var AllIPv6 = parseCIDRUnsafe("0::0/0")
AllIPv6 is a IPv6 CIDR that contains all networks
var ErrInvalidNetworkInput = fmt.Errorf("Invalid network input")
ErrInvalidNetworkInput is returned upon invalid network input.
var ErrInvalidNetworkNumberInput = fmt.Errorf("Invalid network number input")
ErrInvalidNetworkNumberInput is returned upon invalid network input.
Functions ¶
func NewBredthIter ¶
func NewBredthIter(r Ranger) bredthRangerIter
A bredth-first iterator that returns all netblocks with a RangerEntry
func NewShallowBredthIter ¶
func NewShallowBredthIter(r Ranger) bredthRangerIter
A bredth-first iterator that will return only the largest netblocks with an entry
Types ¶
type Ranger ¶
type Ranger interface { // Insert a RangerEntry Insert(entry RangerEntry) error // Remove a network from the Ranger, returning its RangerEntry Remove(network net.IPNet) (RangerEntry, error) // ContainsNetwork returns true if the ip is covered in the Ranger Contains(ip net.IP) (bool, error) // ContainsNetwork returns true if the exact network is in the Ranger ContainsNetwork(network net.IPNet) (bool, error) // ContainingNetworks returns all RangerEntry that contain ip ContainingNetworks(ip net.IP) ([]RangerEntry, error) // CoveredNetworks returns all networks that are subnets of network CoveredNetworks(network net.IPNet) ([]RangerEntry, error) // Len returns number of entries in the Ranger Len() int // String returns a representation for visualization and debugging String() string // MissingNetworks determines the list of CIDR blocks out of the // address space which are not contained in the Ranger MissingNetworks() ([]net.IPNet, error) }
Ranger is an interface for cidr block containment lookups.
func DoRollupApply ¶
func DoRollupApply(r Ranger, f RollupApply) (Ranger, error)
Use a provided RollupApply and return the Ranger after modification
func NewIPv4PCTrieRanger ¶
func NewIPv4PCTrieRanger() Ranger
NewIPv4PCTrieRanger returns an IPv4-only Ranger for use-cases where the additional version checking and second Trie overhead is not desired.
func NewPCTrieRanger ¶
func NewPCTrieRanger() Ranger
NewPCTrieRanger returns a versionedRanger that supports both IPv4 and IPv6 using the path compressed trie implemention.
type RangerEntry ¶
RangerEntry is an interface for insertable entry into a Ranger.
func NewBasicRangerEntry ¶
func NewBasicRangerEntry(ipNet net.IPNet) RangerEntry
NewBasicRangerEntry returns a basic RangerEntry that only stores the network itself.
type RangerIter ¶
type RangerIter interface { Next() bool Get() RangerEntry Error() error }
RangerIter is an interface to use with an iterator-like pattern ri := NewBredthIter(ptrie)
for ri.Next() { entry := ri.Get() ... }
if err := ri.Error(); err != nil { ... }
While it's not really an iterator, this is exactly what bufio.Scanner does. Basically the idea is to have an Error() method which you call after iteration is complete to see whether iteration terminated because it was done or because an error was encountered midway through.
type RollupApply ¶
type RollupApply interface { // Decides if siblings should be rolled up CanRollup(child0 RangerEntry, child1 RangerEntry) bool // Rolls up siblings into a parent entry GetParentEntry(child0 RangerEntry, child1 RangerEntry, parentNet net.IPNet) RangerEntry }
Rollup apply Insert an entry at the parent of one or two children with entries, where the RangerEntry objects at those children meet the criteria of a rollup function
Directories ¶
Path | Synopsis |
---|---|
Example of how to extend github.com/hslatman/cidranger This adds ASN as a string field, along with methods to get the ASN and the CIDR as strings Thank you to yl2chen for his assistance and work on this library
|
Example of how to extend github.com/hslatman/cidranger This adds ASN as a string field, along with methods to get the ASN and the CIDR as strings Thank you to yl2chen for his assistance and work on this library |
Package net provides utility functions for working with IPs (net.IP).
|
Package net provides utility functions for working with IPs (net.IP). |