Coverage Report

Created: 2026-06-02 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
24
static size_t php_fsockopen_format_host_port(char **message, const char *prefix, size_t prefix_len,
25
  const char *host, size_t host_len, zend_long port)
26
0
{
27
0
    char portbuf[32];
28
0
    int portlen = snprintf(portbuf, sizeof(portbuf), ":" ZEND_LONG_FMT, port);
29
0
    size_t total_len = prefix_len + host_len + portlen;
30
31
0
    char *result = emalloc(total_len + 1); 
32
33
0
  if (prefix_len > 0) {
34
0
      memcpy(result, prefix, prefix_len);
35
0
  }
36
0
    memcpy(result + prefix_len, host, host_len);
37
0
    memcpy(result + prefix_len + host_len, portbuf, portlen);
38
39
0
    result[total_len] = '\0';
40
41
0
    *message = result;
42
43
0
  return total_len;
44
0
}
45
46
/* {{{ php_fsockopen() */
47
48
static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
49
0
{
50
0
  char *host;
51
0
  size_t host_len;
52
0
  zend_long port = -1;
53
0
  zval *zerrno = NULL, *zerrstr = NULL;
54
0
  double timeout;
55
0
  bool timeout_is_null = 1;
56
0
#ifndef PHP_WIN32
57
0
  time_t conv;
58
#else
59
  long conv;
60
#endif
61
0
  struct timeval tv;
62
0
  char *hashkey = NULL;
63
0
  php_stream *stream = NULL;
64
0
  int err;
65
0
  char *hostname = NULL;
66
0
  size_t hostname_len;
67
0
  zend_string *errstr = NULL;
68
69
0
  ZEND_PARSE_PARAMETERS_START(1, 5)
70
0
    Z_PARAM_STRING(host, host_len)
71
0
    Z_PARAM_OPTIONAL
72
0
    Z_PARAM_LONG(port)
73
0
    Z_PARAM_ZVAL(zerrno)
74
0
    Z_PARAM_ZVAL(zerrstr)
75
0
    Z_PARAM_DOUBLE_OR_NULL(timeout, timeout_is_null)
76
0
  ZEND_PARSE_PARAMETERS_END();
77
78
0
  RETVAL_FALSE;
79
80
0
  if (timeout_is_null) {
81
0
    timeout = (double)FG(default_socket_timeout);
82
0
  }
83
84
0
  if (persistent) {
85
0
    php_fsockopen_format_host_port(&hashkey, "pfsockopen__", strlen("pfsockopen__"), host,
86
0
        host_len, port);
87
0
  }
88
89
0
  if (port > 0) {
90
0
    hostname_len = php_fsockopen_format_host_port(&hostname, "", 0, host, host_len, port);
91
0
  } else {
92
0
    hostname_len = host_len;
93
0
    hostname = host;
94
0
  }
95
96
  /* prepare the timeout value for use */
97
0
  if (timeout != -1.0 && !(timeout >= 0.0 && timeout <= (double) PHP_TIMEOUT_ULL_MAX / 1000000.0)) {
98
0
    if (port > 0) {
99
0
      efree(hostname);
100
0
    }
101
102
0
    if (hashkey) {
103
0
      efree(hashkey);
104
0
    }
105
106
0
    zend_argument_value_error(6, "must be -1 or between 0 and " ZEND_ULONG_FMT, ((double) PHP_TIMEOUT_ULL_MAX / 1000000.0));
107
0
    RETURN_THROWS();
108
0
  } else {
109
0
#ifndef PHP_WIN32
110
0
    conv = (time_t) (timeout * 1000000.0);
111
0
    tv.tv_sec = conv / 1000000;
112
#else
113
    conv = (long) (timeout * 1000000.0);
114
    tv.tv_sec = conv / 1000000;
115
#endif
116
0
    tv.tv_usec = conv % 1000000;
117
0
  }
118
119
0
  stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS,
120
0
      STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err);
121
122
0
  if (port > 0) {
123
0
    efree(hostname);
124
0
  }
125
0
  if (stream == NULL) {
126
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));
127
0
  }
128
129
0
  if (hashkey) {
130
0
    efree(hashkey);
131
0
  }
132
133
0
  if (stream == NULL) {
134
0
    if (zerrno) {
135
0
      ZEND_TRY_ASSIGN_REF_LONG(zerrno, err);
136
0
    }
137
0
    if (errstr) {
138
0
      if (zerrstr) {
139
0
        ZEND_TRY_ASSIGN_REF_STR(zerrstr, errstr);
140
0
      } else {
141
0
        zend_string_release(errstr);
142
0
      }
143
0
    }
144
145
0
    RETURN_FALSE;
146
0
  }
147
148
0
  if (zerrno) {
149
0
    ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0);
150
0
  }
151
0
  if (zerrstr) {
152
0
    ZEND_TRY_ASSIGN_REF_EMPTY_STRING(zerrstr);
153
0
  }
154
155
0
  if (errstr) {
156
0
    zend_string_release_ex(errstr, 0);
157
0
  }
158
159
0
  php_stream_to_zval(stream, return_value);
160
0
}
161
162
/* }}} */
163
164
/* {{{ Open Internet or Unix domain socket connection */
165
PHP_FUNCTION(fsockopen)
166
0
{
167
0
  php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
168
0
}
169
/* }}} */
170
171
/* {{{ Open persistent Internet or Unix domain socket connection */
172
PHP_FUNCTION(pfsockopen)
173
0
{
174
0
  php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
175
0
}
176
/* }}} */