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: 205 366 56.0 %
Date: 2014-10-22 Functions: 14 19 73.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       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        2081 : PHPAPI void php_network_freeaddresses(struct sockaddr **sal)
     155             : {
     156             :         struct sockaddr **sap;
     157             : 
     158        2081 :         if (sal == NULL)
     159           0 :                 return;
     160        5075 :         for (sap = sal; *sap != NULL; sap++)
     161        2994 :                 efree(*sap);
     162        2081 :         efree(sal);
     163             : }
     164             : /* }}} */
     165             : 
     166             : /* {{{ php_network_getaddresses
     167             :  * Returns number of addresses, 0 for none/error
     168             :  */
     169        2092 : PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **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        2092 :         if (host == NULL) {
     184           0 :                 return 0;
     185             :         }
     186             : #if HAVE_GETADDRINFO
     187        2092 :         memset(&hints, '\0', sizeof(hints));
     188             : 
     189        2092 :         hints.ai_family = AF_INET; /* default to regular inet (see below) */
     190        2092 :         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        2092 :         if (ipv6_borked == -1) {
     200             :                 int s;
     201             : 
     202         232 :                 s = socket(PF_INET6, SOCK_DGRAM, 0);
     203         232 :                 if (s == SOCK_ERR) {
     204           0 :                         ipv6_borked = 1;
     205             :                 } else {
     206         232 :                         ipv6_borked = 0;
     207         232 :                         closesocket(s);
     208             :                 }
     209             :         }
     210        2092 :         hints.ai_family = ipv6_borked ? AF_INET : AF_UNSPEC;
     211             : # endif
     212             : 
     213        2092 :         if ((n = getaddrinfo(host, NULL, &hints, &res))) {
     214          11 :                 if (error_string) {
     215           9 :                         *error_string = strpprintf(0, "php_network_getaddresses: getaddrinfo failed: %s", PHP_GAI_STRERROR(n));
     216           9 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", (*error_string)->val);
     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        2081 :         } else if (res == NULL) {
     222           0 :                 if (error_string) {
     223           0 :                         *error_string = strpprintf(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)->val);
     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        2081 :         sai = res;
     232        2081 :         for (n = 1; (sai = sai->ai_next) != NULL; n++)
     233             :                 ;
     234             : 
     235        2081 :         *sal = safe_emalloc((n + 1), sizeof(*sal), 0);
     236        2081 :         sai = res;
     237        2081 :         sap = *sal;
     238             : 
     239             :         do {
     240        2994 :                 *sap = emalloc(sai->ai_addrlen);
     241        2994 :                 memcpy(*sap, sai->ai_addr, sai->ai_addrlen);
     242        2994 :                 sap++;
     243        2994 :         } while ((sai = sai->ai_next) != NULL);
     244             : 
     245        2081 :         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             :                                 error_string = strpprintf(0, "php_network_getaddresses: gethostbyname failed. errno=%d", errno);
     253             :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", (*error_string)->val);
     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        2081 :         *sap = NULL;
     272        2081 :         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        4488 : 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             :                 zend_string **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        4488 :         int error = 0;
     315             :         socklen_t len;
     316        4488 :         int ret = 0;
     317             : 
     318        4488 :         SET_SOCKET_BLOCKING_MODE(sockfd, orig_flags);
     319             : 
     320        4488 :         if ((n = connect(sockfd, addr, addrlen)) != 0) {
     321        2005 :                 error = php_socket_errno();
     322             : 
     323        2005 :                 if (error_code) {
     324        1974 :                         *error_code = error;
     325             :                 }
     326             : 
     327        2005 :                 if (error != EINPROGRESS) {
     328           5 :                         if (error_string) {
     329           5 :                                 *error_string = php_socket_error_str(error);
     330             :                         }
     331             : 
     332           5 :                         return -1;
     333             :                 }
     334        2000 :                 if (asynchronous && error == EINPROGRESS) {
     335             :                         /* this is fine by us */
     336           1 :                         return 0;
     337             :                 }
     338             :         }
     339             : 
     340        4482 :         if (n == 0) {
     341        2483 :                 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        1999 :         if ((n = php_pollfd_for(sockfd, PHP_POLLREADABLE|POLLOUT, timeout)) == 0) {
     354             : #endif
     355           0 :                 error = PHP_TIMEOUT_ERROR_VALUE;
     356             :         }
     357             : 
     358        1999 :         if (n > 0) {
     359        1999 :                 len = sizeof(error);
     360             :                 /*
     361             :                    BSD-derived systems set errno correctly
     362             :                    Solaris returns -1 from getsockopt in case of error
     363             :                    */
     364        1999 :                 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        4482 :         if (!asynchronous) {
     374             :                 /* back to blocking mode */
     375        4482 :                 RESTORE_SOCKET_BLOCKING_MODE(sockfd, orig_flags);
     376             :         }
     377             : 
     378        4482 :         if (error_code) {
     379        4451 :                 *error_code = error;
     380             :         }
     381             : 
     382        4482 :         if (error) {
     383        1809 :                 ret = -1;
     384        1809 :                 if (error_string) {
     385        1809 :                         *error_string = php_socket_error_str(error);
     386             :                 }
     387             :         }
     388        4482 :         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        1809 : static inline void sub_times(struct timeval a, struct timeval b, struct timeval *result)
     400             : {
     401        1809 :         result->tv_usec = a.tv_usec - b.tv_usec;
     402        1809 :         if (result->tv_usec < 0L) {
     403        1787 :                 a.tv_sec--;
     404        1787 :                 result->tv_usec += 1000000L;
     405             :         }
     406        1809 :         result->tv_sec = a.tv_sec - b.tv_sec;
     407        1809 :         if (result->tv_sec < 0L) {
     408           0 :                 result->tv_sec++;
     409           0 :                 result->tv_usec -= 1000000L;
     410             :         }
     411        1809 : }
     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         119 : php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port,
     419             :                 int socktype, long sockopts, zend_string **error_string, int *error_code
     420             :                 TSRMLS_DC)
     421             : {
     422         119 :         int num_addrs, n, err = 0;
     423             :         php_socket_t sock;
     424             :         struct sockaddr **sal, **psal, *sa;
     425             :         socklen_t socklen;
     426         119 :         int sockoptval = 1;
     427             : 
     428         119 :         num_addrs = php_network_getaddresses(host, socktype, &psal, error_string TSRMLS_CC);
     429             : 
     430         119 :         if (num_addrs == 0) {
     431             :                 /* could not resolve address(es) */
     432           0 :                 return -1;
     433             :         }
     434             : 
     435         119 :         for (sal = psal; *sal != NULL; sal++) {
     436         119 :                 sa = *sal;
     437             : 
     438             :                 /* create a socket for this address */
     439         119 :                 sock = socket(sa->sa_family, socktype, 0);
     440             : 
     441         119 :                 if (sock == SOCK_ERR) {
     442           0 :                         continue;
     443             :                 }
     444             : 
     445         119 :                 switch (sa->sa_family) {
     446             : #if HAVE_GETADDRINFO && HAVE_IPV6
     447             :                         case AF_INET6:
     448           2 :                                 ((struct sockaddr_in6 *)sa)->sin6_family = sa->sa_family;
     449           2 :                                 ((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
     450           2 :                                 socklen = sizeof(struct sockaddr_in6);
     451           2 :                                 break;
     452             : #endif
     453             :                         case AF_INET:
     454         117 :                                 ((struct sockaddr_in *)sa)->sin_family = sa->sa_family;
     455         117 :                                 ((struct sockaddr_in *)sa)->sin_port = htons(port);
     456         117 :                                 socklen = sizeof(struct sockaddr_in);
     457         117 :                                 break;
     458             :                         default:
     459             :                                 /* Unknown family */
     460           0 :                                 socklen = 0;
     461           0 :                                 sa = NULL;
     462             :                 }
     463             : 
     464         119 :                 if (sa) {
     465             :                         /* attempt to bind */
     466             : 
     467             : #ifdef SO_REUSEADDR
     468         119 :                         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&sockoptval, sizeof(sockoptval));
     469             : #endif
     470             : #ifdef SO_REUSEPORT
     471         119 :                         if (sockopts & STREAM_SOCKOP_SO_REUSEPORT) {
     472           0 :                                 setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (char*)&sockoptval, sizeof(sockoptval));
     473             :                         }
     474             : #endif
     475             : #ifdef SO_BROADCAST
     476         119 :                         if (sockopts & STREAM_SOCKOP_SO_BROADCAST) {
     477           0 :                                 setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&sockoptval, sizeof(sockoptval));
     478             :                         }
     479             : #endif
     480             : 
     481         119 :                         n = bind(sock, sa, socklen);
     482             : 
     483         119 :                         if (n != SOCK_CONN_ERR) {
     484         119 :                                 goto bound;
     485             :                         }
     486             : 
     487           0 :                         err = php_socket_errno();
     488             :                 }
     489             : 
     490           0 :                 closesocket(sock);
     491             :         }
     492           0 :         sock = -1;
     493             : 
     494           0 :         if (error_code) {
     495           0 :                 *error_code = err;
     496             :         }
     497           0 :         if (error_string) {
     498           0 :                 *error_string = php_socket_error_str(err);
     499             :         }
     500             : 
     501             : bound:
     502             : 
     503         119 :         php_network_freeaddresses(psal);
     504             : 
     505         119 :         return sock;
     506             : 
     507             : }
     508             : /* }}} */
     509             : 
     510           0 : PHPAPI int php_network_parse_network_address_with_port(const char *addr, zend_long addrlen, struct sockaddr *sa, socklen_t *sl TSRMLS_DC)
     511             : {
     512             :         char *colon;
     513             :         char *tmp;
     514           0 :         int ret = FAILURE;
     515             :         short port;
     516           0 :         struct sockaddr_in *in4 = (struct sockaddr_in*)sa;
     517             :         struct sockaddr **psal;
     518             :         int n;
     519           0 :         zend_string *errstr = NULL;
     520             : #if HAVE_IPV6
     521           0 :         struct sockaddr_in6 *in6 = (struct sockaddr_in6*)sa;
     522             : #endif
     523             : 
     524           0 :         if (*addr == '[') {
     525           0 :                 colon = memchr(addr + 1, ']', addrlen-1);
     526           0 :                 if (!colon || colon[1] != ':') {
     527           0 :                         return FAILURE;
     528             :                 }
     529           0 :                 port = atoi(colon + 2);
     530           0 :                 addr++;
     531             :         } else {
     532           0 :                 colon = memchr(addr, ':', addrlen);
     533           0 :                 if (!colon) {
     534           0 :                         return FAILURE;
     535             :                 }
     536           0 :                 port = atoi(colon + 1);
     537             :         }
     538             : 
     539           0 :         tmp = estrndup(addr, colon - addr);
     540             : 
     541             :         /* first, try interpreting the address as a numeric address */
     542             : 
     543             : #if HAVE_IPV6 && HAVE_INET_PTON
     544           0 :         if (inet_pton(AF_INET6, tmp, &in6->sin6_addr) > 0) {
     545           0 :                 in6->sin6_port = htons(port);
     546           0 :                 in6->sin6_family = AF_INET6;
     547           0 :                 *sl = sizeof(struct sockaddr_in6);
     548           0 :                 ret = SUCCESS;
     549           0 :                 goto out;
     550             :         }
     551             : #endif
     552           0 :         if (inet_aton(tmp, &in4->sin_addr) > 0) {
     553           0 :                 in4->sin_port = htons(port);
     554           0 :                 in4->sin_family = AF_INET;
     555           0 :                 *sl = sizeof(struct sockaddr_in);
     556           0 :                 ret = SUCCESS;
     557           0 :                 goto out;
     558             :         }
     559             : 
     560             :         /* looks like we'll need to resolve it */
     561           0 :         n = php_network_getaddresses(tmp, SOCK_DGRAM, &psal, &errstr TSRMLS_CC);
     562             : 
     563           0 :         if (n == 0) {
     564           0 :                 if (errstr) {
     565           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to resolve `%s': %s", tmp, errstr->val);
     566           0 :                         zend_string_release(errstr);
     567             :                 }
     568           0 :                 goto out;
     569             :         }
     570             : 
     571             :         /* copy the details from the first item */
     572           0 :         switch ((*psal)->sa_family) {
     573             : #if HAVE_GETADDRINFO && HAVE_IPV6
     574             :                 case AF_INET6:
     575           0 :                         *in6 = **(struct sockaddr_in6**)psal;
     576           0 :                         in6->sin6_port = htons(port);
     577           0 :                         *sl = sizeof(struct sockaddr_in6);
     578           0 :                         ret = SUCCESS;
     579           0 :                         break;
     580             : #endif
     581             :                 case AF_INET:
     582           0 :                         *in4 = **(struct sockaddr_in**)psal;
     583           0 :                         in4->sin_port = htons(port);
     584           0 :                         *sl = sizeof(struct sockaddr_in);
     585           0 :                         ret = SUCCESS;
     586             :                         break;
     587             :         }
     588             : 
     589           0 :         php_network_freeaddresses(psal);
     590             : 
     591             : out:
     592           0 :         efree(tmp);
     593           0 :         return ret;
     594             : }
     595             : 
     596             : 
     597          96 : PHPAPI void php_network_populate_name_from_sockaddr(
     598             :                 /* input address */
     599             :                 struct sockaddr *sa, socklen_t sl,
     600             :                 /* output readable address */
     601             :                 zend_string **textaddr,
     602             :                 /* output address */
     603             :                 struct sockaddr **addr,
     604             :                 socklen_t *addrlen
     605             :                 TSRMLS_DC)
     606             : {
     607          96 :         if (addr) {
     608           0 :                 *addr = emalloc(sl);
     609           0 :                 memcpy(*addr, sa, sl);
     610           0 :                 *addrlen = sl;
     611             :         }
     612             : 
     613          96 :         if (textaddr) {
     614             : #if HAVE_IPV6 && HAVE_INET_NTOP
     615             :                 char abuf[256];
     616             : #endif
     617           0 :                 char *buf = NULL;
     618             : 
     619           0 :                 switch (sa->sa_family) {
     620             :                         case AF_INET:
     621             :                                 /* generally not thread safe, but it *is* thread safe under win32 */
     622           0 :                                 buf = inet_ntoa(((struct sockaddr_in*)sa)->sin_addr);
     623           0 :                                 if (buf) {
     624           0 :                                         *textaddr = strpprintf(0, "%s:%d",
     625           0 :                                                 buf, ntohs(((struct sockaddr_in*)sa)->sin_port));
     626             :                                 }
     627             : 
     628           0 :                                 break;
     629             : 
     630             : #if HAVE_IPV6 && HAVE_INET_NTOP
     631             :                         case AF_INET6:
     632           0 :                                 buf = (char*)inet_ntop(sa->sa_family, &((struct sockaddr_in6*)sa)->sin6_addr, (char *)&abuf, sizeof(abuf));
     633           0 :                                 if (buf) {
     634           0 :                                         *textaddr = strpprintf(0, "%s:%d",
     635           0 :                                                 buf, ntohs(((struct sockaddr_in6*)sa)->sin6_port));
     636             :                                 }
     637             : 
     638           0 :                                 break;
     639             : #endif
     640             : #ifdef AF_UNIX
     641             :                         case AF_UNIX:
     642             :                                 {
     643           0 :                                         struct sockaddr_un *ua = (struct sockaddr_un*)sa;
     644             : 
     645           0 :                                         if (ua->sun_path[0] == '\0') {
     646             :                                                 /* abstract name */
     647           0 :                                                 int len = strlen(ua->sun_path + 1) + 1;
     648           0 :                                                 *textaddr = zend_string_init((char*)ua->sun_path, len, 0);
     649             :                                         } else {
     650           0 :                                                 int len = strlen(ua->sun_path);
     651           0 :                                                 *textaddr = zend_string_init((char*)ua->sun_path, len, 0);
     652             :                                         }
     653             :                                 }
     654             :                                 break;
     655             : #endif
     656             : 
     657             :                 }
     658             : 
     659             :         }
     660          96 : }
     661             : 
     662           0 : PHPAPI int php_network_get_peer_name(php_socket_t sock,
     663             :                 zend_string **textaddr,
     664             :                 struct sockaddr **addr,
     665             :                 socklen_t *addrlen
     666             :                 TSRMLS_DC)
     667             : {
     668             :         php_sockaddr_storage sa;
     669           0 :         socklen_t sl = sizeof(sa);
     670           0 :         memset(&sa, 0, sizeof(sa));
     671             : 
     672           0 :         if (getpeername(sock, (struct sockaddr*)&sa, &sl) == 0) {
     673           0 :                 php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
     674             :                                 textaddr,
     675             :                                 addr, addrlen
     676             :                                 TSRMLS_CC);
     677           0 :                 return 0;
     678             :         }
     679           0 :         return -1;
     680             : }
     681             : 
     682           0 : PHPAPI int php_network_get_sock_name(php_socket_t sock,
     683             :                 zend_string **textaddr,
     684             :                 struct sockaddr **addr,
     685             :                 socklen_t *addrlen
     686             :                 TSRMLS_DC)
     687             : {
     688             :         php_sockaddr_storage sa;
     689           0 :         socklen_t sl = sizeof(sa);
     690           0 :         memset(&sa, 0, sizeof(sa));
     691             : 
     692           0 :         if (getsockname(sock, (struct sockaddr*)&sa, &sl) == 0) {
     693           0 :                 php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
     694             :                                 textaddr,
     695             :                                 addr, addrlen
     696             :                                 TSRMLS_CC);
     697           0 :                 return 0;
     698             :         }
     699           0 :         return -1;
     700             : 
     701             : }
     702             : 
     703             : 
     704             : /* Accept a client connection from a server socket,
     705             :  * using an optional timeout.
     706             :  * Returns the peer address in addr/addrlen (it will emalloc
     707             :  * these, so be sure to efree the result).
     708             :  * If you specify textaddr, a text-printable
     709             :  * version of the address will be emalloc'd and returned.
     710             :  * */
     711             : 
     712             : /* {{{ php_network_accept_incoming */
     713          97 : PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock,
     714             :                 zend_string **textaddr,
     715             :                 struct sockaddr **addr,
     716             :                 socklen_t *addrlen,
     717             :                 struct timeval *timeout,
     718             :                 zend_string **error_string,
     719             :                 int *error_code
     720             :                 TSRMLS_DC)
     721             : {
     722          97 :         php_socket_t clisock = -1;
     723          97 :         int error = 0, n;
     724             :         php_sockaddr_storage sa;
     725             :         socklen_t sl;
     726             : 
     727          97 :         n = php_pollfd_for(srvsock, PHP_POLLREADABLE, timeout);
     728             : 
     729          97 :         if (n == 0) {
     730           1 :                 error = PHP_TIMEOUT_ERROR_VALUE;
     731          96 :         } else if (n == -1) {
     732           0 :                 error = php_socket_errno();
     733             :         } else {
     734          96 :                 sl = sizeof(sa);
     735             : 
     736          96 :                 clisock = accept(srvsock, (struct sockaddr*)&sa, &sl);
     737             : 
     738          96 :                 if (clisock != SOCK_ERR) {
     739          96 :                         php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
     740             :                                         textaddr,
     741             :                                         addr, addrlen
     742             :                                         TSRMLS_CC);
     743             :                 } else {
     744           0 :                         error = php_socket_errno();
     745             :                 }
     746             :         }
     747             : 
     748          97 :         if (error_code) {
     749          97 :                 *error_code = error;
     750             :         }
     751          97 :         if (error_string) {
     752          97 :                 *error_string = php_socket_error_str(error);
     753             :         }
     754             : 
     755          97 :         return clisock;
     756             : }
     757             : /* }}} */
     758             : 
     759             : 
     760             : 
     761             : /* Connect to a remote host using an interruptible connect with optional timeout.
     762             :  * Optionally, the connect can be made asynchronously, which will implicitly
     763             :  * enable non-blocking mode on the socket.
     764             :  * Returns the connected (or connecting) socket, or -1 on failure.
     765             :  * */
     766             : 
     767             : /* {{{ php_network_connect_socket_to_host */
     768        1726 : php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
     769             :                 int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string,
     770             :                 int *error_code, char *bindto, unsigned short bindport, long sockopts
     771             :                 TSRMLS_DC)
     772             : {
     773        1726 :         int num_addrs, n, fatal = 0;
     774             :         php_socket_t sock;
     775             :         struct sockaddr **sal, **psal, *sa;
     776             :         struct timeval working_timeout;
     777             :         socklen_t socklen;
     778             : #if HAVE_GETTIMEOFDAY
     779             :         struct timeval limit_time, time_now;
     780             : #endif
     781             : 
     782        1726 :         num_addrs = php_network_getaddresses(host, socktype, &psal, error_string TSRMLS_CC);
     783             : 
     784        1726 :         if (num_addrs == 0) {
     785             :                 /* could not resolve address(es) */
     786          10 :                 return -1;
     787             :         }
     788             : 
     789        1716 :         if (timeout) {
     790        1716 :                 memcpy(&working_timeout, timeout, sizeof(working_timeout));
     791             : #if HAVE_GETTIMEOFDAY
     792        1716 :                 gettimeofday(&limit_time, NULL);
     793        1716 :                 limit_time.tv_sec += working_timeout.tv_sec;
     794        1716 :                 limit_time.tv_usec += working_timeout.tv_usec;
     795        1716 :                 if (limit_time.tv_usec >= 1000000) {
     796          15 :                         limit_time.tv_usec -= 1000000;
     797          15 :                         limit_time.tv_sec++;
     798             :                 }
     799             : #endif
     800             :         }
     801             : 
     802        3525 :         for (sal = psal; !fatal && *sal != NULL; sal++) {
     803        2619 :                 sa = *sal;
     804             : 
     805             :                 /* create a socket for this address */
     806        2619 :                 sock = socket(sa->sa_family, socktype, 0);
     807             : 
     808        2619 :                 if (sock == SOCK_ERR) {
     809           0 :                         continue;
     810             :                 }
     811             : 
     812        2619 :                 switch (sa->sa_family) {
     813             : #if HAVE_GETADDRINFO && HAVE_IPV6
     814             :                         case AF_INET6:
     815        1834 :                                 if (!bindto || strchr(bindto, ':')) {
     816         917 :                                         ((struct sockaddr_in6 *)sa)->sin6_family = sa->sa_family;
     817         917 :                                         ((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
     818         917 :                                         socklen = sizeof(struct sockaddr_in6);
     819             :                                 } else {
     820           0 :                                         socklen = 0;
     821           0 :                                         sa = NULL;
     822             :                                 }
     823         917 :                                 break;
     824             : #endif
     825             :                         case AF_INET:
     826        1702 :                                 ((struct sockaddr_in *)sa)->sin_family = sa->sa_family;
     827        1702 :                                 ((struct sockaddr_in *)sa)->sin_port = htons(port);
     828        1702 :                                 socklen = sizeof(struct sockaddr_in);
     829        1702 :                                 break;
     830             :                         default:
     831             :                                 /* Unknown family */
     832           0 :                                 socklen = 0;
     833           0 :                                 sa = NULL;
     834             :                 }
     835             : 
     836        2619 :                 if (sa) {
     837             :                         /* make a connection attempt */
     838             : 
     839        2619 :                         if (bindto) {
     840           0 :                                 struct sockaddr *local_address = NULL;
     841           0 :                                 int local_address_len = 0;
     842             : 
     843           0 :                                 if (sa->sa_family == AF_INET) {
     844           0 :                                         struct sockaddr_in *in4 = emalloc(sizeof(struct sockaddr_in));
     845             : 
     846           0 :                                         local_address = (struct sockaddr*)in4;
     847           0 :                                         local_address_len = sizeof(struct sockaddr_in);
     848             : 
     849           0 :                                         in4->sin_family = sa->sa_family;
     850           0 :                                         in4->sin_port = htons(bindport);
     851           0 :                                         if (!inet_aton(bindto, &in4->sin_addr)) {
     852           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid IP Address: %s", bindto);
     853           0 :                                                 goto skip_bind;
     854             :                                         }
     855           0 :                                         memset(&(in4->sin_zero), 0, sizeof(in4->sin_zero));
     856             :                                 }
     857             : #if HAVE_IPV6 && HAVE_INET_PTON
     858             :                                  else { /* IPV6 */
     859           0 :                                         struct sockaddr_in6 *in6 = emalloc(sizeof(struct sockaddr_in6));
     860             : 
     861           0 :                                         local_address = (struct sockaddr*)in6;
     862           0 :                                         local_address_len = sizeof(struct sockaddr_in6);
     863             : 
     864           0 :                                         in6->sin6_family = sa->sa_family;
     865           0 :                                         in6->sin6_port = htons(bindport);
     866           0 :                                         if (inet_pton(AF_INET6, bindto, &in6->sin6_addr) < 1) {
     867           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid IP Address: %s", bindto);
     868           0 :                                                 goto skip_bind;
     869             :                                         }
     870             :                                 }
     871             : #endif
     872             : 
     873           0 :                                 if (!local_address || bind(sock, local_address, local_address_len)) {
     874           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to bind to '%s:%d', system said: %s", bindto, bindport, strerror(errno));
     875             :                                 }
     876             : skip_bind:
     877           0 :                                 if (local_address) {
     878           0 :                                         efree(local_address);
     879             :                                 }
     880             :                         }
     881             :                         /* free error string received during previous iteration (if any) */
     882        2619 :                         if (error_string && *error_string) {
     883         903 :                                 zend_string_release(*error_string);
     884         903 :                                 *error_string = NULL;
     885             :                         }
     886             : 
     887             : #ifdef SO_BROADCAST
     888             :                         {
     889        2619 :                                 int val = 1;
     890        2619 :                                 if (sockopts & STREAM_SOCKOP_SO_BROADCAST) {
     891           0 :                                         setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&val, sizeof(val));
     892             :                                 }
     893             :                         }
     894             : #endif
     895        2619 :                         n = php_network_connect_socket(sock, sa, socklen, asynchronous,
     896             :                                         timeout ? &working_timeout : NULL,
     897             :                                         error_string, error_code);
     898             : 
     899        2619 :                         if (n != -1) {
     900         810 :                                 goto connected;
     901             :                         }
     902             : 
     903             :                         /* adjust timeout for next attempt */
     904             : #if HAVE_GETTIMEOFDAY
     905        1809 :                         if (timeout) {
     906        1809 :                                 gettimeofday(&time_now, NULL);
     907             : 
     908        1809 :                                 if (timercmp(&time_now, &limit_time, >=)) {
     909             :                                         /* time limit expired; don't attempt any further connections */
     910           0 :                                         fatal = 1;
     911             :                                 } else {
     912             :                                         /* work out remaining time */
     913        1809 :                                         sub_times(limit_time, time_now, &working_timeout);
     914             :                                 }
     915             :                         }
     916             : #else
     917             :                         if (error_code && *error_code == PHP_TIMEOUT_ERROR_VALUE) {
     918             :                                 /* Don't even bother trying to connect to the next alternative;
     919             :                                  * we have no way to determine how long we have already taken
     920             :                                  * and it is quite likely that the next attempt will fail too. */
     921             :                                 fatal = 1;
     922             :                         } else {
     923             :                                 /* re-use the same initial timeout.
     924             :                                  * Not the best thing, but in practice it should be good-enough */
     925             :                                 if (timeout) {
     926             :                                         memcpy(&working_timeout, timeout, sizeof(working_timeout));
     927             :                                 }
     928             :                         }
     929             : #endif
     930             :                 }
     931             : 
     932        1809 :                 closesocket(sock);
     933             :         }
     934         906 :         sock = -1;
     935             : 
     936             : connected:
     937             : 
     938        1716 :         php_network_freeaddresses(psal);
     939             : 
     940        1716 :         return sock;
     941             : }
     942             : /* }}} */
     943             : 
     944             : /* {{{ php_any_addr
     945             :  * Fills the any (wildcard) address into php_sockaddr_storage
     946             :  */
     947          23 : PHPAPI void php_any_addr(int family, php_sockaddr_storage *addr, unsigned short port)
     948             : {
     949          23 :         memset(addr, 0, sizeof(php_sockaddr_storage));
     950          23 :         switch (family) {
     951             : #if HAVE_IPV6
     952             :         case AF_INET6: {
     953           0 :                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
     954           0 :                 sin6->sin6_family = AF_INET6;
     955           0 :                 sin6->sin6_port = htons(port);
     956           0 :                 sin6->sin6_addr = in6addr_any;
     957           0 :                 break;
     958             :         }
     959             : #endif
     960             :         case AF_INET: {
     961          23 :                 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
     962          23 :                 sin->sin_family = AF_INET;
     963          23 :                 sin->sin_port = htons(port);
     964          23 :                 sin->sin_addr.s_addr = htonl(INADDR_ANY);
     965             :                 break;
     966             :         }
     967             :         }
     968          23 : }
     969             : /* }}} */
     970             : 
     971             : /* {{{ php_sockaddr_size
     972             :  * Returns the size of struct sockaddr_xx for the family
     973             :  */
     974          23 : PHPAPI int php_sockaddr_size(php_sockaddr_storage *addr)
     975             : {
     976          23 :         switch (((struct sockaddr *)addr)->sa_family) {
     977             :         case AF_INET:
     978          23 :                 return sizeof(struct sockaddr_in);
     979             : #if HAVE_IPV6
     980             :         case AF_INET6:
     981           0 :                 return sizeof(struct sockaddr_in6);
     982             : #endif
     983             : #ifdef AF_UNIX
     984             :         case AF_UNIX:
     985           0 :                 return sizeof(struct sockaddr_un);
     986             : #endif
     987             :         default:
     988           0 :                 return 0;
     989             :         }
     990             : }
     991             : /* }}} */
     992             : 
     993             : /* Given a socket error code, if buf == NULL:
     994             :  *   emallocs storage for the error message and returns
     995             :  * else
     996             :  *   sprintf message into provided buffer and returns buf
     997             :  */
     998             : /* {{{ php_socket_strerror */
     999          15 : PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize)
    1000             : {
    1001             : #ifndef PHP_WIN32
    1002             :         char *errstr;
    1003             : 
    1004          15 :         errstr = strerror(err);
    1005          15 :         if (buf == NULL) {
    1006          15 :                 buf = estrdup(errstr);
    1007             :         } else {
    1008           0 :                 strncpy(buf, errstr, bufsize);
    1009           0 :                 buf[bufsize?(bufsize-1):0] = 0;
    1010             :         }
    1011          15 :         return buf;
    1012             : #else
    1013             :         char *sysbuf;
    1014             :         int free_it = 1;
    1015             : 
    1016             :         if (!FormatMessage(
    1017             :                                 FORMAT_MESSAGE_ALLOCATE_BUFFER |
    1018             :                                 FORMAT_MESSAGE_FROM_SYSTEM |
    1019             :                                 FORMAT_MESSAGE_IGNORE_INSERTS,
    1020             :                                 NULL,
    1021             :                                 err,
    1022             :                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    1023             :                                 (LPTSTR)&sysbuf,
    1024             :                                 0,
    1025             :                                 NULL)) {
    1026             :                 free_it = 0;
    1027             :                 sysbuf = "Unknown Error";
    1028             :         }
    1029             : 
    1030             :         if (buf == NULL) {
    1031             :                 buf = estrdup(sysbuf);
    1032             :         } else {
    1033             :                 strncpy(buf, sysbuf, bufsize);
    1034             :                 buf[bufsize?(bufsize-1):0] = 0;
    1035             :         }
    1036             : 
    1037             :         if (free_it) {
    1038             :                 LocalFree(sysbuf);
    1039             :         }
    1040             : 
    1041             :         return buf;
    1042             : #endif
    1043             : }
    1044             : /* }}} */
    1045             : 
    1046             : /* {{{ php_socket_error_str */
    1047        1911 : PHPAPI zend_string *php_socket_error_str(long err)
    1048             : {
    1049             : #ifndef PHP_WIN32
    1050             :         char *errstr;
    1051             : 
    1052        1911 :         errstr = strerror(err);
    1053        3822 :         return zend_string_init(errstr, strlen(errstr), 0);
    1054             : #else
    1055             :         zend_string *ret;
    1056             :         char *sysbuf;
    1057             :         int free_it = 1;
    1058             : 
    1059             :         if (!FormatMessage(
    1060             :                                 FORMAT_MESSAGE_ALLOCATE_BUFFER |
    1061             :                                 FORMAT_MESSAGE_FROM_SYSTEM |
    1062             :                                 FORMAT_MESSAGE_IGNORE_INSERTS,
    1063             :                                 NULL,
    1064             :                                 err,
    1065             :                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    1066             :                                 (LPTSTR)&sysbuf,
    1067             :                                 0,
    1068             :                                 NULL)) {
    1069             :                 free_it = 0;
    1070             :                 sysbuf = "Unknown Error";
    1071             :         }
    1072             : 
    1073             :         ret = zend_string_init(sysbuf, strlen(sysbuf), 0);
    1074             : 
    1075             :         if (free_it) {
    1076             :                 LocalFree(sysbuf);
    1077             :         }
    1078             : 
    1079             :         return ret;
    1080             : #endif
    1081             : }
    1082             : /* }}} */
    1083             : 
    1084             : /* deprecated */
    1085          16 : PHPAPI php_stream *_php_stream_sock_open_from_socket(php_socket_t socket, const char *persistent_id STREAMS_DC TSRMLS_DC)
    1086             : {
    1087             :         php_stream *stream;
    1088             :         php_netstream_data_t *sock;
    1089             : 
    1090          32 :         sock = pemalloc(sizeof(php_netstream_data_t), persistent_id ? 1 : 0);
    1091          16 :         memset(sock, 0, sizeof(php_netstream_data_t));
    1092             : 
    1093          16 :         sock->is_blocked = 1;
    1094          16 :         sock->timeout.tv_sec = FG(default_socket_timeout);
    1095          16 :         sock->timeout.tv_usec = 0;
    1096          16 :         sock->socket = socket;
    1097             : 
    1098          16 :         stream = php_stream_alloc_rel(&php_stream_generic_socket_ops, sock, persistent_id, "r+");
    1099             : 
    1100          16 :         if (stream == NULL) {
    1101           0 :                 pefree(sock, persistent_id ? 1 : 0);
    1102             :         } else {
    1103          16 :                 stream->flags |= PHP_STREAM_FLAG_AVOID_BLOCKING;
    1104             :         }
    1105             : 
    1106          16 :         return stream;
    1107             : }
    1108             : 
    1109           0 : PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port,
    1110             :                 int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC TSRMLS_DC)
    1111             : {
    1112             :         char *res;
    1113             :         zend_long reslen;
    1114             :         php_stream *stream;
    1115             : 
    1116           0 :         reslen = spprintf(&res, 0, "tcp://%s:%d", host, port);
    1117             : 
    1118           0 :         stream = php_stream_xport_create(res, reslen, REPORT_ERRORS,
    1119             :                         STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, persistent_id, timeout, NULL, NULL, NULL);
    1120             : 
    1121           0 :         efree(res);
    1122             : 
    1123           0 :         return stream;
    1124             : }
    1125             : 
    1126         223 : PHPAPI int php_set_sock_blocking(php_socket_t socketd, int block TSRMLS_DC)
    1127             : {
    1128         223 :         int ret = SUCCESS;
    1129             :         int flags;
    1130         223 :         int myflag = 0;
    1131             : 
    1132             : #ifdef PHP_WIN32
    1133             :         /* with ioctlsocket, a non-zero sets nonblocking, a zero sets blocking */
    1134             :         flags = !block;
    1135             :         if (ioctlsocket(socketd, FIONBIO, &flags) == SOCKET_ERROR) {
    1136             :                 ret = FAILURE;
    1137             :         }
    1138             : #else
    1139         223 :         flags = fcntl(socketd, F_GETFL);
    1140             : #ifdef O_NONBLOCK
    1141         223 :         myflag = O_NONBLOCK; /* POSIX version */
    1142             : #elif defined(O_NDELAY)
    1143             :         myflag = O_NDELAY;   /* old non-POSIX version */
    1144             : #endif
    1145         223 :         if (!block) {
    1146         140 :                 flags |= myflag;
    1147             :         } else {
    1148          83 :                 flags &= ~myflag;
    1149             :         }
    1150         223 :         if (fcntl(socketd, F_SETFL, flags) == -1) {
    1151           1 :                 ret = FAILURE;
    1152             :         }
    1153             : #endif
    1154         223 :         return ret;
    1155             : }
    1156             : 
    1157           0 : PHPAPI void _php_emit_fd_setsize_warning(int max_fd)
    1158             : {
    1159             :         TSRMLS_FETCH();
    1160             : 
    1161             : #ifdef PHP_WIN32
    1162             :         php_error_docref(NULL TSRMLS_CC, E_WARNING,
    1163             :                 "PHP needs to be recompiled with a larger value of FD_SETSIZE.\n"
    1164             :                 "If this binary is from an official www.php.net package, file a bug report\n"
    1165             :                 "at http://bugs.php.net, including the following information:\n"
    1166             :                 "FD_SETSIZE=%d, but you are using %d.\n"
    1167             :                 " --enable-fd-setsize=%d is recommended, but you may want to set it\n"
    1168             :                 "to match to maximum number of sockets each script will work with at\n"
    1169             :                 "one time, in order to avoid seeing this error again at a later date.",
    1170             :                 FD_SETSIZE, max_fd, (max_fd + 128) & ~127);
    1171             : #else
    1172           0 :         php_error_docref(NULL TSRMLS_CC, E_WARNING,
    1173             :                 "You MUST recompile PHP with a larger value of FD_SETSIZE.\n"
    1174             :                 "It is set to %d, but you have descriptors numbered at least as high as %d.\n"
    1175             :                 " --enable-fd-setsize=%d is recommended, but you may want to set it\n"
    1176             :                 "to equal the maximum number of open files supported by your system,\n"
    1177             :                 "in order to avoid seeing this error again at a later date.",
    1178           0 :                 FD_SETSIZE, max_fd, (max_fd + 1024) & ~1023);
    1179             : #endif
    1180           0 : }
    1181             : 
    1182             : #if defined(PHP_USE_POLL_2_EMULATION)
    1183             : 
    1184             : /* emulate poll(2) using select(2), safely. */
    1185             : 
    1186             : PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout)
    1187             : {
    1188             :         fd_set rset, wset, eset;
    1189             :         php_socket_t max_fd = SOCK_ERR;
    1190             :         unsigned int i;
    1191             :         int n;
    1192             :         struct timeval tv;
    1193             : 
    1194             :         /* check the highest numbered descriptor */
    1195             :         for (i = 0; i < nfds; i++) {
    1196             :                 if (ufds[i].fd > max_fd)
    1197             :                         max_fd = ufds[i].fd;
    1198             :         }
    1199             : 
    1200             :         PHP_SAFE_MAX_FD(max_fd, nfds + 1);
    1201             : 
    1202             :         FD_ZERO(&rset);
    1203             :         FD_ZERO(&wset);
    1204             :         FD_ZERO(&eset);
    1205             : 
    1206             :         for (i = 0; i < nfds; i++) {
    1207             :                 if (ufds[i].events & PHP_POLLREADABLE) {
    1208             :                         PHP_SAFE_FD_SET(ufds[i].fd, &rset);
    1209             :                 }
    1210             :                 if (ufds[i].events & POLLOUT) {
    1211             :                         PHP_SAFE_FD_SET(ufds[i].fd, &wset);
    1212             :                 }
    1213             :                 if (ufds[i].events & POLLPRI) {
    1214             :                         PHP_SAFE_FD_SET(ufds[i].fd, &eset);
    1215             :                 }
    1216             :         }
    1217             : 
    1218             :         if (timeout >= 0) {
    1219             :                 tv.tv_sec = timeout / 1000;
    1220             :                 tv.tv_usec = (timeout - (tv.tv_sec * 1000)) * 1000;
    1221             :         }
    1222             : /* Reseting/initializing */
    1223             : #ifdef PHP_WIN32
    1224             :         WSASetLastError(0);
    1225             : #else
    1226             :         errno = 0;
    1227             : #endif
    1228             :         n = select(max_fd + 1, &rset, &wset, &eset, timeout >= 0 ? &tv : NULL);
    1229             : 
    1230             :         if (n >= 0) {
    1231             :                 for (i = 0; i < nfds; i++) {
    1232             :                         ufds[i].revents = 0;
    1233             : 
    1234             :                         if (PHP_SAFE_FD_ISSET(ufds[i].fd, &rset)) {
    1235             :                                 /* could be POLLERR or POLLHUP but can't tell without probing */
    1236             :                                 ufds[i].revents |= POLLIN;
    1237             :                         }
    1238             :                         if (PHP_SAFE_FD_ISSET(ufds[i].fd, &wset)) {
    1239             :                                 ufds[i].revents |= POLLOUT;
    1240             :                         }
    1241             :                         if (PHP_SAFE_FD_ISSET(ufds[i].fd, &eset)) {
    1242             :                                 ufds[i].revents |= POLLPRI;
    1243             :                         }
    1244             :                 }
    1245             :         }
    1246             :         return n;
    1247             : }
    1248             : 
    1249             : #endif
    1250             : 
    1251             : 
    1252             : /*
    1253             :  * Local variables:
    1254             :  * tab-width: 8
    1255             :  * c-basic-offset: 8
    1256             :  * End:
    1257             :  * vim600: sw=4 ts=4 fdm=marker
    1258             :  * vim<600: sw=4 ts=4
    1259             :  */

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:25:03 +0000 (2 days ago)

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