php_network.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Stig Venaas <venaas@uninett.no> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifndef _PHP_NETWORK_H
  17. #define _PHP_NETWORK_H
  18. #include <php.h>
  19. #ifdef PHP_WIN32
  20. # include "win32/inet.h"
  21. #else
  22. # undef closesocket
  23. # define closesocket close
  24. # include <netinet/tcp.h>
  25. #endif
  26. #ifndef HAVE_SHUTDOWN
  27. #undef shutdown
  28. #define shutdown(s,n) /* nothing */
  29. #endif
  30. #ifdef PHP_WIN32
  31. # ifdef EWOULDBLOCK
  32. # undef EWOULDBLOCK
  33. # endif
  34. # ifdef EINPROGRESS
  35. # undef EINPROGRESS
  36. # endif
  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. #ifndef EWOULDBLOCK
  43. # define EWOULDBLOCK EAGAIN
  44. #endif
  45. /* This is a work around for GCC bug 69602: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69602 */
  46. #if EAGAIN != EWOULDBLOCK
  47. # define PHP_IS_TRANSIENT_ERROR(err) (err == EAGAIN || err == EWOULDBLOCK)
  48. #else
  49. # define PHP_IS_TRANSIENT_ERROR(err) (err == EAGAIN)
  50. #endif
  51. #ifdef PHP_WIN32
  52. #define php_socket_errno() WSAGetLastError()
  53. #else
  54. #define php_socket_errno() errno
  55. #endif
  56. /* like strerror, but caller must efree the returned string,
  57. * unless buf is not NULL.
  58. * Also works sensibly for win32 */
  59. BEGIN_EXTERN_C()
  60. PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize);
  61. PHPAPI zend_string *php_socket_error_str(long err);
  62. END_EXTERN_C()
  63. #ifdef HAVE_NETINET_IN_H
  64. # include <netinet/in.h>
  65. #endif
  66. #ifdef HAVE_SYS_SOCKET_H
  67. #include <sys/socket.h>
  68. #endif
  69. #ifdef HAVE_GETHOSTBYNAME_R
  70. #include <netdb.h>
  71. #endif
  72. /* These are here, rather than with the win32 counterparts above,
  73. * since <sys/socket.h> defines them. */
  74. #ifndef SHUT_RD
  75. # define SHUT_RD 0
  76. # define SHUT_WR 1
  77. # define SHUT_RDWR 2
  78. #endif
  79. #ifdef HAVE_SYS_TIME_H
  80. #include <sys/time.h>
  81. #endif
  82. #include <stddef.h>
  83. #ifdef PHP_WIN32
  84. typedef SOCKET php_socket_t;
  85. #else
  86. typedef int php_socket_t;
  87. #endif
  88. #ifdef PHP_WIN32
  89. # define SOCK_ERR INVALID_SOCKET
  90. # define SOCK_CONN_ERR SOCKET_ERROR
  91. # define SOCK_RECV_ERR SOCKET_ERROR
  92. #else
  93. # define SOCK_ERR -1
  94. # define SOCK_CONN_ERR -1
  95. # define SOCK_RECV_ERR -1
  96. #endif
  97. #define STREAM_SOCKOP_NONE (1 << 0)
  98. #define STREAM_SOCKOP_SO_REUSEPORT (1 << 1)
  99. #define STREAM_SOCKOP_SO_BROADCAST (1 << 2)
  100. #define STREAM_SOCKOP_IPV6_V6ONLY (1 << 3)
  101. #define STREAM_SOCKOP_IPV6_V6ONLY_ENABLED (1 << 4)
  102. #define STREAM_SOCKOP_TCP_NODELAY (1 << 5)
  103. /* uncomment this to debug poll(2) emulation on systems that have poll(2) */
  104. /* #define PHP_USE_POLL_2_EMULATION 1 */
  105. #if defined(HAVE_POLL)
  106. # if defined(HAVE_POLL_H)
  107. # include <poll.h>
  108. # elif defined(HAVE_SYS_POLL_H)
  109. # include <sys/poll.h>
  110. # endif
  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. PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout);
  119. #ifndef POLLIN
  120. # define POLLIN 0x0001 /* There is data to read */
  121. # define POLLPRI 0x0002 /* There is urgent data to read */
  122. # define POLLOUT 0x0004 /* Writing now will not block */
  123. # define POLLERR 0x0008 /* Error condition */
  124. # define POLLHUP 0x0010 /* Hung up */
  125. # define POLLNVAL 0x0020 /* Invalid request: fd not open */
  126. #endif
  127. # ifndef PHP_USE_POLL_2_EMULATION
  128. # define PHP_USE_POLL_2_EMULATION 1
  129. # endif
  130. #endif
  131. #define PHP_POLLREADABLE (POLLIN|POLLERR|POLLHUP)
  132. #ifndef PHP_USE_POLL_2_EMULATION
  133. # define php_poll2(ufds, nfds, timeout) poll(ufds, nfds, timeout)
  134. #endif
  135. /* timeval-to-timeout (for poll(2)) */
  136. static inline int php_tvtoto(struct timeval *timeouttv)
  137. {
  138. if (timeouttv) {
  139. return (timeouttv->tv_sec * 1000) + (timeouttv->tv_usec / 1000);
  140. }
  141. return -1;
  142. }
  143. /* hybrid select(2)/poll(2) for a single descriptor.
  144. * timeouttv follows same rules as select(2), but is reduced to millisecond accuracy.
  145. * Returns 0 on timeout, -1 on error, or the event mask (ala poll(2)).
  146. */
  147. static inline int php_pollfd_for(php_socket_t fd, int events, struct timeval *timeouttv)
  148. {
  149. php_pollfd p;
  150. int n;
  151. p.fd = fd;
  152. p.events = events;
  153. p.revents = 0;
  154. n = php_poll2(&p, 1, php_tvtoto(timeouttv));
  155. if (n > 0) {
  156. return p.revents;
  157. }
  158. return n;
  159. }
  160. static inline int php_pollfd_for_ms(php_socket_t fd, int events, int timeout)
  161. {
  162. php_pollfd p;
  163. int n;
  164. p.fd = fd;
  165. p.events = events;
  166. p.revents = 0;
  167. n = php_poll2(&p, 1, timeout);
  168. if (n > 0) {
  169. return p.revents;
  170. }
  171. return n;
  172. }
  173. /* emit warning and suggestion for unsafe select(2) usage */
  174. PHPAPI void _php_emit_fd_setsize_warning(int max_fd);
  175. static inline bool _php_check_fd_setsize(php_socket_t *max_fd, int setsize)
  176. {
  177. #ifdef PHP_WIN32
  178. if (setsize + 1 >= FD_SETSIZE) {
  179. _php_emit_fd_setsize_warning(setsize);
  180. return false;
  181. }
  182. #else
  183. if (*max_fd >= FD_SETSIZE) {
  184. _php_emit_fd_setsize_warning(*max_fd);
  185. *max_fd = FD_SETSIZE - 1;
  186. return false;
  187. }
  188. #endif
  189. return true;
  190. }
  191. #ifdef PHP_WIN32
  192. /* it is safe to FD_SET too many fd's under win32; the macro will simply ignore
  193. * descriptors that go beyond the default FD_SETSIZE */
  194. # define PHP_SAFE_FD_SET(fd, set) FD_SET(fd, set)
  195. # define PHP_SAFE_FD_CLR(fd, set) FD_CLR(fd, set)
  196. # define PHP_SAFE_FD_ISSET(fd, set) FD_ISSET(fd, set)
  197. # define PHP_SAFE_MAX_FD(m, n) _php_check_fd_setsize(&m, n)
  198. #else
  199. # define PHP_SAFE_FD_SET(fd, set) do { if (fd < FD_SETSIZE) FD_SET(fd, set); } while(0)
  200. # define PHP_SAFE_FD_CLR(fd, set) do { if (fd < FD_SETSIZE) FD_CLR(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) _php_check_fd_setsize(&m, n)
  203. #endif
  204. #define PHP_SOCK_CHUNK_SIZE 8192
  205. #ifdef HAVE_SOCKADDR_STORAGE
  206. typedef struct sockaddr_storage php_sockaddr_storage;
  207. #else
  208. typedef struct {
  209. #ifdef HAVE_SOCKADDR_SA_LEN
  210. unsigned char ss_len;
  211. unsigned char ss_family;
  212. #else
  213. unsigned short ss_family;
  214. #endif
  215. char info[126];
  216. } php_sockaddr_storage;
  217. #endif
  218. BEGIN_EXTERN_C()
  219. PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **error_string);
  220. PHPAPI void php_network_freeaddresses(struct sockaddr **sal);
  221. PHPAPI php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
  222. int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string,
  223. int *error_code, const char *bindto, unsigned short bindport, long sockopts
  224. );
  225. PHPAPI int php_network_connect_socket(php_socket_t sockfd,
  226. const struct sockaddr *addr,
  227. socklen_t addrlen,
  228. int asynchronous,
  229. struct timeval *timeout,
  230. zend_string **error_string,
  231. int *error_code);
  232. #define php_connect_nonb(sock, addr, addrlen, timeout) \
  233. php_network_connect_socket((sock), (addr), (addrlen), 0, (timeout), NULL, NULL)
  234. PHPAPI php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port,
  235. int socktype, long sockopts, zend_string **error_string, int *error_code
  236. );
  237. PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock,
  238. zend_string **textaddr,
  239. struct sockaddr **addr,
  240. socklen_t *addrlen,
  241. struct timeval *timeout,
  242. zend_string **error_string,
  243. int *error_code,
  244. int tcp_nodelay
  245. );
  246. PHPAPI int php_network_get_sock_name(php_socket_t sock,
  247. zend_string **textaddr,
  248. struct sockaddr **addr,
  249. socklen_t *addrlen
  250. );
  251. PHPAPI int php_network_get_peer_name(php_socket_t sock,
  252. zend_string **textaddr,
  253. struct sockaddr **addr,
  254. socklen_t *addrlen
  255. );
  256. PHPAPI void php_any_addr(int family, php_sockaddr_storage *addr, unsigned short port);
  257. PHPAPI int php_sockaddr_size(php_sockaddr_storage *addr);
  258. END_EXTERN_C()
  259. struct _php_netstream_data_t {
  260. php_socket_t socket;
  261. char is_blocked;
  262. struct timeval timeout;
  263. char timeout_event;
  264. size_t ownsize;
  265. };
  266. typedef struct _php_netstream_data_t php_netstream_data_t;
  267. PHPAPI extern const php_stream_ops php_stream_socket_ops;
  268. extern const php_stream_ops php_stream_generic_socket_ops;
  269. #define PHP_STREAM_IS_SOCKET (&php_stream_socket_ops)
  270. BEGIN_EXTERN_C()
  271. PHPAPI php_stream *_php_stream_sock_open_from_socket(php_socket_t socket, const char *persistent_id STREAMS_DC );
  272. /* open a connection to a host using php_hostconnect and return a stream */
  273. PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port,
  274. int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC);
  275. PHPAPI void php_network_populate_name_from_sockaddr(
  276. /* input address */
  277. struct sockaddr *sa, socklen_t sl,
  278. /* output readable address */
  279. zend_string **textaddr,
  280. /* output address */
  281. struct sockaddr **addr,
  282. socklen_t *addrlen
  283. );
  284. PHPAPI int php_network_parse_network_address_with_port(const char *addr,
  285. zend_long addrlen, struct sockaddr *sa, socklen_t *sl);
  286. PHPAPI struct hostent* php_network_gethostbyname(const char *name);
  287. PHPAPI int php_set_sock_blocking(php_socket_t socketd, int block);
  288. END_EXTERN_C()
  289. #define php_stream_sock_open_from_socket(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_CC)
  290. #define php_stream_sock_open_host(host, port, socktype, timeout, persistent) _php_stream_sock_open_host((host), (port), (socktype), (timeout), (persistent) STREAMS_CC)
  291. /* {{{ memory debug */
  292. #define php_stream_sock_open_from_socket_rel(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_REL_CC)
  293. #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)
  294. #define php_stream_sock_open_unix_rel(path, pathlen, persistent, timeval) _php_stream_sock_open_unix((path), (pathlen), (persistent), (timeval) STREAMS_REL_CC)
  295. /* }}} */
  296. #ifndef MAXFQDNLEN
  297. #define MAXFQDNLEN 255
  298. #endif
  299. #endif /* _PHP_NETWORK_H */