xref: /PHP-7.2/ext/standard/fsock.c (revision 7a7ec01a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Paul Panotzki - Bunyip Information Systems                  |
16    |          Jim Winstead <jimw@php.net>                                 |
17    |          Sascha Schumann <sascha@schumann.cx>                        |
18    +----------------------------------------------------------------------+
19 */
20 
21 /* $Id$ */
22 
23 #include "php.h"
24 #include "php_globals.h"
25 #include <stdlib.h>
26 #include <stddef.h>
27 #include "php_network.h"
28 #include "file.h"
29 
30 /* {{{ php_fsockopen() */
31 
php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS,int persistent)32 static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
33 {
34 	char *host;
35 	size_t host_len;
36 	zend_long port = -1;
37 	zval *zerrno = NULL, *zerrstr = NULL;
38 	double timeout = (double)FG(default_socket_timeout);
39 #ifndef PHP_WIN32
40 	time_t conv;
41 #else
42 	long conv;
43 #endif
44 	struct timeval tv;
45 	char *hashkey = NULL;
46 	php_stream *stream = NULL;
47 	int err;
48 	char *hostname = NULL;
49 	size_t hostname_len;
50 	zend_string *errstr = NULL;
51 
52 	RETVAL_FALSE;
53 
54 	ZEND_PARSE_PARAMETERS_START(1, 5)
55 		Z_PARAM_STRING(host, host_len)
56 		Z_PARAM_OPTIONAL
57 		Z_PARAM_LONG(port)
58 		Z_PARAM_ZVAL_DEREF(zerrno)
59 		Z_PARAM_ZVAL_DEREF(zerrstr)
60 		Z_PARAM_DOUBLE(timeout)
61 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
62 
63 	if (persistent) {
64 		spprintf(&hashkey, 0, "pfsockopen__%s:" ZEND_LONG_FMT, host, port);
65 	}
66 
67 	if (port > 0) {
68 		hostname_len = spprintf(&hostname, 0, "%s:" ZEND_LONG_FMT, host, port);
69 	} else {
70 		hostname_len = host_len;
71 		hostname = host;
72 	}
73 
74 	/* prepare the timeout value for use */
75 #ifndef PHP_WIN32
76 	conv = (time_t) (timeout * 1000000.0);
77 	tv.tv_sec = conv / 1000000;
78 #else
79 	conv = (long) (timeout * 1000000.0);
80 	tv.tv_sec = conv / 1000000;
81 #endif
82 	tv.tv_usec = conv % 1000000;
83 
84 	if (zerrno)	{
85 		zval_ptr_dtor(zerrno);
86 		ZVAL_LONG(zerrno, 0);
87 	}
88 	if (zerrstr) {
89 		zval_ptr_dtor(zerrstr);
90 		ZVAL_EMPTY_STRING(zerrstr);
91 	}
92 
93 	stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS,
94 			STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err);
95 
96 	if (port > 0) {
97 		efree(hostname);
98 	}
99 	if (stream == NULL) {
100 		php_error_docref(NULL, E_WARNING, "unable to connect to %s:" ZEND_LONG_FMT " (%s)", host, port, errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr));
101 	}
102 
103 	if (hashkey) {
104 		efree(hashkey);
105 	}
106 
107 	if (stream == NULL)	{
108 		if (zerrno) {
109 			zval_ptr_dtor(zerrno);
110 			ZVAL_LONG(zerrno, err);
111 		}
112 		if (zerrstr && errstr) {
113 			/* no need to dup; we need to efree buf anyway */
114 			zval_ptr_dtor(zerrstr);
115 			ZVAL_STR(zerrstr, errstr);
116 		} else if (!zerrstr && errstr) {
117 			zend_string_release(errstr);
118 		}
119 
120 		RETURN_FALSE;
121 	}
122 
123 	if (errstr) {
124 		zend_string_release(errstr);
125 	}
126 
127 	php_stream_to_zval(stream, return_value);
128 }
129 
130 /* }}} */
131 
132 /* {{{ proto resource fsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
133    Open Internet or Unix domain socket connection */
PHP_FUNCTION(fsockopen)134 PHP_FUNCTION(fsockopen)
135 {
136 	php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
137 }
138 /* }}} */
139 
140 /* {{{ proto resource pfsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]])
141    Open persistent Internet or Unix domain socket connection */
PHP_FUNCTION(pfsockopen)142 PHP_FUNCTION(pfsockopen)
143 {
144 	php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
145 }
146 /* }}} */
147 
148 /*
149  * Local variables:
150  * tab-width: 4
151  * c-basic-offset: 4
152  * End:
153  * vim600: sw=4 ts=4 fdm=marker
154  * vim<600: sw=4 ts=4
155  */
156