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