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 - main - network.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 198 358 55.3 %
Date: 2014-04-16 Functions: 13 18 72.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 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             :    | Author: Stig Venaas <venaas@uninett.no>                              |
      16             :    | Streams work by Wez Furlong <wez@thebrainroom.com>                   |
      17             :    +----------------------------------------------------------------------+
      18             :  */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : /*#define DEBUG_MAIN_NETWORK 1*/
      23             : 
      24             : #include "php.h"
      25             : 
      26             : #include <stddef.h>
      27             : 
      28             : 
      29             : 
      30             : #ifdef PHP_WIN32
      31             : # include <Ws2tcpip.h>
      32             : # include "win32/inet.h"
      33             : # define O_RDONLY _O_RDONLY
      34             : # include "win32/param.h"
      35             : #elif defined(NETWARE)
      36             : #include <sys/timeval.h>
      37             : #include <sys/param.h>
      38             : #else
      39             : #include <sys/param.h>
      40             : #endif
      41             : 
      42             : #include <sys/types.h>
      43             : #if HAVE_SYS_SOCKET_H
      44             : #include <sys/socket.h>
      45             : #endif
      46             : 
      47             : #ifndef _FCNTL_H
      48             : #include <fcntl.h>
      49             : #endif
      50             : 
      51             : #ifdef HAVE_SYS_SELECT_H
      52             : #include <sys/select.h>
      53             : #endif
      54             : #if HAVE_SYS_POLL_H
      55             : #include <sys/poll.h>
      56             : #endif
      57             : 
      58             : #if defined(NETWARE)
      59             : #ifdef USE_WINSOCK
      60             : #include <novsock2.h>
      61             : #else
      62             : #include <arpa/inet.h>
      63             : #include <netinet/in.h>
      64             : #include <netdb.h>
      65             : #include <sys/select.h>
      66             : #include <sys/socket.h>
      67             : #endif
      68             : #elif !defined(PHP_WIN32)
      69             : #include <netinet/in.h>
      70             : #include <netdb.h>
      71             : #if HAVE_ARPA_INET_H
      72             : #include <arpa/inet.h>
      73             : #endif
      74             : #endif
      75             : 
      76             : #ifndef HAVE_INET_ATON
      77             : int inet_aton(const char *, struct in_addr *);
      78             : #endif
      79             : 
      80             : #include "php_network.h"
      81             : 
      82             : #if defined(PHP_WIN32) || defined(__riscos__) || defined(NETWARE)
      83             : #undef AF_UNIX
      84             : #endif
      85             : 
      86             : #if defined(AF_UNIX)
      87             : #include <sys/un.h>
      88             : #endif
      89             : 
      90             : #include "ext/standard/file.h"
      91             : 
      92             : #ifdef PHP_WIN32
      93             : # include "win32/time.h"
      94             : # define SOCK_ERR INVALID_SOCKET
      95             : # define SOCK_CONN_ERR SOCKET_ERROR
      96             : # define PHP_TIMEOUT_ERROR_VALUE                WSAETIMEDOUT
      97             : 
      98             : #if HAVE_IPV6
      99             : const struct in6_addr in6addr_any = {0}; /* IN6ADDR_ANY_INIT; */
     100             : #endif
     101             : 
     102             : #else
     103             : # define SOCK_ERR -1
     104             : # define SOCK_CONN_ERR -1
     105             : # define PHP_TIMEOUT_ERROR_VALUE                ETIMEDOUT
     106             : #endif
     107             : 
     108             : #if HAVE_GETADDRINFO
     109             : #ifdef HAVE_GAI_STRERROR
     110             : #  define PHP_GAI_STRERROR(x) (gai_strerror(x))
     111             : #else
     112             : #  define PHP_GAI_STRERROR(x) (php_gai_strerror(x))
     113             : /* {{{ php_gai_strerror
     114             :  */
     115             : static const char *php_gai_strerror(int code)
     116             : {
     117             :         static struct {
     118             :                 int code;
     119             :                 const char *msg;
     120             :         } values[] = {
     121             : #  ifdef EAI_ADDRFAMILY
     122             :                 {EAI_ADDRFAMILY, "Address family for hostname not supported"},
     123             : #  endif
     124             :                 {EAI_AGAIN, "Temporary failure in name resolution"},
     125             :                 {EAI_BADFLAGS, "Bad value for ai_flags"},
     126             :                 {EAI_FAIL, "Non-recoverable failure in name resolution"},
     127             :                 {EAI_FAMILY, "ai_family not supported"},
     128             :                 {EAI_MEMORY, "Memory allocation failure"},
     129             : #  ifdef EAI_NODATA
     130             :                 {EAI_NODATA, "No address associated with hostname"},
     131             : #  endif
     132             :                 {EAI_NONAME, "Name or service not known"},
     133             :                 {EAI_SERVICE, "Servname not supported for ai_socktype"},
     134             :                 {EAI_SOCKTYPE, "ai_socktype not supported"},
     135             :                 {EAI_SYSTEM, "System error"},
     136             :                 {0, NULL}
     137             :         };
     138             :         int i;
     139             : 
     140             :         for (i = 0; values[i].msg != NULL; i++) {
     141             :                 if (values[i].code == code) {
     142             :                         return (char *)values[i].msg;
     143             :                 }
     144             :         }
     145             : 
     146             :         return "Unknown error";
     147             : }
     148             : /* }}} */
     149             : #endif
     150             : #endif
     151             : 
     152             : /* {{{ php_network_freeaddresses
     153             :  */
     154        1981 : PHPAPI void php_network_freeaddresses(struct sockaddr **sal)
     155             : {
     156             :         struct sockaddr **sap;
     157             : 
     158        1981 :         if (sal == NULL)
     159           0 :                 return;
     160        4779 :         for (sap = sal; *sap != NULL; sap++)
     161        2798 :                 efree(*sap);
     162        1981 :         efree(sal);
     163             : }
     164             : /* }}} */
     165             : 
     166             : /* {{{ php_network_getaddresses
     167             :  * Returns number of addresses, 0 for none/error
     168             :  */
     169        1992 : PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, char **error_string TSRMLS_DC)
     170             : {
     171             :         struct sockaddr **sap;
     172             :         int n;
     173             : #if HAVE_GETADDRINFO
     174             : # if HAVE_IPV6
     175             :         static int ipv6_borked = -1; /* the way this is used *is* thread safe */
     176             : # endif
     177             :         struct addrinfo hints, *res, *sai;
     178             : #else
     179             :         struct hostent *host_info;
     180             :         struct in_addr in;
     181             : #endif
     182             : 
     183        1992 :         if (host == NULL) {
     184           0 :                 return 0;
     185             :         }
     186             : #if HAVE_GETADDRINFO
     187        1992 :         memset(&hints, '\0', sizeof(hints));
     188             : 
     189        1992 :         hints.ai_family = AF_INET; /* default to regular inet (see below) */
     190        1992 :         hints.ai_socktype = socktype;
     191             : 
     192             : # if HAVE_IPV6
     193             :         /* probe for a working IPv6 stack; even if detected as having v6 at compile
     194             :          * time, at runtime some stacks are slow to resolve or have other issues
     195             :          * if they are not correctly configured.
     196             :          * static variable use is safe here since simple store or fetch operations
     197             :          * are atomic and because the actual probe process is not in danger of
     198             :          * collisions or race conditions. */
     199        1992 :         if (ipv6_borked == -1) {
     200             :                 int s;
     201             : 
     202         227 :                 s = socket(PF_INET6, SOCK_DGRAM, 0);
     203         227 :                 if (s == SOCK_ERR) {
     204           0 :                         ipv6_borked = 1;
     205             :                 } else {
     206         227 :                         ipv6_borked = 0;
     207         227 :                         closesocket(s);
     208             :                 }
     209             :         }
     210        1992 :         hints.ai_family = ipv6_borked ? AF_INET : AF_UNSPEC;
     211             : # endif
     212             : 
     213        1992 :         if ((n = getaddrinfo(host, NULL, &hints, &res))) {
     214          11 :                 if (error_string) {
     215           9 :                         spprintf(error_string, 0, "php_network_getaddresses: getaddrinfo failed: %s", PHP_GAI_STRERROR(n));
     216           9 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", *error_string);
     217             :                 } else {
     218           2 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "php_network_getaddresses: getaddrinfo failed: %s", PHP_GAI_STRERROR(n));
     219             :                 }
     220          11 :                 return 0;
     221        1981 :         } else if (res == NULL) {
     222           0 :                 if (error_string) {
     223           0 :                         spprintf(error_string, 0, "php_network_getaddresses: getaddrinfo failed (null result pointer) errno=%d", errno);
     224           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", *error_string);
     225             :                 } else {
     226           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "php_network_getaddresses: getaddrinfo failed (null result pointer)");
     227             :                 }
     228           0 :                 return 0;
     229             :         }
     230             : 
     231        1981 :         sai = res;
     232        1981 :         for (n = 1; (sai = sai->ai_next) != NULL; n++)
     233             :                 ;
     234             : 
     235        1981 :         *sal = safe_emalloc((n + 1), sizeof(*sal), 0);
     236        1981 :         sai = res;
     237        1981 :         sap = *sal;
     238             : 
     239             :         do {
     240        2798 :                 *sap = emalloc(sai->ai_addrlen);
     241        2798 :                 memcpy(*sap, sai->ai_addr, sai->ai_addrlen);
     242        2798 :                 sap++;
     243        2798 :         } while ((sai = sai->ai_next) != NULL);
     244             : 
     245        1981 :         freeaddrinfo(res);
     246             : #else
     247             :         if (!inet_aton(host, &in)) {
     248             :                 /* XXX NOT THREAD SAFE (is safe under win32) */
     249             :                 host_info = gethostbyname(host);
     250             :                 if (host_info == NULL) {
     251             :                         if (error_string) {
     252             :                                 spprintf(error_string, 0, "php_network_getaddresses: gethostbyname failed. errno=%d", errno);
     253             :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", *error_string);
     254             :                         } else {
     255             :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "php_network_getaddresses: gethostbyname failed");
     256             :                         }
     257             :                         return 0;
     258             :                 }
     259             :                 in = *((struct in_addr *) host_info->h_addr);
     260             :         }
     261             : 
     262             :         *sal = safe_emalloc(2, sizeof(*sal), 0);
     263             :         sap = *sal;
     264             :         *sap = emalloc(sizeof(struct sockaddr_in));
     265             :         (*sap)->sa_family = AF_INET;
     266             :         ((struct sockaddr_in *)*sap)->sin_addr = in;
     267             :         sap++;
     268             :         n = 1;
     269             : #endif
     270             : 
     271        1981 :         *sap = NULL;
     272        1981 :         return n;
     273             : }
     274             : /* }}} */
     275             : 
     276             : #ifndef O_NONBLOCK
     277             : #define O_NONBLOCK O_NDELAY
     278             : #endif
     279             : 
     280             : #if !defined(__BEOS__)
     281             : # define HAVE_NON_BLOCKING_CONNECT 1
     282             : # ifdef PHP_WIN32
     283             : typedef u_long php_non_blocking_flags_t;
     284             : #  define SET_SOCKET_BLOCKING_MODE(sock, save) \
     285             :      save = TRUE; ioctlsocket(sock, FIONBIO, &save)
     286             : #  define RESTORE_SOCKET_BLOCKING_MODE(sock, save) \
     287             :          ioctlsocket(sock, FIONBIO, &save)
     288             : # else
     289             : typedef int php_non_blocking_flags_t;
     290             : #  define SET_SOCKET_BLOCKING_MODE(sock, save) \
     291             :          save = fcntl(sock, F_GETFL, 0); \
     292             :          fcntl(sock, F_SETFL, save | O_NONBLOCK)
     293             : #  define RESTORE_SOCKET_BLOCKING_MODE(sock, save) \
     294             :          fcntl(sock, F_SETFL, save)
     295             : # endif
     296             : #endif
     297             : 
     298             : /* Connect to a socket using an interruptible connect with optional timeout.
     299             :  * Optionally, the connect can be made asynchronously, which will implicitly
     300             :  * enable non-blocking mode on the socket.
     301             :  * */
     302             : /* {{{ php_network_connect_socket */
     303        4292 : PHPAPI int php_network_connect_socket(php_socket_t sockfd,
     304             :                 const struct sockaddr *addr,
     305             :                 socklen_t addrlen,
     306             :                 int asynchronous,
     307             :                 struct timeval *timeout,
     308             :                 char **error_string,
     309             :                 int *error_code)
     310             : {
     311             : #if HAVE_NON_BLOCKING_CONNECT
     312             :         php_non_blocking_flags_t orig_flags;
     313             :         int n;
     314        4292 :         int error = 0;
     315             :         socklen_t len;
     316        4292 :         int ret = 0;
     317             : 
     318        4292 :         SET_SOCKET_BLOCKING_MODE(sockfd, orig_flags);
     319             : 
     320        4292 :         if ((n = connect(sockfd, addr, addrlen)) != 0) {
     321        1814 :                 error = php_socket_errno();
     322             : 
     323        1814 :                 if (error_code) {
     324        1783 :                         *error_code = error;
     325             :                 }
     326             : 
     327        1814 :                 if (error != EINPROGRESS) {
     328           5 :                         if (error_string) {
     329           5 :                                 *error_string = php_socket_strerror(error, NULL, 0);
     330             :                         }
     331             : 
     332           5 :                         return -1;
     333             :                 }
     334        1809 :                 if (asynchronous && error == EINPROGRESS) {
     335             :                         /* this is fine by us */
     336           1 :                         return 0;
     337             :                 }
     338             :         }
     339             : 
     340        4286 :         if (n == 0) {
     341        2478 :                 goto ok;
     342             :         }
     343             : # ifdef PHP_WIN32
     344             :         /* The documentation for connect() says in case of non-blocking connections
     345             :          * the select function reports success in the writefds set and failure in
     346             :          * the exceptfds set. Indeed, using PHP_POLLREADABLE results in select
     347             :          * failing only due to the timeout and not immediately as would be
     348             :          * expected when a connection is actively refused. This way,
     349             :          * php_pollfd_for will return a mask with POLLOUT if the connection
     350             :          * is successful and with POLLPRI otherwise. */
     351             :         if ((n = php_pollfd_for(sockfd, POLLOUT|POLLPRI, timeout)) == 0) {
     352             : #else
     353        1808 :         if ((n = php_pollfd_for(sockfd, PHP_POLLREADABLE|POLLOUT, timeout)) == 0) {
     354             : #endif
     355           0 :                 error = PHP_TIMEOUT_ERROR_VALUE;
     356             :         }
     357             : 
     358        1808 :         if (n > 0) {
     359        1808 :                 len = sizeof(error);
     360             :                 /*
     361             :                    BSD-derived systems set errno correctly
     362             :                    Solaris returns -1 from getsockopt in case of error
     363             :                    */
     364        1808 :                 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (char*)&error, &len) != 0) {
     365           0 :                         ret = -1;
     366             :                 }
     367             :         } else {
     368             :                 /* whoops: sockfd has disappeared */
     369           0 :                 ret = -1;
     370             :         }
     371             : 
     372             : ok:
     373        4286 :         if (!asynchronous) {
     374             :                 /* back to blocking mode */
     375        4286 :                 RESTORE_SOCKET_BLOCKING_MODE(sockfd, orig_flags);
     376             :         }
     377             : 
     378        4286 :         if (error_code) {
     379        4255 :                 *error_code = error;
     380             :         }
     381             : 
     382        4286 :         if (error) {
     383        1621 :                 ret = -1;
     384        1621 :                 if (error_string) {
     385        1621 :                         *error_string = php_socket_strerror(error, NULL, 0);
     386             :                 }
     387             :         }
     388        4286 :         return ret;
     389             : #else
     390             :         if (asynchronous) {
     391             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Asynchronous connect() not supported on this platform");
     392             :         }
     393             :         return (connect(sockfd, addr, addrlen) == 0) ? 0 : -1;
     394             : #endif
     395             : }
     396             : /* }}} */
     397             : 
     398             : /* {{{ sub_times */
     399        1621 : static inline void sub_times(struct timeval a, struct timeval b, struct timeval *result)
     400             : {
     401        1621 :         result->tv_usec = a.tv_usec - b.tv_usec;
     402        1621 :         if (result->tv_usec < 0L) {
     403        1600 :                 a.tv_sec--;
     404        1600 :                 result->tv_usec += 1000000L;
     405             :         }
     406        1621 :         result->tv_sec = a.tv_sec - b.tv_sec;
     407        1621 :         if (result->tv_sec < 0L) {
     408           0 :                 result->tv_sec++;
     409           0 :                 result->tv_usec -= 1000000L;
     410             :         }
     411        1621 : }
     412             : /* }}} */
     413             : 
     414             : /* Bind to a local IP address.
     415             :  * Returns the bound socket, or -1 on failure.
     416             :  * */
     417             : /* {{{ php_network_bind_socket_to_local_addr */
     418         116 : php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port,
     419             :                 int socktype, char **error_string, int *error_code
     420             :                 TSRMLS_DC)
     421             : {
     422         116 :         int num_addrs, n, err = 0;
     423             :         php_socket_t sock;
     424             :         struct sockaddr **sal, **psal, *sa;
     425             :         socklen_t socklen;
     426             : 
     427         116 :         num_addrs = php_network_getaddresses(host, socktype, &psal, error_string TSRMLS_CC);
     428             : 
     429         116 :         if (num_addrs == 0) {
     430             :                 /* could not resolve address(es) */
     431           0 :                 return -1;
     432             :         }
     433             : 
     434         116 :         for (sal = psal; *sal != NULL; sal++) {
     435         116 :                 sa = *sal;
     436             : 
     437             :                 /* create a socket for this address */
     438         116 :                 sock = socket(sa->sa_family, socktype, 0);
     439             : 
     440         116 :                 if (sock == SOCK_ERR) {
     441           0 :                         continue;
     442             :                 }
     443             : 
     444         116 :                 switch (sa->sa_family) {
     445             : #if HAVE_GETADDRINFO && HAVE_IPV6
     446             :                         case AF_INET6:
     447           2 :                                 ((struct sockaddr_in6 *)sa)->sin6_family = sa->sa_family;
     448           2 :                                 ((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
     449           2 :                                 socklen = sizeof(struct sockaddr_in6);
     450           2 :                                 break;
     451             : #endif
     452             :                         case AF_INET:
     453         114 :                                 ((struct sockaddr_in *)sa)->sin_family = sa->sa_family;
     454         114 :                                 ((struct sockaddr_in *)sa)->sin_port = htons(port);
     455         114 :                                 socklen = sizeof(struct sockaddr_in);
     456         114 :                                 break;
     457             :                         default:
     458             :                                 /* Unknown family */
     459           0 :                                 socklen = 0;
     460           0 :                                 sa = NULL;
     461             :                 }
     462             : 
     463         116 :                 if (sa) {
     464             :                         /* attempt to bind */
     465             : 
     466             : #ifdef SO_REUSEADDR
     467             :                         {
     468         116 :                                 int val = 1;
     469         116 :                                 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&val, sizeof(val));
     470             :                         }
     471             : #endif
     472             : 
     473         116 :                         n = bind(sock, sa, socklen);
     474             : 
     475         116 :                         if (n != SOCK_CONN_ERR) {
     476         116 :                                 goto bound;
     477             :                         }
     478             : 
     479           0 :                         err = php_socket_errno();
     480             :                 }
     481             : 
     482           0 :                 closesocket(sock);
     483             :         }
     484           0 :         sock = -1;
     485             : 
     486           0 :         if (error_code) {
     487           0 :                 *error_code = err;
     488             :         }
     489           0 :         if (error_string) {
     490           0 :                 *error_string = php_socket_strerror(err, NULL, 0);
     491             :         }
     492             : 
     493             : bound:
     494             : 
     495         116 :         php_network_freeaddresses(psal);
     496             : 
     497         116 :         return sock;
     498             : 
     499             : }
     500             : /* }}} */
     501             : 
     502           0 : PHPAPI int php_network_parse_network_address_with_port(const char *addr, long addrlen, struct sockaddr *sa, socklen_t *sl TSRMLS_DC)
     503             : {
     504             :         char *colon;
     505             :         char *tmp;
     506           0 :         int ret = FAILURE;
     507             :         short port;
     508           0 :         struct sockaddr_in *in4 = (struct sockaddr_in*)sa;
     509             :         struct sockaddr **psal;
     510             :         int n;
     511           0 :         char *errstr = NULL;
     512             : #if HAVE_IPV6
     513           0 :         struct sockaddr_in6 *in6 = (struct sockaddr_in6*)sa;
     514             : #endif
     515             : 
     516           0 :         if (*addr == '[') {
     517           0 :                 colon = memchr(addr + 1, ']', addrlen-1);
     518           0 :                 if (!colon || colon[1] != ':') {
     519           0 :                         return FAILURE;
     520             :                 }
     521           0 :                 port = atoi(colon + 2);
     522           0 :                 addr++;
     523             :         } else {
     524           0 :                 colon = memchr(addr, ':', addrlen);
     525           0 :                 if (!colon) {
     526           0 :                         return FAILURE;
     527             :                 }
     528           0 :                 port = atoi(colon + 1);
     529             :         }
     530             : 
     531           0 :         tmp = estrndup(addr, colon - addr);
     532             : 
     533             :         /* first, try interpreting the address as a numeric address */
     534             : 
     535             : #if HAVE_IPV6 && HAVE_INET_PTON
     536           0 :         if (inet_pton(AF_INET6, tmp, &in6->sin6_addr) > 0) {
     537           0 :                 in6->sin6_port = htons(port);
     538           0 :                 in6->sin6_family = AF_INET6;
     539           0 :                 *sl = sizeof(struct sockaddr_in6);
     540           0 :                 ret = SUCCESS;
     541           0 :                 goto out;
     542             :         }
     543             : #endif
     544           0 :         if (inet_aton(tmp, &in4->sin_addr) > 0) {
     545           0 :                 in4->sin_port = htons(port);
     546           0 :                 in4->sin_family = AF_INET;
     547           0 :                 *sl = sizeof(struct sockaddr_in);
     548           0 :                 ret = SUCCESS;
     549           0 :                 goto out;
     550             :         }
     551             : 
     552             :         /* looks like we'll need to resolve it */
     553           0 :         n = php_network_getaddresses(tmp, SOCK_DGRAM, &psal, &errstr TSRMLS_CC);
     554             : 
     555           0 :         if (n == 0) {
     556           0 :                 if (errstr) {
     557           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to resolve `%s': %s", tmp, errstr);
     558           0 :                         STR_FREE(errstr);
     559             :                 }
     560           0 :                 goto out;
     561             :         }
     562             : 
     563             :         /* copy the details from the first item */
     564           0 :         switch ((*psal)->sa_family) {
     565             : #if HAVE_GETADDRINFO && HAVE_IPV6
     566             :                 case AF_INET6:
     567           0 :                         *in6 = **(struct sockaddr_in6**)psal;
     568           0 :                         in6->sin6_port = htons(port);
     569           0 :                         *sl = sizeof(struct sockaddr_in6);
     570           0 :                         ret = SUCCESS;
     571           0 :                         break;
     572             : #endif
     573             :                 case AF_INET:
     574           0 :                         *in4 = **(struct sockaddr_in**)psal;
     575           0 :                         in4->sin_port = htons(port);
     576           0 :                         *sl = sizeof(struct sockaddr_in);
     577           0 :                         ret = SUCCESS;
     578             :                         break;
     579             :         }
     580             : 
     581           0 :         php_network_freeaddresses(psal);
     582             : 
     583             : out:
     584           0 :         STR_FREE(tmp);
     585           0 :         return ret;
     586             : }
     587             : 
     588             : 
     589          96 : PHPAPI void php_network_populate_name_from_sockaddr(
     590             :                 /* input address */
     591             :                 struct sockaddr *sa, socklen_t sl,
     592             :                 /* output readable address */
     593             :                 char **textaddr, long *textaddrlen,
     594             :                 /* output address */
     595             :                 struct sockaddr **addr,
     596             :                 socklen_t *addrlen
     597             :                 TSRMLS_DC)
     598             : {
     599          96 :         if (addr) {
     600           0 :                 *addr = emalloc(sl);
     601           0 :                 memcpy(*addr, sa, sl);
     602           0 :                 *addrlen = sl;
     603             :         }
     604             : 
     605          96 :         if (textaddr) {
     606             : #if HAVE_IPV6 && HAVE_INET_NTOP
     607             :                 char abuf[256];
     608             : #endif
     609           0 :                 char *buf = NULL;
     610             : 
     611           0 :                 switch (sa->sa_family) {
     612             :                         case AF_INET:
     613             :                                 /* generally not thread safe, but it *is* thread safe under win32 */
     614           0 :                                 buf = inet_ntoa(((struct sockaddr_in*)sa)->sin_addr);
     615           0 :                                 if (buf) {
     616           0 :                                         *textaddrlen = spprintf(textaddr, 0, "%s:%d",
     617           0 :                                                 buf, ntohs(((struct sockaddr_in*)sa)->sin_port));
     618             :                                 }
     619             : 
     620           0 :                                 break;
     621             : 
     622             : #if HAVE_IPV6 && HAVE_INET_NTOP
     623             :                         case AF_INET6:
     624           0 :                                 buf = (char*)inet_ntop(sa->sa_family, &((struct sockaddr_in6*)sa)->sin6_addr, (char *)&abuf, sizeof(abuf));
     625           0 :                                 if (buf) {
     626           0 :                                         *textaddrlen = spprintf(textaddr, 0, "%s:%d",
     627           0 :                                                 buf, ntohs(((struct sockaddr_in6*)sa)->sin6_port));
     628             :                                 }
     629             : 
     630           0 :                                 break;
     631             : #endif
     632             : #ifdef AF_UNIX
     633             :                         case AF_UNIX:
     634             :                                 {
     635           0 :                                         struct sockaddr_un *ua = (struct sockaddr_un*)sa;
     636             : 
     637           0 :                                         if (ua->sun_path[0] == '\0') {
     638             :                                                 /* abstract name */
     639           0 :                                                 int len = strlen(ua->sun_path + 1) + 1;
     640           0 :                                                 *textaddrlen = len;
     641           0 :                                                 *textaddr = emalloc(len + 1);
     642           0 :                                                 memcpy(*textaddr, ua->sun_path, len);
     643           0 :                                                 (*textaddr)[len] = '\0';
     644             :                                         } else {
     645           0 :                                                 *textaddrlen = strlen(ua->sun_path);
     646           0 :                                                 *textaddr = estrndup(ua->sun_path, *textaddrlen);
     647             :                                         }
     648             :                                 }
     649             :                                 break;
     650             : #endif
     651             : 
     652             :                 }
     653             : 
     654             :         }
     655          96 : }
     656             : 
     657           0 : PHPAPI int php_network_get_peer_name(php_socket_t sock,
     658             :                 char **textaddr, long *textaddrlen,
     659             :                 struct sockaddr **addr,
     660             :                 socklen_t *addrlen
     661             :                 TSRMLS_DC)
     662             : {
     663             :         php_sockaddr_storage sa;
     664           0 :         socklen_t sl = sizeof(sa);
     665           0 :         memset(&sa, 0, sizeof(sa));
     666             : 
     667           0 :         if (getpeername(sock, (struct sockaddr*)&sa, &sl) == 0) {
     668           0 :                 php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
     669             :                                 textaddr, textaddrlen,
     670             :                                 addr, addrlen
     671             :                                 TSRMLS_CC);
     672           0 :                 return 0;
     673             :         }
     674           0 :         return -1;
     675             : }
     676             : 
     677           0 : PHPAPI int php_network_get_sock_name(php_socket_t sock,
     678             :                 char **textaddr, long *textaddrlen,
     679             :                 struct sockaddr **addr,
     680             :                 socklen_t *addrlen
     681             :                 TSRMLS_DC)
     682             : {
     683             :         php_sockaddr_storage sa;
     684           0 :         socklen_t sl = sizeof(sa);
     685           0 :         memset(&sa, 0, sizeof(sa));
     686             : 
     687           0 :         if (getsockname(sock, (struct sockaddr*)&sa, &sl) == 0) {
     688           0 :                 php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
     689             :                                 textaddr, textaddrlen,
     690             :                                 addr, addrlen
     691             :                                 TSRMLS_CC);
     692           0 :                 return 0;
     693             :         }
     694           0 :         return -1;
     695             : 
     696             : }
     697             : 
     698             : 
     699             : /* Accept a client connection from a server socket,
     700             :  * using an optional timeout.
     701             :  * Returns the peer address in addr/addrlen (it will emalloc
     702             :  * these, so be sure to efree the result).
     703             :  * If you specify textaddr/textaddrlen, a text-printable
     704             :  * version of the address will be emalloc'd and returned.
     705             :  * */
     706             : 
     707             : /* {{{ php_network_accept_incoming */
     708          97 : PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock,
     709             :                 char **textaddr, long *textaddrlen,
     710             :                 struct sockaddr **addr,
     711             :                 socklen_t *addrlen,
     712             :                 struct timeval *timeout,
     713             :                 char **error_string,
     714             :                 int *error_code
     715             :                 TSRMLS_DC)
     716             : {
     717          97 :         php_socket_t clisock = -1;
     718          97 :         int error = 0, n;
     719             :         php_sockaddr_storage sa;
     720             :         socklen_t sl;
     721             : 
     722          97 :         n = php_pollfd_for(srvsock, PHP_POLLREADABLE, timeout);
     723             : 
     724          97 :         if (n == 0) {
     725           1 :                 error = PHP_TIMEOUT_ERROR_VALUE;
     726          96 :         } else if (n == -1) {
     727           0 :                 error = php_socket_errno();
     728             :         } else {
     729          96 :                 sl = sizeof(sa);
     730             : 
     731          96 :                 clisock = accept(srvsock, (struct sockaddr*)&sa, &sl);
     732             : 
     733          96 :                 if (clisock != SOCK_ERR) {
     734          96 :                         php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
     735             :                                         textaddr, textaddrlen,
     736             :                                         addr, addrlen
     737             :                                         TSRMLS_CC);
     738             :                 } else {
     739           0 :                         error = php_socket_errno();
     740             :                 }
     741             :         }
     742             : 
     743          97 :         if (error_code) {
     744          97 :                 *error_code = error;
     745             :         }
     746          97 :         if (error_string) {
     747          97 :                 *error_string = php_socket_strerror(error, NULL, 0);
     748             :         }
     749             : 
     750          97 :         return clisock;
     751             : }
     752             : /* }}} */
     753             : 
     754             : 
     755             : 
     756             : /* Connect to a remote host using an interruptible connect with optional timeout.
     757             :  * Optionally, the connect can be made asynchronously, which will implicitly
     758             :  * enable non-blocking mode on the socket.
     759             :  * Returns the connected (or connecting) socket, or -1 on failure.
     760             :  * */
     761             : 
     762             : /* {{{ php_network_connect_socket_to_host */
     763        1629 : php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
     764             :                 int socktype, int asynchronous, struct timeval *timeout, char **error_string,
     765             :                 int *error_code, char *bindto, unsigned short bindport
     766             :                 TSRMLS_DC)
     767             : {
     768        1629 :         int num_addrs, n, fatal = 0;
     769             :         php_socket_t sock;
     770             :         struct sockaddr **sal, **psal, *sa;
     771             :         struct timeval working_timeout;
     772             :         socklen_t socklen;
     773             : #if HAVE_GETTIMEOFDAY
     774             :         struct timeval limit_time, time_now;
     775             : #endif
     776             : 
     777        1629 :         num_addrs = php_network_getaddresses(host, socktype, &psal, error_string TSRMLS_CC);
     778             : 
     779        1629 :         if (num_addrs == 0) {
     780             :                 /* could not resolve address(es) */
     781          10 :                 return -1;
     782             :         }
     783             : 
     784        1619 :         if (timeout) {
     785        1619 :                 memcpy(&working_timeout, timeout, sizeof(working_timeout));
     786             : #if HAVE_GETTIMEOFDAY
     787        1619 :                 gettimeofday(&limit_time, NULL);
     788        1619 :                 limit_time.tv_sec += working_timeout.tv_sec;
     789        1619 :                 limit_time.tv_usec += working_timeout.tv_usec;
     790        1619 :                 if (limit_time.tv_usec >= 1000000) {
     791          13 :                         limit_time.tv_usec -= 1000000;
     792          13 :                         limit_time.tv_sec++;
     793             :                 }
     794             : #endif
     795             :         }
     796             : 
     797        3240 :         for (sal = psal; !fatal && *sal != NULL; sal++) {
     798        2428 :                 sa = *sal;
     799             : 
     800             :                 /* create a socket for this address */
     801        2428 :                 sock = socket(sa->sa_family, socktype, 0);
     802             : 
     803        2428 :                 if (sock == SOCK_ERR) {
     804           0 :                         continue;
     805             :                 }
     806             : 
     807        2428 :                 switch (sa->sa_family) {
     808             : #if HAVE_GETADDRINFO && HAVE_IPV6
     809             :                         case AF_INET6:
     810        1646 :                                 if (!bindto || strchr(bindto, ':')) {
     811         823 :                                         ((struct sockaddr_in6 *)sa)->sin6_family = sa->sa_family;
     812         823 :                                         ((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
     813         823 :                                         socklen = sizeof(struct sockaddr_in6);
     814             :                                 } else {
     815           0 :                                         socklen = 0;
     816           0 :                                         sa = NULL;
     817             :                                 }
     818         823 :                                 break;
     819             : #endif
     820             :                         case AF_INET:
     821        1605 :                                 ((struct sockaddr_in *)sa)->sin_family = sa->sa_family;
     822        1605 :                                 ((struct sockaddr_in *)sa)->sin_port = htons(port);
     823        1605 :                                 socklen = sizeof(struct sockaddr_in);
     824        1605 :                                 break;
     825             :                         default:
     826             :                                 /* Unknown family */
     827           0 :                                 socklen = 0;
     828           0 :                                 sa = NULL;
     829             :                 }
     830             : 
     831        2428 :                 if (sa) {
     832             :                         /* make a connection attempt */
     833             : 
     834        2428 :                         if (bindto) {
     835           0 :                                 struct sockaddr *local_address = NULL;
     836           0 :                                 int local_address_len = 0;
     837             : 
     838           0 :                                 if (sa->sa_family == AF_INET) {
     839           0 :                                         struct sockaddr_in *in4 = emalloc(sizeof(struct sockaddr_in));
     840             : 
     841           0 :                                         local_address = (struct sockaddr*)in4;
     842           0 :                                         local_address_len = sizeof(struct sockaddr_in);
     843             : 
     844           0 :                                         in4->sin_family = sa->sa_family;
     845           0 :                                         in4->sin_port = htons(bindport);
     846           0 :                                         if (!inet_aton(bindto, &in4->sin_addr)) {
     847           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid IP Address: %s", bindto);
     848           0 :                                                 goto skip_bind;
     849             :                                         }
     850           0 :                                         memset(&(in4->sin_zero), 0, sizeof(in4->sin_zero));
     851             :                                 }
     852             : #if HAVE_IPV6 && HAVE_INET_PTON
     853             :                                  else { /* IPV6 */
     854           0 :                                         struct sockaddr_in6 *in6 = emalloc(sizeof(struct sockaddr_in6));
     855             : 
     856           0 :                                         local_address = (struct sockaddr*)in6;
     857           0 :                                         local_address_len = sizeof(struct sockaddr_in6);
     858             : 
     859           0 :                                         in6->sin6_family = sa->sa_family;
     860           0 :                                         in6->sin6_port = htons(bindport);
     861           0 :                                         if (inet_pton(AF_INET6, bindto, &in6->sin6_addr) < 1) {
     862           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid IP Address: %s", bindto);
     863           0 :                                                 goto skip_bind;
     864             :                                         }
     865             :                                 }
     866             : #endif
     867           0 :                                 if (!local_address || bind(sock, local_address, local_address_len)) {
     868           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to bind to '%s:%d', system said: %s", bindto, bindport, strerror(errno));
     869             :                                 }
     870             : skip_bind:
     871           0 :                                 if (local_address) {
     872           0 :                                         efree(local_address);
     873             :                                 }
     874             :                         }
     875             :                         /* free error string received during previous iteration (if any) */
     876        2428 :                         if (error_string && *error_string) {
     877         809 :                                 efree(*error_string);
     878         809 :                                 *error_string = NULL;
     879             :                         }
     880             : 
     881        2428 :                         n = php_network_connect_socket(sock, sa, socklen, asynchronous,
     882             :                                         timeout ? &working_timeout : NULL,
     883             :                                         error_string, error_code);
     884             : 
     885        2428 :                         if (n != -1) {
     886         807 :                                 goto connected;
     887             :                         }
     888             : 
     889             :                         /* adjust timeout for next attempt */
     890             : #if HAVE_GETTIMEOFDAY
     891        1621 :                         if (timeout) {
     892        1621 :                                 gettimeofday(&time_now, NULL);
     893             : 
     894        1621 :                                 if (timercmp(&time_now, &limit_time, >=)) {
     895             :                                         /* time limit expired; don't attempt any further connections */
     896           0 :                                         fatal = 1;
     897             :                                 } else {
     898             :                                         /* work out remaining time */
     899        1621 :                                         sub_times(limit_time, time_now, &working_timeout);
     900             :                                 }
     901             :                         }
     902             : #else
     903             :                         if (error_code && *error_code == PHP_TIMEOUT_ERROR_VALUE) {
     904             :                                 /* Don't even bother trying to connect to the next alternative;
     905             :                                  * we have no way to determine how long we have already taken
     906             :                                  * and it is quite likely that the next attempt will fail too. */
     907             :                                 fatal = 1;
     908             :                         } else {
     909             :                                 /* re-use the same initial timeout.
     910             :                                  * Not the best thing, but in practice it should be good-enough */
     911             :                                 if (timeout) {
     912             :                                         memcpy(&working_timeout, timeout, sizeof(working_timeout));
     913             :                                 }
     914             :                         }
     915             : #endif
     916             :                 }
     917             : 
     918        1621 :                 closesocket(sock);
     919             :         }
     920         812 :         sock = -1;
     921             : 
     922             : connected:
     923             : 
     924        1619 :         php_network_freeaddresses(psal);
     925             : 
     926        1619 :         return sock;
     927             : }
     928             : /* }}} */
     929             : 
     930             : /* {{{ php_any_addr
     931             :  * Fills the any (wildcard) address into php_sockaddr_storage
     932             :  */
     933          23 : PHPAPI void php_any_addr(int family, php_sockaddr_storage *addr, unsigned short port)
     934             : {
     935          23 :         memset(addr, 0, sizeof(php_sockaddr_storage));
     936          23 :         switch (family) {
     937             : #if HAVE_IPV6
     938             :         case AF_INET6: {
     939           0 :                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
     940           0 :                 sin6->sin6_family = AF_INET6;
     941           0 :                 sin6->sin6_port = htons(port);
     942           0 :                 sin6->sin6_addr = in6addr_any;
     943           0 :                 break;
     944             :         }
     945             : #endif
     946             :         case AF_INET: {
     947          23 :                 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
     948          23 :                 sin->sin_family = AF_INET;
     949          23 :                 sin->sin_port = htons(port);
     950          23 :                 sin->sin_addr.s_addr = htonl(INADDR_ANY);
     951             :                 break;
     952             :         }
     953             :         }
     954          23 : }
     955             : /* }}} */
     956             : 
     957             : /* {{{ php_sockaddr_size
     958             :  * Returns the size of struct sockaddr_xx for the family
     959             :  */
     960          23 : PHPAPI int php_sockaddr_size(php_sockaddr_storage *addr)
     961             : {
     962          23 :         switch (((struct sockaddr *)addr)->sa_family) {
     963             :         case AF_INET:
     964          23 :                 return sizeof(struct sockaddr_in);
     965             : #if HAVE_IPV6
     966             :         case AF_INET6:
     967           0 :                 return sizeof(struct sockaddr_in6);
     968             : #endif
     969             : #ifdef AF_UNIX
     970             :         case AF_UNIX:
     971           0 :                 return sizeof(struct sockaddr_un);
     972             : #endif
     973             :         default:
     974           0 :                 return 0;
     975             :         }
     976             : }
     977             : /* }}} */
     978             : 
     979             : /* Given a socket error code, if buf == NULL:
     980             :  *   emallocs storage for the error message and returns
     981             :  * else
     982             :  *   sprintf message into provided buffer and returns buf
     983             :  */
     984             : /* {{{ php_socket_strerror */
     985        1737 : PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize)
     986             : {
     987             : #ifndef PHP_WIN32
     988             :         char *errstr;
     989             : 
     990        1737 :         errstr = strerror(err);
     991        1737 :         if (buf == NULL) {
     992        1737 :                 buf = estrdup(errstr);
     993             :         } else {
     994           0 :                 strncpy(buf, errstr, bufsize);
     995             :         }
     996        1737 :         return buf;
     997             : #else
     998             :         char *sysbuf;
     999             :         int free_it = 1;
    1000             : 
    1001             :         if (!FormatMessage(
    1002             :                                 FORMAT_MESSAGE_ALLOCATE_BUFFER |
    1003             :                                 FORMAT_MESSAGE_FROM_SYSTEM |
    1004             :                                 FORMAT_MESSAGE_IGNORE_INSERTS,
    1005             :                                 NULL,
    1006             :                                 err,
    1007             :                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    1008             :                                 (LPTSTR)&sysbuf,
    1009             :                                 0,
    1010             :                                 NULL)) {
    1011             :                 free_it = 0;
    1012             :                 sysbuf = "Unknown Error";
    1013             :         }
    1014             : 
    1015             :         if (buf == NULL) {
    1016             :                 buf = estrdup(sysbuf);
    1017             :         } else {
    1018             :                 strncpy(buf, sysbuf, bufsize);
    1019             :         }
    1020             : 
    1021             :         if (free_it) {
    1022             :                 LocalFree(sysbuf);
    1023             :         }
    1024             : 
    1025             :         return buf;
    1026             : #endif
    1027             : }
    1028             : /* }}} */
    1029             : 
    1030             : /* deprecated */
    1031          16 : PHPAPI php_stream *_php_stream_sock_open_from_socket(php_socket_t socket, const char *persistent_id STREAMS_DC TSRMLS_DC)
    1032             : {
    1033             :         php_stream *stream;
    1034             :         php_netstream_data_t *sock;
    1035             : 
    1036          16 :         sock = pemalloc(sizeof(php_netstream_data_t), persistent_id ? 1 : 0);
    1037          16 :         memset(sock, 0, sizeof(php_netstream_data_t));
    1038             : 
    1039          16 :         sock->is_blocked = 1;
    1040          16 :         sock->timeout.tv_sec = FG(default_socket_timeout);
    1041          16 :         sock->timeout.tv_usec = 0;
    1042          16 :         sock->socket = socket;
    1043             : 
    1044          16 :         stream = php_stream_alloc_rel(&php_stream_generic_socket_ops, sock, persistent_id, "r+");
    1045             : 
    1046          16 :         if (stream == NULL) {
    1047           0 :                 pefree(sock, persistent_id ? 1 : 0);
    1048             :         } else {
    1049          16 :                 stream->flags |= PHP_STREAM_FLAG_AVOID_BLOCKING;
    1050             :         }
    1051             : 
    1052          16 :         return stream;
    1053             : }
    1054             : 
    1055           0 : PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port,
    1056             :                 int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC TSRMLS_DC)
    1057             : {
    1058             :         char *res;
    1059             :         long reslen;
    1060             :         php_stream *stream;
    1061             : 
    1062           0 :         reslen = spprintf(&res, 0, "tcp://%s:%d", host, port);
    1063             : 
    1064           0 :         stream = php_stream_xport_create(res, reslen, REPORT_ERRORS,
    1065             :                         STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, persistent_id, timeout, NULL, NULL, NULL);
    1066             : 
    1067           0 :         efree(res);
    1068             : 
    1069           0 :         return stream;
    1070             : }
    1071             : 
    1072         220 : PHPAPI int php_set_sock_blocking(int socketd, int block TSRMLS_DC)
    1073             : {
    1074         220 :         int ret = SUCCESS;
    1075             :         int flags;
    1076         220 :         int myflag = 0;
    1077             : 
    1078             : #ifdef PHP_WIN32
    1079             :         /* with ioctlsocket, a non-zero sets nonblocking, a zero sets blocking */
    1080             :         flags = !block;
    1081             :         if (ioctlsocket(socketd, FIONBIO, &flags) == SOCKET_ERROR) {
    1082             :                 ret = FAILURE;
    1083             :         }
    1084             : #else
    1085         220 :         flags = fcntl(socketd, F_GETFL);
    1086             : #ifdef O_NONBLOCK
    1087         220 :         myflag = O_NONBLOCK; /* POSIX version */
    1088             : #elif defined(O_NDELAY)
    1089             :         myflag = O_NDELAY;   /* old non-POSIX version */
    1090             : #endif
    1091         220 :         if (!block) {
    1092         140 :                 flags |= myflag;
    1093             :         } else {
    1094          80 :                 flags &= ~myflag;
    1095             :         }
    1096         220 :         if (fcntl(socketd, F_SETFL, flags) == -1) {
    1097           1 :                 ret = FAILURE;
    1098             :         }
    1099             : #endif
    1100         220 :         return ret;
    1101             : }
    1102             : 
    1103           0 : PHPAPI void _php_emit_fd_setsize_warning(int max_fd)
    1104             : {
    1105             :         TSRMLS_FETCH();
    1106             : 
    1107             : #ifdef PHP_WIN32
    1108             :         php_error_docref(NULL TSRMLS_CC, E_WARNING,
    1109             :                 "PHP needs to be recompiled with a larger value of FD_SETSIZE.\n"
    1110             :                 "If this binary is from an official www.php.net package, file a bug report\n"
    1111             :                 "at http://bugs.php.net, including the following information:\n"
    1112             :                 "FD_SETSIZE=%d, but you are using %d.\n"
    1113             :                 " --enable-fd-setsize=%d is recommended, but you may want to set it\n"
    1114             :                 "to match to maximum number of sockets each script will work with at\n"
    1115             :                 "one time, in order to avoid seeing this error again at a later date.",
    1116             :                 FD_SETSIZE, max_fd, (max_fd + 128) & ~127);
    1117             : #else
    1118           0 :         php_error_docref(NULL TSRMLS_CC, E_WARNING,
    1119             :                 "You MUST recompile PHP with a larger value of FD_SETSIZE.\n"
    1120             :                 "It is set to %d, but you have descriptors numbered at least as high as %d.\n"
    1121             :                 " --enable-fd-setsize=%d is recommended, but you may want to set it\n"
    1122             :                 "to equal the maximum number of open files supported by your system,\n"
    1123             :                 "in order to avoid seeing this error again at a later date.",
    1124           0 :                 FD_SETSIZE, max_fd, (max_fd + 1024) & ~1023);
    1125             : #endif
    1126           0 : }
    1127             : 
    1128             : #if defined(PHP_USE_POLL_2_EMULATION)
    1129             : 
    1130             : /* emulate poll(2) using select(2), safely. */
    1131             : 
    1132             : PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout)
    1133             : {
    1134             :         fd_set rset, wset, eset;
    1135             :         php_socket_t max_fd = SOCK_ERR;
    1136             :         unsigned int i;
    1137             :         int n;
    1138             :         struct timeval tv;
    1139             : 
    1140             :         /* check the highest numbered descriptor */
    1141             :         for (i = 0; i < nfds; i++) {
    1142             :                 if (ufds[i].fd > max_fd)
    1143             :                         max_fd = ufds[i].fd;
    1144             :         }
    1145             : 
    1146             :         PHP_SAFE_MAX_FD(max_fd, nfds + 1);
    1147             : 
    1148             :         FD_ZERO(&rset);
    1149             :         FD_ZERO(&wset);
    1150             :         FD_ZERO(&eset);
    1151             : 
    1152             :         for (i = 0; i < nfds; i++) {
    1153             :                 if (ufds[i].events & PHP_POLLREADABLE) {
    1154             :                         PHP_SAFE_FD_SET(ufds[i].fd, &rset);
    1155             :                 }
    1156             :                 if (ufds[i].events & POLLOUT) {
    1157             :                         PHP_SAFE_FD_SET(ufds[i].fd, &wset);
    1158             :                 }
    1159             :                 if (ufds[i].events & POLLPRI) {
    1160             :                         PHP_SAFE_FD_SET(ufds[i].fd, &eset);
    1161             :                 }
    1162             :         }
    1163             : 
    1164             :         if (timeout >= 0) {
    1165             :                 tv.tv_sec = timeout / 1000;
    1166             :                 tv.tv_usec = (timeout - (tv.tv_sec * 1000)) * 1000;
    1167             :         }
    1168             : /* Reseting/initializing */
    1169             : #ifdef PHP_WIN32
    1170             :         WSASetLastError(0);
    1171             : #else
    1172             :         errno = 0;
    1173             : #endif
    1174             :         n = select(max_fd + 1, &rset, &wset, &eset, timeout >= 0 ? &tv : NULL);
    1175             : 
    1176             :         if (n >= 0) {
    1177             :                 for (i = 0; i < nfds; i++) {
    1178             :                         ufds[i].revents = 0;
    1179             : 
    1180             :                         if (PHP_SAFE_FD_ISSET(ufds[i].fd, &rset)) {
    1181             :                                 /* could be POLLERR or POLLHUP but can't tell without probing */
    1182             :                                 ufds[i].revents |= POLLIN;
    1183             :                         }
    1184             :                         if (PHP_SAFE_FD_ISSET(ufds[i].fd, &wset)) {
    1185             :                                 ufds[i].revents |= POLLOUT;
    1186             :                         }
    1187             :                         if (PHP_SAFE_FD_ISSET(ufds[i].fd, &eset)) {
    1188             :                                 ufds[i].revents |= POLLPRI;
    1189             :                         }
    1190             :                 }
    1191             :         }
    1192             :         return n;
    1193             : }
    1194             : 
    1195             : #endif
    1196             : 
    1197             : 
    1198             : /*
    1199             :  * Local variables:
    1200             :  * tab-width: 8
    1201             :  * c-basic-offset: 8
    1202             :  * End:
    1203             :  * vim600: sw=4 ts=4 fdm=marker
    1204             :  * vim<600: sw=4 ts=4
    1205             :  */

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:48:03 +0000 (6 days ago)

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