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