NAME
Net::IP::LPM - Perl implementation of Longest Prefix Match algorithm
SYNOPSIS
use Net::IP::LPM;
my $lpm = Net::IP::LPM->new();
# add prefixes
$lpm->add('0.0.0.0/0', 'default');
$lpm->add('::/0', 'defaultv6');
$lpm->add('147.229.0.0/16', 'net1');
$lpm->add('147.229.3.0/24', 'net2');
$lpm->add('147.229.3.10/32', 'host3');
$lpm->add('147.229.3.11', 'host4');
$lpm->add('2001:67c:1220::/32', 'net16');
$lpm->add('2001:67c:1220:f565::/64', 'net26');
$lpm->add('2001:67c:1220:f565::1235/128', 'host36');
$lpm->add('2001:67c:1220:f565::1236', 'host46');
printf $lpm->lookup('147.229.100.100'); # returns net1
printf $lpm->lookup('147.229.3.10'); # returns host3
printf $lpm->lookup('2001:67c:1220::1');# returns net16
DESCRIPTION
The module Net::IP::LPM implements the Longest Prefix Matxh algo for both IPv4 and IPv6 protocols. The module uses Trie algo.
PERFORMANCE
The module is able to match ~ 1 mln. lookups per second on complete Internet BGP table (aprox 500 000 prefixes) on ordinary hardware (2.4GHz Xeon CPU). For more detail you can try make test on module source to check performance on your system. Module supports both IPv4 and IPv6 protocol.
CLASS METHODS
new - Class Constructor
$lpm = Net::IP::LPM->new( );
Constructs a new Net::IP::LPM object.
OBJECT METHODS
add - Add Prefix
$code = $lpm->add( $prefix, $value );
Adds prefix $prefix into database with value $value. Returns 1 if the prefix was added sucesfully. Returns 0 when some error happens (typically wrong address formating).
lookup - Lookup Address
$value = $lpm->$lookup( $address );
Lookups the prefix in the database and returns the value. If the prefix is not found or error ocured the undef value is returned.
Before lookups are performed the database have to be rebuilded by $lpm->rebuild()
operation.
lookup_raw - Lookup Address in raw format
$value = $lpm->lookup_raw( $address );
Same as $lpm->lookup
but takes $address in raw format (result of inet_ntop function). It is more effective than $lpm->lookup
, because convertion from text format is not nescessary.
info - Returns information about builded trie
$ref = $lpm->info();
Returns following items
ipv4_nodes_total - total number of allocated nodes in trie
ipv4_nodes_value - number of allocated nodes in trie that have stored some value
ipv4_trie_bytes - number of allocated for trie nodes (withnout data)
ipv6_ - same for IPv6
finish - Release all data in object
$lpm->finish();
SEE ALSO
There are also other implementation of Longest Prefix Matxh in perl. However most of them have some disadvantage (poor performance, lack of support for IPv6 or requires a lot of time for initial database building). However in some cases it might be usefull:
AUTHOR
Tomas Podermanski <tpoder@cis.vutbr.cz> Brno University of Technology
COPYRIGHT AND LICENSE
Copyright (C) 2012, Brno University of Technology
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.