php_network.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #ifndef _PHP_NETWORK_H
  19. #define _PHP_NETWORK_H
  20. #include <php.h>
  21. #ifdef PHP_WIN32
  22. # include "win32/inet.h"
  23. #else
  24. # undef closesocket
  25. # define closesocket close
  26. # include <netinet/tcp.h>
  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. PHPAPI zend_string *php_socket_error_str(long err);
  58. END_EXTERN_C()
  59. #ifdef HAVE_NETINET_IN_H
  60. # include <netinet/in.h>
  61. #endif
  62. #ifdef HAVE_SYS_SOCKET_H
  63. #include <sys/socket.h>
  64. #endif
  65. #ifdef HAVE_GETHOSTBYNAME_R
  66. #include <netdb.h>
  67. #endif
  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. #ifdef HAVE_SYS_TIME_H
  76. #include <sys/time.h>
  77. #endif
  78. #ifdef HAVE_STDDEF_H
  79. #include <stddef.h>
  80. #endif
  81. #ifdef PHP_WIN32
  82. typedef SOCKET php_socket_t;
  83. #else
  84. typedef int php_socket_t;
  85. #endif
  86. #ifdef PHP_WIN32
  87. # define SOCK_ERR INVALID_SOCKET
  88. # define SOCK_CONN_ERR SOCKET_ERROR
  89. # define SOCK_RECV_ERR SOCKET_ERROR
  90. #else
  91. # define SOCK_ERR -1
  92. # define SOCK_CONN_ERR -1
  93. # define SOCK_RECV_ERR -1
  94. #endif
  95. #define STREAM_SOCKOP_NONE (1 << 0)
  96. #define STREAM_SOCKOP_SO_REUSEPORT (1 << 1)
  97. #define STREAM_SOCKOP_SO_BROADCAST (1 << 2)
  98. #define STREAM_SOCKOP_IPV6_V6ONLY (1 << 3)
  99. #define STREAM_SOCKOP_IPV6_V6ONLY_ENABLED (1 << 4)
  100. #define STREAM_SOCKOP_TCP_NODELAY (1 << 5)
  101. /* uncomment this to debug poll(2) emulation on systems that have poll(2) */
  102. /* #define PHP_USE_POLL_2_EMULATION 1 */
  103. #if defined(HAVE_POLL)
  104. # if defined(HAVE_POLL_H)
  105. # include <poll.h>
  106. # elif defined(HAVE_SYS_POLL_H)
  107. # include <sys/poll.h>
  108. # endif
  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. PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout);
  117. #ifndef POLLIN
  118. # define POLLIN 0x0001 /* There is data to read */
  119. # define POLLPRI 0x0002 /* There is urgent data to read */
  120. # define POLLOUT 0x0004 /* Writing now will not block */
  121. # define POLLERR 0x0008 /* Error condition */
  122. # define POLLHUP 0x0010 /* Hung up */
  123. # define POLLNVAL 0x0020 /* Invalid request: fd not open */
  124. #endif
  125. # ifndef PHP_USE_POLL_2_EMULATION
  126. # define PHP_USE_POLL_2_EMULATION 1
  127. # endif
  128. #endif
  129. #define PHP_POLLREADABLE (POLLIN|POLLERR|POLLHUP)
  130. #ifndef PHP_USE_POLL_2_EMULATION
  131. # define php_poll2(ufds, nfds, timeout) poll(ufds, nfds, timeout)
  132. #endif
  133. /* timeval-to-timeout (for poll(2)) */
  134. static inline int php_tvtoto(struct timeval *timeouttv)
  135. {
  136. if (timeouttv) {
  137. return (timeouttv->tv_sec * 1000) + (timeouttv->tv_usec / 1000);
  138. }
  139. return -1;
  140. }
  141. /* hybrid select(2)/poll(2) for a single descriptor.
  142. * timeouttv follows same rules as select(2), but is reduced to millisecond accuracy.
  143. * Returns 0 on timeout, -1 on error, or the event mask (ala poll(2)).
  144. */
  145. static inline int php_pollfd_for(php_socket_t fd, int events, struct timeval *timeouttv)
  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, php_tvtoto(timeouttv));
  153. if (n > 0) {
  154. return p.revents;
  155. }
  156. return n;
  157. }
  158. static inline int php_pollfd_for_ms(php_socket_t fd, int events, int timeout)
  159. {
  160. php_pollfd p;
  161. int n;
  162. p.fd = fd;
  163. p.events = events;
  164. p.revents = 0;
  165. n = php_poll2(&p, 1, timeout);
  166. if (n > 0) {
  167. return p.revents;
  168. }
  169. return n;
  170. }
  171. /* emit warning and suggestion for unsafe select(2) usage */
  172. PHPAPI void _php_emit_fd_setsize_warning(int max_fd);
  173. #ifdef PHP_WIN32
  174. /* it is safe to FD_SET too many fd's under win32; the macro will simply ignore
  175. * descriptors that go beyond the default FD_SETSIZE */
  176. # define PHP_SAFE_FD_SET(fd, set) FD_SET(fd, set)
  177. # define PHP_SAFE_FD_CLR(fd, set) FD_CLR(fd, set)
  178. # define PHP_SAFE_FD_ISSET(fd, set) FD_ISSET(fd, set)
  179. # define PHP_SAFE_MAX_FD(m, n) do { if (n + 1 >= FD_SETSIZE) { _php_emit_fd_setsize_warning(n); }} while(0)
  180. #else
  181. # define PHP_SAFE_FD_SET(fd, set) do { if (fd < FD_SETSIZE) FD_SET(fd, set); } while(0)
  182. # define PHP_SAFE_FD_CLR(fd, set) do { if (fd < FD_SETSIZE) FD_CLR(fd, set); } while(0)
  183. # define PHP_SAFE_FD_ISSET(fd, set) ((fd < FD_SETSIZE) && FD_ISSET(fd, set))
  184. # define PHP_SAFE_MAX_FD(m, n) do { if (m >= FD_SETSIZE) { _php_emit_fd_setsize_warning(m); m = FD_SETSIZE - 1; }} while(0)
  185. #endif
  186. #define PHP_SOCK_CHUNK_SIZE 8192
  187. #ifdef HAVE_SOCKADDR_STORAGE
  188. typedef struct sockaddr_storage php_sockaddr_storage;
  189. #else
  190. typedef struct {
  191. #ifdef HAVE_SOCKADDR_SA_LEN
  192. unsigned char ss_len;
  193. unsigned char ss_family;
  194. #else
  195. unsigned short ss_family;
  196. #endif
  197. char info[126];
  198. } php_sockaddr_storage;
  199. #endif
  200. BEGIN_EXTERN_C()
  201. PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **error_string);
  202. PHPAPI void php_network_freeaddresses(struct sockaddr **sal);
  203. PHPAPI php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
  204. int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string,
  205. int *error_code, char *bindto, unsigned short bindport, long sockopts
  206. );
  207. PHPAPI int php_network_connect_socket(php_socket_t sockfd,
  208. const struct sockaddr *addr,
  209. socklen_t addrlen,
  210. int asynchronous,
  211. struct timeval *timeout,
  212. zend_string **error_string,
  213. int *error_code);
  214. #define php_connect_nonb(sock, addr, addrlen, timeout) \
  215. php_network_connect_socket((sock), (addr), (addrlen), 0, (timeout), NULL, NULL)
  216. PHPAPI php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port,
  217. int socktype, long sockopts, zend_string **error_string, int *error_code
  218. );
  219. PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock,
  220. zend_string **textaddr,
  221. struct sockaddr **addr,
  222. socklen_t *addrlen,
  223. struct timeval *timeout,
  224. zend_string **error_string,
  225. int *error_code,
  226. int tcp_nodelay
  227. );
  228. PHPAPI int php_network_get_sock_name(php_socket_t sock,
  229. zend_string **textaddr,
  230. struct sockaddr **addr,
  231. socklen_t *addrlen
  232. );
  233. PHPAPI int php_network_get_peer_name(php_socket_t sock,
  234. zend_string **textaddr,
  235. struct sockaddr **addr,
  236. socklen_t *addrlen
  237. );
  238. PHPAPI void php_any_addr(int family, php_sockaddr_storage *addr, unsigned short port);
  239. PHPAPI int php_sockaddr_size(php_sockaddr_storage *addr);
  240. END_EXTERN_C()
  241. struct _php_netstream_data_t {
  242. php_socket_t socket;
  243. char is_blocked;
  244. struct timeval timeout;
  245. char timeout_event;
  246. size_t ownsize;
  247. };
  248. typedef struct _php_netstream_data_t php_netstream_data_t;
  249. PHPAPI extern const php_stream_ops php_stream_socket_ops;
  250. extern const php_stream_ops php_stream_generic_socket_ops;
  251. #define PHP_STREAM_IS_SOCKET (&php_stream_socket_ops)
  252. BEGIN_EXTERN_C()
  253. PHPAPI php_stream *_php_stream_sock_open_from_socket(php_socket_t socket, const char *persistent_id STREAMS_DC );
  254. /* open a connection to a host using php_hostconnect and return a stream */
  255. PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port,
  256. int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC);
  257. PHPAPI void php_network_populate_name_from_sockaddr(
  258. /* input address */
  259. struct sockaddr *sa, socklen_t sl,
  260. /* output readable address */
  261. zend_string **textaddr,
  262. /* output address */
  263. struct sockaddr **addr,
  264. socklen_t *addrlen
  265. );
  266. PHPAPI int php_network_parse_network_address_with_port(const char *addr,
  267. zend_long addrlen, struct sockaddr *sa, socklen_t *sl);
  268. PHPAPI struct hostent* php_network_gethostbyname(char *name);
  269. PHPAPI int php_set_sock_blocking(php_socket_t socketd, int block);
  270. END_EXTERN_C()
  271. #define php_stream_sock_open_from_socket(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_CC)
  272. #define php_stream_sock_open_host(host, port, socktype, timeout, persistent) _php_stream_sock_open_host((host), (port), (socktype), (timeout), (persistent) STREAMS_CC)
  273. /* {{{ memory debug */
  274. #define php_stream_sock_open_from_socket_rel(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_REL_CC)
  275. #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)
  276. #define php_stream_sock_open_unix_rel(path, pathlen, persistent, timeval) _php_stream_sock_open_unix((path), (pathlen), (persistent), (timeval) STREAMS_REL_CC)
  277. /* }}} */
  278. #ifndef MAXFQDNLEN
  279. #define MAXFQDNLEN 255
  280. #endif
  281. #endif /* _PHP_NETWORK_H */
  282. /*
  283. * Local variables:
  284. * tab-width: 4
  285. * c-basic-offset: 4
  286. * End:
  287. * vim600: sw=4 ts=4 fdm=marker
  288. * vim<600: sw=4 ts=4
  289. */