Util.pm

filter: Statementstmt 73.4% 293 / 399 Branchbran 50.0% 68 / 136 Conditioncond 48.1% 40 / 83 MC/DCmcdc 21.7% 10 / 46 Subroutinesub 71.7% 38 / 53 Podpod 100% 28 / 28 totaltotal 64.0% 477 / 745 CC 109 SCAR 32.2
CC
109
Cov
65%
new
75.3
validaddr
56.0
radix_sort
30.0
CRAP 25.1
« Util/Collection/Tie.pm ↑ Summary Util/Range.pm »
1 package Net::IPAddress::Util;
2  
3 5 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 '=' => 'new',
7 '""' => 'str',
8 'cmp' => '_spaceship',
9 '<=>' => '_spaceship',
10 '+' => '_do_add',
11 '-' => '_do_subtract',
12 '<<' => '_shift_left',
13 '>>' => '_shift_right',
14 '&' => '_band',
15 '|' => '_bor',
16 '~' => '_neg',
17 5 );
18  
19 5 ▶use Carp qw( carp cluck confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use List::MoreUtils qw( pairwise );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 5 our %EXPORT_TAGS = (
24 constr => [qw( IP n32_to_ipv4 )],
25 manip => [qw( explode_ip implode_ip ip_pad_prefix common_prefix prefix_mask ipv4_mask ipv4_flag )],
26 sort => [qw( radix_sort )],
27 compat => [qw( ip2num num2ip validaddr mask fqdn )]
28 );
29  
30 5 my %EXPORT_OK;
31 5 for my $k (keys %EXPORT_TAGS) {
32 20 for my $v (@{$EXPORT_TAGS{ $k }}) {
33 75 undef $EXPORT_OK{ $v };
34 }
35 }
36  
37 5 our @EXPORT_OK = keys %EXPORT_OK;
38  
39 5 $EXPORT_TAGS{ all } = [ @EXPORT_OK ];
40  
41 5 our $DIE_ON_ERROR = 0;
42 5 our $PROMOTE_N32 = 1;
43  
44 5 our @SIIT = (
45 [ 0, 0, 0xff, 0xff ], # off
46 [ 0xff, 0xff, 0, 0 ], # on
47 );
48  
49 5 our $VERSION = '5.001';
50  
51 5 our $siit_fourish = qr/^(?:::ffff:0+:)?(\d+)\.(\d+)\.(\d+)\.(\d+)$/io;
52 5 our $fourish = qr/^(?:::ffff:)?(\d+)\.(\d+)\.(\d+)\.(\d+)$/io;
53 5 our $numberish = qr/^(\d+)$/o;
54 5 our $normalish = qr/^([0-9a-f]{32})$/io;
55 5 our $sixish = qr/^([0-9a-f:]+)(?:\%.*)?$/io;
56  
57 *__debug
58 = $ENV{ IP_UTIL_DEBUG }
59 0 ▶ ? sub { my ($p, $f, $l) = caller(); warn('@' . " $l: " . join(', ', @_) . "\n"); }
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 6999 ▶ : sub { }
Subroutine __ANON__: called CC=1 SCAR=0.0
61 5 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'IP_UTIL_DEBUG'} ? : 0 5 5
62  
63 *__dnorm
64 = $ENV{ IP_UTIL_DEBUG }
65 0 ▶ ? sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
66 0 my ($p, $f, $l) = caller();
67 0 my $n = 0;
68 0 for my $x (@_) {
69 0 ▶ if (eval { @$x }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { @$x } }) { } 0 0 0
70 0 warn($n++ . ' @' . " $l: [" . join(', ', map { sprintf('%x', $_) } @$x) . "]\n");
71 }
72 else {
73 0 warn($n++ . ' @' . " $l: " . $x . "\n");
74 }
75 }
76 }
77 1562 ▶ : sub { }
Subroutine __ANON__: called CC=1 SCAR=0.0
78 5 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'IP_UTIL_DEBUG'} ? : 0 5 5
79  
80 16 ▶sub SIIT {
Subroutine SIIT: called CC=2 SCAR=7.0
Pod documented
81 16 my $self = shift;
82 16 my ($do) = @_;
83 16 ▶ $do //= 1;
Condition50%1/2
$do //= 1
l!l
016
MC/DC0%0/1
$do //= 1
$do
Truth table50%1/2
A
A= $do
Aresult
11
00
84 16 $self = _set_SIIT($self, $do);
85 16 $self->{ SIIT } = $do;
86 16 return $self;
87 }
88  
89 16 ▶sub _set_SIIT {
Subroutine _set_SIIT: called CC=4 SCAR=13.9
90 16 my ($old, $do) = @_;
91 16 my $normal = [ unpack('C16', $old->{ address }) ];
92 16 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not grep({$_;} @$normal[0..7]) and grep({$normal->[$_] == $Net::IPAddress::Util::SIIT[not !$do][$_];} 8..11) == 4) 1 15 16
Condition100%3/3
not grep({$_;} @$normal[0..7]) and grep({$normal->[$_] == $Net::IPAddress::Util::SIIT[not !$do][$_];} 8..11) == 4
!ll&&!rl&&r
5101
MC/DC100%2/2
not grep({$_;} @$normal[0..7]) and grep({$normal->[$_] == $Net::IPAddress::Util::SIIT[not !$do][$_];} 8..11) == 4
not grep({$_;} @$normal[0..7]) grep({$normal->[$_] == $Net::IPAddress::Util::SIIT[not !$do][$_];} 8..11) == 4
Truth table100%3/3
not(A) and B
A= not grep({$_;} @$normal[0..7])B= grep({$normal->[$_] == $Net::IPAddress::Util::SIIT[not !$do][$_];} 8..11) == 4
ABresult
0X0
100
111
93 128 !(grep { $_ } @$normal[ 0 .. 7 ])
94 44 && (grep { $normal->[ $_ ] == $SIIT[!!$do]->[ $_ ] } (8 .. 11)) == 4
95 ) {
96 1 $normal->[ $_ ] = $SIIT[!$do]->[ $_ ] for (8 .. 11);
97 }
98 16 $old->{ address } = pack('C16', @$normal);
99 16 return $old;
100 }
101  
102 545 ▶sub IP {
Subroutine IP: called CC=1 SCAR=0.0
Pod documented
103 545 return __PACKAGE__->new(@_);
104 }
105  
106 2107 ▶sub new {
Subroutine new: called CC=38 SCAR=43.2
Pod documented
107 2107 my $self = shift;
108 2107 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
021070
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
109 2107 my ($address, %opt) = @_;
110 __debug(
111 2107 eval { @$address }
112 2107 ▶ ? join(' ', map { sprintf($_ > 255 ? '0x%08x' : '%3s', $_ ) } @$address)
Branch: 2 branches
BranchTrueFalseTotal
eval { do { @$address } } ? : 1562 545 2107
$_ > 255 ? : 1212 16436 17648
113 : $address
114 );
115 2107 my @siit_prefix = @{$SIIT[$opt{ SIIT }
116 ||= (
117 2107 ▶ !ref($address)
Condition88%8/9
$opt{"SIIT"} ||= !ref($address) && $address =~ /$Net::IPAddress::Util::siit_fourish/u && !($address =~ /$Net::IPAddress::Util::fourish/u)
l!l&&r!l&&!r
012106
!ref($address) && $address =~ /$Net::IPAddress::Util::siit_fourish/u && !($address =~ /$Net::IPAddress::Util::fourish/u)
!ll&&!rl&&r
2077291
not ref $address and $address =~ /$Net::IPAddress::Util::siit_fourish/u
!ll&&!rl&&r
20562130
MC/DC0%0/4
$opt{"SIIT"} ||= !ref($address) && $address =~ /$Net::IPAddress::Util::siit_fourish/u && !($address =~ /$Net::IPAddress::Util::fourish/u)
$opt{"SIIT"} not ref $address $address =~ /$Net::IPAddress::Util::siit_fourish/u !($address =~ /$Net::IPAddress::Util::fourish/u)
Truth table0%0/5
A ||= not(B) and C && not(D)
A= $opt{"SIIT"}B= not ref $addressC= $address =~ /$Net::IPAddress::Util::siit_fourish/uD= !($address =~ /$Net::IPAddress::Util::fourish/u)
ABCDresult
1XXX1
01111
00XX0
010X0
01100
118 and
119 $address =~ $siit_fourish
120 and
121 $address !~ $fourish
122 )
123 ]};
124 2107 ▶ __debug('is ' . ($opt{ SIIT } ? 'not ' : '') . 'SIIT looking');
Branch: 1 branch
BranchTrueFalseTotal
$opt{'SIIT'} ? : 1 2106 2107
125 2107 ▶ my $promote = $opt{ promote } // $PROMOTE_N32;
Condition33%1/3
$opt{"promote"} // $Net::IPAddress::Util::PROMOTE_N32
l!l&&r!l&&!r
021070
MC/DC0%0/2
$opt{"promote"} // $Net::IPAddress::Util::PROMOTE_N32
$opt{"promote"} $Net::IPAddress::Util::PROMOTE_N32
Truth table33%1/3
A // B
A= $opt{"promote"}B= $Net::IPAddress::Util::PROMOTE_N32
ABresult
1X1
011
000
126 2107 my $normal = [ ];
127 2107 ▶ if (!defined $address) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $address) 0 2107 2107
128 0 $normal = [
129 0, 0, 0, 0,
130 0, 0, 0, 0,
131 0, 0, 0, 0,
132 0, 0, 0, 0,
133 ];
134 0 __dnorm($normal);
135 }
136 2107 ▶ if (ref($address) eq 'ARRAY' && @$address == 16) {
Branch: 9 branches
BranchTrueFalseTotal
if (ref $address eq 'ARRAY' and @$address == 16) { } 950 1157 2107
elsif (ref $address eq 'ARRAY' and @$address == 4) { } 612 545 1157
elsif (ref $address and eval { do { $address->isa(__PACKAGE__) } }) { } 494 51 545
elsif ($address =~ /$Net::IPAddress::Util::fourish/u or $address =~ /$Net::IPAddress::Util::siit_fourish/u) { } 31 20 51
elsif ($opt{'promote'} and $address =~ /$Net::IPAddress::Util::numberish/u and (0 <= $address and $address <= 4294967295)) { } 0 20 20
elsif ("$address" =~ /$Net::IPAddress::Util::normalish/u) { } 0 20 20
elsif ($address =~ /$Net::IPAddress::Util::numberish/u) { } 4 16 20
elsif ($address =~ /$Net::IPAddress::Util::sixish/u and scalar grep({/::/ou;} split(/[[:alnum:]]+/u, $address, 0)) == 1 || scalar grep({/[[:alnum:]]+/u;} split(/:/u, $address, 0)) == 8) { } 16 0 16
elsif (length $address == 16) { } 0 0 0
Condition100%3/3
ref $address eq "ARRAY" and @$address == 16
!ll&&!rl&&r
545612950
MC/DC100%2/2
ref $address eq "ARRAY" and @$address == 16
ref $address eq "ARRAY" @$address == 16
Truth table100%3/3
A and B
A= ref $address eq "ARRAY"B= @$address == 16
ABresult
0X0
100
111
137 950 __debug('16 element array');
138 950 $normal = $address;
139 950 __dnorm($normal);
140 }
141 ▶ elsif (ref($address) eq 'ARRAY' && @$address == 4) {
Condition66%2/3
ref $address eq "ARRAY" and @$address == 4
!ll&&!rl&&r
5450612
MC/DC50%1/2
ref $address eq "ARRAY" and @$address == 4
ref $address eq "ARRAY" @$address == 4
Truth table66%2/3
A and B
A= ref $address eq "ARRAY"B= @$address == 4
ABresult
0X0
100
111
142 # FIXME: Principal of least surprise here? Should feeding in 4 values make an IPv4?
143 612 __debug('4 element array');
144 612 $normal = [ unpack 'C16', pack 'N4', @$address ];
145 612 __dnorm($normal);
146 }
147 494 ▶ elsif (ref $address and eval { $address->isa(__PACKAGE__) }) {
Condition66%2/3
ref $address and eval { do { $address->isa(__PACKAGE__) } }
!ll&&!rl&&r
510494
MC/DC50%1/2
ref $address and eval { do { $address->isa(__PACKAGE__) } }
ref $address eval { do { $address->isa(__PACKAGE__) } }
Truth table66%2/3
A and B
A= ref $addressB= eval { do { $address->isa(__PACKAGE__) } }
ABresult
0X0
100
111
148 494 $normal = [ unpack 'C16', $address->{ address } ];
149 }
150 ▶ elsif ($address =~ $fourish || $address =~ $siit_fourish) {
Condition100%3/3
$address =~ /$Net::IPAddress::Util::fourish/u or $address =~ /$Net::IPAddress::Util::siit_fourish/u
l!l&&r!l&&!r
30120
MC/DC100%2/2
$address =~ /$Net::IPAddress::Util::fourish/u or $address =~ /$Net::IPAddress::Util::siit_fourish/u
$address =~ /$Net::IPAddress::Util::fourish/u $address =~ /$Net::IPAddress::Util::siit_fourish/u
Truth table100%3/3
A or B
A= $address =~ /$Net::IPAddress::Util::fourish/uB= $address =~ /$Net::IPAddress::Util::siit_fourish/u
ABresult
1X1
011
000
151 31 my @addr = ($1, $2, $3, $4);
152 # FIXME: Why can't we do this all on one line?
153 31 ▶ @addr = map { $_ =~ /^0/ ? oct($_) : int($_) } @addr;
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^0/u ? : 38 86 124
154 31 $normal = [
155 0, 0, 0, 0,
156 0, 0, 0, 0,
157 @siit_prefix,
158 @addr
159 ];
160 }
161 ▶ elsif (
Condition22%2/9
$opt{"promote"} and $address =~ /$Net::IPAddress::Util::numberish/u and (0 <= $address and $address <= 4294967295)
!ll&&!rl&&r
2000
$opt{"promote"} and $address =~ /$Net::IPAddress::Util::numberish/u
!ll&&!rl&&r
2000
0 <= $address and $address <= 4294967295
!ll&&!rl&&r
000
MC/DC0%0/4
$opt{"promote"} and $address =~ /$Net::IPAddress::Util::numberish/u and (0 <= $address and $address <= 4294967295)
$opt{"promote"} $address =~ /$Net::IPAddress::Util::numberish/u 0 <= $address $address <= 4294967295
Truth table20%1/5
A and B and C and D
A= $opt{"promote"}B= $address =~ /$Net::IPAddress::Util::numberish/uC= 0 <= $addressD= $address <= 4294967295
ABCDresult
0XXX0
10XX0
110X0
11100
11111
162 $opt{ promote }
163 and $address =~ $numberish
164 and 0 <= $address && $address <= (2 ** 32) - 1
165 ) {
166 0 $normal = [
167 0, 0, 0, 0,
168 0, 0, 0, 0,
169 @siit_prefix,
170 unpack('C4', pack('N', $address))
171 ];
172 }
173 elsif ("$address" =~ $normalish) {
174 0 my $fresh = $1;
175 0 ▶ eval "require Math::BigInt" or return ERROR("Could not load Math::BigInt: $@");
Branch: 1 branch
BranchTrueFalseTotal
eval "require Math::BigInt" or return ERROR("Could not load Math::BigInt: $@") 0 0 0
176 0 my $raw = Math::BigInt->from_hex("$fresh");
177 0 ▶ while ($raw > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw > 0) 0 0 0
178 0 my $word = $raw->copy->band(0xffffffff);
179 0 unshift @$normal, unpack('C4', pack('N', $word));
180 0 $raw = $raw->copy->brsft(32);
181 }
182 0 ▶ while (@$normal < 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$normal < 16) 0 0 0
183 0 unshift @$normal, 0;
184 }
185 0 eval "no Math::BigInt";
186 }
187 elsif ($address =~ $numberish) {
188 4 ▶ eval "require Math::BigInt" or return ERROR("Could not load Math::BigInt: $@");
Branch: 1 branch
BranchTrueFalseTotal
eval "require Math::BigInt" or return ERROR("Could not load Math::BigInt: $@") 4 0 4
189 4 my $raw = Math::BigInt->new("$address");
190 4 ▶ while ($raw > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw > 0) 6 4 10
191 6 my $word = $raw->copy->band(0xffffffff);
192 6 unshift @$normal, unpack('C4', pack('N', $word));
193 6 $raw = $raw->copy->brsft(32);
194 }
195 4 ▶ while (@$normal < 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$normal < 16) 40 4 44
196 40 unshift @$normal, 0;
197 }
198 4 ▶ eval "no Math::BigInt";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
199 }
200 ▶ elsif (
Condition50%3/6
$address =~ /$Net::IPAddress::Util::sixish/u and scalar grep({/::/ou;} split(/[[:alnum:]]+/u, $address, 0)) == 1 || scalar grep({/[[:alnum:]]+/u;} split(/:/u, $address, 0)) == 8
!ll&&!rl&&r
0016
scalar grep({/::/ou;} split(/[[:alnum:]]+/u, $address, 0)) == 1 or scalar grep({/[[:alnum:]]+/u;} split(/:/u, $address, 0)) == 8
l!l&&r!l&&!r
1420
MC/DC0%0/3
$address =~ /$Net::IPAddress::Util::sixish/u and scalar grep({/::/ou;} split(/[[:alnum:]]+/u, $address, 0)) == 1 || scalar grep({/[[:alnum:]]+/u;} split(/:/u, $address, 0)) == 8
$address =~ /$Net::IPAddress::Util::sixish/u scalar grep({/::/ou;} split(/[[:alnum:]]+/u, $address, 0)) == 1 scalar grep({/[[:alnum:]]+/u;} split(/:/u, $address, 0)) == 8
Truth table50%2/4
A and B or C
A= $address =~ /$Net::IPAddress::Util::sixish/uB= scalar grep({/::/ou;} split(/[[:alnum:]]+/u, $address, 0)) == 1C= scalar grep({/[[:alnum:]]+/u;} split(/:/u, $address, 0)) == 8
ABCresult
0XX0
1000
11X1
1011
201 $address =~ $sixish
202 and (
203 51 scalar(grep { /::/o } split(/[[:alnum:]]+/, $address)) == 1
204 16 or scalar(grep { /[[:alnum:]]+/ } split(/:/, $address)) == 8
205 )
206 ) {
207 # new() from IPv6 address, accepting and ignoring the Scope ID
208 16 $address = $1;
209 16 my ($lhs, $rhs) = split /::/, $address;
210 16 ▶ $rhs = '' unless defined $rhs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rhs 2 14 16
211 16 my $hex = '0' x 32;
212 16 $lhs = join '', map { substr('0000' . $_, -4) } split /:/, $lhs;
213 16 $rhs = join '', map { substr('0000' . $_, -4) } split /:/, $rhs;
214 16 substr($hex, 0, length($lhs)) = $lhs;
215 16 substr($hex, - length($rhs), length($rhs)) = $rhs;
216 16 my @hex = split //, $hex;
217 16 ▶ while (@hex) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hex) 256 16 272
218 256 push @$normal, hex(join('', splice(@hex, 0, 2)));
219 }
220 }
221 elsif (length($address) == 16) {
222 0 $normal = [ unpack('C16', $address) ];
223 }
224 else {
225 0 ▶ return ERROR("Invalid argument `$address', a(n) " . (ref($address) || 'bare scalar') . ' provided');
Condition0%0/2
ref $address || 'bare scalar'
l!l
00
MC/DC0%0/1
ref $address || 'bare scalar'
ref $address
Truth table0%0/2
A
A= ref $address
Aresult
11
00
226 }
227 # warn(join(',', @$normal) . "\n");
228 2107 return bless { address => pack('C16', @$normal), %opt } => $class;
229 }
230  
231 1223 ▶sub is_ipv4 {
Subroutine is_ipv4: called CC=2 SCAR=6.9
Pod documented
232 1223 my $self = shift;
233 1223 my @octets = unpack 'C16', $self->{ address };
234 1223 __debug(join(' ', map { sprintf('%3s', $_) } @octets));
235 # my $is_siit = $self->{ SIIT } || 0;
236 1223 ▶ return 0 if grep { $_ } @octets[ 0 .. 7 ];
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_;} @octets[0..7] 98 1125 1223
237 1125 return 1;
238 }
239  
240 621 ▶sub ipv4 {
Subroutine ipv4: called CC=1 SCAR=0.0
Pod documented
241 621 my $self = shift;
242 621 return join '.', unpack 'C4', substr($self->{ address }, -4);
243 }
244  
245 4 ▶sub as_n32 {
Subroutine as_n32: called CC=1 SCAR=0.0
Pod documented
246 4 my $self = shift;
247 4 return unpack 'N', substr($self->{ address }, -4);
248 }
249  
250 2 ▶sub as_n128 {
Subroutine as_n128: called CC=4 SCAR=14.0
Pod documented
251 2 my $self = shift;
252 2 my ($keep) = @_;
253 2 my $rv;
254 {
255 2 ▶ eval "require Math::BigInt" or return ERROR("Could not load Math::BigInt: $@");
Branch: 1 branch
BranchTrueFalseTotal
eval "require Math::BigInt" or return ERROR("Could not load Math::BigInt: $@") 2 0 2
256 2 my $accum = Math::BigInt->from_hex($self->normal_form);
257 2 ▶ eval "no Math::BigInt" unless $keep;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless $keep 2 0 2
258 2 ▶ $rv = $keep ? $accum : "$accum";
Branch: 1 branch
BranchTrueFalseTotal
$keep ? : 0 2 2
259 }
260 2 return $rv;
261 }
262  
263 44 ▶sub normal_form {
Subroutine normal_form: called CC=2 SCAR=6.9
Pod documented
264 44 my $self = shift;
265 44 my @addr = unpack('C16', $self->{ address });
266 44 ▶ splice(@addr, 8, 4, @{$SIIT[$self->{ SIIT }]}) if $self->is_ipv4;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_ipv4 10 34 44
267 44 my $hex = join('', map { sprintf('%02x', $_) } @addr);
268 44 $hex = substr(('0' x 32) . $hex, -32);
269 44 return lc $hex;
270 }
271  
272 28 ▶sub ipv6_expanded {
Subroutine ipv6_expanded: called CC=3 SCAR=11.0
Pod documented
273 28 my $self = shift;
274 28 my $hex = $self->normal_form();
275 28 my $rv;
276 28 ▶ while ($hex =~ /(....)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hex =~ /(....)/gu) 224 28 252
277 224 ▶ $rv .= ':' if defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rv 196 28 224
278 224 $rv .= $1;
279 }
280 28 return $rv;
281 }
282  
283 30 ▶sub ipv6 {
Subroutine ipv6: called CC=4 SCAR=13.9
Pod documented
284 30 my $self = shift;
285 30 ▶ if ($self->is_ipv4()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_ipv4) 2 28 30
286 return $self->{ SIIT }
287 2 ▶ ? '::ffff:0:' . $self->ipv4()
Branch: 1 branch
BranchTrueFalseTotal
$self->{'SIIT'} ? : 1 1 2
288 : '::ffff:' . $self->ipv4()
289 ;
290 }
291 28 my $iv = $self->ipv6_expanded();
292 28 ▶ my $rv = join(':', map { (my $x = $_) =~ s/^0+//; $x ||= '0'; $x } split ':', $iv);
Condition100%2/2
$x ||= 0
l!l
82142
MC/DC100%1/1
$x ||= 0
$x
Truth table100%2/2
A
A= $x
Aresult
11
00
293 28 $rv =~ s/[^[:xdigit:]]0(:0)+/::/;
294 28 $rv =~ s/::+/::/g;
295 28 $rv =~ s/^0::/::/;
296 28 return $rv;
297 }
298  
299 0 ▶sub as_str { return str(@_); }
Subroutine as_str: not called CC=1 SCAR=6.9
Pod documented
300  
301 0 ▶sub as_string { return str(@_); }
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
302  
303 645 ▶sub str {
Subroutine str: called CC=2 SCAR=6.9
Pod documented
304 645 my $self = shift;
305 645 ▶ if ($self->is_ipv4()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_ipv4) 617 28 645
306 617 return $self->ipv4();
307 }
308 28 return $self->ipv6();
309 }
310  
311 432 ▶sub _spaceship {
Subroutine _spaceship: called CC=6 SCAR=20.2
312 432 my $self = shift;
313 432 my ($rhs, $swapped) = @_;
314 432 my $lhs = $self->{ address };
315 432 $lhs = [ unpack 'N4', $lhs ];
316 432 ▶ $rhs = eval { $rhs->{ address } } || pack('N4', (0, 0, 0, $rhs));
Condition33%1/3
eval { do { $rhs->{'address'} } } || pack('N4', (0, 0, 0, $rhs))
l!l&&r!l&&!r
43200
MC/DC0%0/2
eval { do { $rhs->{'address'} } } || pack('N4', (0, 0, 0, $rhs))
eval { do { $rhs->{'address'} } } pack('N4', (0, 0, 0, $rhs))
Truth table33%1/3
A || B
A= eval { do { $rhs->{'address'} } }B= pack('N4', (0, 0, 0, $rhs))
ABresult
1X1
011
000
317 432 $rhs = [ unpack 'N4', $rhs ];
318 432 ▶ ($lhs, $rhs) = ($rhs, $lhs) if $swapped;
Branch: 1 branch
BranchTrueFalseTotal
if $swapped 0 432 432
319 432 ▶ return (1 - (2 * $swapped)) * (
Condition44%4/9
$lhs->[0] <=> $rhs->[0] || $lhs->[1] <=> $rhs->[1] || $lhs->[2] <=> $rhs->[2] || $lhs->[3] <=> $rhs->[3]
l!l&&r!l&&!r
036666
$lhs->[0] <=> $rhs->[0] or $lhs->[1] <=> $rhs->[1] or $lhs->[2] <=> $rhs->[2]
l!l&&r!l&&!r
00432
$lhs->[0] <=> $rhs->[0] or $lhs->[1] <=> $rhs->[1]
l!l&&r!l&&!r
00432
MC/DC25%1/4
$lhs->[0] <=> $rhs->[0] || $lhs->[1] <=> $rhs->[1] || $lhs->[2] <=> $rhs->[2] || $lhs->[3] <=> $rhs->[3]
$lhs->[0] <=> $rhs->[0] $lhs->[1] <=> $rhs->[1] $lhs->[2] <=> $rhs->[2] $lhs->[3] <=> $rhs->[3]
Truth table40%2/5
A or B or C || D
A= $lhs->[0] <=> $rhs->[0]B= $lhs->[1] <=> $rhs->[1]C= $lhs->[2] <=> $rhs->[2]D= $lhs->[3] <=> $rhs->[3]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
320 $lhs->[ 0 ] <=> $rhs->[ 0 ]
321 || $lhs->[ 1 ] <=> $rhs->[ 1 ]
322 || $lhs->[ 2 ] <=> $rhs->[ 2 ]
323 || $lhs->[ 3 ] <=> $rhs->[ 3 ]
324 );
325 }
326  
327 32 ▶sub _do_add {
Subroutine _do_add: called CC=6 SCAR=18.8
328 32 my $self = shift;
329 32 my ($rhs, $swapped) = @_;
330 32 my ($pow, $mask) = $self->_pow_mask;
331 32 my $lhs = $self->{ address };
332 32 $lhs = [ unpack 'N4', $lhs ];
333 32 ▶ $rhs = eval { $rhs->{ address } } || pack('N4', (0, 0, 0, $rhs));
Condition33%1/3
eval { do { $rhs->{'address'} } } || pack('N4', (0, 0, 0, $rhs))
l!l&&r!l&&!r
0320
MC/DC0%0/2
eval { do { $rhs->{'address'} } } || pack('N4', (0, 0, 0, $rhs))
eval { do { $rhs->{'address'} } } pack('N4', (0, 0, 0, $rhs))
Truth table33%1/3
A || B
A= eval { do { $rhs->{'address'} } }B= pack('N4', (0, 0, 0, $rhs))
ABresult
1X1
011
000
334 32 $rhs = [ unpack 'N4', $rhs ];
335 32 ▶ ($lhs, $rhs) = ($rhs, $lhs) if $swapped;
Branch: 1 branch
BranchTrueFalseTotal
if $swapped 0 32 32
336 32 my @l = reverse @$lhs;
337 32 my @r = reverse @$rhs;
338 32 my @rv;
339 32 for my $digit (0 .. 3) {
340 128 my $answer = $l[$digit] + $r[$digit];
341 128 ▶ if ($answer > (2 ** 32) - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer > 4294967295) 0 128 128
342 0 ▶ $r[$digit + 1] += int($answer / (2 ** 32)) if exists $r[$digit + 1];
Branch: 1 branch
BranchTrueFalseTotal
if exists $r[$digit + 1] 0 0 0
343 0 $answer = $answer % (2 ** 32);
344 }
345 128 push @rv, $answer;
346 }
347 32 my %opt;
348 32 @opt{qw( SIIT promote )} = @$self{qw( SIIT promote )};
349 32 @rv = $self->_mask_out($pow, $mask, reverse @rv);
350 32 my $retval = Net::IPAddress::Util->new(\@rv, %opt);
351 32 return $retval;
352 }
353  
354 16 ▶sub _do_subtract {
Subroutine _do_subtract: called CC=6 SCAR=18.8
355 16 my $self = shift;
356 16 my ($rhs, $swapped) = @_;
357 16 my ($pow, $mask) = $self->_pow_mask;
358 16 my $lhs = $self->{ address };
359 16 $lhs = [ unpack 'N4', $lhs ];
360 16 ▶ $rhs = eval { $rhs->{ address } } || pack('N4', (0, 0, 0, $rhs));
Condition33%1/3
eval { do { $rhs->{'address'} } } || pack('N4', (0, 0, 0, $rhs))
l!l&&r!l&&!r
0160
MC/DC0%0/2
eval { do { $rhs->{'address'} } } || pack('N4', (0, 0, 0, $rhs))
eval { do { $rhs->{'address'} } } pack('N4', (0, 0, 0, $rhs))
Truth table33%1/3
A || B
A= eval { do { $rhs->{'address'} } }B= pack('N4', (0, 0, 0, $rhs))
ABresult
1X1
011
000
361 16 $rhs = [ unpack 'N4', $rhs ];
362 16 ▶ ($lhs, $rhs) = ($rhs, $lhs) if $swapped;
Branch: 1 branch
BranchTrueFalseTotal
if $swapped 0 16 16
363 16 my @l = reverse @$lhs;
364 16 my @r = reverse @$rhs;
365 16 my @rv;
366 16 for my $digit (0 .. 3) {
367 64 my $answer = $l[$digit] - $r[$digit];
368 64 ▶ if ($answer < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer < 0) 0 64 64
369 0 $answer += (2 ** 32) - 1;
370 0 ▶ $r[$digit + 1] -= 1 if exists $r[$digit + 1];
Branch: 1 branch
BranchTrueFalseTotal
if exists $r[$digit + 1] 0 0 0
371 }
372 64 push @rv, $answer;
373 }
374 16 my %opt;
375 16 @opt{qw( SIIT promote )} = @$self{qw( SIIT promote )};
376 16 @rv = $self->_mask_out($pow, $mask, reverse @rv);
377 16 my $retval = Net::IPAddress::Util->new(\@rv, %opt);
378 16 return $retval;
379 }
380  
381 0 ▶sub _shift_left {
Subroutine _shift_left: not called CC=4 SCAR=30.0
382 0 my $self = shift;
383 0 my ($rhs, $swapped) = @_;
384 0 my ($pow, $mask) = $self->_pow_mask;
385 0 my @l = reverse unpack('C16', $self->{ address });
386 0 my @rv;
387 0 for my $octet (0 .. 15) {
388 0 $rv[$octet] += $l[$octet] << $rhs;
389 0 ▶ if ($rv[$octet] > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv[$octet] > 255) 0 0 0
390 0 my $lsb = $rv[$octet] % 256;
391 0 ▶ $rv[$octet + 1] += ($rv[$octet] - $lsb) >> 8 if $octet < 15;
Branch: 1 branch
BranchTrueFalseTotal
if $octet < 15 0 0 0
392 0 $rv[$octet] = $lsb;
393 }
394 }
395 0 @rv = $self->_mask_out($pow, $mask, @rv);
396 0 return Net::IPAddress::Util->new(\@rv);
397 }
398  
399 0 ▶sub _shift_right {
Subroutine _shift_right: not called CC=4 SCAR=30.0
400 0 my $self = shift;
401 0 my ($rhs, $swapped) = @_;
402 0 my ($pow, $mask) = $self->_pow_mask;
403 0 my @l = unpack('C16', $self->{ address });
404 0 my @rv;
405 0 for my $octet (0 .. 15) {
406 0 $rv[$octet] += $l[$octet] >> $rhs;
407 0 ▶ if (int($rv[$octet]) - $rv[$octet]) {
Branch: 1 branch
BranchTrueFalseTotal
if (int($rv[$octet]) - $rv[$octet]) 0 0 0
408 0 my $msb = int($rv[$octet]);
409 0 my $lsb = $rv[$octet] << $rhs;
410 0 $rv[$octet] = $msb;
411 0 ▶ $rv[$octet + 1] += $lsb if $octet < 15;
Branch: 1 branch
BranchTrueFalseTotal
if $octet < 15 0 0 0
412 }
413 }
414 0 @rv = $self->_mask_out($pow, $mask, unpack('C16', pack('N4', @rv)));
415 0 return Net::IPAddress::Util->new(\@rv);
416 }
417  
418 282 ▶sub _band {
Subroutine _band: called CC=5 SCAR=16.4
419 282 my $self = shift;
420 282 my ($rhs, $swapped) = @_;
421 282 ▶ ($self, $rhs) = ($rhs, $self) if $swapped;
Branch: 1 branch
BranchTrueFalseTotal
if $swapped 0 282 282
422 282 my $lhs = $self->{ address };
423 282 $lhs = [ unpack 'N4', $lhs ];
424 282 ▶ $rhs = eval { $rhs->{ address } } || pack('N4', (0, 0, 0, $rhs));
Condition33%1/3
eval { do { $rhs->{'address'} } } || pack('N4', (0, 0, 0, $rhs))
l!l&&r!l&&!r
28200
MC/DC0%0/2
eval { do { $rhs->{'address'} } } || pack('N4', (0, 0, 0, $rhs))
eval { do { $rhs->{'address'} } } pack('N4', (0, 0, 0, $rhs))
Truth table33%1/3
A || B
A= eval { do { $rhs->{'address'} } }B= pack('N4', (0, 0, 0, $rhs))
ABresult
1X1
011
000
425 282 $rhs = [ unpack 'N4', $rhs ];
426 282 ▶ ($lhs, $rhs) = ($rhs, $lhs) if $swapped;
Branch: 1 branch
BranchTrueFalseTotal
if $swapped 0 282 282
427 282 my @l = @$lhs;
428 282 my @r = @$rhs;
429 282 my @rv;
430 282 for my $hextet (0 .. 3) {
431 1128 $rv[$hextet] = $l[$hextet] & $r[$hextet];
432 }
433 282 return Net::IPAddress::Util->new(\@rv);
434 }
435  
436 282 ▶sub _bor {
Subroutine _bor: called CC=5 SCAR=16.4
437 282 my $self = shift;
438 282 my ($rhs, $swapped) = @_;
439 282 ▶ ($self, $rhs) = ($rhs, $self) if $swapped;
Branch: 1 branch
BranchTrueFalseTotal
if $swapped 0 282 282
440 282 my $lhs = $self->{ address };
441 282 $lhs = [ unpack 'N4', $lhs ];
442 282 ▶ $rhs = eval { $rhs->{ address } } || pack('N4', (0, 0, 0, $rhs));
Condition33%1/3
eval { do { $rhs->{'address'} } } || pack('N4', (0, 0, 0, $rhs))
l!l&&r!l&&!r
28200
MC/DC0%0/2
eval { do { $rhs->{'address'} } } || pack('N4', (0, 0, 0, $rhs))
eval { do { $rhs->{'address'} } } pack('N4', (0, 0, 0, $rhs))
Truth table33%1/3
A || B
A= eval { do { $rhs->{'address'} } }B= pack('N4', (0, 0, 0, $rhs))
ABresult
1X1
011
000
443 282 $rhs = [ unpack 'N4', $rhs ];
444 282 ▶ ($lhs, $rhs) = ($rhs, $lhs) if $swapped;
Branch: 1 branch
BranchTrueFalseTotal
if $swapped 0 282 282
445 282 my @l = @$lhs;
446 282 my @r = @$rhs;
447 282 my @rv;
448 282 for my $hextet (0 .. 3) {
449 1128 $rv[$hextet] = $l[$hextet] | $r[$hextet];
450 }
451 282 return Net::IPAddress::Util->new(\@rv);
452 }
453  
454 282 ▶sub _neg {
Subroutine _neg: called CC=1 SCAR=0.0
455 282 my $self = shift;
456 282 my @n = unpack('C16', $self->{ address });
457 282 my @rv = map { 255 - $_ } @n;
458 282 return Net::IPAddress::Util->new(\@rv);
459 }
460  
461 48 ▶sub _pow_mask {
Subroutine _pow_mask: called CC=2 SCAR=7.0
462 48 my $self = shift;
463 48 my $pow = 128;
464 48 my $mask = pack('N4', 0, 0, 0, 0);
465 48 ▶ if ($self->is_ipv4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_ipv4) 48 0 48
466 48 $pow = 32;
467 $mask = pack('C16',
468 0, 0, 0, 0,
469 0, 0, 0, 0,
470 48 @{$SIIT[ $self->{ SIIT } ]},
471 0, 0, 0, 0,
472 );
473 }
474 48 return ($pow, $mask);
475 }
476  
477 48 ▶sub _mask_out {
Subroutine _mask_out: called CC=1 SCAR=0.0
478 48 my $self = shift;
479 48 my ($pow, $mask, @rv) = @_;
480 48 my @and = (0, 0, 0, 0);
481 48 map { $and[ 4 - $_ ] = 0xffffffff } grep { $pow / $_ >= 32 } (1 .. 4);
482 48 my @or = unpack('N4', $mask);
483 48 ▶ @rv = pairwise { $a & $b } @rv, @and;
Subroutine __ANON__: called CC=1 SCAR=0.0
484 48 ▶ @rv = pairwise { $a | $b } @rv, @or;
Subroutine __ANON__: called CC=1 SCAR=0.0
485 48 return @rv;
486 }
487  
488 174 ▶sub ipv4_mask {
Subroutine ipv4_mask: called CC=1 SCAR=0.0
Pod documented
489 174 return implode_ip(('0' x 64) . ('1' x 16) . ('0' x 16) . ('1' x 32));
490 }
491  
492 0 ▶sub ipv4_flag {
Subroutine ipv4_flag: not called CC=1 SCAR=6.9
Pod documented
493 0 return implode_ip(('0' x 64) . ('1' x 16) . ('0' x 48));
494 }
495  
496 174 ▶sub common_prefix (\@\@) {
Subroutine common_prefix: called CC=4 SCAR=13.9
Pod documented
497 174 my ($x, $y) = @_;
498 174 ▶ return ERROR("Something isn't right there") unless @$x == @$y;
Branch: 1 branch
BranchTrueFalseTotal
unless @$x == @$y 0 174 174
499 174 my @rv;
500 174 for my $i (0 .. $#$x) {
501 21712 ▶ if($x->[$i] eq $y->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->[$i] eq $y->[$i]) { } 21550 162 21712
502 21550 push @rv, $x->[$i];
503 }
504 else {
505 162 last;
506 }
507 }
508 174 return @rv;
509 }
510  
511 174 ▶sub prefix_mask (\@\@) {
Subroutine prefix_mask: called CC=4 SCAR=13.9
Pod documented
512 174 my ($x, $y) = @_;
513 174 ▶ return ERROR("Something isn't right there") unless @$x == @$y;
Branch: 1 branch
BranchTrueFalseTotal
unless @$x == @$y 0 174 174
514 174 my @rv;
515 174 for my $i (0 .. $#$x) {
516 21712 ▶ if($x->[$i] == $y->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->[$i] == $y->[$i]) { } 21550 162 21712
517 21550 push @rv, 1;
518 }
519 else {
520 162 last;
521 }
522 }
523 174 return @rv;
524 }
525  
526 348 ▶sub ip_pad_prefix (\@) {
Subroutine ip_pad_prefix: called CC=2 SCAR=6.9
Pod documented
527 348 my @array = @{$_[0]};
528 348 for my $i (scalar(@array) .. 127) {
529 1444 push @array, 0;
530 }
531 348 return @array;
532 }
533  
534 348 ▶sub explode_ip {
Subroutine explode_ip: called CC=1 SCAR=0.0
Pod documented
535 348 my $ip = shift;
536 348 return map { ~~$_ } split //, unpack 'B128', $ip->{ address };
537 }
538  
539 628 ▶sub implode_ip {
Subroutine implode_ip: called CC=1 SCAR=0.0
Pod documented
540 628 return Net::IPAddress::Util->new([ unpack 'C16', pack 'B128', join '', map { ~~$_ } map { split // } @_ ]);
541 }
542  
543 0 ▶sub n32_to_ipv4 { local $PROMOTE_N32 = 1; return IP(@_) }
Subroutine n32_to_ipv4: not called CC=1 SCAR=6.9
Pod documented
544  
545 0 ▶sub ERROR {
Subroutine ERROR: not called CC=4 SCAR=30.0
Pod documented
546 0 ▶ my $msg = @_ ? shift() : 'An error has occured';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
547 0 ▶ if ($DIE_ON_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::IPAddress::Util::DIE_ON_ERROR) { } 0 0 0
548 0 confess($msg);
549 }
550 else {
551 0 ▶ cluck($msg) if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
552 }
553 0 return;
554 }
555  
556 0 ▶sub radix_sort (\@) {
Subroutine radix_sort: not called CC=5 SCAR=34.0
Pod documented
557 # In theory, a radix sort is O(N), which beats Perl's O(N log N) by
558 # a fair margin. However, it _does_ discard duplicates, so ymmv.
559 0 ▶ shift if $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq __PACKAGE__ 0 0 0
560 0 my $array = shift;
561 0 my $from = [ map { [ unpack 'C16', $_->{ address } ] } @$array ];
562 0 my $to;
563 0 ▶ for (my $i = 15; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 0 0 0
564 0 $to = [];
565 0 for my $card (@$from) {
566 0 push @{$to->[ $card->[ $i ] ]}, $card;
567 }
568 0 ▶ $from = [ map { @{$_ // []} } @$to ];
Condition0%0/2
$_ // []
l!l
00
MC/DC0%0/1
$_ // []
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
569 }
570 0 my @rv = map { IP(pack 'C16', @$_) } @$from;
571 0 return @rv;
572 }
573  
574 0 ▶sub ip2num {
Subroutine ip2num: not called CC=2 SCAR=17.9
Pod documented
575 0 ▶ carp('Compatibility function ip2num() is deprecated') if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
576 0 my $ip = shift;
577 0 my $self = IP($ip);
578 0 $self &= ((2 ** 32) - 1);
579 0 return $self->as_n32();
580 }
581  
582 0 ▶sub num2ip {
Subroutine num2ip: not called CC=2 SCAR=17.9
Pod documented
583 0 ▶ carp('Compatibility function num2ip() is deprecated') if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
584 0 my $num = shift;
585 0 my $self = n32_to_ipv4($num);
586 0 return $self->str();
587 }
588  
589 0 ▶sub validaddr {
Subroutine validaddr: not called CC=7 SCAR=40.3
Pod documented
590 0 ▶ carp('Compatibility function validaddr() is deprecated') if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
591 0 my $ip = shift;
592 0 my @octets = split(/\./, $ip);
593 0 ▶ return unless scalar @octets == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @octets == 4 0 0 0
594 0 for (@octets) {
595 0 ▶ return unless defined $_ && $_ >= 0 && $_ <= 255;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ and $_ >= 0 and $_ <= 255 0 0 0
Condition0%0/6
defined $_ and $_ >= 0 and $_ <= 255
!ll&&!rl&&r
000
defined $_ and $_ >= 0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_ and $_ >= 0 and $_ <= 255
defined $_ $_ >= 0 $_ <= 255
Truth table0%0/4
A and B and C
A= defined $_B= $_ >= 0C= $_ <= 255
ABCresult
0XX0
10X0
1100
1111
596 }
597 0 return 1;
598 }
599  
600 0 ▶sub mask {
Subroutine mask: not called CC=2 SCAR=17.9
Pod documented
601 0 ▶ carp('Compatibility function mask() is deprecated') if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
602 0 my ($ip, $mask) = @_;
603 0 my $self = IP($ip);
604 0 my $nm = IP($mask);
605 0 return $self & $nm;
606 }
607  
608 0 ▶sub fqdn {
Subroutine fqdn: not called CC=2 SCAR=17.9
Pod documented
609 0 ▶ carp('Compatibility function fqdn() is deprecated') if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
610 0 my $dn = shift;
611 0 return split(/\./, $dn, 2);
612 }
613  
614 5 1;
615  
616 __END__
« Util/Collection/Tie.pm ↑ Summary Util/Range.pm »