From:             kernins at gmail dot com
Operating system: Debian 4.0, WinXP SP3
PHP version:      5.3.0
PHP Bug Type:     Sockets related
Bug description:  socket_read couldn't detect the end of data

Description:
------------
App description: Script should connect to socks4/5 or CONNECT proxy and
establish a tunnel to some host, google.com:80, for example.

Problem: socket_read() couldn't detect the end of data while reading
response from socks4/5 server (response to initial hello msg or connect
command) or from connect-proxy (response to connect command).

Reproduce code:
---------------
$host=gethostbyname("google.com"); $port=80;
$proxy_ip="212.27.33.4"; $proxy_port=8118;
if(!preg_match('/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/',$host,$ip)) die();
$s5_init="\x05\x01\x00";
$s5_connect="\x05\x01\x00\x01".chr($ip[1]).chr($ip[2]).chr($ip[3]).chr($ip[4]).pack('n',$port);
$s4="\x04\x01".pack('n',$port).chr($ip[1]).chr($ip[2]).chr($ip[3]).chr($ip[4])."\x00";
$connect="CONNECT google.com:80 HTTP/1.1\r\nProxy-Connection:
keep-alive\r\n\r\n";

$sock=socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($sock,SOL_SOCKET,SO_SNDTIMEO,array('sec'=>5,'usec'=>0));
socket_set_option($sock,SOL_SOCKET,SO_RCVTIMEO,array('sec'=>15,'usec'=>0));
socket_connect($sock,$proxy_ip,$proxy_port);
echo "written: ".socket_write($sock,$connect).PHP_EOL;
//$data=socket_read($sock,1024,PHP_BINARY_READ);
while($data=socket_read($sock,1024,PHP_BINARY_READ)) var_dump($data);
$err=socket_last_error($sock);
$e=socket_get_option($sock,SOL_SOCKET,SO_ERROR);
var_dump($data,$err,socket_strerror($err),$e,socket_strerror($e));

Expected result:
----------------
In blocking mode:
while($data=socket_read($sock,1024,PHP_BINARY_READ))
will make two or more iterations, at last iteration socket_read will
return emty string indicating end of data.

In non-blocking mode:
socket_select will return socket two or more times as ready for reading.
Last time socket_read will return emty string indicating end of data.

Actual result:
--------------
In blocking mode:
if I use a single socket_read($sock,1024,PHP_BINARY_READ) call, it returns
data without any significant delay or error. In that case there are no
problems.

But
while($data=socket_read($sock,1024,PHP_BINARY_READ))
will return all response in first iteration and then blocks until timeout
was reached (if I set it via socket_set_option) and return false with
EAGAIN error. If timeout is not set it will return epmty string and no
errors as expected, but before this block for couple of minutes.

In non-blocking:
Whole response was also successfully recieved after first socket_select
return, and then this socket has no more returned by socket_select as ready
for reading.

I've tested this on debian PHP 5.2.9-0.dotdeb.1 with Suhosin-Patch 0.9.7
and WinXP php 5.3.0 (binary from php.net) with some public socks and
connect proxies and with SHHTunnel 4.3.0.0 for Win which has built in socks
4/5 proxy.

-- 
Edit bug report at http://bugs.php.net/?id=49486&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=49486&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=49486&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=49486&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=49486&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=49486&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=49486&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=49486&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=49486&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=49486&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=49486&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=49486&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=49486&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=49486&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=49486&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=49486&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=49486&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=49486&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=49486&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=49486&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=49486&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=49486&r=mysqlcfg

Reply via email to