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 - sockaddr_conv.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 29 56 51.8 %
Date: 2014-07-21 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include <php.h>
       2             : #include <php_network.h>
       3             : #include "php_sockets.h"
       4             : 
       5             : #ifdef PHP_WIN32
       6             : #include "windows_common.h"
       7             : #else
       8             : #include <netdb.h>
       9             : #include <arpa/inet.h>
      10             : #endif
      11             : 
      12             : extern int php_string_to_if_index(const char *val, unsigned *out TSRMLS_DC);
      13             : 
      14             : #if HAVE_IPV6
      15             : /* Sets addr by hostname, or by ip in string form (AF_INET6) */
      16          28 : int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_sock TSRMLS_DC) /* {{{ */
      17             : {
      18             :         struct in6_addr tmp;
      19             : #if HAVE_GETADDRINFO
      20             :         struct addrinfo hints;
      21          28 :         struct addrinfo *addrinfo = NULL;
      22             : #endif
      23          28 :         char *scope = strchr(string, '%');
      24             : 
      25          28 :         if (inet_pton(AF_INET6, string, &tmp)) {
      26          28 :                 memcpy(&(sin6->sin6_addr.s6_addr), &(tmp.s6_addr), sizeof(struct in6_addr));
      27             :         } else {
      28             : #if HAVE_GETADDRINFO
      29             : 
      30           0 :                 memset(&hints, 0, sizeof(struct addrinfo));
      31           0 :                 hints.ai_family = AF_INET6;
      32             : #if HAVE_AI_V4MAPPED
      33           0 :                 hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG;
      34             : #else
      35             :                 hints.ai_flags = AI_ADDRCONFIG;
      36             : #endif
      37           0 :                 getaddrinfo(string, NULL, &hints, &addrinfo);
      38           0 :                 if (!addrinfo) {
      39             : #ifdef PHP_WIN32
      40             :                         PHP_SOCKET_ERROR(php_sock, "Host lookup failed", WSAGetLastError());
      41             : #else
      42           0 :                         PHP_SOCKET_ERROR(php_sock, "Host lookup failed", (-10000 - h_errno));
      43             : #endif
      44           0 :                         return 0;
      45             :                 }
      46           0 :                 if (addrinfo->ai_family != PF_INET6 || addrinfo->ai_addrlen != sizeof(struct sockaddr_in6)) {
      47           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host lookup failed: Non AF_INET6 domain returned on AF_INET6 socket");
      48           0 :                         freeaddrinfo(addrinfo);
      49           0 :                         return 0;
      50             :                 }
      51             : 
      52           0 :                 memcpy(&(sin6->sin6_addr.s6_addr), ((struct sockaddr_in6*)(addrinfo->ai_addr))->sin6_addr.s6_addr, sizeof(struct in6_addr));
      53           0 :                 freeaddrinfo(addrinfo);
      54             : 
      55             : #else
      56             :                 /* No IPv6 specific hostname resolution is available on this system? */
      57             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host lookup failed: getaddrinfo() not available on this system");
      58             :                 return 0;
      59             : #endif
      60             : 
      61             :         }
      62             : 
      63          28 :         if (scope++) {
      64           0 :                 long lval = 0;
      65           0 :                 double dval = 0;
      66           0 :                 unsigned scope_id = 0;
      67             : 
      68           0 :                 if (IS_LONG == is_numeric_string(scope, strlen(scope), &lval, &dval, 0)) {
      69           0 :                         if (lval > 0 && lval <= UINT_MAX) {
      70           0 :                                 scope_id = lval;
      71             :                         }
      72             :                 } else {
      73           0 :                         php_string_to_if_index(scope, &scope_id TSRMLS_CC);
      74             :                 }
      75             : 
      76           0 :                 sin6->sin6_scope_id = scope_id;
      77             :         }
      78             : 
      79          28 :         return 1;
      80             : }
      81             : /* }}} */
      82             : #endif
      83             : 
      84             : /* Sets addr by hostname, or by ip in string form (AF_INET)  */
      85          47 : int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock TSRMLS_DC) /* {{{ */
      86             : {
      87             :         struct in_addr tmp;
      88             :         struct hostent *host_entry;
      89             : 
      90          47 :         if (inet_aton(string, &tmp)) {
      91          46 :                 sin->sin_addr.s_addr = tmp.s_addr;
      92             :         } else {
      93           1 :                 if (! (host_entry = gethostbyname(string))) {
      94             :                         /* Note: < -10000 indicates a host lookup error */
      95             : #ifdef PHP_WIN32
      96             :                         PHP_SOCKET_ERROR(php_sock, "Host lookup failed", WSAGetLastError());
      97             : #else
      98           0 :                         PHP_SOCKET_ERROR(php_sock, "Host lookup failed", (-10000 - h_errno));
      99             : #endif
     100           0 :                         return 0;
     101             :                 }
     102           1 :                 if (host_entry->h_addrtype != AF_INET) {
     103           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host lookup failed: Non AF_INET domain returned on AF_INET socket");
     104           0 :                         return 0;
     105             :                 }
     106           1 :                 memcpy(&(sin->sin_addr.s_addr), host_entry->h_addr_list[0], host_entry->h_length);
     107             :         }
     108             : 
     109          47 :         return 1;
     110             : }
     111             : /* }}} */
     112             : 
     113             : /* Sets addr by hostname or by ip in string form (AF_INET or AF_INET6,
     114             :  * depending on the socket) */
     115          25 : int php_set_inet46_addr(php_sockaddr_storage *ss, socklen_t *ss_len, char *string, php_socket *php_sock TSRMLS_DC) /* {{{ */
     116             : {
     117          25 :         if (php_sock->type == AF_INET) {
     118          16 :                 struct sockaddr_in t = {0};
     119          16 :                 if (php_set_inet_addr(&t, string, php_sock TSRMLS_CC)) {
     120          16 :                         memcpy(ss, &t, sizeof t);
     121          16 :                         ss->ss_family = AF_INET;
     122          16 :                         *ss_len = sizeof(t);
     123          16 :                         return 1;
     124             :                 }
     125             :         }
     126             : #if HAVE_IPV6
     127           9 :         else if (php_sock->type == AF_INET6) {
     128           9 :                 struct sockaddr_in6 t = {0};
     129           9 :                 if (php_set_inet6_addr(&t, string, php_sock TSRMLS_CC)) {
     130           9 :                         memcpy(ss, &t, sizeof t);
     131           9 :                         ss->ss_family = AF_INET6;
     132           9 :                         *ss_len = sizeof(t);
     133           9 :                         return 1;
     134             :                 }
     135             :         }
     136             : #endif
     137             :         else {
     138           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING,
     139             :                         "IP address used in the context of an unexpected type of socket");
     140             :         }
     141           0 :         return 0;
     142             : }

Generated by: LCOV version 1.10

Generated at Tue, 22 Jul 2014 01:33:18 +0000 (2 days ago)

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