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