lib/Net/BART.pm

filter: Statementstmt 80.6% 250 / 310 Branchbran 62.3% 96 / 154 Conditioncond 56.6% 17 / 30 MC/DCmcdc 35.0% 7 / 20 Subroutinesub 93.5% 29 / 31 Podpod 0.0% 0 / 10 totaltotal 71.8% 399 / 555 CC 92 SCAR 26.5
CC
92
Cov
73%
_do_delete
47.4
_do_get
17.8
contains
14.2
CRAP 14.1
↑ Summary lib/Net/BART/BitSet256.pm »
1 package Net::BART;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Net::BART::BitSet256;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::BART::SparseArray256;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::BART::Art qw(pfx_to_idx octet_to_idx idx_to_pfx prefix_decompose);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::BART::LPM qw(@LOOKUP_TBL);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::BART::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.01';
13  
14 # --- Fast IPv4 parsing (no regex on hot path) ---
15  
16 284 ▶sub _parse_prefix {
Subroutine _parse_prefix: called CC=5 SCAR=17.2
17 284 my ($str) = @_;
18 284 my $slash = index($str, '/');
19 284 my ($addr_str, $prefix_len);
20 284 ▶ if ($slash >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slash >= 0) { } 284 0 284
21 284 $addr_str = substr($str, 0, $slash);
22 284 $prefix_len = substr($str, $slash + 1) + 0;
23 } else {
24 0 $addr_str = $str;
25 }
26  
27 284 my $is_ipv6 = (index($addr_str, ':') >= 0);
28 284 ▶ my $bytes = $is_ipv6 ? _parse_ipv6($addr_str) : _parse_ipv4_fast($addr_str);
Branch: 1 branch
BranchTrueFalseTotal
$is_ipv6 ? : 3 281 284
29  
30 284 ▶ if (!defined $prefix_len) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prefix_len) 0 284 284
31 0 ▶ $prefix_len = $is_ipv6 ? 128 : 32;
Branch: 1 branch
BranchTrueFalseTotal
$is_ipv6 ? : 0 0 0
32 }
33  
34 284 _mask_prefix($bytes, $prefix_len);
35 284 return ($bytes, $prefix_len, $is_ipv6);
36 }
37  
38 0 ▶sub _parse_ip {
Subroutine _parse_ip: not called CC=2 SCAR=17.9
39 0 my ($str) = @_;
40 0 ▶ if (index($str, ':') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($str, ":") != -1) 0 0 0
41 0 return (_parse_ipv6($str), 1);
42 }
43 0 return (_parse_ipv4_fast($str), 0);
44 }
45  
46 # Fast IPv4 parser - no regex, no validation overhead on hot path
47 281 ▶sub _parse_ipv4_fast {
Subroutine _parse_ipv4_fast: called CC=1 SCAR=0.0
48 281 my ($str) = @_;
49 281 my $d1 = index($str, '.');
50 281 my $d2 = index($str, '.', $d1 + 1);
51 281 my $d3 = index($str, '.', $d2 + 1);
52 return [
53 281 substr($str, 0, $d1) + 0,
54 substr($str, $d1 + 1, $d2 - $d1 - 1) + 0,
55 substr($str, $d2 + 1, $d3 - $d2 - 1) + 0,
56 substr($str, $d3 + 1) + 0,
57 ];
58 }
59  
60 7 ▶sub _parse_ipv6 {
Subroutine _parse_ipv6: called CC=7 SCAR=19.9
61 7 my ($str) = @_;
62 7 my @halves;
63 7 ▶ if (index($str, '::') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($str, '::') != -1) { } 7 0 7
64 7 my ($left, $right) = split /::/, $str, 2;
65 7 ▶ my @left_groups = $left ? (split /:/, $left) : ();
Branch: 1 branch
BranchTrueFalseTotal
$left ? : 7 0 7
66 7 ▶ my @right_groups = $right ? (split /:/, $right) : ();
Branch: 1 branch
BranchTrueFalseTotal
$right ? : 4 3 7
67 7 my $fill = 8 - @left_groups - @right_groups;
68 7 ▶ croak "Invalid IPv6 address: $str" if $fill < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $fill < 0 0 7 7
69 7 @halves = (@left_groups, (('0') x $fill), @right_groups);
70 } else {
71 0 @halves = split /:/, $str;
72 }
73 7 ▶ croak "Invalid IPv6 address: $str" unless @halves == 8;
Branch: 1 branch
BranchTrueFalseTotal
unless @halves == 8 0 7 7
74 7 my @bytes;
75 7 for my $h (@halves) {
76 56 my $val = hex($h);
77 56 push @bytes, ($val >> 8) & 0xFF, $val & 0xFF;
78 }
79 7 return \@bytes;
80 }
81  
82 284 ▶sub _mask_prefix {
Subroutine _mask_prefix: called CC=4 SCAR=13.9
83 284 my ($bytes, $prefix_len) = @_;
84 284 my $full_bytes = $prefix_len >> 3;
85 284 my $remaining = $prefix_len & 7;
86 284 my $total = scalar @$bytes;
87 284 ▶ if ($remaining && $full_bytes < $total) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining and $full_bytes < $total) 2 282 284
Condition66%2/3
$remaining and $full_bytes < $total
!ll&&!rl&&r
28202
MC/DC50%1/2
$remaining and $full_bytes < $total
$remaining $full_bytes < $total
Truth table66%2/3
A and B
A= $remainingB= $full_bytes < $total
ABresult
0X0
100
111
88 2 $bytes->[$full_bytes] &= (0xFF << (8 - $remaining)) & 0xFF;
89 2 $full_bytes++;
90 }
91 284 for my $i ($full_bytes .. $total - 1) {
92 351 $bytes->[$i] = 0;
93 }
94 }
95  
96 2 ▶sub _format_ip {
Subroutine _format_ip: called CC=3 SCAR=16.7
97 2 my ($bytes, $is_ipv6) = @_;
98 2 ▶ if ($is_ipv6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ipv6) 0 2 2
99 0 my @groups;
100 0 ▶ for (my $i = 0; $i < 16; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 16) 0 0 0
101 0 push @groups, sprintf("%x", ($bytes->[$i] << 8) | $bytes->[$i + 1]);
102 }
103 0 return join(':', @groups);
104 }
105 2 return join('.', @$bytes);
106 }
107  
108 # --- Table ---
109  
110 13 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
111 13 return bless {
112 root4 => Net::BART::Node::Bart->new,
113 root6 => Net::BART::Node::Bart->new,
114 size4 => 0,
115 size6 => 0,
116 }, $_[0];
117 }
118  
119 278 ▶sub insert {
Subroutine insert: called CC=4 SCAR=13.9
Pod undocumented
120 278 my ($self, $prefix_str, $value) = @_;
121 278 my ($addr, $prefix_len, $is_ipv6) = _parse_prefix($prefix_str);
122 278 ▶ my $root = $is_ipv6 ? $self->{root6} : $self->{root4};
Branch: 1 branch
BranchTrueFalseTotal
$is_ipv6 ? : 3 275 278
123 278 my $is_new = _do_insert($root, $addr, $prefix_len, 0, $value);
124 278 ▶ if ($is_new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_new) 277 1 278
125 277 ▶ if ($is_ipv6) { $self->{size6}++ } else { $self->{size4}++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ipv6) { } 3 274 277
126 }
127 278 return $is_new;
128 }
129  
130 # Non-method for speed (avoids $self-> dispatch overhead in recursion)
131 804 ▶sub _do_insert {
Subroutine _do_insert: called CC=12 SCAR=25.2
132 804 my ($node, $addr, $prefix_len, $depth, $value) = @_;
133  
134 804 my $strides = $prefix_len >> 3;
135 804 my $lastbits = $prefix_len & 7;
136  
137 804 ▶ if ($prefix_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix_len == 0) 1 803 804
138 1 return $node->insert_prefix(1, $value);
139 }
140  
141 803 ▶ if ($lastbits && $depth == $strides) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastbits and $depth == $strides) 2 801 803
Condition100%3/3
$lastbits and $depth == $strides
!ll&&!rl&&r
79922
MC/DC100%2/2
$lastbits and $depth == $strides
$lastbits $depth == $strides
Truth table100%3/3
A and B
A= $lastbitsB= $depth == $strides
ABresult
0X0
100
111
142 2 return $node->insert_prefix(pfx_to_idx($addr->[$depth], $lastbits), $value);
143 }
144  
145 801 ▶ if (!$lastbits && $depth == $strides - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lastbits and $depth == $strides - 1) 271 530 801
Condition100%3/3
not $lastbits and $depth == $strides - 1
!ll&&!rl&&r
2528271
MC/DC100%2/2
not $lastbits and $depth == $strides - 1
not $lastbits $depth == $strides - 1
Truth table100%3/3
not(A) and B
A= not $lastbitsB= $depth == $strides - 1
ABresult
0X0
100
111
146 271 return _do_insert_fringe($node, $addr, $prefix_len, $depth, $value);
147 }
148  
149 # Navigate
150 530 my $octet = $addr->[$depth];
151 530 my ($child, $exists) = $node->get_child($octet);
152  
153 530 ▶ if (!$exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exists) 10 520 530
154 10 $node->set_child($octet, Net::BART::Node::Leaf->new(
155 addr => [@$addr], prefix_len => $prefix_len, value => $value,
156 ));
157 10 return 1;
158 }
159  
160 520 my $ref = ref($child);
161  
162 520 ▶ if ($ref eq 'Net::BART::Node::Leaf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Leaf") 6 514 520
163 6 ▶ if ($child->matches_prefix($addr, $prefix_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->matches_prefix($addr, $prefix_len)) 0 6 6
164 0 $child->[2] = $value; # LEAF_VALUE
165 0 return 0;
166 }
167 6 my $new_node = Net::BART::Node::Bart->new;
168 6 _do_insert($new_node, $child->[0], $child->[1], $depth + 1, $child->[2]);
169 6 $node->set_child($octet, $new_node);
170 6 return _do_insert($new_node, $addr, $prefix_len, $depth + 1, $value);
171 }
172  
173 514 ▶ if ($ref eq 'Net::BART::Node::Fringe') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Fringe") 5 509 514
174 5 ▶ if (!$lastbits && $depth == $strides - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lastbits and $depth == $strides - 1) 0 5 5
Condition33%1/3
not $lastbits and $depth == $strides - 1
!ll&&!rl&&r
050
MC/DC0%0/2
not $lastbits and $depth == $strides - 1
not $lastbits $depth == $strides - 1
Truth table33%1/3
not(A) and B
A= not $lastbitsB= $depth == $strides - 1
ABresult
0X0
100
111
175 0 $child->[0] = $value;
176 0 return 0;
177 }
178 5 my $new_node = Net::BART::Node::Bart->new;
179 5 $new_node->insert_prefix(1, $child->[0]);
180 5 $node->set_child($octet, $new_node);
181 5 return _do_insert($new_node, $addr, $prefix_len, $depth + 1, $value);
182 }
183  
184 509 return _do_insert($child, $addr, $prefix_len, $depth + 1, $value);
185 }
186  
187 271 ▶sub _do_insert_fringe {
Subroutine _do_insert_fringe: called CC=5 SCAR=21.4
188 271 my ($node, $addr, $prefix_len, $depth, $value) = @_;
189 271 my $octet = $addr->[$depth];
190 271 my ($child, $exists) = $node->get_child($octet);
191  
192 271 ▶ if (!$exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exists) 270 1 271
193 270 $node->set_child($octet, Net::BART::Node::Fringe->new(value => $value));
194 270 return 1;
195 }
196  
197 1 my $ref = ref($child);
198  
199 1 ▶ if ($ref eq 'Net::BART::Node::Fringe') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Fringe") 1 0 1
200 1 $child->[0] = $value;
201 1 return 0;
202 }
203  
204 0 ▶ if ($ref eq 'Net::BART::Node::Bart') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Bart") 0 0 0
205 0 return $child->insert_prefix(1, $value);
206 }
207  
208 0 ▶ if ($ref eq 'Net::BART::Node::Leaf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Leaf") 0 0 0
209 0 my $new_node = Net::BART::Node::Bart->new;
210 0 _do_insert($new_node, $child->[0], $child->[1], $depth + 1, $child->[2]);
211 0 $new_node->insert_prefix(1, $value);
212 0 $node->set_child($octet, $new_node);
213 0 return 1;
214 }
215  
216 0 return 0;
217 }
218  
219 2 ▶sub delete {
Subroutine delete: called CC=4 SCAR=14.2
Pod undocumented
220 2 my ($self, $prefix_str) = @_;
221 2 my ($addr, $prefix_len, $is_ipv6) = _parse_prefix($prefix_str);
222 2 ▶ my $root = $is_ipv6 ? $self->{root6} : $self->{root4};
Branch: 1 branch
BranchTrueFalseTotal
$is_ipv6 ? : 0 2 2
223 2 my ($val, $ok) = _do_delete($root, $addr, $prefix_len, 0);
224 2 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 1 1 2
225 0 ▶ if ($is_ipv6) { $self->{size6}-- } else { $self->{size4}-- }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ipv6) { } 0 1 1
226 }
227 2 return ($val, $ok);
228 }
229  
230 4 ▶sub _do_delete {
Subroutine _do_delete: called CC=17 SCAR=38.6
231 4 my ($node, $addr, $prefix_len, $depth) = @_;
232  
233 4 my $strides = $prefix_len >> 3;
234 4 my $lastbits = $prefix_len & 7;
235  
236 4 ▶ if ($prefix_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix_len == 0) 0 4 4
237 0 return $node->delete_prefix(1);
238 }
239  
240 4 ▶ if ($lastbits && $depth == $strides) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastbits and $depth == $strides) 0 4 4
Condition33%1/3
$lastbits and $depth == $strides
!ll&&!rl&&r
400
MC/DC0%0/2
$lastbits and $depth == $strides
$lastbits $depth == $strides
Truth table33%1/3
A and B
A= $lastbitsB= $depth == $strides
ABresult
0X0
100
111
241 0 return $node->delete_prefix(pfx_to_idx($addr->[$depth], $lastbits));
242 }
243  
244 4 ▶ if (!$lastbits && $depth == $strides - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lastbits and $depth == $strides - 1) 2 2 4
Condition66%2/3
not $lastbits and $depth == $strides - 1
!ll&&!rl&&r
022
MC/DC50%1/2
not $lastbits and $depth == $strides - 1
not $lastbits $depth == $strides - 1
Truth table66%2/3
not(A) and B
A= not $lastbitsB= $depth == $strides - 1
ABresult
0X0
100
111
245 2 my $octet = $addr->[$depth];
246 2 my ($child, $exists) = $node->get_child($octet);
247 2 ▶ return (undef, 0) unless $exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $exists 1 1 2
248  
249 1 my $ref = ref($child);
250 1 ▶ if ($ref eq 'Net::BART::Node::Fringe') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Fringe") 1 0 1
251 1 $node->delete_child($octet);
252 1 return ($child->[0], 1);
253 }
254 0 ▶ if ($ref eq 'Net::BART::Node::Bart') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Bart") 0 0 0
255 0 my ($val, $ok) = $child->delete_prefix(1);
256 0 ▶ if ($ok && $child->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok and $child->is_empty) 0 0 0
Condition0%0/3
$ok and $child->is_empty
!ll&&!rl&&r
000
MC/DC0%0/2
$ok and $child->is_empty
$ok $child->is_empty
Truth table0%0/3
A and B
A= $okB= $child->is_empty
ABresult
0X0
100
111
257 0 $node->delete_child($octet);
258 }
259 0 return ($val, $ok);
260 }
261 0 return (undef, 0);
262 }
263  
264 2 my $octet = $addr->[$depth];
265 2 my ($child, $exists) = $node->get_child($octet);
266 2 ▶ return (undef, 0) unless $exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $exists 0 2 2
267  
268 2 my $ref = ref($child);
269 2 ▶ if ($ref eq 'Net::BART::Node::Leaf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Leaf") 0 2 2
270 0 ▶ if ($child->matches_prefix($addr, $prefix_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->matches_prefix($addr, $prefix_len)) 0 0 0
271 0 $node->delete_child($octet);
272 0 return ($child->[2], 1);
273 }
274 0 return (undef, 0);
275 }
276  
277 2 ▶ if ($ref eq 'Net::BART::Node::Fringe') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Fringe") 0 2 2
278 0 return (undef, 0);
279 }
280  
281 2 my ($val, $ok) = _do_delete($child, $addr, $prefix_len, $depth + 1);
282 2 ▶ if ($ok && $child->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok and $child->is_empty) 0 2 2
Condition66%2/3
$ok and $child->is_empty
!ll&&!rl&&r
110
MC/DC0%0/2
$ok and $child->is_empty
$ok $child->is_empty
Truth table66%2/3
A and B
A= $okB= $child->is_empty
ABresult
0X0
100
111
283 0 $node->delete_child($octet);
284 }
285 2 return ($val, $ok);
286 }
287  
288 # Lookup: longest matching prefix for an IP address.
289 # Heavily optimized - this is the primary hot path.
290 22 ▶sub lookup {
Subroutine lookup: called CC=11 SCAR=24.0
Pod undocumented
291 22 my ($self, $ip_str) = @_;
292  
293 # Inline fast IPv4 parse
294 22 my ($bytes, $is_ipv6);
295 22 ▶ if (index($ip_str, ':') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($ip_str, ':') != -1) { } 4 18 22
296 4 $bytes = _parse_ipv6($ip_str);
297 4 $is_ipv6 = 1;
298 } else {
299 18 my $d1 = index($ip_str, '.');
300 18 my $d2 = index($ip_str, '.', $d1 + 1);
301 18 my $d3 = index($ip_str, '.', $d2 + 1);
302 18 $bytes = [
303 substr($ip_str, 0, $d1) + 0,
304 substr($ip_str, $d1 + 1, $d2 - $d1 - 1) + 0,
305 substr($ip_str, $d2 + 1, $d3 - $d2 - 1) + 0,
306 substr($ip_str, $d3 + 1) + 0,
307 ];
308 18 $is_ipv6 = 0;
309 }
310  
311 22 ▶ my $root = $is_ipv6 ? $self->{root6} : $self->{root4};
Branch: 1 branch
BranchTrueFalseTotal
$is_ipv6 ? : 4 18 22
312 22 ▶ my $max_depth = $is_ipv6 ? 16 : 4;
Branch: 1 branch
BranchTrueFalseTotal
$is_ipv6 ? : 4 18 22
313  
314 # Walk down, storing nodes/octets for backtrack LPM.
315 # Use flat arrays instead of array-of-arrays for speed.
316 22 my (@nodes, @octets);
317 22 my $node = $root;
318 22 my $sp = 0;
319  
320 22 for my $depth (0 .. $max_depth - 1) {
321 48 my $octet = $bytes->[$depth];
322 48 $nodes[$sp] = $node;
323 48 $octets[$sp] = $octet;
324 48 $sp++;
325  
326 # Inline get_child: $node->[1] is children sparse array
327 48 my $chd = $node->[1];
328 48 my $chd_bs = $chd->[0];
329 48 ▶ unless ($chd_bs->[$octet >> 6] & (1 << ($octet & 63))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$chd_bs[$octet >> 6] & 1 << ($octet & 63)) 10 38 48
330 10 last; # no child
331 }
332 38 my $child = $chd->[1][$chd_bs->rank($octet) - 1];
333  
334 38 my $ref = ref($child);
335 38 ▶ if ($ref eq 'Net::BART::Node::Fringe') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Fringe") 7 31 38
336 7 return ($child->[0], 1);
337 }
338 31 ▶ if ($ref eq 'Net::BART::Node::Leaf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Leaf") 5 26 31
339 5 ▶ if ($child->contains_ip($bytes)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->contains_ip($bytes)) 3 2 5
340 3 return ($child->[2], 1);
341 }
342 2 last;
343 }
344 26 $node = $child;
345 }
346  
347 # Backtrack: LPM at each stacked node
348 12 ▶ for (my $i = $sp - 1; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 16 4 20
349 16 my ($val, $ok) = $nodes[$i]->lpm($octets[$i]);
350 16 ▶ return ($val, 1) if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 8 8 16
351 }
352  
353 4 return (undef, 0);
354 }
355  
356 # Contains: check if any prefix contains the IP.
357 3 ▶sub contains {
Subroutine contains: called CC=10 SCAR=26.5
Pod undocumented
358 3 my ($self, $ip_str) = @_;
359  
360 3 my ($bytes, $is_ipv6);
361 3 ▶ if (index($ip_str, ':') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($ip_str, ':') != -1) { } 0 3 3
362 0 $bytes = _parse_ipv6($ip_str);
363 0 $is_ipv6 = 1;
364 } else {
365 3 my $d1 = index($ip_str, '.');
366 3 my $d2 = index($ip_str, '.', $d1 + 1);
367 3 my $d3 = index($ip_str, '.', $d2 + 1);
368 3 $bytes = [
369 substr($ip_str, 0, $d1) + 0,
370 substr($ip_str, $d1 + 1, $d2 - $d1 - 1) + 0,
371 substr($ip_str, $d2 + 1, $d3 - $d2 - 1) + 0,
372 substr($ip_str, $d3 + 1) + 0,
373 ];
374 3 $is_ipv6 = 0;
375 }
376  
377 3 ▶ my $node = $is_ipv6 ? $self->{root6} : $self->{root4};
Branch: 1 branch
BranchTrueFalseTotal
$is_ipv6 ? : 0 3 3
378 3 ▶ my $max_depth = $is_ipv6 ? 16 : 4;
Branch: 1 branch
BranchTrueFalseTotal
$is_ipv6 ? : 0 3 3
379  
380 3 for my $depth (0 .. $max_depth - 1) {
381 3 my $octet = $bytes->[$depth];
382  
383 # Inline lpm_test
384 3 my $pfx_bs = $node->[0][0];
385 3 my $lut = $LOOKUP_TBL[($octet >> 1) + 128];
386 3 ▶ if (($pfx_bs->[0] & $lut->[0]) | ($pfx_bs->[1] & $lut->[1]) |
Branch: 1 branch
BranchTrueFalseTotal
if ($pfx_bs->[0] & $lut->[0] | $pfx_bs->[1] & $lut->[1] | $pfx_bs->[2] & $lut->[2] | $pfx_bs->[3] & $lut->[3]) 0 3 3
387 ($pfx_bs->[2] & $lut->[2]) | ($pfx_bs->[3] & $lut->[3])) {
388 0 return 1;
389 }
390  
391 # Inline get_child
392 3 my $chd = $node->[1];
393 3 my $chd_bs = $chd->[0];
394 3 ▶ unless ($chd_bs->[$octet >> 6] & (1 << ($octet & 63))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$chd_bs[$octet >> 6] & 1 << ($octet & 63)) 1 2 3
395 1 return 0;
396 }
397 2 my $child = $chd->[1][$chd_bs->rank($octet) - 1];
398  
399 2 my $ref = ref($child);
400 2 ▶ if ($ref eq 'Net::BART::Node::Fringe') { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Fringe") 2 0 2
401 0 ▶ if ($ref eq 'Net::BART::Node::Leaf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Leaf") 0 0 0
402 0 ▶ return $child->contains_ip($bytes) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$child->contains_ip($bytes) ? : 0 0 0
403 }
404 0 $node = $child;
405 }
406 0 return 0;
407 }
408  
409 # Exact match get.
410 4 ▶sub get {
Subroutine get: called CC=2 SCAR=7.0
Pod undocumented
411 4 my ($self, $prefix_str) = @_;
412 4 my ($addr, $prefix_len, $is_ipv6) = _parse_prefix($prefix_str);
413 4 ▶ my $root = $is_ipv6 ? $self->{root6} : $self->{root4};
Branch: 1 branch
BranchTrueFalseTotal
$is_ipv6 ? : 0 4 4
414 4 return _do_get($root, $addr, $prefix_len, 0);
415 }
416  
417 6 ▶sub _do_get {
Subroutine _do_get: called CC=13 SCAR=28.8
418 6 my ($node, $addr, $prefix_len, $depth) = @_;
419  
420 6 my $strides = $prefix_len >> 3;
421 6 my $lastbits = $prefix_len & 7;
422  
423 6 ▶ if ($prefix_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix_len == 0) 0 6 6
424 0 return $node->get_prefix(1);
425 }
426  
427 6 ▶ if ($lastbits && $depth == $strides) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastbits and $depth == $strides) 0 6 6
Condition33%1/3
$lastbits and $depth == $strides
!ll&&!rl&&r
600
MC/DC0%0/2
$lastbits and $depth == $strides
$lastbits $depth == $strides
Truth table33%1/3
A and B
A= $lastbitsB= $depth == $strides
ABresult
0X0
100
111
428 0 return $node->get_prefix(pfx_to_idx($addr->[$depth], $lastbits));
429 }
430  
431 6 ▶ if (!$lastbits && $depth == $strides - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lastbits and $depth == $strides - 1) 4 2 6
Condition66%2/3
not $lastbits and $depth == $strides - 1
!ll&&!rl&&r
024
MC/DC50%1/2
not $lastbits and $depth == $strides - 1
not $lastbits $depth == $strides - 1
Truth table66%2/3
not(A) and B
A= not $lastbitsB= $depth == $strides - 1
ABresult
0X0
100
111
432 4 my ($child, $exists) = $node->get_child($addr->[$depth]);
433 4 ▶ return (undef, 0) unless $exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $exists 1 3 4
434 3 my $ref = ref($child);
435 3 ▶ if ($ref eq 'Net::BART::Node::Fringe') { return ($child->[0], 1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Fringe") 2 1 3
436 1 ▶ if ($ref eq 'Net::BART::Node::Bart') { return $child->get_prefix(1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Bart") 1 0 1
437 0 return (undef, 0);
438 }
439  
440 2 my ($child, $exists) = $node->get_child($addr->[$depth]);
441 2 ▶ return (undef, 0) unless $exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $exists 0 2 2
442 2 my $ref = ref($child);
443 2 ▶ if ($ref eq 'Net::BART::Node::Leaf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Net::BART::Node::Leaf") 0 2 2
444 0 ▶ return $child->matches_prefix($addr, $prefix_len) ? ($child->[2], 1) : (undef, 0);
Branch: 1 branch
BranchTrueFalseTotal
$child->matches_prefix($addr, $prefix_len) ? : 0 0 0
445 }
446 2 ▶ return (undef, 0) if $ref eq 'Net::BART::Node::Fringe';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "Net::BART::Node::Fringe" 0 2 2
447 2 return _do_get($child, $addr, $prefix_len, $depth + 1);
448 }
449  
450 6 ▶sub size { return $_[0]->{size4} + $_[0]->{size6} }
Subroutine size: called CC=1 SCAR=0.0
Pod undocumented
451 1 ▶sub size4 { return $_[0]->{size4} }
Subroutine size4: called CC=1 SCAR=0.0
Pod undocumented
452 2 ▶sub size6 { return $_[0]->{size6} }
Subroutine size6: called CC=1 SCAR=0.0
Pod undocumented
453  
454 # Walk all prefixes.
455 1 ▶sub walk {
Subroutine walk: called CC=1 SCAR=0.0
Pod undocumented
456 1 my ($self, $callback) = @_;
457 1 _walk_node($self->{root4}, [], 0, 0, $callback);
458 1 _walk_node($self->{root6}, [], 1, 0, $callback);
459 }
460  
461 2 ▶sub _walk_node {
Subroutine _walk_node: called CC=2 SCAR=8.3
462 2 my ($node, $path, $is_ipv6, $depth, $callback) = @_;
463 2 ▶ my $total_bytes = $is_ipv6 ? 16 : 4;
Branch: 1 branch
BranchTrueFalseTotal
$is_ipv6 ? : 1 1 2
464  
465 # Visit prefixes at this node
466 0 ▶ $node->[0]->each_pair(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
467 0 my ($idx, $val) = @_;
468 0 my ($octet, $pfx_len_in_stride) = idx_to_pfx($idx);
469 0 my $total_bits = $depth * 8 + $pfx_len_in_stride;
470 0 my @addr = @$path;
471 0 ▶ push @addr, $octet if $pfx_len_in_stride > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pfx_len_in_stride > 0 0 0 0
472 0 ▶ while (@addr < $total_bytes) { push @addr, 0 }
Branch: 1 branch
BranchTrueFalseTotal
if (@addr < $total_bytes) 0 0 0
473 0 _mask_prefix(\@addr, $total_bits);
474 0 $callback->(_format_ip(\@addr, $is_ipv6) . "/$total_bits", $val);
475 2 });
476  
477 # Visit children
478 2 ▶ $node->[1]->each_pair(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
479 2 my ($octet, $child) = @_;
480 2 my @child_path = (@$path, $octet);
481 2 my $ref = ref($child);
482  
483 2 ▶ if ($ref eq 'Net::BART::Node::Leaf') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'Net::BART::Node::Leaf') { } 1 1 2
elsif ($ref eq 'Net::BART::Node::Fringe') { } 1 0 1
484 1 $callback->(_format_ip($child->[0], $is_ipv6) . "/$child->[1]", $child->[2]);
485 } elsif ($ref eq 'Net::BART::Node::Fringe') {
486 1 my $total_bits = ($depth + 1) * 8;
487 1 my @addr = @child_path;
488 1 ▶ while (@addr < $total_bytes) { push @addr, 0 }
Branch: 1 branch
BranchTrueFalseTotal
if (@addr < $total_bytes) 3 1 4
489 1 $callback->(_format_ip(\@addr, $is_ipv6) . "/$total_bits", $child->[0]);
490 } else {
491 0 _walk_node($child, \@child_path, $is_ipv6, $depth + 1, $callback);
492 }
493 2 });
494 }
495  
496 1 1;
497  
498 __END__
↑ Summary lib/Net/BART/BitSet256.pm »