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-10-22 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 = FG(default_socket_timeout);
      39             :         zend_ulong conv;
      40             :         struct timeval tv;
      41         918 :         char *hashkey = NULL;
      42         918 :         php_stream *stream = NULL;
      43             :         int err;
      44         918 :         char *hostname = NULL;
      45             :         zend_long hostname_len;
      46         918 :         zend_string *errstr = NULL;
      47             : 
      48         918 :         RETVAL_FALSE;
      49             :         
      50         918 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz/z/d", &host, &host_len, &port, &zerrno, &zerrstr, &timeout) == FAILURE) {
      51           2 :                 RETURN_FALSE;
      52             :         }
      53             : 
      54         916 :         if (persistent) {
      55           2 :                 spprintf(&hashkey, 0, "pfsockopen__%s:" ZEND_LONG_FMT, host, port);
      56             :         }
      57             : 
      58         916 :         if (port > 0) {
      59         906 :                 hostname_len = spprintf(&hostname, 0, "%s:" ZEND_LONG_FMT, host, port);
      60             :         } else {
      61          10 :                 hostname_len = host_len;
      62          10 :                 hostname = host;
      63             :         }
      64             :         
      65             :         /* prepare the timeout value for use */
      66         916 :         conv = (unsigned long) (timeout * 1000000.0);
      67         916 :         tv.tv_sec = conv / 1000000;
      68         916 :         tv.tv_usec = conv % 1000000;
      69             : 
      70         916 :         if (zerrno)     {
      71          29 :                 zval_dtor(zerrno);
      72          29 :                 ZVAL_LONG(zerrno, 0);
      73             :         }
      74         916 :         if (zerrstr) {
      75          29 :                 zval_dtor(zerrstr);
      76          29 :                 ZVAL_EMPTY_STRING(zerrstr);
      77             :         }
      78             : 
      79         916 :         stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS,
      80             :                         STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err);
      81             : 
      82         916 :         if (port > 0) {
      83         906 :                 efree(hostname);
      84             :         }
      85         916 :         if (stream == NULL) {
      86         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);
      87             :         }
      88             : 
      89         916 :         if (hashkey) {
      90           2 :                 efree(hashkey);
      91             :         }
      92             :         
      93         916 :         if (stream == NULL)     {
      94         897 :                 if (zerrno) {
      95          27 :                         zval_dtor(zerrno);
      96          27 :                         ZVAL_LONG(zerrno, err);
      97             :                 }
      98         924 :                 if (zerrstr && errstr) {
      99             :                         /* no need to dup; we need to efree buf anyway */
     100          27 :                         zval_dtor(zerrstr);
     101          27 :                         ZVAL_STR(zerrstr, errstr);
     102         870 :                 } else if (!zerrstr && errstr) {
     103         870 :                         zend_string_release(errstr);
     104             :                 } 
     105             : 
     106         897 :                 RETURN_FALSE;
     107             :         }
     108             : 
     109          19 :         if (errstr) {
     110           0 :                 zend_string_release(errstr);
     111             :         }
     112             :                 
     113          19 :         php_stream_to_zval(stream, return_value);
     114             : }
     115             : 
     116             : /* }}} */
     117             : 
     118             : /* {{{ proto resource fsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
     119             :    Open Internet or Unix domain socket connection */
     120         916 : PHP_FUNCTION(fsockopen)
     121             : {
     122         916 :         php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
     123         916 : }
     124             : /* }}} */
     125             : 
     126             : /* {{{ proto resource pfsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
     127             :    Open persistent Internet or Unix domain socket connection */
     128           2 : PHP_FUNCTION(pfsockopen)
     129             : {
     130           2 :         php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
     131           2 : }
     132             : /* }}} */
     133             : 
     134             : /*
     135             :  * Local variables:
     136             :  * tab-width: 4
     137             :  * c-basic-offset: 4
     138             :  * End:
     139             :  * vim600: sw=4 ts=4 fdm=marker
     140             :  * vim<600: sw=4 ts=4
     141             :  */

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:58 +0000 (37 hours ago)

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