| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
. |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <conio.h> |
| 21 |
#include <errno.h> |
| 22 |
#include <io.h> |
| 23 |
#include <ws2tcpip.h> |
| 24 |
|
| 25 |
static int |
| 26 |
conn_is_closed(int fd) |
| 27 |
{ |
| 28 |
char buf[1]; |
| 29 |
int ret = recv(fd, buf, 1, MSG_PEEK); |
| 30 |
if (ret == -1) { |
| 31 |
switch (WSAGetLastError()) { |
| 32 |
case WSAECONNABORTED: |
| 33 |
case WSAECONNRESET: |
| 34 |
case WSAENETRESET: |
| 35 |
case WSAESHUTDOWN: |
| 36 |
return 1; |
| 37 |
} |
| 38 |
} |
| 39 |
return 0; |
| 40 |
} |
| 41 |
|
| 42 |
static int |
| 43 |
conn_has_oob_data(int fd) |
| 44 |
{ |
| 45 |
char buf[1]; |
| 46 |
return (recv(fd, buf, 1, MSG_PEEK | MSG_OOB) == 1); |
| 47 |
} |
| 48 |
|
| 49 |
static int |
| 50 |
is_socket(int fd) |
| 51 |
{ |
| 52 |
if (fd < 3) |
| 53 |
return 0; |
| 54 |
WSANETWORKEVENTS events; |
| 55 |
return (WSAEnumNetworkEvents((SOCKET)fd, NULL, &events) == 0); |
| 56 |
} |
| 57 |
|
| 58 |
static int |
| 59 |
compute_select_revents(int fd, short events, |
| 60 |
fd_set *rfds, fd_set *wfds, fd_set *efds) |
| 61 |
{ |
| 62 |
int rc = 0; |
| 63 |
|
| 64 |
if ((events & (POLLIN | POLLRDNORM | POLLRDBAND)) && |
| 65 |
FD_ISSET(fd, rfds)) { |
| 66 |
if (conn_is_closed(fd)) |
| 67 |
rc |= POLLHUP; |
| 68 |
else |
| 69 |
rc |= POLLIN | POLLRDNORM; |
| 70 |
} |
| 71 |
|
| 72 |
if ((events & (POLLOUT | POLLWRNORM | POLLWRBAND)) && |
| 73 |
FD_ISSET(fd, wfds)) |
| 74 |
rc |= POLLOUT; |
| 75 |
|
| 76 |
if (FD_ISSET(fd, efds)) { |
| 77 |
if (conn_is_closed(fd)) |
| 78 |
rc |= POLLHUP; |
| 79 |
else if (conn_has_oob_data(fd)) |
| 80 |
rc |= POLLRDBAND | POLLPRI; |
| 81 |
} |
| 82 |
|
| 83 |
return rc; |
| 84 |
} |
| 85 |
|
| 86 |
static int |
| 87 |
compute_wait_revents(HANDLE h, short events, int object, int wait_rc) |
| 88 |
{ |
| 89 |
int rc = 0; |
| 90 |
INPUT_RECORD record; |
| 91 |
DWORD num_read; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
if (events & (POLLOUT | POLLWRNORM)) |
| 98 |
rc |= POLLOUT; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
if ((object == (int)(wait_rc - WAIT_OBJECT_0)) |
| 104 |
&& (events & (POLLIN | POLLRDNORM))) { |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if (h == GetStdHandle(STD_INPUT_HANDLE) && |
| 110 |
PeekConsoleInput(h, &record, 1, &num_read) == 1) { |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if (record.EventType == KEY_EVENT && |
| 118 |
record.Event.KeyEvent.bKeyDown) { |
| 119 |
rc |= POLLIN; |
| 120 |
} else { |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
ReadConsoleInput(h, &record, 1, &num_read); |
| 126 |
} |
| 127 |
} else { |
| 128 |
rc |= POLLIN; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return rc; |
| 133 |
} |
| 134 |
|
| 135 |
static int |
| 136 |
wsa_select_errno(int err) |
| 137 |
{ |
| 138 |
switch (err) { |
| 139 |
case WSAEINTR: |
| 140 |
case WSAEINPROGRESS: |
| 141 |
errno = EINTR; |
| 142 |
break; |
| 143 |
case WSAEFAULT: |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
errno = EFAULT; |
| 150 |
break; |
| 151 |
case WSAEINVAL: |
| 152 |
errno = EINVAL; |
| 153 |
break; |
| 154 |
case WSANOTINITIALISED: |
| 155 |
errno = EPERM; |
| 156 |
break; |
| 157 |
case WSAENETDOWN: |
| 158 |
errno = ENOMEM; |
| 159 |
break; |
| 160 |
} |
| 161 |
return -1; |
| 162 |
} |
| 163 |
|
| 164 |
static int |
| 165 |
poll(struct pollfd *pfds, nfds_t nfds, int timeout_ms) |
| 166 |
{ |
| 167 |
nfds_t i; |
| 168 |
int timespent_ms, looptime_ms; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
fd_set rfds, wfds, efds; |
| 174 |
int rc; |
| 175 |
int num_sockets; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
DWORD wait_rc; |
| 181 |
HANDLE handles[FD_SETSIZE]; |
| 182 |
int num_handles; |
| 183 |
|
| 184 |
if (pfds == NULL) { |
| 185 |
errno = EINVAL; |
| 186 |
return -1; |
| 187 |
} |
| 188 |
|
| 189 |
if (nfds <= 0) { |
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
FD_ZERO(&rfds); |
| 194 |
FD_ZERO(&wfds); |
| 195 |
FD_ZERO(&efds); |
| 196 |
num_sockets = 0; |
| 197 |
num_handles = 0; |
| 198 |
|
| 199 |
for (i = 0; i < nfds; i++) { |
| 200 |
if ((int)pfds[i].fd < 0) |
| 201 |
continue; |
| 202 |
|
| 203 |
if (is_socket(pfds[i].fd)) { |
| 204 |
if (num_sockets >= FD_SETSIZE) { |
| 205 |
errno = EINVAL; |
| 206 |
return -1; |
| 207 |
} |
| 208 |
|
| 209 |
FD_SET(pfds[i].fd, &efds); |
| 210 |
|
| 211 |
if (pfds[i].events & |
| 212 |
(POLLIN | POLLRDNORM | POLLRDBAND)) { |
| 213 |
FD_SET(pfds[i].fd, &rfds); |
| 214 |
} |
| 215 |
|
| 216 |
if (pfds[i].events & |
| 217 |
(POLLOUT | POLLWRNORM | POLLWRBAND)) { |
| 218 |
FD_SET(pfds[i].fd, &wfds); |
| 219 |
} |
| 220 |
num_sockets++; |
| 221 |
|
| 222 |
} else { |
| 223 |
if (num_handles >= FD_SETSIZE) { |
| 224 |
errno = EINVAL; |
| 225 |
return -1; |
| 226 |
} |
| 227 |
|
| 228 |
handles[num_handles++] = |
| 229 |
(HANDLE)_get_osfhandle(pfds[i].fd); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
timespent_ms = 0; |
| 254 |
wait_rc = WAIT_FAILED; |
| 255 |
|
| 256 |
if (timeout_ms < 0) |
| 257 |
timeout_ms = INFINITE; |
| 258 |
looptime_ms = timeout_ms > 100 ? 100 : timeout_ms; |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
fd_set rfds_save = rfds; |
| 266 |
fd_set wfds_save = wfds; |
| 267 |
fd_set efds_save = efds; |
| 268 |
|
| 269 |
do { |
| 270 |
struct timeval tv; |
| 271 |
tv.tv_sec = 0; |
| 272 |
tv.tv_usec = looptime_ms * 1000; |
| 273 |
int handle_signaled = 0; |
| 274 |
|
| 275 |
rfds = rfds_save; |
| 276 |
wfds = wfds_save; |
| 277 |
efds = efds_save; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
if (num_handles) { |
| 283 |
wait_rc = WaitForMultipleObjects(num_handles, handles, |
| 284 |
FALSE, 0); |
| 285 |
if (wait_rc == WAIT_FAILED) { |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
errno = ENOMEM; |
| 294 |
return 0; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
if (num_handles > 0 && |
| 307 |
wait_rc <= WAIT_OBJECT_0 + num_handles - 1) { |
| 308 |
tv.tv_usec = 0; |
| 309 |
handle_signaled = 1; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
rc = select(0, &rfds, &wfds, &efds, &tv); |
| 316 |
if (!handle_signaled && rc == SOCKET_ERROR) |
| 317 |
return wsa_select_errno(WSAGetLastError()); |
| 318 |
|
| 319 |
if (handle_signaled || (num_sockets && rc > 0)) |
| 320 |
break; |
| 321 |
|
| 322 |
timespent_ms += looptime_ms; |
| 323 |
|
| 324 |
} while (timespent_ms < timeout_ms); |
| 325 |
|
| 326 |
rc = 0; |
| 327 |
num_handles = 0; |
| 328 |
for (i = 0; i < nfds; i++) { |
| 329 |
pfds[i].revents = 0; |
| 330 |
|
| 331 |
if ((int)pfds[i].fd < 0) |
| 332 |
continue; |
| 333 |
|
| 334 |
if (is_socket(pfds[i].fd)) { |
| 335 |
|
| 336 |
pfds[i].revents = compute_select_revents(pfds[i].fd, |
| 337 |
pfds[i].events, &rfds, &wfds, &efds); |
| 338 |
|
| 339 |
} else { |
| 340 |
pfds[i].revents = compute_wait_revents( |
| 341 |
handles[num_handles], pfds[i].events, num_handles, |
| 342 |
wait_rc); |
| 343 |
num_handles++; |
| 344 |
} |
| 345 |
|
| 346 |
if (pfds[i].revents) |
| 347 |
rc++; |
| 348 |
} |
| 349 |
|
| 350 |
return rc; |
| 351 |
} |
| 352 |
|