| 1 |
#BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net) |
| 2 |
#BSD All rights reserved. |
| 3 |
#BSD |
| 4 |
#BSD Redistribution and use in source and binary forms, with or without |
| 5 |
#BSD modification, are permitted provided that the following conditions |
| 6 |
#BSD are met: |
| 7 |
#BSD |
| 8 |
#BSD - Redistributions of source code must retain the above copyright |
| 9 |
#BSD notice, this list of conditions and the following disclaimer. |
| 10 |
#BSD |
| 11 |
#BSD - Redistributions in binary form must reproduce the above copyright |
| 12 |
#BSD notice, this list of conditions and the following disclaimer in the |
| 13 |
#BSD documentation and/or other materials provided with the distribution. |
| 14 |
#BSD |
| 15 |
#BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 |
#BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 |
#BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 |
#BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 |
#BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 |
#BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 |
#BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 |
#BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 |
#BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 |
#BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
#BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
#BSD POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
@_stream_FUNCS = ( |
| 29 |
[ 'open', 'close', 'read', 'write', 'writedone', 'valid' ], # stream_NULL |
| 30 |
[ 'socket', 'all', 'socket', 'socket', 'noop', 'socket' ] |
| 31 |
, # stream_SOCKTCP 1 |
| 32 |
[ 'socket', 'all', 'socket', 'socket', 'noop', 'never' ] |
| 33 |
, # stream_SOCKUDP 2 |
| 34 |
[ 'file', 'all', 'socket', 'file', 'noop', 'never' ], # stream_FILE 3 |
| 35 |
[ 'ssl', 'all', 'ssl', 'ssl', 'noop', 'netssleay' ], # stream_NETSSLEAY 4 |
| 36 |
[ 'ssl', 'all', 'ssl', 'ssl', 'noop', 'never' ], # stream_NETSSL 5 |
| 37 |
[ 'buffer', 'buffer', 'buffer', 'buffer', 'noop', |
| 38 |
'never' ] # stream_BUFFER 6 |
| 39 |
); |
| 40 |
|
| 41 |
sub stream_key { |
| 42 |
my ( $key, $type, $wh ) = ( '', 1, shift ); |
| 43 |
|
| 44 |
if ( defined $wh->{whisker}->{UDP} && $wh->{whisker}->{UDP} > 0 ) { |
| 45 |
$type = 2; |
| 46 |
$key = 'udp:'; |
| 47 |
} |
| 48 |
|
| 49 |
if ( $wh->{whisker}->{ssl} > 0 ) { |
| 50 |
$type = 4 if ( $LW_SSL_LIB == 1 ); |
| 51 |
$type = 5 if ( $LW_SSL_LIB == 2 ); |
| 52 |
$key = 'ssl:'; |
| 53 |
} |
| 54 |
|
| 55 |
if ( defined $wh->{whisker}->{file_stream} ) { |
| 56 |
$type = 3; |
| 57 |
$key = 'file=' . $wh->{whisker}->{file_stream} . ':'; |
| 58 |
} |
| 59 |
|
| 60 |
if ( defined $wh->{whisker}->{buffer_stream} ) { |
| 61 |
$type = 6; |
| 62 |
$key = 'buffer:'; |
| 63 |
} |
| 64 |
|
| 65 |
my ( $x, $h, $p ) = (0); |
| 66 |
if ( defined $wh->{whisker}->{proxy_host} ) { |
| 67 |
$h = $wh->{whisker}->{proxy_host}; |
| 68 |
$p = $wh->{whisker}->{proxy_port} || 80; |
| 69 |
$x++; |
| 70 |
$key .= 'proxy:'; |
| 71 |
if ( $type == 5 ) { |
| 72 |
$x = 0; |
| 73 |
$ENV{HTTPS_PROXY} = "$h:$p"; |
| 74 |
$h = $wh->{whisker}->{host}; |
| 75 |
$p = $wh->{whisker}->{port}; |
| 76 |
} |
| 77 |
} |
| 78 |
else { |
| 79 |
$h = $wh->{whisker}->{host}; |
| 80 |
$p = $wh->{whisker}->{port}; |
| 81 |
} |
| 82 |
|
| 83 |
$key .= $h . ':' . $p; |
| 84 |
if ( defined $wh->{whisker}->{stream_num} ) { |
| 85 |
$key .= '/' . $wh->{whisker}->{stream_num}; |
| 86 |
} |
| 87 |
|
| 88 |
return $key if ( !wantarray() ); |
| 89 |
return ( $type, $h, $p, $x, $key ); |
| 90 |
} |
| 91 |
|
| 92 |
sub stream_setsock { |
| 93 |
my $fd = shift; |
| 94 |
my $wh = http_new_request( host => 'localhost', port => 80, ssl => 0 ); |
| 95 |
my $xr = stream_new($wh); |
| 96 |
return undef if ( $xr->{streamtype} != 1 ); |
| 97 |
$xr->{sock} = $fd; |
| 98 |
$xr->{state} = 1; |
| 99 |
$xr->{eof} = 0; |
| 100 |
$xr->{clearall}->(); |
| 101 |
return $xr; |
| 102 |
} |
| 103 |
|
| 104 |
{ |
| 105 |
$SYMCOUNT = 0; |
| 106 |
|
| 107 |
sub stream_new { |
| 108 |
my ( $c, $rh ) = ( 0, shift ); |
| 109 |
my $sock = _stream_newsock(); |
| 110 |
my %x; |
| 111 |
%x = ( |
| 112 |
bufin => '', |
| 113 |
bufout => '', |
| 114 |
error => '', |
| 115 |
streamtype => 0, |
| 116 |
eof => 0, |
| 117 |
ctx => undef, |
| 118 |
sock => $sock, |
| 119 |
state => 0, |
| 120 |
syns => 0, |
| 121 |
reqs => 0, |
| 122 |
timeout => $rh->{whisker}->{timeout} || 10, |
| 123 |
nonblock => 0, |
| 124 |
forceclose => 0 |
| 125 |
); |
| 126 |
|
| 127 |
( $x{streamtype}, $x{chost}, $x{cport}, $x{proxy}, $x{key} ) = |
| 128 |
stream_key($rh); |
| 129 |
return undef if ( $x{streamtype} == 0 ); |
| 130 |
return undef |
| 131 |
if ( |
| 132 |
$LW_SSL_LIB == 0 |
| 133 |
&& ( $x{streamtype} == 4 |
| 134 |
|| $x{streamtype} == 5 ) |
| 135 |
); |
| 136 |
return undef |
| 137 |
if ( $x{streamtype} != 3 |
| 138 |
&& $x{streamtype} != 6 |
| 139 |
&& !defined $Socket::VERSION ); |
| 140 |
|
| 141 |
$x{nonblock} = $LW_NONBLOCK_CONNECT if ( $x{streamtype} == 1 ); |
| 142 |
$x{forceclose} = 1 if ( $x{streamtype} == 5 ); |
| 143 |
|
| 144 |
$x{slurp} = $rh->{whisker}->{trailing_slurp} || 0; |
| 145 |
|
| 146 |
my @N = @{ $_stream_FUNCS[ $x{streamtype} ] }; |
| 147 |
for ( $c = 0 ; $c < 6 ; $c++ ) { |
| 148 |
my $n = $_stream_FUNCS[0]->[$c]; |
| 149 |
my $e = |
| 150 |
'$x{"' . $n . '"}=sub{&_stream_' . $N[$c] . "_$n" . '(\%x,@_)}'; |
| 151 |
eval "$e"; |
| 152 |
} |
| 153 |
$x{queue} = sub { $x{bufout} .= shift }; |
| 154 |
$x{clearall} = sub { $x{bufin} = $x{bufout} = '' }; |
| 155 |
$x{clear} = sub { $x{bufout} = '' }; |
| 156 |
return bless \%x, 'LW2::stream'; |
| 157 |
} |
| 158 |
|
| 159 |
sub _stream_newsock { # same as Symbol::gensym |
| 160 |
my $pkg = "LW2::"; |
| 161 |
my $name = "_STREAM_" . $SYMCOUNT++; |
| 162 |
delete $$pkg{$name}; |
| 163 |
return \*{ $pkg . $name }; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
sub _stream_all_close { |
| 168 |
my $xr = shift; |
| 169 |
$xr->{state} = 0; |
| 170 |
if ( $xr->{streamtype} == 4 ) { |
| 171 |
eval { $xr->{sock}->shutdown() }; |
| 172 |
eval { close( $xr->{origsock} ) }; |
| 173 |
|
| 174 |
# eval { Net::SSLeay::free($xr->{sock}) }; |
| 175 |
} |
| 176 |
else { |
| 177 |
eval { close( $xr->{sock} ) }; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
sub _stream_never_valid { |
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
sub __bad_netssleay_error { |
| 186 |
my $err = Net::SSLeay::ERR_get_error; |
| 187 |
return 0 |
| 188 |
if ( $err == Net::SSLeay::ERROR_NONE |
| 189 |
|| $err == Net::SSLeay::ERROR_WANT_READ |
| 190 |
|| $err == Net::SSLeay::ERROR_WANT_WRITE ); |
| 191 |
return 1; |
| 192 |
} |
| 193 |
|
| 194 |
sub _stream_netssleay_valid { |
| 195 |
my $xr = shift; |
| 196 |
return 0 if ( $LW_SSL_KEEPALIVE == 0 || $xr->{state} == 0 ); |
| 197 |
return 0 if ( &Net::SSLeay::OPENSSL_VERSION_NUMBER < 0x0090601f ); |
| 198 |
|
| 199 |
my $lo = Net::SSLeay::pending( $xr->{sock} ); |
| 200 |
if ( $lo > 0 ) { # leftover data to slurp |
| 201 |
if ( !$xr->{slurp} ) { |
| 202 |
return 0 if ( !_stream_ssl_read($xr) ); |
| 203 |
} |
| 204 |
else { |
| 205 |
|
| 206 |
# todo |
| 207 |
#$xr->{slurped}.=$x."\0"; |
| 208 |
} |
| 209 |
} |
| 210 |
return 0 if ( __bad_netssleay_error() ); |
| 211 |
|
| 212 |
my ( $r, $e, $vin ) = ( undef, undef, '' ); |
| 213 |
my $fno = fileno( $xr->{origsock} ); |
| 214 |
vec( $vin, $fno, 1 ) = 1; |
| 215 |
if ( select( ( $r = $vin ), undef, ( $e = $vin ), .0001 ) ) { |
| 216 |
return 0 if ( vec( $e, $fno, 1 ) ); |
| 217 |
if ( vec( $r, $fno, 1 ) ) { # waiting data, let's peek |
| 218 |
my $temp = Net::SSLeay::peek( $xr->{sock}, 1 ); |
| 219 |
return 0 if ( __bad_netssleay_error() ); |
| 220 |
return 0 if ( $temp <= 0 ); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
return 1; |
| 225 |
} |
| 226 |
|
| 227 |
sub _stream_socket_valid { |
| 228 |
my $xr = shift; |
| 229 |
return 0 if ( $xr->{state} == 0 ); |
| 230 |
my ( $o, $vin ) = ( undef, '' ); |
| 231 |
vec( $vin, fileno( $xr->{sock} ), 1 ) = 1; |
| 232 |
if ( select( ( $o = $vin ), undef, undef, .0001 ) ) { |
| 233 |
my ( $hold, $res ); |
| 234 |
do { |
| 235 |
$res = sysread( $xr->{sock}, $hold, 4096 ); |
| 236 |
return _stream_err( $xr, 1, 'is_valid sysread failed' ) |
| 237 |
if ( !defined $res ); # error |
| 238 |
return 0 if ( $res == 0 ); # EOF |
| 239 |
if ( !$xr->{slurp} ) { |
| 240 |
$xr->{bufin} .= $hold; |
| 241 |
} |
| 242 |
else { |
| 243 |
$xr->{slurped} .= $hold . "\0"; |
| 244 |
} |
| 245 |
} while ( $res && select( ( $o = $vin ), undef, undef, .0001 ) ); |
| 246 |
} |
| 247 |
return 1; |
| 248 |
} |
| 249 |
|
| 250 |
sub _stream_socket_read { |
| 251 |
my $xr = shift; |
| 252 |
return 0 if ( $xr->{state} == 0 ); |
| 253 |
my ( $vin, $t ) = ( '', '' ); |
| 254 |
vec( $vin, fileno( $xr->{sock} ), 1 ) = 1; |
| 255 |
return 0 if ( !select( $vin, undef, undef, $xr->{timeout} ) ); |
| 256 |
my $res = sysread( $xr->{sock}, $t, 4096 ); |
| 257 |
return _stream_err( $xr, 1, 'sysread failed' ) if ( !defined $res ); |
| 258 |
if ( $res == 0 ) { |
| 259 |
$xr->{eof} = 1; |
| 260 |
return 0; |
| 261 |
} |
| 262 |
$xr->{bufin} .= $t; |
| 263 |
$xr->{eof} = 0; |
| 264 |
return 1; |
| 265 |
} |
| 266 |
|
| 267 |
sub _stream_ssl_read { |
| 268 |
my ( $xr, $t ) = ( shift, '' ); |
| 269 |
return 0 if ( $xr->{state} == 0 ); |
| 270 |
if ( $xr->{streamtype} == 4 ) { |
| 271 |
local $SIG{ALRM} = sub { die "lw_timeout\n" }; |
| 272 |
local $SIG{PIPE} = sub { die "lw_pipe\n" }; |
| 273 |
eval { |
| 274 |
eval { alarm( $xr->{timeout} ) }; |
| 275 |
|
| 276 |
# sleep(1) while(!Net::SSLeay::pending($xr->{sock})); |
| 277 |
$t = Net::SSLeay::read( $xr->{sock} ); |
| 278 |
eval { alarm(0) }; |
| 279 |
}; |
| 280 |
return 0 if ( $@ || __bad_netssleay_error() || !defined $t || $t eq '' ); |
| 281 |
} |
| 282 |
elsif ( $xr->{streamtype} == 5 ) { |
| 283 |
return 0 if ( !$xr->{sock}->read( $t, 4096 ) ); |
| 284 |
} |
| 285 |
$xr->{bufin} .= $t; |
| 286 |
return 1; |
| 287 |
} |
| 288 |
|
| 289 |
sub _stream_noop_writedone { } |
| 290 |
|
| 291 |
sub _stream_ssl_writedone { |
| 292 |
my $xr = shift; |
| 293 |
if ( $xr->{streamtype} == 4 ) { # Net::SSLeay |
| 294 |
shutdown $xr->{origsock}, 1; |
| 295 |
} |
| 296 |
else { # Net::SSL |
| 297 |
#shutdown $xr->{sock}, 1; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
sub _stream_socket_write { |
| 302 |
my ( $xr, $data, $v, $wrote ) = ( shift, shift, '', 0 ); |
| 303 |
return 0 if ( $xr->{state} == 0 ); |
| 304 |
$xr->{bufout} .= $data if ( defined $data ); |
| 305 |
my $len = length( $xr->{bufout} ); |
| 306 |
return 1 if ( $len == 0 ); |
| 307 |
vec( $v, fileno( $xr->{sock} ), 1 ) = 1; |
| 308 |
return _stream_err( $xr, 1, 'stream write test failed' ) |
| 309 |
if ( !select( undef, $v, undef, .0001 ) ); |
| 310 |
my $piperr = 0; |
| 311 |
local $SIG{PIPE} = sub { $piperr++ }; |
| 312 |
|
| 313 |
# $wrote=syswrite($xr->{sock},$xr->{bufout},$len); |
| 314 |
# return _stream_err($xr,1,'syswrite failed') |
| 315 |
# if(!defined $wrote || $piperr); |
| 316 |
# $xr->{error} = 'could not send entire queue' && return 0 |
| 317 |
# if($wrote!=$len); |
| 318 |
# $xr->{bufout}=''; |
| 319 |
# return 1; |
| 320 |
|
| 321 |
do { |
| 322 |
$wrote = syswrite( $xr->{sock}, $xr->{bufout}, $len ); |
| 323 |
if ( defined $wrote ) { |
| 324 |
substr( $xr->{bufout}, 0, $wrote ) = ''; |
| 325 |
} |
| 326 |
else { |
| 327 |
if ( $! != EWOULDBLOCK ) { |
| 328 |
$piperr++; |
| 329 |
} |
| 330 |
else { |
| 331 |
vec( $v, fileno( $xr->{sock} ), 1 ) = 1; |
| 332 |
$piperr++ if ( !select( undef, $v, undef, $xr->{timeout} ) ); |
| 333 |
} |
| 334 |
} |
| 335 |
return _stream_err( $xr, 1, 'syswrite failed' ) if ($piperr); |
| 336 |
$len = length( $xr->{bufout} ); |
| 337 |
} while ( $len > 0 ); |
| 338 |
return 1; |
| 339 |
} |
| 340 |
|
| 341 |
sub _stream_ssl_write { |
| 342 |
my ( $xr, $data, $wrote, $err ) = ( shift, shift, 0, '' ); |
| 343 |
return 0 if ( $xr->{state} == 0 ); |
| 344 |
$xr->{bufout} .= $data if ( defined $data ); |
| 345 |
my $len = length( $xr->{bufout} ); |
| 346 |
return 1 if ( $len == 0 ); |
| 347 |
if ( $xr->{streamtype} == 4 ) { |
| 348 |
( $wrote, $err ) = |
| 349 |
Net::SSLeay::ssl_write_all( $xr->{sock}, \$xr->{bufout} ); |
| 350 |
if ( __bad_netssleay_error() || !$wrote ) { |
| 351 |
$xr->{error} = "SSL error: $err"; |
| 352 |
return 0; |
| 353 |
} |
| 354 |
if ( $wrote != $len ) { |
| 355 |
$xr->{error} = 'could not send entire queue'; |
| 356 |
return 0; |
| 357 |
} |
| 358 |
} |
| 359 |
elsif ( $xr->{streamtype} == 5 ) { |
| 360 |
$xr->{sock}->print( $xr->{bufout} ); |
| 361 |
|
| 362 |
# bummer, no error checking? |
| 363 |
} |
| 364 |
$xr->{bufout} = ''; |
| 365 |
return 1; |
| 366 |
} |
| 367 |
|
| 368 |
sub _stream_socket_alloc { |
| 369 |
my ( $xr, $wh ) = @_; |
| 370 |
|
| 371 |
if ( $xr->{streamtype} == 2 ) { |
| 372 |
return _stream_err( $xr, 0, 'socket problems (UDP)' ) |
| 373 |
if ( |
| 374 |
!socket( |
| 375 |
$xr->{sock}, PF_INET, |
| 376 |
SOCK_DGRAM, getprotobyname('udp') || 0 |
| 377 |
) |
| 378 |
); |
| 379 |
} |
| 380 |
else { |
| 381 |
return _stream_err( $xr, 0, 'socket() problems' ) |
| 382 |
if ( |
| 383 |
!socket( |
| 384 |
$xr->{sock}, PF_INET, |
| 385 |
SOCK_STREAM, getprotobyname('tcp') || 0 |
| 386 |
) |
| 387 |
); |
| 388 |
} |
| 389 |
|
| 390 |
if ( defined $wh->{whisker}->{bind_socket} ) { |
| 391 |
my $p = $wh->{whisker}->{bind_port} || '*'; |
| 392 |
$p =~ tr/0-9*//cd; |
| 393 |
return _stream_err( $xr, 0, 'Bad bind_port value' ) |
| 394 |
if ( $p eq '' ); |
| 395 |
my $a = INADDR_ANY; |
| 396 |
$a = inet_aton( $wh->{whisker}->{bind_addr} ) |
| 397 |
if ( defined $wh->{whisker}->{bind_addr} ); |
| 398 |
return _stream_err( $xr, 0, 'Bad bind_addr value' ) |
| 399 |
if ( !defined $a ); |
| 400 |
if ( $p =~ tr/*// ) { |
| 401 |
for ( $p = 14011 ; $p < 65535 ; $p++ ) { |
| 402 |
if ( !bind( $xr->{sock}, sockaddr_in( $p, $a ) ) ) { |
| 403 |
return _stream_err( $xr, 0, 'bind() on socket failed' ) |
| 404 |
if ( $! ne 'Address already in use' ); |
| 405 |
} |
| 406 |
else { |
| 407 |
last; |
| 408 |
} |
| 409 |
} |
| 410 |
return _stream_err( $xr, 0, 'bind() cannot find open socket' ) |
| 411 |
if ( $p >= 65535 ); |
| 412 |
} |
| 413 |
else { |
| 414 |
return _stream_err( $xr, 0, 'bind() on socket failed' ) |
| 415 |
if ( !bind( $xr->{sock}, sockaddr_in( $p, $a ) ) ); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
if ( !defined $xr->{iaton} ) { |
| 420 |
$xr->{iaton} = inet_aton( $xr->{chost} ); |
| 421 |
return _stream_err( $xr, 0, 'can\'t resolve hostname' ) |
| 422 |
if ( !defined $xr->{iaton} ); |
| 423 |
} |
| 424 |
$xr->{socket_alloc}++; |
| 425 |
return 1; |
| 426 |
} |
| 427 |
|
| 428 |
sub _stream_socket_nonblock { |
| 429 |
my ( $fl, $xr, $nonblock ) = ( 0, @_ ); |
| 430 |
|
| 431 |
if ( $^O =~ /Win32/ ) { |
| 432 |
$fl = 1 if ($nonblock); |
| 433 |
|
| 434 |
# 0x8004667e = FIONBIO in Winsock2.h |
| 435 |
if ( !ioctl( $xr->{sock}, 0x8004667e, \$fl ) ) { |
| 436 |
return 0; |
| 437 |
} |
| 438 |
} |
| 439 |
else { |
| 440 |
if ( !( $fl = fcntl( $xr->{sock}, F_GETFL, 0 ) ) ) { |
| 441 |
return 0; |
| 442 |
} |
| 443 |
$fl |= O_NONBLOCK if ($nonblock); |
| 444 |
$fl &= ~O_NONBLOCK if ( !$nonblock ); |
| 445 |
if ( !( fcntl( $xr->{sock}, F_SETFL, $fl ) ) ) { |
| 446 |
return 0; |
| 447 |
} |
| 448 |
|
| 449 |
} |
| 450 |
return 1; |
| 451 |
} |
| 452 |
|
| 453 |
sub _stream_socket_open { |
| 454 |
my ( $vin, $xr, $wh ) = ( '', @_ ); |
| 455 |
return 0 if ( !defined $wh ); |
| 456 |
|
| 457 |
$xr->{'close'}->() if ( $xr->{state} > 0 ); |
| 458 |
return 0 if ( !_stream_socket_alloc( $xr, $wh ) ); |
| 459 |
$xr->{timeout} = $wh->{whisker}->{timeout} || 10; |
| 460 |
|
| 461 |
if ( $xr->{nonblock} ) { |
| 462 |
if ( !_stream_socket_nonblock( $xr, 1 ) ) { |
| 463 |
$xr->{nonblock} = 0; |
| 464 |
$LW_NONBLOCK_CONNECT = 0; |
| 465 |
} |
| 466 |
else { |
| 467 |
my $R = |
| 468 |
connect( $xr->{sock}, sockaddr_in( $xr->{cport}, $xr->{iaton} ) ); |
| 469 |
if ( !$R ) { |
| 470 |
return _stream_err( $xr, 1, 'can\'t connect (connect error)' ) |
| 471 |
if ( $! != EINPROGRESS && $! != EWOULDBLOCK ); |
| 472 |
vec( $vin, fileno( $xr->{sock} ), 1 ) = 1; |
| 473 |
return _stream_err( $xr, 1, 'can\'t connect (timeout)' ) |
| 474 |
if ( !select( undef, $vin, $vin, $xr->{timeout} ) |
| 475 |
|| !getpeername( $xr->{sock} ) ); |
| 476 |
} |
| 477 |
|
| 478 |
# leave in nonblock for normal TCP |
| 479 |
# if($xr->{streamtype} != 1 && !_stream_socket_nonblock($xr,0)){ |
| 480 |
# $LW_NONBLOCK_CONNECT=0; |
| 481 |
# return _stream_err($xr,1,'setting sock to block'); |
| 482 |
# } |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
if ( !$xr->{nonblock} ) { |
| 487 |
eval { |
| 488 |
local $SIG{ALRM} = sub { die "timeout\n" }; |
| 489 |
eval { alarm( $xr->{timeout} ) }; |
| 490 |
if ( |
| 491 |
!connect( |
| 492 |
$xr->{sock}, sockaddr_in( $xr->{cport}, $xr->{iaton} ) |
| 493 |
) |
| 494 |
) |
| 495 |
{ |
| 496 |
eval { alarm(0) }; |
| 497 |
die "connect failed\n"; |
| 498 |
} |
| 499 |
eval { alarm(0) }; |
| 500 |
}; |
| 501 |
return _stream_err( $xr, 0, |
| 502 |
'can\'t connect (' . substr( $@, 0, index( $@, "\n" ) ) . ')' ) |
| 503 |
if ($@); |
| 504 |
} |
| 505 |
|
| 506 |
binmode( $xr->{sock} ); |
| 507 |
my $S = select( $xr->{sock} ); |
| 508 |
$|++; |
| 509 |
select($S); |
| 510 |
$xr->{state} = 1; |
| 511 |
$xr->{syns}++; |
| 512 |
return 1; |
| 513 |
} |
| 514 |
|
| 515 |
sub _stream_ssl_open { |
| 516 |
my ( $xr, $wh ) = @_; |
| 517 |
return 0 if ( !defined $wh ); |
| 518 |
$xr->{close}->() if ( $xr->{state} > 0 ); |
| 519 |
my $W = $wh->{whisker}; |
| 520 |
|
| 521 |
if ( $xr->{streamtype} == 5 ) { |
| 522 |
|
| 523 |
# these have to always be set, to overwrite any previous |
| 524 |
# set values (using ENV is a crappy way to do this) |
| 525 |
$ENV{HTTPS_KEY_FILE} = $W->{ssl_rsacertfile} || ''; |
| 526 |
$ENV{HTTPS_CERT_FILE} = $W->{ssl_certfile} || ''; |
| 527 |
eval { |
| 528 |
$xr->{sock} = Net::SSL->new( |
| 529 |
PeerAddr => $xr->{chost}, |
| 530 |
PeerPort => $xr->{cport}, |
| 531 |
Timeout => $xr->{timeout} |
| 532 |
); |
| 533 |
}; |
| 534 |
return _stream_err( $xr, 0, 'can\'t connect: ' . $@ ) |
| 535 |
if ($@ || !defined $xr->{sock}); |
| 536 |
$xr->{sock}->autoflush(1); |
| 537 |
$xr->{state} = 1; |
| 538 |
|
| 539 |
# Net::SSL doesn't use stream_socket_open, so fake syns |
| 540 |
$xr->{syns}++; |
| 541 |
return 1; |
| 542 |
} |
| 543 |
|
| 544 |
return 0 if ( $xr->{streamtype} != 4 ); |
| 545 |
|
| 546 |
# otherwise, we're stream_NETSSLEAY |
| 547 |
|
| 548 |
if ( !defined $xr->{ctx} ) { |
| 549 |
return _stream_err( $xr, 0, 'ssl ctx create' ) |
| 550 |
if ( !( $xr->{ctx} = Net::SSLeay::CTX_new() ) ); |
| 551 |
Net::SSLeay::CTX_set_options( $xr->{ctx}, &Net::SSLeay::OP_ALL ); |
| 552 |
if ( defined $W->{ssl_rsacertfile} ) { |
| 553 |
if ( |
| 554 |
!( |
| 555 |
Net::SSLeay::CTX_use_RSAPrivateKey_file( |
| 556 |
$xr->{ctx}, $W->{ssl_rsacertfile}, |
| 557 |
&Net::SSLeay::FILETYPE_PEM |
| 558 |
) |
| 559 |
) |
| 560 |
) |
| 561 |
{ |
| 562 |
return _stream_err( $xr, 0, 'ssl ctx rsacert' ); |
| 563 |
} |
| 564 |
} |
| 565 |
if ( defined $W->{ssl_certfile} ) { |
| 566 |
if ( |
| 567 |
!( |
| 568 |
Net::SSLeay::CTX_use_certificate_file( |
| 569 |
$xr->{ctx}, $W->{ssl_certfile}, |
| 570 |
&Net::SSLeay::FILETYPE_PEM |
| 571 |
) |
| 572 |
) |
| 573 |
) |
| 574 |
{ |
| 575 |
return _stream_err( $xr, 0, 'ssl ctx cert' ); |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
# just to be safe, catch any errors that didn't get returned |
| 581 |
return _stream_err($xr, 0, 'ssl setup error' ) |
| 582 |
if( __bad_netssleay_error() ); |
| 583 |
|
| 584 |
return _stream_err( $xr, 0, 'ssl create new' ) |
| 585 |
if ( !( $xr->{sslobj} = Net::SSLeay::new( $xr->{ctx} ) ) ); |
| 586 |
if ( defined $W->{ssl_ciphers} ) { |
| 587 |
if ( |
| 588 |
!( |
| 589 |
Net::SSLeay::set_cipher_list( |
| 590 |
$xr->{sslobj}, $W->{ssl_ciphers} |
| 591 |
) |
| 592 |
) |
| 593 |
) |
| 594 |
{ |
| 595 |
return _stream_err( $xr, 0, 'ssl set ciphers' ); |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
# now we use a normal socket to connect |
| 600 |
return 0 if ( !_stream_socket_open( $xr, $wh ) ); |
| 601 |
$xr->{state} = 1; |
| 602 |
|
| 603 |
if ( $xr->{proxy} ) { |
| 604 |
my $C = 'CONNECT ' . $W->{host} . ':' . $W->{port} . " HTTP/1.0\r\n"; |
| 605 |
$C .= 'Proxy-Authorization: ' . $wh->{'Proxy-Authorization'} . "\r\n" |
| 606 |
if ( defined $wh->{'Proxy-Authorization'} ); |
| 607 |
$C .= "\r\n"; |
| 608 |
|
| 609 |
my $r = syswrite( $xr->{sock}, $C, length($C) ); |
| 610 |
return _stream_err( $xr, 1, 'sending proxy connect string' ) |
| 611 |
if ( !defined $r || $r != length($C) ); |
| 612 |
|
| 613 |
# now we need to read proxy response and parse it |
| 614 |
do { |
| 615 |
return _stream_err( $xr, 1, 'ssl proxy request failed' ) |
| 616 |
if ( !_stream_socket_read($xr) ); |
| 617 |
} while ( index( $xr->{bufin}, "\n\n" ) == -1 |
| 618 |
&& index( $xr->{bufin}, "\r\n\r\n" ) == -1 ); |
| 619 |
return _stream_err( $xr, 1, 'proxy couldn\'t make connection' ) |
| 620 |
if ( $xr->{bufin} !~ /^HTTP\/1.[0-9]+\W+200/ ); |
| 621 |
|
| 622 |
#$xr->{bufin}=''; |
| 623 |
$xr->{clearall}->(); |
| 624 |
} |
| 625 |
|
| 626 |
Net::SSLeay::set_fd( $xr->{sslobj}, fileno( $xr->{sock} ) ); |
| 627 |
Net::SSLeay::set_session( $xr->{sslobj}, $xr->{sslsession} ) |
| 628 |
if ( defined $xr->{sslsession} ); |
| 629 |
return _stream_err( $xr, 1, 'ssl connect failed' ) |
| 630 |
if ( !( Net::SSLeay::connect( $xr->{sslobj} ) ) || |
| 631 |
__bad_netssleay_error() ); |
| 632 |
|
| 633 |
# my $x = Net::SSLeay::ctrl( $xr->{sslobj}, 6, 0, '' ); |
| 634 |
$xr->{sslsession} = Net::SSLeay::get_session( $xr->{sslobj} ) |
| 635 |
if ( defined $W->{ssl_resume} && $W->{ssl_resume} > 0 ); |
| 636 |
|
| 637 |
# little trickery to abstract/normalize stuff |
| 638 |
$xr->{origsock} = $xr->{sock}; |
| 639 |
$xr->{sock} = $xr->{sslobj}; |
| 640 |
return 1; |
| 641 |
} |
| 642 |
|
| 643 |
sub _stream_file_open { |
| 644 |
my ( $xr, $wh ) = @_; |
| 645 |
$xr->{close}->() if ( $xr->{state} > 0 ); |
| 646 |
my $file = $wh->{whisker}->{file_stream}; |
| 647 |
return _stream_err( $xr, 0, 'invalid file' ) |
| 648 |
if ( !-e $file || !-f $file ); |
| 649 |
return _stream_err( $xr, 0, 'file open failure' ) |
| 650 |
if ( !sysopen( $xr->{sock}, $file, 'r' ) ); |
| 651 |
binmode($xr->{sock}); # Stupid Windows |
| 652 |
$xr->{state} = 1; |
| 653 |
} |
| 654 |
|
| 655 |
sub _stream_file_write { |
| 656 |
my $xr = shift; |
| 657 |
$xr->{bufout} = ''; |
| 658 |
return 1; |
| 659 |
} |
| 660 |
|
| 661 |
sub _stream_buffer_open { |
| 662 |
my ( $xr, $wh ) = @_; |
| 663 |
$xr->{close}->() if ( $xr->{state} > 0 ); |
| 664 |
$xr->{state} = 1; |
| 665 |
} |
| 666 |
|
| 667 |
sub _stream_buffer_close { |
| 668 |
my $xr = shift; |
| 669 |
$xr->{state} = 0; |
| 670 |
$xr->{bufout} = $xr->{bufin} = ''; |
| 671 |
} |
| 672 |
|
| 673 |
sub _stream_buffer_read { |
| 674 |
my $xr = shift; |
| 675 |
return 0 if ( $xr->{state} == 0 ); |
| 676 |
if ( length( $xr->{bufout} ) > 0 ) { |
| 677 |
$xr->{bufin} .= $xr->{bufout}; |
| 678 |
$xr->{bufout} = ''; |
| 679 |
} |
| 680 |
if ( length( $xr->{bufin} ) == 0 ) { |
| 681 |
$xr->{eof} = 1; |
| 682 |
return 0; |
| 683 |
} |
| 684 |
$xr->{eof} = 0; |
| 685 |
return 1; |
| 686 |
} |
| 687 |
|
| 688 |
sub _stream_buffer_write { |
| 689 |
my ( $xr, $data ) = ( shift, shift ); |
| 690 |
return 0 if ( $xr->{state} == 0 ); |
| 691 |
$xr->{bufout} .= $data if ( defined $data ); |
| 692 |
my $len = length( $xr->{bufout} ); |
| 693 |
return 1 if ( $len == 0 ); |
| 694 |
$xr->{bufin} .= $xr->{bufout}; |
| 695 |
$xr->{bufout} = ''; |
| 696 |
return 1; |
| 697 |
} |
| 698 |
|
| 699 |
sub _stream_err { |
| 700 |
my ( $xr, $close, $error ) = @_; |
| 701 |
$xr->{error} = $error; |
| 702 |
$xr->{error} .= ": $!" if ( defined $! && $! ne '' ); |
| 703 |
$xr->{'close'}->() if ($close); |
| 704 |
$xr->{state} = 0; |
| 705 |
return 0; |
| 706 |
} |
| 707 |
|