| 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
Condition33%1/3
defined $ip and defined $pref_len
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
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 40 | ▶ | croak 'value should be defined' unless defined $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
$pref_len =~ /^\d+$/ and $pref_len >= 0 and $pref_len <= 32
$pref_len =~ /^\d+$/ and $pref_len >= 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 40 | ▶ | carp "low address bits of $ip/$pref_len are meaningless" unless is_valid_prefix($i_ip, $pref_len); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 535 | ▶ | if ($i_ip & $bit_to_test) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 117 | $next = $node->[RIGHT] | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 418 | $next = $node->[LEFT] | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 535 | ▶ | last unless defined $next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 503 | $bit_to_test >>= 1; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 503 | $node = $next; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 40 | ▶ | if (defined $next) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 8 | ▶ | carp "prefix $ip/$pref_len already exists with value ". $next->[VALUE] if defined $next->[VALUE]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 8 | $next->[VALUE] = $value; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 8 | return; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 32 | ▶ | while ($bit_to_test & $mask) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 192 | $next = []; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 192 | ▶ | if ($i_ip & $bit_to_test) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
$pref_len =~ /^\d+$/ and $pref_len >= 0 and $pref_len <= 32
$pref_len =~ /^\d+$/ and $pref_len >= 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$node and $bit_to_test & $mask
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
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | $prev = $node; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ($i_ip & $bit_to_test) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 1 | ▶ | if ($node->[LEFT] || $node->[RIGHT]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$node->[0] or $node->[1]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%4/4
$node->[0] xor $node->[1]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 180 | my $new_node = []; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 180 | $new_node->[VALUE] = $parent_value; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 180 | ▶ | $node->[LEFT] = $new_node unless $node->[LEFT]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 180 | ▶ | $node->[RIGHT] = $new_node unless $node->[RIGHT]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 414 | ▶ | pass_one_and_two($node->[LEFT], $parent_value) if $node->[LEFT]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 414 | ▶ | pass_one_and_two($node->[RIGHT], $parent_value) if $node->[RIGHT]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | ( $node->[LEFT]->[VALUE] => 1 ); | ||||||||||||||||||||||||||||||||||||||||||
| 231 | my %right = ref $node->[RIGHT]->[VALUE] eq 'ARRAY' ? | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 203 | ▶ | map { $_ => 1 } @{ $node->[RIGHT]->[VALUE] } : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | ( $node->[RIGHT]->[VALUE] => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 203 | my @intersect = grep { $left{$_} } keys %right; | |||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 203 | ▶ | if (scalar @intersect == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 414 | ▶ | pass_three($node->[RIGHT], $node, $parent_value) if $node->[RIGHT]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | # delete empty leaf nodes | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 414 | ▶ | if (!defined $node->[VALUE] && !$node->[LEFT] && !$node->[RIGHT]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $node->[2] || $node->[0] || $node->[1]
defined $node->[2] or $node->[0]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 195 | ▶ | if (ref $parent->[LEFT] && $parent->[LEFT] == $node) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
ref $parent->[0] and $parent->[0] == $node
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
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 38 | undef $parent->[LEFT]; | |||||||||||||||||||||||||||||||||||||||||
| 292 | ▶ | } elsif (ref $parent->[RIGHT] && $parent->[RIGHT] == $node) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $parent->[1] and $parent->[1] == $node
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 63 | my $ip = 0; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 63 | foreach(@oct) { | |||||||||||||||||||||||||||||||||||||||||
| 313 | 252 | ▶ | return undef if $_ > 255 || $_ < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_ > 255 or $_ < 0
MC/DC0%0/2
$_ > 255 or $_ < 0
$_ > 255
$_ < 0
Truth table33%1/3
A or B
A= $_ > 255B= $_ < 0
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$_[0] < 0 or $_[0] > 32
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||