You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
9.3 KiB

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