blib/lib/IO/Socket/Socks.pm

filter: Statementstmt 69.0% 721 / 1045 Branchbran 50.9% 273 / 536 Conditioncond 39.7% 72 / 181 MC/DCmcdc 8.3% 9 / 108 Subroutinesub 78.2% 54 / 69 Podpod 84.6% 11 / 13 totaltotal 58.4% 1140 / 1952 CC 333 SCAR 40.2
CC
333
Cov
60%
send
506.0
recv
156.0
_socks5_connect_reply
68.6
CRAP 55.8
↑ Summary
1 package IO::Socket::Socks;
2  
3 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use Errno qw(EWOULDBLOCK EAGAIN EINPROGRESS ETIMEDOUT ECONNABORTED);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use vars qw( $SOCKET_CLASS @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION $SOCKS_ERROR $SOCKS5_RESOLVE $SOCKS4_RESOLVE $SOCKS_DEBUG %CODES );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 require Exporter;
10  
11 30 $VERSION = '0.66_2';
12  
13 use constant {
14 30 ▶ SOCKS_WANT_READ => 20,
Branch: 1 branch
BranchTrueFalseTotal
exists &Errno::EPROTO ? : 30 0 30
15 SOCKS_WANT_WRITE => 21,
16 ESOCKSPROTO => exists &Errno::EPROTO ? &Errno::EPROTO : 7000,
17 30 ▶};
Subroutine BEGIN: called CC=2 SCAR=6.9
18  
19 30 @ISA = ('Exporter');
20  
21 30 tie $SOCKET_CLASS, 'IO::Socket::Socks::SocketClassVar', $SOCKET_CLASS;
22 30 ▶unless ($SOCKET_CLASS) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($IO::Socket::Socks::SOCKET_CLASS) 30 0 30
23 30 ▶ if (eval { require IO::Socket::IP; IO::Socket::IP->VERSION(0.36) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require IO::Socket::IP; 'IO::Socket::IP'->VERSION(0.36) } }) { } 30 0 30
24 30 $SOCKET_CLASS = 'IO::Socket::IP';
25 }
26 else {
27 0 $SOCKET_CLASS = 'IO::Socket::INET';
28 }
29 }
30  
31 30 @EXPORT = qw( $SOCKS_ERROR SOCKS_WANT_READ SOCKS_WANT_WRITE ESOCKSPROTO );
32 30 @EXPORT_OK = qw(
33 SOCKS5_VER
34 SOCKS4_VER
35 ADDR_IPV4
36 ADDR_DOMAINNAME
37 ADDR_IPV6
38 CMD_CONNECT
39 CMD_BIND
40 CMD_UDPASSOC
41 AUTHMECH_ANON
42 AUTHMECH_USERPASS
43 AUTHMECH_INVALID
44 AUTHREPLY_SUCCESS
45 AUTHREPLY_FAILURE
46 ISS_UNKNOWN_ADDRESS
47 ISS_BAD_VERSION
48 ISS_CANT_RESOLVE
49 REPLY_SUCCESS
50 REPLY_GENERAL_FAILURE
51 REPLY_CONN_NOT_ALLOWED
52 REPLY_NETWORK_UNREACHABLE
53 REPLY_HOST_UNREACHABLE
54 REPLY_CONN_REFUSED
55 REPLY_TTL_EXPIRED
56 REPLY_CMD_NOT_SUPPORTED
57 REPLY_ADDR_NOT_SUPPORTED
58 REQUEST_GRANTED
59 REQUEST_FAILED
60 REQUEST_REJECTED_IDENTD
61 REQUEST_REJECTED_USERID
62 );
63 30 %EXPORT_TAGS = (constants => [ 'SOCKS_WANT_READ', 'SOCKS_WANT_WRITE', @EXPORT_OK ]);
64 30 tie $SOCKS_ERROR, 'IO::Socket::Socks::ReadOnlyVar', IO::Socket::Socks::Error->new();
65  
66 30 $SOCKS5_RESOLVE = 1;
67 30 $SOCKS4_RESOLVE = 0;
68 30 $SOCKS_DEBUG = $ENV{SOCKS_DEBUG};
69  
70 use constant {
71 30 SOCKS5_VER => 5,
72 SOCKS4_VER => 4,
73  
74 ADDR_IPV4 => 1,
75 ADDR_DOMAINNAME => 3,
76 ADDR_IPV6 => 4,
77  
78 CMD_CONNECT => 1,
79 CMD_BIND => 2,
80 CMD_UDPASSOC => 3,
81  
82 AUTHMECH_ANON => 0,
83  
84 #AUTHMECH_GSSAPI => 1,
85 AUTHMECH_USERPASS => 2,
86 AUTHMECH_INVALID => 255,
87  
88 AUTHREPLY_SUCCESS => 0,
89 AUTHREPLY_FAILURE => 10, # to not intersect with other socks5 constants
90  
91 ISS_UNKNOWN_ADDRESS => 500,
92 ISS_BAD_VERSION => 501,
93 ISS_CANT_RESOLVE => 502,
94 30 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 30 $CODES{AUTHMECH}->[AUTHMECH_INVALID] = "No valid auth mechanisms";
97 30 $CODES{AUTHREPLY}->[AUTHREPLY_FAILURE] = "Failed to authenticate";
98  
99 # socks5
100 use constant {
101 30 REPLY_SUCCESS => 0,
102 REPLY_GENERAL_FAILURE => 1,
103 REPLY_CONN_NOT_ALLOWED => 2,
104 REPLY_NETWORK_UNREACHABLE => 3,
105 REPLY_HOST_UNREACHABLE => 4,
106 REPLY_CONN_REFUSED => 5,
107 REPLY_TTL_EXPIRED => 6,
108 REPLY_CMD_NOT_SUPPORTED => 7,
109 REPLY_ADDR_NOT_SUPPORTED => 8,
110 30 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 30 $CODES{REPLY}->{&REPLY_SUCCESS} = "Success";
113 30 $CODES{REPLY}->{&REPLY_GENERAL_FAILURE} = "General failure";
114 30 $CODES{REPLY}->{&REPLY_CONN_NOT_ALLOWED} = "Not allowed";
115 30 $CODES{REPLY}->{&REPLY_NETWORK_UNREACHABLE} = "Network unreachable";
116 30 $CODES{REPLY}->{&REPLY_HOST_UNREACHABLE} = "Host unreachable";
117 30 $CODES{REPLY}->{&REPLY_CONN_REFUSED} = "Connection refused";
118 30 $CODES{REPLY}->{&REPLY_TTL_EXPIRED} = "TTL expired";
119 30 $CODES{REPLY}->{&REPLY_CMD_NOT_SUPPORTED} = "Command not supported";
120 30 $CODES{REPLY}->{&REPLY_ADDR_NOT_SUPPORTED} = "Address not supported";
121  
122 # socks4
123 use constant {
124 30 REQUEST_GRANTED => 90,
125 REQUEST_FAILED => 91,
126 REQUEST_REJECTED_IDENTD => 92,
127 REQUEST_REJECTED_USERID => 93,
128 30 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
129  
130 30 $CODES{REPLY}->{&REQUEST_GRANTED} = "request granted";
131 30 $CODES{REPLY}->{&REQUEST_FAILED} = "request rejected or failed";
132 30 $CODES{REPLY}->{&REQUEST_REJECTED_IDENTD} = "request rejected becasue SOCKS server cannot connect to identd on the client";
133 30 $CODES{REPLY}->{&REQUEST_REJECTED_USERID} = "request rejected because the client program and identd report different user-ids";
134  
135 # queue
136 use constant {
137 30 Q_SUB => 0,
138 Q_ARGS => 1,
139 Q_BUF => 2,
140 Q_READS => 3,
141 Q_SENDS => 4,
142 Q_DEBUGS => 5,
143 30 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145  
146 sub new_from_fd {
147 1 ▶ my ($class, $sock, %arg) = @_;
Subroutine new_from_fd: called CC=3 SCAR=12.0
Pod documented
148  
149 1 bless $sock, $class;
150  
151 1 $sock->autoflush(1);
152 1 ▶ if (exists $arg{Timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg{"Timeout"}) 0 1 1
153 0 ${*$sock}{'io_socket_timeout'} = delete $arg{Timeout};
154 }
155  
156 1 ▶ scalar(%arg) or return $sock;
Branch: 1 branch
BranchTrueFalseTotal
scalar %arg or return $sock 0 1 1
157 1 return $sock->configure(\%arg);
158 }
159  
160 30 *new_from_socket = \&new_from_fd;
161  
162 sub start_SOCKS {
163 1 ▶ my ($class, $sock, %arg) = @_;
Subroutine start_SOCKS: called CC=3 SCAR=11.8
Pod documented
164  
165 1 bless $sock, $class;
166  
167 1 $sock->autoflush(1);
168 1 ▶ if (exists $arg{Timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg{"Timeout"}) 0 1 1
169 0 ${*$sock}{'io_socket_timeout'} = delete $arg{Timeout};
170 }
171  
172 1 ${*$sock}->{SOCKS} = { RequireAuth => 0 };
173  
174 1 ▶ return $sock->command(%arg) ? $sock : undef;
Branch: 1 branch
BranchTrueFalseTotal
$sock->command(%arg) ? : 1 0 1
175 }
176  
177 sub socket {
178 128 ▶ my $self = shift;
Subroutine socket: called CC=5 SCAR=17.2
Pod documented
179  
180 128 ▶ if (-S $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (-S $self) 1 127 128
181 1 ▶ ${*$self}{'io_socket_domain'} ||= $_[0];
Condition33%1/3
do { *$self }->{"io_socket_domain"} ||= $_[0]
l!l&&r!l&&!r
010
MC/DC0%0/2
do { *$self }->{"io_socket_domain"} ||= $_[0]
do { *$self }->{"io_socket_domain"} $_[0]
Truth table33%1/3
A ||= B
A= do { *$self }->{"io_socket_domain"}B= $_[0]
ABresult
1X1
011
000
182 1 ▶ ${*$self}{'io_socket_type'} ||= $_[1];
Condition33%1/3
do { *$self }->{"io_socket_type"} ||= $_[1]
l!l&&r!l&&!r
010
MC/DC0%0/2
do { *$self }->{"io_socket_type"} ||= $_[1]
do { *$self }->{"io_socket_type"} $_[1]
Truth table33%1/3
A ||= B
A= do { *$self }->{"io_socket_type"}B= $_[1]
ABresult
1X1
011
000
183 1 ▶ ${*$self}{'io_socket_proto'} ||= $_[2];
Condition33%1/3
do { *$self }->{"io_socket_proto"} ||= $_[2]
l!l&&r!l&&!r
010
MC/DC0%0/2
do { *$self }->{"io_socket_proto"} ||= $_[2]
do { *$self }->{"io_socket_proto"} $_[2]
Truth table33%1/3
A ||= B
A= do { *$self }->{"io_socket_proto"}B= $_[2]
ABresult
1X1
011
000
184 1 return $self;
185 }
186  
187 127 return $self->SUPER::socket(@_);
188 }
189  
190 sub configure {
191 128 ▶ my $self = shift;
Subroutine configure: called CC=11 SCAR=25.0
Pod undocumented
192 128 my $args = shift;
193  
194 128 ▶ $self->_configure($args)
Branch: 1 branch
BranchTrueFalseTotal
$self->_configure($args) or return 0 128 128
195 or return;
196  
197 128 ${*$self}->{SOCKS}->{ProxyAddr} = (
198 exists($args->{ProxyAddr})
199 ? delete($args->{ProxyAddr})
200 128 ▶ : undef
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'ProxyAddr'} ? : 78 50 128
201 );
202  
203 128 ${*$self}->{SOCKS}->{ProxyPort} = (
204 exists($args->{ProxyPort})
205 ? delete($args->{ProxyPort})
206 128 ▶ : undef
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'ProxyPort'} ? : 78 50 128
207 );
208  
209 128 ${*$self}->{SOCKS}->{COMMAND} = [];
210  
211 128 ▶ if (exists($args->{Listen})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args->{'Listen'}) { } 50 78 128
elsif (${*$self;}->{'SOCKS'}{'ProxyAddr'} and ${*$self;}->{'SOCKS'}{'ProxyPort'}) { } 78 0 78
212 50 $args->{LocalAddr} = ${*$self}->{SOCKS}->{ProxyAddr};
213 50 $args->{LocalPort} = ${*$self}->{SOCKS}->{ProxyPort};
214 50 $args->{Reuse} = 1;
215 50 ${*$self}->{SOCKS}->{Listen} = 1;
216 }
217 78 ▶ elsif (${*$self}->{SOCKS}->{ProxyAddr} && ${*$self}->{SOCKS}->{ProxyPort}) {
Condition33%1/3
${*$self;}->{"SOCKS"}{"ProxyAddr"} and ${*$self;}->{"SOCKS"}{"ProxyPort"}
!ll&&!rl&&r
0078
MC/DC0%0/2
${*$self;}->{"SOCKS"}{"ProxyAddr"} and ${*$self;}->{"SOCKS"}{"ProxyPort"}
${*$self;}->{"SOCKS"}{"ProxyAddr"} ${*$self;}->{"SOCKS"}{"ProxyPort"}
Truth table33%1/3
A and B
A= ${*$self;}->{"SOCKS"}{"ProxyAddr"}B= ${*$self;}->{"SOCKS"}{"ProxyPort"}
ABresult
0X0
100
111
218 78 $args->{PeerAddr} = ${*$self}->{SOCKS}->{ProxyAddr};
219 78 $args->{PeerPort} = ${*$self}->{SOCKS}->{ProxyPort};
220 }
221  
222 128 ▶ unless (defined ${*$self}->{SOCKS}->{TCP}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined ${*$self;}->{'SOCKS'}{'TCP'}) { } 0 128 128
(do { not defined $args->{"Proto"} }) 0 0 0
223 128 $args->{Proto} = "tcp";
224 128 $args->{Type} = SOCK_STREAM;
225 }
226 0 elsif (!defined $args->{Proto}) {
227 0 $args->{Proto} = "udp";
228 0 $args->{Type} = SOCK_DGRAM;
229 }
230  
231 128 $SOCKS_ERROR->set();
232 128 ▶ unless ($self->SUPER::configure($args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->SUPER::configure($args)) 6 122 128
233 6 ▶ if ($SOCKS_ERROR == undef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Socket::Socks::SOCKS_ERROR == undef) 0 6 6
234 0 $SOCKS_ERROR->set($!, $@);
235 }
236 6 return;
237 }
238  
239 122 return $self;
240 }
241  
242 sub _configure {
243 129 ▶ my $self = shift;
Subroutine _configure: called CC=29 SCAR=40.6
244 129 my $args = shift;
245  
246 129 ${*$self}->{SOCKS}->{Version} = (
247 exists($args->{SocksVersion})
248 ? (
249 $args->{SocksVersion} == 4
250 || $args->{SocksVersion} == 5
251 || ( exists $args->{Listen}
252 && ref $args->{SocksVersion} eq 'ARRAY'
253 && _validate_multi_version($args->{SocksVersion}))
254 ? delete($args->{SocksVersion})
255 129 ▶ : croak("Unsupported socks version specified. Should be 4 or 5")
Branch: 2 branches
BranchTrueFalseTotal
exists $args->{'SocksVersion'} ? : 113 16 129
$args->{'SocksVersion'} == 4 || $args->{'SocksVersion'} == 5 || exists $args->{'Listen'} && ref $args->{'SocksVersion'} eq 'ARRAY' && _validate_multi_version($args->{'SocksVersion'}) ? : 113 0 113
Condition58%7/12
$args->{"SocksVersion"} == 4 or $args->{"SocksVersion"} == 5 or exists $args->{"Listen"} and ref $args->{"SocksVersion"} eq "ARRAY" and _validate_multi_version($args->{"SocksVersion"})
l!l&&r!l&&!r
11210
$args->{"SocksVersion"} == 4 or $args->{"SocksVersion"} == 5
l!l&&r!l&&!r
60521
exists $args->{"Listen"} and ref $args->{"SocksVersion"} eq "ARRAY" and _validate_multi_version($args->{"SocksVersion"})
!ll&&!rl&&r
001
exists $args->{"Listen"} and ref $args->{"SocksVersion"} eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/5
$args->{"SocksVersion"} == 4 or $args->{"SocksVersion"} == 5 or exists $args->{"Listen"} and ref $args->{"SocksVersion"} eq "ARRAY" and _validate_multi_version($args->{"SocksVersion"})
$args->{"SocksVersion"} == 4 $args->{"SocksVersion"} == 5 exists $args->{"Listen"} ref $args->{"SocksVersion"} eq "ARRAY" _validate_multi_version($args->{"SocksVersion"})
Truth table50%3/6
A or B or C and D and E
A= $args->{"SocksVersion"} == 4B= $args->{"SocksVersion"} == 5C= exists $args->{"Listen"}D= ref $args->{"SocksVersion"} eq "ARRAY"E= _validate_multi_version($args->{"SocksVersion"})
ABCDEresult
1XXXX1
01XXX1
001111
000XX0
0010X0
001100
256 )
257 : 5
258 );
259  
260 129 ${*$self}->{SOCKS}->{AuthType} = (
261 exists($args->{AuthType})
262 ? delete($args->{AuthType})
263 129 ▶ : "none"
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'AuthType'} ? : 18 111 129
264 );
265  
266 129 ${*$self}->{SOCKS}->{RequireAuth} = (
267 exists($args->{RequireAuth})
268 ? delete($args->{RequireAuth})
269 129 ▶ : 0
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'RequireAuth'} ? : 45 84 129
270 );
271  
272 129 ${*$self}->{SOCKS}->{UserAuth} = (
273 exists($args->{UserAuth})
274 ? delete($args->{UserAuth})
275 129 ▶ : undef
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'UserAuth'} ? : 45 84 129
276 );
277  
278 129 ${*$self}->{SOCKS}->{Username} = (
279 exists($args->{Username}) ? delete($args->{Username})
280 : (
281 129 ▶ (${*$self}->{SOCKS}->{AuthType} eq "none") ? undef
Branch: 2 branches
BranchTrueFalseTotal
exists $args->{'Username'} ? : 18 111 129
${*$self;}->{'SOCKS'}{'AuthType'} eq 'none' ? : 111 0 111
282 : croak("If you set AuthType to userpass, then you must provide a username.")
283 )
284 );
285  
286 129 ${*$self}->{SOCKS}->{Password} = (
287 exists($args->{Password}) ? delete($args->{Password})
288 : (
289 129 ▶ (${*$self}->{SOCKS}->{AuthType} eq "none") ? undef
Branch: 2 branches
BranchTrueFalseTotal
exists $args->{'Password'} ? : 18 111 129
${*$self;}->{'SOCKS'}{'AuthType'} eq 'none' ? : 111 0 111
290 : croak("If you set AuthType to userpass, then you must provide a password.")
291 )
292 );
293  
294 129 ${*$self}->{SOCKS}->{Debug} = (
295 exists($args->{SocksDebug})
296 ? delete($args->{SocksDebug})
297 129 ▶ : $SOCKS_DEBUG
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'SocksDebug'} ? : 0 129 129
298 );
299  
300 129 ${*$self}->{SOCKS}->{Resolve} = (
301 exists($args->{SocksResolve})
302 ? delete($args->{SocksResolve})
303 129 ▶ : undef
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'SocksResolve'} ? : 26 103 129
304 );
305  
306 129 ${*$self}->{SOCKS}->{AuthMethods} = [ 0, 0, 0 ];
307 118 ${*$self}->{SOCKS}->{AuthMethods}->[AUTHMECH_ANON] = 1
308 129 ▶ unless ${*$self}->{SOCKS}->{RequireAuth};
Branch: 1 branch
BranchTrueFalseTotal
unless ${*$self;}->{"SOCKS"}{"RequireAuth"} 118 11 129
309  
310 #${*$self}->{SOCKS}->{AuthMethods}->[AUTHMECH_GSSAPI] = 1
311 # if (${*$self}->{SOCKS}->{AuthType} eq "gssapi");
312 63 ${*$self}->{SOCKS}->{AuthMethods}->[AUTHMECH_USERPASS] = 1
313 if (
314 79 ▶ (!exists($args->{Listen}) && (${*$self}->{SOCKS}->{AuthType} eq "userpass"))
Condition100%3/3
exists $args->{"Listen"} and defined ${*$self;}->{"SOCKS"}{"UserAuth"}
!ll&&!rl&&r
61545
MC/DC100%2/2
exists $args->{"Listen"} and defined ${*$self;}->{"SOCKS"}{"UserAuth"}
exists $args->{"Listen"} defined ${*$self;}->{"SOCKS"}{"UserAuth"}
Truth table100%3/3
A and B
A= exists $args->{"Listen"}B= defined ${*$self;}->{"SOCKS"}{"UserAuth"}
ABresult
0X0
100
111
315 || (exists($args->{Listen})
316 129 ▶ && defined(${*$self}->{SOCKS}->{UserAuth}))
Branch: 1 branch
BranchTrueFalseTotal
if not exists $args->{"Listen"} and ${*$self;}->{"SOCKS"}{"AuthType"} eq "userpass" or exists $args->{"Listen"} and defined ${*$self;}->{"SOCKS"}{"UserAuth"} 63 66 129
Condition100%6/6
not exists $args->{"Listen"} and ${*$self;}->{"SOCKS"}{"AuthType"} eq "userpass" or exists $args->{"Listen"} and defined ${*$self;}->{"SOCKS"}{"UserAuth"}
l!l&&r!l&&!r
186843
not exists $args->{"Listen"} and ${*$self;}->{"SOCKS"}{"AuthType"} eq "userpass"
!ll&&!rl&&r
506118
MC/DC0%0/3
not exists $args->{"Listen"} and ${*$self;}->{"SOCKS"}{"AuthType"} eq "userpass" or exists $args->{"Listen"} and defined ${*$self;}->{"SOCKS"}{"UserAuth"}
not exists $args->{"Listen"} ${*$self;}->{"SOCKS"}{"AuthType"} eq "userpass" exists $args->{"Listen"} and defined ${*$self;}->{"SOCKS"}{"UserAuth"}
Truth table40%2/5
not(A) and B or C
A= not exists $args->{"Listen"}B= ${*$self;}->{"SOCKS"}{"AuthType"} eq "userpass"C= exists $args->{"Listen"} and defined ${*$self;}->{"SOCKS"}{"UserAuth"}
ABCresult
11X1
0X11
1011
0X00
1000
317 );
318  
319 129 ▶ if (exists($args->{BindAddr}) && exists($args->{BindPort})) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $args->{'BindAddr'} and exists $args->{'BindPort'}) { } 0 129 129
elsif (exists $args->{'UdpAddr'} and exists $args->{'UdpPort'}) { } 0 129 129
elsif (exists $args->{'ConnectAddr'} and exists $args->{'ConnectPort'}) { } 79 100 179
Condition33%1/3
exists $args->{"BindAddr"} and exists $args->{"BindPort"}
!ll&&!rl&&r
12900
MC/DC0%0/2
exists $args->{"BindAddr"} and exists $args->{"BindPort"}
exists $args->{"BindAddr"} exists $args->{"BindPort"}
Truth table33%1/3
A and B
A= exists $args->{"BindAddr"}B= exists $args->{"BindPort"}
ABresult
0X0
100
111
320 0 ${*$self}->{SOCKS}->{CmdAddr} = delete($args->{BindAddr});
321 0 ${*$self}->{SOCKS}->{CmdPort} = delete($args->{BindPort});
322 0 ${*$self}->{SOCKS}->{Bind} = 1;
323 }
324 ▶ elsif (exists($args->{UdpAddr}) && exists($args->{UdpPort})) {
Condition33%1/3
exists $args->{"UdpAddr"} and exists $args->{"UdpPort"}
!ll&&!rl&&r
12900
MC/DC0%0/2
exists $args->{"UdpAddr"} and exists $args->{"UdpPort"}
exists $args->{"UdpAddr"} exists $args->{"UdpPort"}
Truth table33%1/3
A and B
A= exists $args->{"UdpAddr"}B= exists $args->{"UdpPort"}
ABresult
0X0
100
111
325 0 ▶ if (${*$self}->{SOCKS}->{Version} == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (${*$self;}->{"SOCKS"}{"Version"} == 4) 0 0 0
326 0 croak("Socks v4 doesn't support UDP association");
327 }
328 0 ${*$self}->{SOCKS}->{CmdAddr} = delete($args->{UdpAddr});
329 0 ${*$self}->{SOCKS}->{CmdPort} = delete($args->{UdpPort});
330 0 $args->{LocalAddr} = ${*$self}->{SOCKS}->{CmdAddr};
331 0 $args->{LocalPort} = ${*$self}->{SOCKS}->{CmdPort};
332 0 ${*$self}->{SOCKS}->{TCP} = __PACKAGE__->new( # TCP backend for UDP socket
333 Timeout => $args->{Timeout},
334 0 ▶ Proto => 'tcp'
Branch: 1 branch
BranchTrueFalseTotal
${*$self;}->{"SOCKS"}{"TCP"} = __PACKAGE__->new("Timeout", $args->{"Timeout"}, "Proto", "tcp") or return 0 0 0
335 ) or return;
336 }
337 ▶ elsif (exists($args->{ConnectAddr}) && exists($args->{ConnectPort})) {
Condition66%2/3
exists $args->{"ConnectAddr"} and exists $args->{"ConnectPort"}
!ll&&!rl&&r
50079
MC/DC50%1/2
exists $args->{"ConnectAddr"} and exists $args->{"ConnectPort"}
exists $args->{"ConnectAddr"} exists $args->{"ConnectPort"}
Truth table66%2/3
A and B
A= exists $args->{"ConnectAddr"}B= exists $args->{"ConnectPort"}
ABresult
0X0
100
111
338 79 ${*$self}->{SOCKS}->{CmdAddr} = delete($args->{ConnectAddr});
339 79 ${*$self}->{SOCKS}->{CmdPort} = delete($args->{ConnectPort});
340 }
341  
342 129 return 1;
343 }
344  
345 sub version {
346 10 ▶ my $self = shift;
Subroutine version: called CC=1 SCAR=0.0
Pod documented
347 10 return ${*$self}->{SOCKS}->{Version};
348 }
349  
350 sub connect {
351 78 ▶ my $self = shift;
Subroutine connect: called CC=7 SCAR=21.8
Pod undocumented
352  
353 78 ▶ croak("Undefined IO::Socket::Socks object passed to connect.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 78 78
354 unless defined($self);
355  
356 my $ok =
357 78 defined(${*$self}->{SOCKS}->{TCP})
358 78 ▶ ? ${*$self}->{SOCKS}->{TCP}->SUPER::connect(@_)
Branch: 1 branch
BranchTrueFalseTotal
defined ${*$self;}->{'SOCKS'}{'TCP'} ? : 0 78 78
359 : $self->SUPER::connect(@_);
360  
361 78 ▶ if (($! == EINPROGRESS || $! == EWOULDBLOCK) && $self->blocking == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($! == 115 || $! == 11 and $self->blocking == 0) { } 40 38 78
elsif (not $ok) { } 0 38 38
Condition66%4/6
$! == 115 || $! == 11 and $self->blocking == 0
!ll&&!rl&&r
38040
$! == 115 or $! == 11
l!l&&r!l&&!r
40038
MC/DC33%1/3
$! == 115 || $! == 11 and $self->blocking == 0
$! == 115 $! == 11 $self->blocking == 0
Truth table40%2/5
A or B and C
A= $! == 115B= $! == 11C= $self->blocking == 0
ABCresult
00X0
1X00
0100
1X11
0111
362 40 $SOCKS_ERROR->set(SOCKS_WANT_WRITE, 'Socks want write');
363 }
364 elsif (!$ok) {
365 0 $SOCKS_ERROR->set($!, $@ = "Connection to proxy failed: $!");
366 0 return;
367 }
368  
369 78 $self->_connect();
370 }
371  
372 sub _connect {
373 79 ▶ my $self = shift;
Subroutine _connect: called CC=7 SCAR=19.6
374 79 ${*$self}->{SOCKS}->{ready} = 0;
375  
376 79 ▶ if (${*$self}->{SOCKS}->{Version} == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (${*$self;}->{'SOCKS'}{'Version'} == 4) { } 35 44 79
377 35 ${*$self}->{SOCKS}->{queue} = [
378  
379 # [sub, [@args], buf, [@reads], sends_cnt]
380 35 ▶ [ '_socks4_connect_command', [ ${*$self}->{SOCKS}->{Bind} ? CMD_BIND : CMD_CONNECT ], undef, [], 0 ],
Branch: 1 branch
BranchTrueFalseTotal
${*$self;}->{'SOCKS'}{'Bind'} ? : 0 35 35
381 [ '_socks4_connect_reply', [], undef, [], 0 ]
382 ];
383 }
384 else {
385 44 ${*$self}->{SOCKS}->{queue} = [
386 [ '_socks5_connect', [], undef, [], 0 ],
387 [ '_socks5_connect_if_auth', [], undef, [], 0 ],
388 [
389 '_socks5_connect_command',
390 [
391 44 ▶ ${*$self}->{SOCKS}->{Bind} ? CMD_BIND
Branch: 1 branch
BranchTrueFalseTotal
${*$self;}->{'SOCKS'}{'TCP'} ? : 0 44 44
392 44 ▶ : ${*$self}->{SOCKS}->{TCP} ? CMD_UDPASSOC
Branch: 1 branch
BranchTrueFalseTotal
${*$self;}->{'SOCKS'}{'Bind'} ? : 0 44 44
393 : CMD_CONNECT
394 ],
395 undef,
396 [],
397 0
398 ],
399 [ '_socks5_connect_reply', [], undef, [], 0 ]
400 ];
401 }
402  
403 79 ▶ if ($SOCKS_ERROR == undef) { # socket connection estabilished
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Socket::Socks::SOCKS_ERROR == undef) 39 40 79
404 39 ▶ defined($self->_run_queue())
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_run_queue or return 6 33 39
405 or return;
406 }
407  
408 73 return $self;
409 }
410  
411 sub _run_queue {
412 # run tasks from queue, return undef on error, -1 if one of the task
413 # returned not completed because of the possible blocking on network operation
414 317 ▶ my $self = shift;
Subroutine _run_queue: called CC=6 SCAR=17.9
415  
416 317 my $retval;
417 my $sub;
418  
419 317 ▶ while (my $elt = ${*$self}->{SOCKS}->{queue}[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $elt = ${*$self;}->{"SOCKS"}{"queue"}[0]) 532 146 678
420 532 $sub = $elt->[Q_SUB];
421 532 $retval = $self->$sub(@{ $elt->[Q_ARGS] });
422 532 ▶ unless (defined $retval) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $retval) 9 523 532
423 9 ${*$self}->{SOCKS}->{queue} = [];
424 9 ${*$self}->{SOCKS}->{queue_results} = {};
425 9 last;
426 }
427  
428 523 ▶ last if ($retval == -1);
Branch: 1 branch
BranchTrueFalseTotal
if $retval == -1 162 361 523
429 361 ${*$self}->{SOCKS}->{queue_results}{ $elt->[Q_SUB] } = $retval;
430 361 shift @{ ${*$self}->{SOCKS}->{queue} };
431 }
432  
433 317 ▶ if (defined($retval) && !@{ ${*$self}->{SOCKS}->{queue} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $retval and not @{${*$self;}->{"SOCKS"}{"queue"};}) 136 181 317
Condition100%3/3
defined $retval and not @{${*$self;}->{"SOCKS"}{"queue"};}
!ll&&!rl&&r
19162136
MC/DC100%2/2
defined $retval and not @{${*$self;}->{"SOCKS"}{"queue"};}
defined $retval not @{${*$self;}->{"SOCKS"}{"queue"};}
Truth table100%3/3
A and not(B)
A= defined $retvalB= not @{${*$self;}->{"SOCKS"}{"queue"};}
ABresult
0X0
100
111
434 136 ${*$self}->{SOCKS}->{queue_results} = {};
435 136 ${*$self}->{SOCKS}->{ready} = 1;
436 }
437  
438 317 return $retval;
439 }
440  
441 sub ready {
442 188 ▶ my $self = shift;
Subroutine ready: called CC=1 SCAR=0.0
Pod documented
443  
444 188 $self->_run_queue();
445 188 return ${*$self}->{SOCKS}->{ready};
446 }
447  
448 sub _socks5_connect {
449 73 ▶ my $self = shift;
Subroutine _socks5_connect: called CC=12 SCAR=28.7
450 73 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 73 73
451 73 my ($reads, $sends, $debugs) = (0, 0, 0);
452 my $sock =
453 73 defined(${*$self}->{SOCKS}->{TCP})
454 0 ? ${*$self}->{SOCKS}->{TCP}
455 73 ▶ : $self;
Branch: 1 branch
BranchTrueFalseTotal
defined ${*$self;}->{'SOCKS'}{'TCP'} ? : 0 73 73
456  
457 #--------------------------------------------------------------------------
458 # Send the auth mechanisms
459 #--------------------------------------------------------------------------
460 # +----+----------+----------+
461 # |VER | NMETHODS | METHODS |
462 # +----+----------+----------+
463 # | 1 | 1 | 1 to 255 |
464 # +----+----------+----------+
465  
466 73 my $nmethods = 0;
467 73 my $methods;
468 73 foreach my $method (0 .. $#{ ${*$self}->{SOCKS}->{AuthMethods} }) {
469 219 ▶ if (${*$self}->{SOCKS}->{AuthMethods}->[$method] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (${*$self;}->{"SOCKS"}{"AuthMethods"}[$method] == 1) 109 110 219
470 109 $methods .= pack('C', $method);
471 109 $nmethods++;
472 }
473 }
474  
475 73 my $reply;
476 73 ▶ $reply = $sock->_socks_send(pack('CCa*', SOCKS5_VER, $nmethods, $methods), ++$sends)
Branch: 1 branch
BranchTrueFalseTotal
$reply = $sock->_socks_send(pack("CCa*", 5, $nmethods, $methods), ++$sends) or return _fail($reply) 0 73 73
477 or return _fail($reply);
478  
479 73 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 73 73
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
7300
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
480 0 $debug->add(
481 ver => SOCKS5_VER,
482 nmethods => $nmethods,
483 methods => join('', unpack("C$nmethods", $methods))
484 );
485 0 $debug->show('Client Send: ');
486 }
487  
488 #--------------------------------------------------------------------------
489 # Read the reply
490 #--------------------------------------------------------------------------
491 # +----+--------+
492 # |VER | METHOD |
493 # +----+--------+
494 # | 1 | 1 |
495 # +----+--------+
496  
497 73 ▶ $reply = $sock->_socks_read(2, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$reply = $sock->_socks_read(2, ++$reads) or return _fail($reply) 32 41 73
498 or return _fail($reply);
499  
500 41 my ($version, $auth_method) = unpack('CC', $reply);
501  
502 41 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 41 41
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
4100
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
503 0 $debug->add(
504 ver => $version,
505 method => $auth_method
506 );
507 0 $debug->show('Client Recv: ');
508 }
509  
510 41 ▶ if ($auth_method == AUTHMECH_INVALID) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_method == 255) 0 41 41
511 0 $! = ESOCKSPROTO;
512 0 $SOCKS_ERROR->set(AUTHMECH_INVALID, $@ = $CODES{AUTHMECH}->[$auth_method]);
513 0 return;
514 }
515  
516 41 return $auth_method;
517 }
518  
519 sub _socks5_connect_if_auth {
520 44 ▶ my $self = shift;
Subroutine _socks5_connect_if_auth: called CC=2 SCAR=6.9
521 44 ▶ if (${*$self}->{SOCKS}->{queue_results}{'_socks5_connect'} != AUTHMECH_ANON) {
Branch: 1 branch
BranchTrueFalseTotal
if (${*$self;}->{"SOCKS"}{"queue_results"}{"_socks5_connect"} != 0) 18 26 44
522 18 unshift @{ ${*$self}->{SOCKS}->{queue} }, [ '_socks5_connect_auth', [], undef, [], 0 ];
523 18 (${*$self}->{SOCKS}->{queue}[0], ${*$self}->{SOCKS}->{queue}[1]) = (${*$self}->{SOCKS}->{queue}[1], ${*$self}->{SOCKS}->{queue}[0]);
524 }
525  
526 44 1;
527 }
528  
529 sub _socks5_connect_auth {
530 # rfc1929
531 24 ▶ my $self = shift;
Subroutine _socks5_connect_auth: called CC=10 SCAR=25.2
532 24 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 24 24
533 24 my ($reads, $sends, $debugs) = (0, 0, 0);
534 my $sock =
535 24 defined(${*$self}->{SOCKS}->{TCP})
536 0 ? ${*$self}->{SOCKS}->{TCP}
537 24 ▶ : $self;
Branch: 1 branch
BranchTrueFalseTotal
defined ${*$self;}->{'SOCKS'}{'TCP'} ? : 0 24 24
538  
539 #--------------------------------------------------------------------------
540 # Send the auth
541 #--------------------------------------------------------------------------
542 # +----+------+----------+------+----------+
543 # |VER | ULEN | UNAME | PLEN | PASSWD |
544 # +----+------+----------+------+----------+
545 # | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
546 # +----+------+----------+------+----------+
547  
548 24 my $uname = ${*$self}->{SOCKS}->{Username};
549 24 my $passwd = ${*$self}->{SOCKS}->{Password};
550 24 my $ulen = length($uname);
551 24 my $plen = length($passwd);
552 24 my $reply;
553 24 ▶ $reply = $sock->_socks_send(pack("CCa${ulen}Ca*", 1, $ulen, $uname, $plen, $passwd), ++$sends)
Branch: 1 branch
BranchTrueFalseTotal
$reply = $sock->_socks_send(pack("CCa${ulen}Ca*", 1, $ulen, $uname, $plen, $passwd), ++$sends) or return _fail($reply) 0 24 24
554 or return _fail($reply);
555  
556 24 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 24 24
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
2400
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
557 0 $debug->add(
558 ver => 1,
559 ulen => $ulen,
560 uname => $uname,
561 plen => $plen,
562 passwd => $passwd
563 );
564 0 $debug->show('Client Send: ');
565 }
566  
567 #--------------------------------------------------------------------------
568 # Read the reply
569 #--------------------------------------------------------------------------
570 # +----+--------+
571 # |VER | STATUS |
572 # +----+--------+
573 # | 1 | 1 |
574 # +----+--------+
575  
576 24 ▶ $reply = $sock->_socks_read(2, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$reply = $sock->_socks_read(2, ++$reads) or return _fail($reply) 6 18 24
577 or return _fail($reply);
578  
579 18 my ($ver, $status) = unpack('CC', $reply);
580  
581 18 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 18 18
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
1800
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
582 0 $debug->add(
583 ver => $ver,
584 status => $status
585 );
586 0 $debug->show('Client Recv: ');
587 }
588  
589 18 ▶ if ($status != AUTHREPLY_SUCCESS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 0) 9 9 18
590 9 $! = ESOCKSPROTO;
591 9 $SOCKS_ERROR->set(AUTHREPLY_FAILURE, $@ = "Authentication failed with SOCKS5 proxy");
592 9 return;
593 }
594  
595 9 return 1;
596 }
597  
598 sub _socks5_connect_command {
599 35 ▶ my $self = shift;
Subroutine _socks5_connect_command: called CC=13 SCAR=32.5
600 35 my $command = shift;
601 35 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 35 35
602 35 my ($reads, $sends, $debugs) = (0, 0, 0);
603 35 ▶ my $resolve = defined(${*$self}->{SOCKS}->{Resolve}) ? ${*$self}->{SOCKS}->{Resolve} : $SOCKS5_RESOLVE;
Branch: 1 branch
BranchTrueFalseTotal
defined ${*$self;}->{'SOCKS'}{'Resolve'} ? : 4 31 35
604 my $sock =
605 35 defined(${*$self}->{SOCKS}->{TCP})
606 0 ? ${*$self}->{SOCKS}->{TCP}
607 35 ▶ : $self;
Branch: 1 branch
BranchTrueFalseTotal
defined ${*$self;}->{'SOCKS'}{'TCP'} ? : 0 35 35
608  
609 #--------------------------------------------------------------------------
610 # Send the command
611 #--------------------------------------------------------------------------
612 # +----+-----+-------+------+----------+----------+
613 # |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
614 # +----+-----+-------+------+----------+----------+
615 # | 1 | 1 | X'00' | 1 | Variable | 2 |
616 # +----+-----+-------+------+----------+----------+
617  
618 35 my ($atyp, $dstaddr) = $resolve ? (ADDR_DOMAINNAME, ${*$self}->{SOCKS}->{CmdAddr}) : _resolve(${*$self}->{SOCKS}->{CmdAddr})
619 35 ▶ or $SOCKS_ERROR->set(ISS_CANT_RESOLVE, $@ = "Can't resolve `" . ${*$self}->{SOCKS}->{CmdAddr} . "'"), return;
Branch: 2 branches
BranchTrueFalseTotal
() = $resolve ? (3, ${*$self;}->{"SOCKS"}{"CmdAddr"}) : _resolve(${*$self;}->{"SOCKS"}{"CmdAddr"}) or $IO::Socket::Socks::SOCKS_ERROR->set(502, $@ = q[Can't resolve `] . ${*$self;}->{"SOCKS"}{"CmdAddr"} . q[']), return 0 35 35
$resolve ? : 35 0 35
620 35 ▶ my $hlen = length($dstaddr) if $resolve;
Branch: 1 branch
BranchTrueFalseTotal
if $resolve 35 0 35
621 35 my $dstport = pack('n', ${*$self}->{SOCKS}->{CmdPort});
622 35 my $reply;
623 35 ▶ $reply = $sock->_socks_send(pack('C4', SOCKS5_VER, $command, 0, $atyp) . (defined($hlen) ? pack('C', $hlen) : '') . $dstaddr . $dstport, ++$sends)
Branch: 2 branches
BranchTrueFalseTotal
$reply = $sock->_socks_send(pack("C4", 5, $command, 0, $atyp) . (defined $hlen ? pack("C", $hlen) : "") . $dstaddr . $dstport, ++$sends) or return _fail($reply) 0 35 35
defined $hlen ? : 35 0 35
624 or return _fail($reply);
625  
626 35 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 35 35
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
3500
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
627 0 $debug->add(
628 ver => SOCKS5_VER,
629 cmd => $command,
630 rsv => 0,
631 atyp => $atyp
632 );
633 0 ▶ $debug->add(hlen => $hlen) if defined $hlen;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hlen 0 0 0
634 $debug->add(
635 dstaddr => $resolve ? $dstaddr : _addr_ntoa($dstaddr, $atyp),
636 0 dstport => ${*$self}->{SOCKS}->{CmdPort}
637 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$resolve ? : 0 0 0
638 0 $debug->show('Client Send: ');
639 }
640  
641 35 return 1;
642 }
643  
644 sub _socks5_connect_reply {
645 44 ▶ my $self = shift;
Subroutine _socks5_connect_reply: called CC=18 SCAR=42.3
646 44 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 44 44
647 44 my ($reads, $sends, $debugs) = (0, 0, 0);
648 my $sock =
649 44 defined(${*$self}->{SOCKS}->{TCP})
650 0 ? ${*$self}->{SOCKS}->{TCP}
651 44 ▶ : $self;
Branch: 1 branch
BranchTrueFalseTotal
defined ${*$self;}->{'SOCKS'}{'TCP'} ? : 0 44 44
652  
653 #--------------------------------------------------------------------------
654 # Read the reply
655 #--------------------------------------------------------------------------
656 # +----+-----+-------+------+----------+----------+
657 # |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
658 # +----+-----+-------+------+----------+----------+
659 # | 1 | 1 | X'00' | 1 | Variable | 2 |
660 # +----+-----+-------+------+----------+----------+
661  
662 44 my $reply;
663 44 ▶ $reply = $sock->_socks_read(4, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$reply = $sock->_socks_read(4, ++$reads) or return _fail($reply) 23 21 44
664 or return _fail($reply);
665  
666 21 my ($ver, $rep, $rsv, $atyp) = unpack('C4', $reply);
667  
668 21 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 21 21
669 0 $debug->add(
670 ver => $ver,
671 rep => $rep,
672 rsv => $rsv,
673 atyp => $atyp
674 );
675 }
676  
677 21 my ($bndaddr, $bndport);
678  
679 21 ▶ if ($atyp == ADDR_DOMAINNAME) {
Branch: 3 branches
BranchTrueFalseTotal
if ($atyp == 3) { } 0 21 21
elsif ($atyp == 1) { } 21 0 21
elsif ($atyp == 4) { } 0 0 0
680 0 ▶ length($reply = $sock->_socks_read(1, ++$reads))
Branch: 1 branch
BranchTrueFalseTotal
length($reply = $sock->_socks_read(1, ++$reads)) or return _fail($reply) 0 0 0
681 or return _fail($reply);
682  
683 0 my $hlen = unpack('C', $reply);
684 0 ▶ $bndaddr = $sock->_socks_read($hlen, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$bndaddr = $sock->_socks_read($hlen, ++$reads) or return _fail($bndaddr) 0 0 0
685 or return _fail($bndaddr);
686  
687 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
688 0 $debug->add(hlen => $hlen);
689 }
690 }
691 elsif ($atyp == ADDR_IPV4) {
692 21 ▶ $bndaddr = $sock->_socks_read(4, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$bndaddr = $sock->_socks_read(4, ++$reads) or return _fail($bndaddr) 0 21 21
693 or return _fail($bndaddr);
694 }
695 elsif ($atyp == ADDR_IPV6) {
696 0 ▶ $bndaddr = $sock->_socks_read(16, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$bndaddr = $sock->_socks_read(16, ++$reads) or return _fail($bndaddr) 0 0 0
697 or return _fail($bndaddr);
698 }
699 else {
700 0 $! = ESOCKSPROTO;
701 0 $SOCKS_ERROR->set(ISS_UNKNOWN_ADDRESS, $@ = "Unsupported address type returned by socks server: $atyp");
702 0 return;
703 }
704  
705 21 ▶ $reply = $sock->_socks_read(2, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$reply = $sock->_socks_read(2, ++$reads) or return _fail($reply) 0 21 21
706 or return _fail($reply);
707 21 $bndport = unpack('n', $reply);
708  
709 21 ${*$self}->{SOCKS}->{DstAddrType} = $atyp;
710 21 ${*$self}->{SOCKS}->{DstAddr} = $bndaddr;
711 21 ${*$self}->{SOCKS}->{DstPort} = $bndport;
712  
713 21 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 21 21
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
2100
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
714 0 my ($addr) = $self->dst;
715 0 $debug->add(
716 bndaddr => $addr,
717 bndport => $bndport
718 );
719 0 $debug->show('Client Recv: ');
720 }
721  
722 21 ▶ if ($rep != REPLY_SUCCESS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rep != 0) 0 21 21
723 0 $! = ESOCKSPROTO;
724 0 ▶ unless (exists $CODES{REPLY}->{$rep}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $IO::Socket::Socks::CODES{"REPLY"}{$rep}) 0 0 0
725 0 $rep = REPLY_GENERAL_FAILURE;
726 }
727 0 $SOCKS_ERROR->set($rep, $@ = $CODES{REPLY}->{$rep});
728 0 return;
729 }
730  
731 21 return 1;
732 }
733  
734  
735 sub _socks4_connect_command {
736 # http://ss5.sourceforge.net/socks4.protocol.txt
737 # http://ss5.sourceforge.net/socks4A.protocol.txt
738 32 ▶ my $self = shift;
Subroutine _socks4_connect_command: called CC=12 SCAR=28.9
739 32 my $command = shift;
740 32 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 32 32
741 32 my ($reads, $sends, $debugs) = (0, 0, 0);
742 32 ▶ my $resolve = defined(${*$self}->{SOCKS}->{Resolve}) ? ${*$self}->{SOCKS}->{Resolve} : $SOCKS4_RESOLVE;
Branch: 1 branch
BranchTrueFalseTotal
defined ${*$self;}->{'SOCKS'}{'Resolve'} ? : 16 16 32
743  
744 #--------------------------------------------------------------------------
745 # Send the command
746 #--------------------------------------------------------------------------
747 # +-----+-----+----------+---------------+----------+------+
748 # | VER | CMD | DST.PORT | DST.ADDR | USERID | NULL |
749 # +-----+-----+----------+---------------+----------+------+
750 # | 1 | 1 | 2 | 4 | variable | 1 |
751 # +-----+-----+----------+---------------+----------+------+
752  
753 16 my $dstaddr = $resolve ? inet_aton('0.0.0.1') : inet_aton(${*$self}->{SOCKS}->{CmdAddr})
754 32 ▶ or $SOCKS_ERROR->set(ISS_CANT_RESOLVE, $@ = "Can't resolve `" . ${*$self}->{SOCKS}->{CmdAddr} . "'"), return;
Branch: 2 branches
BranchTrueFalseTotal
my $dstaddr = $resolve ? inet_aton("0.0.0.1") : inet_aton(${*$self;}->{"SOCKS"}{"CmdAddr"}) or $IO::Socket::Socks::SOCKS_ERROR->set(502, $@ = q[Can't resolve `] . ${*$self;}->{"SOCKS"}{"CmdAddr"} . q[']), return 0 32 32
$resolve ? : 16 16 32
755 32 my $dstport = pack('n', ${*$self}->{SOCKS}->{CmdPort});
756 32 ▶ my $userid = ${*$self}->{SOCKS}->{Username} || '';
Condition50%1/2
${*$self;}->{'SOCKS'}{'Username'} || ''
l!l
032
MC/DC0%0/1
${*$self;}->{'SOCKS'}{'Username'} || ''
${*$self;}->{'SOCKS'}{'Username'}
Truth table50%1/2
A
A= ${*$self;}->{'SOCKS'}{'Username'}
Aresult
11
00
757 32 my $dsthost = '';
758 32 ▶ if ($resolve) { # socks4a
Branch: 1 branch
BranchTrueFalseTotal
if ($resolve) 16 16 32
759 16 $dsthost = ${*$self}->{SOCKS}->{CmdAddr} . pack('C', 0);
760 }
761  
762 32 my $reply;
763 32 ▶ $reply = $self->_socks_send(pack('CC', SOCKS4_VER, $command) . $dstport . $dstaddr . $userid . pack('C', 0) . $dsthost, ++$sends)
Branch: 1 branch
BranchTrueFalseTotal
$reply = $self->_socks_send(pack("CC", 4, $command) . $dstport . $dstaddr . $userid . "\000" . $dsthost, ++$sends) or return _fail($reply) 0 32 32
764 or return _fail($reply);
765  
766 32 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 32 32
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
3200
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
767 $debug->add(
768 ver => SOCKS4_VER,
769 cmd => $command,
770 0 ▶ dstport => ${*$self}->{SOCKS}->{CmdPort},
Branch: 1 branch
BranchTrueFalseTotal
length $dstaddr == 4 ? : 0 0 0
771 0 dstaddr => length($dstaddr) == 4 ? inet_ntoa($dstaddr) : undef,
772 userid => $userid,
773 null => 0
774 );
775 0 ▶ if ($dsthost) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsthost) 0 0 0
776 $debug->add(
777 0 dsthost => ${*$self}->{SOCKS}->{CmdAddr},
778 0 null => 0
779 );
780 }
781 0 $debug->show('Client Send: ');
782 }
783  
784 32 return 1;
785 }
786  
787 sub _socks4_connect_reply {
788 57 ▶ my $self = shift;
Subroutine _socks4_connect_reply: called CC=7 SCAR=23.0
789 57 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 57 57
790 57 my ($reads, $sends, $debugs) = (0, 0, 0);
791  
792 #--------------------------------------------------------------------------
793 # Read the reply
794 #--------------------------------------------------------------------------
795 # +-----+-----+----------+---------------+
796 # | VER | REP | BND.PORT | BND.ADDR |
797 # +-----+-----+----------+---------------+
798 # | 1 | 1 | 2 | 4 |
799 # +-----+-----+----------+---------------+
800  
801 57 my $reply;
802 57 ▶ $reply = $self->_socks_read(8, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$reply = $self->_socks_read(8, ++$reads) or return _fail($reply) 32 25 57
803 or return _fail($reply);
804  
805 25 my ($ver, $rep, $bndport) = unpack('CCn', $reply);
806 25 substr($reply, 0, 4) = '';
807  
808 25 ${*$self}->{SOCKS}->{DstAddrType} = ADDR_IPV4;
809 25 ${*$self}->{SOCKS}->{DstAddr} = $reply;
810 25 ${*$self}->{SOCKS}->{DstPort} = $bndport;
811  
812 25 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 25 25
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
2500
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
813 0 my ($addr) = $self->dst;
814  
815 0 $debug->add(
816 ver => $ver,
817 rep => $rep,
818 bndport => $bndport,
819 bndaddr => $addr
820 );
821 0 $debug->show('Client Recv: ');
822 }
823  
824 25 ▶ if ($rep != REQUEST_GRANTED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rep != 90) 0 25 25
825 0 $! = ESOCKSPROTO;
826 0 ▶ unless (exists $CODES{REPLY}->{$rep}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $IO::Socket::Socks::CODES{"REPLY"}{$rep}) 0 0 0
827 0 $rep = REQUEST_FAILED;
828 }
829 0 $SOCKS_ERROR->set($rep, $@ = $CODES{REPLY}->{$rep});
830 0 return;
831 }
832  
833 25 return 1;
834 }
835  
836 sub accept {
837 46 ▶ my $self = shift;
Subroutine accept: called CC=13 SCAR=30.9
Pod documented
838  
839 46 ▶ croak("Undefined IO::Socket::Socks object passed to accept.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 46 46
840 unless defined($self);
841  
842 46 ▶ if (${*$self}->{SOCKS}->{Listen}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${*$self;}->{'SOCKS'}{'Listen'}) { } 46 0 46
843 46 my $client = $self->SUPER::accept(@_);
844  
845 46 ▶ if (!$client) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client) 0 46 46
846 0 ▶ if ($! == EAGAIN || $! == EWOULDBLOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 11 or $! == 11) { } 0 0 0
Condition0%0/3
$! == 11 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$! == 11 or $! == 11
$! == 11 $! == 11
Truth table0%0/3
A or B
A= $! == 11B= $! == 11
ABresult
1X1
011
000
847 0 $SOCKS_ERROR->set(SOCKS_WANT_READ, "Socks want read");
848 }
849 else {
850 0 $SOCKS_ERROR->set($!, $@ = "Proxy accept new client failed: $!");
851 }
852 0 return;
853 }
854  
855 my $ver =
856 46 ▶ ref ${*$self}->{SOCKS}->{Version}
Branch: 1 branch
BranchTrueFalseTotal
@{${*$self;}->{'SOCKS'}{'Version'};} > 1 ? : 10 0 10
857 10 ? @{ ${*$self}->{SOCKS}->{Version} } > 1
858 10 ? ${*$self}->{SOCKS}->{Version}
859 0 : ${*$self}->{SOCKS}->{Version}->[0]
860 46 ▶ : ${*$self}->{SOCKS}->{Version};
Branch: 1 branch
BranchTrueFalseTotal
ref ${*$self;}->{'SOCKS'}{'Version'} ? : 10 36 46
861  
862 # inherit some socket parameters
863 46 ${*$client}->{SOCKS}->{Debug} = ${*$self}->{SOCKS}->{Debug};
864 46 ${*$client}->{SOCKS}->{Version} = $ver;
865 46 ${*$client}->{SOCKS}->{AuthMethods} = ${*$self}->{SOCKS}->{AuthMethods};
866 46 ${*$client}->{SOCKS}->{UserAuth} = ${*$self}->{SOCKS}->{UserAuth};
867 46 ${*$client}->{SOCKS}->{Resolve} = ${*$self}->{SOCKS}->{Resolve};
868 46 ${*$client}->{SOCKS}->{ready} = 0;
869 46 $client->blocking($self->blocking); # temporarily
870  
871 46 ▶ if (ref $ver) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ver) { } 10 36 46
elsif ($ver == 4) { } 17 19 36
872 10 ${*$client}->{SOCKS}->{queue} = [ [ '_socks_accept', [], undef, [], 0 ] ];
873 }
874 elsif ($ver == 4) {
875 17 ${*$client}->{SOCKS}->{queue} = [ [ '_socks4_accept_command', [], undef, [], 0 ] ];
876  
877 }
878 else {
879 19 ${*$client}->{SOCKS}->{queue} = [
880 19 [ '_socks5_accept', [], undef, [], 0 ],
881 [ '_socks5_accept_if_auth', [], undef, [], 0 ],
882 [ '_socks5_accept_command', [], undef, [], 0 ]
883 ];
884 }
885  
886 46 ▶ defined($client->_run_queue())
Branch: 1 branch
BranchTrueFalseTotal
defined $client->_run_queue or return 0 46 46
887 or return;
888  
889 46 $client->blocking(1); # new socket should be in blocking mode
890 46 return $client;
891 }
892 else {
893 0 ${*$self}->{SOCKS}->{ready} = 0;
894 0 ▶ if ({*$self}->{SOCKS}->{Version} == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ({*$self}->{'SOCKS'}{'Version'} == 4) { } 0 0 0
895 0 push @{ ${*$self}->{SOCKS}->{queue} }, [ '_socks4_connect_reply', [], undef, [], 0 ];
896 }
897 else {
898 0 push @{ ${*$self}->{SOCKS}->{queue} }, [ '_socks5_connect_reply', [], undef, [], 0 ];
899 }
900  
901 0 ▶ defined($self->_run_queue())
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_run_queue or return 0 0 0
902 or return;
903  
904 0 return $self;
905 }
906 }
907  
908 sub _socks_accept {
909 # when 4 and 5 version allowed
910 20 ▶ my $self = shift;
Subroutine _socks_accept: called CC=4 SCAR=14.0
911  
912 20 my $request;
913 20 ▶ $request = $self->_socks_read(1, 0)
Branch: 1 branch
BranchTrueFalseTotal
$request = $self->_socks_read(1, 0) or return _fail($request) 10 10 20
914 or return _fail($request);
915  
916 10 my $ver = unpack('C', $request);
917 10 ▶ if ($ver == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ver == 4) { } 6 4 10
elsif ($ver == 5) { } 4 0 4
918 6 ${*$self}->{SOCKS}->{Version} = 4;
919 6 push @{ ${*$self}->{SOCKS}->{queue} }, [ '_socks4_accept_command', [$ver], undef, [], 0 ];
920 }
921 elsif ($ver == 5) {
922 4 ${*$self}->{SOCKS}->{Version} = 5;
923 4 push @{ ${*$self}->{SOCKS}->{queue} },
924 [ '_socks5_accept', [$ver], undef, [], 0 ],
925 [ '_socks5_accept_if_auth', [], undef, [], 0 ],
926 [ '_socks5_accept_command', [], undef, [], 0 ];
927 }
928 else {
929 0 $! = ESOCKSPROTO;
930 0 $SOCKS_ERROR->set(ISS_BAD_VERSION, $@ = "Socks version should be 4 or 5, $ver recieved");
931 0 return;
932 }
933  
934 10 1;
935 }
936  
937 sub _socks5_accept {
938 39 ▶ my ($self, $ver) = @_;
Subroutine _socks5_accept: called CC=17 SCAR=34.4
939 39 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 39 39
940 39 my ($reads, $sends, $debugs) = (0, 0, 0);
941  
942 #--------------------------------------------------------------------------
943 # Read the auth mechanisms
944 #--------------------------------------------------------------------------
945 # +----+----------+----------+
946 # |VER | NMETHODS | METHODS |
947 # +----+----------+----------+
948 # | 1 | 1 | 1 to 255 |
949 # +----+----------+----------+
950  
951 39 my $request;
952 39 ▶ $request = $self->_socks_read($ver ? 1 : 2, ++$reads)
Branch: 2 branches
BranchTrueFalseTotal
$request = $self->_socks_read($ver ? 1 : 2, ++$reads) or return _fail($request) 11 28 39
$ver ? : 4 35 39
953 or return _fail($request);
954  
955 28 ▶ unless ($ver) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ver) 24 4 28
956 24 $ver = unpack('C', $request);
957 }
958 28 my $nmethods = unpack('C', substr($request, -1, 1));
959  
960 28 ▶ $request = $self->_socks_read($nmethods, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$request = $self->_socks_read($nmethods, ++$reads) or return _fail($request) 5 23 28
961 or return _fail($request);
962  
963 23 my @methods = unpack('C' x $nmethods, $request);
964  
965 23 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 23 23
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
2300
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
966 0 $debug->add(
967 ver => $ver,
968 nmethods => $nmethods,
969 methods => join('', @methods)
970 );
971 0 $debug->show('Server Recv: ');
972 }
973  
974 23 ▶ if ($ver != SOCKS5_VER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver != 5) 0 23 23
975 0 $! = ESOCKSPROTO;
976 0 $SOCKS_ERROR->set(ISS_BAD_VERSION, $@ = "Socks version should be 5, $ver recieved");
977 0 return;
978 }
979  
980 23 ▶ if ($nmethods == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmethods == 0) 0 23 23
981 0 $! = ESOCKSPROTO;
982 0 $SOCKS_ERROR->set(AUTHMECH_INVALID, $@ = "No auth methods sent");
983 0 return;
984 }
985  
986 23 my $authmech;
987  
988 23 foreach my $method (@methods) {
989 25 ▶ if (${*$self}->{SOCKS}->{AuthMethods}->[$method] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (${*$self;}->{"SOCKS"}{"AuthMethods"}[$method] == 1) 23 2 25
990 23 $authmech = $method;
991 23 last;
992 }
993 }
994  
995 23 ▶ if (!defined($authmech)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $authmech) 0 23 23
996 0 $authmech = AUTHMECH_INVALID;
997 }
998  
999 #--------------------------------------------------------------------------
1000 # Send the reply
1001 #--------------------------------------------------------------------------
1002 # +----+--------+
1003 # |VER | METHOD |
1004 # +----+--------+
1005 # | 1 | 1 |
1006 # +----+--------+
1007  
1008 23 ▶ $request = $self->_socks_send(pack('CC', SOCKS5_VER, $authmech), ++$sends)
Branch: 1 branch
BranchTrueFalseTotal
$request = $self->_socks_send(pack("CC", 5, $authmech), ++$sends) or return _fail($request) 0 23 23
1009 or return _fail($request);
1010  
1011 23 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 23 23
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
2300
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
1012 0 $debug->add(
1013 ver => SOCKS5_VER,
1014 method => $authmech
1015 );
1016 0 $debug->show('Server Send: ');
1017 }
1018  
1019 23 ▶ if ($authmech == AUTHMECH_INVALID) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authmech == 255) 0 23 23
1020 0 $! = ESOCKSPROTO;
1021 0 $SOCKS_ERROR->set(AUTHMECH_INVALID, $@ = "No available auth methods");
1022 0 return;
1023 }
1024  
1025 23 return $authmech;
1026 }
1027  
1028 sub _socks5_accept_if_auth {
1029 23 ▶ my $self = shift;
Subroutine _socks5_accept_if_auth: called CC=2 SCAR=6.9
1030  
1031 23 ▶ if (${*$self}->{SOCKS}->{queue_results}{'_socks5_accept'} == AUTHMECH_USERPASS) {
Branch: 1 branch
BranchTrueFalseTotal
if (${*$self;}->{"SOCKS"}{"queue_results"}{"_socks5_accept"} == 2) 2 21 23
1032 2 unshift @{ ${*$self}->{SOCKS}->{queue} }, [ '_socks5_accept_auth', [], undef, [], 0 ];
1033 2 (${*$self}->{SOCKS}->{queue}[0], ${*$self}->{SOCKS}->{queue}[1]) = (${*$self}->{SOCKS}->{queue}[1], ${*$self}->{SOCKS}->{queue}[0]);
1034 }
1035  
1036 23 1;
1037 }
1038  
1039 sub _socks5_accept_auth {
1040 2 ▶ my $self = shift;
Subroutine _socks5_accept_auth: called CC=13 SCAR=30.7
1041 2 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 2 2
1042 2 my ($reads, $sends, $debugs) = (0, 0, 0);
1043  
1044 #--------------------------------------------------------------------------
1045 # Read the auth
1046 #--------------------------------------------------------------------------
1047 # +----+------+----------+------+----------+
1048 # |VER | ULEN | UNAME | PLEN | PASSWD |
1049 # +----+------+----------+------+----------+
1050 # | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
1051 # +----+------+----------+------+----------+
1052  
1053 2 my $request;
1054 2 ▶ $request = $self->_socks_read(2, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$request = $self->_socks_read(2, ++$reads) or return _fail($request) 0 2 2
1055 or return _fail($request);
1056  
1057 2 my ($ver, $ulen) = unpack('CC', $request);
1058 2 ▶ $request = $self->_socks_read($ulen + 1, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$request = $self->_socks_read($ulen + 1, ++$reads) or return _fail($request) 0 2 2
1059 or return _fail($request);
1060  
1061 2 my $uname = substr($request, 0, $ulen);
1062 2 my $plen = unpack('C', substr($request, $ulen));
1063 2 my $passwd;
1064 2 ▶ $passwd = $self->_socks_read($plen, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$passwd = $self->_socks_read($plen, ++$reads) or return _fail($passwd) 0 2 2
1065 or return _fail($passwd);
1066  
1067 2 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 2 2
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
200
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
1068 0 $debug->add(
1069 ver => $ver,
1070 ulen => $ulen,
1071 uname => $uname,
1072 plen => $plen,
1073 passwd => $passwd
1074 );
1075 0 $debug->show('Server Recv: ');
1076 }
1077  
1078 2 my $status = 1;
1079 2 ▶ if (defined(${*$self}->{SOCKS}->{UserAuth})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${*$self;}->{"SOCKS"}{"UserAuth"}) 2 0 2
1080 2 $status = &{ ${*$self}->{SOCKS}->{UserAuth} }($uname, $passwd);
1081 }
1082  
1083 #--------------------------------------------------------------------------
1084 # Send the reply
1085 #--------------------------------------------------------------------------
1086 # +----+--------+
1087 # |VER | STATUS |
1088 # +----+--------+
1089 # | 1 | 1 |
1090 # +----+--------+
1091  
1092 2 ▶ $status = $status ? AUTHREPLY_SUCCESS : 1; #XXX AUTHREPLY_FAILURE broken
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 2 0 2
1093 2 ▶ $request = $self->_socks_send(pack('CC', 1, $status), ++$sends)
Branch: 1 branch
BranchTrueFalseTotal
$request = $self->_socks_send(pack("CC", 1, $status), ++$sends) or return _fail($request) 0 2 2
1094 or return _fail($request);
1095  
1096 2 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 2 2
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
200
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
1097 0 $debug->add(
1098 ver => 1,
1099 status => $status
1100 );
1101 0 $debug->show('Server Send: ');
1102 }
1103  
1104 2 ▶ if ($status != AUTHREPLY_SUCCESS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 0) 0 2 2
1105 0 $! = ESOCKSPROTO;
1106 0 $SOCKS_ERROR->set(AUTHREPLY_FAILURE, $@ = "Authentication failed with SOCKS5 proxy");
1107 0 return;
1108 }
1109  
1110 2 return 1;
1111 }
1112  
1113 sub _socks5_accept_command {
1114 40 ▶ my $self = shift;
Subroutine _socks5_accept_command: called CC=19 SCAR=42.2
1115 40 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 40 40
1116 40 my ($reads, $sends, $debugs) = (0, 0, 0);
1117  
1118 40 @{ ${*$self}->{SOCKS}->{COMMAND} } = ();
1119  
1120 #--------------------------------------------------------------------------
1121 # Read the command
1122 #--------------------------------------------------------------------------
1123 # +----+-----+-------+------+----------+----------+
1124 # |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
1125 # +----+-----+-------+------+----------+----------+
1126 # | 1 | 1 | X'00' | 1 | Variable | 2 |
1127 # +----+-----+-------+------+----------+----------+
1128  
1129 40 my $request;
1130 40 ▶ $request = $self->_socks_read(4, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$request = $self->_socks_read(4, ++$reads) or return _fail($request) 17 23 40
1131 or return _fail($request);
1132  
1133 23 my ($ver, $cmd, $rsv, $atyp) = unpack('CCCC', $request);
1134 23 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 23 23
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
2300
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
1135 0 $debug->add(
1136 ver => $ver,
1137 cmd => $cmd,
1138 rsv => $rsv,
1139 atyp => $atyp
1140 );
1141 }
1142  
1143 23 my $dstaddr;
1144 23 ▶ if ($atyp == ADDR_DOMAINNAME) {
Branch: 3 branches
BranchTrueFalseTotal
if ($atyp == 3) { } 23 0 23
elsif ($atyp == 1) { } 0 0 0
elsif ($atyp == 4) { } 0 0 0
1145 23 ▶ length($request = $self->_socks_read(1, ++$reads))
Branch: 1 branch
BranchTrueFalseTotal
length($request = $self->_socks_read(1, ++$reads)) or return _fail($request) 0 23 23
1146 or return _fail($request);
1147  
1148 23 my $hlen = unpack('C', $request);
1149 23 ▶ $dstaddr = $self->_socks_read($hlen, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$dstaddr = $self->_socks_read($hlen, ++$reads) or return _fail($dstaddr) 0 23 23
1150 or return _fail($dstaddr);
1151  
1152 23 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 23 23
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
2300
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
1153 0 $debug->add(hlen => $hlen);
1154 }
1155 }
1156 elsif ($atyp == ADDR_IPV4) {
1157 0 ▶ $request = $self->_socks_read(4, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$request = $self->_socks_read(4, ++$reads) or return _fail($request) 0 0 0
1158 or return _fail($request);
1159  
1160 0 ▶ $dstaddr = length($request) == 4 ? inet_ntoa($request) : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $request == 4 ? : 0 0 0
1161 }
1162 elsif ($atyp == ADDR_IPV6) {
1163 0 ▶ $request = $self->_socks_read(16, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$request = $self->_socks_read(16, ++$reads) or return _fail($request) 0 0 0
1164 or return _fail($request);
1165  
1166 0 ▶ $dstaddr = length($request) == 16 ? Socket::inet_ntop(AF_INET6, $request) : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $request == 16 ? : 0 0 0
1167 }
1168 else { # unknown address type - how many bytes to read?
1169 0 ${*$self}->{SOCKS}->{queue} = [ [ '_socks5_accept_command_reply', [ REPLY_ADDR_NOT_SUPPORTED, '0.0.0.0', 0 ], undef, [], 0 ] ];
1170 0 $! = ESOCKSPROTO;
1171 0 $SOCKS_ERROR->set(REPLY_ADDR_NOT_SUPPORTED, $@ = $CODES{REPLY}->{REPLY_ADDR_NOT_SUPPORTED});
1172 0 return 1;
1173 }
1174  
1175 23 ▶ $request = $self->_socks_read(2, ++$reads)
Branch: 1 branch
BranchTrueFalseTotal
$request = $self->_socks_read(2, ++$reads) or return _fail($request) 0 23 23
1176 or return _fail($request);
1177  
1178 23 my $dstport = unpack('n', $request);
1179  
1180 23 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 23 23
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
2300
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
1181 0 $debug->add(
1182 dstaddr => $dstaddr,
1183 dstport => $dstport
1184 );
1185 0 $debug->show('Server Recv: ');
1186 }
1187  
1188 23 @{ ${*$self}->{SOCKS}->{COMMAND} } = ($cmd, $dstaddr, $dstport, $atyp);
1189  
1190 23 return 1;
1191 }
1192  
1193 sub _socks5_accept_command_reply {
1194 22 ▶ my $self = shift;
Subroutine _socks5_accept_command_reply: called CC=15 SCAR=35.1
1195 22 my $reply = shift;
1196 22 my $host = shift;
1197 22 my $port = shift;
1198 22 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 22 22
1199 22 ▶ my $resolve = defined(${*$self}->{SOCKS}->{Resolve}) ? ${*$self}->{SOCKS}->{Resolve} : $SOCKS5_RESOLVE;
Branch: 1 branch
BranchTrueFalseTotal
defined ${*$self;}->{'SOCKS'}{'Resolve'} ? : 4 18 22
1200 22 my ($reads, $sends, $debugs) = (0, 0, 0);
1201  
1202 22 ▶ if (!defined($reply) || !defined($host) || !defined($port)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $reply and defined $host and defined $port) 0 22 22
Condition33%2/6
defined $reply and defined $host and defined $port
!ll&&!rl&&r
0022
defined $reply and defined $host
!ll&&!rl&&r
0022
MC/DC0%0/3
defined $reply and defined $host and defined $port
defined $reply defined $host defined $port
Truth table25%1/4
A and B and C
A= defined $replyB= defined $hostC= defined $port
ABCresult
0XX0
10X0
1100
1111
1203 0 croak("You must provide a reply, host, and port on the command reply.");
1204 }
1205  
1206 #--------------------------------------------------------------------------
1207 # Send the reply
1208 #--------------------------------------------------------------------------
1209 # +----+-----+-------+------+----------+----------+
1210 # |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
1211 # +----+-----+-------+------+----------+----------+
1212 # | 1 | 1 | X'00' | 1 | Variable | 2 |
1213 # +----+-----+-------+------+----------+----------+
1214  
1215 22 ▶ my ($atyp, $bndaddr) = $resolve ? _resolve($host) : (ADDR_DOMAINNAME, $host)
Branch: 2 branches
BranchTrueFalseTotal
() = $resolve ? _resolve($host) : (3, $host) or $IO::Socket::Socks::SOCKS_ERROR->set(502, $@ = "Can't resolve `${host}'"), return 0 22 22
$resolve ? : 22 0 22
1216 or $SOCKS_ERROR->set(ISS_CANT_RESOLVE, $@ = "Can't resolve `$host'"), return;
1217 22 ▶ my $hlen = $resolve ? undef : length($bndaddr);
Branch: 1 branch
BranchTrueFalseTotal
$resolve ? : 22 0 22
1218 22 my $rc;
1219 22 ▶ $rc = $self->_socks_send(pack('CCCC', SOCKS5_VER, $reply, 0, $atyp) . ($resolve ? '' : pack('C', $hlen)) . $bndaddr . pack('n', $port), ++$sends)
Branch: 2 branches
BranchTrueFalseTotal
$rc = $self->_socks_send(pack("CCCC", 5, $reply, 0, $atyp) . ($resolve ? "" : pack("C", $hlen)) . $bndaddr . pack("n", $port), ++$sends) or return _fail($rc) 0 22 22
$resolve ? : 22 0 22
1220 or return _fail($rc);
1221  
1222 22 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 22 22
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
2200
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
1223 0 $debug->add(
1224 ver => SOCKS5_VER,
1225 rep => $reply,
1226 rsv => 0,
1227 atyp => $atyp
1228 );
1229 0 ▶ $debug->add(hlen => $hlen) unless $resolve;
Branch: 1 branch
BranchTrueFalseTotal
unless $resolve 0 0 0
1230 0 ▶ $debug->add(
Branch: 1 branch
BranchTrueFalseTotal
$resolve ? : 0 0 0
1231 bndaddr => $resolve ? _addr_ntoa($bndaddr, $atyp) : $bndaddr,
1232 bndport => $port
1233 );
1234 0 $debug->show('Server Send: ');
1235 }
1236  
1237 22 1;
1238 }
1239  
1240 sub _socks4_accept_command {
1241 49 ▶ my ($self, $ver) = @_;
Subroutine _socks4_accept_command: called CC=22 SCAR=34.2
1242 49 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 49 49
1243 49 ▶ my $resolve = defined(${*$self}->{SOCKS}->{Resolve}) ? ${*$self}->{SOCKS}->{Resolve} : $SOCKS4_RESOLVE;
Branch: 1 branch
BranchTrueFalseTotal
defined ${*$self;}->{'SOCKS'}{'Resolve'} ? : 45 4 49
1244 49 my ($reads, $sends, $debugs) = (0, 0, 0);
1245  
1246 49 @{ ${*$self}->{SOCKS}->{COMMAND} } = ();
1247  
1248 #--------------------------------------------------------------------------
1249 # Read the auth mechanisms
1250 #--------------------------------------------------------------------------
1251 # +-----+-----+----------+---------------+----------+------+
1252 # | VER | CMD | DST.PORT | DST.ADDR | USERID | NULL |
1253 # +-----+-----+----------+---------------+----------+------+
1254 # | 1 | 1 | 2 | 4 | variable | 1 |
1255 # +-----+-----+----------+---------------+----------+------+
1256  
1257 49 my $request;
1258 49 ▶ $request = $self->_socks_read($ver ? 7 : 8, ++$reads)
Branch: 2 branches
BranchTrueFalseTotal
$request = $self->_socks_read($ver ? 7 : 8, ++$reads) or return _fail($request) 16 33 49
$ver ? : 6 43 49
1259 or return _fail($request);
1260  
1261 33 ▶ unless ($ver) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ver) 27 6 33
1262 27 $ver = unpack('C', $request);
1263 27 substr($request, 0, 1) = '';
1264 }
1265  
1266 33 my ($cmd, $dstport) = unpack('Cn', $request);
1267 33 substr($request, 0, 3) = '';
1268 33 ▶ my $dstaddr = length($request) == 4 ? inet_ntoa($request) : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $request == 4 ? : 33 0 33
1269  
1270 33 my $userid = '';
1271 33 my $c;
1272  
1273 33 while (1) {
1274 33 ▶ length($c = $self->_socks_read(1, ++$reads))
Branch: 1 branch
BranchTrueFalseTotal
length($c = $self->_socks_read(1, ++$reads)) or return _fail($c) 3 30 33
1275 or return _fail($c);
1276  
1277 30 ▶ if ($c ne "\0") {
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne "\000") { } 0 30 30
1278 0 $userid .= $c;
1279 }
1280 else {
1281 30 last;
1282 }
1283 }
1284  
1285 30 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 30 30
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
3000
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
1286 0 $debug->add(
1287 ver => $ver,
1288 cmd => $cmd,
1289 dstport => $dstport,
1290 dstaddr => $dstaddr,
1291 userid => $userid,
1292 null => 0
1293 );
1294 }
1295  
1296 30 my $atyp = ADDR_IPV4;
1297  
1298 30 ▶ if ($resolve && $dstaddr =~ /^0\.0\.0\.[1-9]/) { # socks4a
Branch: 1 branch
BranchTrueFalseTotal
if ($resolve and $dstaddr =~ /^0\.0\.0\.[1-9]/) 26 4 30
Condition66%2/3
$resolve and $dstaddr =~ /^0\.0\.0\.[1-9]/
!ll&&!rl&&r
4026
MC/DC50%1/2
$resolve and $dstaddr =~ /^0\.0\.0\.[1-9]/
$resolve $dstaddr =~ /^0\.0\.0\.[1-9]/
Truth table66%2/3
A and B
A= $resolveB= $dstaddr =~ /^0\.0\.0\.[1-9]/
ABresult
0X0
100
111
1299 26 $dstaddr = '';
1300 26 $atyp = ADDR_DOMAINNAME;
1301  
1302 26 while (1) {
1303 223 ▶ length($c = $self->_socks_read(1, ++$reads))
Branch: 1 branch
BranchTrueFalseTotal
length($c = $self->_socks_read(1, ++$reads)) or return _fail($c) 7 216 223
1304 or return _fail($c);
1305  
1306 216 ▶ if ($c ne "\0") {
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne "\000") { } 197 19 216
1307 197 $dstaddr .= $c;
1308 }
1309 else {
1310 19 last;
1311 }
1312 }
1313  
1314 19 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 19 19
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
1900
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
1315 0 $debug->add(
1316 dsthost => $dstaddr,
1317 null => 0
1318 );
1319 }
1320 }
1321  
1322 23 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 23 23
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
2300
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
1323 0 $debug->show('Server Recv: ');
1324 }
1325  
1326 23 ▶ if (defined(${*$self}->{SOCKS}->{UserAuth})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${*$self;}->{"SOCKS"}{"UserAuth"}) 4 19 23
1327 4 ▶ unless (&{ ${*$self}->{SOCKS}->{UserAuth} }($userid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&{${*$self;}->{"SOCKS"}{"UserAuth"};}($userid)) 0 4 4
1328 0 ${*$self}->{SOCKS}->{queue} = [ [ '_socks4_accept_command_reply', [ REQUEST_REJECTED_USERID, '0.0.0.0', 0 ], undef, [], 0 ] ];
1329 0 $! = ESOCKSPROTO;
1330 0 $SOCKS_ERROR->set(REQUEST_REJECTED_USERID, $@ = 'Authentication failed with SOCKS4 proxy');
1331 0 return 1;
1332 }
1333 }
1334  
1335 23 ▶ if ($ver != SOCKS4_VER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver != 4) 0 23 23
1336 0 $! = ESOCKSPROTO;
1337 0 $SOCKS_ERROR->set(ISS_BAD_VERSION, $@ = "Socks version should be 4, $ver recieved");
1338 0 return;
1339 }
1340  
1341 23 @{ ${*$self}->{SOCKS}->{COMMAND} } = ($cmd, $dstaddr, $dstport, $atyp);
1342  
1343 23 return 1;
1344 }
1345  
1346 sub _socks4_accept_command_reply {
1347 22 ▶ my $self = shift;
Subroutine _socks4_accept_command_reply: called CC=10 SCAR=29.0
1348 22 my $reply = shift;
1349 22 my $host = shift;
1350 22 my $port = shift;
1351 22 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 22 22
1352 22 my ($reads, $sends, $debugs) = (0, 0, 0);
1353  
1354 22 ▶ if (!defined($reply) || !defined($host) || !defined($port)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $reply and defined $host and defined $port) 0 22 22
Condition33%2/6
defined $reply and defined $host and defined $port
!ll&&!rl&&r
0022
defined $reply and defined $host
!ll&&!rl&&r
0022
MC/DC0%0/3
defined $reply and defined $host and defined $port
defined $reply defined $host defined $port
Truth table25%1/4
A and B and C
A= defined $replyB= defined $hostC= defined $port
ABCresult
0XX0
10X0
1100
1111
1355 0 croak("You must provide a reply, host, and port on the command reply.");
1356 }
1357  
1358 #--------------------------------------------------------------------------
1359 # Send the reply
1360 #--------------------------------------------------------------------------
1361 # +-----+-----+----------+---------------+
1362 # | VER | REP | BND.PORT | BND.ADDR |
1363 # +-----+-----+----------+---------------+
1364 # | 1 | 1 | 2 | 4 |
1365 # +-----+-----+----------+---------------+
1366  
1367 22 ▶ my $bndaddr = inet_aton($host)
Branch: 1 branch
BranchTrueFalseTotal
my $bndaddr = inet_aton($host) or $IO::Socket::Socks::SOCKS_ERROR->set(502, $@ = "Can't resolve `${host}'"), return 0 22 22
1368 or $SOCKS_ERROR->set(ISS_CANT_RESOLVE, $@ = "Can't resolve `$host'"), return;
1369 22 my $rc;
1370 22 ▶ $rc = $self->_socks_send(pack('CCna*', 0, $reply, $port, $bndaddr), ++$sends)
Branch: 1 branch
BranchTrueFalseTotal
$rc = $self->_socks_send(pack("CCna*", 0, $reply, $port, $bndaddr), ++$sends) or return _fail($rc) 0 22 22
1371 or return _fail($rc);
1372  
1373 22 ▶ if ($debug && !$self->_debugged(++$debugs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug and not $self->_debugged(++$debugs)) 0 22 22
Condition33%1/3
$debug and not $self->_debugged(++$debugs)
!ll&&!rl&&r
2200
MC/DC0%0/2
$debug and not $self->_debugged(++$debugs)
$debug not $self->_debugged(++$debugs)
Truth table33%1/3
A and not(B)
A= $debugB= not $self->_debugged(++$debugs)
ABresult
0X0
100
111
1374 0 ▶ $debug->add(
Branch: 1 branch
BranchTrueFalseTotal
length $bndaddr == 4 ? : 0 0 0
1375 ver => 0,
1376 rep => $reply,
1377 bndport => $port,
1378 bndaddr => length($bndaddr) == 4 ? inet_ntoa($bndaddr) : undef
1379 );
1380 0 $debug->show('Server Send: ');
1381 }
1382  
1383 22 1;
1384 }
1385  
1386 sub command {
1387 21 ▶ my $self = shift;
Subroutine command: called CC=5 SCAR=16.5
Pod documented
1388  
1389 21 ▶ unless (exists ${*$self}->{SOCKS}->{RequireAuth}) # TODO: find more correct way
Branch: 1 branch
BranchTrueFalseTotal
if (exists ${*$self;}->{'SOCKS'}{'RequireAuth'}) { } 1 20 21
1390 {
1391 20 return ${*$self}->{SOCKS}->{COMMAND};
1392 }
1393 else {
1394 1 my @keys = qw(Version AuthType RequireAuth UserAuth Username Password
1395 Debug Resolve AuthMethods CmdAddr CmdPort Bind TCP);
1396  
1397 1 my %tmp;
1398 1 $tmp{$_} = ${*$self}->{SOCKS}->{$_} for @keys;
1399  
1400 1 my %args = @_;
1401 1 $self->_configure(\%args);
1402  
1403 1 ▶ if ($self->_connect()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_connect) 1 0 1
1404 1 return 1;
1405 }
1406  
1407 0 ${*$self}->{SOCKS}->{$_} = $tmp{$_} for @keys;
1408 0 return 0;
1409 }
1410 }
1411  
1412 sub command_reply {
1413 44 ▶ my $self = shift;
Subroutine command_reply: called CC=2 SCAR=6.9
Pod documented
1414 44 ${*$self}->{SOCKS}->{ready} = 0;
1415  
1416 44 ▶ if (${*$self}->{SOCKS}->{Version} == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (${*$self;}->{'SOCKS'}{'Version'} == 4) { } 22 22 44
1417 22 ${*$self}->{SOCKS}->{queue} = [ [ '_socks4_accept_command_reply', [@_], undef, [], 0 ] ];
1418 }
1419 else {
1420 22 ${*$self}->{SOCKS}->{queue} = [ [ '_socks5_accept_command_reply', [@_], undef, [], 0 ] ];
1421 }
1422  
1423 44 $self->_run_queue();
1424 }
1425  
1426 sub dst {
1427 0 ▶ my $self = shift;
Subroutine dst: not called CC=1 SCAR=6.9
Pod documented
1428 0 my ($addr, $port, $atype) = @{ ${*$self}->{SOCKS} }{qw/DstAddr DstPort DstAddrType/};
1429 0 return (_addr_ntoa($addr, $atype), $port, $atype);
1430 }
1431  
1432 sub send {
1433 0 ▶ my $self = shift;
Subroutine send: not called CC=22 SCAR=62.3
Pod documented
1434  
1435 0 ▶ unless (defined ${*$self}->{SOCKS}->{TCP}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined ${*$self;}->{"SOCKS"}{"TCP"}) 0 0 0
1436 0 return $self->SUPER::send(@_);
1437 }
1438  
1439 0 my ($msg, $flags, $peer) = @_;
1440 0 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 0 0
1441 0 ▶ my $resolve = defined(${*$self}->{SOCKS}->{Resolve}) ? ${*$self}->{SOCKS}->{Resolve} : $SOCKS5_RESOLVE;
Branch: 1 branch
BranchTrueFalseTotal
defined ${*$self;}->{'SOCKS'}{'Resolve'} ? : 0 0 0
1442  
1443 0 ▶ croak "send: Cannot determine peer address"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $peer 0 0 0
1444 unless defined $peer;
1445  
1446 0 my ($dstport, $dstaddr, $dstaddr_type);
1447 0 ▶ if (ref $peer eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $peer eq 'ARRAY') { } 0 0 0
1448 0 $dstaddr = $peer->[0];
1449 0 $dstport = $peer->[1];
1450 0 $dstaddr_type = ADDR_DOMAINNAME;
1451 }
1452 else {
1453 0 ▶ unless (($dstport, $dstaddr, $dstaddr_type) = eval { (unpack_sockaddr_in($peer), ADDR_IPV4) }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (() = eval { do { unpack_sockaddr_in($peer), 1 } }) 0 0 0
1454 0 ($dstport, $dstaddr, $dstaddr_type) = ((unpack_sockaddr_in6($peer))[ 0, 1 ], ADDR_IPV6);
1455 }
1456 }
1457  
1458 0 my ($sndaddr, $sndport, $sndaddr_type) = $self->dst;
1459 0 ▶ if (($sndaddr eq '0.0.0.0' && $sndaddr_type == ADDR_IPV4) || ($sndaddr eq '::' && $sndaddr_type == ADDR_IPV6)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sndaddr eq "0.0.0.0" and $sndaddr_type == 1 or $sndaddr eq "::" and $sndaddr_type == 4) 0 0 0
Condition0%0/9
$sndaddr eq "0.0.0.0" and $sndaddr_type == 1 or $sndaddr eq "::" and $sndaddr_type == 4
l!l&&r!l&&!r
000
$sndaddr eq "0.0.0.0" and $sndaddr_type == 1
!ll&&!rl&&r
000
$sndaddr eq "::" and $sndaddr_type == 4
!ll&&!rl&&r
000
MC/DC0%0/4
$sndaddr eq "0.0.0.0" and $sndaddr_type == 1 or $sndaddr eq "::" and $sndaddr_type == 4
$sndaddr eq "0.0.0.0" $sndaddr_type == 1 $sndaddr eq "::" $sndaddr_type == 4
Truth table0%0/7
A and B or C and D
A= $sndaddr eq "0.0.0.0"B= $sndaddr_type == 1C= $sndaddr eq "::"D= $sndaddr_type == 4
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1460 0 $sndaddr = ${*$self}->{SOCKS}->{ProxyAddr};
1461 0 $sndaddr_type = ADDR_DOMAINNAME;
1462 }
1463 0 ▶ if ($sndaddr_type == ADDR_DOMAINNAME) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sndaddr_type == 3) { } 0 0 0
1464 0 ▶ ($sndaddr_type, $sndaddr) = _resolve($sndaddr)
Branch: 1 branch
BranchTrueFalseTotal
($sndaddr_type, $sndaddr) = _resolve($sndaddr) or $IO::Socket::Socks::SOCKS_ERROR->set(502, $@ = "Can't resolve `${sndaddr}'"), return 0 0 0
1465 or $SOCKS_ERROR->set(ISS_CANT_RESOLVE, $@ = "Can't resolve `$sndaddr'"), return;
1466 }
1467 else {
1468 0 $sndaddr = ${*$self}->{SOCKS}->{DstAddr};
1469 }
1470  
1471 0 ▶ $peer = $sndaddr_type == ADDR_IPV4 ? pack_sockaddr_in($sndport, $sndaddr) : pack_sockaddr_in6($sndport, $sndaddr);
Branch: 1 branch
BranchTrueFalseTotal
$sndaddr_type == 1 ? : 0 0 0
1472  
1473 0 my $hlen;
1474 0 ▶ if ($dstaddr_type == ADDR_DOMAINNAME) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dstaddr_type == 3) 0 0 0
1475 0 ▶ if ($resolve) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolve) { } 0 0 0
1476 0 $hlen = length $dstaddr;
1477 }
1478 else {
1479 0 ▶ ($dstaddr_type, $dstaddr) = _resolve($dstaddr)
Branch: 1 branch
BranchTrueFalseTotal
($dstaddr_type, $dstaddr) = _resolve($dstaddr) or $IO::Socket::Socks::SOCKS_ERROR->set(502, $@ = "Can't resolve `${dstaddr}'"), return 0 0 0
1480 or $SOCKS_ERROR->set(ISS_CANT_RESOLVE, $@ = "Can't resolve `$dstaddr'"), return;
1481 }
1482 }
1483  
1484 0 ▶ my $msglen = $debug ? length($msg) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$debug ? : 0 0 0
1485  
1486 # we need to add socks header to the message
1487 # +----+------+------+----------+----------+----------+
1488 # |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
1489 # +----+------+------+----------+----------+----------+
1490 # | 2 | 1 | 1 | Variable | 2 | Variable |
1491 # +----+------+------+----------+----------+----------+
1492 0 ▶ $msg = pack('C4', 0, 0, 0, $dstaddr_type) . (defined $hlen ? pack('C', $hlen) : '') . $dstaddr . pack('n', $dstport) . $msg;
Branch: 1 branch
BranchTrueFalseTotal
defined $hlen ? : 0 0 0
1493  
1494 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
1495 0 $debug->add(
1496 rsv => '00',
1497 frag => '0',
1498 atyp => $dstaddr_type
1499 );
1500 0 ▶ $debug->add(hlen => $hlen) if defined $hlen;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hlen 0 0 0
1501 0 ▶ $debug->add(
Branch: 1 branch
BranchTrueFalseTotal
defined $hlen ? : 0 0 0
1502 dstaddr => defined $hlen ? $dstaddr : _addr_ntoa($dstaddr, $dstaddr_type),
1503 dstport => $dstport,
1504 data => "...($msglen)"
1505 );
1506 0 $debug->show('Client Send: ');
1507 }
1508  
1509 0 $self->SUPER::send($msg, $flags, $peer);
1510 }
1511  
1512 sub recv {
1513 0 ▶ my $self = shift;
Subroutine recv: not called CC=12 SCAR=50.5
Pod documented
1514  
1515 0 ▶ unless (defined ${*$self}->{SOCKS}->{TCP}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined ${*$self;}->{"SOCKS"}{"TCP"}) 0 0 0
1516 0 return $self->SUPER::recv(@_);
1517 }
1518  
1519 0 ▶ my $debug = IO::Socket::Socks::Debug->new() if ${*$self}->{SOCKS}->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if ${*$self;}->{"SOCKS"}{"Debug"} 0 0 0
1520  
1521 0 ▶ defined($self->SUPER::recv($_[0], $_[1] + 262, $_[2]))
Branch: 1 branch
BranchTrueFalseTotal
defined $self->SUPER::recv($_[0], $_[1] + 262, $_[2]) or return 0 0 0
1522 or return;
1523  
1524 # we need to remove socks header from the message
1525 # +----+------+------+----------+----------+----------+
1526 # |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
1527 # +----+------+------+----------+----------+----------+
1528 # | 2 | 1 | 1 | Variable | 2 | Variable |
1529 # +----+------+------+----------+----------+----------+
1530 0 my $rsv = join('', unpack('C2', $_[0]));
1531 0 substr($_[0], 0, 2) = '';
1532  
1533 0 my ($frag, $atyp) = unpack('C2', $_[0]);
1534 0 substr($_[0], 0, 2) = '';
1535  
1536 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
1537 0 $debug->add(
1538 rsv => $rsv,
1539 frag => $frag,
1540 atyp => $atyp
1541 );
1542 }
1543  
1544 0 my $dstaddr;
1545 0 ▶ if ($atyp == ADDR_DOMAINNAME) {
Branch: 3 branches
BranchTrueFalseTotal
if ($atyp == 3) { } 0 0 0
elsif ($atyp == 1) { } 0 0 0
elsif ($atyp == 4) { } 0 0 0
1546 0 my $hlen = unpack('C', $_[0]);
1547 0 $dstaddr = substr($_[0], 1, $hlen);
1548 0 substr($_[0], 0, $hlen + 1) = '';
1549  
1550 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
1551 0 $debug->add(hlen => $hlen);
1552 }
1553 }
1554 elsif ($atyp == ADDR_IPV4) {
1555 0 $dstaddr = substr($_[0], 0, 4);
1556 0 substr($_[0], 0, 4) = '';
1557 }
1558 elsif ($atyp == ADDR_IPV6) {
1559 0 $dstaddr = substr($_[0], 0, 16);
1560 0 substr($_[0], 0, 16) = '';
1561 }
1562 else {
1563 0 $! = ESOCKSPROTO;
1564 0 $SOCKS_ERROR->set(ISS_UNKNOWN_ADDRESS, $@ = "Unsupported address type returned by socks server: $atyp");
1565 0 return;
1566 }
1567  
1568 0 my $dstport = unpack('n', $_[0]);
1569 0 substr($_[0], 0, 2) = '';
1570  
1571 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
1572 0 $debug->add(
1573 dstaddr => _addr_ntoa($dstaddr, $atyp),
1574 dstport => $dstport,
1575 data => "...(" . length($_[0]) . ")"
1576 );
1577 0 $debug->show('Client Recv: ');
1578 }
1579  
1580 0 ▶ return pack_sockaddr_in($dstport, $dstaddr) if $atyp == ADDR_IPV4;
Branch: 1 branch
BranchTrueFalseTotal
if $atyp == 1 0 0 0
1581 0 ▶ return pack_sockaddr_in6($dstport, $dstaddr) if $atyp == ADDR_IPV6;
Branch: 1 branch
BranchTrueFalseTotal
if $atyp == 4 0 0 0
1582 0 return [ $dstaddr, $dstport ];
1583 }
1584  
1585 #+-----------------------------------------------------------------------------
1586 #| Helper Functions
1587 #+-----------------------------------------------------------------------------
1588 sub _socks_send {
1589 255 ▶ my $self = shift;
Subroutine _socks_send: called CC=18 SCAR=34.3
1590 255 my $data = shift;
1591 255 my $numb = shift;
1592  
1593 255 local $SIG{PIPE} = 'IGNORE';
1594 255 $SOCKS_ERROR->set();
1595  
1596 255 my $rc;
1597 255 my $writed = 0;
1598 255 ▶ my $blocking = ${*$self}{io_socket_timeout} ? $self->blocking(0) : $self->blocking;
Branch: 1 branch
BranchTrueFalseTotal
do { *$self }->{'io_socket_timeout'} ? : 0 255 255
1599  
1600 255 ▶ unless ($blocking || ${*$self}{io_socket_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($blocking or do { *$self }->{"io_socket_timeout"}) 154 101 255
Condition66%2/3
$blocking or do { *$self }->{"io_socket_timeout"}
l!l&&r!l&&!r
1010154
MC/DC50%1/2
$blocking or do { *$self }->{"io_socket_timeout"}
$blocking do { *$self }->{"io_socket_timeout"}
Truth table66%2/3
A or B
A= $blockingB= do { *$self }->{"io_socket_timeout"}
ABresult
1X1
011
000
1601 154 ▶ if (${*$self}->{SOCKS}->{queue}[0][Q_SENDS] >= $numb) { # already sent
Branch: 1 branch
BranchTrueFalseTotal
if (${*$self;}->{"SOCKS"}{"queue"}[0][4] >= $numb) 38 116 154
1602 38 return 1;
1603 }
1604  
1605 116 ▶ if (defined ${*$self}->{SOCKS}->{queue}[0][Q_BUF]) { # some chunk already sent
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${*$self;}->{"SOCKS"}{"queue"}[0][2]) 0 116 116
1606 0 substr($data, 0, ${*$self}->{SOCKS}->{queue}[0][Q_BUF]) = '';
1607 }
1608  
1609 116 ▶ while (length $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 116 116 232
1610 116 $rc = $self->syswrite($data);
1611 116 ▶ if (defined $rc) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rc) { } 116 0 116
elsif ($! == 11 or $! == 11) { } 0 0 0
1612 116 ▶ if ($rc > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc > 0) { } 116 0 116
1613 116 ${*$self}->{SOCKS}->{queue}[0][Q_BUF] += $rc;
1614 116 substr($data, 0, $rc) = '';
1615 }
1616 else { # XXX: socket closed? if smth writed, but not all?
1617 0 last;
1618 }
1619 }
1620 ▶ elsif ($! == EWOULDBLOCK || $! == EAGAIN) {
Condition0%0/3
$! == 11 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$! == 11 or $! == 11
$! == 11 $! == 11
Truth table0%0/3
A or B
A= $! == 11B= $! == 11
ABresult
1X1
011
000
1621 0 $SOCKS_ERROR->set(SOCKS_WANT_WRITE, 'Socks want write');
1622 0 return undef;
1623 }
1624 else {
1625 0 $SOCKS_ERROR->set($!, $@ = "send: $!");
1626 0 last;
1627 }
1628 }
1629  
1630 116 $writed = int(${*$self}->{SOCKS}->{queue}[0][Q_BUF]);
1631 116 ${*$self}->{SOCKS}->{queue}[0][Q_BUF] = undef;
1632 116 ${*$self}->{SOCKS}->{queue}[0][Q_SENDS]++;
1633 116 return $writed;
1634 }
1635  
1636 101 my $selector = IO::Select->new($self);
1637 101 my $start = time();
1638  
1639 101 while (1) {
1640 101 ▶ if (${*$self}{io_socket_timeout} && time() - $start >= ${*$self}{io_socket_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { *$self }->{"io_socket_timeout"} and time - $start >= do { *$self }->{"io_socket_timeout"}) 0 101 101
Condition33%1/3
do { *$self }->{"io_socket_timeout"} and time - $start >= do { *$self }->{"io_socket_timeout"}
!ll&&!rl&&r
10100
MC/DC0%0/2
do { *$self }->{"io_socket_timeout"} and time - $start >= do { *$self }->{"io_socket_timeout"}
do { *$self }->{"io_socket_timeout"} time - $start >= do { *$self }->{"io_socket_timeout"}
Truth table33%1/3
A and B
A= do { *$self }->{"io_socket_timeout"}B= time - $start >= do { *$self }->{"io_socket_timeout"}
ABresult
0X0
100
111
1641 0 $! = ETIMEDOUT;
1642 0 last;
1643 }
1644  
1645 101 ▶ unless ($selector->can_write(1)) { # socket couldn't accept data for now, check if timeout expired and try again
Branch: 1 branch
BranchTrueFalseTotal
unless ($selector->can_write(1)) 0 101 101
1646 0 next;
1647 }
1648  
1649 101 $rc = $self->syswrite($data);
1650 101 ▶ if ($rc > 0) { # reduce our message
Branch: 1 branch
BranchTrueFalseTotal
if ($rc > 0) { } 101 0 101
1651 101 $writed += $rc;
1652 101 substr($data, 0, $rc) = '';
1653 101 ▶ if (length($data) == 0) { # all data successfully writed
Branch: 1 branch
BranchTrueFalseTotal
if (length $data == 0) 101 0 101
1654 101 last;
1655 }
1656 }
1657 else { # some error in the socket; will return false
1658 0 ▶ $SOCKS_ERROR->set($!, $@ = "send: $!") unless defined $rc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rc 0 0 0
1659 0 last;
1660 }
1661 }
1662  
1663 101 ▶ $self->blocking(1) if $blocking;
Branch: 1 branch
BranchTrueFalseTotal
if $blocking 101 0 101
1664  
1665 101 return $writed;
1666 }
1667  
1668 sub _socks_read {
1669 750 ▶ my $self = shift;
Subroutine _socks_read: called CC=19 SCAR=31.6
1670 750 ▶ my $length = shift || 1;
Condition50%1/2
shift() || 1
l!l
7500
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1671 750 my $numb = shift;
1672  
1673 750 $SOCKS_ERROR->set();
1674 750 my $data = '';
1675 750 my ($buf, $rc);
1676 750 my $blocking = $self->blocking;
1677  
1678 # non-blocking read
1679 750 ▶ unless ($blocking || ${*$self}{io_socket_timeout}) { # no timeout should be specified for non-blocking connect
Branch: 1 branch
BranchTrueFalseTotal
unless ($blocking or do { *$self }->{"io_socket_timeout"}) 595 155 750
Condition66%2/3
$blocking or do { *$self }->{"io_socket_timeout"}
l!l&&r!l&&!r
1550595
MC/DC50%1/2
$blocking or do { *$self }->{"io_socket_timeout"}
$blocking do { *$self }->{"io_socket_timeout"}
Truth table66%2/3
A or B
A= $blockingB= do { *$self }->{"io_socket_timeout"}
ABresult
1X1
011
000
1680 595 ▶ if (defined ${*$self}->{SOCKS}->{queue}[0][Q_READS][$numb]) { # already readed
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${*$self;}->{"SOCKS"}{"queue"}[0][3][$numb]) 63 532 595
1681 63 return ${*$self}->{SOCKS}->{queue}[0][Q_READS][$numb];
1682 }
1683  
1684 532 ▶ if (defined ${*$self}->{SOCKS}->{queue}[0][Q_BUF]) { # some chunk already readed
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${*$self;}->{"SOCKS"}{"queue"}[0][2]) 6 526 532
1685 6 $data = ${*$self}->{SOCKS}->{queue}[0][Q_BUF];
1686 6 $length -= length $data;
1687 }
1688  
1689 532 ▶ while ($length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 535 370 905
1690 535 $rc = $self->sysread($buf, $length);
1691 535 ▶ if (defined $rc) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rc) { } 373 162 535
elsif ($! == 11 or $! == 11) { } 162 0 162
1692 373 ▶ if ($rc > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc > 0) { } 373 0 373
1693 373 $length -= $rc;
1694 373 $data .= $buf;
1695 }
1696 else { # XXX: socket closed, if smth readed but not all?
1697 0 last;
1698 }
1699 }
1700 ▶ elsif ($! == EWOULDBLOCK || $! == EAGAIN) { # no data to read
Condition33%1/3
$! == 11 or $! == 11
l!l&&r!l&&!r
16200
MC/DC0%0/2
$! == 11 or $! == 11
$! == 11 $! == 11
Truth table33%1/3
A or B
A= $! == 11B= $! == 11
ABresult
1X1
011
000
1701 162 ▶ if (length $data) { # save already readed data in the queue buffer
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 6 156 162
1702 6 ${*$self}->{SOCKS}->{queue}[0][Q_BUF] = $data;
1703 }
1704 162 $SOCKS_ERROR->set(SOCKS_WANT_READ, 'Socks want read');
1705 162 return undef;
1706 }
1707 else {
1708 0 $SOCKS_ERROR->set($!, $@ = "read: $!");
1709 0 last;
1710 }
1711 }
1712  
1713 370 ${*$self}->{SOCKS}->{queue}[0][Q_BUF] = undef;
1714 370 ${*$self}->{SOCKS}->{queue}[0][Q_READS][$numb] = $data;
1715 370 return $data;
1716 }
1717  
1718 # blocking read
1719 155 my $selector = IO::Select->new($self);
1720 155 my $start = time();
1721  
1722 155 ▶ while ($length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 171 155 326
1723 171 ▶ if (${*$self}{io_socket_timeout} && time() - $start >= ${*$self}{io_socket_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { *$self }->{"io_socket_timeout"} and time - $start >= do { *$self }->{"io_socket_timeout"}) 0 171 171
Condition33%1/3
do { *$self }->{"io_socket_timeout"} and time - $start >= do { *$self }->{"io_socket_timeout"}
!ll&&!rl&&r
17100
MC/DC0%0/2
do { *$self }->{"io_socket_timeout"} and time - $start >= do { *$self }->{"io_socket_timeout"}
do { *$self }->{"io_socket_timeout"} time - $start >= do { *$self }->{"io_socket_timeout"}
Truth table33%1/3
A and B
A= do { *$self }->{"io_socket_timeout"}B= time - $start >= do { *$self }->{"io_socket_timeout"}
ABresult
0X0
100
111
1724 0 $! = ETIMEDOUT;
1725 0 last;
1726 }
1727  
1728 171 ▶ unless ($selector->can_read(1)) { # no data in socket for now, check if timeout expired and try again
Branch: 1 branch
BranchTrueFalseTotal
unless ($selector->can_read(1)) 16 155 171
1729 16 next;
1730 }
1731  
1732 155 $rc = $self->sysread($buf, $length);
1733 155 ▶ if (defined $rc && $rc > 0) { # reduce limit and modify buffer
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rc and $rc > 0) { } 155 0 155
Condition33%1/3
defined $rc and $rc > 0
!ll&&!rl&&r
00155
MC/DC0%0/2
defined $rc and $rc > 0
defined $rc $rc > 0
Truth table33%1/3
A and B
A= defined $rcB= $rc > 0
ABresult
0X0
100
111
1734 155 $length -= $rc;
1735 155 $data .= $buf;
1736 }
1737 else { # EOF or error in the socket
1738 0 ▶ $SOCKS_ERROR->set($!, $@ = "read: $!") unless defined $rc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rc 0 0 0
1739 0 last; # TODO handle unexpected EOF more correct
1740 }
1741 }
1742  
1743 # XXX it may return incomplete $data if timed out. Could it break smth?
1744 155 return $data;
1745 }
1746  
1747 sub _debugged {
1748 0 ▶ my ($self, $debugs) = @_;
Subroutine _debugged: not called CC=2 SCAR=17.9
1749  
1750 0 ▶ if (${*$self}->{SOCKS}->{queue}[0][Q_DEBUGS] >= $debugs) {
Branch: 1 branch
BranchTrueFalseTotal
if (${*$self;}->{"SOCKS"}{"queue"}[0][5] >= $debugs) 0 0 0
1751 0 return 1;
1752 }
1753  
1754 0 ${*$self}->{SOCKS}->{queue}[0][Q_DEBUGS] = $debugs;
1755 0 return 0;
1756 }
1757  
1758 sub _fail {
1759 162 ▶ if (!@_ || defined($_[0])) {
Subroutine _fail: called CC=4 SCAR=20.9
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ or defined $_[0]) 0 162 162
Condition33%1/3
not @_ or defined $_[0]
l!l&&r!l&&!r
00162
MC/DC0%0/2
not @_ or defined $_[0]
not @_ defined $_[0]
Truth table33%1/3
not(A) or B
A= not @_B= defined $_[0]
ABresult
1X1
011
000
1760 0 ▶ $SOCKS_ERROR->set(ECONNABORTED, $@ = 'Socket closed by remote side') if $SOCKS_ERROR == undef;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Socket::Socks::SOCKS_ERROR == undef 0 0 0
1761 0 return;
1762 }
1763  
1764 162 return -1;
1765 }
1766  
1767 sub _validate_multi_version {
1768 1 ▶ my $multi_ver = shift;
Subroutine _validate_multi_version: called CC=8 SCAR=30.8
1769  
1770 1 ▶ if (@$multi_ver == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$multi_ver == 1) 0 1 1
1771 0 ▶ return $multi_ver->[0] == 4 || $multi_ver->[0] == 5;
Condition0%0/3
$multi_ver->[0] == 4 || $multi_ver->[0] == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
$multi_ver->[0] == 4 || $multi_ver->[0] == 5
$multi_ver->[0] == 4 $multi_ver->[0] == 5
Truth table0%0/3
A || B
A= $multi_ver->[0] == 4B= $multi_ver->[0] == 5
ABresult
1X1
011
000
1772 }
1773  
1774 1 ▶ if (@$multi_ver == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$multi_ver == 2) 1 0 1
1775 return
1776 1 ▶ $multi_ver->[0] != $multi_ver->[1]
Condition33%4/12
$multi_ver->[0] != $multi_ver->[1] && ($multi_ver->[0] == 4 || $multi_ver->[0] == 5) && ($multi_ver->[1] == 4 || $multi_ver->[1] == 5)
!ll&&!rl&&r
001
$multi_ver->[0] != $multi_ver->[1] and $multi_ver->[0] == 4 || $multi_ver->[0] == 5
!ll&&!rl&&r
001
$multi_ver->[0] == 4 or $multi_ver->[0] == 5
l!l&&r!l&&!r
100
$multi_ver->[1] == 4 or $multi_ver->[1] == 5
l!l&&r!l&&!r
010
MC/DC0%0/5
$multi_ver->[0] != $multi_ver->[1] && ($multi_ver->[0] == 4 || $multi_ver->[0] == 5) && ($multi_ver->[1] == 4 || $multi_ver->[1] == 5)
$multi_ver->[0] != $multi_ver->[1] $multi_ver->[0] == 4 $multi_ver->[0] == 5 $multi_ver->[1] == 4 $multi_ver->[1] == 5
Truth table12%1/8
A and B or C && D or E
A= $multi_ver->[0] != $multi_ver->[1]B= $multi_ver->[0] == 4C= $multi_ver->[0] == 5D= $multi_ver->[1] == 4E= $multi_ver->[1] == 5
ABCDEresult
0XXXX0
100XX0
11X000
101000
11X1X1
11X011
1011X1
101011
1777 && ($multi_ver->[0] == 4 || $multi_ver->[0] == 5)
1778 && ($multi_ver->[1] == 4 || $multi_ver->[1] == 5);
1779 }
1780  
1781 0 return;
1782 }
1783  
1784 sub _resolve {
1785 22 ▶ my $addr = shift;
Subroutine _resolve: called CC=4 SCAR=14.6
1786 22 my ($err, @res) = Socket::getaddrinfo($addr, undef, { protocol => Socket::IPPROTO_TCP, socktype => Socket::SOCK_STREAM });
1787 22 ▶ return if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 22 22
1788  
1789 22 for my $r (@res) {
1790 22 ▶ if ($r->{family} == PF_INET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"family"} == 2) 22 0 22
1791 22 return (ADDR_IPV4, (unpack_sockaddr_in($r->{addr}))[1]);
1792 }
1793 }
1794  
1795 0 return (ADDR_IPV6, (unpack_sockaddr_in6($res[0]{addr}))[1]);
1796 }
1797  
1798 sub _addr_ntoa {
1799 0 ▶ my ($addr, $atype) = @_;
Subroutine _addr_ntoa: not called CC=3 SCAR=24.8
1800  
1801 0 ▶ return inet_ntoa($addr) if ($atype == ADDR_IPV4);
Branch: 1 branch
BranchTrueFalseTotal
if $atype == 1 0 0 0
1802 0 ▶ return Socket::inet_ntop(AF_INET6, $addr) if ($atype == ADDR_IPV6);
Branch: 1 branch
BranchTrueFalseTotal
if $atype == 4 0 0 0
1803 0 return $addr;
1804 }
1805  
1806 ###############################################################################
1807 #+-----------------------------------------------------------------------------
1808 #| Helper Package to bring some magic in $SOCKS_ERROR
1809 #+-----------------------------------------------------------------------------
1810 ###############################################################################
1811  
1812 package IO::Socket::Socks::Error;
1813  
1814 use overload
1815 '==' => \&num_eq,
1816 0 ▶ '!=' => sub { !num_eq(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1817 30 '""' => \&as_str,
1818 30 ▶ '0+' => \&as_num;
Subroutine BEGIN: called CC=1 SCAR=0.0
1819  
1820 sub new {
1821 30 ▶ my ($class, $num, $str) = @_;
Subroutine new: called CC=1 SCAR=0.0
1822  
1823 30 my $self = {
1824 num => $num,
1825 str => $str,
1826 };
1827  
1828 30 bless $self, $class;
1829 }
1830  
1831 sub set {
1832 1344 ▶ my ($self, $num, $str) = @_;
Subroutine set: called CC=2 SCAR=6.9
1833  
1834 1344 ▶ $self->{num} = defined $num ? int($num) : $num;
Branch: 1 branch
BranchTrueFalseTotal
defined $num ? : 211 1133 1344
1835 1344 $self->{str} = $str;
1836 }
1837  
1838 sub as_str {
1839 0 ▶ my $self = shift;
Subroutine as_str: not called CC=1 SCAR=6.9
1840 0 return $self->{str};
1841 }
1842  
1843 sub as_num {
1844 0 ▶ my $self = shift;
Subroutine as_num: not called CC=1 SCAR=6.9
1845 0 return $self->{num};
1846 }
1847  
1848 sub num_eq {
1849 223 ▶ my ($self, $num) = @_;
Subroutine num_eq: called CC=2 SCAR=6.9
1850  
1851 223 ▶ unless (defined $num) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $num) 85 138 223
1852 85 return !defined($self->{num});
1853 }
1854 138 return $self->{num} == int($num);
1855 }
1856  
1857 ###############################################################################
1858 #+-----------------------------------------------------------------------------
1859 #| Helper Package to prevent modifications of $SOCKS_ERROR outside this package
1860 #+-----------------------------------------------------------------------------
1861 ###############################################################################
1862  
1863 package IO::Socket::Socks::ReadOnlyVar;
1864  
1865 sub TIESCALAR {
1866 30 ▶ my ($class, $value) = @_;
Subroutine TIESCALAR: called CC=1 SCAR=0.0
1867 30 bless \$value, $class;
1868 }
1869  
1870 sub FETCH {
1871 3134 ▶ my $self = shift;
Subroutine FETCH: called CC=1 SCAR=0.0
1872 3134 return $$self;
1873 }
1874  
1875 0 ▶*STORE = *UNTIE = sub { Carp::croak 'Modification of readonly value attempted' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
1876  
1877 ###############################################################################
1878 #+-----------------------------------------------------------------------------
1879 #| Helper Package to handle assigning of $SOCKET_CLASS
1880 #+-----------------------------------------------------------------------------
1881 ###############################################################################
1882  
1883 package IO::Socket::Socks::SocketClassVar;
1884  
1885 sub TIESCALAR {
1886 30 ▶ my ($class, $value) = @_;
Subroutine TIESCALAR: called CC=1 SCAR=0.0
1887 30 bless { v => $value }, $class;
1888 }
1889  
1890 sub FETCH {
1891 65 ▶ return $_[0]->{v};
Subroutine FETCH: called CC=1 SCAR=0.0
1892 }
1893  
1894 sub STORE {
1895 30 ▶ my ($self, $class) = @_;
Subroutine STORE: called CC=2 SCAR=7.0
1896  
1897 30 $self->{v} = $class;
1898 30 ▶ eval "use $class; 1" or die $@;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "use $class; 1" or die $@ 0 30 30
1899 30 $IO::Socket::Socks::ISA[1] = $class;
1900 }
1901  
1902 sub UNTIE {
1903 0 ▶ Carp::croak 'Untie of tied variable is denied';
Subroutine UNTIE: not called CC=1 SCAR=6.9
1904 }
1905  
1906 ###############################################################################
1907 #+-----------------------------------------------------------------------------
1908 #| Helper Package to display pretty debug messages
1909 #+-----------------------------------------------------------------------------
1910 ###############################################################################
1911  
1912 package IO::Socket::Socks::Debug;
1913  
1914 sub new {
1915 0 ▶ my ($class) = @_;
Subroutine new: not called CC=1 SCAR=6.9
1916 0 my $self = [];
1917  
1918 0 bless $self, $class;
1919 }
1920  
1921 sub add {
1922 0 ▶ my $self = shift;
Subroutine add: not called CC=1 SCAR=6.9
1923 0 push @{$self}, @_;
1924 }
1925  
1926 sub show {
1927 0 ▶ my ($self, $tag) = @_;
Subroutine show: not called CC=1 SCAR=6.9
1928  
1929 0 $self->_separator($tag);
1930 0 $self->_row(0, $tag);
1931 0 $self->_separator($tag);
1932 0 $self->_row(1, $tag);
1933 0 $self->_separator($tag);
1934  
1935 0 print STDERR "\n";
1936  
1937 0 @{$self} = ();
1938 }
1939  
1940 sub _separator {
1941 0 ▶ my $self = shift;
Subroutine _separator: not called CC=3 SCAR=24.8
1942 0 my $tag = shift;
1943 0 my ($row1_len, $row2_len, $len);
1944  
1945 0 print STDERR $tag, '+';
1946  
1947 0 ▶ for (my $i = 0 ; $i < @$self ; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$self) 0 0 0
1948 0 $row1_len = length($self->[$i]);
1949 0 $row2_len = length($self->[ $i + 1 ]);
1950 0 ▶ $len = ($row1_len > $row2_len ? $row1_len : $row2_len) + 2;
Branch: 1 branch
BranchTrueFalseTotal
$row1_len > $row2_len ? : 0 0 0
1951  
1952 0 print STDERR '-' x $len, '+';
1953 }
1954  
1955 0 print STDERR "\n";
1956 }
1957  
1958 sub _row {
1959 0 ▶ my $self = shift;
Subroutine _row: not called CC=3 SCAR=24.8
1960 0 my $row = shift;
1961 0 my $tag = shift;
1962 0 my ($row1_len, $row2_len, $len);
1963  
1964 0 print STDERR $tag, '|';
1965  
1966 0 ▶ for (my $i = 0 ; $i < @$self ; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$self) 0 0 0
1967 0 $row1_len = length($self->[$i]);
1968 0 $row2_len = length($self->[ $i + 1 ]);
1969 0 ▶ $len = ($row1_len > $row2_len ? $row1_len : $row2_len);
Branch: 1 branch
BranchTrueFalseTotal
$row1_len > $row2_len ? : 0 0 0
1970  
1971 0 printf STDERR ' %-' . $len . 's |', $self->[ $i + $row ];
1972 }
1973  
1974 0 print STDERR "\n";
1975 }
1976  
1977 30 1;
1978  
1979 __END__
↑ Summary