select.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #ifndef HEADER_CURL_SELECT_H
  2. #define HEADER_CURL_SELECT_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2021, 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. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef HAVE_POLL_H
  26. #include <poll.h>
  27. #elif defined(HAVE_SYS_POLL_H)
  28. #include <sys/poll.h>
  29. #endif
  30. /*
  31. * Definition of pollfd struct and constants for platforms lacking them.
  32. */
  33. #if !defined(HAVE_STRUCT_POLLFD) && \
  34. !defined(HAVE_SYS_POLL_H) && \
  35. !defined(HAVE_POLL_H) && \
  36. !defined(POLLIN)
  37. #define POLLIN 0x01
  38. #define POLLPRI 0x02
  39. #define POLLOUT 0x04
  40. #define POLLERR 0x08
  41. #define POLLHUP 0x10
  42. #define POLLNVAL 0x20
  43. struct pollfd
  44. {
  45. curl_socket_t fd;
  46. short events;
  47. short revents;
  48. };
  49. #endif
  50. #ifndef POLLRDNORM
  51. #define POLLRDNORM POLLIN
  52. #endif
  53. #ifndef POLLWRNORM
  54. #define POLLWRNORM POLLOUT
  55. #endif
  56. #ifndef POLLRDBAND
  57. #define POLLRDBAND POLLPRI
  58. #endif
  59. /* there are three CSELECT defines that are defined in the public header that
  60. are exposed to users, but this *IN2 bit is only ever used internally and
  61. therefore defined here */
  62. #define CURL_CSELECT_IN2 (CURL_CSELECT_ERR << 1)
  63. int Curl_socket_check(curl_socket_t readfd, curl_socket_t readfd2,
  64. curl_socket_t writefd,
  65. timediff_t timeout_ms);
  66. #define SOCKET_READABLE(x,z) \
  67. Curl_socket_check(x, CURL_SOCKET_BAD, CURL_SOCKET_BAD, z)
  68. #define SOCKET_WRITABLE(x,z) \
  69. Curl_socket_check(CURL_SOCKET_BAD, CURL_SOCKET_BAD, x, z)
  70. int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms);
  71. int Curl_wait_ms(timediff_t timeout_ms);
  72. #ifdef TPF
  73. int tpf_select_libcurl(int maxfds, fd_set* reads, fd_set* writes,
  74. fd_set* excepts, struct timeval *tv);
  75. #endif
  76. /* TPF sockets are not in range [0..FD_SETSIZE-1], which
  77. unfortunately makes it impossible for us to easily check if they're valid
  78. With Winsock the valid range is [0..INVALID_SOCKET-1] according to
  79. https://docs.microsoft.com/en-us/windows/win32/winsock/socket-data-type-2
  80. */
  81. #if defined(TPF)
  82. #define VALID_SOCK(x) 1
  83. #define VERIFY_SOCK(x) Curl_nop_stmt
  84. #define FDSET_SOCK(x) 1
  85. #elif defined(USE_WINSOCK)
  86. #define VALID_SOCK(s) ((s) < INVALID_SOCKET)
  87. #define FDSET_SOCK(x) 1
  88. #define VERIFY_SOCK(x) do { \
  89. if(!VALID_SOCK(x)) { \
  90. SET_SOCKERRNO(WSAEINVAL); \
  91. return -1; \
  92. } \
  93. } while(0)
  94. #else
  95. #define VALID_SOCK(s) ((s) >= 0)
  96. /* If the socket is small enough to get set or read from an fdset */
  97. #define FDSET_SOCK(s) ((s) < FD_SETSIZE)
  98. #define VERIFY_SOCK(x) do { \
  99. if(!VALID_SOCK(x) || !FDSET_SOCK(x)) { \
  100. SET_SOCKERRNO(EINVAL); \
  101. return -1; \
  102. } \
  103. } while(0)
  104. #endif
  105. #endif /* HEADER_CURL_SELECT_H */