| 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, 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.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 | #include <curl/curl.h> |
| 26 | |
| 27 | #include "urldata.h" |
| 28 | #include "transfer.h" |
| 29 | #include "url.h" |
| 30 | #include "connect.h" |
| 31 | #include "progress.h" |
| 32 | #include "easyif.h" |
| 33 | #include "share.h" |
| 34 | #include "psl.h" |
| 35 | #include "multiif.h" |
| 36 | #include "sendf.h" |
| 37 | #include "timeval.h" |
| 38 | #include "http.h" |
| 39 | #include "select.h" |
| 40 | #include "warnless.h" |
| 41 | #include "speedcheck.h" |
| 42 | #include "conncache.h" |
| 43 | #include "multihandle.h" |
| 44 | #include "sigpipe.h" |
| 45 | #include "vtls/vtls.h" |
| 46 | #include "connect.h" |
| 47 | #include "http_proxy.h" |
| 48 | #include "http2.h" |
| 49 | #include "socketpair.h" |
| 50 | #include "socks.h" |
| 51 | /* The last 3 #include files should be in this order */ |
| 52 | #include "curl_printf.h" |
| 53 | #include "curl_memory.h" |
| 54 | #include "memdebug.h" |
| 55 | |
| 56 | /* |
| 57 | CURL_SOCKET_HASH_TABLE_SIZE should be a prime number. Increasing it from 97 |
| 58 | to 911 takes on a 32-bit machine 4 x 804 = 3211 more bytes. Still, every |
| 59 | CURL handle takes 45-50 K memory, therefore this 3K are not significant. |
| 60 | */ |
| 61 | #ifndef CURL_SOCKET_HASH_TABLE_SIZE |
| 62 | #define CURL_SOCKET_HASH_TABLE_SIZE 911 |
| 63 | #endif |
| 64 | |
| 65 | #ifndef CURL_CONNECTION_HASH_SIZE |
| 66 | #define CURL_CONNECTION_HASH_SIZE 97 |
| 67 | #endif |
| 68 | |
| 69 | #define CURL_MULTI_HANDLE 0x000bab1e |
| 70 | |
| 71 | #define GOOD_MULTI_HANDLE(x) \ |
| 72 | ((x) && (x)->magic == CURL_MULTI_HANDLE) |
| 73 | |
| 74 | static CURLMcode singlesocket(struct Curl_multi *multi, |
| 75 | struct Curl_easy *data); |
| 76 | static CURLMcode add_next_timeout(struct curltime now, |
| 77 | struct Curl_multi *multi, |
| 78 | struct Curl_easy *d); |
| 79 | static CURLMcode multi_timeout(struct Curl_multi *multi, |
| 80 | long *timeout_ms); |
| 81 | static void process_pending_handles(struct Curl_multi *multi); |
| 82 | |
| 83 | #ifdef DEBUGBUILD |
| 84 | static const char * const statename[]={ |
| 85 | "INIT" , |
| 86 | "PENDING" , |
| 87 | "CONNECT" , |
| 88 | "RESOLVING" , |
| 89 | "CONNECTING" , |
| 90 | "TUNNELING" , |
| 91 | "PROTOCONNECT" , |
| 92 | "PROTOCONNECTING" , |
| 93 | "DO" , |
| 94 | "DOING" , |
| 95 | "DOING_MORE" , |
| 96 | "DID" , |
| 97 | "PERFORMING" , |
| 98 | "RATELIMITING" , |
| 99 | "DONE" , |
| 100 | "COMPLETED" , |
| 101 | "MSGSENT" , |
| 102 | }; |
| 103 | #endif |
| 104 | |
| 105 | /* function pointer called once when switching TO a state */ |
| 106 | typedef void (*init_multistate_func)(struct Curl_easy *data); |
| 107 | |
| 108 | /* called in DID state, before PERFORMING state */ |
| 109 | static void before_perform(struct Curl_easy *data) |
| 110 | { |
| 111 | data->req.chunk = FALSE; |
| 112 | Curl_pgrsTime(data, TIMER_PRETRANSFER); |
| 113 | } |
| 114 | |
| 115 | static void init_completed(struct Curl_easy *data) |
| 116 | { |
| 117 | /* this is a completed transfer */ |
| 118 | |
| 119 | /* Important: reset the conn pointer so that we don't point to memory |
| 120 | that could be freed anytime */ |
| 121 | Curl_detach_connnection(data); |
| 122 | Curl_expire_clear(data); /* stop all timers */ |
| 123 | } |
| 124 | |
| 125 | /* always use this function to change state, to make debugging easier */ |
| 126 | static void mstate(struct Curl_easy *data, CURLMstate state |
| 127 | #ifdef DEBUGBUILD |
| 128 | , int lineno |
| 129 | #endif |
| 130 | ) |
| 131 | { |
| 132 | CURLMstate oldstate = data->mstate; |
| 133 | static const init_multistate_func finit[MSTATE_LAST] = { |
| 134 | NULL, /* INIT */ |
| 135 | NULL, /* PENDING */ |
| 136 | Curl_init_CONNECT, /* CONNECT */ |
| 137 | NULL, /* RESOLVING */ |
| 138 | NULL, /* CONNECTING */ |
| 139 | NULL, /* TUNNELING */ |
| 140 | NULL, /* PROTOCONNECT */ |
| 141 | NULL, /* PROTOCONNECTING */ |
| 142 | Curl_connect_free, /* DO */ |
| 143 | NULL, /* DOING */ |
| 144 | NULL, /* DOING_MORE */ |
| 145 | before_perform, /* DID */ |
| 146 | NULL, /* PERFORMING */ |
| 147 | NULL, /* RATELIMITING */ |
| 148 | NULL, /* DONE */ |
| 149 | init_completed, /* COMPLETED */ |
| 150 | NULL /* MSGSENT */ |
| 151 | }; |
| 152 | |
| 153 | #if defined(DEBUGBUILD) && defined(CURL_DISABLE_VERBOSE_STRINGS) |
| 154 | (void) lineno; |
| 155 | #endif |
| 156 | |
| 157 | if(oldstate == state) |
| 158 | /* don't bother when the new state is the same as the old state */ |
| 159 | return; |
| 160 | |
| 161 | data->mstate = state; |
| 162 | |
| 163 | #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS) |
| 164 | if(data->mstate >= MSTATE_PENDING && |
| 165 | data->mstate < MSTATE_COMPLETED) { |
| 166 | long connection_id = -5000; |
| 167 | |
| 168 | if(data->conn) |
| 169 | connection_id = data->conn->connection_id; |
| 170 | |
| 171 | infof(data, |
| 172 | "STATE: %s => %s handle %p; line %d (connection #%ld)" , |
| 173 | statename[oldstate], statename[data->mstate], |
| 174 | (void *)data, lineno, connection_id); |
| 175 | } |
| 176 | #endif |
| 177 | |
| 178 | if(state == MSTATE_COMPLETED) { |
| 179 | /* changing to COMPLETED means there's one less easy handle 'alive' */ |
| 180 | DEBUGASSERT(data->multi->num_alive > 0); |
| 181 | data->multi->num_alive--; |
| 182 | } |
| 183 | |
| 184 | /* if this state has an init-function, run it */ |
| 185 | if(finit[state]) |
| 186 | finit[state](data); |
| 187 | } |
| 188 | |
| 189 | #ifndef DEBUGBUILD |
| 190 | #define multistate(x,y) mstate(x,y) |
| 191 | #else |
| 192 | #define multistate(x,y) mstate(x,y, __LINE__) |
| 193 | #endif |
| 194 | |
| 195 | /* |
| 196 | * We add one of these structs to the sockhash for each socket |
| 197 | */ |
| 198 | |
| 199 | struct Curl_sh_entry { |
| 200 | struct Curl_hash transfers; /* hash of transfers using this socket */ |
| 201 | unsigned int action; /* what combined action READ/WRITE this socket waits |
| 202 | for */ |
| 203 | unsigned int users; /* number of transfers using this */ |
| 204 | void *socketp; /* settable by users with curl_multi_assign() */ |
| 205 | unsigned int readers; /* this many transfers want to read */ |
| 206 | unsigned int writers; /* this many transfers want to write */ |
| 207 | }; |
| 208 | /* bits for 'action' having no bits means this socket is not expecting any |
| 209 | action */ |
| 210 | #define SH_READ 1 |
| 211 | #define SH_WRITE 2 |
| 212 | |
| 213 | /* look up a given socket in the socket hash, skip invalid sockets */ |
| 214 | static struct Curl_sh_entry *sh_getentry(struct Curl_hash *sh, |
| 215 | curl_socket_t s) |
| 216 | { |
| 217 | if(s != CURL_SOCKET_BAD) { |
| 218 | /* only look for proper sockets */ |
| 219 | return Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t)); |
| 220 | } |
| 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | #define TRHASH_SIZE 13 |
| 225 | static size_t trhash(void *key, size_t key_length, size_t slots_num) |
| 226 | { |
| 227 | size_t keyval = (size_t)*(struct Curl_easy **)key; |
| 228 | (void) key_length; |
| 229 | |
| 230 | return (keyval % slots_num); |
| 231 | } |
| 232 | |
| 233 | static size_t trhash_compare(void *k1, size_t k1_len, void *k2, size_t k2_len) |
| 234 | { |
| 235 | (void)k1_len; |
| 236 | (void)k2_len; |
| 237 | |
| 238 | return *(struct Curl_easy **)k1 == *(struct Curl_easy **)k2; |
| 239 | } |
| 240 | |
| 241 | static void trhash_dtor(void *nada) |
| 242 | { |
| 243 | (void)nada; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /* make sure this socket is present in the hash for this handle */ |
| 248 | static struct Curl_sh_entry *sh_addentry(struct Curl_hash *sh, |
| 249 | curl_socket_t s) |
| 250 | { |
| 251 | struct Curl_sh_entry *there = sh_getentry(sh, s); |
| 252 | struct Curl_sh_entry *check; |
| 253 | |
| 254 | if(there) { |
| 255 | /* it is present, return fine */ |
| 256 | return there; |
| 257 | } |
| 258 | |
| 259 | /* not present, add it */ |
| 260 | check = calloc(1, sizeof(struct Curl_sh_entry)); |
| 261 | if(!check) |
| 262 | return NULL; /* major failure */ |
| 263 | |
| 264 | if(Curl_hash_init(&check->transfers, TRHASH_SIZE, trhash, |
| 265 | trhash_compare, trhash_dtor)) { |
| 266 | free(check); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | /* make/add new hash entry */ |
| 271 | if(!Curl_hash_add(sh, (char *)&s, sizeof(curl_socket_t), check)) { |
| 272 | Curl_hash_destroy(&check->transfers); |
| 273 | free(check); |
| 274 | return NULL; /* major failure */ |
| 275 | } |
| 276 | |
| 277 | return check; /* things are good in sockhash land */ |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /* delete the given socket + handle from the hash */ |
| 282 | static void sh_delentry(struct Curl_sh_entry *entry, |
| 283 | struct Curl_hash *sh, curl_socket_t s) |
| 284 | { |
| 285 | Curl_hash_destroy(&entry->transfers); |
| 286 | |
| 287 | /* We remove the hash entry. This will end up in a call to |
| 288 | sh_freeentry(). */ |
| 289 | Curl_hash_delete(sh, (char *)&s, sizeof(curl_socket_t)); |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * free a sockhash entry |
| 294 | */ |
| 295 | static void sh_freeentry(void *freethis) |
| 296 | { |
| 297 | struct Curl_sh_entry *p = (struct Curl_sh_entry *) freethis; |
| 298 | |
| 299 | free(p); |
| 300 | } |
| 301 | |
| 302 | static size_t fd_key_compare(void *k1, size_t k1_len, void *k2, size_t k2_len) |
| 303 | { |
| 304 | (void) k1_len; (void) k2_len; |
| 305 | |
| 306 | return (*((curl_socket_t *) k1)) == (*((curl_socket_t *) k2)); |
| 307 | } |
| 308 | |
| 309 | static size_t hash_fd(void *key, size_t key_length, size_t slots_num) |
| 310 | { |
| 311 | curl_socket_t fd = *((curl_socket_t *) key); |
| 312 | (void) key_length; |
| 313 | |
| 314 | return (fd % slots_num); |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * sh_init() creates a new socket hash and returns the handle for it. |
| 319 | * |
| 320 | * Quote from README.multi_socket: |
| 321 | * |
| 322 | * "Some tests at 7000 and 9000 connections showed that the socket hash lookup |
| 323 | * is somewhat of a bottle neck. Its current implementation may be a bit too |
| 324 | * limiting. It simply has a fixed-size array, and on each entry in the array |
| 325 | * it has a linked list with entries. So the hash only checks which list to |
| 326 | * scan through. The code I had used so for used a list with merely 7 slots |
| 327 | * (as that is what the DNS hash uses) but with 7000 connections that would |
| 328 | * make an average of 1000 nodes in each list to run through. I upped that to |
| 329 | * 97 slots (I believe a prime is suitable) and noticed a significant speed |
| 330 | * increase. I need to reconsider the hash implementation or use a rather |
| 331 | * large default value like this. At 9000 connections I was still below 10us |
| 332 | * per call." |
| 333 | * |
| 334 | */ |
| 335 | static int sh_init(struct Curl_hash *hash, int hashsize) |
| 336 | { |
| 337 | return Curl_hash_init(hash, hashsize, hash_fd, fd_key_compare, |
| 338 | sh_freeentry); |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * multi_addmsg() |
| 343 | * |
| 344 | * Called when a transfer is completed. Adds the given msg pointer to |
| 345 | * the list kept in the multi handle. |
| 346 | */ |
| 347 | static CURLMcode multi_addmsg(struct Curl_multi *multi, |
| 348 | struct Curl_message *msg) |
| 349 | { |
| 350 | Curl_llist_insert_next(&multi->msglist, multi->msglist.tail, msg, |
| 351 | &msg->list); |
| 352 | return CURLM_OK; |
| 353 | } |
| 354 | |
| 355 | struct Curl_multi *Curl_multi_handle(int hashsize, /* socket hash */ |
| 356 | int chashsize) /* connection hash */ |
| 357 | { |
| 358 | struct Curl_multi *multi = calloc(1, sizeof(struct Curl_multi)); |
| 359 | |
| 360 | if(!multi) |
| 361 | return NULL; |
| 362 | |
| 363 | multi->magic = CURL_MULTI_HANDLE; |
| 364 | |
| 365 | if(Curl_mk_dnscache(&multi->hostcache)) |
| 366 | goto error; |
| 367 | |
| 368 | if(sh_init(&multi->sockhash, hashsize)) |
| 369 | goto error; |
| 370 | |
| 371 | if(Curl_conncache_init(&multi->conn_cache, chashsize)) |
| 372 | goto error; |
| 373 | |
| 374 | Curl_llist_init(&multi->msglist, NULL); |
| 375 | Curl_llist_init(&multi->pending, NULL); |
| 376 | |
| 377 | multi->multiplexing = TRUE; |
| 378 | |
| 379 | /* -1 means it not set by user, use the default value */ |
| 380 | multi->maxconnects = -1; |
| 381 | multi->max_concurrent_streams = 100; |
| 382 | multi->ipv6_works = Curl_ipv6works(NULL); |
| 383 | |
| 384 | #ifdef USE_WINSOCK |
| 385 | multi->wsa_event = WSACreateEvent(); |
| 386 | if(multi->wsa_event == WSA_INVALID_EVENT) |
| 387 | goto error; |
| 388 | #else |
| 389 | #ifdef ENABLE_WAKEUP |
| 390 | if(Curl_socketpair(AF_UNIX, SOCK_STREAM, 0, multi->wakeup_pair) < 0) { |
| 391 | multi->wakeup_pair[0] = CURL_SOCKET_BAD; |
| 392 | multi->wakeup_pair[1] = CURL_SOCKET_BAD; |
| 393 | } |
| 394 | else if(curlx_nonblock(multi->wakeup_pair[0], TRUE) < 0 || |
| 395 | curlx_nonblock(multi->wakeup_pair[1], TRUE) < 0) { |
| 396 | sclose(multi->wakeup_pair[0]); |
| 397 | sclose(multi->wakeup_pair[1]); |
| 398 | multi->wakeup_pair[0] = CURL_SOCKET_BAD; |
| 399 | multi->wakeup_pair[1] = CURL_SOCKET_BAD; |
| 400 | } |
| 401 | #endif |
| 402 | #endif |
| 403 | |
| 404 | return multi; |
| 405 | |
| 406 | error: |
| 407 | |
| 408 | Curl_hash_destroy(&multi->sockhash); |
| 409 | Curl_hash_destroy(&multi->hostcache); |
| 410 | Curl_conncache_destroy(&multi->conn_cache); |
| 411 | Curl_llist_destroy(&multi->msglist, NULL); |
| 412 | Curl_llist_destroy(&multi->pending, NULL); |
| 413 | |
| 414 | free(multi); |
| 415 | return NULL; |
| 416 | } |
| 417 | |
| 418 | struct Curl_multi *curl_multi_init(void) |
| 419 | { |
| 420 | return Curl_multi_handle(CURL_SOCKET_HASH_TABLE_SIZE, |
| 421 | CURL_CONNECTION_HASH_SIZE); |
| 422 | } |
| 423 | |
| 424 | CURLMcode curl_multi_add_handle(struct Curl_multi *multi, |
| 425 | struct Curl_easy *data) |
| 426 | { |
| 427 | /* First, make some basic checks that the CURLM handle is a good handle */ |
| 428 | if(!GOOD_MULTI_HANDLE(multi)) |
| 429 | return CURLM_BAD_HANDLE; |
| 430 | |
| 431 | /* Verify that we got a somewhat good easy handle too */ |
| 432 | if(!GOOD_EASY_HANDLE(data)) |
| 433 | return CURLM_BAD_EASY_HANDLE; |
| 434 | |
| 435 | /* Prevent users from adding same easy handle more than once and prevent |
| 436 | adding to more than one multi stack */ |
| 437 | if(data->multi) |
| 438 | return CURLM_ADDED_ALREADY; |
| 439 | |
| 440 | if(multi->in_callback) |
| 441 | return CURLM_RECURSIVE_API_CALL; |
| 442 | |
| 443 | /* Initialize timeout list for this handle */ |
| 444 | Curl_llist_init(&data->state.timeoutlist, NULL); |
| 445 | |
| 446 | /* |
| 447 | * No failure allowed in this function beyond this point. And no |
| 448 | * modification of easy nor multi handle allowed before this except for |
| 449 | * potential multi's connection cache growing which won't be undone in this |
| 450 | * function no matter what. |
| 451 | */ |
| 452 | if(data->set.errorbuffer) |
| 453 | data->set.errorbuffer[0] = 0; |
| 454 | |
| 455 | /* set the easy handle */ |
| 456 | multistate(data, MSTATE_INIT); |
| 457 | |
| 458 | /* for multi interface connections, we share DNS cache automatically if the |
| 459 | easy handle's one is currently not set. */ |
| 460 | if(!data->dns.hostcache || |
| 461 | (data->dns.hostcachetype == HCACHE_NONE)) { |
| 462 | data->dns.hostcache = &multi->hostcache; |
| 463 | data->dns.hostcachetype = HCACHE_MULTI; |
| 464 | } |
| 465 | |
| 466 | /* Point to the shared or multi handle connection cache */ |
| 467 | if(data->share && (data->share->specifier & (1<< CURL_LOCK_DATA_CONNECT))) |
| 468 | data->state.conn_cache = &data->share->conn_cache; |
| 469 | else |
| 470 | data->state.conn_cache = &multi->conn_cache; |
| 471 | data->state.lastconnect_id = -1; |
| 472 | |
| 473 | #ifdef USE_LIBPSL |
| 474 | /* Do the same for PSL. */ |
| 475 | if(data->share && (data->share->specifier & (1 << CURL_LOCK_DATA_PSL))) |
| 476 | data->psl = &data->share->psl; |
| 477 | else |
| 478 | data->psl = &multi->psl; |
| 479 | #endif |
| 480 | |
| 481 | /* We add the new entry last in the list. */ |
| 482 | data->next = NULL; /* end of the line */ |
| 483 | if(multi->easyp) { |
| 484 | struct Curl_easy *last = multi->easylp; |
| 485 | last->next = data; |
| 486 | data->prev = last; |
| 487 | multi->easylp = data; /* the new last node */ |
| 488 | } |
| 489 | else { |
| 490 | /* first node, make prev NULL! */ |
| 491 | data->prev = NULL; |
| 492 | multi->easylp = multi->easyp = data; /* both first and last */ |
| 493 | } |
| 494 | |
| 495 | /* make the Curl_easy refer back to this multi handle */ |
| 496 | data->multi = multi; |
| 497 | |
| 498 | /* Set the timeout for this handle to expire really soon so that it will |
| 499 | be taken care of even when this handle is added in the midst of operation |
| 500 | when only the curl_multi_socket() API is used. During that flow, only |
| 501 | sockets that time-out or have actions will be dealt with. Since this |
| 502 | handle has no action yet, we make sure it times out to get things to |
| 503 | happen. */ |
| 504 | Curl_expire(data, 0, EXPIRE_RUN_NOW); |
| 505 | |
| 506 | /* increase the node-counter */ |
| 507 | multi->num_easy++; |
| 508 | |
| 509 | /* increase the alive-counter */ |
| 510 | multi->num_alive++; |
| 511 | |
| 512 | /* A somewhat crude work-around for a little glitch in Curl_update_timer() |
| 513 | that happens if the lastcall time is set to the same time when the handle |
| 514 | is removed as when the next handle is added, as then the check in |
| 515 | Curl_update_timer() that prevents calling the application multiple times |
| 516 | with the same timer info will not trigger and then the new handle's |
| 517 | timeout will not be notified to the app. |
| 518 | |
| 519 | The work-around is thus simply to clear the 'lastcall' variable to force |
| 520 | Curl_update_timer() to always trigger a callback to the app when a new |
| 521 | easy handle is added */ |
| 522 | memset(&multi->timer_lastcall, 0, sizeof(multi->timer_lastcall)); |
| 523 | |
| 524 | CONNCACHE_LOCK(data); |
| 525 | /* The closure handle only ever has default timeouts set. To improve the |
| 526 | state somewhat we clone the timeouts from each added handle so that the |
| 527 | closure handle always has the same timeouts as the most recently added |
| 528 | easy handle. */ |
| 529 | data->state.conn_cache->closure_handle->set.timeout = data->set.timeout; |
| 530 | data->state.conn_cache->closure_handle->set.server_response_timeout = |
| 531 | data->set.server_response_timeout; |
| 532 | data->state.conn_cache->closure_handle->set.no_signal = |
| 533 | data->set.no_signal; |
| 534 | CONNCACHE_UNLOCK(data); |
| 535 | |
| 536 | Curl_update_timer(multi); |
| 537 | return CURLM_OK; |
| 538 | } |
| 539 | |
| 540 | #if 0 |
| 541 | /* Debug-function, used like this: |
| 542 | * |
| 543 | * Curl_hash_print(multi->sockhash, debug_print_sock_hash); |
| 544 | * |
| 545 | * Enable the hash print function first by editing hash.c |
| 546 | */ |
| 547 | static void debug_print_sock_hash(void *p) |
| 548 | { |
| 549 | struct Curl_sh_entry *sh = (struct Curl_sh_entry *)p; |
| 550 | |
| 551 | fprintf(stderr, " [easy %p/magic %x/socket %d]" , |
| 552 | (void *)sh->data, sh->data->magic, (int)sh->socket); |
| 553 | } |
| 554 | #endif |
| 555 | |
| 556 | static CURLcode multi_done(struct Curl_easy *data, |
| 557 | CURLcode status, /* an error if this is called |
| 558 | after an error was detected */ |
| 559 | bool premature) |
| 560 | { |
| 561 | CURLcode result; |
| 562 | struct connectdata *conn = data->conn; |
| 563 | unsigned int i; |
| 564 | |
| 565 | DEBUGF(infof(data, "multi_done" )); |
| 566 | |
| 567 | if(data->state.done) |
| 568 | /* Stop if multi_done() has already been called */ |
| 569 | return CURLE_OK; |
| 570 | |
| 571 | /* Stop the resolver and free its own resources (but not dns_entry yet). */ |
| 572 | Curl_resolver_kill(data); |
| 573 | |
| 574 | /* Cleanup possible redirect junk */ |
| 575 | Curl_safefree(data->req.newurl); |
| 576 | Curl_safefree(data->req.location); |
| 577 | |
| 578 | switch(status) { |
| 579 | case CURLE_ABORTED_BY_CALLBACK: |
| 580 | case CURLE_READ_ERROR: |
| 581 | case CURLE_WRITE_ERROR: |
| 582 | /* When we're aborted due to a callback return code it basically have to |
| 583 | be counted as premature as there is trouble ahead if we don't. We have |
| 584 | many callbacks and protocols work differently, we could potentially do |
| 585 | this more fine-grained in the future. */ |
| 586 | premature = TRUE; |
| 587 | default: |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | /* this calls the protocol-specific function pointer previously set */ |
| 592 | if(conn->handler->done) |
| 593 | result = conn->handler->done(data, status, premature); |
| 594 | else |
| 595 | result = status; |
| 596 | |
| 597 | if(CURLE_ABORTED_BY_CALLBACK != result) { |
| 598 | /* avoid this if we already aborted by callback to avoid this calling |
| 599 | another callback */ |
| 600 | CURLcode rc = Curl_pgrsDone(data); |
| 601 | if(!result && rc) |
| 602 | result = CURLE_ABORTED_BY_CALLBACK; |
| 603 | } |
| 604 | |
| 605 | process_pending_handles(data->multi); /* connection / multiplex */ |
| 606 | |
| 607 | CONNCACHE_LOCK(data); |
| 608 | Curl_detach_connnection(data); |
| 609 | if(CONN_INUSE(conn)) { |
| 610 | /* Stop if still used. */ |
| 611 | CONNCACHE_UNLOCK(data); |
| 612 | DEBUGF(infof(data, "Connection still in use %zu, " |
| 613 | "no more multi_done now!" , |
| 614 | conn->easyq.size)); |
| 615 | return CURLE_OK; |
| 616 | } |
| 617 | |
| 618 | data->state.done = TRUE; /* called just now! */ |
| 619 | |
| 620 | if(conn->dns_entry) { |
| 621 | Curl_resolv_unlock(data, conn->dns_entry); /* done with this */ |
| 622 | conn->dns_entry = NULL; |
| 623 | } |
| 624 | Curl_hostcache_prune(data); |
| 625 | Curl_safefree(data->state.ulbuf); |
| 626 | |
| 627 | /* if the transfer was completed in a paused state there can be buffered |
| 628 | data left to free */ |
| 629 | for(i = 0; i < data->state.tempcount; i++) { |
| 630 | Curl_dyn_free(&data->state.tempwrite[i].b); |
| 631 | } |
| 632 | data->state.tempcount = 0; |
| 633 | |
| 634 | /* if data->set.reuse_forbid is TRUE, it means the libcurl client has |
| 635 | forced us to close this connection. This is ignored for requests taking |
| 636 | place in a NTLM/NEGOTIATE authentication handshake |
| 637 | |
| 638 | if conn->bits.close is TRUE, it means that the connection should be |
| 639 | closed in spite of all our efforts to be nice, due to protocol |
| 640 | restrictions in our or the server's end |
| 641 | |
| 642 | if premature is TRUE, it means this connection was said to be DONE before |
| 643 | the entire request operation is complete and thus we can't know in what |
| 644 | state it is for re-using, so we're forced to close it. In a perfect world |
| 645 | we can add code that keep track of if we really must close it here or not, |
| 646 | but currently we have no such detail knowledge. |
| 647 | */ |
| 648 | |
| 649 | if((data->set.reuse_forbid |
| 650 | #if defined(USE_NTLM) |
| 651 | && !(conn->http_ntlm_state == NTLMSTATE_TYPE2 || |
| 652 | conn->proxy_ntlm_state == NTLMSTATE_TYPE2) |
| 653 | #endif |
| 654 | #if defined(USE_SPNEGO) |
| 655 | && !(conn->http_negotiate_state == GSS_AUTHRECV || |
| 656 | conn->proxy_negotiate_state == GSS_AUTHRECV) |
| 657 | #endif |
| 658 | ) || conn->bits.close |
| 659 | || (premature && !(conn->handler->flags & PROTOPT_STREAM))) { |
| 660 | CURLcode res2; |
| 661 | connclose(conn, "disconnecting" ); |
| 662 | Curl_conncache_remove_conn(data, conn, FALSE); |
| 663 | CONNCACHE_UNLOCK(data); |
| 664 | res2 = Curl_disconnect(data, conn, premature); |
| 665 | |
| 666 | /* If we had an error already, make sure we return that one. But |
| 667 | if we got a new error, return that. */ |
| 668 | if(!result && res2) |
| 669 | result = res2; |
| 670 | } |
| 671 | else { |
| 672 | char buffer[256]; |
| 673 | const char *host = |
| 674 | #ifndef CURL_DISABLE_PROXY |
| 675 | conn->bits.socksproxy ? |
| 676 | conn->socks_proxy.host.dispname : |
| 677 | conn->bits.httpproxy ? conn->http_proxy.host.dispname : |
| 678 | #endif |
| 679 | conn->bits.conn_to_host ? conn->conn_to_host.dispname : |
| 680 | conn->host.dispname; |
| 681 | /* create string before returning the connection */ |
| 682 | msnprintf(buffer, sizeof(buffer), |
| 683 | "Connection #%ld to host %s left intact" , |
| 684 | conn->connection_id, host); |
| 685 | /* the connection is no longer in use by this transfer */ |
| 686 | CONNCACHE_UNLOCK(data); |
| 687 | if(Curl_conncache_return_conn(data, conn)) { |
| 688 | /* remember the most recently used connection */ |
| 689 | data->state.lastconnect_id = conn->connection_id; |
| 690 | infof(data, "%s" , buffer); |
| 691 | } |
| 692 | else |
| 693 | data->state.lastconnect_id = -1; |
| 694 | } |
| 695 | |
| 696 | Curl_safefree(data->state.buffer); |
| 697 | Curl_free_request_state(data); |
| 698 | return result; |
| 699 | } |
| 700 | |
| 701 | static int close_connect_only(struct Curl_easy *data, |
| 702 | struct connectdata *conn, void *param) |
| 703 | { |
| 704 | (void)param; |
| 705 | if(data->state.lastconnect_id != conn->connection_id) |
| 706 | return 0; |
| 707 | |
| 708 | if(!conn->bits.connect_only) |
| 709 | return 1; |
| 710 | |
| 711 | connclose(conn, "Removing connect-only easy handle" ); |
| 712 | |
| 713 | return 1; |
| 714 | } |
| 715 | |
| 716 | CURLMcode curl_multi_remove_handle(struct Curl_multi *multi, |
| 717 | struct Curl_easy *data) |
| 718 | { |
| 719 | struct Curl_easy *easy = data; |
| 720 | bool premature; |
| 721 | struct Curl_llist_element *e; |
| 722 | |
| 723 | /* First, make some basic checks that the CURLM handle is a good handle */ |
| 724 | if(!GOOD_MULTI_HANDLE(multi)) |
| 725 | return CURLM_BAD_HANDLE; |
| 726 | |
| 727 | /* Verify that we got a somewhat good easy handle too */ |
| 728 | if(!GOOD_EASY_HANDLE(data)) |
| 729 | return CURLM_BAD_EASY_HANDLE; |
| 730 | |
| 731 | /* Prevent users from trying to remove same easy handle more than once */ |
| 732 | if(!data->multi) |
| 733 | return CURLM_OK; /* it is already removed so let's say it is fine! */ |
| 734 | |
| 735 | /* Prevent users from trying to remove an easy handle from the wrong multi */ |
| 736 | if(data->multi != multi) |
| 737 | return CURLM_BAD_EASY_HANDLE; |
| 738 | |
| 739 | if(multi->in_callback) |
| 740 | return CURLM_RECURSIVE_API_CALL; |
| 741 | |
| 742 | premature = (data->mstate < MSTATE_COMPLETED) ? TRUE : FALSE; |
| 743 | |
| 744 | /* If the 'state' is not INIT or COMPLETED, we might need to do something |
| 745 | nice to put the easy_handle in a good known state when this returns. */ |
| 746 | if(premature) { |
| 747 | /* this handle is "alive" so we need to count down the total number of |
| 748 | alive connections when this is removed */ |
| 749 | multi->num_alive--; |
| 750 | } |
| 751 | |
| 752 | if(data->conn && |
| 753 | data->mstate > MSTATE_DO && |
| 754 | data->mstate < MSTATE_COMPLETED) { |
| 755 | /* Set connection owner so that the DONE function closes it. We can |
| 756 | safely do this here since connection is killed. */ |
| 757 | streamclose(data->conn, "Removed with partial response" ); |
| 758 | } |
| 759 | |
| 760 | if(data->conn) { |
| 761 | /* multi_done() clears the association between the easy handle and the |
| 762 | connection. |
| 763 | |
| 764 | Note that this ignores the return code simply because there's |
| 765 | nothing really useful to do with it anyway! */ |
| 766 | (void)multi_done(data, data->result, premature); |
| 767 | } |
| 768 | |
| 769 | /* The timer must be shut down before data->multi is set to NULL, else the |
| 770 | timenode will remain in the splay tree after curl_easy_cleanup is |
| 771 | called. Do it after multi_done() in case that sets another time! */ |
| 772 | Curl_expire_clear(data); |
| 773 | |
| 774 | if(data->connect_queue.ptr) |
| 775 | /* the handle was in the pending list waiting for an available connection, |
| 776 | so go ahead and remove it */ |
| 777 | Curl_llist_remove(&multi->pending, &data->connect_queue, NULL); |
| 778 | |
| 779 | if(data->dns.hostcachetype == HCACHE_MULTI) { |
| 780 | /* stop using the multi handle's DNS cache, *after* the possible |
| 781 | multi_done() call above */ |
| 782 | data->dns.hostcache = NULL; |
| 783 | data->dns.hostcachetype = HCACHE_NONE; |
| 784 | } |
| 785 | |
| 786 | Curl_wildcard_dtor(&data->wildcard); |
| 787 | |
| 788 | /* destroy the timeout list that is held in the easy handle, do this *after* |
| 789 | multi_done() as that may actually call Curl_expire that uses this */ |
| 790 | Curl_llist_destroy(&data->state.timeoutlist, NULL); |
| 791 | |
| 792 | /* change state without using multistate(), only to make singlesocket() do |
| 793 | what we want */ |
| 794 | data->mstate = MSTATE_COMPLETED; |
| 795 | singlesocket(multi, easy); /* to let the application know what sockets that |
| 796 | vanish with this handle */ |
| 797 | |
| 798 | /* Remove the association between the connection and the handle */ |
| 799 | Curl_detach_connnection(data); |
| 800 | |
| 801 | if(data->state.lastconnect_id != -1) { |
| 802 | /* Mark any connect-only connection for closure */ |
| 803 | Curl_conncache_foreach(data, data->state.conn_cache, |
| 804 | NULL, close_connect_only); |
| 805 | } |
| 806 | |
| 807 | #ifdef USE_LIBPSL |
| 808 | /* Remove the PSL association. */ |
| 809 | if(data->psl == &multi->psl) |
| 810 | data->psl = NULL; |
| 811 | #endif |
| 812 | |
| 813 | /* as this was using a shared connection cache we clear the pointer to that |
| 814 | since we're not part of that multi handle anymore */ |
| 815 | data->state.conn_cache = NULL; |
| 816 | |
| 817 | data->multi = NULL; /* clear the association to this multi handle */ |
| 818 | |
| 819 | /* make sure there's no pending message in the queue sent from this easy |
| 820 | handle */ |
| 821 | |
| 822 | for(e = multi->msglist.head; e; e = e->next) { |
| 823 | struct Curl_message *msg = e->ptr; |
| 824 | |
| 825 | if(msg->extmsg.easy_handle == easy) { |
| 826 | Curl_llist_remove(&multi->msglist, e, NULL); |
| 827 | /* there can only be one from this specific handle */ |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | /* Remove from the pending list if it is there. Otherwise this will |
| 833 | remain on the pending list forever due to the state change. */ |
| 834 | for(e = multi->pending.head; e; e = e->next) { |
| 835 | struct Curl_easy *curr_data = e->ptr; |
| 836 | |
| 837 | if(curr_data == data) { |
| 838 | Curl_llist_remove(&multi->pending, e, NULL); |
| 839 | break; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | /* make the previous node point to our next */ |
| 844 | if(data->prev) |
| 845 | data->prev->next = data->next; |
| 846 | else |
| 847 | multi->easyp = data->next; /* point to first node */ |
| 848 | |
| 849 | /* make our next point to our previous node */ |
| 850 | if(data->next) |
| 851 | data->next->prev = data->prev; |
| 852 | else |
| 853 | multi->easylp = data->prev; /* point to last node */ |
| 854 | |
| 855 | /* NOTE NOTE NOTE |
| 856 | We do not touch the easy handle here! */ |
| 857 | multi->num_easy--; /* one less to care about now */ |
| 858 | |
| 859 | process_pending_handles(multi); |
| 860 | |
| 861 | Curl_update_timer(multi); |
| 862 | return CURLM_OK; |
| 863 | } |
| 864 | |
| 865 | /* Return TRUE if the application asked for multiplexing */ |
| 866 | bool Curl_multiplex_wanted(const struct Curl_multi *multi) |
| 867 | { |
| 868 | return (multi && (multi->multiplexing)); |
| 869 | } |
| 870 | |
| 871 | /* |
| 872 | * Curl_detach_connnection() removes the given transfer from the connection. |
| 873 | * |
| 874 | * This is the only function that should clear data->conn. This will |
| 875 | * occasionally be called with the data->conn pointer already cleared. |
| 876 | */ |
| 877 | void Curl_detach_connnection(struct Curl_easy *data) |
| 878 | { |
| 879 | struct connectdata *conn = data->conn; |
| 880 | if(conn) { |
| 881 | Curl_llist_remove(&conn->easyq, &data->conn_queue, NULL); |
| 882 | Curl_ssl_detach_conn(data, conn); |
| 883 | } |
| 884 | data->conn = NULL; |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * Curl_attach_connnection() attaches this transfer to this connection. |
| 889 | * |
| 890 | * This is the only function that should assign data->conn |
| 891 | */ |
| 892 | void Curl_attach_connnection(struct Curl_easy *data, |
| 893 | struct connectdata *conn) |
| 894 | { |
| 895 | DEBUGASSERT(!data->conn); |
| 896 | DEBUGASSERT(conn); |
| 897 | data->conn = conn; |
| 898 | Curl_llist_insert_next(&conn->easyq, conn->easyq.tail, data, |
| 899 | &data->conn_queue); |
| 900 | if(conn->handler->attach) |
| 901 | conn->handler->attach(data, conn); |
| 902 | Curl_ssl_associate_conn(data, conn); |
| 903 | } |
| 904 | |
| 905 | static int waitconnect_getsock(struct connectdata *conn, |
| 906 | curl_socket_t *sock) |
| 907 | { |
| 908 | int i; |
| 909 | int s = 0; |
| 910 | int rc = 0; |
| 911 | |
| 912 | #ifdef USE_SSL |
| 913 | #ifndef CURL_DISABLE_PROXY |
| 914 | if(CONNECT_FIRSTSOCKET_PROXY_SSL()) |
| 915 | return Curl_ssl->getsock(conn, sock); |
| 916 | #endif |
| 917 | #endif |
| 918 | |
| 919 | if(SOCKS_STATE(conn->cnnct.state)) |
| 920 | return Curl_SOCKS_getsock(conn, sock, FIRSTSOCKET); |
| 921 | |
| 922 | for(i = 0; i<2; i++) { |
| 923 | if(conn->tempsock[i] != CURL_SOCKET_BAD) { |
| 924 | sock[s] = conn->tempsock[i]; |
| 925 | rc |= GETSOCK_WRITESOCK(s); |
| 926 | #ifdef ENABLE_QUIC |
| 927 | if(conn->transport == TRNSPRT_QUIC) |
| 928 | /* when connecting QUIC, we want to read the socket too */ |
| 929 | rc |= GETSOCK_READSOCK(s); |
| 930 | #endif |
| 931 | s++; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | return rc; |
| 936 | } |
| 937 | |
| 938 | static int waitproxyconnect_getsock(struct connectdata *conn, |
| 939 | curl_socket_t *sock) |
| 940 | { |
| 941 | sock[0] = conn->sock[FIRSTSOCKET]; |
| 942 | |
| 943 | if(conn->connect_state) |
| 944 | return Curl_connect_getsock(conn); |
| 945 | |
| 946 | return GETSOCK_WRITESOCK(0); |
| 947 | } |
| 948 | |
| 949 | static int domore_getsock(struct Curl_easy *data, |
| 950 | struct connectdata *conn, |
| 951 | curl_socket_t *socks) |
| 952 | { |
| 953 | if(conn && conn->handler->domore_getsock) |
| 954 | return conn->handler->domore_getsock(data, conn, socks); |
| 955 | return GETSOCK_BLANK; |
| 956 | } |
| 957 | |
| 958 | static int doing_getsock(struct Curl_easy *data, |
| 959 | struct connectdata *conn, |
| 960 | curl_socket_t *socks) |
| 961 | { |
| 962 | if(conn && conn->handler->doing_getsock) |
| 963 | return conn->handler->doing_getsock(data, conn, socks); |
| 964 | return GETSOCK_BLANK; |
| 965 | } |
| 966 | |
| 967 | static int protocol_getsock(struct Curl_easy *data, |
| 968 | struct connectdata *conn, |
| 969 | curl_socket_t *socks) |
| 970 | { |
| 971 | if(conn->handler->proto_getsock) |
| 972 | return conn->handler->proto_getsock(data, conn, socks); |
| 973 | /* Backup getsock logic. Since there is a live socket in use, we must wait |
| 974 | for it or it will be removed from watching when the multi_socket API is |
| 975 | used. */ |
| 976 | socks[0] = conn->sock[FIRSTSOCKET]; |
| 977 | return GETSOCK_READSOCK(0) | GETSOCK_WRITESOCK(0); |
| 978 | } |
| 979 | |
| 980 | /* returns bitmapped flags for this handle and its sockets. The 'socks[]' |
| 981 | array contains MAX_SOCKSPEREASYHANDLE entries. */ |
| 982 | static int multi_getsock(struct Curl_easy *data, |
| 983 | curl_socket_t *socks) |
| 984 | { |
| 985 | struct connectdata *conn = data->conn; |
| 986 | /* The no connection case can happen when this is called from |
| 987 | curl_multi_remove_handle() => singlesocket() => multi_getsock(). |
| 988 | */ |
| 989 | if(!conn) |
| 990 | return 0; |
| 991 | |
| 992 | switch(data->mstate) { |
| 993 | default: |
| 994 | return 0; |
| 995 | |
| 996 | case MSTATE_RESOLVING: |
| 997 | return Curl_resolv_getsock(data, socks); |
| 998 | |
| 999 | case MSTATE_PROTOCONNECTING: |
| 1000 | case MSTATE_PROTOCONNECT: |
| 1001 | return protocol_getsock(data, conn, socks); |
| 1002 | |
| 1003 | case MSTATE_DO: |
| 1004 | case MSTATE_DOING: |
| 1005 | return doing_getsock(data, conn, socks); |
| 1006 | |
| 1007 | case MSTATE_TUNNELING: |
| 1008 | return waitproxyconnect_getsock(conn, socks); |
| 1009 | |
| 1010 | case MSTATE_CONNECTING: |
| 1011 | return waitconnect_getsock(conn, socks); |
| 1012 | |
| 1013 | case MSTATE_DOING_MORE: |
| 1014 | return domore_getsock(data, conn, socks); |
| 1015 | |
| 1016 | case MSTATE_DID: /* since is set after DO is completed, we switch to |
| 1017 | waiting for the same as the PERFORMING state */ |
| 1018 | case MSTATE_PERFORMING: |
| 1019 | return Curl_single_getsock(data, conn, socks); |
| 1020 | } |
| 1021 | |
| 1022 | } |
| 1023 | |
| 1024 | CURLMcode curl_multi_fdset(struct Curl_multi *multi, |
| 1025 | fd_set *read_fd_set, fd_set *write_fd_set, |
| 1026 | fd_set *exc_fd_set, int *max_fd) |
| 1027 | { |
| 1028 | /* Scan through all the easy handles to get the file descriptors set. |
| 1029 | Some easy handles may not have connected to the remote host yet, |
| 1030 | and then we must make sure that is done. */ |
| 1031 | struct Curl_easy *data; |
| 1032 | int this_max_fd = -1; |
| 1033 | curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE]; |
| 1034 | int i; |
| 1035 | (void)exc_fd_set; /* not used */ |
| 1036 | |
| 1037 | if(!GOOD_MULTI_HANDLE(multi)) |
| 1038 | return CURLM_BAD_HANDLE; |
| 1039 | |
| 1040 | if(multi->in_callback) |
| 1041 | return CURLM_RECURSIVE_API_CALL; |
| 1042 | |
| 1043 | data = multi->easyp; |
| 1044 | while(data) { |
| 1045 | int bitmap; |
| 1046 | #ifdef __clang_analyzer_ |
| 1047 | /* to prevent "The left operand of '>=' is a garbage value" warnings */ |
| 1048 | memset(sockbunch, 0, sizeof(sockbunch)); |
| 1049 | #endif |
| 1050 | bitmap = multi_getsock(data, sockbunch); |
| 1051 | |
| 1052 | for(i = 0; i< MAX_SOCKSPEREASYHANDLE; i++) { |
| 1053 | curl_socket_t s = CURL_SOCKET_BAD; |
| 1054 | |
| 1055 | if((bitmap & GETSOCK_READSOCK(i)) && VALID_SOCK(sockbunch[i])) { |
| 1056 | if(!FDSET_SOCK(sockbunch[i])) |
| 1057 | /* pretend it doesn't exist */ |
| 1058 | continue; |
| 1059 | FD_SET(sockbunch[i], read_fd_set); |
| 1060 | s = sockbunch[i]; |
| 1061 | } |
| 1062 | if((bitmap & GETSOCK_WRITESOCK(i)) && VALID_SOCK(sockbunch[i])) { |
| 1063 | if(!FDSET_SOCK(sockbunch[i])) |
| 1064 | /* pretend it doesn't exist */ |
| 1065 | continue; |
| 1066 | FD_SET(sockbunch[i], write_fd_set); |
| 1067 | s = sockbunch[i]; |
| 1068 | } |
| 1069 | if(s == CURL_SOCKET_BAD) |
| 1070 | /* this socket is unused, break out of loop */ |
| 1071 | break; |
| 1072 | if((int)s > this_max_fd) |
| 1073 | this_max_fd = (int)s; |
| 1074 | } |
| 1075 | |
| 1076 | data = data->next; /* check next handle */ |
| 1077 | } |
| 1078 | |
| 1079 | *max_fd = this_max_fd; |
| 1080 | |
| 1081 | return CURLM_OK; |
| 1082 | } |
| 1083 | |
| 1084 | #define NUM_POLLS_ON_STACK 10 |
| 1085 | |
| 1086 | static CURLMcode multi_wait(struct Curl_multi *multi, |
| 1087 | struct curl_waitfd [], |
| 1088 | unsigned int , |
| 1089 | int timeout_ms, |
| 1090 | int *ret, |
| 1091 | bool , /* when no socket, wait */ |
| 1092 | bool use_wakeup) |
| 1093 | { |
| 1094 | struct Curl_easy *data; |
| 1095 | curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE]; |
| 1096 | int bitmap; |
| 1097 | unsigned int i; |
| 1098 | unsigned int nfds = 0; |
| 1099 | unsigned int curlfds; |
| 1100 | long timeout_internal; |
| 1101 | int retcode = 0; |
| 1102 | struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK]; |
| 1103 | struct pollfd *ufds = &a_few_on_stack[0]; |
| 1104 | bool ufds_malloc = FALSE; |
| 1105 | #ifdef USE_WINSOCK |
| 1106 | WSANETWORKEVENTS wsa_events; |
| 1107 | DEBUGASSERT(multi->wsa_event != WSA_INVALID_EVENT); |
| 1108 | #endif |
| 1109 | #ifndef ENABLE_WAKEUP |
| 1110 | (void)use_wakeup; |
| 1111 | #endif |
| 1112 | |
| 1113 | if(!GOOD_MULTI_HANDLE(multi)) |
| 1114 | return CURLM_BAD_HANDLE; |
| 1115 | |
| 1116 | if(multi->in_callback) |
| 1117 | return CURLM_RECURSIVE_API_CALL; |
| 1118 | |
| 1119 | if(timeout_ms < 0) |
| 1120 | return CURLM_BAD_FUNCTION_ARGUMENT; |
| 1121 | |
| 1122 | /* Count up how many fds we have from the multi handle */ |
| 1123 | data = multi->easyp; |
| 1124 | while(data) { |
| 1125 | bitmap = multi_getsock(data, sockbunch); |
| 1126 | |
| 1127 | for(i = 0; i< MAX_SOCKSPEREASYHANDLE; i++) { |
| 1128 | curl_socket_t s = CURL_SOCKET_BAD; |
| 1129 | |
| 1130 | if((bitmap & GETSOCK_READSOCK(i)) && VALID_SOCK((sockbunch[i]))) { |
| 1131 | ++nfds; |
| 1132 | s = sockbunch[i]; |
| 1133 | } |
| 1134 | if((bitmap & GETSOCK_WRITESOCK(i)) && VALID_SOCK((sockbunch[i]))) { |
| 1135 | ++nfds; |
| 1136 | s = sockbunch[i]; |
| 1137 | } |
| 1138 | if(s == CURL_SOCKET_BAD) { |
| 1139 | break; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | data = data->next; /* check next handle */ |
| 1144 | } |
| 1145 | |
| 1146 | /* If the internally desired timeout is actually shorter than requested from |
| 1147 | the outside, then use the shorter time! But only if the internal timer |
| 1148 | is actually larger than -1! */ |
| 1149 | (void)multi_timeout(multi, &timeout_internal); |
| 1150 | if((timeout_internal >= 0) && (timeout_internal < (long)timeout_ms)) |
| 1151 | timeout_ms = (int)timeout_internal; |
| 1152 | |
| 1153 | curlfds = nfds; /* number of internal file descriptors */ |
| 1154 | nfds += extra_nfds; /* add the externally provided ones */ |
| 1155 | |
| 1156 | #ifdef ENABLE_WAKEUP |
| 1157 | #ifdef USE_WINSOCK |
| 1158 | if(use_wakeup) { |
| 1159 | #else |
| 1160 | if(use_wakeup && multi->wakeup_pair[0] != CURL_SOCKET_BAD) { |
| 1161 | #endif |
| 1162 | ++nfds; |
| 1163 | } |
| 1164 | #endif |
| 1165 | |
| 1166 | if(nfds > NUM_POLLS_ON_STACK) { |
| 1167 | /* 'nfds' is a 32 bit value and 'struct pollfd' is typically 8 bytes |
| 1168 | big, so at 2^29 sockets this value might wrap. When a process gets |
| 1169 | the capability to actually handle over 500 million sockets this |
| 1170 | calculation needs a integer overflow check. */ |
| 1171 | ufds = malloc(nfds * sizeof(struct pollfd)); |
| 1172 | if(!ufds) |
| 1173 | return CURLM_OUT_OF_MEMORY; |
| 1174 | ufds_malloc = TRUE; |
| 1175 | } |
| 1176 | nfds = 0; |
| 1177 | |
| 1178 | /* only do the second loop if we found descriptors in the first stage run |
| 1179 | above */ |
| 1180 | |
| 1181 | if(curlfds) { |
| 1182 | /* Add the curl handles to our pollfds first */ |
| 1183 | data = multi->easyp; |
| 1184 | while(data) { |
| 1185 | bitmap = multi_getsock(data, sockbunch); |
| 1186 | |
| 1187 | for(i = 0; i < MAX_SOCKSPEREASYHANDLE; i++) { |
| 1188 | curl_socket_t s = CURL_SOCKET_BAD; |
| 1189 | #ifdef USE_WINSOCK |
| 1190 | long mask = 0; |
| 1191 | #endif |
| 1192 | if((bitmap & GETSOCK_READSOCK(i)) && VALID_SOCK((sockbunch[i]))) { |
| 1193 | s = sockbunch[i]; |
| 1194 | #ifdef USE_WINSOCK |
| 1195 | mask |= FD_READ|FD_ACCEPT|FD_CLOSE; |
| 1196 | #endif |
| 1197 | ufds[nfds].fd = s; |
| 1198 | ufds[nfds].events = POLLIN; |
| 1199 | ++nfds; |
| 1200 | } |
| 1201 | if((bitmap & GETSOCK_WRITESOCK(i)) && VALID_SOCK((sockbunch[i]))) { |
| 1202 | s = sockbunch[i]; |
| 1203 | #ifdef USE_WINSOCK |
| 1204 | mask |= FD_WRITE|FD_CONNECT|FD_CLOSE; |
| 1205 | send(s, NULL, 0, 0); /* reset FD_WRITE */ |
| 1206 | #endif |
| 1207 | ufds[nfds].fd = s; |
| 1208 | ufds[nfds].events = POLLOUT; |
| 1209 | ++nfds; |
| 1210 | } |
| 1211 | /* s is only set if either being readable or writable is checked */ |
| 1212 | if(s == CURL_SOCKET_BAD) { |
| 1213 | /* break on entry not checked for being readable or writable */ |
| 1214 | break; |
| 1215 | } |
| 1216 | #ifdef USE_WINSOCK |
| 1217 | if(WSAEventSelect(s, multi->wsa_event, mask) != 0) { |
| 1218 | if(ufds_malloc) |
| 1219 | free(ufds); |
| 1220 | return CURLM_INTERNAL_ERROR; |
| 1221 | } |
| 1222 | #endif |
| 1223 | } |
| 1224 | |
| 1225 | data = data->next; /* check next handle */ |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | /* Add external file descriptions from poll-like struct curl_waitfd */ |
| 1230 | for(i = 0; i < extra_nfds; i++) { |
| 1231 | #ifdef USE_WINSOCK |
| 1232 | long mask = 0; |
| 1233 | if(extra_fds[i].events & CURL_WAIT_POLLIN) |
| 1234 | mask |= FD_READ|FD_ACCEPT|FD_CLOSE; |
| 1235 | if(extra_fds[i].events & CURL_WAIT_POLLPRI) |
| 1236 | mask |= FD_OOB; |
| 1237 | if(extra_fds[i].events & CURL_WAIT_POLLOUT) { |
| 1238 | mask |= FD_WRITE|FD_CONNECT|FD_CLOSE; |
| 1239 | send(extra_fds[i].fd, NULL, 0, 0); /* reset FD_WRITE */ |
| 1240 | } |
| 1241 | if(WSAEventSelect(extra_fds[i].fd, multi->wsa_event, mask) != 0) { |
| 1242 | if(ufds_malloc) |
| 1243 | free(ufds); |
| 1244 | return CURLM_INTERNAL_ERROR; |
| 1245 | } |
| 1246 | #endif |
| 1247 | ufds[nfds].fd = extra_fds[i].fd; |
| 1248 | ufds[nfds].events = 0; |
| 1249 | if(extra_fds[i].events & CURL_WAIT_POLLIN) |
| 1250 | ufds[nfds].events |= POLLIN; |
| 1251 | if(extra_fds[i].events & CURL_WAIT_POLLPRI) |
| 1252 | ufds[nfds].events |= POLLPRI; |
| 1253 | if(extra_fds[i].events & CURL_WAIT_POLLOUT) |
| 1254 | ufds[nfds].events |= POLLOUT; |
| 1255 | ++nfds; |
| 1256 | } |
| 1257 | |
| 1258 | #ifdef ENABLE_WAKEUP |
| 1259 | #ifndef USE_WINSOCK |
| 1260 | if(use_wakeup && multi->wakeup_pair[0] != CURL_SOCKET_BAD) { |
| 1261 | ufds[nfds].fd = multi->wakeup_pair[0]; |
| 1262 | ufds[nfds].events = POLLIN; |
| 1263 | ++nfds; |
| 1264 | } |
| 1265 | #endif |
| 1266 | #endif |
| 1267 | |
| 1268 | #if defined(ENABLE_WAKEUP) && defined(USE_WINSOCK) |
| 1269 | if(nfds || use_wakeup) { |
| 1270 | #else |
| 1271 | if(nfds) { |
| 1272 | #endif |
| 1273 | int pollrc; |
| 1274 | #ifdef USE_WINSOCK |
| 1275 | if(nfds) |
| 1276 | pollrc = Curl_poll(ufds, nfds, 0); /* just pre-check with WinSock */ |
| 1277 | else |
| 1278 | pollrc = 0; |
| 1279 | if(pollrc <= 0) /* now wait... if not ready during the pre-check above */ |
| 1280 | WSAWaitForMultipleEvents(1, &multi->wsa_event, FALSE, timeout_ms, FALSE); |
| 1281 | #else |
| 1282 | pollrc = Curl_poll(ufds, nfds, timeout_ms); /* wait... */ |
| 1283 | #endif |
| 1284 | |
| 1285 | if(pollrc > 0) { |
| 1286 | retcode = pollrc; |
| 1287 | #ifdef USE_WINSOCK |
| 1288 | } |
| 1289 | /* With WinSock, we have to run the following section unconditionally |
| 1290 | to call WSAEventSelect(fd, event, 0) on all the sockets */ |
| 1291 | { |
| 1292 | #endif |
| 1293 | /* copy revents results from the poll to the curl_multi_wait poll |
| 1294 | struct, the bit values of the actual underlying poll() implementation |
| 1295 | may not be the same as the ones in the public libcurl API! */ |
| 1296 | for(i = 0; i < extra_nfds; i++) { |
| 1297 | unsigned r = ufds[curlfds + i].revents; |
| 1298 | unsigned short mask = 0; |
| 1299 | #ifdef USE_WINSOCK |
| 1300 | wsa_events.lNetworkEvents = 0; |
| 1301 | if(WSAEnumNetworkEvents(extra_fds[i].fd, NULL, &wsa_events) == 0) { |
| 1302 | if(wsa_events.lNetworkEvents & (FD_READ|FD_ACCEPT|FD_CLOSE)) |
| 1303 | mask |= CURL_WAIT_POLLIN; |
| 1304 | if(wsa_events.lNetworkEvents & (FD_WRITE|FD_CONNECT|FD_CLOSE)) |
| 1305 | mask |= CURL_WAIT_POLLOUT; |
| 1306 | if(wsa_events.lNetworkEvents & FD_OOB) |
| 1307 | mask |= CURL_WAIT_POLLPRI; |
| 1308 | if(ret && pollrc <= 0 && wsa_events.lNetworkEvents) |
| 1309 | retcode++; |
| 1310 | } |
| 1311 | WSAEventSelect(extra_fds[i].fd, multi->wsa_event, 0); |
| 1312 | if(pollrc <= 0) |
| 1313 | continue; |
| 1314 | #endif |
| 1315 | if(r & POLLIN) |
| 1316 | mask |= CURL_WAIT_POLLIN; |
| 1317 | if(r & POLLOUT) |
| 1318 | mask |= CURL_WAIT_POLLOUT; |
| 1319 | if(r & POLLPRI) |
| 1320 | mask |= CURL_WAIT_POLLPRI; |
| 1321 | extra_fds[i].revents = mask; |
| 1322 | } |
| 1323 | |
| 1324 | #ifdef USE_WINSOCK |
| 1325 | /* Count up all our own sockets that had activity, |
| 1326 | and remove them from the event. */ |
| 1327 | if(curlfds) { |
| 1328 | data = multi->easyp; |
| 1329 | while(data) { |
| 1330 | bitmap = multi_getsock(data, sockbunch); |
| 1331 | |
| 1332 | for(i = 0; i < MAX_SOCKSPEREASYHANDLE; i++) { |
| 1333 | if(bitmap & (GETSOCK_READSOCK(i) | GETSOCK_WRITESOCK(i))) { |
| 1334 | wsa_events.lNetworkEvents = 0; |
| 1335 | if(WSAEnumNetworkEvents(sockbunch[i], NULL, &wsa_events) == 0) { |
| 1336 | if(ret && pollrc <= 0 && wsa_events.lNetworkEvents) |
| 1337 | retcode++; |
| 1338 | } |
| 1339 | WSAEventSelect(sockbunch[i], multi->wsa_event, 0); |
| 1340 | } |
| 1341 | else { |
| 1342 | /* break on entry not checked for being readable or writable */ |
| 1343 | break; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | data = data->next; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | WSAResetEvent(multi->wsa_event); |
| 1352 | #else |
| 1353 | #ifdef ENABLE_WAKEUP |
| 1354 | if(use_wakeup && multi->wakeup_pair[0] != CURL_SOCKET_BAD) { |
| 1355 | if(ufds[curlfds + extra_nfds].revents & POLLIN) { |
| 1356 | char buf[64]; |
| 1357 | ssize_t nread; |
| 1358 | while(1) { |
| 1359 | /* the reading socket is non-blocking, try to read |
| 1360 | data from it until it receives an error (except EINTR). |
| 1361 | In normal cases it will get EAGAIN or EWOULDBLOCK |
| 1362 | when there is no more data, breaking the loop. */ |
| 1363 | nread = sread(multi->wakeup_pair[0], buf, sizeof(buf)); |
| 1364 | if(nread <= 0) { |
| 1365 | if(nread < 0 && EINTR == SOCKERRNO) |
| 1366 | continue; |
| 1367 | break; |
| 1368 | } |
| 1369 | } |
| 1370 | /* do not count the wakeup socket into the returned value */ |
| 1371 | retcode--; |
| 1372 | } |
| 1373 | } |
| 1374 | #endif |
| 1375 | #endif |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | if(ufds_malloc) |
| 1380 | free(ufds); |
| 1381 | if(ret) |
| 1382 | *ret = retcode; |
| 1383 | #if defined(ENABLE_WAKEUP) && defined(USE_WINSOCK) |
| 1384 | if(extrawait && !nfds && !use_wakeup) { |
| 1385 | #else |
| 1386 | if(extrawait && !nfds) { |
| 1387 | #endif |
| 1388 | long sleep_ms = 0; |
| 1389 | |
| 1390 | /* Avoid busy-looping when there's nothing particular to wait for */ |
| 1391 | if(!curl_multi_timeout(multi, &sleep_ms) && sleep_ms) { |
| 1392 | if(sleep_ms > timeout_ms) |
| 1393 | sleep_ms = timeout_ms; |
| 1394 | /* when there are no easy handles in the multi, this holds a -1 |
| 1395 | timeout */ |
| 1396 | else if(sleep_ms < 0) |
| 1397 | sleep_ms = timeout_ms; |
| 1398 | Curl_wait_ms(sleep_ms); |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | return CURLM_OK; |
| 1403 | } |
| 1404 | |
| 1405 | CURLMcode curl_multi_wait(struct Curl_multi *multi, |
| 1406 | struct curl_waitfd [], |
| 1407 | unsigned int , |
| 1408 | int timeout_ms, |
| 1409 | int *ret) |
| 1410 | { |
| 1411 | return multi_wait(multi, extra_fds, extra_nfds, timeout_ms, ret, FALSE, |
| 1412 | FALSE); |
| 1413 | } |
| 1414 | |
| 1415 | CURLMcode curl_multi_poll(struct Curl_multi *multi, |
| 1416 | struct curl_waitfd [], |
| 1417 | unsigned int , |
| 1418 | int timeout_ms, |
| 1419 | int *ret) |
| 1420 | { |
| 1421 | return multi_wait(multi, extra_fds, extra_nfds, timeout_ms, ret, TRUE, |
| 1422 | TRUE); |
| 1423 | } |
| 1424 | |
| 1425 | CURLMcode curl_multi_wakeup(struct Curl_multi *multi) |
| 1426 | { |
| 1427 | /* this function is usually called from another thread, |
| 1428 | it has to be careful only to access parts of the |
| 1429 | Curl_multi struct that are constant */ |
| 1430 | |
| 1431 | /* GOOD_MULTI_HANDLE can be safely called */ |
| 1432 | if(!GOOD_MULTI_HANDLE(multi)) |
| 1433 | return CURLM_BAD_HANDLE; |
| 1434 | |
| 1435 | #ifdef ENABLE_WAKEUP |
| 1436 | #ifdef USE_WINSOCK |
| 1437 | if(WSASetEvent(multi->wsa_event)) |
| 1438 | return CURLM_OK; |
| 1439 | #else |
| 1440 | /* the wakeup_pair variable is only written during init and cleanup, |
| 1441 | making it safe to access from another thread after the init part |
| 1442 | and before cleanup */ |
| 1443 | if(multi->wakeup_pair[1] != CURL_SOCKET_BAD) { |
| 1444 | char buf[1]; |
| 1445 | buf[0] = 1; |
| 1446 | while(1) { |
| 1447 | /* swrite() is not thread-safe in general, because concurrent calls |
| 1448 | can have their messages interleaved, but in this case the content |
| 1449 | of the messages does not matter, which makes it ok to call. |
| 1450 | |
| 1451 | The write socket is set to non-blocking, this way this function |
| 1452 | cannot block, making it safe to call even from the same thread |
| 1453 | that will call curl_multi_wait(). If swrite() returns that it |
| 1454 | would block, it's considered successful because it means that |
| 1455 | previous calls to this function will wake up the poll(). */ |
| 1456 | if(swrite(multi->wakeup_pair[1], buf, sizeof(buf)) < 0) { |
| 1457 | int err = SOCKERRNO; |
| 1458 | int return_success; |
| 1459 | #ifdef USE_WINSOCK |
| 1460 | return_success = WSAEWOULDBLOCK == err; |
| 1461 | #else |
| 1462 | if(EINTR == err) |
| 1463 | continue; |
| 1464 | return_success = EWOULDBLOCK == err || EAGAIN == err; |
| 1465 | #endif |
| 1466 | if(!return_success) |
| 1467 | return CURLM_WAKEUP_FAILURE; |
| 1468 | } |
| 1469 | return CURLM_OK; |
| 1470 | } |
| 1471 | } |
| 1472 | #endif |
| 1473 | #endif |
| 1474 | return CURLM_WAKEUP_FAILURE; |
| 1475 | } |
| 1476 | |
| 1477 | /* |
| 1478 | * multi_ischanged() is called |
| 1479 | * |
| 1480 | * Returns TRUE/FALSE whether the state is changed to trigger a CONNECT_PEND |
| 1481 | * => CONNECT action. |
| 1482 | * |
| 1483 | * Set 'clear' to TRUE to have it also clear the state variable. |
| 1484 | */ |
| 1485 | static bool multi_ischanged(struct Curl_multi *multi, bool clear) |
| 1486 | { |
| 1487 | bool retval = multi->recheckstate; |
| 1488 | if(clear) |
| 1489 | multi->recheckstate = FALSE; |
| 1490 | return retval; |
| 1491 | } |
| 1492 | |
| 1493 | CURLMcode Curl_multi_add_perform(struct Curl_multi *multi, |
| 1494 | struct Curl_easy *data, |
| 1495 | struct connectdata *conn) |
| 1496 | { |
| 1497 | CURLMcode rc; |
| 1498 | |
| 1499 | if(multi->in_callback) |
| 1500 | return CURLM_RECURSIVE_API_CALL; |
| 1501 | |
| 1502 | rc = curl_multi_add_handle(multi, data); |
| 1503 | if(!rc) { |
| 1504 | struct SingleRequest *k = &data->req; |
| 1505 | |
| 1506 | /* pass in NULL for 'conn' here since we don't want to init the |
| 1507 | connection, only this transfer */ |
| 1508 | Curl_init_do(data, NULL); |
| 1509 | |
| 1510 | /* take this handle to the perform state right away */ |
| 1511 | multistate(data, MSTATE_PERFORMING); |
| 1512 | Curl_attach_connnection(data, conn); |
| 1513 | k->keepon |= KEEP_RECV; /* setup to receive! */ |
| 1514 | } |
| 1515 | return rc; |
| 1516 | } |
| 1517 | |
| 1518 | static CURLcode multi_do(struct Curl_easy *data, bool *done) |
| 1519 | { |
| 1520 | CURLcode result = CURLE_OK; |
| 1521 | struct connectdata *conn = data->conn; |
| 1522 | |
| 1523 | DEBUGASSERT(conn); |
| 1524 | DEBUGASSERT(conn->handler); |
| 1525 | |
| 1526 | if(conn->handler->do_it) |
| 1527 | /* generic protocol-specific function pointer set in curl_connect() */ |
| 1528 | result = conn->handler->do_it(data, done); |
| 1529 | |
| 1530 | return result; |
| 1531 | } |
| 1532 | |
| 1533 | /* |
| 1534 | * multi_do_more() is called during the DO_MORE multi state. It is basically a |
| 1535 | * second stage DO state which (wrongly) was introduced to support FTP's |
| 1536 | * second connection. |
| 1537 | * |
| 1538 | * 'complete' can return 0 for incomplete, 1 for done and -1 for go back to |
| 1539 | * DOING state there's more work to do! |
| 1540 | */ |
| 1541 | |
| 1542 | static CURLcode multi_do_more(struct Curl_easy *data, int *complete) |
| 1543 | { |
| 1544 | CURLcode result = CURLE_OK; |
| 1545 | struct connectdata *conn = data->conn; |
| 1546 | |
| 1547 | *complete = 0; |
| 1548 | |
| 1549 | if(conn->handler->do_more) |
| 1550 | result = conn->handler->do_more(data, complete); |
| 1551 | |
| 1552 | return result; |
| 1553 | } |
| 1554 | |
| 1555 | /* |
| 1556 | * Check whether a timeout occurred, and handle it if it did |
| 1557 | */ |
| 1558 | static bool multi_handle_timeout(struct Curl_easy *data, |
| 1559 | struct curltime *now, |
| 1560 | bool *stream_error, |
| 1561 | CURLcode *result, |
| 1562 | bool connect_timeout) |
| 1563 | { |
| 1564 | timediff_t timeout_ms; |
| 1565 | timeout_ms = Curl_timeleft(data, now, connect_timeout); |
| 1566 | |
| 1567 | if(timeout_ms < 0) { |
| 1568 | /* Handle timed out */ |
| 1569 | if(data->mstate == MSTATE_RESOLVING) |
| 1570 | failf(data, "Resolving timed out after %" CURL_FORMAT_TIMEDIFF_T |
| 1571 | " milliseconds" , |
| 1572 | Curl_timediff(*now, data->progress.t_startsingle)); |
| 1573 | else if(data->mstate == MSTATE_CONNECTING) |
| 1574 | failf(data, "Connection timed out after %" CURL_FORMAT_TIMEDIFF_T |
| 1575 | " milliseconds" , |
| 1576 | Curl_timediff(*now, data->progress.t_startsingle)); |
| 1577 | else { |
| 1578 | struct SingleRequest *k = &data->req; |
| 1579 | if(k->size != -1) { |
| 1580 | failf(data, "Operation timed out after %" CURL_FORMAT_TIMEDIFF_T |
| 1581 | " milliseconds with %" CURL_FORMAT_CURL_OFF_T " out of %" |
| 1582 | CURL_FORMAT_CURL_OFF_T " bytes received" , |
| 1583 | Curl_timediff(*now, data->progress.t_startsingle), |
| 1584 | k->bytecount, k->size); |
| 1585 | } |
| 1586 | else { |
| 1587 | failf(data, "Operation timed out after %" CURL_FORMAT_TIMEDIFF_T |
| 1588 | " milliseconds with %" CURL_FORMAT_CURL_OFF_T |
| 1589 | " bytes received" , |
| 1590 | Curl_timediff(*now, data->progress.t_startsingle), |
| 1591 | k->bytecount); |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | /* Force connection closed if the connection has indeed been used */ |
| 1596 | if(data->mstate > MSTATE_DO) { |
| 1597 | streamclose(data->conn, "Disconnected with pending data" ); |
| 1598 | *stream_error = TRUE; |
| 1599 | } |
| 1600 | *result = CURLE_OPERATION_TIMEDOUT; |
| 1601 | (void)multi_done(data, *result, TRUE); |
| 1602 | } |
| 1603 | |
| 1604 | return (timeout_ms < 0); |
| 1605 | } |
| 1606 | |
| 1607 | /* |
| 1608 | * We are doing protocol-specific connecting and this is being called over and |
| 1609 | * over from the multi interface until the connection phase is done on |
| 1610 | * protocol layer. |
| 1611 | */ |
| 1612 | |
| 1613 | static CURLcode protocol_connecting(struct Curl_easy *data, bool *done) |
| 1614 | { |
| 1615 | CURLcode result = CURLE_OK; |
| 1616 | struct connectdata *conn = data->conn; |
| 1617 | |
| 1618 | if(conn && conn->handler->connecting) { |
| 1619 | *done = FALSE; |
| 1620 | result = conn->handler->connecting(data, done); |
| 1621 | } |
| 1622 | else |
| 1623 | *done = TRUE; |
| 1624 | |
| 1625 | return result; |
| 1626 | } |
| 1627 | |
| 1628 | /* |
| 1629 | * We are DOING this is being called over and over from the multi interface |
| 1630 | * until the DOING phase is done on protocol layer. |
| 1631 | */ |
| 1632 | |
| 1633 | static CURLcode protocol_doing(struct Curl_easy *data, bool *done) |
| 1634 | { |
| 1635 | CURLcode result = CURLE_OK; |
| 1636 | struct connectdata *conn = data->conn; |
| 1637 | |
| 1638 | if(conn && conn->handler->doing) { |
| 1639 | *done = FALSE; |
| 1640 | result = conn->handler->doing(data, done); |
| 1641 | } |
| 1642 | else |
| 1643 | *done = TRUE; |
| 1644 | |
| 1645 | return result; |
| 1646 | } |
| 1647 | |
| 1648 | /* |
| 1649 | * We have discovered that the TCP connection has been successful, we can now |
| 1650 | * proceed with some action. |
| 1651 | * |
| 1652 | */ |
| 1653 | static CURLcode protocol_connect(struct Curl_easy *data, |
| 1654 | bool *protocol_done) |
| 1655 | { |
| 1656 | CURLcode result = CURLE_OK; |
| 1657 | struct connectdata *conn = data->conn; |
| 1658 | DEBUGASSERT(conn); |
| 1659 | DEBUGASSERT(protocol_done); |
| 1660 | |
| 1661 | *protocol_done = FALSE; |
| 1662 | |
| 1663 | if(conn->bits.tcpconnect[FIRSTSOCKET] && conn->bits.protoconnstart) { |
| 1664 | /* We already are connected, get back. This may happen when the connect |
| 1665 | worked fine in the first call, like when we connect to a local server |
| 1666 | or proxy. Note that we don't know if the protocol is actually done. |
| 1667 | |
| 1668 | Unless this protocol doesn't have any protocol-connect callback, as |
| 1669 | then we know we're done. */ |
| 1670 | if(!conn->handler->connecting) |
| 1671 | *protocol_done = TRUE; |
| 1672 | |
| 1673 | return CURLE_OK; |
| 1674 | } |
| 1675 | |
| 1676 | if(!conn->bits.protoconnstart) { |
| 1677 | #ifndef CURL_DISABLE_PROXY |
| 1678 | result = Curl_proxy_connect(data, FIRSTSOCKET); |
| 1679 | if(result) |
| 1680 | return result; |
| 1681 | |
| 1682 | if(CONNECT_FIRSTSOCKET_PROXY_SSL()) |
| 1683 | /* wait for HTTPS proxy SSL initialization to complete */ |
| 1684 | return CURLE_OK; |
| 1685 | |
| 1686 | if(conn->bits.tunnel_proxy && conn->bits.httpproxy && |
| 1687 | Curl_connect_ongoing(conn)) |
| 1688 | /* when using an HTTP tunnel proxy, await complete tunnel establishment |
| 1689 | before proceeding further. Return CURLE_OK so we'll be called again */ |
| 1690 | return CURLE_OK; |
| 1691 | #endif |
| 1692 | if(conn->handler->connect_it) { |
| 1693 | /* is there a protocol-specific connect() procedure? */ |
| 1694 | |
| 1695 | /* Call the protocol-specific connect function */ |
| 1696 | result = conn->handler->connect_it(data, protocol_done); |
| 1697 | } |
| 1698 | else |
| 1699 | *protocol_done = TRUE; |
| 1700 | |
| 1701 | /* it has started, possibly even completed but that knowledge isn't stored |
| 1702 | in this bit! */ |
| 1703 | if(!result) |
| 1704 | conn->bits.protoconnstart = TRUE; |
| 1705 | } |
| 1706 | |
| 1707 | return result; /* pass back status */ |
| 1708 | } |
| 1709 | |
| 1710 | /* |
| 1711 | * Curl_preconnect() is called immediately before a connect starts. When a |
| 1712 | * redirect is followed, this is then called multiple times during a single |
| 1713 | * transfer. |
| 1714 | */ |
| 1715 | CURLcode Curl_preconnect(struct Curl_easy *data) |
| 1716 | { |
| 1717 | if(!data->state.buffer) { |
| 1718 | data->state.buffer = malloc(data->set.buffer_size + 1); |
| 1719 | if(!data->state.buffer) |
| 1720 | return CURLE_OUT_OF_MEMORY; |
| 1721 | } |
| 1722 | return CURLE_OK; |
| 1723 | } |
| 1724 | |
| 1725 | |
| 1726 | static CURLMcode multi_runsingle(struct Curl_multi *multi, |
| 1727 | struct curltime *nowp, |
| 1728 | struct Curl_easy *data) |
| 1729 | { |
| 1730 | struct Curl_message *msg = NULL; |
| 1731 | bool connected; |
| 1732 | bool async; |
| 1733 | bool protocol_connected = FALSE; |
| 1734 | bool dophase_done = FALSE; |
| 1735 | bool done = FALSE; |
| 1736 | CURLMcode rc; |
| 1737 | CURLcode result = CURLE_OK; |
| 1738 | timediff_t recv_timeout_ms; |
| 1739 | timediff_t send_timeout_ms; |
| 1740 | int control; |
| 1741 | |
| 1742 | if(!GOOD_EASY_HANDLE(data)) |
| 1743 | return CURLM_BAD_EASY_HANDLE; |
| 1744 | |
| 1745 | do { |
| 1746 | /* A "stream" here is a logical stream if the protocol can handle that |
| 1747 | (HTTP/2), or the full connection for older protocols */ |
| 1748 | bool stream_error = FALSE; |
| 1749 | rc = CURLM_OK; |
| 1750 | |
| 1751 | if(multi_ischanged(multi, TRUE)) { |
| 1752 | DEBUGF(infof(data, "multi changed, check CONNECT_PEND queue!" )); |
| 1753 | process_pending_handles(multi); /* multiplexed */ |
| 1754 | } |
| 1755 | |
| 1756 | if(data->mstate > MSTATE_CONNECT && |
| 1757 | data->mstate < MSTATE_COMPLETED) { |
| 1758 | /* Make sure we set the connection's current owner */ |
| 1759 | DEBUGASSERT(data->conn); |
| 1760 | if(!data->conn) |
| 1761 | return CURLM_INTERNAL_ERROR; |
| 1762 | } |
| 1763 | |
| 1764 | if(data->conn && |
| 1765 | (data->mstate >= MSTATE_CONNECT) && |
| 1766 | (data->mstate < MSTATE_COMPLETED)) { |
| 1767 | /* Check for overall operation timeout here but defer handling the |
| 1768 | * connection timeout to later, to allow for a connection to be set up |
| 1769 | * in the window since we last checked timeout. This prevents us |
| 1770 | * tearing down a completed connection in the case where we were slow |
| 1771 | * to check the timeout (e.g. process descheduled during this loop). |
| 1772 | * We set connect_timeout=FALSE to do this. */ |
| 1773 | |
| 1774 | /* we need to wait for the connect state as only then is the start time |
| 1775 | stored, but we must not check already completed handles */ |
| 1776 | if(multi_handle_timeout(data, nowp, &stream_error, &result, FALSE)) { |
| 1777 | /* Skip the statemachine and go directly to error handling section. */ |
| 1778 | goto statemachine_end; |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | switch(data->mstate) { |
| 1783 | case MSTATE_INIT: |
| 1784 | /* init this transfer. */ |
| 1785 | result = Curl_pretransfer(data); |
| 1786 | |
| 1787 | if(!result) { |
| 1788 | /* after init, go CONNECT */ |
| 1789 | multistate(data, MSTATE_CONNECT); |
| 1790 | *nowp = Curl_pgrsTime(data, TIMER_STARTOP); |
| 1791 | rc = CURLM_CALL_MULTI_PERFORM; |
| 1792 | } |
| 1793 | break; |
| 1794 | |
| 1795 | case MSTATE_PENDING: |
| 1796 | /* We will stay here until there is a connection available. Then |
| 1797 | we try again in the MSTATE_CONNECT state. */ |
| 1798 | break; |
| 1799 | |
| 1800 | case MSTATE_CONNECT: |
| 1801 | /* Connect. We want to get a connection identifier filled in. */ |
| 1802 | /* init this transfer. */ |
| 1803 | result = Curl_preconnect(data); |
| 1804 | if(result) |
| 1805 | break; |
| 1806 | |
| 1807 | *nowp = Curl_pgrsTime(data, TIMER_STARTSINGLE); |
| 1808 | if(data->set.timeout) |
| 1809 | Curl_expire(data, data->set.timeout, EXPIRE_TIMEOUT); |
| 1810 | |
| 1811 | if(data->set.connecttimeout) |
| 1812 | Curl_expire(data, data->set.connecttimeout, EXPIRE_CONNECTTIMEOUT); |
| 1813 | |
| 1814 | result = Curl_connect(data, &async, &protocol_connected); |
| 1815 | if(CURLE_NO_CONNECTION_AVAILABLE == result) { |
| 1816 | /* There was no connection available. We will go to the pending |
| 1817 | state and wait for an available connection. */ |
| 1818 | multistate(data, MSTATE_PENDING); |
| 1819 | |
| 1820 | /* add this handle to the list of connect-pending handles */ |
| 1821 | Curl_llist_insert_next(&multi->pending, multi->pending.tail, data, |
| 1822 | &data->connect_queue); |
| 1823 | result = CURLE_OK; |
| 1824 | break; |
| 1825 | } |
| 1826 | else if(data->state.previouslypending) { |
| 1827 | /* this transfer comes from the pending queue so try move another */ |
| 1828 | infof(data, "Transfer was pending, now try another" ); |
| 1829 | process_pending_handles(data->multi); |
| 1830 | } |
| 1831 | |
| 1832 | if(!result) { |
| 1833 | if(async) |
| 1834 | /* We're now waiting for an asynchronous name lookup */ |
| 1835 | multistate(data, MSTATE_RESOLVING); |
| 1836 | else { |
| 1837 | /* after the connect has been sent off, go WAITCONNECT unless the |
| 1838 | protocol connect is already done and we can go directly to |
| 1839 | WAITDO or DO! */ |
| 1840 | rc = CURLM_CALL_MULTI_PERFORM; |
| 1841 | |
| 1842 | if(protocol_connected) |
| 1843 | multistate(data, MSTATE_DO); |
| 1844 | else { |
| 1845 | #ifndef CURL_DISABLE_HTTP |
| 1846 | if(Curl_connect_ongoing(data->conn)) |
| 1847 | multistate(data, MSTATE_TUNNELING); |
| 1848 | else |
| 1849 | #endif |
| 1850 | multistate(data, MSTATE_CONNECTING); |
| 1851 | } |
| 1852 | } |
| 1853 | } |
| 1854 | break; |
| 1855 | |
| 1856 | case MSTATE_RESOLVING: |
| 1857 | /* awaiting an asynch name resolve to complete */ |
| 1858 | { |
| 1859 | struct Curl_dns_entry *dns = NULL; |
| 1860 | struct connectdata *conn = data->conn; |
| 1861 | const char *hostname; |
| 1862 | |
| 1863 | DEBUGASSERT(conn); |
| 1864 | #ifndef CURL_DISABLE_PROXY |
| 1865 | if(conn->bits.httpproxy) |
| 1866 | hostname = conn->http_proxy.host.name; |
| 1867 | else |
| 1868 | #endif |
| 1869 | if(conn->bits.conn_to_host) |
| 1870 | hostname = conn->conn_to_host.name; |
| 1871 | else |
| 1872 | hostname = conn->host.name; |
| 1873 | |
| 1874 | /* check if we have the name resolved by now */ |
| 1875 | dns = Curl_fetch_addr(data, hostname, (int)conn->port); |
| 1876 | |
| 1877 | if(dns) { |
| 1878 | #ifdef CURLRES_ASYNCH |
| 1879 | data->state.async.dns = dns; |
| 1880 | data->state.async.done = TRUE; |
| 1881 | #endif |
| 1882 | result = CURLE_OK; |
| 1883 | infof(data, "Hostname '%s' was found in DNS cache" , hostname); |
| 1884 | } |
| 1885 | |
| 1886 | if(!dns) |
| 1887 | result = Curl_resolv_check(data, &dns); |
| 1888 | |
| 1889 | /* Update sockets here, because the socket(s) may have been |
| 1890 | closed and the application thus needs to be told, even if it |
| 1891 | is likely that the same socket(s) will again be used further |
| 1892 | down. If the name has not yet been resolved, it is likely |
| 1893 | that new sockets have been opened in an attempt to contact |
| 1894 | another resolver. */ |
| 1895 | singlesocket(multi, data); |
| 1896 | |
| 1897 | if(dns) { |
| 1898 | /* Perform the next step in the connection phase, and then move on |
| 1899 | to the WAITCONNECT state */ |
| 1900 | result = Curl_once_resolved(data, &protocol_connected); |
| 1901 | |
| 1902 | if(result) |
| 1903 | /* if Curl_once_resolved() returns failure, the connection struct |
| 1904 | is already freed and gone */ |
| 1905 | data->conn = NULL; /* no more connection */ |
| 1906 | else { |
| 1907 | /* call again please so that we get the next socket setup */ |
| 1908 | rc = CURLM_CALL_MULTI_PERFORM; |
| 1909 | if(protocol_connected) |
| 1910 | multistate(data, MSTATE_DO); |
| 1911 | else { |
| 1912 | #ifndef CURL_DISABLE_HTTP |
| 1913 | if(Curl_connect_ongoing(data->conn)) |
| 1914 | multistate(data, MSTATE_TUNNELING); |
| 1915 | else |
| 1916 | #endif |
| 1917 | multistate(data, MSTATE_CONNECTING); |
| 1918 | } |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | if(result) { |
| 1923 | /* failure detected */ |
| 1924 | stream_error = TRUE; |
| 1925 | break; |
| 1926 | } |
| 1927 | } |
| 1928 | break; |
| 1929 | |
| 1930 | #ifndef CURL_DISABLE_HTTP |
| 1931 | case MSTATE_TUNNELING: |
| 1932 | /* this is HTTP-specific, but sending CONNECT to a proxy is HTTP... */ |
| 1933 | DEBUGASSERT(data->conn); |
| 1934 | result = Curl_http_connect(data, &protocol_connected); |
| 1935 | #ifndef CURL_DISABLE_PROXY |
| 1936 | if(data->conn->bits.proxy_connect_closed) { |
| 1937 | rc = CURLM_CALL_MULTI_PERFORM; |
| 1938 | /* connect back to proxy again */ |
| 1939 | result = CURLE_OK; |
| 1940 | multi_done(data, CURLE_OK, FALSE); |
| 1941 | multistate(data, MSTATE_CONNECT); |
| 1942 | } |
| 1943 | else |
| 1944 | #endif |
| 1945 | if(!result) { |
| 1946 | if( |
| 1947 | #ifndef CURL_DISABLE_PROXY |
| 1948 | (data->conn->http_proxy.proxytype != CURLPROXY_HTTPS || |
| 1949 | data->conn->bits.proxy_ssl_connected[FIRSTSOCKET]) && |
| 1950 | #endif |
| 1951 | Curl_connect_complete(data->conn)) { |
| 1952 | rc = CURLM_CALL_MULTI_PERFORM; |
| 1953 | /* initiate protocol connect phase */ |
| 1954 | multistate(data, MSTATE_PROTOCONNECT); |
| 1955 | } |
| 1956 | } |
| 1957 | else |
| 1958 | stream_error = TRUE; |
| 1959 | break; |
| 1960 | #endif |
| 1961 | |
| 1962 | case MSTATE_CONNECTING: |
| 1963 | /* awaiting a completion of an asynch TCP connect */ |
| 1964 | DEBUGASSERT(data->conn); |
| 1965 | result = Curl_is_connected(data, data->conn, FIRSTSOCKET, &connected); |
| 1966 | if(connected && !result) { |
| 1967 | #ifndef CURL_DISABLE_HTTP |
| 1968 | if( |
| 1969 | #ifndef CURL_DISABLE_PROXY |
| 1970 | (data->conn->http_proxy.proxytype == CURLPROXY_HTTPS && |
| 1971 | !data->conn->bits.proxy_ssl_connected[FIRSTSOCKET]) || |
| 1972 | #endif |
| 1973 | Curl_connect_ongoing(data->conn)) { |
| 1974 | multistate(data, MSTATE_TUNNELING); |
| 1975 | break; |
| 1976 | } |
| 1977 | #endif |
| 1978 | rc = CURLM_CALL_MULTI_PERFORM; |
| 1979 | #ifndef CURL_DISABLE_PROXY |
| 1980 | multistate(data, |
| 1981 | data->conn->bits.tunnel_proxy? |
| 1982 | MSTATE_TUNNELING : MSTATE_PROTOCONNECT); |
| 1983 | #else |
| 1984 | multistate(data, MSTATE_PROTOCONNECT); |
| 1985 | #endif |
| 1986 | } |
| 1987 | else if(result) { |
| 1988 | /* failure detected */ |
| 1989 | Curl_posttransfer(data); |
| 1990 | multi_done(data, result, TRUE); |
| 1991 | stream_error = TRUE; |
| 1992 | break; |
| 1993 | } |
| 1994 | break; |
| 1995 | |
| 1996 | case MSTATE_PROTOCONNECT: |
| 1997 | result = protocol_connect(data, &protocol_connected); |
| 1998 | if(!result && !protocol_connected) |
| 1999 | /* switch to waiting state */ |
| 2000 | multistate(data, MSTATE_PROTOCONNECTING); |
| 2001 | else if(!result) { |
| 2002 | /* protocol connect has completed, go WAITDO or DO */ |
| 2003 | multistate(data, MSTATE_DO); |
| 2004 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2005 | } |
| 2006 | else { |
| 2007 | /* failure detected */ |
| 2008 | Curl_posttransfer(data); |
| 2009 | multi_done(data, result, TRUE); |
| 2010 | stream_error = TRUE; |
| 2011 | } |
| 2012 | break; |
| 2013 | |
| 2014 | case MSTATE_PROTOCONNECTING: |
| 2015 | /* protocol-specific connect phase */ |
| 2016 | result = protocol_connecting(data, &protocol_connected); |
| 2017 | if(!result && protocol_connected) { |
| 2018 | /* after the connect has completed, go WAITDO or DO */ |
| 2019 | multistate(data, MSTATE_DO); |
| 2020 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2021 | } |
| 2022 | else if(result) { |
| 2023 | /* failure detected */ |
| 2024 | Curl_posttransfer(data); |
| 2025 | multi_done(data, result, TRUE); |
| 2026 | stream_error = TRUE; |
| 2027 | } |
| 2028 | break; |
| 2029 | |
| 2030 | case MSTATE_DO: |
| 2031 | if(data->set.connect_only) { |
| 2032 | /* keep connection open for application to use the socket */ |
| 2033 | connkeep(data->conn, "CONNECT_ONLY" ); |
| 2034 | multistate(data, MSTATE_DONE); |
| 2035 | result = CURLE_OK; |
| 2036 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2037 | } |
| 2038 | else { |
| 2039 | /* Perform the protocol's DO action */ |
| 2040 | result = multi_do(data, &dophase_done); |
| 2041 | |
| 2042 | /* When multi_do() returns failure, data->conn might be NULL! */ |
| 2043 | |
| 2044 | if(!result) { |
| 2045 | if(!dophase_done) { |
| 2046 | #ifndef CURL_DISABLE_FTP |
| 2047 | /* some steps needed for wildcard matching */ |
| 2048 | if(data->state.wildcardmatch) { |
| 2049 | struct WildcardData *wc = &data->wildcard; |
| 2050 | if(wc->state == CURLWC_DONE || wc->state == CURLWC_SKIP) { |
| 2051 | /* skip some states if it is important */ |
| 2052 | multi_done(data, CURLE_OK, FALSE); |
| 2053 | |
| 2054 | /* if there's no connection left, skip the DONE state */ |
| 2055 | multistate(data, data->conn ? |
| 2056 | MSTATE_DONE : MSTATE_COMPLETED); |
| 2057 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2058 | break; |
| 2059 | } |
| 2060 | } |
| 2061 | #endif |
| 2062 | /* DO was not completed in one function call, we must continue |
| 2063 | DOING... */ |
| 2064 | multistate(data, MSTATE_DOING); |
| 2065 | rc = CURLM_OK; |
| 2066 | } |
| 2067 | |
| 2068 | /* after DO, go DO_DONE... or DO_MORE */ |
| 2069 | else if(data->conn->bits.do_more) { |
| 2070 | /* we're supposed to do more, but we need to sit down, relax |
| 2071 | and wait a little while first */ |
| 2072 | multistate(data, MSTATE_DOING_MORE); |
| 2073 | rc = CURLM_OK; |
| 2074 | } |
| 2075 | else { |
| 2076 | /* we're done with the DO, now DID */ |
| 2077 | multistate(data, MSTATE_DID); |
| 2078 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2079 | } |
| 2080 | } |
| 2081 | else if((CURLE_SEND_ERROR == result) && |
| 2082 | data->conn->bits.reuse) { |
| 2083 | /* |
| 2084 | * In this situation, a connection that we were trying to use |
| 2085 | * may have unexpectedly died. If possible, send the connection |
| 2086 | * back to the CONNECT phase so we can try again. |
| 2087 | */ |
| 2088 | char *newurl = NULL; |
| 2089 | followtype follow = FOLLOW_NONE; |
| 2090 | CURLcode drc; |
| 2091 | |
| 2092 | drc = Curl_retry_request(data, &newurl); |
| 2093 | if(drc) { |
| 2094 | /* a failure here pretty much implies an out of memory */ |
| 2095 | result = drc; |
| 2096 | stream_error = TRUE; |
| 2097 | } |
| 2098 | |
| 2099 | Curl_posttransfer(data); |
| 2100 | drc = multi_done(data, result, FALSE); |
| 2101 | |
| 2102 | /* When set to retry the connection, we must to go back to |
| 2103 | * the CONNECT state */ |
| 2104 | if(newurl) { |
| 2105 | if(!drc || (drc == CURLE_SEND_ERROR)) { |
| 2106 | follow = FOLLOW_RETRY; |
| 2107 | drc = Curl_follow(data, newurl, follow); |
| 2108 | if(!drc) { |
| 2109 | multistate(data, MSTATE_CONNECT); |
| 2110 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2111 | result = CURLE_OK; |
| 2112 | } |
| 2113 | else { |
| 2114 | /* Follow failed */ |
| 2115 | result = drc; |
| 2116 | } |
| 2117 | } |
| 2118 | else { |
| 2119 | /* done didn't return OK or SEND_ERROR */ |
| 2120 | result = drc; |
| 2121 | } |
| 2122 | } |
| 2123 | else { |
| 2124 | /* Have error handler disconnect conn if we can't retry */ |
| 2125 | stream_error = TRUE; |
| 2126 | } |
| 2127 | free(newurl); |
| 2128 | } |
| 2129 | else { |
| 2130 | /* failure detected */ |
| 2131 | Curl_posttransfer(data); |
| 2132 | if(data->conn) |
| 2133 | multi_done(data, result, FALSE); |
| 2134 | stream_error = TRUE; |
| 2135 | } |
| 2136 | } |
| 2137 | break; |
| 2138 | |
| 2139 | case MSTATE_DOING: |
| 2140 | /* we continue DOING until the DO phase is complete */ |
| 2141 | DEBUGASSERT(data->conn); |
| 2142 | result = protocol_doing(data, &dophase_done); |
| 2143 | if(!result) { |
| 2144 | if(dophase_done) { |
| 2145 | /* after DO, go DO_DONE or DO_MORE */ |
| 2146 | multistate(data, data->conn->bits.do_more? |
| 2147 | MSTATE_DOING_MORE : MSTATE_DID); |
| 2148 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2149 | } /* dophase_done */ |
| 2150 | } |
| 2151 | else { |
| 2152 | /* failure detected */ |
| 2153 | Curl_posttransfer(data); |
| 2154 | multi_done(data, result, FALSE); |
| 2155 | stream_error = TRUE; |
| 2156 | } |
| 2157 | break; |
| 2158 | |
| 2159 | case MSTATE_DOING_MORE: |
| 2160 | /* |
| 2161 | * When we are connected, DOING MORE and then go DID |
| 2162 | */ |
| 2163 | DEBUGASSERT(data->conn); |
| 2164 | result = multi_do_more(data, &control); |
| 2165 | |
| 2166 | if(!result) { |
| 2167 | if(control) { |
| 2168 | /* if positive, advance to DO_DONE |
| 2169 | if negative, go back to DOING */ |
| 2170 | multistate(data, control == 1? |
| 2171 | MSTATE_DID : MSTATE_DOING); |
| 2172 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2173 | } |
| 2174 | else |
| 2175 | /* stay in DO_MORE */ |
| 2176 | rc = CURLM_OK; |
| 2177 | } |
| 2178 | else { |
| 2179 | /* failure detected */ |
| 2180 | Curl_posttransfer(data); |
| 2181 | multi_done(data, result, FALSE); |
| 2182 | stream_error = TRUE; |
| 2183 | } |
| 2184 | break; |
| 2185 | |
| 2186 | case MSTATE_DID: |
| 2187 | DEBUGASSERT(data->conn); |
| 2188 | if(data->conn->bits.multiplex) |
| 2189 | /* Check if we can move pending requests to send pipe */ |
| 2190 | process_pending_handles(multi); /* multiplexed */ |
| 2191 | |
| 2192 | /* Only perform the transfer if there's a good socket to work with. |
| 2193 | Having both BAD is a signal to skip immediately to DONE */ |
| 2194 | if((data->conn->sockfd != CURL_SOCKET_BAD) || |
| 2195 | (data->conn->writesockfd != CURL_SOCKET_BAD)) |
| 2196 | multistate(data, MSTATE_PERFORMING); |
| 2197 | else { |
| 2198 | #ifndef CURL_DISABLE_FTP |
| 2199 | if(data->state.wildcardmatch && |
| 2200 | ((data->conn->handler->flags & PROTOPT_WILDCARD) == 0)) { |
| 2201 | data->wildcard.state = CURLWC_DONE; |
| 2202 | } |
| 2203 | #endif |
| 2204 | multistate(data, MSTATE_DONE); |
| 2205 | } |
| 2206 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2207 | break; |
| 2208 | |
| 2209 | case MSTATE_RATELIMITING: /* limit-rate exceeded in either direction */ |
| 2210 | DEBUGASSERT(data->conn); |
| 2211 | /* if both rates are within spec, resume transfer */ |
| 2212 | if(Curl_pgrsUpdate(data)) |
| 2213 | result = CURLE_ABORTED_BY_CALLBACK; |
| 2214 | else |
| 2215 | result = Curl_speedcheck(data, *nowp); |
| 2216 | |
| 2217 | if(result) { |
| 2218 | if(!(data->conn->handler->flags & PROTOPT_DUAL) && |
| 2219 | result != CURLE_HTTP2_STREAM) |
| 2220 | streamclose(data->conn, "Transfer returned error" ); |
| 2221 | |
| 2222 | Curl_posttransfer(data); |
| 2223 | multi_done(data, result, TRUE); |
| 2224 | } |
| 2225 | else { |
| 2226 | send_timeout_ms = 0; |
| 2227 | if(data->set.max_send_speed) |
| 2228 | send_timeout_ms = |
| 2229 | Curl_pgrsLimitWaitTime(data->progress.uploaded, |
| 2230 | data->progress.ul_limit_size, |
| 2231 | data->set.max_send_speed, |
| 2232 | data->progress.ul_limit_start, |
| 2233 | *nowp); |
| 2234 | |
| 2235 | recv_timeout_ms = 0; |
| 2236 | if(data->set.max_recv_speed) |
| 2237 | recv_timeout_ms = |
| 2238 | Curl_pgrsLimitWaitTime(data->progress.downloaded, |
| 2239 | data->progress.dl_limit_size, |
| 2240 | data->set.max_recv_speed, |
| 2241 | data->progress.dl_limit_start, |
| 2242 | *nowp); |
| 2243 | |
| 2244 | if(!send_timeout_ms && !recv_timeout_ms) { |
| 2245 | multistate(data, MSTATE_PERFORMING); |
| 2246 | Curl_ratelimit(data, *nowp); |
| 2247 | } |
| 2248 | else if(send_timeout_ms >= recv_timeout_ms) |
| 2249 | Curl_expire(data, send_timeout_ms, EXPIRE_TOOFAST); |
| 2250 | else |
| 2251 | Curl_expire(data, recv_timeout_ms, EXPIRE_TOOFAST); |
| 2252 | } |
| 2253 | break; |
| 2254 | |
| 2255 | case MSTATE_PERFORMING: |
| 2256 | { |
| 2257 | char *newurl = NULL; |
| 2258 | bool retry = FALSE; |
| 2259 | bool comeback = FALSE; |
| 2260 | DEBUGASSERT(data->state.buffer); |
| 2261 | /* check if over send speed */ |
| 2262 | send_timeout_ms = 0; |
| 2263 | if(data->set.max_send_speed) |
| 2264 | send_timeout_ms = Curl_pgrsLimitWaitTime(data->progress.uploaded, |
| 2265 | data->progress.ul_limit_size, |
| 2266 | data->set.max_send_speed, |
| 2267 | data->progress.ul_limit_start, |
| 2268 | *nowp); |
| 2269 | |
| 2270 | /* check if over recv speed */ |
| 2271 | recv_timeout_ms = 0; |
| 2272 | if(data->set.max_recv_speed) |
| 2273 | recv_timeout_ms = Curl_pgrsLimitWaitTime(data->progress.downloaded, |
| 2274 | data->progress.dl_limit_size, |
| 2275 | data->set.max_recv_speed, |
| 2276 | data->progress.dl_limit_start, |
| 2277 | *nowp); |
| 2278 | |
| 2279 | if(send_timeout_ms || recv_timeout_ms) { |
| 2280 | Curl_ratelimit(data, *nowp); |
| 2281 | multistate(data, MSTATE_RATELIMITING); |
| 2282 | if(send_timeout_ms >= recv_timeout_ms) |
| 2283 | Curl_expire(data, send_timeout_ms, EXPIRE_TOOFAST); |
| 2284 | else |
| 2285 | Curl_expire(data, recv_timeout_ms, EXPIRE_TOOFAST); |
| 2286 | break; |
| 2287 | } |
| 2288 | |
| 2289 | /* read/write data if it is ready to do so */ |
| 2290 | result = Curl_readwrite(data->conn, data, &done, &comeback); |
| 2291 | |
| 2292 | if(done || (result == CURLE_RECV_ERROR)) { |
| 2293 | /* If CURLE_RECV_ERROR happens early enough, we assume it was a race |
| 2294 | * condition and the server closed the re-used connection exactly when |
| 2295 | * we wanted to use it, so figure out if that is indeed the case. |
| 2296 | */ |
| 2297 | CURLcode ret = Curl_retry_request(data, &newurl); |
| 2298 | if(!ret) |
| 2299 | retry = (newurl)?TRUE:FALSE; |
| 2300 | else if(!result) |
| 2301 | result = ret; |
| 2302 | |
| 2303 | if(retry) { |
| 2304 | /* if we are to retry, set the result to OK and consider the |
| 2305 | request as done */ |
| 2306 | result = CURLE_OK; |
| 2307 | done = TRUE; |
| 2308 | } |
| 2309 | } |
| 2310 | else if((CURLE_HTTP2_STREAM == result) && |
| 2311 | Curl_h2_http_1_1_error(data)) { |
| 2312 | CURLcode ret = Curl_retry_request(data, &newurl); |
| 2313 | |
| 2314 | if(!ret) { |
| 2315 | infof(data, "Downgrades to HTTP/1.1!" ); |
| 2316 | streamclose(data->conn, "Disconnect HTTP/2 for HTTP/1" ); |
| 2317 | data->state.httpwant = CURL_HTTP_VERSION_1_1; |
| 2318 | /* clear the error message bit too as we ignore the one we got */ |
| 2319 | data->state.errorbuf = FALSE; |
| 2320 | if(!newurl) |
| 2321 | /* typically for HTTP_1_1_REQUIRED error on first flight */ |
| 2322 | newurl = strdup(data->state.url); |
| 2323 | /* if we are to retry, set the result to OK and consider the request |
| 2324 | as done */ |
| 2325 | retry = TRUE; |
| 2326 | result = CURLE_OK; |
| 2327 | done = TRUE; |
| 2328 | } |
| 2329 | else |
| 2330 | result = ret; |
| 2331 | } |
| 2332 | |
| 2333 | if(result) { |
| 2334 | /* |
| 2335 | * The transfer phase returned error, we mark the connection to get |
| 2336 | * closed to prevent being re-used. This is because we can't possibly |
| 2337 | * know if the connection is in a good shape or not now. Unless it is |
| 2338 | * a protocol which uses two "channels" like FTP, as then the error |
| 2339 | * happened in the data connection. |
| 2340 | */ |
| 2341 | |
| 2342 | if(!(data->conn->handler->flags & PROTOPT_DUAL) && |
| 2343 | result != CURLE_HTTP2_STREAM) |
| 2344 | streamclose(data->conn, "Transfer returned error" ); |
| 2345 | |
| 2346 | Curl_posttransfer(data); |
| 2347 | multi_done(data, result, TRUE); |
| 2348 | } |
| 2349 | else if(done) { |
| 2350 | |
| 2351 | /* call this even if the readwrite function returned error */ |
| 2352 | Curl_posttransfer(data); |
| 2353 | |
| 2354 | /* When we follow redirects or is set to retry the connection, we must |
| 2355 | to go back to the CONNECT state */ |
| 2356 | if(data->req.newurl || retry) { |
| 2357 | followtype follow = FOLLOW_NONE; |
| 2358 | if(!retry) { |
| 2359 | /* if the URL is a follow-location and not just a retried request |
| 2360 | then figure out the URL here */ |
| 2361 | free(newurl); |
| 2362 | newurl = data->req.newurl; |
| 2363 | data->req.newurl = NULL; |
| 2364 | follow = FOLLOW_REDIR; |
| 2365 | } |
| 2366 | else |
| 2367 | follow = FOLLOW_RETRY; |
| 2368 | (void)multi_done(data, CURLE_OK, FALSE); |
| 2369 | /* multi_done() might return CURLE_GOT_NOTHING */ |
| 2370 | result = Curl_follow(data, newurl, follow); |
| 2371 | if(!result) { |
| 2372 | multistate(data, MSTATE_CONNECT); |
| 2373 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2374 | } |
| 2375 | free(newurl); |
| 2376 | } |
| 2377 | else { |
| 2378 | /* after the transfer is done, go DONE */ |
| 2379 | |
| 2380 | /* but first check to see if we got a location info even though we're |
| 2381 | not following redirects */ |
| 2382 | if(data->req.location) { |
| 2383 | free(newurl); |
| 2384 | newurl = data->req.location; |
| 2385 | data->req.location = NULL; |
| 2386 | result = Curl_follow(data, newurl, FOLLOW_FAKE); |
| 2387 | free(newurl); |
| 2388 | if(result) { |
| 2389 | stream_error = TRUE; |
| 2390 | result = multi_done(data, result, TRUE); |
| 2391 | } |
| 2392 | } |
| 2393 | |
| 2394 | if(!result) { |
| 2395 | multistate(data, MSTATE_DONE); |
| 2396 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2397 | } |
| 2398 | } |
| 2399 | } |
| 2400 | else if(comeback) { |
| 2401 | /* This avoids CURLM_CALL_MULTI_PERFORM so that a very fast transfer |
| 2402 | won't get stuck on this transfer at the expense of other concurrent |
| 2403 | transfers */ |
| 2404 | Curl_expire(data, 0, EXPIRE_RUN_NOW); |
| 2405 | rc = CURLM_OK; |
| 2406 | } |
| 2407 | break; |
| 2408 | } |
| 2409 | |
| 2410 | case MSTATE_DONE: |
| 2411 | /* this state is highly transient, so run another loop after this */ |
| 2412 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2413 | |
| 2414 | if(data->conn) { |
| 2415 | CURLcode res; |
| 2416 | |
| 2417 | if(data->conn->bits.multiplex) |
| 2418 | /* Check if we can move pending requests to connection */ |
| 2419 | process_pending_handles(multi); /* multiplexing */ |
| 2420 | |
| 2421 | /* post-transfer command */ |
| 2422 | res = multi_done(data, result, FALSE); |
| 2423 | |
| 2424 | /* allow a previously set error code take precedence */ |
| 2425 | if(!result) |
| 2426 | result = res; |
| 2427 | } |
| 2428 | |
| 2429 | #ifndef CURL_DISABLE_FTP |
| 2430 | if(data->state.wildcardmatch) { |
| 2431 | if(data->wildcard.state != CURLWC_DONE) { |
| 2432 | /* if a wildcard is set and we are not ending -> lets start again |
| 2433 | with MSTATE_INIT */ |
| 2434 | multistate(data, MSTATE_INIT); |
| 2435 | break; |
| 2436 | } |
| 2437 | } |
| 2438 | #endif |
| 2439 | /* after we have DONE what we're supposed to do, go COMPLETED, and |
| 2440 | it doesn't matter what the multi_done() returned! */ |
| 2441 | multistate(data, MSTATE_COMPLETED); |
| 2442 | break; |
| 2443 | |
| 2444 | case MSTATE_COMPLETED: |
| 2445 | break; |
| 2446 | |
| 2447 | case MSTATE_MSGSENT: |
| 2448 | data->result = result; |
| 2449 | return CURLM_OK; /* do nothing */ |
| 2450 | |
| 2451 | default: |
| 2452 | return CURLM_INTERNAL_ERROR; |
| 2453 | } |
| 2454 | |
| 2455 | if(data->conn && |
| 2456 | data->mstate >= MSTATE_CONNECT && |
| 2457 | data->mstate < MSTATE_DO && |
| 2458 | rc != CURLM_CALL_MULTI_PERFORM && |
| 2459 | !multi_ischanged(multi, false)) { |
| 2460 | /* We now handle stream timeouts if and only if this will be the last |
| 2461 | * loop iteration. We only check this on the last iteration to ensure |
| 2462 | * that if we know we have additional work to do immediately |
| 2463 | * (i.e. CURLM_CALL_MULTI_PERFORM == TRUE) then we should do that before |
| 2464 | * declaring the connection timed out as we may almost have a completed |
| 2465 | * connection. */ |
| 2466 | multi_handle_timeout(data, nowp, &stream_error, &result, TRUE); |
| 2467 | } |
| 2468 | |
| 2469 | statemachine_end: |
| 2470 | |
| 2471 | if(data->mstate < MSTATE_COMPLETED) { |
| 2472 | if(result) { |
| 2473 | /* |
| 2474 | * If an error was returned, and we aren't in completed state now, |
| 2475 | * then we go to completed and consider this transfer aborted. |
| 2476 | */ |
| 2477 | |
| 2478 | /* NOTE: no attempt to disconnect connections must be made |
| 2479 | in the case blocks above - cleanup happens only here */ |
| 2480 | |
| 2481 | /* Check if we can move pending requests to send pipe */ |
| 2482 | process_pending_handles(multi); /* connection */ |
| 2483 | |
| 2484 | if(data->conn) { |
| 2485 | if(stream_error) { |
| 2486 | /* Don't attempt to send data over a connection that timed out */ |
| 2487 | bool dead_connection = result == CURLE_OPERATION_TIMEDOUT; |
| 2488 | struct connectdata *conn = data->conn; |
| 2489 | |
| 2490 | /* This is where we make sure that the conn pointer is reset. |
| 2491 | We don't have to do this in every case block above where a |
| 2492 | failure is detected */ |
| 2493 | Curl_detach_connnection(data); |
| 2494 | |
| 2495 | /* remove connection from cache */ |
| 2496 | Curl_conncache_remove_conn(data, conn, TRUE); |
| 2497 | |
| 2498 | /* disconnect properly */ |
| 2499 | Curl_disconnect(data, conn, dead_connection); |
| 2500 | } |
| 2501 | } |
| 2502 | else if(data->mstate == MSTATE_CONNECT) { |
| 2503 | /* Curl_connect() failed */ |
| 2504 | (void)Curl_posttransfer(data); |
| 2505 | } |
| 2506 | |
| 2507 | multistate(data, MSTATE_COMPLETED); |
| 2508 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2509 | } |
| 2510 | /* if there's still a connection to use, call the progress function */ |
| 2511 | else if(data->conn && Curl_pgrsUpdate(data)) { |
| 2512 | /* aborted due to progress callback return code must close the |
| 2513 | connection */ |
| 2514 | result = CURLE_ABORTED_BY_CALLBACK; |
| 2515 | streamclose(data->conn, "Aborted by callback" ); |
| 2516 | |
| 2517 | /* if not yet in DONE state, go there, otherwise COMPLETED */ |
| 2518 | multistate(data, (data->mstate < MSTATE_DONE)? |
| 2519 | MSTATE_DONE: MSTATE_COMPLETED); |
| 2520 | rc = CURLM_CALL_MULTI_PERFORM; |
| 2521 | } |
| 2522 | } |
| 2523 | |
| 2524 | if(MSTATE_COMPLETED == data->mstate) { |
| 2525 | if(data->set.fmultidone) { |
| 2526 | /* signal via callback instead */ |
| 2527 | data->set.fmultidone(data, result); |
| 2528 | } |
| 2529 | else { |
| 2530 | /* now fill in the Curl_message with this info */ |
| 2531 | msg = &data->msg; |
| 2532 | |
| 2533 | msg->extmsg.msg = CURLMSG_DONE; |
| 2534 | msg->extmsg.easy_handle = data; |
| 2535 | msg->extmsg.data.result = result; |
| 2536 | |
| 2537 | rc = multi_addmsg(multi, msg); |
| 2538 | DEBUGASSERT(!data->conn); |
| 2539 | } |
| 2540 | multistate(data, MSTATE_MSGSENT); |
| 2541 | } |
| 2542 | } while((rc == CURLM_CALL_MULTI_PERFORM) || multi_ischanged(multi, FALSE)); |
| 2543 | |
| 2544 | data->result = result; |
| 2545 | return rc; |
| 2546 | } |
| 2547 | |
| 2548 | |
| 2549 | CURLMcode curl_multi_perform(struct Curl_multi *multi, int *running_handles) |
| 2550 | { |
| 2551 | struct Curl_easy *data; |
| 2552 | CURLMcode returncode = CURLM_OK; |
| 2553 | struct Curl_tree *t; |
| 2554 | struct curltime now = Curl_now(); |
| 2555 | |
| 2556 | if(!GOOD_MULTI_HANDLE(multi)) |
| 2557 | return CURLM_BAD_HANDLE; |
| 2558 | |
| 2559 | if(multi->in_callback) |
| 2560 | return CURLM_RECURSIVE_API_CALL; |
| 2561 | |
| 2562 | data = multi->easyp; |
| 2563 | while(data) { |
| 2564 | CURLMcode result; |
| 2565 | SIGPIPE_VARIABLE(pipe_st); |
| 2566 | |
| 2567 | sigpipe_ignore(data, &pipe_st); |
| 2568 | result = multi_runsingle(multi, &now, data); |
| 2569 | sigpipe_restore(&pipe_st); |
| 2570 | |
| 2571 | if(result) |
| 2572 | returncode = result; |
| 2573 | |
| 2574 | data = data->next; /* operate on next handle */ |
| 2575 | } |
| 2576 | |
| 2577 | /* |
| 2578 | * Simply remove all expired timers from the splay since handles are dealt |
| 2579 | * with unconditionally by this function and curl_multi_timeout() requires |
| 2580 | * that already passed/handled expire times are removed from the splay. |
| 2581 | * |
| 2582 | * It is important that the 'now' value is set at the entry of this function |
| 2583 | * and not for the current time as it may have ticked a little while since |
| 2584 | * then and then we risk this loop to remove timers that actually have not |
| 2585 | * been handled! |
| 2586 | */ |
| 2587 | do { |
| 2588 | multi->timetree = Curl_splaygetbest(now, multi->timetree, &t); |
| 2589 | if(t) |
| 2590 | /* the removed may have another timeout in queue */ |
| 2591 | (void)add_next_timeout(now, multi, t->payload); |
| 2592 | |
| 2593 | } while(t); |
| 2594 | |
| 2595 | *running_handles = multi->num_alive; |
| 2596 | |
| 2597 | if(CURLM_OK >= returncode) |
| 2598 | Curl_update_timer(multi); |
| 2599 | |
| 2600 | return returncode; |
| 2601 | } |
| 2602 | |
| 2603 | CURLMcode curl_multi_cleanup(struct Curl_multi *multi) |
| 2604 | { |
| 2605 | struct Curl_easy *data; |
| 2606 | struct Curl_easy *nextdata; |
| 2607 | |
| 2608 | if(GOOD_MULTI_HANDLE(multi)) { |
| 2609 | if(multi->in_callback) |
| 2610 | return CURLM_RECURSIVE_API_CALL; |
| 2611 | |
| 2612 | multi->magic = 0; /* not good anymore */ |
| 2613 | |
| 2614 | /* Firsrt remove all remaining easy handles */ |
| 2615 | data = multi->easyp; |
| 2616 | while(data) { |
| 2617 | nextdata = data->next; |
| 2618 | if(!data->state.done && data->conn) |
| 2619 | /* if DONE was never called for this handle */ |
| 2620 | (void)multi_done(data, CURLE_OK, TRUE); |
| 2621 | if(data->dns.hostcachetype == HCACHE_MULTI) { |
| 2622 | /* clear out the usage of the shared DNS cache */ |
| 2623 | Curl_hostcache_clean(data, data->dns.hostcache); |
| 2624 | data->dns.hostcache = NULL; |
| 2625 | data->dns.hostcachetype = HCACHE_NONE; |
| 2626 | } |
| 2627 | |
| 2628 | /* Clear the pointer to the connection cache */ |
| 2629 | data->state.conn_cache = NULL; |
| 2630 | data->multi = NULL; /* clear the association */ |
| 2631 | |
| 2632 | #ifdef USE_LIBPSL |
| 2633 | if(data->psl == &multi->psl) |
| 2634 | data->psl = NULL; |
| 2635 | #endif |
| 2636 | |
| 2637 | data = nextdata; |
| 2638 | } |
| 2639 | |
| 2640 | /* Close all the connections in the connection cache */ |
| 2641 | Curl_conncache_close_all_connections(&multi->conn_cache); |
| 2642 | |
| 2643 | Curl_hash_destroy(&multi->sockhash); |
| 2644 | Curl_conncache_destroy(&multi->conn_cache); |
| 2645 | Curl_llist_destroy(&multi->msglist, NULL); |
| 2646 | Curl_llist_destroy(&multi->pending, NULL); |
| 2647 | |
| 2648 | Curl_hash_destroy(&multi->hostcache); |
| 2649 | Curl_psl_destroy(&multi->psl); |
| 2650 | |
| 2651 | #ifdef USE_WINSOCK |
| 2652 | WSACloseEvent(multi->wsa_event); |
| 2653 | #else |
| 2654 | #ifdef ENABLE_WAKEUP |
| 2655 | sclose(multi->wakeup_pair[0]); |
| 2656 | sclose(multi->wakeup_pair[1]); |
| 2657 | #endif |
| 2658 | #endif |
| 2659 | free(multi); |
| 2660 | |
| 2661 | return CURLM_OK; |
| 2662 | } |
| 2663 | return CURLM_BAD_HANDLE; |
| 2664 | } |
| 2665 | |
| 2666 | /* |
| 2667 | * curl_multi_info_read() |
| 2668 | * |
| 2669 | * This function is the primary way for a multi/multi_socket application to |
| 2670 | * figure out if a transfer has ended. We MUST make this function as fast as |
| 2671 | * possible as it will be polled frequently and we MUST NOT scan any lists in |
| 2672 | * here to figure out things. We must scale fine to thousands of handles and |
| 2673 | * beyond. The current design is fully O(1). |
| 2674 | */ |
| 2675 | |
| 2676 | CURLMsg *curl_multi_info_read(struct Curl_multi *multi, int *msgs_in_queue) |
| 2677 | { |
| 2678 | struct Curl_message *msg; |
| 2679 | |
| 2680 | *msgs_in_queue = 0; /* default to none */ |
| 2681 | |
| 2682 | if(GOOD_MULTI_HANDLE(multi) && |
| 2683 | !multi->in_callback && |
| 2684 | Curl_llist_count(&multi->msglist)) { |
| 2685 | /* there is one or more messages in the list */ |
| 2686 | struct Curl_llist_element *e; |
| 2687 | |
| 2688 | /* extract the head of the list to return */ |
| 2689 | e = multi->msglist.head; |
| 2690 | |
| 2691 | msg = e->ptr; |
| 2692 | |
| 2693 | /* remove the extracted entry */ |
| 2694 | Curl_llist_remove(&multi->msglist, e, NULL); |
| 2695 | |
| 2696 | *msgs_in_queue = curlx_uztosi(Curl_llist_count(&multi->msglist)); |
| 2697 | |
| 2698 | return &msg->extmsg; |
| 2699 | } |
| 2700 | return NULL; |
| 2701 | } |
| 2702 | |
| 2703 | /* |
| 2704 | * singlesocket() checks what sockets we deal with and their "action state" |
| 2705 | * and if we have a different state in any of those sockets from last time we |
| 2706 | * call the callback accordingly. |
| 2707 | */ |
| 2708 | static CURLMcode singlesocket(struct Curl_multi *multi, |
| 2709 | struct Curl_easy *data) |
| 2710 | { |
| 2711 | curl_socket_t socks[MAX_SOCKSPEREASYHANDLE]; |
| 2712 | int i; |
| 2713 | struct Curl_sh_entry *entry; |
| 2714 | curl_socket_t s; |
| 2715 | int num; |
| 2716 | unsigned int curraction; |
| 2717 | unsigned char actions[MAX_SOCKSPEREASYHANDLE]; |
| 2718 | |
| 2719 | for(i = 0; i< MAX_SOCKSPEREASYHANDLE; i++) |
| 2720 | socks[i] = CURL_SOCKET_BAD; |
| 2721 | |
| 2722 | /* Fill in the 'current' struct with the state as it is now: what sockets to |
| 2723 | supervise and for what actions */ |
| 2724 | curraction = multi_getsock(data, socks); |
| 2725 | |
| 2726 | /* We have 0 .. N sockets already and we get to know about the 0 .. M |
| 2727 | sockets we should have from now on. Detect the differences, remove no |
| 2728 | longer supervised ones and add new ones */ |
| 2729 | |
| 2730 | /* walk over the sockets we got right now */ |
| 2731 | for(i = 0; (i< MAX_SOCKSPEREASYHANDLE) && |
| 2732 | (curraction & (GETSOCK_READSOCK(i) | GETSOCK_WRITESOCK(i))); |
| 2733 | i++) { |
| 2734 | unsigned char action = CURL_POLL_NONE; |
| 2735 | unsigned char prevaction = 0; |
| 2736 | int comboaction; |
| 2737 | bool sincebefore = FALSE; |
| 2738 | |
| 2739 | s = socks[i]; |
| 2740 | |
| 2741 | /* get it from the hash */ |
| 2742 | entry = sh_getentry(&multi->sockhash, s); |
| 2743 | |
| 2744 | if(curraction & GETSOCK_READSOCK(i)) |
| 2745 | action |= CURL_POLL_IN; |
| 2746 | if(curraction & GETSOCK_WRITESOCK(i)) |
| 2747 | action |= CURL_POLL_OUT; |
| 2748 | |
| 2749 | actions[i] = action; |
| 2750 | if(entry) { |
| 2751 | /* check if new for this transfer */ |
| 2752 | int j; |
| 2753 | for(j = 0; j< data->numsocks; j++) { |
| 2754 | if(s == data->sockets[j]) { |
| 2755 | prevaction = data->actions[j]; |
| 2756 | sincebefore = TRUE; |
| 2757 | break; |
| 2758 | } |
| 2759 | } |
| 2760 | } |
| 2761 | else { |
| 2762 | /* this is a socket we didn't have before, add it to the hash! */ |
| 2763 | entry = sh_addentry(&multi->sockhash, s); |
| 2764 | if(!entry) |
| 2765 | /* fatal */ |
| 2766 | return CURLM_OUT_OF_MEMORY; |
| 2767 | } |
| 2768 | if(sincebefore && (prevaction != action)) { |
| 2769 | /* Socket was used already, but different action now */ |
| 2770 | if(prevaction & CURL_POLL_IN) |
| 2771 | entry->readers--; |
| 2772 | if(prevaction & CURL_POLL_OUT) |
| 2773 | entry->writers--; |
| 2774 | if(action & CURL_POLL_IN) |
| 2775 | entry->readers++; |
| 2776 | if(action & CURL_POLL_OUT) |
| 2777 | entry->writers++; |
| 2778 | } |
| 2779 | else if(!sincebefore) { |
| 2780 | /* a new user */ |
| 2781 | entry->users++; |
| 2782 | if(action & CURL_POLL_IN) |
| 2783 | entry->readers++; |
| 2784 | if(action & CURL_POLL_OUT) |
| 2785 | entry->writers++; |
| 2786 | |
| 2787 | /* add 'data' to the transfer hash on this socket! */ |
| 2788 | if(!Curl_hash_add(&entry->transfers, (char *)&data, /* hash key */ |
| 2789 | sizeof(struct Curl_easy *), data)) |
| 2790 | return CURLM_OUT_OF_MEMORY; |
| 2791 | } |
| 2792 | |
| 2793 | comboaction = (entry->writers? CURL_POLL_OUT : 0) | |
| 2794 | (entry->readers ? CURL_POLL_IN : 0); |
| 2795 | |
| 2796 | /* socket existed before and has the same action set as before */ |
| 2797 | if(sincebefore && ((int)entry->action == comboaction)) |
| 2798 | /* same, continue */ |
| 2799 | continue; |
| 2800 | |
| 2801 | if(multi->socket_cb) |
| 2802 | multi->socket_cb(data, s, comboaction, multi->socket_userp, |
| 2803 | entry->socketp); |
| 2804 | |
| 2805 | entry->action = comboaction; /* store the current action state */ |
| 2806 | } |
| 2807 | |
| 2808 | num = i; /* number of sockets */ |
| 2809 | |
| 2810 | /* when we've walked over all the sockets we should have right now, we must |
| 2811 | make sure to detect sockets that are removed */ |
| 2812 | for(i = 0; i< data->numsocks; i++) { |
| 2813 | int j; |
| 2814 | bool stillused = FALSE; |
| 2815 | s = data->sockets[i]; |
| 2816 | for(j = 0; j < num; j++) { |
| 2817 | if(s == socks[j]) { |
| 2818 | /* this is still supervised */ |
| 2819 | stillused = TRUE; |
| 2820 | break; |
| 2821 | } |
| 2822 | } |
| 2823 | if(stillused) |
| 2824 | continue; |
| 2825 | |
| 2826 | entry = sh_getentry(&multi->sockhash, s); |
| 2827 | /* if this is NULL here, the socket has been closed and notified so |
| 2828 | already by Curl_multi_closed() */ |
| 2829 | if(entry) { |
| 2830 | unsigned char oldactions = data->actions[i]; |
| 2831 | /* this socket has been removed. Decrease user count */ |
| 2832 | entry->users--; |
| 2833 | if(oldactions & CURL_POLL_OUT) |
| 2834 | entry->writers--; |
| 2835 | if(oldactions & CURL_POLL_IN) |
| 2836 | entry->readers--; |
| 2837 | if(!entry->users) { |
| 2838 | if(multi->socket_cb) |
| 2839 | multi->socket_cb(data, s, CURL_POLL_REMOVE, |
| 2840 | multi->socket_userp, |
| 2841 | entry->socketp); |
| 2842 | sh_delentry(entry, &multi->sockhash, s); |
| 2843 | } |
| 2844 | else { |
| 2845 | /* still users, but remove this handle as a user of this socket */ |
| 2846 | if(Curl_hash_delete(&entry->transfers, (char *)&data, |
| 2847 | sizeof(struct Curl_easy *))) { |
| 2848 | DEBUGASSERT(NULL); |
| 2849 | } |
| 2850 | } |
| 2851 | } |
| 2852 | } /* for loop over numsocks */ |
| 2853 | |
| 2854 | memcpy(data->sockets, socks, num*sizeof(curl_socket_t)); |
| 2855 | memcpy(data->actions, actions, num*sizeof(char)); |
| 2856 | data->numsocks = num; |
| 2857 | return CURLM_OK; |
| 2858 | } |
| 2859 | |
| 2860 | void Curl_updatesocket(struct Curl_easy *data) |
| 2861 | { |
| 2862 | singlesocket(data->multi, data); |
| 2863 | } |
| 2864 | |
| 2865 | |
| 2866 | /* |
| 2867 | * Curl_multi_closed() |
| 2868 | * |
| 2869 | * Used by the connect code to tell the multi_socket code that one of the |
| 2870 | * sockets we were using is about to be closed. This function will then |
| 2871 | * remove it from the sockethash for this handle to make the multi_socket API |
| 2872 | * behave properly, especially for the case when libcurl will create another |
| 2873 | * socket again and it gets the same file descriptor number. |
| 2874 | */ |
| 2875 | |
| 2876 | void Curl_multi_closed(struct Curl_easy *data, curl_socket_t s) |
| 2877 | { |
| 2878 | if(data) { |
| 2879 | /* if there's still an easy handle associated with this connection */ |
| 2880 | struct Curl_multi *multi = data->multi; |
| 2881 | if(multi) { |
| 2882 | /* this is set if this connection is part of a handle that is added to |
| 2883 | a multi handle, and only then this is necessary */ |
| 2884 | struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s); |
| 2885 | |
| 2886 | if(entry) { |
| 2887 | if(multi->socket_cb) |
| 2888 | multi->socket_cb(data, s, CURL_POLL_REMOVE, |
| 2889 | multi->socket_userp, |
| 2890 | entry->socketp); |
| 2891 | |
| 2892 | /* now remove it from the socket hash */ |
| 2893 | sh_delentry(entry, &multi->sockhash, s); |
| 2894 | } |
| 2895 | } |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | /* |
| 2900 | * add_next_timeout() |
| 2901 | * |
| 2902 | * Each Curl_easy has a list of timeouts. The add_next_timeout() is called |
| 2903 | * when it has just been removed from the splay tree because the timeout has |
| 2904 | * expired. This function is then to advance in the list to pick the next |
| 2905 | * timeout to use (skip the already expired ones) and add this node back to |
| 2906 | * the splay tree again. |
| 2907 | * |
| 2908 | * The splay tree only has each sessionhandle as a single node and the nearest |
| 2909 | * timeout is used to sort it on. |
| 2910 | */ |
| 2911 | static CURLMcode add_next_timeout(struct curltime now, |
| 2912 | struct Curl_multi *multi, |
| 2913 | struct Curl_easy *d) |
| 2914 | { |
| 2915 | struct curltime *tv = &d->state.expiretime; |
| 2916 | struct Curl_llist *list = &d->state.timeoutlist; |
| 2917 | struct Curl_llist_element *e; |
| 2918 | struct time_node *node = NULL; |
| 2919 | |
| 2920 | /* move over the timeout list for this specific handle and remove all |
| 2921 | timeouts that are now passed tense and store the next pending |
| 2922 | timeout in *tv */ |
| 2923 | for(e = list->head; e;) { |
| 2924 | struct Curl_llist_element *n = e->next; |
| 2925 | timediff_t diff; |
| 2926 | node = (struct time_node *)e->ptr; |
| 2927 | diff = Curl_timediff(node->time, now); |
| 2928 | if(diff <= 0) |
| 2929 | /* remove outdated entry */ |
| 2930 | Curl_llist_remove(list, e, NULL); |
| 2931 | else |
| 2932 | /* the list is sorted so get out on the first mismatch */ |
| 2933 | break; |
| 2934 | e = n; |
| 2935 | } |
| 2936 | e = list->head; |
| 2937 | if(!e) { |
| 2938 | /* clear the expire times within the handles that we remove from the |
| 2939 | splay tree */ |
| 2940 | tv->tv_sec = 0; |
| 2941 | tv->tv_usec = 0; |
| 2942 | } |
| 2943 | else { |
| 2944 | /* copy the first entry to 'tv' */ |
| 2945 | memcpy(tv, &node->time, sizeof(*tv)); |
| 2946 | |
| 2947 | /* Insert this node again into the splay. Keep the timer in the list in |
| 2948 | case we need to recompute future timers. */ |
| 2949 | multi->timetree = Curl_splayinsert(*tv, multi->timetree, |
| 2950 | &d->state.timenode); |
| 2951 | } |
| 2952 | return CURLM_OK; |
| 2953 | } |
| 2954 | |
| 2955 | static CURLMcode multi_socket(struct Curl_multi *multi, |
| 2956 | bool checkall, |
| 2957 | curl_socket_t s, |
| 2958 | int ev_bitmask, |
| 2959 | int *running_handles) |
| 2960 | { |
| 2961 | CURLMcode result = CURLM_OK; |
| 2962 | struct Curl_easy *data = NULL; |
| 2963 | struct Curl_tree *t; |
| 2964 | struct curltime now = Curl_now(); |
| 2965 | |
| 2966 | if(checkall) { |
| 2967 | /* *perform() deals with running_handles on its own */ |
| 2968 | result = curl_multi_perform(multi, running_handles); |
| 2969 | |
| 2970 | /* walk through each easy handle and do the socket state change magic |
| 2971 | and callbacks */ |
| 2972 | if(result != CURLM_BAD_HANDLE) { |
| 2973 | data = multi->easyp; |
| 2974 | while(data && !result) { |
| 2975 | result = singlesocket(multi, data); |
| 2976 | data = data->next; |
| 2977 | } |
| 2978 | } |
| 2979 | |
| 2980 | /* or should we fall-through and do the timer-based stuff? */ |
| 2981 | return result; |
| 2982 | } |
| 2983 | if(s != CURL_SOCKET_TIMEOUT) { |
| 2984 | struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s); |
| 2985 | |
| 2986 | if(!entry) |
| 2987 | /* Unmatched socket, we can't act on it but we ignore this fact. In |
| 2988 | real-world tests it has been proved that libevent can in fact give |
| 2989 | the application actions even though the socket was just previously |
| 2990 | asked to get removed, so thus we better survive stray socket actions |
| 2991 | and just move on. */ |
| 2992 | ; |
| 2993 | else { |
| 2994 | struct Curl_hash_iterator iter; |
| 2995 | struct Curl_hash_element *he; |
| 2996 | |
| 2997 | /* the socket can be shared by many transfers, iterate */ |
| 2998 | Curl_hash_start_iterate(&entry->transfers, &iter); |
| 2999 | for(he = Curl_hash_next_element(&iter); he; |
| 3000 | he = Curl_hash_next_element(&iter)) { |
| 3001 | data = (struct Curl_easy *)he->ptr; |
| 3002 | DEBUGASSERT(data); |
| 3003 | DEBUGASSERT(data->magic == CURLEASY_MAGIC_NUMBER); |
| 3004 | |
| 3005 | if(data->conn && !(data->conn->handler->flags & PROTOPT_DIRLOCK)) |
| 3006 | /* set socket event bitmask if they're not locked */ |
| 3007 | data->conn->cselect_bits = ev_bitmask; |
| 3008 | |
| 3009 | Curl_expire(data, 0, EXPIRE_RUN_NOW); |
| 3010 | } |
| 3011 | |
| 3012 | /* Now we fall-through and do the timer-based stuff, since we don't want |
| 3013 | to force the user to have to deal with timeouts as long as at least |
| 3014 | one connection in fact has traffic. */ |
| 3015 | |
| 3016 | data = NULL; /* set data to NULL again to avoid calling |
| 3017 | multi_runsingle() in case there's no need to */ |
| 3018 | now = Curl_now(); /* get a newer time since the multi_runsingle() loop |
| 3019 | may have taken some time */ |
| 3020 | } |
| 3021 | } |
| 3022 | else { |
| 3023 | /* Asked to run due to time-out. Clear the 'lastcall' variable to force |
| 3024 | Curl_update_timer() to trigger a callback to the app again even if the |
| 3025 | same timeout is still the one to run after this call. That handles the |
| 3026 | case when the application asks libcurl to run the timeout |
| 3027 | prematurely. */ |
| 3028 | memset(&multi->timer_lastcall, 0, sizeof(multi->timer_lastcall)); |
| 3029 | } |
| 3030 | |
| 3031 | /* |
| 3032 | * The loop following here will go on as long as there are expire-times left |
| 3033 | * to process in the splay and 'data' will be re-assigned for every expired |
| 3034 | * handle we deal with. |
| 3035 | */ |
| 3036 | do { |
| 3037 | /* the first loop lap 'data' can be NULL */ |
| 3038 | if(data) { |
| 3039 | SIGPIPE_VARIABLE(pipe_st); |
| 3040 | |
| 3041 | sigpipe_ignore(data, &pipe_st); |
| 3042 | result = multi_runsingle(multi, &now, data); |
| 3043 | sigpipe_restore(&pipe_st); |
| 3044 | |
| 3045 | if(CURLM_OK >= result) { |
| 3046 | /* get the socket(s) and check if the state has been changed since |
| 3047 | last */ |
| 3048 | result = singlesocket(multi, data); |
| 3049 | if(result) |
| 3050 | return result; |
| 3051 | } |
| 3052 | } |
| 3053 | |
| 3054 | /* Check if there's one (more) expired timer to deal with! This function |
| 3055 | extracts a matching node if there is one */ |
| 3056 | |
| 3057 | multi->timetree = Curl_splaygetbest(now, multi->timetree, &t); |
| 3058 | if(t) { |
| 3059 | data = t->payload; /* assign this for next loop */ |
| 3060 | (void)add_next_timeout(now, multi, t->payload); |
| 3061 | } |
| 3062 | |
| 3063 | } while(t); |
| 3064 | |
| 3065 | *running_handles = multi->num_alive; |
| 3066 | return result; |
| 3067 | } |
| 3068 | |
| 3069 | #undef curl_multi_setopt |
| 3070 | CURLMcode curl_multi_setopt(struct Curl_multi *multi, |
| 3071 | CURLMoption option, ...) |
| 3072 | { |
| 3073 | CURLMcode res = CURLM_OK; |
| 3074 | va_list param; |
| 3075 | |
| 3076 | if(!GOOD_MULTI_HANDLE(multi)) |
| 3077 | return CURLM_BAD_HANDLE; |
| 3078 | |
| 3079 | if(multi->in_callback) |
| 3080 | return CURLM_RECURSIVE_API_CALL; |
| 3081 | |
| 3082 | va_start(param, option); |
| 3083 | |
| 3084 | switch(option) { |
| 3085 | case CURLMOPT_SOCKETFUNCTION: |
| 3086 | multi->socket_cb = va_arg(param, curl_socket_callback); |
| 3087 | break; |
| 3088 | case CURLMOPT_SOCKETDATA: |
| 3089 | multi->socket_userp = va_arg(param, void *); |
| 3090 | break; |
| 3091 | case CURLMOPT_PUSHFUNCTION: |
| 3092 | multi->push_cb = va_arg(param, curl_push_callback); |
| 3093 | break; |
| 3094 | case CURLMOPT_PUSHDATA: |
| 3095 | multi->push_userp = va_arg(param, void *); |
| 3096 | break; |
| 3097 | case CURLMOPT_PIPELINING: |
| 3098 | multi->multiplexing = va_arg(param, long) & CURLPIPE_MULTIPLEX; |
| 3099 | break; |
| 3100 | case CURLMOPT_TIMERFUNCTION: |
| 3101 | multi->timer_cb = va_arg(param, curl_multi_timer_callback); |
| 3102 | break; |
| 3103 | case CURLMOPT_TIMERDATA: |
| 3104 | multi->timer_userp = va_arg(param, void *); |
| 3105 | break; |
| 3106 | case CURLMOPT_MAXCONNECTS: |
| 3107 | multi->maxconnects = va_arg(param, long); |
| 3108 | break; |
| 3109 | case CURLMOPT_MAX_HOST_CONNECTIONS: |
| 3110 | multi->max_host_connections = va_arg(param, long); |
| 3111 | break; |
| 3112 | case CURLMOPT_MAX_TOTAL_CONNECTIONS: |
| 3113 | multi->max_total_connections = va_arg(param, long); |
| 3114 | break; |
| 3115 | /* options formerly used for pipelining */ |
| 3116 | case CURLMOPT_MAX_PIPELINE_LENGTH: |
| 3117 | break; |
| 3118 | case CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE: |
| 3119 | break; |
| 3120 | case CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE: |
| 3121 | break; |
| 3122 | case CURLMOPT_PIPELINING_SITE_BL: |
| 3123 | break; |
| 3124 | case CURLMOPT_PIPELINING_SERVER_BL: |
| 3125 | break; |
| 3126 | case CURLMOPT_MAX_CONCURRENT_STREAMS: |
| 3127 | { |
| 3128 | long streams = va_arg(param, long); |
| 3129 | if(streams < 1) |
| 3130 | streams = 100; |
| 3131 | multi->max_concurrent_streams = curlx_sltoui(streams); |
| 3132 | } |
| 3133 | break; |
| 3134 | default: |
| 3135 | res = CURLM_UNKNOWN_OPTION; |
| 3136 | break; |
| 3137 | } |
| 3138 | va_end(param); |
| 3139 | return res; |
| 3140 | } |
| 3141 | |
| 3142 | /* we define curl_multi_socket() in the public multi.h header */ |
| 3143 | #undef curl_multi_socket |
| 3144 | |
| 3145 | CURLMcode curl_multi_socket(struct Curl_multi *multi, curl_socket_t s, |
| 3146 | int *running_handles) |
| 3147 | { |
| 3148 | CURLMcode result; |
| 3149 | if(multi->in_callback) |
| 3150 | return CURLM_RECURSIVE_API_CALL; |
| 3151 | result = multi_socket(multi, FALSE, s, 0, running_handles); |
| 3152 | if(CURLM_OK >= result) |
| 3153 | Curl_update_timer(multi); |
| 3154 | return result; |
| 3155 | } |
| 3156 | |
| 3157 | CURLMcode curl_multi_socket_action(struct Curl_multi *multi, curl_socket_t s, |
| 3158 | int ev_bitmask, int *running_handles) |
| 3159 | { |
| 3160 | CURLMcode result; |
| 3161 | if(multi->in_callback) |
| 3162 | return CURLM_RECURSIVE_API_CALL; |
| 3163 | result = multi_socket(multi, FALSE, s, ev_bitmask, running_handles); |
| 3164 | if(CURLM_OK >= result) |
| 3165 | Curl_update_timer(multi); |
| 3166 | return result; |
| 3167 | } |
| 3168 | |
| 3169 | CURLMcode curl_multi_socket_all(struct Curl_multi *multi, int *running_handles) |
| 3170 | { |
| 3171 | CURLMcode result; |
| 3172 | if(multi->in_callback) |
| 3173 | return CURLM_RECURSIVE_API_CALL; |
| 3174 | result = multi_socket(multi, TRUE, CURL_SOCKET_BAD, 0, running_handles); |
| 3175 | if(CURLM_OK >= result) |
| 3176 | Curl_update_timer(multi); |
| 3177 | return result; |
| 3178 | } |
| 3179 | |
| 3180 | static CURLMcode multi_timeout(struct Curl_multi *multi, |
| 3181 | long *timeout_ms) |
| 3182 | { |
| 3183 | static struct curltime tv_zero = {0, 0}; |
| 3184 | |
| 3185 | if(multi->timetree) { |
| 3186 | /* we have a tree of expire times */ |
| 3187 | struct curltime now = Curl_now(); |
| 3188 | |
| 3189 | /* splay the lowest to the bottom */ |
| 3190 | multi->timetree = Curl_splay(tv_zero, multi->timetree); |
| 3191 | |
| 3192 | if(Curl_splaycomparekeys(multi->timetree->key, now) > 0) { |
| 3193 | /* some time left before expiration */ |
| 3194 | timediff_t diff = Curl_timediff(multi->timetree->key, now); |
| 3195 | if(diff <= 0) |
| 3196 | /* |
| 3197 | * Since we only provide millisecond resolution on the returned value |
| 3198 | * and the diff might be less than one millisecond here, we don't |
| 3199 | * return zero as that may cause short bursts of busyloops on fast |
| 3200 | * processors while the diff is still present but less than one |
| 3201 | * millisecond! instead we return 1 until the time is ripe. |
| 3202 | */ |
| 3203 | *timeout_ms = 1; |
| 3204 | else |
| 3205 | /* this should be safe even on 64 bit archs, as we don't use that |
| 3206 | overly long timeouts */ |
| 3207 | *timeout_ms = (long)diff; |
| 3208 | } |
| 3209 | else |
| 3210 | /* 0 means immediately */ |
| 3211 | *timeout_ms = 0; |
| 3212 | } |
| 3213 | else |
| 3214 | *timeout_ms = -1; |
| 3215 | |
| 3216 | return CURLM_OK; |
| 3217 | } |
| 3218 | |
| 3219 | CURLMcode curl_multi_timeout(struct Curl_multi *multi, |
| 3220 | long *timeout_ms) |
| 3221 | { |
| 3222 | /* First, make some basic checks that the CURLM handle is a good handle */ |
| 3223 | if(!GOOD_MULTI_HANDLE(multi)) |
| 3224 | return CURLM_BAD_HANDLE; |
| 3225 | |
| 3226 | if(multi->in_callback) |
| 3227 | return CURLM_RECURSIVE_API_CALL; |
| 3228 | |
| 3229 | return multi_timeout(multi, timeout_ms); |
| 3230 | } |
| 3231 | |
| 3232 | /* |
| 3233 | * Tell the application it should update its timers, if it subscribes to the |
| 3234 | * update timer callback. |
| 3235 | */ |
| 3236 | void Curl_update_timer(struct Curl_multi *multi) |
| 3237 | { |
| 3238 | long timeout_ms; |
| 3239 | |
| 3240 | if(!multi->timer_cb) |
| 3241 | return; |
| 3242 | if(multi_timeout(multi, &timeout_ms)) { |
| 3243 | return; |
| 3244 | } |
| 3245 | if(timeout_ms < 0) { |
| 3246 | static const struct curltime none = {0, 0}; |
| 3247 | if(Curl_splaycomparekeys(none, multi->timer_lastcall)) { |
| 3248 | multi->timer_lastcall = none; |
| 3249 | /* there's no timeout now but there was one previously, tell the app to |
| 3250 | disable it */ |
| 3251 | multi->timer_cb(multi, -1, multi->timer_userp); |
| 3252 | return; |
| 3253 | } |
| 3254 | return; |
| 3255 | } |
| 3256 | |
| 3257 | /* When multi_timeout() is done, multi->timetree points to the node with the |
| 3258 | * timeout we got the (relative) time-out time for. We can thus easily check |
| 3259 | * if this is the same (fixed) time as we got in a previous call and then |
| 3260 | * avoid calling the callback again. */ |
| 3261 | if(Curl_splaycomparekeys(multi->timetree->key, multi->timer_lastcall) == 0) |
| 3262 | return; |
| 3263 | |
| 3264 | multi->timer_lastcall = multi->timetree->key; |
| 3265 | |
| 3266 | multi->timer_cb(multi, timeout_ms, multi->timer_userp); |
| 3267 | } |
| 3268 | |
| 3269 | /* |
| 3270 | * multi_deltimeout() |
| 3271 | * |
| 3272 | * Remove a given timestamp from the list of timeouts. |
| 3273 | */ |
| 3274 | static void |
| 3275 | multi_deltimeout(struct Curl_easy *data, expire_id eid) |
| 3276 | { |
| 3277 | struct Curl_llist_element *e; |
| 3278 | struct Curl_llist *timeoutlist = &data->state.timeoutlist; |
| 3279 | /* find and remove the specific node from the list */ |
| 3280 | for(e = timeoutlist->head; e; e = e->next) { |
| 3281 | struct time_node *n = (struct time_node *)e->ptr; |
| 3282 | if(n->eid == eid) { |
| 3283 | Curl_llist_remove(timeoutlist, e, NULL); |
| 3284 | return; |
| 3285 | } |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | /* |
| 3290 | * multi_addtimeout() |
| 3291 | * |
| 3292 | * Add a timestamp to the list of timeouts. Keep the list sorted so that head |
| 3293 | * of list is always the timeout nearest in time. |
| 3294 | * |
| 3295 | */ |
| 3296 | static CURLMcode |
| 3297 | multi_addtimeout(struct Curl_easy *data, |
| 3298 | struct curltime *stamp, |
| 3299 | expire_id eid) |
| 3300 | { |
| 3301 | struct Curl_llist_element *e; |
| 3302 | struct time_node *node; |
| 3303 | struct Curl_llist_element *prev = NULL; |
| 3304 | size_t n; |
| 3305 | struct Curl_llist *timeoutlist = &data->state.timeoutlist; |
| 3306 | |
| 3307 | node = &data->state.expires[eid]; |
| 3308 | |
| 3309 | /* copy the timestamp and id */ |
| 3310 | memcpy(&node->time, stamp, sizeof(*stamp)); |
| 3311 | node->eid = eid; /* also marks it as in use */ |
| 3312 | |
| 3313 | n = Curl_llist_count(timeoutlist); |
| 3314 | if(n) { |
| 3315 | /* find the correct spot in the list */ |
| 3316 | for(e = timeoutlist->head; e; e = e->next) { |
| 3317 | struct time_node *check = (struct time_node *)e->ptr; |
| 3318 | timediff_t diff = Curl_timediff(check->time, node->time); |
| 3319 | if(diff > 0) |
| 3320 | break; |
| 3321 | prev = e; |
| 3322 | } |
| 3323 | |
| 3324 | } |
| 3325 | /* else |
| 3326 | this is the first timeout on the list */ |
| 3327 | |
| 3328 | Curl_llist_insert_next(timeoutlist, prev, node, &node->list); |
| 3329 | return CURLM_OK; |
| 3330 | } |
| 3331 | |
| 3332 | /* |
| 3333 | * Curl_expire() |
| 3334 | * |
| 3335 | * given a number of milliseconds from now to use to set the 'act before |
| 3336 | * this'-time for the transfer, to be extracted by curl_multi_timeout() |
| 3337 | * |
| 3338 | * The timeout will be added to a queue of timeouts if it defines a moment in |
| 3339 | * time that is later than the current head of queue. |
| 3340 | * |
| 3341 | * Expire replaces a former timeout using the same id if already set. |
| 3342 | */ |
| 3343 | void Curl_expire(struct Curl_easy *data, timediff_t milli, expire_id id) |
| 3344 | { |
| 3345 | struct Curl_multi *multi = data->multi; |
| 3346 | struct curltime *nowp = &data->state.expiretime; |
| 3347 | struct curltime set; |
| 3348 | |
| 3349 | /* this is only interesting while there is still an associated multi struct |
| 3350 | remaining! */ |
| 3351 | if(!multi) |
| 3352 | return; |
| 3353 | |
| 3354 | DEBUGASSERT(id < EXPIRE_LAST); |
| 3355 | |
| 3356 | set = Curl_now(); |
| 3357 | set.tv_sec += (time_t)(milli/1000); /* might be a 64 to 32 bit conversion */ |
| 3358 | set.tv_usec += (unsigned int)(milli%1000)*1000; |
| 3359 | |
| 3360 | if(set.tv_usec >= 1000000) { |
| 3361 | set.tv_sec++; |
| 3362 | set.tv_usec -= 1000000; |
| 3363 | } |
| 3364 | |
| 3365 | /* Remove any timer with the same id just in case. */ |
| 3366 | multi_deltimeout(data, id); |
| 3367 | |
| 3368 | /* Add it to the timer list. It must stay in the list until it has expired |
| 3369 | in case we need to recompute the minimum timer later. */ |
| 3370 | multi_addtimeout(data, &set, id); |
| 3371 | |
| 3372 | if(nowp->tv_sec || nowp->tv_usec) { |
| 3373 | /* This means that the struct is added as a node in the splay tree. |
| 3374 | Compare if the new time is earlier, and only remove-old/add-new if it |
| 3375 | is. */ |
| 3376 | timediff_t diff = Curl_timediff(set, *nowp); |
| 3377 | int rc; |
| 3378 | |
| 3379 | if(diff > 0) { |
| 3380 | /* The current splay tree entry is sooner than this new expiry time. |
| 3381 | We don't need to update our splay tree entry. */ |
| 3382 | return; |
| 3383 | } |
| 3384 | |
| 3385 | /* Since this is an updated time, we must remove the previous entry from |
| 3386 | the splay tree first and then re-add the new value */ |
| 3387 | rc = Curl_splayremove(multi->timetree, &data->state.timenode, |
| 3388 | &multi->timetree); |
| 3389 | if(rc) |
| 3390 | infof(data, "Internal error removing splay node = %d" , rc); |
| 3391 | } |
| 3392 | |
| 3393 | /* Indicate that we are in the splay tree and insert the new timer expiry |
| 3394 | value since it is our local minimum. */ |
| 3395 | *nowp = set; |
| 3396 | data->state.timenode.payload = data; |
| 3397 | multi->timetree = Curl_splayinsert(*nowp, multi->timetree, |
| 3398 | &data->state.timenode); |
| 3399 | } |
| 3400 | |
| 3401 | /* |
| 3402 | * Curl_expire_done() |
| 3403 | * |
| 3404 | * Removes the expire timer. Marks it as done. |
| 3405 | * |
| 3406 | */ |
| 3407 | void Curl_expire_done(struct Curl_easy *data, expire_id id) |
| 3408 | { |
| 3409 | /* remove the timer, if there */ |
| 3410 | multi_deltimeout(data, id); |
| 3411 | } |
| 3412 | |
| 3413 | /* |
| 3414 | * Curl_expire_clear() |
| 3415 | * |
| 3416 | * Clear ALL timeout values for this handle. |
| 3417 | */ |
| 3418 | void Curl_expire_clear(struct Curl_easy *data) |
| 3419 | { |
| 3420 | struct Curl_multi *multi = data->multi; |
| 3421 | struct curltime *nowp = &data->state.expiretime; |
| 3422 | |
| 3423 | /* this is only interesting while there is still an associated multi struct |
| 3424 | remaining! */ |
| 3425 | if(!multi) |
| 3426 | return; |
| 3427 | |
| 3428 | if(nowp->tv_sec || nowp->tv_usec) { |
| 3429 | /* Since this is an cleared time, we must remove the previous entry from |
| 3430 | the splay tree */ |
| 3431 | struct Curl_llist *list = &data->state.timeoutlist; |
| 3432 | int rc; |
| 3433 | |
| 3434 | rc = Curl_splayremove(multi->timetree, &data->state.timenode, |
| 3435 | &multi->timetree); |
| 3436 | if(rc) |
| 3437 | infof(data, "Internal error clearing splay node = %d" , rc); |
| 3438 | |
| 3439 | /* flush the timeout list too */ |
| 3440 | while(list->size > 0) { |
| 3441 | Curl_llist_remove(list, list->tail, NULL); |
| 3442 | } |
| 3443 | |
| 3444 | #ifdef DEBUGBUILD |
| 3445 | infof(data, "Expire cleared (transfer %p)" , data); |
| 3446 | #endif |
| 3447 | nowp->tv_sec = 0; |
| 3448 | nowp->tv_usec = 0; |
| 3449 | } |
| 3450 | } |
| 3451 | |
| 3452 | |
| 3453 | |
| 3454 | |
| 3455 | CURLMcode curl_multi_assign(struct Curl_multi *multi, curl_socket_t s, |
| 3456 | void *hashp) |
| 3457 | { |
| 3458 | struct Curl_sh_entry *there = NULL; |
| 3459 | |
| 3460 | if(multi->in_callback) |
| 3461 | return CURLM_RECURSIVE_API_CALL; |
| 3462 | |
| 3463 | there = sh_getentry(&multi->sockhash, s); |
| 3464 | |
| 3465 | if(!there) |
| 3466 | return CURLM_BAD_SOCKET; |
| 3467 | |
| 3468 | there->socketp = hashp; |
| 3469 | |
| 3470 | return CURLM_OK; |
| 3471 | } |
| 3472 | |
| 3473 | size_t Curl_multi_max_host_connections(struct Curl_multi *multi) |
| 3474 | { |
| 3475 | return multi ? multi->max_host_connections : 0; |
| 3476 | } |
| 3477 | |
| 3478 | size_t Curl_multi_max_total_connections(struct Curl_multi *multi) |
| 3479 | { |
| 3480 | return multi ? multi->max_total_connections : 0; |
| 3481 | } |
| 3482 | |
| 3483 | /* |
| 3484 | * When information about a connection has appeared, call this! |
| 3485 | */ |
| 3486 | |
| 3487 | void Curl_multiuse_state(struct Curl_easy *data, |
| 3488 | int bundlestate) /* use BUNDLE_* defines */ |
| 3489 | { |
| 3490 | struct connectdata *conn; |
| 3491 | DEBUGASSERT(data); |
| 3492 | DEBUGASSERT(data->multi); |
| 3493 | conn = data->conn; |
| 3494 | DEBUGASSERT(conn); |
| 3495 | DEBUGASSERT(conn->bundle); |
| 3496 | |
| 3497 | conn->bundle->multiuse = bundlestate; |
| 3498 | process_pending_handles(data->multi); |
| 3499 | } |
| 3500 | |
| 3501 | static void process_pending_handles(struct Curl_multi *multi) |
| 3502 | { |
| 3503 | struct Curl_llist_element *e = multi->pending.head; |
| 3504 | if(e) { |
| 3505 | struct Curl_easy *data = e->ptr; |
| 3506 | |
| 3507 | DEBUGASSERT(data->mstate == MSTATE_PENDING); |
| 3508 | |
| 3509 | multistate(data, MSTATE_CONNECT); |
| 3510 | |
| 3511 | /* Remove this node from the list */ |
| 3512 | Curl_llist_remove(&multi->pending, e, NULL); |
| 3513 | |
| 3514 | /* Make sure that the handle will be processed soonish. */ |
| 3515 | Curl_expire(data, 0, EXPIRE_RUN_NOW); |
| 3516 | |
| 3517 | /* mark this as having been in the pending queue */ |
| 3518 | data->state.previouslypending = TRUE; |
| 3519 | } |
| 3520 | } |
| 3521 | |
| 3522 | void Curl_set_in_callback(struct Curl_easy *data, bool value) |
| 3523 | { |
| 3524 | /* might get called when there is no data pointer! */ |
| 3525 | if(data) { |
| 3526 | if(data->multi_easy) |
| 3527 | data->multi_easy->in_callback = value; |
| 3528 | else if(data->multi) |
| 3529 | data->multi->in_callback = value; |
| 3530 | } |
| 3531 | } |
| 3532 | |
| 3533 | bool Curl_is_in_callback(struct Curl_easy *easy) |
| 3534 | { |
| 3535 | return ((easy->multi && easy->multi->in_callback) || |
| 3536 | (easy->multi_easy && easy->multi_easy->in_callback)); |
| 3537 | } |
| 3538 | |
| 3539 | #ifdef DEBUGBUILD |
| 3540 | void Curl_multi_dump(struct Curl_multi *multi) |
| 3541 | { |
| 3542 | struct Curl_easy *data; |
| 3543 | int i; |
| 3544 | fprintf(stderr, "* Multi status: %d handles, %d alive\n" , |
| 3545 | multi->num_easy, multi->num_alive); |
| 3546 | for(data = multi->easyp; data; data = data->next) { |
| 3547 | if(data->mstate < MSTATE_COMPLETED) { |
| 3548 | /* only display handles that are not completed */ |
| 3549 | fprintf(stderr, "handle %p, state %s, %d sockets\n" , |
| 3550 | (void *)data, |
| 3551 | statename[data->mstate], data->numsocks); |
| 3552 | for(i = 0; i < data->numsocks; i++) { |
| 3553 | curl_socket_t s = data->sockets[i]; |
| 3554 | struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s); |
| 3555 | |
| 3556 | fprintf(stderr, "%d " , (int)s); |
| 3557 | if(!entry) { |
| 3558 | fprintf(stderr, "INTERNAL CONFUSION\n" ); |
| 3559 | continue; |
| 3560 | } |
| 3561 | fprintf(stderr, "[%s %s] " , |
| 3562 | (entry->action&CURL_POLL_IN)?"RECVING" :"" , |
| 3563 | (entry->action&CURL_POLL_OUT)?"SENDING" :"" ); |
| 3564 | } |
| 3565 | if(data->numsocks) |
| 3566 | fprintf(stderr, "\n" ); |
| 3567 | } |
| 3568 | } |
| 3569 | } |
| 3570 | #endif |
| 3571 | |
| 3572 | unsigned int Curl_multi_max_concurrent_streams(struct Curl_multi *multi) |
| 3573 | { |
| 3574 | DEBUGASSERT(multi); |
| 3575 | return multi->max_concurrent_streams; |
| 3576 | } |
| 3577 | |