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