/src/php-src/ext/standard/fsock.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 | | | http://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 | | |
28 | | static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent) |
29 | 0 | { |
30 | 0 | char *host; |
31 | 0 | size_t host_len; |
32 | 0 | zend_long port = -1; |
33 | 0 | zval *zerrno = NULL, *zerrstr = NULL; |
34 | 0 | double timeout = (double)FG(default_socket_timeout); |
35 | 0 | #ifndef PHP_WIN32 |
36 | 0 | time_t conv; |
37 | | #else |
38 | | long conv; |
39 | | #endif |
40 | 0 | struct timeval tv; |
41 | 0 | char *hashkey = NULL; |
42 | 0 | php_stream *stream = NULL; |
43 | 0 | int err; |
44 | 0 | char *hostname = NULL; |
45 | 0 | size_t hostname_len; |
46 | 0 | zend_string *errstr = NULL; |
47 | |
|
48 | 0 | RETVAL_FALSE; |
49 | |
|
50 | 0 | ZEND_PARSE_PARAMETERS_START(1, 5) |
51 | 0 | Z_PARAM_STRING(host, host_len) |
52 | 0 | Z_PARAM_OPTIONAL |
53 | 0 | Z_PARAM_LONG(port) |
54 | 0 | Z_PARAM_ZVAL(zerrno) |
55 | 0 | Z_PARAM_ZVAL(zerrstr) |
56 | 0 | Z_PARAM_DOUBLE(timeout) |
57 | 0 | ZEND_PARSE_PARAMETERS_END(); |
58 | |
|
59 | 0 | if (persistent) { |
60 | 0 | spprintf(&hashkey, 0, "pfsockopen__%s:" ZEND_LONG_FMT, host, port); |
61 | 0 | } |
62 | |
|
63 | 0 | if (port > 0) { |
64 | 0 | hostname_len = spprintf(&hostname, 0, "%s:" ZEND_LONG_FMT, host, port); |
65 | 0 | } else { |
66 | 0 | hostname_len = host_len; |
67 | 0 | hostname = host; |
68 | 0 | } |
69 | | |
70 | | /* prepare the timeout value for use */ |
71 | 0 | #ifndef PHP_WIN32 |
72 | 0 | conv = (time_t) (timeout * 1000000.0); |
73 | 0 | tv.tv_sec = conv / 1000000; |
74 | | #else |
75 | | conv = (long) (timeout * 1000000.0); |
76 | | tv.tv_sec = conv / 1000000; |
77 | | #endif |
78 | 0 | tv.tv_usec = conv % 1000000; |
79 | |
|
80 | 0 | stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS, |
81 | 0 | STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err); |
82 | |
|
83 | 0 | if (port > 0) { |
84 | 0 | efree(hostname); |
85 | 0 | } |
86 | 0 | if (stream == NULL) { |
87 | 0 | php_error_docref(NULL, E_WARNING, "Unable to connect to %s:" ZEND_LONG_FMT " (%s)", host, port, errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr)); |
88 | 0 | } |
89 | |
|
90 | 0 | if (hashkey) { |
91 | 0 | efree(hashkey); |
92 | 0 | } |
93 | |
|
94 | 0 | if (stream == NULL) { |
95 | 0 | if (zerrno) { |
96 | 0 | ZEND_TRY_ASSIGN_REF_LONG(zerrno, err); |
97 | 0 | } |
98 | 0 | if (errstr) { |
99 | 0 | if (zerrstr) { |
100 | 0 | ZEND_TRY_ASSIGN_REF_STR(zerrstr, errstr); |
101 | 0 | } else { |
102 | 0 | zend_string_release(errstr); |
103 | 0 | } |
104 | 0 | } |
105 | |
|
106 | 0 | RETURN_FALSE; |
107 | 0 | } |
108 | |
|
109 | 0 | if (zerrno) { |
110 | 0 | ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0); |
111 | 0 | } |
112 | 0 | if (zerrstr) { |
113 | 0 | ZEND_TRY_ASSIGN_REF_EMPTY_STRING(zerrstr); |
114 | 0 | } |
115 | |
|
116 | 0 | if (errstr) { |
117 | 0 | zend_string_release_ex(errstr, 0); |
118 | 0 | } |
119 | |
|
120 | 0 | php_stream_to_zval(stream, return_value); |
121 | 0 | } |
122 | | |
123 | | /* }}} */ |
124 | | |
125 | | /* {{{ proto resource fsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]]) |
126 | | Open Internet or Unix domain socket connection */ |
127 | | PHP_FUNCTION(fsockopen) |
128 | 0 | { |
129 | 0 | php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); |
130 | 0 | } |
131 | | /* }}} */ |
132 | | |
133 | | /* {{{ proto resource pfsockopen(string hostname, int port [, int errno [, string errstr [, float timeout]]]) |
134 | | Open persistent Internet or Unix domain socket connection */ |
135 | | PHP_FUNCTION(pfsockopen) |
136 | 0 | { |
137 | 0 | php_fsockopen_stream(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); |
138 | 0 | } |
139 | | /* }}} */ |