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