php_network.h 9.6 KB

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