Hi,

i recently wrote small function that allows the creation of a php-socket by directly giving the fd#

this is very useful under linux/unix environments for Ipc.
For example:
getting forked by a webserver as fastcgi / scgi .., the webserver gives an acceptable socket as fd#0 to the process..

I submitted the patch about 3 weeks ago @ pecl-dev list in the fdpass extension topic.

It would be nice to see such function in dist php :)


Florian

diff -Naur php-5.3.6_dist/ext/sockets/php_sockets.h 
php-5.3.6_socketpatch/ext/sockets/php_sockets.h
--- php-5.3.6_dist/ext/sockets/php_sockets.h    2011-01-01 03:19:59.000000000 
+0100
+++ php-5.3.6_socketpatch/ext/sockets/php_sockets.h     2011-04-27 
15:16:21.000000000 +0200
@@ -56,6 +56,9 @@
 PHP_FUNCTION(socket_getsockname);
 PHP_FUNCTION(socket_getpeername);
 PHP_FUNCTION(socket_create);
+#ifndef PHP_WIN32
+PHP_FUNCTION(socket_create_from_fdno);
+#endif
 PHP_FUNCTION(socket_connect);
 PHP_FUNCTION(socket_strerror);
 PHP_FUNCTION(socket_bind);
diff -Naur php-5.3.6_dist/ext/sockets/sockets.c 
php-5.3.6_socketpatch/ext/sockets/sockets.c
--- php-5.3.6_dist/ext/sockets/sockets.c        2011-01-01 03:19:59.000000000 
+0100
+++ php-5.3.6_socketpatch/ext/sockets/sockets.c 2011-04-27 15:19:53.000000000 
+0200
@@ -177,6 +177,13 @@
        ZEND_ARG_INFO(0, protocol)
 ZEND_END_ARG_INFO()
 
+#ifndef PHP_WIN32
+ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create_from_fdno, 0, 0, 1)
+       ZEND_ARG_INFO(0, fdno)
+       ZEND_ARG_INFO(0, domain)
+ZEND_END_ARG_INFO()
+#endif
+
 ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_connect, 0, 0, 2)
        ZEND_ARG_INFO(0, socket)
        ZEND_ARG_INFO(0, addr)
@@ -269,6 +276,9 @@
        PHP_FE(socket_select,                   arginfo_socket_select)
        PHP_FE(socket_create,                   arginfo_socket_create)
        PHP_FE(socket_create_listen,    arginfo_socket_create_listen)
+#ifndef PHP_WIN32
+       PHP_FE(socket_create_from_fdno, arginfo_socket_create_from_fdno)
+#endif
 #ifdef HAVE_SOCKETPAIR
        PHP_FE(socket_create_pair,              arginfo_socket_create_pair)
 #endif
@@ -1272,6 +1282,40 @@
 }
 /* }}} */
 
+#ifndef PHP_WIN32
+/* {{{ proto resource socket_create_from_fdno(int fdno, int domain)
+       Creates an socket from given fd-number */
+PHP_FUNCTION(socket_create_from_fdno)
+{
+       long arg1, arg2;
+       php_socket *php_sock = NULL;
+       
+       if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &arg1, &arg2) 
== FAILURE){
+               return;
+       }
+       
+       if(arg2 != AF_UNIX 
+#if HAVE_IPV6
+               && arg2 != AF_INET6 
+#endif
+               && arg2 != AF_INET) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket 
domain [%ld] specified for argument 2, assuming AF_INET", arg2);
+               arg2 = AF_INET;
+       }
+       
+       
+       php_sock = (php_socket*)emalloc( sizeof(php_socket) );
+       
+       php_sock->bsd_socket = arg1;
+       php_sock->type = arg2;
+       php_sock->error = 0;
+       php_sock->blocking = 1;
+       
+       ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
+}
+/* }}} */
+#endif
+
 /* {{{ proto bool socket_connect(resource socket, string addr [, int port])
    Opens a connection to addr:port on the socket specified by socket */
 PHP_FUNCTION(socket_connect)

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to