xref: /PHP-5.4/main/php_network.h (revision b30a6d60)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2014 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: Stig Venaas <venaas@uninett.no>                              |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* $Id$ */
20 
21 #ifndef _PHP_NETWORK_H
22 #define _PHP_NETWORK_H
23 
24 #ifdef PHP_WIN32
25 # include "win32/inet.h"
26 #else
27 # undef closesocket
28 # define closesocket close
29 #endif
30 
31 #ifndef HAVE_SHUTDOWN
32 #undef shutdown
33 #define shutdown(s,n)	/* nothing */
34 #endif
35 
36 #ifdef PHP_WIN32
37 # ifdef EWOULDBLOCK
38 #  undef EWOULDBLOCK
39 # endif
40 # ifdef EINPROGRESS
41 #  undef EINPROGRESS
42 # endif
43 # define EWOULDBLOCK WSAEWOULDBLOCK
44 # define EINPROGRESS	WSAEWOULDBLOCK
45 # define fsync _commit
46 # define ftruncate(a, b) chsize(a, b)
47 #endif /* defined(PHP_WIN32) */
48 
49 #ifndef EWOULDBLOCK
50 # define EWOULDBLOCK EAGAIN
51 #endif
52 
53 #ifdef PHP_WIN32
54 #define php_socket_errno() WSAGetLastError()
55 #else
56 #define php_socket_errno() errno
57 #endif
58 
59 /* like strerror, but caller must efree the returned string,
60  * unless buf is not NULL.
61  * Also works sensibly for win32 */
62 BEGIN_EXTERN_C()
63 PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize);
64 END_EXTERN_C()
65 
66 #ifdef HAVE_NETINET_IN_H
67 # include <netinet/in.h>
68 #endif
69 
70 #ifdef HAVE_SYS_SOCKET_H
71 #include <sys/socket.h>
72 #endif
73 
74 /* These are here, rather than with the win32 counterparts above,
75  * since <sys/socket.h> defines them. */
76 #ifndef SHUT_RD
77 # define SHUT_RD 0
78 # define SHUT_WR 1
79 # define SHUT_RDWR 2
80 #endif
81 
82 #ifdef HAVE_SYS_TIME_H
83 #include <sys/time.h>
84 #endif
85 
86 #ifdef HAVE_STDDEF_H
87 #include <stddef.h>
88 #endif
89 
90 #ifdef PHP_WIN32
91 typedef SOCKET php_socket_t;
92 #else
93 typedef int php_socket_t;
94 #endif
95 
96 #ifdef PHP_WIN32
97 # define SOCK_ERR INVALID_SOCKET
98 # define SOCK_CONN_ERR SOCKET_ERROR
99 # define SOCK_RECV_ERR SOCKET_ERROR
100 #else
101 # define SOCK_ERR -1
102 # define SOCK_CONN_ERR -1
103 # define SOCK_RECV_ERR -1
104 #endif
105 
106 /* uncomment this to debug poll(2) emulation on systems that have poll(2) */
107 /* #define PHP_USE_POLL_2_EMULATION 1 */
108 
109 #if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
110 # include <sys/poll.h>
111 typedef struct pollfd php_pollfd;
112 #else
113 typedef struct _php_pollfd {
114 	php_socket_t fd;
115 	short events;
116 	short revents;
117 } php_pollfd;
118 
119 PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout);
120 
121 #ifndef POLLIN
122 # define POLLIN      0x0001    /* There is data to read */
123 # define POLLPRI     0x0002    /* There is urgent data to read */
124 # define POLLOUT     0x0004    /* Writing now will not block */
125 # define POLLERR     0x0008    /* Error condition */
126 # define POLLHUP     0x0010    /* Hung up */
127 # define POLLNVAL    0x0020    /* Invalid request: fd not open */
128 #endif
129 
130 # ifndef PHP_USE_POLL_2_EMULATION
131 #  define PHP_USE_POLL_2_EMULATION 1
132 # endif
133 #endif
134 
135 #define PHP_POLLREADABLE	(POLLIN|POLLERR|POLLHUP)
136 
137 #ifndef PHP_USE_POLL_2_EMULATION
138 # define php_poll2(ufds, nfds, timeout)		poll(ufds, nfds, timeout)
139 #endif
140 
141 /* timeval-to-timeout (for poll(2)) */
php_tvtoto(struct timeval * timeouttv)142 static inline int php_tvtoto(struct timeval *timeouttv)
143 {
144 	if (timeouttv) {
145 		return (timeouttv->tv_sec * 1000) + (timeouttv->tv_usec / 1000);
146 	}
147 	return -1;
148 }
149 
150 /* hybrid select(2)/poll(2) for a single descriptor.
151  * timeouttv follows same rules as select(2), but is reduced to millisecond accuracy.
152  * Returns 0 on timeout, -1 on error, or the event mask (ala poll(2)).
153  */
php_pollfd_for(php_socket_t fd,int events,struct timeval * timeouttv)154 static inline int php_pollfd_for(php_socket_t fd, int events, struct timeval *timeouttv)
155 {
156 	php_pollfd p;
157 	int n;
158 
159 	p.fd = fd;
160 	p.events = events;
161 	p.revents = 0;
162 
163 	n = php_poll2(&p, 1, php_tvtoto(timeouttv));
164 
165 	if (n > 0) {
166 		return p.revents;
167 	}
168 
169 	return n;
170 }
171 
php_pollfd_for_ms(php_socket_t fd,int events,int timeout)172 static inline int php_pollfd_for_ms(php_socket_t fd, int events, int timeout)
173 {
174 	php_pollfd p;
175 	int n;
176 
177 	p.fd = fd;
178 	p.events = events;
179 	p.revents = 0;
180 
181 	n = php_poll2(&p, 1, timeout);
182 
183 	if (n > 0) {
184 		return p.revents;
185 	}
186 
187 	return n;
188 }
189 
190 /* emit warning and suggestion for unsafe select(2) usage */
191 PHPAPI void _php_emit_fd_setsize_warning(int max_fd);
192 
193 #ifdef PHP_WIN32
194 /* it is safe to FD_SET too many fd's under win32; the macro will simply ignore
195  * descriptors that go beyond the default FD_SETSIZE */
196 # define PHP_SAFE_FD_SET(fd, set)	FD_SET(fd, set)
197 # define PHP_SAFE_FD_CLR(fd, set)	FD_CLR(fd, set)
198 # define PHP_SAFE_FD_ISSET(fd, set)	FD_ISSET(fd, set)
199 # define PHP_SAFE_MAX_FD(m, n)		do { if (n + 1 >= FD_SETSIZE) { _php_emit_fd_setsize_warning(n); }} while(0)
200 #else
201 # define PHP_SAFE_FD_SET(fd, set)	do { if (fd < FD_SETSIZE) FD_SET(fd, set); } while(0)
202 # define PHP_SAFE_FD_CLR(fd, set)	do { if (fd < FD_SETSIZE) FD_CLR(fd, set); } while(0)
203 # define PHP_SAFE_FD_ISSET(fd, set)	((fd < FD_SETSIZE) && FD_ISSET(fd, set))
204 # define PHP_SAFE_MAX_FD(m, n)		do { if (m >= FD_SETSIZE) { _php_emit_fd_setsize_warning(m); m = FD_SETSIZE - 1; }} while(0)
205 #endif
206 
207 
208 #define PHP_SOCK_CHUNK_SIZE	8192
209 
210 #ifdef HAVE_SOCKADDR_STORAGE
211 typedef struct sockaddr_storage php_sockaddr_storage;
212 #else
213 typedef struct {
214 #ifdef HAVE_SOCKADDR_SA_LEN
215 		unsigned char ss_len;
216 		unsigned char ss_family;
217 #else
218         unsigned short ss_family;
219 #endif
220         char info[126];
221 } php_sockaddr_storage;
222 #endif
223 
224 BEGIN_EXTERN_C()
225 PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, char **error_string TSRMLS_DC);
226 PHPAPI void php_network_freeaddresses(struct sockaddr **sal);
227 
228 PHPAPI php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
229 		int socktype, int asynchronous, struct timeval *timeout, char **error_string,
230 		int *error_code, char *bindto, unsigned short bindport
231 		TSRMLS_DC);
232 
233 PHPAPI int php_network_connect_socket(php_socket_t sockfd,
234 		const struct sockaddr *addr,
235 		socklen_t addrlen,
236 		int asynchronous,
237 		struct timeval *timeout,
238 		char **error_string,
239 		int *error_code);
240 
241 #define php_connect_nonb(sock, addr, addrlen, timeout) \
242 	php_network_connect_socket((sock), (addr), (addrlen), 0, (timeout), NULL, NULL)
243 
244 PHPAPI php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port,
245 		int socktype, char **error_string, int *error_code
246 		TSRMLS_DC);
247 
248 PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock,
249 		char **textaddr, long *textaddrlen,
250 		struct sockaddr **addr,
251 		socklen_t *addrlen,
252 		struct timeval *timeout,
253 		char **error_string,
254 		int *error_code
255 		TSRMLS_DC);
256 
257 PHPAPI int php_network_get_sock_name(php_socket_t sock,
258 		char **textaddr, long *textaddrlen,
259 		struct sockaddr **addr,
260 		socklen_t *addrlen
261 		TSRMLS_DC);
262 
263 PHPAPI int php_network_get_peer_name(php_socket_t sock,
264 		char **textaddr, long *textaddrlen,
265 		struct sockaddr **addr,
266 		socklen_t *addrlen
267 		TSRMLS_DC);
268 
269 PHPAPI void php_any_addr(int family, php_sockaddr_storage *addr, unsigned short port);
270 PHPAPI int php_sockaddr_size(php_sockaddr_storage *addr);
271 END_EXTERN_C()
272 
273 struct _php_netstream_data_t	{
274 	php_socket_t socket;
275 	char is_blocked;
276 	struct timeval timeout;
277 	char timeout_event;
278 	size_t ownsize;
279 };
280 typedef struct _php_netstream_data_t php_netstream_data_t;
281 PHPAPI extern php_stream_ops php_stream_socket_ops;
282 extern php_stream_ops php_stream_generic_socket_ops;
283 #define PHP_STREAM_IS_SOCKET	(&php_stream_socket_ops)
284 
285 BEGIN_EXTERN_C()
286 PHPAPI php_stream *_php_stream_sock_open_from_socket(php_socket_t socket, const char *persistent_id STREAMS_DC TSRMLS_DC );
287 /* open a connection to a host using php_hostconnect and return a stream */
288 PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port,
289 		int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC TSRMLS_DC);
290 PHPAPI void php_network_populate_name_from_sockaddr(
291 		/* input address */
292 		struct sockaddr *sa, socklen_t sl,
293 		/* output readable address */
294 		char **textaddr, long *textaddrlen,
295 		/* output address */
296 		struct sockaddr **addr,
297 		socklen_t *addrlen
298 		TSRMLS_DC);
299 
300 PHPAPI int php_network_parse_network_address_with_port(const char *addr,
301 		long addrlen, struct sockaddr *sa, socklen_t *sl TSRMLS_DC);
302 END_EXTERN_C()
303 
304 #define php_stream_sock_open_from_socket(socket, persistent)	_php_stream_sock_open_from_socket((socket), (persistent) STREAMS_CC TSRMLS_CC)
305 #define php_stream_sock_open_host(host, port, socktype, timeout, persistent)	_php_stream_sock_open_host((host), (port), (socktype), (timeout), (persistent) STREAMS_CC TSRMLS_CC)
306 
307 /* {{{ memory debug */
308 #define php_stream_sock_open_from_socket_rel(socket, persistent)	_php_stream_sock_open_from_socket((socket), (persistent) STREAMS_REL_CC TSRMLS_CC)
309 #define php_stream_sock_open_host_rel(host, port, socktype, timeout, persistent)	_php_stream_sock_open_host((host), (port), (socktype), (timeout), (persistent) STREAMS_REL_CC TSRMLS_CC)
310 #define php_stream_sock_open_unix_rel(path, pathlen, persistent, timeval)	_php_stream_sock_open_unix((path), (pathlen), (persistent), (timeval) STREAMS_REL_CC TSRMLS_CC)
311 
312 /* }}} */
313 
314 #ifndef MAXFQDNLEN
315 #define MAXFQDNLEN 255
316 #endif
317 
318 #endif /* _PHP_NETWORK_H */
319 
320 /*
321  * Local variables:
322  * tab-width: 8
323  * c-basic-offset: 8
324  * End:
325  */
326