blib/lib/NetAddr/IP/InetBase.pm

filter: Statementstmt 85.4% 88 / 103 Branchbran 66.6% 32 / 48 Conditioncond 37.5% 18 / 48 MC/DCmcdc 5.0% 1 / 20 Subroutinesub 94.7% 18 / 19 Podpod 100% 10 / 10 totaltotal 67.3% 167 / 248 CC 32 SCAR 38.8
CC
32
Cov
69%
fillIPv4
99.0
isIPv4
4.1
ipv6_n2x
2.0
CRAP 48.5
« blib/lib/NetAddr/IP.pm ↑ Summary Lite/t/netaddr.t »
1 #!/usr/bin/perl
2 package NetAddr::IP::InetBase;
3  
4 143 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use diagnostics;
6 #use lib qw(blib lib);
7  
8 143 ▶use vars qw($VERSION @EXPORT_OK @ISA %EXPORT_TAGS $Mode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 143 ▶use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 143 require Exporter;
11  
12 143 @ISA = qw(Exporter);
13  
14 143 $VERSION = do { my @r = (q$Revision: 0.08 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
15  
16 143 @EXPORT_OK = qw(
17 inet_aton
18 inet_ntoa
19 ipv6_aton
20 ipv6_ntoa
21 ipv6_n2x
22 ipv6_n2d
23 inet_any2n
24 inet_n2dx
25 inet_n2ad
26 inet_ntop
27 inet_pton
28 packzeros
29 isIPv4
30 isNewIPv4
31 isAnyIPv4
32 AF_INET
33 AF_INET6
34 fake_AF_INET6
35 fillIPv4
36 );
37  
38 143 %EXPORT_TAGS = (
39 all => [@EXPORT_OK],
40 ipv4 => [qw(
41 inet_aton
42 inet_ntoa
43 fillIPv4
44 )],
45 ipv6 => [qw(
46 ipv6_aton
47 ipv6_ntoa
48 ipv6_n2x
49 ipv6_n2d
50 inet_any2n
51 inet_n2dx
52 inet_n2ad
53 inet_pton
54 inet_ntop
55 packzeros
56 )],
57 );
58  
59 # prototypes
60 sub inet_ntoa;
61 sub ipv6_aton;
62 sub ipv6_ntoa;
63 sub inet_any2n($);
64 sub inet_n2dx($);
65 sub inet_n2ad($);
66 sub _inet_ntop;
67 sub _inet_pton;
68  
69 143 my $emulateAF_INET6 = 0;
70  
71 143 ▶{ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 143 *packzeros = \&_packzeros;
74  
75 ## dynamic configuraton for IPv6
76  
77 143 require Socket;
78  
79 143 *AF_INET = \&Socket::AF_INET;
80  
81 143 ▶if (eval { AF_INET6() } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { AF_INET6() } }) 0 143 143
82 0 *AF_INET6 = \&Socket::AF_INET6;
83 0 $emulateAF_INET6 = -1; # have it, remind below
84 }
85 143 ▶if (eval{ require Socket6 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Socket6 } }) { } 0 143 143
86 0 import Socket6 qw(
87 inet_pton
88 inet_ntop
89 );
90 0 ▶ unless ($emulateAF_INET6) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($emulateAF_INET6) 0 0 0
91 0 *AF_INET6 = \&Socket6::AF_INET6;
92 }
93 0 $emulateAF_INET6 = 0; # clear, have it from elsewhere or here
94 } else {
95 143 ▶ unless ($emulateAF_INET6) { # unlikely at this point
Branch: 1 branch
BranchTrueFalseTotal
if ($emulateAF_INET6) { } 0 143 143
96 143 ▶ if ($^O =~ /(?:free|dragon.+)bsd/i) { # FreeBSD, DragonFlyBSD
Branch: 5 branches
BranchTrueFalseTotal
if ($^O =~ /(?:free|dragon.+)bsd/i) { } 0 143 143
elsif ($^O =~ /bsd/i) { } 0 143 143
elsif ($^O =~ /(?:darwin|mac)/i) { } 0 143 143
elsif ($^O =~ /win/i) { } 0 143 143
elsif ($^O =~ /(?:solaris|sun)/i) { } 0 143 143
97 0 $emulateAF_INET6 = 28;
98 } elsif ($^O =~ /bsd/i) { # other BSD flavors like NetBDS, OpenBSD, BSD
99 0 $emulateAF_INET6 = 24;
100 } elsif ($^O =~ /(?:darwin|mac)/i) { # Mac OS X
101 0 $emulateAF_INET6 = 30;
102 } elsif ($^O =~ /win/i) { # Windows
103 0 $emulateAF_INET6 = 23;
104 } elsif ($^O =~ /(?:solaris|sun)/i) { # Sun box
105 0 $emulateAF_INET6 = 26;
106 } else { # use linux default
107 143 $emulateAF_INET6 = 10;
108 }
109 169 ▶ *AF_INET6 = sub { $emulateAF_INET6; };
Subroutine __ANON__: called CC=1 SCAR=0.0
110 } else {
111 0 $emulateAF_INET6 = 0; # clear, have it from elsewhere
112 }
113 143 *inet_pton = \&_inet_pton;
114 143 *inet_ntop = \&_inet_ntop;
115 }
116  
117 } # end no warnings 'once'
118  
119 1 ▶sub fake_AF_INET6 {
Subroutine fake_AF_INET6: called CC=1 SCAR=0.0
Pod documented
120 1 return $emulateAF_INET6;
121 }
122  
123 # allow user to choose upper or lower case
124 143 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
125 143 ▶ use vars qw($n2x_format $n2d_format);
Subroutine BEGIN: called CC=1 SCAR=0.0
126 143 $n2x_format = "%x:%x:%x:%x:%x:%x:%x:%x";
127 143 $n2d_format = "%x:%x:%x:%x:%x:%x:%d.%d.%d.%d";
128 }
129  
130 143 my $case = 0; # default lower case
131  
132 139 ▶sub upper { $n2x_format = uc($n2x_format); $n2d_format = uc($n2d_format); $case = 1; }
Subroutine upper: called CC=1 SCAR=0.0
Pod documented
133 2 ▶sub lower { $n2x_format = lc($n2x_format); $n2d_format = lc($n2d_format); $case = 0; }
Subroutine lower: called CC=1 SCAR=0.0
Pod documented
134  
135 25737 ▶sub ipv6_n2x {
Subroutine ipv6_n2x: called CC=2 SCAR=6.9
Pod documented
136 25737 ▶ die "Bad arg length for 'ipv6_n2x', length is ". length($_[0]) ." should be 16"
Branch: 1 branch
BranchTrueFalseTotal
unless length $_[0] == 16 6 25731 25737
137 unless length($_[0]) == 16;
138 25731 return sprintf($n2x_format,unpack("n8",$_[0]));
139 }
140  
141 76317 ▶sub ipv6_n2d {
Subroutine ipv6_n2d: called CC=2 SCAR=6.9
Pod documented
142 76317 ▶ die "Bad arg length for 'ipv6_n2d', length is ". length($_[0]) ." should be 16"
Branch: 1 branch
BranchTrueFalseTotal
unless length $_[0] == 16 2 76315 76317
143 unless length($_[0]) == 16;
144 76315 my @hex = (unpack("n8",$_[0]));
145 76315 $hex[9] = $hex[7] & 0xff;
146 76315 $hex[8] = $hex[7] >> 8;
147 76315 $hex[7] = $hex[6] & 0xff;
148 76315 $hex[6] >>= 8;
149 76315 return sprintf($n2d_format,@hex);
150 }
151  
152 # if Socket lib is broken in some way, check for overange values
153 #
154 #my $overange = yinet_aton('256.1') ? 1:0;
155 #my $overange = gethostbyname('256.1') ? 1:0;
156  
157 #sub inet_aton {
158 # unless (! $overange || $_[0] =~ /[^0-9\.]/) { # hostname
159 # my @dq = split(/\./,$_[0]);
160 # foreach (@dq) {
161 # return undef if $_ > 255;
162 # }
163 # }
164 # scalar gethostbyname($_[0]);
165 #}
166  
167 8180 ▶sub fillIPv4 {
Subroutine fillIPv4: called CC=24 SCAR=46.0
Pod documented
168 8180 my $host = $_[0];
169 8180 ▶ return undef unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 8180 8180
170 8180 ▶ if ($host =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/) 8133 47 8180
171 8133 ▶ if (defined $4) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $4) { } 8131 2 8133
elsif (defined $3) { } 0 2 2
elsif (defined $2) { } 2 0 2
172 return undef unless
173 8131 ▶ $1 >= 0 && $1 < 256 &&
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 8 8123 8131
Condition66%14/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
808123
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0 and $3 < 256 and $4 >= 0
!ll&&!rl&&r
808123
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0 and $3 < 256
!ll&&!rl&&r
808123
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0
!ll&&!rl&&r
808123
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256
!ll&&!rl&&r
808123
$1 >= 0 and $1 < 256 and $2 >= 0
!ll&&!rl&&r
808123
$1 >= 0 and $1 < 256
!ll&&!rl&&r
088123
MC/DC12%1/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 table22%2/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
174 $2 >= 0 && $2 < 256 &&
175 $3 >= 0 && $3 < 256 &&
176 $4 >= 0 && $4 < 256;
177 8123 $host = $1.'.'.$2.'.'.$3.'.'.$4;
178 # return pack('C4',$1,$2,$3,$4);
179 # $host = ($1 << 24) + ($2 << 16) + ($3 << 8) + $4;
180 } elsif (defined $3) {
181 return undef unless
182 0 ▶ $1 >= 0 && $1 < 256 &&
Branch: 1 branch
BranchTrueFalseTotal
unless $1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0 and $3 < 256 0 0 0
Condition0%0/15
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0 and $3 < 256
!ll&&!rl&&r
000
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 and $3 >= 0
!ll&&!rl&&r
000
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256
!ll&&!rl&&r
000
$1 >= 0 and $1 < 256 and $2 >= 0
!ll&&!rl&&r
000
$1 >= 0 and $1 < 256
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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
183 $2 >= 0 && $2 < 256 &&
184 $3 >= 0 && $3 < 256;
185 0 $host = $1.'.'.$2.'.0.'.$3
186 # return pack('C4',$1,$2,0,$3);
187 # $host = ($1 << 24) + ($2 << 16) + $3;
188 } elsif (defined $2) {
189 return undef unless
190 2 ▶ $1 >= 0 && $1 < 256 &&
Branch: 1 branch
BranchTrueFalseTotal
unless $1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256 0 2 2
Condition33%3/9
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256
!ll&&!rl&&r
002
$1 >= 0 and $1 < 256 and $2 >= 0
!ll&&!rl&&r
002
$1 >= 0 and $1 < 256
!ll&&!rl&&r
002
MC/DC0%0/4
$1 >= 0 and $1 < 256 and $2 >= 0 and $2 < 256
$1 >= 0 $1 < 256 $2 >= 0 $2 < 256
Truth table20%1/5
A and B and C and D
A= $1 >= 0B= $1 < 256C= $2 >= 0D= $2 < 256
ABCDresult
0XXX0
10XX0
110X0
11100
11111
191 $2 >= 0 && $2 < 256;
192 2 $host = $1.'.0.0.'.$2;
193 # return pack('C4',$1,0,0,$2);
194 # $host = ($1 << 24) + $2;
195 } else {
196 0 $host = '0.0.0.'.$1;
197 # return pack('C4',0,0,0,$1);
198 # $host = $1;
199 }
200 # return pack('N',$host);
201 }
202 8172 $host;
203 }
204  
205 8131 ▶sub inet_aton {
Subroutine inet_aton: called CC=2 SCAR=6.9
Pod documented
206 8131 my $host = fillIPv4($_[0]);
207 8131 ▶ return $host ? scalar gethostbyname($host) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$host ? : 8123 8 8131
208 }
209  
210 #sub inet_aton {
211 # my $host = $_[0];
212 # return undef unless defined $host;
213 # if ($host =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/) {
214 # if (defined $4) {
215 # return undef unless
216 # $1 >= 0 && $1 < 256 &&
217 # $2 >= 0 && $2 < 256 &&
218 # $3 >= 0 && $3 < 256 &&
219 # $4 >= 0 && $4 < 256;
220 # return pack('C4',$1,$2,$3,$4);
221 ## $host = ($1 << 24) + ($2 << 16) + ($3 << 8) + $4;
222 # } elsif (defined $3) {
223 # return undef unless
224 # $1 >= 0 && $1 < 256 &&
225 # $2 >= 0 && $2 < 256 &&
226 # $3 >= 0 && $3 < 256;
227 # return pack('C4',$1,$2,0,$3);
228 ## $host = ($1 << 24) + ($2 << 16) + $3;
229 # } elsif (defined $2) {
230 # return undef unless
231 # $1 >= 0 && $1 < 256 &&
232 # $2 >= 0 && $2 < 256;
233 # return pack('C4',$1,0,0,$2);
234 ## $host = ($1 << 24) + $2;
235 # } else {
236 # return pack('C4',0,0,0,$1);
237 ## $host = $1;
238 # }
239 ## return pack('N',$host);
240 # }
241 # scalar gethostbyname($host);
242 #}
243  
244 143 my $_zero = pack('L4',0,0,0,0);
245 143 my $_ipv4mask = pack('L4',0xffffffff,0xffffffff,0xffffffff,0);
246  
247 93974 ▶sub isIPv4 {
Subroutine isIPv4: called CC=4 SCAR=14.1
Pod documented
248 93974 ▶ if (length($_[0]) != 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_[0] != 16) 1 93973 93974
249 1 ▶ my $sub = (caller(1))[3] || (caller(0))[3];
Condition33%1/3
(caller 1)[3] || (caller 0)[3]
l!l&&r!l&&!r
100
MC/DC0%0/2
(caller 1)[3] || (caller 0)[3]
(caller 1)[3] (caller 0)[3]
Truth table33%1/3
A || B
A= (caller 1)[3]B= (caller 0)[3]
ABresult
1X1
011
000
250 1 die "Bad arg length for $sub, length is ". (length($_[0]) *8) .", should be 128";
251 }
252 93973 ▶ return ($_[0] & $_ipv4mask) eq $_zero
Branch: 1 branch
BranchTrueFalseTotal
($_[0] & $_ipv4mask) eq $_zero ? : 92632 1341 93973
253 ? 1 : 0;
254 }
255  
256 143 my $_newV4compat = pack('N4',0,0,0xffff,0);
257  
258 687 ▶sub isNewIPv4 {
Subroutine isNewIPv4: called CC=1 SCAR=0.0
Pod documented
259 687 my $naddr = $_[0] ^ $_newV4compat;
260 687 return isIPv4($naddr);
261 }
262  
263 76810 ▶sub isAnyIPv4 {
Subroutine isAnyIPv4: called CC=2 SCAR=6.9
Pod documented
264 76810 my $naddr = $_[0];
265 76810 my $rv = isIPv4($_[0]);
266 76810 ▶ return $rv if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 76333 477 76810
267 477 return isNewIPv4($naddr);
268 }
269  
270 0 ▶sub DESTROY {};
Subroutine DESTROY: not called CC=1 SCAR=0.0
271  
272 247 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
273 247 ▶ if (grep { $_ eq ':upper' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":upper";} @_) 139 108 247
274 139 upper();
275 139 @_ = grep { $_ ne ':upper' } @_;
276 }
277 247 NetAddr::IP::InetBase->export_to_level(1,@_);
278 }
279  
280 143 1;
281  
282 __END__
« blib/lib/NetAddr/IP.pm ↑ Summary Lite/t/netaddr.t »