blib/lib/Net/IPv6Addr.pm

filter: Statementstmt 83.4% 262 / 314 Branchbran 65.5% 59 / 90 Conditioncond 85.7% 18 / 21 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 94.4% 34 / 36 Podpod 50.0% 16 / 32 totaltotal 78.7% 396 / 503 CC 58 SCAR 17.3
CC
58
Cov
80%
ipv6_parse_base85
20.0
to_string_base85
20.0
in_network_of_size
8.6
CRAP 5.7
↑ Summary
1 package Net::IPv6Addr;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 our @ISA = qw(Exporter);
7 15 our @EXPORT = qw();
8 15 our @EXPORT_OK = qw(
9 from_bigint
10 in_network
11 in_network_of_size
12 ipv6_chkip
13 ipv6_parse
14 is_ipv6
15 to_array
16 to_bigint
17 to_intarray
18 to_string_base85
19 to_string_compressed
20 to_string_ip6_int
21 to_string_ipv4
22 to_string_ipv4_compressed
23 to_string_preferred
24 );
25  
26 15 our %EXPORT_TAGS = (all => \@EXPORT_OK);
27  
28 15 our $VERSION = '1.02';
29  
30 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 15 ▶use Math::BigInt '1.999813';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 15 my $base85ok;
34 15 eval {
35 15 require Math::Base85;
36 };
37 15 ▶if (! $@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 15 15
38 0 $base85ok = 1;
39 }
40  
41 sub base85ok
42 1 ▶{
Subroutine base85ok: called CC=1 SCAR=0.0
Pod undocumented
43 1 return $base85ok
44 }
45  
46 # ____ _ _
47 # | _ \ __ _| |_| |_ ___ _ __ _ __ ___
48 # | |_) / _` | __| __/ _ \ '__| '_ \/ __|
49 # | __/ (_| | |_| || __/ | | | | \__ \
50 # |_| \__,_|\__|\__\___|_| |_| |_|___/
51 #
52  
53 # Match one to four digits of hexadecimal
54  
55 15 my $h = qr/[a-f0-9]{1,4}/i;
56  
57 15 my $ipn = qr!
58 (
59 25[0-5]
60 |
61 2[0-4][0-9]
62 |
63 1[0-9]{2}
64 |
65 [1-9][0-9]
66 |
67 [0-9]
68 )
69 !x;
70  
71 15 my $ipv4 = qr!($ipn\.){3}$ipn!;
72  
73 sub ipv4_validate
74 30 ▶{
Subroutine ipv4_validate: called CC=2 SCAR=8.1
Pod undocumented
75 30 my ($ip) = @_;
76 30 ▶ if ($ip !~ /$ipv4/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ip =~ /$ipv4/) 0 30 30
77 0 croak "Holy macaroni batman";
78 }
79 }
80  
81 # base-85
82  
83 15 my $base85_re;
84 15 ▶if ($base85ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base85ok) 0 15 15
85 0 my $digits = $Math::Base85::base85_digits;
86 0 $digits =~ s/-//;
87 0 $base85_re = qr![$digits-]{20}!;
88 }
89  
90 15 my %ipv6_patterns = (
91 'preferred' => [
92 qr/^(?:$h:){7}$h$/i,
93 \&ipv6_parse_preferred,
94 ],
95 'compressed' => [
96 qr/^[a-f0-9]{0,4}::$/i,
97 qr/^:(?::$h){1,7}$/i,
98 qr/^(?:$h:){1,}:$/i,
99 qr/^(?:$h:)(?::$h){1,6}$/i,
100 qr/^(?:$h:){2}(?::$h){1,5}$/i,
101 qr/^(?:$h:){3}(?::$h){1,4}$/i,
102 qr/^(?:$h:){4}(?::$h){1,3}$/i,
103 qr/^(?:$h:){5}(?::$h){1,2}$/i,
104 qr/^(?:$h:){6}(?::$h)$/i,
105 \&ipv6_parse_compressed,
106 ],
107 'ipv4' => [
108 qr/^(?:0:){5}ffff:$ipv4$/i,
109 qr/^(?:0:){6}$ipv4$/,
110 \&ipv6_parse_ipv4,
111 ],
112 'ipv4 compressed' => [
113 qr/^::(?:ffff:)?$ipv4$/i,
114 \&ipv6_parse_ipv4_compressed,
115 ],
116 'ipv6v4' => [
117 qr/^[a-f0-9]{0,4}::$ipv4$/i,
118 # ::1:2:3:4:1.2.3.4
119 qr/^::(?:$h:){1,5}$ipv4$/i,
120 qr/^(?:$h:):(?:$h:){1,4}$ipv4$/i,
121 qr/^(?:$h:){2}:(?:$h:){1,3}$ipv4$/i,
122 qr/^(?:$h:){3}:(?:$h:){1,2}$ipv4$/i,
123 qr/^(?:$h:){4}:(?:$h:){1}$ipv4$/i,
124 # 1:2:3:4:5::1.2.3.4
125 qr/^(?:$h:){1,5}:$ipv4$/i,
126 # 1:2:3:4:5:6:1.2.3.4
127 qr/^(?:$h:){6}$ipv4$/i,
128 \&parse_mixed_ipv6v4_compressed,
129 ],
130 );
131  
132 15 ▶if ($base85ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base85ok) 0 15 15
133 0 $ipv6_patterns{'base85'} = [
134 $base85_re,
135 \&ipv6_parse_base85,
136 ],
137 }
138  
139 # ____ _ _
140 # | _ \ _ __(_)_ ____ _| |_ ___
141 # | |_) | '__| \ \ / / _` | __/ _ \
142 # | __/| | | |\ V / (_| | || __/
143 # |_| |_| |_| \_/ \__,_|\__\___|
144 #
145  
146 # Errors which include the package name and the subroutine name. This
147 # is for consistency with earlier versions of the module.
148  
149 sub mycroak
150 74 ▶{
Subroutine mycroak: called CC=1 SCAR=0.0
Pod undocumented
151 74 my ($message) = @_;
152 74 my @caller = caller (1);
153 74 croak $caller[3] . ' -- ' . $message;
154 }
155  
156 # Given one argument with a slash or two arguments, return them as two
157 # arguments, and check there are one or two arguments.
158  
159 sub getargs
160 21 ▶{
Subroutine getargs: called CC=3 SCAR=11.2
Pod undocumented
161 21 my ($ip, $pfx);
162 21 ▶ if (@_ == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2) { } 5 16 21
elsif (@_ == 1) { } 16 0 16
163 5 ($ip, $pfx) = @_;
164 }
165 elsif (@_ == 1) {
166 16 ($ip, $pfx) = split(m!/!, $_[0], 2)
167 }
168 else {
169 0 mycroak "wrong number of arguments (need 1 or 2)";
170 }
171 21 return ($ip, $pfx);
172 }
173  
174 # Match $ip against the regexes of type $type, or die.
175  
176 sub match_or_die
177 497 ▶{
Subroutine match_or_die: called CC=5 SCAR=16.1
Pod undocumented
178 497 my ($ip, $type) = @_;
179 # Instead of trying to construct a gigantic regex which only
180 # allows two colons in a row, just check here.
181 497 ▶ if ($ip =~ /:::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /:::/) 2 495 497
182 2 mycroak "invalid address $ip for type $type";
183 }
184 495 my $patterns = $ipv6_patterns{$type};
185 495 for my $p (@$patterns) {
186 # The last thing in the pattern is a code reference, so this
187 # match indicates no matches were found.
188 913 ▶ if (ref($p) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq "CODE") 33 880 913
189 33 mycroak "invalid address $ip for type $type";
190 }
191 880 ▶ if ($ip =~ $p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /$p/) 462 418 880
192 462 return;
193 }
194 }
195 }
196  
197 # Make the bit mask for "in_network_of_size".
198  
199 sub bitmask
200 3 ▶{
Subroutine bitmask: called CC=1 SCAR=0.0
Pod undocumented
201 3 my ($j) = @_;
202 3 my $bitmask = '1' x $j . '0' x (16 - $j);
203 3 my $k = unpack("n",pack("B16", $bitmask));
204 3 return $k;
205 }
206  
207 # ____
208 # | _ \ __ _ _ __ ___ ___ _ __ ___
209 # | |_) / _` | '__/ __|/ _ \ '__/ __|
210 # | __/ (_| | | \__ \ __/ | \__ \
211 # |_| \__,_|_| |___/\___|_| |___/
212 #
213  
214 # Private parser
215  
216 sub ipv6_parse_preferred
217 358 ▶{
Subroutine ipv6_parse_preferred: called CC=1 SCAR=0.0
Pod undocumented
218 358 my $ip = shift;
219 358 match_or_die ($ip, 'preferred');
220 350 my @pieces = split (/:/, $ip);
221 350 splice (@pieces, 8);
222 350 return map { hex } @pieces;
223 }
224  
225 # Private parser
226  
227 sub ipv6_parse_compressed
228 87 ▶{
Subroutine ipv6_parse_compressed: called CC=1 SCAR=0.0
Pod undocumented
229 87 my $ip = shift;
230 87 my $type = 'compressed';
231 87 match_or_die ($ip, $type);
232 82 my $colons = ($ip =~ tr/:/:/);
233 82 my $expanded = ':' x (9 - $colons);
234 82 $ip =~ s/::/$expanded/;
235 82 my @pieces = split (/:/, $ip, 8);
236 82 return map { hex } @pieces;
237 }
238  
239 sub parse_mixed_ipv6v4_compressed
240 26 ▶{
Subroutine parse_mixed_ipv6v4_compressed: called CC=1 SCAR=0.0
Pod undocumented
241 26 my $ip = shift;
242 26 match_or_die ($ip, 'ipv6v4');
243 26 my @result;
244 my $v4addr;
245 26 my $colons;
246 26 $colons = ($ip =~ tr/:/:/);
247 26 my $expanded = ':' x (8 - $colons);
248 26 $ip =~ s/::/$expanded/;
249 26 my @v6pcs = split(/:/, $ip, 7);
250 26 $v4addr = $v6pcs[-1];
251 26 splice(@v6pcs, 6);
252 26 push @result, map { hex } @v6pcs;
253 26 ipv4_validate($v4addr);
254 26 my @v4pcs = split(/\./, $v4addr);
255 26 splice(@v4pcs, 4);
256 26 push @result, unpack("n", pack("CC", @v4pcs[0,1]));
257 26 push @result, unpack("n", pack("CC", @v4pcs[2,3]));
258 26 return @result;
259 }
260  
261 # Private parser
262  
263 sub ipv6_parse_ipv4
264 12 ▶{
Subroutine ipv6_parse_ipv4: called CC=1 SCAR=0.0
Pod undocumented
265 12 my $ip = shift;
266 12 match_or_die ($ip, 'ipv4');
267 1 my @result;
268 my $v4addr;
269 1 my @v6pcs = split(/:/, $ip);
270 1 $v4addr = $v6pcs[-1];
271 1 splice(@v6pcs, 6);
272 1 push @result, map { hex } @v6pcs;
273 1 ipv4_validate($v4addr);
274 1 my @v4pcs = split(/\./, $v4addr);
275 1 push @result, unpack("n", pack("CC", @v4pcs[0,1]));
276 1 push @result, unpack("n", pack("CC", @v4pcs[2,3]));
277 1 return @result;
278 }
279  
280 # Private parser
281  
282 sub ipv6_parse_ipv4_compressed
283 14 ▶{
Subroutine ipv6_parse_ipv4_compressed: called CC=1 SCAR=0.0
Pod undocumented
284 14 my $ip = shift;
285 14 match_or_die ($ip, 'ipv4 compressed');
286 3 my @result;
287 my $v4addr;
288 3 my $colons;
289 3 $colons = ($ip =~ tr/:/:/);
290 3 my $expanded = ':' x (8 - $colons);
291 3 $ip =~ s/::/$expanded/;
292 3 my @v6pcs = split(/:/, $ip, 7);
293 3 $v4addr = $v6pcs[-1];
294 3 splice(@v6pcs, 6);
295 3 push @result, map { hex } @v6pcs;
296 3 ipv4_validate($v4addr);
297 3 my @v4pcs = split(/\./, $v4addr);
298 3 splice(@v4pcs, 4);
299 3 push @result, unpack("n", pack("CC", @v4pcs[0,1]));
300 3 push @result, unpack("n", pack("CC", @v4pcs[2,3]));
301 3 return @result;
302 }
303  
304 # Private parser
305  
306 sub ipv6_parse_base85
307 0 ▶{
Subroutine ipv6_parse_base85: not called CC=4 SCAR=30.0
Pod undocumented
308 0 ▶ if (! $base85ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base85ok) 0 0 0
309 0 carp "Math::Base85 is not installed";
310 0 return ();
311 }
312 0 my $ip = shift;
313 0 match_or_die ($ip, 'base85');
314 0 my $r;
315 0 my $bigint = Math::Base85::from_base85($ip);
316 0 my @result;
317 0 ▶ while ($bigint > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bigint > 0) 0 0 0
318 0 $r = $bigint & 0xffff;
319 0 unshift @result, sprintf("%d", $r);
320 0 $bigint = $bigint >> 16;
321 }
322 0 foreach $r ($#result+1..7) {
323 0 $result[$r] = 0;
324 }
325 0 return @result;
326 }
327  
328 # ____ _ _ _
329 # | _ \ _ _| |__ | (_) ___
330 # | |_) | | | | '_ \| | |/ __|
331 # | __/| |_| | |_) | | | (__
332 # |_| \__,_|_.__/|_|_|\___|
333 #
334  
335 # Public
336  
337 sub new
338 486 ▶{
Subroutine new: called CC=3 SCAR=11.1
Pod documented
339 486 my $proto = shift;
340 486 ▶ my $class = ref ($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
04860
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
341 486 my $maybe_ip = shift;
342 486 my $parser = ipv6_chkip ($maybe_ip);
343 486 ▶ if (ref $parser ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parser ne "CODE") 29 457 486
344 29 mycroak "invalid IPv6 address $maybe_ip";
345 }
346 457 my @hexadecets = $parser->($maybe_ip);
347 457 my $self = \@hexadecets;
348 457 bless $self, $class;
349 457 return $self;
350 }
351  
352 # Public
353  
354 sub ipv6_chkip
355 733 ▶{
Subroutine ipv6_chkip: called CC=5 SCAR=16.1
Pod documented
356 733 my $ip = shift;
357  
358 733 my $parser = undef;
359  
360 TYPE:
361 733 for my $k (keys %ipv6_patterns) {
362 2661 my @patlist = @{$ipv6_patterns{$k}};
363 PATTERN:
364 2661 for my $pattern (@patlist) {
365 13874 ▶ last PATTERN if (ref($pattern) eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if ref $pattern eq "CODE" 2083 11791 13874
366 11791 ▶ if ($ip =~ $pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /$pattern/) 578 11213 11791
367 578 $parser = $patlist[-1];
368 578 last TYPE;
369 }
370 }
371 }
372 733 return $parser;
373 }
374  
375 # Public
376  
377 sub ipv6_parse
378 16 ▶{
Subroutine ipv6_parse: called CC=6 SCAR=17.9
Pod documented
379 16 my ($ip, $pfx) = getargs (@_);
380  
381 16 ▶ if (! ipv6_chkip ($ip)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ipv6_chkip($ip)) 3 13 16
382 3 mycroak "invalid IPv6 address $ip";
383 }
384  
385 13 ▶ if (! defined $pfx) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pfx) 3 10 13
386 3 return $ip;
387 }
388  
389 10 $pfx =~ s/\s+//g;
390  
391 10 ▶ if ($pfx =~ /^[0-9]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pfx =~ /^[0-9]+$/) { } 6 4 10
392 6 ▶ if ($pfx > 128) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pfx > 128) 2 4 6
393 2 mycroak "invalid prefix length $pfx";
394 }
395 }
396 else {
397 4 mycroak "non-numeric prefix length $pfx";
398 }
399  
400 4 ▶ if (wantarray ()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 2 2 4
401 2 return ($ip, $pfx);
402 }
403 2 return "$ip/$pfx";
404 }
405  
406 # Public
407  
408 sub is_ipv6
409 7 ▶{
Subroutine is_ipv6: called CC=2 SCAR=6.9
Pod documented
410 7 my $r;
411 7 eval {
412 7 $r = ipv6_parse (@_);
413 };
414 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 3 7
415 4 return undef;
416 }
417 3 return $r;
418 }
419  
420 # Public
421  
422 sub to_string_preferred
423 26 ▶{
Subroutine to_string_preferred: called CC=2 SCAR=7.6
Pod documented
424 26 my $self = shift;
425 26 ▶ if (ref $self ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne __PACKAGE__) 0 26 26
426 0 $self = Net::IPv6Addr->new ($self);
427 }
428 26 return v6part (@$self);
429 }
430  
431 # Public
432  
433 sub to_string_compressed
434 426 ▶{
Subroutine to_string_compressed: called CC=8 SCAR=20.8
Pod documented
435 426 my $self = shift;
436 426 ▶ if (ref $self ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne __PACKAGE__) 212 214 426
437 212 $self = Net::IPv6Addr->new ($self);
438 }
439 426 my $expanded = v6part (@$self);
440 426 $expanded =~ s/^0:/:/;
441 426 $expanded =~ s/:0/:/g;
442 426 ▶ if ($expanded =~ s/:::::::/_/ or
Branch: 1 branch
BranchTrueFalseTotal
if ($expanded =~ s/:::::::/_/ or $expanded =~ s/::::::/_/ or $expanded =~ s/:::::/_/ or $expanded =~ s/::::/_/ or $expanded =~ s/:::/_/ or $expanded =~ s/::/_/) 409 17 426
Condition100%15/15
$expanded =~ s/:::::::/_/ or $expanded =~ s/::::::/_/ or $expanded =~ s/:::::/_/ or $expanded =~ s/::::/_/ or $expanded =~ s/:::/_/ or $expanded =~ s/::/_/
l!l&&r!l&&!r
3812817
$expanded =~ s/:::::::/_/ or $expanded =~ s/::::::/_/ or $expanded =~ s/:::::/_/ or $expanded =~ s/::::/_/ or $expanded =~ s/:::/_/
l!l&&r!l&&!r
3206145
$expanded =~ s/:::::::/_/ or $expanded =~ s/::::::/_/ or $expanded =~ s/:::::/_/ or $expanded =~ s/::::/_/
l!l&&r!l&&!r
26357106
$expanded =~ s/:::::::/_/ or $expanded =~ s/::::::/_/ or $expanded =~ s/:::::/_/
l!l&&r!l&&!r
152111163
$expanded =~ s/:::::::/_/ or $expanded =~ s/::::::/_/
l!l&&r!l&&!r
10250274
MC/DC100%6/6
$expanded =~ s/:::::::/_/ or $expanded =~ s/::::::/_/ or $expanded =~ s/:::::/_/ or $expanded =~ s/::::/_/ or $expanded =~ s/:::/_/ or $expanded =~ s/::/_/
$expanded =~ s/:::::::/_/ $expanded =~ s/::::::/_/ $expanded =~ s/:::::/_/ $expanded =~ s/::::/_/ $expanded =~ s/:::/_/ $expanded =~ s/::/_/
Truth table100%7/7
A or B or C or D or E or F
A= $expanded =~ s/:::::::/_/B= $expanded =~ s/::::::/_/C= $expanded =~ s/:::::/_/D= $expanded =~ s/::::/_/E= $expanded =~ s/:::/_/F= $expanded =~ s/::/_/
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
443 $expanded =~ s/::::::/_/ or
444 $expanded =~ s/:::::/_/ or
445 $expanded =~ s/::::/_/ or
446 $expanded =~ s/:::/_/ or
447 $expanded =~ s/::/_/
448 ) {
449 409 $expanded =~ s/:(?=:)/:0/g;
450 409 $expanded =~ s/^:(?=[0-9a-f])/0:/;
451 409 $expanded =~ s/([0-9a-f]):$/$1:0/;
452 409 $expanded =~ s/_/::/;
453 }
454 426 return $expanded;
455 }
456  
457 # Private
458  
459 sub bytes
460 24 ▶{
Subroutine bytes: called CC=1 SCAR=0.0
Pod undocumented
461 24 my ($in) = @_;
462 24 my $low = $in & 0xff;
463 24 my $high = $in >> 8;
464 24 return ($high, $low);
465 }
466  
467 # Private
468  
469 sub v4part
470 12 ▶{
Subroutine v4part: called CC=1 SCAR=0.0
Pod undocumented
471 12 my ($t, $b) = @_;
472 12 return join('.', bytes ($t), bytes ($b));
473 }
474  
475 # Private
476  
477 sub v6part
478 464 ▶{
Subroutine v6part: called CC=1 SCAR=0.0
Pod undocumented
479 464 return join(':', map { sprintf("%x", $_) } @_);
480 }
481  
482 # Public
483  
484 sub to_string_ipv4
485 6 ▶{
Subroutine to_string_ipv4: called CC=2 SCAR=7.2
Pod documented
486 6 my $self = shift;
487 6 ▶ if (ref $self ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne __PACKAGE__) 0 6 6
488 0 $self = Net::IPv6Addr->new ($self);
489 }
490 6 my $v6part = v6part (@$self[0..5]);
491 6 my $v4part = v4part (@$self[6, 7]);
492 6 return "$v6part:$v4part";
493 }
494  
495 # Public
496  
497 sub to_string_ipv4_compressed
498 6 ▶{
Subroutine to_string_ipv4_compressed: called CC=2 SCAR=7.1
Pod documented
499 6 my $self = shift;
500 6 ▶ if (ref $self ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne __PACKAGE__) 0 6 6
501 0 $self = Net::IPv6Addr->new ($self);
502 }
503 6 my $v6part = v6part (@$self[0..5]);
504 6 $v6part .= ':';
505 6 $v6part =~ s/(^|:)(0:)+/::/;
506 6 my $v4part = v4part (@$self[6, 7]);
507 6 return "$v6part$v4part";
508 }
509  
510 # Public
511  
512 sub to_string_base85
513 0 ▶{
Subroutine to_string_base85: not called CC=4 SCAR=30.0
Pod documented
514 0 ▶ if (! $base85ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base85ok) 0 0 0
515 0 carp "Math::Base85 is not installed";
516 0 return undef;
517 }
518 0 my $self = shift;
519 0 ▶ if (ref $self ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne __PACKAGE__) 0 0 0
520 0 $self = Net::IPv6Addr->new ($self);
521 }
522 0 my $bigint = new Math::BigInt("0");
523 0 for my $i (@{$self}[0..6]) {
524 0 $bigint = $bigint + $i;
525 0 $bigint = $bigint << 16;
526 }
527 0 $bigint = $bigint + $self->[7];
528 0 return Math::Base85::to_base85($bigint);
529 }
530  
531 # Public
532  
533 sub to_bigint
534 104 ▶{
Subroutine to_bigint: called CC=3 SCAR=11.1
Pod documented
535 104 my $self = shift;
536 104 ▶ if (ref $self ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne __PACKAGE__) 0 104 104
537 0 $self = Net::IPv6Addr->new ($self);
538 }
539 104 my $bigint = new Math::BigInt("0");
540 104 for my $i (@{$self}[0..6]) {
541 728 $bigint = $bigint + $i;
542 728 $bigint = $bigint << 16;
543 }
544 104 $bigint = $bigint + $self->[7];
545 104 $bigint =~ s/\+//;
546 104 return $bigint;
547 }
548  
549 # Public
550  
551 sub to_array
552 104 ▶{
Subroutine to_array: called CC=2 SCAR=7.4
Pod documented
553 104 my $self = shift;
554 104 ▶ if (ref $self ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne __PACKAGE__) 0 104 104
555 0 $self = Net::IPv6Addr->new ($self);
556 }
557 104 return map {sprintf "%04x", $_} @$self;
558 }
559  
560 # Public
561  
562 sub to_intarray
563 114 ▶{
Subroutine to_intarray: called CC=2 SCAR=7.6
Pod documented
564 114 my $self = shift;
565 114 ▶ if (ref $self ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne __PACKAGE__) 0 114 114
566 0 $self = Net::IPv6Addr->new ($self);
567 }
568 114 return @$self;
569 }
570  
571 # Public
572  
573 sub to_string_ip6_int
574 6 ▶{
Subroutine to_string_ip6_int: called CC=2 SCAR=7.1
Pod documented
575 6 my $self = shift;
576 6 ▶ if (ref $self ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne __PACKAGE__) 0 6 6
577 0 $self = Net::IPv6Addr->new ($self);
578 }
579 6 my $hexdigits = sprintf("%04x" x 8, @$self);
580 6 my @nibbles = ('INT', 'IP6', split(//, $hexdigits));
581 6 my $ptr = join('.', reverse @nibbles);
582 6 return $ptr . ".";
583 }
584  
585 # Private - validate a given netsize
586  
587 sub validate_netsize
588 15 ▶{
Subroutine validate_netsize: called CC=3 SCAR=11.0
Pod undocumented
589 15 my ($netsize) = @_;
590 15 ▶ if ($netsize !~ /^[0-9]+$/ || $netsize > 128) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $netsize =~ /^[0-9]+$/ or $netsize > 128) 1 14 15
Condition66%2/3
not $netsize =~ /^[0-9]+$/ or $netsize > 128
l!l&&r!l&&!r
0114
MC/DC50%1/2
not $netsize =~ /^[0-9]+$/ or $netsize > 128
not $netsize =~ /^[0-9]+$/ $netsize > 128
Truth table66%2/3
not(A) or B
A= not $netsize =~ /^[0-9]+$/B= $netsize > 128
ABresult
1X1
011
000
591 1 mycroak "invalid network size $netsize";
592 }
593 }
594  
595 # Public
596  
597 sub in_network_of_size
598 10 ▶{
Subroutine in_network_of_size: called CC=7 SCAR=21.6
Pod documented
599 10 my $self = shift;
600 10 ▶ if (ref $self ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne __PACKAGE__) 0 10 10
601 0 ▶ if ($self =~ m!(.+)/(.+)!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self =~ m[(.+)/(.+)]) 0 0 0
602 0 unshift @_, $2;
603 0 $self = $1;
604 }
605 0 $self = Net::IPv6Addr->new($self);
606 }
607 10 my $netsize = shift;
608 10 ▶ if (! defined $netsize) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $netsize) 0 10 10
609 0 mycroak "network size not given";
610 }
611 10 $netsize =~ s!/!!;
612 10 validate_netsize ($netsize);
613 10 my @parts = @$self;
614 10 my $i = int ($netsize / 16);
615 10 ▶ if ($i < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 8) 10 0 10
616 10 my $j = $netsize % 16;
617 10 ▶ if ($j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j) 3 7 10
618 # If $netsize is not a multiple of 16, truncate the lowest
619 # 16-$j bits of the $ith element of @parts.
620 3 $parts[$i] &= bitmask ($j);
621 # Jump over this element.
622 3 $i++;
623 }
624 # Set all the remaining lower parts to zero.
625 10 for ($i..$#parts) {
626 41 $parts[$_] = 0;
627 }
628 }
629 10 return bless \@parts;
630 }
631  
632 # Public
633  
634 sub in_network
635 5 ▶{
Subroutine in_network: called CC=7 SCAR=20.4
Pod documented
636 5 my $self = shift;
637 5 ▶ if (ref $self ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne __PACKAGE__) 0 5 5
638 0 $self = Net::IPv6Addr->new ($self);
639 }
640 5 my ($net, $netsize) = getargs (@_);
641 5 ▶ unless (defined $netsize) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $netsize) 0 5 5
642 0 mycroak "not enough parameters, need netsize";
643 }
644 5 $netsize =~ s!/!!;
645 5 validate_netsize ($netsize);
646 4 ▶ if (! ref $net) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $net) 4 0 4
647 4 $net = Net::IPv6Addr->new($net);
648 }
649 4 my @s = $self->in_network_of_size($netsize)->to_intarray;
650 4 my @n = $net->in_network_of_size($netsize)->to_intarray;
651 4 my $i = int ($netsize / 16) + 1;
652 4 ▶ if ($i > $#s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $#s) 0 4 4
653 0 $i = $#s;
654 }
655 4 for (0..$i) {
656 17 ▶ if ($s[$_] != $n[$_]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s[$_] != $n[$_]) 1 16 17
657 1 return undef;
658 }
659 }
660 3 return 1;
661 }
662  
663 # Public
664  
665 sub from_bigint
666 104 ▶{
Subroutine from_bigint: called CC=4 SCAR=14.2
Pod documented
667 104 my ($big) = @_;
668 # Input is a scalar or a Math::BigInt object.
669 104 ▶ if (! ref ($big)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $big) 0 104 104
670 0 $big = Math::BigInt->new ($big);
671 }
672 104 ▶ if (ref ($big) ne 'Math::BigInt') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $big ne "Math::BigInt") 0 104 104
673 0 mycroak "Cannot process non-scalar, non-Math::BigInt input";
674 }
675 # Convert the number to a hexadecimal string
676 104 my $hex = $big->to_hex ();
677 # Pad if necessary for the colon placement
678 104 ▶ if (length ($hex) < 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $hex < 32) 66 38 104
679 66 my $leading = '0' x (32 - length ($hex));
680 66 $hex = $leading . $hex;
681 }
682 # Reversing the string makes adding colons with a substitution
683 # operator easier.
684 104 my $ipr = reverse $hex;
685 104 $ipr =~ s/(....)/$1:/g;
686 104 $ipr = reverse $ipr;
687 # Remove the excess colon.
688 104 $ipr =~ s/^://;
689 # Should be OK now, let "new" handle any further issues.
690 104 return Net::IPv6Addr->new ($ipr);
691 }
692  
693 15 1;
↑ Summary