fakepoll.c 3.8 KB

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