IP/InetBase.pm

filter: Statementstmt 99.5% 202 / 203 Branchbran 97.2% 140 / 144 Conditioncond 78.1% 93 / 119 MC/DCmcdc 67.7% 42 / 62 Subroutinesub 100% 30 / 30 Podpod 100% 19 / 19 totaltotal 91.1% 526 / 577 CC 115 SCAR 26.6
CC
115
Cov
93%
fillIPv4
36.8
ipv6_aton
21.1
inet_ntop
10.1
CRAP 14.3
« IP.pm ↑ Summary IP/UtilPP.pm »
1 #!/bin/false
2 # ABSTRACT: IPv4 and IPv6 address parsing and formatting utilities
3 # PODNAME: NetAddr::IP::InetBase
4  
5 176 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 176 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package NetAddr::IP::InetBase;
9  
10 176 our $VERSION = '4.080_09'; # TRIAL VERSION
11  
12 176 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 176 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 176 ▶use NetAddr::IP::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 $IPV6_BITS
16 $MAX_OCTET
17 $OCTET_BITS
18 $V4_PACKED_BYTES
19 $V6_PACKED_BYTES
20 176 );
21 176 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 176 ▶use Socket qw( AF_INET AF_INET6 );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 176 our @EXPORT_OK = qw(
25 inet_aton
26 inet_ntoa
27 ipv6_aton
28 ipv6_ntoa
29 ipv6_n2x
30 ipv6_n2d
31 inet_any2n
32 inet_n2dx
33 inet_n2ad
34 inet_ntop
35 inet_pton
36 packzeros
37 isIPv4
38 isNewIPv4
39 isAnyIPv4
40 AF_INET
41 AF_INET6
42 fake_AF_INET6
43 fillIPv4
44 );
45 176 our %EXPORT_TAGS = (
46 all => [@EXPORT_OK],
47 ipv4 => [ qw(
48 inet_aton
49 inet_ntoa
50 fillIPv4
51 )
52 ],
53 ipv6 => [ qw(
54 ipv6_aton
55 ipv6_ntoa
56 ipv6_n2x
57 ipv6_n2d
58 inet_any2n
59 inet_n2dx
60 inet_n2ad
61 inet_pton
62 inet_ntop
63 packzeros
64 )
65 ],
66 );
67 176 our $Mode;
68  
69 # prototypes
70 sub inet_ntoa;
71 sub ipv6_aton;
72 sub ipv6_ntoa;
73 sub inet_any2n($);
74 sub inet_n2dx($);
75 sub inet_n2ad($);
76  
77 # the two families inet_ntop and inet_pton accept, named in their errors
78 176 my $FAMILY_SHOULD_BE = sprintf 'should be AF_INET (%d) or AF_INET6 (%d)',
79 AF_INET(), AF_INET6();
80  
81 # the length error from ipv6_n2x and ipv6_n2d, which name themselves quoted
82 176 my $N2_LENGTH_DIES = q{Bad arg length for '%s', length is %s should be %d bytes};
83  
84 # the error for a packed argument with a character above 0xFF, worded as perl's
85 # own from the XS, with the function named in place of "subroutine entry"
86 176 my $WIDE_DIES = 'Wide character in %s';
87  
88 # deprecated: AF_INET6 always comes from Socket, so there is no guess to report
89 2 ▶sub fake_AF_INET6 {
Subroutine fake_AF_INET6: called CC=1 SCAR=0.0
Pod documented
90 2 warnings::warnif(
91 'deprecated',
92 'fake_AF_INET6 is deprecated and will be removed in version 5; AF_INET6 always comes from Socket'
93 );
94 2 return 0;
95 }
96  
97 176 our ($n2x_format, $n2d_format, $Upper);
98  
99 176 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
100 176 $n2x_format = "%x:%x:%x:%x:%x:%x:%x:%x";
101 176 $n2d_format = "%x:%x:%x:%x:%x:%x:%d.%d.%d.%d";
102 176 $Upper = 0; # one case setting for IPv6 text in every NetAddr::IP module, uppercase while true
103 }
104  
105 # the same formats in uppercase, used while $Upper is true
106 176 my $n2x_upper = uc $n2x_format;
107 176 my $n2d_upper = uc $n2d_format;
108  
109 163 ▶sub upper { $Upper = 1 }
Subroutine upper: called CC=1 SCAR=0.0
Pod documented
110 7 ▶sub lower { $Upper = 0 }
Subroutine lower: called CC=1 SCAR=0.0
Pod documented
111  
112 # sets the case from the :lower and :upper tags, :upper last so it wins, and drops both from the list
113 376 ▶sub _case_tags {
Subroutine _case_tags: called CC=4 SCAR=13.9
114 376 my ($args) = @_;
115 376 ▶ lower() if grep { $_ eq ':lower' } @$args;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq ":lower";} @$args 6 370 376
116 376 ▶ upper() if grep { $_ eq ':upper' } @$args;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq ":upper";} @$args 159 217 376
117 376 ▶ @$args = grep { $_ ne ':lower' && $_ ne ':upper' } @$args;
Condition100%3/3
$_ ne ':lower' && $_ ne ':upper'
!ll&&!rl&&r
61594224
MC/DC100%2/2
$_ ne ':lower' && $_ ne ':upper'
$_ ne ':lower' $_ ne ':upper'
Truth table100%3/3
A && B
A= $_ ne ':lower'B= $_ ne ':upper'
ABresult
0X0
100
111
118 376 return;
119 }
120  
121 27684 ▶sub ipv6_n2x {
Subroutine ipv6_n2x: called CC=6 SCAR=17.9
Pod documented
122 27684 my $packed = $_[0];
123  
124 # measure bytes, as the XS does: a character above 0xFF fails to downgrade
125 27684 ▶ croak sprintf($WIDE_DIES, __PACKAGE__ . '::ipv6_n2x')
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($packed, 1) 2 27682 27684
126 unless utf8::downgrade($packed, 1);
127 27682 ▶ croak sprintf(
Branch: 2 branches
BranchTrueFalseTotal
unless defined $packed and length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES 7 27675 27682
defined $packed ? : 5 2 7
Condition100%3/3
defined $packed and length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
!ll&&!rl&&r
2527675
MC/DC100%2/2
defined $packed and length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
defined $packed length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
Truth table100%3/3
A and B
A= defined $packedB= length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
ABresult
0X0
100
111
128 $N2_LENGTH_DIES, 'ipv6_n2x',
129 defined $packed ? length($packed) : 'undefined', $V6_PACKED_BYTES
130 ) unless defined $packed && length($packed) == $V6_PACKED_BYTES;
131 27675 ▶ return sprintf($Upper ? $n2x_upper : $n2x_format, unpack("n8", $packed));
Branch: 1 branch
BranchTrueFalseTotal
$NetAddr::IP::InetBase::Upper ? : 27289 386 27675
132 }
133  
134 14470 ▶sub ipv6_n2d {
Subroutine ipv6_n2d: called CC=6 SCAR=17.9
Pod documented
135 14470 my $packed = $_[0];
136 14470 ▶ croak sprintf($WIDE_DIES, __PACKAGE__ . '::ipv6_n2d')
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($packed, 1) 3 14467 14470
137 unless utf8::downgrade($packed, 1);
138 14467 ▶ croak sprintf(
Branch: 2 branches
BranchTrueFalseTotal
unless defined $packed and length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES 10 14457 14467
defined $packed ? : 6 4 10
Condition100%3/3
defined $packed and length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
!ll&&!rl&&r
4614457
MC/DC100%2/2
defined $packed and length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
defined $packed length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
Truth table100%3/3
A and B
A= defined $packedB= length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
ABresult
0X0
100
111
139 $N2_LENGTH_DIES, 'ipv6_n2d',
140 defined $packed ? length($packed) : 'undefined', $V6_PACKED_BYTES
141 ) unless defined $packed && length($packed) == $V6_PACKED_BYTES;
142 14457 my @hex = (unpack("n8", $packed));
143 14457 $hex[9] = $hex[7] & $MAX_OCTET;
144 14457 $hex[8] = $hex[7] >> $OCTET_BITS;
145 14457 $hex[7] = $hex[6] & $MAX_OCTET;
146 14457 $hex[6] >>= $OCTET_BITS;
147 14457 ▶ return sprintf($Upper ? $n2d_upper : $n2d_format, @hex);
Branch: 1 branch
BranchTrueFalseTotal
$NetAddr::IP::InetBase::Upper ? : 14443 14 14457
148 }
149  
150 # if Socket lib is broken in some way, check for overange values
151 #
152  
153 9790 ▶sub fillIPv4 {
Subroutine fillIPv4: called CC=28 SCAR=36.0
Pod documented
154 9790 my $host = $_[0];
155 9790 ▶ return undef unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 1 9789 9790
156  
157 # the C resolver stops at a NUL byte, so the text after it would go unread
158 9789 ▶ return undef if index($host, "\0") >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($host, "\0") != -1 8 9781 9789
159 9781 ▶ if ($host =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /^([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+)(?:|\.([0-9]+))))\z/) 9676 105 9781
160  
161 # a leading zero reads as octal to inet_aton and as decimal to people, so it is refused
162 9676 ▶ return undef if $host =~ /(?:\A|[.])0[0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /(?:\A|[.])0[0-9]/ 40 9636 9676
163 9636 ▶ if (defined $4) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $4) { } 9617 19 9636
elsif (defined $3) { } 3 16 19
elsif (defined $2) { } 8 8 16
164 return undef
165 9617 ▶ unless $1 >= 0
Branch: 1 branch
BranchTrueFalseTotal
unless $1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0 and $3 < 256 and $4 >= 0 and $4 < 256 15 9602 9617
Condition80%17/21
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0 and $3 < 256 and $4 >= 0 and $4 < 256
!ll&&!rl&&r
1419602
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0 and $3 < 256 and $4 >= 0
!ll&&!rl&&r
1409603
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0 and $3 < 256
!ll&&!rl&&r
1319603
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0
!ll&&!rl&&r
1309604
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256
!ll&&!rl&&r
1219604
$1 >= 0 and $1 < 256 and $2 >= 0
!ll&&!rl&&r
1209605
$1 >= 0 and $1 < 256
!ll&&!rl&&r
0129605
MC/DC50%4/8
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0 and $3 < 256 and $4 >= 0 and $4 < 256
$1 >= 0 $1 < 256 $2 >= 0 $2 < 256 $3 >= 0 $3 < 256 $4 >= 0 $4 < 256
Truth table55%5/9
A and B and C and D and E and F and G and H
A= $1 >= 0B= $1 < 256C= $2 >= 0D= $2 < 256E= $3 >= 0F= $3 < 256G= $4 >= 0H= $4 < 256
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
11110XXX0
111110XX0
1111110X0
111111100
111111111
166 && $1 < 256
167 && $2 >= 0
168 && $2 < 256
169 && $3 >= 0
170 && $3 < 256
171 && $4 >= 0
172 && $4 < 256;
173 9602 $host = $1 . '.' . $2 . '.' . $3 . '.' . $4;
174 }
175 elsif (defined $3) {
176 return undef
177 3 ▶ unless $1 >= 0
Branch: 1 branch
BranchTrueFalseTotal
unless $1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0 and $3 < 256 1 2 3
Condition40%6/15
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0 and $3 < 256
!ll&&!rl&&r
012
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0
!ll&&!rl&&r
003
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256
!ll&&!rl&&r
003
$1 >= 0 and $1 < 256 and $2 >= 0
!ll&&!rl&&r
003
$1 >= 0 and $1 < 256
!ll&&!rl&&r
003
MC/DC16%1/6
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0 and $3 < 256
$1 >= 0 $1 < 256 $2 >= 0 $2 < 256 $3 >= 0 $3 < 256
Truth table28%2/7
A and B and C and D and E and F
A= $1 >= 0B= $1 < 256C= $2 >= 0D= $2 < 256E= $3 >= 0F= $3 < 256
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
178 && $1 < 256
179 && $2 >= 0
180 && $2 < 256
181 && $3 >= 0
182 && $3 < 256;
183 2 $host = $1 . '.' . $2 . '.0.' . $3;
184 }
185 elsif (defined $2) {
186 return undef
187 8 ▶ unless $1 >= 0
Branch: 1 branch
BranchTrueFalseTotal
unless $1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 1 7 8
Condition44%4/9
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256
!ll&&!rl&&r
017
$1 >= 0 and $1 < 256 and $2 >= 0
!ll&&!rl&&r
008
$1 >= 0 and $1 < 256
!ll&&!rl&&r
008
MC/DC25%1/4
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256
$1 >= 0 $1 < 256 $2 >= 0 $2 < 256
Truth table40%2/5
A and B and C and D
A= $1 >= 0B= $1 < 256C= $2 >= 0D= $2 < 256
ABCDresult
0XXX0
10XX0
110X0
11100
11111
188 && $1 < 256
189 && $2 >= 0
190 && $2 < 256;
191 7 $host = $1 . '.0.0.' . $2;
192 }
193 else {
194 8 ▶ return undef unless $1 >= 0 && $1 < 256;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 >= 0 and $1 < 256 4 4 8
Condition66%2/3
$1 >= 0 and $1 < 256
!ll&&!rl&&r
044
MC/DC50%1/2
$1 >= 0 and $1 < 256
$1 >= 0 $1 < 256
Truth table66%2/3
A and B
A= $1 >= 0B= $1 < 256
ABresult
0X0
100
111
195 4 $host = '0.0.0.' . $1;
196 }
197 }
198 9720 $host;
199 }
200  
201 # whitespace, which no address or host name holds, is refused before the resolver sees it
202 176 my $WHITESPACE = qr/[\t\n\x0B\f\r ]/;
203  
204 9643 ▶sub inet_aton {
Subroutine inet_aton: called CC=6 SCAR=18.0
Pod documented
205 9643 ▶ return undef if defined $_[0] && $_[0] =~ $WHITESPACE;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] =~ /$WHITESPACE/ 5 9638 9643
Condition66%2/3
defined $_[0] and $_[0] =~ /$WHITESPACE/
!ll&&!rl&&r
096385
MC/DC50%1/2
defined $_[0] and $_[0] =~ /$WHITESPACE/
defined $_[0] $_[0] =~ /$WHITESPACE/
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] =~ /$WHITESPACE/
ABresult
0X0
100
111
206 9638 my $host = fillIPv4($_[0]);
207 9638 ▶ return undef unless defined $host && $host ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and $host ne "" 36 9602 9638
Condition66%2/3
defined $host and $host ne ""
!ll&&!rl&&r
3609602
MC/DC50%1/2
defined $host and $host ne ""
defined $host $host ne ""
Truth table66%2/3
A and B
A= defined $hostB= $host ne ""
ABresult
0X0
100
111
208  
209 # gethostbyname keeps the answer in one static buffer. Two threads
210 # looking up at once can swap answers, so a literal is packed here.
211 9602 ▶ if ($host =~ /\A([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /\A([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)\z/) 9601 1 9602
212 9601 return pack('C4', $1, $2, $3, $4);
213 }
214 1 return scalar gethostbyname($host);
215 }
216  
217 176 my $_zero = pack('L4', 0, 0, 0, 0);
218 176 my $_ipv4mask = pack('L4', 0xffffffff, 0xffffffff, 0xffffffff, 0);
219  
220 46711 ▶sub isIPv4 {
Subroutine isIPv4: called CC=9 SCAR=22.0
Pod documented
221 46711 my $naddr = $_[0];
222 46711 my $bytes = utf8::downgrade($naddr, 1);
223 46711 ▶ if (!$bytes || !defined $naddr || length($naddr) != $V6_PACKED_BYTES) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $bytes && defined $naddr or length $naddr != $NetAddr::IP::InetBase::V6_PACKED_BYTES) 37 46674 46711
Condition100%6/6
not $bytes && defined $naddr or length $naddr != $NetAddr::IP::InetBase::V6_PACKED_BYTES
l!l&&r!l&&!r
122546674
$bytes && defined $naddr
!ll&&!rl&&r
7546699
MC/DC100%3/3
not $bytes && defined $naddr or length $naddr != $NetAddr::IP::InetBase::V6_PACKED_BYTES
$bytes defined $naddr length $naddr != $NetAddr::IP::InetBase::V6_PACKED_BYTES
Truth table100%4/4
not(A && B) or C
A= $bytesB= defined $naddrC= length $naddr != $NetAddr::IP::InetBase::V6_PACKED_BYTES
ABCresult
0XX1
10X1
1111
1100
224  
225 # name the InetBase function the caller entered, else isIPv4 itself
226 37 my $caller = (caller(1))[3];
227 37 ▶ my $sub =
Branch: 1 branch
BranchTrueFalseTotal
defined $caller && index($caller, 'NetAddr::IP::InetBase::') == 0 ? : 23 14 37
Condition66%2/3
defined $caller and index($caller, "NetAddr::IP::InetBase::") == 0
!ll&&!rl&&r
01423
MC/DC50%1/2
defined $caller and index($caller, "NetAddr::IP::InetBase::") == 0
defined $caller index($caller, "NetAddr::IP::InetBase::") == 0
Truth table66%2/3
A and B
A= defined $callerB= index($caller, "NetAddr::IP::InetBase::") == 0
ABresult
0X0
100
111
228 defined $caller && index($caller, __PACKAGE__ . '::') == 0
229 ? $caller
230 : (caller(0))[3];
231 37 ▶ croak sprintf($WIDE_DIES, $sub) unless $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless $bytes 7 30 37
232 30 ▶ my $bits = defined $naddr ? length($naddr) * $OCTET_BITS : 'undefined';
Branch: 1 branch
BranchTrueFalseTotal
defined $naddr ? : 25 5 30
233 30 croak "Bad arg length for $sub, length is $bits, should be $IPV6_BITS";
234 }
235 46674 ▶ return ($naddr & $_ipv4mask) eq $_zero ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($naddr & $_ipv4mask) eq $_zero ? : 44985 1689 46674
236 }
237  
238 176 my $_newV4compat = pack('N4', 0, 0, 0xffff, 0);
239  
240 642 ▶sub isNewIPv4 {
Subroutine isNewIPv4: called CC=1 SCAR=0.0
Pod documented
241  
242 # croaks unless 16 bytes, naming isNewIPv4, as isIPv4 and isAnyIPv4 do
243 642 isIPv4($_[0]);
244 633 my $naddr = $_[0] ^ $_newV4compat;
245 633 return isIPv4($naddr);
246 }
247  
248 14854 ▶sub isAnyIPv4 {
Subroutine isAnyIPv4: called CC=2 SCAR=6.9
Pod documented
249 14854 my $naddr = $_[0];
250 14854 my $rv = isIPv4($_[0]);
251 14840 ▶ return $rv if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 14490 350 14840
252 350 return isNewIPv4($naddr);
253 }
254  
255 1 ▶sub DESTROY { }
Subroutine DESTROY: called CC=1 SCAR=0.0
256  
257 299 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
258 299 _case_tags(\@_);
259 299 NetAddr::IP::InetBase->export_to_level(1, @_);
260 }
261  
262 176 1;
263  
264  
265 # one octet of a dotted quad tail: decimal, without the leading zero inet_aton reads as octal
266 176 my $TAIL_OCTET = qr/0|[1-9][0-9]{0,2}/;
267  
268 4481 ▶sub ipv6_aton {
Subroutine ipv6_aton: called CC=21 SCAR=30.5
Pod documented
269 4481 my ($ipv6) = @_;
270 4481 ▶ return undef unless $ipv6;
Branch: 1 branch
BranchTrueFalseTotal
unless $ipv6 1 4480 4481
271 4480 ▶ if (my ($hex, @quad) = $ipv6 =~ /^(.*:)($TAIL_OCTET)\.($TAIL_OCTET)\.($TAIL_OCTET)\.($TAIL_OCTET)\z/)
Branch: 1 branch
BranchTrueFalseTotal
if (() = $ipv6 =~ /^(.*:)($TAIL_OCTET)\.($TAIL_OCTET)\.($TAIL_OCTET)\.($TAIL_OCTET)\z/) 922 3558 4480
272 { # mixed hex, dot-quad
273 922 ▶ return undef if grep { $_ > $MAX_OCTET } @quad;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ > $NetAddr::IP::InetBase::MAX_OCTET;} @quad 1 921 922
274 921 $ipv6 = sprintf("%s%X%02X:%X%02X", $hex, @quad); # convert to pure hex
275 }
276 4479 my $c;
277 return undef
278 4479 ▶ if $ipv6 =~ /[^:0-9a-fA-F]/ || # non-hex character
Branch: 1 branch
BranchTrueFalseTotal
if $ipv6 =~ /[^:0-9a-fA-F]/ or ($c = $ipv6) =~ s/::/x/ and $c =~ /(?:x|:):/ or $ipv6 =~ /[0-9a-fA-F]{5,}/ or $ipv6 =~ /^:(?!:)/ or $ipv6 =~ /(?<!:):$/ 111 4368 4479
Condition80%12/15
$ipv6 =~ /[^:0-9a-fA-F]/ or ($c = $ipv6) =~ s/::/x/ and $c =~ /(?:x|:):/ or $ipv6 =~ /[0-9a-fA-F]{5,}/ or $ipv6 =~ /^:(?!:)/ or $ipv6 =~ /(?<!:):$/
l!l&&r!l&&!r
10924368
$ipv6 =~ /[^:0-9a-fA-F]/ or ($c = $ipv6) =~ s/::/x/ and $c =~ /(?:x|:):/ or $ipv6 =~ /[0-9a-fA-F]{5,}/ or $ipv6 =~ /^:(?!:)/
l!l&&r!l&&!r
10814370
$ipv6 =~ /[^:0-9a-fA-F]/ or ($c = $ipv6) =~ s/::/x/ and $c =~ /(?:x|:):/ or $ipv6 =~ /[0-9a-fA-F]{5,}/
l!l&&r!l&&!r
10804371
$ipv6 =~ /[^:0-9a-fA-F]/ or ($c = $ipv6) =~ s/::/x/ and $c =~ /(?:x|:):/
l!l&&r!l&&!r
10804371
($c = $ipv6) =~ s/::/x/ and $c =~ /(?:x|:):/
!ll&&!rl&&r
83735340
MC/DC50%3/6
$ipv6 =~ /[^:0-9a-fA-F]/ or ($c = $ipv6) =~ s/::/x/ and $c =~ /(?:x|:):/ or $ipv6 =~ /[0-9a-fA-F]{5,}/ or $ipv6 =~ /^:(?!:)/ or $ipv6 =~ /(?<!:):$/
$ipv6 =~ /[^:0-9a-fA-F]/ ($c = $ipv6) =~ s/::/x/ $c =~ /(?:x|:):/ $ipv6 =~ /[0-9a-fA-F]{5,}/ $ipv6 =~ /^:(?!:)/ $ipv6 =~ /(?<!:):$/
Truth table60%6/10
A or B and C or D or E or F
A= $ipv6 =~ /[^:0-9a-fA-F]/B= ($c = $ipv6) =~ s/::/x/C= $c =~ /(?:x|:):/D= $ipv6 =~ /[0-9a-fA-F]{5,}/E= $ipv6 =~ /^:(?!:)/F= $ipv6 =~ /(?<!:):$/
ABCDEFresult
1XXXXX1
011XXX1
00X1XX1
0101XX1
00X01X1
01001X1
00X0011
0100011
00X0000
0100000
279 (($c = $ipv6) =~ s/::/x/ && $c =~ /(?:x|:):/) || # double :: ::?
280 $ipv6 =~ /[0-9a-fA-F]{5,}/ || # more than 4 digits
281 $ipv6 =~ /^:(?!:)/ || # single leading colon
282 $ipv6 =~ /(?<!:):$/; # single trailing colon
283 4368 $c = $ipv6 =~ tr/:/:/; # count the colons
284 4368 ▶ return undef if $c < 7 && $ipv6 !~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
if $c < 7 and not $ipv6 =~ /::/ 7 4361 4368
Condition100%3/3
$c < 7 and not $ipv6 =~ /::/
!ll&&!rl&&r
88834737
MC/DC100%2/2
$c < 7 and not $ipv6 =~ /::/
$c < 7 not $ipv6 =~ /::/
Truth table100%3/3
A and not(B)
A= $c < 7B= not $ipv6 =~ /::/
ABresult
0X0
100
111
285  
286 4361 ▶ if ($c > 7) { # strip leading or trailing ::
Branch: 1 branch
BranchTrueFalseTotal
if ($c > 7) 30 4331 4361
287 return undef
288 30 ▶ unless $ipv6 =~ s/^::/:/
Branch: 1 branch
BranchTrueFalseTotal
unless $ipv6 =~ s/^::/:/ or $ipv6 =~ s/::$/:/ 1 29 30
Condition100%3/3
$ipv6 =~ s/^::/:/ or $ipv6 =~ s/::$/:/
l!l&&r!l&&!r
2091
MC/DC100%2/2
$ipv6 =~ s/^::/:/ or $ipv6 =~ s/::$/:/
$ipv6 =~ s/^::/:/ $ipv6 =~ s/::$/:/
Truth table100%3/3
A or B
A= $ipv6 =~ s/^::/:/B= $ipv6 =~ s/::$/:/
ABresult
1X1
011
000
289 || $ipv6 =~ s/::$/:/;
290 29 ▶ return undef if --$c > 7;
Branch: 1 branch
BranchTrueFalseTotal
if --$c > 7 1 28 29
291 }
292 4359 ▶ while ($c++ < 7) { # expand compressed fields
Branch: 1 branch
BranchTrueFalseTotal
if ($c++ < 7) 14060 4359 18419
293 14060 $ipv6 =~ s/::/:::/;
294 }
295 4359 ▶ $ipv6 .= 0 if $ipv6 =~ /:$/;
Branch: 1 branch
BranchTrueFalseTotal
if $ipv6 =~ /:$/ 1103 3256 4359
296 4359 my @hex = split(/:/, $ipv6);
297 4359 ▶ return undef if @hex > 8; # too many fields
Branch: 1 branch
BranchTrueFalseTotal
if @hex > 8 0 4359 4359
298 4359 for (0 .. $#hex) {
299 34872 ▶ $hex[$_] = hex($hex[$_] || 0);
Condition100%2/2
$hex[$_] || 0
l!l
869026182
MC/DC100%1/1
$hex[$_] || 0
$hex[$_]
Truth table100%2/2
A
A= $hex[$_]
Aresult
11
00
300 }
301 4359 pack("n8", @hex);
302 }
303  
304  
305 934 ▶sub inet_any2n($) {
Subroutine inet_any2n: called CC=5 SCAR=16.1
Pod documented
306 934 my ($addr) = @_;
307 934 ▶ $addr = '' unless defined $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr 1 933 934
308  
309 # text without a colon is IPv4, so it has to be a dotted quad; a bare number is no address
310 934 ▶ if ($addr !~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($addr =~ /:/) 754 180 934
311 754 ▶ return undef unless $addr eq '' || $addr =~ /\A[0-9]+(?:[.][0-9]+){3}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $addr eq "" or $addr =~ /\A[0-9]+(?:[.][0-9]+){3}\z/ 8 746 754
Condition100%3/3
$addr eq "" or $addr =~ /\A[0-9]+(?:[.][0-9]+){3}\z/
l!l&&r!l&&!r
27448
MC/DC100%2/2
$addr eq "" or $addr =~ /\A[0-9]+(?:[.][0-9]+){3}\z/
$addr eq "" $addr =~ /\A[0-9]+(?:[.][0-9]+){3}\z/
Truth table100%3/3
A or B
A= $addr eq ""B= $addr =~ /\A[0-9]+(?:[.][0-9]+){3}\z/
ABresult
1X1
011
000
312 746 $addr = '::' . $addr;
313 }
314 926 return ipv6_aton($addr);
315 }
316  
317  
318 94 ▶sub inet_pton {
Subroutine inet_pton: called CC=10 SCAR=23.0
Pod documented
319 94 my ($af, $text) = @_;
320  
321 # A numeric compare, or Socket's int typemap, warns on undef and on
322 # text that is not a number. Those families croak here, with no warning.
323 94 ▶ croak sprintf(
Branch: 2 branches
BranchTrueFalseTotal
unless defined $af and &looks_like_number($af) 7 87 94
defined $af ? : 6 1 7
Condition100%3/3
defined $af and &looks_like_number($af)
!ll&&!rl&&r
1687
MC/DC100%2/2
defined $af and &looks_like_number($af)
defined $af &looks_like_number($af)
Truth table100%3/3
A and B
A= defined $afB= &looks_like_number($af)
ABresult
0X0
100
111
324 'Bad address family for %s::inet_pton, got %s, %s',
325 __PACKAGE__, defined $af ? $af : 'undefined', $FAMILY_SHOULD_BE
326 ) unless defined $af && looks_like_number($af);
327  
328 # any other number gets Socket's error
329 87 ▶ goto &Socket::inet_pton unless $af == AF_INET() || $af == AF_INET6();
Branch: 1 branch
BranchTrueFalseTotal
unless $af == 2 or $af == 10 2 85 87
Condition100%3/3
$af == 2 or $af == 10
l!l&&r!l&&!r
41442
MC/DC100%2/2
$af == 2 or $af == 10
$af == 2 $af == 10
Truth table100%3/3
A or B
A= $af == 2B= $af == 10
ABresult
1X1
011
000
330  
331 # the text is read here, not by the platform inet_pton, so every host
332 # reads it alike
333 85 ▶ if ($af == AF_INET()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($af == 2) 41 44 85
334 41 ▶ my @octets =
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 39 2 41
335 defined $text
336 ? $text =~ /\A($TAIL_OCTET)\.($TAIL_OCTET)\.($TAIL_OCTET)\.($TAIL_OCTET)\z/
337 : ();
338 41 ▶ return undef if !@octets || grep { $_ > $MAX_OCTET } @octets;
Branch: 1 branch
BranchTrueFalseTotal
if not @octets or grep {$_ > $NetAddr::IP::InetBase::MAX_OCTET;} @octets 21 20 41
Condition100%3/3
not @octets or grep {$_ > $NetAddr::IP::InetBase::MAX_OCTET;} @octets
l!l&&r!l&&!r
20120
MC/DC100%2/2
not @octets or grep {$_ > $NetAddr::IP::InetBase::MAX_OCTET;} @octets
not @octets grep {$_ > $NetAddr::IP::InetBase::MAX_OCTET;} @octets
Truth table100%3/3
not(A) or B
A= not @octetsB= grep {$_ > $NetAddr::IP::InetBase::MAX_OCTET;} @octets
ABresult
1X1
011
000
339 20 return pack 'C4', @octets;
340 }
341 44 return ipv6_aton($text);
342 }
343  
344  
345 35 ▶sub inet_ntoa {
Subroutine inet_ntoa: called CC=5 SCAR=16.1
Pod documented
346 35 my $packed = $_[0];
347 35 ▶ croak sprintf($WIDE_DIES, __PACKAGE__ . '::inet_ntoa')
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($packed, 1) 2 33 35
348 unless utf8::downgrade($packed, 1);
349 33 ▶ croak 'Bad arg length for '
Branch: 2 branches
BranchTrueFalseTotal
unless defined $packed and length $packed == $NetAddr::IP::InetBase::V4_PACKED_BYTES 6 27 33
defined $packed ? : 4 2 6
Condition100%3/3
defined $packed and length $packed == $NetAddr::IP::InetBase::V4_PACKED_BYTES
!ll&&!rl&&r
2427
MC/DC100%2/2
defined $packed and length $packed == $NetAddr::IP::InetBase::V4_PACKED_BYTES
defined $packed length $packed == $NetAddr::IP::InetBase::V4_PACKED_BYTES
Truth table100%3/3
A and B
A= defined $packedB= length $packed == $NetAddr::IP::InetBase::V4_PACKED_BYTES
ABresult
0X0
100
111
350 . __PACKAGE__
351 . "::inet_ntoa, length is "
352 . (defined $packed ? length($packed) : 'undefined')
353 . " should be $V4_PACKED_BYTES bytes"
354 unless defined $packed && length($packed) == $V4_PACKED_BYTES;
355 27 my @hex = (unpack("n2", $packed));
356 27 $hex[3] = $hex[1] & $MAX_OCTET;
357 27 $hex[2] = $hex[1] >> $OCTET_BITS;
358 27 $hex[1] = $hex[0] & $MAX_OCTET;
359 27 $hex[0] >>= $OCTET_BITS;
360 27 return sprintf("%d.%d.%d.%d", @hex);
361 }
362  
363  
364 35 ▶sub ipv6_ntoa {
Subroutine ipv6_ntoa: called CC=5 SCAR=16.1
Pod documented
365 35 my $packed = $_[0];
366  
367 # measured here, so the error names ipv6_ntoa, and the string is
368 # downgraded before Socket reads it
369 35 ▶ croak sprintf($WIDE_DIES, __PACKAGE__ . '::ipv6_ntoa')
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($packed, 1) 2 33 35
370 unless utf8::downgrade($packed, 1);
371 33 ▶ croak 'Bad arg length for '
Branch: 2 branches
BranchTrueFalseTotal
unless defined $packed and length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES 11 22 33
defined $packed ? : 8 3 11
Condition100%3/3
defined $packed and length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
!ll&&!rl&&r
3822
MC/DC100%2/2
defined $packed and length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
defined $packed length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
Truth table100%3/3
A and B
A= defined $packedB= length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
ABresult
0X0
100
111
372 . __PACKAGE__
373 . "::ipv6_ntoa, length is "
374 . (defined $packed ? length($packed) : 'undefined')
375 . " should be $V6_PACKED_BYTES bytes"
376 unless defined $packed && length($packed) == $V6_PACKED_BYTES;
377 22 return inet_ntop(AF_INET6(), $packed);
378 }
379  
380  
381 14412 ▶sub inet_n2dx($) {
Subroutine inet_n2dx: called CC=2 SCAR=6.9
Pod documented
382 14412 my ($nadr) = @_;
383 14412 ▶ if (isAnyIPv4($nadr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isAnyIPv4($nadr)) 14403 5 14408
384 14403 local $1;
385 14403 ipv6_n2d($nadr) =~ /([^:]+)$/;
386 14403 return $1;
387 }
388 5 return ipv6_n2x($nadr);
389 }
390  
391  
392 11 ▶sub inet_n2ad($) {
Subroutine inet_n2ad: called CC=2 SCAR=6.9
Pod documented
393 11 my ($nadr) = @_;
394 11 my $addr = ipv6_n2d($nadr);
395 7 ▶ return $addr unless isAnyIPv4($nadr);
Branch: 1 branch
BranchTrueFalseTotal
unless isAnyIPv4($nadr) 5 2 7
396 2 local $1;
397 2 $addr =~ /([^:]+)$/;
398 2 return $1;
399 }
400  
401  
402 73 ▶sub inet_ntop {
Subroutine inet_ntop: called CC=10 SCAR=23.1
Pod documented
403 73 my ($af, $packed) = @_;
404  
405 # Same gate as inet_pton: undef and a non-number must not reach Socket.
406 73 ▶ croak sprintf(
Branch: 2 branches
BranchTrueFalseTotal
unless defined $af and &looks_like_number($af) 7 66 73
defined $af ? : 6 1 7
Condition100%3/3
defined $af and &looks_like_number($af)
!ll&&!rl&&r
1666
MC/DC100%2/2
defined $af and &looks_like_number($af)
defined $af &looks_like_number($af)
Truth table100%3/3
A and B
A= defined $afB= &looks_like_number($af)
ABresult
0X0
100
111
407 'Unsupported address family for %s::inet_ntop, af is %s, %s',
408 __PACKAGE__, defined $af ? $af : 'undefined', $FAMILY_SHOULD_BE
409 ) unless defined $af && looks_like_number($af);
410 66 ▶ if ($af == AF_INET()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($af == 2) 16 50 66
411  
412 # inet_ntoa reads $_[0]. The family must not go with it.
413 16 @_ = ($packed);
414 16 goto &inet_ntoa;
415 }
416 50 ▶ if ($af == AF_INET6()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($af == 10) 48 2 50
417  
418 # Socket 1.94 copies 16 bytes and does not check the length, so a
419 # shorter string would be read past its end.
420 48 ▶ croak sprintf($WIDE_DIES, __PACKAGE__ . '::inet_ntop')
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($packed, 1) 0 48 48
421 unless utf8::downgrade($packed, 1);
422 48 ▶ croak 'Bad arg length for '
Branch: 2 branches
BranchTrueFalseTotal
unless defined $packed and length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES 3 45 48
defined $packed ? : 3 0 3
Condition66%2/3
defined $packed and length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
!ll&&!rl&&r
0345
MC/DC50%1/2
defined $packed and length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
defined $packed length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
Truth table66%2/3
A and B
A= defined $packedB= length $packed == $NetAddr::IP::InetBase::V6_PACKED_BYTES
ABresult
0X0
100
111
423 . __PACKAGE__
424 . "::inet_ntop, length is "
425 . (defined $packed ? length($packed) : 'undefined')
426 . " should be $V6_PACKED_BYTES bytes"
427 unless defined $packed && length($packed) == $V6_PACKED_BYTES;
428 }
429 47 goto &Socket::inet_ntop;
430 }
431  
432  
433 37 ▶sub packzeros {
Subroutine packzeros: called CC=9 SCAR=22.0
Pod documented
434 37 my $x6 = shift;
435 37 ▶ if ($x6 =~ /\:\:/) { # already contains ::
Branch: 1 branch
BranchTrueFalseTotal
if ($x6 =~ /\:\:/) 16 21 37
436 # then re-optimize
437 16 ▶ $x6 = ($x6 =~ /\:[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) # ipv4 notation ?
Branch: 1 branch
BranchTrueFalseTotal
$x6 =~ /\:[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ ? : 6 10 16
438 ? ipv6_n2d(ipv6_aton($x6))
439 : ipv6_n2x(ipv6_aton($x6));
440 }
441 37 $x6 = ':' . lc $x6; # prefix : & always lower case
442 37 my $d = '';
443 37 ▶ if ($x6 =~ /(.+\:)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) { # if contains dot quad
Branch: 1 branch
BranchTrueFalseTotal
if ($x6 =~ /(.+\:)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) 17 20 37
444 17 $x6 = $1; # save hex piece
445 17 $d = $2; # and dot quad piece
446 }
447 37 $x6 .= ':'; # suffix :
448 37 $x6 =~ s/\:0+/\:0/g; # compress strings of 0's to single '0'
449 37 $x6 =~ s/\:0([1-9a-f]+)/\:$1/g; # eliminate leading 0's in hex strings
450 37 my @x = $x6 =~ /(?:\:0)*/g; # split only strings of :0:0..."
451  
452 37 my $m = 0;
453 37 my $i = 0;
454  
455 37 for (0 .. $#x) { # find next longest pattern :0:0:0...
456 341 my $len = length($x[$_]);
457 341 ▶ next unless $len > $m;
Branch: 1 branch
BranchTrueFalseTotal
unless $len > $m 298 43 341
458 43 $m = $len;
459 43 $i = $_; # index to first longest pattern
460 }
461  
462 37 ▶ if ($m > 2) { # there was a string of 2 or more zeros
Branch: 1 branch
BranchTrueFalseTotal
if ($m > 2) { } 37 0 37
463 37 $x6 =~ s/$x[$i]/\:/; # replace first longest :0:0:0... with "::"
464 37 ▶ unless ($i) { # if it is the first match, $i = 0
Branch: 1 branch
BranchTrueFalseTotal
if ($i) { } 14 23 37
465 23 $x6 = substr($x6, 0, -1); # keep the leading ::, remove trailing ':'
466 }
467 else {
468 14 $x6 = substr($x6, 1, -1); # else remove leading & trailing ':'
469 }
470 37 ▶ $x6 .= ':' unless $x6 =~ /\:\:/; # restore ':' if match and we can't see it, implies trailing '::'
Branch: 1 branch
BranchTrueFalseTotal
unless $x6 =~ /\:\:/ 10 27 37
471 }
472 else { # there was no match
473 0 $x6 = substr($x6, 1, -1); # remove leading & trailing ':'
474 }
475 37 $x6 .= $d; # append digits if any
476 37 return $x6; # lowercase whatever the case setting, RFC 5952 s4.3
477 }
478  
479  
480 176 1;
481  
482 __END__
« IP.pm ↑ Summary IP/UtilPP.pm »