1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2017 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   | Author: Wez Furlong <wez@thebrainroom.com>                           |
16   +----------------------------------------------------------------------+
17 */
18 
19 /* $Id$ */
20 #ifdef PHP_WIN32
21 #include "config.w32.h"
22 #include <Ws2tcpip.h>
23 #endif
24 
25 #if HAVE_SYS_SOCKET_H
26 # include <sys/socket.h>
27 #endif
28 
29 typedef php_stream *(php_stream_transport_factory_func)(const char *proto, size_t protolen,
30 		const char *resourcename, size_t resourcenamelen,
31 		const char *persistent_id, int options, int flags,
32 		struct timeval *timeout,
33 		php_stream_context *context STREAMS_DC);
34 typedef php_stream_transport_factory_func *php_stream_transport_factory;
35 
36 BEGIN_EXTERN_C()
37 PHPAPI int php_stream_xport_register(const char *protocol, php_stream_transport_factory factory);
38 PHPAPI int php_stream_xport_unregister(const char *protocol);
39 
40 #define STREAM_XPORT_CLIENT			0
41 #define STREAM_XPORT_SERVER			1
42 
43 #define STREAM_XPORT_CONNECT		2
44 #define STREAM_XPORT_BIND			4
45 #define STREAM_XPORT_LISTEN			8
46 #define STREAM_XPORT_CONNECT_ASYNC	16
47 
48 /* Open a client or server socket connection */
49 PHPAPI php_stream *_php_stream_xport_create(const char *name, size_t namelen, int options,
50 		int flags, const char *persistent_id,
51 		struct timeval *timeout,
52 		php_stream_context *context,
53 		zend_string **error_string,
54 		int *error_code
55 		STREAMS_DC);
56 
57 #define php_stream_xport_create(name, namelen, options, flags, persistent_id, timeout, context, estr, ecode) \
58 	_php_stream_xport_create(name, namelen, options, flags, persistent_id, timeout, context, estr, ecode STREAMS_CC)
59 
60 /* Bind the stream to a local address */
61 PHPAPI int php_stream_xport_bind(php_stream *stream,
62 		const char *name, size_t namelen,
63 		zend_string **error_text
64 		);
65 
66 /* Connect to a remote address */
67 PHPAPI int php_stream_xport_connect(php_stream *stream,
68 		const char *name, size_t namelen,
69 		int asynchronous,
70 		struct timeval *timeout,
71 		zend_string **error_text,
72 		int *error_code
73 		);
74 
75 /* Prepare to listen */
76 PHPAPI int php_stream_xport_listen(php_stream *stream,
77 		int backlog,
78 		zend_string **error_text
79 		);
80 
81 /* Get the next client and their address as a string, or the underlying address
82  * structure.  You must efree either of these if you request them */
83 PHPAPI int php_stream_xport_accept(php_stream *stream, php_stream **client,
84 		zend_string **textaddr,
85 		void **addr, socklen_t *addrlen,
86 		struct timeval *timeout,
87 		zend_string **error_text
88 		);
89 
90 /* Get the name of either the socket or it's peer */
91 PHPAPI int php_stream_xport_get_name(php_stream *stream, int want_peer,
92 		zend_string **textaddr,
93 		void **addr, socklen_t *addrlen
94 		);
95 
96 enum php_stream_xport_send_recv_flags {
97 	STREAM_OOB = 1,
98 	STREAM_PEEK = 2
99 };
100 
101 /* Similar to recv() system call; read data from the stream, optionally
102  * peeking, optionally retrieving OOB data */
103 PHPAPI int php_stream_xport_recvfrom(php_stream *stream, char *buf, size_t buflen,
104 		int flags, void **addr, socklen_t *addrlen,
105 		zend_string **textaddr);
106 
107 /* Similar to send() system call; send data to the stream, optionally
108  * sending it as OOB data */
109 PHPAPI int php_stream_xport_sendto(php_stream *stream, const char *buf, size_t buflen,
110 		int flags, void *addr, socklen_t addrlen);
111 
112 typedef enum {
113 	STREAM_SHUT_RD,
114 	STREAM_SHUT_WR,
115 	STREAM_SHUT_RDWR
116 } stream_shutdown_t;
117 
118 /* Similar to shutdown() system call; shut down part of a full-duplex
119  * connection */
120 PHPAPI int php_stream_xport_shutdown(php_stream *stream, stream_shutdown_t how);
121 END_EXTERN_C()
122 
123 
124 /* Structure definition for the set_option interface that the above functions wrap */
125 
126 typedef struct _php_stream_xport_param {
127 	enum {
128 		STREAM_XPORT_OP_BIND, STREAM_XPORT_OP_CONNECT,
129 		STREAM_XPORT_OP_LISTEN, STREAM_XPORT_OP_ACCEPT,
130 		STREAM_XPORT_OP_CONNECT_ASYNC,
131 		STREAM_XPORT_OP_GET_NAME,
132 		STREAM_XPORT_OP_GET_PEER_NAME,
133 		STREAM_XPORT_OP_RECV,
134 		STREAM_XPORT_OP_SEND,
135 		STREAM_XPORT_OP_SHUTDOWN
136 	} op;
137 	unsigned int want_addr:1;
138 	unsigned int want_textaddr:1;
139 	unsigned int want_errortext:1;
140 	unsigned int how:2;
141 
142 	struct {
143 		char *name;
144 		size_t namelen;
145 		struct timeval *timeout;
146 		struct sockaddr *addr;
147 		char *buf;
148 		size_t buflen;
149 		socklen_t addrlen;
150 		int backlog;
151 		int flags;
152 	} inputs;
153 	struct {
154 		php_stream *client;
155 		struct sockaddr *addr;
156 		socklen_t addrlen;
157 		zend_string *textaddr;
158 		zend_string *error_text;
159 		int returncode;
160 		int error_code;
161 	} outputs;
162 } php_stream_xport_param;
163 
164 /* Because both client and server streams use the same mechanisms
165    for encryption we use the LSB to denote clients.
166 */
167 typedef enum {
168 	STREAM_CRYPTO_METHOD_SSLv2_CLIENT = (1 << 1 | 1),
169 	STREAM_CRYPTO_METHOD_SSLv3_CLIENT = (1 << 2 | 1),
170 	/* v23 no longer negotiates SSL2 or SSL3 */
171 	STREAM_CRYPTO_METHOD_SSLv23_CLIENT = ((1 << 3) | (1 << 4) | (1 << 5) | 1),
172 	STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT = (1 << 3 | 1),
173 	STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT = (1 << 4 | 1),
174 	STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT = (1 << 5 | 1),
175 	/* tls now equates only to the specific TLSv1 method for BC with pre-5.6 */
176 	STREAM_CRYPTO_METHOD_TLS_CLIENT = (1 << 3 | 1),
177 	STREAM_CRYPTO_METHOD_TLS_ANY_CLIENT = ((1 << 3) | (1 << 4) | (1 << 5) | 1),
178 	STREAM_CRYPTO_METHOD_ANY_CLIENT = ((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | 1),
179 	STREAM_CRYPTO_METHOD_SSLv2_SERVER = (1 << 1),
180 	STREAM_CRYPTO_METHOD_SSLv3_SERVER = (1 << 2),
181 	/* v23 no longer negotiates SSL2 or SSL3 */
182 	STREAM_CRYPTO_METHOD_SSLv23_SERVER = ((1 << 3) | (1 << 4) | (1 << 5)),
183 	STREAM_CRYPTO_METHOD_TLSv1_0_SERVER = (1 << 3),
184 	STREAM_CRYPTO_METHOD_TLSv1_1_SERVER = (1 << 4),
185 	STREAM_CRYPTO_METHOD_TLSv1_2_SERVER = (1 << 5),
186 	/* tls equates only to the specific TLSv1 method for BC with pre-5.6 */
187 	STREAM_CRYPTO_METHOD_TLS_SERVER = (1 << 3),
188 	STREAM_CRYPTO_METHOD_TLS_ANY_SERVER = ((1 << 3) | (1 << 4) | (1 << 5)),
189 	STREAM_CRYPTO_METHOD_ANY_SERVER = ((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5))
190 } php_stream_xport_crypt_method_t;
191 
192 /* These functions provide crypto support on the underlying transport */
193 
194 BEGIN_EXTERN_C()
195 PHPAPI int php_stream_xport_crypto_setup(php_stream *stream, php_stream_xport_crypt_method_t crypto_method, php_stream *session_stream);
196 PHPAPI int php_stream_xport_crypto_enable(php_stream *stream, int activate);
197 END_EXTERN_C()
198 
199 typedef struct _php_stream_xport_crypto_param {
200 	struct {
201 		php_stream *session;
202 		int activate;
203 		php_stream_xport_crypt_method_t method;
204 	} inputs;
205 	struct {
206 		int returncode;
207 	} outputs;
208 	enum {
209 		STREAM_XPORT_CRYPTO_OP_SETUP,
210 		STREAM_XPORT_CRYPTO_OP_ENABLE
211 	} op;
212 } php_stream_xport_crypto_param;
213 
214 BEGIN_EXTERN_C()
215 PHPAPI HashTable *php_stream_xport_get_hash(void);
216 PHPAPI php_stream_transport_factory_func php_stream_generic_socket_factory;
217 END_EXTERN_C()
218 
219 /*
220  * Local variables:
221  * tab-width: 4
222  * c-basic-offset: 4
223  * End:
224  * vim600: noet sw=4 ts=4 fdm=marker
225  * vim<600: noet sw=4 ts=4
226  */
227