Coverage Report

Created: 2025-06-13 06:43

/src/php-src/ext/standard/fsock.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright (c) The PHP Group                                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to version 3.01 of the PHP license,      |
6
   | that is bundled with this package in the file LICENSE, and is        |
7
   | available through the world-wide-web at the following url:           |
8
   | https://www.php.net/license/3_01.txt                                 |
9
   | If you did not receive a copy of the PHP license and are unable to   |
10
   | obtain it through the world-wide-web, please send a note to          |
11
   | license@php.net so we can mail you a copy immediately.               |
12
   +----------------------------------------------------------------------+
13
   | Authors: Paul Panotzki - Bunyip Information Systems                  |
14
   |          Jim Winstead <jimw@php.net>                                 |
15
   |          Sascha Schumann <sascha@schumann.cx>                        |
16
   +----------------------------------------------------------------------+
17
*/
18
19
#include "php.h"
20
#include "php_globals.h"
21
#include <stdlib.h>
22
#include <stddef.h>
23
#include "php_network.h"
24
#include "file.h"
25
26
/* {{{ php_fsockopen() */
27
28
static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
29
0
{
30
0
  char *host;
31
0
  size_t host_len;
32
0
  zend_long port = -1;
33
0
  zval *zerrno = NULL, *zerrstr = NULL;
34
0
  double timeout;
35
0
  bool timeout_is_null = 1;
36
0
#ifndef PHP_WIN32
37
0
  time_t conv;
38
#else
39
  long conv;
40
#endif
41
0
  struct timeval tv;
42
0
  char *hashkey = NULL;
43
0
  php_stream *stream = NULL;
44
0
  int err;
45
0
  char *hostname = NULL;
46
0
  size_t hostname_len;
47
0
  zend_string *errstr = NULL;
48
49
0
  ZEND_PARSE_PARAMETERS_START(1, 5)
50
0
    Z_PARAM_STRING(host, host_len)
51
0
    Z_PARAM_OPTIONAL
52
0
    Z_PARAM_LONG(port)
53
0
    Z_PARAM_ZVAL(zerrno)
54
0
    Z_PARAM_ZVAL(zerrstr)
55
0
    Z_PARAM_DOUBLE_OR_NULL(timeout, timeout_is_null)
56
0
  ZEND_PARSE_PARAMETERS_END();
57
58
0
  RETVAL_FALSE;
59
60
0
  if (timeout_is_null) {
61
0
    timeout = (double)FG(default_socket_timeout);
62
0
  }
63
64
0
  if (persistent) {
65
0
    spprintf(&hashkey, 0, "pfsockopen__%s:" ZEND_LONG_FMT, host, port);
66
0
  }
67
68
0
  if (port > 0) {
69
0
    hostname_len = spprintf(&hostname, 0, "%s:" ZEND_LONG_FMT, host, port);
70
0
  } else {
71
0
    hostname_len = host_len;
72
0
    hostname = host;
73
0
  }
74
75
  /* prepare the timeout value for use */
76
0
  if (timeout != -1.0 && !(timeout >= 0.0 && timeout <= (double) PHP_TIMEOUT_ULL_MAX / 1000000.0)) {
77
0
    if (port > 0) {
78
0
      efree(hostname);
79
0
    }
80
81
0
    if (hashkey) {
82
0
      efree(hashkey);
83
0
    }
84
85
0
    zend_argument_value_error(6, "must be -1 or between 0 and " ZEND_ULONG_FMT, ((double) PHP_TIMEOUT_ULL_MAX / 1000000.0));
86
0
    RETURN_THROWS();
87
0
  } else {
88
0
#ifndef PHP_WIN32
89
0
    conv = (time_t) (timeout * 1000000.0);
90
0
    tv.tv_sec = conv / 1000000;
91
#else
92
    conv = (long) (timeout * 1000000.0);
93
    tv.tv_sec = conv / 1000000;
94
#endif
95
0
    tv.tv_usec = conv % 1000000;
96
0
  }
97
98
0
  stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS,
99
0
      STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err);
100
101
0
  if (port > 0) {
102
0
    efree(hostname);
103
0
  }
104
0
  if (stream == NULL) {
105
0
    php_error_docref(NULL, E_WARNING, "Unable to connect to %s:" ZEND_LONG_FMT " (%s)", host, port, errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr));
106
0
  }
107
108
0
  if (hashkey) {
109
0
    efree(hashkey);
110
0
  }
111
112
0
  if (stream == NULL) {
113
0
    if (zerrno) {
114
0
      ZEND_TRY_ASSIGN_REF_LONG(zerrno, err);
115
0
    }
116
0
    if (errstr) {
117
0
      if (zerrstr) {
118
0
        ZEND_TRY_ASSIGN_REF_STR(zerrstr, errstr);
119
0
      } else {
120
0
        zend_string_release(errstr);
121
0
      }
122
0
    }
123
124
0
    RETURN_FALSE;
125
0
  }
126
127
0
  if (zerrno) {
128
0
    ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0);
129
0
  }
130
0
  if (zerrstr) {
131
0
    ZEND_TRY_ASSIGN_REF_EMPTY_STRING(zerrstr);
132
0
  }
133
134
0
  if (errstr) {
135
0
    zend_string_release_ex(errstr, 0);
136
0
  }
137
138
0
  php_stream_to_zval(stream, return_value);
139
0
}
140
141
/* }}} */
142
143
/* {{{ Open Internet or Unix domain socket connection */
144
PHP_FUNCTION(fsockopen)
145
0
{
146
0
  php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
147
0
}
148
/* }}} */
149
150
/* {{{ Open persistent Internet or Unix domain socket connection */
151
PHP_FUNCTION(pfsockopen)
152
0
{
153
0
  php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
154
0
}
155
/* }}} */