Coverage Report

Created: 2026-07-25 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/standard/fsock.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Authors: Paul Panotzki - Bunyip Information Systems                  |
12
   |          Jim Winstead <jimw@php.net>                                 |
13
   |          Sascha Schumann <sascha@schumann.cx>                        |
14
   +----------------------------------------------------------------------+
15
*/
16
17
#include "php.h"
18
#include "php_globals.h"
19
#include <stdlib.h>
20
#include <stddef.h>
21
#include "php_network.h"
22
#include "file.h"
23
#include "streams/php_streams_int.h"
24
25
static size_t php_fsockopen_format_host_port(char **message, const char *prefix, size_t prefix_len,
26
  const char *host, size_t host_len, zend_long port)
27
0
{
28
0
    char portbuf[32];
29
0
    int portlen = snprintf(portbuf, sizeof(portbuf), ":" ZEND_LONG_FMT, port);
30
0
    size_t total_len = prefix_len + host_len + portlen;
31
32
0
    *message = zend_cstr_concat3(
33
0
        prefix, prefix_len,
34
0
        host, host_len,
35
0
        portbuf, portlen);
36
37
0
  return total_len;
38
0
}
39
40
/* {{{ php_fsockopen() */
41
42
static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
43
0
{
44
0
  char *host;
45
0
  size_t host_len;
46
0
  zend_long port = -1;
47
0
  zval *zerrno = NULL, *zerrstr = NULL;
48
0
  double timeout;
49
0
  bool timeout_is_null = 1;
50
0
#ifndef PHP_WIN32
51
0
  time_t conv;
52
#else
53
  long conv;
54
#endif
55
0
  struct timeval tv;
56
0
  char *hashkey = NULL;
57
0
  php_stream *stream = NULL;
58
0
  int err;
59
0
  char *hostname = NULL;
60
0
  size_t hostname_len;
61
0
  zend_string *errstr = NULL;
62
63
0
  ZEND_PARSE_PARAMETERS_START(1, 5)
64
0
    Z_PARAM_STRING(host, host_len)
65
0
    Z_PARAM_OPTIONAL
66
0
    Z_PARAM_LONG(port)
67
0
    Z_PARAM_ZVAL(zerrno)
68
0
    Z_PARAM_ZVAL(zerrstr)
69
0
    Z_PARAM_DOUBLE_OR_NULL(timeout, timeout_is_null)
70
0
  ZEND_PARSE_PARAMETERS_END();
71
72
0
  RETVAL_FALSE;
73
74
0
  if (timeout_is_null) {
75
0
    timeout = (double)FG(default_socket_timeout);
76
0
  }
77
78
0
  if (persistent) {
79
0
    zend_string *escaped = php_stream_escape_persistent_key(host, host_len);
80
0
    spprintf(&hashkey, 0, "pfsockopen__%s:"  ZEND_LONG_FMT, ZSTR_VAL(escaped), port);
81
0
    zend_string_release_ex(escaped, false);
82
0
  }
83
84
0
  if (port > 0) {
85
0
    hostname_len = php_fsockopen_format_host_port(&hostname, "", 0, host, host_len, port);
86
0
  } else {
87
0
    hostname_len = host_len;
88
0
    hostname = host;
89
0
  }
90
91
  /* prepare the timeout value for use */
92
0
  if (timeout != -1.0 && !(timeout >= 0.0 && timeout <= (double) PHP_TIMEOUT_ULL_MAX / 1000000.0)) {
93
0
    if (port > 0) {
94
0
      efree(hostname);
95
0
    }
96
97
0
    if (hashkey) {
98
0
      efree(hashkey);
99
0
    }
100
101
0
    zend_argument_value_error(6, "must be -1 or between 0 and " ZEND_ULONG_FMT, ((double) PHP_TIMEOUT_ULL_MAX / 1000000.0));
102
0
    RETURN_THROWS();
103
0
  } else {
104
0
#ifndef PHP_WIN32
105
0
    conv = (time_t) (timeout * 1000000.0);
106
0
    tv.tv_sec = conv / 1000000;
107
#else
108
    conv = (long) (timeout * 1000000.0);
109
    tv.tv_sec = conv / 1000000;
110
#endif
111
0
    tv.tv_usec = conv % 1000000;
112
0
  }
113
114
0
  stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS,
115
0
      STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err);
116
117
0
  if (port > 0) {
118
0
    efree(hostname);
119
0
  }
120
0
  if (stream == NULL) {
121
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));
122
0
  }
123
124
0
  if (hashkey) {
125
0
    efree(hashkey);
126
0
  }
127
128
0
  if (stream == NULL) {
129
0
    if (zerrno) {
130
0
      ZEND_TRY_ASSIGN_REF_LONG(zerrno, err);
131
0
    }
132
0
    if (errstr) {
133
0
      if (zerrstr) {
134
0
        ZEND_TRY_ASSIGN_REF_STR(zerrstr, errstr);
135
0
      } else {
136
0
        zend_string_release(errstr);
137
0
      }
138
0
    }
139
140
0
    RETURN_FALSE;
141
0
  }
142
143
0
  if (zerrno) {
144
0
    ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0);
145
0
  }
146
0
  if (zerrstr) {
147
0
    ZEND_TRY_ASSIGN_REF_EMPTY_STRING(zerrstr);
148
0
  }
149
150
0
  if (errstr) {
151
0
    zend_string_release_ex(errstr, 0);
152
0
  }
153
154
0
  php_stream_to_zval(stream, return_value);
155
0
}
156
157
/* }}} */
158
159
/* {{{ Open Internet or Unix domain socket connection */
160
PHP_FUNCTION(fsockopen)
161
0
{
162
0
  php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
163
0
}
164
/* }}} */
165
166
/* {{{ Open persistent Internet or Unix domain socket connection */
167
PHP_FUNCTION(pfsockopen)
168
0
{
169
0
  php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
170
0
}
171
/* }}} */