Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:52443 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 12683 invoked from network); 18 May 2011 10:44:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 May 2011 10:44:41 -0000 Authentication-Results: pb1.pair.com smtp.mail=fw@f-ws.de; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=fw@f-ws.de; sender-id=pass Received-SPF: pass (pb1.pair.com: domain f-ws.de designates 209.85.161.42 as permitted sender) X-PHP-List-Original-Sender: fw@f-ws.de X-Host-Fingerprint: 209.85.161.42 mail-fx0-f42.google.com Received: from [209.85.161.42] ([209.85.161.42:41191] helo=mail-fx0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F1/72-30014-613A3DD4 for ; Wed, 18 May 2011 06:44:41 -0400 Received: by fxm1 with SMTP id 1so1249018fxm.29 for ; Wed, 18 May 2011 03:44:35 -0700 (PDT) Received: by 10.223.149.70 with SMTP id s6mr2179359fav.66.1305715475661; Wed, 18 May 2011 03:44:35 -0700 (PDT) Received: from [172.16.7.25] ([212.48.107.10]) by mx.google.com with ESMTPS id d18sm565989fak.22.2011.05.18.03.44.34 (version=SSLv3 cipher=OTHER); Wed, 18 May 2011 03:44:34 -0700 (PDT) Message-ID: <4DD3A313.7030508@f-ws.de> Date: Wed, 18 May 2011 12:44:35 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 MIME-Version: 1.0 To: internals@lists.php.net Content-Type: multipart/mixed; boundary="------------060105080409000302020709" Subject: Patch - create socket by fd# From: fw@f-ws.de (Florian Wilkemeyer) --------------060105080409000302020709 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit 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 --------------060105080409000302020709 Content-Type: text/plain; name="sockets_add_create_from_fdno.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="sockets_add_create_from_fdno.diff" 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) --------------060105080409000302020709--