Net::IP::LPM - Perl implementation of Longest Prefix Match algorithm
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
The module Net::IP::LPM implements the Longest Prefix Match algorithm to both protocols, IPv4 and IPv6. The module uses Trie algo.
The module is able to match ~ 1 mln. lookups per second to a complete Internet BGP table (approx. 500,000 prefixes) using a common hardware (2.4GHz Xeon CPU). For more detail, make a test on the module source to check its performance on your system. Module supports both, IPv4 and IPv6 protocols.
$lpm = Net::IP::LPM->new( );
Constructs a new Net::IP::LPM object.
$code = $lpm->add( $prefix, $value );
Adds a prefix $prefix into the database with value $value. Returns 1 if the prefix was added successfully. Returns 0 when an error occurs (typically the wrong address formating).
$value = $lpm->$lookup( $address );
Looks up the prefix in the database and returns the value. If the prefix is not found or an error occured, the undef value is returned.
Before lookups are performed the database has to be rebuilt by $lpm->rebuild() operation.
$lpm->rebuild()
$value = $lpm->lookup_raw( $address );
The same case as $lpm->lookup but it takes $address in raw format (result of the inet_ntop function). It is more effective than $lpm->lookup, because the conversion from text format is not necessary.
$lpm->lookup
$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 bytes allocated for trie nodes (without data) ipv6_ - the same for IPv6
$ref = $lpm->dump();
$lpm->finish();
There are also other implementations of the Longest Prefix Match in Perl. However, most of them have some disadvantages (poor performance, lack of support for IPv6 or require a lot of time for initial database building). However, in some cases it might be usefull:
Net::IPTrie
Net::IP::Match
Net::IP::Match::Trie
Net::IP::Match-XS
Net::CIDR::Lookup
Net::CIDR::Compare
Tomas Podermanski <tpoder@cis.vutbr.cz>, Martin Ministr <leadersmash@email.cz>, Brno University of Technology
Copyright (C) 2012, Brno University of Technology
This library is a free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Net::IP::LPM, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::IP::LPM
CPAN shell
perl -MCPAN -e shell install Net::IP::LPM
For more information on module installation, please visit the detailed CPAN module installation guide.