]> granicus.if.org Git - php/commitdiff
- Store last errno in the module global 'last_error' implicitely
authorMarkus Fischer <mfischer@php.net>
Mon, 29 Apr 2002 14:52:29 +0000 (14:52 +0000)
committerMarkus Fischer <mfischer@php.net>
Mon, 29 Apr 2002 14:52:29 +0000 (14:52 +0000)
- Set the global 'last_error' explicitely for functions which can't return an
  error withing a single socket context (socket_create and socket_select)
- Modified socket_last_error() to return global modules last
  error if no socket resource is given
- Added a couple of more E_WARNING messages in case something
  goes foobar so the user isn't left alone in the dark.

ext/sockets/php_sockets.h
ext/sockets/sockets.c

index 06529f7e0bb48e830cd4d8bc300539535e796f70..f3986f7b0c2a62f81a3f41ec12c5ab5f33f00923 100644 (file)
@@ -102,12 +102,14 @@ int open_listen_sock(php_socket **php_sock, int port, int backlog TSRMLS_DC);
 int accept_connect(php_socket *in_sock, php_socket **new_sock, struct sockaddr *la TSRMLS_DC);
 int php_read(int bsd_socket, void *buf, int maxlen);
 
+ZEND_BEGIN_MODULE_GLOBALS(sockets)
+    int last_error;
+ZEND_END_MODULE_GLOBALS(sockets)
+
 #ifdef ZTS
-#define SOCKETSG(v) (sockets_globals->v)
-#define SOCKETSLS_FETCH() php_sockets_globals *sockets_globals = ts_resource(sockets_globals_id)
+#define SOCKETS_G(v) TSRMG(sockets_globals_id, zend_sockets_globals *, v)
 #else
-#define SOCKETSG(v) (sockets_globals.v)
-#define SOCKETSLS_FETCH()
+#define SOCKETS_G(v) (sockets_globals.v)
 #endif
 
 #else
index 7c52f8c38c848a12a627167fee626e2874fd4c40..d175e49c5197708025b01bc5c6070b5623ea10ce 100644 (file)
 # define IS_INVALID_SOCKET(a)  (a->bsd_socket == INVALID_SOCKET)
 #endif
 
-#ifdef ZTS
-int sockets_globals_id;
-#else
-php_sockets_globals sockets_globals;
-#endif
-
+ZEND_DECLARE_MODULE_GLOBALS(sockets)
 
 #ifndef MSG_WAITALL
 #ifdef LINUX
@@ -90,7 +85,9 @@ php_sockets_globals sockets_globals;
 #define PHP_BINARY_READ 0x0002
 
 #define PHP_SOCKET_ERROR(socket,msg,errn)      socket->error = errn;   \
-                                                                                       php_error(E_WARNING, "%s() %s [%d]: %s", get_active_function_name(TSRMLS_C), msg, errn, php_strerror(errn))
+                                                                                       SOCKETS_G(last_error) = errn; \
+                                                                                       php_error(E_WARNING, "%s() %s [%d]: %s", \
+                                                                                                         get_active_function_name(TSRMLS_C), msg, errn, php_strerror(errn))
 
 static int le_iov;
 #define le_iov_name "Socket I/O vector"
@@ -374,12 +371,20 @@ int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_soc
        return 1;
 }
                
+
+static void php_sockets_init_globals(zend_sockets_globals *sockets_globals TSRMLS_DC)
+{
+       sockets_globals->last_error = 0;
+}
+
 /* {{{ PHP_MINIT_FUNCTION
  */
 PHP_MINIT_FUNCTION(sockets)
 {
        struct protoent *pe;
 
+       ZEND_INIT_MODULE_GLOBALS(sockets, php_sockets_init_globals, NULL);
+
        le_socket       = zend_register_list_destructors_ex(destroy_socket,     NULL, le_socket_name, module_number);
        le_iov          = zend_register_list_destructors_ex(destroy_iovec,      NULL, le_iov_name, module_number);
 
@@ -526,6 +531,7 @@ PHP_FUNCTION(socket_select)
        retval = select(max_fd+1, &rfds, &wfds, &efds, tv_p);
 
        if (retval == -1) {
+               SOCKETS_G(last_error) = errno;
                php_error(E_WARNING, "%s() %s [%d]: %s", get_active_function_name(TSRMLS_C), "unable to select", errno, php_strerror(errno));
                RETURN_FALSE;
        }
@@ -570,6 +576,8 @@ PHP_FUNCTION(socket_accept)
        ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
        
        if (!accept_connect(php_sock, &new_sock, (struct sockaddr *) &sa TSRMLS_CC)) {
+               php_error(E_WARNING, "%s() unable to accept socket connection [%d]: %s",
+                                 get_active_function_name(TSRMLS_C), errno, php_strerror(errno));
                RETURN_FALSE;
        }
        
@@ -798,6 +806,8 @@ PHP_FUNCTION(socket_getsockname)
                        RETURN_TRUE;
 
                default:
+                       php_error(E_WARNING, "%s() Unsupported address family %d",
+                                         get_active_function_name(TSRMLS_C), sa->sa_family);
                        RETURN_FALSE;
        }
 }
@@ -854,6 +864,8 @@ PHP_FUNCTION(socket_getpeername)
                        RETURN_TRUE;
 
                default:
+                       php_error(E_WARNING, "%s() Unsupported address family %d",
+                                         get_active_function_name(TSRMLS_C), sa->sa_family);
                        RETURN_FALSE;
        }
 }
@@ -885,6 +897,9 @@ PHP_FUNCTION(socket_create)
        php_sock->type = arg1;
 
        if (IS_INVALID_SOCKET(php_sock)) {
+               SOCKETS_G(last_error) = errno;
+               php_error(E_WARNING, "%s() Unable to create socket [%d]: %s",
+                                 get_active_function_name(TSRMLS_C), errno, php_strerror(errno));
                efree(php_sock);
                RETURN_FALSE;
        }
@@ -912,6 +927,8 @@ PHP_FUNCTION(socket_connect)
        switch(php_sock->type) {
                case AF_INET:
                        if (ZEND_NUM_ARGS() != 3) {
+                               php_error(E_WARNING, "%s() Socket of type AF_INET requires 3 arguments",
+                                                 get_active_function_name(TSRMLS_C));
                                RETURN_FALSE;
                        }
 
@@ -932,6 +949,8 @@ PHP_FUNCTION(socket_connect)
                        break;
 
                default:
+                       php_error(E_WARNING, "%s() Unsupported socket type %d",
+                                         get_active_function_name(TSRMLS_C), php_sock->type);
                        RETURN_FALSE;
                }       
        
@@ -1370,6 +1389,8 @@ PHP_FUNCTION(socket_recvfrom)
                        break;
 
                default:
+                       php_error(E_WARNING, "%s() Unsupported socket type %d",
+                                         get_active_function_name(TSRMLS_C), php_sock->type);
                        RETURN_FALSE;
        }
 
@@ -1420,6 +1441,8 @@ PHP_FUNCTION(socket_sendto)
                        break;
 
                default:
+                       php_error(E_WARNING, "%s() Unsupported socket type %d",
+                                         get_active_function_name(TSRMLS_C), php_sock->type);
                        RETURN_FALSE;
        }
 
@@ -1565,6 +1588,8 @@ PHP_FUNCTION(socket_recvmsg)
                }
 
        default:
+               php_error(E_WARNING, "%s() Unsupported address family %d",
+                                 get_active_function_name(TSRMLS_C), sa->sa_family);
                RETURN_FALSE;
        }
 }
@@ -1649,6 +1674,8 @@ PHP_FUNCTION(socket_sendmsg)
                        }
 
                default:
+                       php_error(E_WARNING, "%s() Unsupported address family %d",
+                                         get_active_function_name(TSRMLS_C), sa.sa_family);
                        RETURN_FALSE;
        }
 }
@@ -1895,19 +1922,19 @@ PHP_FUNCTION(socket_shutdown)
    Returns the last error on the socket */
 PHP_FUNCTION(socket_last_error)
 {
-       zval            *arg1;
+       zval            *arg1 = NULL;
        php_socket      *php_sock;
-       int                     error;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE)
-               return; 
-
-       ZEND_FETCH_RESOURCE(php_sock, php_socket*, &arg1, -1, le_socket_name, le_socket);
-
-       error = php_sock->error;
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &arg1) == FAILURE)
+               return;
 
-       RETURN_LONG(error);
-}  
+       if (arg1) {
+               ZEND_FETCH_RESOURCE(php_sock, php_socket*, &arg1, -1, le_socket_name, le_socket);
+               RETVAL_LONG(php_sock->error);
+       } else {
+               RETVAL_LONG(SOCKETS_G(last_error));
+       }
+}
 /* }}} */
 
 /* {{{ proto void socket_clear_error(resource socket)