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 ¶
This section is empty.
Types ¶
type Ranger ¶
type Ranger interface {
Insert(entry RangerEntry) error
Remove(network net.IPNet) (RangerEntry, error)
Contains(ip net.IP) (bool, error)
ContainingNetworks(ip net.IP) ([]RangerEntry, error)
// returns the list of RangerEntry(s) covered by "network".
// i.e. CoveredNetworks("1.1.1.0/24") would return "1.1.1.1/32" if it's in the cidranger list,
// but CoveredNetworks("1.1.1.0/24") would never return "1.1.0.0/16".
CoveredNetworks(network net.IPNet) ([]RangerEntry, error)
// returns the list of RangerEntry(s) covering "network".
// i.e. CoveringNetworks("1.1.1.0/24") would return "1.1.0.0/16" if it's in the cidranger list,
// but CoveringNetworks("1.1.1.0/24") would never return "1.1.1.1/32".
CoveringNetworks(network net.IPNet) ([]RangerEntry, error)
// returns the list of RangerEntry(s) that are either covering "network" or covered by "network".
// i.e. CoveringOrCoveredNetworks("1.1.1.0/24") would return "1.1.0.0/16" if it's in the cidranger list,
// and CoveringOrCoveredNetworks("1.1.1.0/24") would return "1.1.1.1/32" if it's also in the cidranger list.
CoveringOrCoveredNetworks(network net.IPNet) ([]RangerEntry, error)
Len() int
}
Ranger is an interface for cidr block containment lookups.
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.

