fakepoll.c 3.8 KB

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