php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
fsock.c
Go to the documentation of this file.
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
28static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
29{
30 char *host;
31 size_t host_len;
32 zend_long port = -1;
33 zval *zerrno = NULL, *zerrstr = NULL;
34 double timeout;
35 bool timeout_is_null = 1;
36#ifndef PHP_WIN32
37 time_t conv;
38#else
39 long conv;
40#endif
41 struct timeval tv;
42 char *hashkey = NULL;
43 php_stream *stream = NULL;
44 int err;
45 char *hostname = NULL;
46 size_t hostname_len;
47 zend_string *errstr = NULL;
48
50 Z_PARAM_STRING(host, host_len)
52 Z_PARAM_LONG(port)
53 Z_PARAM_ZVAL(zerrno)
54 Z_PARAM_ZVAL(zerrstr)
55 Z_PARAM_DOUBLE_OR_NULL(timeout, timeout_is_null)
57
59
60 if (timeout_is_null) {
61 timeout = (double)FG(default_socket_timeout);
62 }
63
64 if (persistent) {
65 spprintf(&hashkey, 0, "pfsockopen__%s:" ZEND_LONG_FMT, host, port);
66 }
67
68 if (port > 0) {
69 hostname_len = spprintf(&hostname, 0, "%s:" ZEND_LONG_FMT, host, port);
70 } else {
71 hostname_len = host_len;
72 hostname = host;
73 }
74
75 /* prepare the timeout value for use */
76 if (timeout != -1.0 && !(timeout >= 0.0 && timeout <= (double) PHP_TIMEOUT_ULL_MAX / 1000000.0)) {
77 if (port > 0) {
78 efree(hostname);
79 }
80
81 if (hashkey) {
82 efree(hashkey);
83 }
84
85 zend_argument_value_error(6, "must be -1 or between 0 and " ZEND_ULONG_FMT, ((double) PHP_TIMEOUT_ULL_MAX / 1000000.0));
87 } else {
88#ifndef PHP_WIN32
89 conv = (time_t) (timeout * 1000000.0);
90 tv.tv_sec = conv / 1000000;
91#else
92 conv = (long) (timeout * 1000000.0);
93 tv.tv_sec = conv / 1000000;
94#endif
95 tv.tv_usec = conv % 1000000;
96 }
97
99 STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err);
100
101 if (port > 0) {
102 efree(hostname);
103 }
104 if (stream == NULL) {
105 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 }
107
108 if (hashkey) {
109 efree(hashkey);
110 }
111
112 if (stream == NULL) {
113 if (zerrno) {
115 }
116 if (errstr) {
117 if (zerrstr) {
118 ZEND_TRY_ASSIGN_REF_STR(zerrstr, errstr);
119 } else {
120 zend_string_release(errstr);
121 }
122 }
123
125 }
126
127 if (zerrno) {
128 ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0);
129 }
130 if (zerrstr) {
132 }
133
134 if (errstr) {
135 zend_string_release_ex(errstr, 0);
136 }
137
139}
140
141/* }}} */
142
143/* {{{ Open Internet or Unix domain socket connection */
145{
146 php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
147}
148/* }}} */
149
150/* {{{ Open persistent Internet or Unix domain socket connection */
152{
153 php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
154}
155/* }}} */
pfsockopen(string $hostname, int $port=-1, &$error_code=null, &$error_message=null, ?float $timeout=null)
fsockopen(string $hostname, int $port=-1, &$error_code=null, &$error_message=null, ?float $timeout=null)
size_t hostname_len
Definition dns_win32.c:46
char * err
Definition ffi.c:3029
ffi persistent
Definition ffi.c:3633
#define NULL
Definition gdcache.h:45
foreach($dp as $el) foreach( $dp as $el) if( $pass2< 2) echo ""
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
Definition main.c:1173
#define PHP_FUNCTION
Definition php.h:364
#define php_stream_xport_create(name, namelen, options, flags, persistent_id, timeout, context, estr, ecode)
#define STREAM_XPORT_CONNECT
#define STREAM_XPORT_CLIENT
struct _php_stream php_stream
Definition php_streams.h:96
#define REPORT_ERRORS
#define php_stream_to_zval(stream, zval)
struct timeval tv
Definition session.c:1280
#define spprintf
Definition spprintf.h:29
#define PHP_TIMEOUT_ULL_MAX
Definition file.h:64
#define FG(v)
Definition file.h:117
#define INTERNAL_FUNCTION_PARAMETERS
Definition zend.h:49
#define INTERNAL_FUNCTION_PARAM_PASSTHRU
Definition zend.h:50
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:433
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define RETURN_FALSE
Definition zend_API.h:1058
#define Z_PARAM_OPTIONAL
Definition zend_API.h:1667
#define Z_PARAM_STRING(dest, dest_len)
Definition zend_API.h:2071
#define Z_PARAM_DOUBLE_OR_NULL(dest, is_null)
Definition zend_API.h:1806
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define ZEND_TRY_ASSIGN_REF_LONG(zv, lval)
Definition zend_API.h:1205
#define Z_PARAM_LONG(dest)
Definition zend_API.h:1896
#define RETURN_THROWS()
Definition zend_API.h:1060
#define ZEND_TRY_ASSIGN_REF_EMPTY_STRING(zv)
Definition zend_API.h:1249
#define Z_PARAM_ZVAL(dest)
Definition zend_API.h:2100
#define ZEND_TRY_ASSIGN_REF_STR(zv, str)
Definition zend_API.h:1271
#define RETVAL_FALSE
Definition zend_API.h:1032
#define efree(ptr)
Definition zend_alloc.h:155
struct _zval_struct zval
zend_string_release_ex(func->internal_function.function_name, 0)
#define E_WARNING
Definition zend_errors.h:24
#define ZEND_ULONG_FMT
Definition zend_long.h:88
int32_t zend_long
Definition zend_long.h:42
#define ZEND_LONG_FMT
Definition zend_long.h:87
struct _zend_string zend_string
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
zval * return_value