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