fakepoll.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * fakepoll.c
  3. *
  4. * On systems where 'poll' doesn't exist, fake it with 'select'.
  5. *
  6. * Copyright (c) 2001-2003, Nick Mathewson <nickm@freehaven.net>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the names of the copyright owners nor the names of its
  21. * contributors may be used to endorse or promote products derived from
  22. * this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "config.h"
  37. #include "defines.h"
  38. #include "common.h"
  39. /* prevents ISO C error */
  40. static void FAKEPOLL(int stop)
  41. {
  42. if (! stop)
  43. FAKEPOLL(1);
  44. return;
  45. }
  46. #ifdef USE_FAKE_POLL
  47. #include <sys/types.h>
  48. #ifdef HAVE_UNISTD_H
  49. #include <unistd.h>
  50. #endif
  51. #ifdef HAVE_STRING_H
  52. #include <string.h>
  53. #endif
  54. #if _MSC_VER > 1300
  55. #include <winsock2.h>
  56. #include <ws2tcpip.h>
  57. #elif defined(_MSC_VER)
  58. #include <winsock.h>
  59. #endif
  60. /* by default, windows handles only 64 fd's */
  61. #if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
  62. #define FD_SETSIZE MAXCONNECTIONS
  63. #endif
  64. #include "util.h"
  65. /**
  66. * custom version of poll() using select() in the backend
  67. * only used if you don't actually have poll on your system.
  68. */
  69. int
  70. poll(struct pollfd *ufds, unsigned int nfds, int timeout)
  71. {
  72. int idx, maxfd, fd;
  73. int r;
  74. #ifdef MS_WINDOWS
  75. int any_fds_set = 0;
  76. #endif
  77. fd_set readfds, writefds, exceptfds;
  78. #ifdef USING_FAKE_TIMEVAL
  79. #undef timeval
  80. #undef tv_sec
  81. #undef tv_usec
  82. #endif
  83. struct timeval _timeout;
  84. _timeout.tv_sec = timeout / 1000;
  85. _timeout.tv_usec = (timeout % 1000) * 1000;
  86. FD_ZERO(&readfds);
  87. FD_ZERO(&writefds);
  88. FD_ZERO(&exceptfds);
  89. maxfd = -1;
  90. for (idx = 0; idx < nfds; ++idx) {
  91. ufds[idx].revents = 0;
  92. fd = ufds[idx].fd;
  93. if (fd > maxfd) {
  94. maxfd = fd;
  95. #ifdef MS_WINDOWS
  96. any_fds_set = 1;
  97. #endif
  98. }
  99. if (ufds[idx].events & POLLIN)
  100. FD_SET(fd, &readfds);
  101. if (ufds[idx].events & POLLOUT)
  102. FD_SET(fd, &writefds);
  103. FD_SET(fd, &exceptfds);
  104. }
  105. #ifdef MS_WINDOWS
  106. if (!any_fds_set) {
  107. Sleep(timeout);
  108. return 0;
  109. }
  110. #endif
  111. r = select(maxfd + 1, &readfds, &writefds, &exceptfds,
  112. timeout == -1 ? NULL : &_timeout);
  113. if (r <= 0)
  114. return r;
  115. r = 0;
  116. for (idx = 0; idx < nfds; ++idx) {
  117. fd = ufds[idx].fd;
  118. if (FD_ISSET(fd, &readfds))
  119. ufds[idx].revents |= POLLIN;
  120. if (FD_ISSET(fd, &writefds))
  121. ufds[idx].revents |= POLLOUT;
  122. if (FD_ISSET(fd, &exceptfds))
  123. ufds[idx].revents |= POLLERR;
  124. if (ufds[idx].revents)
  125. ++r;
  126. }
  127. return r;
  128. }
  129. #endif
  130. /*
  131. Local Variables:
  132. mode:c
  133. indent-tabs-mode:nil
  134. c-basic-offset:4
  135. End:
  136. */