IP/Lite.pm

filter: Statementstmt 97.8% 548 / 560 Branchbran 91.2% 418 / 458 Conditioncond 83.6% 301 / 360 MC/DCmcdc 70.9% 139 / 196 Subroutinesub 100% 80 / 80 Podpod 86.8% 33 / 38 totaltotal 89.7% 1519 / 1692 CC 352 SCAR 45.7
CC
352
Cov
92%
_xnew
212.0
nth
19.6
bigint
13.6
CRAP 96.3
↑ Summary IP.pm »
1 #!/bin/false
2 # ABSTRACT: Manages IPv4 and IPv6 addresses and subnets (lightweight)
3 # PODNAME: NetAddr::IP::Lite
4  
5 122 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 122 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package NetAddr::IP::Lite;
9  
10 122 our $VERSION = '4.080_09'; # TRIAL VERSION
11  
12 122 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 122 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 122 ▶use Scalar::Util qw( blessed looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 122 ▶use NetAddr::IP::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
17 $IPV6_BITS
18 $IPV4_BITS
19 $IPV4_OFFSET
20 $V6_PACKED_BYTES
21 $V4_PACKED_BYTES
22 122 );
23 122 ▶use NetAddr::IP::InetBase qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
24 fillIPv4
25 inet_any2n
26 inet_aton
27 inet_n2dx
28 ipv6_aton
29 ipv6_n2x
30 isIPv4
31 122 );
32 122 ▶use NetAddr::IP::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
33 add128
34 addconst
35 bcd2bin
36 bin2bcd
37 hasbits
38 havegethostbyname2
39 ipv4to6
40 ipv6to4
41 isAnyIPv4
42 mask4to6
43 notcontiguous
44 shiftleft
45 sub128
46 122 );
47  
48 122 our @EXPORT_OK = qw(Zeros Zero Ones V4mask V4net);
49 122 our $Accept_Binary_IP = 0;
50 122 our $Old_nth = 0;
51 122 our $NoFQDN;
52 122 our $AUTOLOAD;
53  
54 122 *Zero = \&Zeros;
55  
56  
57 # in the off chance that NetAddr::IP::Lite objects are created
58 # and the caller later loads NetAddr::IP and expects to use
59 # those objects, let the AUTOLOAD routine find and redirect
60 # NetAddr::IP::Lite method and subroutine calls to NetAddr::IP.
61 #
62  
63 122 my $parent = 'NetAddr::IP';
64  
65 1 ▶sub DESTROY { }
Subroutine DESTROY: called CC=1 SCAR=0.0
66  
67 9 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=17.1
68 122 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 9 my ($pkg, $func) = ($AUTOLOAD =~ /(.*)::([^:]+)$/);
70 9 my $other = $parent . '::';
71  
72 9 ▶ if ($pkg =~ /^$other/o && exists ${$other}{$func}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg =~ /^$other/o and exists do { $other }->{$func}) 9 0 9
Condition33%1/3
$pkg =~ /^$other/o and exists do { $other }->{$func}
!ll&&!rl&&r
009
MC/DC0%0/2
$pkg =~ /^$other/o and exists do { $other }->{$func}
$pkg =~ /^$other/o exists do { $other }->{$func}
Truth table33%1/3
A and B
A= $pkg =~ /^$other/oB= exists do { $other }->{$func}
ABresult
0X0
100
111
73 9 $other .= $func;
74 9 goto &{$other};
75 }
76  
77 0 my @stack = caller(0);
78  
79 0 ▶ if ($pkg eq ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg eq ref $_[0]) { } 0 0 0
80 0 $other = qq|Can't locate object method "$func" via|;
81 }
82 else {
83 0 $other = qq|Undefined subroutine \&$AUTOLOAD not found in|;
84 }
85 0 die $other . qq| package "$parent" or "$pkg" (did you forgot to load a module?) at $stack[1] line $stack[2].\n|;
86 }
87  
88  
89 # these really should be packed in Network Long order but since they are
90 # symmetrical, that extra internal processing can be skipped
91  
92 122 my $_v4zero = pack('L', 0);
93 122 my $_zero = pack('L4', 0, 0, 0, 0);
94 122 my $_ones = ~$_zero;
95 122 my $_v4mask = pack('L4', 0xffffffff, 0xffffffff, 0xffffffff, 0);
96 122 my $_v4net = ~$_v4mask;
97 122 my $_ipv4FFFF = pack('N4', 0, 0, 0xffff, 0);
98 122 my $_one128 = pack('N4', 0, 0, 0, 1); # the number 1 in 128 bits
99  
100 1104 ▶sub Zeros() {
Subroutine Zeros: called CC=1 SCAR=0.0
Pod documented
101 1104 return $_zero;
102 }
103  
104 10045 ▶sub Ones() {
Subroutine Ones: called CC=1 SCAR=0.0
Pod documented
105 10045 return $_ones;
106 }
107  
108 4 ▶sub V4mask() {
Subroutine V4mask: called CC=1 SCAR=0.0
Pod documented
109 4 return $_v4mask;
110 }
111  
112 613 ▶sub V4net() {
Subroutine V4net: called CC=1 SCAR=0.0
Pod documented
113 613 return $_v4net;
114 }
115  
116 #############################################
117 # These are the overload methods, placed here
118 # for convenience.
119 #############################################
120  
121 use overload
122  
123 122 ▶ '+' => \&plus,
Subroutine BEGIN: called CC=1 SCAR=0.0
124  
125 '-' => \&minus,
126  
127 '++' => \&plusplus,
128  
129 '--' => \&minusminus,
130  
131 1 ▶ 'neg' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 1 croak 'cannot negate a ' . ref($_[0]) . ' object';
133 },
134  
135 1 ▶ 'abs' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
136 1 croak 'cannot take the absolute value of a ' . ref($_[0]) . ' object';
137 },
138  
139 "=" => \&copy,
140  
141 25154 ▶ '""' => sub { $_[0]->cidr(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
142  
143 579 ▶ 'eq' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
144 579 ▶ my $a = _is_address($_[0]) ? $_[0]->cidr : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
_is_address($_[0]) ? : 579 0 579
145 579 ▶ my $b = _is_address($_[1]) ? $_[1]->cidr : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
_is_address($_[1]) ? : 53 526 579
146 579 $a eq $b;
147 },
148  
149 5 ▶ 'ne' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
150 5 ▶ my $a = _is_address($_[0]) ? $_[0]->cidr : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
_is_address($_[0]) ? : 5 0 5
151 5 ▶ my $b = _is_address($_[1]) ? $_[1]->cidr : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
_is_address($_[1]) ? : 1 4 5
152 5 $a ne $b;
153 },
154  
155 75 ▶ '==' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
156 75 ▶ return 0 unless _is_address($_[0]) && _is_address($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_address($_[0]) and _is_address($_[1]) 6 69 75
Condition66%2/3
_is_address($_[0]) and _is_address($_[1])
!ll&&!rl&&r
0669
MC/DC50%1/2
_is_address($_[0]) and _is_address($_[1])
_is_address($_[0]) _is_address($_[1])
Truth table66%2/3
A and B
A= _is_address($_[0])B= _is_address($_[1])
ABresult
0X0
100
111
157 69 $_[0]->cidr eq $_[1]->cidr;
158 },
159  
160 7 ▶ '!=' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
161 7 ▶ return 1 unless _is_address($_[0]) && _is_address($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_address($_[0]) and _is_address($_[1]) 3 4 7
Condition66%2/3
_is_address($_[0]) and _is_address($_[1])
!ll&&!rl&&r
034
MC/DC50%1/2
_is_address($_[0]) and _is_address($_[1])
_is_address($_[0]) _is_address($_[1])
Truth table66%2/3
A and B
A= _is_address($_[0])B= _is_address($_[1])
ABresult
0X0
100
111
162 4 $_[0]->cidr ne $_[1]->cidr;
163 },
164  
165 41 ▶ '>' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
166 41 my $order = &comp_addr_mask;
167 41 ▶ return defined $order && $order > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $order && $order > 0 ? : 14 27 41
Condition100%3/3
defined $order and $order > 0
!ll&&!rl&&r
141314
MC/DC100%2/2
defined $order and $order > 0
defined $order $order > 0
Truth table100%3/3
A and B
A= defined $orderB= $order > 0
ABresult
0X0
100
111
168 },
169  
170 8487 ▶ '<' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
171 8487 my $order = &comp_addr_mask;
172 8487 ▶ return defined $order && $order < 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $order && $order < 0 ? : 8334 153 8487
Condition100%3/3
defined $order and $order < 0
!ll&&!rl&&r
151388334
MC/DC100%2/2
defined $order and $order < 0
defined $order $order < 0
Truth table100%3/3
A and B
A= defined $orderB= $order < 0
ABresult
0X0
100
111
173 },
174  
175 74 ▶ '>=' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
176 74 my $order = &comp_addr_mask;
177 74 ▶ return defined $order && $order >= 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $order && $order >= 0 ? : 28 46 74
Condition100%3/3
defined $order and $order >= 0
!ll&&!rl&&r
143228
MC/DC100%2/2
defined $order and $order >= 0
defined $order $order >= 0
Truth table100%3/3
A and B
A= defined $orderB= $order >= 0
ABresult
0X0
100
111
178 },
179  
180 46 ▶ '<=' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
181 46 my $order = &comp_addr_mask;
182 46 ▶ return defined $order && $order <= 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $order && $order <= 0 ? : 11 35 46
Condition100%3/3
defined $order and $order <= 0
!ll&&!rl&&r
142111
MC/DC100%2/2
defined $order and $order <= 0
defined $order $order <= 0
Truth table100%3/3
A and B
A= defined $orderB= $order <= 0
ABresult
0X0
100
111
183 },
184  
185 '<=>' => \&comp_addr_mask,
186  
187 58004 ▶ 'cmp' => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
188 58004 my ($ip, $other, $swapped) = @_;
189 58004 ▶ return comp_addr_mask($ip, $other)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($other) and $other->isa(__PACKAGE__) 57984 20 58004
Condition66%2/3
builtin::blessed($other) and $other->isa(__PACKAGE__)
!ll&&!rl&&r
20057984
MC/DC50%1/2
builtin::blessed($other) and $other->isa(__PACKAGE__)
builtin::blessed($other) $other->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($other)B= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
190 if blessed $other && $other->isa(__PACKAGE__);
191  
192 # a non-object is the CIDR text, in the written order, as eq does
193 20 ▶ return $swapped ? $other cmp $ip->cidr : $ip->cidr cmp $other;
Branch: 1 branch
BranchTrueFalseTotal
$swapped ? : 10 10 20
194 122 };
195  
196 66729 ▶sub comp_addr_mask {
Subroutine comp_addr_mask: called CC=9 SCAR=22.0
Pod undocumented
197  
198 # no address order against a non-object: undef, as <=> is for NaN
199 66729 ▶ return undef unless blessed $_[1] && $_[1]->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[1]) and $_[1]->isa(__PACKAGE__) 71 66658 66729
Condition100%3/3
builtin::blessed($_[1]) and $_[1]->isa(__PACKAGE__)
!ll&&!rl&&r
66566658
MC/DC100%2/2
builtin::blessed($_[1]) and $_[1]->isa(__PACKAGE__)
builtin::blessed($_[1]) $_[1]->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= builtin::blessed($_[1])B= $_[1]->isa(__PACKAGE__)
ABresult
0X0
100
111
200 66658 my ($c, $rv) = sub128($_[0]->{addr}, $_[1]->{addr});
201 66658 ▶ return -1 unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 64026 2632 66658
202 2632 ▶ return 1 if hasbits($rv);
Branch: 1 branch
BranchTrueFalseTotal
if &hasbits($rv) 2283 349 2632
203 349 ($c, $rv) = sub128($_[0]->{mask}, $_[1]->{mask});
204 349 ▶ return -1 unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 48 301 349
205 301 ▶ return 1 if hasbits($rv);
Branch: 1 branch
BranchTrueFalseTotal
if &hasbits($rv) 42 259 301
206  
207 # 192.0.2.4/24 and ::192.0.2.4/120 hold the same bits: IPv4 first, so
208 # <=> gives 0 only when == holds
209 259 ▶ return ($_[0]->{isv6} ? 1 : 0) <=> ($_[1]->{isv6} ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$_[0]{'isv6'} ? : 168 91 259
$_[1]{'isv6'} ? : 180 79 259
210 }
211  
212  
213 54579 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
214 54579 return _new($_[0], $_[0]->{addr}, $_[0]->{mask});
215 }
216  
217  
218 127 ▶sub plus {
Subroutine plus: called CC=1 SCAR=0.0
Pod undocumented
219 127 my ($ip, $const) = @_;
220 127 return _add_constant($ip, $const, 0);
221 }
222  
223 # adds $const to the host part, or subtracts it when $subtract is true
224 199 ▶sub _add_constant {
Subroutine _add_constant: called CC=12 SCAR=24.9
225 199 my ($ip, $const, $subtract) = @_;
226  
227 199 ▶ return $ip->copy unless $const;
Branch: 1 branch
BranchTrueFalseTotal
unless $const 10 189 199
228  
229 # a number, integer, unit precision (rejects floats above 2**53),
230 # magnitude below 2**64, as text so Math::BigInt reads the bound exactly
231 189 ▶ croak 'constant must be an exact integer with magnitude below 2**64'
Branch: 2 branches
BranchTrueFalseTotal
unless defined $const and &looks_like_number($const) and $const == int $const and $const > 0 ? $const - 1 != $const : $const + 1 != $const and abs $const <= "18446744073709551615" 30 159 189
$const > 0 ? : 163 8 171
Condition91%11/12
defined $const and &looks_like_number($const) and $const == int $const and $const > 0 ? $const - 1 != $const : $const + 1 != $const and abs $const <= "18446744073709551615"
!ll&&!rl&&r
246159
defined $const and &looks_like_number($const) and $const == int $const and $const > 0 ? $const - 1 != $const : $const + 1 != $const
!ll&&!rl&&r
186165
defined $const and &looks_like_number($const) and $const == int $const
!ll&&!rl&&r
171171
defined $const and &looks_like_number($const)
!ll&&!rl&&r
017172
MC/DC80%4/5
defined $const and &looks_like_number($const) and $const == int $const and $const > 0 ? $const - 1 != $const : $const + 1 != $const and abs $const <= "18446744073709551615"
defined $const &looks_like_number($const) $const == int $const $const > 0 ? $const - 1 != $const : $const + 1 != $const abs $const <= "18446744073709551615"
Truth table83%5/6
A and B and C and D and E
A= defined $constB= &looks_like_number($const)C= $const == int $constD= $const > 0 ? $const - 1 != $const : $const + 1 != $constE= abs $const <= "18446744073709551615"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
232 unless defined $const
233 && looks_like_number($const)
234 && $const == int($const)
235 && ($const > 0 ? $const - 1 != $const : $const + 1 != $const)
236 && abs($const) <= '18446744073709551615';
237  
238 159 my $a = $ip->{addr};
239 159 my $m = $ip->{mask};
240  
241 159 my $lo = $a & ~$m;
242 159 my $hi = $a & $m;
243  
244 # the sign travels apart from the magnitude, which stays exact: negating a
245 # UV above 2**63 would give a float
246 159 ▶ my $down = $const < 0 ? !$subtract : $subtract;
Branch: 1 branch
BranchTrueFalseTotal
$const < 0 ? : 4 155 159
247 159 my $magnitude = abs($const);
248 159 my $new;
249 159 ▶ if ($magnitude < 2147483648) {
Branch: 1 branch
BranchTrueFalseTotal
if ($magnitude < 2147483648) { } 117 42 159
250 117 ▶ $new = (addconst($lo, $down ? -$magnitude : $magnitude))[1];
Branch: 1 branch
BranchTrueFalseTotal
$down ? : 22 95 117
251 }
252 else {
253 42 my $mag = _const2bin($magnitude);
254 42 ▶ $new =
Branch: 1 branch
BranchTrueFalseTotal
$down ? : 28 14 42
255 $down
256 ? (sub128($lo, $mag))[1]
257 : (add128($lo, $mag))[1];
258 }
259 159 $new = ($new & ~$m) | $hi;
260  
261 159 return _new($ip, $new, $m);
262 }
263  
264 # 128 bit string for a non-negative integer constant below 2**64.
265 # Bit operators give exact results on a 64 bit perl; the division
266 # fallback is exact up to 2**53 on a 32 bit perl.
267 42 ▶sub _const2bin {
Subroutine _const2bin: called CC=2 SCAR=6.9
268 42 my $c = shift;
269 42 my ($hi, $lo);
270 42 ▶ if ($c <= 4294967295) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c <= 4294967295) { } 6 36 42
271 6 ($hi, $lo) = (0, $c);
272 }
273 elsif (~0 > 4294967295) {
274 36 $hi = $c >> 32;
275 36 $lo = $c & 4294967295;
276 }
277 else {
278 $hi = int($c / 4294967296);
279 $lo = $c - $hi * 4294967296;
280 }
281 42 return pack('N4', 0, 0, $hi, $lo);
282 }
283  
284  
285 # the largest difference minus() returns; one more is allowed below zero
286 122 my $_int32_max = 0x7fff_ffff;
287  
288 103 ▶sub minus {
Subroutine minus: called CC=8 SCAR=20.8
Pod undocumented
289 103 my ($ip, $arg, $swapped) = @_;
290 103 ▶ croak 'cannot subtract a NetAddr::IP::Lite object from a constant'
Branch: 1 branch
BranchTrueFalseTotal
if $swapped 1 102 103
291 if $swapped;
292  
293 # A Math::BigInt is a reference too, so test for an address rather
294 # than for any reference. blessed first, or the isa call on an
295 # unblessed reference dies instead of being answered.
296 102 ▶ unless (blessed $arg && $arg->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($arg) and $arg->isa(__PACKAGE__)) 72 30 102
Condition100%3/3
builtin::blessed($arg) and $arg->isa(__PACKAGE__)
!ll&&!rl&&r
611130
MC/DC100%2/2
builtin::blessed($arg) and $arg->isa(__PACKAGE__)
builtin::blessed($arg) $arg->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= builtin::blessed($arg)B= $arg->isa(__PACKAGE__)
ABresult
0X0
100
111
297 72 return _add_constant($ip, $arg, 1);
298 }
299  
300 # subtract the lower address from the higher, then give the difference its sign
301 my ($sign, $high, $low) =
302 $ip->{addr} lt $arg->{addr}
303 ? (-1, $arg->{addr}, $ip->{addr})
304 30 ▶ : (1, $ip->{addr}, $arg->{addr});
Branch: 1 branch
BranchTrueFalseTotal
$ip->{'addr'} lt $arg->{'addr'} ? : 13 17 30
305 30 my (undef, $dif) = sub128($high, $low);
306 30 my $magnitude = (unpack('N4', $dif))[3];
307 30 ▶ my $limit = $sign > 0 ? $_int32_max : $_int32_max + 1;
Branch: 1 branch
BranchTrueFalseTotal
$sign > 0 ? : 17 13 30
308 30 ▶ return undef if hasbits($dif & $_v4mask) || $magnitude > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if &hasbits($dif & $_v4mask) or $magnitude > $limit 3 27 30
Condition100%3/3
&hasbits($dif & $_v4mask) or $magnitude > $limit
l!l&&r!l&&!r
1227
MC/DC100%2/2
&hasbits($dif & $_v4mask) or $magnitude > $limit
&hasbits($dif & $_v4mask) $magnitude > $limit
Truth table100%3/3
A or B
A= &hasbits($dif & $_v4mask)B= $magnitude > $limit
ABresult
1X1
011
000
309 27 return $sign * $magnitude;
310 }
311  
312  
313 8339 ▶sub plusplus {
Subroutine plusplus: called CC=1 SCAR=0.0
Pod undocumented
314 8339 my $ip = shift;
315  
316 8339 my $a = $ip->{addr};
317 8339 my $m = $ip->{mask};
318  
319 8339 my $lo = $a & ~$m;
320 8339 my $hi = $a & $m;
321  
322 8339 $ip->{addr} = ((addconst($lo, 1))[1] & ~$m) | $hi;
323 8339 return $ip;
324 }
325  
326  
327 6 ▶sub minusminus {
Subroutine minusminus: called CC=1 SCAR=0.0
Pod undocumented
328 6 my $ip = shift;
329  
330 6 my $a = $ip->{addr};
331 6 my $m = $ip->{mask};
332  
333 6 my $lo = $a & ~$m;
334 6 my $hi = $a & $m;
335  
336 6 $ip->{addr} = ((addconst($lo, -1))[1] & ~$m) | $hi;
337 6 return $ip;
338 }
339  
340 #############################################
341 # End of the overload methods.
342 #############################################
343  
344 # Preloaded methods go here.
345  
346 # This is a variant to ->new() that
347 # creates and blesses a new object
348 # without the fancy parsing of
349 # IP formats and shorthands.
350  
351 # return a blessed IP object without parsing
352 # input: prototype, naddr, nmask
353 # returns: blessed IP object
354 #
355 217081 ▶sub _new ($$$) {
Subroutine _new: called CC=2 SCAR=7.0
356 217081 my $proto = shift;
357 217081 ▶ my $class = ref($proto) || die "reference required";
Condition50%1/2
ref $proto || die('reference required')
l!l
2170810
MC/DC0%0/1
ref $proto || die('reference required')
ref $proto
Truth table50%1/2
A
A= ref $proto
Aresult
11
00
358 217081 $proto = $proto->{isv6};
359 217081 my $self = {
360 addr => $_[0],
361 mask => $_[1],
362 isv6 => $proto,
363 };
364 217081 return bless $self, $class;
365 }
366  
367  
368 122 my $_p4broad = inet_any2n('255.255.255.255');
369 122 my $_p4loop = inet_any2n('127.0.0.1');
370  
371 # a literal mask must not need a resolver: InetBase::inet_aton is a
372 # gethostbyname call, which fails at load time wherever the resolver cannot
373 # answer a dotted quad
374 122 my $_p4mloop = mask4to6(pack('C4', 255, 0, 0, 0));
375 122 my $_p6loop = inet_any2n('::1');
376  
377 122 my %fip4 = (
378 default => Zeros,
379 any => Zeros,
380 broadcast => $_p4broad,
381 loopback => $_p4loop,
382 unspecified => undef,
383 );
384 122 my %fip4m = (
385 default => Zeros,
386 any => Zeros,
387 broadcast => Ones,
388 loopback => $_p4mloop,
389 unspecified => undef, # not applicable for ipV4
390 host => Ones,
391 );
392  
393 122 my %fip6 = (
394 default => Zeros,
395 any => Zeros,
396 broadcast => undef, # not applicable for ipV6
397 loopback => $_p6loop,
398 unspecified => Zeros,
399 );
400  
401 122 my %fip6m = (
402 default => Zeros,
403 any => Zeros,
404 broadcast => undef, # not applicable for ipV6
405 loopback => Ones,
406 unspecified => Ones,
407 host => Ones,
408 );
409  
410 122 my $ff000000 = pack('L3N', 0xffffffff, 0xffffffff, 0xffffffff, 0xFF000000);
411 122 my $ffff0000 = pack('L3N', 0xffffffff, 0xffffffff, 0xffffffff, 0xFFFF0000);
412 122 my $ffffff00 = pack('L3N', 0xffffffff, 0xffffffff, 0xffffffff, 0xFFFFFF00);
413  
414 32938 ▶sub _obits ($$) {
Subroutine _obits: called CC=3 SCAR=11.0
415 32938 my ($lo, $hi) = @_;
416 32938 my $size = $hi - $lo + 1;
417  
418 # undef unless lo-hi is one aligned block; the byte is the ones above it
419 32938 ▶ return undef if $size & ($size - 1) || $lo % $size;
Branch: 1 branch
BranchTrueFalseTotal
if $size & $size - 1 or $lo % $size 32397 541 32938
Condition100%3/3
$size & $size - 1 or $lo % $size
l!l&&r!l&&!r
310971300541
MC/DC100%2/2
$size & $size - 1 or $lo % $size
$size & $size - 1 $lo % $size
Truth table100%3/3
A or B
A= $size & $size - 1B= $lo % $size
ABresult
1X1
011
000
420 541 return 256 - $size;
421 }
422  
423 59 ▶sub new_no($;$$) {
Subroutine new_no: called CC=1 SCAR=0.0
Pod documented
424 59 unshift @_, -1;
425 59 goto &_xnew;
426 }
427  
428 43659 ▶sub new($;$$) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
429 43659 unshift @_, 0;
430 43659 goto &_xnew;
431 }
432  
433 9 ▶sub new_from_aton($$) {
Subroutine new_from_aton: called CC=5 SCAR=16.4
Pod documented
434 9 my $proto = shift;
435 9 ▶ my $class = ref $proto || $proto || __PACKAGE__;
Condition40%2/5
ref $proto || $proto || __PACKAGE__
l!l
90
ref $proto or $proto
l!l&&r!l&&!r
090
MC/DC0%0/2
ref $proto || $proto || __PACKAGE__
ref $proto $proto
Truth table33%1/3
A or B
A= ref $protoB= $proto
ABresult
1X1
011
000
436 9 my $ip = shift;
437 9 ▶ return undef unless defined $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 1 8 9
438 8 my $addrlen = length($ip);
439 8 ▶ return undef unless $addrlen == $V4_PACKED_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
unless $addrlen == $NetAddr::IP::Lite::V4_PACKED_BYTES 2 6 8
440 6 my $self = {
441 addr => ipv4to6($ip),
442 mask => &Ones,
443 isv6 => 0,
444 };
445 6 return bless $self, $class;
446 }
447  
448 39 ▶sub new4($;$$) {
Subroutine new4: called CC=5 SCAR=16.1
Pod documented
449 39 my $ip = _xnew(0, @_);
450 39 ▶ return $ip unless defined $ip && $ip->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and $ip->{"isv6"} 24 15 39
Condition66%2/3
defined $ip and $ip->{"isv6"}
!ll&&!rl&&r
02415
MC/DC50%1/2
defined $ip and $ip->{"isv6"}
defined $ip $ip->{"isv6"}
Truth table66%2/3
A and B
A= defined $ipB= $ip->{"isv6"}
ABresult
0X0
100
111
451  
452 # an IPv4-mapped address with a prefix of 96 bits or more is that IPv4 address
453 return undef
454 unless ($ip->{addr} & $_v4mask) eq $_ipv4FFFF
455 15 ▶ && ($ip->{mask} & $_v4mask) eq $_v4mask;
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip->{"addr"} & $_v4mask) eq $_ipv4FFFF and ($ip->{"mask"} & $_v4mask) eq $_v4mask 10 5 15
Condition100%3/3
($ip->{"addr"} & $_v4mask) eq $_ipv4FFFF and ($ip->{"mask"} & $_v4mask) eq $_v4mask
!ll&&!rl&&r
915
MC/DC100%2/2
($ip->{"addr"} & $_v4mask) eq $_ipv4FFFF and ($ip->{"mask"} & $_v4mask) eq $_v4mask
($ip->{"addr"} & $_v4mask) eq $_ipv4FFFF ($ip->{"mask"} & $_v4mask) eq $_v4mask
Truth table100%3/3
A and B
A= ($ip->{"addr"} & $_v4mask) eq $_ipv4FFFFB= ($ip->{"mask"} & $_v4mask) eq $_v4mask
ABresult
0X0
100
111
456 5 $ip->{addr} ^= $_ipv4FFFF;
457 5 $ip->{isv6} = 0;
458 5 return $ip;
459 }
460  
461 293 ▶sub new6($;$$) {
Subroutine new6: called CC=1 SCAR=0.0
Pod documented
462 293 unshift @_, 1;
463 293 goto &_xnew;
464 }
465  
466 31 ▶sub new6FFFF($;$$) {
Subroutine new6FFFF: called CC=1 SCAR=0.0
Pod documented
467 31 unshift @_, 2;
468 31 goto &_xnew;
469 }
470  
471 134 ▶sub new_cis($;$$) {
Subroutine new_cis: called CC=3 SCAR=11.0
Pod documented
472 134 warnings::warnif(
473 'deprecated',
474 'new_cis is deprecated and will be removed in version 5; ->new() accepts the same notation'
475 );
476 134 my @in = @_;
477 134 ▶ if ($in[1] && $in[1] =~ m!^(.+)\s+(.+)$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in[1] and $in[1] =~ /^(.+)\s+(.+)$/) 126 8 134
Condition66%2/3
$in[1] and $in[1] =~ /^(.+)\s+(.+)$/
!ll&&!rl&&r
08126
MC/DC50%1/2
$in[1] and $in[1] =~ /^(.+)\s+(.+)$/
$in[1] $in[1] =~ /^(.+)\s+(.+)$/
Truth table66%2/3
A and B
A= $in[1]B= $in[1] =~ /^(.+)\s+(.+)$/
ABresult
0X0
100
111
478 126 $in[1] = $1 . '/' . $2;
479 }
480 134 @_ = (0, @in);
481 134 goto &_xnew;
482 }
483  
484 27 ▶sub new_cis6($;$$) {
Subroutine new_cis6: called CC=3 SCAR=11.0
Pod documented
485 27 warnings::warnif(
486 'deprecated',
487 'new_cis6 is deprecated and will be removed in version 5; ->new6() accepts the same notation'
488 );
489 27 my @in = @_;
490 27 ▶ if ($in[1] && $in[1] =~ m!^(.+)\s+(.+)$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in[1] and $in[1] =~ /^(.+)\s+(.+)$/) 18 9 27
Condition66%2/3
$in[1] and $in[1] =~ /^(.+)\s+(.+)$/
!ll&&!rl&&r
0918
MC/DC50%1/2
$in[1] and $in[1] =~ /^(.+)\s+(.+)$/
$in[1] $in[1] =~ /^(.+)\s+(.+)$/
Truth table66%2/3
A and B
A= $in[1]B= $in[1] =~ /^(.+)\s+(.+)$/
ABresult
0X0
100
111
491 18 $in[1] = $1 . '/' . $2;
492 }
493 27 @_ = (1, @in);
494 27 goto &_xnew;
495 }
496  
497 # true for a number written with a leading zero, such as a range bound or a negative number:
498 # new refuses it, as it does a leading zero in any part of an address, and new_no reads it as decimal
499 33000 ▶sub _leading_zero {
Subroutine _leading_zero: called CC=1 SCAR=0.0
500 33000 return $_[0] =~ /\A-?0[0-9]/;
501 }
502  
503 54 ▶sub _no_octal {
Subroutine _no_octal: called CC=1 SCAR=0.0
504 54 (my $rv = $_[0]) =~ s#\b0*([1-9][0-9]*/?|0/?)#$1#g; # suppress leading zeros
505 54 $rv;
506 }
507  
508 # bcd2bin croaks on input that is too long, has a non-digit, or is larger
509 # than 128 bits. The constructor returns undef for bad input, so map the
510 # croak to undef here.
511 #
512 # true when a 0b or 0x literal needs more than the 32 bits of an IPv4 address
513 27 ▶sub _wider_than_ipv4 {
Subroutine _wider_than_ipv4: called CC=2 SCAR=6.9
514 27 my ($literal) = @_;
515 27 my ($base, $digits) = $literal =~ /\A0([bx])0*([0-9a-f]*)\z/;
516 27 ▶ return length($digits) > ($base eq 'x' ? $IPV4_BITS / 4 : $IPV4_BITS);
Branch: 1 branch
BranchTrueFalseTotal
$base eq 'x' ? : 20 7 27
517 }
518  
519 504 ▶sub _bcd2bin_or_undef {
Subroutine _bcd2bin_or_undef: called CC=1 SCAR=0.0
520 504 local ($@, $SIG{__DIE__}); # keep the croak out of the caller's error state
521 504 return scalar eval { bcd2bin($_[0]) };
522 }
523  
524 # The first address of a family is the one used. All zeros is no answer:
525 # a resolver result of 0.0.0.0 or :: is discarded, as naip_gethostbyname
526 # discards it.
527 108 ▶sub _first_addr {
Subroutine _first_addr: called CC=5 SCAR=16.2
528 108 my ($addrs, $zero) = @_;
529 108 ▶ return unless $addrs && @$addrs;
Branch: 1 branch
BranchTrueFalseTotal
unless $addrs and @$addrs 12 96 108
Condition66%2/3
$addrs and @$addrs
!ll&&!rl&&r
01296
MC/DC50%1/2
$addrs and @$addrs
$addrs @$addrs
Truth table66%2/3
A and B
A= $addrsB= @$addrs
ABresult
0X0
100
111
530 96 my $addr = $addrs->[0];
531 96 ▶ return if !defined $addr || $addr eq $zero;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $addr or $addr eq $zero 2 94 96
Condition66%2/3
not defined $addr or $addr eq $zero
l!l&&r!l&&!r
0294
MC/DC50%1/2
not defined $addr or $addr eq $zero
not defined $addr $addr eq $zero
Truth table66%2/3
not(A) or B
A= not defined $addrB= $addr eq $zero
ABresult
1X1
011
000
532 94 return $addr;
533 }
534  
535 # One getaddrinfo, with no family. $want_v6 is the new6 constructor: a
536 # real AAAA answer wins, and an IPv4 answer is the ::a.b.c.d form. new
537 # uses an IPv4 answer when there is one. An AAAA result that is itself
538 # an IPv4 address is the mapped form, so new6 keeps the A answer while
539 # one exists. :noSock6 stays on gethostbyname. An AAAA answer comes
540 # with a true second value. An empty list means this text did not
541 # resolve.
542 86 ▶sub _resolve_host {
Subroutine _resolve_host: called CC=13 SCAR=25.7
543 86 my ($name, $want_v6) = @_;
544 86 my $host = fillIPv4($name);
545 86 ▶ return unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 86 86
546  
547 # text ending in a number or 0x hex label is a number to the resolver, as in _gethostbyname
548 86 ▶ return if NetAddr::IP::UtilPolluted::_numeric_text($host);
Branch: 1 branch
BranchTrueFalseTotal
if NetAddr::IP::UtilPolluted::_numeric_text($host) 8 78 86
549 78 ▶ unless (havegethostbyname2()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (havegethostbyname2()) 8 70 78
550 8 my $addr = gethostbyname($host);
551 8 ▶ return unless defined $addr && $addr ne $_v4zero && $addr ne $_zero;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr and $addr ne $_v4zero and $addr ne $_zero 5 3 8
Condition83%5/6
defined $addr and $addr ne $_v4zero and $addr ne $_zero
!ll&&!rl&&r
503
defined $addr and $addr ne $_v4zero
!ll&&!rl&&r
323
MC/DC66%2/3
defined $addr and $addr ne $_v4zero and $addr ne $_zero
defined $addr $addr ne $_v4zero $addr ne $_zero
Truth table75%3/4
A and B and C
A= defined $addrB= $addr ne $_v4zeroC= $addr ne $_zero
ABCresult
0XX0
10X0
1100
1111
552 3 return ipv4to6($addr);
553 }
554 70 my ($v4, $v6) = NetAddr::IP::UtilPolluted::_lookup_both($host);
555 70 ▶ return unless $v4;
Branch: 1 branch
BranchTrueFalseTotal
unless $v4 16 54 70
556 54 my $aaaa = _first_addr($v6, $_zero);
557 54 my $a = _first_addr($v4, $_v4zero);
558 54 ▶ if ($want_v6 && defined $aaaa && !isAnyIPv4($aaaa)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_v6 and defined $aaaa and not isAnyIPv4($aaaa)) 3 51 54
Condition100%6/6
$want_v6 and defined $aaaa and not isAnyIPv4($aaaa)
!ll&&!rl&&r
4743
$want_v6 and defined $aaaa
!ll&&!rl&&r
4527
MC/DC100%3/3
$want_v6 and defined $aaaa and not isAnyIPv4($aaaa)
$want_v6 defined $aaaa not isAnyIPv4($aaaa)
Truth table100%4/4
A and B and not(C)
A= $want_v6B= defined $aaaaC= not isAnyIPv4($aaaa)
ABCresult
0XX0
10X0
1100
1111
559 3 return ($aaaa, 1);
560 }
561 51 ▶ return ipv4to6($a) if defined $a;
Branch: 1 branch
BranchTrueFalseTotal
if defined $a 42 9 51
562 9 ▶ return ($aaaa, 1) if defined $aaaa;
Branch: 1 branch
BranchTrueFalseTotal
if defined $aaaa 7 2 9
563 2 return;
564 }
565  
566 44242 ▶sub _xnew($$;$$) {
Subroutine _xnew: called CC=188 SCAR=53.6
567 44242 my $noctal = 0;
568 44242 my $map_ipv4 = 0;
569  
570 # true once the address is read as IPv6: IPv6 text, a number over 32 bits,
571 # an IPv6 keyword, an AAAA answer or 16 packed bytes
572 44242 my $v6_input = 0;
573 44242 my $isV6 = shift;
574 44242 ▶ if ($isV6 < 0) { # flag for no octal?
Branch: 2 branches
BranchTrueFalseTotal
if ($isV6 < 0) { } 59 44183 44242
elsif ($isV6 > 1) { } 31 44152 44183
575 59 $isV6 = 0;
576 59 $noctal = 1;
577 }
578 elsif ($isV6 > 1) { # the new6FFFF flag: an address read as IPv4 becomes ::ffff:a.b.c.d
579 31 $isV6 = 1;
580 31 $map_ipv4 = 1;
581 }
582 44242 my $proto = shift;
583 44242 ▶ my $class = ref $proto || $proto || __PACKAGE__;
Condition40%2/5
ref $proto || $proto || __PACKAGE__
l!l
442420
ref $proto or $proto
l!l&&r!l&&!r
0442420
MC/DC0%0/2
ref $proto || $proto || __PACKAGE__
ref $proto $proto
Truth table33%1/3
A or B
A= ref $protoB= $proto
ABresult
1X1
011
000
584 44242 ▶ my $ip = @_ ? shift : 'default'; # no argument at all means default
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 44242 0 44242
585  
586 44242 ▶ if ($ip && $noctal && $ip !~ m|(?:[^\s0123456789/. -])|) { # octal suppression required if not an IPv4 address
Branch: 1 branch
BranchTrueFalseTotal
if ($ip and $noctal and not $ip =~ m[(?:[^\s0123456789/. -])]) 50 44192 44242
Condition100%6/6
$ip and $noctal and not $ip =~ m[(?:[^\s0123456789/. -])]
!ll&&!rl&&r
44183950
$ip and $noctal
!ll&&!rl&&r
164416759
MC/DC100%3/3
$ip and $noctal and not $ip =~ m[(?:[^\s0123456789/. -])]
$ip $noctal not $ip =~ m[(?:[^\s0123456789/. -])]
Truth table100%4/4
A and B and not(C)
A= $ipB= $noctalC= not $ip =~ m[(?:[^\s0123456789/. -])]
ABCresult
0XX0
10X0
1100
1111
587 50 $ip = _no_octal($ip);
588 }
589  
590 # fix for bug #75976, and an explicit undef is a missing value, not a route
591 44242 ▶ return undef unless defined $ip && $ip ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and $ip ne "" 6 44236 44242
Condition100%3/3
defined $ip and $ip ne ""
!ll&&!rl&&r
3344236
MC/DC100%2/2
defined $ip and $ip ne ""
defined $ip $ip ne ""
Truth table100%3/3
A and B
A= defined $ipB= $ip ne ""
ABresult
0X0
100
111
592 44236 ▶ $ip = _retMBIstring($ip) # treat as big bcd string
Branch: 1 branch
BranchTrueFalseTotal
if ref $ip and ref $ip eq "Math::BigInt" 71 44165 44236
Condition66%2/3
ref $ip and ref $ip eq "Math::BigInt"
!ll&&!rl&&r
44165071
MC/DC50%1/2
ref $ip and ref $ip eq "Math::BigInt"
ref $ip ref $ip eq "Math::BigInt"
Truth table66%2/3
A and B
A= ref $ipB= ref $ip eq "Math::BigInt"
ABresult
0X0
100
111
593 if ref $ip && ref $ip eq 'Math::BigInt'; # can /CIDR notation
594 44236 ▶ return undef unless defined $ip; # NaN and infinity are not addresses
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 6 44230 44236
595 44230 my $hasmask = 1;
596 44230 my ($mask, $tmp);
597  
598 # IP to lower case AFTER ref test for Math::BigInt. 'lc' strips blessing
599 # keep the untouched input for the :aton binary branch, lc corrupts bytes 0x41-0x5A
600 44230 my $raw = $ip;
601 44230 $ip = lc $ip;
602  
603 # strip surrounding whitespace so "10.0.0.1\n" and " 10.0.0.1" are treated alike.
604 # skipped under :aton for 4 or 16 bytes, a packed address that may begin or end with a whitespace byte
605 44230 ▶ unless ($Accept_Binary_IP
Branch: 1 branch
BranchTrueFalseTotal
unless ($NetAddr::IP::Lite::Accept_Binary_IP and length $raw == $NetAddr::IP::Lite::V4_PACKED_BYTES || length $raw == $NetAddr::IP::Lite::V6_PACKED_BYTES) 44194 36 44230
Condition100%6/6
$NetAddr::IP::Lite::Accept_Binary_IP and length $raw == $NetAddr::IP::Lite::V4_PACKED_BYTES || length $raw == $NetAddr::IP::Lite::V6_PACKED_BYTES
!ll&&!rl&&r
441821236
length $raw == $NetAddr::IP::Lite::V4_PACKED_BYTES or length $raw == $NetAddr::IP::Lite::V6_PACKED_BYTES
l!l&&r!l&&!r
29712
MC/DC100%3/3
$NetAddr::IP::Lite::Accept_Binary_IP and length $raw == $NetAddr::IP::Lite::V4_PACKED_BYTES || length $raw == $NetAddr::IP::Lite::V6_PACKED_BYTES
$NetAddr::IP::Lite::Accept_Binary_IP length $raw == $NetAddr::IP::Lite::V4_PACKED_BYTES length $raw == $NetAddr::IP::Lite::V6_PACKED_BYTES
Truth table100%4/4
A and B or C
A= $NetAddr::IP::Lite::Accept_Binary_IPB= length $raw == $NetAddr::IP::Lite::V4_PACKED_BYTESC= length $raw == $NetAddr::IP::Lite::V6_PACKED_BYTES
ABCresult
0XX0
1000
11X1
1011
606 && (length($raw) == $V4_PACKED_BYTES || length($raw) == $V6_PACKED_BYTES)) {
607 44194 $ip =~ s/^\s+//;
608 44194 $ip =~ s/\s+\z//;
609 44194 ▶ return undef if $ip eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $ip eq "" 1 44193 44194
610  
611 # reject non-ASCII digits (Arabic-Indic, Devanagari, Fullwidth, etc.)
612 44193 (my $ipcore = $ip) =~ s{/[^/]+$}{};
613 44193 ▶ return undef if $ipcore =~ /[^\x00-\x7F]/;
Branch: 1 branch
BranchTrueFalseTotal
if $ipcore =~ /[^\x00-\x7F]/ 10 44183 44193
614 }
615  
616 # a whole number with a leading zero is octal to inet_aton and decimal to people, so it is refused
617 44219 ▶ return undef if $ip =~ m{\A0[0-9]+(?:/|\s|\z)};
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ m[\A0[0-9]+(?:/|\s|\z)] 6 44213 44219
618  
619 44213 while (1) {
620  
621 # process IP's with no CIDR or that have the CIDR as part of the IP argument string
622 44213 ▶ unless (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 1385 42828 44213
if (do { defined $_[0] }) 1376 9 1385
623 42828 ▶ if ($ip !~ /[^0-9]/) { # binary number notation
Branch: 4 branches
BranchTrueFalseTotal
if (not $ip =~ /[^0-9]/) { } 89 42739 42828
elsif (not $ip =~ /\s-|-\s/ and $ip =~ m[^([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)$]) { } 7632 35107 42739
elsif ($ip =~ m(^\[([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)\]$) or $ip =~ m(^\[([a-z0-9.:-]+)\](?:/|\s+)([a-z0-9.:-]+)$)) { } 56 35051 35107
elsif (grep(($ip eq $_), ('default', 'any', 'broadcast', 'loopback', 'unspecified'))) { } 46 35005 35051
624 89 ▶ return undef unless defined($ip = _bcd2bin_or_undef($ip));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($ip = _bcd2bin_or_undef($ip)) 6 83 89
625 83 $mask = Ones;
626 83 ▶ $isV6 = $v6_input = 1 unless isIPv4($ip);
Branch: 1 branch
BranchTrueFalseTotal
unless isIPv4($ip) 55 28 83
627 83 last;
628 }
629  
630 # whitespace beside a dash means range notation, read further down
631 ▶ elsif ($ip !~ /\s-|-\s/
Condition100%3/3
not $ip =~ /\s-|-\s/ and $ip =~ m[^([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)$]
!ll&&!rl&&r
71350367632
MC/DC100%2/2
not $ip =~ /\s-|-\s/ and $ip =~ m[^([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)$]
not $ip =~ /\s-|-\s/ $ip =~ m[^([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)$]
Truth table100%3/3
not(A) and B
A= not $ip =~ /\s-|-\s/B= $ip =~ m[^([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)$]
ABresult
0X0
100
111
632 && $ip =~ m!^([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)$!) {
633 7632 $ip = $1;
634 7632 $mask = $2;
635 }
636 ▶ elsif ($ip =~ m!^\[([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)\]$!
Condition100%3/3
$ip =~ m(^\[([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)\]$) or $ip =~ m(^\[([a-z0-9.:-]+)\](?:/|\s+)([a-z0-9.:-]+)$)
l!l&&r!l&&!r
47935051
MC/DC100%2/2
$ip =~ m(^\[([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)\]$) or $ip =~ m(^\[([a-z0-9.:-]+)\](?:/|\s+)([a-z0-9.:-]+)$)
$ip =~ m(^\[([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)\]$) $ip =~ m(^\[([a-z0-9.:-]+)\](?:/|\s+)([a-z0-9.:-]+)$)
Truth table100%3/3
A or B
A= $ip =~ m(^\[([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)\]$)B= $ip =~ m(^\[([a-z0-9.:-]+)\](?:/|\s+)([a-z0-9.:-]+)$)
ABresult
1X1
011
000
637 || $ip =~ m!^\[([a-z0-9.:-]+)\](?:/|\s+)([a-z0-9.:-]+)$!) {
638 56 ▶ return undef if index($1, ':') < 0; # RFC 3986 brackets enclose an IPv6 literal only
Branch: 1 branch
BranchTrueFalseTotal
if index($1, ":") == -1 3 53 56
639 53 $ip = $1;
640 53 $mask = $2;
641 }
642 elsif (grep($ip eq $_, (qw(default any broadcast loopback unspecified)))) {
643 46 ▶ $isV6 = 1 if $ip eq 'unspecified';
Branch: 1 branch
BranchTrueFalseTotal
if $ip eq "unspecified" 2 44 46
644 46 ▶ if ($isV6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isV6) { } 7 39 46
645 7 $v6_input = 1;
646 7 $mask = $fip6m{$ip};
647 7 ▶ return undef unless defined($ip = $fip6{$ip});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($ip = $fip6{$ip}) 0 7 7
648 }
649 else {
650 39 $mask = $fip4m{$ip};
651 39 ▶ return undef unless defined($ip = $fip4{$ip});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($ip = $fip4{$ip}) 0 39 39
652 }
653 46 last;
654 }
655 }
656  
657 # process "ipv6" token and default IP's
658 0 elsif (defined $_[0]) {
659  
660 # brackets come off an IPv6 literal before the mask argument is read, as they do for /mask
661 1376 ▶ if ($ip =~ /\A\[([0-9a-f.:]+)\]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /\A\[([0-9a-f.:]+)\]\z/) 92 1284 1376
662 92 ▶ return undef if index($1, ':') < 0; # RFC 3986 brackets enclose an IPv6 literal only
Branch: 1 branch
BranchTrueFalseTotal
if index($1, ":") == -1 2 90 92
663 90 $ip = $1;
664 }
665 1374 ▶ if ($_[0] =~ /ipv6/i || $isV6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /ipv6/i or $isV6) 176 1198 1374
Condition100%3/3
$_[0] =~ /ipv6/i or $isV6
l!l&&r!l&&!r
101661198
MC/DC100%2/2
$_[0] =~ /ipv6/i or $isV6
$_[0] =~ /ipv6/i $isV6
Truth table100%3/3
A or B
A= $_[0] =~ /ipv6/iB= $isV6
ABresult
1X1
011
000
666 176 ▶ if (grep($ip eq $_, (qw(default any loopback unspecified)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($ip eq $_), ('default', 'any', 'loopback', 'unspecified'))) { } 30 146 176
667 30 ▶ $isV6 = 1 unless $isV6; # the ipv6 mask picks the IPv6 address of the keyword
Branch: 1 branch
BranchTrueFalseTotal
unless $isV6 7 23 30
668 30 ▶ if ($_[0] =~ /ipv6/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /ipv6/i) 9 21 30
669 9 $v6_input = 1;
670 9 $mask = $fip6m{$ip};
671 9 $ip = $fip6{$ip};
672 9 last;
673 }
674 }
675 else {
676 146 ▶ return undef unless $isV6;
Branch: 1 branch
BranchTrueFalseTotal
unless $isV6 1 145 146
677  
678 # add for ipv6 notation "12345, 1"
679 }
680 }
681  
682 # extract mask
683 1364 $mask = $_[0];
684  
685 # surrounding whitespace is stripped from a text mask as from the address
686 1364 ▶ unless (ref $mask) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $mask) 1321 43 1364
687 1321 $mask =~ s/^\s+//;
688 1321 $mask =~ s/\s+\z//;
689 }
690  
691 # an empty mask argument is a caller error, not a /0
692 1364 ▶ return undef if $mask eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $mask eq "" 3 1361 1364
693 }
694  
695 # under new6 a keyword is the text of its IPv6 address, so a mask in
696 # the string, a mask argument and an undef mask all apply to it
697 44060 ▶ $ip = ipv6_n2x($fip6{$ip})
Branch: 1 branch
BranchTrueFalseTotal
if $isV6 and grep(($ip eq $_), ("default", "any", "loopback", "unspecified")) 33 44027 44060
Condition100%3/3
$isV6 and grep(($ip eq $_), ("default", "any", "loopback", "unspecified"))
!ll&&!rl&&r
4372929833
MC/DC100%2/2
$isV6 and grep(($ip eq $_), ("default", "any", "loopback", "unspecified"))
$isV6 grep(($ip eq $_), ("default", "any", "loopback", "unspecified"))
Truth table100%3/3
A and B
A= $isV6B= grep(($ip eq $_), ("default", "any", "loopback", "unspecified"))
ABresult
0X0
100
111
698 if $isV6 && grep($ip eq $_, qw(default any loopback unspecified));
699 ###
700 ### process mask
701 44060 ▶ unless (defined $mask) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mask) 35014 9046 44060
702 35014 $hasmask = 0;
703 35014 $mask = 'host';
704 }
705  
706 # a whole-number mask above 128 is a raw mask, as numeric() returns
707 44060 ▶ my $raw_mask = $hasmask && "$mask" =~ /\A[0-9]+\z/ && "$mask" > $IPV6_BITS;
Condition100%6/6
$hasmask && "$mask" =~ /\A[0-9]+\z/ && "$mask" > $NetAddr::IP::Lite::IPV6_BITS
!ll&&!rl&&r
352018743116
$hasmask and "$mask" =~ /\A[0-9]+\z/
!ll&&!rl&&r
350141878859
MC/DC100%3/3
$hasmask && "$mask" =~ /\A[0-9]+\z/ && "$mask" > $NetAddr::IP::Lite::IPV6_BITS
$hasmask "$mask" =~ /\A[0-9]+\z/ "$mask" > $NetAddr::IP::Lite::IPV6_BITS
Truth table100%4/4
A and B && C
A= $hasmaskB= "$mask" =~ /\A[0-9]+\z/C= "$mask" > $NetAddr::IP::Lite::IPV6_BITS
ABCresult
0XX0
10X0
1100
1111
708  
709 # two kinds of IP's can turn on the isV6 flag
710 # 1) big digits that are over the IPv4 boundry
711 # 2) IPv6 IP syntax
712 #
713 # check these conditions and set isV6 as appropriate
714 #
715 44060 my $try;
716 44060 ▶ $isV6 = $v6_input = 1 if # check big bcd and IPv6 RFC 4291 s2.2
Branch: 1 branch
BranchTrueFalseTotal
if not $ip =~ /[^0-9]/ and length $ip > 3 || $ip > 255 and $try = _bcd2bin_or_undef($ip) and not isIPv4($try) or index($ip, ":") != -1 and $try = ipv6_aton($ip) 1758 42302 44060
Condition94%17/18
not $ip =~ /[^0-9]/ and length $ip > 3 || $ip > 255 and $try = _bcd2bin_or_undef($ip) and not isIPv4($try) or index($ip, ":") != -1 and $try = ipv6_aton($ip)
l!l&&r!l&&!r
39171942302
not $ip =~ /[^0-9]/ and length $ip > 3 || $ip > 255 and $try = _bcd2bin_or_undef($ip) and not isIPv4($try)
!ll&&!rl&&r
439388339
not $ip =~ /[^0-9]/ and length $ip > 3 || $ip > 255 and $try = _bcd2bin_or_undef($ip)
!ll&&!rl&&r
439380122
not $ip =~ /[^0-9]/ and length $ip > 3 || $ip > 255
!ll&&!rl&&r
4386177122
length $ip > 3 or $ip > 255
l!l&&r!l&&!r
120277
index($ip, ":") != -1 and $try = ipv6_aton($ip)
!ll&&!rl&&r
42257451719
MC/DC71%5/7
not $ip =~ /[^0-9]/ and length $ip > 3 || $ip > 255 and $try = _bcd2bin_or_undef($ip) and not isIPv4($try) or index($ip, ":") != -1 and $try = ipv6_aton($ip)
not $ip =~ /[^0-9]/ length $ip > 3 $ip > 255 $try = _bcd2bin_or_undef($ip) not isIPv4($try) index($ip, ":") != -1 $try = ipv6_aton($ip)
Truth table35%7/20
not(A) and B or C and D and not(E) or F and G
A= not $ip =~ /[^0-9]/B= length $ip > 3C= $ip > 255D= $try = _bcd2bin_or_undef($ip)E= not isIPv4($try)F= index($ip, ":") != -1G= $try = ipv6_aton($ip)
ABCDEFGresult
11X11XX1
10111XX1
0XXXX111
100XX111
11X0X111
1010X111
11X10111
10110111
0XXXX0X0
0XXXX100
100XX0X0
100XX100
11X0X0X0
11X0X100
1010X0X0
1010X100
11X100X0
11X10100
101100X0
10110100
717 (
718 $ip !~ /[^0-9]/ && # ip is all decimal
719 (length($ip) > 3 || $ip > 255)
720 && # exclude a single digit in the range of zero to 255, could be funny IPv4
721 ($try = _bcd2bin_or_undef($ip)) && !isIPv4($try))
722 || # precedence so $try is not corrupted
723 (index($ip, ':') >= 0 && ($try = ipv6_aton($ip))); # fails if not an RFC 4291 s2.2 address
724  
725 # if either of the above conditions is true, $try contains the NetAddr 128 bit address
726  
727 # checkfor Math::BigInt mask
728 44060 ▶ $mask = _retMBIstring($mask) # treat as big bcd string
Branch: 1 branch
BranchTrueFalseTotal
if ref $mask and ref $mask eq "Math::BigInt" 43 44017 44060
Condition66%2/3
ref $mask and ref $mask eq "Math::BigInt"
!ll&&!rl&&r
44017043
MC/DC50%1/2
ref $mask and ref $mask eq "Math::BigInt"
ref $mask ref $mask eq "Math::BigInt"
Truth table66%2/3
A and B
A= ref $maskB= ref $mask eq "Math::BigInt"
ABresult
0X0
100
111
729 if ref $mask && ref $mask eq 'Math::BigInt';
730 44060 ▶ return undef unless defined $mask; # NaN and infinity are not masks
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mask 2 44058 44060
731  
732 # MASK to lower case AFTER ref test for Math::BigInt, 'lc' strips blessing
733 44058 $mask = lc $mask;
734  
735 44058 ▶ if ($mask !~ /[^0-9]/) { # bcd or CIDR notation
Branch: 3 branches
BranchTrueFalseTotal
if (not $mask =~ /[^0-9]/) { } 8859 35199 44058
elsif ($mask =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) { } 96 35103 35199
elsif (grep(($mask eq $_), ('default', 'any', 'broadcast', 'loopback', 'unspecified', 'host'))) { } 35048 55 35103
736 8859 ▶ my $isCIDR = length($mask) < 4 && $mask <= $IPV6_BITS;
Condition100%3/3
length $mask < 4 && $mask <= $NetAddr::IP::Lite::IPV6_BITS
!ll&&!rl&&r
10798743
MC/DC100%2/2
length $mask < 4 && $mask <= $NetAddr::IP::Lite::IPV6_BITS
length $mask < 4 $mask <= $NetAddr::IP::Lite::IPV6_BITS
Truth table100%3/3
A && B
A= length $mask < 4B= $mask <= $NetAddr::IP::Lite::IPV6_BITS
ABresult
0X0
100
111
737 8859 ▶ if ($isV6) {
Branch: 2 branches
BranchTrueFalseTotal
if ($isV6) { } 1248 7611 8859
elsif ($isCIDR and $mask < 33) { } 7538 73 7611
738 1248 ▶ if ($isCIDR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isCIDR) { } 1199 49 1248
739 1199 my ($dq1, $dq2, $dq3, $dq4);
740  
741 # dotted text keeps an IPv4 prefix length; a whole number is 128 bits
742 1199 ▶ if ( index($ip, '.') >= 0
Branch: 2 branches
BranchTrueFalseTotal
if (index($ip, '.') != -1 and $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 } and $dq1 >= 0 and $dq1 < 256 and $dq2 >= 0 and $dq2 < 256 and $dq3 >= 0 and $dq3 < 256 and $dq4 >= 0 and $dq4 < 256) { } 9 1190 1199
elsif ($mask < $NetAddr::IP::Lite::IPV6_BITS) { } 1101 89 1190
Condition70%21/30
index($ip, ".") != -1 and $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 } and $dq1 >= 0 and $dq1 < 256 and $dq2 >= 0 and $dq2 < 256 and $dq3 >= 0 and $dq3 < 256 and $dq4 >= 0 and $dq4 < 256
!ll&&!rl&&r
119009
index($ip, ".") != -1 and $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 } and $dq1 >= 0 and $dq1 < 256 and $dq2 >= 0 and $dq2 < 256 and $dq3 >= 0 and $dq3 < 256 and $dq4 >= 0
!ll&&!rl&&r
119009
index($ip, ".") != -1 and $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 } and $dq1 >= 0 and $dq1 < 256 and $dq2 >= 0 and $dq2 < 256 and $dq3 >= 0 and $dq3 < 256
!ll&&!rl&&r
119009
index($ip, ".") != -1 and $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 } and $dq1 >= 0 and $dq1 < 256 and $dq2 >= 0 and $dq2 < 256 and $dq3 >= 0
!ll&&!rl&&r
119009
index($ip, ".") != -1 and $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 } and $dq1 >= 0 and $dq1 < 256 and $dq2 >= 0 and $dq2 < 256
!ll&&!rl&&r
119009
index($ip, ".") != -1 and $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 } and $dq1 >= 0 and $dq1 < 256 and $dq2 >= 0
!ll&&!rl&&r
119009
index($ip, ".") != -1 and $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 } and $dq1 >= 0 and $dq1 < 256
!ll&&!rl&&r
119009
index($ip, ".") != -1 and $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 } and $dq1 >= 0
!ll&&!rl&&r
119009
index($ip, ".") != -1 and $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 }
!ll&&!rl&&r
119009
index($ip, ".") != -1 and $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/
!ll&&!rl&&r
1117739
MC/DC18%2/11
index($ip, ".") != -1 and $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 } and $dq1 >= 0 and $dq1 < 256 and $dq2 >= 0 and $dq2 < 256 and $dq3 >= 0 and $dq3 < 256 and $dq4 >= 0 and $dq4 < 256
index($ip, ".") != -1 $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/ do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 } $dq1 >= 0 $dq1 < 256 $dq2 >= 0 $dq2 < 256 $dq3 >= 0 $dq3 < 256 $dq4 >= 0 $dq4 < 256
Truth table25%3/12
A and B and C and D and E and F and G and H and I and J and K
A= index($ip, ".") != -1B= $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/C= do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 }D= $dq1 >= 0E= $dq1 < 256F= $dq2 >= 0G= $dq2 < 256H= $dq3 >= 0I= $dq3 < 256J= $dq4 >= 0K= $dq4 < 256
ABCDEFGHIJKresult
0XXXXXXXXXX0
10XXXXXXXXX0
110XXXXXXXX0
1110XXXXXXX0
11110XXXXXX0
111110XXXXX0
1111110XXXX0
11111110XXX0
111111110XX0
1111111110X0
111111111100
111111111111
743 && $ip =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))$/
744 && do {
745 9 $dq1 = $1;
746 9 ▶ $dq2 = $2 || 0;
Condition100%2/2
$2 || 0
l!l
18
MC/DC100%1/1
$2 || 0
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
747 9 ▶ $dq3 = $3 || 0;
Condition100%2/2
$3 || 0
l!l
54
MC/DC100%1/1
$3 || 0
$3
Truth table100%2/2
A
A= $3
Aresult
11
00
748 9 ▶ $dq4 = $4 || 0;
Condition100%2/2
$4 || 0
l!l
36
MC/DC100%1/1
$4 || 0
$4
Truth table100%2/2
A
A= $4
Aresult
11
00
749 9 1;
750 }
751 && $dq1 >= 0
752 && $dq1 < 256
753 && $dq2 >= 0
754 && $dq2 < 256
755 && $dq3 >= 0
756 && $dq3 < 256
757 && $dq4 >= 0
758 && $dq4 < 256
759 ) { # corner condition of IPv4 with isV6
760 9 $ip = join('.', $dq1, $dq2, $dq3, $dq4);
761 9 my $packed = inet_aton($ip);
762 9 ▶ return undef unless defined $packed; # inet_aton refuses a part with a leading zero
Branch: 1 branch
BranchTrueFalseTotal
unless defined $packed 1 8 9
763 8 $try = ipv4to6($packed);
764 8 ▶ if ($mask < $IPV4_BITS) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mask < $NetAddr::IP::Lite::IPV4_BITS) { } 8 0 8
elsif ($mask == $NetAddr::IP::Lite::IPV4_BITS) { } 0 0 0
765 8 $mask = shiftleft(Ones, $IPV4_BITS - $mask);
766 }
767 elsif ($mask == $IPV4_BITS) {
768 0 $mask = Ones;
769 }
770 else {
771 0 return undef; # undoubtably an error
772 }
773 }
774 elsif ($mask < $IPV6_BITS) {
775 1101 $mask = shiftleft(Ones, $IPV6_BITS - $mask); # small cidr
776 }
777 else {
778 89 $mask = Ones();
779 }
780 }
781 else {
782 49 ▶ return undef unless defined($mask = _bcd2bin_or_undef($mask));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($mask = _bcd2bin_or_undef($mask)) 0 49 49
783 }
784 }
785 ▶ elsif ($isCIDR && $mask < 33) { # is V4
Condition100%3/3
$isCIDR and $mask < 33
!ll&&!rl&&r
6767538
MC/DC100%2/2
$isCIDR and $mask < 33
$isCIDR $mask < 33
Truth table100%3/3
A and B
A= $isCIDRB= $mask < 33
ABresult
0X0
100
111
786 # if ($ip && $noctal && $ip !~ m|(?:[^\s0123456789.])|) { # octal suppression required if not an IPv4 address
787 # $mask = _no_octal($mask);
788 # }
789 7538 ▶ if ($mask < $IPV4_BITS) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mask < $NetAddr::IP::Lite::IPV4_BITS) { } 7148 390 7538
elsif ($mask == $NetAddr::IP::Lite::IPV4_BITS) { } 390 0 390
790 7148 $mask = shiftleft(Ones, $IPV4_BITS - $mask);
791 }
792 elsif ($mask == $IPV4_BITS) {
793 390 $mask = Ones;
794 }
795 else {
796 0 ▶ return undef unless defined($mask = _bcd2bin_or_undef($mask));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($mask = _bcd2bin_or_undef($mask)) 0 0 0
797 0 $mask |= $_v4mask; # v4 always
798 }
799 }
800 else { # also V4
801 73 ▶ return undef unless defined($mask = _bcd2bin_or_undef($mask));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($mask = _bcd2bin_or_undef($mask)) 1 72 73
802  
803 # a 32-bit mask gains the 96 ones above it; a wider one must already have them
804 72 ▶ $mask |= $_v4mask unless hasbits($mask & $_v4mask);
Branch: 1 branch
BranchTrueFalseTotal
unless &hasbits($mask & $_v4mask) 63 9 72
805 }
806 8857 ▶ if ($try) { # is a big number
Branch: 1 branch
BranchTrueFalseTotal
if ($try) 1276 7581 8857
807 1276 $ip = $try;
808 1276 last;
809 }
810 }
811 elsif ($mask =~ m/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) { # ipv4 form of mask
812 96 ▶ $mask = _no_octal($mask) if $noctal; # filter for octal
Branch: 1 branch
BranchTrueFalseTotal
if $noctal 0 96 96
813 96 ▶ return undef unless defined($mask = inet_aton($mask));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($mask = inet_aton($mask)) 1 95 96
814 95 $mask = mask4to6($mask);
815 }
816 elsif (grep($mask eq $_, qw(default any broadcast loopback unspecified host))) {
817 35048 ▶ if (index($ip, ':') < 0 && !$isV6) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($ip, ':') == -1 and not $isV6) { } 34410 638 35048
Condition100%3/3
index($ip, ":") == -1 and not $isV6
!ll&&!rl&&r
5716734410
MC/DC100%2/2
index($ip, ":") == -1 and not $isV6
index($ip, ":") == -1 not $isV6
Truth table100%3/3
A and not(B)
A= index($ip, ":") == -1B= not $isV6
ABresult
0X0
100
111
818 34410 ▶ return undef unless defined($mask = $fip4m{$mask});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($mask = $fip4m{$mask}) 0 34410 34410
819 }
820 else {
821 638 ▶ return undef unless defined($mask = $fip6m{$mask});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($mask = $fip6m{$mask}) 0 638 638
822 }
823 }
824 else {
825 55 ▶ return undef unless defined($mask = ipv6_aton($mask)); # try ipv6 form of mask
Branch: 1 branch
BranchTrueFalseTotal
unless defined($mask = ipv6_aton($mask)) 24 31 55
826 }
827  
828 # process remaining IP's
829 42755 ▶ if (index($ip, ':') < 0) { # ipv4 address
Branch: 1 branch
BranchTrueFalseTotal
if (index($ip, ':') == -1) { } 42161 594 42755
830 42161 ▶ if ($ip =~ m/^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/) {
Branch: 19 branches
BranchTrueFalseTotal
if ($ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/) { } 8721 33440 42161
elsif (grep(($ip eq $_), ('default', 'any', 'broadcast', 'loopback'))) { } 65 33375 33440
elsif ($ip =~ /^([0-9]+)\.([0-9]+)$/) { } 15 33360 33375
elsif ($ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)$/) { } 7 33353 33360
elsif ($ip =~ /^([0-9]+)$/ and $hasmask and not $isV6 and not $raw_mask and $1 >= 0 and $1 < 256) { } 41 33312 33353
elsif ($ip =~ /^[0-9]+$/) { } 52 33260 33312
elsif ($ip =~ /^(?:0b[01]+|0x[0-9a-f]+)$/ and $hasmask and not $isV6 and not $raw_mask and not _wider_than_ipv4($ip) and ($tmp = oct $ip) < 256) { } 2 33258 33260
elsif ($ip =~ /^-?[0-9]+$/) { } 68 33190 33258
elsif ($ip =~ /^(-?)(0b[01]+|0x[0-9a-f]+)$/) { } 23 33167 33190
elsif ($ip =~ /^([0-9]+)\.$/) { } 3 33164 33167
elsif ($ip =~ /^([0-9]+)\.([0-9]+)-([0-9]+)\.?$/ and $2 <= $3 and $3 < 256) { } 5 33159 33164
elsif ($ip =~ /^([0-9]+)-([0-9]+)\.?$/ and ($1 <= $2 and $2 < 256)) { } 15 33144 33159
elsif ($ip =~ /^([0-9]+)\.([0-9]+)\.$/) { } 2 33142 33144
elsif ($ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)\.?$/ and $3 <= $4 and $4 < 256) { } 9 33133 33142
elsif ($ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.$/) { } 3 33130 33133
elsif ($ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)$/ and $4 <= $5 and $5 < 256) { } 32925 205 33130
elsif ($ip =~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+) \s*-\s*([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/x) { } 79 126 205
elsif (not $NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/ and my(@answer) = _resolve_host($ip, $isV6)) { } 61 65 126
elsif ($NetAddr::IP::Lite::Accept_Binary_IP and not $hasmask) { } 27 38 65
831 ; # the common case
832 }
833 elsif (grep($ip eq $_, (qw(default any broadcast loopback)))) {
834 65 ▶ return undef unless defined($ip = $fip4{$ip});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($ip = $fip4{$ip}) 0 65 65
835 65 last;
836 }
837 elsif ($ip =~ m/^([0-9]+)\.([0-9]+)$/) {
838 15 ▶ $ip =
Branch: 1 branch
BranchTrueFalseTotal
$hasmask ? : 3 12 15
839 ($hasmask)
840 ? "${1}.${2}.0.0"
841 : "${1}.0.0.${2}";
842 }
843 elsif ($ip =~ m/^([0-9]+)\.([0-9]+)\.([0-9]+)$/) {
844 7 ▶ $ip =
Branch: 1 branch
BranchTrueFalseTotal
$hasmask ? : 5 2 7
845 ($hasmask)
846 ? "${1}.${2}.${3}.0"
847 : "${1}.${2}.0.${3}";
848 }
849  
850 # with a prefix length a small number is the first octet, except under new6
851 # or with a raw mask, which keep it a number
852 ▶ elsif ($ip =~ /^([0-9]+)$/ && $hasmask && !$isV6 && !$raw_mask && $1 >= 0 and $1 < 256) {
Condition86%13/15
$ip =~ /^([0-9]+)$/ and $hasmask and not $isV6 and not $raw_mask and $1 >= 0 and $1 < 256
!ll&&!rl&&r
332961641
$ip =~ /^([0-9]+)$/ and $hasmask and not $isV6 and not $raw_mask and $1 >= 0
!ll&&!rl&&r
33296057
$ip =~ /^([0-9]+)$/ and $hasmask and not $isV6 and not $raw_mask
!ll&&!rl&&r
332821457
$ip =~ /^([0-9]+)$/ and $hasmask and not $isV6
!ll&&!rl&&r
332602271
$ip =~ /^([0-9]+)$/ and $hasmask
!ll&&!rl&&r
33260093
MC/DC66%4/6
$ip =~ /^([0-9]+)$/ and $hasmask and not $isV6 and not $raw_mask and $1 >= 0 and $1 < 256
$ip =~ /^([0-9]+)$/ $hasmask not $isV6 not $raw_mask $1 >= 0 $1 < 256
Truth table71%5/7
A and B and not(C) and not(D) and E and F
A= $ip =~ /^([0-9]+)$/B= $hasmaskC= not $isV6D= not $raw_maskE= $1 >= 0F= $1 < 256
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
853 41 $ip = sprintf("%d.0.0.0", $1);
854 }
855  
856 # elsif ($ip =~ /^[0-9]+$/ && !$hasmask) { # a big integer
857 elsif ($ip =~ /^[0-9]+$/) { # a big integer
858 52 ▶ return undef unless defined($ip = _bcd2bin_or_undef($ip));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($ip = _bcd2bin_or_undef($ip)) 0 52 52
859 52 last;
860 }
861  
862 # binary and hex literals, 0b[01]+ or 0x[0-9a-f]+ ($ip is already lower case)
863 ▶ elsif ($ip =~ /^(?:0b[01]+|0x[0-9a-f]+)$/
Condition93%14/15
$ip =~ /^(?:0b[01]+|0x[0-9a-f]+)$/ and $hasmask and not $isV6 and not $raw_mask and not _wider_than_ipv4($ip) and ($tmp = oct $ip) < 256
!ll&&!rl&&r
3325802
$ip =~ /^(?:0b[01]+|0x[0-9a-f]+)$/ and $hasmask and not $isV6 and not $raw_mask and not _wider_than_ipv4($ip)
!ll&&!rl&&r
3325622
$ip =~ /^(?:0b[01]+|0x[0-9a-f]+)$/ and $hasmask and not $isV6 and not $raw_mask
!ll&&!rl&&r
3325424
$ip =~ /^(?:0b[01]+|0x[0-9a-f]+)$/ and $hasmask and not $isV6
!ll&&!rl&&r
3325226
$ip =~ /^(?:0b[01]+|0x[0-9a-f]+)$/ and $hasmask
!ll&&!rl&&r
33238148
MC/DC83%5/6
$ip =~ /^(?:0b[01]+|0x[0-9a-f]+)$/ and $hasmask and not $isV6 and not $raw_mask and not _wider_than_ipv4($ip) and ($tmp = oct $ip) < 256
$ip =~ /^(?:0b[01]+|0x[0-9a-f]+)$/ $hasmask not $isV6 not $raw_mask not _wider_than_ipv4($ip) ($tmp = oct $ip) < 256
Truth table85%6/7
A and B and not(C) and not(D) and not(E) and F
A= $ip =~ /^(?:0b[01]+|0x[0-9a-f]+)$/B= $hasmaskC= not $isV6D= not $raw_maskE= not _wider_than_ipv4($ip)F= ($tmp = oct $ip) < 256
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
864 && $hasmask
865 && !$isV6
866 && !$raw_mask
867 && !_wider_than_ipv4($ip)
868 && ($tmp = oct($ip)) < 256) {
869 2 $ip = sprintf("%d.0.0.0", $tmp);
870 }
871 elsif ($ip =~ /^-?[0-9]+$/) {
872  
873 # a negative number is a 32-bit two's complement, so new6 refuses it
874 68 ▶ return undef if $isV6 || (!$noctal && _leading_zero($ip));
Branch: 1 branch
BranchTrueFalseTotal
if $isV6 or not $noctal and _leading_zero($ip) 16 52 68
Condition100%6/6
$isV6 or not $noctal and _leading_zero($ip)
l!l&&r!l&&!r
13352
not $noctal and _leading_zero($ip)
!ll&&!rl&&r
5473
MC/DC100%3/3
$isV6 or not $noctal and _leading_zero($ip)
$isV6 not $noctal _leading_zero($ip)
Truth table100%4/4
A or not(B) and C
A= $isV6B= not $noctalC= _leading_zero($ip)
ABCresult
1XX1
0111
00X0
0100
875  
876 # negative values in -1 .. -(2**32) are accepted as 2's complement
877 52 $ip += 2**$IPV4_BITS;
878 52 ▶ return undef if $ip < 0; # would wrap in pack
Branch: 1 branch
BranchTrueFalseTotal
if $ip < 0 5 47 52
879 47 $ip = pack('L3N', 0, 0, 0, $ip);
880 47 last;
881 }
882 elsif ($ip =~ /^(-?)(0b[01]+|0x[0-9a-f]+)$/) {
883  
884 # more than 32 bits is not an IPv4 address, and oct would warn
885 23 ▶ return undef if _wider_than_ipv4($2) || ($1 && $isV6);
Branch: 1 branch
BranchTrueFalseTotal
if _wider_than_ipv4($2) or $1 and $isV6 8 15 23
Condition100%6/6
_wider_than_ipv4($2) or $1 and $isV6
l!l&&r!l&&!r
7115
$1 and $isV6
!ll&&!rl&&r
1411
MC/DC100%3/3
_wider_than_ipv4($2) or $1 and $isV6
_wider_than_ipv4($2) $1 $isV6
Truth table100%4/4
A or B and C
A= _wider_than_ipv4($2)B= $1C= $isV6
ABCresult
1XX1
0111
00X0
0100
886 15 $ip = oct($2);
887 15 ▶ $ip = 2**$IPV4_BITS - $ip if $1 && $ip;
Branch: 1 branch
BranchTrueFalseTotal
if $1 and $ip 1 14 15
Condition66%2/3
$1 and $ip
!ll&&!rl&&r
1401
MC/DC50%1/2
$1 and $ip
$1 $ip
Truth table66%2/3
A and B
A= $1B= $ip
ABresult
0X0
100
111
888 15 $ip = pack('L3N', 0, 0, 0, $ip);
889 15 last;
890 }
891  
892 # notations below include an implicit mask specification
893  
894 elsif ($ip =~ m/^([0-9]+)\.$/) {
895 3 $ip = "${1}.0.0.0";
896 3 $mask = $ff000000;
897 }
898 ▶ elsif ($ip =~ m/^([0-9]+)\.([0-9]+)-([0-9]+)\.?$/ && $2 <= $3 && $3 < 256) {
Condition66%4/6
$ip =~ /^([0-9]+)\.([0-9]+)-([0-9]+)\.?$/ and $2 <= $3 and $3 < 256
!ll&&!rl&&r
3315905
$ip =~ /^([0-9]+)\.([0-9]+)-([0-9]+)\.?$/ and $2 <= $3
!ll&&!rl&&r
3315905
MC/DC33%1/3
$ip =~ /^([0-9]+)\.([0-9]+)-([0-9]+)\.?$/ and $2 <= $3 and $3 < 256
$ip =~ /^([0-9]+)\.([0-9]+)-([0-9]+)\.?$/ $2 <= $3 $3 < 256
Truth table50%2/4
A and B and C
A= $ip =~ /^([0-9]+)\.([0-9]+)-([0-9]+)\.?$/B= $2 <= $3C= $3 < 256
ABCresult
0XX0
10X0
1100
1111
899 5 ▶ return undef if !$noctal && _leading_zero($3);
Branch: 1 branch
BranchTrueFalseTotal
if not $noctal and _leading_zero($3) 2 3 5
Condition66%2/3
not $noctal and _leading_zero($3)
!ll&&!rl&&r
032
MC/DC50%1/2
not $noctal and _leading_zero($3)
not $noctal _leading_zero($3)
Truth table66%2/3
not(A) and B
A= not $noctalB= _leading_zero($3)
ABresult
0X0
100
111
900 3 ▶ return undef unless defined($tmp = _obits($2, $3));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($tmp = &_obits($2, $3)) 1 2 3
901 2 $ip = "${1}.${2}.0.0";
902 2 $mask = pack('L3C4', 0xffffffff, 0xffffffff, 0xffffffff, 255, $tmp, 0, 0);
903 }
904 ▶ elsif ($ip =~ m/^([0-9]+)-([0-9]+)\.?$/ and $1 <= $2 && $2 < 256) {
Condition83%5/6
$ip =~ /^([0-9]+)-([0-9]+)\.?$/ and ($1 <= $2 and $2 < 256)
!ll&&!rl&&r
33143115
$1 <= $2 and $2 < 256
!ll&&!rl&&r
1015
MC/DC66%2/3
$ip =~ /^([0-9]+)-([0-9]+)\.?$/ and ($1 <= $2 and $2 < 256)
$ip =~ /^([0-9]+)-([0-9]+)\.?$/ $1 <= $2 $2 < 256
Truth table75%3/4
A and B and C
A= $ip =~ /^([0-9]+)-([0-9]+)\.?$/B= $1 <= $2C= $2 < 256
ABCresult
0XX0
10X0
1100
1111
905 15 ▶ return undef if !$noctal && _leading_zero($2);
Branch: 1 branch
BranchTrueFalseTotal
if not $noctal and _leading_zero($2) 2 13 15
Condition100%3/3
not $noctal and _leading_zero($2)
!ll&&!rl&&r
1122
MC/DC100%2/2
not $noctal and _leading_zero($2)
not $noctal _leading_zero($2)
Truth table100%3/3
not(A) and B
A= not $noctalB= _leading_zero($2)
ABresult
0X0
100
111
906 13 ▶ return undef unless defined($tmp = _obits($1, $2));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($tmp = &_obits($1, $2)) 1 12 13
907 12 $ip = "${1}.0.0.0";
908 12 $mask = pack('L3C4', 0xffffffff, 0xffffffff, 0xffffffff, $tmp, 0, 0, 0);
909 }
910 elsif ($ip =~ m/^([0-9]+)\.([0-9]+)\.$/) {
911 2 $ip = "${1}.${2}.0.0";
912 2 $mask = $ffff0000;
913 }
914 ▶ elsif ($ip =~ m/^([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)\.?$/ && $3 <= $4 && $4 < 256) {
Condition66%4/6
$ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)\.?$/ and $3 <= $4 and $4 < 256
!ll&&!rl&&r
3313309
$ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)\.?$/ and $3 <= $4
!ll&&!rl&&r
3313309
MC/DC33%1/3
$ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)\.?$/ and $3 <= $4 and $4 < 256
$ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)\.?$/ $3 <= $4 $4 < 256
Truth table50%2/4
A and B and C
A= $ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)\.?$/B= $3 <= $4C= $4 < 256
ABCresult
0XX0
10X0
1100
1111
915 9 ▶ return undef if !$noctal && _leading_zero($4);
Branch: 1 branch
BranchTrueFalseTotal
if not $noctal and _leading_zero($4) 4 5 9
Condition66%2/3
not $noctal and _leading_zero($4)
!ll&&!rl&&r
054
MC/DC50%1/2
not $noctal and _leading_zero($4)
not $noctal _leading_zero($4)
Truth table66%2/3
not(A) and B
A= not $noctalB= _leading_zero($4)
ABresult
0X0
100
111
916 5 ▶ return undef unless defined($tmp = _obits($3, $4));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($tmp = &_obits($3, $4)) 1 4 5
917 4 $ip = "${1}.${2}.${3}.0";
918 4 $mask = pack('L3C4', 0xffffffff, 0xffffffff, 0xffffffff, 255, 255, $tmp, 0);
919 }
920 elsif ($ip =~ m/^([0-9]+)\.([0-9]+)\.([0-9]+)\.$/) {
921 3 $ip = "${1}.${2}.${3}.0";
922 3 $mask = $ffffff00;
923 }
924 ▶ elsif ($ip =~ m/^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)$/ && $4 <= $5 && $5 < 256) {
Condition66%4/6
$ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)$/ and $4 <= $5 and $5 < 256
!ll&&!rl&&r
205032925
$ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)$/ and $4 <= $5
!ll&&!rl&&r
205032925
MC/DC33%1/3
$ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)$/ and $4 <= $5 and $5 < 256
$ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)$/ $4 <= $5 $5 < 256
Truth table50%2/4
A and B and C
A= $ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)$/B= $4 <= $5C= $5 < 256
ABCresult
0XX0
10X0
1100
1111
925 32925 ▶ return undef if !$noctal && _leading_zero($5);
Branch: 1 branch
BranchTrueFalseTotal
if not $noctal and _leading_zero($5) 8 32917 32925
Condition100%3/3
not $noctal and _leading_zero($5)
!ll&&!rl&&r
3329148
MC/DC100%2/2
not $noctal and _leading_zero($5)
not $noctal _leading_zero($5)
Truth table100%3/3
not(A) and B
A= not $noctalB= _leading_zero($5)
ABresult
0X0
100
111
926 32917 ▶ return undef unless defined($tmp = _obits($4, $5));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($tmp = &_obits($4, $5)) 32394 523 32917
927 523 $ip = "${1}.${2}.${3}.${4}";
928 523 $mask = pack('L3C4', 0xffffffff, 0xffffffff, 0xffffffff, 255, 255, 255, $tmp);
929 }
930 elsif (
931 $ip =~ m/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)
932 \s*-\s*([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/x
933 ) {
934 # if ($noctal) {
935 # return undef unless ($ip = inet_aton(_no_octal($1)));
936 # return undef unless ($tmp = inet_aton(_no_octal($2)));
937 # } else {
938 79 ▶ return undef unless ($ip = inet_aton($1));
Branch: 1 branch
BranchTrueFalseTotal
unless $ip = inet_aton($1) 0 79 79
939 79 ▶ return undef unless ($tmp = inet_aton($2));
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp = inet_aton($2) 0 79 79
940  
941 # }
942 # check for left side greater than right side
943 # save numeric difference in $mask
944 79 ▶ return undef if ($tmp = unpack('N', $tmp) - unpack('N', $ip)) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp = unpack("N", $tmp) - unpack("N", $ip)) < 0 1 78 79
945 78 $ip = ipv4to6($ip);
946 78 $tmp = pack('L3N', 0, 0, 0, $tmp);
947 78 $mask = ~$tmp;
948 78 ▶ return undef if notcontiguous($mask);
Branch: 1 branch
BranchTrueFalseTotal
if &notcontiguous($mask) 62 16 78
949  
950 # check for non-aligned left side
951 16 ▶ return undef if hasbits($ip & $tmp);
Branch: 1 branch
BranchTrueFalseTotal
if &hasbits($ip & $tmp) 0 16 16
952 16 last;
953 }
954  
955 # One lookup. A failed name falls through, so :aton can still
956 # read packed bytes that did not resolve.
957 ▶ elsif (!$NoFQDN
Condition100%6/6
not $NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/ and my(@answer) = _resolve_host($ip, $isV6)
!ll&&!rl&&r
343161
$NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/
l!l&&r!l&&!r
62892
MC/DC100%3/3
not $NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/ and my(@answer) = _resolve_host($ip, $isV6)
$NetAddr::IP::Lite::NoFQDN $ip =~ /[^a-zA-Z0-9\._-]/ my(@answer) = _resolve_host($ip, $isV6)
Truth table100%4/4
not(A || B) and C
A= $NetAddr::IP::Lite::NoFQDNB= $ip =~ /[^a-zA-Z0-9\._-]/C= my(@answer) = _resolve_host($ip, $isV6)
ABCresult
1XX0
01X0
0000
0011
958 && $ip !~ /[^a-zA-Z0-9\._-]/
959 && (my @answer = _resolve_host($ip, $isV6))) {
960 61 ($ip, my $from_aaaa) = @answer;
961  
962 # an AAAA answer stays IPv6 even inside ::/96; an A answer keeps the caller's family
963 61 ▶ $isV6 = $v6_input = 1 if $from_aaaa;
Branch: 1 branch
BranchTrueFalseTotal
if $from_aaaa 13 48 61
964 61 last;
965 }
966 ▶ elsif ($Accept_Binary_IP && !$hasmask) {
Condition66%2/3
$NetAddr::IP::Lite::Accept_Binary_IP and not $hasmask
!ll&&!rl&&r
38027
MC/DC50%1/2
$NetAddr::IP::Lite::Accept_Binary_IP and not $hasmask
$NetAddr::IP::Lite::Accept_Binary_IP not $hasmask
Truth table66%2/3
A and not(B)
A= $NetAddr::IP::Lite::Accept_Binary_IPB= not $hasmask
ABresult
0X0
100
111
967 27 ▶ if (length($raw) == $V4_PACKED_BYTES) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $raw == $NetAddr::IP::Lite::V4_PACKED_BYTES) { } 21 6 27
elsif (length $raw == $NetAddr::IP::Lite::V6_PACKED_BYTES) { } 5 1 6
968 21 $ip = ipv4to6($raw);
969 }
970 elsif (length($raw) == $V6_PACKED_BYTES) {
971 5 $ip = $raw;
972 5 $isV6 = $v6_input = 1;
973 }
974 else {
975 1 return undef;
976 }
977 26 last;
978 }
979 else {
980 38 return undef;
981 }
982 9335 ▶ return undef unless defined($ip = inet_aton($ip));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($ip = inet_aton($ip)) 22 9313 9335
983 9313 $ip = ipv4to6($ip);
984 9313 last;
985 }
986 ########## continuing
987 else { # ipv6 address
988 594 $isV6 = $v6_input = 1;
989 594 ▶ if (index($ip, '[') >= 0 || index($ip, ']') >= 0) { # transform URI notation
Branch: 1 branch
BranchTrueFalseTotal
if (index($ip, "[") != -1 or index($ip, "]") != -1) 33 561 594
Condition100%3/3
index($ip, "[") != -1 or index($ip, "]") != -1
l!l&&r!l&&!r
321561
MC/DC100%2/2
index($ip, "[") != -1 or index($ip, "]") != -1
index($ip, "[") != -1 index($ip, "]") != -1
Truth table100%3/3
A or B
A= index($ip, "[") != -1B= index($ip, "]") != -1
ABresult
1X1
011
000
990 33 ▶ return undef unless $ip =~ /^\[([^[\]]+)\]$/; # nothing may follow the closing bracket
Branch: 1 branch
BranchTrueFalseTotal
unless $ip =~ /^\[([^[\]]+)\]$/ 20 13 33
991 13 $ip = $1;
992 }
993 574 ▶ if (defined($tmp = ipv6_aton($ip))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($tmp = ipv6_aton($ip))) 562 12 574
994 562 $ip = $tmp;
995 562 last;
996 }
997 last
998 if grep($ip eq $_, (qw(default any loopback unspecified)))
999 12 ▶ && defined($ip = $fip6{$ip});
Branch: 1 branch
BranchTrueFalseTotal
if grep(($ip eq $_), ("default", "any", "loopback", "unspecified")) and defined($ip = $fip6{$ip}) 0 12 12
Condition33%1/3
grep(($ip eq $_), ("default", "any", "loopback", "unspecified")) and defined($ip = $fip6{$ip})
!ll&&!rl&&r
1200
MC/DC0%0/2
grep(($ip eq $_), ("default", "any", "loopback", "unspecified")) and defined($ip = $fip6{$ip})
grep(($ip eq $_), ("default", "any", "loopback", "unspecified")) defined($ip = $fip6{$ip})
Truth table33%1/3
A and B
A= grep(($ip eq $_), ("default", "any", "loopback", "unspecified"))B= defined($ip = $fip6{$ip})
ABresult
0X0
100
111
1000 12 return undef;
1001 }
1002 }
1003 11571 my ($spurious, $ones) = notcontiguous($mask);
1004 11571 ▶ return undef if $spurious; # invalid if not contiguous
Branch: 1 branch
BranchTrueFalseTotal
if $spurious 24 11547 11571
1005 # an IPv4 mask keeps the 96 ones above the address, or its length would
1006 # be negative; a mask of no ones reads as /0
1007 11547 ▶ return undef if !$isV6 && $ones && $ones < $IPV4_OFFSET && isIPv4($ip);
Branch: 1 branch
BranchTrueFalseTotal
if not $isV6 and $ones and $ones < $NetAddr::IP::Lite::IPV4_OFFSET and isIPv4($ip) 18 11529 11547
Condition88%8/9
not $isV6 and $ones and $ones < $NetAddr::IP::Lite::IPV4_OFFSET and isIPv4($ip)
!ll&&!rl&&r
11529018
not $isV6 and $ones and $ones < $NetAddr::IP::Lite::IPV4_OFFSET
!ll&&!rl&&r
1945958418
not $isV6 and $ones
!ll&&!rl&&r
1911349602
MC/DC75%3/4
not $isV6 and $ones and $ones < $NetAddr::IP::Lite::IPV4_OFFSET and isIPv4($ip)
not $isV6 $ones $ones < $NetAddr::IP::Lite::IPV4_OFFSET isIPv4($ip)
Truth table80%4/5
not(A) and B and C and D
A= not $isV6B= $onesC= $ones < $NetAddr::IP::Lite::IPV4_OFFSETD= isIPv4($ip)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1008 11529 ▶ $ip |= $_ipv4FFFF if $map_ipv4 && !$v6_input;
Branch: 1 branch
BranchTrueFalseTotal
if $map_ipv4 and not $v6_input 14 11515 11529
Condition100%3/3
$map_ipv4 and not $v6_input
!ll&&!rl&&r
115031214
MC/DC100%2/2
$map_ipv4 and not $v6_input
$map_ipv4 not $v6_input
Truth table100%3/3
A and not(B)
A= $map_ipv4B= not $v6_input
ABresult
0X0
100
111
1009  
1010 11529 my $self = {
1011 addr => $ip,
1012 mask => $mask,
1013 isv6 => $isV6,
1014 };
1015 11529 return bless $self, $class;
1016 }
1017  
1018  
1019 40745 ▶sub addr ($) {
Subroutine addr: called CC=2 SCAR=6.9
Pod documented
1020 return ($_[0]->{isv6})
1021 ? ipv6_n2x($_[0]->{addr})
1022 40745 ▶ : inet_n2dx($_[0]->{addr});
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'isv6'} ? : 26594 14151 40745
1023 }
1024  
1025  
1026 246 ▶sub mask ($) {
Subroutine mask: called CC=3 SCAR=11.1
Pod documented
1027 246 ▶ return ipv6_n2x($_[0]->{mask}) if $_[0]->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"isv6"} 2 244 246
1028 my $mask =
1029 isIPv4($_[0]->{addr})
1030 ? $_[0]->{mask} & V4net
1031 244 ▶ : $_[0]->{mask};
Branch: 1 branch
BranchTrueFalseTotal
isIPv4($_[0]{'addr'}) ? : 244 0 244
1032 244 return inet_n2dx($mask);
1033 }
1034  
1035  
1036 56673 ▶sub masklen ($) {
Subroutine masklen: called CC=4 SCAR=13.9
Pod documented
1037 56673 my $len = (notcontiguous($_[0]->{mask}))[1];
1038 56673 ▶ return 0 unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 70 56603 56673
1039 56603 ▶ return $len if $_[0]->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"isv6"} 26747 29856 56603
1040 return isIPv4($_[0]->{addr})
1041 29856 ▶ ? $len - $IPV4_OFFSET
Branch: 1 branch
BranchTrueFalseTotal
isIPv4($_[0]{'addr'}) ? : 29856 0 29856
1042 : $len;
1043 }
1044  
1045  
1046 348 ▶sub bits {
Subroutine bits: called CC=2 SCAR=6.9
Pod documented
1047 348 ▶ return $_[0]->{isv6} ? $IPV6_BITS : $IPV4_BITS;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'isv6'} ? : 134 214 348
1048 }
1049  
1050  
1051 391 ▶sub version {
Subroutine version: called CC=2 SCAR=6.9
Pod documented
1052 391 my $self = shift;
1053 391 ▶ return $self->{isv6} ? 6 : 4;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'isv6'} ? : 152 239 391
1054 }
1055  
1056  
1057 26116 ▶sub cidr ($) {
Subroutine cidr: called CC=1 SCAR=0.0
Pod documented
1058 26116 return $_[0]->addr . '/' . $_[0]->masklen;
1059 }
1060  
1061  
1062 13 ▶sub aton {
Subroutine aton: called CC=3 SCAR=11.1
Pod documented
1063 13 ▶ return $_[0]->{addr} if $_[0]->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"isv6"} 4 9 13
1064 return isIPv4($_[0]->{addr})
1065 ? ipv6to4($_[0]->{addr})
1066 9 ▶ : $_[0]->{addr};
Branch: 1 branch
BranchTrueFalseTotal
isIPv4($_[0]{'addr'}) ? : 9 0 9
1067 }
1068  
1069  
1070 60353 ▶sub network ($) {
Subroutine network: called CC=1 SCAR=0.0
Pod documented
1071 60353 return _new($_[0], $_[0]->{addr} & $_[0]->{mask}, $_[0]->{mask});
1072 }
1073  
1074  
1075 8924 ▶sub broadcast ($) {
Subroutine broadcast: called CC=2 SCAR=6.9
Pod documented
1076 8924 my $ip = _new($_[0], $_[0]->{addr} | ~$_[0]->{mask}, $_[0]->{mask});
1077 8924 ▶ $ip->{addr} &= V4net unless $ip->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
unless $ip->{"isv6"} 359 8565 8924
1078 8924 return $ip;
1079 }
1080  
1081  
1082 122 my $_cidr127 = pack('N4', 0xffffffff, 0xffffffff, 0xffffffff, 0xfffffffe);
1083  
1084 26 ▶sub first ($) {
Subroutine first: called CC=2 SCAR=6.9
Pod documented
1085 26 ▶ if (hasbits($_[0]->{mask} ^ $_cidr127)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&hasbits($_[0]{'mask'} ^ $_cidr127)) { } 24 2 26
1086 24 return $_[0]->network + 1;
1087 }
1088 else {
1089 2 return $_[0]->network;
1090 }
1091  
1092 # return $_[0]->network + 1;
1093 }
1094  
1095  
1096 18 ▶sub last ($) {
Subroutine last: called CC=2 SCAR=6.9
Pod documented
1097 18 ▶ if (hasbits($_[0]->{mask} ^ $_cidr127)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&hasbits($_[0]{'mask'} ^ $_cidr127)) { } 16 2 18
1098 16 return $_[0]->broadcast - 1;
1099 }
1100 else {
1101 2 return $_[0]->broadcast;
1102 }
1103 }
1104  
1105  
1106 6 ▶sub range ($) {
Subroutine range: called CC=1 SCAR=0.0
Pod documented
1107 6 return $_[0]->network->addr . ' - ' . $_[0]->broadcast->addr;
1108 }
1109  
1110  
1111 2281 ▶sub numeric ($) {
Subroutine numeric: called CC=6 SCAR=18.0
Pod documented
1112 2281 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 59 2222 2281
1113 59 ▶ if (!$_[0]->{isv6} && isIPv4($_[0]->{addr})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[0]{'isv6'} and isIPv4($_[0]{'addr'})) { } 18 41 59
Condition66%2/3
not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})
!ll&&!rl&&r
41018
MC/DC50%1/2
not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})
not $_[0]{"isv6"} isIPv4($_[0]{"addr"})
Truth table66%2/3
not(A) and B
A= not $_[0]{"isv6"}B= isIPv4($_[0]{"addr"})
ABresult
0X0
100
111
1114 return (
1115 sprintf("%u", unpack('N', ipv6to4($_[0]->{addr}))),
1116 18 sprintf("%u", unpack('N', ipv6to4($_[0]->{mask}))));
1117 }
1118 else {
1119 return (
1120 bin2bcd($_[0]->{addr}),
1121 41 bin2bcd($_[0]->{mask}));
1122 }
1123 }
1124 return (!$_[0]->{isv6} && isIPv4($_[0]->{addr}))
1125 ? sprintf("%u", unpack('N', ipv6to4($_[0]->{addr})))
1126 2222 ▶ : bin2bcd($_[0]->{addr});
Branch: 1 branch
BranchTrueFalseTotal
!$_[0]{'isv6'} && isIPv4($_[0]{'addr'}) ? : 1 2221 2222
Condition66%2/3
not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})
!ll&&!rl&&r
222101
MC/DC50%1/2
not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})
not $_[0]{"isv6"} isIPv4($_[0]{"addr"})
Truth table66%2/3
not(A) and B
A= not $_[0]{"isv6"}B= isIPv4($_[0]{"addr"})
ABresult
0X0
100
111
1127 }
1128  
1129  
1130 122 my $biloaded;
1131 my $bi2strng;
1132  
1133 114 ▶sub _bi_stfy {
Subroutine _bi_stfy: called CC=1 SCAR=0.0
1134 114 my $text = "$_[0]";
1135  
1136 # signed decimal text, or undef when that text is NaN or infinity
1137 114 return ($text =~ /\A\+?(-?[0-9]+)\z/)[0];
1138 }
1139  
1140 3 ▶sub _loadMBI { # load Math::BigInt on demand
Subroutine _loadMBI: called CC=2 SCAR=7.2
1141 3 ▶ if (eval { local $SIG{__DIE__}; require Math::BigInt }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { local $SIG{'__DIE__'}; require Math::BigInt } }) { } 3 0 3
1142 3 $biloaded = \&Math::BigInt::new;
1143 3 $bi2strng = \&_bi_stfy;
1144 }
1145 else {
1146 0 croak "Math::BigInt is required for bigint() support";
1147 }
1148 }
1149  
1150 114 ▶sub _retMBIstring {
Subroutine _retMBIstring: called CC=2 SCAR=6.9
1151 114 ▶ _loadMBI unless $biloaded; # load Math::BigInt on demand
Branch: 1 branch
BranchTrueFalseTotal
unless $biloaded 2 112 114
1152 114 $bi2strng->(@_);
1153 }
1154  
1155 92 ▶sub _biRef {
Subroutine _biRef: called CC=2 SCAR=6.9
1156 92 ▶ _loadMBI unless $biloaded; # load Math::BigInt on demand
Branch: 1 branch
BranchTrueFalseTotal
unless $biloaded 1 91 92
1157 92 $biloaded->('Math::BigInt', $_[0]);
1158 }
1159  
1160 51 ▶sub bigint($) {
Subroutine bigint: called CC=12 SCAR=26.1
Pod documented
1161 51 my ($addr, $mask);
1162 51 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 41 10 51
1163 41 ▶ if (!$_[0]->{isv6} && isIPv4($_[0]->{addr})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[0]{'isv6'} and isIPv4($_[0]{'addr'})) { } 9 32 41
Condition66%2/3
not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})
!ll&&!rl&&r
3209
MC/DC50%1/2
not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})
not $_[0]{"isv6"} isIPv4($_[0]{"addr"})
Truth table66%2/3
not(A) and B
A= not $_[0]{"isv6"}B= isIPv4($_[0]{"addr"})
ABresult
0X0
100
111
1164 $addr =
1165 $_[0]->{addr}
1166 9 ▶ ? sprintf("%u", unpack('N', ipv6to4($_[0]->{addr})))
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'addr'} ? : 9 0 9
1167 : 0;
1168 $mask =
1169 $_[0]->{mask}
1170 9 ▶ ? sprintf("%u", unpack('N', ipv6to4($_[0]->{mask})))
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'mask'} ? : 9 0 9
1171 : 0;
1172 }
1173 else {
1174 $addr =
1175 $_[0]->{addr}
1176 ? bin2bcd($_[0]->{addr})
1177 32 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'addr'} ? : 32 0 32
1178 $mask =
1179 $_[0]->{mask}
1180 ? bin2bcd($_[0]->{mask})
1181 32 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'mask'} ? : 32 0 32
1182 }
1183 41 (_biRef($addr), _biRef($mask));
1184  
1185 }
1186 else { # not wantarray
1187  
1188 10 ▶ if (!$_[0]->{isv6} && isIPv4($_[0]->{addr})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[0]{'isv6'} and isIPv4($_[0]{'addr'})) { } 5 5 10
Condition66%2/3
not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})
!ll&&!rl&&r
505
MC/DC50%1/2
not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})
not $_[0]{"isv6"} isIPv4($_[0]{"addr"})
Truth table66%2/3
not(A) and B
A= not $_[0]{"isv6"}B= isIPv4($_[0]{"addr"})
ABresult
0X0
100
111
1189 $addr =
1190 $_[0]->{addr}
1191 5 ▶ ? sprintf("%u", unpack('N', ipv6to4($_[0]->{addr})))
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'addr'} ? : 5 0 5
1192 : 0;
1193 }
1194 else {
1195 $addr =
1196 $_[0]->{addr}
1197 ? bin2bcd($_[0]->{addr})
1198 5 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'addr'} ? : 5 0 5
1199 }
1200 10 _biRef($addr);
1201 }
1202 }
1203  
1204  
1205 298534 ▶sub contains ($$) {
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
1206 298534 return within(@_[ 1, 0 ]);
1207 }
1208  
1209  
1210 # true for a NetAddr::IP::Lite object, NetAddr::IP included; a class name
1211 # such as 'NetAddr::IP' is a string, not an address
1212 598677 ▶sub _is_address {
Subroutine _is_address: called CC=2 SCAR=7.2
1213 598677 ▶ return blessed $_[0] && $_[0]->isa(__PACKAGE__);
Condition66%2/3
builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
5450598132
MC/DC50%1/2
builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__)
builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__)
Truth table66%2/3
A && B
A= builtin::blessed($_[0])B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
1214 }
1215  
1216 298674 ▶sub within ($$) {
Subroutine within: called CC=9 SCAR=22.0
Pod documented
1217 298674 ▶ return undef unless _is_address($_[0]) && _is_address($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_address($_[0]) and _is_address($_[1]) 6 298668 298674
Condition100%3/3
_is_address($_[0]) and _is_address($_[1])
!ll&&!rl&&r
33298668
MC/DC100%2/2
_is_address($_[0]) and _is_address($_[1])
_is_address($_[0]) _is_address($_[1])
Truth table100%3/3
A and B
A= _is_address($_[0])B= _is_address($_[1])
ABresult
0X0
100
111
1218 298668 ▶ return 0 if ($_[0]->{isv6} ? 1 : 0) != ($_[1]->{isv6} ? 1 : 0); # different address families
Branch: 3 branches
BranchTrueFalseTotal
if ($_[0]{"isv6"} ? 1 : 0) != ($_[1]{"isv6"} ? 1 : 0) 13 298655 298668
$_[0]{'isv6'} ? : 106 298562 298668
$_[1]{'isv6'} ? : 103 298565 298668
1219 298655 ▶ return 1 unless hasbits($_[1]->{mask}); # 0x0 contains everything
Branch: 1 branch
BranchTrueFalseTotal
unless &hasbits($_[1]{"mask"}) 8 298647 298655
1220 298647 my $netme = $_[0]->{addr} & $_[0]->{mask};
1221 298647 my $brdme = $_[0]->{addr} | ~$_[0]->{mask};
1222 298647 my $neto = $_[1]->{addr} & $_[1]->{mask};
1223 298647 my $brdo = $_[1]->{addr} | ~$_[1]->{mask};
1224 298647 ▶ return (sub128($netme, $neto) && sub128($brdo, $brdme)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&sub128($netme, $neto) && &sub128($brdo, $brdme) ? : 54472 244175 298647
Condition100%3/3
&sub128($netme, $neto) and &sub128($brdo, $brdme)
!ll&&!rl&&r
5734918682654472
MC/DC100%2/2
&sub128($netme, $neto) and &sub128($brdo, $brdme)
&sub128($netme, $neto) &sub128($brdo, $brdme)
Truth table100%3/3
A and B
A= &sub128($netme, $neto)B= &sub128($brdo, $brdme)
ABresult
0X0
100
111
1225 }
1226  
1227  
1228 # The network and broadcast of an object read as IPv4. An IPv6 object that
1229 # holds ::a.b.c.d or ::ffff:a.b.c.d reads as a.b.c.d; any other gives ().
1230 49 ▶sub _ipv4_bounds {
Subroutine _ipv4_bounds: called CC=3 SCAR=11.0
1231 49 my $self = $_[0];
1232 49 my $addr = $self->{addr};
1233 49 ▶ if ($self->{isv6}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"isv6"}) 27 22 49
1234 27 ▶ return unless isAnyIPv4($addr);
Branch: 1 branch
BranchTrueFalseTotal
unless isAnyIPv4($addr) 3 24 27
1235 24 $addr &= ~$_ipv4FFFF; # drop the mapped prefix
1236 }
1237 46 return ($addr & $self->{mask}, $addr | ~$self->{mask});
1238 }
1239  
1240 # each network carries its own mask. inet_any2n packs the literal, so
1241 # loading does not ask the resolver
1242 122 my $ip_10n = inet_any2n('10.0.0.0');
1243 122 my $ip_10b = $ip_10n | ~mask4to6(pack('C4', 255, 0, 0, 0));
1244  
1245 122 my $ip_172n = inet_any2n('172.16.0.0');
1246 122 my $ip_172b = $ip_172n | ~mask4to6(pack('C4', 255, 240, 0, 0));
1247  
1248 122 my $ip_192n = inet_any2n('192.168.0.0');
1249 122 my $ip_192b = $ip_192n | ~mask4to6(pack('C4', 255, 255, 0, 0));
1250  
1251 33 ▶sub is_rfc1918 ($) {
Subroutine is_rfc1918: called CC=8 SCAR=20.8
Pod documented
1252 33 ▶ my ($netme, $brdme) = _ipv4_bounds($_[0]) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
() = _ipv4_bounds($_[0]) or return 0 30 3 33
1253 30 ▶ return 1 if (sub128($netme, $ip_10n) && sub128($ip_10b, $brdme));
Branch: 1 branch
BranchTrueFalseTotal
if &sub128($netme, $ip_10n) and &sub128($ip_10b, $brdme) 7 23 30
Condition100%3/3
&sub128($netme, $ip_10n) and &sub128($ip_10b, $brdme)
!ll&&!rl&&r
1227
MC/DC100%2/2
&sub128($netme, $ip_10n) and &sub128($ip_10b, $brdme)
&sub128($netme, $ip_10n) &sub128($ip_10b, $brdme)
Truth table100%3/3
A and B
A= &sub128($netme, $ip_10n)B= &sub128($ip_10b, $brdme)
ABresult
0X0
100
111
1254 23 ▶ return 1 if (sub128($netme, $ip_192n) && sub128($ip_192b, $brdme));
Branch: 1 branch
BranchTrueFalseTotal
if &sub128($netme, $ip_192n) and &sub128($ip_192b, $brdme) 5 18 23
Condition100%3/3
&sub128($netme, $ip_192n) and &sub128($ip_192b, $brdme)
!ll&&!rl&&r
1445
MC/DC100%2/2
&sub128($netme, $ip_192n) and &sub128($ip_192b, $brdme)
&sub128($netme, $ip_192n) &sub128($ip_192b, $brdme)
Truth table100%3/3
A and B
A= &sub128($netme, $ip_192n)B= &sub128($ip_192b, $brdme)
ABresult
0X0
100
111
1255 18 ▶ return (sub128($netme, $ip_172n) && sub128($ip_172b, $brdme)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&sub128($netme, $ip_172n) && &sub128($ip_172b, $brdme) ? : 5 13 18
Condition100%3/3
&sub128($netme, $ip_172n) and &sub128($ip_172b, $brdme)
!ll&&!rl&&r
675
MC/DC100%2/2
&sub128($netme, $ip_172n) and &sub128($ip_172b, $brdme)
&sub128($netme, $ip_172n) &sub128($ip_172b, $brdme)
Truth table100%3/3
A and B
A= &sub128($netme, $ip_172n)B= &sub128($ip_172b, $brdme)
ABresult
0X0
100
111
1256 }
1257  
1258  
1259 122 my $_lclhost6 = NetAddr::IP::Lite->new('::1');
1260 122 my $_lclnetn = inet_any2n('127.0.0.0');
1261 122 my $_lclnetb = $_lclnetn | ~mask4to6(pack('C4', 255, 0, 0, 0));
1262  
1263 17 ▶sub is_local ($) {
Subroutine is_local: called CC=6 SCAR=17.9
Pod documented
1264 17 ▶ return 1 if $_[0]->{isv6} && $_[0] == $_lclhost6;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"isv6"} and $_[0] == $_lclhost6 1 16 17
Condition100%3/3
$_[0]{"isv6"} and $_[0] == $_lclhost6
!ll&&!rl&&r
6101
MC/DC100%2/2
$_[0]{"isv6"} and $_[0] == $_lclhost6
$_[0]{"isv6"} $_[0] == $_lclhost6
Truth table100%3/3
A and B
A= $_[0]{"isv6"}B= $_[0] == $_lclhost6
ABresult
0X0
100
111
1265 16 ▶ my ($netme, $brdme) = _ipv4_bounds($_[0]) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
() = _ipv4_bounds($_[0]) or return 0 16 0 16
1266 16 ▶ return (sub128($netme, $_lclnetn) && sub128($_lclnetb, $brdme)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&sub128($netme, $_lclnetn) && &sub128($_lclnetb, $brdme) ? : 9 7 16
Condition100%3/3
&sub128($netme, $_lclnetn) and &sub128($_lclnetb, $brdme)
!ll&&!rl&&r
439
MC/DC100%2/2
&sub128($netme, $_lclnetn) and &sub128($_lclnetb, $brdme)
&sub128($netme, $_lclnetn) &sub128($_lclnetb, $brdme)
Truth table100%3/3
A and B
A= &sub128($netme, $_lclnetn)B= &sub128($_lclnetb, $brdme)
ABresult
0X0
100
111
1267 }
1268  
1269  
1270 116 ▶sub num ($) {
Subroutine num: called CC=9 SCAR=22.0
Pod documented
1271 116 ▶ if ($Old_nth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($NetAddr::IP::Lite::Old_nth) { } 70 46 116
1272 70 my @net = unpack('L3N', $_[0]->{mask} ^ Ones);
1273  
1274 # number of ip's less broadcast
1275 70 ▶ return 0xfffffffe if $net[0] || $net[1] || $net[2]; # 2**32 - 2
Branch: 1 branch
BranchTrueFalseTotal
if $net[0] or $net[1] or $net[2] 37 33 70
Condition83%5/6
$net[0] or $net[1] or $net[2]
l!l&&r!l&&!r
33433
$net[0] or $net[1]
l!l&&r!l&&!r
03337
MC/DC66%2/3
$net[0] or $net[1] or $net[2]
$net[0] $net[1] $net[2]
Truth table75%3/4
A or B or C
A= $net[0]B= $net[1]C= $net[2]
ABCresult
1XX1
01X1
0011
0000
1276 33 ▶ return $net[3] if $net[3];
Branch: 1 branch
BranchTrueFalseTotal
if $net[3] 22 11 33
1277 }
1278 else { # returns 1 for /32 /128, 2 for /31 /127 else n-2 up to 2**32
1279 46 (undef, my $net) = addconst($_[0]->{mask}, 1);
1280 46 ▶ return 1 unless hasbits($net); # ipV4/32 or ipV6/128
Branch: 1 branch
BranchTrueFalseTotal
unless &hasbits($net) 6 40 46
1281 40 $net = $net ^ Ones;
1282 40 ▶ return 2 unless hasbits($net); # ipV4/31 or ipV6/127
Branch: 1 branch
BranchTrueFalseTotal
unless &hasbits($net) 6 34 40
1283 34 ▶ $net &= $_v4net unless $_[0]->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"isv6"} 10 24 34
1284 34 return bin2bcd($net);
1285 }
1286 }
1287  
1288  
1289 184 ▶sub nth ($$) {
Subroutine nth: called CC=18 SCAR=29.7
Pod documented
1290 184 my $self = shift;
1291 184 my $count = shift;
1292  
1293 184 ▶ return undef unless defined $count && $count =~ /^-?[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count and $count =~ /^-?[0-9]+$/ 6 178 184
Condition100%3/3
defined $count and $count =~ /^-?[0-9]+$/
!ll&&!rl&&r
15178
MC/DC100%2/2
defined $count and $count =~ /^-?[0-9]+$/
defined $count $count =~ /^-?[0-9]+$/
Truth table100%3/3
A and B
A= defined $countB= $count =~ /^-?[0-9]+$/
ABresult
0X0
100
111
1294  
1295 178 my $slash31 = !hasbits($self->{mask} ^ $_cidr127);
1296 178 ▶ if ($Old_nth) {
Branch: 2 branches
BranchTrueFalseTotal
if ($NetAddr::IP::Lite::Old_nth) { } 58 120 178
elsif ($slash31) { } 0 120 120
1297 58 ▶ return undef if $slash31 && $count != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $slash31 and $count != 1 0 58 58
Condition33%1/3
$slash31 and $count != 1
!ll&&!rl&&r
5800
MC/DC0%0/2
$slash31 and $count != 1
$slash31 $count != 1
Truth table33%1/3
A and B
A= $slash31B= $count != 1
ABresult
0X0
100
111
1298 58 ▶ return undef if ($count < 1 or $count > $self->num());
Branch: 1 branch
BranchTrueFalseTotal
if $count < 1 or $count > $self->num 13 45 58
Condition100%3/3
$count < 1 or $count > $self->num
l!l&&r!l&&!r
21145
MC/DC100%2/2
$count < 1 or $count > $self->num
$count < 1 $count > $self->num
Truth table100%3/3
A or B
A= $count < 1B= $count > $self->num
ABresult
1X1
011
000
1299 }
1300 elsif ($slash31) {
1301 0 ▶ return undef if ($count && $count != 1); # only index 0, 1 allowed for /31
Branch: 1 branch
BranchTrueFalseTotal
if $count and $count != 1 0 0 0
Condition0%0/3
$count and $count != 1
!ll&&!rl&&r
000
MC/DC0%0/2
$count and $count != 1
$count $count != 1
Truth table0%0/3
A and B
A= $countB= $count != 1
ABresult
0X0
100
111
1302 }
1303 else {
1304 # read as a 128 bit number, so an index past 2**53 stays exact; -0 is index 0
1305 120 my ($minus, $digits) = "$count" =~ /\A(-?)0*([0-9]+)/;
1306 120 ▶ return undef if $minus && $digits ne '0';
Branch: 1 branch
BranchTrueFalseTotal
if $minus and $digits ne 0 1 119 120
Condition100%3/3
$minus and $digits ne 0
!ll&&!rl&&r
11811
MC/DC100%2/2
$minus and $digits ne 0
$minus $digits ne 0
Truth table100%3/3
A and B
A= $minusB= $digits ne 0
ABresult
0X0
100
111
1307 119 my $index = _bcd2bin_or_undef($digits);
1308 119 ▶ return undef unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 1 118 119
1309  
1310 # index 0 is one past the network address, the last one before the broadcast
1311 118 my ($carry, $offset) = add128($index, $_one128);
1312  
1313 # the usable hosts as num() counts them: a /32 or /128 has one
1314 118 (undef, my $hosts) = addconst($self->{mask}, 1);
1315 118 ▶ $hosts = hasbits($hosts) ? ~$hosts : $_one128;
Branch: 1 branch
BranchTrueFalseTotal
&hasbits($hosts) ? : 100 18 118
1316 118 ▶ $hosts &= $_v4net unless $self->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"isv6"} 16 102 118
1317 118 ▶ return undef if $carry || $offset gt $hosts;
Branch: 1 branch
BranchTrueFalseTotal
if $carry or $offset gt $hosts 16 102 118
Condition100%3/3
$carry or $offset gt $hosts
l!l&&r!l&&!r
115102
MC/DC100%2/2
$carry or $offset gt $hosts
$carry $offset gt $hosts
Truth table100%3/3
A or B
A= $carryB= $offset gt $hosts
ABresult
1X1
011
000
1318 return _new(
1319 $self, ($self->{addr} & $self->{mask}) | ($offset & ~$self->{mask}),
1320 102 $self->{mask});
1321 }
1322 45 return $self->network + $count;
1323 }
1324  
1325  
1326 61 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
1327 61 ▶ if (grep { $_ eq ':aton' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":aton";} @_) 3 58 61
1328 3 warnings::warnif(
1329 'deprecated',
1330 ':aton is deprecated and will be removed in version 5; new() accepts inet_aton notation without it, and new_from_aton() takes a packed IPv4 address'
1331 );
1332 3 $Accept_Binary_IP = 1;
1333 3 @_ = grep { $_ ne ':aton' } @_;
1334 }
1335 61 ▶ if (grep { $_ eq ':old_nth' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":old_nth";} @_) 7 54 61
1336 7 warnings::warnif(
1337 'deprecated',
1338 ':old_nth is deprecated and will be removed in version 5; without it nth(0) is the first usable host and num() counts only usable hosts'
1339 );
1340 7 $Old_nth = 1;
1341 7 @_ = grep { $_ ne ':old_nth' } @_;
1342 }
1343 61 NetAddr::IP::InetBase::_case_tags(\@_);
1344 61 ▶ if (grep { $_ eq ':nofqdn' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":nofqdn";} @_) 1 60 61
1345 1 $NoFQDN = 1;
1346 1 @_ = grep { $_ ne ':nofqdn' } @_;
1347 }
1348 61 NetAddr::IP::Lite->export_to_level(1, @_);
1349 }
1350  
1351  
1352 122 1;
1353  
1354 __END__
↑ Summary IP.pm »