Coverage Report

Created: 2025-06-13 06:43

/src/php-src/main/streams/transports.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
  | Author: Wez Furlong <wez@thebrainroom.com>                           |
14
  +----------------------------------------------------------------------+
15
*/
16
17
#include "php.h"
18
#include "php_streams_int.h"
19
#include "ext/standard/file.h"
20
21
static HashTable xport_hash;
22
23
PHPAPI HashTable *php_stream_xport_get_hash(void)
24
25
{
25
25
  return &xport_hash;
26
25
}
27
28
PHPAPI int php_stream_xport_register(const char *protocol, php_stream_transport_factory factory)
29
64
{
30
64
  zend_string *str = zend_string_init_interned(protocol, strlen(protocol), 1);
31
32
64
  zend_hash_update_ptr(&xport_hash, str, factory);
33
64
  zend_string_release_ex(str, 1);
34
64
  return SUCCESS;
35
64
}
36
37
PHPAPI int php_stream_xport_unregister(const char *protocol)
38
0
{
39
0
  return zend_hash_str_del(&xport_hash, protocol, strlen(protocol));
40
0
}
41
42
#define ERR_REPORT(out_err, fmt, arg) \
43
0
  if (out_err) { *out_err = strpprintf(0, fmt, arg); } \
44
0
  else { php_error_docref(NULL, E_WARNING, fmt, arg); }
45
46
#define ERR_RETURN(out_err, local_err, fmt) \
47
0
  if (out_err) { *out_err = local_err; } \
48
0
  else { php_error_docref(NULL, E_WARNING, fmt, local_err ? ZSTR_VAL(local_err) : "Unspecified error"); \
49
0
    if (local_err) { zend_string_release_ex(local_err, 0); local_err = NULL; } \
50
0
  }
51
52
PHPAPI php_stream *_php_stream_xport_create(const char *name, size_t namelen, int options,
53
    int flags, const char *persistent_id,
54
    struct timeval *timeout,
55
    php_stream_context *context,
56
    zend_string **error_string,
57
    int *error_code
58
    STREAMS_DC)
59
0
{
60
0
  php_stream *stream = NULL;
61
0
  php_stream_transport_factory factory = NULL;
62
0
  const char *p, *protocol, *orig_path = NULL;
63
0
  size_t n = 0;
64
0
  bool failed = false;
65
0
  bool bailout = false;
66
0
  zend_string *error_text = NULL;
67
0
  struct timeval default_timeout = { 0, 0 };
68
69
0
  default_timeout.tv_sec = FG(default_socket_timeout);
70
71
0
  if (timeout == NULL) {
72
0
    timeout = &default_timeout;
73
0
  }
74
75
  /* check for a cached persistent socket */
76
0
  if (persistent_id) {
77
0
    switch(php_stream_from_persistent_id(persistent_id, &stream)) {
78
0
      case PHP_STREAM_PERSISTENT_SUCCESS:
79
        /* use a 0-second timeout when checking if the socket
80
         * has already died */
81
0
        if (PHP_STREAM_OPTION_RETURN_OK == php_stream_set_option(stream, PHP_STREAM_OPTION_CHECK_LIVENESS, 0, NULL)) {
82
0
          return stream;
83
0
        }
84
        /* dead - kill it */
85
0
        php_stream_pclose(stream);
86
0
        stream = NULL;
87
88
        /* fall through */
89
90
0
      case PHP_STREAM_PERSISTENT_FAILURE:
91
0
      default:
92
        /* failed; get a new one */
93
0
        ;
94
0
    }
95
0
  }
96
97
0
  orig_path = name;
98
0
  for (p = name; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++) {
99
0
    n++;
100
0
  }
101
102
0
  if ((*p == ':') && (n > 1) && !strncmp("://", p, 3)) {
103
0
    protocol = name;
104
0
    name = p + 3;
105
0
    namelen -= n + 3;
106
0
  } else {
107
0
    protocol = "tcp";
108
0
    n = 3;
109
0
  }
110
111
0
  if (protocol) {
112
0
    if (NULL == (factory = zend_hash_str_find_ptr(&xport_hash, protocol, n))) {
113
0
      char wrapper_name[32];
114
115
0
      if (n >= sizeof(wrapper_name))
116
0
        n = sizeof(wrapper_name) - 1;
117
0
      PHP_STRLCPY(wrapper_name, protocol, sizeof(wrapper_name), n);
118
119
0
      ERR_REPORT(error_string, "Unable to find the socket transport \"%s\" - did you forget to enable it when you configured PHP?",
120
0
          wrapper_name);
121
122
0
      return NULL;
123
0
    }
124
0
  }
125
126
0
  if (factory == NULL) {
127
    /* should never happen */
128
0
    php_error_docref(NULL, E_WARNING, "Could not find a factory !?");
129
0
    return NULL;
130
0
  }
131
132
0
  stream = (factory)(protocol, n,
133
0
      (char*)name, namelen, persistent_id, options, flags, timeout,
134
0
      context STREAMS_REL_CC);
135
136
0
  if (stream) {
137
0
    zend_try {
138
0
      php_stream_context_set(stream, context);
139
0
      stream->orig_path = pestrdup(orig_path, persistent_id ? 1 : 0);
140
141
0
      if ((flags & STREAM_XPORT_SERVER) == 0) {
142
        /* client */
143
144
0
        if (flags & (STREAM_XPORT_CONNECT|STREAM_XPORT_CONNECT_ASYNC)) {
145
0
          if (-1 == php_stream_xport_connect(stream, name, namelen,
146
0
                flags & STREAM_XPORT_CONNECT_ASYNC ? 1 : 0,
147
0
                timeout, &error_text, error_code)) {
148
149
0
            ERR_RETURN(error_string, error_text, "connect() failed: %s");
150
151
0
            failed = true;
152
0
          }
153
0
        }
154
155
0
      } else {
156
        /* server */
157
0
        if (flags & STREAM_XPORT_BIND) {
158
0
          if (0 != php_stream_xport_bind(stream, name, namelen, &error_text)) {
159
0
            ERR_RETURN(error_string, error_text, "bind() failed: %s");
160
0
            failed = true;
161
0
          } else if (flags & STREAM_XPORT_LISTEN) {
162
0
            zval *zbacklog = NULL;
163
0
            int backlog = 32;
164
165
0
            if (PHP_STREAM_CONTEXT(stream) && (zbacklog = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "backlog")) != NULL) {
166
0
              backlog = zval_get_long(zbacklog);
167
0
            }
168
169
0
            if (0 != php_stream_xport_listen(stream, backlog, &error_text)) {
170
0
              ERR_RETURN(error_string, error_text, "listen() failed: %s");
171
0
              failed = true;
172
0
            }
173
0
          }
174
0
          if (!failed) {
175
0
            stream->flags |= PHP_STREAM_FLAG_NO_IO;
176
0
          }
177
0
        }
178
0
      }
179
0
    } zend_catch {
180
0
      bailout = true;
181
0
    } zend_end_try();
182
0
  }
183
184
0
  if (failed || bailout) {
185
    /* failure means that they don't get a stream to play with */
186
0
    if (persistent_id) {
187
0
      php_stream_pclose(stream);
188
0
    } else {
189
0
      php_stream_close(stream);
190
0
    }
191
0
    stream = NULL;
192
0
    if (bailout) {
193
0
      zend_bailout();
194
0
    }
195
0
  }
196
197
0
  return stream;
198
0
}
199
200
/* Bind the stream to a local address */
201
PHPAPI int php_stream_xport_bind(php_stream *stream,
202
    const char *name, size_t namelen,
203
    zend_string **error_text
204
    )
205
0
{
206
0
  php_stream_xport_param param;
207
0
  int ret;
208
209
0
  memset(&param, 0, sizeof(param));
210
0
  param.op = STREAM_XPORT_OP_BIND;
211
0
  param.inputs.name = (char*)name;
212
0
  param.inputs.namelen = namelen;
213
0
  param.want_errortext = error_text ? 1 : 0;
214
215
0
  ret = php_stream_set_option(stream, PHP_STREAM_OPTION_XPORT_API, 0, &param);
216
217
0
  if (ret == PHP_STREAM_OPTION_RETURN_OK) {
218
0
    if (error_text) {
219
0
      *error_text = param.outputs.error_text;
220
0
    }
221
222
0
    return param.outputs.returncode;
223
0
  }
224
225
0
  return ret;
226
0
}
227
228
/* Connect to a remote address */
229
PHPAPI int php_stream_xport_connect(php_stream *stream,
230
    const char *name, size_t namelen,
231
    int asynchronous,
232
    struct timeval *timeout,
233
    zend_string **error_text,
234
    int *error_code
235
    )
236
0
{
237
0
  php_stream_xport_param param;
238
0
  int ret;
239
240
0
  memset(&param, 0, sizeof(param));
241
0
  param.op = asynchronous ? STREAM_XPORT_OP_CONNECT_ASYNC: STREAM_XPORT_OP_CONNECT;
242
0
  param.inputs.name = (char*)name;
243
0
  param.inputs.namelen = namelen;
244
0
  param.inputs.timeout = timeout;
245
246
0
  param.want_errortext = error_text ? 1 : 0;
247
248
0
  ret = php_stream_set_option(stream, PHP_STREAM_OPTION_XPORT_API, 0, &param);
249
250
0
  if (ret == PHP_STREAM_OPTION_RETURN_OK) {
251
0
    if (error_text) {
252
0
      *error_text = param.outputs.error_text;
253
0
    }
254
0
    if (error_code) {
255
0
      *error_code = param.outputs.error_code;
256
0
    }
257
0
    return param.outputs.returncode;
258
0
  }
259
260
0
  return ret;
261
262
0
}
263
264
/* Prepare to listen */
265
PHPAPI int php_stream_xport_listen(php_stream *stream, int backlog, zend_string **error_text)
266
0
{
267
0
  php_stream_xport_param param;
268
0
  int ret;
269
270
0
  memset(&param, 0, sizeof(param));
271
0
  param.op = STREAM_XPORT_OP_LISTEN;
272
0
  param.inputs.backlog = backlog;
273
0
  param.want_errortext = error_text ? 1 : 0;
274
275
0
  ret = php_stream_set_option(stream, PHP_STREAM_OPTION_XPORT_API, 0, &param);
276
277
0
  if (ret == PHP_STREAM_OPTION_RETURN_OK) {
278
0
    if (error_text) {
279
0
      *error_text = param.outputs.error_text;
280
0
    }
281
282
0
    return param.outputs.returncode;
283
0
  }
284
285
0
  return ret;
286
0
}
287
288
/* Get the next client and their address (as a string) */
289
PHPAPI int php_stream_xport_accept(php_stream *stream, php_stream **client,
290
    zend_string **textaddr,
291
    void **addr, socklen_t *addrlen,
292
    struct timeval *timeout,
293
    zend_string **error_text
294
    )
295
0
{
296
0
  php_stream_xport_param param;
297
0
  int ret;
298
299
0
  memset(&param, 0, sizeof(param));
300
301
0
  param.op = STREAM_XPORT_OP_ACCEPT;
302
0
  param.inputs.timeout = timeout;
303
0
  param.want_addr = addr ? 1 : 0;
304
0
  param.want_textaddr = textaddr ? 1 : 0;
305
0
  param.want_errortext = error_text ? 1 : 0;
306
307
0
  ret = php_stream_set_option(stream, PHP_STREAM_OPTION_XPORT_API, 0, &param);
308
309
0
  if (ret == PHP_STREAM_OPTION_RETURN_OK) {
310
0
    *client = param.outputs.client;
311
0
    if (addr) {
312
0
      *addr = param.outputs.addr;
313
0
      *addrlen = param.outputs.addrlen;
314
0
    }
315
0
    if (textaddr) {
316
0
      *textaddr = param.outputs.textaddr;
317
0
    }
318
0
    if (error_text) {
319
0
      *error_text = param.outputs.error_text;
320
0
    }
321
322
0
    return param.outputs.returncode;
323
0
  }
324
0
  return ret;
325
0
}
326
327
PHPAPI int php_stream_xport_get_name(php_stream *stream, int want_peer,
328
    zend_string **textaddr,
329
    void **addr, socklen_t *addrlen
330
    )
331
0
{
332
0
  php_stream_xport_param param;
333
0
  int ret;
334
335
0
  memset(&param, 0, sizeof(param));
336
337
0
  param.op = want_peer ? STREAM_XPORT_OP_GET_PEER_NAME : STREAM_XPORT_OP_GET_NAME;
338
0
  param.want_addr = addr ? 1 : 0;
339
0
  param.want_textaddr = textaddr ? 1 : 0;
340
341
0
  ret = php_stream_set_option(stream, PHP_STREAM_OPTION_XPORT_API, 0, &param);
342
343
0
  if (ret == PHP_STREAM_OPTION_RETURN_OK) {
344
0
    if (addr) {
345
0
      *addr = param.outputs.addr;
346
0
      *addrlen = param.outputs.addrlen;
347
0
    }
348
0
    if (textaddr) {
349
0
      *textaddr = param.outputs.textaddr;
350
0
    }
351
352
0
    return param.outputs.returncode;
353
0
  }
354
0
  return ret;
355
0
}
356
357
PHPAPI int php_stream_xport_crypto_setup(php_stream *stream, php_stream_xport_crypt_method_t crypto_method, php_stream *session_stream)
358
0
{
359
0
  php_stream_xport_crypto_param param;
360
0
  int ret;
361
362
0
  memset(&param, 0, sizeof(param));
363
0
  param.op = STREAM_XPORT_CRYPTO_OP_SETUP;
364
0
  param.inputs.method = crypto_method;
365
0
  param.inputs.session = session_stream;
366
367
0
  ret = php_stream_set_option(stream, PHP_STREAM_OPTION_CRYPTO_API, 0, &param);
368
369
0
  if (ret == PHP_STREAM_OPTION_RETURN_OK) {
370
0
    return param.outputs.returncode;
371
0
  }
372
373
0
  php_error_docref("streams.crypto", E_WARNING, "This stream does not support SSL/crypto");
374
375
0
  return ret;
376
0
}
377
378
PHPAPI int php_stream_xport_crypto_enable(php_stream *stream, int activate)
379
0
{
380
0
  php_stream_xport_crypto_param param;
381
0
  int ret;
382
383
0
  memset(&param, 0, sizeof(param));
384
0
  param.op = STREAM_XPORT_CRYPTO_OP_ENABLE;
385
0
  param.inputs.activate = activate;
386
387
0
  ret = php_stream_set_option(stream, PHP_STREAM_OPTION_CRYPTO_API, 0, &param);
388
389
0
  if (ret == PHP_STREAM_OPTION_RETURN_OK) {
390
0
    return param.outputs.returncode;
391
0
  }
392
393
0
  php_error_docref("streams.crypto", E_WARNING, "This stream does not support SSL/crypto");
394
395
0
  return ret;
396
0
}
397
398
/* Similar to recv() system call; read data from the stream, optionally
399
 * peeking, optionally retrieving OOB data */
400
PHPAPI int php_stream_xport_recvfrom(php_stream *stream, char *buf, size_t buflen,
401
    int flags, void **addr, socklen_t *addrlen, zend_string **textaddr
402
    )
403
0
{
404
0
  php_stream_xport_param param;
405
0
  int ret = 0;
406
0
  int recvd_len = 0;
407
#if 0
408
  int oob;
409
410
  if (flags == 0 && addr == NULL) {
411
    return php_stream_read(stream, buf, buflen);
412
  }
413
414
  if (stream->readfilters.head) {
415
    php_error_docref(NULL, E_WARNING, "Cannot peek or fetch OOB data from a filtered stream");
416
    return -1;
417
  }
418
419
  oob = (flags & STREAM_OOB) == STREAM_OOB;
420
421
  if (!oob && addr == NULL) {
422
    /* must be peeking at regular data; copy content from the buffer
423
     * first, then adjust the pointer/len before handing off to the
424
     * stream */
425
    recvd_len = stream->writepos - stream->readpos;
426
    if (recvd_len > buflen) {
427
      recvd_len = buflen;
428
    }
429
    if (recvd_len) {
430
      memcpy(buf, stream->readbuf, recvd_len);
431
      buf += recvd_len;
432
      buflen -= recvd_len;
433
    }
434
    /* if we filled their buffer, return */
435
    if (buflen == 0) {
436
      return recvd_len;
437
    }
438
  }
439
#endif
440
441
  /* otherwise, we are going to bypass the buffer */
442
443
0
  memset(&param, 0, sizeof(param));
444
445
0
  param.op = STREAM_XPORT_OP_RECV;
446
0
  param.want_addr = addr ? 1 : 0;
447
0
  param.want_textaddr = textaddr ? 1 : 0;
448
0
  param.inputs.buf = buf;
449
0
  param.inputs.buflen = buflen;
450
0
  param.inputs.flags = flags;
451
452
0
  ret = php_stream_set_option(stream, PHP_STREAM_OPTION_XPORT_API, 0, &param);
453
454
0
  if (ret == PHP_STREAM_OPTION_RETURN_OK) {
455
0
    if (addr) {
456
0
      *addr = param.outputs.addr;
457
0
      *addrlen = param.outputs.addrlen;
458
0
    }
459
0
    if (textaddr) {
460
0
      *textaddr = param.outputs.textaddr;
461
0
    }
462
0
    return recvd_len + param.outputs.returncode;
463
0
  }
464
0
  return recvd_len ? recvd_len : -1;
465
0
}
466
467
/* Similar to send() system call; send data to the stream, optionally
468
 * sending it as OOB data */
469
PHPAPI int php_stream_xport_sendto(php_stream *stream, const char *buf, size_t buflen,
470
    int flags, void *addr, socklen_t addrlen)
471
0
{
472
0
  php_stream_xport_param param;
473
0
  int ret = 0;
474
0
  int oob;
475
476
#if 0
477
  if (flags == 0 && addr == NULL) {
478
    return php_stream_write(stream, buf, buflen);
479
  }
480
#endif
481
482
0
  oob = (flags & STREAM_OOB) == STREAM_OOB;
483
484
0
  if ((oob || addr) && stream->writefilters.head) {
485
0
    php_error_docref(NULL, E_WARNING, "Cannot write OOB data, or data to a targeted address on a filtered stream");
486
0
    return -1;
487
0
  }
488
489
0
  memset(&param, 0, sizeof(param));
490
491
0
  param.op = STREAM_XPORT_OP_SEND;
492
0
  param.want_addr = addr ? 1 : 0;
493
0
  param.inputs.buf = (char*)buf;
494
0
  param.inputs.buflen = buflen;
495
0
  param.inputs.flags = flags;
496
0
  param.inputs.addr = addr;
497
0
  param.inputs.addrlen = addrlen;
498
499
0
  ret = php_stream_set_option(stream, PHP_STREAM_OPTION_XPORT_API, 0, &param);
500
501
0
  if (ret == PHP_STREAM_OPTION_RETURN_OK) {
502
0
    return param.outputs.returncode;
503
0
  }
504
0
  return -1;
505
0
}
506
507
/* Similar to shutdown() system call; shut down part of a full-duplex
508
 * connection */
509
PHPAPI int php_stream_xport_shutdown(php_stream *stream, stream_shutdown_t how)
510
0
{
511
0
  php_stream_xport_param param;
512
0
  int ret = 0;
513
514
0
  memset(&param, 0, sizeof(param));
515
516
0
  param.op = STREAM_XPORT_OP_SHUTDOWN;
517
0
  param.how = how;
518
519
0
  ret = php_stream_set_option(stream, PHP_STREAM_OPTION_XPORT_API, 0, &param);
520
521
0
  if (ret == PHP_STREAM_OPTION_RETURN_OK) {
522
0
    return param.outputs.returncode;
523
0
  }
524
0
  return -1;
525
0
}