blib/lib/Net/IPAM/Tree.pm

filter: Statementstmt 100% 70 / 70 Branchbran 100% 32 / 32 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 100% 128 / 128 SCAR 29.4
CC
19
Cov
100%
new
6.0
walk
5.0
superset
4.0
CRAP 19.0
↑ Summary blib/lib/Net/IPAM/Tree/Private.pm »
1 package Net::IPAM::Tree;
2  
3 6 our $VERSION = '3.00';
4  
5 6 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Scalar::Util qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use Net::IPAM::Tree::Private qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Net::IPAM::Block qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Net::IPAM::Tree - A CIDR/Block tree library for fast IP lookup with longest-prefix-match.
19  
20 =head1 DESCRIPTION
21  
22 A module for fast IP-routing-table lookups and IP-ACLs (Access Control Lists).
23  
24 It is B<NOT> a standard patricia-trie implementation.
25 This isn't possible for general blocks not represented by bitmasks.
26 Every tree item is a Net::IPAM::Block or a subclass of it.
27  
28 =encoding utf8
29  
30 =head1 SYNOPSIS
31  
32 use Net::IPAM::Tree;
33  
34 my ($t, $dups) = Net::IPAM::Tree->new(@blocks);
35 if (@$dups) {
36 warn("items are duplicate: " . join("\n", @$dups));
37 }
38  
39 my $block = $t->lookup($ip_or_block)
40 && printf( "longest-prefix-match in tree for %s is %s\n", $ip_or_block, $block );
41  
42 my $superset = $t->superset($ip_or_block)
43 && printf( "superset in tree for ip or block %s is %s\n", $ip_or_block, $superset );
44  
45 say $t->to_string;
46  
47 â–¼
48 ├─ ::/8
49 ├─ 100::/8
50 ├─ 2000::/3
51 │ ├─ 2000::/4
52 │ └─ 3000::/4
53 ├─ 4000::/3
54 ...
55  
56 =head1 METHODS
57  
58 =head2 new(@blocks)
59  
60 Create Net::IPAM::Tree object.
61  
62 my ($t, $dups) = Net::IPAM::Tree->new(@blocks);
63  
64 In scalar context just returns the tree object, duplicate items produce a warning.
65  
66 In list context returns the tree object and the arrayref of duplicate items, if any.
67  
68 =cut
69  
70 sub new {
71 10 ▶ my $self = bless {}, shift;
Subroutine new: called CC=6 SCAR=17.9
Pod documented
72  
73 10 $self->{_items} = [ Net::IPAM::Block::sort_block(@_) ];
74 10 $self->{_tree} = {}; # {parent_idx}->[child_idxs]
75  
76 10 my @dups;
77 10 ▶ for ( my $i = 0 ; $i < @{ $self->{_items} } ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"_items"};}) 38 10 48
78  
79 # check for dups
80 38 ▶ if ( $i > 0 && $self->{_items}[$i]->cmp( $self->{_items}[ $i - 1 ] ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0 and $self->{"_items"}[$i]->cmp($self->{"_items"}[$i - 1]) == 0) 2 36 38
Condition100%3/3
$i > 0 and $self->{"_items"}[$i]->cmp($self->{"_items"}[$i - 1]) == 0
!ll&&!rl&&r
6302
MC/DC100%2/2
$i > 0 and $self->{"_items"}[$i]->cmp($self->{"_items"}[$i - 1]) == 0
$i > 0 $self->{"_items"}[$i]->cmp($self->{"_items"}[$i - 1]) == 0
Truth table100%3/3
A and B
A= $i > 0B= $self->{"_items"}[$i]->cmp($self->{"_items"}[$i - 1]) == 0
ABresult
0X0
100
111
81 2 push @dups, $self->{_items}[$i];
82 2 next;
83 }
84  
85 36 Net::IPAM::Tree::Private::_build_index_tree( $self, '_ROOT', $i );
86 }
87  
88 10 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 1 9 10
89 1 return ( $self, \@dups );
90 }
91  
92 9 ▶ if (@dups) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dups) 1 8 9
93 1 Carp::carp('duplicate items,');
94 }
95  
96 9 return $self;
97 }
98  
99 =head2 superset($thing)
100  
101 Returns the outermost block if the given $thing (L<Net::IPAM::IP> or L<Net::IPAM::Block>)
102 is contained in the tree or undef.
103  
104 =cut
105  
106 sub superset {
107 11 ▶ my ( $self, $thing ) = @_;
Subroutine superset: called CC=4 SCAR=13.9
Pod documented
108 11 ▶ Carp::croak("missing or wrong arg,") unless Scalar::Util::blessed($thing);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 2 9 11
109  
110 # make a /32 or /128 block if thing is an IP
111 9 ▶ $thing = Net::IPAM::Block->new($thing) if $thing->isa('Net::IPAM::IP');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->isa("Net::IPAM::IP") 1 8 9
112  
113 9 ▶ Carp::croak("wrong arg,") unless $thing->isa('Net::IPAM::Block');
Branch: 1 branch
BranchTrueFalseTotal
unless $thing->isa("Net::IPAM::Block") 1 8 9
114  
115 8 return Net::IPAM::Tree::Private::_superset( $self, $thing );
116 }
117  
118 =head2 lookup($thing)
119  
120 Returns L<Net::IPAM::Block> with longest prefix match for $thing (L<Net::IPAM::IP> or L<Net::IPAM::Block>)
121 in the tree, undef if not found.
122  
123 This can be used for ACL or fast routing table lookups.
124  
125 # make blocks
126 my @priv = map { Net::IPAM::Block->new($_) } qw(10.0.0.0/8 172.16.0.0/12 192.168.0.0 fc00::/7);
127  
128 # make tree
129 my $priv = Net::IPAM::Tree->new(@priv);
130  
131 my $b = Net::IPAM::Block->new('fdcd:aa59:8bce::/48') or die;
132  
133 my $lpm = $priv->lookup($b)
134 && say "longest-prefix-match for $b is $lpm";
135  
136 =cut
137  
138 sub lookup {
139 14 ▶ my ( $self, $thing ) = @_;
Subroutine lookup: called CC=4 SCAR=13.9
Pod documented
140 14 ▶ Carp::croak("missing or wrong arg,") unless Scalar::Util::blessed($thing);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 2 12 14
141  
142 # make a /32 or /128 block if thing is an IP
143 12 ▶ $thing = Net::IPAM::Block->new($thing) if $thing->isa('Net::IPAM::IP');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->isa("Net::IPAM::IP") 1 11 12
144  
145 12 ▶ Carp::croak("wrong arg,") unless $thing->isa('Net::IPAM::Block');
Branch: 1 branch
BranchTrueFalseTotal
unless $thing->isa("Net::IPAM::Block") 1 11 12
146  
147 11 return Net::IPAM::Tree::Private::_lookup( $self, '_ROOT', $thing );
148 }
149  
150 =head2 to_string
151  
152 Returns the tree as ordered graph or undef on empty trees.
153  
154 $t->to_string($callback);
155  
156 The optional callback is called on every block. Returns the decorated string for block.
157  
158 $t->to_string( sub { my $block = shift; return decorate($block) } );
159  
160 example (without callback):
161  
162 â–¼
163 ├─ ::/8
164 ├─ 100::/8
165 ├─ 2000::/3
166 │ ├─ 2000::/4
167 │ └─ 3000::/4
168 ├─ 6000::/3
169  
170 possible example (with callback):
171  
172 â–¼
173 ├─ ::/8................. "Reserved by IETF [RFC3513][RFC4291]"
174 ├─ 100::/8.............. "Reserved by IETF [RFC3513][RFC4291]"
175 ├─ 2000::/3............. "Global Unicast [RFC3513][RFC4291]"
176 │ ├─ 2000::/4............. "Test"
177 │ └─ 3000::/4............. "FREE"
178 ├─ 6000::/3............. "Reserved by IETF [RFC3513][RFC4291]"
179  
180 =cut
181  
182 sub to_string {
183 5 ▶ my ( $self, $cb ) = @_;
Subroutine to_string: called CC=4 SCAR=13.9
Pod documented
184  
185 5 ▶ if ( defined $cb ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cb) { } 2 3 5
186 2 ▶ Carp::croak("attribute 'cb' is no CODE_REF,") unless ref $cb eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 1 1 2
187 }
188 else {
189 3 ▶ $cb = sub { return "$_[0]" };
Subroutine __ANON__: called CC=1 SCAR=0.0
190 }
191  
192 4 my $prefix = '';
193 4 my $buf = '';
194  
195 4 $buf = Net::IPAM::Tree::Private::_to_string( $self, $cb, '_ROOT', $buf, $prefix );
196  
197 4 ▶ return "â–¼\n" . $buf if $buf;
Branch: 1 branch
BranchTrueFalseTotal
if $buf 3 1 4
198 1 return;
199 }
200  
201 =head2 walk
202  
203 Walks the ordered tree, see L<to_string()>.
204  
205 my $err_string = $t->walk($callback);
206  
207 For every item the callback function is called with the following hash-ref:
208  
209 my $err = $callback->(
210 {
211 depth => $i, # starts at 0
212 item => $item, # current block
213 parent => $parent, # parent block, undef for root items
214 childs => [@childs], # child blocks, empty for leaf items
215 }
216 );
217  
218 The current depth is counting from 0.
219  
220 On error, the walk is stopped and the error is returned to the caller.
221 The callback B<MUST> return undef if there is no error!
222  
223 =cut
224  
225 sub walk {
226 5 ▶ my ( $self, $cb ) = @_;
Subroutine walk: called CC=5 SCAR=16.1
Pod documented
227 5 ▶ Carp::croak("missing arg,") unless defined $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cb 1 4 5
228 4 ▶ Carp::croak("wrong arg, callback is no CODE_REF,") unless ref $cb eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 1 3 4
229  
230 3 foreach my $c ( @{ $self->{_tree}{_ROOT} } ) {
231 3 my $err = Net::IPAM::Tree::Private::_walk( $self, $cb, 0, undef, $c );
232 3 ▶ return $err if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 1 2 3
233 }
234  
235 2 return;
236 }
237  
238 =head2 len
239  
240 Returns the number of blocks in the tree.
241  
242 =cut
243  
244 sub len {
245 1 ▶ return scalar @{ $_[0]->{_items} };
Subroutine len: called CC=1 SCAR=0.0
Pod documented
246 }
247  
248 =head1 AUTHOR
249  
250 Karl Gaissmaier, C<< <karl.gaissmaier(at)uni-ulm.de> >>
251  
252 =head1 SUPPORT
253  
254 You can find documentation for this module with the perldoc command.
255  
256 perldoc Net::IPAM::Tree
257  
258 You can also look for information at:
259  
260 =over 4
261  
262 =item * on github
263  
264 TODO
265  
266 =back
267  
268 =head1 SEE ALSO
269  
270 L<Net::IPAM::IP>
271 L<Net::IPAM::Block>
272  
273 =head1 LICENSE AND COPYRIGHT
274  
275 This software is copyright (c) 2020-2021 by Karl Gaissmaier.
276  
277 This is free software; you can redistribute it and/or modify it under
278 the same terms as the Perl 5 programming language system itself.
279  
280 =cut
281  
282 6 1; # End of Net::IPAM::Tree
↑ Summary blib/lib/Net/IPAM/Tree/Private.pm »