lib/Net/Netmask.pm

filter: Statementstmt 96.2% 531 / 552 Branchbran 90.0% 342 / 380 Conditioncond 90.9% 110 / 121 MC/DCmcdc 83.7% 62 / 74 Subroutinesub 96.9% 64 / 66 Podpod 60.0% 33 / 55 totaltotal 91.5% 1142 / 1248 SCAR 55.3
CC
236
Cov
93%
new
68.6
findOuterNetblock
14.0
findAllNetblock
13.1
CRAP 252.4
« blib/lib/Net/Netmask.pm ↑ Summary
1 # Copyright (C) 1998-2006 David Muir Sharnoff <muir@idiom.org>
2 # Copyright (C) 2011-2013 Google, Inc.
3 # Copyright (C) 2018-2021 Joelle Maslak <jmaslak@antelope.net>
4  
5 package Net::Netmask;
6 8 $Net::Netmask::VERSION = '2.0002';
7 8 ▶use 5.006_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: Understand and manipulate IP netmasks
10  
11 # Disable one-arg bless to preserve the existing interface.
12 ## no critic (ClassHierarchies::ProhibitOneArgBless)
13  
14 8 require Exporter;
15 8 @ISA = qw(Exporter);
16 8 @EXPORT = qw(findNetblock findOuterNetblock findAllNetblock
17 cidrs2contiglists range2cidrlist sort_by_ip_address
18 dumpNetworkTable sort_network_blocks cidrs2cidrs
19 cidrs2inverse);
20 8 @EXPORT_OK = (
21 @EXPORT, qw(ascii2int int2quad quad2int %quadmask2bits
22 %quadhostmask2bits imask i6mask int2ascii sameblock cmpblocks contains)
23 );
24  
25 8 my $remembered = {};
26 8 my %imask2bits;
27 my %size2bits;
28 8 my @imask;
29 8 my @i6mask;
30  
31 8 our $SHORTNET_DEFAULT = undef;
32  
33 8 ▶use vars qw($error $debug %quadmask2bits %quadhostmask2bits);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 8 $debug = 1;
35  
36 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 8 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 8 ▶use POSIX qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 use overload
42 8 '""' => \&desc,
43 '<=>' => \&cmp_net_netmask_block,
44 'cmp' => \&cmp_net_netmask_block,
45 8 ▶ 'fallback' => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 sub new {
48 1900 ▶ my ( $package, $net, @params) = @_;
Subroutine new: called CC=68 SCAR=42.3
Pod undocumented
49  
50 1900 my $mask = '';
51 1900 ▶ if (@params % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params % 2) 81 1819 1900
52 81 $mask = shift(@params);
53 81 ▶ $mask = '' if !defined($mask);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mask 38 43 81
54 }
55 1900 my (%options) = @params;
56 1900 ▶ my $shortnet = ( ( exists($options{shortnet}) && $options{shortnet} ) || $SHORTNET_DEFAULT );
Condition100%6/6
exists $options{'shortnet'} && $options{'shortnet'} || $Net::Netmask::SHORTNET_DEFAULT
l!l&&r!l&&!r
6211873
exists $options{"shortnet"} and $options{"shortnet"}
!ll&&!rl&&r
1879156
MC/DC100%3/3
exists $options{'shortnet'} && $options{'shortnet'} || $Net::Netmask::SHORTNET_DEFAULT
exists $options{"shortnet"} $options{"shortnet"} $Net::Netmask::SHORTNET_DEFAULT
Truth table80%4/5
A and B || C
A= exists $options{"shortnet"}B= $options{"shortnet"}C= $Net::Netmask::SHORTNET_DEFAULT
ABCresult
11X1
0X11
1011
0X00
1000
57  
58 1900 my $base;
59 my $bits;
60 1900 my $ibase;
61 1900 my $proto = 'IPv4';
62 1900 undef $error;
63  
64 1900 ▶ if ( $net =~ m,^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/([0-9]+)$, ) {
Branch: 17 branches
BranchTrueFalseTotal
if ($net =~ m[^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/([0-9]+)$]) { } 417 1483 1900
elsif ($net =~ m[^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)[:/]([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$]) { } 6 1477 1483
elsif ($net =~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)[#]([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/) { } 4 1473 1477
elsif ($net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ and $mask =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) { } 7 1466 1473
elsif ($net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ and $mask =~ /0x[a-f0-9]+/i) { } 6 1460 1466
elsif ($net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ and not $mask) { } 770 690 1460
elsif ($net =~ /^[0-9]+\.[0-9]+\.[0-9]+$/ and not $mask and $shortnet) { } 2 688 690
elsif ($net =~ /^[0-9]+\.[0-9]+$/ and not $mask and $shortnet) { } 2 686 688
elsif ($net =~ /^[0-9]+$/ and not $mask and $shortnet) { } 2 684 686
elsif ($net =~ m[^([0-9]+\.[0-9]+\.[0-9]+)/([0-9]+)$] and $shortnet) { } 2 682 684
elsif ($net =~ m[^([0-9]+\.[0-9]+)/([0-9]+)$] and $shortnet) { } 2 680 682
elsif ($net =~ m[^([0-9]+)/([0-9]+)$] and $shortnet) { } 2 678 680
elsif ($net eq 'default' or $net eq 'any') { } 5 673 678
elsif ($net =~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\s*-\s*([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/) { } 12 661 673
elsif ($net =~ m[^([0-9a-f]*:[0-9a-f]*:[0-9a-f:]*)/([0-9]+)$]) { } 326 335 661
elsif ($net =~ /^([0-9a-f]*:[0-9a-f]*:[0-9a-f:]*)$/) { } 272 63 335
elsif ($net eq 'default6' or $net eq 'any6') { } 4 59 63
65 417 ( $base, $bits ) = ( $1, $2 );
66 } elsif ( $net =~ m,^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)[:/]([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$, ) {
67 6 $base = $1;
68 6 my $quadmask = $2;
69 6 ▶ if ( exists $quadmask2bits{$quadmask} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Net::Netmask::quadmask2bits{$quadmask}) { } 4 2 6
70 4 $bits = $quadmask2bits{$quadmask};
71 } else {
72 2 $error = "illegal netmask: $quadmask";
73 }
74 } elsif ( $net =~ m,^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)[#]([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$, ) {
75 4 $base = $1;
76 4 my $hostmask = $2;
77 4 ▶ if ( exists $quadhostmask2bits{$hostmask} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Net::Netmask::quadhostmask2bits{$hostmask}) { } 2 2 4
78 2 $bits = $quadhostmask2bits{$hostmask};
79 } else {
80 2 $error = "illegal hostmask: $hostmask";
81 }
82 ▶ } elsif ( ( $net =~ m,^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$, )
Condition100%3/3
$net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ and $mask =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/
!ll&&!rl&&r
6787887
MC/DC100%2/2
$net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ and $mask =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/
$net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ $mask =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/
Truth table100%3/3
A and B
A= $net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/B= $mask =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/
ABresult
0X0
100
111
83 && ( $mask =~ m,[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$, ) )
84 {
85 7 $base = $net;
86 7 ▶ if ( exists $quadmask2bits{$mask} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Net::Netmask::quadmask2bits{$mask}) { } 5 2 7
87 5 $bits = $quadmask2bits{$mask};
88 } else {
89 2 $error = "illegal netmask: $mask";
90 }
91 ▶ } elsif ( ( $net =~ m,^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$, )
Condition100%3/3
$net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ and $mask =~ /0x[a-f0-9]+/i
!ll&&!rl&&r
6787826
MC/DC100%2/2
$net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ and $mask =~ /0x[a-f0-9]+/i
$net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ $mask =~ /0x[a-f0-9]+/i
Truth table100%3/3
A and B
A= $net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/B= $mask =~ /0x[a-f0-9]+/i
ABresult
0X0
100
111
92 && ( $mask =~ m,0x[a-f0-9]+,i ) )
93 {
94 6 $base = $net;
95 6 my $imask = hex($mask);
96 6 ▶ if ( exists $imask2bits{$imask} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $imask2bits{$imask}) { } 4 2 6
97 4 $bits = $imask2bits{$imask};
98 } else {
99 2 $error = "illegal netmask: $mask ($imask)";
100 }
101 ▶ } elsif ( $net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ && !$mask ) {
Condition100%3/3
$net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ and not $mask
!ll&&!rl&&r
67812770
MC/DC100%2/2
$net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ and not $mask
$net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ not $mask
Truth table100%3/3
A and not(B)
A= $net =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/B= not $mask
ABresult
0X0
100
111
102 770 ( $base, $bits ) = ( $net, 32 );
103 ▶ } elsif ( $net =~ /^[0-9]+\.[0-9]+\.[0-9]+$/ && !$mask && $shortnet ) {
Condition100%6/6
$net =~ /^[0-9]+\.[0-9]+\.[0-9]+$/ and not $mask and $shortnet
!ll&&!rl&&r
68442
$net =~ /^[0-9]+\.[0-9]+\.[0-9]+$/ and not $mask
!ll&&!rl&&r
68046
MC/DC100%3/3
$net =~ /^[0-9]+\.[0-9]+\.[0-9]+$/ and not $mask and $shortnet
$net =~ /^[0-9]+\.[0-9]+\.[0-9]+$/ not $mask $shortnet
Truth table100%4/4
A and not(B) and C
A= $net =~ /^[0-9]+\.[0-9]+\.[0-9]+$/B= not $maskC= $shortnet
ABCresult
0XX0
10X0
1100
1111
104 2 ( $base, $bits ) = ( "$net.0", 24 );
105 ▶ } elsif ( $net =~ /^[0-9]+\.[0-9]+$/ && !$mask && $shortnet ) {
Condition100%6/6
$net =~ /^[0-9]+\.[0-9]+$/ and not $mask and $shortnet
!ll&&!rl&&r
68062
$net =~ /^[0-9]+\.[0-9]+$/ and not $mask
!ll&&!rl&&r
67468
MC/DC100%3/3
$net =~ /^[0-9]+\.[0-9]+$/ and not $mask and $shortnet
$net =~ /^[0-9]+\.[0-9]+$/ not $mask $shortnet
Truth table100%4/4
A and not(B) and C
A= $net =~ /^[0-9]+\.[0-9]+$/B= not $maskC= $shortnet
ABCresult
0XX0
10X0
1100
1111
106 2 ( $base, $bits ) = ( "$net.0.0", 16 );
107 ▶ } elsif ( $net =~ /^[0-9]+$/ && !$mask && $shortnet ) {
Condition100%6/6
$net =~ /^[0-9]+$/ and not $mask and $shortnet
!ll&&!rl&&r
68222
$net =~ /^[0-9]+$/ and not $mask
!ll&&!rl&&r
67664
MC/DC100%3/3
$net =~ /^[0-9]+$/ and not $mask and $shortnet
$net =~ /^[0-9]+$/ not $mask $shortnet
Truth table100%4/4
A and not(B) and C
A= $net =~ /^[0-9]+$/B= not $maskC= $shortnet
ABCresult
0XX0
10X0
1100
1111
108 2 ( $base, $bits ) = ( "$net.0.0.0", 8 );
109 ▶ } elsif ( $net =~ m,^([0-9]+\.[0-9]+\.[0-9]+)/([0-9]+)$, && $shortnet ) {
Condition100%3/3
$net =~ m[^([0-9]+\.[0-9]+\.[0-9]+)/([0-9]+)$] and $shortnet
!ll&&!rl&&r
67842
MC/DC100%2/2
$net =~ m[^([0-9]+\.[0-9]+\.[0-9]+)/([0-9]+)$] and $shortnet
$net =~ m[^([0-9]+\.[0-9]+\.[0-9]+)/([0-9]+)$] $shortnet
Truth table100%3/3
A and B
A= $net =~ m[^([0-9]+\.[0-9]+\.[0-9]+)/([0-9]+)$]B= $shortnet
ABresult
0X0
100
111
110 2 ( $base, $bits ) = ( "$1.0", $2 );
111 ▶ } elsif ( $net =~ m,^([0-9]+\.[0-9]+)/([0-9]+)$, && $shortnet ) {
Condition100%3/3
$net =~ m[^([0-9]+\.[0-9]+)/([0-9]+)$] and $shortnet
!ll&&!rl&&r
67642
MC/DC100%2/2
$net =~ m[^([0-9]+\.[0-9]+)/([0-9]+)$] and $shortnet
$net =~ m[^([0-9]+\.[0-9]+)/([0-9]+)$] $shortnet
Truth table100%3/3
A and B
A= $net =~ m[^([0-9]+\.[0-9]+)/([0-9]+)$]B= $shortnet
ABresult
0X0
100
111
112 2 ( $base, $bits ) = ( "$1.0.0", $2 );
113 ▶ } elsif ( $net =~ m,^([0-9]+)/([0-9]+)$, && $shortnet ) {
Condition100%3/3
$net =~ m[^([0-9]+)/([0-9]+)$] and $shortnet
!ll&&!rl&&r
67622
MC/DC100%2/2
$net =~ m[^([0-9]+)/([0-9]+)$] and $shortnet
$net =~ m[^([0-9]+)/([0-9]+)$] $shortnet
Truth table100%3/3
A and B
A= $net =~ m[^([0-9]+)/([0-9]+)$]B= $shortnet
ABresult
0X0
100
111
114 2 ( $base, $bits ) = ( "$1.0.0.0", $2 );
115 ▶ } elsif ( $net eq 'default' || $net eq 'any' ) {
Condition100%3/3
$net eq "default" or $net eq "any"
l!l&&r!l&&!r
41673
MC/DC100%2/2
$net eq "default" or $net eq "any"
$net eq "default" $net eq "any"
Truth table100%3/3
A or B
A= $net eq "default"B= $net eq "any"
ABresult
1X1
011
000
116 5 ( $base, $bits ) = ( "0.0.0.0", 0 );
117 } elsif ( $net =~ m,^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\s*-\s*([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$, )
118 {
119 # whois format
120 12 $ibase = quad2int($1);
121 12 my $end = quad2int($2);
122 12 ▶ $error = "illegal dotted quad: $net"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ibase and defined $end 4 8 12
Condition100%3/3
defined $ibase and defined $end
!ll&&!rl&&r
228
MC/DC100%2/2
defined $ibase and defined $end
defined $ibase defined $end
Truth table100%3/3
A and B
A= defined $ibaseB= defined $end
ABresult
0X0
100
111
123 unless defined($ibase) && defined($end);
124 12 ▶ my $diff = ( $end || 0 ) - ( $ibase || 0 ) + 1;
Condition100%4/4
$end || 0
l!l
102
$ibase || 0
l!l
102
MC/DC100%1/1
$end || 0
$end
MC/DC100%1/1
$ibase || 0
$ibase
Truth table100%2/2
A
A= $end
Aresult
11
00
Truth table100%2/2
A
A= $ibase
Aresult
11
00
125 12 $bits = $size2bits{$diff};
126 12 ▶ $error = "could not find exact fit for $net"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $Net::Netmask::error and !defined($bits) || $ibase & ~$imask[$bits] 4 8 12
Condition100%6/6
not defined $Net::Netmask::error and !defined($bits) || $ibase & ~$imask[$bits]
!ll&&!rl&&r
444
not defined $bits or $ibase & ~$imask[$bits]
l!l&&r!l&&!r
224
MC/DC66%2/3
not defined $Net::Netmask::error and !defined($bits) || $ibase & ~$imask[$bits]
not defined $Net::Netmask::error not defined $bits $ibase & ~$imask[$bits]
Truth table75%3/4
not(A) and not(B) or C
A= not defined $Net::Netmask::errorB= not defined $bitsC= $ibase & ~$imask[$bits]
ABCresult
0XX0
1000
11X1
1011
127 if !defined $error
128 && ( !defined $bits
129 || ( $ibase & ~$imask[$bits] ) );
130 } elsif ( $net =~ m,^([0-9a-f]*:[0-9a-f]*:[0-9a-f:]*)/([0-9]+)$, ) {
131 # IPv6 with netmask - ex: 2001:db8::/32
132 326 ▶ if ( $mask ne '' ) { $error = "mask ignored for IPv6 address" }
Branch: 1 branch
BranchTrueFalseTotal
if ($mask ne "") 0 326 326
133 326 ( $base, $bits, $proto ) = ( $1, $2, 'IPv6' );
134 } elsif ( $net =~ m,^([0-9a-f]*:[0-9a-f]*:[0-9a-f:]*)$, ) {
135 # IPv6 without netmask - ex: 2001:db8::1234
136 272 ▶ if ( $mask ne '' ) { $error = "mask ignored for IPv6 address" }
Branch: 1 branch
BranchTrueFalseTotal
if ($mask ne "") 0 272 272
137 272 ( $base, $bits, $proto ) = ( $1, 128, 'IPv6' );
138 ▶ } elsif ( $net eq 'default6' || $net eq 'any6' ) {
Condition66%2/3
$net eq "default6" or $net eq "any6"
l!l&&r!l&&!r
4059
MC/DC50%1/2
$net eq "default6" or $net eq "any6"
$net eq "default6" $net eq "any6"
Truth table66%2/3
A or B
A= $net eq "default6"B= $net eq "any6"
ABresult
1X1
011
000
139 4 ▶ if ( $mask ne '' ) { $error = "mask ignored for IPv6 address" }
Branch: 1 branch
BranchTrueFalseTotal
if ($mask ne "") 0 4 4
140 4 ( $base, $bits, $proto ) = ( "::", 0, 'IPv6' );
141 } else {
142 59 $error = "could not parse $net";
143 59 ▶ $error .= " $mask" if $mask;
Branch: 1 branch
BranchTrueFalseTotal
if $mask 30 29 59
144 }
145  
146 1900 ▶ carp $error if $error && $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Netmask::error and $Net::Netmask::debug 0 1900 1900
Condition66%2/3
$Net::Netmask::error and $Net::Netmask::debug
!ll&&!rl&&r
1825750
MC/DC0%0/2
$Net::Netmask::error and $Net::Netmask::debug
$Net::Netmask::error $Net::Netmask::debug
Truth table66%2/3
A and B
A= $Net::Netmask::errorB= $Net::Netmask::debug
ABresult
0X0
100
111
147  
148 1900 ▶ $bits = 0 unless $bits;
Branch: 1 branch
BranchTrueFalseTotal
unless $bits 85 1815 1900
149 1900 ▶ if ( ( $proto eq 'IPv4' ) && ( $bits > 32 ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($proto eq 'IPv4' and $bits > 32) { } 2 1898 1900
elsif ($proto eq 'IPv6' and $bits > 128) { } 2 3192 3194
Condition100%3/3
$proto eq "IPv4" and $bits > 32
!ll&&!rl&&r
60212962
MC/DC100%2/2
$proto eq "IPv4" and $bits > 32
$proto eq "IPv4" $bits > 32
Truth table100%3/3
A and B
A= $proto eq "IPv4"B= $bits > 32
ABresult
0X0
100
111
150 2 ▶ $error = "illegal number of bits: $bits"
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::Netmask::error 2 0 2
151 unless $error;
152 2 $bits = 32;
153 ▶ } elsif ( ( $proto eq 'IPv6' ) && ( $bits > 128 ) ) {
Condition100%3/3
$proto eq "IPv6" and $bits > 128
!ll&&!rl&&r
12966002
MC/DC100%2/2
$proto eq "IPv6" and $bits > 128
$proto eq "IPv6" $bits > 128
Truth table100%3/3
A and B
A= $proto eq "IPv6"B= $bits > 128
ABresult
0X0
100
111
154 2 ▶ $error = "illegal number of bits: $bits"
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::Netmask::error 2 0 2
155 unless $error;
156 2 $bits = 128;
157 }
158  
159 1900 ▶ $ibase = ascii2int( ( $base || '::' ), $proto ) unless (defined $ibase or $error);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ibase or $Net::Netmask::error 1817 83 1900
Condition80%4/5
defined $ibase or $Net::Netmask::error
l!l&&r!l&&!r
10731817
$base || '::'
l!l
18170
MC/DC100%2/2
defined $ibase or $Net::Netmask::error
defined $ibase $Net::Netmask::error
MC/DC0%0/1
$base || '::'
$base
Truth table100%3/3
A or B
A= defined $ibaseB= $Net::Netmask::error
ABresult
1X1
011
000
Truth table50%1/2
A
A= $base
Aresult
11
00
160 1900 ▶ unless ( defined($ibase) || defined($error) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ibase or defined $Net::Netmask::error) 24 1876 1900
Condition100%3/3
defined $ibase or defined $Net::Netmask::error
l!l&&r!l&&!r
18037324
MC/DC100%2/2
defined $ibase or defined $Net::Netmask::error
defined $ibase defined $Net::Netmask::error
Truth table100%3/3
A or B
A= defined $ibaseB= defined $Net::Netmask::error
ABresult
1X1
011
000
161 24 $error = "could not parse $net";
162 24 ▶ $error .= " $mask" if $mask;
Branch: 1 branch
BranchTrueFalseTotal
if $mask 4 20 24
163 }
164  
165 1900 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Netmask::error) 103 1797 1900
166 103 $ibase = 0;
167 103 $bits = 0;
168 }
169  
170 1900 $ibase = i_getnet_addr( $ibase, $bits, $proto );
171  
172 1900 ▶ return bless {
Branch: 1 branch
BranchTrueFalseTotal
$Net::Netmask::error ? : 103 1797 1900
173 'IBASE' => $ibase,
174 'BITS' => $bits,
175 'PROTOCOL' => $proto,
176 ( $error ? ( 'ERROR' => $error ) : () ),
177 };
178 }
179  
180 sub i_getnet_addr {
181 17743 ▶ my ( $ibase, $bits, $proto ) = @_;
Subroutine i_getnet_addr: called CC=3 SCAR=11.2
Pod undocumented
182  
183 17743 ▶ if ( !defined($ibase) ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ibase) 0 17743 17743
184  
185 17743 ▶ if ( $proto eq 'IPv4' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq 'IPv4') { } 3784 13959 17743
186 3784 return $ibase & $imask[$bits];
187 } else {
188 13959 return $ibase & $i6mask[$bits];
189 }
190 }
191  
192 sub new2 {
193 50 ▶ goto &safe_new;
Subroutine new2: called CC=1 SCAR=0.0
Pod undocumented
194 }
195  
196 sub safe_new {
197 105 ▶ local ($debug) = 0;
Subroutine safe_new: called CC=2 SCAR=6.9
Pod undocumented
198 105 my $net = new(@_);
199 105 ▶ return if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Netmask::error 52 53 105
200 53 return $net;
201 }
202  
203 52 ▶sub errstr { return $error; }
Subroutine errstr: called CC=1 SCAR=0.0
Pod undocumented
204 2 ▶sub debug { my $this = shift; return ( @_ ? $debug = shift : $debug ) }
Subroutine debug: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 0 2
205  
206 1021 ▶sub base { my ($this) = @_; return int2ascii( $this->{IBASE}, $this->{PROTOCOL} ); }
Subroutine base: called CC=1 SCAR=0.0
Pod documented
207 25 ▶sub bits { my ($this) = @_; return $this->{'BITS'}; }
Subroutine bits: called CC=1 SCAR=0.0
Pod documented
208 21 ▶sub protocol { my ($this) = @_; return $this->{'PROTOCOL'}; }
Subroutine protocol: called CC=1 SCAR=0.0
Pod documented
209  
210 sub size {
211 700 ▶ my ($this) = @_;
Subroutine size: called CC=2 SCAR=6.9
Pod documented
212  
213 700 ▶ if ( $this->{PROTOCOL} eq 'IPv4' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'PROTOCOL'} eq 'IPv4') { } 392 308 700
214 392 return 2**( 32 - $this->{'BITS'} );
215 } else {
216 308 return Math::BigInt->new(2)->bpow( 128 - $this->{'BITS'} );
217 }
218 }
219  
220 sub next { ## no critic: (Subroutines::ProhibitBuiltinHomonyms)
221 3 ▶ my ($this) = @_;
Subroutine next: called CC=2 SCAR=6.9
Pod documented
222 # TODO: CONSOLIDATE
223 3 ▶ if ( $this->{PROTOCOL} eq 'IPv4' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'PROTOCOL'} eq 'IPv4') { } 1 2 3
224 1 return int2quad( $this->{'IBASE'} + $this->size() );
225 } else {
226 2 return $this->_ipv6next( $this->size );
227 }
228 }
229  
230 sub broadcast {
231 2 ▶ my ($this) = @_;
Subroutine broadcast: called CC=1 SCAR=0.0
Pod documented
232  
233 2 return int2ascii( $this->{'IBASE'} + $this->size() - 1, $this->{PROTOCOL} );
234 }
235  
236 8 *first = \&base;
237 8 *last = \&broadcast;
238  
239 sub desc {
240 480 ▶ return int2ascii( $_[0]->{IBASE}, $_[0]->{PROTOCOL} ) . '/' . $_[0]->{BITS};
Subroutine desc: called CC=1 SCAR=0.0
Pod documented
241 }
242  
243 sub imask {
244 264 ▶ return ( 2**32 - ( 2**( 32 - $_[0] ) ) );
Subroutine imask: called CC=1 SCAR=0.0
Pod undocumented
245 }
246  
247 sub i6mask {
248 1032 ▶ my $bits = shift;
Subroutine i6mask: called CC=1 SCAR=0.0
Pod undocumented
249 1032 return Math::BigInt->new(2)->bpow(128) - Math::BigInt->new(2)->bpow( 128 - $bits );
250 }
251  
252 sub mask {
253 21 ▶ my ($this) = @_;
Subroutine mask: called CC=2 SCAR=6.9
Pod documented
254  
255 21 ▶ if ( $this->{PROTOCOL} eq 'IPv4' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'PROTOCOL'} eq 'IPv4') { } 15 6 21
256 15 return int2quad( $imask[ $this->{'BITS'} ] );
257 } else {
258 6 return int2ascii( $i6mask[ $this->{'BITS'} ], $this->{PROTOCOL} );
259 }
260 }
261  
262 sub hostmask {
263 4 ▶ my ($this) = @_;
Subroutine hostmask: called CC=2 SCAR=6.9
Pod documented
264  
265 4 ▶ if ( $this->{PROTOCOL} eq 'IPv4' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'PROTOCOL'} eq 'IPv4') { } 1 3 4
266 1 return int2quad( ~$imask[ $this->{BITS} ] );
267 } else {
268 3 return int2ascii( $i6mask[ $this->{BITS} ] ^ $i6mask[128], $this->{PROTOCOL} );
269 }
270 }
271  
272 sub nth {
273 528 ▶ my ( $this, $index, $bitstep ) = @_;
Subroutine nth: called CC=6 SCAR=17.9
Pod documented
274  
275 528 ▶ my $maxbits = $this->{PROTOCOL} eq 'IPv4' ? 32 : 128;
Branch: 1 branch
BranchTrueFalseTotal
$this->{'PROTOCOL'} eq 'IPv4' ? : 265 263 528
276  
277 528 my $size = $this->size();
278 528 my $ibase = $this->{'IBASE'};
279 528 ▶ $bitstep = $maxbits unless $bitstep;
Branch: 1 branch
BranchTrueFalseTotal
unless $bitstep 527 1 528
280 528 my $increment = 2**( $maxbits - $bitstep );
281 528 $index *= $increment;
282 528 ▶ $index += $size if $index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 6 522 528
283 528 ▶ return if $index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 2 526 528
284 526 ▶ return if $index >= $size;
Branch: 1 branch
BranchTrueFalseTotal
if $index >= $size 2 524 526
285  
286 524 my $i = $ibase + $index;
287 524 return int2ascii( $i, $this->{PROTOCOL} );
288 }
289  
290 sub enumerate {
291 5 ▶ my ( $this, $bitstep ) = @_;
Subroutine enumerate: called CC=7 SCAR=19.5
Pod documented
292 5 my $proto = $this->{PROTOCOL};
293  
294 # Set default step size by protocol
295 5 ▶ $bitstep = ( $proto eq 'IPv4' ? 32 : 128 ) unless $bitstep;
Branch: 2 branches
BranchTrueFalseTotal
unless $bitstep 3 2 5
$proto eq 'IPv4' ? : 2 1 3
296  
297 5 my $size = $this->size();
298  
299 5 my @ary;
300 ### We should be able to consolidate this
301 5 ▶ if ( $proto eq 'IPv4' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq 'IPv4') { } 3 2 5
302 3 my $increment = 2**( 32 - $bitstep );
303 3 my $ibase = $this->{'IBASE'};
304 3 ▶ for ( my $i = 0; $i < $size; $i += $increment ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 8240 3 8243
305 8240 push( @ary, int2quad( $ibase + $i ) );
306 }
307 } else {
308 2 my $increment = Math::BigInt->new(2)->bpow( 128 - $bitstep );
309  
310 2 ▶ if ( ( $size / $increment ) > 1_000_000_000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size / $increment > 1000000000) 1 1 2
311 # Let's help the user out and catch really obvious issues.
312 # Asking for a billion IP addresses is probably one of them.
313 #
314 # That said, please contact the author if this number causes
315 # you issues!
316 1 confess("More than 1,000,000,000 results would be returned, dieing");
317 }
318  
319 1 ▶ for ( my $i = Math::BigInt->new(0); $i < $size; $i += $increment ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 256 1 257
320 256 push( @ary, $this->_ipv6next($i) );
321 }
322 }
323 4 return @ary;
324 }
325  
326 sub _ipv6next {
327 258 ▶ my ( $this, $bitstep ) = @_;
Subroutine _ipv6next: called CC=1 SCAR=0.0
328  
329 258 my $istart = $this->{IBASE};
330 258 my $val = $istart + $bitstep;
331  
332 258 return ipv6Cannonical( int2ascii( $val, $this->{PROTOCOL} ) );
333 }
334  
335 sub inaddr {
336 8 ▶ my ($this) = @_;
Subroutine inaddr: called CC=2 SCAR=6.9
Pod documented
337  
338 8 ▶ if ( $this->{PROTOCOL} eq 'IPv4' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'PROTOCOL'} eq 'IPv4') { } 4 4 8
339 4 return $this->inaddr4();
340 } else {
341 4 return $this->inaddr6();
342 }
343 }
344  
345 sub inaddr4 {
346 4 ▶ my ($this) = @_;
Subroutine inaddr4: called CC=3 SCAR=11.0
Pod undocumented
347 4 my $ibase = $this->{'IBASE'};
348 4 my $blocks = floor( $this->size() / 256 );
349 return (
350 4 ▶ join( '.', unpack( 'xC3', pack( 'V', $ibase ) ) ) . ".in-addr.arpa",
Branch: 1 branch
BranchTrueFalseTotal
if $blocks == 0 3 1 4
351 $ibase % 256,
352 $ibase % 256 + $this->size() - 1
353 ) if $blocks == 0;
354 1 my @ary;
355 1 ▶ for ( my $i = 0; $i < $blocks; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $blocks) 32 1 33
356 32 push( @ary,
357 join( '.', unpack( 'xC3', pack( 'V', $ibase + $i * 256 ) ) ) . ".in-addr.arpa",
358 0, 255 );
359 }
360 1 return @ary;
361 }
362  
363 sub inaddr6 {
364 4 ▶ my ($this) = @_;
Subroutine inaddr6: called CC=5 SCAR=16.1
Pod undocumented
365  
366 4 my (@digits) = split //, $this->{IBASE}->to_hex;
367  
368 4 my $static = floor( $this->{BITS} / 4 );
369 4 my $len = floor( ( $static + 3 ) / 4 );
370 4 my $remainder = $this->{BITS} % 4;
371 4 ▶ my $blocks = $remainder ? ( 2**( 4 - $remainder ) ) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$remainder ? : 1 3 4
372  
373 4 my @tail;
374 4 ▶ if ( !$len ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($len) 1 3 4
375 # Specal case: 0 len
376 1 return ('ip6.arpa');
377 }
378 3 push @tail, reverse( @digits[ 0 .. ( $static - 1 ) ] ), 'ip6.arpa';
379  
380 3 ▶ if ( !$remainder ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($remainder) 2 1 3
381 # Special case - at nibble boundary already
382 2 return ( join '.', @tail );
383 }
384  
385 1 my $last = hex $digits[$static];
386 1 my @ary;
387 1 ▶ for ( my $i = 0; $i < $blocks; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $blocks) 8 1 9
388 8 push @ary, join( '.', sprintf( "%x", $last ), @tail );
389 8 $last++;
390 }
391  
392 1 return @ary;
393 }
394  
395 sub tag {
396 11 ▶ my $this = shift;
Subroutine tag: called CC=2 SCAR=6.9
Pod documented
397 11 my $tag = shift;
398 11 my $val = $this->{ 'T' . $tag };
399 11 ▶ $this->{ 'T' . $tag } = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 6 11
400 11 return $val;
401 }
402  
403 sub quad2int {
404 1336 ▶ my @bytes = split( /\./, $_[0] );
Subroutine quad2int: called CC=4 SCAR=13.9
Pod undocumented
405  
406 1336 ▶ return unless @bytes == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @bytes == 4 1 1335 1336
407 1335 ▶ return unless !grep { !( /^(([0-9])|([1-9][0-9]*))$/ && $_ < 256 ) } @bytes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not /^(([0-9])|([1-9][0-9]*))$/ && $_ < 256;} @bytes 28 1307 1335
Condition100%3/3
/^(([0-9])|([1-9][0-9]*))$/ && $_ < 256
!ll&&!rl&&r
18125310
MC/DC100%2/2
/^(([0-9])|([1-9][0-9]*))$/ && $_ < 256
/^(([0-9])|([1-9][0-9]*))$/ $_ < 256
Truth table100%3/3
A && B
A= /^(([0-9])|([1-9][0-9]*))$/B= $_ < 256
ABresult
0X0
100
111
408  
409 1307 return unpack( "N", pack( "C4", @bytes ) );
410 }
411  
412 sub int2quad {
413 8785 ▶ return join( '.', unpack( 'C4', pack( "N", $_[0] ) ) );
Subroutine int2quad: called CC=1 SCAR=0.0
Pod undocumented
414 }
415  
416 # Uses the internal "raw" representation (such as IBASE).
417 # For IPv4, this is an integer
418 # For IPv6, this is a raw bit string.
419 sub int2ascii {
420 2312 ▶ if ( $_[1] eq 'IPv4' ) {
Subroutine int2ascii: called CC=4 SCAR=14.1
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if ($_[1] eq 'IPv4') { } 1611 701 2312
elsif ($_[1] eq 'IPv6') { } 701 0 701
421 1611 return join( '.', unpack( 'C4', pack( "N", $_[0] ) ) );
422 } elsif ( $_[1] eq 'IPv6' ) {
423 701 ▶ my $addr = ( ref $_[0] ) ne '' ? $_[0]->to_hex : Math::BigInt->new( $_[0] )->to_hex;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ne '' ? : 692 9 701
424 701 return ipv6Cannonical($addr);
425 } else {
426 0 confess("Incorrect call");
427 }
428 }
429  
430 # Produces the internal "raw" representation (such as IBASE).
431 # For IPv4, this is an integer
432 # For IPv6, this is a raw bit string.
433 sub ascii2int {
434 1946 ▶ if ( $_[1] eq 'IPv4' ) {
Subroutine ascii2int: called CC=3 SCAR=11.4
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if ($_[1] eq 'IPv4') { } 1312 634 1946
elsif ($_[1] eq 'IPv6') { } 634 0 634
435 1312 return quad2int( $_[0] );
436 } elsif ( $_[1] eq 'IPv6' ) {
437 634 return ipv6ascii2int( $_[0] );
438 } else {
439 0 confess("Incorrect call");
440 }
441 }
442  
443 # Take an IPv6 ASCII address and produce a raw value
444 sub ipv6ascii2int {
445 634 ▶ my $addr = shift;
Subroutine ipv6ascii2int: called CC=1 SCAR=0.0
Pod undocumented
446  
447 634 $addr = ipv6NonCompacted($addr);
448 634 $addr = join '', map { sprintf( "%04x", hex($_) ) } split( /:/, $addr );
449  
450 634 return Math::BigInt->from_hex($addr);
451 }
452  
453 # Takes an IPv6 address and produces a standard version seperated by
454 # colons (without compacting)
455 sub ipv6NonCompacted {
456 1610 ▶ my $addr = shift;
Subroutine ipv6NonCompacted: called CC=6 SCAR=17.9
Pod undocumented
457  
458 1610 ▶ if ( $addr !~ /:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($addr =~ /:/) 701 909 1610
459 701 ▶ if ( length($addr) < 32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $addr < 32) 37 664 701
460 37 $addr = ( "0" x ( 32 - length($addr) ) ) . $addr;
461 }
462 701 $addr =~ s/(....)(?=....)/$1:/gsx;
463 }
464  
465 # Handle address format with trailing IPv6
466 # Ex: 0:0:0:0:1.2.3.4
467 1610 ▶ if ( $addr =~ m/^[0-9a-f:]+[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr =~ /^[0-9a-f:]+[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/i) 2 1608 1610
468 2 my ( $l, $r1, $r2, $r3, $r4 ) =
469 $addr =~ m/^([0-9a-f:]+)([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/i;
470 2 $addr = sprintf( "%s%02x%02x:%02x%02x", $l, $r1, $r2, $r3, $r4 );
471 }
472  
473 1610 my ( $left, $right ) = split /::/, $addr;
474 1610 ▶ if ( !defined($right) ) { $right = '' }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $right) 724 886 1610
475 1610 my (@lparts) = split /:/, $left;
476 1610 my (@rparts) = split /:/, $right;
477  
478 # Strip leading 0's & lowercase
479 1610 @lparts = map { $_ =~ s/^0+([0-9a-f]+)/$1/; lc($_) } @lparts;
480 1610 @rparts = map { $_ =~ s/^0+([0-9a-f]+)/$1/; lc($_) } @rparts;
481  
482 # Expand ::
483 1610 my $missing = 8 - ( @lparts + @rparts );
484 1610 ▶ if ($missing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($missing) { } 887 723 1610
485 887 $addr = join ':', @lparts, ( 0, 0, 0, 0, 0, 0, 0, 0 )[ 0 .. $missing - 1 ], @rparts;
486 } else {
487 723 $addr = join ':', @lparts, @rparts;
488 }
489  
490 1610 return $addr;
491 }
492  
493 # Compacts an IPv6 address (reduces successive :0: runs)
494 sub ipv6AsciiCompact {
495 976 ▶ my $addr = shift;
Subroutine ipv6AsciiCompact: called CC=9 SCAR=22.0
Pod undocumented
496  
497 # Compress, per RFC5952
498 976 ▶ if ( $addr =~ s/^0:0:0:0:0:0:0:0$/::/ ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($addr =~ s/^0:0:0:0:0:0:0:0$/::/) { } 18 958 976
elsif ($addr =~ s/(:?^|:)0:0:0:0:0:0:0(:?:|$)/::/) { } 2 956 958
elsif ($addr =~ s/(:?^|:)0:0:0:0:0:0(:?:|$)/::/) { } 11 945 956
elsif ($addr =~ s/(:?^|:)0:0:0:0:0(:?:|$)/::/) { } 285 660 945
elsif ($addr =~ s/(:?^|:)0:0:0:0(:?:|$)/::/) { } 567 93 660
elsif ($addr =~ s/(:?^|:)0:0:0(:?:|$)/::/) { } 39 54 93
elsif ($addr =~ s/(:?^|:)0:0(:?:|$)/::/) { } 29 25 54
elsif ($addr =~ s/(:?^|:)0(:?:|$)/::/) { } 16 9 25
499 18 return $addr;
500 } elsif ( $addr =~ s/(:?^|:)0:0:0:0:0:0:0(:?:|$)/::/ ) {
501 2 return $addr;
502 } elsif ( $addr =~ s/(:?^|:)0:0:0:0:0:0(:?:|$)/::/ ) {
503 11 return $addr;
504 } elsif ( $addr =~ s/(:?^|:)0:0:0:0:0(:?:|$)/::/ ) {
505 285 return $addr;
506 } elsif ( $addr =~ s/(:?^|:)0:0:0:0(:?:|$)/::/ ) {
507 567 return $addr;
508 } elsif ( $addr =~ s/(:?^|:)0:0:0(:?:|$)/::/ ) {
509 39 return $addr;
510 } elsif ( $addr =~ s/(:?^|:)0:0(:?:|$)/::/ ) {
511 29 return $addr;
512 } elsif ( $addr =~ s/(:?^|:)0(:?:|$)/::/ ) {
513 16 return $addr;
514 }
515 9 return $addr;
516 }
517 # Cannonicalize IPv6 addresses in ascii format
518 sub ipv6Cannonical {
519 976 ▶ my $addr = shift;
Subroutine ipv6Cannonical: called CC=1 SCAR=0.0
Pod undocumented
520  
521 976 $addr = ipv6NonCompacted($addr);
522 976 $addr = ipv6AsciiCompact($addr);
523  
524 976 return $addr;
525 }
526  
527 # IPv6 addresses are stored with a leading zero.
528 sub storeNetblock {
529 46 ▶ my ( $this, $t ) = @_;
Subroutine storeNetblock: called CC=4 SCAR=13.9
Pod documented
530 46 ▶ $t = $remembered unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 11 35 46
531  
532 46 my $base = $this->{'IBASE'};
533 46 ▶ if ( $this->{PROTOCOL} eq 'IPv6' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"PROTOCOL"} eq "IPv6") 14 32 46
534 14 $base = "0$base";
535 }
536  
537 46 ▶ $t->{$base} = [] unless exists $t->{$base};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $t->{$base} 40 6 46
538  
539 46 my $mb = maxblock($this);
540 46 my $bits = $this->{'BITS'};
541 46 my $i = $bits - $mb;
542  
543 46 return ( $t->{$base}[$i] = $this );
544 }
545  
546 sub deleteNetblock {
547 13 ▶ my ( $this, $t ) = @_;
Subroutine deleteNetblock: called CC=6 SCAR=18.0
Pod documented
548 13 ▶ $t = $remembered unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 0 13 13
549  
550 13 my $base = $this->{'IBASE'};
551 13 ▶ if ( $this->{PROTOCOL} eq 'IPv6' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"PROTOCOL"} eq "IPv6") 1 12 13
552 1 $base = "0$base";
553 }
554  
555 13 my $mb = maxblock($this);
556 13 my $bits = $this->{'BITS'};
557 13 my $i = $bits - $mb;
558  
559 13 ▶ return unless defined $t->{$base};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t->{$base} 0 13 13
560  
561 13 undef $t->{$base}->[$i];
562  
563 13 for my $x ( @{ $t->{$base} } ) {
564 46 ▶ return if $x;
Branch: 1 branch
BranchTrueFalseTotal
if $x 3 43 46
565 }
566 10 return delete $t->{$base};
567 }
568  
569 sub findNetblock {
570 56 ▶ my ( $ascii, $t ) = @_;
Subroutine findNetblock: called CC=11 SCAR=24.0
Pod documented
571 56 ▶ $t = $remembered unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 7 49 56
572  
573 56 ▶ my $proto = ( $ascii =~ m/:/ ) ? 'IPv6' : 'IPv4';
Branch: 1 branch
BranchTrueFalseTotal
$ascii =~ /:/ ? : 7 49 56
574  
575 56 my $ip = ascii2int( $ascii, $proto );
576 56 ▶ return unless defined $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 1 55 56
577 55 my %done;
578  
579 55 ▶ my $maxbits = $proto eq 'IPv6' ? 128 : 32;
Branch: 1 branch
BranchTrueFalseTotal
$proto eq 'IPv6' ? : 7 48 55
580 55 ▶ for ( my $bits = $maxbits; $bits >= 0; $bits-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits >= 0) 1251 15 1266
581 1251 my $nb = i_getnet_addr( $ip, $bits, $proto );
582 1251 ▶ if ( $proto eq 'IPv6' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "IPv6") 568 683 1251
583 568 $nb = "0$nb";
584 }
585 1251 ▶ next unless exists $t->{$nb};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $t->{$nb} 1207 44 1251
586 44 my $mb = imaxblock( $nb, $maxbits, $proto );
587 44 ▶ next if $done{$mb}++;
Branch: 1 branch
BranchTrueFalseTotal
if $done{$mb}++ 3 41 44
588 41 my $i = $bits - $mb;
589 41 ▶ while ( $i >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 308 1 309
590 return $t->{$nb}->[$i]
591 308 ▶ if defined $t->{$nb}->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $t->{$nb}[$i] 40 268 308
592 268 $i--;
593 }
594 }
595 15 return;
596 }
597  
598 sub findOuterNetblock {
599 56 ▶ my ( $ipstr, $t ) = @_;
Subroutine findOuterNetblock: called CC=14 SCAR=26.4
Pod documented
600 56 ▶ $t = $remembered unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 0 56 56
601  
602 56 my $proto;
603 my $maxbits;
604  
605 56 my $ip;
606 56 my $len;
607 56 ▶ if ( ref($ipstr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ipstr) { } 28 28 56
608 28 $proto = $ipstr->{PROTOCOL};
609 28 ▶ $maxbits = $proto eq 'IPv4' ? 32 : 128;
Branch: 1 branch
BranchTrueFalseTotal
$proto eq 'IPv4' ? : 25 3 28
610 28 $ip = $ipstr->{IBASE};
611 28 $len = $ipstr->{BITS};
612 } else {
613 28 ▶ $proto = ( $ipstr =~ m/:/ ) ? 'IPv6' : 'IPv4';
Branch: 1 branch
BranchTrueFalseTotal
$ipstr =~ /:/ ? : 3 25 28
614 28 ▶ $maxbits = $proto eq 'IPv4' ? 32 : 128;
Branch: 1 branch
BranchTrueFalseTotal
$proto eq 'IPv4' ? : 25 3 28
615 28 $ip = ascii2int( $ipstr, $proto );
616 28 $len = $maxbits;
617 }
618  
619 56 ▶ for ( my $bits = 0; $bits <= $len; $bits++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits <= $len) 2028 34 2062
620 2028 ▶ my $nb = $ip & ( $proto eq 'IPv4' ? $imask[$bits] : $i6mask[$bits] );
Branch: 1 branch
BranchTrueFalseTotal
$proto eq 'IPv4' ? : 1382 646 2028
621 2028 ▶ if ( $proto eq 'IPv6' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "IPv6") 646 1382 2028
622 646 $nb = "0$nb";
623 }
624 2028 ▶ next unless exists $t->{$nb};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $t->{$nb} 1954 74 2028
625 74 my $mb = imaxblock( $nb, $len, $proto );
626 74 my $i = $bits - $mb;
627 74 ▶ confess "$mb, $bits, $ipstr, $nb" if $i < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 0 74 74
628 74 ▶ confess "$mb, $bits, $ipstr, $nb" if $i > $maxbits;
Branch: 1 branch
BranchTrueFalseTotal
if $i > $maxbits 0 74 74
629 74 ▶ while ( $i >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 173 52 225
630 return $t->{$nb}->[$i]
631 173 ▶ if defined $t->{$nb}->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $t->{$nb}[$i] 22 151 173
632 151 $i--;
633 }
634 }
635 34 return;
636 }
637  
638 sub findAllNetblock {
639 2 ▶ my ( $ipstr, $t ) = @_;
Subroutine findAllNetblock: called CC=13 SCAR=25.7
Pod documented
640 2 ▶ $t = $remembered unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 0 2 2
641  
642 2 ▶ my $proto = ( $ipstr =~ m/:/ ) ? 'IPv6' : 'IPv4';
Branch: 1 branch
BranchTrueFalseTotal
$ipstr =~ /:/ ? : 1 1 2
643 2 ▶ my $maxbits = $proto eq 'IPv4' ? 32 : 128;
Branch: 1 branch
BranchTrueFalseTotal
$proto eq 'IPv4' ? : 1 1 2
644  
645 2 my $ip = ascii2int( $ipstr, $proto );
646  
647 2 my %done;
648 my @ary;
649 2 ▶ for ( my $bits = $maxbits; $bits >= 0; $bits-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits >= 0) 162 2 164
650 162 ▶ my $nb = $ip & ( $proto eq 'IPv4' ? $imask[$bits] : $i6mask[$bits] );
Branch: 1 branch
BranchTrueFalseTotal
$proto eq 'IPv4' ? : 33 129 162
651 162 ▶ if ( $proto eq 'IPv6' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "IPv6") 129 33 162
652 129 $nb = "0$nb";
653 }
654 162 ▶ next unless exists $t->{$nb};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $t->{$nb} 53 109 162
655 109 my $mb = imaxblock( $nb, $maxbits, $proto );
656 109 ▶ next if $done{$mb}++;
Branch: 1 branch
BranchTrueFalseTotal
if $done{$mb}++ 106 3 109
657 3 my $i = $bits - $mb;
658 3 ▶ confess "$mb, $bits, $ipstr, $nb" if $i < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 0 3 3
659 3 ▶ confess "$mb, $bits, $ipstr, $nb" if $i > $maxbits;
Branch: 1 branch
BranchTrueFalseTotal
if $i > $maxbits 0 3 3
660 3 ▶ while ( $i >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 109 3 112
661 push( @ary, $t->{$nb}->[$i] )
662 109 ▶ if defined $t->{$nb}->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $t->{$nb}[$i] 3 106 109
663 109 $i--;
664 }
665 }
666 2 return @ary;
667 }
668  
669 sub dumpNetworkTable {
670 1 ▶ my ($t) = @_;
Subroutine dumpNetworkTable: called CC=5 SCAR=16.2
Pod documented
671 1 ▶ $t = $remembered unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 0 1 1
672  
673 1 my @ary;
674 1 foreach my $base ( keys %$t ) {
675 6 push @ary, grep { defined($_) } @{ $t->{base} };
676 6 for my $x ( @{ $t->{$base} } ) {
677 19 ▶ push( @ary, $x )
Branch: 1 branch
BranchTrueFalseTotal
if defined $x 6 13 19
678 if defined $x;
679 }
680 }
681  
682 1 return ( sort @ary );
683 }
684  
685 sub checkNetblock {
686 0 ▶ my ( $this, $t ) = @_;
Subroutine checkNetblock: not called CC=2 SCAR=17.9
Pod documented
687 0 ▶ $t = $remembered unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 0 0 0
688  
689 0 my $base = $this->{'IBASE'};
690  
691 0 my $mb = maxblock($this);
692 0 my $bits = $this->{'BITS'};
693 0 my $i = $bits - $mb;
694  
695 0 return defined $t->{$base}->[$i];
696 }
697  
698 sub match {
699 24 ▶ my ( $this, $ip ) = @_;
Subroutine match: called CC=9 SCAR=22.1
Pod documented
700 24 my $proto = $this->{PROTOCOL};
701  
702 # Two different protocols: return undef
703 24 ▶ if ( $ip =~ /:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /:/) { } 10 14 24
704 10 ▶ if ( $proto ne 'IPv6' ) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($proto ne "IPv6") 0 10 10
705 } else {
706 14 ▶ if ( $proto ne 'IPv4' ) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($proto ne "IPv4") 0 14 14
707 }
708  
709 24 my $i = ascii2int( $ip, $this->{PROTOCOL} );
710 24 my $ia = i_getnet_addr( $i, $this->{BITS}, $proto );
711  
712 24 ▶ if ( $proto eq 'IPv4' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq 'IPv4') { } 14 10 24
713 14 ▶ if ( $ia == $this->{IBASE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ia == $this->{'IBASE'}) { } 8 6 14
714 8 ▶ return ( ( $i & ~( $this->{IBASE} ) ) || "0 " );
Condition100%2/2
$i & ~$this->{'IBASE'} || '0 '
l!l
53
MC/DC100%1/1
$i & ~$this->{'IBASE'} || '0 '
$i & ~$this->{'IBASE'}
Truth table100%2/2
A
A= $i & ~$this->{'IBASE'}
Aresult
11
00
715 } else {
716 6 return 0;
717 }
718 } else {
719 10 ▶ if ( $ia == $this->{IBASE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ia == $this->{'IBASE'}) { } 6 4 10
720 6 ▶ return ( ( $i - $this->{IBASE} ) || "0 " );
Condition100%2/2
$i - $this->{'IBASE'} || '0 '
l!l
33
MC/DC100%1/1
$i - $this->{'IBASE'} || '0 '
$i - $this->{'IBASE'}
Truth table100%2/2
A
A= $i - $this->{'IBASE'}
Aresult
11
00
721 } else {
722 4 return 0;
723 }
724 }
725 }
726  
727 sub maxblock {
728 80 ▶ my ($this) = @_;
Subroutine maxblock: called CC=2 SCAR=7.2
Pod documented
729 return ( !defined $this->{ERROR} )
730 ? imaxblock( $this->{IBASE}, $this->{BITS}, $this->{PROTOCOL} )
731 80 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
!defined($this->{'ERROR'}) ? : 80 0 80
732 }
733  
734 sub nextblock {
735 8 ▶ my ( $this, $index ) = @_;
Subroutine nextblock: called CC=7 SCAR=19.6
Pod documented
736 8 ▶ $index = 1 unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 2 6 8
737 8 my $ibase = $this->{IBASE};
738 8 ▶ if ( $this->{PROTOCOL} eq 'IPv4' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'PROTOCOL'} eq 'IPv4') { } 4 4 8
739 4 $ibase += $index * 2**( 32 - $this->{BITS} );
740 } else {
741 4 $ibase += $index * Math::BigInt->new(2)->bpow( 128 - $this->{BITS} );
742 }
743  
744 my $newblock = bless {
745 IBASE => $ibase,
746 BITS => $this->{BITS},
747 PROTOCOL => $this->{PROTOCOL},
748 8 };
749  
750 8 ▶ if ( $this->{PROTOCOL} eq 'IPv4' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'PROTOCOL'} eq 'IPv4') { } 4 4 8
751 4 ▶ return if $newblock->{IBASE} >= 2**32;
Branch: 1 branch
BranchTrueFalseTotal
if $newblock->{"IBASE"} >= "4294967296" 0 4 4
752 } else {
753 4 ▶ return if $newblock->{IBASE} >= Math::BigInt->new(2)->bpow(128);
Branch: 1 branch
BranchTrueFalseTotal
if $newblock->{"IBASE"} >= "Math::BigInt"->new(2)->bpow(128) 0 4 4
754 }
755  
756 8 ▶ return if $newblock->{IBASE} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $newblock->{"IBASE"} < 0 0 8 8
757 8 return $newblock;
758 }
759  
760 sub imaxblock {
761 427 ▶ my ( $ibase, $tbit, $proto ) = @_;
Subroutine imaxblock: called CC=5 SCAR=16.4
Pod undocumented
762 427 ▶ confess unless defined $ibase;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ibase 0 427 427
763  
764 427 ▶ if ( !defined($proto) ) { $proto = 'IPv4'; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proto) 0 427 427
765  
766 427 ▶ while ( $tbit > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tbit > 0) 14568 12 14580
767 14568 my $ia = i_getnet_addr( $ibase, $tbit - 1, $proto );
768 14568 ▶ last if ( $ia != $ibase );
Branch: 1 branch
BranchTrueFalseTotal
if $ia != $ibase 415 14153 14568
769 14153 $tbit--;
770 }
771 427 return $tbit;
772 }
773  
774 sub range2cidrlist {
775 5 ▶ my ( $startip, $endip ) = @_;
Subroutine range2cidrlist: called CC=6 SCAR=18.2
Pod documented
776  
777 5 my $proto;
778 5 ▶ if ( $startip =~ m/:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($startip =~ /:/) { } 2 3 5
779 2 ▶ if ( $endip =~ m/:/ ) { $proto = 'IPv6'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($endip =~ /:/) 2 0 2
780 } else {
781 3 ▶ if ( $endip !~ m/:/ ) { $proto = 'IPv4'; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($endip =~ /:/) 3 0 3
782 }
783 5 ▶ if ( !defined($proto) ) { confess("Cannot mix IPv4 and IPv6 in range2cidrlist()"); }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proto) 0 5 5
784  
785 5 my $start = ascii2int( $startip, $proto );
786 5 my $end = ascii2int( $endip, $proto );
787  
788 5 ▶ ( $start, $end ) = ( $end, $start )
Branch: 1 branch
BranchTrueFalseTotal
if $start > $end 1 4 5
789 if $start > $end;
790 5 return irange2cidrlist( $start, $end, $proto );
791 }
792  
793 sub irange2cidrlist {
794 39 ▶ my ( $start, $end, $proto ) = @_;
Subroutine irange2cidrlist: called CC=7 SCAR=19.5
Pod undocumented
795 39 ▶ if ( !defined($proto) ) { $proto = 'IPv4' }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proto) 0 39 39
796  
797 39 ▶ my $bits = $proto eq 'IPv4' ? 32 : 128;
Branch: 1 branch
BranchTrueFalseTotal
$proto eq 'IPv4' ? : 30 9 39
798  
799 39 my @result;
800 39 ▶ while ( $end >= $start ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end >= $start) 120 39 159
801 120 my $maxsize = imaxblock( $start, $bits, $proto );
802 120 my $maxdiff;
803 120 ▶ if ( $proto eq 'IPv4' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq 'IPv4') { } 40 80 120
804 40 $maxdiff = $bits - _log2( $end - $start + 1 );
805 } else {
806 80 $maxdiff = $bits - ( $end - $start + 1 )->blog(2);
807 }
808 120 ▶ $maxsize = $maxdiff if $maxsize < $maxdiff;
Branch: 1 branch
BranchTrueFalseTotal
if $maxsize < $maxdiff 33 87 120
809 120 push(
810 @result,
811 bless {
812 'IBASE' => $start,
813 'BITS' => $maxsize,
814 'PROTOCOL' => $proto,
815 }
816 );
817 120 ▶ if ( $proto eq 'IPv4' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq 'IPv4') { } 40 80 120
818 40 $start += 2**( 32 - $maxsize );
819 } else {
820 80 $start += Math::BigInt->new(2)->bpow( $bits - $maxsize );
821 }
822 }
823 39 return @result;
824 }
825  
826 sub cidrs2contiglists {
827 1 ▶ my (@cidrs) = sort_network_blocks(@_);
Subroutine cidrs2contiglists: called CC=5 SCAR=16.1
Pod documented
828 1 my @result;
829 1 ▶ while (@cidrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cidrs) 2 1 3
830 2 my (@r) = shift(@cidrs);
831 2 my $max = $r[0]->{IBASE} + $r[0]->size;
832 2 ▶ while ( $cidrs[0] && $cidrs[0]->{IBASE} <= $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidrs[0] and $cidrs[0]{"IBASE"} <= $max) 1 2 3
Condition100%3/3
$cidrs[0] and $cidrs[0]{"IBASE"} <= $max
!ll&&!rl&&r
111
MC/DC100%2/2
$cidrs[0] and $cidrs[0]{"IBASE"} <= $max
$cidrs[0] $cidrs[0]{"IBASE"} <= $max
Truth table100%3/3
A and B
A= $cidrs[0]B= $cidrs[0]{"IBASE"} <= $max
ABresult
0X0
100
111
833 1 my $nm = $cidrs[0]->{IBASE} + $cidrs[0]->size;
834 1 ▶ $max = $nm if $nm > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $nm > $max 1 0 1
835 1 push( @r, shift(@cidrs) );
836 }
837 2 push( @result, [@r] );
838 }
839 1 return @result;
840 }
841  
842 sub cidrs2cidrs {
843 15 ▶ my (@cidrs) = sort_network_blocks(@_);
Subroutine cidrs2cidrs: called CC=7 SCAR=19.5
Pod documented
844 15 my @result;
845  
846 my $proto;
847 15 ▶ if ( scalar(@cidrs) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @cidrs) 15 0 15
848 15 $proto = $cidrs[0]->{PROTOCOL};
849 15 ▶ if ( grep { $proto ne $_->{PROTOCOL} } @cidrs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$proto ne $_->{"PROTOCOL"};} @cidrs) 0 15 15
850 0 confess("Cannot call cidrs2cidrs with mixed protocol arguments");
851 }
852 }
853  
854 15 ▶ while (@cidrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cidrs) 24 15 39
855 24 my (@r) = shift(@cidrs);
856  
857 24 my $max = $r[0]->{IBASE} + $r[0]->size;
858 24 ▶ while ( $cidrs[0] && $cidrs[0]->{IBASE} <= $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidrs[0] and $cidrs[0]{"IBASE"} <= $max) 25 24 49
Condition100%3/3
$cidrs[0] and $cidrs[0]{"IBASE"} <= $max
!ll&&!rl&&r
15925
MC/DC100%2/2
$cidrs[0] and $cidrs[0]{"IBASE"} <= $max
$cidrs[0] $cidrs[0]{"IBASE"} <= $max
Truth table100%3/3
A and B
A= $cidrs[0]B= $cidrs[0]{"IBASE"} <= $max
ABresult
0X0
100
111
859 25 my $nm = $cidrs[0]->{IBASE} + $cidrs[0]->size;
860 25 ▶ $max = $nm if $nm > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $nm > $max 14 11 25
861 25 push( @r, shift(@cidrs) );
862 }
863 24 my $start = $r[0]->{IBASE};
864 24 my $end = $max - 1;
865 24 push( @result, irange2cidrlist( $start, $end, $proto ) );
866 }
867 15 return @result;
868 }
869  
870 sub cidrs2inverse {
871 10 ▶ my $outer = shift;
Subroutine cidrs2inverse: called CC=12 SCAR=25.1
Pod documented
872 10 ▶ $outer = __PACKAGE__->new2($outer) || croak($error) unless ref($outer);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $outer 4 6 10
Condition33%1/3
__PACKAGE__->new2($outer) || croak($Net::Netmask::error)
l!l&&r!l&&!r
400
MC/DC0%0/2
__PACKAGE__->new2($outer) || croak($Net::Netmask::error)
__PACKAGE__->new2($outer) croak($Net::Netmask::error)
Truth table33%1/3
A || B
A= __PACKAGE__->new2($outer)B= croak($Net::Netmask::error)
ABresult
1X1
011
000
873  
874 # cidrs2cidrs validates that everything is in the same address
875 # family
876 10 my (@cidrs) = cidrs2cidrs(@_);
877 10 my $proto;
878 10 ▶ if ( scalar(@cidrs) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @cidrs) 10 0 10
879 10 $proto = $cidrs[0]->{PROTOCOL};
880 }
881  
882 10 my $first = $outer->{IBASE};
883 10 my $last = $first + $outer->size() - 1;
884 10 ▶ shift(@cidrs) while $cidrs[0] && $cidrs[0]->{IBASE} + $cidrs[0]->size < $first;
Branch: 1 branch
BranchTrueFalseTotal
$cidrs[0] and $cidrs[0]{"IBASE"} + $cidrs[0]->size < $first and do { shift @cidrs } 2 10 12
Condition66%2/3
$cidrs[0] and $cidrs[0]{"IBASE"} + $cidrs[0]->size < $first
!ll&&!rl&&r
0102
MC/DC50%1/2
$cidrs[0] and $cidrs[0]{"IBASE"} + $cidrs[0]->size < $first
$cidrs[0] $cidrs[0]{"IBASE"} + $cidrs[0]->size < $first
Truth table66%2/3
A and B
A= $cidrs[0]B= $cidrs[0]{"IBASE"} + $cidrs[0]->size < $first
ABresult
0X0
100
111
885 10 my @r;
886 10 ▶ while ( @cidrs && $first <= $last ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cidrs and $first <= $last) 16 8 24
Condition66%2/3
@cidrs and $first <= $last
!ll&&!rl&&r
8016
MC/DC50%1/2
@cidrs and $first <= $last
@cidrs $first <= $last
Truth table66%2/3
A and B
A= @cidrsB= $first <= $last
ABresult
0X0
100
111
887  
888 16 ▶ if ( $first < $cidrs[0]->{IBASE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first < $cidrs[0]{"IBASE"}) 6 10 16
889 6 ▶ if ( $last <= $cidrs[0]->{IBASE} - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last <= $cidrs[0]{"IBASE"} - 1) 2 4 6
890 2 return ( @r, irange2cidrlist( $first, $last, $proto ) );
891 }
892 4 push( @r, irange2cidrlist( $first, $cidrs[0]->{IBASE} - 1, $proto ) );
893 }
894 14 ▶ last if $cidrs[0]->{IBASE} > $last;
Branch: 1 branch
BranchTrueFalseTotal
if $cidrs[0]{"IBASE"} > $last 0 14 14
895 14 $first = $cidrs[0]->{IBASE} + $cidrs[0]->size;
896 14 shift(@cidrs);
897 }
898 8 ▶ if ( $first <= $last ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first <= $last) 4 4 8
899 4 push( @r, irange2cidrlist( $first, $last, $proto ) );
900 }
901 8 return @r;
902 }
903  
904 sub by_net_netmask_block {
905 return $a->{'IBASE'} <=> $b->{'IBASE'}
906 0 ▶ || $a->{'BITS'} <=> $b->{'BITS'};
Subroutine by_net_netmask_block: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
$Net::Netmask::a->{'IBASE'} <=> $Net::Netmask::b->{'IBASE'} || $Net::Netmask::a->{'BITS'} <=> $Net::Netmask::b->{'BITS'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Net::Netmask::a->{'IBASE'} <=> $Net::Netmask::b->{'IBASE'} || $Net::Netmask::a->{'BITS'} <=> $Net::Netmask::b->{'BITS'}
$Net::Netmask::a->{'IBASE'} <=> $Net::Netmask::b->{'IBASE'} $Net::Netmask::a->{'BITS'} <=> $Net::Netmask::b->{'BITS'}
Truth table0%0/3
A || B
A= $Net::Netmask::a->{'IBASE'} <=> $Net::Netmask::b->{'IBASE'}B= $Net::Netmask::a->{'BITS'} <=> $Net::Netmask::b->{'BITS'}
ABresult
1X1
011
000
907 }
908  
909 sub sameblock {
910 29 ▶ return !cmpblocks(@_);
Subroutine sameblock: called CC=1 SCAR=0.0
Pod documented
911 }
912  
913 sub cmpblocks {
914 45 ▶ my $this = shift;
Subroutine cmpblocks: called CC=2 SCAR=6.9
Pod documented
915 45 my $class = ref $this;
916 45 ▶ my $other = ( ref $_[0] ) ? shift : $class->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 13 32 45
917 45 return cmp_net_netmask_block( $this, $other );
918 }
919  
920 sub contains {
921 66 ▶ my $this = shift;
Subroutine contains: called CC=5 SCAR=16.1
Pod documented
922 66 my $class = ref $this;
923 66 ▶ my $other = ( ref $_[0] ) ? shift : $class->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 64 2 66
924 66 ▶ return 0 if $this->{IBASE} > $other->{IBASE};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"IBASE"} > $other->{"IBASE"} 15 51 66
925 51 ▶ return 0 if $this->{BITS} > $other->{BITS};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"BITS"} > $other->{"BITS"} 5 46 51
926 46 ▶ return 0 if $other->{IBASE} > $this->{IBASE} + $this->size - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $other->{"IBASE"} > $this->{"IBASE"} + $this->size - 1 6 40 46
927 40 return 1;
928 }
929  
930 sub cmp_net_netmask_block {
931 7865 ▶ if ( ( $_[0]->{PROTOCOL} eq 'IPv4' ) && ( $_[1]->{PROTOCOL} eq 'IPv4' ) ) {
Subroutine cmp_net_netmask_block: called CC=7 SCAR=19.5
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0]{'PROTOCOL'} eq 'IPv4' and $_[1]{'PROTOCOL'} eq 'IPv4') { } 7826 39 7865
elsif ($_[0]{'PROTOCOL'} eq 'IPv6' and $_[1]{'PROTOCOL'} eq 'IPv6') { } 36 3 39
Condition100%3/3
$_[0]{"PROTOCOL"} eq "IPv4" and $_[1]{"PROTOCOL"} eq "IPv4"
!ll&&!rl&&r
3817826
MC/DC100%2/2
$_[0]{"PROTOCOL"} eq "IPv4" and $_[1]{"PROTOCOL"} eq "IPv4"
$_[0]{"PROTOCOL"} eq "IPv4" $_[1]{"PROTOCOL"} eq "IPv4"
Truth table100%3/3
A and B
A= $_[0]{"PROTOCOL"} eq "IPv4"B= $_[1]{"PROTOCOL"} eq "IPv4"
ABresult
0X0
100
111
932 # IPv4
933 7826 ▶ return ( $_[0]->{IBASE} <=> $_[1]->{IBASE} || $_[0]->{BITS} <=> $_[1]->{BITS} );
Condition100%3/3
$_[0]{'IBASE'} <=> $_[1]{'IBASE'} || $_[0]{'BITS'} <=> $_[1]{'BITS'}
l!l&&r!l&&!r
77891522
MC/DC100%2/2
$_[0]{'IBASE'} <=> $_[1]{'IBASE'} || $_[0]{'BITS'} <=> $_[1]{'BITS'}
$_[0]{'IBASE'} <=> $_[1]{'IBASE'} $_[0]{'BITS'} <=> $_[1]{'BITS'}
Truth table100%3/3
A || B
A= $_[0]{'IBASE'} <=> $_[1]{'IBASE'}B= $_[0]{'BITS'} <=> $_[1]{'BITS'}
ABresult
1X1
011
000
934 ▶ } elsif ( ( $_[0]->{PROTOCOL} eq 'IPv6' ) && ( $_[1]->{PROTOCOL} eq 'IPv6' ) ) {
Condition100%3/3
$_[0]{"PROTOCOL"} eq "IPv6" and $_[1]{"PROTOCOL"} eq "IPv6"
!ll&&!rl&&r
1236
MC/DC100%2/2
$_[0]{"PROTOCOL"} eq "IPv6" and $_[1]{"PROTOCOL"} eq "IPv6"
$_[0]{"PROTOCOL"} eq "IPv6" $_[1]{"PROTOCOL"} eq "IPv6"
Truth table100%3/3
A and B
A= $_[0]{"PROTOCOL"} eq "IPv6"B= $_[1]{"PROTOCOL"} eq "IPv6"
ABresult
0X0
100
111
935 # IPv6
936 36 ▶ return ( $_[0]->{IBASE} <=> $_[1]->{IBASE} || $_[0]->{BITS} <=> $_[1]->{BITS} );
Condition100%3/3
$_[0]{'IBASE'} <=> $_[1]{'IBASE'} || $_[0]{'BITS'} <=> $_[1]{'BITS'}
l!l&&r!l&&!r
111213
MC/DC100%2/2
$_[0]{'IBASE'} <=> $_[1]{'IBASE'} || $_[0]{'BITS'} <=> $_[1]{'BITS'}
$_[0]{'IBASE'} <=> $_[1]{'IBASE'} $_[0]{'BITS'} <=> $_[1]{'BITS'}
Truth table100%3/3
A || B
A= $_[0]{'IBASE'} <=> $_[1]{'IBASE'}B= $_[0]{'BITS'} <=> $_[1]{'BITS'}
ABresult
1X1
011
000
937 } else {
938 # IPv4 to IPv6, order by protocol
939 3 return ( $_[0]->{PROTOCOL} cmp $_[1]->{PROTOCOL} );
940 }
941 }
942  
943 sub sort_network_blocks {
944 52 return map { $_->[0] }
945 64 ▶ sort { $a->[3] cmp $b->[3] || $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] }
Branch: 1 branch
BranchTrueFalseTotal
$Net::Netmask::a->[3] cmp $Net::Netmask::b->[3] or $Net::Netmask::a->[1] <=> $Net::Netmask::b->[1] or $Net::Netmask::a->[2] <=> $Net::Netmask::b->[2] 1 63 64
Condition66%2/3
$Net::Netmask::a->[3] cmp $Net::Netmask::b->[3] or $Net::Netmask::a->[1] <=> $Net::Netmask::b->[1]
l!l&&r!l&&!r
0631
MC/DC50%1/2
$Net::Netmask::a->[3] cmp $Net::Netmask::b->[3] or $Net::Netmask::a->[1] <=> $Net::Netmask::b->[1]
$Net::Netmask::a->[3] cmp $Net::Netmask::b->[3] $Net::Netmask::a->[1] <=> $Net::Netmask::b->[1]
Truth table66%2/3
A or B
A= $Net::Netmask::a->[3] cmp $Net::Netmask::b->[3]B= $Net::Netmask::a->[1] <=> $Net::Netmask::b->[1]
ABresult
1X1
011
000
946 16 ▶ map { [ $_, $_->{IBASE}, $_->{BITS}, $_->{PROTOCOL} ] } @_;
Subroutine sort_network_blocks: called CC=3 SCAR=11.0
Pod documented
947 }
948  
949 sub sort_by_ip_address {
950 500 return map { $_->[0] }
951 3887 sort { $a->[1] cmp $b->[1] }
952 1 ▶ map { [ $_, pack( "C4", split( /\./, $_ ) ) ] } @_;
Subroutine sort_by_ip_address: called CC=1 SCAR=0.0
Pod documented
953  
954 }
955  
956 sub split ## no critic: (Subroutines::ProhibitBuiltinHomonyms)
957 {
958 14 ▶ my ( $self, $parts ) = @_;
Subroutine split: called CC=5 SCAR=16.1
Pod documented
959  
960 14 my $num_ips = $self->size;
961  
962 14 ▶ confess "Parts must be defined and greater than 0."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parts and $parts > 0 6 8 14
Condition100%3/3
defined $parts and $parts > 0
!ll&&!rl&&r
248
MC/DC100%2/2
defined $parts and $parts > 0
defined $parts $parts > 0
Truth table100%3/3
A and B
A= defined $partsB= $parts > 0
ABresult
0X0
100
111
963 unless defined($parts) && $parts > 0;
964  
965 8 ▶ confess "Netmask only contains $num_ips IPs. Cannot split into $parts."
Branch: 1 branch
BranchTrueFalseTotal
unless $num_ips >= $parts 2 6 8
966 unless $num_ips >= $parts;
967  
968 6 my $log2 = _log2($parts);
969  
970 6 ▶ confess "Parts count must be a number of base 2. Got: $parts"
Branch: 1 branch
BranchTrueFalseTotal
unless 2 ** $log2 == $parts 2 4 6
971 unless ( 2**$log2 ) == $parts;
972  
973 4 my $new_mask = $self->bits + $log2;
974  
975 516 return map { Net::Netmask->new( $_ . "/" . $new_mask ) }
976 4 map { $self->nth( ( $num_ips / $parts ) * $_ ) } ( 0 .. ( $parts - 1 ) );
977 }
978  
979 # Implement log2 sub routine directly, to avoid precision problems with floor()
980 # problems with perls built with uselongdouble defined.
981 # Credit: xenu, on IRC
982 sub _log2 {
983 46 ▶ my $n = shift;
Subroutine _log2: called CC=2 SCAR=6.9
984  
985 46 my $ret = 0;
986 46 ▶ $ret++ while ( $n >>= 1 );
Branch: 1 branch
BranchTrueFalseTotal
$n >>= 1 and do { ++$ret } 261 46 307
987  
988 46 return $ret;
989 }
990  
991 BEGIN {
992 8 ▶ for ( my $i = 0; $i <= 32; $i++ ) {
Subroutine BEGIN: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 32) 264 8 272
993 264 $imask[$i] = imask($i);
994 264 $imask2bits{ $imask[$i] } = $i;
995 264 $quadmask2bits{ int2quad( $imask[$i] ) } = $i;
996 264 $quadhostmask2bits{ int2quad( ~$imask[$i] ) } = $i;
997 264 $size2bits{ 2**( 32 - $i ) } = $i;
998 }
999  
1000 8 ▶ for ( my $i = 0; $i <= 128; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 128) 1032 8 1040
1001 1032 $i6mask[$i] = i6mask($i);
1002 }
1003 }
1004 8 1;
« blib/lib/Net/Netmask.pm ↑ Summary