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