/src/php-src/main/php_network.h
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Author: Stig Venaas <venaas@uninett.no> | |
12 | | +----------------------------------------------------------------------+ |
13 | | */ |
14 | | |
15 | | #ifndef _PHP_NETWORK_H |
16 | | #define _PHP_NETWORK_H |
17 | | |
18 | | #include <php.h> |
19 | | |
20 | | #ifndef PHP_WIN32 |
21 | | # undef closesocket |
22 | 0 | # define closesocket close |
23 | | # include <netinet/tcp.h> |
24 | | #endif |
25 | | |
26 | | #ifndef HAVE_SHUTDOWN |
27 | | #undef shutdown |
28 | | #define shutdown(s,n) /* nothing */ |
29 | | #endif |
30 | | |
31 | | #ifdef PHP_WIN32 |
32 | | # ifdef EWOULDBLOCK |
33 | | # undef EWOULDBLOCK |
34 | | # endif |
35 | | # ifdef EINPROGRESS |
36 | | # undef EINPROGRESS |
37 | | # endif |
38 | | # define EWOULDBLOCK WSAEWOULDBLOCK |
39 | | # define EINPROGRESS WSAEWOULDBLOCK |
40 | | # define fsync _commit |
41 | | # define ftruncate(a, b) chsize(a, b) |
42 | | #endif /* defined(PHP_WIN32) */ |
43 | | |
44 | | #ifndef EWOULDBLOCK |
45 | | # define EWOULDBLOCK EAGAIN |
46 | | #endif |
47 | | |
48 | | /* This is a workaround for GCC bug 69602: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69602 */ |
49 | | #if EAGAIN != EWOULDBLOCK |
50 | | # define PHP_IS_TRANSIENT_ERROR(err) (err == EAGAIN || err == EWOULDBLOCK) |
51 | | #else |
52 | 0 | # define PHP_IS_TRANSIENT_ERROR(err) (err == EAGAIN) |
53 | | #endif |
54 | | |
55 | | #ifdef PHP_WIN32 |
56 | | #define php_socket_errno() WSAGetLastError() |
57 | | #else |
58 | 0 | #define php_socket_errno() errno |
59 | | #endif |
60 | | |
61 | | /* like strerror, but caller must efree the returned string, |
62 | | * unless buf is not NULL. |
63 | | * Also works sensibly for win32 */ |
64 | | BEGIN_EXTERN_C() |
65 | | #ifdef PHP_WIN32 |
66 | | char *php_socket_strerror_s(long err, char *buf, size_t bufsize); |
67 | | #else |
68 | 289 | #define php_socket_strerror_s php_socket_strerror |
69 | | #endif |
70 | | PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize); |
71 | | PHPAPI zend_string *php_socket_error_str(long err); |
72 | | END_EXTERN_C() |
73 | | |
74 | | #ifdef HAVE_NETINET_IN_H |
75 | | # include <netinet/in.h> |
76 | | #endif |
77 | | |
78 | | #ifdef HAVE_SYS_SOCKET_H |
79 | | #include <sys/socket.h> |
80 | | #endif |
81 | | |
82 | | #ifdef HAVE_GETHOSTBYNAME_R |
83 | | #include <netdb.h> |
84 | | #endif |
85 | | |
86 | | /* These are here, rather than with the win32 counterparts above, |
87 | | * since <sys/socket.h> defines them. */ |
88 | | #ifndef SHUT_RD |
89 | | # define SHUT_RD 0 |
90 | | # define SHUT_WR 1 |
91 | | # define SHUT_RDWR 2 |
92 | | #endif |
93 | | |
94 | | #ifdef HAVE_SYS_TIME_H |
95 | | #include <sys/time.h> |
96 | | #endif |
97 | | |
98 | | #include <stddef.h> |
99 | | |
100 | | #ifdef PHP_WIN32 |
101 | | typedef SOCKET php_socket_t; |
102 | | #define PHP_SOCKET_FMT "%" PRIxPTR |
103 | | #else |
104 | | typedef int php_socket_t; |
105 | | #define PHP_SOCKET_FMT "%d" |
106 | | #endif |
107 | | |
108 | | #ifdef PHP_WIN32 |
109 | | # define SOCK_ERR INVALID_SOCKET |
110 | | # define SOCK_CONN_ERR SOCKET_ERROR |
111 | | # define SOCK_RECV_ERR SOCKET_ERROR |
112 | | #else |
113 | 0 | # define SOCK_ERR -1 |
114 | 0 | # define SOCK_CONN_ERR -1 |
115 | | # define SOCK_RECV_ERR -1 |
116 | | #endif |
117 | | |
118 | 0 | #define STREAM_SOCKOP_NONE (1 << 0) |
119 | 0 | #define STREAM_SOCKOP_SO_REUSEPORT (1 << 1) |
120 | 0 | #define STREAM_SOCKOP_SO_BROADCAST (1 << 2) |
121 | 0 | #define STREAM_SOCKOP_IPV6_V6ONLY (1 << 3) |
122 | 0 | #define STREAM_SOCKOP_IPV6_V6ONLY_ENABLED (1 << 4) |
123 | 0 | #define STREAM_SOCKOP_TCP_NODELAY (1 << 5) |
124 | 0 | #define STREAM_SOCKOP_SO_REUSEADDR (1 << 6) |
125 | 0 | #define STREAM_SOCKOP_SO_KEEPALIVE (1 << 7) |
126 | | |
127 | | /* uncomment this to debug poll(2) emulation on systems that have poll(2) */ |
128 | | /* #define PHP_USE_POLL_2_EMULATION 1 */ |
129 | | |
130 | | #if defined(HAVE_POLL) |
131 | | # if defined(HAVE_POLL_H) |
132 | | # include <poll.h> |
133 | | # elif defined(HAVE_SYS_POLL_H) |
134 | | # include <sys/poll.h> |
135 | | # endif |
136 | | typedef struct pollfd php_pollfd; |
137 | | #else |
138 | | typedef struct _php_pollfd { |
139 | | php_socket_t fd; |
140 | | short events; |
141 | | short revents; |
142 | | } php_pollfd; |
143 | | |
144 | | PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout); |
145 | | |
146 | | #ifndef POLLIN |
147 | | # define POLLIN 0x0001 /* There is data to read */ |
148 | | # define POLLPRI 0x0002 /* There is urgent data to read */ |
149 | | # define POLLOUT 0x0004 /* Writing now will not block */ |
150 | | # define POLLERR 0x0008 /* Error condition */ |
151 | | # define POLLHUP 0x0010 /* Hung up */ |
152 | | # define POLLNVAL 0x0020 /* Invalid request: fd not open */ |
153 | | #endif |
154 | | |
155 | | # ifndef PHP_USE_POLL_2_EMULATION |
156 | | # define PHP_USE_POLL_2_EMULATION 1 |
157 | | # endif |
158 | | #endif |
159 | | |
160 | 0 | #define PHP_POLLREADABLE (POLLIN|POLLERR|POLLHUP) |
161 | | |
162 | | #ifndef PHP_USE_POLL_2_EMULATION |
163 | 0 | # define php_poll2(ufds, nfds, timeout) poll(ufds, nfds, timeout) |
164 | | #endif |
165 | | |
166 | | /* timeval-to-timeout (for poll(2)) */ |
167 | | static inline int php_tvtoto(struct timeval *timeouttv) |
168 | 0 | { |
169 | 0 | if (timeouttv && timeouttv->tv_sec >= 0 && timeouttv->tv_sec <= ((INT_MAX - 1000) / 1000)) { |
170 | 0 | return (timeouttv->tv_sec * 1000) + (timeouttv->tv_usec / 1000); |
171 | 0 | } |
172 | 0 | return -1; |
173 | 0 | } Unexecuted instantiation: exif.c:php_tvtoto Unexecuted instantiation: hash.c:php_tvtoto Unexecuted instantiation: spl_directory.c:php_tvtoto Unexecuted instantiation: basic_functions.c:php_tvtoto Unexecuted instantiation: dir.c:php_tvtoto Unexecuted instantiation: dns.c:php_tvtoto Unexecuted instantiation: exec.c:php_tvtoto Unexecuted instantiation: file.c:php_tvtoto Unexecuted instantiation: filters.c:php_tvtoto Unexecuted instantiation: fsock.c:php_tvtoto Unexecuted instantiation: ftp_fopen_wrapper.c:php_tvtoto Unexecuted instantiation: head.c:php_tvtoto Unexecuted instantiation: html.c:php_tvtoto Unexecuted instantiation: http_fopen_wrapper.c:php_tvtoto Unexecuted instantiation: image.c:php_tvtoto Unexecuted instantiation: io_poll.c:php_tvtoto Unexecuted instantiation: net.c:php_tvtoto Unexecuted instantiation: php_fopen_wrapper.c:php_tvtoto Unexecuted instantiation: proc_open.c:php_tvtoto Unexecuted instantiation: streamsfuncs.c:php_tvtoto Unexecuted instantiation: string.c:php_tvtoto Unexecuted instantiation: url.c:php_tvtoto Unexecuted instantiation: user_filters.c:php_tvtoto Unexecuted instantiation: fopen_wrappers.c:php_tvtoto Unexecuted instantiation: main.c:php_tvtoto Unexecuted instantiation: network.c:php_tvtoto Unexecuted instantiation: php_variables.c:php_tvtoto Unexecuted instantiation: php_io.c:php_tvtoto Unexecuted instantiation: php_io_copy_linux.c:php_tvtoto Unexecuted instantiation: poll_backend_epoll.c:php_tvtoto Unexecuted instantiation: poll_backend_eventport.c:php_tvtoto Unexecuted instantiation: poll_backend_kqueue.c:php_tvtoto Unexecuted instantiation: poll_backend_poll.c:php_tvtoto Unexecuted instantiation: poll_core.c:php_tvtoto Unexecuted instantiation: poll_fd_table.c:php_tvtoto Unexecuted instantiation: poll_handle.c:php_tvtoto Unexecuted instantiation: cast.c:php_tvtoto Unexecuted instantiation: filter.c:php_tvtoto Unexecuted instantiation: plain_wrapper.c:php_tvtoto Unexecuted instantiation: stream_errors.c:php_tvtoto Unexecuted instantiation: streams.c:php_tvtoto Unexecuted instantiation: transports.c:php_tvtoto Unexecuted instantiation: userspace.c:php_tvtoto Unexecuted instantiation: xp_socket.c:php_tvtoto Unexecuted instantiation: internal_functions_cli.c:php_tvtoto |
174 | | |
175 | | /* hybrid select(2)/poll(2) for a single descriptor. |
176 | | * timeouttv follows same rules as select(2), but is reduced to millisecond accuracy. |
177 | | * Returns 0 on timeout, -1 on error, or the event mask (ala poll(2)). |
178 | | */ |
179 | | static inline int php_pollfd_for(php_socket_t fd, int events, struct timeval *timeouttv) |
180 | 0 | { |
181 | 0 | php_pollfd p; |
182 | 0 | int n; |
183 | |
|
184 | 0 | p.fd = fd; |
185 | 0 | p.events = events; |
186 | 0 | p.revents = 0; |
187 | |
|
188 | 0 | n = php_poll2(&p, 1, php_tvtoto(timeouttv)); |
189 | |
|
190 | 0 | if (n > 0) { |
191 | 0 | return p.revents; |
192 | 0 | } |
193 | | |
194 | 0 | return n; |
195 | 0 | } Unexecuted instantiation: exif.c:php_pollfd_for Unexecuted instantiation: hash.c:php_pollfd_for Unexecuted instantiation: spl_directory.c:php_pollfd_for Unexecuted instantiation: basic_functions.c:php_pollfd_for Unexecuted instantiation: dir.c:php_pollfd_for Unexecuted instantiation: dns.c:php_pollfd_for Unexecuted instantiation: exec.c:php_pollfd_for Unexecuted instantiation: file.c:php_pollfd_for Unexecuted instantiation: filters.c:php_pollfd_for Unexecuted instantiation: fsock.c:php_pollfd_for Unexecuted instantiation: ftp_fopen_wrapper.c:php_pollfd_for Unexecuted instantiation: head.c:php_pollfd_for Unexecuted instantiation: html.c:php_pollfd_for Unexecuted instantiation: http_fopen_wrapper.c:php_pollfd_for Unexecuted instantiation: image.c:php_pollfd_for Unexecuted instantiation: io_poll.c:php_pollfd_for Unexecuted instantiation: net.c:php_pollfd_for Unexecuted instantiation: php_fopen_wrapper.c:php_pollfd_for Unexecuted instantiation: proc_open.c:php_pollfd_for Unexecuted instantiation: streamsfuncs.c:php_pollfd_for Unexecuted instantiation: string.c:php_pollfd_for Unexecuted instantiation: url.c:php_pollfd_for Unexecuted instantiation: user_filters.c:php_pollfd_for Unexecuted instantiation: fopen_wrappers.c:php_pollfd_for Unexecuted instantiation: main.c:php_pollfd_for Unexecuted instantiation: network.c:php_pollfd_for Unexecuted instantiation: php_variables.c:php_pollfd_for Unexecuted instantiation: php_io.c:php_pollfd_for Unexecuted instantiation: php_io_copy_linux.c:php_pollfd_for Unexecuted instantiation: poll_backend_epoll.c:php_pollfd_for Unexecuted instantiation: poll_backend_eventport.c:php_pollfd_for Unexecuted instantiation: poll_backend_kqueue.c:php_pollfd_for Unexecuted instantiation: poll_backend_poll.c:php_pollfd_for Unexecuted instantiation: poll_core.c:php_pollfd_for Unexecuted instantiation: poll_fd_table.c:php_pollfd_for Unexecuted instantiation: poll_handle.c:php_pollfd_for Unexecuted instantiation: cast.c:php_pollfd_for Unexecuted instantiation: filter.c:php_pollfd_for Unexecuted instantiation: plain_wrapper.c:php_pollfd_for Unexecuted instantiation: stream_errors.c:php_pollfd_for Unexecuted instantiation: streams.c:php_pollfd_for Unexecuted instantiation: transports.c:php_pollfd_for Unexecuted instantiation: userspace.c:php_pollfd_for Unexecuted instantiation: xp_socket.c:php_pollfd_for Unexecuted instantiation: internal_functions_cli.c:php_pollfd_for |
196 | | |
197 | | static inline int php_pollfd_for_ms(php_socket_t fd, int events, int timeout) |
198 | 0 | { |
199 | 0 | php_pollfd p; |
200 | 0 | int n; |
201 | |
|
202 | 0 | p.fd = fd; |
203 | 0 | p.events = events; |
204 | 0 | p.revents = 0; |
205 | |
|
206 | 0 | n = php_poll2(&p, 1, timeout); |
207 | |
|
208 | 0 | if (n > 0) { |
209 | 0 | return p.revents; |
210 | 0 | } |
211 | | |
212 | 0 | return n; |
213 | 0 | } Unexecuted instantiation: exif.c:php_pollfd_for_ms Unexecuted instantiation: hash.c:php_pollfd_for_ms Unexecuted instantiation: spl_directory.c:php_pollfd_for_ms Unexecuted instantiation: basic_functions.c:php_pollfd_for_ms Unexecuted instantiation: dir.c:php_pollfd_for_ms Unexecuted instantiation: dns.c:php_pollfd_for_ms Unexecuted instantiation: exec.c:php_pollfd_for_ms Unexecuted instantiation: file.c:php_pollfd_for_ms Unexecuted instantiation: filters.c:php_pollfd_for_ms Unexecuted instantiation: fsock.c:php_pollfd_for_ms Unexecuted instantiation: ftp_fopen_wrapper.c:php_pollfd_for_ms Unexecuted instantiation: head.c:php_pollfd_for_ms Unexecuted instantiation: html.c:php_pollfd_for_ms Unexecuted instantiation: http_fopen_wrapper.c:php_pollfd_for_ms Unexecuted instantiation: image.c:php_pollfd_for_ms Unexecuted instantiation: io_poll.c:php_pollfd_for_ms Unexecuted instantiation: net.c:php_pollfd_for_ms Unexecuted instantiation: php_fopen_wrapper.c:php_pollfd_for_ms Unexecuted instantiation: proc_open.c:php_pollfd_for_ms Unexecuted instantiation: streamsfuncs.c:php_pollfd_for_ms Unexecuted instantiation: string.c:php_pollfd_for_ms Unexecuted instantiation: url.c:php_pollfd_for_ms Unexecuted instantiation: user_filters.c:php_pollfd_for_ms Unexecuted instantiation: fopen_wrappers.c:php_pollfd_for_ms Unexecuted instantiation: main.c:php_pollfd_for_ms Unexecuted instantiation: network.c:php_pollfd_for_ms Unexecuted instantiation: php_variables.c:php_pollfd_for_ms Unexecuted instantiation: php_io.c:php_pollfd_for_ms Unexecuted instantiation: php_io_copy_linux.c:php_pollfd_for_ms Unexecuted instantiation: poll_backend_epoll.c:php_pollfd_for_ms Unexecuted instantiation: poll_backend_eventport.c:php_pollfd_for_ms Unexecuted instantiation: poll_backend_kqueue.c:php_pollfd_for_ms Unexecuted instantiation: poll_backend_poll.c:php_pollfd_for_ms Unexecuted instantiation: poll_core.c:php_pollfd_for_ms Unexecuted instantiation: poll_fd_table.c:php_pollfd_for_ms Unexecuted instantiation: poll_handle.c:php_pollfd_for_ms Unexecuted instantiation: cast.c:php_pollfd_for_ms Unexecuted instantiation: filter.c:php_pollfd_for_ms Unexecuted instantiation: plain_wrapper.c:php_pollfd_for_ms Unexecuted instantiation: stream_errors.c:php_pollfd_for_ms Unexecuted instantiation: streams.c:php_pollfd_for_ms Unexecuted instantiation: transports.c:php_pollfd_for_ms Unexecuted instantiation: userspace.c:php_pollfd_for_ms Unexecuted instantiation: xp_socket.c:php_pollfd_for_ms Unexecuted instantiation: internal_functions_cli.c:php_pollfd_for_ms |
214 | | |
215 | | /* emit warning and suggestion for unsafe select(2) usage */ |
216 | | PHPAPI void _php_emit_fd_setsize_warning(int max_fd); |
217 | | |
218 | | static inline bool _php_check_fd_setsize(php_socket_t *max_fd, int setsize) |
219 | 0 | { |
220 | 0 | #ifdef PHP_WIN32 |
221 | 0 | (void)(max_fd); // Unused |
222 | 0 | if (setsize + 1 >= FD_SETSIZE) { |
223 | 0 | _php_emit_fd_setsize_warning(setsize); |
224 | 0 | return false; |
225 | 0 | } |
226 | 0 | #else |
227 | 0 | (void)(setsize); // Unused |
228 | 0 | if (*max_fd >= FD_SETSIZE) { |
229 | 0 | _php_emit_fd_setsize_warning(*max_fd); |
230 | 0 | *max_fd = FD_SETSIZE - 1; |
231 | 0 | return false; |
232 | 0 | } |
233 | 0 | #endif |
234 | 0 | return true; |
235 | 0 | } Unexecuted instantiation: exif.c:_php_check_fd_setsize Unexecuted instantiation: hash.c:_php_check_fd_setsize Unexecuted instantiation: spl_directory.c:_php_check_fd_setsize Unexecuted instantiation: basic_functions.c:_php_check_fd_setsize Unexecuted instantiation: dir.c:_php_check_fd_setsize Unexecuted instantiation: dns.c:_php_check_fd_setsize Unexecuted instantiation: exec.c:_php_check_fd_setsize Unexecuted instantiation: file.c:_php_check_fd_setsize Unexecuted instantiation: filters.c:_php_check_fd_setsize Unexecuted instantiation: fsock.c:_php_check_fd_setsize Unexecuted instantiation: ftp_fopen_wrapper.c:_php_check_fd_setsize Unexecuted instantiation: head.c:_php_check_fd_setsize Unexecuted instantiation: html.c:_php_check_fd_setsize Unexecuted instantiation: http_fopen_wrapper.c:_php_check_fd_setsize Unexecuted instantiation: image.c:_php_check_fd_setsize Unexecuted instantiation: io_poll.c:_php_check_fd_setsize Unexecuted instantiation: net.c:_php_check_fd_setsize Unexecuted instantiation: php_fopen_wrapper.c:_php_check_fd_setsize Unexecuted instantiation: proc_open.c:_php_check_fd_setsize Unexecuted instantiation: streamsfuncs.c:_php_check_fd_setsize Unexecuted instantiation: string.c:_php_check_fd_setsize Unexecuted instantiation: url.c:_php_check_fd_setsize Unexecuted instantiation: user_filters.c:_php_check_fd_setsize Unexecuted instantiation: fopen_wrappers.c:_php_check_fd_setsize Unexecuted instantiation: main.c:_php_check_fd_setsize Unexecuted instantiation: network.c:_php_check_fd_setsize Unexecuted instantiation: php_variables.c:_php_check_fd_setsize Unexecuted instantiation: php_io.c:_php_check_fd_setsize Unexecuted instantiation: php_io_copy_linux.c:_php_check_fd_setsize Unexecuted instantiation: poll_backend_epoll.c:_php_check_fd_setsize Unexecuted instantiation: poll_backend_eventport.c:_php_check_fd_setsize Unexecuted instantiation: poll_backend_kqueue.c:_php_check_fd_setsize Unexecuted instantiation: poll_backend_poll.c:_php_check_fd_setsize Unexecuted instantiation: poll_core.c:_php_check_fd_setsize Unexecuted instantiation: poll_fd_table.c:_php_check_fd_setsize Unexecuted instantiation: poll_handle.c:_php_check_fd_setsize Unexecuted instantiation: cast.c:_php_check_fd_setsize Unexecuted instantiation: filter.c:_php_check_fd_setsize Unexecuted instantiation: plain_wrapper.c:_php_check_fd_setsize Unexecuted instantiation: stream_errors.c:_php_check_fd_setsize Unexecuted instantiation: streams.c:_php_check_fd_setsize Unexecuted instantiation: transports.c:_php_check_fd_setsize Unexecuted instantiation: userspace.c:_php_check_fd_setsize Unexecuted instantiation: xp_socket.c:_php_check_fd_setsize Unexecuted instantiation: internal_functions_cli.c:_php_check_fd_setsize |
236 | | |
237 | | #ifdef PHP_WIN32 |
238 | | /* it is safe to FD_SET too many fd's under win32; the macro will simply ignore |
239 | | * descriptors that go beyond the default FD_SETSIZE */ |
240 | | # define PHP_SAFE_FD_SET(fd, set) FD_SET(fd, set) |
241 | | # define PHP_SAFE_FD_CLR(fd, set) FD_CLR(fd, set) |
242 | | # define PHP_SAFE_FD_ISSET(fd, set) FD_ISSET(fd, set) |
243 | | # define PHP_SAFE_MAX_FD(m, n) _php_check_fd_setsize(&m, n) |
244 | | #else |
245 | | # define PHP_SAFE_FD_SET(fd, set) do { if (fd < FD_SETSIZE) FD_SET(fd, set); } while(0) |
246 | | # define PHP_SAFE_FD_CLR(fd, set) do { if (fd < FD_SETSIZE) FD_CLR(fd, set); } while(0) |
247 | | # define PHP_SAFE_FD_ISSET(fd, set) ((fd < FD_SETSIZE) && FD_ISSET(fd, set)) |
248 | | # define PHP_SAFE_MAX_FD(m, n) _php_check_fd_setsize(&m, n) |
249 | | #endif |
250 | | |
251 | | /* A growable fd_set for stream_select(), without the FD_SETSIZE limit. |
252 | | * |
253 | | * On POSIX, fd_set is a bitset indexed by descriptor, and select() takes any nfds: the set |
254 | | * is a bitset that grows to hold the highest descriptor added. |
255 | | * On Windows, fd_set is an array of SOCKETs ({ u_int fd_count; SOCKET fd_array[]; }), and |
256 | | * select() reads fd_count of them: the set is that array, grown to hold every socket added. |
257 | | * Either way, `set` can be passed to select() as it is. */ |
258 | | #ifdef PHP_WIN32 |
259 | | typedef struct { |
260 | | u_int capacity; /* SOCKET slots in set->fd_array */ |
261 | | fd_set *set; |
262 | | } php_growable_fd_set; |
263 | | |
264 | | # define PHP_GROWABLE_FD_SET_ALLOC_SIZE(cap) \ |
265 | | (offsetof(fd_set, fd_array) + (size_t)(cap) * sizeof(SOCKET)) |
266 | | |
267 | | static zend_always_inline void php_growable_fd_set_init(php_growable_fd_set *s, u_int capacity) |
268 | | { |
269 | | s->capacity = MAX(capacity, FD_SETSIZE); |
270 | | s->set = (fd_set *) pemalloc(PHP_GROWABLE_FD_SET_ALLOC_SIZE(s->capacity), 1); |
271 | | s->set->fd_count = 0; |
272 | | } |
273 | | |
274 | | /* Makes room for capacity sockets */ |
275 | | static zend_always_inline void php_growable_fd_set_reserve(php_growable_fd_set *s, u_int capacity) |
276 | | { |
277 | | if (capacity > s->capacity) { |
278 | | do { |
279 | | s->capacity *= 2; |
280 | | } while (capacity > s->capacity); |
281 | | s->set = (fd_set *) perealloc(s->set, PHP_GROWABLE_FD_SET_ALLOC_SIZE(s->capacity), 1); |
282 | | } |
283 | | } |
284 | | |
285 | | static zend_always_inline void php_growable_fd_set_add(php_growable_fd_set *s, php_socket_t fd) |
286 | | { |
287 | | /* Appends without the duplicate scan of winsock's FD_SET(); the set is |
288 | | * de-duplicated in one pass later by php_growable_fd_set_sort_unique(). */ |
289 | | php_growable_fd_set_reserve(s, s->set->fd_count + 1); |
290 | | s->set->fd_array[s->set->fd_count++] = fd; |
291 | | } |
292 | | |
293 | | static zend_always_inline int php_growable_fd_set_cmp(const void *a, const void *b) |
294 | | { |
295 | | SOCKET x = *(const SOCKET *) a; |
296 | | SOCKET y = *(const SOCKET *) b; |
297 | | return (x > y) - (x < y); |
298 | | } |
299 | | |
300 | | /* Sorts the sockets ascending and drops duplicates. This makes select() count a |
301 | | * socket listed more than once (e.g. stream_select([$a, $a], ...)) a single time |
302 | | * - as the POSIX bitset does - and lets php_growable_fd_set_isset() binary-search |
303 | | * instead of scanning, so a whole stream_select() call is O(n log n), not O(n^2). */ |
304 | | static zend_always_inline void php_growable_fd_set_sort_unique(php_growable_fd_set *s) |
305 | | { |
306 | | u_int n = s->set->fd_count; |
307 | | if (n < 2) { |
308 | | return; |
309 | | } |
310 | | qsort(s->set->fd_array, n, sizeof(SOCKET), php_growable_fd_set_cmp); |
311 | | u_int w = 1; |
312 | | for (u_int r = 1; r < n; r++) { |
313 | | if (s->set->fd_array[r] != s->set->fd_array[w - 1]) { |
314 | | s->set->fd_array[w++] = s->set->fd_array[r]; |
315 | | } |
316 | | } |
317 | | s->set->fd_count = w; |
318 | | } |
319 | | |
320 | | /* Binary search; the set must have been sorted by php_growable_fd_set_sort_unique(). */ |
321 | | static zend_always_inline bool php_growable_fd_set_isset(const php_growable_fd_set *s, php_socket_t fd) |
322 | | { |
323 | | SOCKET target = (SOCKET) fd; |
324 | | u_int lo = 0, hi = s->set->fd_count; |
325 | | while (lo < hi) { |
326 | | u_int mid = lo + (hi - lo) / 2; |
327 | | SOCKET v = s->set->fd_array[mid]; |
328 | | if (v < target) { |
329 | | lo = mid + 1; |
330 | | } else if (v > target) { |
331 | | hi = mid; |
332 | | } else { |
333 | | return true; |
334 | | } |
335 | | } |
336 | | return false; |
337 | | } |
338 | | |
339 | | static zend_always_inline void php_growable_fd_set_zero(php_growable_fd_set *s) |
340 | | { |
341 | | s->set->fd_count = 0; |
342 | | } |
343 | | |
344 | | static zend_always_inline void php_growable_fd_set_copy(php_growable_fd_set *dst, const php_growable_fd_set *src) |
345 | | { |
346 | | php_growable_fd_set_reserve(dst, src->set->fd_count); |
347 | | memcpy(dst->set, src->set, PHP_GROWABLE_FD_SET_ALLOC_SIZE(src->set->fd_count)); |
348 | | } |
349 | | |
350 | | static zend_always_inline void php_growable_fd_set_destroy(php_growable_fd_set *s) |
351 | | { |
352 | | if (s->set) { |
353 | | pefree(s->set, 1); |
354 | | s->set = NULL; |
355 | | } |
356 | | s->capacity = 0; |
357 | | } |
358 | | #else |
359 | | typedef struct { |
360 | | size_t size; /* bytes in set */ |
361 | | fd_set *set; |
362 | | } php_growable_fd_set; |
363 | | |
364 | | /* Use the platform's fd_mask: on big-endian systems the bit positions depend on the word size */ |
365 | | # ifdef NFDBITS |
366 | | typedef fd_mask php_growable_fd_mask; |
367 | 0 | # define PHP_GROWABLE_FD_SET_WORD_BITS NFDBITS |
368 | | # else |
369 | | typedef unsigned long php_growable_fd_mask; |
370 | | # define PHP_GROWABLE_FD_SET_WORD_BITS (CHAR_BIT * sizeof(unsigned long)) |
371 | | # endif |
372 | | /* select() operates on whole words so the size must be a multiple of sizeof(php_growable_fd_mask) */ |
373 | 0 | # define PHP_GROWABLE_FD_SET_SIZE(nfds) (ZEND_MM_ALIGNED_SIZE_EX((size_t)(nfds), PHP_GROWABLE_FD_SET_WORD_BITS) / CHAR_BIT) |
374 | | |
375 | | static zend_always_inline void php_growable_fd_set_init(php_growable_fd_set *s, unsigned int capacity) |
376 | 0 | { |
377 | 0 | s->size = PHP_GROWABLE_FD_SET_SIZE(capacity); |
378 | 0 | s->set = (fd_set *) ecalloc(1, s->size); |
379 | 0 | } Unexecuted instantiation: exif.c:php_growable_fd_set_init Unexecuted instantiation: hash.c:php_growable_fd_set_init Unexecuted instantiation: spl_directory.c:php_growable_fd_set_init Unexecuted instantiation: basic_functions.c:php_growable_fd_set_init Unexecuted instantiation: dir.c:php_growable_fd_set_init Unexecuted instantiation: dns.c:php_growable_fd_set_init Unexecuted instantiation: exec.c:php_growable_fd_set_init Unexecuted instantiation: file.c:php_growable_fd_set_init Unexecuted instantiation: filters.c:php_growable_fd_set_init Unexecuted instantiation: fsock.c:php_growable_fd_set_init Unexecuted instantiation: ftp_fopen_wrapper.c:php_growable_fd_set_init Unexecuted instantiation: head.c:php_growable_fd_set_init Unexecuted instantiation: html.c:php_growable_fd_set_init Unexecuted instantiation: http_fopen_wrapper.c:php_growable_fd_set_init Unexecuted instantiation: image.c:php_growable_fd_set_init Unexecuted instantiation: io_poll.c:php_growable_fd_set_init Unexecuted instantiation: net.c:php_growable_fd_set_init Unexecuted instantiation: php_fopen_wrapper.c:php_growable_fd_set_init Unexecuted instantiation: proc_open.c:php_growable_fd_set_init Unexecuted instantiation: streamsfuncs.c:php_growable_fd_set_init Unexecuted instantiation: string.c:php_growable_fd_set_init Unexecuted instantiation: url.c:php_growable_fd_set_init Unexecuted instantiation: user_filters.c:php_growable_fd_set_init Unexecuted instantiation: fopen_wrappers.c:php_growable_fd_set_init Unexecuted instantiation: main.c:php_growable_fd_set_init Unexecuted instantiation: network.c:php_growable_fd_set_init Unexecuted instantiation: php_variables.c:php_growable_fd_set_init Unexecuted instantiation: php_io.c:php_growable_fd_set_init Unexecuted instantiation: php_io_copy_linux.c:php_growable_fd_set_init Unexecuted instantiation: poll_backend_epoll.c:php_growable_fd_set_init Unexecuted instantiation: poll_backend_eventport.c:php_growable_fd_set_init Unexecuted instantiation: poll_backend_kqueue.c:php_growable_fd_set_init Unexecuted instantiation: poll_backend_poll.c:php_growable_fd_set_init Unexecuted instantiation: poll_core.c:php_growable_fd_set_init Unexecuted instantiation: poll_fd_table.c:php_growable_fd_set_init Unexecuted instantiation: poll_handle.c:php_growable_fd_set_init Unexecuted instantiation: cast.c:php_growable_fd_set_init Unexecuted instantiation: filter.c:php_growable_fd_set_init Unexecuted instantiation: plain_wrapper.c:php_growable_fd_set_init Unexecuted instantiation: stream_errors.c:php_growable_fd_set_init Unexecuted instantiation: streams.c:php_growable_fd_set_init Unexecuted instantiation: transports.c:php_growable_fd_set_init Unexecuted instantiation: userspace.c:php_growable_fd_set_init Unexecuted instantiation: xp_socket.c:php_growable_fd_set_init Unexecuted instantiation: internal_functions_cli.c:php_growable_fd_set_init |
380 | | |
381 | | /* Makes room for the descriptors below capacity */ |
382 | | static zend_always_inline void php_growable_fd_set_reserve(php_growable_fd_set *s, unsigned int capacity) |
383 | 0 | { |
384 | 0 | size_t size = PHP_GROWABLE_FD_SET_SIZE(capacity); |
385 | 0 | if (size > s->size) { |
386 | 0 | size_t old_size = s->size; |
387 | 0 | s->size = MAX(size, old_size * 2); |
388 | 0 | s->set = (fd_set *) erealloc(s->set, s->size); |
389 | 0 | memset((char *) s->set + old_size, 0, s->size - old_size); |
390 | 0 | } |
391 | 0 | } Unexecuted instantiation: exif.c:php_growable_fd_set_reserve Unexecuted instantiation: hash.c:php_growable_fd_set_reserve Unexecuted instantiation: spl_directory.c:php_growable_fd_set_reserve Unexecuted instantiation: basic_functions.c:php_growable_fd_set_reserve Unexecuted instantiation: dir.c:php_growable_fd_set_reserve Unexecuted instantiation: dns.c:php_growable_fd_set_reserve Unexecuted instantiation: exec.c:php_growable_fd_set_reserve Unexecuted instantiation: file.c:php_growable_fd_set_reserve Unexecuted instantiation: filters.c:php_growable_fd_set_reserve Unexecuted instantiation: fsock.c:php_growable_fd_set_reserve Unexecuted instantiation: ftp_fopen_wrapper.c:php_growable_fd_set_reserve Unexecuted instantiation: head.c:php_growable_fd_set_reserve Unexecuted instantiation: html.c:php_growable_fd_set_reserve Unexecuted instantiation: http_fopen_wrapper.c:php_growable_fd_set_reserve Unexecuted instantiation: image.c:php_growable_fd_set_reserve Unexecuted instantiation: io_poll.c:php_growable_fd_set_reserve Unexecuted instantiation: net.c:php_growable_fd_set_reserve Unexecuted instantiation: php_fopen_wrapper.c:php_growable_fd_set_reserve Unexecuted instantiation: proc_open.c:php_growable_fd_set_reserve Unexecuted instantiation: streamsfuncs.c:php_growable_fd_set_reserve Unexecuted instantiation: string.c:php_growable_fd_set_reserve Unexecuted instantiation: url.c:php_growable_fd_set_reserve Unexecuted instantiation: user_filters.c:php_growable_fd_set_reserve Unexecuted instantiation: fopen_wrappers.c:php_growable_fd_set_reserve Unexecuted instantiation: main.c:php_growable_fd_set_reserve Unexecuted instantiation: network.c:php_growable_fd_set_reserve Unexecuted instantiation: php_variables.c:php_growable_fd_set_reserve Unexecuted instantiation: php_io.c:php_growable_fd_set_reserve Unexecuted instantiation: php_io_copy_linux.c:php_growable_fd_set_reserve Unexecuted instantiation: poll_backend_epoll.c:php_growable_fd_set_reserve Unexecuted instantiation: poll_backend_eventport.c:php_growable_fd_set_reserve Unexecuted instantiation: poll_backend_kqueue.c:php_growable_fd_set_reserve Unexecuted instantiation: poll_backend_poll.c:php_growable_fd_set_reserve Unexecuted instantiation: poll_core.c:php_growable_fd_set_reserve Unexecuted instantiation: poll_fd_table.c:php_growable_fd_set_reserve Unexecuted instantiation: poll_handle.c:php_growable_fd_set_reserve Unexecuted instantiation: cast.c:php_growable_fd_set_reserve Unexecuted instantiation: filter.c:php_growable_fd_set_reserve Unexecuted instantiation: plain_wrapper.c:php_growable_fd_set_reserve Unexecuted instantiation: stream_errors.c:php_growable_fd_set_reserve Unexecuted instantiation: streams.c:php_growable_fd_set_reserve Unexecuted instantiation: transports.c:php_growable_fd_set_reserve Unexecuted instantiation: userspace.c:php_growable_fd_set_reserve Unexecuted instantiation: xp_socket.c:php_growable_fd_set_reserve Unexecuted instantiation: internal_functions_cli.c:php_growable_fd_set_reserve |
392 | | |
393 | | static zend_always_inline void php_growable_fd_set_add(php_growable_fd_set *s, php_socket_t fd) |
394 | 0 | { |
395 | 0 | php_growable_fd_set_reserve(s, fd + 1); |
396 | 0 | ((php_growable_fd_mask *) s->set)[fd / PHP_GROWABLE_FD_SET_WORD_BITS] |= (php_growable_fd_mask) (1UL << (fd % PHP_GROWABLE_FD_SET_WORD_BITS)); |
397 | 0 | } Unexecuted instantiation: exif.c:php_growable_fd_set_add Unexecuted instantiation: hash.c:php_growable_fd_set_add Unexecuted instantiation: spl_directory.c:php_growable_fd_set_add Unexecuted instantiation: basic_functions.c:php_growable_fd_set_add Unexecuted instantiation: dir.c:php_growable_fd_set_add Unexecuted instantiation: dns.c:php_growable_fd_set_add Unexecuted instantiation: exec.c:php_growable_fd_set_add Unexecuted instantiation: file.c:php_growable_fd_set_add Unexecuted instantiation: filters.c:php_growable_fd_set_add Unexecuted instantiation: fsock.c:php_growable_fd_set_add Unexecuted instantiation: ftp_fopen_wrapper.c:php_growable_fd_set_add Unexecuted instantiation: head.c:php_growable_fd_set_add Unexecuted instantiation: html.c:php_growable_fd_set_add Unexecuted instantiation: http_fopen_wrapper.c:php_growable_fd_set_add Unexecuted instantiation: image.c:php_growable_fd_set_add Unexecuted instantiation: io_poll.c:php_growable_fd_set_add Unexecuted instantiation: net.c:php_growable_fd_set_add Unexecuted instantiation: php_fopen_wrapper.c:php_growable_fd_set_add Unexecuted instantiation: proc_open.c:php_growable_fd_set_add Unexecuted instantiation: streamsfuncs.c:php_growable_fd_set_add Unexecuted instantiation: string.c:php_growable_fd_set_add Unexecuted instantiation: url.c:php_growable_fd_set_add Unexecuted instantiation: user_filters.c:php_growable_fd_set_add Unexecuted instantiation: fopen_wrappers.c:php_growable_fd_set_add Unexecuted instantiation: main.c:php_growable_fd_set_add Unexecuted instantiation: network.c:php_growable_fd_set_add Unexecuted instantiation: php_variables.c:php_growable_fd_set_add Unexecuted instantiation: php_io.c:php_growable_fd_set_add Unexecuted instantiation: php_io_copy_linux.c:php_growable_fd_set_add Unexecuted instantiation: poll_backend_epoll.c:php_growable_fd_set_add Unexecuted instantiation: poll_backend_eventport.c:php_growable_fd_set_add Unexecuted instantiation: poll_backend_kqueue.c:php_growable_fd_set_add Unexecuted instantiation: poll_backend_poll.c:php_growable_fd_set_add Unexecuted instantiation: poll_core.c:php_growable_fd_set_add Unexecuted instantiation: poll_fd_table.c:php_growable_fd_set_add Unexecuted instantiation: poll_handle.c:php_growable_fd_set_add Unexecuted instantiation: cast.c:php_growable_fd_set_add Unexecuted instantiation: filter.c:php_growable_fd_set_add Unexecuted instantiation: plain_wrapper.c:php_growable_fd_set_add Unexecuted instantiation: stream_errors.c:php_growable_fd_set_add Unexecuted instantiation: streams.c:php_growable_fd_set_add Unexecuted instantiation: transports.c:php_growable_fd_set_add Unexecuted instantiation: userspace.c:php_growable_fd_set_add Unexecuted instantiation: xp_socket.c:php_growable_fd_set_add Unexecuted instantiation: internal_functions_cli.c:php_growable_fd_set_add |
398 | | |
399 | | static zend_always_inline bool php_growable_fd_set_isset(const php_growable_fd_set *s, php_socket_t fd) |
400 | 0 | { |
401 | 0 | return (size_t) fd < s->size * CHAR_BIT |
402 | 0 | && (((const php_growable_fd_mask *) s->set)[fd / PHP_GROWABLE_FD_SET_WORD_BITS] >> (fd % PHP_GROWABLE_FD_SET_WORD_BITS)) & 1; |
403 | 0 | } Unexecuted instantiation: exif.c:php_growable_fd_set_isset Unexecuted instantiation: hash.c:php_growable_fd_set_isset Unexecuted instantiation: spl_directory.c:php_growable_fd_set_isset Unexecuted instantiation: basic_functions.c:php_growable_fd_set_isset Unexecuted instantiation: dir.c:php_growable_fd_set_isset Unexecuted instantiation: dns.c:php_growable_fd_set_isset Unexecuted instantiation: exec.c:php_growable_fd_set_isset Unexecuted instantiation: file.c:php_growable_fd_set_isset Unexecuted instantiation: filters.c:php_growable_fd_set_isset Unexecuted instantiation: fsock.c:php_growable_fd_set_isset Unexecuted instantiation: ftp_fopen_wrapper.c:php_growable_fd_set_isset Unexecuted instantiation: head.c:php_growable_fd_set_isset Unexecuted instantiation: html.c:php_growable_fd_set_isset Unexecuted instantiation: http_fopen_wrapper.c:php_growable_fd_set_isset Unexecuted instantiation: image.c:php_growable_fd_set_isset Unexecuted instantiation: io_poll.c:php_growable_fd_set_isset Unexecuted instantiation: net.c:php_growable_fd_set_isset Unexecuted instantiation: php_fopen_wrapper.c:php_growable_fd_set_isset Unexecuted instantiation: proc_open.c:php_growable_fd_set_isset Unexecuted instantiation: streamsfuncs.c:php_growable_fd_set_isset Unexecuted instantiation: string.c:php_growable_fd_set_isset Unexecuted instantiation: url.c:php_growable_fd_set_isset Unexecuted instantiation: user_filters.c:php_growable_fd_set_isset Unexecuted instantiation: fopen_wrappers.c:php_growable_fd_set_isset Unexecuted instantiation: main.c:php_growable_fd_set_isset Unexecuted instantiation: network.c:php_growable_fd_set_isset Unexecuted instantiation: php_variables.c:php_growable_fd_set_isset Unexecuted instantiation: php_io.c:php_growable_fd_set_isset Unexecuted instantiation: php_io_copy_linux.c:php_growable_fd_set_isset Unexecuted instantiation: poll_backend_epoll.c:php_growable_fd_set_isset Unexecuted instantiation: poll_backend_eventport.c:php_growable_fd_set_isset Unexecuted instantiation: poll_backend_kqueue.c:php_growable_fd_set_isset Unexecuted instantiation: poll_backend_poll.c:php_growable_fd_set_isset Unexecuted instantiation: poll_core.c:php_growable_fd_set_isset Unexecuted instantiation: poll_fd_table.c:php_growable_fd_set_isset Unexecuted instantiation: poll_handle.c:php_growable_fd_set_isset Unexecuted instantiation: cast.c:php_growable_fd_set_isset Unexecuted instantiation: filter.c:php_growable_fd_set_isset Unexecuted instantiation: plain_wrapper.c:php_growable_fd_set_isset Unexecuted instantiation: stream_errors.c:php_growable_fd_set_isset Unexecuted instantiation: streams.c:php_growable_fd_set_isset Unexecuted instantiation: transports.c:php_growable_fd_set_isset Unexecuted instantiation: userspace.c:php_growable_fd_set_isset Unexecuted instantiation: xp_socket.c:php_growable_fd_set_isset Unexecuted instantiation: internal_functions_cli.c:php_growable_fd_set_isset |
404 | | |
405 | | static zend_always_inline void php_growable_fd_set_destroy(php_growable_fd_set *s) |
406 | 0 | { |
407 | 0 | efree(s->set); |
408 | 0 | } Unexecuted instantiation: exif.c:php_growable_fd_set_destroy Unexecuted instantiation: hash.c:php_growable_fd_set_destroy Unexecuted instantiation: spl_directory.c:php_growable_fd_set_destroy Unexecuted instantiation: basic_functions.c:php_growable_fd_set_destroy Unexecuted instantiation: dir.c:php_growable_fd_set_destroy Unexecuted instantiation: dns.c:php_growable_fd_set_destroy Unexecuted instantiation: exec.c:php_growable_fd_set_destroy Unexecuted instantiation: file.c:php_growable_fd_set_destroy Unexecuted instantiation: filters.c:php_growable_fd_set_destroy Unexecuted instantiation: fsock.c:php_growable_fd_set_destroy Unexecuted instantiation: ftp_fopen_wrapper.c:php_growable_fd_set_destroy Unexecuted instantiation: head.c:php_growable_fd_set_destroy Unexecuted instantiation: html.c:php_growable_fd_set_destroy Unexecuted instantiation: http_fopen_wrapper.c:php_growable_fd_set_destroy Unexecuted instantiation: image.c:php_growable_fd_set_destroy Unexecuted instantiation: io_poll.c:php_growable_fd_set_destroy Unexecuted instantiation: net.c:php_growable_fd_set_destroy Unexecuted instantiation: php_fopen_wrapper.c:php_growable_fd_set_destroy Unexecuted instantiation: proc_open.c:php_growable_fd_set_destroy Unexecuted instantiation: streamsfuncs.c:php_growable_fd_set_destroy Unexecuted instantiation: string.c:php_growable_fd_set_destroy Unexecuted instantiation: url.c:php_growable_fd_set_destroy Unexecuted instantiation: user_filters.c:php_growable_fd_set_destroy Unexecuted instantiation: fopen_wrappers.c:php_growable_fd_set_destroy Unexecuted instantiation: main.c:php_growable_fd_set_destroy Unexecuted instantiation: network.c:php_growable_fd_set_destroy Unexecuted instantiation: php_variables.c:php_growable_fd_set_destroy Unexecuted instantiation: php_io.c:php_growable_fd_set_destroy Unexecuted instantiation: php_io_copy_linux.c:php_growable_fd_set_destroy Unexecuted instantiation: poll_backend_epoll.c:php_growable_fd_set_destroy Unexecuted instantiation: poll_backend_eventport.c:php_growable_fd_set_destroy Unexecuted instantiation: poll_backend_kqueue.c:php_growable_fd_set_destroy Unexecuted instantiation: poll_backend_poll.c:php_growable_fd_set_destroy Unexecuted instantiation: poll_core.c:php_growable_fd_set_destroy Unexecuted instantiation: poll_fd_table.c:php_growable_fd_set_destroy Unexecuted instantiation: poll_handle.c:php_growable_fd_set_destroy Unexecuted instantiation: cast.c:php_growable_fd_set_destroy Unexecuted instantiation: filter.c:php_growable_fd_set_destroy Unexecuted instantiation: plain_wrapper.c:php_growable_fd_set_destroy Unexecuted instantiation: stream_errors.c:php_growable_fd_set_destroy Unexecuted instantiation: streams.c:php_growable_fd_set_destroy Unexecuted instantiation: transports.c:php_growable_fd_set_destroy Unexecuted instantiation: userspace.c:php_growable_fd_set_destroy Unexecuted instantiation: xp_socket.c:php_growable_fd_set_destroy Unexecuted instantiation: internal_functions_cli.c:php_growable_fd_set_destroy |
409 | | #endif |
410 | | |
411 | | |
412 | 16 | #define PHP_SOCK_CHUNK_SIZE 8192 |
413 | | |
414 | | #ifdef HAVE_STRUCT_SOCKADDR_STORAGE |
415 | | typedef struct sockaddr_storage php_sockaddr_storage; |
416 | | #else |
417 | | typedef struct { |
418 | | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN |
419 | | unsigned char ss_len; |
420 | | unsigned char ss_family; |
421 | | #else |
422 | | unsigned short ss_family; |
423 | | #endif |
424 | | char info[126]; |
425 | | } php_sockaddr_storage; |
426 | | #endif |
427 | | |
428 | 0 | #define PHP_SOCKVAL_TCP_NODELAY (1 << 0) |
429 | 0 | #define PHP_SOCKVAL_TCP_KEEPIDLE (1 << 1) |
430 | 0 | #define PHP_SOCKVAL_TCP_KEEPCNT (1 << 2) |
431 | 0 | #define PHP_SOCKVAL_TCP_KEEPINTVL (1 << 3) |
432 | 0 | #define PHP_SOCKVAL_SO_LINGER (1 << 4) |
433 | 0 | #define PHP_SOCKVAL_SO_RCVBUF (1 << 5) |
434 | 0 | #define PHP_SOCKVAL_SO_SNDBUF (1 << 6) |
435 | | |
436 | 0 | #define PHP_SOCKVAL_IS_SET(sockvals, opt) ((sockvals)->mask & (opt)) |
437 | | |
438 | | typedef struct { |
439 | | unsigned int mask; |
440 | | int tcp_nodelay; |
441 | | int linger; |
442 | | int rcvbuf; |
443 | | int sndbuf; |
444 | | struct { |
445 | | int keepidle; |
446 | | int keepcnt; |
447 | | int keepintvl; |
448 | | } keepalive; |
449 | | } php_sockvals; |
450 | | |
451 | | BEGIN_EXTERN_C() |
452 | | PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **error_string); |
453 | | PHPAPI void php_network_freeaddresses(struct sockaddr **sal); |
454 | | |
455 | | PHPAPI void php_network_apply_sockvals(php_socket_t sock, const php_sockvals *sockvals); |
456 | | |
457 | | PHPAPI php_socket_t php_network_connect_socket_to_host_ex(const char *host, unsigned short port, |
458 | | int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string, |
459 | | int *error_code, const char *bindto, unsigned short bindport, long sockopts, php_sockvals *sockvals |
460 | | ); |
461 | | |
462 | | PHPAPI php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port, |
463 | | int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string, |
464 | | int *error_code, const char *bindto, unsigned short bindport, long sockopts |
465 | | ); |
466 | | |
467 | | PHPAPI int php_network_connect_socket(php_socket_t sockfd, |
468 | | const struct sockaddr *addr, |
469 | | socklen_t addrlen, |
470 | | int asynchronous, |
471 | | struct timeval *timeout, |
472 | | zend_string **error_string, |
473 | | int *error_code); |
474 | | |
475 | | #define php_connect_nonb(sock, addr, addrlen, timeout) \ |
476 | | php_network_connect_socket((sock), (addr), (addrlen), 0, (timeout), NULL, NULL) |
477 | | |
478 | | PHPAPI php_socket_t php_network_bind_socket_to_local_addr_ex(const char *host, unsigned port, |
479 | | int socktype, long sockopts, php_sockvals *sockvals, zend_string **error_string, int *error_code |
480 | | ); |
481 | | |
482 | | PHPAPI php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port, |
483 | | int socktype, long sockopts, zend_string **error_string, int *error_code |
484 | | ); |
485 | | |
486 | | PHPAPI php_socket_t php_network_accept_incoming_ex(php_socket_t srvsock, |
487 | | zend_string **textaddr, |
488 | | struct sockaddr **addr, |
489 | | socklen_t *addrlen, |
490 | | struct timeval *timeout, |
491 | | zend_string **error_string, |
492 | | int *error_code, |
493 | | php_sockvals *sockvals |
494 | | ); |
495 | | |
496 | | PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock, |
497 | | zend_string **textaddr, |
498 | | struct sockaddr **addr, |
499 | | socklen_t *addrlen, |
500 | | struct timeval *timeout, |
501 | | zend_string **error_string, |
502 | | int *error_code, |
503 | | int tcp_nodelay |
504 | | ); |
505 | | |
506 | | PHPAPI int php_network_get_sock_name(php_socket_t sock, |
507 | | zend_string **textaddr, |
508 | | struct sockaddr **addr, |
509 | | socklen_t *addrlen |
510 | | ); |
511 | | |
512 | | PHPAPI int php_network_get_peer_name(php_socket_t sock, |
513 | | zend_string **textaddr, |
514 | | struct sockaddr **addr, |
515 | | socklen_t *addrlen |
516 | | ); |
517 | | |
518 | | PHPAPI void php_any_addr(int family, php_sockaddr_storage *addr, unsigned short port); |
519 | | PHPAPI socklen_t php_sockaddr_size(php_sockaddr_storage *addr); |
520 | | END_EXTERN_C() |
521 | | |
522 | | struct _php_netstream_data_t { |
523 | | php_socket_t socket; |
524 | | bool is_blocked; |
525 | | bool timeout_event; |
526 | | struct timeval timeout; |
527 | | size_t ownsize; |
528 | | }; |
529 | | typedef struct _php_netstream_data_t php_netstream_data_t; |
530 | | PHPAPI extern const php_stream_ops php_stream_socket_ops; |
531 | | extern const php_stream_ops php_stream_generic_socket_ops; |
532 | | #define PHP_STREAM_IS_SOCKET (&php_stream_socket_ops) |
533 | | |
534 | | BEGIN_EXTERN_C() |
535 | | PHPAPI php_stream *_php_stream_sock_open_from_socket(php_socket_t socket, const char *persistent_id STREAMS_DC ); |
536 | | /* open a connection to a host using php_hostconnect and return a stream */ |
537 | | PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port, |
538 | | int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC); |
539 | | PHPAPI void php_network_populate_name_from_sockaddr( |
540 | | /* input address */ |
541 | | struct sockaddr *sa, socklen_t sl, |
542 | | /* output readable address */ |
543 | | zend_string **textaddr, |
544 | | /* output address */ |
545 | | struct sockaddr **addr, |
546 | | socklen_t *addrlen |
547 | | ); |
548 | | |
549 | | PHPAPI zend_result php_network_parse_network_address_with_port(const char *addr, |
550 | | size_t addrlen, struct sockaddr *sa, socklen_t *sl); |
551 | | |
552 | | PHPAPI struct hostent* php_network_gethostbyname(const char *name); |
553 | | |
554 | | PHPAPI zend_result php_set_sock_blocking(php_socket_t socketd, bool block); |
555 | | END_EXTERN_C() |
556 | | |
557 | 0 | #define php_stream_sock_open_from_socket(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_CC) |
558 | 0 | #define php_stream_sock_open_host(host, port, socktype, timeout, persistent) _php_stream_sock_open_host((host), (port), (socktype), (timeout), (persistent) STREAMS_CC) |
559 | | |
560 | | /* {{{ memory debug */ |
561 | | #define php_stream_sock_open_from_socket_rel(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_REL_CC) |
562 | | #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) |
563 | | #define php_stream_sock_open_unix_rel(path, pathlen, persistent, timeval) _php_stream_sock_open_unix((path), (pathlen), (persistent), (timeval) STREAMS_REL_CC) |
564 | | |
565 | | /* }}} */ |
566 | | |
567 | | #ifndef MAXFQDNLEN |
568 | 0 | #define MAXFQDNLEN 255 |
569 | | #endif |
570 | | |
571 | | #endif /* _PHP_NETWORK_H */ |