blib/lib/IO/Socket/Socks.pm

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