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