fakepoll.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "fakepoll.h"
  38. /* prevents ISO C error */
  39. static void FAKEPOLL(int stop)
  40. {
  41. if (! stop)
  42. FAKEPOLL(1);
  43. return;
  44. }
  45. #ifdef USE_FAKE_POLL
  46. #include <sys/types.h>
  47. #ifdef HAVE_UNISTD_H
  48. #include <unistd.h>
  49. #endif
  50. #ifdef HAVE_STRING_H
  51. #include <string.h>
  52. #endif
  53. #if _MSC_VER > 1300
  54. #include <winsock2.h>
  55. #include <ws2tcpip.h>
  56. #elif defined(_MSC_VER)
  57. #include <winsock.h>
  58. #endif
  59. /* by default, windows handles only 64 fd's */
  60. #if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
  61. #define FD_SETSIZE MAXCONNECTIONS
  62. #endif
  63. #include "util.h"
  64. int
  65. poll(struct pollfd *ufds, unsigned int nfds, int timeout)
  66. {
  67. int idx, maxfd, fd;
  68. int r;
  69. #ifdef MS_WINDOWS
  70. int any_fds_set = 0;
  71. #endif
  72. fd_set readfds, writefds, exceptfds;
  73. #ifdef USING_FAKE_TIMEVAL
  74. #undef timeval
  75. #undef tv_sec
  76. #undef tv_usec
  77. #endif
  78. struct timeval _timeout;
  79. _timeout.tv_sec = timeout / 1000;
  80. _timeout.tv_usec = (timeout % 1000) * 1000;
  81. FD_ZERO(&readfds);
  82. FD_ZERO(&writefds);
  83. FD_ZERO(&exceptfds);
  84. maxfd = -1;
  85. for (idx = 0; idx < nfds; ++idx) {
  86. ufds[idx].revents = 0;
  87. fd = ufds[idx].fd;
  88. if (fd > maxfd) {
  89. maxfd = fd;
  90. #ifdef MS_WINDOWS
  91. any_fds_set = 1;
  92. #endif
  93. }
  94. if (ufds[idx].events & POLLIN)
  95. FD_SET(fd, &readfds);
  96. if (ufds[idx].events & POLLOUT)
  97. FD_SET(fd, &writefds);
  98. FD_SET(fd, &exceptfds);
  99. }
  100. #ifdef MS_WINDOWS
  101. if (!any_fds_set) {
  102. Sleep(timeout);
  103. return 0;
  104. }
  105. #endif
  106. r = select(maxfd + 1, &readfds, &writefds, &exceptfds,
  107. timeout == -1 ? NULL : &_timeout);
  108. if (r <= 0)
  109. return r;
  110. r = 0;
  111. for (idx = 0; idx < nfds; ++idx) {
  112. fd = ufds[idx].fd;
  113. if (FD_ISSET(fd, &readfds))
  114. ufds[idx].revents |= POLLIN;
  115. if (FD_ISSET(fd, &writefds))
  116. ufds[idx].revents |= POLLOUT;
  117. if (FD_ISSET(fd, &exceptfds))
  118. ufds[idx].revents |= POLLERR;
  119. if (ufds[idx].revents)
  120. ++r;
  121. }
  122. return r;
  123. }
  124. #endif