This file is indexed.

/usr/lib/perl5/Net/Patricia.pm is in libnet-patricia-perl 1.19-1build2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#  Net::Patricia - Patricia Trie perl module for fast IP address lookups
#  Copyright (C) 2000-2005  Dave Plonka
#  Copyright (C) 2009       Dave Plonka & Philip Prindeville
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Dave Plonka <plonka@doit.wisc.edu>
# Philip Prindeville <philipp@redfish-solutions.com>

package Net::Patricia;

use strict;
use warnings;

require 5.008;

use version;
use Carp;
use vars qw($VERSION @ISA @EXPORT);
use Socket qw(AF_INET AF_INET6);

BEGIN {
  require Exporter;
  require DynaLoader;
  @ISA = qw(Exporter DynaLoader);
  @EXPORT = qw(AF_INET AF_INET6);
}

'$Revision: 1.19 $' =~ m/(\d+)\.(\d+)((_\d+)|)/ && ( $VERSION = "$1.$2$3");

bootstrap Net::Patricia $VERSION;

sub new {
  my ($class, $type) = @_;

  $type ||= AF_INET;

  if ($type == AF_INET) {
    return bless _new(32), 'Net::Patricia::AF_INET';
  }

  if ($type == AF_INET6) {
    return bless _new(128), 'Net::Patricia::AF_INET6';
  }

  croak "new: unimplemented type";
}

##
## Compat functions
##

sub _ip_bits {
  my ($self, $str) = @_;
  my $bits;

  if (ref ($self) eq 'Net::Patricia::AF_INET6') { 
	$bits = ($str =~ s|/(\d+)$||) ? $1 : 128; 
  } else { 
	$bits = ($str =~ s|/(\d+)$||) ? $1 : 32; 
  }
  ($str,$bits);
}

sub add_string {
  croak "add_string: wrong number of args" if (@_ < 2 || @_ > 3);
  my ($self,$str,$data) = @_;
  $data = $str unless @_ > 2;
  $self->add($self->_ip_bits($str),$data);
}

sub match_string {
  croak "match_string: wrong number of args" if (@_ != 2);
  my ($self,$str) = @_;
  $self->match($self->_ip_bits($str))
}

sub match_exact_string {
  croak "match_exact_string: wrong number of args" if (@_ != 2);
  my ($self,$str) = @_;
  $self->exact($self->_ip_bits($str))
}

sub match_exact_integer {
  shift->exact_integer(@_)
}

sub remove_string {
  croak "remove_string: wrong number of args" if (@_ != 2);
  my ($self,$str) = @_;
  $self->remove($self->_ip_bits($str))
}

BEGIN {
  eval {
    my $class = 'Net::CIDR::Lite';
    eval "require $class";
  };
  last if (@_);

  sub add_cidr {
    croak "add_cidr: wrong number of args" if (@_ != 3);
    my ($self, $range, $data) = @_;
    my $cidr = Net::CIDR::Lite->new();
    $cidr->add_range($range);

    my @list = ();
    for ($cidr->list()) {
      push(@list, $_) if ($self->add_string($_, $data));
    }
    @list;
  }

  sub remove_cidr {
    croak "remove_cidr: wrong number of args" if (@_ != 2);
    my ($self, $range) = @_;
    my $cidr = Net::CIDR::Lite->new();
    $cidr->add_range($range);

    my @list = ();
    for ($cidr->list()) {
      push(@list, $_) if ($self->remove_string($_));
    }
    @list;
  }
}

##
## AF_INET
##

package Net::Patricia::AF_INET;

use Carp;
use Socket qw(AF_INET inet_aton inet_ntoa);
use vars qw(@ISA @EXPORT);

BEGIN {
  require Exporter;
  require DynaLoader;
  @ISA = qw(Exporter DynaLoader Net::Patricia);
  @EXPORT = qw(AF_INET);
}

sub add {
  croak "add: wrong number of args" if (@_ < 2 || @_ > 4);
  my ($self, $ip, $bits, $data) = @_;
  $data = (defined $bits ? "$ip/$bits" : $ip) if (@_ < 4);
  my $packed = inet_aton($ip);
  croak("invalid key") unless (defined $packed);
  $bits = 32 if (@_ < 3);
  $self->SUPER::_add(AF_INET, $packed, $bits, $data);
}

sub add_integer {
  croak "add_integer: wrong number of args" if (@_ < 2 || @_ > 4);
  my ($self, $num, $bits, $data) = @_;
  my $packed = pack("N", $num);
  my $ip = inet_ntoa($packed);
  croak("invalid address") unless (defined $ip);
  $data = (defined $bits ? "$ip/$bits" : $ip) if (@_ < 4);
  $bits = 32 if (@_ < 3);
  $self->SUPER::_add(AF_INET, $packed, $bits, $data);
}

sub match_integer {
  croak "match_integer: wrong number of args" if (@_ < 2 || @_ > 3);
  my ($self, $num, $bits) = @_;
  $bits = 32 if (@_ < 3);
  $self->SUPER::_match(AF_INET, pack("N",$num), $bits);
}

sub exact_integer {
  croak "exact_integer: wrong number of args" if (@_ < 2 || @_ > 3);
  my ($self, $num, $bits) = @_;
  $bits = 32 if (@_ < 3);
  $self->SUPER::_exact(AF_INET, pack("N",$num), $bits);
}

sub match {
  croak "match: wrong number of args" if (@_ < 2 || @_ > 3);
  my ($self, $ip, $bits) = @_;
  my $packed = inet_aton($ip);
  croak("invalid key") unless (defined $packed);
  $bits = 32 if (@_ < 3);
  $self->SUPER::_match(AF_INET, $packed, $bits);
}

sub exact {
  croak "exact: wrong number of args" if (@_ < 2 || @_ > 3);
  my ($self, $ip, $bits) = @_;
  my $packed = inet_aton($ip);
  croak("invalid key") unless (defined $packed);
  $bits = 32 if (@_ < 3);
  $self->SUPER::_exact(AF_INET, $packed, $bits);
}

sub remove {
  croak "remove: wrong number of args" if (@_ < 2 || @_ > 3);
  my ($self, $ip, $bits) = @_;
  my $packed = inet_aton($ip);
  croak("invalid key") unless (defined $packed);
  $bits = 32 if (@_ < 3);
  $self->SUPER::_remove(AF_INET, $packed, $bits);
}

sub remove_integer {
  croak "remote_integer: wrong number of args" if (@_ < 2 || @_ > 3);
  my ($self, $num, $bits) = @_;
  $bits = 32 if (@_ < 3);
  $self->SUPER::_remove(AF_INET, pack("N",$num), $bits);
}

##
## AF_INET6
##

package Net::Patricia::AF_INET6;

use Carp;
use Socket qw(AF_INET6);
use Socket6 qw(inet_pton inet_ntop);
use vars qw(@ISA @EXPORT);

BEGIN {
  require Exporter;
  require DynaLoader;
  @ISA = qw(Exporter DynaLoader Net::Patricia);
  @EXPORT = qw(AF_INET6);
}

sub add {
  croak "add: wrong number of args" if (@_ < 2 || @_ > 4);
  my ($self, $ip, $bits, $data) = @_;
  $data = (defined $bits ? "$ip/$bits" : $ip) if (@_ < 3);
  my $packed = inet_pton(AF_INET6, $ip);
  croak("invalid key") unless (defined $packed);
  $bits = 128 if (@_ < 4);
  $self->SUPER::_add(AF_INET6, $packed, $bits, $data);
}

sub add_integer {
  croak "add_integer: wrong number of args" if (@_ < 2 || @_ > 4);
  my ($self, $num, $bits, $data) = @_;
  my $packed = pack("N", $num);
  my $ip = inet_ntop(AF_INET6, $packed);
  croak("invalid address") unless (defined $ip);
  $data = (defined $bits ? "$ip/$bits" : $ip) if (@_ < 3);
  $bits = 128 if (@_ < 4);
  $self->SUPER::_add(AF_INET6, $packed, $bits, $data);
}

sub match_integer {
  croak "match_integer: wrong number of args" if (@_ < 2 || @_ > 3);
  my ($self, $num, $bits) = @_;
  $bits = 128 if (@_ < 3);
  $self->SUPER::_match(AF_INET6, pack("N",$num), $bits);
}

sub exact_integer {
  croak "exact_integer: wrong number of args" if (@_ < 2 || @_ > 3);
  my ($self, $num, $bits) = @_;
  $bits = 128 if (@_ < 3);
  $self->SUPER::_exact(AF_INET6, pack("N",$num), $bits);
}

sub match {
  croak "match: wrong number of args" if (@_ < 2 || @_ > 3);
  my ($self, $ip, $bits) = @_;
  my $packed = inet_pton(AF_INET6, $ip);
  croak("invalid key") unless (defined $packed);
  $bits = 128 if (@_ < 3);
  $self->SUPER::_match(AF_INET6, $packed, $bits);
}

sub exact {
  croak "exact: wrong number of args" if (@_ < 2 || @_ > 3);
  my ($self, $ip, $bits) = @_;
  my $packed = inet_pton(AF_INET6, $ip);
  croak("invalid key") unless (defined $packed);
  $bits = 128 if (@_ < 3);
  $self->SUPER::_exact(AF_INET6, $packed, $bits);
}

sub remove {
  croak "remove: wrong number of args" if (@_ < 2 || @_ > 3);
  my ($self, $ip, $bits) = @_;
  my $packed = inet_pton(AF_INET6, $ip);
  croak("invalid key") unless (defined $packed);
  $bits = 128 if (@_ < 3);
  $self->SUPER::_remove(AF_INET6, $packed, $bits);
}

sub remove_integer {
  croak "remote_integer: wrong number of args" if (@_ < 2 || @_ > 3);
  my ($self, $num, $bits) = @_;
  $bits = 128 if (@_ < 3);
  $self->SUPER::_remove(AF_INET6, pack("N",$num), $bits);
}

1;
__END__
# Below is the stub of documentation for your module. You better edit it!

=head1 NAME

Net::Patricia - Patricia Trie perl module for fast IP address lookups

=head1 SYNOPSIS

  use Net::Patricia;

  my $pt = new Net::Patricia;

  $pt->add_string('127.0.0.0/8', \$user_data);
  $pt->match_string('127.0.0.1');
  $pt->match_exact_string('127.0.0.0');
  $pt->match_integer(2130706433); # 127.0.0.1
  $pt->match_exact_integer(2130706432, 8); # 127.0.0.0
  $pt->remove_string('127.0.0.0/8');
  $pt->climb(sub { print "climbing at node $_[0]\n" });

  undef $pt; # automatically destroys the Patricia Trie

=head1 DESCRIPTION

This module uses a Patricia Trie data structure to quickly
perform IP address prefix matching for applications such as IP subnet,
network or routing table lookups.  The data structure is based on a
radix tree using a radix of two, so sometimes you see patricia
implementations called "radix" as well.  The term "Trie" is derived
from the word "retrieval" but is pronounced like "try".  Patricia
stands for "Practical Algorithm to Retrieve Information Coded as
Alphanumeric", and was first suggested for routing table lookups by Van
Jacobsen.  Patricia Trie performance characteristics are well-known as
it has been employed for routing table lookups within the BSD kernel
since the 4.3 Reno release.

The BSD radix code is thoroughly described in "TCP/IP Illustrated,
Volume 2" by Wright and Stevens and in the paper ``A Tree-Based Packet
Routing Table for Berkeley Unix'' by Keith Sklower.

=head1 METHODS

=over 4

=item B<new> - create a new Net::Patricia object

   $pt = new Net::Patricia;

This is the class' constructor - it returns a C<Net::Patricia> object
upon success or undef on failure.  The constructor takes an
optional argument (of AF_INET or AF_INET6, defaulting to the former),
and creates a tree with address and mask values of that type as keys.

The C<Net::Patricia> object will be destroyed automatically when
there are no longer any references to it.

=item B<add_string>

  $pt->add_string(key_string[,user_data]);

The first argument, key_string, is a network or subnet specification in
canonical form, e.g. "10.0.0.0/8", where the number after the slash
represents the number of bits in the netmask.  If no mask width is
specified, the longest possible mask is assumed, i.e. 32 bits for
AF_INET addresses.

The second argument, user_data, is optional.  If supplied, it should be
a SCALAR value (which may be a perl reference) specifying the user data
that will be stored in the Patricia Trie node.  Subsequently, this
value will be returned by the match methods described below to indicate
a successful search.  Remember that perl references and objects are
represented as SCALAR values and therefore the user data can be
complicated data objects.

If no second argument is passed, the key_string will be stored as the
user data and therfore will likewise be returned by the match
functions.

On success, this method returns the user_data passed as the second
argument or key_string if no user data was specified.  It returns undef
on failure.

=item B<match_string>

  $pt->match_string(key_string);

This method searches the Patricia Trie to find a matching node,
according to normal subnetting rules for the address and mask
specified.

The key_string argument is a network or subnet specification in
canonical form, e.g. "10.0.0.0/8", where the number after the slash
represents the number of bits in the netmask.  If no mask width value
is specified, the longest mask is assumed, i.e. 32 bits for AF_INET
addresses.

If a matching node is found in the Patricia Trie, this method returns
the user data for the node.  This method returns undef on failure.

=item B<match_exact_string>

  $pt->match_exact_string(key_string);

This method searches the Patricia Trie to find a matching node.  Its
semantics are exactly the same as those described for C<match_string>
except that the key must match a node exactly.  I.e. it is not
sufficient that the address and mask specified merely falls within the
subnet specified by a particular node.

=item B<match_integer>

  $pt->match_integer(integer[,mask_bits]);

This method searches the Patricia Trie to find a matching node,
according to normal subnetting rules for the address and mask
specified.  Its semantics are similar to those described for
C<match_string> except that the key is specified using an integer
(i.e.  SCALAR), such as that returned by perl's C<unpack> function for
values converted using the "N" (network-ordered long).  Note that this
argument is not a packed network-ordered long.

Just to be completely clear, the integer argument should be a value of
the sort produced by this code:

   use Socket;
   $integer = unpack("N", inet_aton("10.0.0.0"));

=item B<match_exact_integer>

  $pt->match_exact_integer(integer[,mask_bits]);

This method searches the Patricia Trie to find a matching node.  Its
semantics are exactly the same as C<match_integer> except that the key
must match a node exactly.  I.e. it is not sufficient that the address
and mask specified merely falls within the subnet specified by a
particular node.

=item B<remove_string>

  $pt->remove_string(key_string);

This method removes the node which exactly matches the the address and
mask specified from the Patricia Trie.

If the matching node is found in the Patricia Trie, it is removed, and
this method returns the user data for the node.  This method returns
undef on failure.

=item B<climb>

   $pt->climb([CODEREF]);

This method climbs the Patricia Trie, visiting each node as it does
so.  It performs a non-recursive, "preorder" traversal.

The CODEREF argument is optional.  It is a perl code reference used to
specify a user-defined subroutine to be called when visiting each
node.  The node's user data will be passed as the sole argument to that
subroutine.

This method returns the number of nodes successfully visited while
climbing the Trie.  That is, without a CODEREF argument, it simply
counts the number of nodes in the Patricia Trie.

Note that currently the return value from your CODEREF subroutine is
ignored.  In the future the climb method may return the number of times
your subroutine returned non-zero, as it is called once per node.  So,
if you are currently relying on the climb return value to accurately
report a count of the number of nodes in the Patricia Trie, it would be
prudent to have your subroutine return a non-zero value.

This method is called climb() rather than walk() because climbing trees
(and therfore tries) is a more popular pass-time than walking them.

=item B<climb_inorder>

   $pt->climb_inorder([CODEREF]);

This method climbs the Patricia Trie, visiting each node in order as it
does so.  That is, it performs an "inorder" traversal.

The CODEREF argument is optional.  It is a perl code reference used to
specify a user-defined subroutine to be called when visiting each
node.  The node's user data will be passed as the sole argument to that
subroutine.

This method returns the number of nodes successfully visited while
climbing the Trie.  That is, without a CODEREF argument, it simply
counts the number of nodes in the Patricia Trie.

Note that currently the return value from your CODEREF subroutine is
ignored.  In the future the climb method may return the number of times
your subroutine returned non-zero, as it is called once per node.  So,
if you are currently relying on the climb return value to accurately
report a count of the number of nodes in the Patricia Trie, it would be
prudent to have your subroutine return a non-zero value.

This method is called climb() rather than walk() because climbing trees
(and therfore tries) is a more popular pass-time than walking them.

=back

=head1 BUGS

This modules does not yet support AF_INET6 (IP version 6) 128 bit
addresses, although the underlying patricialib C code does.

When passing a CODEREF argument to the climb method, the return value
from your CODEREF subroutine is currently ignored.  In the future the
climb method may return the number of times your subroutine returned
non-zero, as it is called once per node.  So, if you are currently
relying on the climb return value to accurately report a count of the
number of nodes in the Patricia Trie, it would be prudent to have your
subroutine return a non-zero value.

=head1 AUTHOR

Dave Plonka <plonka@doit.wisc.edu>
Philip Prindeville <philipp@redfish-solutions.com>

Copyright (C) 2000-2005  Dave Plonka.  Copyright (C) 2009  Dave Plonka
& Philip Prindeville.  This program is free software; you
can redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This product includes software developed by the University of Michigan,
Merit Network, Inc., and their contributors.  See the copyright file in
the patricialib sub-directory of the distribution for details.

patricialib, the C library used by this perl extension, is an extracted
version of MRT's patricia code from radix.[ch], which was worked on by
Masaki Hirabaru and Craig Labovitz.  For more info on MRT see:

   http://www.mrtd.net/

The MRT patricia code owes some heritage to GateD's radix code, which
in turn owes something to the BSD kernel.

=head1 SEE ALSO

perl(1), Socket, Net::Netmask, Text::Trie, Tree::Trie.

Tree::Radix and Net::RoutingTable are modules by Daniel Hagerty
<hag@linnaean.org> written entirely in perl, unlike this module.  At
the time of this writing, they are works-in-progress but may be
available at:

   http://www.linnaean.org/~hag/

=cut