blib/lib/Net/IP/LPM.pm

filter: Statementstmt 75.9% 60 / 79 Branchbran 50.0% 8 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 16 / 20 Podpod 70.0% 7 / 10 totaltotal 72.8% 91 / 125 CC 9 SCAR 12.6
CC
9
Cov
72%
format_addr
12.0
AUTOLOAD
8.2
add
4.0
CRAP 3.5
↑ Summary const-c.inc »
1 package Net::IP::LPM;
2  
3 #use 5.010001;
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 require Exporter;
9 #use AutoLoader;
10  
11 #use Socket qw( AF_INET );
12 #use Socket6 qw( inet_ntop inet_pton AF_INET6 );
13 2 ▶use if $] < 5.014000, Socket => qw(AF_INET);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use if $] < 5.014000, Socket6 => qw(inet_ntop inet_pton AF_INET6);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶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 2 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 2 our %EXPORT_TAGS = ( 'all' => [ qw(
29  
30 ) ] );
31  
32 2 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
33  
34 2 our @EXPORT = qw(
35  
36 );
37  
38 2 our $VERSION = '1.11';
39 sub AUTOLOAD {
40 # This AUTOLOAD is used to 'autoload' constants from the constant()
41 # XS function.
42  
43 0 ▶ my $constname;
Subroutine AUTOLOAD: not called CC=3 SCAR=21.1
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 2 ▶ 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 2 require XSLoader;
64 2 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 sub new {
124 5 ▶ my ($class, $dbfile) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
125 5 my %h;
126 5 my $self = {};
127  
128 5 $self->{handle} = lpm_init();
129  
130 5 bless $self, $class;
131 5 return $self;
132 }
133  
134 # converts IPv4 and IPv6 address into common format
135 sub format_addr {
136 0 ▶ my ($addr) = @_;
Subroutine format_addr: not called CC=3 SCAR=24.8
Pod undocumented
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 sub add {
161 458940 ▶ my ($self, $prefix, $value) = @_;
Subroutine add: called CC=4 SCAR=13.9
Pod documented
162  
163 # printf "PPP: %s %s %s\n", $self->{handle}, $prefix, $value;
164 458940 my ($prefix_bin, $prefix_len);
165  
166 458940 ($prefix, $prefix_len) = split('/', $prefix);
167  
168 458940 ▶ if (! ($prefix_bin = inet_pton(AF_INET, $prefix)) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prefix_bin = inet_pton(2, $prefix)) 11953 446987 458940
169 11953 $prefix_bin = inet_pton(AF_INET6, $prefix);
170 }
171  
172 458940 ▶ if (!defined($prefix_len)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prefix_len) 4 458936 458940
173 4 ▶ if (length($prefix_bin) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $prefix_bin == 4) { } 2 2 4
174 2 $prefix_len = 32;
175 } else {
176 2 $prefix_len = 128;
177 }
178 }
179  
180 458940 return lpm_add_raw($self->{handle}, $prefix_bin, $prefix_len, $value);
181 }
182  
183 # legacy code
184 ▶sub rebuild {
Subroutine rebuild: called CC=1 SCAR=0.0
Pod undocumented
185 }
186  
187 =head2 lookup - Lookup Address
188  
189  
190 $value = $lpm->$lookup( $address );
191  
192 Looks up the prefix in the database and returns the value. If the prefix is
193 not found or an error occured, the undef value is returned.
194  
195 Before lookups are performed the database has to be rebuilt by C<$lpm-E<gt>rebuild()> operation.
196  
197 =cut
198  
199 sub lookup {
200 9000074 ▶ my ($self, $addr) = @_;
Subroutine lookup: called CC=2 SCAR=6.9
Pod documented
201 9000074 my $addr_bin;
202  
203 9000074 ▶ if (! ($addr_bin = inet_pton(AF_INET, $addr)) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($addr_bin = inet_pton(2, $addr)) 35 9000039 9000074
204 35 $addr_bin = inet_pton(AF_INET6, $addr);
205 }
206  
207 9000074 return lpm_lookup_raw($self->{handle}, $addr_bin);
208 # return lpm_lookup($self->{handle}, $addr);
209 }
210  
211 =head2 lookup_raw - Lookup Address in raw format
212  
213  
214 $value = $lpm->lookup_raw( $address );
215  
216 The same case as C<$lpm-E<gt>lookup> but it takes $address in raw format (result of the inet_ntop function). It is
217 more effective than C<$lpm-E<gt>lookup>, because the conversion from text format is not
218 necessary.
219  
220 =cut
221  
222 sub lookup_raw {
223 # my ($self, $addr_bin) = @_;
224  
225 9000072 ▶ return lpm_lookup_raw($_[0]->{handle}, $_[1]);
Subroutine lookup_raw: called CC=1 SCAR=0.0
Pod documented
226 }
227  
228 # legacy code
229  
230 sub lookup_cache_raw {
231 # my ($self, $addr_bin) = @_;
232  
233 72 ▶ return lpm_lookup_raw($_[0]->{handle}, $_[1]);
Subroutine lookup_cache_raw: called CC=1 SCAR=0.0
Pod undocumented
234  
235 }
236  
237 =head2 info - Returns information about the built trie
238  
239 $ref = $lpm->info();
240  
241 Returns following items
242  
243 ipv4_nodes_total - total number of allocated nodes in trie
244 ipv4_nodes_value - number of allocated nodes in trie that have stored some value
245 ipv4_trie_bytes - number of bytes allocated for trie nodes (without data)
246 ipv6_ - the same for IPv6
247  
248 =cut
249  
250 sub info {
251 1 ▶ my ($self) = @_;
Subroutine info: called CC=1 SCAR=0.0
Pod documented
252  
253 1 return lpm_info($self->{handle});
254 }
255  
256 =head2 dump - Return hash array reference containg all stored prefixes in the trie
257  
258 $ref = $lpm->dump();
259  
260 =cut
261  
262 sub dump {
263 3 ▶ my ($self) = @_;
Subroutine dump: called CC=1 SCAR=0.0
Pod documented
264  
265 3 return lpm_dump($self->{handle});
266 }
267  
268 =head2 finish - Release all data in object
269  
270  
271 $lpm->finish();
272  
273 =cut
274 sub finish {
275 0 ▶ my ($self) = @_;
Subroutine finish: not called CC=1 SCAR=6.9
Pod documented
276  
277 0 lpm_finish($self->{handle});
278 }
279  
280 sub DESTROY {
281 5 ▶ my ($self) = @_;
Subroutine DESTROY: called CC=1 SCAR=0.0
282  
283 5 lpm_destroy($self->{handle});
284 }
285  
286 =head1 SEE ALSO
287  
288 There are also other implementations of the Longest Prefix Match in Perl. However,
289 most of them have some disadvantages (poor performance, lack of support for IPv6
290 or require a lot of time for initial database building). However, in some cases
291 it might be usefull:
292  
293 L<Net::IPTrie>
294  
295 L<Net::IP::Match>
296  
297 L<Net::IP::Match::Trie>
298  
299 L<Net::IP::Match-XS>
300  
301 L<Net::CIDR::Lookup>
302  
303 L<Net::CIDR::Compare>
304  
305 L<Net::Patricia>
306  
307 =head1 AUTHOR
308  
309 Tomas Podermanski <tpoder@cis.vutbr.cz>, Martin Ministr <leadersmash@email.cz>, Brno University of Technology
310  
311 Currently maintained by Robert Rothenberg <perl@rhizomnic.com>.
312  
313 =head1 COPYRIGHT AND LICENSE
314  
315 Copyright (C) 2012, 2026 Brno University of Technology
316  
317 This library is a free software; you can redistribute it and/or modify
318 it under the same terms as Perl itself.
319  
320  
321 =cut
322  
323 2 1;
324 __END__
↑ Summary const-c.inc »