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 #ifndef PHP_WIN32
77 conv = (time_t) (timeout * 1000000.0);
78 tv.tv_sec = conv / 1000000;
79 #else
80 conv = (long) (timeout * 1000000.0);
81 tv.tv_sec = conv / 1000000;
82 #endif
83 tv.tv_usec = conv % 1000000;
84
85 stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS,
86 STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err);
87
88 if (port > 0) {
89 efree(hostname);
90 }
91 if (stream == NULL) {
92 php_error_docref(NULL, E_WARNING, "Unable to connect to %s:" ZEND_LONG_FMT " (%s)", host, port, errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr));
93 }
94
95 if (hashkey) {
96 efree(hashkey);
97 }
98
99 if (stream == NULL) {
100 if (zerrno) {
101 ZEND_TRY_ASSIGN_REF_LONG(zerrno, err);
102 }
103 if (errstr) {
104 if (zerrstr) {
105 ZEND_TRY_ASSIGN_REF_STR(zerrstr, errstr);
106 } else {
107 zend_string_release(errstr);
108 }
109 }
110
111 RETURN_FALSE;
112 }
113
114 if (zerrno) {
115 ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0);
116 }
117 if (zerrstr) {
118 ZEND_TRY_ASSIGN_REF_EMPTY_STRING(zerrstr);
119 }
120
121 if (errstr) {
122 zend_string_release_ex(errstr, 0);
123 }
124
125 php_stream_to_zval(stream, return_value);
126 }
127
128 /* }}} */
129
130 /* {{{ Open Internet or Unix domain socket connection */
PHP_FUNCTION(fsockopen)131 PHP_FUNCTION(fsockopen)
132 {
133 php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
134 }
135 /* }}} */
136
137 /* {{{ Open persistent Internet or Unix domain socket connection */
PHP_FUNCTION(pfsockopen)138 PHP_FUNCTION(pfsockopen)
139 {
140 php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
141 }
142 /* }}} */
143