cvs: php-src /ext/sockets php_sockets.h php_sockets_win.c php_sockets_win.h sockets.c

From: Date: Sun, 22 Jul 2007 23:00:14 +0000
Subject: cvs: php-src /ext/sockets php_sockets.h php_sockets_win.c php_sockets_win.h sockets.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-45517@lists.php.net to get a copy of this message
jani Sun Jul 22 23:00:14 2007 UTC Modified files: /php-src/ext/sockets php_sockets.h php_sockets_win.c php_sockets_win.h sockets.c Log: - Fixed bug #21197 (socket_read() outputs error with PHP_NORMAL_READ)

http://cvs.php.net/viewvc.cgi/php-src/ext/sockets/php_sockets.h?r1=1.41&r2=1.42&diff_format=u Index: php-src/ext/sockets/php_sockets.h diff -u php-src/ext/sockets/php_sockets.h:1.41 php-src/ext/sockets/php_sockets.h:1.42 --- php-src/ext/sockets/php_sockets.h:1.41 Sun Jul 22 22:17:51 2007 +++ php-src/ext/sockets/php_sockets.h Sun Jul 22 23:00:14 2007 @@ -22,7 +22,7 @@ #ifndef PHP_SOCKETS_H #define PHP_SOCKETS_H -/* $Id: php_sockets.h,v 1.41 2007/07/22 22:17:51 jani Exp $ */ +/* $Id: php_sockets.h,v 1.42 2007/07/22 23:00:14 jani Exp $ */ #if HAVE_SOCKETS @@ -83,13 +83,14 @@ PHP_SOCKET bsd_socket; int type; int error; + int blocking; } php_socket; /* Prototypes */ #ifdef ilia_0 /* not needed, only causes a compiler warning */ static int php_open_listen_sock(php_socket **php_sock, int port, int backlog TSRMLS_DC); static int php_accept_connect(php_socket *in_sock, php_socket **new_sock, struct sockaddr *la TSRMLS_DC); -static int php_read(int bsd_socket, void *buf, size_t maxlen, int flags); +static int php_read(php_socket *sock, void *buf, size_t maxlen, int flags); static char *php_strerror(int error TSRMLS_DC); #endif http://cvs.php.net/viewvc.cgi/php-src/ext/sockets/php_sockets_win.c?r1=1.14&r2=1.15&diff_format=u Index: php-src/ext/sockets/php_sockets_win.c diff -u php-src/ext/sockets/php_sockets_win.c:1.14 php-src/ext/sockets/php_sockets_win.c:1.15 --- php-src/ext/sockets/php_sockets_win.c:1.14 Mon Jan 1 09:29:29 2007 +++ php-src/ext/sockets/php_sockets_win.c Sun Jul 22 23:00:14 2007 @@ -19,7 +19,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_sockets_win.c,v 1.14 2007/01/01 09:29:29 sebastian Exp $ */ +/* $Id: php_sockets_win.c,v 1.15 2007/07/22 23:00:14 jani Exp $ */ #ifdef PHP_WIN32 @@ -81,29 +81,4 @@ return 1; } - -int fcntl(int fd, int cmd, ...) { - va_list va; - int retval, io, mode; - - va_start(va, cmd); - - switch(cmd) { - case F_GETFL: - case F_SETFD: - case F_GETFD: - default: - retval = -1; - break; - - case F_SETFL: - io = va_arg(va, int); - mode = io == O_NONBLOCK ? 1 : 0; - retval = ioctlsocket(fd, io, &mode); - break; - } - - va_end(va); - return retval; -} #endif http://cvs.php.net/viewvc.cgi/php-src/ext/sockets/php_sockets_win.h?r1=1.14&r2=1.15&diff_format=u Index: php-src/ext/sockets/php_sockets_win.h diff -u php-src/ext/sockets/php_sockets_win.h:1.14 php-src/ext/sockets/php_sockets_win.h:1.15 --- php-src/ext/sockets/php_sockets_win.h:1.14 Mon Jan 1 09:29:29 2007 +++ php-src/ext/sockets/php_sockets_win.h Sun Jul 22 23:00:14 2007 @@ -19,18 +19,11 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_sockets_win.h,v 1.14 2007/01/01 09:29:29 sebastian Exp $ */ +/* $Id: php_sockets_win.h,v 1.15 2007/07/22 23:00:14 jani Exp $ */ #ifdef PHP_WIN32 -#define F_SETFL 0 -#define F_GETFL 1 -#define F_SETFD 2 -#define F_GETFD 3 - -#define O_NONBLOCK FIONBIO - #define EPROTONOSUPPORT WSAEPROTONOSUPPORT #define ECONNRESET WSAECONNRESET @@ -50,6 +43,5 @@ int socketpair(int domain, int type, int protocol, SOCKET sock[2]); int inet_aton(const char *cp, struct in_addr *inp); -int fcntl(int fd, int cmd, ...); #endif http://cvs.php.net/viewvc.cgi/php-src/ext/sockets/sockets.c?r1=1.192&r2=1.193&diff_format=u Index: php-src/ext/sockets/sockets.c diff -u php-src/ext/sockets/sockets.c:1.192 php-src/ext/sockets/sockets.c:1.193 --- php-src/ext/sockets/sockets.c:1.192 Sun Jul 22 22:46:52 2007 +++ php-src/ext/sockets/sockets.c Sun Jul 22 23:00:14 2007 @@ -19,7 +19,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: sockets.c,v 1.192 2007/07/22 22:46:52 jani Exp $ */ +/* $Id: sockets.c,v 1.193 2007/07/22 23:00:14 jani Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -30,6 +30,7 @@ #if HAVE_SOCKETS #include "php_network.h" +#include "ext/standard/file.h" #include "ext/standard/info.h" #include "php_ini.h" @@ -211,6 +212,7 @@ la.sin_port = htons((unsigned short) port); sock->bsd_socket = socket(PF_INET, SOCK_STREAM, 0); + sock->blocking = 1; if (IS_INVALID_SOCKET(sock)) { PHP_SOCKET_ERROR(sock, "unable to create listening socket", errno); @@ -245,6 +247,7 @@ *new_sock = out_sock; salen = sizeof(*la); + out_sock->blocking = 1; out_sock->bsd_socket = accept(in_sock->bsd_socket, la, &salen); @@ -259,7 +262,7 @@ /* }}} */ /* {{{ php_read -- wrapper around read() so that it only reads to a \r or \n. */ -static int php_read(int bsd_socket, void *buf, size_t maxlen, int flags) +static int php_read(php_socket *sock, void *buf, size_t maxlen, int flags) { int m = 0; size_t n = 0; @@ -267,14 +270,16 @@ int nonblock = 0; char *t = (char *) buf; - m = fcntl(bsd_socket, F_GETFL); +#ifndef PHP_WIN32 + m = fcntl(sock->bsd_socket, F_GETFL); if (m < 0) { return m; } - nonblock = (m & O_NONBLOCK); m = 0; - +#else + nonblock = !sock->blocking; +#endif set_errno(0); *t = '\0'; @@ -298,7 +303,7 @@ } if (n < maxlen) { - m = recv(bsd_socket, (void *) t, 1, flags); + m = recv(sock->bsd_socket, (void *) t, 1, flags); } if (errno != 0 && errno != ESPIPE && errno != EAGAIN) { @@ -689,6 +694,7 @@ } php_sock->error = 0; + php_sock->blocking = 1; ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket); } @@ -713,6 +719,7 @@ } new_sock->error = 0; + new_sock->blocking = 1; ZEND_REGISTER_RESOURCE(return_value, new_sock, le_socket); } @@ -724,7 +731,6 @@ { zval *arg1; php_socket *php_sock; - int flags; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) { return; @@ -732,18 +738,10 @@ ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket); - flags = fcntl(php_sock->bsd_socket, F_GETFL); - - /* Safely append non blocking to other flags unless the get fails. - * Note: This does not abort on failure becuse getfl will always fail - * under the current win32 code. */ - if (flags > -1) flags |= O_NONBLOCK; - else flags = O_NONBLOCK; - - if (fcntl(php_sock->bsd_socket, F_SETFL, flags) > -1) { + if (php_set_sock_blocking(php_sock->bsd_socket, 0 TSRMLS_CC) == SUCCESS) { + php_sock->blocking = 0; RETURN_TRUE; } - RETURN_FALSE; } /* }}} */ @@ -754,7 +752,6 @@ { zval *arg1; php_socket *php_sock; - int flags; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) { return; @@ -762,18 +759,10 @@ ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket); - flags = fcntl(php_sock->bsd_socket, F_GETFL); - - /* Safely remove blocking from flags unless the get fails. - * Note: This does not abort on failure becuse getfl will always fail - * under the current win32 code. */ - if (flags > -1) flags &= ~O_NONBLOCK; - else flags = 0; - - if (fcntl(php_sock->bsd_socket, F_SETFL, flags) > -1) { + if (php_set_sock_blocking(php_sock->bsd_socket, 1 TSRMLS_CC) == SUCCESS) { + php_sock->blocking = 1; RETURN_TRUE; } - RETURN_FALSE; } /* }}} */ @@ -796,7 +785,6 @@ PHP_SOCKET_ERROR(php_sock, "unable to listen on socket", errno); RETURN_FALSE; } - RETURN_TRUE; } /* }}} */ @@ -876,7 +864,7 @@ ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket); if (type == PHP_NORMAL_READ) { - retval = php_read(php_sock->bsd_socket, tmpbuf, length, 0); + retval = php_read(php_sock, tmpbuf, length, 0); } else { retval = recv(php_sock->bsd_socket, tmpbuf, length, 0); } @@ -1072,7 +1060,7 @@ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &arg1, &arg2, &arg3) == FAILURE) { efree(php_sock); return; - } + } if (arg1 != AF_UNIX #if HAVE_IPV6 @@ -1099,6 +1087,7 @@ } php_sock->error = 0; + php_sock->blocking = 1; ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket); } @@ -1491,7 +1480,6 @@ ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket); - switch (php_sock->type) { case AF_UNIX: memset(&s_un, 0, sizeof(s_un)); @@ -1604,6 +1592,7 @@ #endif array_init(return_value); + add_assoc_long(return_value, "sec", tv.tv_sec); add_assoc_long(return_value, "usec", tv.tv_usec); break; @@ -1615,6 +1604,7 @@ PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno); RETURN_FALSE; } + RETURN_LONG(other_val); break; } @@ -1632,14 +1622,13 @@ #ifdef PHP_WIN32 int timeout; #else - struct timeval tv; + struct timeval tv; #endif long level, optname; void *opt_ptr; HashTable *opt_ht; zval **l_onoff, **l_linger; zval **sec, **usec; - /* key name constants */ char *l_onoff_key = "l_onoff"; char *l_linger_key = "l_linger"; @@ -1777,6 +1766,8 @@ php_sock[1]->type = domain; php_sock[0]->error = 0; php_sock[1]->error = 0; + php_sock[0]->blocking = 1; + php_sock[1]->blocking = 1; ZEND_REGISTER_RESOURCE(retval[0], php_sock[0], le_socket); ZEND_REGISTER_RESOURCE(retval[1], php_sock[1], le_socket);
« previous php.cvs (#45517) next »