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/streams - xp_socket.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 231 336 68.8 %
Date: 2015-09-02 Functions: 16 18 88.9 %
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: Wez Furlong <wez@thebrainroom.com>                           |
      16             :   +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include "php.h"
      22             : #include "ext/standard/file.h"
      23             : #include "streams/php_streams_int.h"
      24             : #include "php_network.h"
      25             : 
      26             : #if defined(PHP_WIN32) || defined(__riscos__) || defined(NETWARE)
      27             : # undef AF_UNIX
      28             : #endif
      29             : 
      30             : #if defined(AF_UNIX)
      31             : #include <sys/un.h>
      32             : #endif
      33             : 
      34             : #ifndef MSG_DONTWAIT
      35             : # define MSG_DONTWAIT 0
      36             : #endif
      37             : 
      38             : #ifndef MSG_PEEK
      39             : # define MSG_PEEK 0
      40             : #endif
      41             : 
      42             : #ifdef PHP_WIN32
      43             : /* send/recv family on windows expects int */
      44             : # define XP_SOCK_BUF_SIZE(sz) (((sz) > INT_MAX) ? INT_MAX : (int)(sz))
      45             : #else
      46             : # define XP_SOCK_BUF_SIZE(sz) (sz)
      47             : #endif
      48             : 
      49             : php_stream_ops php_stream_generic_socket_ops;
      50             : PHPAPI php_stream_ops php_stream_socket_ops;
      51             : php_stream_ops php_stream_udp_socket_ops;
      52             : #ifdef AF_UNIX
      53             : php_stream_ops php_stream_unix_socket_ops;
      54             : php_stream_ops php_stream_unixdg_socket_ops;
      55             : #endif
      56             : 
      57             : 
      58             : static int php_tcp_sockop_set_option(php_stream *stream, int option, int value, void *ptrparam);
      59             : 
      60             : /* {{{ Generic socket stream operations */
      61       41372 : static size_t php_sockop_write(php_stream *stream, const char *buf, size_t count)
      62             : {
      63       41372 :         php_netstream_data_t *sock = (php_netstream_data_t*)stream->abstract;
      64             :         int didwrite;
      65             :         struct timeval *ptimeout;
      66             : 
      67       41372 :         if (!sock || sock->socket == -1) {
      68           0 :                 return 0;
      69             :         }
      70             : 
      71       41372 :         if (sock->timeout.tv_sec == -1)
      72           0 :                 ptimeout = NULL;
      73             :         else
      74       41372 :                 ptimeout = &sock->timeout;
      75             : 
      76             : retry:
      77       41373 :         didwrite = send(sock->socket, buf, XP_SOCK_BUF_SIZE(count), (sock->is_blocked && ptimeout) ? MSG_DONTWAIT : 0);
      78             : 
      79       41373 :         if (didwrite <= 0) {
      80          12 :                 int err = php_socket_errno();
      81             :                 char *estr;
      82             : 
      83          12 :                 if (sock->is_blocked && (err == EWOULDBLOCK || err == EAGAIN)) {
      84             :                         int retval;
      85             : 
      86           1 :                         sock->timeout_event = 0;
      87             : 
      88             :                         do {
      89           1 :                                 retval = php_pollfd_for(sock->socket, POLLOUT, ptimeout);
      90             : 
      91           1 :                                 if (retval == 0) {
      92           0 :                                         sock->timeout_event = 1;
      93           0 :                                         break;
      94             :                                 }
      95             : 
      96           1 :                                 if (retval > 0) {
      97             :                                         /* writable now; retry */
      98           1 :                                         goto retry;
      99             :                                 }
     100             : 
     101           0 :                                 err = php_socket_errno();
     102           0 :                         } while (err == EINTR);
     103             :                 }
     104          11 :                 estr = php_socket_strerror(err, NULL, 0);
     105          11 :                 php_error_docref(NULL, E_NOTICE, "send of " ZEND_LONG_FMT " bytes failed with errno=%ld %s",
     106             :                                 (zend_long)count, err, estr);
     107          11 :                 efree(estr);
     108             :         }
     109             : 
     110       41372 :         if (didwrite > 0) {
     111       41361 :                 php_stream_notify_progress_increment(PHP_STREAM_CONTEXT(stream), didwrite, 0);
     112             :         }
     113             : 
     114       41372 :         if (didwrite < 0) {
     115          11 :                 didwrite = 0;
     116             :         }
     117             : 
     118       41372 :         return didwrite;
     119             : }
     120             : 
     121      120914 : static void php_sock_stream_wait_for_data(php_stream *stream, php_netstream_data_t *sock)
     122             : {
     123             :         int retval;
     124             :         struct timeval *ptimeout;
     125             : 
     126      120914 :         if (!sock || sock->socket == -1) {
     127           0 :                 return;
     128             :         }
     129             : 
     130      120914 :         sock->timeout_event = 0;
     131             : 
     132      120914 :         if (sock->timeout.tv_sec == -1)
     133           0 :                 ptimeout = NULL;
     134             :         else
     135      120914 :                 ptimeout = &sock->timeout;
     136             : 
     137             :         while(1) {
     138      120914 :                 retval = php_pollfd_for(sock->socket, PHP_POLLREADABLE, ptimeout);
     139             : 
     140      120914 :                 if (retval == 0)
     141           3 :                         sock->timeout_event = 1;
     142             : 
     143      120914 :                 if (retval >= 0)
     144      120914 :                         break;
     145             : 
     146           0 :                 if (php_socket_errno() != EINTR)
     147           0 :                         break;
     148           0 :         }
     149             : }
     150             : 
     151      120927 : static size_t php_sockop_read(php_stream *stream, char *buf, size_t count)
     152             : {
     153      120927 :         php_netstream_data_t *sock = (php_netstream_data_t*)stream->abstract;
     154      120927 :         ssize_t nr_bytes = 0;
     155             :         int err;
     156             : 
     157      120927 :         if (!sock || sock->socket == -1) {
     158           0 :                 return 0;
     159             :         }
     160             : 
     161      120927 :         if (sock->is_blocked) {
     162      120914 :                 php_sock_stream_wait_for_data(stream, sock);
     163      120914 :                 if (sock->timeout_event)
     164           3 :                         return 0;
     165             :         }
     166             : 
     167      120924 :         nr_bytes = recv(sock->socket, buf, XP_SOCK_BUF_SIZE(count), (sock->is_blocked && sock->timeout.tv_sec != -1) ? MSG_DONTWAIT : 0);
     168      120924 :         err = php_socket_errno();
     169             : 
     170      120924 :         stream->eof = (nr_bytes == 0 || (nr_bytes == -1 && err != EWOULDBLOCK && err != EAGAIN));
     171             : 
     172      120924 :         if (nr_bytes > 0) {
     173      120859 :                 php_stream_notify_progress_increment(PHP_STREAM_CONTEXT(stream), nr_bytes, 0);
     174             :         }
     175             : 
     176      120924 :         if (nr_bytes < 0) {
     177          11 :                 nr_bytes = 0;
     178             :         }
     179             : 
     180      120924 :         return nr_bytes;
     181             : }
     182             : 
     183             : 
     184        2337 : static int php_sockop_close(php_stream *stream, int close_handle)
     185             : {
     186        2337 :         php_netstream_data_t *sock = (php_netstream_data_t*)stream->abstract;
     187             : #ifdef PHP_WIN32
     188             :         int n;
     189             : #endif
     190             : 
     191        2337 :         if (!sock) {
     192           0 :                 return 0;
     193             :         }
     194             : 
     195        2337 :         if (close_handle) {
     196             : 
     197             : #ifdef PHP_WIN32
     198             :                 if (sock->socket == -1)
     199             :                         sock->socket = SOCK_ERR;
     200             : #endif
     201        2337 :                 if (sock->socket != SOCK_ERR) {
     202             : #ifdef PHP_WIN32
     203             :                         /* prevent more data from coming in */
     204             :                         shutdown(sock->socket, SHUT_RD);
     205             : 
     206             :                         /* try to make sure that the OS sends all data before we close the connection.
     207             :                          * Essentially, we are waiting for the socket to become writeable, which means
     208             :                          * that all pending data has been sent.
     209             :                          * We use a small timeout which should encourage the OS to send the data,
     210             :                          * but at the same time avoid hanging indefinitely.
     211             :                          * */
     212             :                         do {
     213             :                                 n = php_pollfd_for_ms(sock->socket, POLLOUT, 500);
     214             :                         } while (n == -1 && php_socket_errno() == EINTR);
     215             : #endif
     216        2337 :                         closesocket(sock->socket);
     217        2337 :                         sock->socket = SOCK_ERR;
     218             :                 }
     219             : 
     220             :         }
     221             : 
     222        2337 :         pefree(sock, php_stream_is_persistent(stream));
     223             : 
     224        2337 :         return 0;
     225             : }
     226             : 
     227        1826 : static int php_sockop_flush(php_stream *stream)
     228             : {
     229             : #if 0
     230             :         php_netstream_data_t *sock = (php_netstream_data_t*)stream->abstract;
     231             :         return fsync(sock->socket);
     232             : #endif
     233        1826 :         return 0;
     234             : }
     235             : 
     236           6 : static int php_sockop_stat(php_stream *stream, php_stream_statbuf *ssb)
     237             : {
     238             : #if ZEND_WIN32
     239             :         return 0;
     240             : #else
     241           6 :         php_netstream_data_t *sock = (php_netstream_data_t*)stream->abstract;
     242             : 
     243           6 :         return zend_fstat(sock->socket, &ssb->sb);
     244             : #endif
     245             : }
     246             : 
     247           0 : static inline int sock_sendto(php_netstream_data_t *sock, const char *buf, size_t buflen, int flags,
     248             :                 struct sockaddr *addr, socklen_t addrlen
     249             :                 )
     250             : {
     251             :         int ret;
     252           0 :         if (addr) {
     253           0 :                 ret = sendto(sock->socket, buf, XP_SOCK_BUF_SIZE(buflen), flags, addr, XP_SOCK_BUF_SIZE(addrlen));
     254             : 
     255           0 :                 return (ret == SOCK_CONN_ERR) ? -1 : ret;
     256             :         }
     257             : #ifdef PHP_WIN32
     258             :         return ((ret = send(sock->socket, buf, buflen > INT_MAX ? INT_MAX : (int)buflen, flags)) == SOCK_CONN_ERR) ? -1 : ret;
     259             : #else
     260           0 :         return ((ret = send(sock->socket, buf, buflen, flags)) == SOCK_CONN_ERR) ? -1 : ret;
     261             : #endif
     262             : }
     263             : 
     264           0 : static inline int sock_recvfrom(php_netstream_data_t *sock, char *buf, size_t buflen, int flags,
     265             :                 zend_string **textaddr,
     266             :                 struct sockaddr **addr, socklen_t *addrlen
     267             :                 )
     268             : {
     269             :         php_sockaddr_storage sa;
     270           0 :         socklen_t sl = sizeof(sa);
     271             :         int ret;
     272           0 :         int want_addr = textaddr || addr;
     273             : 
     274           0 :         if (want_addr) {
     275           0 :                 ret = recvfrom(sock->socket, buf, XP_SOCK_BUF_SIZE(buflen), flags, (struct sockaddr*)&sa, &sl);
     276           0 :                 ret = (ret == SOCK_CONN_ERR) ? -1 : ret;
     277           0 :                 php_network_populate_name_from_sockaddr((struct sockaddr*)&sa, sl,
     278             :                         textaddr, addr, addrlen);
     279             :         } else {
     280           0 :                 ret = recv(sock->socket, buf, XP_SOCK_BUF_SIZE(buflen), flags);
     281           0 :                 ret = (ret == SOCK_CONN_ERR) ? -1 : ret;
     282             :         }
     283             : 
     284           0 :         return ret;
     285             : }
     286             : 
     287        1889 : static int php_sockop_set_option(php_stream *stream, int option, int value, void *ptrparam)
     288             : {
     289             :         int oldmode, flags;
     290        1889 :         php_netstream_data_t *sock = (php_netstream_data_t*)stream->abstract;
     291             :         php_stream_xport_param *xparam;
     292             : 
     293        1889 :         if (!sock) {
     294           0 :                 return PHP_STREAM_OPTION_RETURN_NOTIMPL;
     295             :         }
     296             : 
     297        1889 :         switch(option) {
     298             :                 case PHP_STREAM_OPTION_CHECK_LIVENESS:
     299             :                         {
     300             :                                 struct timeval tv;
     301             :                                 char buf;
     302           3 :                                 int alive = 1;
     303             : 
     304           3 :                                 if (value == -1) {
     305           0 :                                         if (sock->timeout.tv_sec == -1) {
     306           0 :                                                 tv.tv_sec = FG(default_socket_timeout);
     307           0 :                                                 tv.tv_usec = 0;
     308             :                                         } else {
     309           0 :                                                 tv = sock->timeout;
     310             :                                         }
     311             :                                 } else {
     312           3 :                                         tv.tv_sec = value;
     313           3 :                                         tv.tv_usec = 0;
     314             :                                 }
     315             : 
     316           3 :                                 if (sock->socket == -1) {
     317           0 :                                         alive = 0;
     318           3 :                                 } else if (php_pollfd_for(sock->socket, PHP_POLLREADABLE|POLLPRI, &tv) > 0) {
     319             : #ifdef PHP_WIN32
     320             :                                         int ret;
     321             : #else
     322             :                                         ssize_t ret;
     323             : #endif
     324             :                                         int err;
     325             : 
     326           3 :                                         ret = recv(sock->socket, &buf, sizeof(buf), MSG_PEEK);
     327           3 :                                         err = php_socket_errno();
     328           3 :                                         if (0 == ret || /* the counterpart did properly shutdown*/
     329             :                                                 (0 > ret && err != EWOULDBLOCK && err != EAGAIN)) { /* there was an unrecoverable error */
     330           1 :                                                 alive = 0;
     331             :                                         }
     332             :                                 }
     333           3 :                                 return alive ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
     334             :                         }
     335             : 
     336             :                 case PHP_STREAM_OPTION_BLOCKING:
     337           8 :                         oldmode = sock->is_blocked;
     338           8 :                         if (SUCCESS == php_set_sock_blocking(sock->socket, value)) {
     339           8 :                                 sock->is_blocked = value;
     340           8 :                                 return oldmode;
     341             :                         }
     342           0 :                         return PHP_STREAM_OPTION_RETURN_ERR;
     343             : 
     344             :                 case PHP_STREAM_OPTION_READ_TIMEOUT:
     345        1738 :                         sock->timeout = *(struct timeval*)ptrparam;
     346        1738 :                         sock->timeout_event = 0;
     347        1738 :                         return PHP_STREAM_OPTION_RETURN_OK;
     348             : 
     349             :                 case PHP_STREAM_OPTION_META_DATA_API:
     350           0 :                         add_assoc_bool((zval *)ptrparam, "timed_out", sock->timeout_event);
     351           0 :                         add_assoc_bool((zval *)ptrparam, "blocked", sock->is_blocked);
     352           0 :                         add_assoc_bool((zval *)ptrparam, "eof", stream->eof);
     353           0 :                         return PHP_STREAM_OPTION_RETURN_OK;
     354             : 
     355             :                 case PHP_STREAM_OPTION_XPORT_API:
     356         128 :                         xparam = (php_stream_xport_param *)ptrparam;
     357             : 
     358         128 :                         switch (xparam->op) {
     359             :                                 case STREAM_XPORT_OP_LISTEN:
     360         123 :                                         xparam->outputs.returncode = (listen(sock->socket, xparam->inputs.backlog) == 0) ?  0: -1;
     361         123 :                                         return PHP_STREAM_OPTION_RETURN_OK;
     362             : 
     363             :                                 case STREAM_XPORT_OP_GET_NAME:
     364           0 :                                         xparam->outputs.returncode = php_network_get_sock_name(sock->socket,
     365           0 :                                                         xparam->want_textaddr ? &xparam->outputs.textaddr : NULL,
     366           0 :                                                         xparam->want_addr ? &xparam->outputs.addr : NULL,
     367           0 :                                                         xparam->want_addr ? &xparam->outputs.addrlen : NULL
     368             :                                                         );
     369           0 :                                         return PHP_STREAM_OPTION_RETURN_OK;
     370             : 
     371             :                                 case STREAM_XPORT_OP_GET_PEER_NAME:
     372           0 :                                         xparam->outputs.returncode = php_network_get_peer_name(sock->socket,
     373           0 :                                                         xparam->want_textaddr ? &xparam->outputs.textaddr : NULL,
     374           0 :                                                         xparam->want_addr ? &xparam->outputs.addr : NULL,
     375           0 :                                                         xparam->want_addr ? &xparam->outputs.addrlen : NULL
     376             :                                                         );
     377           0 :                                         return PHP_STREAM_OPTION_RETURN_OK;
     378             : 
     379             :                                 case STREAM_XPORT_OP_SEND:
     380           0 :                                         flags = 0;
     381           0 :                                         if ((xparam->inputs.flags & STREAM_OOB) == STREAM_OOB) {
     382           0 :                                                 flags |= MSG_OOB;
     383             :                                         }
     384           0 :                                         xparam->outputs.returncode = sock_sendto(sock,
     385           0 :                                                         xparam->inputs.buf, xparam->inputs.buflen,
     386             :                                                         flags,
     387             :                                                         xparam->inputs.addr,
     388             :                                                         xparam->inputs.addrlen);
     389           0 :                                         if (xparam->outputs.returncode == -1) {
     390           0 :                                                 char *err = php_socket_strerror(php_socket_errno(), NULL, 0);
     391           0 :                                                 php_error_docref(NULL, E_WARNING,
     392             :                                                         "%s\n", err);
     393           0 :                                                 efree(err);
     394             :                                         }
     395           0 :                                         return PHP_STREAM_OPTION_RETURN_OK;
     396             : 
     397             :                                 case STREAM_XPORT_OP_RECV:
     398           0 :                                         flags = 0;
     399           0 :                                         if ((xparam->inputs.flags & STREAM_OOB) == STREAM_OOB) {
     400           0 :                                                 flags |= MSG_OOB;
     401             :                                         }
     402           0 :                                         if ((xparam->inputs.flags & STREAM_PEEK) == STREAM_PEEK) {
     403           0 :                                                 flags |= MSG_PEEK;
     404             :                                         }
     405           0 :                                         xparam->outputs.returncode = sock_recvfrom(sock,
     406             :                                                         xparam->inputs.buf, xparam->inputs.buflen,
     407             :                                                         flags,
     408           0 :                                                         xparam->want_textaddr ? &xparam->outputs.textaddr : NULL,
     409           0 :                                                         xparam->want_addr ? &xparam->outputs.addr : NULL,
     410           0 :                                                         xparam->want_addr ? &xparam->outputs.addrlen : NULL
     411             :                                                         );
     412           0 :                                         return PHP_STREAM_OPTION_RETURN_OK;
     413             : 
     414             : 
     415             : #ifdef HAVE_SHUTDOWN
     416             : # ifndef SHUT_RD
     417             : #  define SHUT_RD 0
     418             : # endif
     419             : # ifndef SHUT_WR
     420             : #  define SHUT_WR 1
     421             : # endif
     422             : # ifndef SHUT_RDWR
     423             : #  define SHUT_RDWR 2
     424             : # endif
     425             :                                 case STREAM_XPORT_OP_SHUTDOWN: {
     426             :                                         static const int shutdown_how[] = {SHUT_RD, SHUT_WR, SHUT_RDWR};
     427             : 
     428           5 :                                         xparam->outputs.returncode = shutdown(sock->socket, shutdown_how[xparam->how]);
     429           5 :                                         return PHP_STREAM_OPTION_RETURN_OK;
     430             :                                 }
     431             : #endif
     432             : 
     433             :                                 default:
     434           0 :                                         return PHP_STREAM_OPTION_RETURN_NOTIMPL;
     435             :                         }
     436             : 
     437             :                 default:
     438          12 :                         return PHP_STREAM_OPTION_RETURN_NOTIMPL;
     439             :         }
     440             : }
     441             : 
     442         222 : static int php_sockop_cast(php_stream *stream, int castas, void **ret)
     443             : {
     444         222 :         php_netstream_data_t *sock = (php_netstream_data_t*)stream->abstract;
     445             : 
     446         222 :         if (!sock) {
     447           0 :                 return FAILURE;
     448             :         }
     449             : 
     450         222 :         switch(castas)  {
     451             :                 case PHP_STREAM_AS_STDIO:
     452           0 :                         if (ret)        {
     453           0 :                                 *(FILE**)ret = fdopen(sock->socket, stream->mode);
     454           0 :                                 if (*ret)
     455           0 :                                         return SUCCESS;
     456           0 :                                 return FAILURE;
     457             :                         }
     458           0 :                         return SUCCESS;
     459             :                 case PHP_STREAM_AS_FD_FOR_SELECT:
     460             :                 case PHP_STREAM_AS_FD:
     461             :                 case PHP_STREAM_AS_SOCKETD:
     462         222 :                         if (ret)
     463         222 :                                 *(php_socket_t *)ret = sock->socket;
     464         222 :                         return SUCCESS;
     465             :                 default:
     466           0 :                         return FAILURE;
     467             :         }
     468             : }
     469             : /* }}} */
     470             : 
     471             : /* These may look identical, but we need them this way so that
     472             :  * we can determine which type of socket we are dealing with
     473             :  * by inspecting stream->ops.
     474             :  * A "useful" side-effect is that the user's scripts can then
     475             :  * make similar decisions using stream_get_meta_data.
     476             :  * */
     477             : php_stream_ops php_stream_generic_socket_ops = {
     478             :         php_sockop_write, php_sockop_read,
     479             :         php_sockop_close, php_sockop_flush,
     480             :         "generic_socket",
     481             :         NULL, /* seek */
     482             :         php_sockop_cast,
     483             :         php_sockop_stat,
     484             :         php_sockop_set_option,
     485             : };
     486             : 
     487             : 
     488             : php_stream_ops php_stream_socket_ops = {
     489             :         php_sockop_write, php_sockop_read,
     490             :         php_sockop_close, php_sockop_flush,
     491             :         "tcp_socket",
     492             :         NULL, /* seek */
     493             :         php_sockop_cast,
     494             :         php_sockop_stat,
     495             :         php_tcp_sockop_set_option,
     496             : };
     497             : 
     498             : php_stream_ops php_stream_udp_socket_ops = {
     499             :         php_sockop_write, php_sockop_read,
     500             :         php_sockop_close, php_sockop_flush,
     501             :         "udp_socket",
     502             :         NULL, /* seek */
     503             :         php_sockop_cast,
     504             :         php_sockop_stat,
     505             :         php_tcp_sockop_set_option,
     506             : };
     507             : 
     508             : #ifdef AF_UNIX
     509             : php_stream_ops php_stream_unix_socket_ops = {
     510             :         php_sockop_write, php_sockop_read,
     511             :         php_sockop_close, php_sockop_flush,
     512             :         "unix_socket",
     513             :         NULL, /* seek */
     514             :         php_sockop_cast,
     515             :         php_sockop_stat,
     516             :         php_tcp_sockop_set_option,
     517             : };
     518             : php_stream_ops php_stream_unixdg_socket_ops = {
     519             :         php_sockop_write, php_sockop_read,
     520             :         php_sockop_close, php_sockop_flush,
     521             :         "udg_socket",
     522             :         NULL, /* seek */
     523             :         php_sockop_cast,
     524             :         php_sockop_stat,
     525             :         php_tcp_sockop_set_option,
     526             : };
     527             : #endif
     528             : 
     529             : 
     530             : /* network socket operations */
     531             : 
     532             : #ifdef AF_UNIX
     533        1690 : static inline int parse_unix_address(php_stream_xport_param *xparam, struct sockaddr_un *unix_addr)
     534             : {
     535        1690 :         memset(unix_addr, 0, sizeof(*unix_addr));
     536        1690 :         unix_addr->sun_family = AF_UNIX;
     537             : 
     538             :         /* we need to be binary safe on systems that support an abstract
     539             :          * namespace */
     540        1690 :         if (xparam->inputs.namelen >= sizeof(unix_addr->sun_path)) {
     541             :                 /* On linux, when the path begins with a NUL byte we are
     542             :                  * referring to an abstract namespace.  In theory we should
     543             :                  * allow an extra byte below, since we don't need the NULL.
     544             :                  * BUT, to get into this branch of code, the name is too long,
     545             :                  * so we don't care. */
     546           1 :                 xparam->inputs.namelen = sizeof(unix_addr->sun_path) - 1;
     547           1 :                 php_error_docref(NULL, E_NOTICE,
     548             :                         "socket path exceeded the maximum allowed length of %lu bytes "
     549             :                         "and was truncated", (unsigned long)sizeof(unix_addr->sun_path));
     550             :         }
     551             : 
     552        1690 :         memcpy(unix_addr->sun_path, xparam->inputs.name, xparam->inputs.namelen);
     553             : 
     554        1690 :         return 1;
     555             : }
     556             : #endif
     557             : 
     558        2054 : static inline char *parse_ip_address_ex(const char *str, size_t str_len, int *portno, int get_err, zend_string **err)
     559             : {
     560             :         char *colon;
     561        2054 :         char *host = NULL;
     562             : 
     563             : #ifdef HAVE_IPV6
     564             :         char *p;
     565             : 
     566        2054 :         if (*(str) == '[' && str_len > 1) {
     567             :                 /* IPV6 notation to specify raw address with port (i.e. [fe80::1]:80) */
     568          13 :                 p = memchr(str + 1, ']', str_len - 2);
     569          13 :                 if (!p || *(p + 1) != ':') {
     570           1 :                         if (get_err) {
     571           1 :                                 *err = strpprintf(0, "Failed to parse IPv6 address \"%s\"", str);
     572             :                         }
     573           1 :                         return NULL;
     574             :                 }
     575          12 :                 *portno = atoi(p + 2);
     576          12 :                 return estrndup(str + 1, p - str - 1);
     577             :         }
     578             : #endif
     579        2041 :         if (str_len) {
     580        2040 :                 colon = memchr(str, ':', str_len - 1);
     581             :         } else {
     582           1 :                 colon = NULL;
     583             :         }
     584        2041 :         if (colon) {
     585        2037 :                 *portno = atoi(colon + 1);
     586        2037 :                 host = estrndup(str, colon - str);
     587             :         } else {
     588           4 :                 if (get_err) {
     589           4 :                         *err = strpprintf(0, "Failed to parse address \"%s\"", str);
     590             :                 }
     591           4 :                 return NULL;
     592             :         }
     593             : 
     594        2037 :         return host;
     595             : }
     596             : 
     597        2054 : static inline char *parse_ip_address(php_stream_xport_param *xparam, int *portno)
     598             : {
     599        2054 :         return parse_ip_address_ex(xparam->inputs.name, xparam->inputs.namelen, portno, xparam->want_errortext, &xparam->outputs.error_text);
     600             : }
     601             : 
     602         134 : static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t *sock,
     603             :                 php_stream_xport_param *xparam)
     604             : {
     605         134 :         char *host = NULL;
     606             :         int portno, err;
     607         134 :         long sockopts = STREAM_SOCKOP_NONE;
     608         134 :         zval *tmpzval = NULL;
     609             : 
     610             : #ifdef AF_UNIX
     611         134 :         if (stream->ops == &php_stream_unix_socket_ops || stream->ops == &php_stream_unixdg_socket_ops) {
     612             :                 struct sockaddr_un unix_addr;
     613             : 
     614           5 :                 sock->socket = socket(PF_UNIX, stream->ops == &php_stream_unix_socket_ops ? SOCK_STREAM : SOCK_DGRAM, 0);
     615             : 
     616           5 :                 if (sock->socket == SOCK_ERR) {
     617           0 :                         if (xparam->want_errortext) {
     618           0 :                                 xparam->outputs.error_text = strpprintf(0, "Failed to create unix%s socket %s",
     619           0 :                                                 stream->ops == &php_stream_unix_socket_ops ? "" : "datagram",
     620             :                                                 strerror(errno));
     621             :                         }
     622           0 :                         return -1;
     623             :                 }
     624             : 
     625           5 :                 parse_unix_address(xparam, &unix_addr);
     626             : 
     627           5 :                 return bind(sock->socket, (const struct sockaddr *)&unix_addr,
     628             :                         (socklen_t) XtOffsetOf(struct sockaddr_un, sun_path) + xparam->inputs.namelen);
     629             :         }
     630             : #endif
     631             : 
     632         129 :         host = parse_ip_address(xparam, &portno);
     633             : 
     634         129 :         if (host == NULL) {
     635           0 :                 return -1;
     636             :         }
     637             : 
     638             : #ifdef SO_REUSEPORT
     639         258 :         if (PHP_STREAM_CONTEXT(stream)
     640         129 :                 && (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "so_reuseport")) != NULL
     641             :                 && zend_is_true(tmpzval)
     642           0 :         ) {
     643           0 :                 sockopts |= STREAM_SOCKOP_SO_REUSEPORT;
     644             :         }
     645             : #endif
     646             : 
     647             : #ifdef SO_BROADCAST
     648         159 :         if (stream->ops == &php_stream_udp_socket_ops /* SO_BROADCAST is only applicable for UDP */
     649         149 :                 && PHP_STREAM_CONTEXT(stream)
     650          10 :                 && (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "so_broadcast")) != NULL
     651             :                 && zend_is_true(tmpzval)
     652           0 :         ) {
     653           0 :                 sockopts |= STREAM_SOCKOP_SO_BROADCAST;
     654             :         }
     655             : #endif
     656             : 
     657         258 :         sock->socket = php_network_bind_socket_to_local_addr(host, portno,
     658         129 :                         stream->ops == &php_stream_udp_socket_ops ? SOCK_DGRAM : SOCK_STREAM,
     659             :                         sockopts,
     660         129 :                         xparam->want_errortext ? &xparam->outputs.error_text : NULL,
     661             :                         &err
     662             :                         );
     663             : 
     664         129 :         if (host) {
     665         129 :                 efree(host);
     666             :         }
     667             : 
     668         129 :         return sock->socket == -1 ? -1 : 0;
     669             : }
     670             : 
     671        3610 : static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_t *sock,
     672             :                 php_stream_xport_param *xparam)
     673             : {
     674        3610 :         char *host = NULL, *bindto = NULL;
     675        3610 :         int portno, bindport = 0;
     676        3610 :         int err = 0;
     677             :         int ret;
     678        3610 :         zval *tmpzval = NULL;
     679        3610 :         long sockopts = STREAM_SOCKOP_NONE;
     680             : 
     681             : #ifdef AF_UNIX
     682        3610 :         if (stream->ops == &php_stream_unix_socket_ops || stream->ops == &php_stream_unixdg_socket_ops) {
     683             :                 struct sockaddr_un unix_addr;
     684             : 
     685        1685 :                 sock->socket = socket(PF_UNIX, stream->ops == &php_stream_unix_socket_ops ? SOCK_STREAM : SOCK_DGRAM, 0);
     686             : 
     687        1685 :                 if (sock->socket == SOCK_ERR) {
     688           0 :                         if (xparam->want_errortext) {
     689           0 :                                 xparam->outputs.error_text = strpprintf(0, "Failed to create unix socket");
     690             :                         }
     691           0 :                         return -1;
     692             :                 }
     693             : 
     694        1685 :                 parse_unix_address(xparam, &unix_addr);
     695             : 
     696        1685 :                 ret = php_network_connect_socket(sock->socket,
     697             :                                 (const struct sockaddr *)&unix_addr, (socklen_t) XtOffsetOf(struct sockaddr_un, sun_path) + xparam->inputs.namelen,
     698             :                                 xparam->op == STREAM_XPORT_OP_CONNECT_ASYNC, xparam->inputs.timeout,
     699        1685 :                                 xparam->want_errortext ? &xparam->outputs.error_text : NULL,
     700             :                                 &err);
     701             : 
     702        1685 :                 xparam->outputs.error_code = err;
     703             : 
     704        1685 :                 goto out;
     705             :         }
     706             : #endif
     707             : 
     708        1925 :         host = parse_ip_address(xparam, &portno);
     709             : 
     710        1925 :         if (host == NULL) {
     711           5 :                 return -1;
     712             :         }
     713             : 
     714        1920 :         if (PHP_STREAM_CONTEXT(stream) && (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "bindto")) != NULL) {
     715           0 :                 if (Z_TYPE_P(tmpzval) != IS_STRING) {
     716           0 :                         if (xparam->want_errortext) {
     717           0 :                                 xparam->outputs.error_text = strpprintf(0, "local_addr context option is not a string.");
     718             :                         }
     719           0 :                         efree(host);
     720           0 :                         return -1;
     721             :                 }
     722           0 :                 bindto = parse_ip_address_ex(Z_STRVAL_P(tmpzval), Z_STRLEN_P(tmpzval), &bindport, xparam->want_errortext, &xparam->outputs.error_text);
     723             :         }
     724             : 
     725             : #ifdef SO_BROADCAST
     726        3767 :         if (stream->ops == &php_stream_udp_socket_ops /* SO_BROADCAST is only applicable for UDP */
     727        3153 :                 && PHP_STREAM_CONTEXT(stream)
     728         614 :                 && (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "so_broadcast")) != NULL
     729             :                 && zend_is_true(tmpzval)
     730           0 :         ) {
     731           0 :                 sockopts |= STREAM_SOCKOP_SO_BROADCAST;
     732             :         }
     733             : #endif
     734             : 
     735             :         /* Note: the test here for php_stream_udp_socket_ops is important, because we
     736             :          * want the default to be TCP sockets so that the openssl extension can
     737             :          * re-use this code. */
     738             : 
     739        5760 :         sock->socket = php_network_connect_socket_to_host(host, portno,
     740        1920 :                         stream->ops == &php_stream_udp_socket_ops ? SOCK_DGRAM : SOCK_STREAM,
     741             :                         xparam->op == STREAM_XPORT_OP_CONNECT_ASYNC,
     742             :                         xparam->inputs.timeout,
     743        1920 :                         xparam->want_errortext ? &xparam->outputs.error_text : NULL,
     744             :                         &err,
     745             :                         bindto,
     746             :                         bindport,
     747             :                         sockopts
     748             :                         );
     749             : 
     750        1920 :         ret = sock->socket == -1 ? -1 : 0;
     751        1920 :         xparam->outputs.error_code = err;
     752             : 
     753        1920 :         if (host) {
     754        1920 :                 efree(host);
     755             :         }
     756        1920 :         if (bindto) {
     757           0 :                 efree(bindto);
     758             :         }
     759             : 
     760             : #ifdef AF_UNIX
     761             : out:
     762             : #endif
     763             : 
     764        3605 :         if (ret >= 0 && xparam->op == STREAM_XPORT_OP_CONNECT_ASYNC && err == EINPROGRESS) {
     765             :                 /* indicates pending connection */
     766           1 :                 return 1;
     767             :         }
     768             : 
     769        3604 :         return ret;
     770             : }
     771             : 
     772           1 : static inline int php_tcp_sockop_accept(php_stream *stream, php_netstream_data_t *sock,
     773             :                 php_stream_xport_param *xparam STREAMS_DC)
     774             : {
     775             :         int clisock;
     776             : 
     777           1 :         xparam->outputs.client = NULL;
     778             : 
     779           5 :         clisock = php_network_accept_incoming(sock->socket,
     780           1 :                         xparam->want_textaddr ? &xparam->outputs.textaddr : NULL,
     781           1 :                         xparam->want_addr ? &xparam->outputs.addr : NULL,
     782           1 :                         xparam->want_addr ? &xparam->outputs.addrlen : NULL,
     783             :                         xparam->inputs.timeout,
     784           1 :                         xparam->want_errortext ? &xparam->outputs.error_text : NULL,
     785             :                         &xparam->outputs.error_code
     786             :                         );
     787             : 
     788           1 :         if (clisock >= 0) {
     789             :                 php_netstream_data_t *clisockdata;
     790             : 
     791           1 :                 clisockdata = emalloc(sizeof(*clisockdata));
     792             : 
     793           1 :                 if (clisockdata == NULL) {
     794           0 :                         close(clisock);
     795             :                         /* technically a fatal error */
     796             :                 } else {
     797           1 :                         memcpy(clisockdata, sock, sizeof(*clisockdata));
     798           1 :                         clisockdata->socket = clisock;
     799             : 
     800           1 :                         xparam->outputs.client = php_stream_alloc_rel(stream->ops, clisockdata, NULL, "r+");
     801           1 :                         if (xparam->outputs.client) {
     802           1 :                                 xparam->outputs.client->ctx = stream->ctx;
     803           1 :                                 if (stream->ctx) {
     804           1 :                                         GC_REFCOUNT(stream->ctx)++;
     805             :                                 }
     806             :                         }
     807             :                 }
     808             :         }
     809             : 
     810           1 :         return xparam->outputs.client == NULL ? -1 : 0;
     811             : }
     812             : 
     813        5625 : static int php_tcp_sockop_set_option(php_stream *stream, int option, int value, void *ptrparam)
     814             : {
     815        5625 :         php_netstream_data_t *sock = (php_netstream_data_t*)stream->abstract;
     816             :         php_stream_xport_param *xparam;
     817             : 
     818        5625 :         switch(option) {
     819             :                 case PHP_STREAM_OPTION_XPORT_API:
     820        3871 :                         xparam = (php_stream_xport_param *)ptrparam;
     821             : 
     822        3871 :                         switch(xparam->op) {
     823             :                                 case STREAM_XPORT_OP_CONNECT:
     824             :                                 case STREAM_XPORT_OP_CONNECT_ASYNC:
     825        3610 :                                         xparam->outputs.returncode = php_tcp_sockop_connect(stream, sock, xparam);
     826        3610 :                                         return PHP_STREAM_OPTION_RETURN_OK;
     827             : 
     828             :                                 case STREAM_XPORT_OP_BIND:
     829         134 :                                         xparam->outputs.returncode = php_tcp_sockop_bind(stream, sock, xparam);
     830         134 :                                         return PHP_STREAM_OPTION_RETURN_OK;
     831             : 
     832             : 
     833             :                                 case STREAM_XPORT_OP_ACCEPT:
     834           1 :                                         xparam->outputs.returncode = php_tcp_sockop_accept(stream, sock, xparam STREAMS_CC);
     835           1 :                                         return PHP_STREAM_OPTION_RETURN_OK;
     836             :                                 default:
     837             :                                         /* fall through */
     838             :                                         ;
     839             :                         }
     840             :         }
     841        1880 :         return php_sockop_set_option(stream, option, value, ptrparam);
     842             : }
     843             : 
     844             : 
     845        2319 : PHPAPI php_stream *php_stream_generic_socket_factory(const char *proto, size_t protolen,
     846             :                 const char *resourcename, size_t resourcenamelen,
     847             :                 const char *persistent_id, int options, int flags,
     848             :                 struct timeval *timeout,
     849             :                 php_stream_context *context STREAMS_DC)
     850             : {
     851        2319 :         php_stream *stream = NULL;
     852             :         php_netstream_data_t *sock;
     853             :         php_stream_ops *ops;
     854             : 
     855             :         /* which type of socket ? */
     856        2319 :         if (strncmp(proto, "tcp", protolen) == 0) {
     857           0 :                 ops = &php_stream_socket_ops;
     858        2319 :         } else if (strncmp(proto, "udp", protolen) == 0) {
     859         629 :                 ops = &php_stream_udp_socket_ops;
     860             :         }
     861             : #ifdef AF_UNIX
     862        1690 :         else if (strncmp(proto, "unix", protolen) == 0) {
     863        1688 :                 ops = &php_stream_unix_socket_ops;
     864           2 :         } else if (strncmp(proto, "udg", protolen) == 0) {
     865           2 :                 ops = &php_stream_unixdg_socket_ops;
     866             :         }
     867             : #endif
     868             :         else {
     869             :                 /* should never happen */
     870           0 :                 return NULL;
     871             :         }
     872             : 
     873        4638 :         sock = pemalloc(sizeof(php_netstream_data_t), persistent_id ? 1 : 0);
     874        2319 :         memset(sock, 0, sizeof(php_netstream_data_t));
     875             : 
     876        2319 :         sock->is_blocked = 1;
     877        2319 :         sock->timeout.tv_sec = FG(default_socket_timeout);
     878        2319 :         sock->timeout.tv_usec = 0;
     879             : 
     880             :         /* we don't know the socket until we have determined if we are binding or
     881             :          * connecting */
     882        2319 :         sock->socket = -1;
     883             : 
     884        2319 :         stream = php_stream_alloc_rel(ops, sock, persistent_id, "r+");
     885             : 
     886        2319 :         if (stream == NULL)     {
     887           0 :                 pefree(sock, persistent_id ? 1 : 0);
     888           0 :                 return NULL;
     889             :         }
     890             : 
     891        2319 :         if (flags == 0) {
     892           0 :                 return stream;
     893             :         }
     894             : 
     895        2319 :         return stream;
     896             : }
     897             : 
     898             : 
     899             : /*
     900             :  * Local variables:
     901             :  * tab-width: 4
     902             :  * c-basic-offset: 4
     903             :  * End:
     904             :  * vim600: noet sw=4 ts=4 fdm=marker
     905             :  * vim<600: noet sw=4 ts=4
     906             :  */

Generated by: LCOV version 1.10

Generated at Wed, 02 Sep 2015 17:19:25 +0000 (2 days ago)

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