blib/lib/Net/NBsocket.pm

filter: Statementstmt 87.2% 143 / 164 Branchbran 52.0% 51 / 98 Conditioncond 39.3% 13 / 33 MC/DCmcdc 4.7% 1 / 21 Subroutinesub 76.9% 20 / 26 Podpod 78.5% 11 / 14 totaltotal 67.1% 239 / 356 CC 63 SCAR 27.2
CC
63
Cov
70%
open_Listen
33.6
_connect
22.5
dyn_bind
16.7
CRAP 15.1
↑ Summary
1 #!/usr/bin/perl
2  
3 package Net::NBsocket;
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use diagnostics;
6  
7 16 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 $VERSION @ISA @EXPORT_OK $UDP $TCP
9 16 );
10 16 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use NetAddr::IP::InetBase qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 ipv6_aton
13 ipv6_n2x
14 ipv6_n2d
15 inet_pton
16 inet_ntop
17 AF_INET6
18 16 );
19 16 ▶use AutoLoader 'AUTOLOAD';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 require Socket;
21 7 import Socket qw(
22 sockaddr_in
23 sockaddr_un
24 inet_aton
25 inet_ntoa
26 INADDR_ANY
27 INADDR_BROADCAST
28 INADDR_LOOPBACK
29 INADDR_NONE
30 AF_INET
31 AF_UNIX
32 SOCK_STREAM
33 SOCK_DGRAM
34 SO_LINGER
35 SO_REUSEADDR
36 SOL_SOCKET
37 SOMAXCONN
38 );
39
40 7 require Exporter;
41  
42 7 @ISA = qw(Exporter);
43  
44 7 $VERSION = do { my @r = (q$Revision: 0.24 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
45  
46 # dynamic configuration for this HOST
47  
48 # used a lot, create once per session
49 # SEGFAULTS with Apache mod_perl
50 #$UDP = getprotobyname('udp');
51 #$TCP = getprotobyname('tcp');
52  
53 5 $UDP = 17;
54 7 $TCP = 6;
55  
56 7 my $havesock6 = 0;
57 7 my $isupport6 = 0;
58 7 my $in6addr_any = ipv6_aton('::0');
59 7 my $in6addr_loopback = ipv6_aton('::1');
60  
61 7 ▶if (eval { require Socket6 } ) { # have Socket6?
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Socket6 } }) { } 0 7 7
62 2 $havesock6 = 1;
63 2 import Socket6 qw(
64 pack_sockaddr_in6
65 unpack_sockaddr_in6
66 in6addr_any
67 in6addr_loopback
68 );
69  
70 2 my $sock = do { local *SOCK; };
71  
72 2 ▶ if (socket($sock,AF_INET6(),SOCK_STREAM(),$TCP)) {
Branch: 1 branch
BranchTrueFalseTotal
if (socket $sock, AF_INET6(), &SOCK_STREAM(), $Net::NBsocket::TCP) 0 2 2
73 2 $isupport6 = 1;
74 2 close $sock;
75 }
76 }
77 else {
78 7 *pack_sockaddr_in6 = \&_dummy;
79 5 *unpack_sockaddr_in6 = \&_dummy;
80 0 ▶ *in6addr_any = sub { return $in6addr_any };
Subroutine __ANON__: not called CC=1 SCAR=1.2
81 0 ▶ *in6addr_loopback = sub { return $in6addr_loopback};
Subroutine __ANON__: not called CC=1 SCAR=1.2
82 }
83  
84 # get the index into sockaddr_in for the AF family byte
85 #
86  
87 # return an index of '0' or '1' for the AF family byte
88 # '2' in the first rv indicates a not found
89 # true in the second rv indicates "don't know"
90 # neither of these conditions should occur
91  
92 16 ▶sub findAFi {
Subroutine findAFi: called CC=6 SCAR=19.0
Pod undocumented
93 16 my($af,$pa) = @_;
94 16 my @pa = unpack('C2',$pa);
95 16 my $pai;
96 16 ▶ for ($pai = 0; $pai < 2; $pai++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pai < 2) 16 0 16
97 16 ▶ last if $pa[$pai] == $af;
Branch: 1 branch
BranchTrueFalseTotal
if $pa[$pai] == $af 16 0 16
98 }
99 16 my $unsure = 0;
100 16 ▶ unless ($pai) { # if $pai is zero
Branch: 1 branch
BranchTrueFalseTotal
unless ($pai) 16 0 16
101 16 ▶ $unsure = 1 if $pa[0] == $pa[1];
Branch: 1 branch
BranchTrueFalseTotal
if $pa[0] == $pa[1] 0 16 16
102 }
103 return wantarray
104 16 ▶ ? ($pai,$unsure)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 16 0 16
105 : $pai;
106 }
107  
108 5 my($af_index,$unsure) = findAFi(AF_INET(),
109 scalar sockaddr_in(1,INADDR_LOOPBACK()));
110  
111 5 ▶sub _af_index {
Subroutine _af_index: called CC=1 SCAR=0.0
112 5 return $af_index;
113 }
114  
115 5 @EXPORT_OK = qw(
116 open_UDP
117 open_udpNB
118 open_Listen
119 open_listenNB
120 connectBlk
121 connect_NB
122 accept_Blk
123 accept_NB
124 set_NB
125 set_so_linger
126 dyn_bind
127 inet_aton
128 inet_ntoa
129 sockaddr_in
130 sockaddr_un
131 inet_pton
132 inet_ntop
133 ipv6_aton
134 ipv6_n2x
135 ipv6_n2d
136 INADDR_ANY
137 INADDR_BROADCAST
138 INADDR_LOOPBACK
139 INADDR_NONE
140 in6addr_any
141 in6addr_loopback
142 AF_INET
143 AF_INET6
144 havesock6
145 isupport6
146 pack_sockaddr_in6
147 unpack_sockaddr_in6
148 );
149  
150 9 ▶sub TCP {
Subroutine TCP: called CC=1 SCAR=0.0
Pod undocumented
151 9 return $TCP;
152 }
153  
154 5 ▶sub UDP {
Subroutine UDP: called CC=1 SCAR=0.0
Pod undocumented
155 5 return $UDP;
156 }
157  
158 2 ▶sub _dummy {
Subroutine _dummy: called CC=2 SCAR=6.9
159 return wantarray
160 ? ()
161 2 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
162 }
163  
164 7 ▶sub havesock6 {
Subroutine havesock6: called CC=1 SCAR=0.0
Pod documented
165 7 return $havesock6;
166 }
167  
168 2 ▶sub isupport6 {
Subroutine isupport6: called CC=1 SCAR=0.0
Pod documented
169 2 return $isupport6;
170 }
171  
172 5 ▶sub open_listenNB {
Subroutine open_listenNB: called CC=4 SCAR=20.9
Pod documented
173 5 my $lsock = &open_Listen;
174 5 ▶ return $lsock if $lsock && set_NB($lsock);
Branch: 1 branch
BranchTrueFalseTotal
if $lsock and set_NB($lsock) 5 0 5
Condition33%1/3
$lsock and set_NB($lsock)
!ll&&!rl&&r
005
MC/DC0%0/2
$lsock and set_NB($lsock)
$lsock set_NB($lsock)
Truth table33%1/3
A and B
A= $lsockB= set_NB($lsock)
ABresult
0X0
100
111
175 0 ▶ close $lsock if $lsock;
Branch: 1 branch
BranchTrueFalseTotal
if $lsock 0 0 0
176 0 return undef;
177 }
178  
179 0 ▶sub connectBlk {
Subroutine connectBlk: not called CC=1 SCAR=6.9
Pod documented
180 0 unshift @_,1;
181 0 &_connect;
182 }
183  
184 5 ▶sub connect_NB {
Subroutine connect_NB: called CC=1 SCAR=0.0
Pod documented
185 5 unshift @_,0;
186 5 &_connect;
187 }
188  
189 9 ▶sub accept_NB {
Subroutine accept_NB: called CC=3 SCAR=11.0
Pod documented
190 9 unshift @_,0;
191 9 my @rv = &_accept;
192 9 ▶ return @rv if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 6 3 9
193 3 ▶ return $rv[0] if @rv;
Branch: 1 branch
BranchTrueFalseTotal
if @rv 1 2 3
194 2 return undef;
195 }
196  
197 0 ▶sub accept_Blk {
Subroutine accept_Blk: not called CC=3 SCAR=24.8
Pod documented
198 0 unshift @_,1;
199 0 my @rv= &_accept;
200 0 ▶ return @rv if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
201 0 ▶ return $rv[0] if @rv;
Branch: 1 branch
BranchTrueFalseTotal
if @rv 0 0 0
202 0 return undef;
203 }
204  
205 0 ▶sub set_so_linger {
Subroutine set_so_linger: not called CC=1 SCAR=6.9
Pod documented
206 0 my ($FH,$sec) = @_;
207 0 setsockopt($FH,SOL_SOCKET(),SO_LINGER(),pack("ll",1,$sec));
208 }
209  
210 0 ▶sub DESTROY {};
Subroutine DESTROY: not called CC=1 SCAR=0.0
211  
212 5 1;
213  
214 __END__
↑ Summary