blib/lib/NetAddr/IP/Lite.pm

filter: Statementstmt 89.5% 396 / 442 Branchbran 84.3% 258 / 306 Conditioncond 66.5% 155 / 233 MC/DCmcdc 44.1% 53 / 120 Subroutinesub 92.5% 62 / 67 Podpod 75.6% 28 / 37 totaltotal 79.0% 952 / 1205 CC 234 SCAR 49.5
CC
234
Cov
82%
_xnew
313.4
nth
15.1
AUTOLOAD
14.3
CRAP 140.8
↑ Summary blib/lib/NetAddr/IP.pm »
1 #!/usr/bin/perl
2  
3 package NetAddr::IP::Lite;
4  
5 104 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 104 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 #use diagnostics;
8 #use warnings;
9 104 ▶use NetAddr::IP::InetBase qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 inet_any2n
11 isIPv4
12 inet_n2dx
13 inet_aton
14 ipv6_aton
15 ipv6_n2x
16 fillIPv4
17 104 );
18 104 ▶use NetAddr::IP::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
19 addconst
20 sub128
21 ipv6to4
22 notcontiguous
23 shiftleft
24 hasbits
25 bin2bcd
26 bcd2bin
27 mask4to6
28 ipv4to6
29 naip_gethostbyname
30 havegethostbyname2
31 104 );
32  
33 104 ▶use vars qw(@ISA @EXPORT_OK $VERSION $Accept_Binary_IP $Old_nth $NoFQDN $AUTOLOAD *Zero);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 104 $VERSION = do { my @r = (q$Revision: 1.57 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
36  
37 104 require Exporter;
38  
39 104 @ISA = qw(Exporter);
40  
41 104 @EXPORT_OK = qw(Zeros Zero Ones V4mask V4net);
42  
43 # Set to true, to enable recognizing of ipV4 && ipV6 binary notation IP
44 # addresses. Thanks to Steve Snodgrass for reporting. This can be done
45 # at the time of use-ing the module. See docs for details.
46  
47 104 $Accept_Binary_IP = 0;
48 104 $Old_nth = 0;
49 104 *Zero = \&Zeros;
50  
51 =pod
52  
53 =encoding UTF-8
54  
55 =head1 NAME
56  
57 NetAddr::IP::Lite - Manages IPv4 and IPv6 addresses and subnets
58  
59 =head1 SYNOPSIS
60  
61 use NetAddr::IP::Lite qw(
62 Zeros
63 Ones
64 V4mask
65 V4net
66 :aton DEPRECATED !
67 :old_nth
68 :upper
69 :lower
70 :nofqdn
71 );
72  
73 my $ip = new NetAddr::IP::Lite '127.0.0.1';
74 or if your prefer
75 my $ip = NetAddr::IP::Lite->new('127.0.0.1);
76 or from a packed IPv4 address
77 my $ip = new_from_aton NetAddr::IP::Lite (inet_aton('127.0.0.1'));
78 or from an octal filtered IPv4 address
79 my $ip = new_no NetAddr::IP::Lite '127.012.0.0';
80  
81 print "The address is ", $ip->addr, " with mask ", $ip->mask, "\n" ;
82  
83 if ($ip->within(new NetAddr::IP::Lite "127.0.0.0", "255.0.0.0")) {
84 print "Is a loopback address\n";
85 }
86  
87 # This prints 127.0.0.1/32
88 print "You can also say $ip...\n";
89  
90 The following four functions return ipV6 representations of:
91  
92 :: = Zeros();
93 FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF = Ones();
94 FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:: = V4mask();
95 ::FFFF:FFFF = V4net();
96  
97 Will also return an ipV4 or ipV6 representation of a
98 resolvable Fully Qualified Domanin Name (FQDN).
99  
100 =head1 INSTALLATION
101  
102 Un-tar the distribution in an appropriate directory and type:
103  
104 perl Makefile.PL
105 make
106 make test
107 make install
108  
109 B<NetAddr::IP::Lite> depends on B<NetAddr::IP::Util> which installs by default with its primary functions compiled
110 using Perl's XS extensions to build a 'C' library. If you do not have a 'C'
111 complier available or would like the slower Pure Perl version for some other
112 reason, then type:
113  
114 perl Makefile.PL -noxs
115 make
116 make test
117 make install
118  
119 =head1 DESCRIPTION
120  
121 This module provides an object-oriented abstraction on top of IP
122 addresses or IP subnets, that allows for easy manipulations. Most of the
123 operations of NetAddr::IP are supported. This module will work with older
124 versions of Perl and is compatible with Math::BigInt.
125  
126 * By default B<NetAddr::IP> functions and methods return string IPv6
127 addresses in uppercase. To change that to lowercase:
128  
129 NOTE: the AUGUST 2010 RFC5952 states:
130  
131 4.3. Lowercase
132  
133 The characters "a", "b", "c", "d", "e", and "f" in an IPv6
134 address MUST be represented in lowercase.
135  
136 It is recommended that all NEW applications using NetAddr::IP::Lite be
137 invoked as shown on the next line.
138  
139 use NetAddr::IP::Lite qw(:lower);
140  
141 * To ensure the current IPv6 string case behavior even if the default changes:
142  
143 use NetAddr::IP::Lite qw(:upper);
144  
145  
146 The internal representation of all IP objects is in 128 bit IPv6 notation.
147 IPv4 and IPv6 objects may be freely mixed.
148  
149 The supported operations are described below:
150  
151 =cut
152  
153 # in the off chance that NetAddr::IP::Lite objects are created
154 # and the caller later loads NetAddr::IP and expects to use
155 # those objects, let the AUTOLOAD routine find and redirect
156 # NetAddr::IP::Lite method and subroutine calls to NetAddr::IP.
157 #
158  
159 104 my $parent = 'NetAddr::IP';
160  
161 # test function
162 #
163 # input: subroutine name in NetAddr::IP
164 # output: t/f if sub name exists in NetAddr::IP namespace
165 #
166 #sub sub_exists {
167 # my $other = $parent .'::';
168 # return exists ${$other}{$_[0]};
169 #}
170  
171 0 ▶sub DESTROY {};
Subroutine DESTROY: not called CC=1 SCAR=0.0
172  
173 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=4 SCAR=26.6
174 104 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 0 my ($pkg,$func) = ($AUTOLOAD =~ /(.*)::([^:]+)$/);
176 0 my $other = $parent .'::';
177  
178 0 ▶ if ($pkg =~ /^$other/o && exists ${$other}{$func}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg =~ /^$other/o and exists do { $other }->{$func}) 0 0 0
Condition0%0/3
$pkg =~ /^$other/o and exists do { $other }->{$func}
!ll&&!rl&&r
000
MC/DC0%0/2
$pkg =~ /^$other/o and exists do { $other }->{$func}
$pkg =~ /^$other/o exists do { $other }->{$func}
Truth table0%0/3
A and B
A= $pkg =~ /^$other/oB= exists do { $other }->{$func}
ABresult
0X0
100
111
179 0 $other .= $func;
180 0 goto &{$other};
181 }
182  
183 0 my @stack = caller(0);
184  
185 0 ▶ if ( $pkg eq ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg eq ref $_[0]) { } 0 0 0
186 0 $other = qq|Can't locate object method "$func" via|;
187 }
188 else {
189 0 $other = qq|Undefined subroutine \&$AUTOLOAD not found in|;
190 }
191 0 die $other . qq| package "$parent" or "$pkg" (did you forgot to load a module?) at $stack[1] line $stack[2].\n|;
192 }
193  
194 =head2 Overloaded Operators
195  
196 =cut
197  
198 # these really should be packed in Network Long order but since they are
199 # symmetrical, that extra internal processing can be skipped
200  
201 104 my $_v4zero = pack('L',0);
202 104 my $_zero = pack('L4',0,0,0,0);
203 104 my $_ones = ~$_zero;
204 104 my $_v4mask = pack('L4',0xffffffff,0xffffffff,0xffffffff,0);
205 104 my $_v4net = ~ $_v4mask;
206 104 my $_ipv4FFFF = pack('N4',0,0,0xffff,0);
207  
208 940 ▶sub Zeros() {
Subroutine Zeros: called CC=1 SCAR=0.0
Pod undocumented
209 940 return $_zero;
210 }
211 10396 ▶sub Ones() {
Subroutine Ones: called CC=1 SCAR=0.0
Pod undocumented
212 10396 return $_ones;
213 }
214 4 ▶sub V4mask() {
Subroutine V4mask: called CC=1 SCAR=0.0
Pod undocumented
215 4 return $_v4mask;
216 }
217 617 ▶sub V4net() {
Subroutine V4net: called CC=1 SCAR=0.0
Pod undocumented
218 617 return $_v4net;
219 }
220  
221 #############################################
222 # These are the overload methods, placed here
223 # for convenience.
224 #############################################
225  
226 use overload
227  
228 104 ▶ '+' => \&plus,
Subroutine BEGIN: called CC=1 SCAR=0.0
229  
230 '-' => \&minus,
231  
232 '++' => \&plusplus,
233  
234 '--' => \&minusminus,
235  
236 "=" => \&copy,
237  
238 22577 ▶ '""' => sub { $_[0]->cidr(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
239  
240 698 ▶ 'eq' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
241 698 ▶ my $a = (UNIVERSAL::isa($_[0],__PACKAGE__)) ? $_[0]->cidr : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 698 0 698
242 698 ▶ my $b = (UNIVERSAL::isa($_[1],__PACKAGE__)) ? $_[1]->cidr : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[1], __PACKAGE__) ? : 13 685 698
243 698 $a eq $b;
244 },
245  
246 3 ▶ 'ne' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
247 3 ▶ my $a = (UNIVERSAL::isa($_[0],__PACKAGE__)) ? $_[0]->cidr : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 3 0 3
248 3 ▶ my $b = (UNIVERSAL::isa($_[1],__PACKAGE__)) ? $_[1]->cidr : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[1], __PACKAGE__) ? : 1 2 3
249 3 $a ne $b;
250 },
251  
252 14 ▶ '==' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
253 14 ▶ return 0 unless UNIVERSAL::isa($_[0],__PACKAGE__) && UNIVERSAL::isa($_[1],__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($_[0], __PACKAGE__) and UNIVERSAL::isa($_[1], __PACKAGE__) 2 12 14
Condition66%2/3
UNIVERSAL::isa($_[0], __PACKAGE__) and UNIVERSAL::isa($_[1], __PACKAGE__)
!ll&&!rl&&r
0212
MC/DC50%1/2
UNIVERSAL::isa($_[0], __PACKAGE__) and UNIVERSAL::isa($_[1], __PACKAGE__)
UNIVERSAL::isa($_[0], __PACKAGE__) UNIVERSAL::isa($_[1], __PACKAGE__)
Truth table66%2/3
A and B
A= UNIVERSAL::isa($_[0], __PACKAGE__)B= UNIVERSAL::isa($_[1], __PACKAGE__)
ABresult
0X0
100
111
254 12 $_[0]->cidr eq $_[1]->cidr;
255 },
256  
257 5 ▶ '!=' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
258 5 ▶ return 1 unless UNIVERSAL::isa($_[0],__PACKAGE__) && UNIVERSAL::isa($_[1],__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($_[0], __PACKAGE__) and UNIVERSAL::isa($_[1], __PACKAGE__) 1 4 5
Condition66%2/3
UNIVERSAL::isa($_[0], __PACKAGE__) and UNIVERSAL::isa($_[1], __PACKAGE__)
!ll&&!rl&&r
014
MC/DC50%1/2
UNIVERSAL::isa($_[0], __PACKAGE__) and UNIVERSAL::isa($_[1], __PACKAGE__)
UNIVERSAL::isa($_[0], __PACKAGE__) UNIVERSAL::isa($_[1], __PACKAGE__)
Truth table66%2/3
A and B
A= UNIVERSAL::isa($_[0], __PACKAGE__)B= UNIVERSAL::isa($_[1], __PACKAGE__)
ABresult
0X0
100
111
259 4 $_[0]->cidr ne $_[1]->cidr;
260 },
261  
262 21 ▶ '>' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
263 21 ▶ return &comp_addr_mask > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&comp_addr_mask > 0 ? : 11 10 21
264 },
265  
266 8482 ▶ '<' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
267 8482 ▶ return &comp_addr_mask < 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&comp_addr_mask < 0 ? : 8347 135 8482
268 },
269  
270 54 ▶ '>=' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
271 54 ▶ return &comp_addr_mask < 0 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
&comp_addr_mask < 0 ? : 29 25 54
272 },
273  
274 26 ▶ '<=' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
275 26 ▶ return &comp_addr_mask > 0 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
&comp_addr_mask > 0 ? : 18 8 26
276 },
277  
278 104 '<=>' => \&comp_addr_mask,
279  
280 104 'cmp' => \&comp_addr_mask;
281  
282 68800 ▶sub comp_addr_mask {
Subroutine comp_addr_mask: called CC=5 SCAR=16.1
Pod undocumented
283 68800 my($c,$rv) = sub128($_[0]->{addr},$_[1]->{addr});
284 68800 ▶ return -1 unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 64909 3891 68800
285 3891 ▶ return 1 if hasbits($rv);
Branch: 1 branch
BranchTrueFalseTotal
if &hasbits($rv) 3665 226 3891
286 226 ($c,$rv) = sub128($_[0]->{mask},$_[1]->{mask});
287 226 ▶ return -1 unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 32 194 226
288 194 ▶ return hasbits($rv) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&hasbits($rv) ? : 26 168 194
289 }
290  
291 #sub comp_addr {
292 # my($c,$rv) = sub128($_[0]->{addr},$_[1]->{addr});
293 # return -1 unless $c;
294 # return hasbits($rv) ? 1 : 0;
295 #}
296  
297 =pod
298  
299 =over
300  
301 =item B<Assignment (C<=>)>
302  
303 Has been optimized to copy one NetAddr::IP::Lite object to another very quickly.
304  
305 =item B<C<-E<gt>copy()>>
306  
307 The B<assignment (C<=>)> operation is only put in to operation when the
308 copied object is further mutated by another overloaded operation. See
309 L<overload> B<SPECIAL SYMBOLS FOR "use overload"> for details.
310  
311 B<C<-E<gt>copy()>> actually creates a new object when called.
312  
313 =cut
314  
315 54559 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
316 54559 return _new($_[0],$_[0]->{addr}, $_[0]->{mask});
317 }
318  
319 =item B<Stringification>
320  
321 An object can be used just as a string. For instance, the following code
322  
323 my $ip = new NetAddr::IP::Lite '192.168.1.123';
324 print "$ip\n";
325  
326 Will print the string 192.168.1.123/32.
327  
328 my $ip = new6 NetAddr::IP::Lite '192.168.1.123';
329 print "$ip\n";
330  
331 Will print the string 0:0:0:0:0:0:C0A8:17B/128
332  
333 =item B<Equality>
334  
335 You can test for equality with either C<eq>, C<ne>, C<==> or C<!=>. C<eq>, C<ne> allows the
336 comparison with arbitrary strings as well as NetAddr::IP::Lite objects. The
337 following example:
338  
339 if (NetAddr::IP::Lite->new('127.0.0.1','255.0.0.0') eq '127.0.0.1/8')
340 { print "Yes\n"; }
341  
342 Will print out "Yes".
343  
344 Comparison with C<==> and C<!=> requires both operands to be NetAddr::IP::Lite objects.
345  
346 =item B<Comparison via E<gt>, E<lt>, E<gt>=, E<lt>=, E<lt>=E<gt> and C<cmp>>
347  
348 Internally, all network objects are represented in 128 bit format.
349 The numeric representation of the network is compared through the
350 corresponding operation. Comparisons are tried first on the address portion
351 of the object and if that is equal then the NUMERIC cidr portion of the
352 masks are compared. This leads to the counterintuitive result that
353  
354 /24 > /16
355  
356 Comparison should not be done on netaddr objects with different CIDR as
357 this may produce indeterminate - unexpected results,
358 rather the determination of which netblock is larger or smaller should be
359 done by comparing
360  
361 $ip1->masklen <=> $ip2->masklen
362  
363 =item B<Addition of a constant (C<+>)>
364  
365 Add a 32 bit signed constant to the address part of a NetAddr object.
366 This operation changes the address part to point so many hosts above the
367 current objects start address. For instance, this code:
368  
369 print NetAddr::IP::Lite->new('127.0.0.1/8') + 5;
370  
371 will output 127.0.0.6/8. The address will wrap around at the broadcast
372 back to the network address. This code:
373  
374 print NetAddr::IP::Lite->new('10.0.0.1/24') + 255;
375  
376 outputs 10.0.0.0/24.
377  
378 Returns the the unchanged object when the constant is missing or out of range.
379  
380 2147483647 <= constant >= -2147483648
381  
382 =cut
383  
384 204 ▶sub plus {
Subroutine plus: called CC=4 SCAR=13.9
Pod undocumented
385 204 my $ip = shift;
386 204 my $const = shift;
387  
388 204 ▶ return $ip unless $const &&
Branch: 1 branch
BranchTrueFalseTotal
unless $const and $const < 2147483648 and $const > '-2147483649' 4 200 204
Condition100%6/6
$const and $const < 2147483648 and $const > '-2147483649'
!ll&&!rl&&r
31200
$const and $const < 2147483648
!ll&&!rl&&r
21201
MC/DC100%3/3
$const and $const < 2147483648 and $const > '-2147483649'
$const $const < 2147483648 $const > '-2147483649'
Truth table100%4/4
A and B and C
A= $constB= $const < 2147483648C= $const > '-2147483649'
ABCresult
0XX0
10X0
1100
1111
389 $const < 2147483648 &&
390 $const > -2147483649;
391  
392 200 my $a = $ip->{addr};
393 200 my $m = $ip->{mask};
394  
395 200 my $lo = $a & ~$m;
396 200 my $hi = $a & $m;
397  
398 200 my $new = ((addconst($lo,$const))[1] & ~$m) | $hi;
399  
400 200 return _new($ip,$new,$m);
401 }
402  
403 =item B<Subtraction of a constant (C<->)>
404  
405 The complement of the addition of a constant.
406  
407 =item B<Difference (C<->)>
408  
409 Returns the difference between the address parts of two NetAddr::IP::Lite
410 objects address parts as a 32 bit signed number.
411  
412 Returns B<undef> if the difference is out of range.
413  
414 =cut
415  
416 104 my $_smsk = pack('L3N',0xffffffff,0xffffffff,0xffffffff,0x80000000);
417  
418 46 ▶sub minus {
Subroutine minus: called CC=5 SCAR=16.1
Pod undocumented
419 46 my $ip = shift;
420 46 my $arg = shift;
421 46 ▶ unless (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $arg) 20 26 46
422 20 return plus($ip, -$arg);
423 }
424 26 my($carry,$dif) = sub128($ip->{addr},$arg->{addr});
425 26 ▶ if ($carry) { # value is positive
Branch: 1 branch
BranchTrueFalseTotal
if ($carry) { } 13 13 26
426 13 ▶ return undef if hasbits($dif & $_smsk); # all sign bits should be 0's
Branch: 1 branch
BranchTrueFalseTotal
if &hasbits($dif & $_smsk) 1 12 13
427 12 return (unpack('L3N',$dif))[3];
428 } else {
429 13 ▶ return undef if hasbits(($dif & $_smsk) ^ $_smsk); # sign is 1's
Branch: 1 branch
BranchTrueFalseTotal
if &hasbits($dif & $_smsk ^ $_smsk) 1 12 13
430 12 return (unpack('L3N',$dif))[3] - 4294967296;
431 }
432 }
433  
434 # Auto-increment an object
435  
436 =item B<Auto-increment>
437  
438 Auto-incrementing a NetAddr::IP::Lite object causes the address part to be
439 adjusted to the next host address within the subnet. It will wrap at
440 the broadcast address and start again from the network address.
441  
442 =cut
443  
444 8355 ▶sub plusplus {
Subroutine plusplus: called CC=1 SCAR=0.0
Pod undocumented
445 8355 my $ip = shift;
446  
447 8355 my $a = $ip->{addr};
448 8355 my $m = $ip->{mask};
449  
450 8355 my $lo = $a & ~ $m;
451 8355 my $hi = $a & $m;
452  
453 8355 $ip->{addr} = ((addconst($lo,1))[1] & ~ $m) | $hi;
454 8355 return $ip;
455 }
456  
457 =item B<Auto-decrement>
458  
459 Auto-decrementing a NetAddr::IP::Lite object performs exactly the opposite
460 of auto-incrementing it, as you would expect.
461  
462 =cut
463  
464 6 ▶sub minusminus {
Subroutine minusminus: called CC=1 SCAR=0.0
Pod undocumented
465 6 my $ip = shift;
466  
467 6 my $a = $ip->{addr};
468 6 my $m = $ip->{mask};
469  
470 6 my $lo = $a & ~$m;
471 6 my $hi = $a & $m;
472  
473 6 $ip->{addr} = ((addconst($lo,-1))[1] & ~$m) | $hi;
474 6 return $ip;
475 }
476  
477 #############################################
478 # End of the overload methods.
479 #############################################
480  
481 # Preloaded methods go here.
482  
483 # This is a variant to ->new() that
484 # creates and blesses a new object
485 # without the fancy parsing of
486 # IP formats and shorthands.
487  
488 # return a blessed IP object without parsing
489 # input: prototype, naddr, nmask
490 # returns: blessed IP object
491 #
492 150680 ▶sub _new ($$$) {
Subroutine _new: called CC=2 SCAR=7.0
493 150680 my $proto = shift;
494 150680 ▶ my $class = ref($proto) || die "reference required";
Condition50%1/2
ref $proto || die('reference required')
l!l
1506800
MC/DC0%0/1
ref $proto || die('reference required')
ref $proto
Truth table50%1/2
A
A= ref $proto
Aresult
11
00
495 150680 $proto = $proto->{isv6};
496 150680 my $self = {
497 addr => $_[0],
498 mask => $_[1],
499 isv6 => $proto,
500 };
501 150680 return bless $self, $class;
502 }
503  
504 =pod
505  
506 =back
507  
508 =head2 Methods
509  
510 =over
511  
512 =item C<-E<gt>new([$addr, [ $mask|IPv6 ]])>
513  
514 =item C<-E<gt>new6([$addr, [ $mask]])>
515  
516 =item C<-E<gt>new6FFFF([$addr, [ $mask]])>
517  
518 =item C<-E<gt>new_no([$addr, [ $mask]])>
519  
520 =item C<-E<gt>new_from_aton($netaddr)>
521  
522 =item new_cis and new_cis6 are DEPRECATED
523  
524 =item C<-E<gt>new_cis("$addr $mask)>
525  
526 =item C<-E<gt>new_cis6("$addr $mask)>
527  
528 The first three methods create a new address with the supplied address in
529 C<$addr> and an optional netmask C<$mask>, which can be omitted to get
530 a /32 or /128 netmask for IPv4 / IPv6 addresses respectively.
531  
532 new6FFFF specifically returns an IPv4 address in IPv6 format according to RFC4291
533  
534 new6 ::xxxx:xxxx
535 new6FFFF ::FFFF:xxxx:xxxx
536  
537 The third method C<new_no> is exclusively for IPv4 addresses and filters
538 improperly formatted
539 dot quad strings for leading 0's that would normally be interpreted as octal
540 format by NetAddr per the specifications for inet_aton.
541  
542 B<new_from_aton> takes a packed IPv4 address and assumes a /32 mask. This
543 function replaces the DEPRECATED :aton functionality which is fundamentally
544 broken.
545  
546 The last two methods B<new_cis> and B<new_cis6> differ from B<new> and
547 B<new6> only in that they except the common Cisco address notation for
548 address/mask pairs with a B<space> as a separator instead of a slash (/)
549  
550 These methods are DEPRECATED because the functionality is now included
551 in the other "new" methods
552  
553 i.e. ->new_cis('1.2.3.0 24')
554 or
555 ->new_cis6('::1.2.3.0 120')
556  
557 C<-E<gt>new6> and
558 C<-E<gt>new_cis6> mark the address as being in ipV6 address space even
559 if the format would suggest otherwise.
560  
561 i.e. ->new6('1.2.3.4') will result in ::102:304
562  
563 addresses submitted to ->new in ipV6 notation will
564 remain in that notation permanently. i.e.
565 ->new('::1.2.3.4') will result in ::102:304
566 whereas new('1.2.3.4') would print out as 1.2.3.4
567  
568 See "STRINGIFICATION" below.
569  
570 C<$addr> can be almost anything that can be resolved to an IP address
571 in all the notations I have seen over time. It can optionally contain
572 the mask in CIDR notation. If the OPTIONAL perl module Socket6 is
573 available in the local library it will autoload and ipV6 host6
574 names will be resolved as well as ipV4 hostnames.
575  
576 B<prefix> notation is understood, with the limitation that the range
577 specified by the prefix must match with a valid subnet.
578  
579 Addresses in the same format returned by C<inet_aton> or
580 C<gethostbyname> can also be understood, although no mask can be
581 specified for them. The default is to not attempt to recognize this
582 format, as it seems to be seldom used.
583  
584 ###### DEPRECATED, will be remove in version 5 ############
585 To accept addresses in that format, invoke the module as in
586  
587 use NetAddr::IP::Lite ':aton'
588  
589 ###### USE new_from_aton instead ##########################
590  
591 If called with no arguments, 'default' is assumed.
592  
593 If called with an empty string as the argument, returns 'undef'
594  
595 C<$addr> can be any of the following and possibly more...
596  
597 n.n
598 n.n/mm
599 n.n mm
600 n.n.n
601 n.n.n/mm
602 n.n.n mm
603 n.n.n.n
604 n.n.n.n/mm 32 bit cidr notation
605 n.n.n.n mm
606 n.n.n.n/m.m.m.m
607 n.n.n.n m.m.m.m
608 loopback, localhost, broadcast, any, default
609 x.x.x.x/host
610 0xABCDEF, 0b111111000101011110, (or a bcd number)
611 a netaddr as returned by 'inet_aton'
612  
613  
614 Any RFC1884 notation
615  
616 ::n.n.n.n
617 ::n.n.n.n/mmm 128 bit cidr notation
618 ::n.n.n.n/::m.m.m.m
619 ::x:x
620 ::x:x/mmm
621 x:x:x:x:x:x:x:x
622 x:x:x:x:x:x:x:x/mmm
623 x:x:x:x:x:x:x:x/m:m:m:m:m:m:m:m any RFC1884 notation
624 loopback, localhost, unspecified, any, default
625 ::x:x/host
626 0xABCDEF, 0b111111000101011110 within the limits
627 of perl's number resolution
628 123456789012 a 'big' bcd number (bigger than perl likes)
629 and Math::BigInt
630  
631 A Fully Qualified Domain Name which returns an ipV4 address or an ipV6
632 address, embodied in that order. This previously undocumented feature
633 may be disabled with:
634  
635 use NetAddr::IP::Lite ':nofqdn';
636  
637 If called with no arguments, 'default' is assumed.
638  
639 If called with and empty string as the argument, 'undef' is returned;
640  
641 =cut
642  
643 104 my $lbmask = inet_aton('255.0.0.0');
644 104 my $_p4broad = inet_any2n('255.255.255.255');
645 104 my $_p4loop = inet_any2n('127.0.0.1');
646 104 my $_p4mloop = inet_aton('255.0.0.0');
647 104 $_p4mloop = mask4to6($_p4mloop);
648 104 my $_p6loop = inet_any2n('::1');
649  
650 104 my %fip4 = (
651 default => Zeros,
652 any => Zeros,
653 broadcast => $_p4broad,
654 loopback => $_p4loop,
655 unspecified => undef,
656 );
657 104 my %fip4m = (
658 default => Zeros,
659 any => Zeros,
660 broadcast => Ones,
661 loopback => $_p4mloop,
662 unspecified => undef, # not applicable for ipV4
663 host => Ones,
664 );
665  
666 104 my %fip6 = (
667 default => Zeros,
668 any => Zeros,
669 broadcast => undef, # not applicable for ipV6
670 loopback => $_p6loop,
671 unspecified => Zeros,
672 );
673  
674 104 my %fip6m = (
675 default => Zeros,
676 any => Zeros,
677 broadcast => undef, # not applicable for ipV6
678 loopback => Ones,
679 unspecified => Ones,
680 host => Ones,
681 );
682  
683 104 my $ff000000 = pack('L3N',0xffffffff,0xffffffff,0xffffffff,0xFF000000);
684 104 my $ffff0000 = pack('L3N',0xffffffff,0xffffffff,0xffffffff,0xFFFF0000);
685 104 my $ffffff00 = pack('L3N',0xffffffff,0xffffffff,0xffffffff,0xFFFFFF00);
686  
687 10 ▶sub _obits ($$) {
Subroutine _obits: called CC=2 SCAR=7.1
688 10 my($lo,$hi) = @_;
689  
690 10 ▶ return 0xFF if $lo == $hi;
Branch: 1 branch
BranchTrueFalseTotal
if $lo == $hi 0 10 10
691 10 return (~ ($hi ^ $lo)) & 0xFF;
692 }
693  
694 36 ▶sub new_no($;$$) {
Subroutine new_no: called CC=1 SCAR=0.0
Pod documented
695 36 unshift @_, -1;
696 36 goto &_xnew;
697 }
698  
699 8747 ▶sub new($;$$) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
700 8747 unshift @_, 0;
701 8747 goto &_xnew;
702 }
703  
704 6 ▶sub new_from_aton($$) {
Subroutine new_from_aton: called CC=5 SCAR=16.4
Pod documented
705 6 my $proto = shift;
706 6 ▶ my $class = ref $proto || $proto || __PACKAGE__;
Condition40%2/5
ref $proto || $proto || __PACKAGE__
l!l
60
ref $proto or $proto
l!l&&r!l&&!r
060
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
707 6 my $ip = shift;
708 6 ▶ return undef unless defined $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 1 5 6
709 5 my $addrlen = length($ip);
710 5 ▶ return undef unless $addrlen == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless $addrlen == 4 2 3 5
711 3 my $self = {
712 addr => ipv4to6($ip),
713 mask => &Ones,
714 isv6 => 0,
715 };
716 3 return bless $self, $class;
717 }
718  
719 10 ▶sub new6($;$$) {
Subroutine new6: called CC=1 SCAR=0.0
Pod documented
720 10 unshift @_, 1;
721 10 goto &_xnew;
722 }
723  
724 1 ▶sub new6FFFF($;$$) {
Subroutine new6FFFF: called CC=1 SCAR=0.0
Pod documented
725 1 my $ip = _xnew(1,@_);
726 1 $ip->{addr} |= $_ipv4FFFF;
727 1 return $ip;
728 }
729  
730 129 ▶sub new_cis($;$$) {
Subroutine new_cis: called CC=3 SCAR=11.0
Pod documented
731 129 my @in = @_;
732 129 ▶ if ( $in[1] && $in[1] =~ m!^(.+)\s+(.+)$! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in[1] and $in[1] =~ /^(.+)\s+(.+)$/) 122 7 129
Condition66%2/3
$in[1] and $in[1] =~ /^(.+)\s+(.+)$/
!ll&&!rl&&r
07122
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
733 122 $in[1] = $1 .'/'. $2;
734 }
735 129 @_ = (0,@in);
736 129 goto &_xnew;
737 }
738  
739 24 ▶sub new_cis6($;$$) {
Subroutine new_cis6: called CC=3 SCAR=11.0
Pod documented
740 24 my @in = @_;
741 24 ▶ if ( $in[1] && $in[1] =~ m!^(.+)\s+(.+)$! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in[1] and $in[1] =~ /^(.+)\s+(.+)$/) 17 7 24
Condition66%2/3
$in[1] and $in[1] =~ /^(.+)\s+(.+)$/
!ll&&!rl&&r
0717
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
742 17 $in[1] = $1 .'/'. $2;
743 }
744 24 @_ = (1,@in);
745 24 goto &_xnew;
746 }
747  
748 40 ▶sub _no_octal {
Subroutine _no_octal: called CC=1 SCAR=0.0
749 # $_[0] =~ m/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
750 # return sprintf("%d.%d.%d.%d",$1,$2,$3,$4);
751 40 (my $rv = $_[0]) =~ s#\b0*([1-9]\d*/?|0/?)#$1#g; # suppress leading zeros
752 40 $rv;
753 }
754  
755 8947 ▶sub _xnew($$;$$) {
Subroutine _xnew: called CC=132 SCAR=57.5
756 8947 my $noctal = 0;
757 8947 my $isV6 = shift;
758 8947 ▶ if ($isV6 < 0) { # flag for no octal?
Branch: 1 branch
BranchTrueFalseTotal
if ($isV6 < 0) 36 8911 8947
759 36 $isV6 = 0;
760 36 $noctal = 1;
761 }
762 8947 my $proto = shift;
763 8947 ▶ my $class = ref $proto || $proto || __PACKAGE__;
Condition40%2/5
ref $proto || $proto || __PACKAGE__
l!l
89470
ref $proto or $proto
l!l&&r!l&&!r
089470
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
764 8947 my $ip = shift;
765  
766 8947 ▶ 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/. -])]) 36 8911 8947
Condition83%5/6
$ip and $noctal and not $ip =~ m[(?:[^\s0123456789/. -])]
!ll&&!rl&&r
8911036
$ip and $noctal
!ll&&!rl&&r
7890436
MC/DC66%2/3
$ip and $noctal and not $ip =~ m[(?:[^\s0123456789/. -])]
$ip $noctal not $ip =~ m[(?:[^\s0123456789/. -])]
Truth table75%3/4
A and B and not(C)
A= $ipB= $noctalC= not $ip =~ m[(?:[^\s0123456789/. -])]
ABCresult
0XX0
10X0
1100
1111
767 36 $ip = _no_octal($ip);
768 }
769  
770 # fix for bug #75976
771 8947 ▶ return undef if defined $ip && $ip eq '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $ip and $ip eq "" 2 8945 8947
Condition100%3/3
defined $ip and $ip eq ""
!ll&&!rl&&r
289432
MC/DC100%2/2
defined $ip and $ip eq ""
defined $ip $ip eq ""
Truth table100%3/3
A and B
A= defined $ipB= $ip eq ""
ABresult
0X0
100
111
772  
773 8945 ▶ $ip = 'default' unless defined $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 2 8943 8945
774 8945 ▶ $ip = _retMBIstring($ip) # treat as big bcd string
Branch: 1 branch
BranchTrueFalseTotal
if ref $ip and ref $ip eq "Math::BigInt" 49 8896 8945
Condition66%2/3
ref $ip and ref $ip eq "Math::BigInt"
!ll&&!rl&&r
8896049
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
775 if ref $ip && ref $ip eq 'Math::BigInt'; # can /CIDR notation
776 8945 my $hasmask = 1;
777 8945 my($mask,$tmp);
778  
779 # IP to lower case AFTER ref test for Math::BigInt. 'lc' strips blessing
780  
781 8945 $ip = lc $ip;
782  
783 8945 while (1) {
784 # process IP's with no CIDR or that have the CIDR as part of the IP argument string
785 8945 ▶ unless (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 809 8136 8945
if (do { defined $_[0] }) 808 1 809
786 # if ($ip =~ m!^(.+)/(.+)$!) {
787 8136 ▶ if ($ip !~ /\D/) { # binary number notation
Branch: 3 branches
BranchTrueFalseTotal
if (not $ip =~ /\D/) { } 68 8068 8136
elsif ($ip =~ m[^([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)$] or $ip =~ m(^[\[]{1}([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)[\]]{1}$)) { } 6647 1421 8068
elsif (grep(($ip eq $_), ('default', 'any', 'broadcast', 'loopback', 'unspecified'))) { } 30 1391 1421
788 68 $ip = bcd2bin($ip);
789 68 $mask = Ones;
790 68 last;
791 }
792 ▶ 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(^[\[]{1}([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)[\]]{1}$)
l!l&&r!l&&!r
6603441421
MC/DC100%2/2
$ip =~ m[^([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)$] or $ip =~ m(^[\[]{1}([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)[\]]{1}$)
$ip =~ m[^([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)$] $ip =~ m(^[\[]{1}([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)[\]]{1}$)
Truth table100%3/3
A or B
A= $ip =~ m[^([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)$]B= $ip =~ m(^[\[]{1}([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)[\]]{1}$)
ABresult
1X1
011
000
793 $ip =~ m!^[\[]{1}([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)[\]]{1}$!) {
794 6647 $ip = $1;
795 6647 $mask = $2;
796 } elsif (grep($ip eq $_,(qw(default any broadcast loopback unspecified)))) {
797 30 ▶ $isV6 = 1 if $ip eq 'unspecified';
Branch: 1 branch
BranchTrueFalseTotal
if $ip eq "unspecified" 0 30 30
798 30 ▶ if ($isV6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isV6) { } 1 29 30
799 1 $mask = $fip6m{$ip};
800 1 ▶ return undef unless defined ($ip = $fip6{$ip});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($ip = $fip6{$ip}) 0 1 1
801 } else {
802 29 $mask = $fip4m{$ip};
803 29 ▶ return undef unless defined ($ip = $fip4{$ip});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($ip = $fip4{$ip}) 0 29 29
804 }
805 30 last;
806 }
807 }
808 # process "ipv6" token and default IP's
809 0 elsif (defined $_[0]) {
810 808 ▶ if ($_[0] =~ /ipv6/i || $isV6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /ipv6/i or $isV6) 2 806 808
Condition66%2/3
$_[0] =~ /ipv6/i or $isV6
l!l&&r!l&&!r
02806
MC/DC50%1/2
$_[0] =~ /ipv6/i or $isV6
$_[0] =~ /ipv6/i $isV6
Truth table66%2/3
A or B
A= $_[0] =~ /ipv6/iB= $isV6
ABresult
1X1
011
000
811 2 ▶ if (grep($ip eq $_,(qw(default any loopback unspecified)))) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($ip eq $_), ('default', 'any', 'loopback', 'unspecified'))) { } 0 2 2
812 0 $mask = $fip6m{$ip};
813 0 $ip = $fip6{$ip};
814 0 last;
815 } else {
816 2 ▶ return undef unless $isV6;
Branch: 1 branch
BranchTrueFalseTotal
unless $isV6 0 2 2
817 # add for ipv6 notation "12345, 1"
818 }
819 # $mask = lc $_[0];
820 # } else {
821 # $mask = lc $_[0];
822 }
823 # extract mask
824 808 $mask = $_[0];
825 }
826 ###
827 ### process mask
828 8847 ▶ unless (defined $mask) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mask) 1392 7455 8847
829 1392 $hasmask = 0;
830 1392 $mask = 'host';
831 }
832  
833 # two kinds of IP's can turn on the isV6 flag
834 # 1) big digits that are over the IPv4 boundry
835 # 2) IPv6 IP syntax
836 #
837 # check these conditions and set isV6 as appropriate
838 #
839 8847 my $try;
840 8847 ▶ $isV6 = 1 if # check big bcd and IPv6 rfc1884
Branch: 1 branch
BranchTrueFalseTotal
if not $ip =~ /\D/ and length $ip > 3 || $ip > 255 and $try = &bcd2bin($ip) and not isIPv4($try) or index($ip, ":") != -1 and $try = ipv6_aton($ip) 855 7992 8847
Condition88%16/18
not $ip =~ /\D/ and length $ip > 3 || $ip > 255 and $try = &bcd2bin($ip) and not isIPv4($try) or index($ip, ":") != -1 and $try = ipv6_aton($ip)
l!l&&r!l&&!r
358207992
not $ip =~ /\D/ and length $ip > 3 || $ip > 255 and $try = &bcd2bin($ip) and not isIPv4($try)
!ll&&!rl&&r
87347835
not $ip =~ /\D/ and length $ip > 3 || $ip > 255 and $try = &bcd2bin($ip)
!ll&&!rl&&r
87340113
not $ip =~ /\D/ and length $ip > 3 || $ip > 255
!ll&&!rl&&r
8590144113
length $ip > 3 or $ip > 255
l!l&&r!l&&!r
1130144
index($ip, ":") != -1 and $try = ipv6_aton($ip)
!ll&&!rl&&r
798012820
MC/DC71%5/7
not $ip =~ /\D/ and length $ip > 3 || $ip > 255 and $try = &bcd2bin($ip) and not isIPv4($try) or index($ip, ":") != -1 and $try = ipv6_aton($ip)
not $ip =~ /\D/ length $ip > 3 $ip > 255 $try = &bcd2bin($ip) not isIPv4($try) index($ip, ":") != -1 $try = ipv6_aton($ip)
Truth table30%6/20
not(A) and B or C and D and not(E) or F and G
A= not $ip =~ /\D/B= length $ip > 3C= $ip > 255D= $try = &bcd2bin($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
841 ( $ip !~ /\D/ && # ip is all decimal
842 (length($ip) > 3 || $ip > 255) && # exclude a single digit in the range of zero to 255, could be funny IPv4
843 ($try = bcd2bin($ip)) && ! isIPv4($try)) || # precedence so $try is not corrupted
844 (index($ip,':') >= 0 && ($try = ipv6_aton($ip))); # fails if not an rfc1884 address
845  
846 # if either of the above conditions is true, $try contains the NetAddr 128 bit address
847  
848 # checkfor Math::BigInt mask
849 8847 ▶ $mask = _retMBIstring($mask) # treat as big bcd string
Branch: 1 branch
BranchTrueFalseTotal
if ref $mask and ref $mask eq "Math::BigInt" 39 8808 8847
Condition66%2/3
ref $mask and ref $mask eq "Math::BigInt"
!ll&&!rl&&r
8808039
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
850 if ref $mask && ref $mask eq 'Math::BigInt';
851  
852 # MASK to lower case AFTER ref test for Math::BigInt, 'lc' strips blessing
853  
854 8847 $mask = lc $mask;
855  
856 8847 ▶ if ($mask !~ /\D/) { # bcd or CIDR notation
Branch: 3 branches
BranchTrueFalseTotal
if (not $mask =~ /\D/) { } 7336 1511 8847
elsif ($mask =~ /^\d+\.\d+\.\d+\.\d+$/) { } 84 1427 1511
elsif (grep(($mask eq $_), ('default', 'any', 'broadcast', 'loopback', 'unspecified', 'host'))) { } 1422 5 1427
857 7336 ▶ my $isCIDR = length($mask) < 4 && $mask < 129;
Condition100%3/3
length $mask < 4 && $mask < 129
!ll&&!rl&&r
6927265
MC/DC100%2/2
length $mask < 4 && $mask < 129
length $mask < 4 $mask < 129
Truth table100%3/3
A && B
A= length $mask < 4B= $mask < 129
ABresult
0X0
100
111
858 7336 ▶ if ($isV6) {
Branch: 2 branches
BranchTrueFalseTotal
if ($isV6) { } 464 6872 7336
elsif ($isCIDR and $mask < 33) { } 6833 39 6872
859 464 ▶ if ($isCIDR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isCIDR) { } 432 32 464
860 432 my($dq1,$dq2,$dq3,$dq4);
861 432 ▶ if ($ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ &&
Branch: 2 branches
BranchTrueFalseTotal
if ($ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ 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) { } 2 430 432
elsif ($mask < 128) { } 416 14 430
Condition70%19/27
$ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ 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
43002
$ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ 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
43002
$ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ 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
43002
$ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ 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
43002
$ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ 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
43002
$ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ 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
43002
$ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 } and $dq1 >= 0 and $dq1 < 256
!ll&&!rl&&r
42282
$ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 } and $dq1 >= 0
!ll&&!rl&&r
422010
$ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ and do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 }
!ll&&!rl&&r
422010
MC/DC20%2/10
$ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ 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
$ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ 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 table27%3/11
A and B and C and D and E and F and G and H and I and J
A= $ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/B= do { $dq1 = $1; $dq2 = $2 || 0; $dq3 = $3 || 0; $dq4 = $4 || 0; 1 }C= $dq1 >= 0D= $dq1 < 256E= $dq2 >= 0F= $dq2 < 256G= $dq3 >= 0H= $dq3 < 256I= $dq4 >= 0J= $dq4 < 256
ABCDEFGHIJresult
0XXXXXXXXX0
10XXXXXXXX0
110XXXXXXX0
1110XXXXXX0
11110XXXXX0
111110XXXX0
1111110XXX0
11111110XX0
111111110X0
11111111100
11111111111
862 10 do {$dq1 = $1;
863 10 ▶ $dq2 = $2 || 0;
Condition50%1/2
$2 || 0
l!l
010
MC/DC0%0/1
$2 || 0
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
864 10 ▶ $dq3 = $3 || 0;
Condition50%1/2
$3 || 0
l!l
010
MC/DC0%0/1
$3 || 0
$3
Truth table50%1/2
A
A= $3
Aresult
11
00
865 10 ▶ $dq4 = $4 || 0;
Condition50%1/2
$4 || 0
l!l
010
MC/DC0%0/1
$4 || 0
$4
Truth table50%1/2
A
A= $4
Aresult
11
00
866 10 1;
867 } &&
868 $dq1 >= 0 && $dq1 < 256 &&
869 $dq2 >= 0 && $dq2 < 256 &&
870 $dq3 >= 0 && $dq3 < 256 &&
871 $dq4 >= 0 && $dq4 < 256
872 ) { # corner condition of IPv4 with isV6
873 2 $ip = join('.',$dq1,$dq2,$dq3,$dq4);
874 2 $try = ipv4to6(inet_aton($ip));
875 2 ▶ if ($mask < 32) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mask < 32) { } 2 0 2
elsif ($mask == 32) { } 0 0 0
876 2 $mask = shiftleft(Ones,32 -$mask);
877 }
878 elsif ($mask == 32) {
879 0 $mask = Ones;
880 } else {
881 0 return undef; # undoubtably an error
882 }
883 }
884 elsif ($mask < 128) {
885 416 $mask = shiftleft(Ones,128 -$mask); # small cidr
886 } else {
887 14 $mask = Ones();
888 }
889 } else {
890 32 $mask = bcd2bin($mask);
891 }
892 }
893 ▶ elsif ($isCIDR && $mask < 33) { # is V4
Condition66%2/3
$isCIDR and $mask < 33
!ll&&!rl&&r
3906833
MC/DC50%1/2
$isCIDR and $mask < 33
$isCIDR $mask < 33
Truth table66%2/3
A and B
A= $isCIDRB= $mask < 33
ABresult
0X0
100
111
894 # if ($ip && $noctal && $ip !~ m|(?:[^\s0123456789.])|) { # octal suppression required if not an IPv4 address
895 # $mask = _no_octal($mask);
896 # }
897 6833 ▶ if ($mask < 32) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mask < 32) { } 6771 62 6833
elsif ($mask == 32) { } 62 0 62
898 6771 $mask = shiftleft(Ones,32 -$mask);
899 }
900 elsif ( $mask == 32) {
901 62 $mask = Ones;
902 } else {
903 0 $mask = bcd2bin($mask);
904 0 $mask |= $_v4mask; # v4 always
905 }
906 } else { # also V4
907 39 $mask = bcd2bin($mask);
908 39 $mask |= $_v4mask;
909 }
910 7336 ▶ if ($try) { # is a big number
Branch: 1 branch
BranchTrueFalseTotal
if ($try) 523 6813 7336
911 523 $ip = $try;
912 523 last;
913 }
914 } elsif ($mask =~ m/^\d+\.\d+\.\d+\.\d+$/) { # ipv4 form of mask
915 84 ▶ $mask = _no_octal($mask) if $noctal; # filter for octal
Branch: 1 branch
BranchTrueFalseTotal
if $noctal 0 84 84
916 84 ▶ return undef unless defined ($mask = inet_aton($mask));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($mask = inet_aton($mask)) 0 84 84
917 84 $mask = mask4to6($mask);
918 } elsif (grep($mask eq $_,qw(default any broadcast loopback unspecified host))) {
919 1422 ▶ if (index($ip,':') < 0 && ! $isV6) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($ip, ':') == -1 and not $isV6) { } 1017 405 1422
Condition100%3/3
index($ip, ":") == -1 and not $isV6
!ll&&!rl&&r
40411017
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
920 1017 ▶ return undef unless defined ($mask = $fip4m{$mask});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($mask = $fip4m{$mask}) 0 1017 1017
921 } else {
922 405 ▶ return undef unless defined ($mask = $fip6m{$mask});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($mask = $fip6m{$mask}) 0 405 405
923 }
924 } else {
925 5 ▶ return undef unless defined ($mask = ipv6_aton($mask)); # try ipv6 form of mask
Branch: 1 branch
BranchTrueFalseTotal
unless defined($mask = ipv6_aton($mask)) 2 3 5
926 }
927  
928 # process remaining IP's
929  
930 8322 ▶ if (index($ip,':') < 0) { # ipv4 address
Branch: 1 branch
BranchTrueFalseTotal
if (index($ip, ':') == -1) { } 7915 407 8322
931 7915 ▶ if ($ip =~ m/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) {
Branch: 20 branches
BranchTrueFalseTotal
if ($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) { } 7499 416 7915
elsif (grep(($ip eq $_), ('default', 'any', 'broadcast', 'loopback'))) { } 65 351 416
elsif ($ip =~ /^(\d+)\.(\d+)$/) { } 8 343 351
elsif ($ip =~ /^(\d+)\.(\d+)\.(\d+)$/) { } 9 334 343
elsif ($ip =~ /^(\d+)$/ and $hasmask and $1 >= 0 and $1 < 256) { } 143 191 334
elsif ($ip =~ /^\d+$/) { } 16 175 191
elsif ($ip =~ /^0[xb]\d+$/ and $hasmask and ($tmp = eval "$ip") || 1 and $tmp >= 0 and $tmp < 256) { } 0 175 175
elsif ($ip =~ /^-?\d+$/) { } 30 145 175
elsif ($ip =~ /^-?0[xb]\d+$/) { } 0 145 145
elsif ($ip =~ /^(\d+)\.$/) { } 3 142 145
elsif ($ip =~ /^(\d+)\.(\d+)-(\d+)\.?$/ and $2 <= $3 and $3 < 256) { } 0 142 142
elsif ($ip =~ /^(\d+)-(\d+)\.?$/ and ($1 <= $2 and $2 < 256)) { } 8 134 142
elsif ($ip =~ /^(\d+)\.(\d+)\.$/) { } 2 132 134
elsif ($ip =~ /^(\d+)\.(\d+)\.(\d+)-(\d+)\.?$/ and $3 <= $4 and $4 < 256) { } 2 130 132
elsif ($ip =~ /^(\d+)\.(\d+)\.(\d+)\.$/) { } 2 128 130
elsif ($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)-(\d+)$/ and $4 <= $5 and $5 < 256) { } 0 128 128
elsif ($ip =~ /^(\d+\.\d+\.\d+\.\d+) \s*-\s*(\d+\.\d+\.\d+\.\d+)$/x) { } 69 59 128
elsif (not $NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/ and $tmp = gethostbyname fillIPv4($ip) and $tmp ne $_v4zero and $tmp ne $_zero) { } 36 23 59
elsif (not $NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/ and havegethostbyname2() and $tmp = naip_gethostbyname($ip)) { } 0 23 23
elsif ($NetAddr::IP::Lite::Accept_Binary_IP and not $hasmask) { } 12 11 23
932 ; # the common case
933 }
934 elsif (grep($ip eq $_,(qw(default any broadcast loopback)))) {
935 65 ▶ return undef unless defined ($ip = $fip4{$ip});
Branch: 1 branch
BranchTrueFalseTotal
unless defined($ip = $fip4{$ip}) 0 65 65
936 65 last;
937 }
938 elsif ($ip =~ m/^(\d+)\.(\d+)$/) {
939 8 ▶ $ip = ($hasmask)
Branch: 1 branch
BranchTrueFalseTotal
$hasmask ? : 3 5 8
940 ? "${1}.${2}.0.0"
941 : "${1}.0.0.${2}";
942 }
943 elsif ($ip =~ m/^(\d+)\.(\d+)\.(\d+)$/) {
944 9 ▶ $ip = ($hasmask)
Branch: 1 branch
BranchTrueFalseTotal
$hasmask ? : 7 2 9
945 ? "${1}.${2}.${3}.0"
946 : "${1}.${2}.0.${3}";
947 }
948 ▶ elsif ($ip =~ /^(\d+)$/ && $hasmask && $1 >= 0 and $1 < 256) { # pure numeric
Condition77%7/9
$ip =~ /^(\d+)$/ and $hasmask and $1 >= 0 and $1 < 256
!ll&&!rl&&r
17516143
$ip =~ /^(\d+)$/ and $hasmask and $1 >= 0
!ll&&!rl&&r
1750159
$ip =~ /^(\d+)$/ and $hasmask
!ll&&!rl&&r
1750159
MC/DC50%2/4
$ip =~ /^(\d+)$/ and $hasmask and $1 >= 0 and $1 < 256
$ip =~ /^(\d+)$/ $hasmask $1 >= 0 $1 < 256
Truth table60%3/5
A and B and C and D
A= $ip =~ /^(\d+)$/B= $hasmaskC= $1 >= 0D= $1 < 256
ABCDresult
0XXX0
10XX0
110X0
11100
11111
949 143 $ip = sprintf("%d.0.0.0",$1);
950 }
951 # elsif ($ip =~ /^\d+$/ && !$hasmask) { # a big integer
952 elsif ($ip =~ /^\d+$/ ) { # a big integer
953 16 $ip = bcd2bin($ip);
954 16 last;
955 }
956 # these next three might be broken??? but they have been in the code a long time and no one has complained
957 ▶ elsif ($ip =~ /^0[xb]\d+$/ && $hasmask &&
Condition28%4/14
$ip =~ /^0[xb]\d+$/ and $hasmask and ($tmp = eval "$ip") || 1 and $tmp >= 0 and $tmp < 256
!ll&&!rl&&r
17500
$ip =~ /^0[xb]\d+$/ and $hasmask and ($tmp = eval "$ip") || 1 and $tmp >= 0
!ll&&!rl&&r
17500
$ip =~ /^0[xb]\d+$/ and $hasmask and ($tmp = eval "$ip") || 1
!ll&&!rl&&r
17500
$ip =~ /^0[xb]\d+$/ and $hasmask
!ll&&!rl&&r
17500
$tmp = eval "$ip" or 1
l!l
00
MC/DC0%0/5
$ip =~ /^0[xb]\d+$/ and $hasmask and ($tmp = eval "$ip") || 1 and $tmp >= 0 and $tmp < 256
$ip =~ /^0[xb]\d+$/ $hasmask $tmp = eval "$ip" $tmp >= 0 $tmp < 256
Truth table16%1/6
A and B and C and D and E
A= $ip =~ /^0[xb]\d+$/B= $hasmaskC= $tmp = eval "$ip"D= $tmp >= 0E= $tmp < 256
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
958 (($tmp = eval "$ip") || 1) &&
959 $tmp >= 0 && $tmp < 256) {
960 0 $ip = sprintf("%d.0.0.0",$tmp);
961 }
962 elsif ($ip =~ /^-?\d+$/) {
963 30 ▶ $ip += 2 ** 32 if $ip < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ip < 0 30 0 30
964 30 $ip = pack('L3N',0,0,0,$ip);
965 30 last;
966 }
967 elsif ($ip =~ /^-?0[xb]\d+$/) {
968 0 $ip = eval "$ip";
969 0 $ip = pack('L3N',0,0,0,$ip);
970 0 last;
971 }
972  
973 # notations below include an implicit mask specification
974  
975 elsif ($ip =~ m/^(\d+)\.$/) {
976 3 $ip = "${1}.0.0.0";
977 3 $mask = $ff000000;
978 }
979 ▶ elsif ($ip =~ m/^(\d+)\.(\d+)-(\d+)\.?$/ && $2 <= $3 && $3 < 256) {
Condition33%2/6
$ip =~ /^(\d+)\.(\d+)-(\d+)\.?$/ and $2 <= $3 and $3 < 256
!ll&&!rl&&r
14200
$ip =~ /^(\d+)\.(\d+)-(\d+)\.?$/ and $2 <= $3
!ll&&!rl&&r
14200
MC/DC0%0/3
$ip =~ /^(\d+)\.(\d+)-(\d+)\.?$/ and $2 <= $3 and $3 < 256
$ip =~ /^(\d+)\.(\d+)-(\d+)\.?$/ $2 <= $3 $3 < 256
Truth table25%1/4
A and B and C
A= $ip =~ /^(\d+)\.(\d+)-(\d+)\.?$/B= $2 <= $3C= $3 < 256
ABCresult
0XX0
10X0
1100
1111
980 0 $ip = "${1}.${2}.0.0";
981 0 $mask = pack('L3C4',0xffffffff,0xffffffff,0xffffffff,255,_obits($2,$3),0,0);
982 }
983 ▶ elsif ($ip =~ m/^(\d+)-(\d+)\.?$/ and $1 <= $2 && $2 < 256) {
Condition50%3/6
$ip =~ /^(\d+)-(\d+)\.?$/ and ($1 <= $2 and $2 < 256)
!ll&&!rl&&r
13408
$1 <= $2 and $2 < 256
!ll&&!rl&&r
008
MC/DC33%1/3
$ip =~ /^(\d+)-(\d+)\.?$/ and ($1 <= $2 and $2 < 256)
$ip =~ /^(\d+)-(\d+)\.?$/ $1 <= $2 $2 < 256
Truth table50%2/4
A and B and C
A= $ip =~ /^(\d+)-(\d+)\.?$/B= $1 <= $2C= $2 < 256
ABCresult
0XX0
10X0
1100
1111
984 8 $ip = "${1}.0.0.0";
985 8 $mask = pack('L3C4',0xffffffff,0xffffffff,0xffffffff,_obits($1,$2),0,0,0)
986 }
987 elsif ($ip =~ m/^(\d+)\.(\d+)\.$/) {
988 2 $ip = "${1}.${2}.0.0";
989 2 $mask = $ffff0000;
990 }
991 ▶ elsif ($ip =~ m/^(\d+)\.(\d+)\.(\d+)-(\d+)\.?$/ && $3 <= $4 && $4 < 256) {
Condition66%4/6
$ip =~ /^(\d+)\.(\d+)\.(\d+)-(\d+)\.?$/ and $3 <= $4 and $4 < 256
!ll&&!rl&&r
13002
$ip =~ /^(\d+)\.(\d+)\.(\d+)-(\d+)\.?$/ and $3 <= $4
!ll&&!rl&&r
13002
MC/DC33%1/3
$ip =~ /^(\d+)\.(\d+)\.(\d+)-(\d+)\.?$/ and $3 <= $4 and $4 < 256
$ip =~ /^(\d+)\.(\d+)\.(\d+)-(\d+)\.?$/ $3 <= $4 $4 < 256
Truth table50%2/4
A and B and C
A= $ip =~ /^(\d+)\.(\d+)\.(\d+)-(\d+)\.?$/B= $3 <= $4C= $4 < 256
ABCresult
0XX0
10X0
1100
1111
992 2 $ip = "${1}.${2}.${3}.0";
993 2 $mask = pack('L3C4',0xffffffff,0xffffffff,0xffffffff,255,255,_obits($3,$4),0);
994 }
995 elsif ($ip =~ m/^(\d+)\.(\d+)\.(\d+)\.$/) {
996 2 $ip = "${1}.${2}.${3}.0";
997 2 $mask = $ffffff00;
998 }
999 ▶ elsif ($ip =~ m/^(\d+)\.(\d+)\.(\d+)\.(\d+)-(\d+)$/ && $4 <= $5 && $5 < 256) {
Condition33%2/6
$ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)-(\d+)$/ and $4 <= $5 and $5 < 256
!ll&&!rl&&r
12800
$ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)-(\d+)$/ and $4 <= $5
!ll&&!rl&&r
12800
MC/DC0%0/3
$ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)-(\d+)$/ and $4 <= $5 and $5 < 256
$ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)-(\d+)$/ $4 <= $5 $5 < 256
Truth table25%1/4
A and B and C
A= $ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)-(\d+)$/B= $4 <= $5C= $5 < 256
ABCresult
0XX0
10X0
1100
1111
1000 0 $ip = "${1}.${2}.${3}.${4}";
1001 0 $mask = pack('L3C4',0xffffffff,0xffffffff,0xffffffff,255,255,255,_obits($4,$5));
1002 }
1003 elsif ($ip =~ m/^(\d+\.\d+\.\d+\.\d+)
1004 \s*-\s*(\d+\.\d+\.\d+\.\d+)$/x) {
1005 # if ($noctal) {
1006 # return undef unless ($ip = inet_aton(_no_octal($1)));
1007 # return undef unless ($tmp = inet_aton(_no_octal($2)));
1008 # } else {
1009 69 ▶ return undef unless ($ip = inet_aton($1));
Branch: 1 branch
BranchTrueFalseTotal
unless $ip = inet_aton($1) 0 69 69
1010 69 ▶ return undef unless ($tmp = inet_aton($2));
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp = inet_aton($2) 0 69 69
1011 # }
1012 # check for left side greater than right side
1013 # save numeric difference in $mask
1014 69 ▶ return undef if ($tmp = unpack('N',$tmp) - unpack('N',$ip)) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp = unpack("N", $tmp) - unpack("N", $ip)) < 0 0 69 69
1015 69 $ip = ipv4to6($ip);
1016 69 $tmp = pack('L3N',0,0,0,$tmp);
1017 69 $mask = ~$tmp;
1018 69 ▶ return undef if notcontiguous($mask);
Branch: 1 branch
BranchTrueFalseTotal
if &notcontiguous($mask) 61 8 69
1019 # check for non-aligned left side
1020 8 ▶ return undef if hasbits($ip & $tmp);
Branch: 1 branch
BranchTrueFalseTotal
if &hasbits($ip & $tmp) 0 8 8
1021 8 last;
1022 }
1023 # check for resolvable IPv4 hosts
1024 ▶ elsif (! $NoFQDN && $ip !~ /[^a-zA-Z0-9\._-]/ && ($tmp = gethostbyname(fillIPv4($ip))) && $tmp ne $_v4zero && $tmp ne $_zero ) {
Condition83%10/12
not $NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/ and $tmp = gethostbyname fillIPv4($ip) and $tmp ne $_v4zero and $tmp ne $_zero
!ll&&!rl&&r
23036
not $NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/ and $tmp = gethostbyname fillIPv4($ip) and $tmp ne $_v4zero
!ll&&!rl&&r
23036
not $NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/ and $tmp = gethostbyname fillIPv4($ip)
!ll&&!rl&&r
14936
$NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/
l!l&&r!l&&!r
11345
MC/DC60%3/5
not $NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/ and $tmp = gethostbyname fillIPv4($ip) and $tmp ne $_v4zero and $tmp ne $_zero
$NetAddr::IP::Lite::NoFQDN $ip =~ /[^a-zA-Z0-9\._-]/ $tmp = gethostbyname fillIPv4($ip) $tmp ne $_v4zero $tmp ne $_zero
Truth table66%4/6
not(A || B) and C and D and E
A= $NetAddr::IP::Lite::NoFQDNB= $ip =~ /[^a-zA-Z0-9\._-]/C= $tmp = gethostbyname fillIPv4($ip)D= $tmp ne $_v4zeroE= $tmp ne $_zero
ABCDEresult
1XXXX0
01XXX0
000XX0
0010X0
001100
001111
1025 36 $ip = ipv4to6($tmp);
1026 36 last;
1027 }
1028 # check for resolvable IPv6 hosts
1029 ▶ elsif (! $NoFQDN && $ip !~ /[^a-zA-Z0-9\._-]/ && havegethostbyname2() && ($tmp = naip_gethostbyname($ip))) {
Condition66%6/9
not $NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/ and havegethostbyname2() and $tmp = naip_gethostbyname($ip)
!ll&&!rl&&r
2300
not $NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/ and havegethostbyname2()
!ll&&!rl&&r
1490
$NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/
l!l&&r!l&&!r
1139
MC/DC0%0/4
not $NetAddr::IP::Lite::NoFQDN || $ip =~ /[^a-zA-Z0-9\._-]/ and havegethostbyname2() and $tmp = naip_gethostbyname($ip)
$NetAddr::IP::Lite::NoFQDN $ip =~ /[^a-zA-Z0-9\._-]/ havegethostbyname2() $tmp = naip_gethostbyname($ip)
Truth table60%3/5
not(A || B) and C and D
A= $NetAddr::IP::Lite::NoFQDNB= $ip =~ /[^a-zA-Z0-9\._-]/C= havegethostbyname2()D= $tmp = naip_gethostbyname($ip)
ABCDresult
1XXX0
01XX0
000X0
00100
00111
1030 0 $ip = $tmp;
1031 0 $isV6 = 1;
1032 0 last;
1033 }
1034 ▶ elsif ($Accept_Binary_IP && ! $hasmask) {
Condition66%2/3
$NetAddr::IP::Lite::Accept_Binary_IP and not $hasmask
!ll&&!rl&&r
11012
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
1035 12 ▶ if (length($ip) == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $ip == 4) { } 11 1 12
elsif (length $ip == 16) { } 0 1 1
1036 11 $ip = ipv4to6($ip);
1037 } elsif (length($ip) == 16) {
1038 0 $isV6 = 1;
1039 } else {
1040 1 return undef;
1041 }
1042 11 last;
1043 } else {
1044 11 return undef;
1045 }
1046 7676 ▶ return undef unless defined ($ip = inet_aton($ip));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($ip = inet_aton($ip)) 8 7668 7676
1047 7668 $ip = ipv4to6($ip);
1048 7668 last;
1049 }
1050 ########## continuing
1051 else { # ipv6 address
1052 407 $isV6 = 1;
1053 407 ▶ $ip = $1 if $ip =~ /\[([^\]]+)\]/; # transform URI notation
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /\[([^\]]+)\]/ 11 396 407
1054 407 ▶ if (defined ($tmp = ipv6_aton($ip))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($tmp = ipv6_aton($ip))) 406 1 407
1055 406 $ip = $tmp;
1056 406 last;
1057 }
1058 last if grep($ip eq $_,(qw(default any loopback unspecified))) &&
1059 1 ▶ defined ($ip = $fip6{$ip});
Branch: 1 branch
BranchTrueFalseTotal
if grep(($ip eq $_), ("default", "any", "loopback", "unspecified")) and defined($ip = $fip6{$ip}) 0 1 1
Condition33%1/3
grep(($ip eq $_), ("default", "any", "loopback", "unspecified")) and defined($ip = $fip6{$ip})
!ll&&!rl&&r
100
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
1060 1 return undef;
1061 }
1062 } # end while (1)
1063 8861 ▶ return undef if notcontiguous($mask); # invalid if not contiguous
Branch: 1 branch
BranchTrueFalseTotal
if &notcontiguous($mask) 16 8845 8861
1064  
1065 8845 my $self = {
1066 addr => $ip,
1067 mask => $mask,
1068 isv6 => $isV6,
1069 };
1070 8845 return bless $self, $class;
1071 }
1072  
1073 =item C<-E<gt>broadcast()>
1074  
1075 Returns a new object referring to the broadcast address of a given
1076 subnet. The broadcast address has all ones in all the bit positions
1077 where the netmask has zero bits. This is normally used to address all
1078 the hosts in a given subnet.
1079  
1080 =cut
1081  
1082 8908 ▶sub broadcast ($) {
Subroutine broadcast: called CC=2 SCAR=6.9
Pod documented
1083 8908 my $ip = _new($_[0],$_[0]->{addr} | ~$_[0]->{mask},$_[0]->{mask});
1084 8908 ▶ $ip->{addr} &= V4net unless $ip->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
unless $ip->{"isv6"} 359 8549 8908
1085 8908 return $ip;
1086 }
1087  
1088 =item C<-E<gt>network()>
1089  
1090 Returns a new object referring to the network address of a given
1091 subnet. A network address has all zero bits where the bits of the
1092 netmask are zero. Normally this is used to refer to a subnet.
1093  
1094 =cut
1095  
1096 59568 ▶sub network ($) {
Subroutine network: called CC=1 SCAR=0.0
Pod documented
1097 59568 return _new($_[0],$_[0]->{addr} & $_[0]->{mask},$_[0]->{mask});
1098 }
1099  
1100 =item C<-E<gt>addr()>
1101  
1102 Returns a scalar with the address part of the object as an IPv4 or IPv6 text
1103 string as appropriate. This is useful for printing or for passing the address
1104 part of the NetAddr::IP::Lite object to other components that expect an IP
1105 address. If the object is an ipV6 address or was created using ->new6($ip)
1106 it will be reported in ipV6 hex format otherwise it will be reported in dot
1107 quad format only if it resides in ipV4 address space.
1108  
1109 =cut
1110  
1111 101561 ▶sub addr ($) {
Subroutine addr: called CC=2 SCAR=6.9
Pod documented
1112 return ($_[0]->{isv6})
1113 ? ipv6_n2x($_[0]->{addr})
1114 101561 ▶ : inet_n2dx($_[0]->{addr});
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'isv6'} ? : 25443 76118 101561
1115 }
1116  
1117 =item C<-E<gt>mask()>
1118  
1119 Returns a scalar with the mask as an IPv4 or IPv6 text string as
1120 described above.
1121  
1122 =cut
1123  
1124 250 ▶sub mask ($) {
Subroutine mask: called CC=3 SCAR=11.1
Pod documented
1125 250 ▶ return ipv6_n2x($_[0]->{mask}) if $_[0]->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"isv6"} 2 248 250
1126 my $mask = isIPv4($_[0]->{addr})
1127 ? $_[0]->{mask} & V4net
1128 248 ▶ : $_[0]->{mask};
Branch: 1 branch
BranchTrueFalseTotal
isIPv4($_[0]{'addr'}) ? : 248 0 248
1129 248 return inet_n2dx($mask);
1130 }
1131  
1132 =item C<-E<gt>masklen()>
1133  
1134 Returns a scalar the number of one bits in the mask.
1135  
1136 =cut
1137  
1138 41169 ▶sub masklen ($) {
Subroutine masklen: called CC=4 SCAR=13.9
Pod documented
1139 41169 my $len = (notcontiguous($_[0]->{mask}))[1];
1140 41169 ▶ return 0 unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 30 41139 41169
1141 41139 ▶ return $len if $_[0]->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"isv6"} 25268 15871 41139
1142 return isIPv4($_[0]->{addr})
1143 15871 ▶ ? $len -96
Branch: 1 branch
BranchTrueFalseTotal
isIPv4($_[0]{'addr'}) ? : 15774 97 15871
1144 : $len;
1145 }
1146  
1147 =item C<-E<gt>bits()>
1148  
1149 Returns the width of the address in bits. Normally 32 for v4 and 128 for v6.
1150  
1151 =cut
1152  
1153 348 ▶sub bits {
Subroutine bits: called CC=2 SCAR=6.9
Pod documented
1154 348 ▶ return $_[0]->{isv6} ? 128 : 32;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'isv6'} ? : 134 214 348
1155 }
1156  
1157 =item C<-E<gt>version()>
1158  
1159 Returns the version of the address or subnet. Currently this can be
1160 either 4 or 6.
1161  
1162 =cut
1163  
1164 347 ▶sub version {
Subroutine version: called CC=2 SCAR=6.9
Pod documented
1165 347 my $self = shift;
1166 347 ▶ return $self->{isv6} ? 6 : 4;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'isv6'} ? : 133 214 347
1167 }
1168  
1169 =item C<-E<gt>cidr()>
1170  
1171 Returns a scalar with the address and mask in CIDR notation. A
1172 NetAddr::IP::Lite object I<stringifies> to the result of this function.
1173 (see comments about ->new6() and ->addr() for output formats)
1174  
1175 =cut
1176  
1177 23397 ▶sub cidr ($) {
Subroutine cidr: called CC=1 SCAR=0.0
Pod documented
1178 23397 return $_[0]->addr . '/' . $_[0]->masklen;
1179 }
1180  
1181 =item C<-E<gt>aton()>
1182  
1183 Returns the address part of the NetAddr::IP::Lite object in the same format
1184 as the C<inet_aton()> or C<ipv6_aton> function respectively. If the object
1185 was created using ->new6($ip), the address returned will always be in ipV6
1186 format, even for addresses in ipV4 address space.
1187  
1188 =cut
1189  
1190 7 ▶sub aton {
Subroutine aton: called CC=3 SCAR=11.1
Pod documented
1191 7 ▶ return $_[0]->{addr} if $_[0]->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"isv6"} 2 5 7
1192 return isIPv4($_[0]->{addr})
1193 ? ipv6to4($_[0]->{addr})
1194 5 ▶ : $_[0]->{addr};
Branch: 1 branch
BranchTrueFalseTotal
isIPv4($_[0]{'addr'}) ? : 5 0 5
1195 }
1196  
1197 =item C<-E<gt>range()>
1198  
1199 Returns a scalar with the base address and the broadcast address
1200 separated by a dash and spaces. This is called range notation.
1201  
1202 =cut
1203  
1204 6 ▶sub range ($) {
Subroutine range: called CC=1 SCAR=0.0
Pod documented
1205 6 return $_[0]->network->addr . ' - ' . $_[0]->broadcast->addr;
1206 }
1207  
1208 =item C<-E<gt>numeric()>
1209  
1210 When called in a scalar context, will return a numeric representation
1211 of the address part of the IP address. When called in an array
1212 context, it returns a list of two elements. The first element is as
1213 described, the second element is the numeric representation of the
1214 netmask.
1215  
1216 This method is essential for serializing the representation of a
1217 subnet.
1218  
1219 =cut
1220  
1221 2253 ▶sub numeric ($) {
Subroutine numeric: called CC=6 SCAR=18.0
Pod documented
1222 2253 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 31 2222 2253
1223 31 ▶ if (! $_[0]->{isv6} && isIPv4($_[0]->{addr})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[0]{'isv6'} and isIPv4($_[0]{'addr'})) { } 6 25 31
Condition66%2/3
not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})
!ll&&!rl&&r
2506
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
1224 return ( sprintf("%u",unpack('N',ipv6to4($_[0]->{addr}))),
1225 6 sprintf("%u",unpack('N',ipv6to4($_[0]->{mask}))));
1226 }
1227 else {
1228 return ( bin2bcd($_[0]->{addr}),
1229 25 bin2bcd($_[0]->{mask}));
1230 }
1231 }
1232 return (! $_[0]->{isv6} && isIPv4($_[0]->{addr}))
1233 ? sprintf("%u",unpack('N',ipv6to4($_[0]->{addr})))
1234 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
1235 }
1236  
1237 =item C<-E<gt>bigint()>
1238  
1239 When called in a scalar context, will return a Math::BigInt representation
1240 of the address part of the IP address. When called in an array
1241 contest, it returns a list of two elements. The first element is as
1242 described, the second element is the Math::BigInt representation of the
1243 netmask.
1244  
1245 =cut
1246  
1247 104 my $biloaded;
1248 my $bi2strng;
1249 104 my $no_mbi_emu = 1;
1250  
1251 # function to force into test development mode
1252 #
1253 0 ▶sub _force_bi_emu {
Subroutine _force_bi_emu: not called CC=1 SCAR=6.9
1254 0 undef $biloaded;
1255 0 undef $bi2strng;
1256 0 $no_mbi_emu = 0;
1257 0 print STDERR "\n\n\tWARNING: test development mode, this
1258 \tmessage SHOULD NEVER BE SEEN IN PRODUCTION!
1259 set my \$no_mbi_emu = 1 in t/bigint.t to remove this warning\n\n";
1260 }
1261  
1262 # function to stringify various flavors of Math::BigInt objects
1263 # tests to see if the object is a hash or a signed scalar
1264  
1265 88 ▶sub _bi_stfy {
Subroutine _bi_stfy: called CC=1 SCAR=0.0
1266 88 "$_[0]" =~ /(\d+)/; # stringify and remove '+' if present
1267 88 $1;
1268 }
1269  
1270 0 ▶sub _fakebi2strg {
Subroutine _fakebi2strg: not called CC=1 SCAR=6.9
1271 0 ${$_[0]} =~ /(\d+)/;
1272 0 $1;
1273 }
1274  
1275 # fake new from bi string Math::BigInt 0.01
1276 #
1277 0 ▶sub _bi_fake {
Subroutine _bi_fake: not called CC=1 SCAR=6.9
1278 0 bless \('+'. $_[1]), 'Math::BigInt';
1279 }
1280  
1281 # as of this writing there are three known flavors of Math::BigInt
1282 # v0.01 MBI::new returns a scalar ref
1283 # v1.?? - 1.69 CALC::_new takes a reference to a scalar, returns an array, MBI returns a hash ref
1284 # v1.70 and up CALC::_new takes a scalar, returns and array, MBI returns a hash ref
1285  
1286 1 ▶sub _loadMBI { # load Math::BigInt on demand
Subroutine _loadMBI: called CC=3 SCAR=13.0
1287 1 ▶ if (eval {$no_mbi_emu && require Math::BigInt}) { # any version should work, three known
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $no_mbi_emu and require Math::BigInt } }) { } 1 0 1
Condition33%1/3
$no_mbi_emu && require Math::BigInt
!ll&&!rl&&r
001
MC/DC0%0/2
$no_mbi_emu && require Math::BigInt
$no_mbi_emu require Math::BigInt
Truth table33%1/3
A && B
A= $no_mbi_emuB= require Math::BigInt
ABresult
0X0
100
111
1288 1 import Math::BigInt;
1289 1 $biloaded = \&Math::BigInt::new;
1290 1 $bi2strng = \&_bi_stfy;
1291 } else {
1292 0 $biloaded = \&_bi_fake;
1293 0 $bi2strng = \&_fakebi2strg;
1294 }
1295 }
1296  
1297 88 ▶sub _retMBIstring {
Subroutine _retMBIstring: called CC=2 SCAR=7.2
1298 88 ▶ _loadMBI unless $biloaded; # load Math::BigInt on demand
Branch: 1 branch
BranchTrueFalseTotal
unless $biloaded 0 88 88
1299 88 $bi2strng->(@_);
1300 }
1301  
1302 92 ▶sub _biRef {
Subroutine _biRef: called CC=2 SCAR=6.9
1303 92 ▶ _loadMBI unless $biloaded; # load Math::BigInt on demand
Branch: 1 branch
BranchTrueFalseTotal
unless $biloaded 1 91 92
1304 92 $biloaded->('Math::BigInt',$_[0]);
1305 }
1306  
1307 51 ▶sub bigint($) {
Subroutine bigint: called CC=12 SCAR=26.1
Pod documented
1308 51 my($addr,$mask);
1309 51 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 41 10 51
1310 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
1311 $addr = $_[0]->{addr}
1312 9 ▶ ? sprintf("%u",unpack('N',ipv6to4($_[0]->{addr})))
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'addr'} ? : 9 0 9
1313 : 0;
1314 $mask = $_[0]->{mask}
1315 9 ▶ ? sprintf("%u",unpack('N',ipv6to4($_[0]->{mask})))
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'mask'} ? : 9 0 9
1316 : 0;
1317 }
1318 else {
1319 $addr = $_[0]->{addr}
1320 ? bin2bcd($_[0]->{addr})
1321 32 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'addr'} ? : 32 0 32
1322 $mask = $_[0]->{mask}
1323 ? bin2bcd($_[0]->{mask})
1324 32 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'mask'} ? : 32 0 32
1325 }
1326 41 (_biRef($addr),_biRef($mask));
1327  
1328 } else { # not wantarray
1329  
1330 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
1331 $addr = $_[0]->{addr}
1332 5 ▶ ? sprintf("%u",unpack('N',ipv6to4($_[0]->{addr})))
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'addr'} ? : 5 0 5
1333 : 0;
1334 } else {
1335 $addr = $_[0]->{addr}
1336 ? bin2bcd($_[0]->{addr})
1337 5 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'addr'} ? : 5 0 5
1338 }
1339 10 _biRef($addr);
1340 }
1341 }
1342  
1343 =item C<$me-E<gt>contains($other)>
1344  
1345 Returns true when C<$me> completely contains C<$other>. False is
1346 returned otherwise and C<undef> is returned if C<$me> and C<$other>
1347 are not both C<NetAddr::IP::Lite> objects.
1348  
1349 =cut
1350  
1351 293455 ▶sub contains ($$) {
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
1352 293455 return within(@_[1,0]);
1353 }
1354  
1355 =item C<$me-E<gt>within($other)>
1356  
1357 The complement of C<-E<gt>contains()>. Returns true when C<$me> is
1358 completely contained within C<$other>, undef if C<$me> and C<$other>
1359 are not both C<NetAddr::IP::Lite> objects.
1360  
1361 =cut
1362  
1363 293595 ▶sub within ($$) {
Subroutine within: called CC=4 SCAR=13.9
Pod documented
1364 293595 ▶ return 1 unless hasbits($_[1]->{mask}); # 0x0 contains everything
Branch: 1 branch
BranchTrueFalseTotal
unless &hasbits($_[1]{"mask"}) 6 293589 293595
1365 293589 my $netme = $_[0]->{addr} & $_[0]->{mask};
1366 293589 my $brdme = $_[0]->{addr} | ~ $_[0]->{mask};
1367 293589 my $neto = $_[1]->{addr} & $_[1]->{mask};
1368 293589 my $brdo = $_[1]->{addr} | ~ $_[1]->{mask};
1369 293589 ▶ return (sub128($netme,$neto) && sub128($brdo,$brdme))
Branch: 1 branch
BranchTrueFalseTotal
&sub128($netme, $neto) && &sub128($brdo, $brdme) ? : 54371 239218 293589
Condition100%3/3
&sub128($netme, $neto) and &sub128($brdo, $brdme)
!ll&&!rl&&r
6734617187254371
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
1370 ? 1 : 0;
1371 }
1372  
1373 =item C-E<gt>is_rfc1918()>
1374  
1375 Returns true when C<$me> is an RFC 1918 address.
1376  
1377 10.0.0.0 - 10.255.255.255 (10/8 prefix)
1378 172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
1379 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
1380  
1381 =cut
1382  
1383 104 my $ip_10 = NetAddr::IP::Lite->new('10.0.0.0/8');
1384 104 my $ip_10n = $ip_10->{addr}; # already the right value
1385 104 my $ip_10b = $ip_10n | ~ $ip_10->{mask};
1386  
1387 104 my $ip_172 = NetAddr::IP::Lite->new('172.16.0.0/12');
1388 104 my $ip_172n = $ip_172->{addr}; # already the right value
1389 104 my $ip_172b = $ip_172n | ~ $ip_172->{mask};
1390  
1391 104 my $ip_192 = NetAddr::IP::Lite->new('192.168.0.0/16');
1392 104 my $ip_192n = $ip_192->{addr}; # already the right value
1393 104 my $ip_192b = $ip_192n | ~ $ip_192->{mask};
1394  
1395 12 ▶sub is_rfc1918 ($) {
Subroutine is_rfc1918: called CC=7 SCAR=19.5
Pod documented
1396 12 my $netme = $_[0]->{addr} & $_[0]->{mask};
1397 12 my $brdme = $_[0]->{addr} | ~ $_[0]->{mask};
1398 12 ▶ 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) 2 10 12
Condition100%3/3
&sub128($netme, $ip_10n) and &sub128($ip_10b, $brdme)
!ll&&!rl&&r
192
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
1399 10 ▶ 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) 2 8 10
Condition100%3/3
&sub128($netme, $ip_192n) and &sub128($ip_192b, $brdme)
!ll&&!rl&&r
712
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
1400 8 ▶ return (sub128($netme,$ip_172n) && sub128($ip_172b,$brdme))
Branch: 1 branch
BranchTrueFalseTotal
&sub128($netme, $ip_172n) && &sub128($ip_172b, $brdme) ? : 2 6 8
Condition100%3/3
&sub128($netme, $ip_172n) and &sub128($ip_172b, $brdme)
!ll&&!rl&&r
332
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
1401 ? 1 : 0;
1402 }
1403  
1404 =item C<-E<gt>is_local()>
1405  
1406 Returns true when C<$me> is a local network address.
1407  
1408 i.e. ipV4 127.0.0.0 - 127.255.255.255
1409 or ipV6 === ::1
1410  
1411 =cut
1412  
1413 104 my $_lclhost6 = NetAddr::IP::Lite->new('::1');
1414 104 my $_lclnet = NetAddr::IP::Lite->new('127/8');
1415  
1416 9 ▶sub is_local ($) {
Subroutine is_local: called CC=2 SCAR=6.9
Pod documented
1417 return ($_[0]->{isv6})
1418 9 ▶ ? $_[0] == $_lclhost6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'isv6'} ? : 3 6 9
1419 : $_[0]->within($_lclnet);
1420 }
1421  
1422 =item C<-E<gt>first()>
1423  
1424 Returns a new object representing the first usable IP address within
1425 the subnet (ie, the first host address).
1426  
1427 =cut
1428  
1429 104 my $_cidr127 = pack('N4',0xffffffff,0xffffffff,0xffffffff,0xfffffffe);
1430  
1431 26 ▶sub first ($) {
Subroutine first: called CC=2 SCAR=6.9
Pod documented
1432 26 ▶ if (hasbits($_[0]->{mask} ^ $_cidr127)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&hasbits($_[0]{'mask'} ^ $_cidr127)) { } 24 2 26
1433 24 return $_[0]->network + 1;
1434 } else {
1435 2 return $_[0]->network;
1436 }
1437 # return $_[0]->network + 1;
1438 }
1439  
1440 =item C<-E<gt>last()>
1441  
1442 Returns a new object representing the last usable IP address within
1443 the subnet (ie, one less than the broadcast address).
1444  
1445 =cut
1446  
1447 18 ▶sub last ($) {
Subroutine last: called CC=2 SCAR=6.9
Pod documented
1448 18 ▶ if (hasbits($_[0]->{mask} ^ $_cidr127)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&hasbits($_[0]{'mask'} ^ $_cidr127)) { } 16 2 18
1449 16 return $_[0]->broadcast - 1;
1450 } else {
1451 2 return $_[0]->broadcast;
1452 }
1453 # return $_[0]->broadcast - 1;
1454 }
1455  
1456 =item C<-E<gt>nth($index)>
1457  
1458 Returns a new object representing the I<n>-th usable IP address within
1459 the subnet (ie, the I<n>-th host address). If no address is available
1460 (for example, when the network is too small for C<$index> hosts),
1461 C<undef> is returned.
1462  
1463 Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite implements
1464 C<-E<gt>nth($index)> and C<-E<gt>num()> exactly as the documentation states.
1465 Previous versions behaved slightly differently and not in a consistent
1466 manner.
1467  
1468 To use the old behavior for C<-E<gt>nth($index)> and C<-E<gt>num()>:
1469  
1470 use NetAddr::IP::Lite qw(:old_nth);
1471  
1472 old behavior:
1473 NetAddr::IP->new('10/32')->nth(0) == undef
1474 NetAddr::IP->new('10/32')->nth(1) == undef
1475 NetAddr::IP->new('10/31')->nth(0) == undef
1476 NetAddr::IP->new('10/31')->nth(1) == 10.0.0.1/31
1477 NetAddr::IP->new('10/30')->nth(0) == undef
1478 NetAddr::IP->new('10/30')->nth(1) == 10.0.0.1/30
1479 NetAddr::IP->new('10/30')->nth(2) == 10.0.0.2/30
1480 NetAddr::IP->new('10/30')->nth(3) == 10.0.0.3/30
1481  
1482 Note that in each case, the broadcast address is represented in the
1483 output set and that the 'zero'th index is alway undef except for
1484 a point-to-point /31 or /127 network where there are exactly two
1485 addresses in the network.
1486  
1487 new behavior:
1488 NetAddr::IP->new('10/32')->nth(0) == 10.0.0.0/32
1489 NetAddr::IP->new('10.1/32'->nth(0) == 10.0.0.1/32
1490 NetAddr::IP->new('10/31')->nth(0) == 10.0.0.0/32
1491 NetAddr::IP->new('10/31')->nth(1) == 10.0.0.1/32
1492 NetAddr::IP->new('10/30')->nth(0) == 10.0.0.1/30
1493 NetAddr::IP->new('10/30')->nth(1) == 10.0.0.2/30
1494 NetAddr::IP->new('10/30')->nth(2) == undef
1495  
1496 Note that a /32 net always has 1 usable address while a /31 has exactly
1497 two usable addresses for point-to-point addressing. The first
1498 index (0) returns the address immediately following the network address
1499 except for a /31 or /127 when it return the network address.
1500  
1501 =cut
1502  
1503 154 ▶sub nth ($$) {
Subroutine nth: called CC=11 SCAR=27.1
Pod documented
1504 154 my $self = shift;
1505 154 my $count = shift;
1506  
1507 154 my $slash31 = ! hasbits($self->{mask} ^ $_cidr127);
1508 154 ▶ if ($Old_nth) {
Branch: 2 branches
BranchTrueFalseTotal
if ($NetAddr::IP::Lite::Old_nth) { } 56 98 154
elsif ($slash31) { } 0 98 98
1509 56 ▶ return undef if $slash31 && $count != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $slash31 and $count != 1 0 56 56
Condition33%1/3
$slash31 and $count != 1
!ll&&!rl&&r
5600
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
1510 56 ▶ return undef if ($count < 1 or $count > $self->num ());
Branch: 1 branch
BranchTrueFalseTotal
if $count < 1 or $count > $self->num 11 45 56
Condition100%3/3
$count < 1 or $count > $self->num
l!l&&r!l&&!r
11045
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
1511 }
1512 elsif ($slash31) {
1513 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
1514 } else {
1515 98 ++$count;
1516 98 ▶ return undef if ($count < 1 or $count > $self->num ());
Branch: 1 branch
BranchTrueFalseTotal
if $count < 1 or $count > $self->num 10 88 98
Condition66%2/3
$count < 1 or $count > $self->num
l!l&&r!l&&!r
01088
MC/DC50%1/2
$count < 1 or $count > $self->num
$count < 1 $count > $self->num
Truth table66%2/3
A or B
A= $count < 1B= $count > $self->num
ABresult
1X1
011
000
1517 }
1518 133 return $self->network + $count;
1519 }
1520  
1521 =item C<-E<gt>num()>
1522  
1523 As of version 4.42 of NetAddr::IP and version 1.27 of NetAddr::IP::Lite
1524 a /31 and /127 with return a net B<num> value of 2 instead of 0 (zero)
1525 for point-to-point networks.
1526  
1527 Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite
1528 return the number of usable IP addresses within the subnet,
1529 not counting the broadcast or network address.
1530  
1531 Previous versions worked only for ipV4 addresses, returned a
1532 maximum span of 2**32 and returned the number of IP addresses
1533 not counting the broadcast address.
1534 (one greater than the new behavior)
1535  
1536 To use the old behavior for C<-E<gt>nth($index)> and C<-E<gt>num()>:
1537  
1538 use NetAddr::IP::Lite qw(:old_nth);
1539  
1540 WARNING:
1541  
1542 NetAddr::IP will calculate and return a numeric string for network
1543 ranges as large as 2**128. These values are TEXT strings and perl
1544 can treat them as integers for numeric calculations.
1545  
1546 Perl on 32 bit platforms only handles integer numbers up to 2**32
1547 and on 64 bit platforms to 2**64.
1548  
1549 If you wish to manipulate numeric strings returned by NetAddr::IP
1550 that are larger than 2**32 or 2**64, respectively, you must load
1551 additional modules such as Math::BigInt, bignum or some similar
1552 package to do the integer math.
1553  
1554 =cut
1555  
1556 4821 ▶sub num ($) {
Subroutine num: called CC=9 SCAR=22.0
Pod documented
1557 4821 ▶ if ($Old_nth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($NetAddr::IP::Lite::Old_nth) { } 2373 2448 4821
1558 2373 my @net = unpack('L3N',$_[0]->{mask} ^ Ones);
1559 # number of ip's less broadcast
1560 2373 ▶ return 0xfffffffe if $net[0] || $net[1] || $net[2]; # 2**32 -1
Branch: 1 branch
BranchTrueFalseTotal
if $net[0] or $net[1] or $net[2] 36 2337 2373
Condition83%5/6
$net[0] or $net[1] or $net[2]
l!l&&r!l&&!r
3242337
$net[0] or $net[1]
l!l&&r!l&&!r
0322341
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
1561 2337 ▶ return $net[3] if $net[3];
Branch: 1 branch
BranchTrueFalseTotal
if $net[3] 22 2315 2337
1562 } else { # returns 1 for /32 /128, 2 for /31 /127 else n-2 up to 2**32
1563 2448 (undef, my $net) = addconst($_[0]->{mask},1);
1564 2448 ▶ return 1 unless hasbits($net); # ipV4/32 or ipV6/128
Branch: 1 branch
BranchTrueFalseTotal
unless &hasbits($net) 2326 122 2448
1565 122 $net = $net ^ Ones;
1566 122 ▶ return 2 unless hasbits($net); # ipV4/31 or ipV6/127
Branch: 1 branch
BranchTrueFalseTotal
unless &hasbits($net) 6 116 122
1567 116 ▶ $net &= $_v4net unless $_[0]->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"isv6"} 20 96 116
1568 116 return bin2bcd($net);
1569 }
1570 }
1571  
1572 # deprecated
1573 #sub num ($) {
1574 # my @net = unpack('L3N',$_[0]->{mask} ^ Ones);
1575 # if ($Old_nth) {
1576 ## number of ip's less broadcast
1577 # return 0xfffffffe if $net[0] || $net[1] || $net[2]; # 2**32 -1
1578 # return $net[3] if $net[3];
1579 # } else { # returns 1 for /32 /128, 0 for /31 /127 else n-2 up to 2**32
1580 ## number of usable IP's === number of ip's less broadcast & network addys
1581 # return 0xfffffffd if $net[0] || $net[1] || $net[2]; # 2**32 -2
1582 # return 1 unless $net[3];
1583 # $net[3]--;
1584 # }
1585 # return $net[3];
1586 #}
1587  
1588 =pod
1589  
1590 =back
1591  
1592 =cut
1593  
1594 107 ▶sub import {
Subroutine import: called CC=6 SCAR=18.0
1595 107 ▶ if (grep { $_ eq ':aton' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":aton";} @_) 1 106 107
1596 1 $Accept_Binary_IP = 1;
1597 1 @_ = grep { $_ ne ':aton' } @_;
1598 }
1599 107 ▶ if (grep { $_ eq ':old_nth' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":old_nth";} @_) 6 101 107
1600 6 $Old_nth = 1;
1601 6 @_ = grep { $_ ne ':old_nth' } @_;
1602 }
1603 107 ▶ if (grep { $_ eq ':lower' } @_)
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":lower";} @_) 1 106 107
1604 {
1605 1 NetAddr::IP::Util::lower();
1606 1 @_ = grep { $_ ne ':lower' } @_;
1607 }
1608 107 ▶ if (grep { $_ eq ':upper' } @_)
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":upper";} @_) 0 107 107
1609 {
1610 0 NetAddr::IP::Util::upper();
1611 0 @_ = grep { $_ ne ':upper' } @_;
1612 }
1613 107 ▶ if (grep { $_ eq ':nofqdn' } @_)
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":nofqdn";} @_) 1 106 107
1614 {
1615 1 $NoFQDN = 1;
1616 1 @_ = grep { $_ ne ':nofqdn' } @_;
1617 }
1618 107 NetAddr::IP::Lite->export_to_level(1, @_);
1619 }
1620  
1621 =head1 EXPORT_OK
1622  
1623 Zeros
1624 Ones
1625 V4mask
1626 V4net
1627 :aton DEPRECATED
1628 :old_nth
1629 :upper
1630 :lower
1631 :nofqdn
1632  
1633 =head1 AUTHORS
1634  
1635 Luis E. Muñoz E<lt>luismunoz@cpan.orgE<gt>,
1636 Michael Robinton E<lt>michael@bizsystems.comE<gt>
1637  
1638 =head1 WARRANTY
1639  
1640 This software comes with the same warranty as perl itself (ie, none),
1641 so by using it you accept any and all the liability.
1642  
1643 =head1 COPYRIGHT
1644  
1645 This software is (c) Luis E. Muñoz, 1999 - 2005
1646 and (c) Michael Robinton, 2006 - 2014.
1647  
1648 All rights reserved.
1649  
1650 This program is free software; you can redistribute it and/or modify
1651 it under the terms of either:
1652  
1653 a) the GNU General Public License as published by the Free
1654 Software Foundation; either version 2, or (at your option) any
1655 later version, or
1656  
1657 b) the "Artistic License" which comes with this distribution.
1658  
1659 This program is distributed in the hope that it will be useful,
1660 but WITHOUT ANY WARRANTY; without even the implied warranty of
1661 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
1662 the GNU General Public License or the Artistic License for more details.
1663  
1664 You should have received a copy of the Artistic License with this
1665 distribution, in the file named "Artistic". If not, I'll be glad to provide
1666 one.
1667  
1668 You should also have received a copy of the GNU General Public License
1669 along with this program in the file named "Copying". If not, write to the
1670  
1671 Free Software Foundation, Inc.,
1672 51 Franklin Street, Fifth Floor
1673 Boston, MA 02110-1301 USA
1674  
1675 or visit their web page on the internet at:
1676  
1677 http://www.gnu.org/copyleft/gpl.html.
1678  
1679 =head1 SEE ALSO
1680  
1681 NetAddr::IP(3), NetAddr::IP::Util(3), NetAddr::IP::InetBase(3)
1682  
1683 =cut
1684  
1685 104 1;
↑ Summary blib/lib/NetAddr/IP.pm »