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

Generated by: LCOV version 1.10

Generated at Fri, 31 Jul 2015 08:59:03 +0000 (2 days ago)

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