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