IP/Util.pm

filter: Statementstmt 95.7% 113 / 118 Branchbran 89.5% 43 / 48 Conditioncond 66.6% 12 / 18 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 21 / 21 Podpod 100% 4 / 4 totaltotal 90.0% 198 / 220 CC 29 SCAR 14.2
CC
29
Cov
91%
_end_gethostbyname
8.6
_gethostbyname
7.0
inet_4map6
4.1
CRAP 4.1
« IP/Count.pm ↑ Summary IP/Constants.pm »
1 #!/bin/false
2 # ABSTRACT: Native C and pure perl implementations of IPv4 and IPv6 address utilities
3 # PODNAME: NetAddr::IP::Util
4  
5 158 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 158 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package NetAddr::IP::Util;
9  
10 158 our $VERSION = '4.080_09'; # TRIAL VERSION
11  
12 158 ▶use parent qw(Exporter DynaLoader);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 158 ▶use DynaLoader ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 158 our @EXPORT_OK = qw(
16 inet_aton
17 inet_ntoa
18 ipv6_aton
19 ipv6_ntoa
20 ipv6_n2x
21 ipv6_n2d
22 inet_any2n
23 hasbits
24 isIPv4
25 isNewIPv4
26 isAnyIPv4
27 inet_n2dx
28 inet_n2ad
29 inet_pton
30 inet_ntop
31 inet_4map6
32 shiftleft
33 addconst
34 add128
35 sub128
36 notcontiguous
37 bin2bcd
38 bcd2bin
39 mode
40 ipv4to6
41 mask4to6
42 ipanyto6
43 maskanyto6
44 ipv6to4
45 bin2bcdn
46 bcdn2txt
47 bcdn2bin
48 simple_pack
49 comp128
50 packzeros
51 AF_INET
52 AF_INET6
53 naip_gethostbyname
54 havegethostbyname2
55 );
56 158 our %EXPORT_TAGS = (
57 all => [@EXPORT_OK],
58 inet => [ qw(
59 inet_aton
60 inet_ntoa
61 ipv6_aton
62 ipv6_ntoa
63 ipv6_n2x
64 ipv6_n2d
65 inet_any2n
66 inet_n2dx
67 inet_n2ad
68 inet_pton
69 inet_ntop
70 inet_4map6
71 ipv4to6
72 mask4to6
73 ipanyto6
74 maskanyto6
75 ipv6to4
76 packzeros
77 naip_gethostbyname
78 )
79 ],
80 math => [ qw(
81 shiftleft
82 hasbits
83 isIPv4
84 isNewIPv4
85 isAnyIPv4
86 addconst
87 add128
88 sub128
89 notcontiguous
90 bin2bcd
91 bcd2bin
92 )
93 ],
94 ipv4 => [ qw(
95 inet_aton
96 inet_ntoa
97 )
98 ],
99 ipv6 => [ qw(
100 ipv6_aton
101 ipv6_ntoa
102 ipv6_n2x
103 ipv6_n2d
104 inet_any2n
105 inet_n2dx
106 inet_n2ad
107 inet_pton
108 inet_ntop
109 inet_4map6
110 ipv4to6
111 mask4to6
112 ipanyto6
113 maskanyto6
114 ipv6to4
115 packzeros
116 naip_gethostbyname
117 )
118 ],
119 );
120 158 our $Mode;
121  
122 158 ▶use NetAddr::IP::Constants qw($V4_PACKED_BYTES $V6_PACKED_BYTES);
Subroutine BEGIN: called CC=1 SCAR=0.0
123 158 ▶use NetAddr::IP::Util_IS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
124 158 ▶use NetAddr::IP::InetBase qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
125 :upper
126 AF_INET
127 AF_INET6
128 inet_any2n
129 inet_aton
130 inet_n2ad
131 inet_n2dx
132 inet_ntoa
133 inet_ntop
134 inet_pton
135 ipv6_aton
136 ipv6_n2d
137 ipv6_n2x
138 ipv6_ntoa
139 isAnyIPv4
140 isIPv4
141 isNewIPv4
142 packzeros
143 158 );
144  
145 158 *NetAddr::IP::Util::upper = \&NetAddr::IP::InetBase::upper;
146 158 *NetAddr::IP::Util::lower = \&NetAddr::IP::InetBase::lower;
147  
148 158 my $xs_ok;
149 158 ▶if (NetAddr::IP::Util_IS->not_pure) {
Branch: 1 branch
BranchTrueFalseTotal
if ("NetAddr::IP::Util_IS"->not_pure) 158 0 158
150 158 my $xs_err;
151 158 eval { ## attempt to load 'C' version of utilities
152 158 local $SIG{__DIE__};
153 158 __PACKAGE__->bootstrap;
154 };
155 158 $xs_err = $@;
156 158 $xs_ok = !$xs_err;
157 158 ▶ warn "XS bootstrap failed with: $xs_err\n" if $xs_err;
Branch: 1 branch
BranchTrueFalseTotal
if $xs_err 0 158 158
158 }
159 158 ▶if (NetAddr::IP::Util_IS->pure || !$xs_ok) { ## load the pure perl version if 'C' lib missing
Branch: 1 branch
BranchTrueFalseTotal
if ('NetAddr::IP::Util_IS'->pure or not $xs_ok) { } 0 158 158
Condition33%1/3
"NetAddr::IP::Util_IS"->pure or not $xs_ok
l!l&&r!l&&!r
00158
MC/DC0%0/2
"NetAddr::IP::Util_IS"->pure or not $xs_ok
"NetAddr::IP::Util_IS"->pure not $xs_ok
Truth table33%1/3
A or not(B)
A= "NetAddr::IP::Util_IS"->pureB= not $xs_ok
ABresult
1X1
011
000
160 0 require NetAddr::IP::UtilPP;
161 0 import NetAddr::IP::UtilPP qw( :all );
162 0 $Mode = 'Pure Perl';
163 }
164 else {
165 158 $Mode = 'CC XS';
166 }
167  
168 # if Socket lib is broken in some way, check for overange values
169 #
170 #my $overange = yinet_aton('256.1') ? 1:0;
171 #my $overange = gethostbyname('256.1') ? 1:0;
172  
173 1 ▶sub mode() { $Mode }
Subroutine mode: called CC=1 SCAR=0.0
Pod documented
174  
175 158 my $_newV4compat = pack('N4', 0, 0, 0xffff, 0);
176  
177 22 ▶sub inet_4map6 {
Subroutine inet_4map6: called CC=4 SCAR=14.0
Pod documented
178 22 my $naddr = shift;
179 22 ▶ if (length($naddr) == $V4_PACKED_BYTES) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $naddr == $NetAddr::IP::Util::V4_PACKED_BYTES) { } 2 20 22
elsif (length $naddr == $NetAddr::IP::Util::V6_PACKED_BYTES) { } 20 0 20
180 2 $naddr = ipv4to6($naddr);
181 }
182 elsif (length($naddr) == $V6_PACKED_BYTES) {
183 ; # is OK
184 20 ▶ return undef unless isAnyIPv4($naddr);
Branch: 1 branch
BranchTrueFalseTotal
unless isAnyIPv4($naddr) 3 17 20
185 }
186 else {
187 0 return undef;
188 }
189 19 $naddr |= $_newV4compat;
190 19 return $naddr;
191 }
192  
193 1 ▶sub DESTROY { }
Subroutine DESTROY: called CC=1 SCAR=0.0
194  
195 # True on every supported perl. :noSock6 clears $_Sock6ok, and
196 # havegethostbyname2 then returns false.
197 158 my $havegethostbyname2 = 1;
198  
199 158 my $_Sock6ok = 1;
200  
201 85 ▶sub havegethostbyname2 {
Subroutine havegethostbyname2: called CC=2 SCAR=6.9
Pod documented
202 85 ▶ return $_Sock6ok
Branch: 1 branch
BranchTrueFalseTotal
$_Sock6ok ? : 74 11 85
203 ? $havegethostbyname2
204 : 0;
205 }
206  
207 213 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
208 213 ▶ if (grep { $_ eq ':noSock6' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":noSock6";} @_) 4 209 213
209 4 $_Sock6ok = 0;
210 4 @_ = grep { $_ ne ':noSock6' } @_;
211 }
212 213 NetAddr::IP::Util->export_to_level(1, @_);
213 }
214  
215 # Socket's default export includes inet_aton and inet_ntoa, which this
216 # module defines itself. The lookup names stay in this package. The block
217 # ends back in NetAddr::IP::Util.
218 158 package NetAddr::IP::UtilPolluted {
219  
220 158 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
221 158 ▶ use Socket qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
222 AF_INET
223 AF_INET6
224 SOCK_STREAM
225 getaddrinfo
226 unpack_sockaddr_in
227 unpack_sockaddr_in6
228 158 );
229  
230 158 my $_v4zero = pack('L', 0);
231 158 my $_zero = pack('L4', 0, 0, 0, 0);
232  
233 # getaddrinfo can hand back the same address more than once. The first
234 # copy is the one kept, in the order the results arrived.
235 121 ▶ sub _addrs_once {
Subroutine _addrs_once: called CC=4 SCAR=13.9
236 121 my ($unpack, @res) = @_;
237 121 my (@addrs, %seen);
238 121 for my $res (@res) {
239 117 my (undef, $addr) = $unpack->($res->{addr});
240 117 ▶ next unless defined $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr 1 116 117
241 116 ▶ next if $seen{$addr}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$addr}++ 4 112 116
242 112 push @addrs, $addr;
243 }
244 121 return @addrs;
245 }
246  
247 14 ▶ sub _end_gethostbyname {
Subroutine _end_gethostbyname: called CC=8 SCAR=21.5
248  
249 # my ($name, $aliases, $addrtype, $length, @addrs) = @_;
250 14 my @rv = @_;
251  
252 # first ip address = rv[4]
253 14 my $tip = $rv[4];
254 14 ▶ unless ($tip && $tip ne $_v4zero && $tip ne $_zero) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tip and $tip ne $_v4zero and $tip ne $_zero) { } 13 1 14
if (do { $rv[3] and $rv[3] == $NetAddr::IP::Util::V4_PACKED_BYTES }) { } 9 4 13
elsif ($rv[3] == $NetAddr::IP::Util::V6_PACKED_BYTES) { } 4 0 4
Condition66%4/6
$tip and $tip ne $_v4zero and $tip ne $_zero
!ll&&!rl&&r
1013
$tip and $tip ne $_v4zero
!ll&&!rl&&r
0113
MC/DC33%1/3
$tip and $tip ne $_v4zero and $tip ne $_zero
$tip $tip ne $_v4zero $tip ne $_zero
Truth table50%2/4
A and B and C
A= $tipB= $tip ne $_v4zeroC= $tip ne $_zero
ABCresult
0XX0
10X0
1100
1111
255 1 @rv = ();
256 }
257  
258 # length = rv[3]
259 ▶ elsif ($rv[3] && $rv[3] == $NetAddr::IP::Util::V4_PACKED_BYTES) {
Condition66%2/3
$rv[3] and $rv[3] == $NetAddr::IP::Util::V4_PACKED_BYTES
!ll&&!rl&&r
049
MC/DC50%1/2
$rv[3] and $rv[3] == $NetAddr::IP::Util::V4_PACKED_BYTES
$rv[3] $rv[3] == $NetAddr::IP::Util::V4_PACKED_BYTES
Truth table66%2/3
A and B
A= $rv[3]B= $rv[3] == $NetAddr::IP::Util::V4_PACKED_BYTES
ABresult
0X0
100
111
260 9 for (4 .. $#rv) {
261 12 $rv[$_] = NetAddr::IP::Util::inet_4map6(NetAddr::IP::Util::ipv4to6($rv[$_]));
262 }
263 9 $rv[3] = $NetAddr::IP::Util::V6_PACKED_BYTES; # unconditionally set length to 16
264 }
265 elsif ($rv[3] == $NetAddr::IP::Util::V6_PACKED_BYTES) {
266 ; # is ok
267 }
268 else {
269 0 @rv = ();
270 }
271 14 return @rv;
272 }
273  
274 # Text that ends in a label of digits or 0x hex, and is not the decimal dotted quad
275 # fillIPv4 accepts, is a number to the resolver, read the inet_aton way with octal parts.
276 107 ▶ sub _numeric_text {
Subroutine _numeric_text: called CC=3 SCAR=11.0
277 107 my ($host) = @_;
278 107 ▶ return 0 if index($host, ':') >= 0 || $host =~ /\A[0-9]+(?:[.][0-9]+){3}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if index($host, ":") != -1 or $host =~ /\A[0-9]+(?:[.][0-9]+){3}\z/ 9 98 107
Condition100%3/3
index($host, ":") != -1 or $host =~ /\A[0-9]+(?:[.][0-9]+){3}\z/
l!l&&r!l&&!r
3698
MC/DC100%2/2
index($host, ":") != -1 or $host =~ /\A[0-9]+(?:[.][0-9]+){3}\z/
index($host, ":") != -1 $host =~ /\A[0-9]+(?:[.][0-9]+){3}\z/
Truth table100%3/3
A or B
A= index($host, ":") != -1B= $host =~ /\A[0-9]+(?:[.][0-9]+){3}\z/
ABresult
1X1
011
000
279 98 return $host =~ /(?:\A|[.])(?:[0-9]+|0[xX][0-9a-fA-F]*)[.]?\z/;
280 }
281  
282 # fillIPv4 has already rejected a leading zero and an octet past 255.
283 # What remains is a decimal dotted quad, so the resolver is not asked.
284 15 ▶ sub _packed_quad {
Subroutine _packed_quad: called CC=2 SCAR=6.9
285 15 my ($host) = @_;
286 15 ▶ return unless $host =~ /\A([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $host =~ /\A([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)\z/ 9 6 15
287 6 return pack('C4', $1, $2, $3, $4);
288 }
289  
290 # No family, so one call returns AAAA and A. SOCK_STREAM asks for each
291 # address once; Windows rejects SOCK_RAW here unless a protocol is set.
292 # An empty service is a NULL service pointer. An error is an empty return.
293 # The two lists keep the order the results arrived, with duplicates gone.
294 78 ▶ sub _lookup_both {
Subroutine _lookup_both: called CC=2 SCAR=6.9
295 78 my ($name) = @_;
296 78 my ($err, @res) = getaddrinfo($name, '', { socktype => SOCK_STREAM });
297 78 ▶ return if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 18 60 78
298 my @v6 = _addrs_once(
299 \&unpack_sockaddr_in6,
300 60 grep { $_->{family} == AF_INET6() } @res
301 );
302 my @v4 = _addrs_once(
303 \&unpack_sockaddr_in,
304 60 grep { $_->{family} == AF_INET() } @res
305 );
306 60 return (\@v4, \@v6);
307 }
308  
309 31 ▶ sub _gethostbyname {
Subroutine _gethostbyname: called CC=7 SCAR=19.5
310 31 my ($name) = @_;
311  
312 # A leading zero or an octet past 255 is not an address. fillIPv4
313 # returns undef for those. getaddrinfo would read the zero as octal.
314 31 my $host = NetAddr::IP::InetBase::fillIPv4($name);
315 31 ▶ return unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 10 21 31
316 21 ▶ return if _numeric_text($host);
Branch: 1 branch
BranchTrueFalseTotal
if _numeric_text($host) 6 15 21
317  
318 # A literal is not a name. gethostbyname keeps one static buffer, so
319 # a short form such as 127.1 is packed after fillIPv4 expands it.
320 15 ▶ if (my $addr = _packed_quad($host)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $addr = _packed_quad($host)) 6 9 15
321 6 return &_end_gethostbyname($name, '', AF_INET(), $NetAddr::IP::Util::V4_PACKED_BYTES, $addr);
322 }
323  
324 # :noSock6 does not call getaddrinfo. The name goes to gethostbyname.
325 9 ▶ unless ($_Sock6ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_Sock6ok) 1 8 9
326 1 my @tip = gethostbyname($host);
327 1 return &_end_gethostbyname(@tip);
328 }
329 8 my ($v4, $v6) = _lookup_both($host);
330 8 ▶ return unless $v4;
Branch: 1 branch
BranchTrueFalseTotal
unless $v4 2 6 8
331  
332 # A name with both an AAAA and an A answer returns only the IPv6
333 # addresses.
334 6 ▶ if (@$v6) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$v6) 4 2 6
335 4 return &_end_gethostbyname($name, '', AF_INET6(), $NetAddr::IP::Util::V6_PACKED_BYTES, @$v6);
336 }
337 2 return &_end_gethostbyname($name, '', AF_INET(), $NetAddr::IP::Util::V4_PACKED_BYTES, @$v4);
338 }
339  
340 }
341  
342 40 ▶sub naip_gethostbyname {
Subroutine naip_gethostbyname: called CC=4 SCAR=13.9
Pod documented
343 40 ▶ return if defined $_[0] && $_[0] =~ /[\0\t\n\x0B\f\r ]/; # no host name holds whitespace or a NUL byte
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] =~ /[\0\t\n\x0B\f\r ]/ 9 31 40
Condition66%2/3
defined $_[0] and $_[0] =~ /[\0\t\n\x0B\f\r ]/
!ll&&!rl&&r
0319
MC/DC50%1/2
defined $_[0] and $_[0] =~ /[\0\t\n\x0B\f\r ]/
defined $_[0] $_[0] =~ /[\0\t\n\x0B\f\r ]/
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] =~ /[\0\t\n\x0B\f\r ]/
ABresult
0X0
100
111
344 31 my @rv = NetAddr::IP::UtilPolluted::_gethostbyname($_[0]);
345 return wantarray
346 ? @rv
347 31 ▶ : $rv[4];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 12 19 31
348 }
349  
350 158 1;
351  
352  
353 158 1;
354  
355 __END__
« IP/Count.pm ↑ Summary IP/Constants.pm »