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