Wrapper.pm

filter: Statementstmt 51.5% 118 / 229 Branchbran 47.0% 48 / 102 Conditioncond 58.1% 25 / 43 MC/DCmcdc 36.3% 8 / 22 Subroutinesub 55.8% 19 / 34 Podpod 0.0% 0 / 1 totaltotal 50.5% 218 / 431 CC 69 SCAR 39.5
CC
69
Cov
51%
_connect
176.7
__ANON__
42.0
__ANON__
42.0
CRAP 51.9
↑ Summary Wrapped.pm »
1 package IO::Socket::Socks::Wrapper;
2  
3 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 41 ▶no warnings 'prototype';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 41 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 41 ▶use Socket qw(:DEFAULT inet_ntop);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 41 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 41 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 41 our $VERSION = '0.16';
11 41 our @EXPORT_OK = ('connect', 'wrap_connection');
12  
13 # cache
14 # pkg -> ref to pkg::sub || undef(if pkg has no connect)
15 41 my %PKGS;
16  
17 # reference to &IO::Socket::connect
18 my $io_socket_connect;
19 sub _io_socket_connect_ref {
20 181 ▶ return $io_socket_connect if $io_socket_connect;
Subroutine _io_socket_connect_ref: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $io_socket_connect 159 22 181
21 22 $io_socket_connect = \&IO::Socket::connect;
22 }
23  
24 # fake handle to put under event loop while making socks handshake
25 sub _get_blocking_handles {
26 0 ▶ pipe(my $blocking_reader, my $blocking_writer)
Subroutine _get_blocking_handles: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
pipe my $blocking_reader, my $blocking_writer or die "pipe(): ", $! 0 0 0
27 or die 'pipe(): ', $!;
28
29 0 $blocking_writer->blocking(0);
30 0 $blocking_reader->blocking(0);
31 0 my $garbage = '\0' x 1024;
32
33 0 my ($writed, $total_writed);
34 0 ▶ while ($writed = syswrite($blocking_writer, $garbage)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writed = syswrite($blocking_writer, $garbage)) 0 0 0
35 0 $total_writed += $writed;
36
37 0 ▶ if ($total_writed > 2097152) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_writed > 2097152) 0 0 0
38 # socket with buffer more than 2 mb
39 # are u kidding me?
40 0 die "Can't create blocking handle";
41 }
42 }
43
44 0 return ($blocking_reader, $blocking_writer);
45 }
46  
47 sub _unblock_handles {
48 0 ▶ my ($blocking_reader, $blocking_writer) = @_;
Subroutine _unblock_handles: not called CC=3 SCAR=24.8
49
50 0 ▶ while (sysread($blocking_reader, my $buf, 4096)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread $blocking_reader, my $buf, 4096) 0 0 0
51 0 vec(my $win, fileno($blocking_writer), 1) = 1;
52 0 ▶ last if select(undef, $win, undef, 0);
Branch: 1 branch
BranchTrueFalseTotal
if select undef, $win, undef, 0 0 0 0
53 }
54 }
55  
56 sub import {
57 75 ▶ my $mypkg = shift;
Subroutine import: called CC=16 SCAR=27.8
58
59 75 ▶ if (@_ == 1 && !ref($_[0]) && $_[0] eq 'wrap_connection') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0] and $_[0] eq "wrap_connection") 0 75 75
Condition50%3/6
@_ == 1 and not ref $_[0] and $_[0] eq "wrap_connection"
!ll&&!rl&&r
7500
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
7140
MC/DC0%0/3
@_ == 1 and not ref $_[0] and $_[0] eq "wrap_connection"
@_ == 1 not ref $_[0] $_[0] eq "wrap_connection"
Truth table50%2/4
A and not(B) and C
A= @_ == 1B= not ref $_[0]C= $_[0] eq "wrap_connection"
ABCresult
0XX0
10X0
1100
1111
60 0 return __PACKAGE__->export_to_level(1, $mypkg, 'wrap_connection');
61 }
62
63 75 ▶ while (my ($pkg, $cfg) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 34 75 109
64 34 ▶ unless (defined $cfg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cfg) 4 30 34
65 4 $cfg = $pkg;
66 4 $pkg = undef;
67 }
68
69 34 ▶ if ($pkg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg) { } 30 4 34
70 41 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
71
72 30 my $sub;
73 30 ▶ if ($pkg =~ /^(.+)::([^:]+)\(\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg =~ /^(.+)::([^:]+)\(\)$/) 3 27 30
74 3 $pkg = $1;
75 3 $sub = $2;
76 }
77
78 # override in the package
79
80 # try to load if not already available
81 # and if not requested to do not load
82 30 ▶ unless(delete $cfg->{_norequire} || %{$pkg.'::'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (delete $cfg->{"_norequire"} or %{$pkg . '::';}) 1 29 30
Condition100%3/3
delete $cfg->{"_norequire"} or %{$pkg . '::';}
l!l&&r!l&&!r
2271
MC/DC100%2/2
delete $cfg->{"_norequire"} or %{$pkg . '::';}
delete $cfg->{"_norequire"} %{$pkg . '::';}
Truth table100%3/3
A or B
A= delete $cfg->{"_norequire"}B= %{$pkg . '::';}
ABresult
1X1
011
000
83 1 ▶ eval "require $pkg" # make @ISA available
Branch: 1 branch
BranchTrueFalseTotal
eval "require $pkg" or die $@ 0 1 1
84 or die $@;
85 }
86
87 30 ▶ if ($sub) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sub) { } 3 27 30
elsif ($pkg->isa('IO::Socket')) { } 24 3 27
88 # localize IO::Socket::connect overriding
89 # in the sub where IO::Socket::connect called
90 3 my $symbol = $pkg.'::'.$sub;
91 my $pkg_sub = exists $PKGS{$symbol} ?
92 $PKGS{$symbol} :
93 3 ▶ ($PKGS{$symbol} = \&$symbol);
Branch: 1 branch
BranchTrueFalseTotal
exists $PKGS{$symbol} ? : 1 2 3
94
95 3 _io_socket_connect_ref();
96
97 *$symbol = sub {
98 local *IO::Socket::IP::connect = local *IO::Socket::connect = sub {
99 7 ▶ _connect(@_, $cfg, 1);
Subroutine __ANON__: called CC=1 SCAR=0.0
100 4 ▶ };
Subroutine __ANON__: called CC=1 SCAR=0.0
101
102 4 $pkg_sub->(@_);
103 3 };
104 3 next;
105 }
106 elsif ($pkg->isa('IO::Socket')) {
107 # replace IO::Socket::connect
108 # if package inherits from IO::Socket
109 # save replaceable package version of the connect
110 # if it has own
111 # will call it from the our new connect
112 24 my $symbol = $pkg.'::connect';
113 my $pkg_connect = exists $PKGS{$pkg} ?
114 $PKGS{$pkg} :
115 24 ▶ ($PKGS{$pkg} = eval{ *{$symbol}{CODE} } ? \&$symbol : undef);
Branch: 2 branches
BranchTrueFalseTotal
exists $PKGS{$pkg} ? : 1 23 24
eval { do { *{$symbol;}{'CODE'} } } ? : 3 20 23
116
117 *connect = sub {
118 51 ▶ _io_socket_connect_ref();
Subroutine __ANON__: called CC=5 SCAR=16.1
119
120 local *IO::Socket::IP::connect = local *IO::Socket::connect = sub {
121 100 ▶ _connect(@_, $cfg, 1);
Subroutine __ANON__: called CC=1 SCAR=0.0
122 51 };
123
124 51 my $self = shift;
125
126 51 ▶ if ($pkg_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg_connect) { } 12 39 51
127 # package has its own connect
128 12 $pkg_connect->($self, @_);
129 }
130 else {
131 # get first parent which has connect sub
132 # and call it
133 39 my $ref = ref($self);
134
135 39 foreach my $parent (@{$pkg.'::ISA'}) {
136 76 ▶ if($parent->isa('IO::Socket')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa("IO::Socket")) 39 37 76
137 39 bless $self, $parent;
138 39 my $connected = $self->connect(@_);
139 39 bless $self, $ref;
140 39 ▶ return $connected ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$connected ? : 20 19 39
141 }
142 }
143 }
144 }
145 24 }
146 else {
147 # replace package version of connect
148 *connect = sub {
149 4 ▶ _connect(@_, $cfg);
Subroutine __ANON__: called CC=1 SCAR=0.0
150 }
151 3 }
152
153 27 $mypkg->export($pkg, 'connect');
154 }
155 else {
156 # override connect() globally
157 *connect = sub(*$) {
158 21 ▶ my $socket = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
159 21 ▶ unless (ref $socket) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $socket) 2 19 21
160 # old-style bareword used
161 41 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
162 2 my $caller = caller;
163 2 $socket = $caller . '::' . $socket;
164 2 $socket = \*{$socket};
165 }
166
167 21 _connect($socket, @_, $cfg);
168 4 };
169
170 4 $mypkg->export('CORE::GLOBAL', 'connect');
171 }
172 }
173 }
174  
175 sub wrap_connection {
176 15 ▶ require IO::Socket::Socks::Wrapped;
Subroutine wrap_connection: called CC=1 SCAR=0.0
Pod undocumented
177 15 return IO::Socket::Socks::Wrapped->new(@_);
178 }
179  
180 sub _connect {
181 204 ▶ my ($socket, $name, $cfg, $io_socket) = @_;
Subroutine _connect: called CC=24 SCAR=51.7
182
183 204 my $ref = ref($socket);
184 204 my $connected;
185
186 204 ▶ if ($socket->isa('IO::Socket::Socks') || !$cfg || ( $connected = defined getpeername($socket) )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket->isa("IO::Socket::Socks") or not $cfg or $connected = defined getpeername $socket) 3 101 104
Condition83%5/6
$socket->isa("IO::Socket::Socks") or not $cfg or $connected = defined getpeername $socket
l!l&&r!l&&!r
1030101
$socket->isa("IO::Socket::Socks") or not $cfg
l!l&&r!l&&!r
1003101
MC/DC66%2/3
$socket->isa("IO::Socket::Socks") or not $cfg or $connected = defined getpeername $socket
$socket->isa("IO::Socket::Socks") not $cfg $connected = defined getpeername $socket
Truth table75%3/4
A or not(B) or C
A= $socket->isa("IO::Socket::Socks")B= not $cfgC= $connected = defined getpeername $socket
ABCresult
1XX1
01X1
0011
0000
187 103 ▶ unless (!$connected && $io_socket and ${*$socket}{'io_socket_timeout'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $connected and $io_socket and do { *$socket }->{"io_socket_timeout"}) 12 91 103
Condition66%4/6
not $connected and $io_socket and do { *$socket }->{"io_socket_timeout"}
!ll&&!rl&&r
12091
not $connected and $io_socket
!ll&&!rl&&r
01291
MC/DC33%1/3
not $connected and $io_socket and do { *$socket }->{"io_socket_timeout"}
not $connected $io_socket do { *$socket }->{"io_socket_timeout"}
Truth table50%2/4
not(A) and B and C
A= not $connectedB= $io_socketC= do { *$socket }->{"io_socket_timeout"}
ABCresult
0XX0
10X0
1100
1111
188 12 return CORE::connect( $socket, $name );
189 }
190
191 # use IO::Socket::connect for timeout support
192 91 ▶ local *connect = sub { CORE::connect($_[0], $_[1]) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
193 91 return _io_socket_connect_ref->( $socket, $name );
194 }
195
196 101 my ($port, $host);
197 101 ▶ if (($port, $host) = eval { unpack_sockaddr_in($name) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = eval { do { unpack_sockaddr_in($name) } }) { } 101 0 101
198 101 $host = inet_ntoa($host);
199 }
200 else {
201 0 ($port, $host) = unpack_sockaddr_in6($name);
202 0 $host = inet_ntop(AF_INET6, $host);
203 }
204
205 # global overriding will not work with `use'
206 101 require IO::Socket::Socks;
207 101 my $io_handler = $cfg->{_io_handler};
208
209 101 ▶ unless ($io_handler || exists $cfg->{Timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($io_handler or exists $cfg->{"Timeout"}) 40 61 101
Condition66%2/3
$io_handler or exists $cfg->{"Timeout"}
l!l&&r!l&&!r
06140
MC/DC50%1/2
$io_handler or exists $cfg->{"Timeout"}
$io_handler exists $cfg->{"Timeout"}
Truth table66%2/3
A or B
A= $io_handlerB= exists $cfg->{"Timeout"}
ABresult
1X1
011
000
210 40 ▶ $cfg->{Timeout} = $ref && $socket->isa('IO::Socket') && ${*$socket}{'io_socket_timeout'} || 180;
Condition87%7/8
$ref && $socket->isa('IO::Socket') && do { *$socket }->{'io_socket_timeout'} || 180
l!l
346
$ref and $socket->isa("IO::Socket") and do { *$socket }->{"io_socket_timeout"}
!ll&&!rl&&r
1534
$ref and $socket->isa("IO::Socket")
!ll&&!rl&&r
0139
MC/DC66%2/3
$ref && $socket->isa('IO::Socket') && do { *$socket }->{'io_socket_timeout'} || 180
$ref $socket->isa("IO::Socket") do { *$socket }->{"io_socket_timeout"}
Truth table75%3/4
A and B and C
A= $refB= $socket->isa("IO::Socket")C= do { *$socket }->{"io_socket_timeout"}
ABCresult
1111
0XX0
10X0
1100
211 }
212
213 101 my $need_nb;
214
215 101 ▶ if ($io_handler) {
Branch: 2 branches
BranchTrueFalseTotal
if ($io_handler) { } 0 101 101
elsif (not $socket->blocking) { } 5 96 101
216 0 $io_handler = $io_handler->();
217 0 require POSIX;
218
219
220 0 my $fd = fileno($socket);
221 0 ▶ my $tmp_fd = POSIX::dup($fd) // die 'dup(): ', $!;
Condition0%0/2
POSIX::dup($fd) // die("dup(): ", $!)
l!l
00
MC/DC0%0/1
POSIX::dup($fd) // die("dup(): ", $!)
POSIX::dup($fd)
Truth table0%0/2
A
A= POSIX::dup($fd)
Aresult
11
00
222 0 ▶ open my $tmp_socket, '+<&=' . $tmp_fd or die 'open(): ', $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $tmp_socket, '+<&=' . $tmp_fd or die "open(): ", $! 0 0 0
223
224 0 my ($blocking_reader, $blocking_writer) = _get_blocking_handles();
225 0 ▶ POSIX::dup2(fileno($blocking_writer), $fd) // die 'dup2(): ', $!;
Condition0%0/2
POSIX::dup2(fileno $blocking_writer, $fd) // die("dup2(): ", $!)
l!l
00
MC/DC0%0/1
POSIX::dup2(fileno $blocking_writer, $fd) // die("dup2(): ", $!)
POSIX::dup2(fileno $blocking_writer, $fd)
Truth table0%0/2
A
A= POSIX::dup2(fileno $blocking_writer, $fd)
Aresult
11
00
226
227 0 $io_handler->{blocking_reader} = $blocking_reader;
228 0 $io_handler->{blocking_writer} = $blocking_writer;
229 0 $io_handler->{orig_socket} = $socket;
230 0 Scalar::Util::weaken($io_handler->{orig_socket});
231 0 $socket = $tmp_socket;
232 }
233 elsif (!$socket->blocking) {
234 # without io handler non-blocking connection will not be success
235 # so set socket to blocking mode while making socks handshake
236 5 $socket->blocking(1);
237 5 $need_nb = 1;
238 }
239
240 101 my $ok = IO::Socket::Socks->new_from_socket(
241 $socket,
242 ConnectAddr => $host,
243 ConnectPort => $port,
244 %$cfg
245 );
246
247 101 ▶ if ($need_nb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_nb) 5 96 101
248 5 $socket->blocking(0);
249 }
250
251 101 ▶ return unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 46 55 101
252 55 ▶ bless $socket, $ref if $ref && !$io_handler; # XXX: should we unbless for GLOB?
Branch: 1 branch
BranchTrueFalseTotal
if $ref and not $io_handler 55 0 55
Condition33%1/3
$ref and not $io_handler
!ll&&!rl&&r
0055
MC/DC0%0/2
$ref and not $io_handler
$ref not $io_handler
Truth table33%1/3
A and not(B)
A= $refB= not $io_handler
ABresult
0X0
100
111
253
254 55 ▶ if ($io_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io_handler) 0 55 55
255 0 my ($r_cb, $w_cb);
256 0 my $done;
257
258 0 tie *{$io_handler->{orig_socket}}, 'IO::Socket::Socks::Wrapper::Handle', $io_handler->{orig_socket}, sub {
259 0 ▶ unless ($done) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 0 0 0
260 0 $io_handler->{unset_read_watcher}->($socket);
261 0 $io_handler->{unset_write_watcher}->($socket);
262
263 0 ▶ if ($io_handler->{destroy_io_watcher}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io_handler->{"destroy_io_watcher"}) 0 0 0
264 0 $io_handler->{destroy_io_watcher}->($socket);
265 }
266
267 0 close $socket;
268 }
269
270 # clean circular references
271 0 undef $r_cb;
272 0 undef $w_cb;
273 0 };
274
275 my $on_finish = sub {
276 0 ▶ tied(*{$io_handler->{orig_socket}})->handshake_done($done = 1);
Subroutine __ANON__: not called CC=2 SCAR=17.9
277 0 ▶ POSIX::dup2(fileno($socket), fileno($io_handler->{orig_socket})) // die 'dup2(): ', $!;
Condition0%0/2
POSIX::dup2(fileno $socket, fileno $io_handler->{"orig_socket"}) // die("dup2(): ", $!)
l!l
00
MC/DC0%0/1
POSIX::dup2(fileno $socket, fileno $io_handler->{"orig_socket"}) // die("dup2(): ", $!)
POSIX::dup2(fileno $socket, fileno $io_handler->{"orig_socket"})
Truth table0%0/2
A
A= POSIX::dup2(fileno $socket, fileno $io_handler->{"orig_socket"})
Aresult
11
00
278 0 close $socket;
279 0 _unblock_handles($io_handler->{blocking_reader}, $io_handler->{blocking_writer});
280 0 };
281
282 my $on_error = sub {
283 0 ▶ tied(*{$io_handler->{orig_socket}})->handshake_done($done = 1);
Subroutine __ANON__: not called CC=2 SCAR=17.9
284 0 shutdown($socket, 0);
285 0 ▶ POSIX::dup2(fileno($socket), fileno($io_handler->{orig_socket})) // die 'dup2(): ', $!;
Condition0%0/2
POSIX::dup2(fileno $socket, fileno $io_handler->{"orig_socket"}) // die("dup2(): ", $!)
l!l
00
MC/DC0%0/1
POSIX::dup2(fileno $socket, fileno $io_handler->{"orig_socket"}) // die("dup2(): ", $!)
POSIX::dup2(fileno $socket, fileno $io_handler->{"orig_socket"})
Truth table0%0/2
A
A= POSIX::dup2(fileno $socket, fileno $io_handler->{"orig_socket"})
Aresult
11
00
286 0 close $socket;
287 0 };
288
289 $r_cb = sub {
290 0 ▶ if ($socket->ready) {
Subroutine __ANON__: not called CC=6 SCAR=37.4
Branch: 3 branches
BranchTrueFalseTotal
if ($socket->ready) { } 0 0 0
elsif ($IO::Socket::Socks::SOCKS_ERROR == &IO::Socket::Socks::SOCKS_WANT_WRITE) { } 0 0 0
elsif ($IO::Socket::Socks::SOCKS_ERROR != &IO::Socket::Socks::SOCKS_WANT_READ) { } 0 0 0
291 0 $io_handler->{unset_read_watcher}->($socket);
292
293 0 ▶ if ($io_handler->{destroy_io_watcher}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io_handler->{"destroy_io_watcher"}) 0 0 0
294 0 $io_handler->{destroy_io_watcher}->($socket);
295 }
296
297 0 $on_finish->();
298 }
299 elsif ($IO::Socket::Socks::SOCKS_ERROR == &IO::Socket::Socks::SOCKS_WANT_WRITE) {
300 0 $io_handler->{unset_read_watcher}->($socket);
301 0 $io_handler->{set_write_watcher}->($socket, $w_cb);
302 }
303 elsif ($IO::Socket::Socks::SOCKS_ERROR != &IO::Socket::Socks::SOCKS_WANT_READ) {
304 0 $io_handler->{unset_read_watcher}->($socket);
305
306 0 ▶ if ($io_handler->{destroy_io_watcher}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io_handler->{"destroy_io_watcher"}) 0 0 0
307 0 $io_handler->{destroy_io_watcher}->($socket);
308 }
309
310 0 $on_error->();
311 }
312 0 };
313
314 $w_cb = sub {
315 0 ▶ if ($socket->ready) {
Subroutine __ANON__: not called CC=6 SCAR=37.4
Branch: 3 branches
BranchTrueFalseTotal
if ($socket->ready) { } 0 0 0
elsif ($IO::Socket::Socks::SOCKS_ERROR == &IO::Socket::Socks::SOCKS_WANT_READ) { } 0 0 0
elsif ($IO::Socket::Socks::SOCKS_ERROR != &IO::Socket::Socks::SOCKS_WANT_WRITE) { } 0 0 0
316 0 $io_handler->{unset_write_watcher}->($socket);
317
318 0 ▶ if ($io_handler->{destroy_io_watcher}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io_handler->{"destroy_io_watcher"}) 0 0 0
319 0 $io_handler->{destroy_io_watcher}->($socket);
320 }
321
322 0 $on_finish->();
323 }
324 elsif ($IO::Socket::Socks::SOCKS_ERROR == &IO::Socket::Socks::SOCKS_WANT_READ) {
325 0 $io_handler->{unset_write_watcher}->($socket);
326 0 $io_handler->{set_read_watcher}->($socket, $r_cb);
327 }
328 elsif ($IO::Socket::Socks::SOCKS_ERROR != &IO::Socket::Socks::SOCKS_WANT_WRITE) {
329 0 $io_handler->{unset_write_watcher}->($socket);
330
331 0 ▶ if ($io_handler->{destroy_io_watcher}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io_handler->{"destroy_io_watcher"}) 0 0 0
332 0 $io_handler->{destroy_io_watcher}->($socket);
333 }
334
335 0 $on_error->();
336 }
337 0 };
338
339 0 ▶ if ($io_handler->{init_io_watcher}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io_handler->{"init_io_watcher"}) 0 0 0
340 0 $io_handler->{init_io_watcher}->($socket, $r_cb, $w_cb);
341 }
342
343 0 $io_handler->{set_write_watcher}->($socket, $w_cb);
344
345 0 $! = Errno::EINPROGRESS;
346 0 return 0;
347 }
348
349 55 return 1;
350 }
351  
352 package IO::Socket::Socks::Wrapper::Handle;
353  
354 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
355  
356 sub TIEHANDLE {
357 0 ▶ my ($class, $orig_handle, $cleanup_cb) = @_;
Subroutine TIEHANDLE: not called CC=2 SCAR=17.9
358
359 0 ▶ open my $self, '+<&=' . fileno($orig_handle)
Branch: 1 branch
BranchTrueFalseTotal
open my $self, '+<&=' . fileno($orig_handle) or die "open: ", $! 0 0 0
360 or die 'open: ', $!;
361
362 0 ${*$self}{handshake_done} = 0;
363 0 ${*$self}{cleanup_cb} = $cleanup_cb;
364
365 0 bless $self, $class;
366 }
367  
368 sub handshake_done {
369 0 ▶ my $self = shift;
Subroutine handshake_done: not called CC=2 SCAR=17.9
370
371 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
372 0 ${*$self}{handshake_done} = $_[0];
373 }
374
375 0 return ${*$self}{handshake_done};
376 }
377  
378 sub READ {
379 0 ▶ my $self = shift;
Subroutine READ: not called CC=2 SCAR=17.9
380 0 ▶ sysread($self, $_[0], $_[1], @_ > 2 ? $_[2] : ());
Branch: 1 branch
BranchTrueFalseTotal
@_ > 2 ? : 0 0 0
381 }
382  
383 sub WRITE {
384 0 ▶ my $self = shift;
Subroutine WRITE: not called CC=2 SCAR=17.9
385 0 ▶ syswrite($self, $_[0], $_[1], @_ > 2 ? $_[2] : ());
Branch: 1 branch
BranchTrueFalseTotal
@_ > 2 ? : 0 0 0
386 }
387  
388 sub FILENO {
389 0 ▶ my $self = shift;
Subroutine FILENO: not called CC=1 SCAR=6.9
390 0 fileno($self);
391 }
392  
393 sub CLOSE {
394 0 ▶ my $self = shift;
Subroutine CLOSE: not called CC=2 SCAR=17.9
395
396 0 ▶ unless ($self->handshake_done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->handshake_done) 0 0 0
397 0 $self->handshake_done(1);
398 0 ${*$self}{cleanup_cb}->();
399 }
400
401 0 close $self;
402 }
403  
404 sub DESTROY {
405 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=1 SCAR=6.9
406 0 ${*$self}{cleanup_cb}->();
407 }
408  
409 41 1;
410  
411 __END__
↑ Summary Wrapped.pm »