select.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Wez Furlong <wez@thebrainroom.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php.h"
  17. #include "php_network.h"
  18. /* Win32 select() will only work with sockets, so we roll our own implementation here.
  19. * - If you supply only sockets, this simply passes through to winsock select().
  20. * - If you supply file handles, there is no way to distinguish between
  21. * ready for read/write or OOB, so any set in which the handle is found will
  22. * be marked as ready.
  23. * - If you supply a mixture of handles and sockets, the system will interleave
  24. * calls between select() and WaitForMultipleObjects(). The time slicing may
  25. * cause this function call to take up to 100 ms longer than you specified.
  26. * - Calling this with NULL sets as a portable way to sleep with sub-second
  27. * accuracy is not supported.
  28. * */
  29. PHPAPI int php_select(php_socket_t max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
  30. {
  31. ULONGLONG ms_total, limit;
  32. HANDLE handles[MAXIMUM_WAIT_OBJECTS];
  33. int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
  34. int n_handles = 0, i;
  35. fd_set sock_read, sock_write, sock_except;
  36. fd_set aread, awrite, aexcept;
  37. int sock_max_fd = -1;
  38. struct timeval tvslice;
  39. int retcode;
  40. /* As max_fd is unsigned, non socket might overflow. */
  41. if (max_fd > (php_socket_t)INT_MAX) {
  42. return -1;
  43. }
  44. #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
  45. /* calculate how long we need to wait in milliseconds */
  46. if (tv == NULL) {
  47. ms_total = INFINITE;
  48. } else {
  49. ms_total = tv->tv_sec * 1000;
  50. ms_total += tv->tv_usec / 1000;
  51. }
  52. FD_ZERO(&sock_read);
  53. FD_ZERO(&sock_write);
  54. FD_ZERO(&sock_except);
  55. /* build an array of handles for non-sockets */
  56. for (i = 0; (uint32_t)i < max_fd; i++) {
  57. if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
  58. handles[n_handles] = (HANDLE)(zend_uintptr_t)_get_osfhandle(i);
  59. if (handles[n_handles] == INVALID_HANDLE_VALUE) {
  60. /* socket */
  61. if (SAFE_FD_ISSET(i, rfds)) {
  62. FD_SET((uint32_t)i, &sock_read);
  63. }
  64. if (SAFE_FD_ISSET(i, wfds)) {
  65. FD_SET((uint32_t)i, &sock_write);
  66. }
  67. if (SAFE_FD_ISSET(i, efds)) {
  68. FD_SET((uint32_t)i, &sock_except);
  69. }
  70. if (i > sock_max_fd) {
  71. sock_max_fd = i;
  72. }
  73. } else {
  74. handle_slot_to_fd[n_handles] = i;
  75. n_handles++;
  76. }
  77. }
  78. }
  79. if (n_handles == 0) {
  80. /* plain sockets only - let winsock handle the whole thing */
  81. return select(-1, rfds, wfds, efds, tv);
  82. }
  83. /* mixture of handles and sockets; lets multiplex between
  84. * winsock and waiting on the handles */
  85. FD_ZERO(&aread);
  86. FD_ZERO(&awrite);
  87. FD_ZERO(&aexcept);
  88. limit = GetTickCount64() + ms_total;
  89. do {
  90. retcode = 0;
  91. if (sock_max_fd >= 0) {
  92. /* overwrite the zero'd sets here; the select call
  93. * will clear those that are not active */
  94. aread = sock_read;
  95. awrite = sock_write;
  96. aexcept = sock_except;
  97. tvslice.tv_sec = 0;
  98. tvslice.tv_usec = 100000;
  99. retcode = select(-1, &aread, &awrite, &aexcept, &tvslice);
  100. }
  101. if (n_handles > 0) {
  102. /* check handles */
  103. DWORD wret;
  104. wret = WaitForMultipleObjects(n_handles, handles, FALSE, retcode > 0 ? 0 : 100);
  105. if (wret == WAIT_TIMEOUT) {
  106. /* set retcode to 0; this is the default.
  107. * select() may have set it to something else,
  108. * in which case we leave it alone, so this branch
  109. * does nothing */
  110. ;
  111. } else if (wret == WAIT_FAILED) {
  112. if (retcode == 0) {
  113. retcode = -1;
  114. }
  115. } else {
  116. if (retcode < 0) {
  117. retcode = 0;
  118. }
  119. for (i = 0; i < n_handles; i++) {
  120. if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
  121. if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
  122. FD_SET((uint32_t)handle_slot_to_fd[i], &aread);
  123. }
  124. if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
  125. FD_SET((uint32_t)handle_slot_to_fd[i], &awrite);
  126. }
  127. if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
  128. FD_SET((uint32_t)handle_slot_to_fd[i], &aexcept);
  129. }
  130. retcode++;
  131. }
  132. }
  133. }
  134. }
  135. } while (retcode == 0 && (ms_total == INFINITE || GetTickCount64() < limit));
  136. if (rfds) {
  137. *rfds = aread;
  138. }
  139. if (wfds) {
  140. *wfds = awrite;
  141. }
  142. if (efds) {
  143. *efds = aexcept;
  144. }
  145. return retcode;
  146. }