select.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, <daniel@haxx.se>, 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. int Curl_wait_ms(timediff_t timeout_ms);
  73. /*
  74. With Winsock the valid range is [0..INVALID_SOCKET-1] according to
  75. https://docs.microsoft.com/en-us/windows/win32/winsock/socket-data-type-2
  76. */
  77. #ifdef USE_WINSOCK
  78. #define VALID_SOCK(s) ((s) < INVALID_SOCKET)
  79. #define FDSET_SOCK(x) 1
  80. #define VERIFY_SOCK(x) do { \
  81. if(!VALID_SOCK(x)) { \
  82. SET_SOCKERRNO(WSAEINVAL); \
  83. return -1; \
  84. } \
  85. } while(0)
  86. #else
  87. #define VALID_SOCK(s) ((s) >= 0)
  88. /* If the socket is small enough to get set or read from an fdset */
  89. #define FDSET_SOCK(s) ((s) < FD_SETSIZE)
  90. #define VERIFY_SOCK(x) do { \
  91. if(!VALID_SOCK(x) || !FDSET_SOCK(x)) { \
  92. SET_SOCKERRNO(EINVAL); \
  93. return -1; \
  94. } \
  95. } while(0)
  96. #endif
  97. struct curl_pollfds {
  98. struct pollfd *pfds;
  99. unsigned int n;
  100. unsigned int count;
  101. BIT(allocated_pfds);
  102. };
  103. void Curl_pollfds_init(struct curl_pollfds *cpfds,
  104. struct pollfd *static_pfds,
  105. unsigned int static_count);
  106. void Curl_pollfds_cleanup(struct curl_pollfds *cpfds);
  107. CURLcode Curl_pollfds_add_ps(struct curl_pollfds *cpfds,
  108. struct easy_pollset *ps);
  109. CURLcode Curl_pollfds_add_sock(struct curl_pollfds *cpfds,
  110. curl_socket_t sock, short events);
  111. struct curl_waitfds {
  112. struct curl_waitfd *wfds;
  113. unsigned int n;
  114. unsigned int count;
  115. };
  116. void Curl_waitfds_init(struct curl_waitfds *cwfds,
  117. struct curl_waitfd *static_wfds,
  118. unsigned int static_count);
  119. CURLcode Curl_waitfds_add_ps(struct curl_waitfds *cwfds,
  120. struct easy_pollset *ps);
  121. #endif /* HEADER_CURL_SELECT_H */