scottmac Sat, 23 Jul 2011 01:29:44 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=313616
Log: When we have a blocking SSL socket, respect the timeout option. reading from SSL sockets could block indefinitely due to the lack of timeout Changed paths: U php/php-src/branches/PHP_5_3/NEWS U php/php-src/branches/PHP_5_3/ext/openssl/xp_ssl.c U php/php-src/branches/PHP_5_4/ext/openssl/xp_ssl.c U php/php-src/trunk/ext/openssl/xp_ssl.c Modified: php/php-src/branches/PHP_5_3/NEWS =================================================================== --- php/php-src/branches/PHP_5_3/NEWS 2011-07-23 01:08:24 UTC (rev 313615) +++ php/php-src/branches/PHP_5_3/NEWS 2011-07-23 01:29:44 UTC (rev 313616) @@ -7,6 +7,8 @@ - OpenSSL . openssl_encrypt()/openssl_decrypt() truncated keys of variable length ciphers to the OpenSSL default for the algorithm. (Scott) + . On blocking SSL sockets respect the timeout option where possible. + (Scott) 14 Jul 2011, PHP 5.3.7 RC3 - Zend Engine: Modified: php/php-src/branches/PHP_5_3/ext/openssl/xp_ssl.c =================================================================== --- php/php-src/branches/PHP_5_3/ext/openssl/xp_ssl.c 2011-07-23 01:08:24 UTC (rev 313615) +++ php/php-src/branches/PHP_5_3/ext/openssl/xp_ssl.c 2011-07-23 01:29:44 UTC (rev 313616) @@ -204,6 +204,36 @@ return didwrite; } +static void php_openssl_stream_wait_for_data(php_stream *stream, php_netstream_data_t *sock TSRMLS_DC) +{ + int retval; + struct timeval *ptimeout; + + if (sock->socket == -1) { + return; + } + + sock->timeout_event = 0; + + if (sock->timeout.tv_sec == -1) + ptimeout = NULL; + else + ptimeout = &sock->timeout; + + while(1) { + retval = php_pollfd_for(sock->socket, PHP_POLLREADABLE, ptimeout); + + if (retval == 0) + sock->timeout_event = 1; + + if (retval >= 0) + break; + + if (php_socket_errno() != EINTR) + break; + } +} + static size_t php_openssl_sockop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) { php_openssl_netstream_data_t *sslsock = (php_openssl_netstream_data_t*)stream->abstract; @@ -213,6 +243,13 @@ int retry = 1; do { + if (sslsock->s.is_blocked) { + php_openssl_stream_wait_for_data(stream, &(sslsock->s) TSRMLS_CC); + if (sslsock->s.timeout_event) { + break; + } + /* there is no guarantee that there is application data available but something is there */ + } nr_bytes = SSL_read(sslsock->ssl_handle, buf, count); if (nr_bytes <= 0) { Modified: php/php-src/branches/PHP_5_4/ext/openssl/xp_ssl.c =================================================================== --- php/php-src/branches/PHP_5_4/ext/openssl/xp_ssl.c 2011-07-23 01:08:24 UTC (rev 313615) +++ php/php-src/branches/PHP_5_4/ext/openssl/xp_ssl.c 2011-07-23 01:29:44 UTC (rev 313616) @@ -204,6 +204,36 @@ return didwrite; } +static void php_openssl_stream_wait_for_data(php_stream *stream, php_netstream_data_t *sock TSRMLS_DC) +{ + int retval; + struct timeval *ptimeout; + + if (sock->socket == -1) { + return; + } + + sock->timeout_event = 0; + + if (sock->timeout.tv_sec == -1) + ptimeout = NULL; + else + ptimeout = &sock->timeout; + + while(1) { + retval = php_pollfd_for(sock->socket, PHP_POLLREADABLE, ptimeout); + + if (retval == 0) + sock->timeout_event = 1; + + if (retval >= 0) + break; + + if (php_socket_errno() != EINTR) + break; + } +} + static size_t php_openssl_sockop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) { php_openssl_netstream_data_t *sslsock = (php_openssl_netstream_data_t*)stream->abstract; @@ -213,6 +243,13 @@ int retry = 1; do { + if (sslsock->s.is_blocked) { + php_openssl_stream_wait_for_data(stream, &(sslsock->s) TSRMLS_CC); + if (sslsock->s.timeout_event) { + break; + } + /* there is no guarantee that there is application data available but something is there */ + } nr_bytes = SSL_read(sslsock->ssl_handle, buf, count); if (nr_bytes <= 0) { Modified: php/php-src/trunk/ext/openssl/xp_ssl.c =================================================================== --- php/php-src/trunk/ext/openssl/xp_ssl.c 2011-07-23 01:08:24 UTC (rev 313615) +++ php/php-src/trunk/ext/openssl/xp_ssl.c 2011-07-23 01:29:44 UTC (rev 313616) @@ -204,6 +204,36 @@ return didwrite; } +static void php_openssl_stream_wait_for_data(php_stream *stream, php_netstream_data_t *sock TSRMLS_DC) +{ + int retval; + struct timeval *ptimeout; + + if (sock->socket == -1) { + return; + } + + sock->timeout_event = 0; + + if (sock->timeout.tv_sec == -1) + ptimeout = NULL; + else + ptimeout = &sock->timeout; + + while(1) { + retval = php_pollfd_for(sock->socket, PHP_POLLREADABLE, ptimeout); + + if (retval == 0) + sock->timeout_event = 1; + + if (retval >= 0) + break; + + if (php_socket_errno() != EINTR) + break; + } +} + static size_t php_openssl_sockop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) { php_openssl_netstream_data_t *sslsock = (php_openssl_netstream_data_t*)stream->abstract; @@ -213,6 +243,13 @@ int retry = 1; do { + if (sslsock->s.is_blocked) { + php_openssl_stream_wait_for_data(stream, &(sslsock->s) TSRMLS_CC); + if (sslsock->s.timeout_event) { + break; + } + /* there is no guarantee that there is application data available but something is there */ + } nr_bytes = SSL_read(sslsock->ssl_handle, buf, count); if (nr_bytes <= 0) {
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php