This source file includes following definitions.
- php_tvtoto
- php_pollfd_for
- php_pollfd_for_ms
- END_EXTERN_C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #ifndef _PHP_NETWORK_H
22 #define _PHP_NETWORK_H
23
24 #include <php.h>
25
26 #ifdef PHP_WIN32
27 # include "win32/inet.h"
28 #else
29 # undef closesocket
30 # define closesocket close
31 #endif
32
33 #ifndef HAVE_SHUTDOWN
34 #undef shutdown
35 #define shutdown(s,n)
36 #endif
37
38 #ifdef PHP_WIN32
39 # ifdef EWOULDBLOCK
40 # undef EWOULDBLOCK
41 # endif
42 # ifdef EINPROGRESS
43 # undef EINPROGRESS
44 # endif
45 # define EWOULDBLOCK WSAEWOULDBLOCK
46 # define EINPROGRESS WSAEWOULDBLOCK
47 # define fsync _commit
48 # define ftruncate(a, b) chsize(a, b)
49 #endif
50
51 #ifndef EWOULDBLOCK
52 # define EWOULDBLOCK EAGAIN
53 #endif
54
55 #ifdef PHP_WIN32
56 #define php_socket_errno() WSAGetLastError()
57 #else
58 #define php_socket_errno() errno
59 #endif
60
61
62
63
64 BEGIN_EXTERN_C()
65 PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize);
66 PHPAPI zend_string *php_socket_error_str(long err);
67 END_EXTERN_C()
68
69 #ifdef HAVE_NETINET_IN_H
70 # include <netinet/in.h>
71 #endif
72
73 #ifdef HAVE_SYS_SOCKET_H
74 #include <sys/socket.h>
75 #endif
76
77
78
79 #ifndef SHUT_RD
80 # define SHUT_RD 0
81 # define SHUT_WR 1
82 # define SHUT_RDWR 2
83 #endif
84
85 #ifdef HAVE_SYS_TIME_H
86 #include <sys/time.h>
87 #endif
88
89 #ifdef HAVE_STDDEF_H
90 #include <stddef.h>
91 #endif
92
93 #ifdef PHP_WIN32
94 typedef SOCKET php_socket_t;
95 #else
96 typedef int php_socket_t;
97 #endif
98
99 #ifdef PHP_WIN32
100 # define SOCK_ERR INVALID_SOCKET
101 # define SOCK_CONN_ERR SOCKET_ERROR
102 # define SOCK_RECV_ERR SOCKET_ERROR
103 #else
104 # define SOCK_ERR -1
105 # define SOCK_CONN_ERR -1
106 # define SOCK_RECV_ERR -1
107 #endif
108
109 #define STREAM_SOCKOP_NONE (1 << 0)
110 #define STREAM_SOCKOP_SO_REUSEPORT (1 << 1)
111 #define STREAM_SOCKOP_SO_BROADCAST (1 << 2)
112 #define STREAM_SOCKOP_IPV6_V6ONLY (1 << 3)
113 #define STREAM_SOCKOP_IPV6_V6ONLY_ENABLED (1 << 4)
114
115
116
117
118
119 #if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
120 # include <sys/poll.h>
121 typedef struct pollfd php_pollfd;
122 #else
123 typedef struct _php_pollfd {
124 php_socket_t fd;
125 short events;
126 short revents;
127 } php_pollfd;
128
129 PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout);
130
131 #ifndef POLLIN
132 # define POLLIN 0x0001
133 # define POLLPRI 0x0002
134 # define POLLOUT 0x0004
135 # define POLLERR 0x0008
136 # define POLLHUP 0x0010
137 # define POLLNVAL 0x0020
138 #endif
139
140 # ifndef PHP_USE_POLL_2_EMULATION
141 # define PHP_USE_POLL_2_EMULATION 1
142 # endif
143 #endif
144
145 #define PHP_POLLREADABLE (POLLIN|POLLERR|POLLHUP)
146
147 #ifndef PHP_USE_POLL_2_EMULATION
148 # define php_poll2(ufds, nfds, timeout) poll(ufds, nfds, timeout)
149 #endif
150
151
152 static inline int php_tvtoto(struct timeval *timeouttv)
153 {
154 if (timeouttv) {
155 return (timeouttv->tv_sec * 1000) + (timeouttv->tv_usec / 1000);
156 }
157 return -1;
158 }
159
160
161
162
163
164 static inline int php_pollfd_for(php_socket_t fd, int events, struct timeval *timeouttv)
165 {
166 php_pollfd p;
167 int n;
168
169 p.fd = fd;
170 p.events = events;
171 p.revents = 0;
172
173 n = php_poll2(&p, 1, php_tvtoto(timeouttv));
174
175 if (n > 0) {
176 return p.revents;
177 }
178
179 return n;
180 }
181
182 static inline int php_pollfd_for_ms(php_socket_t fd, int events, int timeout)
183 {
184 php_pollfd p;
185 int n;
186
187 p.fd = fd;
188 p.events = events;
189 p.revents = 0;
190
191 n = php_poll2(&p, 1, timeout);
192
193 if (n > 0) {
194 return p.revents;
195 }
196
197 return n;
198 }
199
200
201 PHPAPI void _php_emit_fd_setsize_warning(int max_fd);
202
203 #ifdef PHP_WIN32
204
205
206 # define PHP_SAFE_FD_SET(fd, set) FD_SET(fd, set)
207 # define PHP_SAFE_FD_CLR(fd, set) FD_CLR(fd, set)
208 # define PHP_SAFE_FD_ISSET(fd, set) FD_ISSET(fd, set)
209 # define PHP_SAFE_MAX_FD(m, n) do { if (n + 1 >= FD_SETSIZE) { _php_emit_fd_setsize_warning(n); }} while(0)
210 #else
211 # define PHP_SAFE_FD_SET(fd, set) do { if (fd < FD_SETSIZE) FD_SET(fd, set); } while(0)
212 # define PHP_SAFE_FD_CLR(fd, set) do { if (fd < FD_SETSIZE) FD_CLR(fd, set); } while(0)
213 # define PHP_SAFE_FD_ISSET(fd, set) ((fd < FD_SETSIZE) && FD_ISSET(fd, set))
214 # define PHP_SAFE_MAX_FD(m, n) do { if (m >= FD_SETSIZE) { _php_emit_fd_setsize_warning(m); m = FD_SETSIZE - 1; }} while(0)
215 #endif
216
217
218 #define PHP_SOCK_CHUNK_SIZE 8192
219
220 #ifdef HAVE_SOCKADDR_STORAGE
221 typedef struct sockaddr_storage php_sockaddr_storage;
222 #else
223 typedef struct {
224 #ifdef HAVE_SOCKADDR_SA_LEN
225 unsigned char ss_len;
226 unsigned char ss_family;
227 #else
228 unsigned short ss_family;
229 #endif
230 char info[126];
231 } php_sockaddr_storage;
232 #endif
233
234 BEGIN_EXTERN_C()
235 PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **error_string);
236 PHPAPI void php_network_freeaddresses(struct sockaddr **sal);
237
238 PHPAPI php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
239 int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string,
240 int *error_code, char *bindto, unsigned short bindport, long sockopts
241 );
242
243 PHPAPI int php_network_connect_socket(php_socket_t sockfd,
244 const struct sockaddr *addr,
245 socklen_t addrlen,
246 int asynchronous,
247 struct timeval *timeout,
248 zend_string **error_string,
249 int *error_code);
250
251 #define php_connect_nonb(sock, addr, addrlen, timeout) \
252 php_network_connect_socket((sock), (addr), (addrlen), 0, (timeout), NULL, NULL)
253
254 PHPAPI php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port,
255 int socktype, long sockopts, zend_string **error_string, int *error_code
256 );
257
258 PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock,
259 zend_string **textaddr,
260 struct sockaddr **addr,
261 socklen_t *addrlen,
262 struct timeval *timeout,
263 zend_string **error_string,
264 int *error_code
265 );
266
267 PHPAPI int php_network_get_sock_name(php_socket_t sock,
268 zend_string **textaddr,
269 struct sockaddr **addr,
270 socklen_t *addrlen
271 );
272
273 PHPAPI int php_network_get_peer_name(php_socket_t sock,
274 zend_string **textaddr,
275 struct sockaddr **addr,
276 socklen_t *addrlen
277 );
278
279 PHPAPI void php_any_addr(int family, php_sockaddr_storage *addr, unsigned short port);
280 PHPAPI int php_sockaddr_size(php_sockaddr_storage *addr);
281 END_EXTERN_C()
282
283 struct _php_netstream_data_t {
284 php_socket_t socket;
285 char is_blocked;
286 struct timeval timeout;
287 char timeout_event;
288 size_t ownsize;
289 };
290 typedef struct _php_netstream_data_t php_netstream_data_t;
291 PHPAPI extern php_stream_ops php_stream_socket_ops;
292 extern php_stream_ops php_stream_generic_socket_ops;
293 #define PHP_STREAM_IS_SOCKET (&php_stream_socket_ops)
294
295 BEGIN_EXTERN_C()
296 PHPAPI php_stream *_php_stream_sock_open_from_socket(php_socket_t socket, const char *persistent_id STREAMS_DC );
297
298 PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port,
299 int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC);
300 PHPAPI void php_network_populate_name_from_sockaddr(
301
302 struct sockaddr *sa, socklen_t sl,
303
304 zend_string **textaddr,
305
306 struct sockaddr **addr,
307 socklen_t *addrlen
308 );
309
310 PHPAPI int php_network_parse_network_address_with_port(const char *addr,
311 zend_long addrlen, struct sockaddr *sa, socklen_t *sl);
312 END_EXTERN_C()
313
314 #define php_stream_sock_open_from_socket(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_CC)
315 #define php_stream_sock_open_host(host, port, socktype, timeout, persistent) _php_stream_sock_open_host((host), (port), (socktype), (timeout), (persistent) STREAMS_CC)
316
317
318 #define php_stream_sock_open_from_socket_rel(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_REL_CC)
319 #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)
320 #define php_stream_sock_open_unix_rel(path, pathlen, persistent, timeval) _php_stream_sock_open_unix((path), (pathlen), (persistent), (timeval) STREAMS_REL_CC)
321
322
323
324 #ifndef MAXFQDNLEN
325 #define MAXFQDNLEN 255
326 #endif
327
328 #endif
329
330
331
332
333
334
335