blib/lib/Net/IP/LPM.pm

filter: Statementstmt 75.6% 62 / 82 Branchbran 55.0% 11 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 76.1% 16 / 21 Podpod 70.0% 7 / 10 totaltotal 72.1% 96 / 133 CC 11 SCAR 13.6
CC
11
Cov
72%
format_addr
12.0
AUTOLOAD
8.2
add
6.0
CRAP 3.9
↑ Summary const-c.inc »
1 package Net::IP::LPM;
2  
3 #use 5.010001;
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 require Exporter;
9 #use AutoLoader;
10  
11 #use Socket qw( AF_INET );
12 #use Socket6 qw( inet_ntop inet_pton AF_INET6 );
13 3 ▶use if $] < 5.014000, Socket => qw(AF_INET);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use if $] < 5.014000, Socket6 => qw(inet_ntop inet_pton AF_INET6);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use if $] >= 5.014000, Socket => qw(inet_ntop inet_pton AF_INET6 AF_INET);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 #use Data::Dumper;
17  
18 #our @ISA = qw(DB_File);
19 3 our @ISA = qw();
20  
21 # Items to export into callers namespace by default. Note: do not export
22 # names by default without a very good reason. Use EXPORT_OK instead.
23 # Do not simply export all your public functions/methods/constants.
24  
25 # This allows declaration use Net::IP::LPM ':all';
26 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
27 # will save memory.
28 3 our %EXPORT_TAGS = ( 'all' => [ qw(
29  
30 ) ] );
31  
32 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
33  
34 3 our @EXPORT = qw(
35  
36 );
37  
38 3 our $VERSION = '1.13';
39 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=3 SCAR=21.1
40 # This AUTOLOAD is used to 'autoload' constants from the constant()
41 # XS function.
42  
43 0 my $constname;
44 0 our $AUTOLOAD;
45 0 ($constname = $AUTOLOAD) =~ s/.*:://;
46 0 ▶ croak "&Net::NfDump::constant not defined" if $constname eq 'constant';
Branch: 1 branch
BranchTrueFalseTotal
if $constname eq "constant" 0 0 0
47 0 my ($error, $val) = constant($constname);
48 0 ▶ if ($error) { croak $error; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
49 {
50 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
51 # Fixed between 5.005_53 and 5.005_61
52  
53 #XXX if ($] >= 5.00561) {
54 #XXX *$AUTOLOAD = sub () { $val };
55 #XXX }
56 #XXX else {
57 0 ▶ *$AUTOLOAD = sub { $val };
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 #XXX }
59 }
60 0 goto &$AUTOLOAD;
61 }
62  
63 3 require XSLoader;
64 3 XSLoader::load('Net::IP::LPM', $VERSION);
65  
66 # Preloaded methods go here.
67  
68 # Autoload methods go after =cut, and are processed by the autosplit program.
69  
70 # Below is stub documentation for your module. You'd better edit it!
71  
72 =head1 NAME
73  
74 Net::IP::LPM - Perl implementation of Longest Prefix Match algorithm
75  
76 =head1 SYNOPSIS
77  
78 use Net::IP::LPM;
79  
80 my $lpm = Net::IP::LPM->new();
81  
82 # add prefixes
83 $lpm->add('0.0.0.0/0', 'default');
84 $lpm->add('::/0', 'defaultv6');
85 $lpm->add('147.229.0.0/16', 'net1');
86 $lpm->add('147.229.3.0/24', 'net2');
87 $lpm->add('147.229.3.10/32', 'host3');
88 $lpm->add('147.229.3.11', 'host4');
89 $lpm->add('2001:67c:1220::/32', 'net16');
90 $lpm->add('2001:67c:1220:f565::/64', 'net26');
91 $lpm->add('2001:67c:1220:f565::1235/128', 'host36');
92 $lpm->add('2001:67c:1220:f565::1236', 'host46');
93  
94  
95 printf $lpm->lookup('147.229.100.100'); # returns net1
96 printf $lpm->lookup('147.229.3.10'); # returns host3
97 printf $lpm->lookup('2001:67c:1220::1');# returns net16
98  
99  
100 =head1 DESCRIPTION
101  
102 The module Net::IP::LPM implements the Longest Prefix Match algorithm
103 to both protocols, IPv4 and IPv6. The module uses Trie algo.
104  
105 =head1 PERFORMANCE
106  
107 The module is able to match ~ 1 mln. lookups
108 per second to a complete Internet BGP table (approx. 500,000 prefixes) using a common
109 hardware (2.4GHz Xeon CPU). For more detail, make a test on the module source
110 to check its performance on your system. Module supports both, IPv4 and IPv6 protocols.
111  
112 =head1 CLASS METHODS
113  
114  
115 =head2 new - Class Constructor
116  
117  
118 $lpm = Net::IP::LPM->new( );
119  
120 Constructs a new Net::IP::LPM object.
121  
122 =cut
123 26 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
124 26 my ($class, $dbfile) = @_;
125 26 my %h;
126 26 my $self = {};
127  
128 26 $self->{handle} = lpm_init();
129  
130 26 bless $self, $class;
131 26 return $self;
132 }
133  
134 # converts IPv4 and IPv6 address into common format
135 0 ▶sub format_addr {
Subroutine format_addr: not called CC=3 SCAR=24.8
Pod undocumented
136 0 my ($addr) = @_;
137  
138 0 ▶ if (!defined($addr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $addr) 0 0 0
139 0 return undef;
140 }
141  
142 0 ▶ if ((my $addr_bin = inet_pton(AF_INET, $addr))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $addr_bin = inet_pton(2, $addr)) { } 0 0 0
143 0 return $addr_bin;
144 } else {
145 0 return inet_pton(AF_INET6, $addr);
146 }
147  
148 }
149  
150 =head1 OBJECT METHODS
151  
152 =head2 add - Add Prefix
153  
154 $code = $lpm->add( $prefix, $value );
155  
156 Adds a prefix B<$prefix> into the database with value B<$value>. Returns 1 if
157 the prefix was added successfully. Returns 0 when an error occurs (typically the wrong address formating).
158  
159 =cut
160 458965 ▶sub add {
Subroutine add: called CC=6 SCAR=17.9
Pod documented
161 458965 my ($self, $prefix, $value) = @_;
162  
163 # printf "PPP: %s %s %s\n", $self->{handle}, $prefix, $value;
164 458965 my ($prefix_bin, $prefix_len);
165  
166 458965 ($prefix, $prefix_len) = split('/', $prefix);
167  
168 458965 ▶ if (! ($prefix_bin = inet_pton(AF_INET, $prefix)) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prefix_bin = inet_pton(2, $prefix)) 11959 447006 458965
169 11959 $prefix_bin = inet_pton(AF_INET6, $prefix);
170 }
171  
172 458965 ▶ croak "invalid prefix address '${prefix}'" unless defined $prefix_bin;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix_bin 0 458965 458965
173  
174 458965 ▶ if (!defined($prefix_len)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prefix_len) 5 458960 458965
175 5 ▶ if (length($prefix_bin) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $prefix_bin == 4) { } 3 2 5
176 3 $prefix_len = 32;
177 } else {
178 2 $prefix_len = 128;
179 }
180 }
181  
182 458965 ▶ croak "prefix length must be a decimal integer" unless $prefix_len =~ /^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix_len =~ /^[0-9]+$/ 11 458954 458965
183  
184 458954 return lpm_add_raw($self->{handle}, $prefix_bin, $prefix_len, $value);
185 }
186  
187 # legacy code
188 2 ▶sub rebuild {
Subroutine rebuild: called CC=1 SCAR=0.0
Pod undocumented
189 }
190  
191 =head2 lookup - Lookup Address
192  
193  
194 $value = $lpm->$lookup( $address );
195  
196 Looks up the prefix in the database and returns the value. If the prefix is
197 not found or an error occured, the undef value is returned.
198  
199 Before lookups are performed the database has to be rebuilt by C<$lpm-E<gt>rebuild()> operation.
200  
201 =cut
202  
203 9000101 ▶sub lookup {
Subroutine lookup: called CC=2 SCAR=6.9
Pod documented
204 9000101 my ($self, $addr) = @_;
205 9000101 my $addr_bin;
206  
207 9000101 ▶ if (! ($addr_bin = inet_pton(AF_INET, $addr)) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($addr_bin = inet_pton(2, $addr)) 41 9000060 9000101
208 41 $addr_bin = inet_pton(AF_INET6, $addr);
209 }
210  
211 9000101 return lpm_lookup_raw($self->{handle}, $addr_bin);
212 # return lpm_lookup($self->{handle}, $addr);
213 }
214  
215 =head2 lookup_raw - Lookup Address in raw format
216  
217  
218 $value = $lpm->lookup_raw( $address );
219  
220 The same case as C<$lpm-E<gt>lookup> but it takes $address in raw format (result of the inet_ntop function). It is
221 more effective than C<$lpm-E<gt>lookup>, because the conversion from text format is not
222 necessary.
223  
224 =cut
225  
226 9000072 ▶sub lookup_raw {
Subroutine lookup_raw: called CC=1 SCAR=0.0
Pod documented
227 # my ($self, $addr_bin) = @_;
228  
229 9000072 return lpm_lookup_raw($_[0]->{handle}, $_[1]);
230 }
231  
232 # legacy code
233  
234 72 ▶sub lookup_cache_raw {
Subroutine lookup_cache_raw: called CC=1 SCAR=0.0
Pod undocumented
235 # my ($self, $addr_bin) = @_;
236  
237 72 return lpm_lookup_raw($_[0]->{handle}, $_[1]);
238  
239 }
240  
241 =head2 info - Returns information about the built trie
242  
243 $ref = $lpm->info();
244  
245 Returns following items
246  
247 ipv4_nodes_total - total number of allocated nodes in trie
248 ipv4_nodes_value - number of allocated nodes in trie that have stored some value
249 ipv4_trie_bytes - number of bytes allocated for trie nodes (without data)
250 ipv6_ - the same for IPv6
251  
252 =cut
253  
254 1 ▶sub info {
Subroutine info: called CC=1 SCAR=0.0
Pod documented
255 1 my ($self) = @_;
256  
257 1 return lpm_info($self->{handle});
258 }
259  
260 =head2 dump - Return hash array reference containg all stored prefixes in the trie
261  
262 $ref = $lpm->dump();
263  
264 =cut
265  
266 3 ▶sub dump {
Subroutine dump: called CC=1 SCAR=0.0
Pod documented
267 3 my ($self) = @_;
268  
269 3 return lpm_dump($self->{handle});
270 }
271  
272 =head2 finish - Release all data in object
273  
274  
275 $lpm->finish();
276  
277 =cut
278 0 ▶sub finish {
Subroutine finish: not called CC=1 SCAR=6.9
Pod documented
279 0 my ($self) = @_;
280  
281 0 lpm_finish($self->{handle});
282 }
283  
284 26 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
285 26 my ($self) = @_;
286  
287 26 lpm_destroy($self->{handle});
288 }
289  
290 =head1 KNOWN ISSUES
291  
292 Threads are not supported.
293  
294 =cut
295  
296 # The object is a thin handle onto one process-wide C instance table; a clone
297 # made by threads->create would share the same handle and its DESTROY would
298 # free the parent's trie. Refuse to clone: in a new thread the object
299 # becomes undef instead of a second owner of the same native table.
300 0 ▶sub CLONE_SKIP { 1 }
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
301  
302 =head1 SEE ALSO
303  
304 There are also other implementations of the Longest Prefix Match in Perl. However,
305 most of them have some disadvantages (poor performance, lack of support for IPv6
306 or require a lot of time for initial database building). However, in some cases
307 it might be usefull:
308  
309 L<Net::IPTrie>
310  
311 L<Net::IP::Match>
312  
313 L<Net::IP::Match::Trie>
314  
315 L<Net::IP::Match-XS>
316  
317 L<Net::CIDR::Lookup>
318  
319 L<Net::CIDR::Compare>
320  
321 L<Net::Patricia>
322  
323 =head1 AUTHOR
324  
325 Tomas Podermanski <tpoder@cis.vutbr.cz>, Martin Ministr <leadersmash@email.cz>, Brno University of Technology
326  
327 Currently maintained by Robert Rothenberg <perl@rhizomnic.com>.
328  
329 =head1 COPYRIGHT AND LICENSE
330  
331 Copyright (C) 2012, 2026 Brno University of Technology
332  
333 This library is a free software; you can redistribute it and/or modify
334 it under the same terms as Perl itself.
335  
336  
337 =cut
338  
339 3 1;
340 __END__
↑ Summary const-c.inc »