NAME
Net::IPTrie - Perl module for building IPv4 and IPv6 address space hierarchies fast
SYNOPSIS
use Net::IPTrie;
my $tr = Net::IPTrie->new(version=>4); # IPv4
my $n = $tr->add(address=>'10.0.0.0/8', prefix=>8);
my $a = $tr->add(address=>'10.0.0.1', data=>$data) # prefix defaults to 32
$a->parent->address eq $n->address and print "$a is within $n";
# Addresses can be provided in integer (decimal) format
# 10.0.0.7 == 167772167
my $b = $tr->add(iaddress=>'167772167', data=>'blah');
if ( my $c = $tr->find(address=>"10.0.0.7" ) {
print $c->data; # should print "blah"
}
# If the IP does not exist:
my $d = $tr->find(address=>"10.0.0.8")
print $d->address; # should print "10.0.0.0", which is the closest parent block
DESCRIPTION
This module uses a radix tree (or trie) to quickly build the hierarchy of a given address space
(both IPv4 and IPv6). This allows the user to perform fast subnet or routing lookups.
It is implemented exclusively in Perl.
CLASS METHODS
new - Class Constructor
Arguments:
Hash with the following keys:
version - IP version (4|6)
Returns:
New Net::IPTrie object
Examples:
my $tr = Net::IPTrie->new(version=>4);
INSTANCE METHODS
version - Set or get IP version (4 or 6)
Arguments:
IP version (4 or 6) - optional
Returns:
version (4 or 6)
Examples:
print $tr->version;
size - Set or get IP size (32 or 128)
Arguments:
Size (32 or 128) - optional
Returns:
Address size in bits (32 or 128)
Examples:
print $tr->size;
find - Find an IP object in the trie
If the given IP does not exist, there are two options:
a) If the "deep" flag is off, the closest covering IP block is returned. This is
the default behavior.
b) If the "deep" flag is on, the node where the searched IP should be inserted is returned.
This is basically only useful for the "add" method.
Arguments:
Hash with following keys:
address - String (i.e. "10.0.0.1") address
iaddress - Integer (i.e. "167772161") address, IPv4 or IPv6.
prefix - Prefix Length (optional - defaults to host mask)
deep - Flag (optional). If not found, return the node where object should be inserted.
Returns:
Net::IPTrie::Node object.
Examples:
my $n = $tr->find("10.0.0.1", 32);
add - Add an IP to the trie
Arguments:
Hash with following keys:
address - String address, IPv4 or IPv6 (i.e. "10.0.0.1")
iaddress - Integer address, IPv4 or IPv6 (i.e. "167772161")
prefix - Prefix Length (optional - defaults to host mask)
data - Data (optional)
Returns:
New Net::IPTrie::Node object
Examples:
my $n = $tr->add(address=>"10.0.0.1", prefix=>32, data=>\$data);
traverse - Traverse every node in the tree
Arguments:
root - node object (optional - defaults to tree root)
code - coderef (will be passed the Net::IPTrie::Node object to act upon)
mode - (depth_first only, for now)
Returns:
Number of actual IP nodes visited
Examples:
# Store all IP nodes in an array, ordered.
my $list = ();
my $code = sub { push @$list, shift @_ };
my $count = $tr->traverse(code=>$code);
AUTHOR
Carlos Vicente <cvicente@cpan.org
>
SEE ALSO
Net::IPTrie::Node
Net::Patricia relies on a C library and currently does not support IPv6. It also contains known bugs. I have not done any performance comparisons.
LICENCE AND COPYRIGHT
Copyright (c) 2007-2010, Carlos Vicente <cvicente@cpan.org
>. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.