xref: /PHP-8.3/ext/standard/fsock.c (revision 350c10d9)
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 
php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS,int persistent)28 static 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 
49 	ZEND_PARSE_PARAMETERS_START(1, 5)
50 		Z_PARAM_STRING(host, host_len)
51 		Z_PARAM_OPTIONAL
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)
56 	ZEND_PARSE_PARAMETERS_END();
57 
58 	RETVAL_FALSE;
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));
86 		RETURN_THROWS();
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 
98 	stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS,
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) {
114 			ZEND_TRY_ASSIGN_REF_LONG(zerrno, err);
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 
124 		RETURN_FALSE;
125 	}
126 
127 	if (zerrno) {
128 		ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0);
129 	}
130 	if (zerrstr) {
131 		ZEND_TRY_ASSIGN_REF_EMPTY_STRING(zerrstr);
132 	}
133 
134 	if (errstr) {
135 		zend_string_release_ex(errstr, 0);
136 	}
137 
138 	php_stream_to_zval(stream, return_value);
139 }
140 
141 /* }}} */
142 
143 /* {{{ Open Internet or Unix domain socket connection */
PHP_FUNCTION(fsockopen)144 PHP_FUNCTION(fsockopen)
145 {
146 	php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
147 }
148 /* }}} */
149 
150 /* {{{ Open persistent Internet or Unix domain socket connection */
PHP_FUNCTION(pfsockopen)151 PHP_FUNCTION(pfsockopen)
152 {
153 	php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
154 }
155 /* }}} */
156