blib/lib/Net/CIDR/ORTC.pm

filter: Statementstmt 90.8% 139 / 153 Branchbran 73.9% 71 / 96 Conditioncond 53.4% 23 / 43 MC/DCmcdc 28.0% 7 / 25 Subroutinesub 100% 19 / 19 Podpod 45.4% 5 / 11 totaltotal 76.0% 264 / 347 CC 64 SCAR 26.8
CC
64
Cov
80%
remove
32.2
add
17.6
pass_three
16.2
CRAP 14.5
↑ Summary
1 package Net::CIDR::ORTC;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
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  
7 2 ▶use Carp qw/carp croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.02';
10  
11 =head1 NAME
12  
13 Net::CIDR::ORTC - CIDR map compression
14  
15 =head1 SYNOPSIS
16  
17 use Net::CIDR::ORTC;
18  
19 my $map = Net::CIDR::ORTC->new();
20  
21 $map->add('0.0.0.0/0', 0);
22 $map->add('192.168.0.0/24', 'value1');
23 $map->add('192.168.1.0/24', 'value1');
24  
25 $map->compress();
26  
27 my $prefixes = $map->list;
28  
29 foreach (@$prefixes) {
30 say $_->[0] . "\t" . $_->[1];
31 }
32  
33 =head1 DESCRIPTION
34  
35 This module implements Optimal Routing Table Compressor (ORTC) algorithm as described in
36 L<Technical Report MSR-TR-98-59|http://research.microsoft.com/pubs/69698/tr-98-59.pdf>.
37  
38 This module intended for offline data processing and not optimal in terms of
39 CPU time and memory usage, but output table should have smallest number of
40 prefixes whits same behaviour (with longest-prefix match lookup).
41  
42 Sometimes this algorithm makes unnecessary changes to input data (prefixes
43 changed, but number of prefixes in output is same as in input), but it is not
44 easy to fix this without making algorithm non-optimal (increasing number of
45 output prefixes in general case).
46  
47 =cut
48  
49 2 ▶use constant IPv4_BITS => 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use constant ALL_ONES => 2**IPv4_BITS - 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # node array fields
53 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 LEFT => 0,
55 RIGHT => 1,
56 VALUE => 2,
57 OLD_VAL => 3,
58 2 };
59  
60 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
61 7 my $class = shift;
62 7 my $self = bless {}, $class;
63 # tree root node (head)
64 7 $self->{root} = [];
65 7 return $self;
66 }
67  
68 40 ▶sub add {
Subroutine add: called CC=16 SCAR=28.7
Pod documented
69 40 my $self = shift;
70 40 my ($ip, $pref_len) = split '/', shift;
71 40 my $value = shift;
72  
73 40 ▶ croak 'missing required argument: prefix in ip/len form' unless defined $ip && defined $pref_len;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and defined $pref_len 0 40 40
Condition33%1/3
defined $ip and defined $pref_len
!ll&&!rl&&r
0040
MC/DC0%0/2
defined $ip and defined $pref_len
defined $ip defined $pref_len
Truth table33%1/3
A and B
A= defined $ipB= defined $pref_len
ABresult
0X0
100
111
74 40 ▶ croak 'value should be defined' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 40 40
75 40 ▶ croak "bad prefix length: $pref_len in prefix $ip/$pref_len" unless $pref_len =~ /^\d+$/ && $pref_len >= 0 && $pref_len <= IPv4_BITS;
Branch: 1 branch
BranchTrueFalseTotal
unless $pref_len =~ /^\d+$/ and $pref_len >= 0 and $pref_len <= 32 0 40 40
Condition33%2/6
$pref_len =~ /^\d+$/ and $pref_len >= 0 and $pref_len <= 32
!ll&&!rl&&r
0040
$pref_len =~ /^\d+$/ and $pref_len >= 0
!ll&&!rl&&r
0040
MC/DC0%0/3
$pref_len =~ /^\d+$/ and $pref_len >= 0 and $pref_len <= 32
$pref_len =~ /^\d+$/ $pref_len >= 0 $pref_len <= 32
Truth table25%1/4
A and B and C
A= $pref_len =~ /^\d+$/B= $pref_len >= 0C= $pref_len <= 32
ABCresult
0XX0
10X0
1100
1111
76  
77 40 my $i_ip = dd2int($ip);
78 40 ▶ croak "bad ip address: $ip in prefix $ip/$pref_len" unless defined $i_ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i_ip 0 40 40
79 40 ▶ carp "low address bits of $ip/$pref_len are meaningless" unless is_valid_prefix($i_ip, $pref_len);
Branch: 1 branch
BranchTrueFalseTotal
unless is_valid_prefix($i_ip, $pref_len) 0 40 40
80  
81 40 my $mask = len2mask($pref_len);
82 # start from most significant bit
83 40 my $bit_to_test = 1 << (IPv4_BITS - 1);
84  
85 40 my $node = $self->{root};
86 40 my $next = $self->{root};
87  
88 40 ▶ while ($bit_to_test & $mask) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bit_to_test & $mask) 535 8 543
89 535 ▶ if ($i_ip & $bit_to_test) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_ip & $bit_to_test) { } 117 418 535
90 117 $next = $node->[RIGHT]
91 }
92 else {
93 418 $next = $node->[LEFT]
94 }
95 535 ▶ last unless defined $next;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 32 503 535
96  
97 503 $bit_to_test >>= 1;
98 503 $node = $next;
99 }
100  
101 40 ▶ if (defined $next) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next) 8 32 40
102 8 ▶ carp "prefix $ip/$pref_len already exists with value ". $next->[VALUE] if defined $next->[VALUE];
Branch: 1 branch
BranchTrueFalseTotal
if defined $next->[2] 0 8 8
103 8 $next->[VALUE] = $value;
104 8 return;
105 }
106  
107 32 ▶ while ($bit_to_test & $mask) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bit_to_test & $mask) 192 32 224
108 192 $next = [];
109 192 ▶ if ($i_ip & $bit_to_test) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_ip & $bit_to_test) { } 49 143 192
110 49 $node->[RIGHT] = $next;
111 }
112 else {
113 143 $node->[LEFT] = $next;
114 }
115  
116 192 $bit_to_test >>= 1;
117 192 $node = $next;
118 }
119 32 $node->[VALUE] = $value;
120 }
121  
122 1 ▶sub remove {
Subroutine remove: called CC=12 SCAR=34.7
Pod documented
123 1 my $self = shift;
124 1 my ($ip, $pref_len) = split '/', shift;
125 1 my $value = shift;
126  
127 1 ▶ croak "bad prefix length: $pref_len in prefix $ip/$pref_len" unless $pref_len =~ /^\d+$/ && $pref_len >= 0 && $pref_len <= IPv4_BITS;
Branch: 1 branch
BranchTrueFalseTotal
unless $pref_len =~ /^\d+$/ and $pref_len >= 0 and $pref_len <= 32 0 1 1
Condition33%2/6
$pref_len =~ /^\d+$/ and $pref_len >= 0 and $pref_len <= 32
!ll&&!rl&&r
001
$pref_len =~ /^\d+$/ and $pref_len >= 0
!ll&&!rl&&r
001
MC/DC0%0/3
$pref_len =~ /^\d+$/ and $pref_len >= 0 and $pref_len <= 32
$pref_len =~ /^\d+$/ $pref_len >= 0 $pref_len <= 32
Truth table25%1/4
A and B and C
A= $pref_len =~ /^\d+$/B= $pref_len >= 0C= $pref_len <= 32
ABCresult
0XX0
10X0
1100
1111
128  
129 1 my $i_ip = dd2int($ip);
130 1 ▶ croak "bad ip address: $ip in prefix $ip/$pref_len" unless defined $i_ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i_ip 0 1 1
131  
132 1 my $mask = len2mask($pref_len);
133 # start from most significant bit
134 1 my $bit_to_test = 1 << (IPv4_BITS - 1);
135  
136 1 my $node = $self->{root};
137 1 my $prev;
138  
139 1 ▶ while ($node && ($bit_to_test & $mask)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node and $bit_to_test & $mask) 0 1 1
Condition33%1/3
$node and $bit_to_test & $mask
!ll&&!rl&&r
010
MC/DC0%0/2
$node and $bit_to_test & $mask
$node $bit_to_test & $mask
Truth table33%1/3
A and B
A= $nodeB= $bit_to_test & $mask
ABresult
0X0
100
111
140 0 $prev = $node;
141 0 ▶ if ($i_ip & $bit_to_test) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_ip & $bit_to_test) { } 0 0 0
142 0 $node = $node->[RIGHT];
143 } else {
144 0 $node = $node->[LEFT];
145 }
146 0 $bit_to_test >>= 1;
147 }
148 1 ▶ return undef unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 1 1
149  
150 1 ▶ if ($node->[LEFT] || $node->[RIGHT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->[0] or $node->[1]) { } 1 0 1
Condition33%1/3
$node->[0] or $node->[1]
l!l&&r!l&&!r
100
MC/DC0%0/2
$node->[0] or $node->[1]
$node->[0] $node->[1]
Truth table33%1/3
A or B
A= $node->[0]B= $node->[1]
ABresult
1X1
011
000
151 1 undef $node->[VALUE];
152 } else {
153 # delete leaf node
154 0 $bit_to_test <<= 1;
155 0 ▶ if ($i_ip & $bit_to_test) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_ip & $bit_to_test) { } 0 0 0
156 0 undef $prev->[RIGHT];
157 } else {
158 0 undef $prev->[LEFT];
159 }
160 }
161 1 return 1;
162 }
163  
164 # dump all prefixes into array ref
165 10 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
166 10 my $self = shift;
167  
168 10 my $r = [];
169  
170 10 _list($self->{root}, 0, 0, $r);
171  
172 10 return $r;
173 }
174  
175 # recursive depth-first preorder tree traversal
176 278 ▶sub _list {
Subroutine _list: called CC=4 SCAR=13.9
177 278 my ($node, $int_ip, $depth, $r) = @_;
178  
179 278 ▶ if (defined $node->[VALUE]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->[2]) 41 237 278
180 41 my $ip = int2dd($int_ip);
181 41 push @$r, [ "$ip/$depth", $node->[VALUE] ];
182 }
183  
184 278 $depth++;
185 278 ▶ _list($node->[LEFT], $int_ip, $depth, $r)
Branch: 1 branch
BranchTrueFalseTotal
if $node->[0] 217 61 278
186 if $node->[LEFT];
187 # set current bit to 1
188 278 ▶ _list($node->[RIGHT], $int_ip | (1 << IPv4_BITS - $depth), $depth, $r)
Branch: 1 branch
BranchTrueFalseTotal
if $node->[1] 51 227 278
189 if $node->[RIGHT];
190 }
191  
192 8 ▶sub compress {
Subroutine compress: called CC=2 SCAR=7.0
Pod documented
193 8 my $self = shift;
194  
195 8 ▶ croak 'value for default (0.0.0.0/0) should be defined' unless defined $self->{root}->[VALUE];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"root"}[2] 0 8 8
196  
197 8 pass_one_and_two($self->{root});
198 8 pass_three($self->{root});
199 }
200  
201 # internal functions
202  
203 # recursive tree traversal
204 414 ▶sub pass_one_and_two {
Subroutine pass_one_and_two: called CC=15 SCAR=27.1
Pod undocumented
205 414 my ($node, $parent_value) = @_;
206  
207 414 ▶ $parent_value = $node->[VALUE] if defined $node->[VALUE];
Branch: 1 branch
BranchTrueFalseTotal
if defined $node->[2] 225 189 414
208  
209 # expand (deaggregate) tree
210 # if node has exactly one child - create second one
211 # this operation performed in depth-first preorder
212 414 ▶ if ($node->[LEFT] xor $node->[RIGHT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->[0] xor $node->[1]) 180 234 414
Condition100%4/4
$node->[0] xor $node->[1]
l&&rl&&!r!l&&r!l&&!r
2315030211
MC/DC100%2/2
$node->[0] xor $node->[1]
$node->[0] $node->[1]
Truth table100%4/4
A xor B
A= $node->[0]B= $node->[1]
ABresult
110
101
011
000
213 180 my $new_node = [];
214 180 $new_node->[VALUE] = $parent_value;
215 180 ▶ $node->[LEFT] = $new_node unless $node->[LEFT];
Branch: 1 branch
BranchTrueFalseTotal
unless $node->[0] 30 150 180
216 180 ▶ $node->[RIGHT] = $new_node unless $node->[RIGHT];
Branch: 1 branch
BranchTrueFalseTotal
unless $node->[1] 150 30 180
217 }
218  
219 414 ▶ pass_one_and_two($node->[LEFT], $parent_value) if $node->[LEFT];
Branch: 1 branch
BranchTrueFalseTotal
if $node->[0] 203 211 414
220 414 ▶ pass_one_and_two($node->[RIGHT], $parent_value) if $node->[RIGHT];
Branch: 1 branch
BranchTrueFalseTotal
if $node->[1] 203 211 414
221  
222 # at this point all nodes has two or no children
223  
224 # this operation performed depth-first postorder
225 414 ▶ if ($node->[LEFT]) { # if node has 2 children
Branch: 1 branch
BranchTrueFalseTotal
if ($node->[0]) 203 211 414
226  
227 # compute nexthops(left) # nexthops(right)
228 my %left = ref $node->[LEFT]->[VALUE] eq 'ARRAY' ?
229 203 ▶ map { $_ => 1 } @{ $node->[LEFT]->[VALUE] } :
Branch: 1 branch
BranchTrueFalseTotal
ref $node->[0][2] eq 'ARRAY' ? : 18 185 203
230 ( $node->[LEFT]->[VALUE] => 1 );
231 my %right = ref $node->[RIGHT]->[VALUE] eq 'ARRAY' ?
232 203 ▶ map { $_ => 1 } @{ $node->[RIGHT]->[VALUE] } :
Branch: 1 branch
BranchTrueFalseTotal
ref $node->[1][2] eq 'ARRAY' ? : 8 195 203
233 ( $node->[RIGHT]->[VALUE] => 1);
234 203 my @intersect = grep { $left{$_} } keys %right;
235  
236 203 ▶ if (scalar @intersect == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @intersect == 1) { } 177 26 203
elsif (scalar @intersect > 1) { } 1 25 26
237 # old value don't need for node with single new value
238 177 $node->[VALUE] = $intersect[0];
239 }
240 elsif (scalar @intersect > 1) {
241 1 ▶ $node->[OLD_VAL] = $node->[VALUE] if defined $node->[VALUE];
Branch: 1 branch
BranchTrueFalseTotal
if defined $node->[2] 0 1 1
242 1 $node->[VALUE] = \@intersect;
243 }
244 else {
245 # intersect empty, use union
246 25 ▶ $node->[OLD_VAL] = $node->[VALUE] if defined $node->[VALUE];
Branch: 1 branch
BranchTrueFalseTotal
if defined $node->[2] 0 25 25
247 25 my %union = (%left, %right);
248 25 $node->[VALUE] = [ keys %union ];
249 }
250 }
251 }
252  
253 # recursive depth-first preorder traversal
254 414 ▶sub pass_three {
Subroutine pass_three: called CC=14 SCAR=27.9
Pod undocumented
255 414 my ($node, $parent, $parent_value) = @_;
256  
257 414 ▶ if ($parent_value ~~ $node->[VALUE]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_value ~~ $node->[2]) { } 373 41 414
258 # parent value is member of node's potential values
259 373 undef $node->[VALUE];
260 }
261 else {
262 41 ▶ if (ref $node->[VALUE] ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node->[2] ne 'ARRAY') { } 34 7 41
263 # only one value, leave it as is
264 34 $parent_value = $node->[VALUE];
265 } else {
266 # there are several values
267 7 ▶ if (!defined $node->[OLD_VAL]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $node->[3]) { } 7 0 7
elsif ($node->[3] ~~ $node->[2]) { } 0 0 0
268 # there is more than one new values in this node (so this node has children
269 # with different values) but in original tree there is no value for this node
270 # remove this value (prefixes from children will be used)
271 7 undef $node->[VALUE];
272 } elsif ($node->[OLD_VAL] ~~ $node->[VALUE]) {
273 # use old value if it found in set of potential new values
274 0 $node->[VALUE] = $node->[OLD_VAL];
275 0 $parent_value = $node->[VALUE];
276 } else {
277 # last resort: use arbitrary value e. g. first one
278 0 $node->[VALUE] = $node->[VALUE]->[0];
279 0 $parent_value = $node->[VALUE];
280 }
281 }
282 }
283 414 undef $node->[OLD_VAL];
284  
285 414 ▶ pass_three($node->[LEFT], $node, $parent_value) if $node->[LEFT];
Branch: 1 branch
BranchTrueFalseTotal
if $node->[0] 203 211 414
286 414 ▶ pass_three($node->[RIGHT], $node, $parent_value) if $node->[RIGHT];
Branch: 1 branch
BranchTrueFalseTotal
if $node->[1] 203 211 414
287  
288 # delete empty leaf nodes
289 414 ▶ if (!defined $node->[VALUE] && !$node->[LEFT] && !$node->[RIGHT]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $node->[2] || $node->[0] || $node->[1]) 195 219 414
Condition100%6/6
defined $node->[2] || $node->[0] || $node->[1]
l!l&&r!l&&!r
19227195
defined $node->[2] or $node->[0]
l!l&&r!l&&!r
34158222
MC/DC100%3/3
defined $node->[2] || $node->[0] || $node->[1]
defined $node->[2] $node->[0] $node->[1]
Truth table100%4/4
A or B || C
A= defined $node->[2]B= $node->[0]C= $node->[1]
ABCresult
1XX1
01X1
0011
0000
290 195 ▶ if (ref $parent->[LEFT] && $parent->[LEFT] == $node) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $parent->[0] and $parent->[0] == $node) { } 38 157 195
elsif (ref $parent->[1] and $parent->[1] == $node) { } 157 0 157
Condition100%3/3
ref $parent->[0] and $parent->[0] == $node
!ll&&!rl&&r
715038
MC/DC100%2/2
ref $parent->[0] and $parent->[0] == $node
ref $parent->[0] $parent->[0] == $node
Truth table100%3/3
A and B
A= ref $parent->[0]B= $parent->[0] == $node
ABresult
0X0
100
111
291 38 undef $parent->[LEFT];
292 ▶ } elsif (ref $parent->[RIGHT] && $parent->[RIGHT] == $node) {
Condition33%1/3
ref $parent->[1] and $parent->[1] == $node
!ll&&!rl&&r
00157
MC/DC0%0/2
ref $parent->[1] and $parent->[1] == $node
ref $parent->[1] $parent->[1] == $node
Truth table33%1/3
A and B
A= ref $parent->[1]B= $parent->[1] == $node
ABresult
0X0
100
111
293 157 undef $parent->[RIGHT];
294 } else {
295 0 die 'internal error: bad parent for this node';
296 }
297 }
298 }
299  
300 # utility functions
301  
302 # same as unpack('N*',inet_aton($x));
303 # Parameters:
304 # - ip in dot-decimal form, e. g. 192.0.2.1
305 # Returns:
306 # - undef if ip is bad
307 # - integer ip
308 63 ▶sub dd2int {
Subroutine dd2int: called CC=5 SCAR=17.2
Pod undocumented
309 63 my @oct = split /\./, $_[0];
310 63 ▶ return undef unless @oct == IPv4_BITS / 8;
Branch: 1 branch
BranchTrueFalseTotal
unless @oct == 4 0 63 63
311 63 my $ip = 0;
312 63 foreach(@oct) {
313 252 ▶ return undef if $_ > 255 || $_ < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > 255 or $_ < 0 0 252 252
Condition33%1/3
$_ > 255 or $_ < 0
l!l&&r!l&&!r
00252
MC/DC0%0/2
$_ > 255 or $_ < 0
$_ > 255 $_ < 0
Truth table33%1/3
A or B
A= $_ > 255B= $_ < 0
ABresult
1X1
011
000
314 252 $ip = $ip<<8 | $_;
315 }
316 63 return $ip;
317 }
318  
319 # ip from integer to dot-decimal (text) form
320 # reverse to dd2int
321 54 ▶sub int2dd {
Subroutine int2dd: called CC=1 SCAR=0.0
Pod undocumented
322 54 return join '.', unpack('C*', pack('N', $_[0]));
323 }
324  
325 # convert prefix length to netmask as integer
326 91 ▶sub len2mask {
Subroutine len2mask: called CC=3 SCAR=13.1
Pod undocumented
327 91 ▶ die "bad prefix length $_[0]" if $_[0] < 0 || $_[0] > IPv4_BITS;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] < 0 or $_[0] > 32 0 91 91
Condition33%1/3
$_[0] < 0 or $_[0] > 32
l!l&&r!l&&!r
0091
MC/DC0%0/2
$_[0] < 0 or $_[0] > 32
$_[0] < 0 $_[0] > 32
Truth table33%1/3
A or B
A= $_[0] < 0B= $_[0] > 32
ABresult
1X1
011
000
328 91 return ALL_ONES - 2**(IPv4_BITS - $_[0]) + 1;
329 }
330  
331 # $net - is integer
332 # $len - is prefix length 0 .. 32
333 44 ▶sub is_valid_prefix {
Subroutine is_valid_prefix: called CC=1 SCAR=0.0
Pod undocumented
334 44 my ($net, $len) = @_;
335 44 return (($net & len2mask($len)) == $net);
336 }
337  
338 2 1;
339 __END__
↑ Summary