Debian Codegraph @

deps/curl/lib/multi.c

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