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

Generated by: LCOV version 1.10

Generated at Thu, 30 Oct 2014 07:41:46 +0000 (34 hours ago)

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