1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2007 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,v 1.56.2.1.2.1 2007/01/01 09:36:11 sebastian Exp $ */
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 : #ifdef PHP_WIN32
57 : #define php_socket_errno() WSAGetLastError()
58 : #else
59 : #define php_socket_errno() errno
60 : #endif
61 :
62 : /* like strerror, but caller must efree the returned string,
63 : * unless buf is not NULL.
64 : * Also works sensibly for win32 */
65 : BEGIN_EXTERN_C()
66 : PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize);
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 : /* These are here, rather than with the win32 counterparts above,
78 : * since <sys/socket.h> defines them. */
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 : /* uncomment this to debug poll(2) emulation on systems that have poll(2) */
110 : /* #define PHP_USE_POLL_2_EMULATION 1 */
111 :
112 : #if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
113 : # include <sys/poll.h>
114 : typedef struct pollfd php_pollfd;
115 : #else
116 : typedef struct _php_pollfd {
117 : php_socket_t fd;
118 : short events;
119 : short revents;
120 : } php_pollfd;
121 :
122 : PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout);
123 :
124 : # define POLLIN 0x0001 /* There is data to read */
125 : # define POLLPRI 0x0002 /* There is urgent data to read */
126 : # define POLLOUT 0x0004 /* Writing now will not block */
127 : # define POLLERR 0x0008 /* Error condition */
128 : # define POLLHUP 0x0010 /* Hung up */
129 : # define POLLNVAL 0x0020 /* Invalid request: fd not open */
130 :
131 : # ifndef PHP_USE_POLL_2_EMULATION
132 : # define PHP_USE_POLL_2_EMULATION 1
133 : # endif
134 : #endif
135 :
136 : #define PHP_POLLREADABLE (POLLIN|POLLERR|POLLHUP)
137 :
138 : #ifndef PHP_USE_POLL_2_EMULATION
139 : # define php_poll2(ufds, nfds, timeout) poll(ufds, nfds, timeout)
140 : #endif
141 :
142 : /* timeval-to-timeout (for poll(2)) */
143 : static inline int php_tvtoto(struct timeval *timeouttv)
144 14 : {
145 14 : if (timeouttv) {
146 14 : return (timeouttv->tv_sec * 1000) + (timeouttv->tv_usec / 1000);
147 : }
148 0 : return -1;
149 : }
150 :
151 : /* hybrid select(2)/poll(2) for a single descriptor.
152 : * timeouttv follows same rules as select(2), but is reduced to millisecond accuracy.
153 : * Returns 0 on timeout, -1 on error, or the event mask (ala poll(2)).
154 : */
155 : static inline int php_pollfd_for(php_socket_t fd, int events, struct timeval *timeouttv)
156 14 : {
157 : php_pollfd p;
158 : int n;
159 :
160 14 : p.fd = fd;
161 14 : p.events = events;
162 14 : p.revents = 0;
163 :
164 14 : n = php_poll2(&p, 1, php_tvtoto(timeouttv));
165 :
166 14 : if (n > 0) {
167 14 : return p.revents;
168 : }
169 :
170 0 : return n;
171 : }
172 :
173 : static inline int php_pollfd_for_ms(php_socket_t fd, int events, int timeout)
174 0 : {
175 : php_pollfd p;
176 : int n;
177 :
178 0 : p.fd = fd;
179 0 : p.events = events;
180 0 : p.revents = 0;
181 :
182 0 : n = php_poll2(&p, 1, timeout);
183 :
184 0 : if (n > 0) {
185 0 : return p.revents;
186 : }
187 :
188 0 : return n;
189 : }
190 :
191 : /* emit warning and suggestion for unsafe select(2) usage */
192 : PHPAPI void _php_emit_fd_setsize_warning(int max_fd);
193 :
194 : #ifdef PHP_WIN32
195 : /* it is safe to FD_SET too many fd's under win32; the macro will simply ignore
196 : * descriptors that go beyond the default FD_SETSIZE */
197 : # define PHP_SAFE_FD_SET(fd, set) FD_SET(fd, set)
198 : # define PHP_SAFE_FD_ISSET(fd, set) FD_ISSET(fd, set)
199 : # define PHP_SAFE_MAX_FD(m, n) do { if (n + 1 >= FD_SETSIZE) { _php_emit_fd_setsize_warning(n); }} while(0)
200 : #else
201 : # define PHP_SAFE_FD_SET(fd, set) do { if (fd < FD_SETSIZE) FD_SET(fd, set); } while(0)
202 : # define PHP_SAFE_FD_ISSET(fd, set) ((fd < FD_SETSIZE) && FD_ISSET(fd, set))
203 : # define PHP_SAFE_MAX_FD(m, n) do { if (m >= FD_SETSIZE) { _php_emit_fd_setsize_warning(m); m = FD_SETSIZE - 1; }} while(0)
204 : #endif
205 :
206 :
207 : #define PHP_SOCK_CHUNK_SIZE 8192
208 :
209 : #ifdef HAVE_SOCKADDR_STORAGE
210 : typedef struct sockaddr_storage php_sockaddr_storage;
211 : #else
212 : typedef struct {
213 : #ifdef HAVE_SOCKADDR_SA_LEN
214 : unsigned char ss_len;
215 : unsigned char ss_family;
216 : #else
217 : unsigned short ss_family;
218 : #endif
219 : char info[126];
220 : } php_sockaddr_storage;
221 : #endif
222 :
223 : BEGIN_EXTERN_C()
224 : PHPAPI php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
225 : int socktype, int asynchronous, struct timeval *timeout, char **error_string,
226 : int *error_code, char *bindto, unsigned short bindport
227 : TSRMLS_DC);
228 :
229 : PHPAPI int php_network_connect_socket(php_socket_t sockfd,
230 : const struct sockaddr *addr,
231 : socklen_t addrlen,
232 : int asynchronous,
233 : struct timeval *timeout,
234 : char **error_string,
235 : int *error_code);
236 :
237 : #define php_connect_nonb(sock, addr, addrlen, timeout) \
238 : php_network_connect_socket((sock), (addr), (addrlen), 0, (timeout), NULL, NULL)
239 :
240 : PHPAPI php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port,
241 : int socktype, char **error_string, int *error_code
242 : TSRMLS_DC);
243 :
244 : PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock,
245 : char **textaddr, long *textaddrlen,
246 : struct sockaddr **addr,
247 : socklen_t *addrlen,
248 : struct timeval *timeout,
249 : char **error_string,
250 : int *error_code
251 : TSRMLS_DC);
252 :
253 : PHPAPI int php_network_get_sock_name(php_socket_t sock,
254 : char **textaddr, long *textaddrlen,
255 : struct sockaddr **addr,
256 : socklen_t *addrlen
257 : TSRMLS_DC);
258 :
259 : PHPAPI int php_network_get_peer_name(php_socket_t sock,
260 : char **textaddr, long *textaddrlen,
261 : struct sockaddr **addr,
262 : socklen_t *addrlen
263 : TSRMLS_DC);
264 :
265 : PHPAPI void php_any_addr(int family, php_sockaddr_storage *addr, unsigned short port);
266 : PHPAPI int php_sockaddr_size(php_sockaddr_storage *addr);
267 : END_EXTERN_C()
268 :
269 : struct _php_netstream_data_t {
270 : php_socket_t socket;
271 : char is_blocked;
272 : struct timeval timeout;
273 : char timeout_event;
274 : size_t ownsize;
275 : };
276 : typedef struct _php_netstream_data_t php_netstream_data_t;
277 : PHPAPI extern php_stream_ops php_stream_socket_ops;
278 : extern php_stream_ops php_stream_generic_socket_ops;
279 : #define PHP_STREAM_IS_SOCKET (&php_stream_socket_ops)
280 :
281 : BEGIN_EXTERN_C()
282 : PHPAPI php_stream *_php_stream_sock_open_from_socket(php_socket_t socket, const char *persistent_id STREAMS_DC TSRMLS_DC );
283 : /* open a connection to a host using php_hostconnect and return a stream */
284 : PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port,
285 : int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC TSRMLS_DC);
286 : PHPAPI void php_network_populate_name_from_sockaddr(
287 : /* input address */
288 : struct sockaddr *sa, socklen_t sl,
289 : /* output readable address */
290 : char **textaddr, long *textaddrlen,
291 : /* output address */
292 : struct sockaddr **addr,
293 : socklen_t *addrlen
294 : TSRMLS_DC);
295 :
296 : PHPAPI int php_network_parse_network_address_with_port(const char *addr,
297 : long addrlen, struct sockaddr *sa, socklen_t *sl TSRMLS_DC);
298 : END_EXTERN_C()
299 :
300 : #define php_stream_sock_open_from_socket(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_CC TSRMLS_CC)
301 : #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)
302 :
303 : /* {{{ memory debug */
304 : #define php_stream_sock_open_from_socket_rel(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_REL_CC TSRMLS_CC)
305 : #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)
306 : #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)
307 :
308 : /* }}} */
309 :
310 : #endif /* _PHP_NETWORK_H */
311 :
312 : /*
313 : * Local variables:
314 : * tab-width: 8
315 : * c-basic-offset: 8
316 : * End:
317 : */
|