Net::CIDR::Lookup
This class implements a lookup table indexed by IPv4 networks or hosts.
Addresses are accepted in numeric form (integer with separate netbits argument), as strings in CIDR notation or as IP address ranges
Overlapping or adjacent networks are automatically coalesced if their associated values are equal.
The table is implemented as a binary tree so lookup and insertion take O(log n) time.
Methods generally return a true value on success and undef on error. In the latter case, an error message will be available in $Net::CIDR::Lookup::errstr
undef
$Net::CIDR::Lookup::errstr
use Net::CIDR::Lookup; $cidr = Net::CIDR::Lookup->new; $cidr->add("192.168.42.0/24",1); # Add first network, value 1 $cidr->add_num(167772448,27,2); # 10.0.1.32/27 => 2 $cidr->add("192.168.43.0/24",1); # Automatic coalescing to a /23 $cidr->add("192.168.41.0/24",2); # Stays separate due to different value $cidr->add("192.168.42.128/25",2); # Error: overlaps with different value $h = $cidr->dump; # Convert tree to a hash print "$k => $v\n" while(($k,$v) = each %$h); # Output (order may vary): # 192.168.42.0/23 => 1 # 10.0.1.32/27 => 2 # 192.168.41.0/24 => 2 $cidr->walk(sub { my ($addr, $bits, $val) = @_; print join('.', unpack 'C*', pack 'N', $addr), "/$bits => $val\n" } ); # Output (fixed order): # 10.0.1.32/27 => 2 # 192.168.41.0/24 => 2 # 192.168.42.0/23 => 1 $cidr->clear; # Remove all entries $cidr->add_range('1.2.3.11 - 1.2.4.234', 42); # Add a range of addresses, automatically split into CIDR blocks $h = $cidr->dump; print "$k => $v\n" while(($k,$v) = each %$h); # Output (order may vary): # 1.2.4.128/26 => 42 # 1.2.3.32/27 => 42 # 1.2.3.64/26 => 42 # 1.2.4.234/32 => 42 # 1.2.4.0/25 => 42 # 1.2.3.12/30 => 42 # 1.2.3.128/25 => 42 # 1.2.3.16/28 => 42 # 1.2.4.224/29 => 42 # 1.2.4.232/31 => 42 # 1.2.3.11/32 => 42 # 1.2.4.192/27 => 42
Replaced the simplistic list-based CIDR block splitting function with bit-fiddling for about a threefold speedup of add_num_range and slightly less in add_range.
add_num_range
add_range
Recursive merging-up up of blocks during add_* works now. If e.g. you had a /24 and an adjacent /25 net with the same value before, adding a new /25 would have merged the new block with the existing /25, resulting in two adjacent /24s with the same value because only single-level merging was possible. Now the two will be merged to a single /23.
add_*
Removed some redundant tests and added new ones.
Removed some leftover debug code.
Some small fixes/improvements like stricter range checking in add_range
Arguments: none
Return Value: new object
Arguments: $cidr, $value
$cidr
$value
Return Value: true for successful completion, undef otherwise
Adds VALUE to the tree under the key CIDR. CIDR must be a string containing an IPv4 address followed by a slash and a number of network bits. Bits to the right of this mask will be ignored.
Arguments: $range, $value
$range
Adds VALUE to the tree for each address included in RANGE which must be a hyphenated range of IP addresses in dotted-quad format (e.g. "192.168.0.150-192.168.10.1") and with the first address being numerically smaller the second. This range will be split up into as many CIDR blocks as necessary (algorithm adapted from a script by Dr. Liviu Daia).
Arguments: $address, $bits, $value
$address
$bits
Like add() but accepts address and bits as separate integer arguments instead of a string.
add()
Arguments: $start, $end, $value
$start
$end
Like add_range() but accepts addresses as separate integer arguments instead of a range string.
add_range()
Arguments: $address
Return Value: value assoiated with this address or undef
Looks up an address and returns the value associated with the network containing it. So far there is no way to tell which network that is though.
Like lookup() but accepts the address in integer form.
lookup()
Return Value: $hashref
$hashref
Returns a hash representation of the tree with keys being CIDR-style network addresses.
Arguments: $coderef to call for each tree entry. Callback arguments are:
$coderef
The network address in integer form
The current CIDR block's number of network bits
The value associated with this block
Return Value: nothing useful
Remove all entries from the tree.
I didn't need deletions yet and deleting parts of a CIDR block is a bit more complicated than anything this class does so far, so it's not implemented.
Storing an undef value does not work and yields an error. This would be relatively easy to fix at the cost of some memory so that's more a design decision.
Using a package-global for error reporting was an incredibly stupid idea initially. This will change in the next version.
IPv6 is not supported, which is a shame.
Matthias Bethke <matthias@towiski.de> while working for 1&1 Internet AG
Licensed unter the Artistic License 2.0
This module's methods are based loosely on those of Net::CIDR::Lite
Net::CIDR::Lite
To install Net::CIDR::Lookup, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::CIDR::Lookup
CPAN shell
perl -MCPAN -e shell install Net::CIDR::Lookup
For more information on module installation, please visit the detailed CPAN module installation guide.