select.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifndef HEADER_CURL_SELECT_H
  2. #define HEADER_CURL_SELECT_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #ifdef HAVE_POLL_H
  28. #include <poll.h>
  29. #elif defined(HAVE_SYS_POLL_H)
  30. #include <sys/poll.h>
  31. #endif
  32. /*
  33. * Definition of pollfd struct and constants for platforms lacking them.
  34. */
  35. #if !defined(HAVE_SYS_POLL_H) && \
  36. !defined(HAVE_POLL_H) && \
  37. !defined(POLLIN)
  38. #define POLLIN 0x01
  39. #define POLLPRI 0x02
  40. #define POLLOUT 0x04
  41. #define POLLERR 0x08
  42. #define POLLHUP 0x10
  43. #define POLLNVAL 0x20
  44. struct pollfd
  45. {
  46. curl_socket_t fd;
  47. short events;
  48. short revents;
  49. };
  50. #endif
  51. #ifndef POLLRDNORM
  52. #define POLLRDNORM POLLIN
  53. #endif
  54. #ifndef POLLWRNORM
  55. #define POLLWRNORM POLLOUT
  56. #endif
  57. #ifndef POLLRDBAND
  58. #define POLLRDBAND POLLPRI
  59. #endif
  60. /* there are three CSELECT defines that are defined in the public header that
  61. are exposed to users, but this *IN2 bit is only ever used internally and
  62. therefore defined here */
  63. #define CURL_CSELECT_IN2 (CURL_CSELECT_ERR << 1)
  64. int Curl_socket_check(curl_socket_t readfd, curl_socket_t readfd2,
  65. curl_socket_t writefd,
  66. timediff_t timeout_ms);
  67. #define SOCKET_READABLE(x,z) \
  68. Curl_socket_check(x, CURL_SOCKET_BAD, CURL_SOCKET_BAD, z)
  69. #define SOCKET_WRITABLE(x,z) \
  70. Curl_socket_check(CURL_SOCKET_BAD, CURL_SOCKET_BAD, x, z)
  71. int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms);
  72. /*
  73. With Winsock the valid range is [0..INVALID_SOCKET-1] according to
  74. https://learn.microsoft.com/windows/win32/winsock/socket-data-type-2
  75. */
  76. #ifdef USE_WINSOCK
  77. #define VALID_SOCK(s) ((s) < INVALID_SOCKET)
  78. #define FDSET_SOCK(x) 1
  79. #define VERIFY_SOCK(x) do { \
  80. if(!VALID_SOCK(x)) { \
  81. SET_SOCKERRNO(SOCKEINVAL); \
  82. return -1; \
  83. } \
  84. } while(0)
  85. #else
  86. #define VALID_SOCK(s) ((s) >= 0)
  87. /* If the socket is small enough to get set or read from an fdset */
  88. #define FDSET_SOCK(s) ((s) < FD_SETSIZE)
  89. #define VERIFY_SOCK(x) do { \
  90. if(!VALID_SOCK(x) || !FDSET_SOCK(x)) { \
  91. SET_SOCKERRNO(SOCKEINVAL); \
  92. return -1; \
  93. } \
  94. } while(0)
  95. #endif
  96. /* Keep the sockets to poll for an easy handle.
  97. * `actions` are bitmaps of CURL_POLL_IN and CURL_POLL_OUT.
  98. * Starts with small capacity, grows on demand.
  99. */
  100. #define EZ_POLLSET_DEF_COUNT 2
  101. struct easy_pollset {
  102. curl_socket_t *sockets;
  103. unsigned char *actions;
  104. unsigned int n;
  105. unsigned int count;
  106. #ifdef DEBUGBUILD
  107. int init;
  108. #endif
  109. curl_socket_t def_sockets[EZ_POLLSET_DEF_COUNT];
  110. unsigned char def_actions[EZ_POLLSET_DEF_COUNT];
  111. };
  112. #ifdef DEBUGBUILD
  113. #define CURL_EASY_POLLSET_MAGIC 0x7a657370
  114. #endif
  115. /* allocate and initialise */
  116. struct easy_pollset *Curl_pollset_create(void);
  117. /* Initialize before first use */
  118. void Curl_pollset_init(struct easy_pollset *ps);
  119. /* Free any allocated resources */
  120. void Curl_pollset_cleanup(struct easy_pollset *ps);
  121. /* Reset to an empty pollset */
  122. void Curl_pollset_reset(struct easy_pollset *ps);
  123. /* Move pollset from to pollset to, replacing all in to,
  124. * leaving from empty. */
  125. void Curl_pollset_move(struct easy_pollset *to, struct easy_pollset *from);
  126. /* Change the poll flags (CURL_POLL_IN/CURL_POLL_OUT) to the poll set for
  127. * socket `sock`. If the socket is not already part of the poll set, it
  128. * will be added.
  129. * If the socket is present and all poll flags are cleared, it will be removed.
  130. */
  131. CURLcode Curl_pollset_change(struct Curl_easy *data,
  132. struct easy_pollset *ps, curl_socket_t sock,
  133. int add_flags,
  134. int remove_flags) WARN_UNUSED_RESULT;
  135. CURLcode Curl_pollset_set(struct Curl_easy *data,
  136. struct easy_pollset *ps, curl_socket_t sock,
  137. bool do_in, bool do_out) WARN_UNUSED_RESULT;
  138. #define Curl_pollset_add_in(data, ps, sock) \
  139. Curl_pollset_change((data), (ps), (sock), CURL_POLL_IN, 0)
  140. #define Curl_pollset_add_out(data, ps, sock) \
  141. Curl_pollset_change((data), (ps), (sock), CURL_POLL_OUT, 0)
  142. #define Curl_pollset_add_inout(data, ps, sock) \
  143. Curl_pollset_change((data), (ps), (sock), \
  144. CURL_POLL_IN|CURL_POLL_OUT, 0)
  145. #define Curl_pollset_set_in_only(data, ps, sock) \
  146. Curl_pollset_change((data), (ps), (sock), \
  147. CURL_POLL_IN, CURL_POLL_OUT)
  148. #define Curl_pollset_set_out_only(data, ps, sock) \
  149. Curl_pollset_change((data), (ps), (sock), \
  150. CURL_POLL_OUT, CURL_POLL_IN)
  151. /* return < = on error, 0 on timeout or how many sockets are ready */
  152. int Curl_pollset_poll(struct Curl_easy *data,
  153. struct easy_pollset *ps,
  154. timediff_t timeout_ms);
  155. /**
  156. * Check if the pollset, as is, wants to read and/or write regarding
  157. * the given socket.
  158. */
  159. void Curl_pollset_check(struct Curl_easy *data,
  160. struct easy_pollset *ps, curl_socket_t sock,
  161. bool *pwant_read, bool *pwant_write);
  162. /**
  163. * Return TRUE if the pollset contains socket with CURL_POLL_IN.
  164. */
  165. bool Curl_pollset_want_read(struct Curl_easy *data,
  166. struct easy_pollset *ps,
  167. curl_socket_t sock);
  168. struct curl_pollfds {
  169. struct pollfd *pfds;
  170. unsigned int n;
  171. unsigned int count;
  172. BIT(allocated_pfds);
  173. };
  174. void Curl_pollfds_init(struct curl_pollfds *cpfds,
  175. struct pollfd *static_pfds,
  176. unsigned int static_count);
  177. void Curl_pollfds_reset(struct curl_pollfds *cpfds);
  178. void Curl_pollfds_cleanup(struct curl_pollfds *cpfds);
  179. CURLcode Curl_pollfds_add_ps(struct curl_pollfds *cpfds,
  180. struct easy_pollset *ps);
  181. CURLcode Curl_pollfds_add_sock(struct curl_pollfds *cpfds,
  182. curl_socket_t sock, short events);
  183. struct Curl_waitfds {
  184. struct curl_waitfd *wfds;
  185. unsigned int n;
  186. unsigned int count;
  187. };
  188. void Curl_waitfds_init(struct Curl_waitfds *cwfds,
  189. struct curl_waitfd *static_wfds,
  190. unsigned int static_count);
  191. unsigned int Curl_waitfds_add_ps(struct Curl_waitfds *cwfds,
  192. struct easy_pollset *ps);
  193. #endif /* HEADER_CURL_SELECT_H */