blib/lib/NetAddr/IP/Util.pm

filter: Statementstmt 79.3% 73 / 92 Branchbran 57.1% 16 / 28 Conditioncond 25.0% 6 / 24 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 82.3% 14 / 17 Podpod 100% 4 / 4 totaltotal 64.0% 114 / 178 CC 17 SCAR 18.2
CC
17
Cov
66%
__ANON__
20.0
_end_gethostbyname
10.4
inet_4map6
4.1
CRAP 6.2
« Lite/t/netaddr.t ↑ Summary Lite/t/v4-num.t »
1 #!/usr/bin/perl
2 package NetAddr::IP::Util;
3  
4 133 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use diagnostics;
6 #use lib qw(blib/lib);
7  
8 133 ▶use vars qw($VERSION @EXPORT_OK @ISA %EXPORT_TAGS $Mode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 133 ▶use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 133 ▶use NetAddr::IP::Util_IS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 133 ▶use NetAddr::IP::InetBase qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 :upper
13 :all
14 133 );
15  
16 133 *NetAddr::IP::Util::upper = \&NetAddr::IP::InetBase::upper;
17 133 *NetAddr::IP::Util::lower = \&NetAddr::IP::InetBase::lower;
18  
19 133 require DynaLoader;
20 133 require Exporter;
21  
22 133 @ISA = qw(Exporter DynaLoader);
23  
24 133 $VERSION = do { my @r = (q$Revision: 1.53 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
25  
26 133 @EXPORT_OK = qw(
27 inet_aton
28 inet_ntoa
29 ipv6_aton
30 ipv6_ntoa
31 ipv6_n2x
32 ipv6_n2d
33 inet_any2n
34 hasbits
35 isIPv4
36 isNewIPv4
37 isAnyIPv4
38 inet_n2dx
39 inet_n2ad
40 inet_pton
41 inet_ntop
42 inet_4map6
43 shiftleft
44 addconst
45 add128
46 sub128
47 notcontiguous
48 bin2bcd
49 bcd2bin
50 mode
51 ipv4to6
52 mask4to6
53 ipanyto6
54 maskanyto6
55 ipv6to4
56 bin2bcdn
57 bcdn2txt
58 bcdn2bin
59 simple_pack
60 comp128
61 packzeros
62 AF_INET
63 AF_INET6
64 naip_gethostbyname
65 havegethostbyname2
66 );
67  
68 133 %EXPORT_TAGS = (
69 all => [@EXPORT_OK],
70 inet => [qw(
71 inet_aton
72 inet_ntoa
73 ipv6_aton
74 ipv6_ntoa
75 ipv6_n2x
76 ipv6_n2d
77 inet_any2n
78 inet_n2dx
79 inet_n2ad
80 inet_pton
81 inet_ntop
82 inet_4map6
83 ipv4to6
84 mask4to6
85 ipanyto6
86 maskanyto6
87 ipv6to4
88 packzeros
89 naip_gethostbyname
90 )],
91 math => [qw(
92 shiftleft
93 hasbits
94 isIPv4
95 isNewIPv4
96 isAnyIPv4
97 addconst
98 add128
99 sub128
100 notcontiguous
101 bin2bcd
102 bcd2bin
103 )],
104 ipv4 => [qw(
105 inet_aton
106 inet_ntoa
107 )],
108 ipv6 => [qw(
109 ipv6_aton
110 ipv6_ntoa
111 ipv6_n2x
112 ipv6_n2d
113 inet_any2n
114 inet_n2dx
115 inet_n2ad
116 inet_pton
117 inet_ntop
118 inet_4map6
119 ipv4to6
120 mask4to6
121 ipanyto6
122 maskanyto6
123 ipv6to4
124 packzeros
125 naip_gethostbyname
126 )],
127 );
128  
129 133 ▶if (NetAddr::IP::Util_IS->not_pure) {
Branch: 1 branch
BranchTrueFalseTotal
if ("NetAddr::IP::Util_IS"->not_pure) 133 0 133
130 133 eval { ## attempt to load 'C' version of utilities
131 133 bootstrap NetAddr::IP::Util $VERSION;
132 };
133 }
134 133 ▶if (NetAddr::IP::Util_IS->pure || $@) { ## load the pure perl version if 'C' lib missing
Branch: 1 branch
BranchTrueFalseTotal
if ('NetAddr::IP::Util_IS'->pure or $@) { } 0 133 133
Condition33%1/3
"NetAddr::IP::Util_IS"->pure or $@
l!l&&r!l&&!r
00133
MC/DC0%0/2
"NetAddr::IP::Util_IS"->pure or $@
"NetAddr::IP::Util_IS"->pure $@
Truth table33%1/3
A or B
A= "NetAddr::IP::Util_IS"->pureB= $@
ABresult
1X1
011
000
135 0 require NetAddr::IP::UtilPP;
136 0 import NetAddr::IP::UtilPP qw( :all );
137 # require Socket;
138 # import Socket qw(inet_ntoa);
139 # *yinet_aton = \&Socket::inet_aton;
140 0 $Mode = 'Pure Perl';
141 }
142 else {
143 133 $Mode = 'CC XS';
144 }
145  
146 # if Socket lib is broken in some way, check for overange values
147 #
148 #my $overange = yinet_aton('256.1') ? 1:0;
149 #my $overange = gethostbyname('256.1') ? 1:0;
150  
151 1 ▶sub mode() { $Mode };
Subroutine mode: called CC=1 SCAR=0.0
Pod documented
152  
153 133 my $_newV4compat = pack('N4',0,0,0xffff,0);
154  
155 8 ▶sub inet_4map6 {
Subroutine inet_4map6: called CC=4 SCAR=14.0
Pod documented
156 8 my $naddr = shift;
157 8 ▶ if (length($naddr) == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $naddr == 4) { } 2 6 8
elsif (length $naddr == 16) { } 6 0 6
158 2 $naddr = ipv4to6($naddr);
159 }
160 elsif (length($naddr) == 16) {
161 ; # is OK
162 6 ▶ return undef unless isAnyIPv4($naddr);
Branch: 1 branch
BranchTrueFalseTotal
unless isAnyIPv4($naddr) 2 4 6
163 } else {
164 0 return undef;
165 }
166 6 $naddr |= $_newV4compat;
167 6 return $naddr;
168 }
169  
170 0 ▶sub DESTROY {};
Subroutine DESTROY: not called CC=1 SCAR=0.0
171  
172 133 my $havegethostbyname2 = 0;
173  
174 133 my $mygethostbyname;
175  
176 133 my $_Sock6ok = 1; # for testing gethostbyname
177  
178 11 ▶sub havegethostbyname2 {
Subroutine havegethostbyname2: called CC=2 SCAR=6.9
Pod documented
179 11 ▶ return $_Sock6ok
Branch: 1 branch
BranchTrueFalseTotal
$_Sock6ok ? : 10 1 11
180 ? $havegethostbyname2
181 : 0;
182 }
183  
184 171 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
185 171 ▶ if (grep { $_ eq ':noSock6' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":noSock6";} @_) 1 170 171
186 1 $_Sock6ok = 0;
187 1 @_ = grep { $_ ne ':noSock6' } @_;
188 }
189 171 NetAddr::IP::Util->export_to_level(1,@_);
190 }
191  
192 package NetAddr::IP::UtilPolluted;
193  
194 # Socket pollutes the name space with all of its symbols. Since
195 # we don't want them all, confine them to this name space.
196  
197 133 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
198 133 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
199  
200 133 my $_v4zero = pack('L',0);
201 133 my $_zero = pack('L4',0,0,0,0);
202  
203 # invoke replacement subroutine for Perl's "gethostbyname"
204 # if Socket6 is available.
205 #
206 # NOTE: in certain BSD implementations, Perl's gethostbyname is broken
207 # we will use our own InetBase::inet_aton instead
208  
209 4 ▶sub _end_gethostbyname {
Subroutine _end_gethostbyname: called CC=8 SCAR=23.4
210 # my ($name,$aliases,$addrtype,$length,@addrs) = @_;
211 4 my @rv = @_;
212 # first ip address = rv[4]
213 4 my $tip = $rv[4];
214 4 ▶ unless ($tip && $tip ne $_v4zero && $tip ne $_zero) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tip and $tip ne $_v4zero and $tip ne $_zero) { } 2 2 4
if (do { $rv[3] and $rv[3] == 4 }) { } 2 0 2
elsif ($rv[3] == 16) { } 0 0 0
Condition66%4/6
$tip and $tip ne $_v4zero and $tip ne $_zero
!ll&&!rl&&r
202
$tip and $tip ne $_v4zero
!ll&&!rl&&r
202
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
215 2 @rv = ();
216 }
217 # length = rv[3]
218 ▶ elsif ($rv[3] && $rv[3] == 4) {
Condition33%1/3
$rv[3] and $rv[3] == 4
!ll&&!rl&&r
002
MC/DC0%0/2
$rv[3] and $rv[3] == 4
$rv[3] $rv[3] == 4
Truth table33%1/3
A and B
A= $rv[3]B= $rv[3] == 4
ABresult
0X0
100
111
219 2 foreach (4..$#rv) {
220 2 $rv[$_] = NetAddr::IP::Util::inet_4map6(NetAddr::IP::Util::ipv4to6($rv[$_]));
221 }
222 2 $rv[3] = 16; # unconditionally set length to 16
223 }
224 elsif ($rv[3] == 16) {
225 ; # is ok
226 } else {
227 0 @rv = ();
228 }
229 4 return @rv;
230 }
231  
232 133 ▶unless ( eval { require Socket6 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Socket6 } }) { } 0 133 133
233 4 ▶ $mygethostbyname = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
234 # SEE NOTE above about broken BSD
235 4 my @tip = gethostbyname(NetAddr::IP::InetBase::fillIPv4($_[0]));
236 4 return &_end_gethostbyname(@tip);
237 133 };
238 } else {
239 0 import Socket6 qw( gethostbyname2 getipnodebyname );
240 0 my $try = eval { my @try = gethostbyname2('127.0.0.1',NetAddr::IP::Util::AF_INET()); $try[4] };
241 0 ▶ if (! $@ && $try && $try eq INADDR_LOOPBACK()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and $try and $try eq "\177\000\000\cA") { } 0 0 0
Condition0%0/6
not $@ and $try and $try eq "\177\0\0\1"
!ll&&!rl&&r
000
not $@ and $try
!ll&&!rl&&r
000
MC/DC0%0/3
not $@ and $try and $try eq "\177\0\0\1"
not $@ $try $try eq "\177\0\0\1"
Truth table0%0/4
not(A) and B and C
A= not $@B= $tryC= $try eq "\177\0\0\1"
ABCresult
0XX0
10X0
1100
1111
242 0 *_ghbn2 = \&Socket6::gethostbyname2;
243 0 $havegethostbyname2 = 1;
244 } else {
245 0 ▶ *_ghbn2 = sub { return () }; # use failure branch below
Subroutine __ANON__: not called CC=1 SCAR=6.9
246 }
247  
248 0 ▶ $mygethostbyname = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
249 0 my @tip;
250 0 ▶ unless ($_Sock6ok && (@tip = _ghbn2($_[0],NetAddr::IP::Util::AF_INET6())) && @tip > 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_Sock6ok and @tip = _ghbn2($_[0], NetAddr::IP::Util::AF_INET6()) and @tip > 1) 0 0 0
Condition0%0/6
$_Sock6ok and @tip = _ghbn2($_[0], NetAddr::IP::Util::AF_INET6()) and @tip > 1
!ll&&!rl&&r
000
$_Sock6ok and @tip = _ghbn2($_[0], NetAddr::IP::Util::AF_INET6())
!ll&&!rl&&r
000
MC/DC0%0/3
$_Sock6ok and @tip = _ghbn2($_[0], NetAddr::IP::Util::AF_INET6()) and @tip > 1
$_Sock6ok @tip = _ghbn2($_[0], NetAddr::IP::Util::AF_INET6()) @tip > 1
Truth table0%0/4
A and B and C
A= $_Sock6okB= @tip = _ghbn2($_[0], NetAddr::IP::Util::AF_INET6())C= @tip > 1
ABCresult
0XX0
10X0
1100
1111
251 # SEE NOTE above about broken BSD
252 0 @tip = gethostbyname(NetAddr::IP::InetBase::fillIPv4($_[0]));
253 }
254 0 return &_end_gethostbyname(@tip);
255 0 };
256 }
257  
258 package NetAddr::IP::Util;
259  
260 4 ▶sub naip_gethostbyname {
Subroutine naip_gethostbyname: called CC=2 SCAR=7.1
Pod documented
261 # turn off complaint from Socket6 about missing numeric argument
262 4 undef local $^W;
263 4 my @rv = &$mygethostbyname($_[0]);
264 return wantarray
265 ? @rv
266 4 ▶ : $rv[4];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
267 }
268  
269 133 1;
270  
271 __END__
« Lite/t/netaddr.t ↑ Summary Lite/t/v4-num.t »