| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) 1998 - 2020, Daniel Stenberg, <[email protected]>, et al. |
| 9 |
* |
| 10 |
* This software is licensed as described in the file COPYING, which |
| 11 |
* you should have received as part of this distribution. The terms |
| 12 |
* are also available at https://curl.haxx.se/docs/copyright.html. |
| 13 |
* |
| 14 |
* You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 |
* copies of the Software, and permit persons to whom the Software is |
| 16 |
* furnished to do so, under the terms of the COPYING file. |
| 17 |
* |
| 18 |
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 |
* KIND, either express or implied. |
| 20 |
* |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include "curl_setup.h" |
| 24 |
|
| 25 |
#ifdef HAVE_SYS_SELECT_H |
| 26 |
#include <sys/select.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#if !defined(HAVE_SELECT) && !defined(HAVE_POLL_FINE) |
| 30 |
#error "We can't compile without select() or poll() support." |
| 31 |
#endif |
| 32 |
|
| 33 |
#if defined(__BEOS__) && !defined(__HAIKU__) |
| 34 |
/* BeOS has FD_SET defined in socket.h */ |
| 35 |
#include <socket.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifdef MSDOS |
| 39 |
#include <dos.h> /* delay() */ |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifdef __VXWORKS__ |
| 43 |
#include <strings.h> /* bzero() in FD_SET */ |
| 44 |
#endif |
| 45 |
|
| 46 |
#include <curl/curl.h> |
| 47 |
|
| 48 |
#include "urldata.h" |
| 49 |
#include "connect.h" |
| 50 |
#include "select.h" |
| 51 |
#include "warnless.h" |
| 52 |
|
| 53 |
/* Convenience local macros */ |
| 54 |
#define ELAPSED_MS() (int)Curl_timediff(Curl_now(), initial_tv) |
| 55 |
|
| 56 |
/* |
| 57 |
* Internal function used for waiting a specific amount of ms |
| 58 |
* in Curl_socket_check() and Curl_poll() when no file descriptor |
| 59 |
* is provided to wait on, just being used to delay execution. |
| 60 |
* WinSock select() and poll() timeout mechanisms need a valid |
| 61 |
* socket descriptor in a not null file descriptor set to work. |
| 62 |
* Waiting indefinitely with this function is not allowed, a |
| 63 |
* zero or negative timeout value will return immediately. |
| 64 |
* Timeout resolution, accuracy, as well as maximum supported |
| 65 |
* value is system dependent, neither factor is a citical issue |
| 66 |
* for the intended use of this function in the library. |
| 67 |
* |
| 68 |
* Return values: |
| 69 |
* -1 = system call error, invalid timeout value, or interrupted |
| 70 |
* 0 = specified timeout has elapsed |
| 71 |
*/ |
| 72 |
int Curl_wait_ms(int timeout_ms) |
| 73 |
{ |
| 74 |
int r = 0; |
| 75 |
|
| 76 |
if(!timeout_ms) |
| 77 |
return 0; |
| 78 |
if(timeout_ms < 0) { |
| 79 |
SET_SOCKERRNO(EINVAL); |
| 80 |
return -1; |
| 81 |
} |
| 82 |
#if defined(MSDOS) |
| 83 |
delay(timeout_ms); |
| 84 |
#elif defined(USE_WINSOCK) |
| 85 |
Sleep(timeout_ms); |
| 86 |
#else |
| 87 |
#if defined(HAVE_POLL_FINE) |
| 88 |
r = poll(NULL, 0, timeout_ms); |
| 89 |
#else |
| 90 |
{ |
| 91 |
struct timeval pending_tv; |
| 92 |
pending_tv.tv_sec = timeout_ms / 1000; |
| 93 |
pending_tv.tv_usec = (timeout_ms % 1000) * 1000; |
| 94 |
r = select(0, NULL, NULL, NULL, &pending_tv); |
| 95 |
} |
| 96 |
#endif /* HAVE_POLL_FINE */ |
| 97 |
#endif /* USE_WINSOCK */ |
| 98 |
if(r) |
| 99 |
r = -1; |
| 100 |
return r; |
| 101 |
} |
| 102 |
|
| 103 |
/* |
| 104 |
* Wait for read or write events on a set of file descriptors. It uses poll() |
| 105 |
* when a fine poll() is available, in order to avoid limits with FD_SETSIZE, |
| 106 |
* otherwise select() is used. An error is returned if select() is being used |
| 107 |
* and a file descriptor is too large for FD_SETSIZE. |
| 108 |
* |
| 109 |
* A negative timeout value makes this function wait indefinitely, |
| 110 |
* unless no valid file descriptor is given, when this happens the |
| 111 |
* negative timeout is ignored and the function times out immediately. |
| 112 |
* |
| 113 |
* Return values: |
| 114 |
* -1 = system call error or fd >= FD_SETSIZE |
| 115 |
* 0 = timeout |
| 116 |
* [bitmask] = action as described below |
| 117 |
* |
| 118 |
* CURL_CSELECT_IN - first socket is readable |
| 119 |
* CURL_CSELECT_IN2 - second socket is readable |
| 120 |
* CURL_CSELECT_OUT - write socket is writable |
| 121 |
* CURL_CSELECT_ERR - an error condition occurred |
| 122 |
*/ |
| 123 |
int Curl_socket_check(curl_socket_t readfd0, /* two sockets to read from */ |
| 124 |
curl_socket_t readfd1, |
| 125 |
curl_socket_t writefd, /* socket to write to */ |
| 126 |
time_t timeout_ms) /* milliseconds to wait */ |
| 127 |
{ |
| 128 |
#ifdef HAVE_POLL_FINE |
| 129 |
struct pollfd pfd[3]; |
| 130 |
int num; |
| 131 |
#else |
| 132 |
struct timeval pending_tv; |
| 133 |
struct timeval *ptimeout; |
| 134 |
fd_set fds_read; |
| 135 |
fd_set fds_write; |
| 136 |
fd_set fds_err; |
| 137 |
curl_socket_t maxfd; |
| 138 |
#endif |
| 139 |
int pending_ms = 0; |
| 140 |
int r; |
| 141 |
int ret; |
| 142 |
|
| 143 |
#if SIZEOF_TIME_T != SIZEOF_INT |
| 144 |
/* wrap-around precaution */ |
| 145 |
if(timeout_ms >= INT_MAX) |
| 146 |
timeout_ms = INT_MAX; |
| 147 |
#endif |
| 148 |
|
| 149 |
if((readfd0 == CURL_SOCKET_BAD) && (readfd1 == CURL_SOCKET_BAD) && |
| 150 |
(writefd == CURL_SOCKET_BAD)) { |
| 151 |
/* no sockets, just wait */ |
| 152 |
r = Curl_wait_ms((int)timeout_ms); |
| 153 |
return r; |
| 154 |
} |
| 155 |
|
| 156 |
/* Avoid initial timestamp, avoid Curl_now() call, when elapsed |
| 157 |
time in this function does not need to be measured. This happens |
| 158 |
when function is called with a zero timeout or a negative timeout |
| 159 |
value indicating a blocking call should be performed. */ |
| 160 |
|
| 161 |
if(timeout_ms > 0) { |
| 162 |
pending_ms = (int)timeout_ms; |
| 163 |
} |
| 164 |
|
| 165 |
#ifdef HAVE_POLL_FINE |
| 166 |
|
| 167 |
num = 0; |
| 168 |
if(readfd0 != CURL_SOCKET_BAD) { |
| 169 |
pfd[num].fd = readfd0; |
| 170 |
pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI; |
| 171 |
pfd[num].revents = 0; |
| 172 |
num++; |
| 173 |
} |
| 174 |
if(readfd1 != CURL_SOCKET_BAD) { |
| 175 |
pfd[num].fd = readfd1; |
| 176 |
pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI; |
| 177 |
pfd[num].revents = 0; |
| 178 |
num++; |
| 179 |
} |
| 180 |
if(writefd != CURL_SOCKET_BAD) { |
| 181 |
pfd[num].fd = writefd; |
| 182 |
pfd[num].events = POLLWRNORM|POLLOUT; |
| 183 |
pfd[num].revents = 0; |
| 184 |
num++; |
| 185 |
} |
| 186 |
|
| 187 |
if(timeout_ms < 0) |
| 188 |
pending_ms = -1; |
| 189 |
else if(!timeout_ms) |
| 190 |
pending_ms = 0; |
| 191 |
r = poll(pfd, num, pending_ms); |
| 192 |
|
| 193 |
if(r < 0) |
| 194 |
return -1; |
| 195 |
if(r == 0) |
| 196 |
return 0; |
| 197 |
|
| 198 |
ret = 0; |
| 199 |
num = 0; |
| 200 |
if(readfd0 != CURL_SOCKET_BAD) { |
| 201 |
if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP)) |
| 202 |
ret |= CURL_CSELECT_IN; |
| 203 |
if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL)) |
| 204 |
ret |= CURL_CSELECT_ERR; |
| 205 |
num++; |
| 206 |
} |
| 207 |
if(readfd1 != CURL_SOCKET_BAD) { |
| 208 |
if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP)) |
| 209 |
ret |= CURL_CSELECT_IN2; |
| 210 |
if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL)) |
| 211 |
ret |= CURL_CSELECT_ERR; |
| 212 |
num++; |
| 213 |
} |
| 214 |
if(writefd != CURL_SOCKET_BAD) { |
| 215 |
if(pfd[num].revents & (POLLWRNORM|POLLOUT)) |
| 216 |
ret |= CURL_CSELECT_OUT; |
| 217 |
if(pfd[num].revents & (POLLERR|POLLHUP|POLLNVAL)) |
| 218 |
ret |= CURL_CSELECT_ERR; |
| 219 |
} |
| 220 |
|
| 221 |
return ret; |
| 222 |
|
| 223 |
#else /* HAVE_POLL_FINE */ |
| 224 |
|
| 225 |
FD_ZERO(&fds_err); |
| 226 |
maxfd = (curl_socket_t)-1; |
| 227 |
|
| 228 |
FD_ZERO(&fds_read); |
| 229 |
if(readfd0 != CURL_SOCKET_BAD) { |
| 230 |
VERIFY_SOCK(readfd0); |
| 231 |
FD_SET(readfd0, &fds_read); |
| 232 |
FD_SET(readfd0, &fds_err); |
| 233 |
maxfd = readfd0; |
| 234 |
} |
| 235 |
if(readfd1 != CURL_SOCKET_BAD) { |
| 236 |
VERIFY_SOCK(readfd1); |
| 237 |
FD_SET(readfd1, &fds_read); |
| 238 |
FD_SET(readfd1, &fds_err); |
| 239 |
if(readfd1 > maxfd) |
| 240 |
maxfd = readfd1; |
| 241 |
} |
| 242 |
|
| 243 |
FD_ZERO(&fds_write); |
| 244 |
if(writefd != CURL_SOCKET_BAD) { |
| 245 |
VERIFY_SOCK(writefd); |
| 246 |
FD_SET(writefd, &fds_write); |
| 247 |
FD_SET(writefd, &fds_err); |
| 248 |
if(writefd > maxfd) |
| 249 |
maxfd = writefd; |
| 250 |
} |
| 251 |
|
| 252 |
ptimeout = (timeout_ms < 0) ? NULL : &pending_tv; |
| 253 |
|
| 254 |
if(timeout_ms > 0) { |
| 255 |
pending_tv.tv_sec = pending_ms / 1000; |
| 256 |
pending_tv.tv_usec = (pending_ms % 1000) * 1000; |
| 257 |
} |
| 258 |
else if(!timeout_ms) { |
| 259 |
pending_tv.tv_sec = 0; |
| 260 |
pending_tv.tv_usec = 0; |
| 261 |
} |
| 262 |
|
| 263 |
/* WinSock select() must not be called with an fd_set that contains zero |
| 264 |
fd flags, or it will return WSAEINVAL. But, it also can't be called |
| 265 |
with no fd_sets at all! From the documentation: |
| 266 |
|
| 267 |
Any two of the parameters, readfds, writefds, or exceptfds, can be |
| 268 |
given as null. At least one must be non-null, and any non-null |
| 269 |
descriptor set must contain at least one handle to a socket. |
| 270 |
|
| 271 |
We know that we have at least one bit set in at least two fd_sets in |
| 272 |
this case, but we may have no bits set in either fds_read or fd_write, |
| 273 |
so check for that and handle it. Luckily, with WinSock, we can _also_ |
| 274 |
ask how many bits are set on an fd_set. |
| 275 |
|
| 276 |
It is unclear why WinSock doesn't just handle this for us instead of |
| 277 |
calling this an error. |
| 278 |
|
| 279 |
Note also that WinSock ignores the first argument, so we don't worry |
| 280 |
about the fact that maxfd is computed incorrectly with WinSock (since |
| 281 |
curl_socket_t is unsigned in such cases and thus -1 is the largest |
| 282 |
value). |
| 283 |
*/ |
| 284 |
#ifdef USE_WINSOCK |
| 285 |
r = select((int)maxfd + 1, |
| 286 |
fds_read.fd_count ? &fds_read : NULL, |
| 287 |
fds_write.fd_count ? &fds_write : NULL, |
| 288 |
&fds_err, ptimeout); |
| 289 |
#else |
| 290 |
r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout); |
| 291 |
#endif |
| 292 |
|
| 293 |
if(r < 0) |
| 294 |
return -1; |
| 295 |
if(r == 0) |
| 296 |
return 0; |
| 297 |
|
| 298 |
ret = 0; |
| 299 |
if(readfd0 != CURL_SOCKET_BAD) { |
| 300 |
if(FD_ISSET(readfd0, &fds_read)) |
| 301 |
ret |= CURL_CSELECT_IN; |
| 302 |
if(FD_ISSET(readfd0, &fds_err)) |
| 303 |
ret |= CURL_CSELECT_ERR; |
| 304 |
} |
| 305 |
if(readfd1 != CURL_SOCKET_BAD) { |
| 306 |
if(FD_ISSET(readfd1, &fds_read)) |
| 307 |
ret |= CURL_CSELECT_IN2; |
| 308 |
if(FD_ISSET(readfd1, &fds_err)) |
| 309 |
ret |= CURL_CSELECT_ERR; |
| 310 |
} |
| 311 |
if(writefd != CURL_SOCKET_BAD) { |
| 312 |
if(FD_ISSET(writefd, &fds_write)) |
| 313 |
ret |= CURL_CSELECT_OUT; |
| 314 |
if(FD_ISSET(writefd, &fds_err)) |
| 315 |
ret |= CURL_CSELECT_ERR; |
| 316 |
} |
| 317 |
|
| 318 |
return ret; |
| 319 |
|
| 320 |
#endif /* HAVE_POLL_FINE */ |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
/* |
| 325 |
* This is a wrapper around poll(). If poll() does not exist, then |
| 326 |
* select() is used instead. An error is returned if select() is |
| 327 |
* being used and a file descriptor is too large for FD_SETSIZE. |
| 328 |
* A negative timeout value makes this function wait indefinitely, |
| 329 |
* unless no valid file descriptor is given, when this happens the |
| 330 |
* negative timeout is ignored and the function times out immediately. |
| 331 |
* |
| 332 |
* Return values: |
| 333 |
* -1 = system call error or fd >= FD_SETSIZE |
| 334 |
* 0 = timeout |
| 335 |
* N = number of structures with non zero revent fields |
| 336 |
*/ |
| 337 |
int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms) |
| 338 |
{ |
| 339 |
#ifndef HAVE_POLL_FINE |
| 340 |
struct timeval pending_tv; |
| 341 |
struct timeval *ptimeout; |
| 342 |
fd_set fds_read; |
| 343 |
fd_set fds_write; |
| 344 |
fd_set fds_err; |
| 345 |
curl_socket_t maxfd; |
| 346 |
#endif |
| 347 |
bool fds_none = TRUE; |
| 348 |
unsigned int i; |
| 349 |
int pending_ms = 0; |
| 350 |
int r; |
| 351 |
|
| 352 |
if(ufds) { |
| 353 |
for(i = 0; i < nfds; i++) { |
| 354 |
if(ufds[i].fd != CURL_SOCKET_BAD) { |
| 355 |
fds_none = FALSE; |
| 356 |
break; |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
if(fds_none) { |
| 361 |
r = Curl_wait_ms(timeout_ms); |
| 362 |
return r; |
| 363 |
} |
| 364 |
|
| 365 |
/* Avoid initial timestamp, avoid Curl_now() call, when elapsed |
| 366 |
time in this function does not need to be measured. This happens |
| 367 |
when function is called with a zero timeout or a negative timeout |
| 368 |
value indicating a blocking call should be performed. */ |
| 369 |
|
| 370 |
if(timeout_ms > 0) { |
| 371 |
pending_ms = timeout_ms; |
| 372 |
} |
| 373 |
|
| 374 |
#ifdef HAVE_POLL_FINE |
| 375 |
|
| 376 |
if(timeout_ms < 0) |
| 377 |
pending_ms = -1; |
| 378 |
else if(!timeout_ms) |
| 379 |
pending_ms = 0; |
| 380 |
r = poll(ufds, nfds, pending_ms); |
| 381 |
|
| 382 |
if(r < 0) |
| 383 |
return -1; |
| 384 |
if(r == 0) |
| 385 |
return 0; |
| 386 |
|
| 387 |
for(i = 0; i < nfds; i++) { |
| 388 |
if(ufds[i].fd == CURL_SOCKET_BAD) |
| 389 |
continue; |
| 390 |
if(ufds[i].revents & POLLHUP) |
| 391 |
ufds[i].revents |= POLLIN; |
| 392 |
if(ufds[i].revents & POLLERR) |
| 393 |
ufds[i].revents |= (POLLIN|POLLOUT); |
| 394 |
} |
| 395 |
|
| 396 |
#else /* HAVE_POLL_FINE */ |
| 397 |
|
| 398 |
FD_ZERO(&fds_read); |
| 399 |
FD_ZERO(&fds_write); |
| 400 |
FD_ZERO(&fds_err); |
| 401 |
maxfd = (curl_socket_t)-1; |
| 402 |
|
| 403 |
for(i = 0; i < nfds; i++) { |
| 404 |
ufds[i].revents = 0; |
| 405 |
if(ufds[i].fd == CURL_SOCKET_BAD) |
| 406 |
continue; |
| 407 |
VERIFY_SOCK(ufds[i].fd); |
| 408 |
if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI| |
| 409 |
POLLRDNORM|POLLWRNORM|POLLRDBAND)) { |
| 410 |
if(ufds[i].fd > maxfd) |
| 411 |
maxfd = ufds[i].fd; |
| 412 |
if(ufds[i].events & (POLLRDNORM|POLLIN)) |
| 413 |
FD_SET(ufds[i].fd, &fds_read); |
| 414 |
if(ufds[i].events & (POLLWRNORM|POLLOUT)) |
| 415 |
FD_SET(ufds[i].fd, &fds_write); |
| 416 |
if(ufds[i].events & (POLLRDBAND|POLLPRI)) |
| 417 |
FD_SET(ufds[i].fd, &fds_err); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
#ifdef USE_WINSOCK |
| 422 |
/* WinSock select() can't handle zero events. See the comment about this in |
| 423 |
Curl_check_socket(). */ |
| 424 |
if(fds_read.fd_count == 0 && fds_write.fd_count == 0 |
| 425 |
&& fds_err.fd_count == 0) { |
| 426 |
r = Curl_wait_ms(timeout_ms); |
| 427 |
return r; |
| 428 |
} |
| 429 |
#endif |
| 430 |
|
| 431 |
ptimeout = (timeout_ms < 0) ? NULL : &pending_tv; |
| 432 |
|
| 433 |
if(timeout_ms > 0) { |
| 434 |
pending_tv.tv_sec = pending_ms / 1000; |
| 435 |
pending_tv.tv_usec = (pending_ms % 1000) * 1000; |
| 436 |
} |
| 437 |
else if(!timeout_ms) { |
| 438 |
pending_tv.tv_sec = 0; |
| 439 |
pending_tv.tv_usec = 0; |
| 440 |
} |
| 441 |
|
| 442 |
#ifdef USE_WINSOCK |
| 443 |
r = select((int)maxfd + 1, |
| 444 |
/* WinSock select() can't handle fd_sets with zero bits set, so |
| 445 |
don't give it such arguments. See the comment about this in |
| 446 |
Curl_check_socket(). |
| 447 |
*/ |
| 448 |
fds_read.fd_count ? &fds_read : NULL, |
| 449 |
fds_write.fd_count ? &fds_write : NULL, |
| 450 |
fds_err.fd_count ? &fds_err : NULL, ptimeout); |
| 451 |
#else |
| 452 |
r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout); |
| 453 |
#endif |
| 454 |
|
| 455 |
if(r < 0) |
| 456 |
return -1; |
| 457 |
if(r == 0) |
| 458 |
return 0; |
| 459 |
|
| 460 |
r = 0; |
| 461 |
for(i = 0; i < nfds; i++) { |
| 462 |
ufds[i].revents = 0; |
| 463 |
if(ufds[i].fd == CURL_SOCKET_BAD) |
| 464 |
continue; |
| 465 |
if(FD_ISSET(ufds[i].fd, &fds_read)) |
| 466 |
ufds[i].revents |= POLLIN; |
| 467 |
if(FD_ISSET(ufds[i].fd, &fds_write)) |
| 468 |
ufds[i].revents |= POLLOUT; |
| 469 |
if(FD_ISSET(ufds[i].fd, &fds_err)) |
| 470 |
ufds[i].revents |= POLLPRI; |
| 471 |
if(ufds[i].revents != 0) |
| 472 |
r++; |
| 473 |
} |
| 474 |
|
| 475 |
#endif /* HAVE_POLL_FINE */ |
| 476 |
|
| 477 |
return r; |
| 478 |
} |
| 479 |
|
| 480 |
#ifdef TPF |
| 481 |
/* |
| 482 |
* This is a replacement for select() on the TPF platform. |
| 483 |
* It is used whenever libcurl calls select(). |
| 484 |
* The call below to tpf_process_signals() is required because |
| 485 |
* TPF's select calls are not signal interruptible. |
| 486 |
* |
| 487 |
* Return values are the same as select's. |
| 488 |
*/ |
| 489 |
int tpf_select_libcurl(int maxfds, fd_set *reads, fd_set *writes, |
| 490 |
fd_set *excepts, struct timeval *tv) |
| 491 |
{ |
| 492 |
int rc; |
| 493 |
|
| 494 |
rc = tpf_select_bsd(maxfds, reads, writes, excepts, tv); |
| 495 |
tpf_process_signals(); |
| 496 |
return rc; |
| 497 |
} |
| 498 |
#endif /* TPF */ |