1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2013 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, long protolen,
30 		char *resourcename, long resourcenamelen,
31 		const char *persistent_id, int options, int flags,
32 		struct timeval *timeout,
33 		php_stream_context *context STREAMS_DC TSRMLS_DC);
34 typedef php_stream_transport_factory_func *php_stream_transport_factory;
35 
36 BEGIN_EXTERN_C()
37 PHPAPI int php_stream_xport_register(char *protocol, php_stream_transport_factory factory TSRMLS_DC);
38 PHPAPI int php_stream_xport_unregister(char *protocol TSRMLS_DC);
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, long namelen, int options,
50 		int flags, const char *persistent_id,
51 		struct timeval *timeout,
52 		php_stream_context *context,
53 		char **error_string,
54 		int *error_code
55 		STREAMS_DC TSRMLS_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 TSRMLS_CC)
59 
60 /* Bind the stream to a local address */
61 PHPAPI int php_stream_xport_bind(php_stream *stream,
62 		const char *name, long namelen,
63 		char **error_text
64 		TSRMLS_DC);
65 
66 /* Connect to a remote address */
67 PHPAPI int php_stream_xport_connect(php_stream *stream,
68 		const char *name, long namelen,
69 		int asynchronous,
70 		struct timeval *timeout,
71 		char **error_text,
72 		int *error_code
73 		TSRMLS_DC);
74 
75 /* Prepare to listen */
76 PHPAPI int php_stream_xport_listen(php_stream *stream,
77 		int backlog,
78 		char **error_text
79 		TSRMLS_DC);
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 		char **textaddr, int *textaddrlen,
85 		void **addr, socklen_t *addrlen,
86 		struct timeval *timeout,
87 		char **error_text
88 		TSRMLS_DC);
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 		char **textaddr, int *textaddrlen,
93 		void **addr, socklen_t *addrlen
94 		TSRMLS_DC);
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 		long flags, void **addr, socklen_t *addrlen,
105 		char **textaddr, int *textaddrlen TSRMLS_DC);
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 		long flags, void *addr, socklen_t addrlen TSRMLS_DC);
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 TSRMLS_DC);
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 		long namelen;
145 		int backlog;
146 		struct timeval *timeout;
147 		struct sockaddr *addr;
148 		socklen_t addrlen;
149 		char *buf;
150 		size_t buflen;
151 		long flags;
152 	} inputs;
153 	struct {
154 		php_stream *client;
155 		int returncode;
156 		struct sockaddr *addr;
157 		socklen_t addrlen;
158 		char *textaddr;
159 		long textaddrlen;
160 
161 		char *error_text;
162 		int error_code;
163 	} outputs;
164 } php_stream_xport_param;
165 
166 
167 /* These functions provide crypto support on the underlying transport */
168 typedef enum {
169 	STREAM_CRYPTO_METHOD_SSLv2_CLIENT,
170 	STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
171 	STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
172 	STREAM_CRYPTO_METHOD_TLS_CLIENT,
173 	STREAM_CRYPTO_METHOD_SSLv2_SERVER,
174 	STREAM_CRYPTO_METHOD_SSLv3_SERVER,
175 	STREAM_CRYPTO_METHOD_SSLv23_SERVER,
176 	STREAM_CRYPTO_METHOD_TLS_SERVER
177 } php_stream_xport_crypt_method_t;
178 
179 BEGIN_EXTERN_C()
180 PHPAPI int php_stream_xport_crypto_setup(php_stream *stream, php_stream_xport_crypt_method_t crypto_method, php_stream *session_stream TSRMLS_DC);
181 PHPAPI int php_stream_xport_crypto_enable(php_stream *stream, int activate TSRMLS_DC);
182 END_EXTERN_C()
183 
184 typedef struct _php_stream_xport_crypto_param {
185 	enum {
186 		STREAM_XPORT_CRYPTO_OP_SETUP,
187 		STREAM_XPORT_CRYPTO_OP_ENABLE
188 	} op;
189 	struct {
190 		int activate;
191 		php_stream_xport_crypt_method_t method;
192 		php_stream *session;
193 	} inputs;
194 	struct {
195 		int returncode;
196 	} outputs;
197 } php_stream_xport_crypto_param;
198 
199 BEGIN_EXTERN_C()
200 PHPAPI HashTable *php_stream_xport_get_hash(void);
201 PHPAPI php_stream_transport_factory_func php_stream_generic_socket_factory;
202 END_EXTERN_C()
203 
204 /*
205  * Local variables:
206  * tab-width: 4
207  * c-basic-offset: 4
208  * End:
209  * vim600: noet sw=4 ts=4 fdm=marker
210  * vim<600: noet sw=4 ts=4
211  */
212