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 - ext/sockets - sendrecvmsg.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 99 138 71.7 %
Date: 2015-08-29 Functions: 11 11 100.0 %
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             :    | Authors: Gustavo Lopes    <cataphract@php.net>                       |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : #include <php.h>
      20             : #include "php_sockets.h"
      21             : #include "sendrecvmsg.h"
      22             : #include "conversions.h"
      23             : #include <limits.h>
      24             : #include <Zend/zend_llist.h>
      25             : #ifdef ZTS
      26             : #include <TSRM/TSRM.h>
      27             : #endif
      28             : 
      29             : #define MAX_USER_BUFF_SIZE ((size_t)(100*1024*1024))
      30             : #define DEFAULT_BUFF_SIZE 8192
      31             : #define MAX_ARRAY_KEY_SIZE 128
      32             : 
      33             : #ifdef PHP_WIN32
      34             : #include "windows_common.h"
      35             : #include <Mswsock.h>
      36             : #define IPV6_RECVPKTINFO        IPV6_PKTINFO
      37             : #define IPV6_RECVHOPLIMIT       IPV6_HOPLIMIT
      38             : #define msghdr _WSAMSG
      39             : 
      40             : static GUID WSARecvMsg_GUID = WSAID_WSARECVMSG;
      41             : static __declspec(thread) LPFN_WSARECVMSG WSARecvMsg = NULL;
      42             : inline ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
      43             : {
      44             :         DWORD   recvd = 0,
      45             :                         bytesReturned;
      46             : 
      47             :         if (WSARecvMsg == NULL) {
      48             :                 int res = WSAIoctl((SOCKET) sockfd, SIO_GET_EXTENSION_FUNCTION_POINTER,
      49             :                         &WSARecvMsg_GUID, sizeof(WSARecvMsg_GUID),
      50             :                         &WSARecvMsg, sizeof(WSARecvMsg),
      51             :                         &bytesReturned, NULL, NULL);
      52             :                 if (res != 0) {
      53             :                         return -1;
      54             :                 }
      55             :         }
      56             : 
      57             :         msg->dwFlags = (DWORD)flags;
      58             :         return WSARecvMsg((SOCKET)sockfd, msg, &recvd, NULL, NULL) == 0
      59             :                 ? (ssize_t)recvd
      60             :                 : -1;
      61             : }
      62             : inline ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags)
      63             : {
      64             :         DWORD sent = 0;
      65             :         return WSASendMsg((SOCKET)sockfd, (struct msghdr*)msg, (DWORD)flags, &sent, NULL, NULL) == 0
      66             :                 ? (ssize_t)sent
      67             :                 : -1;
      68             : }
      69             : #endif
      70             : 
      71             : #define LONG_CHECK_VALID_INT(l) \
      72             :         do { \
      73             :                 if ((l) < INT_MIN && (l) > INT_MAX) { \
      74             :                         php_error_docref0(NULL, E_WARNING, "The value %pd does not fit inside " \
      75             :                                         "the boundaries of a native integer", (l)); \
      76             :                         return; \
      77             :                 } \
      78             :         } while (0)
      79             : 
      80             : static struct {
      81             :         int                     initialized;
      82             :         HashTable       ht;
      83             : } ancillary_registry;
      84             : 
      85             : 
      86          20 : static void ancillary_registery_free_elem(zval *el) {
      87          20 :         pefree(Z_PTR_P(el), 1);
      88          20 : }
      89             : 
      90             : #ifdef ZTS
      91             : static MUTEX_T ancillary_mutex;
      92             : #endif
      93           4 : static void init_ancillary_registry(void)
      94             : {
      95             :         ancillary_reg_entry entry;
      96             :         anc_reg_key key;
      97           4 :         ancillary_registry.initialized = 1;
      98             : 
      99           4 :         zend_hash_init(&ancillary_registry.ht, 32, NULL, ancillary_registery_free_elem, 1);
     100             : 
     101             : #define PUT_ENTRY(sizev, var_size, calc, from, to, level, type) \
     102             :         entry.size                      = sizev; \
     103             :         entry.var_el_size       = var_size; \
     104             :         entry.calc_space        = calc; \
     105             :         entry.from_array        = from; \
     106             :         entry.to_array          = to; \
     107             :         key.cmsg_level          = level; \
     108             :         key.cmsg_type           = type; \
     109             :         zend_hash_str_update_mem(&ancillary_registry.ht, (char*)&key, sizeof(key) - 1, (void*)&entry, sizeof(entry))
     110             : 
     111             : #if defined(IPV6_PKTINFO) && HAVE_IPV6
     112           4 :         PUT_ENTRY(sizeof(struct in6_pktinfo), 0, 0, from_zval_write_in6_pktinfo,
     113             :                         to_zval_read_in6_pktinfo, IPPROTO_IPV6, IPV6_PKTINFO);
     114             : #endif
     115             : 
     116             : #if defined(IPV6_HOPLIMIT) && HAVE_IPV6
     117           4 :         PUT_ENTRY(sizeof(int), 0, 0, from_zval_write_int,
     118             :                         to_zval_read_int, IPPROTO_IPV6, IPV6_HOPLIMIT);
     119             : #endif
     120             : 
     121             : #if defined(IPV6_TCLASS) && HAVE_IPV6
     122           4 :         PUT_ENTRY(sizeof(int), 0, 0, from_zval_write_int,
     123             :                         to_zval_read_int, IPPROTO_IPV6, IPV6_TCLASS);
     124             : #endif
     125             : 
     126             : #ifdef SO_PASSCRED
     127           4 :         PUT_ENTRY(sizeof(struct ucred), 0, 0, from_zval_write_ucred,
     128             :                         to_zval_read_ucred, SOL_SOCKET, SCM_CREDENTIALS);
     129             : #endif
     130             : 
     131             : #ifdef SCM_RIGHTS
     132           4 :         PUT_ENTRY(0, sizeof(int), calculate_scm_rights_space, from_zval_write_fd_array,
     133             :                         to_zval_read_fd_array, SOL_SOCKET, SCM_RIGHTS);
     134             : #endif
     135             : 
     136           4 : }
     137       21327 : static void destroy_ancillary_registry(void)
     138             : {
     139       21327 :         if (ancillary_registry.initialized) {
     140           4 :                 zend_hash_destroy(&ancillary_registry.ht);
     141           4 :                 ancillary_registry.initialized = 0;
     142             :         }
     143       21327 : }
     144          12 : ancillary_reg_entry *get_ancillary_reg_entry(int cmsg_level, int msg_type)
     145             : {
     146          12 :         anc_reg_key                     key = { cmsg_level, msg_type };
     147             :         ancillary_reg_entry     *entry;
     148             : 
     149             : #ifdef ZTS
     150             :         tsrm_mutex_lock(ancillary_mutex);
     151             : #endif
     152          12 :         if (!ancillary_registry.initialized) {
     153           4 :                 init_ancillary_registry();
     154             :         }
     155             : #ifdef ZTS
     156             :         tsrm_mutex_unlock(ancillary_mutex);
     157             : #endif
     158             : 
     159          24 :         if ((entry = zend_hash_str_find_ptr(&ancillary_registry.ht, (char*)&key, sizeof(key) - 1)) != NULL) {
     160          12 :                 return entry;
     161             :         } else {
     162           0 :                 return NULL;
     163             :         }
     164             : }
     165             : 
     166           4 : PHP_FUNCTION(socket_sendmsg)
     167             : {
     168             :         zval                    *zsocket,
     169             :                                         *zmsg;
     170           4 :         zend_long                       flags = 0;
     171             :         php_socket              *php_sock;
     172             :         struct msghdr   *msghdr;
     173             :         zend_llist              *allocations;
     174           4 :         struct err_s    err = {0};
     175             :         ssize_t                 res;
     176             : 
     177             :         /* zmsg should be passed by ref */
     178           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ra|l", &zsocket, &zmsg, &flags) == FAILURE) {
     179           0 :                 return;
     180             :         }
     181             : 
     182           4 :         LONG_CHECK_VALID_INT(flags);
     183             : 
     184           4 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(zsocket),
     185             :                                         php_sockets_le_socket_name, php_sockets_le_socket())) == NULL) {
     186           0 :                 RETURN_FALSE;
     187             :         }
     188             : 
     189           4 :         msghdr = from_zval_run_conversions(zmsg, php_sock, from_zval_write_msghdr_send,
     190             :                         sizeof(*msghdr), "msghdr", &allocations, &err);
     191             : 
     192           4 :         if (err.has_error) {
     193           0 :                 err_msg_dispose(&err);
     194           0 :                 RETURN_FALSE;
     195             :         }
     196             : 
     197           4 :         res = sendmsg(php_sock->bsd_socket, msghdr, (int)flags);
     198             : 
     199           4 :         if (res != -1) {
     200           4 :                 zend_llist_destroy(allocations);
     201           4 :                 efree(allocations);
     202             : 
     203           4 :                 RETURN_LONG((zend_long)res);
     204             :         } else {
     205           0 :                 PHP_SOCKET_ERROR(php_sock, "error in sendmsg", errno);
     206           0 :                 RETURN_FALSE;
     207             :         }
     208             : }
     209             : 
     210           4 : PHP_FUNCTION(socket_recvmsg)
     211             : {
     212             :         zval                    *zsocket,
     213             :                                         *zmsg;
     214           4 :         zend_long                       flags = 0;
     215             :         php_socket              *php_sock;
     216             :         ssize_t                 res;
     217             :         struct msghdr   *msghdr;
     218             :         zend_llist              *allocations;
     219           4 :         struct err_s    err = {0};
     220             : 
     221             :         //ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
     222           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ra/|l",
     223             :                         &zsocket, &zmsg, &flags) == FAILURE) {
     224           0 :                 return;
     225             :         }
     226             : 
     227           4 :         LONG_CHECK_VALID_INT(flags);
     228             : 
     229           4 :         if ((php_sock = (php_socket *)zend_fetch_resource(Z_RES_P(zsocket),
     230             :                                         php_sockets_le_socket_name, php_sockets_le_socket())) == NULL) {
     231           0 :                 RETURN_FALSE;
     232             :         }
     233             : 
     234           4 :         msghdr = from_zval_run_conversions(zmsg, php_sock, from_zval_write_msghdr_recv,
     235             :                         sizeof(*msghdr), "msghdr", &allocations, &err);
     236             : 
     237           4 :         if (err.has_error) {
     238           0 :                 err_msg_dispose(&err);
     239           0 :                 RETURN_FALSE;
     240             :         }
     241             : 
     242           4 :         res = recvmsg(php_sock->bsd_socket, msghdr, (int)flags);
     243             : 
     244           4 :         if (res != -1) {
     245             :                 zval *zres, tmp;
     246             :                 struct key_value kv[] = {
     247             :                                 {KEY_RECVMSG_RET, sizeof(KEY_RECVMSG_RET), &res},
     248             :                                 {0}
     249           4 :                 };
     250             : 
     251             : 
     252           4 :                 zres = to_zval_run_conversions((char *)msghdr, to_zval_read_msghdr,
     253             :                                 "msghdr", kv, &err, &tmp);
     254             : 
     255             :                 /* we don;t need msghdr anymore; free it */
     256           4 :                 msghdr = NULL;
     257           4 :                 allocations_dispose(&allocations);
     258             : 
     259           4 :                 zval_dtor(zmsg);
     260           4 :                 if (!err.has_error) {
     261           4 :                         ZVAL_COPY_VALUE(zmsg, zres);
     262             :                 } else {
     263           0 :                         err_msg_dispose(&err);
     264           0 :                         ZVAL_FALSE(zmsg);
     265             :                         /* no need to destroy/free zres -- it's NULL in this circumstance */
     266             :                         assert(zres == NULL);
     267             :                 }
     268             :         } else {
     269           0 :                 SOCKETS_G(last_error) = errno;
     270           0 :                 php_error_docref(NULL, E_WARNING, "error in recvmsg [%d]: %s",
     271             :                                 errno, sockets_strerror(errno));
     272           0 :                 RETURN_FALSE;
     273             :         }
     274             : 
     275           4 :         RETURN_LONG((zend_long)res);
     276             : }
     277             : 
     278           5 : PHP_FUNCTION(socket_cmsg_space)
     279             : {
     280             :         zend_long                               level,
     281             :                                                 type,
     282           5 :                                                 n = 0;
     283             :         ancillary_reg_entry     *entry;
     284             : 
     285           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll|l",
     286             :                         &level, &type, &n) == FAILURE) {
     287           0 :                 return;
     288             :         }
     289             : 
     290           5 :         LONG_CHECK_VALID_INT(level);
     291           5 :         LONG_CHECK_VALID_INT(type);
     292           5 :         LONG_CHECK_VALID_INT(n);
     293             : 
     294           5 :         if (n < 0) {
     295           0 :                 php_error_docref0(NULL, E_WARNING, "The third argument "
     296             :                                 "cannot be negative");
     297           0 :                 return;
     298             :         }
     299             : 
     300           5 :         entry = get_ancillary_reg_entry(level, type);
     301           5 :         if (entry == NULL) {
     302           0 :                 php_error_docref0(NULL, E_WARNING, "The pair level %pd/type %pd is "
     303             :                                 "not supported by PHP", level, type);
     304           0 :                 return;
     305             :         }
     306             : 
     307           6 :         if (entry->var_el_size > 0 && n > (ZEND_LONG_MAX - (zend_long)entry->size -
     308           1 :                         (zend_long)CMSG_SPACE(0) - 15L) / entry->var_el_size) {
     309             :                 /* the -15 is to account for any padding CMSG_SPACE may add after the data */
     310           0 :                 php_error_docref0(NULL, E_WARNING, "The value for the "
     311             :                                 "third argument (%pd) is too large", n);
     312           0 :                 return;
     313             :         }
     314             : 
     315           5 :         RETURN_LONG((zend_long)CMSG_SPACE(entry->size + n * entry->var_el_size));
     316             : }
     317             : 
     318             : #if HAVE_IPV6
     319           5 : int php_do_setsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname, zval *arg4)
     320             : {
     321           5 :         struct err_s    err = {0};
     322           5 :         zend_llist              *allocations = NULL;
     323             :         void                    *opt_ptr;
     324             :         socklen_t               optlen;
     325             :         int                             retval;
     326             : 
     327             :         assert(level == IPPROTO_IPV6);
     328             : 
     329           5 :         switch (optname) {
     330             : #ifdef IPV6_PKTINFO
     331             :         case IPV6_PKTINFO:
     332             : #ifdef PHP_WIN32
     333             :                 if (Z_TYPE_P(arg4) == IS_ARRAY) {
     334             :                         php_error_docref0(NULL, E_WARNING, "Windows does not "
     335             :                                         "support sticky IPV6_PKTINFO");
     336             :                         return FAILURE;
     337             :                 } else {
     338             :                         /* windows has no IPV6_RECVPKTINFO, and uses IPV6_PKTINFO
     339             :                          * for the same effect. We define IPV6_RECVPKTINFO to be
     340             :                          * IPV6_PKTINFO, so assume the assume user used IPV6_RECVPKTINFO */
     341             :                         return 1;
     342             :                 }
     343             : #endif
     344           2 :                 opt_ptr = from_zval_run_conversions(arg4, php_sock, from_zval_write_in6_pktinfo,
     345             :                                 sizeof(struct in6_pktinfo),     "in6_pktinfo", &allocations, &err);
     346           2 :                 if (err.has_error) {
     347           1 :                         err_msg_dispose(&err);
     348           1 :                         return FAILURE;
     349             :                 }
     350             : 
     351           1 :                 optlen = sizeof(struct in6_pktinfo);
     352           1 :                 goto dosockopt;
     353             : #endif
     354             :         }
     355             : 
     356             :         /* we also support IPV6_TCLASS, but that can be handled by the default
     357             :          * integer optval handling in the caller */
     358           3 :         return 1;
     359             : 
     360             : dosockopt:
     361           1 :         retval = setsockopt(php_sock->bsd_socket, level, optname, opt_ptr, optlen);
     362           1 :         if (retval != 0) {
     363           0 :                 PHP_SOCKET_ERROR(php_sock, "unable to set socket option", errno);
     364             :         }
     365           1 :         allocations_dispose(&allocations);
     366             : 
     367           1 :         return retval != 0 ? FAILURE : SUCCESS;
     368             : }
     369             : 
     370           5 : int php_do_getsockopt_ipv6_rfc3542(php_socket *php_sock, int level, int optname, zval *result)
     371             : {
     372           5 :         struct err_s            err = {0};
     373             :         void                            *buffer;
     374             :         socklen_t                       size;
     375             :         int                                     res;
     376             :         to_zval_read_field      *reader;
     377             : 
     378             :         assert(level == IPPROTO_IPV6);
     379             : 
     380           5 :         switch (optname) {
     381             : #ifdef IPV6_PKTINFO
     382             :         case IPV6_PKTINFO:
     383           0 :                 size = sizeof(struct in6_pktinfo);
     384           0 :                 reader = &to_zval_read_in6_pktinfo;
     385           0 :                 break;
     386             : #endif
     387             :         default:
     388           5 :                 return 1;
     389             :         }
     390             : 
     391           0 :         buffer = ecalloc(1, size);
     392           0 :         res = getsockopt(php_sock->bsd_socket, level, optname, buffer, &size);
     393           0 :         if (res != 0) {
     394           0 :                 PHP_SOCKET_ERROR(php_sock, "unable to get socket option", errno);
     395             :         } else {
     396             :                 zval tmp;
     397           0 :                 zval *zv = to_zval_run_conversions(buffer, reader, "in6_pktinfo",
     398           0 :                                 empty_key_value_list, &err, &tmp);
     399           0 :                 if (err.has_error) {
     400           0 :                         err_msg_dispose(&err);
     401           0 :                         res = -1;
     402             :                 } else {
     403           0 :                         ZVAL_COPY_VALUE(result, zv);
     404             :                 }
     405             :         }
     406           0 :         efree(buffer);
     407             : 
     408           0 :         return res == 0 ? SUCCESS : FAILURE;
     409             : }
     410             : #endif /* HAVE_IPV6 */
     411             : 
     412       21291 : void php_socket_sendrecvmsg_init(INIT_FUNC_ARGS)
     413             : {
     414             :         /* IPv6 ancillary data */
     415             : #if defined(IPV6_RECVPKTINFO) && HAVE_IPV6
     416       21291 :         REGISTER_LONG_CONSTANT("IPV6_RECVPKTINFO",            IPV6_RECVPKTINFO,       CONST_CS | CONST_PERSISTENT);
     417       21291 :         REGISTER_LONG_CONSTANT("IPV6_PKTINFO",          IPV6_PKTINFO,       CONST_CS | CONST_PERSISTENT);
     418             : #endif
     419             : #if defined(IPV6_RECVHOPLIMIT) && HAVE_IPV6
     420       21291 :         REGISTER_LONG_CONSTANT("IPV6_RECVHOPLIMIT",           IPV6_RECVHOPLIMIT,      CONST_CS | CONST_PERSISTENT);
     421       21291 :         REGISTER_LONG_CONSTANT("IPV6_HOPLIMIT",         IPV6_HOPLIMIT,      CONST_CS | CONST_PERSISTENT);
     422             : #endif
     423             :         /* would require some effort:
     424             :         REGISTER_LONG_CONSTANT("IPV6_RECVRTHDR",              IPV6_RECVRTHDR,         CONST_CS | CONST_PERSISTENT);
     425             :         REGISTER_LONG_CONSTANT("IPV6_RECVHOPOPTS",            IPV6_RECVHOPOPTS,       CONST_CS | CONST_PERSISTENT);
     426             :         REGISTER_LONG_CONSTANT("IPV6_RECVDSTOPTS",            IPV6_RECVDSTOPTS,       CONST_CS | CONST_PERSISTENT);
     427             :         */
     428             : #if defined(IPV6_RECVTCLASS) && HAVE_IPV6
     429       21291 :         REGISTER_LONG_CONSTANT("IPV6_RECVTCLASS",             IPV6_RECVTCLASS,        CONST_CS | CONST_PERSISTENT);
     430       21291 :         REGISTER_LONG_CONSTANT("IPV6_TCLASS",                 IPV6_TCLASS,            CONST_CS | CONST_PERSISTENT);
     431             : #endif
     432             : 
     433             :         /*
     434             :         REGISTER_LONG_CONSTANT("IPV6_RTHDR",                  IPV6_RTHDR,                     CONST_CS | CONST_PERSISTENT);
     435             :         REGISTER_LONG_CONSTANT("IPV6_HOPOPTS",                        IPV6_HOPOPTS,           CONST_CS | CONST_PERSISTENT);
     436             :         REGISTER_LONG_CONSTANT("IPV6_DSTOPTS",                        IPV6_DSTOPTS,           CONST_CS | CONST_PERSISTENT);
     437             :         */
     438             : 
     439             : #ifdef SCM_RIGHTS
     440       21291 :         REGISTER_LONG_CONSTANT("SCM_RIGHTS",                  SCM_RIGHTS,                     CONST_CS | CONST_PERSISTENT);
     441             : #endif
     442             : #ifdef SO_PASSCRED
     443       21291 :         REGISTER_LONG_CONSTANT("SCM_CREDENTIALS",             SCM_CREDENTIALS,        CONST_CS | CONST_PERSISTENT);
     444       21291 :         REGISTER_LONG_CONSTANT("SO_PASSCRED",                 SO_PASSCRED,            CONST_CS | CONST_PERSISTENT);
     445             : #endif
     446             : 
     447             : #ifdef ZTS
     448             :         ancillary_mutex = tsrm_mutex_alloc();
     449             : #endif
     450       21291 : }
     451             : 
     452       21327 : void php_socket_sendrecvmsg_shutdown(SHUTDOWN_FUNC_ARGS)
     453             : {
     454             : #ifdef ZTS
     455             :         tsrm_mutex_free(ancillary_mutex);
     456             : #endif
     457             : 
     458       21327 :         destroy_ancillary_registry();
     459       21327 : }

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:23 +0000 (29 hours ago)

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