PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/sockets - sockets.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 671 872 76.9 %
Date: 2015-06-27 Functions: 36 41 87.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2015 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Chris Vandomelen <chrisv@b0rked.dhs.org>                    |
      16             :    |          Sterling Hughes  <sterling@php.net>                         |
      17             :    |          Jason Greene     <jason@php.net>                            |
      18             :    |          Gustavo Lopes    <cataphract@php.net>                       |
      19             :    | WinSock: Daniel Beulshausen <daniel@php4win.de>                      |
      20             :    +----------------------------------------------------------------------+
      21             :  */
      22             : 
      23             : /* $Id$ */
      24             : 
      25             : #ifdef HAVE_CONFIG_H
      26             : #include "config.h"
      27             : #endif
      28             : 
      29             : #include "php.h"
      30             : 
      31             : #include "php_network.h"
      32             : #include "ext/standard/file.h"
      33             : #include "ext/standard/info.h"
      34             : #include "php_ini.h"
      35             : #ifdef PHP_WIN32
      36             : # include "windows_common.h"
      37             : # include <win32/inet.h>
      38             : # include <windows.h>
      39             : # include <Ws2tcpip.h>
      40             : # include "php_sockets.h"
      41             : # include <win32/sockets.h>
      42             : #else
      43             : # include <sys/types.h>
      44             : # include <sys/socket.h>
      45             : # include <netdb.h>
      46             : # include <netinet/in.h>
      47             : # include <netinet/tcp.h>
      48             : # include <sys/un.h>
      49             : # include <arpa/inet.h>
      50             : # include <sys/time.h>
      51             : # include <unistd.h>
      52             : # include <errno.h>
      53             : # include <fcntl.h>
      54             : # include <signal.h>
      55             : # include <sys/uio.h>
      56             : # define IS_INVALID_SOCKET(a)   (a->bsd_socket < 0)
      57             : # define set_errno(a) (errno = a)
      58             : # include "php_sockets.h"
      59             : # if HAVE_IF_NAMETOINDEX
      60             : #  include <net/if.h>
      61             : # endif
      62             : #endif
      63             : 
      64             : #include <stddef.h>
      65             : 
      66             : #include "sockaddr_conv.h"
      67             : #include "multicast.h"
      68             : #include "sendrecvmsg.h"
      69             : 
      70             : ZEND_DECLARE_MODULE_GLOBALS(sockets)
      71             : 
      72             : #ifndef MSG_WAITALL
      73             : #ifdef LINUX
      74             : #define MSG_WAITALL 0x00000100
      75             : #else
      76             : #define MSG_WAITALL 0x00000000
      77             : #endif
      78             : #endif
      79             : 
      80             : #ifndef MSG_EOF
      81             : #ifdef MSG_FIN
      82             : #define MSG_EOF MSG_FIN
      83             : #endif
      84             : #endif
      85             : 
      86             : #ifndef SUN_LEN
      87             : #define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
      88             : #endif
      89             : 
      90             : #ifndef PF_INET
      91             : #define PF_INET AF_INET
      92             : #endif
      93             : 
      94             : #define PHP_NORMAL_READ 0x0001
      95             : #define PHP_BINARY_READ 0x0002
      96             : 
      97             : static int le_socket;
      98             : #define le_socket_name php_sockets_le_socket_name
      99             : 
     100             : /* {{{ arginfo */
     101             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_select, 0, 0, 4)
     102             :         ZEND_ARG_INFO(1, read_fds)
     103             :         ZEND_ARG_INFO(1, write_fds)
     104             :         ZEND_ARG_INFO(1, except_fds)
     105             :         ZEND_ARG_INFO(0, tv_sec)
     106             :         ZEND_ARG_INFO(0, tv_usec)
     107             : ZEND_END_ARG_INFO()
     108             : 
     109             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create_listen, 0, 0, 1)
     110             :         ZEND_ARG_INFO(0, port)
     111             :         ZEND_ARG_INFO(0, backlog)
     112             : ZEND_END_ARG_INFO()
     113             : 
     114             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_accept, 0, 0, 1)
     115             :         ZEND_ARG_INFO(0, socket)
     116             : ZEND_END_ARG_INFO()
     117             : 
     118             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_set_nonblock, 0, 0, 1)
     119             :         ZEND_ARG_INFO(0, socket)
     120             : ZEND_END_ARG_INFO()
     121             : 
     122             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_set_block, 0, 0, 1)
     123             :         ZEND_ARG_INFO(0, socket)
     124             : ZEND_END_ARG_INFO()
     125             : 
     126             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_listen, 0, 0, 1)
     127             :         ZEND_ARG_INFO(0, socket)
     128             :         ZEND_ARG_INFO(0, backlog)
     129             : ZEND_END_ARG_INFO()
     130             : 
     131             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_close, 0, 0, 1)
     132             :         ZEND_ARG_INFO(0, socket)
     133             : ZEND_END_ARG_INFO()
     134             : 
     135             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_write, 0, 0, 2)
     136             :         ZEND_ARG_INFO(0, socket)
     137             :         ZEND_ARG_INFO(0, buf)
     138             :         ZEND_ARG_INFO(0, length)
     139             : ZEND_END_ARG_INFO()
     140             : 
     141             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_read, 0, 0, 2)
     142             :         ZEND_ARG_INFO(0, socket)
     143             :         ZEND_ARG_INFO(0, length)
     144             :         ZEND_ARG_INFO(0, type)
     145             : ZEND_END_ARG_INFO()
     146             : 
     147             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_getsockname, 0, 0, 2)
     148             :         ZEND_ARG_INFO(0, socket)
     149             :         ZEND_ARG_INFO(1, addr)
     150             :         ZEND_ARG_INFO(1, port)
     151             : ZEND_END_ARG_INFO()
     152             : 
     153             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_getpeername, 0, 0, 2)
     154             :         ZEND_ARG_INFO(0, socket)
     155             :         ZEND_ARG_INFO(1, addr)
     156             :         ZEND_ARG_INFO(1, port)
     157             : ZEND_END_ARG_INFO()
     158             : 
     159             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create, 0, 0, 3)
     160             :         ZEND_ARG_INFO(0, domain)
     161             :         ZEND_ARG_INFO(0, type)
     162             :         ZEND_ARG_INFO(0, protocol)
     163             : ZEND_END_ARG_INFO()
     164             : 
     165             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_connect, 0, 0, 2)
     166             :         ZEND_ARG_INFO(0, socket)
     167             :         ZEND_ARG_INFO(0, addr)
     168             :         ZEND_ARG_INFO(0, port)
     169             : ZEND_END_ARG_INFO()
     170             : 
     171             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_strerror, 0, 0, 1)
     172             :         ZEND_ARG_INFO(0, errno)
     173             : ZEND_END_ARG_INFO()
     174             : 
     175             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_bind, 0, 0, 2)
     176             :         ZEND_ARG_INFO(0, socket)
     177             :         ZEND_ARG_INFO(0, addr)
     178             :         ZEND_ARG_INFO(0, port)
     179             : ZEND_END_ARG_INFO()
     180             : 
     181             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_recv, 0, 0, 4)
     182             :         ZEND_ARG_INFO(0, socket)
     183             :         ZEND_ARG_INFO(1, buf)
     184             :         ZEND_ARG_INFO(0, len)
     185             :         ZEND_ARG_INFO(0, flags)
     186             : ZEND_END_ARG_INFO()
     187             : 
     188             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_send, 0, 0, 4)
     189             :         ZEND_ARG_INFO(0, socket)
     190             :         ZEND_ARG_INFO(0, buf)
     191             :         ZEND_ARG_INFO(0, len)
     192             :         ZEND_ARG_INFO(0, flags)
     193             : ZEND_END_ARG_INFO()
     194             : 
     195             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_recvfrom, 0, 0, 5)
     196             :         ZEND_ARG_INFO(0, socket)
     197             :         ZEND_ARG_INFO(1, buf)
     198             :         ZEND_ARG_INFO(0, len)
     199             :         ZEND_ARG_INFO(0, flags)
     200             :         ZEND_ARG_INFO(1, name)
     201             :         ZEND_ARG_INFO(1, port)
     202             : ZEND_END_ARG_INFO()
     203             : 
     204             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_sendto, 0, 0, 5)
     205             :         ZEND_ARG_INFO(0, socket)
     206             :         ZEND_ARG_INFO(0, buf)
     207             :         ZEND_ARG_INFO(0, len)
     208             :         ZEND_ARG_INFO(0, flags)
     209             :         ZEND_ARG_INFO(0, addr)
     210             :         ZEND_ARG_INFO(0, port)
     211             : ZEND_END_ARG_INFO()
     212             : 
     213             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_get_option, 0, 0, 3)
     214             :         ZEND_ARG_INFO(0, socket)
     215             :         ZEND_ARG_INFO(0, level)
     216             :         ZEND_ARG_INFO(0, optname)
     217             : ZEND_END_ARG_INFO()
     218             : 
     219             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_set_option, 0, 0, 4)
     220             :         ZEND_ARG_INFO(0, socket)
     221             :         ZEND_ARG_INFO(0, level)
     222             :         ZEND_ARG_INFO(0, optname)
     223             :         ZEND_ARG_INFO(0, optval)
     224             : ZEND_END_ARG_INFO()
     225             : 
     226             : #ifdef HAVE_SOCKETPAIR
     227             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create_pair, 0, 0, 4)
     228             :         ZEND_ARG_INFO(0, domain)
     229             :         ZEND_ARG_INFO(0, type)
     230             :         ZEND_ARG_INFO(0, protocol)
     231             :         ZEND_ARG_INFO(1, fd)
     232             : ZEND_END_ARG_INFO()
     233             : #endif
     234             : 
     235             : #ifdef HAVE_SHUTDOWN
     236             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_shutdown, 0, 0, 1)
     237             :         ZEND_ARG_INFO(0, socket)
     238             :         ZEND_ARG_INFO(0, how)
     239             : ZEND_END_ARG_INFO()
     240             : #endif
     241             : 
     242             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_last_error, 0, 0, 0)
     243             :         ZEND_ARG_INFO(0, socket)
     244             : ZEND_END_ARG_INFO()
     245             : 
     246             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_clear_error, 0, 0, 0)
     247             :         ZEND_ARG_INFO(0, socket)
     248             : ZEND_END_ARG_INFO()
     249             : 
     250             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_import_stream, 0, 0, 1)
     251             :         ZEND_ARG_INFO(0, stream)
     252             : ZEND_END_ARG_INFO()
     253             : 
     254             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_sendmsg, 0, 0, 3)
     255             :         ZEND_ARG_INFO(0, socket)
     256             :         ZEND_ARG_INFO(0, msghdr)
     257             :         ZEND_ARG_INFO(0, flags)
     258             : ZEND_END_ARG_INFO()
     259             : 
     260             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_recvmsg, 0, 0, 3)
     261             :         ZEND_ARG_INFO(0, socket)
     262             :         ZEND_ARG_INFO(1, msghdr)
     263             :         ZEND_ARG_INFO(0, flags)
     264             : ZEND_END_ARG_INFO()
     265             : 
     266             : ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_cmsg_space, 0, 0, 2)
     267             :         ZEND_ARG_INFO(0, level)
     268             :         ZEND_ARG_INFO(0, type)
     269             : ZEND_END_ARG_INFO()
     270             : /* }}} */
     271             : 
     272             : static PHP_GINIT_FUNCTION(sockets);
     273             : static PHP_MINIT_FUNCTION(sockets);
     274             : static PHP_MSHUTDOWN_FUNCTION(sockets);
     275             : static PHP_MINFO_FUNCTION(sockets);
     276             : static PHP_RSHUTDOWN_FUNCTION(sockets);
     277             : 
     278             : PHP_FUNCTION(socket_select);
     279             : PHP_FUNCTION(socket_create_listen);
     280             : #ifdef HAVE_SOCKETPAIR
     281             : PHP_FUNCTION(socket_create_pair);
     282             : #endif
     283             : PHP_FUNCTION(socket_accept);
     284             : PHP_FUNCTION(socket_set_nonblock);
     285             : PHP_FUNCTION(socket_set_block);
     286             : PHP_FUNCTION(socket_listen);
     287             : PHP_FUNCTION(socket_close);
     288             : PHP_FUNCTION(socket_write);
     289             : PHP_FUNCTION(socket_read);
     290             : PHP_FUNCTION(socket_getsockname);
     291             : PHP_FUNCTION(socket_getpeername);
     292             : PHP_FUNCTION(socket_create);
     293             : PHP_FUNCTION(socket_connect);
     294             : PHP_FUNCTION(socket_strerror);
     295             : PHP_FUNCTION(socket_bind);
     296             : PHP_FUNCTION(socket_recv);
     297             : PHP_FUNCTION(socket_send);
     298             : PHP_FUNCTION(socket_recvfrom);
     299             : PHP_FUNCTION(socket_sendto);
     300             : PHP_FUNCTION(socket_get_option);
     301             : PHP_FUNCTION(socket_set_option);
     302             : #ifdef HAVE_SHUTDOWN
     303             : PHP_FUNCTION(socket_shutdown);
     304             : #endif
     305             : PHP_FUNCTION(socket_last_error);
     306             : PHP_FUNCTION(socket_clear_error);
     307             : PHP_FUNCTION(socket_import_stream);
     308             : 
     309             : /* {{{ sockets_functions[]
     310             :  */
     311             : const zend_function_entry sockets_functions[] = {
     312             :         PHP_FE(socket_select,                   arginfo_socket_select)
     313             :         PHP_FE(socket_create,                   arginfo_socket_create)
     314             :         PHP_FE(socket_create_listen,    arginfo_socket_create_listen)
     315             : #ifdef HAVE_SOCKETPAIR
     316             :         PHP_FE(socket_create_pair,              arginfo_socket_create_pair)
     317             : #endif
     318             :         PHP_FE(socket_accept,                   arginfo_socket_accept)
     319             :         PHP_FE(socket_set_nonblock,             arginfo_socket_set_nonblock)
     320             :         PHP_FE(socket_set_block,                arginfo_socket_set_block)
     321             :         PHP_FE(socket_listen,                   arginfo_socket_listen)
     322             :         PHP_FE(socket_close,                    arginfo_socket_close)
     323             :         PHP_FE(socket_write,                    arginfo_socket_write)
     324             :         PHP_FE(socket_read,                             arginfo_socket_read)
     325             :         PHP_FE(socket_getsockname,              arginfo_socket_getsockname)
     326             :         PHP_FE(socket_getpeername,              arginfo_socket_getpeername)
     327             :         PHP_FE(socket_connect,                  arginfo_socket_connect)
     328             :         PHP_FE(socket_strerror,                 arginfo_socket_strerror)
     329             :         PHP_FE(socket_bind,                             arginfo_socket_bind)
     330             :         PHP_FE(socket_recv,                             arginfo_socket_recv)
     331             :         PHP_FE(socket_send,                             arginfo_socket_send)
     332             :         PHP_FE(socket_recvfrom,                 arginfo_socket_recvfrom)
     333             :         PHP_FE(socket_sendto,                   arginfo_socket_sendto)
     334             :         PHP_FE(socket_get_option,               arginfo_socket_get_option)
     335             :         PHP_FE(socket_set_option,               arginfo_socket_set_option)
     336             : #ifdef HAVE_SHUTDOWN
     337             :         PHP_FE(socket_shutdown,                 arginfo_socket_shutdown)
     338             : #endif
     339             :         PHP_FE(socket_last_error,               arginfo_socket_last_error)
     340             :         PHP_FE(socket_clear_error,              arginfo_socket_clear_error)
     341             :         PHP_FE(socket_import_stream,    arginfo_socket_import_stream)
     342             :         PHP_FE(socket_sendmsg,                  arginfo_socket_sendmsg)
     343             :         PHP_FE(socket_recvmsg,                  arginfo_socket_recvmsg)
     344             :         PHP_FE(socket_cmsg_space,               arginfo_socket_cmsg_space)
     345             : 
     346             :         /* for downwards compatibility */
     347             :         PHP_FALIAS(socket_getopt, socket_get_option, arginfo_socket_get_option)
     348             :         PHP_FALIAS(socket_setopt, socket_set_option, arginfo_socket_set_option)
     349             : 
     350             :         PHP_FE_END
     351             : };
     352             : /* }}} */
     353             : 
     354             : zend_module_entry sockets_module_entry = {
     355             :         STANDARD_MODULE_HEADER,
     356             :         "sockets",
     357             :         sockets_functions,
     358             :         PHP_MINIT(sockets),
     359             :         PHP_MSHUTDOWN(sockets),
     360             :         NULL,
     361             :         PHP_RSHUTDOWN(sockets),
     362             :         PHP_MINFO(sockets),
     363             :         PHP_SOCKETS_VERSION,
     364             :         PHP_MODULE_GLOBALS(sockets),
     365             :         PHP_GINIT(sockets),
     366             :         NULL,
     367             :         NULL,
     368             :         STANDARD_MODULE_PROPERTIES_EX
     369             : };
     370             : 
     371             : 
     372             : #ifdef COMPILE_DL_SOCKETS
     373             : #ifdef ZTS
     374             :         ZEND_TSRMLS_CACHE_DEFINE();
     375             : #endif
     376             : ZEND_GET_MODULE(sockets)
     377             : #endif
     378             : 
     379             : /* inet_ntop should be used instead of inet_ntoa */
     380             : int inet_ntoa_lock = 0;
     381             : 
     382          12 : PHP_SOCKETS_API int php_sockets_le_socket(void) /* {{{ */
     383             : {
     384          12 :         return le_socket;
     385             : }
     386             : /* }}} */
     387             : 
     388             : /* allocating function to make programming errors due to uninitialized fields
     389             :  * less likely */
     390         100 : PHP_SOCKETS_API php_socket *php_create_socket(void) /* {{{ */
     391             : {
     392         100 :         php_socket *php_sock = emalloc(sizeof(php_socket));
     393             : 
     394         100 :         php_sock->bsd_socket = -1; /* invalid socket */
     395         100 :         php_sock->type                = PF_UNSPEC;
     396         100 :         php_sock->error               = 0;
     397         100 :         php_sock->blocking    = 1;
     398         100 :         ZVAL_UNDEF(&php_sock->zstream);
     399             : 
     400         100 :         return php_sock;
     401             : }
     402             : /* }}} */
     403             : 
     404          85 : PHP_SOCKETS_API void php_destroy_socket(zend_resource *rsrc) /* {{{ */
     405             : {
     406          85 :         php_socket *php_sock = rsrc->ptr;
     407             : 
     408         170 :         if (Z_ISUNDEF(php_sock->zstream)) {
     409          78 :                 if (!IS_INVALID_SOCKET(php_sock)) {
     410          78 :                         close(php_sock->bsd_socket);
     411             :                 }
     412             :         } else {
     413           7 :                 zval_ptr_dtor(&php_sock->zstream);
     414             :         }
     415          85 :         efree(php_sock);
     416          85 : }
     417             : /* }}} */
     418             : 
     419          13 : static int php_open_listen_sock(php_socket **php_sock, int port, int backlog) /* {{{ */
     420             : {
     421             :         struct sockaddr_in  la;
     422             :         struct hostent          *hp;
     423          13 :         php_socket                      *sock = php_create_socket();
     424             : 
     425          13 :         *php_sock = sock;
     426             : 
     427             : #ifndef PHP_WIN32
     428          13 :         if ((hp = gethostbyname("0.0.0.0")) == NULL) {
     429             : #else
     430             :         if ((hp = gethostbyname("localhost")) == NULL) {
     431             : #endif
     432           0 :                 efree(sock);
     433           0 :                 return 0;
     434             :         }
     435             : 
     436          13 :         memcpy((char *) &la.sin_addr, hp->h_addr, hp->h_length);
     437          13 :         la.sin_family = hp->h_addrtype;
     438          13 :         la.sin_port = htons((unsigned short) port);
     439             : 
     440          13 :         sock->bsd_socket = socket(PF_INET, SOCK_STREAM, 0);
     441          13 :         sock->blocking = 1;
     442             : 
     443          13 :         if (IS_INVALID_SOCKET(sock)) {
     444           0 :                 PHP_SOCKET_ERROR(sock, "unable to create listening socket", errno);
     445           0 :                 efree(sock);
     446           0 :                 return 0;
     447             :         }
     448             : 
     449          13 :         sock->type = PF_INET;
     450             : 
     451          13 :         if (bind(sock->bsd_socket, (struct sockaddr *)&la, sizeof(la)) != 0) {
     452           2 :                 PHP_SOCKET_ERROR(sock, "unable to bind to given address", errno);
     453           2 :                 close(sock->bsd_socket);
     454           2 :                 efree(sock);
     455           2 :                 return 0;
     456             :         }
     457             : 
     458          11 :         if (listen(sock->bsd_socket, backlog) != 0) {
     459           0 :                 PHP_SOCKET_ERROR(sock, "unable to listen on socket", errno);
     460           0 :                 close(sock->bsd_socket);
     461           0 :                 efree(sock);
     462           0 :                 return 0;
     463             :         }
     464             : 
     465          11 :         return 1;
     466             : }
     467             : /* }}} */
     468             : 
     469           6 : static int php_accept_connect(php_socket *in_sock, php_socket **new_sock, struct sockaddr *la, socklen_t *la_len) /* {{{ */
     470             : {
     471           6 :         php_socket      *out_sock = php_create_socket();
     472             : 
     473           6 :         *new_sock = out_sock;
     474             : 
     475           6 :         out_sock->bsd_socket = accept(in_sock->bsd_socket, la, la_len);
     476             : 
     477           6 :         if (IS_INVALID_SOCKET(out_sock)) {
     478           0 :                 PHP_SOCKET_ERROR(out_sock, "unable to accept incoming connection", errno);
     479           0 :                 efree(out_sock);
     480           0 :                 return 0;
     481             :         }
     482             : 
     483           6 :         out_sock->error = 0;
     484           6 :         out_sock->blocking = 1;
     485           6 :         out_sock->type = la->sa_family;
     486             : 
     487           6 :         return 1;
     488             : }
     489             : /* }}} */
     490             : 
     491             : /* {{{ php_read -- wrapper around read() so that it only reads to a \r or \n. */
     492           0 : static int php_read(php_socket *sock, void *buf, size_t maxlen, int flags)
     493             : {
     494           0 :         int m = 0;
     495           0 :         size_t n = 0;
     496           0 :         int no_read = 0;
     497           0 :         int nonblock = 0;
     498           0 :         char *t = (char *) buf;
     499             : 
     500             : #ifndef PHP_WIN32
     501           0 :         m = fcntl(sock->bsd_socket, F_GETFL);
     502           0 :         if (m < 0) {
     503           0 :                 return m;
     504             :         }
     505           0 :         nonblock = (m & O_NONBLOCK);
     506           0 :         m = 0;
     507             : #else
     508             :         nonblock = !sock->blocking;
     509             : #endif
     510           0 :         set_errno(0);
     511             : 
     512           0 :         *t = '\0';
     513           0 :         while (*t != '\n' && *t != '\r' && n < maxlen) {
     514           0 :                 if (m > 0) {
     515           0 :                         t++;
     516           0 :                         n++;
     517           0 :                 } else if (m == 0) {
     518           0 :                         no_read++;
     519           0 :                         if (nonblock && no_read >= 2) {
     520           0 :                                 return n;
     521             :                                 /* The first pass, m always is 0, so no_read becomes 1
     522             :                                  * in the first pass. no_read becomes 2 in the second pass,
     523             :                                  * and if this is nonblocking, we should return.. */
     524             :                         }
     525             : 
     526           0 :                         if (no_read > 200) {
     527           0 :                                 set_errno(ECONNRESET);
     528           0 :                                 return -1;
     529             :                         }
     530             :                 }
     531             : 
     532           0 :                 if (n < maxlen) {
     533           0 :                         m = recv(sock->bsd_socket, (void *) t, 1, flags);
     534             :                 }
     535             : 
     536           0 :                 if (errno != 0 && errno != ESPIPE && errno != EAGAIN) {
     537           0 :                         return -1;
     538             :                 }
     539             : 
     540           0 :                 set_errno(0);
     541             :         }
     542             : 
     543           0 :         if (n < maxlen) {
     544           0 :                 n++;
     545             :                 /* The only reasons it makes it to here is
     546             :                  * if '\n' or '\r' are encountered. So, increase
     547             :                  * the return by 1 to make up for the lack of the
     548             :                  * '\n' or '\r' in the count (since read() takes
     549             :                  * place at the end of the loop..) */
     550             :         }
     551             : 
     552           0 :         return n;
     553             : }
     554             : /* }}} */
     555             : 
     556         148 : char *sockets_strerror(int error) /* {{{ */
     557             : {
     558             :         const char *buf;
     559             : 
     560             : #ifndef PHP_WIN32
     561         148 :         if (error < -10000) {
     562           0 :                 error = -error - 10000;
     563             : 
     564             : #ifdef HAVE_HSTRERROR
     565           0 :                 buf = hstrerror(error);
     566             : #else
     567             :                 {
     568             :                         if (SOCKETS_G(strerror_buf)) {
     569             :                                 efree(SOCKETS_G(strerror_buf));
     570             :                         }
     571             : 
     572             :                         spprintf(&(SOCKETS_G(strerror_buf)), 0, "Host lookup error %d", error);
     573             :                         buf = SOCKETS_G(strerror_buf);
     574             :                 }
     575             : #endif
     576             :         } else {
     577         148 :                 buf = strerror(error);
     578             :         }
     579             : #else
     580             :         {
     581             :                 LPTSTR tmp = NULL;
     582             :                 buf = NULL;
     583             : 
     584             :                 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
     585             :                         NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &tmp, 0, NULL)
     586             :                 ) {
     587             :                         if (SOCKETS_G(strerror_buf)) {
     588             :                                 efree(SOCKETS_G(strerror_buf));
     589             :                         }
     590             : 
     591             :                         SOCKETS_G(strerror_buf) = estrdup(tmp);
     592             :                         LocalFree(tmp);
     593             : 
     594             :                         buf = SOCKETS_G(strerror_buf);
     595             :                 }
     596             :         }
     597             : #endif
     598             : 
     599         148 :         return (buf ? (char *) buf : "");
     600             : }
     601             : /* }}} */
     602             : 
     603             : /* {{{ PHP_GINIT_FUNCTION */
     604       21049 : static PHP_GINIT_FUNCTION(sockets)
     605             : {
     606             : #if defined(COMPILE_DL_SOCKETS) && defined(ZTS)
     607             :         ZEND_TSRMLS_CACHE_UPDATE();
     608             : #endif
     609       21049 :         sockets_globals->last_error = 0;
     610       21049 :         sockets_globals->strerror_buf = NULL;
     611       21049 : }
     612             : /* }}} */
     613             : 
     614             : /* {{{ PHP_MINIT_FUNCTION
     615             :  */
     616       21049 : static PHP_MINIT_FUNCTION(sockets)
     617             : {
     618             : #if defined(COMPILE_DL_SOCKETS) && defined(ZTS)
     619             :         ZEND_TSRMLS_CACHE_UPDATE();
     620             : #endif
     621       21049 :         le_socket = zend_register_list_destructors_ex(php_destroy_socket, NULL, le_socket_name, module_number);
     622             : 
     623       21049 :         REGISTER_LONG_CONSTANT("AF_UNIX",             AF_UNIX,                CONST_CS | CONST_PERSISTENT);
     624       21049 :         REGISTER_LONG_CONSTANT("AF_INET",             AF_INET,                CONST_CS | CONST_PERSISTENT);
     625             : #if HAVE_IPV6
     626       21049 :         REGISTER_LONG_CONSTANT("AF_INET6",            AF_INET6,               CONST_CS | CONST_PERSISTENT);
     627             : #endif
     628       21049 :         REGISTER_LONG_CONSTANT("SOCK_STREAM", SOCK_STREAM,    CONST_CS | CONST_PERSISTENT);
     629       21049 :         REGISTER_LONG_CONSTANT("SOCK_DGRAM",  SOCK_DGRAM,             CONST_CS | CONST_PERSISTENT);
     630       21049 :         REGISTER_LONG_CONSTANT("SOCK_RAW",            SOCK_RAW,               CONST_CS | CONST_PERSISTENT);
     631       21049 :         REGISTER_LONG_CONSTANT("SOCK_SEQPACKET",SOCK_SEQPACKET, CONST_CS | CONST_PERSISTENT);
     632       21049 :         REGISTER_LONG_CONSTANT("SOCK_RDM",            SOCK_RDM,               CONST_CS | CONST_PERSISTENT);
     633             : 
     634       21049 :         REGISTER_LONG_CONSTANT("MSG_OOB",             MSG_OOB,                CONST_CS | CONST_PERSISTENT);
     635       21049 :         REGISTER_LONG_CONSTANT("MSG_WAITALL", MSG_WAITALL,    CONST_CS | CONST_PERSISTENT);
     636       21049 :         REGISTER_LONG_CONSTANT("MSG_CTRUNC",  MSG_CTRUNC,             CONST_CS | CONST_PERSISTENT);
     637       21049 :         REGISTER_LONG_CONSTANT("MSG_TRUNC",           MSG_TRUNC,              CONST_CS | CONST_PERSISTENT);
     638       21049 :         REGISTER_LONG_CONSTANT("MSG_PEEK",            MSG_PEEK,               CONST_CS | CONST_PERSISTENT);
     639       21049 :         REGISTER_LONG_CONSTANT("MSG_DONTROUTE", MSG_DONTROUTE,        CONST_CS | CONST_PERSISTENT);
     640             : #ifdef MSG_EOR
     641       21049 :         REGISTER_LONG_CONSTANT("MSG_EOR",             MSG_EOR,                CONST_CS | CONST_PERSISTENT);
     642             : #endif
     643             : #ifdef MSG_EOF
     644       21049 :         REGISTER_LONG_CONSTANT("MSG_EOF",             MSG_EOF,                CONST_CS | CONST_PERSISTENT);
     645             : #endif
     646             : 
     647             : #ifdef MSG_CONFIRM
     648       21049 :         REGISTER_LONG_CONSTANT("MSG_CONFIRM", MSG_CONFIRM,    CONST_CS | CONST_PERSISTENT);
     649             : #endif
     650             : #ifdef MSG_ERRQUEUE
     651       21049 :         REGISTER_LONG_CONSTANT("MSG_ERRQUEUE",        MSG_ERRQUEUE,   CONST_CS | CONST_PERSISTENT);
     652             : #endif
     653             : #ifdef MSG_NOSIGNAL
     654       21049 :         REGISTER_LONG_CONSTANT("MSG_NOSIGNAL",        MSG_NOSIGNAL,   CONST_CS | CONST_PERSISTENT);
     655             : #endif
     656             : #ifdef MSG_DONTWAIT
     657       21049 :         REGISTER_LONG_CONSTANT("MSG_DONTWAIT",        MSG_DONTWAIT,   CONST_CS | CONST_PERSISTENT);
     658             : #endif
     659             : #ifdef MSG_MORE
     660       21049 :         REGISTER_LONG_CONSTANT("MSG_MORE",            MSG_MORE,               CONST_CS | CONST_PERSISTENT);
     661             : #endif
     662             : #ifdef MSG_WAITFORONE
     663       21049 :         REGISTER_LONG_CONSTANT("MSG_WAITFORONE",MSG_WAITFORONE,       CONST_CS | CONST_PERSISTENT);
     664             : #endif
     665             : #ifdef MSG_CMSG_CLOEXEC
     666       21049 :         REGISTER_LONG_CONSTANT("MSG_CMSG_CLOEXEC",MSG_CMSG_CLOEXEC,CONST_CS | CONST_PERSISTENT);
     667             : #endif
     668             : 
     669       21049 :         REGISTER_LONG_CONSTANT("SO_DEBUG",            SO_DEBUG,               CONST_CS | CONST_PERSISTENT);
     670       21049 :         REGISTER_LONG_CONSTANT("SO_REUSEADDR",        SO_REUSEADDR,   CONST_CS | CONST_PERSISTENT);
     671             : #ifdef SO_REUSEPORT
     672       21049 :         REGISTER_LONG_CONSTANT("SO_REUSEPORT",        SO_REUSEPORT,   CONST_CS | CONST_PERSISTENT);
     673             : #endif
     674       21049 :         REGISTER_LONG_CONSTANT("SO_KEEPALIVE",        SO_KEEPALIVE,   CONST_CS | CONST_PERSISTENT);
     675       21049 :         REGISTER_LONG_CONSTANT("SO_DONTROUTE",        SO_DONTROUTE,   CONST_CS | CONST_PERSISTENT);
     676       21049 :         REGISTER_LONG_CONSTANT("SO_LINGER",           SO_LINGER,              CONST_CS | CONST_PERSISTENT);
     677       21049 :         REGISTER_LONG_CONSTANT("SO_BROADCAST",        SO_BROADCAST,   CONST_CS | CONST_PERSISTENT);
     678       21049 :         REGISTER_LONG_CONSTANT("SO_OOBINLINE",        SO_OOBINLINE,   CONST_CS | CONST_PERSISTENT);
     679       21049 :         REGISTER_LONG_CONSTANT("SO_SNDBUF",           SO_SNDBUF,              CONST_CS | CONST_PERSISTENT);
     680       21049 :         REGISTER_LONG_CONSTANT("SO_RCVBUF",           SO_RCVBUF,              CONST_CS | CONST_PERSISTENT);
     681       21049 :         REGISTER_LONG_CONSTANT("SO_SNDLOWAT", SO_SNDLOWAT,    CONST_CS | CONST_PERSISTENT);
     682       21049 :         REGISTER_LONG_CONSTANT("SO_RCVLOWAT", SO_RCVLOWAT,    CONST_CS | CONST_PERSISTENT);
     683       21049 :         REGISTER_LONG_CONSTANT("SO_SNDTIMEO", SO_SNDTIMEO,    CONST_CS | CONST_PERSISTENT);
     684       21049 :         REGISTER_LONG_CONSTANT("SO_RCVTIMEO", SO_RCVTIMEO,    CONST_CS | CONST_PERSISTENT);
     685       21049 :         REGISTER_LONG_CONSTANT("SO_TYPE",             SO_TYPE,                CONST_CS | CONST_PERSISTENT);
     686             : #ifdef SO_FAMILY
     687             :         REGISTER_LONG_CONSTANT("SO_FAMILY",           SO_FAMILY,              CONST_CS | CONST_PERSISTENT);
     688             : #endif
     689       21049 :         REGISTER_LONG_CONSTANT("SO_ERROR",            SO_ERROR,               CONST_CS | CONST_PERSISTENT);
     690             : #ifdef SO_BINDTODEVICE
     691       21049 :         REGISTER_LONG_CONSTANT("SO_BINDTODEVICE",       SO_BINDTODEVICE,        CONST_CS | CONST_PERSISTENT);
     692             : #endif
     693       21049 :         REGISTER_LONG_CONSTANT("SOL_SOCKET",  SOL_SOCKET,             CONST_CS | CONST_PERSISTENT);
     694       21049 :         REGISTER_LONG_CONSTANT("SOMAXCONN",           SOMAXCONN,              CONST_CS | CONST_PERSISTENT);
     695             : #ifdef TCP_NODELAY
     696       21049 :         REGISTER_LONG_CONSTANT("TCP_NODELAY",   TCP_NODELAY,    CONST_CS | CONST_PERSISTENT);
     697             : #endif
     698       21049 :         REGISTER_LONG_CONSTANT("PHP_NORMAL_READ", PHP_NORMAL_READ, CONST_CS | CONST_PERSISTENT);
     699       21049 :         REGISTER_LONG_CONSTANT("PHP_BINARY_READ", PHP_BINARY_READ, CONST_CS | CONST_PERSISTENT);
     700             : 
     701       21049 :         REGISTER_LONG_CONSTANT("MCAST_JOIN_GROUP",                    PHP_MCAST_JOIN_GROUP,                   CONST_CS | CONST_PERSISTENT);
     702       21049 :         REGISTER_LONG_CONSTANT("MCAST_LEAVE_GROUP",                   PHP_MCAST_LEAVE_GROUP,                  CONST_CS | CONST_PERSISTENT);
     703             : #ifdef HAS_MCAST_EXT
     704       21049 :         REGISTER_LONG_CONSTANT("MCAST_BLOCK_SOURCE",          PHP_MCAST_BLOCK_SOURCE,                 CONST_CS | CONST_PERSISTENT);
     705       21049 :         REGISTER_LONG_CONSTANT("MCAST_UNBLOCK_SOURCE",                PHP_MCAST_UNBLOCK_SOURCE,               CONST_CS | CONST_PERSISTENT);
     706       21049 :         REGISTER_LONG_CONSTANT("MCAST_JOIN_SOURCE_GROUP",     PHP_MCAST_JOIN_SOURCE_GROUP,    CONST_CS | CONST_PERSISTENT);
     707       21049 :         REGISTER_LONG_CONSTANT("MCAST_LEAVE_SOURCE_GROUP",    PHP_MCAST_LEAVE_SOURCE_GROUP,   CONST_CS | CONST_PERSISTENT);
     708             : #endif
     709             : 
     710       21049 :         REGISTER_LONG_CONSTANT("IP_MULTICAST_IF",                     IP_MULTICAST_IF,                CONST_CS | CONST_PERSISTENT);
     711       21049 :         REGISTER_LONG_CONSTANT("IP_MULTICAST_TTL",                    IP_MULTICAST_TTL,               CONST_CS | CONST_PERSISTENT);
     712       21049 :         REGISTER_LONG_CONSTANT("IP_MULTICAST_LOOP",                   IP_MULTICAST_LOOP,              CONST_CS | CONST_PERSISTENT);
     713             : #if HAVE_IPV6
     714       21049 :         REGISTER_LONG_CONSTANT("IPV6_MULTICAST_IF",                   IPV6_MULTICAST_IF,              CONST_CS | CONST_PERSISTENT);
     715       21049 :         REGISTER_LONG_CONSTANT("IPV6_MULTICAST_HOPS",         IPV6_MULTICAST_HOPS,    CONST_CS | CONST_PERSISTENT);
     716       21049 :         REGISTER_LONG_CONSTANT("IPV6_MULTICAST_LOOP",         IPV6_MULTICAST_LOOP,    CONST_CS | CONST_PERSISTENT);
     717             : #endif
     718             : 
     719             : #ifndef WIN32
     720             : # include "unix_socket_constants.h"
     721             : #else
     722             : # include "win32_socket_constants.h"
     723             : #endif
     724             : 
     725       21049 :         REGISTER_LONG_CONSTANT("IPPROTO_IP",  IPPROTO_IP,             CONST_CS | CONST_PERSISTENT);
     726             : #if HAVE_IPV6
     727       21049 :         REGISTER_LONG_CONSTANT("IPPROTO_IPV6",        IPPROTO_IPV6,   CONST_CS | CONST_PERSISTENT);
     728             : #endif
     729             : 
     730       21049 :         REGISTER_LONG_CONSTANT("SOL_TCP",             IPPROTO_TCP,    CONST_CS | CONST_PERSISTENT);
     731       21049 :         REGISTER_LONG_CONSTANT("SOL_UDP",             IPPROTO_UDP,    CONST_CS | CONST_PERSISTENT);
     732             : 
     733             : #if HAVE_IPV6
     734       21049 :         REGISTER_LONG_CONSTANT("IPV6_UNICAST_HOPS",                   IPV6_UNICAST_HOPS,      CONST_CS | CONST_PERSISTENT);
     735             : #endif
     736             : 
     737       21049 :         php_socket_sendrecvmsg_init(INIT_FUNC_ARGS_PASSTHRU);
     738             : 
     739       21049 :         return SUCCESS;
     740             : }
     741             : /* }}} */
     742             : 
     743             : /* {{{ PHP_MSHUTDOWN_FUNCTION
     744             :  */
     745       21085 : static PHP_MSHUTDOWN_FUNCTION(sockets)
     746             : {
     747       21085 :         php_socket_sendrecvmsg_shutdown(SHUTDOWN_FUNC_ARGS_PASSTHRU);
     748             : 
     749       21085 :         return SUCCESS;
     750             : }
     751             : /* }}} */
     752             : 
     753             : /* {{{ PHP_MINFO_FUNCTION
     754             :  */
     755         142 : static PHP_MINFO_FUNCTION(sockets)
     756             : {
     757         142 :         php_info_print_table_start();
     758         142 :         php_info_print_table_row(2, "Sockets Support", "enabled");
     759         142 :         php_info_print_table_end();
     760         142 : }
     761             : /* }}} */
     762             : 
     763             : /* {{{ PHP_RSHUTDOWN_FUNCTION */
     764       21044 : static PHP_RSHUTDOWN_FUNCTION(sockets)
     765             : {
     766       21044 :         if (SOCKETS_G(strerror_buf)) {
     767           0 :                 efree(SOCKETS_G(strerror_buf));
     768           0 :                 SOCKETS_G(strerror_buf) = NULL;
     769             :         }
     770             : 
     771       21044 :         return SUCCESS;
     772             : }
     773             : /* }}} */
     774             : 
     775          28 : static int php_sock_array_to_fd_set(zval *sock_array, fd_set *fds, PHP_SOCKET *max_fd) /* {{{ */
     776             : {
     777             :         zval            *element;
     778             :         php_socket      *php_sock;
     779          28 :         int                     num = 0;
     780             : 
     781          28 :         if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
     782             : 
     783          60 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(sock_array), element) {
     784          16 :                 php_sock = (php_socket*) zend_fetch_resource_ex(element, le_socket_name, le_socket);
     785          16 :                 if (!php_sock) continue; /* If element is not a resource, skip it */
     786             : 
     787          16 :                 PHP_SAFE_FD_SET(php_sock->bsd_socket, fds);
     788          16 :                 if (php_sock->bsd_socket > *max_fd) {
     789          16 :                         *max_fd = php_sock->bsd_socket;
     790             :                 }
     791          16 :                 num++;
     792             :         } ZEND_HASH_FOREACH_END();
     793             : 
     794          28 :         return num ? 1 : 0;
     795             : }
     796             : /* }}} */
     797             : 
     798          27 : static int php_sock_array_from_fd_set(zval *sock_array, fd_set *fds) /* {{{ */
     799             : {
     800             :         zval            *element;
     801             :         zval            *dest_element;
     802             :         php_socket      *php_sock;
     803             :         zval            new_hash;
     804          27 :         int                     num = 0;
     805             :         zend_ulong       num_key;
     806             :         zend_string *key;
     807             : 
     808          27 :         if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
     809             : 
     810          27 :         array_init(&new_hash);
     811          55 :         ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(sock_array), num_key, key, element) {
     812          14 :                 php_sock = (php_socket*) zend_fetch_resource_ex(element, le_socket_name, le_socket);
     813          14 :                 if (!php_sock) continue; /* If element is not a resource, skip it */
     814             : 
     815          14 :                 if (PHP_SAFE_FD_ISSET(php_sock->bsd_socket, fds)) {
     816             :                         /* Add fd to new array */
     817           7 :                         if (key) {
     818           0 :                                 dest_element = zend_hash_add(Z_ARRVAL(new_hash), key, element);
     819             :                         } else {
     820           7 :                                 dest_element = zend_hash_index_update(Z_ARRVAL(new_hash), num_key, element);
     821             :                         }
     822           7 :                         if (dest_element) {
     823             :                                 Z_ADDREF_P(dest_element);
     824             :                         }
     825             :                 }
     826          14 :                 num++;
     827             :         } ZEND_HASH_FOREACH_END();
     828             : 
     829             :         /* Destroy old array, add new one */
     830          27 :         zval_ptr_dtor(sock_array);
     831             : 
     832          27 :         ZVAL_COPY_VALUE(sock_array, &new_hash);
     833             : 
     834          27 :         return num ? 1 : 0;
     835             : }
     836             : /* }}} */
     837             : 
     838             : /* {{{ proto int socket_select(array &read_fds, array &write_fds, array &except_fds, int tv_sec[, int tv_usec])
     839             :    Runs the select() system call on the sets mentioned with a timeout specified by tv_sec and tv_usec */
     840          14 : PHP_FUNCTION(socket_select)
     841             : {
     842             :         zval                    *r_array, *w_array, *e_array, *sec;
     843             :         struct timeval  tv;
     844          14 :         struct timeval *tv_p = NULL;
     845             :         fd_set                  rfds, wfds, efds;
     846          14 :         PHP_SOCKET              max_fd = 0;
     847          14 :         int                             retval, sets = 0;
     848          14 :         zend_long                       usec = 0;
     849             : 
     850          14 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "a/!a/!a/!z!|l", &r_array, &w_array, &e_array, &sec, &usec) == FAILURE) {
     851           1 :                 return;
     852             :         }
     853             : 
     854          13 :         FD_ZERO(&rfds);
     855          13 :         FD_ZERO(&wfds);
     856          13 :         FD_ZERO(&efds);
     857             : 
     858          13 :         if (r_array != NULL) sets += php_sock_array_to_fd_set(r_array, &rfds, &max_fd);
     859          13 :         if (w_array != NULL) sets += php_sock_array_to_fd_set(w_array, &wfds, &max_fd);
     860          13 :         if (e_array != NULL) sets += php_sock_array_to_fd_set(e_array, &efds, &max_fd);
     861             : 
     862          13 :         if (!sets) {
     863           1 :                 php_error_docref(NULL, E_WARNING, "no resource arrays were passed to select");
     864           1 :                 RETURN_FALSE;
     865             :         }
     866             : 
     867          12 :         PHP_SAFE_MAX_FD(max_fd, 0); /* someone needs to make this look more like stream_socket_select */
     868             : 
     869             :         /* If seconds is not set to null, build the timeval, else we wait indefinitely */
     870          12 :         if (sec != NULL) {
     871             :                 zval tmp;
     872             : 
     873          24 :                 if (Z_TYPE_P(sec) != IS_LONG) {
     874           1 :                         tmp = *sec;
     875             :                         zval_copy_ctor(&tmp);
     876           1 :                         convert_to_long(&tmp);
     877           1 :                         sec = &tmp;
     878             :                 }
     879             : 
     880             :                 /* Solaris + BSD do not like microsecond values which are >= 1 sec */
     881          12 :                 if (usec > 999999) {
     882           1 :                         tv.tv_sec = Z_LVAL_P(sec) + (usec / 1000000);
     883           1 :                         tv.tv_usec = usec % 1000000;
     884             :                 } else {
     885          11 :                         tv.tv_sec = Z_LVAL_P(sec);
     886          11 :                         tv.tv_usec = usec;
     887             :                 }
     888             : 
     889          12 :                 tv_p = &tv;
     890             : 
     891          12 :                 if (sec == &tmp) {
     892             :                         zval_dtor(&tmp);
     893             :                 }
     894             :         }
     895             : 
     896          12 :         retval = select(max_fd+1, &rfds, &wfds, &efds, tv_p);
     897             : 
     898          12 :         if (retval == -1) {
     899           1 :                 SOCKETS_G(last_error) = errno;
     900           1 :                 php_error_docref(NULL, E_WARNING, "unable to select [%d]: %s", errno, sockets_strerror(errno));
     901           1 :                 RETURN_FALSE;
     902             :         }
     903             : 
     904          11 :         if (r_array != NULL) php_sock_array_from_fd_set(r_array, &rfds);
     905          11 :         if (w_array != NULL) php_sock_array_from_fd_set(w_array, &wfds);
     906          11 :         if (e_array != NULL) php_sock_array_from_fd_set(e_array, &efds);
     907             : 
     908          11 :         RETURN_LONG(retval);
     909             : }
     910             : /* }}} */
     911             : 
     912             : /* {{{ proto resource socket_create_listen(int port[, int backlog])
     913             :    Opens a socket on port to accept connections */
     914          16 : PHP_FUNCTION(socket_create_listen)
     915             : {
     916             :         php_socket      *php_sock;
     917          16 :         zend_long               port, backlog = 128;
     918             : 
     919          16 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &port, &backlog) == FAILURE) {
     920           3 :                 return;
     921             :         }
     922             : 
     923          13 :         if (!php_open_listen_sock(&php_sock, port, backlog)) {
     924           2 :                 RETURN_FALSE;
     925             :         }
     926             : 
     927          11 :         php_sock->error = 0;
     928          11 :         php_sock->blocking = 1;
     929             : 
     930          11 :         RETURN_RES(zend_register_resource(php_sock, le_socket));
     931             : }
     932             : /* }}} */
     933             : 
     934             : /* {{{ proto resource socket_accept(resource socket)
     935             :    Accepts a connection on the listening socket fd */
     936           7 : PHP_FUNCTION(socket_accept)
     937             : {
     938             :         zval                             *arg1;
     939             :         php_socket                       *php_sock, *new_sock;
     940             :         php_sockaddr_storage sa;
     941           7 :         socklen_t                        php_sa_len = sizeof(sa);
     942             : 
     943           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg1) == FAILURE) {
     944           1 :                 return;
     945             :         }
     946             : 
     947           6 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
     948           0 :                 RETURN_FALSE;
     949             :         }
     950             : 
     951           6 :         if (!php_accept_connect(php_sock, &new_sock, (struct sockaddr*)&sa, &php_sa_len)) {
     952           0 :                 RETURN_FALSE;
     953             :         }
     954             : 
     955           6 :         RETURN_RES(zend_register_resource(new_sock, le_socket));
     956             : }
     957             : /* }}} */
     958             : 
     959             : /* {{{ proto bool socket_set_nonblock(resource socket)
     960             :    Sets nonblocking mode on a socket resource */
     961          15 : PHP_FUNCTION(socket_set_nonblock)
     962             : {
     963             :         zval            *arg1;
     964             :         php_socket      *php_sock;
     965             : 
     966          15 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg1) == FAILURE) {
     967           1 :                 return;
     968             :         }
     969             : 
     970          14 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
     971           1 :                 RETURN_FALSE;
     972             :         }
     973             : 
     974          26 :         if (!Z_ISUNDEF(php_sock->zstream)) {
     975             :                 php_stream *stream;
     976             :                 /* omit notice if resource doesn't exist anymore */
     977           0 :                 stream = zend_fetch_resource2_ex(&php_sock->zstream, NULL, php_file_le_stream(), php_file_le_pstream());
     978           0 :                 if (stream != NULL) {
     979           0 :                         if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 0,
     980             :                                         NULL) != -1) {
     981           0 :                                 php_sock->blocking = 0;
     982           0 :                                 RETURN_TRUE;
     983             :                         }
     984             :                 }
     985             :         }
     986             : 
     987          13 :         if (php_set_sock_blocking(php_sock->bsd_socket, 0) == SUCCESS) {
     988          13 :                 php_sock->blocking = 0;
     989          13 :                 RETURN_TRUE;
     990             :         } else {
     991           0 :                 PHP_SOCKET_ERROR(php_sock, "unable to set nonblocking mode", errno);
     992           0 :                 RETURN_FALSE;
     993             :         }
     994             : }
     995             : /* }}} */
     996             : 
     997             : /* {{{ proto bool socket_set_block(resource socket)
     998             :    Sets blocking mode on a socket resource */
     999           9 : PHP_FUNCTION(socket_set_block)
    1000             : {
    1001             :         zval            *arg1;
    1002             :         php_socket      *php_sock;
    1003             : 
    1004           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg1) == FAILURE) {
    1005           1 :                 return;
    1006             :         }
    1007             : 
    1008           8 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    1009           2 :                 RETURN_FALSE;
    1010             :         }
    1011             : 
    1012             :         /* if socket was created from a stream, give the stream a chance to take
    1013             :          * care of the operation itself, thereby allowing it to update its internal
    1014             :          * state */
    1015          12 :         if (!Z_ISUNDEF(php_sock->zstream)) {
    1016             :                 php_stream *stream;
    1017           3 :                 stream = zend_fetch_resource2_ex(&php_sock->zstream, NULL, php_file_le_stream(), php_file_le_pstream());
    1018           3 :                 if (stream != NULL) {
    1019           2 :                         if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, 1,
    1020             :                                         NULL) != -1) {
    1021           2 :                                 php_sock->blocking = 1;
    1022           2 :                                 RETURN_TRUE;
    1023             :                         }
    1024             :                 }
    1025             :         }
    1026             : 
    1027           4 :         if (php_set_sock_blocking(php_sock->bsd_socket, 1) == SUCCESS) {
    1028           3 :                 php_sock->blocking = 1;
    1029           3 :                 RETURN_TRUE;
    1030             :         } else {
    1031           1 :                 PHP_SOCKET_ERROR(php_sock, "unable to set blocking mode", errno);
    1032           1 :                 RETURN_FALSE;
    1033             :         }
    1034             : }
    1035             : /* }}} */
    1036             : 
    1037             : /* {{{ proto bool socket_listen(resource socket[, int backlog])
    1038             :    Sets the maximum number of connections allowed to be waited for on the socket specified by fd */
    1039           9 : PHP_FUNCTION(socket_listen)
    1040             : {
    1041             :         zval            *arg1;
    1042             :         php_socket      *php_sock;
    1043           9 :         zend_long               backlog = 0;
    1044             : 
    1045           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &arg1, &backlog) == FAILURE) {
    1046           2 :                 return;
    1047             :         }
    1048             : 
    1049           7 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    1050           0 :                 RETURN_FALSE;
    1051             :         }
    1052             : 
    1053           7 :         if (listen(php_sock->bsd_socket, backlog) != 0) {
    1054           1 :                 PHP_SOCKET_ERROR(php_sock, "unable to listen on socket", errno);
    1055           1 :                 RETURN_FALSE;
    1056             :         }
    1057           6 :         RETURN_TRUE;
    1058             : }
    1059             : /* }}} */
    1060             : 
    1061             : /* {{{ proto void socket_close(resource socket)
    1062             :    Closes a file descriptor */
    1063          40 : PHP_FUNCTION(socket_close)
    1064             : {
    1065             :         zval            *arg1;
    1066             :         php_socket      *php_sock;
    1067             : 
    1068          40 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg1) == FAILURE) {
    1069           3 :                 return;
    1070             :         }
    1071             : 
    1072          37 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    1073           0 :                 RETURN_FALSE;
    1074             :         }
    1075             : 
    1076          74 :         if (!Z_ISUNDEF(php_sock->zstream)) {
    1077           2 :                 php_stream *stream = NULL;
    1078           2 :                 php_stream_from_zval_no_verify(stream, &php_sock->zstream);
    1079           2 :                 if (stream != NULL) {
    1080             :                         /* close & destroy stream, incl. removing it from the rsrc list;
    1081             :                          * resource stored in php_sock->zstream will become invalid */
    1082           2 :                         php_stream_free(stream,
    1083             :                                         PHP_STREAM_FREE_KEEP_RSRC | PHP_STREAM_FREE_CLOSE |
    1084             :                                         (stream->is_persistent?PHP_STREAM_FREE_CLOSE_PERSISTENT:0));
    1085             :                 }
    1086             :         }
    1087          37 :         zend_list_close(Z_RES_P(arg1));
    1088             : }
    1089             : /* }}} */
    1090             : 
    1091             : /* {{{ proto int socket_write(resource socket, string buf[, int length])
    1092             :    Writes the buffer to the socket resource, length is optional */
    1093           8 : PHP_FUNCTION(socket_write)
    1094             : {
    1095             :         zval            *arg1;
    1096             :         php_socket      *php_sock;
    1097             :         int                     retval;
    1098             :         size_t      str_len;
    1099           8 :         zend_long       length = 0;
    1100             :         char            *str;
    1101             : 
    1102           8 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|l", &arg1, &str, &str_len, &length) == FAILURE) {
    1103           2 :                 return;
    1104             :         }
    1105             : 
    1106           6 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    1107           0 :                 RETURN_FALSE;
    1108             :         }
    1109             : 
    1110           6 :         if (ZEND_NUM_ARGS() < 3) {
    1111           6 :                 length = str_len;
    1112             :         }
    1113             : 
    1114             : #ifndef PHP_WIN32
    1115           6 :         retval = write(php_sock->bsd_socket, str, MIN(length, str_len));
    1116             : #else
    1117             :         retval = send(php_sock->bsd_socket, str, min(length, str_len), 0);
    1118             : #endif
    1119             : 
    1120           6 :         if (retval < 0) {
    1121           1 :                 PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
    1122           1 :                 RETURN_FALSE;
    1123             :         }
    1124             : 
    1125           5 :         RETURN_LONG(retval);
    1126             : }
    1127             : /* }}} */
    1128             : 
    1129             : /* {{{ proto string socket_read(resource socket, int length [, int type])
    1130             :    Reads a maximum of length bytes from socket */
    1131          14 : PHP_FUNCTION(socket_read)
    1132             : {
    1133             :         zval            *arg1;
    1134             :         php_socket      *php_sock;
    1135             :         zend_string     *tmpbuf;
    1136             :         int                     retval;
    1137          14 :         zend_long               length, type = PHP_BINARY_READ;
    1138             : 
    1139          14 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl|l", &arg1, &length, &type) == FAILURE) {
    1140           2 :                 return;
    1141             :         }
    1142             : 
    1143             :         /* overflow check */
    1144          12 :         if ((length + 1) < 2) {
    1145           0 :                 RETURN_FALSE;
    1146             :         }
    1147             : 
    1148          24 :         tmpbuf = zend_string_alloc(length, 0);
    1149             : 
    1150          12 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    1151           0 :                 RETURN_FALSE;
    1152             :         }
    1153             : 
    1154          12 :         if (type == PHP_NORMAL_READ) {
    1155           0 :                 retval = php_read(php_sock, tmpbuf->val, length, 0);
    1156             :         } else {
    1157          12 :                 retval = recv(php_sock->bsd_socket, tmpbuf->val, length, 0);
    1158             :         }
    1159             : 
    1160          12 :         if (retval == -1) {
    1161             :                 /* if the socket is in non-blocking mode and there's no data to read,
    1162             :                 don't output any error, as this is a normal situation, and not an error */
    1163           2 :                 if (errno == EAGAIN
    1164             : #ifdef EWOULDBLOCK
    1165           1 :                 || errno == EWOULDBLOCK
    1166             : #endif
    1167             :                 ) {
    1168           0 :                         php_sock->error = errno;
    1169           0 :                         SOCKETS_G(last_error) = errno;
    1170             :                 } else {
    1171           1 :                         PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
    1172             :                 }
    1173             : 
    1174             :                 zend_string_free(tmpbuf);
    1175           1 :                 RETURN_FALSE;
    1176          11 :         } else if (!retval) {
    1177             :                 zend_string_free(tmpbuf);
    1178           0 :                 RETURN_EMPTY_STRING();
    1179             :         }
    1180             : 
    1181          22 :         tmpbuf = zend_string_truncate(tmpbuf, retval, 0);
    1182          11 :         tmpbuf->len = retval;
    1183          11 :         tmpbuf->val[tmpbuf->len] = '\0' ;
    1184             : 
    1185          11 :         RETURN_NEW_STR(tmpbuf);
    1186             : }
    1187             : /* }}} */
    1188             : 
    1189             : /* {{{ proto bool socket_getsockname(resource socket, string &addr[, int &port])
    1190             :    Queries the remote side of the given socket which may either result in host/port or in a UNIX filesystem path, dependent on its type. */
    1191           2 : PHP_FUNCTION(socket_getsockname)
    1192             : {
    1193           2 :         zval                                    *arg1, *addr, *port = NULL;
    1194             :         php_sockaddr_storage    sa_storage;
    1195             :         php_socket                              *php_sock;
    1196             :         struct sockaddr                 *sa;
    1197             :         struct sockaddr_in              *sin;
    1198             : #if HAVE_IPV6
    1199             :         struct sockaddr_in6             *sin6;
    1200             :         char                                    addr6[INET6_ADDRSTRLEN+1];
    1201             : #endif
    1202             :         struct sockaddr_un              *s_un;
    1203             :         char                                    *addr_string;
    1204           2 :         socklen_t                               salen = sizeof(php_sockaddr_storage);
    1205             : 
    1206           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz/|z/", &arg1, &addr, &port) == FAILURE) {
    1207           0 :                 return;
    1208             :         }
    1209             : 
    1210           2 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    1211           0 :                 RETURN_FALSE;
    1212             :         }
    1213             : 
    1214           2 :         sa = (struct sockaddr *) &sa_storage;
    1215             : 
    1216           2 :         if (getsockname(php_sock->bsd_socket, sa, &salen) != 0) {
    1217           0 :                 PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket name", errno);
    1218           0 :                 RETURN_FALSE;
    1219             :         }
    1220             : 
    1221           2 :         if (port != NULL) {
    1222           4 :                 ZVAL_DEREF(port);
    1223             :         }
    1224             : 
    1225           2 :         switch (sa->sa_family) {
    1226             : #if HAVE_IPV6
    1227             :                 case AF_INET6:
    1228           0 :                         sin6 = (struct sockaddr_in6 *) sa;
    1229           0 :                         inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
    1230           0 :                         zval_dtor(addr);
    1231           0 :                         ZVAL_STRING(addr, addr6);
    1232             : 
    1233           0 :                         if (port != NULL) {
    1234           0 :                                 zval_dtor(port);
    1235           0 :                                 ZVAL_LONG(port, htons(sin6->sin6_port));
    1236             :                         }
    1237           0 :                         RETURN_TRUE;
    1238             :                         break;
    1239             : #endif
    1240             :                 case AF_INET:
    1241           2 :                         sin = (struct sockaddr_in *) sa;
    1242           2 :                         while (inet_ntoa_lock == 1);
    1243           2 :                         inet_ntoa_lock = 1;
    1244           2 :                         addr_string = inet_ntoa(sin->sin_addr);
    1245           2 :                         inet_ntoa_lock = 0;
    1246             : 
    1247           2 :                         zval_dtor(addr);
    1248           4 :                         ZVAL_STRING(addr, addr_string);
    1249             : 
    1250           2 :                         if (port != NULL) {
    1251           2 :                                 zval_dtor(port);
    1252           2 :                                 ZVAL_LONG(port, htons(sin->sin_port));
    1253             :                         }
    1254           2 :                         RETURN_TRUE;
    1255             :                         break;
    1256             : 
    1257             :                 case AF_UNIX:
    1258           0 :                         s_un = (struct sockaddr_un *) sa;
    1259             : 
    1260           0 :                         zval_dtor(addr);
    1261           0 :                         ZVAL_STRING(addr, s_un->sun_path);
    1262           0 :                         RETURN_TRUE;
    1263             :                         break;
    1264             : 
    1265             :                 default:
    1266           0 :                         php_error_docref(NULL, E_WARNING, "Unsupported address family %d", sa->sa_family);
    1267           0 :                         RETURN_FALSE;
    1268             :         }
    1269             : }
    1270             : /* }}} */
    1271             : 
    1272             : /* {{{ proto bool socket_getpeername(resource socket, string &addr[, int &port])
    1273             :    Queries the remote side of the given socket which may either result in host/port or in a UNIX filesystem path, dependent on its type. */
    1274           3 : PHP_FUNCTION(socket_getpeername)
    1275             : {
    1276           3 :         zval                                    *arg1, *arg2, *arg3 = NULL;
    1277             :         php_sockaddr_storage    sa_storage;
    1278             :         php_socket                              *php_sock;
    1279             :         struct sockaddr                 *sa;
    1280             :         struct sockaddr_in              *sin;
    1281             : #if HAVE_IPV6
    1282             :         struct sockaddr_in6             *sin6;
    1283             :         char                                    addr6[INET6_ADDRSTRLEN+1];
    1284             : #endif
    1285             :         struct sockaddr_un              *s_un;
    1286             :         char                                    *addr_string;
    1287           3 :         socklen_t                               salen = sizeof(php_sockaddr_storage);
    1288             : 
    1289           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz/|z/", &arg1, &arg2, &arg3) == FAILURE) {
    1290           0 :                 return;
    1291             :         }
    1292             : 
    1293           3 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    1294           0 :                 RETURN_FALSE;
    1295             :         }
    1296             : 
    1297           3 :         sa = (struct sockaddr *) &sa_storage;
    1298             : 
    1299           3 :         if (getpeername(php_sock->bsd_socket, sa, &salen) < 0) {
    1300           1 :                 PHP_SOCKET_ERROR(php_sock, "unable to retrieve peer name", errno);
    1301           1 :                 RETURN_FALSE;
    1302             :         }
    1303             : 
    1304           2 :         switch (sa->sa_family) {
    1305             : #if HAVE_IPV6
    1306             :                 case AF_INET6:
    1307           1 :                         sin6 = (struct sockaddr_in6 *) sa;
    1308           1 :                         inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
    1309           1 :                         zval_dtor(arg2);
    1310           2 :                         ZVAL_STRING(arg2, addr6);
    1311             : 
    1312           1 :                         if (arg3 != NULL) {
    1313           1 :                                 zval_dtor(arg3);
    1314           1 :                                 ZVAL_LONG(arg3, htons(sin6->sin6_port));
    1315             :                         }
    1316             : 
    1317           1 :                         RETURN_TRUE;
    1318             :                         break;
    1319             : #endif
    1320             :                 case AF_INET:
    1321           1 :                         sin = (struct sockaddr_in *) sa;
    1322           1 :                         while (inet_ntoa_lock == 1);
    1323           1 :                         inet_ntoa_lock = 1;
    1324           1 :                         addr_string = inet_ntoa(sin->sin_addr);
    1325           1 :                         inet_ntoa_lock = 0;
    1326             : 
    1327           1 :                         zval_dtor(arg2);
    1328           2 :                         ZVAL_STRING(arg2, addr_string);
    1329             : 
    1330           1 :                         if (arg3 != NULL) {
    1331           1 :                                 zval_dtor(arg3);
    1332           1 :                                 ZVAL_LONG(arg3, htons(sin->sin_port));
    1333             :                         }
    1334             : 
    1335           1 :                         RETURN_TRUE;
    1336             :                         break;
    1337             : 
    1338             :                 case AF_UNIX:
    1339           0 :                         s_un = (struct sockaddr_un *) sa;
    1340             : 
    1341           0 :                         zval_dtor(arg2);
    1342           0 :                         ZVAL_STRING(arg2, s_un->sun_path);
    1343           0 :                         RETURN_TRUE;
    1344             :                         break;
    1345             : 
    1346             :                 default:
    1347           0 :                         php_error_docref(NULL, E_WARNING, "Unsupported address family %d", sa->sa_family);
    1348           0 :                         RETURN_FALSE;
    1349             :         }
    1350             : }
    1351             : /* }}} */
    1352             : 
    1353             : /* {{{ proto resource socket_create(int domain, int type, int protocol)
    1354             :    Creates an endpoint for communication in the domain specified by domain, of type specified by type */
    1355          58 : PHP_FUNCTION(socket_create)
    1356             : {
    1357             :         zend_long               arg1, arg2, arg3;
    1358          58 :         php_socket      *php_sock = php_create_socket();
    1359             : 
    1360          58 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &arg1, &arg2, &arg3) == FAILURE) {
    1361           6 :                 efree(php_sock);
    1362           6 :                 return;
    1363             :         }
    1364             : 
    1365         114 :         if (arg1 != AF_UNIX
    1366             : #if HAVE_IPV6
    1367          52 :                 && arg1 != AF_INET6
    1368             : #endif
    1369          62 :                 && arg1 != AF_INET) {
    1370           0 :                 php_error_docref(NULL, E_WARNING, "invalid socket domain [%pd] specified for argument 1, assuming AF_INET", arg1);
    1371           0 :                 arg1 = AF_INET;
    1372             :         }
    1373             : 
    1374          52 :         if (arg2 > 10) {
    1375           0 :                 php_error_docref(NULL, E_WARNING, "invalid socket type [%pd] specified for argument 2, assuming SOCK_STREAM", arg2);
    1376           0 :                 arg2 = SOCK_STREAM;
    1377             :         }
    1378             : 
    1379          52 :         php_sock->bsd_socket = socket(arg1, arg2, arg3);
    1380          52 :         php_sock->type = arg1;
    1381             : 
    1382          52 :         if (IS_INVALID_SOCKET(php_sock)) {
    1383           1 :                 SOCKETS_G(last_error) = errno;
    1384           1 :                 php_error_docref(NULL, E_WARNING, "Unable to create socket [%d]: %s", errno, sockets_strerror(errno));
    1385           1 :                 efree(php_sock);
    1386           1 :                 RETURN_FALSE;
    1387             :         }
    1388             : 
    1389          51 :         php_sock->error = 0;
    1390          51 :         php_sock->blocking = 1;
    1391             : 
    1392          51 :         RETURN_RES(zend_register_resource(php_sock, le_socket));
    1393             : }
    1394             : /* }}} */
    1395             : 
    1396             : /* {{{ proto bool socket_connect(resource socket, string addr [, int port])
    1397             :    Opens a connection to addr:port on the socket specified by socket */
    1398          11 : PHP_FUNCTION(socket_connect)
    1399             : {
    1400             :         zval                            *arg1;
    1401             :         php_socket                      *php_sock;
    1402             :         char                            *addr;
    1403             :         int                                     retval;
    1404             :         size_t              addr_len;
    1405          11 :         zend_long                               port = 0;
    1406          11 :         int                                     argc = ZEND_NUM_ARGS();
    1407             : 
    1408          11 :         if (zend_parse_parameters(argc, "rs|l", &arg1, &addr, &addr_len, &port) == FAILURE) {
    1409           2 :                 return;
    1410             :         }
    1411             : 
    1412           9 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    1413           0 :                 RETURN_FALSE;
    1414             :         }
    1415             : 
    1416           9 :         switch(php_sock->type) {
    1417             : #if HAVE_IPV6
    1418             :                 case AF_INET6: {
    1419           2 :                         struct sockaddr_in6 sin6 = {0};
    1420             : 
    1421           2 :                         if (argc != 3) {
    1422           0 :                                 php_error_docref(NULL, E_WARNING, "Socket of type AF_INET6 requires 3 arguments");
    1423           0 :                                 RETURN_FALSE;
    1424             :                         }
    1425             : 
    1426           2 :                         memset(&sin6, 0, sizeof(struct sockaddr_in6));
    1427             : 
    1428           2 :                         sin6.sin6_family = AF_INET6;
    1429           2 :                         sin6.sin6_port   = htons((unsigned short int)port);
    1430             : 
    1431           2 :                         if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
    1432           0 :                                 RETURN_FALSE;
    1433             :                         }
    1434             : 
    1435           2 :                         retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin6, sizeof(struct sockaddr_in6));
    1436           2 :                         break;
    1437             :                 }
    1438             : #endif
    1439             :                 case AF_INET: {
    1440           5 :                         struct sockaddr_in sin = {0};
    1441             : 
    1442           5 :                         if (argc != 3) {
    1443           1 :                                 php_error_docref(NULL, E_WARNING, "Socket of type AF_INET requires 3 arguments");
    1444           1 :                                 RETURN_FALSE;
    1445             :                         }
    1446             : 
    1447           4 :                         sin.sin_family = AF_INET;
    1448           4 :                         sin.sin_port   = htons((unsigned short int)port);
    1449             : 
    1450           4 :                         if (! php_set_inet_addr(&sin, addr, php_sock)) {
    1451           0 :                                 RETURN_FALSE;
    1452             :                         }
    1453             : 
    1454           4 :                         retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in));
    1455           4 :                         break;
    1456             :                 }
    1457             : 
    1458             :                 case AF_UNIX: {
    1459           2 :                         struct sockaddr_un s_un = {0};
    1460             : 
    1461           2 :                         if (addr_len >= sizeof(s_un.sun_path)) {
    1462           0 :                                 php_error_docref(NULL, E_WARNING, "Path too long");
    1463           0 :                                 RETURN_FALSE;
    1464             :                         }
    1465             : 
    1466           2 :                         s_un.sun_family = AF_UNIX;
    1467           2 :                         memcpy(&s_un.sun_path, addr, addr_len);
    1468           2 :                         retval = connect(php_sock->bsd_socket, (struct sockaddr *) &s_un,
    1469             :                                 (socklen_t)(XtOffsetOf(struct sockaddr_un, sun_path) + addr_len));
    1470           2 :                         break;
    1471             :                 }
    1472             : 
    1473             :                 default:
    1474           0 :                         php_error_docref(NULL, E_WARNING, "Unsupported socket type %d", php_sock->type);
    1475           0 :                         RETURN_FALSE;
    1476             :                 }
    1477             : 
    1478           8 :         if (retval != 0) {
    1479           1 :                 PHP_SOCKET_ERROR(php_sock, "unable to connect", errno);
    1480           1 :                 RETURN_FALSE;
    1481             :         }
    1482             : 
    1483           7 :         RETURN_TRUE;
    1484             : }
    1485             : /* }}} */
    1486             : 
    1487             : /* {{{ proto string socket_strerror(int errno)
    1488             :    Returns a string describing an error */
    1489         134 : PHP_FUNCTION(socket_strerror)
    1490             : {
    1491             :         zend_long       arg1;
    1492             : 
    1493         134 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &arg1) == FAILURE) {
    1494           1 :                 return;
    1495             :         }
    1496             : 
    1497         266 :         RETURN_STRING(sockets_strerror(arg1));
    1498             : }
    1499             : /* }}} */
    1500             : 
    1501             : /* {{{ proto bool socket_bind(resource socket, string addr [, int port])
    1502             :    Binds an open socket to a listening port, port is only specified in AF_INET family. */
    1503          30 : PHP_FUNCTION(socket_bind)
    1504             : {
    1505             :         zval                                    *arg1;
    1506          30 :         php_sockaddr_storage    sa_storage = {0};
    1507          30 :         struct sockaddr                 *sock_type = (struct sockaddr*) &sa_storage;
    1508             :         php_socket                              *php_sock;
    1509             :         char                                    *addr;
    1510             :         size_t                                          addr_len;
    1511          30 :         zend_long                                       port = 0;
    1512          30 :         zend_long                                       retval = 0;
    1513             : 
    1514          30 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|l", &arg1, &addr, &addr_len, &port) == FAILURE) {
    1515           2 :                 return;
    1516             :         }
    1517             : 
    1518          28 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    1519           0 :                 RETURN_FALSE;
    1520             :         }
    1521             : 
    1522          28 :         switch(php_sock->type) {
    1523             :                 case AF_UNIX:
    1524             :                         {
    1525           6 :                                 struct sockaddr_un *sa = (struct sockaddr_un *) sock_type;
    1526             : 
    1527           6 :                                 sa->sun_family = AF_UNIX;
    1528             : 
    1529           6 :                                 if (addr_len >= sizeof(sa->sun_path)) {
    1530           0 :                                         php_error_docref(NULL, E_WARNING,
    1531             :                                                         "Invalid path: too long (maximum size is %d)",
    1532             :                                                         (int)sizeof(sa->sun_path) - 1);
    1533           0 :                                         RETURN_FALSE;
    1534             :                                 }
    1535           6 :                                 memcpy(&sa->sun_path, addr, addr_len);
    1536             : 
    1537           6 :                                 retval = bind(php_sock->bsd_socket, (struct sockaddr *) sa,
    1538             :                                                 offsetof(struct sockaddr_un, sun_path) + addr_len);
    1539           6 :                                 break;
    1540             :                         }
    1541             : 
    1542             :                 case AF_INET:
    1543             :                         {
    1544          12 :                                 struct sockaddr_in *sa = (struct sockaddr_in *) sock_type;
    1545             : 
    1546          12 :                                 sa->sin_family = AF_INET;
    1547          12 :                                 sa->sin_port = htons((unsigned short) port);
    1548             : 
    1549          12 :                                 if (! php_set_inet_addr(sa, addr, php_sock)) {
    1550           0 :                                         RETURN_FALSE;
    1551             :                                 }
    1552             : 
    1553          12 :                                 retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in));
    1554          12 :                                 break;
    1555             :                         }
    1556             : #if HAVE_IPV6
    1557             :                 case AF_INET6:
    1558             :                         {
    1559          10 :                                 struct sockaddr_in6 *sa = (struct sockaddr_in6 *) sock_type;
    1560             : 
    1561          10 :                                 sa->sin6_family = AF_INET6;
    1562          10 :                                 sa->sin6_port = htons((unsigned short) port);
    1563             : 
    1564          10 :                                 if (! php_set_inet6_addr(sa, addr, php_sock)) {
    1565           0 :                                         RETURN_FALSE;
    1566             :                                 }
    1567             : 
    1568          10 :                                 retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in6));
    1569          10 :                                 break;
    1570             :                         }
    1571             : #endif
    1572             :                 default:
    1573           0 :                         php_error_docref(NULL, E_WARNING, "unsupported socket type '%d', must be AF_UNIX, AF_INET, or AF_INET6", php_sock->type);
    1574           0 :                         RETURN_FALSE;
    1575             :         }
    1576             : 
    1577          28 :         if (retval != 0) {
    1578           0 :                 PHP_SOCKET_ERROR(php_sock, "unable to bind address", errno);
    1579           0 :                 RETURN_FALSE;
    1580             :         }
    1581             : 
    1582          28 :         RETURN_TRUE;
    1583             : }
    1584             : /* }}} */
    1585             : 
    1586             : /* {{{ proto int socket_recv(resource socket, string &buf, int len, int flags)
    1587             :    Receives data from a connected socket */
    1588           2 : PHP_FUNCTION(socket_recv)
    1589             : {
    1590             :         zval            *php_sock_res, *buf;
    1591             :         zend_string     *recv_buf;
    1592             :         php_socket      *php_sock;
    1593             :         int                     retval;
    1594             :         zend_long               len, flags;
    1595             : 
    1596           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz/ll", &php_sock_res, &buf, &len, &flags) == FAILURE) {
    1597           0 :                 return;
    1598             :         }
    1599             : 
    1600           2 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(php_sock_res), le_socket_name, le_socket)) == NULL) {
    1601           0 :                 RETURN_FALSE;
    1602             :         }
    1603             : 
    1604             :         /* overflow check */
    1605           2 :         if ((len + 1) < 2) {
    1606           0 :                 RETURN_FALSE;
    1607             :         }
    1608             : 
    1609           4 :         recv_buf = zend_string_alloc(len, 0);
    1610             : 
    1611           2 :         if ((retval = recv(php_sock->bsd_socket, recv_buf->val, len, flags)) < 1) {
    1612           0 :                 efree(recv_buf);
    1613             : 
    1614           0 :                 zval_dtor(buf);
    1615           0 :                 ZVAL_NULL(buf);
    1616             :         } else {
    1617           2 :                 recv_buf->len = retval;
    1618           2 :                 recv_buf->val[recv_buf->len] = '\0';
    1619             : 
    1620             :                 /* Rebuild buffer zval */
    1621           2 :                 zval_dtor(buf);
    1622           2 :                 ZVAL_NEW_STR(buf, recv_buf);
    1623             :         }
    1624             : 
    1625           2 :         if (retval == -1) {
    1626           0 :                 PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
    1627           0 :                 RETURN_FALSE;
    1628             :         }
    1629             : 
    1630           2 :         RETURN_LONG(retval);
    1631             : }
    1632             : /* }}} */
    1633             : 
    1634             : /* {{{ proto int socket_send(resource socket, string buf, int len, int flags)
    1635             :    Sends data to a connected socket */
    1636           0 : PHP_FUNCTION(socket_send)
    1637             : {
    1638             :         zval            *arg1;
    1639             :         php_socket      *php_sock;
    1640             :         size_t                  buf_len, retval;
    1641             :         zend_long               len, flags;
    1642             :         char            *buf;
    1643             : 
    1644           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsll", &arg1, &buf, &buf_len, &len, &flags) == FAILURE) {
    1645           0 :                 return;
    1646             :         }
    1647             : 
    1648           0 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    1649           0 :                 RETURN_FALSE;
    1650             :         }
    1651             : 
    1652           0 :         retval = send(php_sock->bsd_socket, buf, (buf_len < len ? buf_len : len), flags);
    1653             : 
    1654           0 :         if (retval == -1) {
    1655           0 :                 PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
    1656           0 :                 RETURN_FALSE;
    1657             :         }
    1658             : 
    1659           0 :         RETURN_LONG(retval);
    1660             : }
    1661             : /* }}} */
    1662             : 
    1663             : /* {{{ proto int socket_recvfrom(resource socket, string &buf, int len, int flags, string &name [, int &port])
    1664             :    Receives data from a socket, connected or not */
    1665          11 : PHP_FUNCTION(socket_recvfrom)
    1666             : {
    1667          11 :         zval                            *arg1, *arg2, *arg5, *arg6 = NULL;
    1668             :         php_socket                      *php_sock;
    1669             :         struct sockaddr_un      s_un;
    1670             :         struct sockaddr_in      sin;
    1671             : #if HAVE_IPV6
    1672             :         struct sockaddr_in6     sin6;
    1673             :         char                            addr6[INET6_ADDRSTRLEN];
    1674             : #endif
    1675             :         socklen_t                       slen;
    1676             :         int                                     retval;
    1677             :         zend_long                               arg3, arg4;
    1678             :         char                            *address;
    1679             :         zend_string                     *recv_buf;
    1680             : 
    1681          11 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz/llz/|z/", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6) == FAILURE) {
    1682           2 :                 return;
    1683             :         }
    1684             : 
    1685           9 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    1686           0 :                 RETURN_FALSE;
    1687             :         }
    1688             : 
    1689             :         /* overflow check */
    1690           9 :         if ((arg3 + 2) < 3) {
    1691           1 :                 RETURN_FALSE;
    1692             :         }
    1693             : 
    1694          16 :         recv_buf = zend_string_alloc(arg3 + 1, 0);
    1695             : 
    1696           8 :         switch (php_sock->type) {
    1697             :                 case AF_UNIX:
    1698           2 :                         slen = sizeof(s_un);
    1699           2 :                         s_un.sun_family = AF_UNIX;
    1700           2 :                         retval = recvfrom(php_sock->bsd_socket, recv_buf->val, arg3, arg4, (struct sockaddr *)&s_un, (socklen_t *)&slen);
    1701             : 
    1702           2 :                         if (retval < 0) {
    1703           1 :                                 PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
    1704             :                                 zend_string_free(recv_buf);
    1705           1 :                                 RETURN_FALSE;
    1706             :                         }
    1707           1 :                         recv_buf->len = retval;
    1708           1 :                         recv_buf->val[recv_buf->len] = '\0';
    1709             : 
    1710           1 :                         zval_dtor(arg2);
    1711           1 :                         zval_dtor(arg5);
    1712             : 
    1713           1 :                         ZVAL_NEW_STR(arg2, recv_buf);
    1714           2 :                         ZVAL_STRING(arg5, s_un.sun_path);
    1715           1 :                         break;
    1716             : 
    1717             :                 case AF_INET:
    1718           3 :                         slen = sizeof(sin);
    1719           3 :                         memset(&sin, 0, slen);
    1720           3 :                         sin.sin_family = AF_INET;
    1721             : 
    1722           3 :                         if (arg6 == NULL) {
    1723             :                                 zend_string_free(recv_buf);
    1724           1 :                                 WRONG_PARAM_COUNT;
    1725             :                         }
    1726             : 
    1727           2 :                         retval = recvfrom(php_sock->bsd_socket, recv_buf->val, arg3, arg4, (struct sockaddr *)&sin, (socklen_t *)&slen);
    1728             : 
    1729           2 :                         if (retval < 0) {
    1730           1 :                                 PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
    1731             :                                 zend_string_free(recv_buf);
    1732           1 :                                 RETURN_FALSE;
    1733             :                         }
    1734           1 :                         recv_buf->len = retval;
    1735           1 :                         recv_buf->val[recv_buf->len] = '\0';
    1736             : 
    1737           1 :                         zval_dtor(arg2);
    1738           1 :                         zval_dtor(arg5);
    1739           1 :                         zval_dtor(arg6);
    1740             : 
    1741           1 :                         address = inet_ntoa(sin.sin_addr);
    1742             : 
    1743           1 :                         ZVAL_NEW_STR(arg2, recv_buf);
    1744           2 :                         ZVAL_STRING(arg5, address ? address : "0.0.0.0");
    1745           1 :                         ZVAL_LONG(arg6, ntohs(sin.sin_port));
    1746           1 :                         break;
    1747             : #if HAVE_IPV6
    1748             :                 case AF_INET6:
    1749           3 :                         slen = sizeof(sin6);
    1750           3 :                         memset(&sin6, 0, slen);
    1751           3 :                         sin6.sin6_family = AF_INET6;
    1752             : 
    1753           3 :                         if (arg6 == NULL) {
    1754           1 :                                 efree(recv_buf);
    1755           1 :                                 WRONG_PARAM_COUNT;
    1756             :                         }
    1757             : 
    1758           2 :                         retval = recvfrom(php_sock->bsd_socket, recv_buf->val, arg3, arg4, (struct sockaddr *)&sin6, (socklen_t *)&slen);
    1759             : 
    1760           2 :                         if (retval < 0) {
    1761           1 :                                 PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
    1762             :                                 zend_string_free(recv_buf);
    1763           1 :                                 RETURN_FALSE;
    1764             :                         }
    1765           1 :                         recv_buf->len = retval;
    1766           1 :                         recv_buf->val[recv_buf->len] = '\0';
    1767             : 
    1768           1 :                         zval_dtor(arg2);
    1769           1 :                         zval_dtor(arg5);
    1770           1 :                         zval_dtor(arg6);
    1771             : 
    1772           1 :                         memset(addr6, 0, INET6_ADDRSTRLEN);
    1773           1 :                         inet_ntop(AF_INET6, &sin6.sin6_addr, addr6, INET6_ADDRSTRLEN);
    1774             : 
    1775           1 :                         ZVAL_NEW_STR(arg2, recv_buf);
    1776           2 :                         ZVAL_STRING(arg5, addr6[0] ? addr6 : "::");
    1777           1 :                         ZVAL_LONG(arg6, ntohs(sin6.sin6_port));
    1778           1 :                         break;
    1779             : #endif
    1780             :                 default:
    1781           0 :                         php_error_docref(NULL, E_WARNING, "Unsupported socket type %d", php_sock->type);
    1782           0 :                         RETURN_FALSE;
    1783             :         }
    1784             : 
    1785           3 :         RETURN_LONG(retval);
    1786             : }
    1787             : /* }}} */
    1788             : 
    1789             : /* {{{ proto int socket_sendto(resource socket, string buf, int len, int flags, string addr [, int port])
    1790             :    Sends a message to a socket, whether it is connected or not */
    1791          25 : PHP_FUNCTION(socket_sendto)
    1792             : {
    1793             :         zval                            *arg1;
    1794             :         php_socket                      *php_sock;
    1795             :         struct sockaddr_un      s_un;
    1796             :         struct sockaddr_in      sin;
    1797             : #if HAVE_IPV6
    1798             :         struct sockaddr_in6     sin6;
    1799             : #endif
    1800             :         int                                     retval;
    1801             :         size_t              buf_len, addr_len;
    1802          25 :         zend_long                       len, flags, port = 0;
    1803             :         char                            *buf, *addr;
    1804          25 :         int                                     argc = ZEND_NUM_ARGS();
    1805             : 
    1806          25 :         if (zend_parse_parameters(argc, "rslls|l", &arg1, &buf, &buf_len, &len, &flags, &addr, &addr_len, &port) == FAILURE) {
    1807           1 :                 return;
    1808             :         }
    1809             : 
    1810          24 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    1811           0 :                 RETURN_FALSE;
    1812             :         }
    1813             : 
    1814          24 :         switch (php_sock->type) {
    1815             :                 case AF_UNIX:
    1816           2 :                         memset(&s_un, 0, sizeof(s_un));
    1817           2 :                         s_un.sun_family = AF_UNIX;
    1818           2 :                         snprintf(s_un.sun_path, 108, "%s", addr);
    1819             : 
    1820           2 :                         retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len,       flags, (struct sockaddr *) &s_un, SUN_LEN(&s_un));
    1821           2 :                         break;
    1822             : 
    1823             :                 case AF_INET:
    1824          16 :                         if (argc != 6) {
    1825           1 :                                 WRONG_PARAM_COUNT;
    1826             :                         }
    1827             : 
    1828          15 :                         memset(&sin, 0, sizeof(sin));
    1829          15 :                         sin.sin_family = AF_INET;
    1830          15 :                         sin.sin_port = htons((unsigned short) port);
    1831             : 
    1832          15 :                         if (! php_set_inet_addr(&sin, addr, php_sock)) {
    1833           0 :                                 RETURN_FALSE;
    1834             :                         }
    1835             : 
    1836          15 :                         retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len, flags, (struct sockaddr *) &sin, sizeof(sin));
    1837          15 :                         break;
    1838             : #if HAVE_IPV6
    1839             :                 case AF_INET6:
    1840           6 :                         if (argc != 6) {
    1841           1 :                                 WRONG_PARAM_COUNT;
    1842             :                         }
    1843             : 
    1844           5 :                         memset(&sin6, 0, sizeof(sin6));
    1845           5 :                         sin6.sin6_family = AF_INET6;
    1846           5 :                         sin6.sin6_port = htons((unsigned short) port);
    1847             : 
    1848           5 :                         if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
    1849           0 :                                 RETURN_FALSE;
    1850             :                         }
    1851             : 
    1852           5 :                         retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len, flags, (struct sockaddr *) &sin6, sizeof(sin6));
    1853           5 :                         break;
    1854             : #endif
    1855             :                 default:
    1856           0 :                         php_error_docref(NULL, E_WARNING, "Unsupported socket type %d", php_sock->type);
    1857           0 :                         RETURN_FALSE;
    1858             :         }
    1859             : 
    1860          22 :         if (retval == -1) {
    1861           0 :                 PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
    1862           0 :                 RETURN_FALSE;
    1863             :         }
    1864             : 
    1865          22 :         RETURN_LONG(retval);
    1866             : }
    1867             : /* }}} */
    1868             : 
    1869             : /* {{{ proto mixed socket_get_option(resource socket, int level, int optname)
    1870             :    Gets socket options for the socket */
    1871          22 : PHP_FUNCTION(socket_get_option)
    1872             : {
    1873             :         zval                    *arg1;
    1874             :         struct linger   linger_val;
    1875             :         struct timeval  tv;
    1876             : #ifdef PHP_WIN32
    1877             :         int                             timeout = 0;
    1878             : #endif
    1879             :         socklen_t               optlen;
    1880             :         php_socket              *php_sock;
    1881             :         int                             other_val;
    1882             :         zend_long                       level, optname;
    1883             : 
    1884          22 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rll", &arg1, &level, &optname) == FAILURE) {
    1885           0 :                 return;
    1886             :         }
    1887             : 
    1888          22 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    1889           1 :                 RETURN_FALSE;
    1890             :         }
    1891             : 
    1892          21 :         if (level == IPPROTO_IP) {
    1893          10 :                 switch (optname) {
    1894             :                 case IP_MULTICAST_IF: {
    1895             :                         struct in_addr if_addr;
    1896             :                         unsigned int if_index;
    1897           2 :                         optlen = sizeof(if_addr);
    1898           2 :                         if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&if_addr, &optlen) != 0) {
    1899           0 :                                 PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
    1900           0 :                                 RETURN_FALSE;
    1901             :                         }
    1902           2 :                         if (php_add4_to_if_index(&if_addr, php_sock, &if_index) == SUCCESS) {
    1903           2 :                                 RETURN_LONG((zend_long) if_index);
    1904             :                         } else {
    1905           0 :                                 RETURN_FALSE;
    1906             :                         }
    1907             :                 }
    1908             :                 }
    1909             :         }
    1910             : #if HAVE_IPV6
    1911          11 :         else if (level == IPPROTO_IPV6) {
    1912           5 :                 int ret = php_do_getsockopt_ipv6_rfc3542(php_sock, level, optname, return_value);
    1913           5 :                 if (ret == SUCCESS) {
    1914           0 :                         return;
    1915           5 :                 } else if (ret == FAILURE) {
    1916           0 :                         RETURN_FALSE;
    1917             :                 } /* else continue */
    1918             :         }
    1919             : #endif
    1920             : 
    1921             :         /* sol_socket options and general case */
    1922          19 :         switch(optname) {
    1923             :                 case SO_LINGER:
    1924           1 :                         optlen = sizeof(linger_val);
    1925             : 
    1926           1 :                         if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&linger_val, &optlen) != 0) {
    1927           0 :                                 PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
    1928           0 :                                 RETURN_FALSE;
    1929             :                         }
    1930             : 
    1931           1 :                         array_init(return_value);
    1932           1 :                         add_assoc_long(return_value, "l_onoff", linger_val.l_onoff);
    1933           1 :                         add_assoc_long(return_value, "l_linger", linger_val.l_linger);
    1934           1 :                         break;
    1935             : 
    1936             :                 case SO_RCVTIMEO:
    1937             :                 case SO_SNDTIMEO:
    1938             : #ifndef PHP_WIN32
    1939           2 :                         optlen = sizeof(tv);
    1940             : 
    1941           2 :                         if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&tv, &optlen) != 0) {
    1942           0 :                                 PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
    1943           0 :                                 RETURN_FALSE;
    1944             :                         }
    1945             : #else
    1946             :                         optlen = sizeof(int);
    1947             : 
    1948             :                         if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&timeout, &optlen) != 0) {
    1949             :                                 PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
    1950             :                                 RETURN_FALSE;
    1951             :                         }
    1952             : 
    1953             :                         tv.tv_sec = timeout ? timeout / 1000 : 0;
    1954             :                         tv.tv_usec = timeout ? (timeout * 1000) % 1000000 : 0;
    1955             : #endif
    1956             : 
    1957           2 :                         array_init(return_value);
    1958             : 
    1959           2 :                         add_assoc_long(return_value, "sec", tv.tv_sec);
    1960           2 :                         add_assoc_long(return_value, "usec", tv.tv_usec);
    1961           2 :                         break;
    1962             : 
    1963             :                 default:
    1964          16 :                         optlen = sizeof(other_val);
    1965             : 
    1966          16 :                         if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&other_val, &optlen) != 0) {
    1967           1 :                                 PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
    1968           1 :                                 RETURN_FALSE;
    1969             :                         }
    1970          15 :                         if (optlen == 1)
    1971           0 :                                 other_val = *((unsigned char *)&other_val);
    1972             : 
    1973          15 :                         RETURN_LONG(other_val);
    1974             :                         break;
    1975             :         }
    1976             : }
    1977             : /* }}} */
    1978             : 
    1979             : /* {{{ proto bool socket_set_option(resource socket, int level, int optname, int|array optval)
    1980             :    Sets socket options for the socket */
    1981          51 : PHP_FUNCTION(socket_set_option)
    1982             : {
    1983             :         zval                                    *arg1, *arg4;
    1984             :         struct linger                   lv;
    1985             :         php_socket                              *php_sock;
    1986             :         int                                             ov, optlen, retval;
    1987             : #ifdef PHP_WIN32
    1988             :         int                                             timeout;
    1989             : #else
    1990             :         struct                                  timeval tv;
    1991             : #endif
    1992             :         zend_long                                       level, optname;
    1993             :         void                                    *opt_ptr;
    1994             :         HashTable                               *opt_ht;
    1995             :         zval                                    *l_onoff, *l_linger;
    1996             :         zval                                    *sec, *usec;
    1997             : 
    1998             : 
    1999          51 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rllz", &arg1, &level, &optname, &arg4) == FAILURE) {
    2000           1 :                 return;
    2001             :         }
    2002             : 
    2003          50 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    2004           0 :                 RETURN_FALSE;
    2005             :         }
    2006             : 
    2007          50 :         set_errno(0);
    2008             : 
    2009             : #define HANDLE_SUBCALL(res) \
    2010             :         do { \
    2011             :                 if (res == 1) { goto default_case; } \
    2012             :                 else if (res == SUCCESS) { RETURN_TRUE; } \
    2013             :                 else { RETURN_FALSE; } \
    2014             :         } while (0)
    2015             : 
    2016             : 
    2017          50 :         if (level == IPPROTO_IP) {
    2018          23 :                 int res = php_do_setsockopt_ip_mcast(php_sock, level, optname, arg4);
    2019          23 :                 HANDLE_SUBCALL(res);
    2020             :         }
    2021             : 
    2022             : #if HAVE_IPV6
    2023          27 :         else if (level == IPPROTO_IPV6) {
    2024          18 :                 int res = php_do_setsockopt_ipv6_mcast(php_sock, level, optname, arg4);
    2025          18 :                 if (res == 1) {
    2026           5 :                         res = php_do_setsockopt_ipv6_rfc3542(php_sock, level, optname, arg4);
    2027             :                 }
    2028          18 :                 HANDLE_SUBCALL(res);
    2029             :         }
    2030             : #endif
    2031             : 
    2032           9 :         switch (optname) {
    2033             :                 case SO_LINGER: {
    2034           3 :                         const char l_onoff_key[] = "l_onoff";
    2035           3 :                         const char l_linger_key[] = "l_linger";
    2036             : 
    2037           6 :                         convert_to_array_ex(arg4);
    2038           6 :                         opt_ht = HASH_OF(arg4);
    2039             : 
    2040           3 :                         if ((l_onoff = zend_hash_str_find(opt_ht, l_onoff_key, sizeof(l_onoff_key) - 1)) == NULL) {
    2041           1 :                                 php_error_docref(NULL, E_WARNING, "no key \"%s\" passed in optval", l_onoff_key);
    2042           1 :                                 RETURN_FALSE;
    2043             :                         }
    2044           2 :                         if ((l_linger = zend_hash_str_find(opt_ht, l_linger_key, sizeof(l_linger_key) - 1)) == NULL) {
    2045           1 :                                 php_error_docref(NULL, E_WARNING, "no key \"%s\" passed in optval", l_linger_key);
    2046           1 :                                 RETURN_FALSE;
    2047             :                         }
    2048             : 
    2049           1 :                         convert_to_long_ex(l_onoff);
    2050           1 :                         convert_to_long_ex(l_linger);
    2051             : 
    2052           1 :                         lv.l_onoff = (unsigned short)Z_LVAL_P(l_onoff);
    2053           1 :                         lv.l_linger = (unsigned short)Z_LVAL_P(l_linger);
    2054             : 
    2055           1 :                         optlen = sizeof(lv);
    2056           1 :                         opt_ptr = &lv;
    2057           1 :                         break;
    2058             :                 }
    2059             : 
    2060             :                 case SO_RCVTIMEO:
    2061             :                 case SO_SNDTIMEO: {
    2062           4 :                         const char sec_key[] = "sec";
    2063           4 :                         const char usec_key[] = "usec";
    2064             : 
    2065           8 :                         convert_to_array_ex(arg4);
    2066           8 :                         opt_ht = HASH_OF(arg4);
    2067             : 
    2068           4 :                         if ((sec = zend_hash_str_find(opt_ht, sec_key, sizeof(sec_key) - 1)) == NULL) {
    2069           2 :                                 php_error_docref(NULL, E_WARNING, "no key \"%s\" passed in optval", sec_key);
    2070           2 :                                 RETURN_FALSE;
    2071             :                         }
    2072           2 :                         if ((usec = zend_hash_str_find(opt_ht, usec_key, sizeof(usec_key) - 1)) == NULL) {
    2073           0 :                                 php_error_docref(NULL, E_WARNING, "no key \"%s\" passed in optval", usec_key);
    2074           0 :                                 RETURN_FALSE;
    2075             :                         }
    2076             : 
    2077           2 :                         convert_to_long_ex(sec);
    2078           2 :                         convert_to_long_ex(usec);
    2079             : #ifndef PHP_WIN32
    2080           2 :                         tv.tv_sec = Z_LVAL_P(sec);
    2081           2 :                         tv.tv_usec = Z_LVAL_P(usec);
    2082           2 :                         optlen = sizeof(tv);
    2083           2 :                         opt_ptr = &tv;
    2084             : #else
    2085             :                         timeout = Z_LVAL_P(sec) * 1000 + Z_LVAL_P(usec) / 1000;
    2086             :                         optlen = sizeof(int);
    2087             :                         opt_ptr = &timeout;
    2088             : #endif
    2089           2 :                         break;
    2090             :                 }
    2091             : #ifdef SO_BINDTODEVICE
    2092             :                 case SO_BINDTODEVICE: {
    2093           0 :                         if (Z_TYPE_P(arg4) == IS_STRING) {
    2094           0 :                                 opt_ptr = Z_STRVAL_P(arg4);
    2095           0 :                                 optlen = Z_STRLEN_P(arg4);
    2096             :                         } else {
    2097           0 :                                 opt_ptr = "";
    2098           0 :                                 optlen = 0;
    2099             :                         }
    2100           0 :                         break;
    2101             :                 }
    2102             : #endif
    2103             : 
    2104             :                 default:
    2105             : default_case:
    2106          10 :                         convert_to_long_ex(arg4);
    2107           5 :                         ov = Z_LVAL_P(arg4);
    2108             : 
    2109           5 :                         optlen = sizeof(ov);
    2110           5 :                         opt_ptr = &ov;
    2111             :                         break;
    2112             :         }
    2113             : 
    2114           8 :         retval = setsockopt(php_sock->bsd_socket, level, optname, opt_ptr, optlen);
    2115           8 :         if (retval != 0) {
    2116           1 :                 PHP_SOCKET_ERROR(php_sock, "unable to set socket option", errno);
    2117           1 :                 RETURN_FALSE;
    2118             :         }
    2119             : 
    2120           7 :         RETURN_TRUE;
    2121             : }
    2122             : /* }}} */
    2123             : 
    2124             : #ifdef HAVE_SOCKETPAIR
    2125             : /* {{{ proto bool socket_create_pair(int domain, int type, int protocol, array &fd)
    2126             :    Creates a pair of indistinguishable sockets and stores them in fds. */
    2127          10 : PHP_FUNCTION(socket_create_pair)
    2128             : {
    2129             :         zval            retval[2], *fds_array_zval;
    2130             :         php_socket      *php_sock[2];
    2131             :         PHP_SOCKET      fds_array[2];
    2132             :         zend_long               domain, type, protocol;
    2133             : 
    2134          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "lllz/", &domain, &type, &protocol, &fds_array_zval) == FAILURE) {
    2135           2 :                 return;
    2136             :         }
    2137             : 
    2138           8 :         php_sock[0] = php_create_socket();
    2139           8 :         php_sock[1] = php_create_socket();
    2140             : 
    2141          20 :         if (domain != AF_INET
    2142             : #if HAVE_IPV6
    2143           8 :                 && domain != AF_INET6
    2144             : #endif
    2145          12 :                 && domain != AF_UNIX) {
    2146           1 :                 php_error_docref(NULL, E_WARNING, "invalid socket domain [%pd] specified for argument 1, assuming AF_INET", domain);
    2147           1 :                 domain = AF_INET;
    2148             :         }
    2149             : 
    2150           8 :         if (type > 10) {
    2151           1 :                 php_error_docref(NULL, E_WARNING, "invalid socket type [%pd] specified for argument 2, assuming SOCK_STREAM", type);
    2152           1 :                 type = SOCK_STREAM;
    2153             :         }
    2154             : 
    2155           8 :         if (socketpair(domain, type, protocol, fds_array) != 0) {
    2156           3 :                 SOCKETS_G(last_error) = errno;
    2157           3 :                 php_error_docref(NULL, E_WARNING, "unable to create socket pair [%d]: %s", errno, sockets_strerror(errno));
    2158           3 :                 efree(php_sock[0]);
    2159           3 :                 efree(php_sock[1]);
    2160           3 :                 RETURN_FALSE;
    2161             :         }
    2162             : 
    2163           5 :         zval_dtor(fds_array_zval);
    2164           5 :         array_init(fds_array_zval);
    2165             : 
    2166           5 :         php_sock[0]->bsd_socket = fds_array[0];
    2167           5 :         php_sock[1]->bsd_socket = fds_array[1];
    2168           5 :         php_sock[0]->type            = domain;
    2169           5 :         php_sock[1]->type            = domain;
    2170           5 :         php_sock[0]->error           = 0;
    2171           5 :         php_sock[1]->error           = 0;
    2172           5 :         php_sock[0]->blocking        = 1;
    2173           5 :         php_sock[1]->blocking        = 1;
    2174             : 
    2175           5 :         ZVAL_RES(&retval[0], zend_register_resource(php_sock[0], le_socket));
    2176           5 :         ZVAL_RES(&retval[1], zend_register_resource(php_sock[1], le_socket));
    2177             : 
    2178           5 :         add_index_zval(fds_array_zval, 0, &retval[0]);
    2179           5 :         add_index_zval(fds_array_zval, 1, &retval[1]);
    2180             : 
    2181           5 :         RETURN_TRUE;
    2182             : }
    2183             : /* }}} */
    2184             : #endif
    2185             : 
    2186             : #ifdef HAVE_SHUTDOWN
    2187             : /* {{{ proto bool socket_shutdown(resource socket[, int how])
    2188             :    Shuts down a socket for receiving, sending, or both. */
    2189           0 : PHP_FUNCTION(socket_shutdown)
    2190             : {
    2191             :         zval            *arg1;
    2192           0 :         zend_long               how_shutdown = 2;
    2193             :         php_socket      *php_sock;
    2194             : 
    2195           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &arg1, &how_shutdown) == FAILURE) {
    2196           0 :                 return;
    2197             :         }
    2198             : 
    2199           0 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    2200           0 :                 RETURN_FALSE;
    2201             :         }
    2202             : 
    2203           0 :         if (shutdown(php_sock->bsd_socket, how_shutdown) != 0) {
    2204           0 :                 PHP_SOCKET_ERROR(php_sock, "unable to shutdown socket", errno);
    2205           0 :                 RETURN_FALSE;
    2206             :         }
    2207             : 
    2208           0 :         RETURN_TRUE;
    2209             : }
    2210             : /* }}} */
    2211             : #endif
    2212             : 
    2213             : /* {{{ proto int socket_last_error([resource socket])
    2214             :    Returns the last socket error (either the last used or the provided socket resource) */
    2215           0 : PHP_FUNCTION(socket_last_error)
    2216             : {
    2217           0 :         zval            *arg1 = NULL;
    2218             :         php_socket      *php_sock;
    2219             : 
    2220           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &arg1) == FAILURE) {
    2221           0 :                 return;
    2222             :         }
    2223             : 
    2224           0 :         if (arg1) {
    2225           0 :                 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    2226           0 :                         RETURN_FALSE;
    2227             :                 }
    2228           0 :                 RETVAL_LONG(php_sock->error);
    2229             :         } else {
    2230           0 :                 RETVAL_LONG(SOCKETS_G(last_error));
    2231             :         }
    2232             : }
    2233             : /* }}} */
    2234             : 
    2235             : /* {{{ proto void socket_clear_error([resource socket])
    2236             :    Clears the error on the socket or the last error code. */
    2237           0 : PHP_FUNCTION(socket_clear_error)
    2238             : {
    2239           0 :         zval            *arg1 = NULL;
    2240             :         php_socket      *php_sock;
    2241             : 
    2242           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &arg1) == FAILURE) {
    2243           0 :                 return;
    2244             :         }
    2245             : 
    2246           0 :         if (arg1) {
    2247           0 :                 if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(arg1), le_socket_name, le_socket)) == NULL) {
    2248           0 :                         RETURN_FALSE;
    2249             :                 }
    2250           0 :                 php_sock->error = 0;
    2251             :         } else {
    2252           0 :                 SOCKETS_G(last_error) = 0;
    2253             :         }
    2254             : 
    2255           0 :         return;
    2256             : }
    2257             : /* }}} */
    2258             : 
    2259           7 : php_socket *socket_import_file_descriptor(PHP_SOCKET socket)
    2260             : {
    2261             : #ifdef SO_DOMAIN
    2262             :         int                                             type;
    2263           7 :         socklen_t                               type_len = sizeof(type);
    2264             : #endif
    2265             :         php_socket                              *retsock;
    2266             :         php_sockaddr_storage    addr;
    2267           7 :         socklen_t                               addr_len = sizeof(addr);
    2268             : #ifndef PHP_WIN32
    2269             :         int                                      t;
    2270             : #endif
    2271             : 
    2272           7 :     retsock = php_create_socket();
    2273           7 :     retsock->bsd_socket = socket;
    2274             : 
    2275             :     /* determine family */
    2276             : #ifdef SO_DOMAIN
    2277           7 :     if (getsockopt(socket, SOL_SOCKET, SO_DOMAIN, &type, &type_len) == 0) {
    2278           7 :                 retsock->type = type;
    2279             :         } else
    2280             : #endif
    2281           0 :         if (getsockname(socket, (struct sockaddr*)&addr, &addr_len) == 0) {
    2282           0 :                 retsock->type = addr.ss_family;
    2283             :         } else {
    2284           0 :                 PHP_SOCKET_ERROR(retsock, "unable to obtain socket family", errno);
    2285           0 :                 goto error;
    2286             :         }
    2287             : 
    2288             :     /* determine blocking mode */
    2289             : #ifndef PHP_WIN32
    2290           7 :     t = fcntl(socket, F_GETFL);
    2291           7 :     if (t == -1) {
    2292           0 :                 PHP_SOCKET_ERROR(retsock, "unable to obtain blocking state", errno);
    2293           0 :                 goto error;
    2294             :     } else {
    2295           7 :         retsock->blocking = !(t & O_NONBLOCK);
    2296             :     }
    2297             : #endif
    2298             : 
    2299           7 :     return retsock;
    2300             : 
    2301             : error:
    2302           0 :         efree(retsock);
    2303           0 :         return NULL;
    2304             : }
    2305             : 
    2306             : /* {{{ proto void socket_import_stream(resource stream)
    2307             :    Imports a stream that encapsulates a socket into a socket extension resource. */
    2308          14 : PHP_FUNCTION(socket_import_stream)
    2309             : {
    2310             :         zval                             *zstream;
    2311             :         php_stream                       *stream;
    2312          14 :         php_socket                       *retsock = NULL;
    2313             :         PHP_SOCKET                       socket; /* fd */
    2314             : 
    2315          14 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zstream) == FAILURE) {
    2316           4 :                 return;
    2317             :         }
    2318          10 :         php_stream_from_zval(stream, zstream);
    2319             : 
    2320           8 :         if (php_stream_cast(stream, PHP_STREAM_AS_SOCKETD, (void**)&socket, 1)) {
    2321             :                 /* error supposedly already shown */
    2322           1 :                 RETURN_FALSE;
    2323             :         }
    2324             : 
    2325           7 :         retsock = socket_import_file_descriptor(socket);
    2326           7 :         if (retsock == NULL) {
    2327           0 :                 RETURN_FALSE;
    2328             :         }
    2329             : 
    2330             : #ifdef PHP_WIN32
    2331             :         /* on windows, check if the stream is a socket stream and read its
    2332             :          * private data; otherwise assume it's in non-blocking mode */
    2333             :         if (php_stream_is(stream, PHP_STREAM_IS_SOCKET)) {
    2334             :                 retsock->blocking =
    2335             :                                 ((php_netstream_data_t *)stream->abstract)->is_blocked;
    2336             :         } else {
    2337             :                 retsock->blocking = 1;
    2338             :         }
    2339             : #endif
    2340             : 
    2341             :         /* hold a zval reference to the stream (holding a php_stream* directly could
    2342             :          * also be done, but this might be slightly better if in the future we want
    2343             :          * to provide a socket_export_stream) */
    2344           7 :         ZVAL_COPY(&retsock->zstream, zstream);
    2345             : 
    2346           7 :         php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER,
    2347             :                 PHP_STREAM_BUFFER_NONE, NULL);
    2348             : 
    2349           7 :         RETURN_RES(zend_register_resource(retsock, le_socket));
    2350             : }
    2351             : /* }}} */
    2352             : 
    2353             : /*
    2354             :  * Local variables:
    2355             :  * tab-width: 4
    2356             :  * c-basic-offset: 4
    2357             :  * End:
    2358             :  * vim600: fdm=marker
    2359             :  * vim: noet sw=4 ts=4
    2360             :  */

Generated by: LCOV version 1.10

Generated at Sat, 27 Jun 2015 09:41:18 +0000 (6 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.