Commit:    131245474bf95490cf1a1dfdb5debe5d46133522
Author:    Gustavo Lopes <glo...@nebm.ist.utl.pt>         Mon, 5 Nov 2012 
14:52:48 +0100
Parents:   b3effa60c73922ddf4a7df3be3a0e4e5ca47f70d
Branches:  PHP-5.5 master

Link:       
http://git.php.net/?p=php-src.git;a=commitdiff;h=131245474bf95490cf1a1dfdb5debe5d46133522

Log:
Redactor to expose socket_import_file_descriptor()

Changed paths:
  M  ext/sockets/php_sockets.h
  M  ext/sockets/sockets.c


Diff:
diff --git a/ext/sockets/php_sockets.h b/ext/sockets/php_sockets.h
index 9158ca4..3138eb6 100644
--- a/ext/sockets/php_sockets.h
+++ b/ext/sockets/php_sockets.h
@@ -88,6 +88,7 @@ ZEND_END_MODULE_GLOBALS(sockets)
 ZEND_EXTERN_MODULE_GLOBALS(sockets);
 
 char *sockets_strerror(int error TSRMLS_DC);
+php_socket *socket_import_file_descriptor(PHP_SOCKET sock TSRMLS_DC);
 
 #define PHP_SOCKET_ERROR(socket,msg,errn) \
                socket->error = errn;   \
diff --git a/ext/sockets/sockets.c b/ext/sockets/sockets.c
index 863825d..449be8f 100644
--- a/ext/sockets/sockets.c
+++ b/ext/sockets/sockets.c
@@ -2426,41 +2426,25 @@ PHP_FUNCTION(socket_clear_error)
 }
 /* }}} */
 
-/* {{{ proto void socket_import_stream(resource stream)
-   Imports a stream that encapsulates a socket into a socket extension 
resource. */
-PHP_FUNCTION(socket_import_stream)
+php_socket *socket_import_file_descriptor(PHP_SOCKET socket TSRMLS_DC)
 {
-       zval                             *zstream;
-       php_stream                       *stream;
-       php_socket                       *retsock = NULL;
-       PHP_SOCKET                       socket; /* fd */
-       php_sockaddr_storage addr;
-       socklen_t                        addr_len = sizeof(addr);
+#ifdef SO_DOMAIN
+       int                                             type;
+       socklen_t                               type_len = sizeof(type);
+#endif
+       php_socket                              *retsock;
+       php_sockaddr_storage    addr;
+       socklen_t                               addr_len = sizeof(addr);
 #ifndef PHP_WIN32
        int                                      t;
 #endif
-#ifdef SO_DOMAIN
-       int                                     type;
-       socklen_t                       type_len = sizeof(type);
-#endif
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) == 
FAILURE) {
-               return;
-       }
-       php_stream_from_zval(stream, &zstream);
-
-       if (php_stream_cast(stream, PHP_STREAM_AS_SOCKETD, (void**)&socket, 1)) 
{
-               /* error supposedly already shown */
-               RETURN_FALSE;
-       }
-
-       retsock = php_create_socket();
+    retsock = php_create_socket();
+    retsock->bsd_socket = socket;
 
-       retsock->bsd_socket = socket;
-
-       /* determine family */
+    /* determine family */
 #ifdef SO_DOMAIN
-       if (getsockopt(socket, SOL_SOCKET, SO_DOMAIN, &type, &type_len) == 0) {
+    if (getsockopt(socket, SOL_SOCKET, SO_DOMAIN, &type, &type_len) == 0) {
                retsock->type = type;
        } else
 #endif
@@ -2471,16 +2455,49 @@ PHP_FUNCTION(socket_import_stream)
                goto error;
        }
 
-       /* determine blocking mode */
+    /* determine blocking mode */
 #ifndef PHP_WIN32
-       t = fcntl(socket, F_GETFL);
-       if(t == -1) {
+    t = fcntl(socket, F_GETFL);
+    if (t == -1) {
                PHP_SOCKET_ERROR(retsock, "unable to obtain blocking state", 
errno);
                goto error;
-       } else {
-               retsock->blocking = !(t & O_NONBLOCK);
+    } else {
+       retsock->blocking = !(t & O_NONBLOCK);
+    }
+#endif
+
+    return retsock;
+
+error:
+       efree(retsock);
+       return NULL;
+}
+
+/* {{{ proto void socket_import_stream(resource stream)
+   Imports a stream that encapsulates a socket into a socket extension 
resource. */
+PHP_FUNCTION(socket_import_stream)
+{
+       zval                             *zstream;
+       php_stream                       *stream;
+       php_socket                       *retsock = NULL;
+       PHP_SOCKET                       socket; /* fd */
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) == 
FAILURE) {
+               return;
        }
-#else
+       php_stream_from_zval(stream, &zstream);
+
+       if (php_stream_cast(stream, PHP_STREAM_AS_SOCKETD, (void**)&socket, 1)) 
{
+               /* error supposedly already shown */
+               RETURN_FALSE;
+       }
+
+       retsock = socket_import_file_descriptor(socket);
+       if (retsock == NULL) {
+               RETURN_FALSE;
+       }
+
+#ifdef PHP_WIN32
        /* on windows, check if the stream is a socket stream and read its
         * private data; otherwise assume it's in non-blocking mode */
        if (php_stream_is(stream, PHP_STREAM_IS_SOCKET)) {
@@ -2504,11 +2521,6 @@ PHP_FUNCTION(socket_import_stream)
                PHP_STREAM_BUFFER_NONE, NULL);
 
        ZEND_REGISTER_RESOURCE(return_value, retsock, le_socket);
-       return;
-error:
-       if (retsock != NULL)
-               efree(retsock);
-       RETURN_FALSE;
 }
 /* }}} */


--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to