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/standard - fsock.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 51 52 98.1 %
Date: 2014-11-10 Functions: 3 3 100.0 %
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             :    | Authors: Paul Panotzki - Bunyip Information Systems                  |
      16             :    |          Jim Winstead <jimw@php.net>                                 |
      17             :    |          Sascha Schumann <sascha@schumann.cx>                        |
      18             :    +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : #include "php.h"
      24             : #include "php_globals.h"
      25             : #include <stdlib.h>
      26             : #include <stddef.h>
      27             : #include "php_network.h"
      28             : #include "file.h"
      29             : 
      30             : /* {{{ php_fsockopen() */
      31             : 
      32         918 : static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
      33             : {
      34             :         char *host;
      35             :         size_t host_len;
      36         918 :         zend_long port = -1;
      37         918 :         zval *zerrno = NULL, *zerrstr = NULL;
      38         918 :         double timeout = (double)FG(default_socket_timeout);
      39             : #ifndef PHP_WIN32
      40             :         time_t conv;
      41             : #else
      42             :         long conv;
      43             : #endif
      44             :         struct timeval tv;
      45         918 :         char *hashkey = NULL;
      46         918 :         php_stream *stream = NULL;
      47             :         int err;
      48         918 :         char *hostname = NULL;
      49             :         size_t hostname_len;
      50         918 :         zend_string *errstr = NULL;
      51             : 
      52         918 :         RETVAL_FALSE;
      53             :         
      54         918 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz/z/d", &host, &host_len, &port, &zerrno, &zerrstr, &timeout) == FAILURE) {
      55           2 :                 RETURN_FALSE;
      56             :         }
      57             : 
      58         916 :         if (persistent) {
      59           2 :                 spprintf(&hashkey, 0, "pfsockopen__%s:" ZEND_LONG_FMT, host, port);
      60             :         }
      61             : 
      62         916 :         if (port > 0) {
      63         906 :                 hostname_len = spprintf(&hostname, 0, "%s:" ZEND_LONG_FMT, host, port);
      64             :         } else {
      65          10 :                 hostname_len = host_len;
      66          10 :                 hostname = host;
      67             :         }
      68             :         
      69             :         /* prepare the timeout value for use */
      70             : #ifndef PHP_WIN32
      71         916 :         conv = (time_t) (timeout * 1000000.0);
      72         916 :         tv.tv_sec = conv / 1000000;
      73             : #else
      74             :         conv = (long) (timeout * 1000000.0);
      75             :         tv.tv_sec = conv / 1000000;
      76             : #endif
      77         916 :         tv.tv_usec = conv % 1000000;
      78             : 
      79         916 :         if (zerrno)     {
      80          29 :                 zval_dtor(zerrno);
      81          29 :                 ZVAL_LONG(zerrno, 0);
      82             :         }
      83         916 :         if (zerrstr) {
      84          29 :                 zval_dtor(zerrstr);
      85          29 :                 ZVAL_EMPTY_STRING(zerrstr);
      86             :         }
      87             : 
      88         916 :         stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS,
      89             :                         STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err);
      90             : 
      91         916 :         if (port > 0) {
      92         906 :                 efree(hostname);
      93             :         }
      94         916 :         if (stream == NULL) {
      95         897 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s:" ZEND_LONG_FMT " (%s)", host, port, errstr == NULL ? "Unknown error" : errstr->val);
      96             :         }
      97             : 
      98         916 :         if (hashkey) {
      99           2 :                 efree(hashkey);
     100             :         }
     101             :         
     102         916 :         if (stream == NULL)     {
     103         897 :                 if (zerrno) {
     104          27 :                         zval_dtor(zerrno);
     105          27 :                         ZVAL_LONG(zerrno, err);
     106             :                 }
     107         924 :                 if (zerrstr && errstr) {
     108             :                         /* no need to dup; we need to efree buf anyway */
     109          27 :                         zval_dtor(zerrstr);
     110          27 :                         ZVAL_STR(zerrstr, errstr);
     111         870 :                 } else if (!zerrstr && errstr) {
     112         870 :                         zend_string_release(errstr);
     113             :                 } 
     114             : 
     115         897 :                 RETURN_FALSE;
     116             :         }
     117             : 
     118          19 :         if (errstr) {
     119           0 :                 zend_string_release(errstr);
     120             :         }
     121             :                 
     122          19 :         php_stream_to_zval(stream, return_value);
     123             : }
     124             : 
     125             : /* }}} */
     126             : 
     127             : /* {{{ proto resource fsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
     128             :    Open Internet or Unix domain socket connection */
     129         916 : PHP_FUNCTION(fsockopen)
     130             : {
     131         916 :         php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
     132         916 : }
     133             : /* }}} */
     134             : 
     135             : /* {{{ proto resource pfsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
     136             :    Open persistent Internet or Unix domain socket connection */
     137           2 : PHP_FUNCTION(pfsockopen)
     138             : {
     139           2 :         php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
     140           2 : }
     141             : /* }}} */
     142             : 
     143             : /*
     144             :  * Local variables:
     145             :  * tab-width: 4
     146             :  * c-basic-offset: 4
     147             :  * End:
     148             :  * vim600: sw=4 ts=4 fdm=marker
     149             :  * vim<600: sw=4 ts=4
     150             :  */

Generated by: LCOV version 1.10

Generated at Mon, 10 Nov 2014 22:46:49 +0000 (10 days ago)

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