blob: 98e5fca2ab903ab4b2d5b66ca37bfa3c3be70851 [file] [log] [blame]
Kristian Monsen5ab50182010-05-14 18:53:44 +01001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Elliott Hughescac39802018-04-27 16:19:43 -07008 * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
Kristian Monsen5ab50182010-05-14 18:53:44 +01009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymod15eaac2016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Kristian Monsen5ab50182010-05-14 18:53:44 +010013 *
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
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070023#include "curl_setup.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010024
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"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070033#include "share.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010034#include "multiif.h"
35#include "sendf.h"
36#include "timeval.h"
37#include "http.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070038#include "select.h"
39#include "warnless.h"
40#include "speedcheck.h"
41#include "conncache.h"
42#include "multihandle.h"
43#include "pipeline.h"
44#include "sigpipe.h"
Elliott Hughescee03382017-06-23 12:17:18 -070045#include "vtls/vtls.h"
46#include "connect.h"
Elliott Hughes82be86d2017-09-20 17:00:17 -070047#include "http_proxy.h"
Alex Deymod15eaac2016-06-28 14:49:26 -070048/* The last 3 #include files should be in this order */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070049#include "curl_printf.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010050#include "curl_memory.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010051#include "memdebug.h"
52
53/*
54 CURL_SOCKET_HASH_TABLE_SIZE should be a prime number. Increasing it from 97
55 to 911 takes on a 32-bit machine 4 x 804 = 3211 more bytes. Still, every
56 CURL handle takes 45-50 K memory, therefore this 3K are not significant.
57*/
58#ifndef CURL_SOCKET_HASH_TABLE_SIZE
59#define CURL_SOCKET_HASH_TABLE_SIZE 911
60#endif
61
Alex Deymo486467e2017-12-19 19:04:07 +010062#ifndef CURL_CONNECTION_HASH_SIZE
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070063#define CURL_CONNECTION_HASH_SIZE 97
Alex Deymo486467e2017-12-19 19:04:07 +010064#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +010065
66#define CURL_MULTI_HANDLE 0x000bab1e
67
68#define GOOD_MULTI_HANDLE(x) \
Alex Deymoe3149cc2016-10-05 11:18:42 -070069 ((x) && (x)->type == CURL_MULTI_HANDLE)
Kristian Monsen5ab50182010-05-14 18:53:44 +010070
Kristian Monsen5ab50182010-05-14 18:53:44 +010071static void singlesocket(struct Curl_multi *multi,
Alex Deymoe3149cc2016-10-05 11:18:42 -070072 struct Curl_easy *data);
Kristian Monsen5ab50182010-05-14 18:53:44 +010073static int update_timer(struct Curl_multi *multi);
74
Elliott Hughes82be86d2017-09-20 17:00:17 -070075static CURLMcode add_next_timeout(struct curltime now,
Lucas Eckels9bd90e62012-08-06 15:07:02 -070076 struct Curl_multi *multi,
Alex Deymoe3149cc2016-10-05 11:18:42 -070077 struct Curl_easy *d);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070078static CURLMcode multi_timeout(struct Curl_multi *multi,
79 long *timeout_ms);
Kristian Monsen5ab50182010-05-14 18:53:44 +010080
81#ifdef DEBUGBUILD
82static const char * const statename[]={
83 "INIT",
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070084 "CONNECT_PEND",
Kristian Monsen5ab50182010-05-14 18:53:44 +010085 "CONNECT",
86 "WAITRESOLVE",
87 "WAITCONNECT",
88 "WAITPROXYCONNECT",
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070089 "SENDPROTOCONNECT",
Kristian Monsen5ab50182010-05-14 18:53:44 +010090 "PROTOCONNECT",
91 "WAITDO",
92 "DO",
93 "DOING",
94 "DO_MORE",
95 "DO_DONE",
96 "WAITPERFORM",
97 "PERFORM",
98 "TOOFAST",
99 "DONE",
100 "COMPLETED",
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700101 "MSGSENT",
Kristian Monsen5ab50182010-05-14 18:53:44 +0100102};
103#endif
104
Alex Deymod15eaac2016-06-28 14:49:26 -0700105/* function pointer called once when switching TO a state */
Alex Deymoe3149cc2016-10-05 11:18:42 -0700106typedef void (*init_multistate_func)(struct Curl_easy *data);
Alex Deymod15eaac2016-06-28 14:49:26 -0700107
Kristian Monsen5ab50182010-05-14 18:53:44 +0100108/* always use this function to change state, to make debugging easier */
Alex Deymoe3149cc2016-10-05 11:18:42 -0700109static void mstate(struct Curl_easy *data, CURLMstate state
Kristian Monsen5ab50182010-05-14 18:53:44 +0100110#ifdef DEBUGBUILD
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700111 , int lineno
Kristian Monsen5ab50182010-05-14 18:53:44 +0100112#endif
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700113)
114{
115 CURLMstate oldstate = data->mstate;
Alex Deymod15eaac2016-06-28 14:49:26 -0700116 static const init_multistate_func finit[CURLM_STATE_LAST] = {
117 NULL,
118 NULL,
119 Curl_init_CONNECT, /* CONNECT */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700120 NULL,
121 NULL,
122 NULL,
123 NULL,
124 NULL,
125 NULL,
126 Curl_connect_free /* DO */
Alex Deymod15eaac2016-06-28 14:49:26 -0700127 /* the rest is NULL too */
128 };
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700129
130#if defined(DEBUGBUILD) && defined(CURL_DISABLE_VERBOSE_STRINGS)
131 (void) lineno;
132#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +0100133
134 if(oldstate == state)
135 /* don't bother when the new state is the same as the old state */
136 return;
137
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700138 data->mstate = state;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100139
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700140#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
141 if(data->mstate >= CURLM_STATE_CONNECT_PEND &&
142 data->mstate < CURLM_STATE_COMPLETED) {
143 long connection_id = -5000;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100144
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700145 if(data->easy_conn)
146 connection_id = data->easy_conn->connection_id;
147
148 infof(data,
Alex Deymod15eaac2016-06-28 14:49:26 -0700149 "STATE: %s => %s handle %p; line %d (connection #%ld)\n",
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700150 statename[oldstate], statename[data->mstate],
151 (void *)data, lineno, connection_id);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100152 }
153#endif
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700154
Kristian Monsen5ab50182010-05-14 18:53:44 +0100155 if(state == CURLM_STATE_COMPLETED)
156 /* changing to COMPLETED means there's one less easy handle 'alive' */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700157 data->multi->num_alive--;
Alex Deymod15eaac2016-06-28 14:49:26 -0700158
159 /* if this state has an init-function, run it */
160 if(finit[state])
161 finit[state](data);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100162}
163
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700164#ifndef DEBUGBUILD
165#define multistate(x,y) mstate(x,y)
166#else
167#define multistate(x,y) mstate(x,y, __LINE__)
168#endif
169
Kristian Monsen5ab50182010-05-14 18:53:44 +0100170/*
171 * We add one of these structs to the sockhash for a particular socket
172 */
173
174struct Curl_sh_entry {
Alex Deymoe3149cc2016-10-05 11:18:42 -0700175 struct Curl_easy *easy;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100176 int action; /* what action READ/WRITE this socket waits for */
177 curl_socket_t socket; /* mainly to ease debugging */
178 void *socketp; /* settable by users with curl_multi_assign() */
179};
180/* bits for 'action' having no bits means this socket is not expecting any
181 action */
182#define SH_READ 1
183#define SH_WRITE 2
184
Alex Deymod15eaac2016-06-28 14:49:26 -0700185/* look up a given socket in the socket hash, skip invalid sockets */
186static struct Curl_sh_entry *sh_getentry(struct curl_hash *sh,
187 curl_socket_t s)
188{
189 if(s != CURL_SOCKET_BAD)
190 /* only look for proper sockets */
191 return Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t));
192 return NULL;
193}
194
Kristian Monsen5ab50182010-05-14 18:53:44 +0100195/* make sure this socket is present in the hash for this handle */
196static struct Curl_sh_entry *sh_addentry(struct curl_hash *sh,
197 curl_socket_t s,
Alex Deymoe3149cc2016-10-05 11:18:42 -0700198 struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100199{
Alex Deymod15eaac2016-06-28 14:49:26 -0700200 struct Curl_sh_entry *there = sh_getentry(sh, s);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100201 struct Curl_sh_entry *check;
202
203 if(there)
204 /* it is present, return fine */
205 return there;
206
207 /* not present, add it */
208 check = calloc(1, sizeof(struct Curl_sh_entry));
209 if(!check)
210 return NULL; /* major failure */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700211
Kristian Monsen5ab50182010-05-14 18:53:44 +0100212 check->easy = data;
213 check->socket = s;
214
215 /* make/add new hash entry */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700216 if(!Curl_hash_add(sh, (char *)&s, sizeof(curl_socket_t), check)) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100217 free(check);
218 return NULL; /* major failure */
219 }
220
221 return check; /* things are good in sockhash land */
222}
223
224
225/* delete the given socket + handle from the hash */
226static void sh_delentry(struct curl_hash *sh, curl_socket_t s)
227{
Alex Deymod15eaac2016-06-28 14:49:26 -0700228 /* We remove the hash entry. This will end up in a call to
229 sh_freeentry(). */
230 Curl_hash_delete(sh, (char *)&s, sizeof(curl_socket_t));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100231}
232
233/*
234 * free a sockhash entry
235 */
236static void sh_freeentry(void *freethis)
237{
238 struct Curl_sh_entry *p = (struct Curl_sh_entry *) freethis;
239
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700240 free(p);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100241}
242
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700243static size_t fd_key_compare(void *k1, size_t k1_len, void *k2, size_t k2_len)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100244{
245 (void) k1_len; (void) k2_len;
246
Alex Deymod15eaac2016-06-28 14:49:26 -0700247 return (*((curl_socket_t *) k1)) == (*((curl_socket_t *) k2));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100248}
249
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700250static size_t hash_fd(void *key, size_t key_length, size_t slots_num)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100251{
Alex Deymod15eaac2016-06-28 14:49:26 -0700252 curl_socket_t fd = *((curl_socket_t *) key);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100253 (void) key_length;
254
Alex Deymod15eaac2016-06-28 14:49:26 -0700255 return (fd % slots_num);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100256}
257
258/*
259 * sh_init() creates a new socket hash and returns the handle for it.
260 *
261 * Quote from README.multi_socket:
262 *
263 * "Some tests at 7000 and 9000 connections showed that the socket hash lookup
264 * is somewhat of a bottle neck. Its current implementation may be a bit too
265 * limiting. It simply has a fixed-size array, and on each entry in the array
266 * it has a linked list with entries. So the hash only checks which list to
267 * scan through. The code I had used so for used a list with merely 7 slots
268 * (as that is what the DNS hash uses) but with 7000 connections that would
269 * make an average of 1000 nodes in each list to run through. I upped that to
270 * 97 slots (I believe a prime is suitable) and noticed a significant speed
271 * increase. I need to reconsider the hash implementation or use a rather
272 * large default value like this. At 9000 connections I was still below 10us
273 * per call."
274 *
275 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700276static int sh_init(struct curl_hash *hash, int hashsize)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100277{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700278 return Curl_hash_init(hash, hashsize, hash_fd, fd_key_compare,
279 sh_freeentry);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100280}
281
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700282/*
283 * multi_addmsg()
284 *
285 * Called when a transfer is completed. Adds the given msg pointer to
286 * the list kept in the multi handle.
287 */
288static CURLMcode multi_addmsg(struct Curl_multi *multi,
289 struct Curl_message *msg)
290{
Elliott Hughes82be86d2017-09-20 17:00:17 -0700291 Curl_llist_insert_next(&multi->msglist, multi->msglist.tail, msg,
292 &msg->list);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700293 return CURLM_OK;
294}
295
296/*
297 * multi_freeamsg()
298 *
299 * Callback used by the llist system when a single list entry is destroyed.
300 */
301static void multi_freeamsg(void *a, void *b)
302{
303 (void)a;
304 (void)b;
305}
306
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700307struct Curl_multi *Curl_multi_handle(int hashsize, /* socket hash */
308 int chashsize) /* connection hash */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100309{
310 struct Curl_multi *multi = calloc(1, sizeof(struct Curl_multi));
311
312 if(!multi)
313 return NULL;
314
315 multi->type = CURL_MULTI_HANDLE;
316
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700317 if(Curl_mk_dnscache(&multi->hostcache))
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700318 goto error;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100319
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700320 if(sh_init(&multi->sockhash, hashsize))
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700321 goto error;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100322
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700323 if(Curl_conncache_init(&multi->conn_cache, chashsize))
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700324 goto error;
325
Elliott Hughes82be86d2017-09-20 17:00:17 -0700326 Curl_llist_init(&multi->msglist, multi_freeamsg);
327 Curl_llist_init(&multi->pending, multi_freeamsg);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100328
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700329 multi->max_pipeline_length = 5;
330
331 /* -1 means it not set by user, use the default value */
332 multi->maxconnects = -1;
Alex Deymoe3149cc2016-10-05 11:18:42 -0700333 return multi;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700334
335 error:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700336
337 Curl_hash_destroy(&multi->sockhash);
338 Curl_hash_destroy(&multi->hostcache);
339 Curl_conncache_destroy(&multi->conn_cache);
Elliott Hughes82be86d2017-09-20 17:00:17 -0700340 Curl_llist_destroy(&multi->msglist, NULL);
341 Curl_llist_destroy(&multi->pending, NULL);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700342
343 free(multi);
344 return NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100345}
346
Alex Deymoe3149cc2016-10-05 11:18:42 -0700347struct Curl_multi *curl_multi_init(void)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700348{
349 return Curl_multi_handle(CURL_SOCKET_HASH_TABLE_SIZE,
350 CURL_CONNECTION_HASH_SIZE);
351}
352
Alex Deymoe3149cc2016-10-05 11:18:42 -0700353CURLMcode curl_multi_add_handle(struct Curl_multi *multi,
354 struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100355{
Kristian Monsen5ab50182010-05-14 18:53:44 +0100356 /* First, make some basic checks that the CURLM handle is a good handle */
357 if(!GOOD_MULTI_HANDLE(multi))
358 return CURLM_BAD_HANDLE;
359
360 /* Verify that we got a somewhat good easy handle too */
Alex Deymoe3149cc2016-10-05 11:18:42 -0700361 if(!GOOD_EASY_HANDLE(data))
Kristian Monsen5ab50182010-05-14 18:53:44 +0100362 return CURLM_BAD_EASY_HANDLE;
363
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700364 /* Prevent users from adding same easy handle more than once and prevent
365 adding to more than one multi stack */
366 if(data->multi)
367 return CURLM_ADDED_ALREADY;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100368
Elliott Hughescac39802018-04-27 16:19:43 -0700369 if(multi->in_callback)
370 return CURLM_RECURSIVE_API_CALL;
371
Elliott Hughes82be86d2017-09-20 17:00:17 -0700372 /* Initialize timeout list for this handle */
373 Curl_llist_init(&data->state.timeoutlist, NULL);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700374
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700375 /*
376 * No failure allowed in this function beyond this point. And no
377 * modification of easy nor multi handle allowed before this except for
378 * potential multi's connection cache growing which won't be undone in this
379 * function no matter what.
380 */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100381
Kristian Monsen5ab50182010-05-14 18:53:44 +0100382 /* set the easy handle */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700383 multistate(data, CURLM_STATE_INIT);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100384
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700385 if((data->set.global_dns_cache) &&
386 (data->dns.hostcachetype != HCACHE_GLOBAL)) {
387 /* global dns cache was requested but still isn't */
388 struct curl_hash *global = Curl_global_host_cache_init();
389 if(global) {
390 /* only do this if the global cache init works */
391 data->dns.hostcache = global;
392 data->dns.hostcachetype = HCACHE_GLOBAL;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100393 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100394 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700395 /* for multi interface connections, we share DNS cache automatically if the
396 easy handle's one is currently not set. */
397 else if(!data->dns.hostcache ||
398 (data->dns.hostcachetype == HCACHE_NONE)) {
399 data->dns.hostcache = &multi->hostcache;
400 data->dns.hostcachetype = HCACHE_MULTI;
401 }
402
Alex Deymo486467e2017-12-19 19:04:07 +0100403 /* Point to the shared or multi handle connection cache */
404 if(data->share && (data->share->specifier & (1<< CURL_LOCK_DATA_CONNECT)))
405 data->state.conn_cache = &data->share->conn_cache;
406 else
407 data->state.conn_cache = &multi->conn_cache;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700408
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700409 /* This adds the new entry at the 'end' of the doubly-linked circular
Alex Deymoe3149cc2016-10-05 11:18:42 -0700410 list of Curl_easy structs to try and maintain a FIFO queue so
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700411 the pipelined requests are in order. */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100412
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700413 /* We add this new entry last in the list. */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100414
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700415 data->next = NULL; /* end of the line */
416 if(multi->easyp) {
Alex Deymoe3149cc2016-10-05 11:18:42 -0700417 struct Curl_easy *last = multi->easylp;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700418 last->next = data;
419 data->prev = last;
420 multi->easylp = data; /* the new last node */
421 }
422 else {
423 /* first node, make prev NULL! */
424 data->prev = NULL;
425 multi->easylp = multi->easyp = data; /* both first and last */
426 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100427
Alex Deymoe3149cc2016-10-05 11:18:42 -0700428 /* make the Curl_easy refer back to this multi handle */
429 data->multi = multi;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100430
431 /* Set the timeout for this handle to expire really soon so that it will
432 be taken care of even when this handle is added in the midst of operation
433 when only the curl_multi_socket() API is used. During that flow, only
434 sockets that time-out or have actions will be dealt with. Since this
435 handle has no action yet, we make sure it times out to get things to
436 happen. */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700437 Curl_expire(data, 0, EXPIRE_RUN_NOW);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100438
439 /* increase the node-counter */
440 multi->num_easy++;
441
Kristian Monsen5ab50182010-05-14 18:53:44 +0100442 /* increase the alive-counter */
443 multi->num_alive++;
444
445 /* A somewhat crude work-around for a little glitch in update_timer() that
446 happens if the lastcall time is set to the same time when the handle is
447 removed as when the next handle is added, as then the check in
448 update_timer() that prevents calling the application multiple times with
449 the same timer infor will not trigger and then the new handle's timeout
450 will not be notified to the app.
451
452 The work-around is thus simply to clear the 'lastcall' variable to force
453 update_timer() to always trigger a callback to the app when a new easy
454 handle is added */
455 memset(&multi->timer_lastcall, 0, sizeof(multi->timer_lastcall));
456
Elliott Hughescee03382017-06-23 12:17:18 -0700457 /* The closure handle only ever has default timeouts set. To improve the
458 state somewhat we clone the timeouts from each added handle so that the
459 closure handle always has the same timeouts as the most recently added
460 easy handle. */
Alex Deymo486467e2017-12-19 19:04:07 +0100461 data->state.conn_cache->closure_handle->set.timeout = data->set.timeout;
462 data->state.conn_cache->closure_handle->set.server_response_timeout =
Elliott Hughescee03382017-06-23 12:17:18 -0700463 data->set.server_response_timeout;
464
Kristian Monsen5ab50182010-05-14 18:53:44 +0100465 update_timer(multi);
466 return CURLM_OK;
467}
468
469#if 0
470/* Debug-function, used like this:
471 *
472 * Curl_hash_print(multi->sockhash, debug_print_sock_hash);
473 *
474 * Enable the hash print function first by editing hash.c
475 */
476static void debug_print_sock_hash(void *p)
477{
478 struct Curl_sh_entry *sh = (struct Curl_sh_entry *)p;
479
480 fprintf(stderr, " [easy %p/magic %x/socket %d]",
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700481 (void *)sh->data, sh->data->magic, (int)sh->socket);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100482}
483#endif
484
Alex Deymod15eaac2016-06-28 14:49:26 -0700485static CURLcode multi_done(struct connectdata **connp,
486 CURLcode status, /* an error if this is called
487 after an error was detected */
488 bool premature)
489{
490 CURLcode result;
491 struct connectdata *conn;
Alex Deymoe3149cc2016-10-05 11:18:42 -0700492 struct Curl_easy *data;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700493 unsigned int i;
Alex Deymod15eaac2016-06-28 14:49:26 -0700494
495 DEBUGASSERT(*connp);
496
497 conn = *connp;
498 data = conn->data;
499
500 DEBUGF(infof(data, "multi_done\n"));
501
502 if(data->state.done)
503 /* Stop if multi_done() has already been called */
504 return CURLE_OK;
505
506 Curl_getoff_all_pipelines(data, conn);
507
508 /* Cleanup possible redirect junk */
509 free(data->req.newurl);
510 data->req.newurl = NULL;
511 free(data->req.location);
512 data->req.location = NULL;
513
514 switch(status) {
515 case CURLE_ABORTED_BY_CALLBACK:
516 case CURLE_READ_ERROR:
517 case CURLE_WRITE_ERROR:
518 /* When we're aborted due to a callback return code it basically have to
519 be counted as premature as there is trouble ahead if we don't. We have
520 many callbacks and protocols work differently, we could potentially do
521 this more fine-grained in the future. */
522 premature = TRUE;
523 default:
524 break;
525 }
526
527 /* this calls the protocol-specific function pointer previously set */
528 if(conn->handler->done)
529 result = conn->handler->done(conn, status, premature);
530 else
531 result = status;
532
533 if(CURLE_ABORTED_BY_CALLBACK != result) {
534 /* avoid this if we already aborted by callback to avoid this calling
535 another callback */
536 CURLcode rc = Curl_pgrsDone(conn);
537 if(!result && rc)
538 result = CURLE_ABORTED_BY_CALLBACK;
539 }
540
Elliott Hughescac39802018-04-27 16:19:43 -0700541 if(conn->send_pipe.size || conn->recv_pipe.size) {
542 /* Stop if pipeline is not empty . */
Elliott Hughescee03382017-06-23 12:17:18 -0700543 data->easy_conn = NULL;
Alex Deymod15eaac2016-06-28 14:49:26 -0700544 DEBUGF(infof(data, "Connection still in use, no more multi_done now!\n"));
545 return CURLE_OK;
546 }
547
548 data->state.done = TRUE; /* called just now! */
549 Curl_resolver_cancel(conn);
550
551 if(conn->dns_entry) {
552 Curl_resolv_unlock(data, conn->dns_entry); /* done with this */
553 conn->dns_entry = NULL;
554 }
Elliott Hughes0128fe42018-02-27 14:57:55 -0800555 Curl_hostcache_prune(data);
Alex Deymod15eaac2016-06-28 14:49:26 -0700556
557 /* if the transfer was completed in a paused state there can be buffered
Elliott Hughes82be86d2017-09-20 17:00:17 -0700558 data left to free */
Alex Deymo486467e2017-12-19 19:04:07 +0100559 for(i = 0; i < data->state.tempcount; i++) {
Elliott Hughes82be86d2017-09-20 17:00:17 -0700560 free(data->state.tempwrite[i].buf);
561 }
562 data->state.tempcount = 0;
Alex Deymod15eaac2016-06-28 14:49:26 -0700563
564 /* if data->set.reuse_forbid is TRUE, it means the libcurl client has
565 forced us to close this connection. This is ignored for requests taking
566 place in a NTLM authentication handshake
567
568 if conn->bits.close is TRUE, it means that the connection should be
569 closed in spite of all our efforts to be nice, due to protocol
570 restrictions in our or the server's end
571
572 if premature is TRUE, it means this connection was said to be DONE before
573 the entire request operation is complete and thus we can't know in what
574 state it is for re-using, so we're forced to close it. In a perfect world
575 we can add code that keep track of if we really must close it here or not,
576 but currently we have no such detail knowledge.
577 */
578
579 if((data->set.reuse_forbid
580#if defined(USE_NTLM)
581 && !(conn->ntlm.state == NTLMSTATE_TYPE2 ||
582 conn->proxyntlm.state == NTLMSTATE_TYPE2)
583#endif
Elliott Hughes0128fe42018-02-27 14:57:55 -0800584 ) || conn->bits.close
585 || (premature && !(conn->handler->flags & PROTOPT_STREAM))) {
Alex Deymod15eaac2016-06-28 14:49:26 -0700586 CURLcode res2 = Curl_disconnect(conn, premature); /* close connection */
587
588 /* If we had an error already, make sure we return that one. But
589 if we got a new error, return that. */
590 if(!result && res2)
591 result = res2;
592 }
593 else {
Elliott Hughes0128fe42018-02-27 14:57:55 -0800594 char buffer[256];
595 /* create string before returning the connection */
596 snprintf(buffer, sizeof(buffer),
597 "Connection #%ld to host %s left intact",
598 conn->connection_id,
599 conn->bits.socksproxy ? conn->socks_proxy.host.dispname :
600 conn->bits.httpproxy ? conn->http_proxy.host.dispname :
601 conn->bits.conn_to_host ? conn->conn_to_host.dispname :
602 conn->host.dispname);
603
Alex Deymod15eaac2016-06-28 14:49:26 -0700604 /* the connection is no longer in use */
Elliott Hughes0128fe42018-02-27 14:57:55 -0800605 if(Curl_conncache_return_conn(conn)) {
Alex Deymod15eaac2016-06-28 14:49:26 -0700606 /* remember the most recently used connection */
607 data->state.lastconnect = conn;
Elliott Hughes0128fe42018-02-27 14:57:55 -0800608 infof(data, "%s\n", buffer);
Alex Deymod15eaac2016-06-28 14:49:26 -0700609 }
610 else
611 data->state.lastconnect = NULL;
612 }
613
614 *connp = NULL; /* to make the caller of this function better detect that
615 this was either closed or handed over to the connection
616 cache here, and therefore cannot be used from this point on
617 */
618 Curl_free_request_state(data);
619
620 return result;
621}
622
Alex Deymoe3149cc2016-10-05 11:18:42 -0700623CURLMcode curl_multi_remove_handle(struct Curl_multi *multi,
624 struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100625{
Alex Deymoe3149cc2016-10-05 11:18:42 -0700626 struct Curl_easy *easy = data;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700627 bool premature;
628 bool easy_owns_conn;
629 struct curl_llist_element *e;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100630
631 /* First, make some basic checks that the CURLM handle is a good handle */
632 if(!GOOD_MULTI_HANDLE(multi))
633 return CURLM_BAD_HANDLE;
634
635 /* Verify that we got a somewhat good easy handle too */
Alex Deymoe3149cc2016-10-05 11:18:42 -0700636 if(!GOOD_EASY_HANDLE(data))
Kristian Monsen5ab50182010-05-14 18:53:44 +0100637 return CURLM_BAD_EASY_HANDLE;
638
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700639 /* Prevent users from trying to remove same easy handle more than once */
640 if(!data->multi)
641 return CURLM_OK; /* it is already removed so let's say it is fine! */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100642
Elliott Hughescac39802018-04-27 16:19:43 -0700643 if(multi->in_callback)
644 return CURLM_RECURSIVE_API_CALL;
645
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700646 premature = (data->mstate < CURLM_STATE_COMPLETED) ? TRUE : FALSE;
647 easy_owns_conn = (data->easy_conn && (data->easy_conn->data == easy)) ?
648 TRUE : FALSE;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100649
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700650 /* If the 'state' is not INIT or COMPLETED, we might need to do something
651 nice to put the easy_handle in a good known state when this returns. */
652 if(premature) {
653 /* this handle is "alive" so we need to count down the total number of
654 alive connections when this is removed */
655 multi->num_alive--;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100656
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700657 /* When this handle gets removed, other handles may be able to get the
658 connection */
659 Curl_multi_process_pending_handles(multi);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100660 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700661
662 if(data->easy_conn &&
663 data->mstate > CURLM_STATE_DO &&
664 data->mstate < CURLM_STATE_COMPLETED) {
Elliott Hughes82be86d2017-09-20 17:00:17 -0700665 /* Set connection owner so that the DONE function closes it. We can
666 safely do this here since connection is killed. */
667 data->easy_conn->data = easy;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700668 /* If the handle is in a pipeline and has started sending off its
669 request but not received its response yet, we need to close
670 connection. */
Elliott Hughescee03382017-06-23 12:17:18 -0700671 streamclose(data->easy_conn, "Removed with partial response");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700672 easy_owns_conn = TRUE;
673 }
674
675 /* The timer must be shut down before data->multi is set to NULL,
676 else the timenode will remain in the splay tree after
677 curl_easy_cleanup is called. */
Elliott Hughescee03382017-06-23 12:17:18 -0700678 Curl_expire_clear(data);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700679
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700680 if(data->easy_conn) {
681
Alex Deymod15eaac2016-06-28 14:49:26 -0700682 /* we must call multi_done() here (if we still own the connection) so that
683 we don't leave a half-baked one around */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700684 if(easy_owns_conn) {
685
Alex Deymod15eaac2016-06-28 14:49:26 -0700686 /* multi_done() clears the conn->data field to lose the association
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700687 between the easy handle and the connection
688
689 Note that this ignores the return code simply because there's
690 nothing really useful to do with it anyway! */
Alex Deymod15eaac2016-06-28 14:49:26 -0700691 (void)multi_done(&data->easy_conn, data->result, premature);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700692 }
693 else
Alex Deymod15eaac2016-06-28 14:49:26 -0700694 /* Clear connection pipelines, if multi_done above was not called */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700695 Curl_getoff_all_pipelines(data, data->easy_conn);
696 }
697
Alex Deymo486467e2017-12-19 19:04:07 +0100698 if(data->dns.hostcachetype == HCACHE_MULTI) {
699 /* stop using the multi handle's DNS cache, *after* the possible
700 multi_done() call above */
701 data->dns.hostcache = NULL;
702 data->dns.hostcachetype = HCACHE_NONE;
703 }
704
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700705 Curl_wildcard_dtor(&data->wildcard);
706
Alex Deymod15eaac2016-06-28 14:49:26 -0700707 /* destroy the timeout list that is held in the easy handle, do this *after*
708 multi_done() as that may actually call Curl_expire that uses this */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700709 Curl_llist_destroy(&data->state.timeoutlist, NULL);
Alex Deymod15eaac2016-06-28 14:49:26 -0700710
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700711 /* as this was using a shared connection cache we clear the pointer to that
712 since we're not part of that multi handle anymore */
713 data->state.conn_cache = NULL;
714
715 /* change state without using multistate(), only to make singlesocket() do
716 what we want */
717 data->mstate = CURLM_STATE_COMPLETED;
718 singlesocket(multi, easy); /* to let the application know what sockets that
719 vanish with this handle */
720
721 /* Remove the association between the connection and the handle */
722 if(data->easy_conn) {
723 data->easy_conn->data = NULL;
724 data->easy_conn = NULL;
725 }
726
727 data->multi = NULL; /* clear the association to this multi handle */
728
729 /* make sure there's no pending message in the queue sent from this easy
730 handle */
731
Elliott Hughes82be86d2017-09-20 17:00:17 -0700732 for(e = multi->msglist.head; e; e = e->next) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700733 struct Curl_message *msg = e->ptr;
734
735 if(msg->extmsg.easy_handle == easy) {
Elliott Hughes82be86d2017-09-20 17:00:17 -0700736 Curl_llist_remove(&multi->msglist, e, NULL);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700737 /* there can only be one from this specific handle */
738 break;
739 }
740 }
741
742 /* make the previous node point to our next */
743 if(data->prev)
744 data->prev->next = data->next;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100745 else
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700746 multi->easyp = data->next; /* point to first node */
747
748 /* make our next point to our previous node */
749 if(data->next)
750 data->next->prev = data->prev;
751 else
752 multi->easylp = data->prev; /* point to last node */
753
754 /* NOTE NOTE NOTE
755 We do not touch the easy handle here! */
756 multi->num_easy--; /* one less to care about now */
757
758 update_timer(multi);
759 return CURLM_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100760}
761
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700762/* Return TRUE if the application asked for a certain set of pipelining */
763bool Curl_pipeline_wanted(const struct Curl_multi *multi, int bits)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100764{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700765 return (multi && (multi->pipelining & bits)) ? TRUE : FALSE;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100766}
767
Alex Deymoe3149cc2016-10-05 11:18:42 -0700768void Curl_multi_handlePipeBreak(struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100769{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700770 data->easy_conn = NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100771}
772
773static int waitconnect_getsock(struct connectdata *conn,
774 curl_socket_t *sock,
775 int numsocks)
776{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700777 int i;
Alex Deymo486467e2017-12-19 19:04:07 +0100778 int s = 0;
779 int rc = 0;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700780
781 if(!numsocks)
782 return GETSOCK_BLANK;
783
Elliott Hughescee03382017-06-23 12:17:18 -0700784#ifdef USE_SSL
785 if(CONNECT_FIRSTSOCKET_PROXY_SSL())
786 return Curl_ssl_getsock(conn, sock, numsocks);
787#endif
788
Alex Deymo486467e2017-12-19 19:04:07 +0100789 for(i = 0; i<2; i++) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700790 if(conn->tempsock[i] != CURL_SOCKET_BAD) {
791 sock[s] = conn->tempsock[i];
792 rc |= GETSOCK_WRITESOCK(s++);
793 }
794 }
795
796 return rc;
797}
798
799static int waitproxyconnect_getsock(struct connectdata *conn,
800 curl_socket_t *sock,
801 int numsocks)
802{
Kristian Monsen5ab50182010-05-14 18:53:44 +0100803 if(!numsocks)
804 return GETSOCK_BLANK;
805
806 sock[0] = conn->sock[FIRSTSOCKET];
807
808 /* when we've sent a CONNECT to a proxy, we should rather wait for the
809 socket to become readable to be able to get the response headers */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700810 if(conn->connect_state)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100811 return GETSOCK_READSOCK(0);
812
813 return GETSOCK_WRITESOCK(0);
814}
815
816static int domore_getsock(struct connectdata *conn,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700817 curl_socket_t *socks,
Kristian Monsen5ab50182010-05-14 18:53:44 +0100818 int numsocks)
819{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700820 if(conn && conn->handler->domore_getsock)
821 return conn->handler->domore_getsock(conn, socks, numsocks);
822 return GETSOCK_BLANK;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100823}
824
825/* returns bitmapped flags for this handle and its sockets */
Alex Deymoe3149cc2016-10-05 11:18:42 -0700826static int multi_getsock(struct Curl_easy *data,
Kristian Monsen5ab50182010-05-14 18:53:44 +0100827 curl_socket_t *socks, /* points to numsocks number
828 of sockets */
829 int numsocks)
830{
831 /* If the pipe broke, or if there's no connection left for this easy handle,
832 then we MUST bail out now with no bitmask set. The no connection case can
833 happen when this is called from curl_multi_remove_handle() =>
834 singlesocket() => multi_getsock().
835 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700836 if(data->state.pipe_broke || !data->easy_conn)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100837 return 0;
838
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700839 if(data->mstate > CURLM_STATE_CONNECT &&
840 data->mstate < CURLM_STATE_COMPLETED) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100841 /* Set up ownership correctly */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700842 data->easy_conn->data = data;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100843 }
844
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700845 switch(data->mstate) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100846 default:
847#if 0 /* switch back on these cases to get the compiler to check for all enums
848 to be present */
849 case CURLM_STATE_TOOFAST: /* returns 0, so will not select. */
850 case CURLM_STATE_COMPLETED:
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700851 case CURLM_STATE_MSGSENT:
Kristian Monsen5ab50182010-05-14 18:53:44 +0100852 case CURLM_STATE_INIT:
853 case CURLM_STATE_CONNECT:
854 case CURLM_STATE_WAITDO:
855 case CURLM_STATE_DONE:
856 case CURLM_STATE_LAST:
857 /* this will get called with CURLM_STATE_COMPLETED when a handle is
858 removed */
859#endif
860 return 0;
861
862 case CURLM_STATE_WAITRESOLVE:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700863 return Curl_resolver_getsock(data->easy_conn, socks, numsocks);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100864
865 case CURLM_STATE_PROTOCONNECT:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700866 case CURLM_STATE_SENDPROTOCONNECT:
867 return Curl_protocol_getsock(data->easy_conn, socks, numsocks);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100868
869 case CURLM_STATE_DO:
870 case CURLM_STATE_DOING:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700871 return Curl_doing_getsock(data->easy_conn, socks, numsocks);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100872
873 case CURLM_STATE_WAITPROXYCONNECT:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700874 return waitproxyconnect_getsock(data->easy_conn, socks, numsocks);
875
Kristian Monsen5ab50182010-05-14 18:53:44 +0100876 case CURLM_STATE_WAITCONNECT:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700877 return waitconnect_getsock(data->easy_conn, socks, numsocks);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100878
879 case CURLM_STATE_DO_MORE:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700880 return domore_getsock(data->easy_conn, socks, numsocks);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100881
882 case CURLM_STATE_DO_DONE: /* since is set after DO is completed, we switch
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700883 to waiting for the same as the *PERFORM
884 states */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100885 case CURLM_STATE_PERFORM:
886 case CURLM_STATE_WAITPERFORM:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700887 return Curl_single_getsock(data->easy_conn, socks, numsocks);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100888 }
889
890}
891
Alex Deymoe3149cc2016-10-05 11:18:42 -0700892CURLMcode curl_multi_fdset(struct Curl_multi *multi,
Kristian Monsen5ab50182010-05-14 18:53:44 +0100893 fd_set *read_fd_set, fd_set *write_fd_set,
894 fd_set *exc_fd_set, int *max_fd)
895{
896 /* Scan through all the easy handles to get the file descriptors set.
897 Some easy handles may not have connected to the remote host yet,
898 and then we must make sure that is done. */
Alex Deymoe3149cc2016-10-05 11:18:42 -0700899 struct Curl_easy *data;
Alex Deymo486467e2017-12-19 19:04:07 +0100900 int this_max_fd = -1;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100901 curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
902 int bitmap;
903 int i;
904 (void)exc_fd_set; /* not used */
905
906 if(!GOOD_MULTI_HANDLE(multi))
907 return CURLM_BAD_HANDLE;
908
Elliott Hughescac39802018-04-27 16:19:43 -0700909 if(multi->in_callback)
910 return CURLM_RECURSIVE_API_CALL;
911
Alex Deymo486467e2017-12-19 19:04:07 +0100912 data = multi->easyp;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700913 while(data) {
914 bitmap = multi_getsock(data, sockbunch, MAX_SOCKSPEREASYHANDLE);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100915
Alex Deymo486467e2017-12-19 19:04:07 +0100916 for(i = 0; i< MAX_SOCKSPEREASYHANDLE; i++) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100917 curl_socket_t s = CURL_SOCKET_BAD;
918
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700919 if((bitmap & GETSOCK_READSOCK(i)) && VALID_SOCK((sockbunch[i]))) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100920 FD_SET(sockbunch[i], read_fd_set);
921 s = sockbunch[i];
922 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700923 if((bitmap & GETSOCK_WRITESOCK(i)) && VALID_SOCK((sockbunch[i]))) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100924 FD_SET(sockbunch[i], write_fd_set);
925 s = sockbunch[i];
926 }
927 if(s == CURL_SOCKET_BAD)
928 /* this socket is unused, break out of loop */
929 break;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700930 if((int)s > this_max_fd)
931 this_max_fd = (int)s;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100932 }
933
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700934 data = data->next; /* check next handle */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100935 }
936
937 *max_fd = this_max_fd;
938
939 return CURLM_OK;
940}
941
Elliott Hughes82be86d2017-09-20 17:00:17 -0700942#define NUM_POLLS_ON_STACK 10
943
Alex Deymoe3149cc2016-10-05 11:18:42 -0700944CURLMcode curl_multi_wait(struct Curl_multi *multi,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700945 struct curl_waitfd extra_fds[],
946 unsigned int extra_nfds,
947 int timeout_ms,
948 int *ret)
949{
Alex Deymoe3149cc2016-10-05 11:18:42 -0700950 struct Curl_easy *data;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700951 curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
952 int bitmap;
953 unsigned int i;
954 unsigned int nfds = 0;
955 unsigned int curlfds;
956 struct pollfd *ufds = NULL;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700957 bool ufds_malloc = FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700958 long timeout_internal;
Alex Deymod15eaac2016-06-28 14:49:26 -0700959 int retcode = 0;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700960 struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700961
962 if(!GOOD_MULTI_HANDLE(multi))
963 return CURLM_BAD_HANDLE;
964
Elliott Hughescac39802018-04-27 16:19:43 -0700965 if(multi->in_callback)
966 return CURLM_RECURSIVE_API_CALL;
967
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700968 /* If the internally desired timeout is actually shorter than requested from
969 the outside, then use the shorter time! But only if the internal timer
970 is actually larger than -1! */
971 (void)multi_timeout(multi, &timeout_internal);
972 if((timeout_internal >= 0) && (timeout_internal < (long)timeout_ms))
973 timeout_ms = (int)timeout_internal;
974
975 /* Count up how many fds we have from the multi handle */
Alex Deymo486467e2017-12-19 19:04:07 +0100976 data = multi->easyp;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700977 while(data) {
978 bitmap = multi_getsock(data, sockbunch, MAX_SOCKSPEREASYHANDLE);
979
Alex Deymo486467e2017-12-19 19:04:07 +0100980 for(i = 0; i< MAX_SOCKSPEREASYHANDLE; i++) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700981 curl_socket_t s = CURL_SOCKET_BAD;
982
983 if(bitmap & GETSOCK_READSOCK(i)) {
984 ++nfds;
985 s = sockbunch[i];
986 }
987 if(bitmap & GETSOCK_WRITESOCK(i)) {
988 ++nfds;
989 s = sockbunch[i];
990 }
991 if(s == CURL_SOCKET_BAD) {
992 break;
993 }
994 }
995
996 data = data->next; /* check next handle */
997 }
998
999 curlfds = nfds; /* number of internal file descriptors */
1000 nfds += extra_nfds; /* add the externally provided ones */
1001
Elliott Hughes82be86d2017-09-20 17:00:17 -07001002 if(nfds) {
1003 if(nfds > NUM_POLLS_ON_STACK) {
1004 /* 'nfds' is a 32 bit value and 'struct pollfd' is typically 8 bytes
1005 big, so at 2^29 sockets this value might wrap. When a process gets
1006 the capability to actually handle over 500 million sockets this
1007 calculation needs a integer overflow check. */
1008 ufds = malloc(nfds * sizeof(struct pollfd));
1009 if(!ufds)
1010 return CURLM_OUT_OF_MEMORY;
1011 ufds_malloc = TRUE;
1012 }
1013 else
1014 ufds = &a_few_on_stack[0];
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001015 }
1016 nfds = 0;
1017
1018 /* only do the second loop if we found descriptors in the first stage run
1019 above */
1020
1021 if(curlfds) {
1022 /* Add the curl handles to our pollfds first */
Alex Deymo486467e2017-12-19 19:04:07 +01001023 data = multi->easyp;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001024 while(data) {
1025 bitmap = multi_getsock(data, sockbunch, MAX_SOCKSPEREASYHANDLE);
1026
Alex Deymo486467e2017-12-19 19:04:07 +01001027 for(i = 0; i< MAX_SOCKSPEREASYHANDLE; i++) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001028 curl_socket_t s = CURL_SOCKET_BAD;
1029
1030 if(bitmap & GETSOCK_READSOCK(i)) {
1031 ufds[nfds].fd = sockbunch[i];
1032 ufds[nfds].events = POLLIN;
1033 ++nfds;
1034 s = sockbunch[i];
1035 }
1036 if(bitmap & GETSOCK_WRITESOCK(i)) {
1037 ufds[nfds].fd = sockbunch[i];
1038 ufds[nfds].events = POLLOUT;
1039 ++nfds;
1040 s = sockbunch[i];
1041 }
1042 if(s == CURL_SOCKET_BAD) {
1043 break;
1044 }
1045 }
1046
1047 data = data->next; /* check next handle */
1048 }
1049 }
1050
1051 /* Add external file descriptions from poll-like struct curl_waitfd */
1052 for(i = 0; i < extra_nfds; i++) {
1053 ufds[nfds].fd = extra_fds[i].fd;
1054 ufds[nfds].events = 0;
1055 if(extra_fds[i].events & CURL_WAIT_POLLIN)
1056 ufds[nfds].events |= POLLIN;
1057 if(extra_fds[i].events & CURL_WAIT_POLLPRI)
1058 ufds[nfds].events |= POLLPRI;
1059 if(extra_fds[i].events & CURL_WAIT_POLLOUT)
1060 ufds[nfds].events |= POLLOUT;
1061 ++nfds;
1062 }
1063
1064 if(nfds) {
Alex Deymod15eaac2016-06-28 14:49:26 -07001065 int pollrc;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001066 /* wait... */
Alex Deymod15eaac2016-06-28 14:49:26 -07001067 pollrc = Curl_poll(ufds, nfds, timeout_ms);
1068 DEBUGF(infof(data, "Curl_poll(%d ds, %d ms) == %d\n",
1069 nfds, timeout_ms, pollrc));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001070
Alex Deymod15eaac2016-06-28 14:49:26 -07001071 if(pollrc > 0) {
1072 retcode = pollrc;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001073 /* copy revents results from the poll to the curl_multi_wait poll
1074 struct, the bit values of the actual underlying poll() implementation
1075 may not be the same as the ones in the public libcurl API! */
Alex Deymod15eaac2016-06-28 14:49:26 -07001076 for(i = 0; i < extra_nfds; i++) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001077 unsigned short mask = 0;
Alex Deymod15eaac2016-06-28 14:49:26 -07001078 unsigned r = ufds[curlfds + i].revents;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001079
1080 if(r & POLLIN)
1081 mask |= CURL_WAIT_POLLIN;
1082 if(r & POLLOUT)
1083 mask |= CURL_WAIT_POLLOUT;
1084 if(r & POLLPRI)
1085 mask |= CURL_WAIT_POLLPRI;
1086
Alex Deymod15eaac2016-06-28 14:49:26 -07001087 extra_fds[i].revents = mask;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001088 }
1089 }
1090 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001091
Elliott Hughes82be86d2017-09-20 17:00:17 -07001092 if(ufds_malloc)
1093 free(ufds);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001094 if(ret)
Alex Deymod15eaac2016-06-28 14:49:26 -07001095 *ret = retcode;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001096 return CURLM_OK;
1097}
1098
1099/*
1100 * Curl_multi_connchanged() is called to tell that there is a connection in
1101 * this multi handle that has changed state (pipelining become possible, the
1102 * number of allowed streams changed or similar), and a subsequent use of this
1103 * multi handle should move CONNECT_PEND handles back to CONNECT to have them
1104 * retry.
1105 */
1106void Curl_multi_connchanged(struct Curl_multi *multi)
1107{
1108 multi->recheckstate = TRUE;
1109}
1110
1111/*
1112 * multi_ischanged() is called
1113 *
1114 * Returns TRUE/FALSE whether the state is changed to trigger a CONNECT_PEND
1115 * => CONNECT action.
1116 *
1117 * Set 'clear' to TRUE to have it also clear the state variable.
1118 */
1119static bool multi_ischanged(struct Curl_multi *multi, bool clear)
1120{
1121 bool retval = multi->recheckstate;
1122 if(clear)
1123 multi->recheckstate = FALSE;
1124 return retval;
1125}
1126
Alex Deymod15eaac2016-06-28 14:49:26 -07001127CURLMcode Curl_multi_add_perform(struct Curl_multi *multi,
Alex Deymoe3149cc2016-10-05 11:18:42 -07001128 struct Curl_easy *data,
Alex Deymod15eaac2016-06-28 14:49:26 -07001129 struct connectdata *conn)
1130{
1131 CURLMcode rc;
1132
Elliott Hughescac39802018-04-27 16:19:43 -07001133 if(multi->in_callback)
1134 return CURLM_RECURSIVE_API_CALL;
1135
Alex Deymod15eaac2016-06-28 14:49:26 -07001136 rc = curl_multi_add_handle(multi, data);
1137 if(!rc) {
1138 struct SingleRequest *k = &data->req;
1139
1140 /* pass in NULL for 'conn' here since we don't want to init the
1141 connection, only this transfer */
1142 Curl_init_do(data, NULL);
1143
1144 /* take this handle to the perform state right away */
1145 multistate(data, CURLM_STATE_PERFORM);
1146 data->easy_conn = conn;
1147 k->keepon |= KEEP_RECV; /* setup to receive! */
1148 }
1149 return rc;
1150}
1151
1152static CURLcode multi_reconnect_request(struct connectdata **connp)
1153{
1154 CURLcode result = CURLE_OK;
1155 struct connectdata *conn = *connp;
Alex Deymoe3149cc2016-10-05 11:18:42 -07001156 struct Curl_easy *data = conn->data;
Alex Deymod15eaac2016-06-28 14:49:26 -07001157
1158 /* This was a re-use of a connection and we got a write error in the
1159 * DO-phase. Then we DISCONNECT this connection and have another attempt to
1160 * CONNECT and then DO again! The retry cannot possibly find another
1161 * connection to re-use, since we only keep one possible connection for
1162 * each. */
1163
1164 infof(data, "Re-used connection seems dead, get a new one\n");
1165
1166 connclose(conn, "Reconnect dead connection"); /* enforce close */
1167 result = multi_done(&conn, result, FALSE); /* we are so done with this */
1168
1169 /* conn may no longer be a good pointer, clear it to avoid mistakes by
1170 parent functions */
1171 *connp = NULL;
1172
1173 /*
1174 * We need to check for CURLE_SEND_ERROR here as well. This could happen
1175 * when the request failed on a FTP connection and thus multi_done() itself
1176 * tried to use the connection (again).
1177 */
1178 if(!result || (CURLE_SEND_ERROR == result)) {
1179 bool async;
1180 bool protocol_done = TRUE;
1181
1182 /* Now, redo the connect and get a new connection */
1183 result = Curl_connect(data, connp, &async, &protocol_done);
1184 if(!result) {
1185 /* We have connected or sent away a name resolve query fine */
1186
1187 conn = *connp; /* setup conn to again point to something nice */
1188 if(async) {
1189 /* Now, if async is TRUE here, we need to wait for the name
1190 to resolve */
1191 result = Curl_resolver_wait_resolv(conn, NULL);
1192 if(result)
1193 return result;
1194
1195 /* Resolved, continue with the connection */
1196 result = Curl_async_resolved(conn, &protocol_done);
1197 if(result)
1198 return result;
1199 }
1200 }
1201 }
1202
1203 return result;
1204}
1205
1206/*
1207 * do_complete is called when the DO actions are complete.
1208 *
1209 * We init chunking and trailer bits to their default values here immediately
1210 * before receiving any header data for the current request in the pipeline.
1211 */
1212static void do_complete(struct connectdata *conn)
1213{
Alex Deymo486467e2017-12-19 19:04:07 +01001214 conn->data->req.chunk = FALSE;
Alex Deymod15eaac2016-06-28 14:49:26 -07001215 conn->data->req.maxfd = (conn->sockfd>conn->writesockfd?
Alex Deymo486467e2017-12-19 19:04:07 +01001216 conn->sockfd:conn->writesockfd) + 1;
Alex Deymod15eaac2016-06-28 14:49:26 -07001217 Curl_pgrsTime(conn->data, TIMER_PRETRANSFER);
1218}
1219
1220static CURLcode multi_do(struct connectdata **connp, bool *done)
1221{
Alex Deymo486467e2017-12-19 19:04:07 +01001222 CURLcode result = CURLE_OK;
Alex Deymod15eaac2016-06-28 14:49:26 -07001223 struct connectdata *conn = *connp;
Alex Deymoe3149cc2016-10-05 11:18:42 -07001224 struct Curl_easy *data = conn->data;
Alex Deymod15eaac2016-06-28 14:49:26 -07001225
1226 if(conn->handler->do_it) {
1227 /* generic protocol-specific function pointer set in curl_connect() */
1228 result = conn->handler->do_it(conn, done);
1229
1230 /* This was formerly done in transfer.c, but we better do it here */
1231 if((CURLE_SEND_ERROR == result) && conn->bits.reuse) {
1232 /*
1233 * If the connection is using an easy handle, call reconnect
1234 * to re-establish the connection. Otherwise, let the multi logic
1235 * figure out how to re-establish the connection.
1236 */
1237 if(!data->multi) {
1238 result = multi_reconnect_request(connp);
1239
1240 if(!result) {
1241 /* ... finally back to actually retry the DO phase */
1242 conn = *connp; /* re-assign conn since multi_reconnect_request
1243 creates a new connection */
1244 result = conn->handler->do_it(conn, done);
1245 }
1246 }
1247 else
1248 return result;
1249 }
1250
1251 if(!result && *done)
1252 /* do_complete must be called after the protocol-specific DO function */
1253 do_complete(conn);
1254 }
1255 return result;
1256}
1257
1258/*
1259 * multi_do_more() is called during the DO_MORE multi state. It is basically a
1260 * second stage DO state which (wrongly) was introduced to support FTP's
1261 * second connection.
1262 *
1263 * TODO: A future libcurl should be able to work away this state.
1264 *
1265 * 'complete' can return 0 for incomplete, 1 for done and -1 for go back to
1266 * DOING state there's more work to do!
1267 */
1268
1269static CURLcode multi_do_more(struct connectdata *conn, int *complete)
1270{
Alex Deymo486467e2017-12-19 19:04:07 +01001271 CURLcode result = CURLE_OK;
Alex Deymod15eaac2016-06-28 14:49:26 -07001272
1273 *complete = 0;
1274
1275 if(conn->handler->do_more)
1276 result = conn->handler->do_more(conn, complete);
1277
1278 if(!result && (*complete == 1))
1279 /* do_complete must be called after the protocol-specific DO function */
1280 do_complete(conn);
1281
1282 return result;
1283}
1284
Kristian Monsen5ab50182010-05-14 18:53:44 +01001285static CURLMcode multi_runsingle(struct Curl_multi *multi,
Elliott Hughes82be86d2017-09-20 17:00:17 -07001286 struct curltime now,
Alex Deymoe3149cc2016-10-05 11:18:42 -07001287 struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001288{
1289 struct Curl_message *msg = NULL;
1290 bool connected;
1291 bool async;
1292 bool protocol_connect = FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001293 bool dophase_done = FALSE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001294 bool done = FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001295 CURLMcode rc;
1296 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001297 struct SingleRequest *k;
Elliott Hughescee03382017-06-23 12:17:18 -07001298 time_t timeout_ms;
Elliott Hughes82be86d2017-09-20 17:00:17 -07001299 time_t recv_timeout_ms;
Alex Deymo486467e2017-12-19 19:04:07 +01001300 timediff_t send_timeout_ms;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001301 int control;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001302
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001303 if(!GOOD_EASY_HANDLE(data))
Kristian Monsen5ab50182010-05-14 18:53:44 +01001304 return CURLM_BAD_EASY_HANDLE;
1305
1306 do {
Elliott Hughescee03382017-06-23 12:17:18 -07001307 /* A "stream" here is a logical stream if the protocol can handle that
1308 (HTTP/2), or the full connection for older protocols */
1309 bool stream_error = FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001310 rc = CURLM_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001311
1312 /* Handle the case when the pipe breaks, i.e., the connection
1313 we're using gets cleaned up and we're left with nothing. */
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001314 if(data->state.pipe_broke) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001315 infof(data, "Pipe broke: handle %p, url = %s\n",
1316 (void *)data, data->state.path);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001317
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001318 if(data->mstate < CURLM_STATE_COMPLETED) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001319 /* Head back to the CONNECT state */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001320 multistate(data, CURLM_STATE_CONNECT);
1321 rc = CURLM_CALL_MULTI_PERFORM;
1322 result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001323 }
1324
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001325 data->state.pipe_broke = FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001326 data->easy_conn = NULL;
1327 continue;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001328 }
1329
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001330 if(!data->easy_conn &&
1331 data->mstate > CURLM_STATE_CONNECT &&
1332 data->mstate < CURLM_STATE_DONE) {
1333 /* In all these states, the code will blindly access 'data->easy_conn'
1334 so this is precaution that it isn't NULL. And it silences static
1335 analyzers. */
1336 failf(data, "In state %d with no easy_conn, bail out!\n", data->mstate);
1337 return CURLM_INTERNAL_ERROR;
1338 }
1339
1340 if(multi_ischanged(multi, TRUE)) {
1341 DEBUGF(infof(data, "multi changed, check CONNECT_PEND queue!\n"));
1342 Curl_multi_process_pending_handles(multi);
1343 }
1344
1345 if(data->easy_conn && data->mstate > CURLM_STATE_CONNECT &&
Elliott Hughes0128fe42018-02-27 14:57:55 -08001346 data->mstate < CURLM_STATE_COMPLETED) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001347 /* Make sure we set the connection's current owner */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001348 data->easy_conn->data = data;
Elliott Hughes0128fe42018-02-27 14:57:55 -08001349 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001350
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001351 if(data->easy_conn &&
1352 (data->mstate >= CURLM_STATE_CONNECT) &&
1353 (data->mstate < CURLM_STATE_COMPLETED)) {
1354 /* we need to wait for the connect state as only then is the start time
1355 stored, but we must not check already completed handles */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001356 timeout_ms = Curl_timeleft(data, &now,
1357 (data->mstate <= CURLM_STATE_WAITDO)?
1358 TRUE:FALSE);
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001359
1360 if(timeout_ms < 0) {
1361 /* Handle timed out */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001362 if(data->mstate == CURLM_STATE_WAITRESOLVE)
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001363 failf(data, "Resolving timed out after %ld milliseconds",
Alex Deymo486467e2017-12-19 19:04:07 +01001364 Curl_timediff(now, data->progress.t_startsingle));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001365 else if(data->mstate == CURLM_STATE_WAITCONNECT)
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001366 failf(data, "Connection timed out after %ld milliseconds",
Alex Deymo486467e2017-12-19 19:04:07 +01001367 Curl_timediff(now, data->progress.t_startsingle));
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001368 else {
1369 k = &data->req;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001370 if(k->size != -1) {
1371 failf(data, "Operation timed out after %ld milliseconds with %"
1372 CURL_FORMAT_CURL_OFF_T " out of %"
1373 CURL_FORMAT_CURL_OFF_T " bytes received",
Alex Deymo486467e2017-12-19 19:04:07 +01001374 Curl_timediff(now, data->progress.t_startsingle),
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001375 k->bytecount, k->size);
1376 }
1377 else {
1378 failf(data, "Operation timed out after %ld milliseconds with %"
1379 CURL_FORMAT_CURL_OFF_T " bytes received",
Alex Deymo486467e2017-12-19 19:04:07 +01001380 Curl_timediff(now, data->progress.t_startsingle),
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001381 k->bytecount);
1382 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001383 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001384
1385 /* Force connection closed if the connection has indeed been used */
1386 if(data->mstate > CURLM_STATE_DO) {
Elliott Hughescee03382017-06-23 12:17:18 -07001387 streamclose(data->easy_conn, "Disconnected with pending data");
1388 stream_error = TRUE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001389 }
1390 result = CURLE_OPERATION_TIMEDOUT;
Alex Deymod15eaac2016-06-28 14:49:26 -07001391 (void)multi_done(&data->easy_conn, result, TRUE);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001392 /* Skip the statemachine and go directly to error handling section. */
1393 goto statemachine_end;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001394 }
1395 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001396
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001397 switch(data->mstate) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001398 case CURLM_STATE_INIT:
1399 /* init this transfer. */
Alex Deymo486467e2017-12-19 19:04:07 +01001400 result = Curl_pretransfer(data);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001401
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001402 if(!result) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001403 /* after init, go CONNECT */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001404 multistate(data, CURLM_STATE_CONNECT);
1405 Curl_pgrsTime(data, TIMER_STARTOP);
1406 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001407 }
1408 break;
1409
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001410 case CURLM_STATE_CONNECT_PEND:
1411 /* We will stay here until there is a connection available. Then
1412 we try again in the CURLM_STATE_CONNECT state. */
1413 break;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001414
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001415 case CURLM_STATE_CONNECT:
1416 /* Connect. We want to get a connection identifier filled in. */
1417 Curl_pgrsTime(data, TIMER_STARTSINGLE);
1418 result = Curl_connect(data, &data->easy_conn,
1419 &async, &protocol_connect);
1420 if(CURLE_NO_CONNECTION_AVAILABLE == result) {
1421 /* There was no connection available. We will go to the pending
1422 state and wait for an available connection. */
1423 multistate(data, CURLM_STATE_CONNECT_PEND);
1424
1425 /* add this handle to the list of connect-pending handles */
Elliott Hughes82be86d2017-09-20 17:00:17 -07001426 Curl_llist_insert_next(&multi->pending, multi->pending.tail, data,
1427 &data->connect_queue);
1428 result = CURLE_OK;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001429 break;
1430 }
1431
1432 if(!result) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001433 /* Add this handle to the send or pend pipeline */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001434 result = Curl_add_handle_to_pipeline(data, data->easy_conn);
1435 if(result)
Elliott Hughescee03382017-06-23 12:17:18 -07001436 stream_error = TRUE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001437 else {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001438 if(async)
1439 /* We're now waiting for an asynchronous name lookup */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001440 multistate(data, CURLM_STATE_WAITRESOLVE);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001441 else {
1442 /* after the connect has been sent off, go WAITCONNECT unless the
1443 protocol connect is already done and we can go directly to
1444 WAITDO or DO! */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001445 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001446
1447 if(protocol_connect)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001448 multistate(data, Curl_pipeline_wanted(multi, CURLPIPE_HTTP1)?
Kristian Monsen5ab50182010-05-14 18:53:44 +01001449 CURLM_STATE_WAITDO:CURLM_STATE_DO);
1450 else {
1451#ifndef CURL_DISABLE_HTTP
Elliott Hughes82be86d2017-09-20 17:00:17 -07001452 if(Curl_connect_ongoing(data->easy_conn))
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001453 multistate(data, CURLM_STATE_WAITPROXYCONNECT);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001454 else
1455#endif
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001456 multistate(data, CURLM_STATE_WAITCONNECT);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001457 }
1458 }
1459 }
1460 }
1461 break;
1462
1463 case CURLM_STATE_WAITRESOLVE:
1464 /* awaiting an asynch name resolve to complete */
1465 {
1466 struct Curl_dns_entry *dns = NULL;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001467 struct connectdata *conn = data->easy_conn;
Alex Deymod15eaac2016-06-28 14:49:26 -07001468 const char *hostname;
1469
Elliott Hughes82be86d2017-09-20 17:00:17 -07001470 if(conn->bits.httpproxy)
1471 hostname = conn->http_proxy.host.name;
Alex Deymod15eaac2016-06-28 14:49:26 -07001472 else if(conn->bits.conn_to_host)
1473 hostname = conn->conn_to_host.name;
1474 else
1475 hostname = conn->host.name;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001476
1477 /* check if we have the name resolved by now */
Alex Deymod15eaac2016-06-28 14:49:26 -07001478 dns = Curl_fetch_addr(conn, hostname, (int)conn->port);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001479
1480 if(dns) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001481#ifdef CURLRES_ASYNCH
1482 conn->async.dns = dns;
1483 conn->async.done = TRUE;
1484#endif
1485 result = CURLE_OK;
Alex Deymod15eaac2016-06-28 14:49:26 -07001486 infof(data, "Hostname '%s' was found in DNS cache\n", hostname);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001487 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001488
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001489 if(!dns)
1490 result = Curl_resolver_is_resolved(data->easy_conn, &dns);
1491
1492 /* Update sockets here, because the socket(s) may have been
1493 closed and the application thus needs to be told, even if it
1494 is likely that the same socket(s) will again be used further
1495 down. If the name has not yet been resolved, it is likely
1496 that new sockets have been opened in an attempt to contact
1497 another resolver. */
1498 singlesocket(multi, data);
1499
1500 if(dns) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001501 /* Perform the next step in the connection phase, and then move on
1502 to the WAITCONNECT state */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001503 result = Curl_async_resolved(data->easy_conn, &protocol_connect);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001504
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001505 if(result)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001506 /* if Curl_async_resolved() returns failure, the connection struct
1507 is already freed and gone */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001508 data->easy_conn = NULL; /* no more connection */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001509 else {
1510 /* call again please so that we get the next socket setup */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001511 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001512 if(protocol_connect)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001513 multistate(data, Curl_pipeline_wanted(multi, CURLPIPE_HTTP1)?
Kristian Monsen5ab50182010-05-14 18:53:44 +01001514 CURLM_STATE_WAITDO:CURLM_STATE_DO);
1515 else {
1516#ifndef CURL_DISABLE_HTTP
Elliott Hughes82be86d2017-09-20 17:00:17 -07001517 if(Curl_connect_ongoing(data->easy_conn))
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001518 multistate(data, CURLM_STATE_WAITPROXYCONNECT);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001519 else
1520#endif
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001521 multistate(data, CURLM_STATE_WAITCONNECT);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001522 }
1523 }
1524 }
1525
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001526 if(result) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001527 /* failure detected */
Elliott Hughescee03382017-06-23 12:17:18 -07001528 stream_error = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001529 break;
1530 }
1531 }
1532 break;
1533
1534#ifndef CURL_DISABLE_HTTP
1535 case CURLM_STATE_WAITPROXYCONNECT:
1536 /* this is HTTP-specific, but sending CONNECT to a proxy is HTTP... */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001537 result = Curl_http_connect(data->easy_conn, &protocol_connect);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001538
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001539 if(data->easy_conn->bits.proxy_connect_closed) {
Alex Deymod15eaac2016-06-28 14:49:26 -07001540 rc = CURLM_CALL_MULTI_PERFORM;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001541 /* connect back to proxy again */
1542 result = CURLE_OK;
Alex Deymod15eaac2016-06-28 14:49:26 -07001543 multi_done(&data->easy_conn, CURLE_OK, FALSE);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001544 multistate(data, CURLM_STATE_CONNECT);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001545 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001546 else if(!result) {
Elliott Hughescee03382017-06-23 12:17:18 -07001547 if((data->easy_conn->http_proxy.proxytype != CURLPROXY_HTTPS ||
1548 data->easy_conn->bits.proxy_ssl_connected[FIRSTSOCKET]) &&
Elliott Hughes82be86d2017-09-20 17:00:17 -07001549 Curl_connect_complete(data->easy_conn)) {
Alex Deymod15eaac2016-06-28 14:49:26 -07001550 rc = CURLM_CALL_MULTI_PERFORM;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001551 /* initiate protocol connect phase */
1552 multistate(data, CURLM_STATE_SENDPROTOCONNECT);
Alex Deymod15eaac2016-06-28 14:49:26 -07001553 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001554 }
1555 break;
1556#endif
1557
1558 case CURLM_STATE_WAITCONNECT:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001559 /* awaiting a completion of an asynch TCP connect */
1560 result = Curl_is_connected(data->easy_conn, FIRSTSOCKET, &connected);
1561 if(connected && !result) {
Elliott Hughescee03382017-06-23 12:17:18 -07001562#ifndef CURL_DISABLE_HTTP
1563 if((data->easy_conn->http_proxy.proxytype == CURLPROXY_HTTPS &&
1564 !data->easy_conn->bits.proxy_ssl_connected[FIRSTSOCKET]) ||
Elliott Hughes82be86d2017-09-20 17:00:17 -07001565 Curl_connect_ongoing(data->easy_conn)) {
Elliott Hughescee03382017-06-23 12:17:18 -07001566 multistate(data, CURLM_STATE_WAITPROXYCONNECT);
1567 break;
1568 }
1569#endif
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001570 rc = CURLM_CALL_MULTI_PERFORM;
1571 multistate(data, data->easy_conn->bits.tunnel_proxy?
1572 CURLM_STATE_WAITPROXYCONNECT:
1573 CURLM_STATE_SENDPROTOCONNECT);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001574 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001575 else if(result) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001576 /* failure detected */
1577 /* Just break, the cleaning up is handled all in one place */
Elliott Hughescee03382017-06-23 12:17:18 -07001578 stream_error = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001579 break;
1580 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001581 break;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001582
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001583 case CURLM_STATE_SENDPROTOCONNECT:
1584 result = Curl_protocol_connect(data->easy_conn, &protocol_connect);
1585 if(!protocol_connect)
1586 /* switch to waiting state */
1587 multistate(data, CURLM_STATE_PROTOCONNECT);
1588 else if(!result) {
1589 /* protocol connect has completed, go WAITDO or DO */
1590 multistate(data, Curl_pipeline_wanted(multi, CURLPIPE_HTTP1)?
1591 CURLM_STATE_WAITDO:CURLM_STATE_DO);
1592 rc = CURLM_CALL_MULTI_PERFORM;
1593 }
1594 else if(result) {
1595 /* failure detected */
1596 Curl_posttransfer(data);
Alex Deymod15eaac2016-06-28 14:49:26 -07001597 multi_done(&data->easy_conn, result, TRUE);
Elliott Hughescee03382017-06-23 12:17:18 -07001598 stream_error = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001599 }
1600 break;
1601
1602 case CURLM_STATE_PROTOCONNECT:
1603 /* protocol-specific connect phase */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001604 result = Curl_protocol_connecting(data->easy_conn, &protocol_connect);
1605 if(!result && protocol_connect) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001606 /* after the connect has completed, go WAITDO or DO */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001607 multistate(data, Curl_pipeline_wanted(multi, CURLPIPE_HTTP1)?
Kristian Monsen5ab50182010-05-14 18:53:44 +01001608 CURLM_STATE_WAITDO:CURLM_STATE_DO);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001609 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001610 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001611 else if(result) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001612 /* failure detected */
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001613 Curl_posttransfer(data);
Alex Deymod15eaac2016-06-28 14:49:26 -07001614 multi_done(&data->easy_conn, result, TRUE);
Elliott Hughescee03382017-06-23 12:17:18 -07001615 stream_error = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001616 }
1617 break;
1618
1619 case CURLM_STATE_WAITDO:
1620 /* Wait for our turn to DO when we're pipelining requests */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001621 if(Curl_pipeline_checkget_write(data, data->easy_conn)) {
1622 /* Grabbed the channel */
1623 multistate(data, CURLM_STATE_DO);
1624 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001625 }
1626 break;
1627
1628 case CURLM_STATE_DO:
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001629 if(data->set.connect_only) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001630 /* keep connection open for application to use the socket */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001631 connkeep(data->easy_conn, "CONNECT_ONLY");
1632 multistate(data, CURLM_STATE_DONE);
1633 result = CURLE_OK;
1634 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001635 }
1636 else {
1637 /* Perform the protocol's DO action */
Alex Deymod15eaac2016-06-28 14:49:26 -07001638 result = multi_do(&data->easy_conn, &dophase_done);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001639
Alex Deymod15eaac2016-06-28 14:49:26 -07001640 /* When multi_do() returns failure, data->easy_conn might be NULL! */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001641
1642 if(!result) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001643 if(!dophase_done) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001644 /* some steps needed for wildcard matching */
Alex Deymo486467e2017-12-19 19:04:07 +01001645 if(data->state.wildcardmatch) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001646 struct WildcardData *wc = &data->wildcard;
1647 if(wc->state == CURLWC_DONE || wc->state == CURLWC_SKIP) {
1648 /* skip some states if it is important */
Alex Deymod15eaac2016-06-28 14:49:26 -07001649 multi_done(&data->easy_conn, CURLE_OK, FALSE);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001650 multistate(data, CURLM_STATE_DONE);
1651 rc = CURLM_CALL_MULTI_PERFORM;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001652 break;
1653 }
1654 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001655 /* DO was not completed in one function call, we must continue
1656 DOING... */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001657 multistate(data, CURLM_STATE_DOING);
1658 rc = CURLM_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001659 }
1660
1661 /* after DO, go DO_DONE... or DO_MORE */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001662 else if(data->easy_conn->bits.do_more) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001663 /* we're supposed to do more, but we need to sit down, relax
1664 and wait a little while first */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001665 multistate(data, CURLM_STATE_DO_MORE);
1666 rc = CURLM_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001667 }
1668 else {
1669 /* we're done with the DO, now DO_DONE */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001670 multistate(data, CURLM_STATE_DO_DONE);
1671 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001672 }
1673 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001674 else if((CURLE_SEND_ERROR == result) &&
1675 data->easy_conn->bits.reuse) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001676 /*
1677 * In this situation, a connection that we were trying to use
1678 * may have unexpectedly died. If possible, send the connection
1679 * back to the CONNECT phase so we can try again.
1680 */
1681 char *newurl = NULL;
Alex Deymo486467e2017-12-19 19:04:07 +01001682 followtype follow = FOLLOW_NONE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001683 CURLcode drc;
1684 bool retry = FALSE;
1685
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001686 drc = Curl_retry_request(data->easy_conn, &newurl);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001687 if(drc) {
1688 /* a failure here pretty much implies an out of memory */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001689 result = drc;
Elliott Hughescee03382017-06-23 12:17:18 -07001690 stream_error = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001691 }
1692 else
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001693 retry = (newurl)?TRUE:FALSE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001694
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001695 Curl_posttransfer(data);
Alex Deymod15eaac2016-06-28 14:49:26 -07001696 drc = multi_done(&data->easy_conn, result, FALSE);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001697
1698 /* When set to retry the connection, we must to go back to
1699 * the CONNECT state */
1700 if(retry) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001701 if(!drc || (drc == CURLE_SEND_ERROR)) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001702 follow = FOLLOW_RETRY;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001703 drc = Curl_follow(data, newurl, follow);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001704 if(!drc) {
1705 multistate(data, CURLM_STATE_CONNECT);
1706 rc = CURLM_CALL_MULTI_PERFORM;
1707 result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001708 }
1709 else {
1710 /* Follow failed */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001711 result = drc;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001712 }
1713 }
1714 else {
1715 /* done didn't return OK or SEND_ERROR */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001716 result = drc;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001717 }
1718 }
1719 else {
1720 /* Have error handler disconnect conn if we can't retry */
Elliott Hughescee03382017-06-23 12:17:18 -07001721 stream_error = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001722 }
Elliott Hughes82be86d2017-09-20 17:00:17 -07001723 free(newurl);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001724 }
1725 else {
1726 /* failure detected */
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001727 Curl_posttransfer(data);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001728 if(data->easy_conn)
Alex Deymod15eaac2016-06-28 14:49:26 -07001729 multi_done(&data->easy_conn, result, FALSE);
Elliott Hughescee03382017-06-23 12:17:18 -07001730 stream_error = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001731 }
1732 }
1733 break;
1734
1735 case CURLM_STATE_DOING:
1736 /* we continue DOING until the DO phase is complete */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001737 result = Curl_protocol_doing(data->easy_conn,
1738 &dophase_done);
1739 if(!result) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001740 if(dophase_done) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001741 /* after DO, go DO_DONE or DO_MORE */
1742 multistate(data, data->easy_conn->bits.do_more?
1743 CURLM_STATE_DO_MORE:
1744 CURLM_STATE_DO_DONE);
1745 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001746 } /* dophase_done */
1747 }
1748 else {
1749 /* failure detected */
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001750 Curl_posttransfer(data);
Alex Deymod15eaac2016-06-28 14:49:26 -07001751 multi_done(&data->easy_conn, result, FALSE);
Elliott Hughescee03382017-06-23 12:17:18 -07001752 stream_error = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001753 }
1754 break;
1755
1756 case CURLM_STATE_DO_MORE:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001757 /*
1758 * When we are connected, DO MORE and then go DO_DONE
1759 */
Alex Deymod15eaac2016-06-28 14:49:26 -07001760 result = multi_do_more(data->easy_conn, &control);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001761
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001762 /* No need to remove this handle from the send pipeline here since that
Alex Deymod15eaac2016-06-28 14:49:26 -07001763 is done in multi_done() */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001764 if(!result) {
1765 if(control) {
1766 /* if positive, advance to DO_DONE
1767 if negative, go back to DOING */
Alex Deymo486467e2017-12-19 19:04:07 +01001768 multistate(data, control == 1?
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001769 CURLM_STATE_DO_DONE:
1770 CURLM_STATE_DOING);
1771 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001772 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001773 else
1774 /* stay in DO_MORE */
1775 rc = CURLM_OK;
1776 }
1777 else {
1778 /* failure detected */
1779 Curl_posttransfer(data);
Alex Deymod15eaac2016-06-28 14:49:26 -07001780 multi_done(&data->easy_conn, result, FALSE);
Elliott Hughescee03382017-06-23 12:17:18 -07001781 stream_error = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001782 }
1783 break;
1784
1785 case CURLM_STATE_DO_DONE:
1786 /* Move ourselves from the send to recv pipeline */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001787 Curl_move_handle_from_send_to_recv_pipe(data, data->easy_conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001788 /* Check if we can move pending requests to send pipe */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001789 Curl_multi_process_pending_handles(multi);
1790
1791 /* Only perform the transfer if there's a good socket to work with.
1792 Having both BAD is a signal to skip immediately to DONE */
1793 if((data->easy_conn->sockfd != CURL_SOCKET_BAD) ||
1794 (data->easy_conn->writesockfd != CURL_SOCKET_BAD))
1795 multistate(data, CURLM_STATE_WAITPERFORM);
1796 else
Alex Deymo486467e2017-12-19 19:04:07 +01001797 {
1798 if(data->state.wildcardmatch &&
1799 ((data->easy_conn->handler->flags & PROTOPT_WILDCARD) == 0)) {
1800 data->wildcard.state = CURLWC_DONE;
1801 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001802 multistate(data, CURLM_STATE_DONE);
Alex Deymo486467e2017-12-19 19:04:07 +01001803 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001804 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001805 break;
1806
1807 case CURLM_STATE_WAITPERFORM:
1808 /* Wait for our turn to PERFORM */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001809 if(Curl_pipeline_checkget_read(data, data->easy_conn)) {
1810 /* Grabbed the channel */
1811 multistate(data, CURLM_STATE_PERFORM);
1812 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001813 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001814 break;
1815
1816 case CURLM_STATE_TOOFAST: /* limit-rate exceeded in either direction */
1817 /* if both rates are within spec, resume transfer */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001818 if(Curl_pgrsUpdate(data->easy_conn))
1819 result = CURLE_ABORTED_BY_CALLBACK;
1820 else
1821 result = Curl_speedcheck(data, now);
1822
Elliott Hughes82be86d2017-09-20 17:00:17 -07001823 if(!result) {
1824 send_timeout_ms = 0;
1825 if(data->set.max_send_speed > 0)
1826 send_timeout_ms = Curl_pgrsLimitWaitTime(data->progress.uploaded,
1827 data->progress.ul_limit_size,
1828 data->set.max_send_speed,
1829 data->progress.ul_limit_start,
1830 now);
1831
1832 recv_timeout_ms = 0;
1833 if(data->set.max_recv_speed > 0)
1834 recv_timeout_ms = Curl_pgrsLimitWaitTime(data->progress.downloaded,
1835 data->progress.dl_limit_size,
1836 data->set.max_recv_speed,
1837 data->progress.dl_limit_start,
1838 now);
1839
1840 if(send_timeout_ms <= 0 && recv_timeout_ms <= 0)
1841 multistate(data, CURLM_STATE_PERFORM);
1842 else if(send_timeout_ms >= recv_timeout_ms)
1843 Curl_expire(data, send_timeout_ms, EXPIRE_TOOFAST);
1844 else
1845 Curl_expire(data, recv_timeout_ms, EXPIRE_TOOFAST);
1846 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001847 break;
1848
1849 case CURLM_STATE_PERFORM:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001850 {
1851 char *newurl = NULL;
1852 bool retry = FALSE;
Elliott Hughescee03382017-06-23 12:17:18 -07001853 bool comeback = FALSE;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001854
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001855 /* check if over send speed */
Elliott Hughes82be86d2017-09-20 17:00:17 -07001856 send_timeout_ms = 0;
1857 if(data->set.max_send_speed > 0)
1858 send_timeout_ms = Curl_pgrsLimitWaitTime(data->progress.uploaded,
1859 data->progress.ul_limit_size,
1860 data->set.max_send_speed,
1861 data->progress.ul_limit_start,
1862 now);
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001863
1864 /* check if over recv speed */
Elliott Hughes82be86d2017-09-20 17:00:17 -07001865 recv_timeout_ms = 0;
1866 if(data->set.max_recv_speed > 0)
1867 recv_timeout_ms = Curl_pgrsLimitWaitTime(data->progress.downloaded,
1868 data->progress.dl_limit_size,
1869 data->set.max_recv_speed,
1870 data->progress.dl_limit_start,
1871 now);
1872
1873 if(send_timeout_ms > 0 || recv_timeout_ms > 0) {
1874 multistate(data, CURLM_STATE_TOOFAST);
1875 if(send_timeout_ms >= recv_timeout_ms)
1876 Curl_expire(data, send_timeout_ms, EXPIRE_TOOFAST);
1877 else
1878 Curl_expire(data, recv_timeout_ms, EXPIRE_TOOFAST);
1879 break;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001880 }
1881
1882 /* read/write data if it is ready to do so */
Elliott Hughescee03382017-06-23 12:17:18 -07001883 result = Curl_readwrite(data->easy_conn, data, &done, &comeback);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001884
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001885 k = &data->req;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001886
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001887 if(!(k->keepon & KEEP_RECV))
Kristian Monsen5ab50182010-05-14 18:53:44 +01001888 /* We're done receiving */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001889 Curl_pipeline_leave_read(data->easy_conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001890
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001891 if(!(k->keepon & KEEP_SEND))
Kristian Monsen5ab50182010-05-14 18:53:44 +01001892 /* We're done sending */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001893 Curl_pipeline_leave_write(data->easy_conn);
1894
1895 if(done || (result == CURLE_RECV_ERROR)) {
1896 /* If CURLE_RECV_ERROR happens early enough, we assume it was a race
1897 * condition and the server closed the re-used connection exactly when
1898 * we wanted to use it, so figure out if that is indeed the case.
1899 */
1900 CURLcode ret = Curl_retry_request(data->easy_conn, &newurl);
1901 if(!ret)
1902 retry = (newurl)?TRUE:FALSE;
1903
1904 if(retry) {
1905 /* if we are to retry, set the result to OK and consider the
1906 request as done */
1907 result = CURLE_OK;
1908 done = TRUE;
1909 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001910 }
1911
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001912 if(result) {
1913 /*
1914 * The transfer phase returned error, we mark the connection to get
Kristian Monsen5ab50182010-05-14 18:53:44 +01001915 * closed to prevent being re-used. This is because we can't possibly
1916 * know if the connection is in a good shape or not now. Unless it is
1917 * a protocol which uses two "channels" like FTP, as then the error
1918 * happened in the data connection.
1919 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001920
Alex Deymod15eaac2016-06-28 14:49:26 -07001921 if(!(data->easy_conn->handler->flags & PROTOPT_DUAL) &&
1922 result != CURLE_HTTP2_STREAM)
Elliott Hughescee03382017-06-23 12:17:18 -07001923 streamclose(data->easy_conn, "Transfer returned error");
Kristian Monsen5ab50182010-05-14 18:53:44 +01001924
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001925 Curl_posttransfer(data);
Elliott Hughescee03382017-06-23 12:17:18 -07001926 multi_done(&data->easy_conn, result, TRUE);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001927 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001928 else if(done) {
Alex Deymo486467e2017-12-19 19:04:07 +01001929 followtype follow = FOLLOW_NONE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001930
Kristian Monsen5ab50182010-05-14 18:53:44 +01001931 /* call this even if the readwrite function returned error */
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001932 Curl_posttransfer(data);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001933
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001934 /* we're no longer receiving */
Elliott Hughes82be86d2017-09-20 17:00:17 -07001935 Curl_removeHandleFromPipeline(data, &data->easy_conn->recv_pipe);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001936
1937 /* expire the new receiving pipeline head */
Elliott Hughes82be86d2017-09-20 17:00:17 -07001938 if(data->easy_conn->recv_pipe.head)
1939 Curl_expire(data->easy_conn->recv_pipe.head->ptr, 0, EXPIRE_RUN_NOW);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001940
1941 /* Check if we can move pending requests to send pipe */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001942 Curl_multi_process_pending_handles(multi);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001943
1944 /* When we follow redirects or is set to retry the connection, we must
1945 to go back to the CONNECT state */
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001946 if(data->req.newurl || retry) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001947 if(!retry) {
1948 /* if the URL is a follow-location and not just a retried request
1949 then figure out the URL here */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001950 free(newurl);
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001951 newurl = data->req.newurl;
1952 data->req.newurl = NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001953 follow = FOLLOW_REDIR;
1954 }
1955 else
1956 follow = FOLLOW_RETRY;
Alex Deymod15eaac2016-06-28 14:49:26 -07001957 result = multi_done(&data->easy_conn, CURLE_OK, FALSE);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001958 if(!result) {
1959 result = Curl_follow(data, newurl, follow);
1960 if(!result) {
1961 multistate(data, CURLM_STATE_CONNECT);
1962 rc = CURLM_CALL_MULTI_PERFORM;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001963 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001964 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001965 }
1966 else {
1967 /* after the transfer is done, go DONE */
1968
1969 /* but first check to see if we got a location info even though we're
1970 not following redirects */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001971 if(data->req.location) {
1972 free(newurl);
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001973 newurl = data->req.location;
1974 data->req.location = NULL;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001975 result = Curl_follow(data, newurl, FOLLOW_FAKE);
Elliott Hughes82be86d2017-09-20 17:00:17 -07001976 if(result)
Elliott Hughescee03382017-06-23 12:17:18 -07001977 stream_error = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001978 }
1979
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001980 multistate(data, CURLM_STATE_DONE);
1981 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001982 }
1983 }
Elliott Hughescee03382017-06-23 12:17:18 -07001984 else if(comeback)
1985 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001986
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001987 free(newurl);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001988 break;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001989 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001990
1991 case CURLM_STATE_DONE:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001992 /* this state is highly transient, so run another loop after this */
1993 rc = CURLM_CALL_MULTI_PERFORM;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001994
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001995 if(data->easy_conn) {
1996 CURLcode res;
1997
1998 /* Remove ourselves from the receive pipeline, if we are there. */
Elliott Hughes82be86d2017-09-20 17:00:17 -07001999 Curl_removeHandleFromPipeline(data, &data->easy_conn->recv_pipe);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002000 /* Check if we can move pending requests to send pipe */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002001 Curl_multi_process_pending_handles(multi);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002002
2003 /* post-transfer command */
Alex Deymod15eaac2016-06-28 14:49:26 -07002004 res = multi_done(&data->easy_conn, result, FALSE);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002005
2006 /* allow a previously set error code take precedence */
2007 if(!result)
2008 result = res;
2009
Kristian Monsen5ab50182010-05-14 18:53:44 +01002010 /*
Alex Deymod15eaac2016-06-28 14:49:26 -07002011 * If there are other handles on the pipeline, multi_done won't set
Kristian Monsen5ab50182010-05-14 18:53:44 +01002012 * easy_conn to NULL. In such a case, curl_multi_remove_handle() can
2013 * access free'd data, if the connection is free'd and the handle
2014 * removed before we perform the processing in CURLM_STATE_COMPLETED
2015 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002016 if(data->easy_conn)
2017 data->easy_conn = NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002018 }
2019
Alex Deymo486467e2017-12-19 19:04:07 +01002020 if(data->state.wildcardmatch) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002021 if(data->wildcard.state != CURLWC_DONE) {
2022 /* if a wildcard is set and we are not ending -> lets start again
2023 with CURLM_STATE_INIT */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002024 multistate(data, CURLM_STATE_INIT);
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002025 break;
2026 }
2027 }
2028
Kristian Monsen5ab50182010-05-14 18:53:44 +01002029 /* after we have DONE what we're supposed to do, go COMPLETED, and
Alex Deymod15eaac2016-06-28 14:49:26 -07002030 it doesn't matter what the multi_done() returned! */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002031 multistate(data, CURLM_STATE_COMPLETED);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002032 break;
2033
2034 case CURLM_STATE_COMPLETED:
2035 /* this is a completed transfer, it is likely to still be connected */
2036
2037 /* This node should be delinked from the list now and we should post
2038 an information message that we are complete. */
2039
2040 /* Important: reset the conn pointer so that we don't point to memory
2041 that could be freed anytime */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002042 data->easy_conn = NULL;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002043
Elliott Hughescee03382017-06-23 12:17:18 -07002044 Curl_expire_clear(data); /* stop all timers */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002045 break;
2046
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002047 case CURLM_STATE_MSGSENT:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002048 data->result = result;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002049 return CURLM_OK; /* do nothing */
2050
Kristian Monsen5ab50182010-05-14 18:53:44 +01002051 default:
2052 return CURLM_INTERNAL_ERROR;
2053 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002054 statemachine_end:
Kristian Monsen5ab50182010-05-14 18:53:44 +01002055
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002056 if(data->mstate < CURLM_STATE_COMPLETED) {
2057 if(result) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002058 /*
2059 * If an error was returned, and we aren't in completed state now,
2060 * then we go to completed and consider this transfer aborted.
2061 */
2062
2063 /* NOTE: no attempt to disconnect connections must be made
2064 in the case blocks above - cleanup happens only here */
2065
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002066 data->state.pipe_broke = FALSE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002067
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002068 /* Check if we can move pending requests to send pipe */
2069 Curl_multi_process_pending_handles(multi);
2070
2071 if(data->easy_conn) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002072 /* if this has a connection, unsubscribe from the pipelines */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002073 Curl_pipeline_leave_write(data->easy_conn);
2074 Curl_pipeline_leave_read(data->easy_conn);
Elliott Hughes82be86d2017-09-20 17:00:17 -07002075 Curl_removeHandleFromPipeline(data, &data->easy_conn->send_pipe);
2076 Curl_removeHandleFromPipeline(data, &data->easy_conn->recv_pipe);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002077
Elliott Hughescee03382017-06-23 12:17:18 -07002078 if(stream_error) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002079 /* Don't attempt to send data over a connection that timed out */
2080 bool dead_connection = result == CURLE_OPERATION_TIMEDOUT;
2081 /* disconnect properly */
2082 Curl_disconnect(data->easy_conn, dead_connection);
2083
2084 /* This is where we make sure that the easy_conn pointer is reset.
2085 We don't have to do this in every case block above where a
2086 failure is detected */
2087 data->easy_conn = NULL;
2088 }
2089 }
2090 else if(data->mstate == CURLM_STATE_CONNECT) {
2091 /* Curl_connect() failed */
2092 (void)Curl_posttransfer(data);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002093 }
2094
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002095 multistate(data, CURLM_STATE_COMPLETED);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002096 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002097 /* if there's still a connection to use, call the progress function */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002098 else if(data->easy_conn && Curl_pgrsUpdate(data->easy_conn)) {
2099 /* aborted due to progress callback return code must close the
2100 connection */
2101 result = CURLE_ABORTED_BY_CALLBACK;
Elliott Hughescee03382017-06-23 12:17:18 -07002102 streamclose(data->easy_conn, "Aborted by callback");
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002103
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002104 /* if not yet in DONE state, go there, otherwise COMPLETED */
2105 multistate(data, (data->mstate < CURLM_STATE_DONE)?
2106 CURLM_STATE_DONE: CURLM_STATE_COMPLETED);
2107 rc = CURLM_CALL_MULTI_PERFORM;
2108 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01002109 }
2110
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002111 if(CURLM_STATE_COMPLETED == data->mstate) {
2112 /* now fill in the Curl_message with this info */
2113 msg = &data->msg;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002114
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002115 msg->extmsg.msg = CURLMSG_DONE;
2116 msg->extmsg.easy_handle = data;
2117 msg->extmsg.data.result = result;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002118
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002119 rc = multi_addmsg(multi, msg);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002120
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002121 multistate(data, CURLM_STATE_MSGSENT);
2122 }
2123 } while((rc == CURLM_CALL_MULTI_PERFORM) || multi_ischanged(multi, FALSE));
Kristian Monsen5ab50182010-05-14 18:53:44 +01002124
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002125 data->result = result;
2126
2127
2128 return rc;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002129}
2130
2131
Alex Deymoe3149cc2016-10-05 11:18:42 -07002132CURLMcode curl_multi_perform(struct Curl_multi *multi, int *running_handles)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002133{
Alex Deymoe3149cc2016-10-05 11:18:42 -07002134 struct Curl_easy *data;
Alex Deymo486467e2017-12-19 19:04:07 +01002135 CURLMcode returncode = CURLM_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002136 struct Curl_tree *t;
Alex Deymo486467e2017-12-19 19:04:07 +01002137 struct curltime now = Curl_now();
Kristian Monsen5ab50182010-05-14 18:53:44 +01002138
2139 if(!GOOD_MULTI_HANDLE(multi))
2140 return CURLM_BAD_HANDLE;
2141
Elliott Hughescac39802018-04-27 16:19:43 -07002142 if(multi->in_callback)
2143 return CURLM_RECURSIVE_API_CALL;
2144
Alex Deymo486467e2017-12-19 19:04:07 +01002145 data = multi->easyp;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002146 while(data) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002147 CURLMcode result;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002148 SIGPIPE_VARIABLE(pipe_st);
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002149
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002150 sigpipe_ignore(data, &pipe_st);
2151 result = multi_runsingle(multi, now, data);
2152 sigpipe_restore(&pipe_st);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002153
2154 if(result)
2155 returncode = result;
2156
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002157 data = data->next; /* operate on next handle */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002158 }
2159
2160 /*
2161 * Simply remove all expired timers from the splay since handles are dealt
2162 * with unconditionally by this function and curl_multi_timeout() requires
2163 * that already passed/handled expire times are removed from the splay.
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002164 *
2165 * It is important that the 'now' value is set at the entry of this function
2166 * and not for the current time as it may have ticked a little while since
2167 * then and then we risk this loop to remove timers that actually have not
2168 * been handled!
Kristian Monsen5ab50182010-05-14 18:53:44 +01002169 */
2170 do {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002171 multi->timetree = Curl_splaygetbest(now, multi->timetree, &t);
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002172 if(t)
2173 /* the removed may have another timeout in queue */
2174 (void)add_next_timeout(now, multi, t->payload);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002175
2176 } while(t);
2177
2178 *running_handles = multi->num_alive;
2179
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002180 if(CURLM_OK >= returncode)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002181 update_timer(multi);
2182
2183 return returncode;
2184}
2185
Alex Deymoe3149cc2016-10-05 11:18:42 -07002186CURLMcode curl_multi_cleanup(struct Curl_multi *multi)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002187{
Alex Deymoe3149cc2016-10-05 11:18:42 -07002188 struct Curl_easy *data;
2189 struct Curl_easy *nextdata;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002190
2191 if(GOOD_MULTI_HANDLE(multi)) {
Elliott Hughescac39802018-04-27 16:19:43 -07002192 if(multi->in_callback)
2193 return CURLM_RECURSIVE_API_CALL;
2194
Kristian Monsen5ab50182010-05-14 18:53:44 +01002195 multi->type = 0; /* not good anymore */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002196
Alex Deymo486467e2017-12-19 19:04:07 +01002197 /* Firsrt remove all remaining easy handles */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002198 data = multi->easyp;
2199 while(data) {
Alex Deymo486467e2017-12-19 19:04:07 +01002200 nextdata = data->next;
2201 if(!data->state.done && data->easy_conn)
2202 /* if DONE was never called for this handle */
2203 (void)multi_done(&data->easy_conn, CURLE_OK, TRUE);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002204 if(data->dns.hostcachetype == HCACHE_MULTI) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002205 /* clear out the usage of the shared DNS cache */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002206 Curl_hostcache_clean(data, data->dns.hostcache);
2207 data->dns.hostcache = NULL;
2208 data->dns.hostcachetype = HCACHE_NONE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002209 }
2210
2211 /* Clear the pointer to the connection cache */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002212 data->state.conn_cache = NULL;
2213 data->multi = NULL; /* clear the association */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002214
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002215 data = nextdata;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002216 }
2217
Alex Deymo486467e2017-12-19 19:04:07 +01002218 /* Close all the connections in the connection cache */
2219 Curl_conncache_close_all_connections(&multi->conn_cache);
2220
2221 Curl_hash_destroy(&multi->sockhash);
2222 Curl_conncache_destroy(&multi->conn_cache);
2223 Curl_llist_destroy(&multi->msglist, NULL);
2224 Curl_llist_destroy(&multi->pending, NULL);
2225
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002226 Curl_hash_destroy(&multi->hostcache);
2227
2228 /* Free the blacklists by setting them to NULL */
2229 Curl_pipeline_set_site_blacklist(NULL, &multi->pipelining_site_bl);
2230 Curl_pipeline_set_server_blacklist(NULL, &multi->pipelining_server_bl);
2231
Kristian Monsen5ab50182010-05-14 18:53:44 +01002232 free(multi);
2233
2234 return CURLM_OK;
2235 }
Elliott Hughes82be86d2017-09-20 17:00:17 -07002236 return CURLM_BAD_HANDLE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002237}
2238
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002239/*
2240 * curl_multi_info_read()
2241 *
2242 * This function is the primary way for a multi/multi_socket application to
2243 * figure out if a transfer has ended. We MUST make this function as fast as
2244 * possible as it will be polled frequently and we MUST NOT scan any lists in
2245 * here to figure out things. We must scale fine to thousands of handles and
2246 * beyond. The current design is fully O(1).
2247 */
2248
Alex Deymoe3149cc2016-10-05 11:18:42 -07002249CURLMsg *curl_multi_info_read(struct Curl_multi *multi, int *msgs_in_queue)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002250{
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002251 struct Curl_message *msg;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002252
2253 *msgs_in_queue = 0; /* default to none */
2254
Elliott Hughescac39802018-04-27 16:19:43 -07002255 if(GOOD_MULTI_HANDLE(multi) &&
2256 !multi->in_callback &&
2257 Curl_llist_count(&multi->msglist)) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002258 /* there is one or more messages in the list */
2259 struct curl_llist_element *e;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002260
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002261 /* extract the head of the list to return */
Elliott Hughes82be86d2017-09-20 17:00:17 -07002262 e = multi->msglist.head;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002263
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002264 msg = e->ptr;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002265
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002266 /* remove the extracted entry */
Elliott Hughes82be86d2017-09-20 17:00:17 -07002267 Curl_llist_remove(&multi->msglist, e, NULL);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002268
Elliott Hughes82be86d2017-09-20 17:00:17 -07002269 *msgs_in_queue = curlx_uztosi(Curl_llist_count(&multi->msglist));
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002270
2271 return &msg->extmsg;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002272 }
Elliott Hughes82be86d2017-09-20 17:00:17 -07002273 return NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002274}
2275
2276/*
2277 * singlesocket() checks what sockets we deal with and their "action state"
2278 * and if we have a different state in any of those sockets from last time we
2279 * call the callback accordingly.
2280 */
2281static void singlesocket(struct Curl_multi *multi,
Alex Deymoe3149cc2016-10-05 11:18:42 -07002282 struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002283{
2284 curl_socket_t socks[MAX_SOCKSPEREASYHANDLE];
2285 int i;
2286 struct Curl_sh_entry *entry;
2287 curl_socket_t s;
2288 int num;
2289 unsigned int curraction;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002290
Alex Deymo486467e2017-12-19 19:04:07 +01002291 for(i = 0; i< MAX_SOCKSPEREASYHANDLE; i++)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002292 socks[i] = CURL_SOCKET_BAD;
2293
2294 /* Fill in the 'current' struct with the state as it is now: what sockets to
2295 supervise and for what actions */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002296 curraction = multi_getsock(data, socks, MAX_SOCKSPEREASYHANDLE);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002297
2298 /* We have 0 .. N sockets already and we get to know about the 0 .. M
2299 sockets we should have from now on. Detect the differences, remove no
2300 longer supervised ones and add new ones */
2301
2302 /* walk over the sockets we got right now */
Alex Deymo486467e2017-12-19 19:04:07 +01002303 for(i = 0; (i< MAX_SOCKSPEREASYHANDLE) &&
Kristian Monsen5ab50182010-05-14 18:53:44 +01002304 (curraction & (GETSOCK_READSOCK(i) | GETSOCK_WRITESOCK(i)));
2305 i++) {
2306 int action = CURL_POLL_NONE;
2307
2308 s = socks[i];
2309
2310 /* get it from the hash */
Alex Deymod15eaac2016-06-28 14:49:26 -07002311 entry = sh_getentry(&multi->sockhash, s);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002312
2313 if(curraction & GETSOCK_READSOCK(i))
2314 action |= CURL_POLL_IN;
2315 if(curraction & GETSOCK_WRITESOCK(i))
2316 action |= CURL_POLL_OUT;
2317
2318 if(entry) {
2319 /* yeps, already present so check if it has the same action set */
2320 if(entry->action == action)
2321 /* same, continue */
2322 continue;
2323 }
2324 else {
2325 /* this is a socket we didn't have before, add it! */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002326 entry = sh_addentry(&multi->sockhash, s, data);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002327 if(!entry)
2328 /* fatal */
2329 return;
2330 }
2331
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002332 /* we know (entry != NULL) at this point, see the logic above */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002333 if(multi->socket_cb)
2334 multi->socket_cb(data,
2335 s,
2336 action,
2337 multi->socket_userp,
2338 entry->socketp);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002339
2340 entry->action = action; /* store the current action state */
2341 }
2342
2343 num = i; /* number of sockets */
2344
2345 /* when we've walked over all the sockets we should have right now, we must
2346 make sure to detect sockets that are removed */
Alex Deymo486467e2017-12-19 19:04:07 +01002347 for(i = 0; i< data->numsocks; i++) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002348 int j;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002349 s = data->sockets[i];
Alex Deymo486467e2017-12-19 19:04:07 +01002350 for(j = 0; j<num; j++) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002351 if(s == socks[j]) {
2352 /* this is still supervised */
2353 s = CURL_SOCKET_BAD;
2354 break;
2355 }
2356 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01002357
Alex Deymod15eaac2016-06-28 14:49:26 -07002358 entry = sh_getentry(&multi->sockhash, s);
2359 if(entry) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002360 /* this socket has been removed. Tell the app to remove it */
Alex Deymod15eaac2016-06-28 14:49:26 -07002361 bool remove_sock_from_hash = TRUE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002362
Alex Deymod15eaac2016-06-28 14:49:26 -07002363 /* check if the socket to be removed serves a connection which has
2364 other easy-s in a pipeline. In this case the socket should not be
2365 removed. */
2366 struct connectdata *easy_conn = data->easy_conn;
2367 if(easy_conn) {
Elliott Hughes82be86d2017-09-20 17:00:17 -07002368 if(easy_conn->recv_pipe.size > 1) {
Alex Deymod15eaac2016-06-28 14:49:26 -07002369 /* the handle should not be removed from the pipe yet */
2370 remove_sock_from_hash = FALSE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002371
Alex Deymod15eaac2016-06-28 14:49:26 -07002372 /* Update the sockhash entry to instead point to the next in line
2373 for the recv_pipe, or the first (in case this particular easy
2374 isn't already) */
2375 if(entry->easy == data) {
2376 if(Curl_recvpipe_head(data, easy_conn))
Elliott Hughes82be86d2017-09-20 17:00:17 -07002377 entry->easy = easy_conn->recv_pipe.head->next->ptr;
Alex Deymod15eaac2016-06-28 14:49:26 -07002378 else
Elliott Hughes82be86d2017-09-20 17:00:17 -07002379 entry->easy = easy_conn->recv_pipe.head->ptr;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002380 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01002381 }
Elliott Hughes82be86d2017-09-20 17:00:17 -07002382 if(easy_conn->send_pipe.size > 1) {
Alex Deymod15eaac2016-06-28 14:49:26 -07002383 /* the handle should not be removed from the pipe yet */
2384 remove_sock_from_hash = FALSE;
2385
2386 /* Update the sockhash entry to instead point to the next in line
2387 for the send_pipe, or the first (in case this particular easy
2388 isn't already) */
2389 if(entry->easy == data) {
2390 if(Curl_sendpipe_head(data, easy_conn))
Elliott Hughes82be86d2017-09-20 17:00:17 -07002391 entry->easy = easy_conn->send_pipe.head->next->ptr;
Alex Deymod15eaac2016-06-28 14:49:26 -07002392 else
Elliott Hughes82be86d2017-09-20 17:00:17 -07002393 entry->easy = easy_conn->send_pipe.head->ptr;
Alex Deymod15eaac2016-06-28 14:49:26 -07002394 }
2395 }
2396 /* Don't worry about overwriting recv_pipe head with send_pipe_head,
2397 when action will be asked on the socket (see multi_socket()), the
2398 head of the correct pipe will be taken according to the
2399 action. */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002400 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01002401
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002402 if(remove_sock_from_hash) {
2403 /* in this case 'entry' is always non-NULL */
2404 if(multi->socket_cb)
2405 multi->socket_cb(data,
2406 s,
2407 CURL_POLL_REMOVE,
2408 multi->socket_userp,
2409 entry->socketp);
2410 sh_delentry(&multi->sockhash, s);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002411 }
Alex Deymod15eaac2016-06-28 14:49:26 -07002412 } /* if sockhash entry existed */
2413 } /* for loop over numsocks */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002414
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002415 memcpy(data->sockets, socks, num*sizeof(curl_socket_t));
2416 data->numsocks = num;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002417}
2418
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002419/*
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002420 * Curl_multi_closed()
2421 *
2422 * Used by the connect code to tell the multi_socket code that one of the
2423 * sockets we were using is about to be closed. This function will then
2424 * remove it from the sockethash for this handle to make the multi_socket API
2425 * behave properly, especially for the case when libcurl will create another
2426 * socket again and it gets the same file descriptor number.
2427 */
2428
2429void Curl_multi_closed(struct connectdata *conn, curl_socket_t s)
2430{
2431 struct Curl_multi *multi = conn->data->multi;
2432 if(multi) {
2433 /* this is set if this connection is part of a handle that is added to
2434 a multi handle, and only then this is necessary */
Alex Deymod15eaac2016-06-28 14:49:26 -07002435 struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002436
2437 if(entry) {
2438 if(multi->socket_cb)
2439 multi->socket_cb(conn->data, s, CURL_POLL_REMOVE,
2440 multi->socket_userp,
2441 entry->socketp);
2442
2443 /* now remove it from the socket hash */
2444 sh_delentry(&multi->sockhash, s);
2445 }
2446 }
2447}
2448
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002449/*
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002450 * add_next_timeout()
2451 *
Alex Deymoe3149cc2016-10-05 11:18:42 -07002452 * Each Curl_easy has a list of timeouts. The add_next_timeout() is called
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002453 * when it has just been removed from the splay tree because the timeout has
2454 * expired. This function is then to advance in the list to pick the next
2455 * timeout to use (skip the already expired ones) and add this node back to
2456 * the splay tree again.
2457 *
2458 * The splay tree only has each sessionhandle as a single node and the nearest
2459 * timeout is used to sort it on.
2460 */
Elliott Hughes82be86d2017-09-20 17:00:17 -07002461static CURLMcode add_next_timeout(struct curltime now,
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002462 struct Curl_multi *multi,
Alex Deymoe3149cc2016-10-05 11:18:42 -07002463 struct Curl_easy *d)
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002464{
Elliott Hughes82be86d2017-09-20 17:00:17 -07002465 struct curltime *tv = &d->state.expiretime;
2466 struct curl_llist *list = &d->state.timeoutlist;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002467 struct curl_llist_element *e;
Elliott Hughes82be86d2017-09-20 17:00:17 -07002468 struct time_node *node = NULL;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002469
2470 /* move over the timeout list for this specific handle and remove all
2471 timeouts that are now passed tense and store the next pending
2472 timeout in *tv */
Alex Deymod15eaac2016-06-28 14:49:26 -07002473 for(e = list->head; e;) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002474 struct curl_llist_element *n = e->next;
Alex Deymo486467e2017-12-19 19:04:07 +01002475 timediff_t diff;
Elliott Hughes82be86d2017-09-20 17:00:17 -07002476 node = (struct time_node *)e->ptr;
Alex Deymo486467e2017-12-19 19:04:07 +01002477 diff = Curl_timediff(node->time, now);
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002478 if(diff <= 0)
2479 /* remove outdated entry */
2480 Curl_llist_remove(list, e, NULL);
2481 else
2482 /* the list is sorted so get out on the first mismatch */
2483 break;
2484 e = n;
2485 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002486 e = list->head;
2487 if(!e) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002488 /* clear the expire times within the handles that we remove from the
2489 splay tree */
2490 tv->tv_sec = 0;
2491 tv->tv_usec = 0;
2492 }
2493 else {
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002494 /* copy the first entry to 'tv' */
Elliott Hughes82be86d2017-09-20 17:00:17 -07002495 memcpy(tv, &node->time, sizeof(*tv));
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002496
Elliott Hughes82be86d2017-09-20 17:00:17 -07002497 /* Insert this node again into the splay. Keep the timer in the list in
2498 case we need to recompute future timers. */
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002499 multi->timetree = Curl_splayinsert(*tv, multi->timetree,
2500 &d->state.timenode);
2501 }
2502 return CURLM_OK;
2503}
2504
Kristian Monsen5ab50182010-05-14 18:53:44 +01002505static CURLMcode multi_socket(struct Curl_multi *multi,
2506 bool checkall,
2507 curl_socket_t s,
2508 int ev_bitmask,
2509 int *running_handles)
2510{
2511 CURLMcode result = CURLM_OK;
Alex Deymoe3149cc2016-10-05 11:18:42 -07002512 struct Curl_easy *data = NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002513 struct Curl_tree *t;
Alex Deymo486467e2017-12-19 19:04:07 +01002514 struct curltime now = Curl_now();
Kristian Monsen5ab50182010-05-14 18:53:44 +01002515
2516 if(checkall) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002517 /* *perform() deals with running_handles on its own */
2518 result = curl_multi_perform(multi, running_handles);
2519
2520 /* walk through each easy handle and do the socket state change magic
2521 and callbacks */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002522 if(result != CURLM_BAD_HANDLE) {
Alex Deymo486467e2017-12-19 19:04:07 +01002523 data = multi->easyp;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002524 while(data) {
2525 singlesocket(multi, data);
2526 data = data->next;
2527 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01002528 }
2529
2530 /* or should we fall-through and do the timer-based stuff? */
2531 return result;
2532 }
Elliott Hughes82be86d2017-09-20 17:00:17 -07002533 if(s != CURL_SOCKET_TIMEOUT) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002534
Alex Deymod15eaac2016-06-28 14:49:26 -07002535 struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002536
2537 if(!entry)
2538 /* Unmatched socket, we can't act on it but we ignore this fact. In
2539 real-world tests it has been proved that libevent can in fact give
2540 the application actions even though the socket was just previously
2541 asked to get removed, so thus we better survive stray socket actions
2542 and just move on. */
2543 ;
2544 else {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002545 SIGPIPE_VARIABLE(pipe_st);
2546
Kristian Monsen5ab50182010-05-14 18:53:44 +01002547 data = entry->easy;
2548
2549 if(data->magic != CURLEASY_MAGIC_NUMBER)
2550 /* bad bad bad bad bad bad bad */
2551 return CURLM_INTERNAL_ERROR;
2552
2553 /* If the pipeline is enabled, take the handle which is in the head of
2554 the pipeline. If we should write into the socket, take the send_pipe
2555 head. If we should read from the socket, take the recv_pipe head. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002556 if(data->easy_conn) {
2557 if((ev_bitmask & CURL_POLL_OUT) &&
Elliott Hughes82be86d2017-09-20 17:00:17 -07002558 data->easy_conn->send_pipe.head)
2559 data = data->easy_conn->send_pipe.head->ptr;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002560 else if((ev_bitmask & CURL_POLL_IN) &&
Elliott Hughes82be86d2017-09-20 17:00:17 -07002561 data->easy_conn->recv_pipe.head)
2562 data = data->easy_conn->recv_pipe.head->ptr;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002563 }
2564
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002565 if(data->easy_conn &&
2566 !(data->easy_conn->handler->flags & PROTOPT_DIRLOCK))
2567 /* set socket event bitmask if they're not locked */
2568 data->easy_conn->cselect_bits = ev_bitmask;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002569
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002570 sigpipe_ignore(data, &pipe_st);
2571 result = multi_runsingle(multi, now, data);
2572 sigpipe_restore(&pipe_st);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002573
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002574 if(data->easy_conn &&
2575 !(data->easy_conn->handler->flags & PROTOPT_DIRLOCK))
2576 /* clear the bitmask only if not locked */
2577 data->easy_conn->cselect_bits = 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002578
2579 if(CURLM_OK >= result)
2580 /* get the socket(s) and check if the state has been changed since
2581 last */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002582 singlesocket(multi, data);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002583
2584 /* Now we fall-through and do the timer-based stuff, since we don't want
2585 to force the user to have to deal with timeouts as long as at least
2586 one connection in fact has traffic. */
2587
2588 data = NULL; /* set data to NULL again to avoid calling
2589 multi_runsingle() in case there's no need to */
Alex Deymo486467e2017-12-19 19:04:07 +01002590 now = Curl_now(); /* get a newer time since the multi_runsingle() loop
2591 may have taken some time */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002592 }
2593 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002594 else {
2595 /* Asked to run due to time-out. Clear the 'lastcall' variable to force
2596 update_timer() to trigger a callback to the app again even if the same
2597 timeout is still the one to run after this call. That handles the case
2598 when the application asks libcurl to run the timeout prematurely. */
2599 memset(&multi->timer_lastcall, 0, sizeof(multi->timer_lastcall));
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002600 }
2601
Kristian Monsen5ab50182010-05-14 18:53:44 +01002602 /*
2603 * The loop following here will go on as long as there are expire-times left
2604 * to process in the splay and 'data' will be re-assigned for every expired
2605 * handle we deal with.
2606 */
2607 do {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002608 /* the first loop lap 'data' can be NULL */
2609 if(data) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002610 SIGPIPE_VARIABLE(pipe_st);
2611
2612 sigpipe_ignore(data, &pipe_st);
2613 result = multi_runsingle(multi, now, data);
2614 sigpipe_restore(&pipe_st);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002615
2616 if(CURLM_OK >= result)
2617 /* get the socket(s) and check if the state has been changed since
2618 last */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002619 singlesocket(multi, data);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002620 }
2621
2622 /* Check if there's one (more) expired timer to deal with! This function
2623 extracts a matching node if there is one */
2624
Kristian Monsen5ab50182010-05-14 18:53:44 +01002625 multi->timetree = Curl_splaygetbest(now, multi->timetree, &t);
2626 if(t) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002627 data = t->payload; /* assign this for next loop */
2628 (void)add_next_timeout(now, multi, t->payload);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002629 }
2630
2631 } while(t);
2632
2633 *running_handles = multi->num_alive;
2634 return result;
2635}
2636
2637#undef curl_multi_setopt
Alex Deymoe3149cc2016-10-05 11:18:42 -07002638CURLMcode curl_multi_setopt(struct Curl_multi *multi,
Kristian Monsen5ab50182010-05-14 18:53:44 +01002639 CURLMoption option, ...)
2640{
Kristian Monsen5ab50182010-05-14 18:53:44 +01002641 CURLMcode res = CURLM_OK;
2642 va_list param;
2643
2644 if(!GOOD_MULTI_HANDLE(multi))
2645 return CURLM_BAD_HANDLE;
2646
Elliott Hughescac39802018-04-27 16:19:43 -07002647 if(multi->in_callback)
2648 return CURLM_RECURSIVE_API_CALL;
2649
Kristian Monsen5ab50182010-05-14 18:53:44 +01002650 va_start(param, option);
2651
2652 switch(option) {
2653 case CURLMOPT_SOCKETFUNCTION:
2654 multi->socket_cb = va_arg(param, curl_socket_callback);
2655 break;
2656 case CURLMOPT_SOCKETDATA:
2657 multi->socket_userp = va_arg(param, void *);
2658 break;
Alex Deymod15eaac2016-06-28 14:49:26 -07002659 case CURLMOPT_PUSHFUNCTION:
2660 multi->push_cb = va_arg(param, curl_push_callback);
2661 break;
2662 case CURLMOPT_PUSHDATA:
2663 multi->push_userp = va_arg(param, void *);
2664 break;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002665 case CURLMOPT_PIPELINING:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002666 multi->pipelining = va_arg(param, long);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002667 break;
2668 case CURLMOPT_TIMERFUNCTION:
2669 multi->timer_cb = va_arg(param, curl_multi_timer_callback);
2670 break;
2671 case CURLMOPT_TIMERDATA:
2672 multi->timer_userp = va_arg(param, void *);
2673 break;
2674 case CURLMOPT_MAXCONNECTS:
2675 multi->maxconnects = va_arg(param, long);
2676 break;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002677 case CURLMOPT_MAX_HOST_CONNECTIONS:
2678 multi->max_host_connections = va_arg(param, long);
2679 break;
2680 case CURLMOPT_MAX_PIPELINE_LENGTH:
2681 multi->max_pipeline_length = va_arg(param, long);
2682 break;
2683 case CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE:
2684 multi->content_length_penalty_size = va_arg(param, long);
2685 break;
2686 case CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE:
2687 multi->chunk_length_penalty_size = va_arg(param, long);
2688 break;
2689 case CURLMOPT_PIPELINING_SITE_BL:
2690 res = Curl_pipeline_set_site_blacklist(va_arg(param, char **),
2691 &multi->pipelining_site_bl);
2692 break;
2693 case CURLMOPT_PIPELINING_SERVER_BL:
2694 res = Curl_pipeline_set_server_blacklist(va_arg(param, char **),
2695 &multi->pipelining_server_bl);
2696 break;
2697 case CURLMOPT_MAX_TOTAL_CONNECTIONS:
2698 multi->max_total_connections = va_arg(param, long);
2699 break;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002700 default:
2701 res = CURLM_UNKNOWN_OPTION;
2702 break;
2703 }
2704 va_end(param);
2705 return res;
2706}
2707
2708/* we define curl_multi_socket() in the public multi.h header */
2709#undef curl_multi_socket
2710
Alex Deymoe3149cc2016-10-05 11:18:42 -07002711CURLMcode curl_multi_socket(struct Curl_multi *multi, curl_socket_t s,
Kristian Monsen5ab50182010-05-14 18:53:44 +01002712 int *running_handles)
2713{
Elliott Hughescac39802018-04-27 16:19:43 -07002714 CURLMcode result;
2715 if(multi->in_callback)
2716 return CURLM_RECURSIVE_API_CALL;
2717 result = multi_socket(multi, FALSE, s, 0, running_handles);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002718 if(CURLM_OK >= result)
Alex Deymoe3149cc2016-10-05 11:18:42 -07002719 update_timer(multi);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002720 return result;
2721}
2722
Alex Deymoe3149cc2016-10-05 11:18:42 -07002723CURLMcode curl_multi_socket_action(struct Curl_multi *multi, curl_socket_t s,
Kristian Monsen5ab50182010-05-14 18:53:44 +01002724 int ev_bitmask, int *running_handles)
2725{
Elliott Hughescac39802018-04-27 16:19:43 -07002726 CURLMcode result;
2727 if(multi->in_callback)
2728 return CURLM_RECURSIVE_API_CALL;
2729 result = multi_socket(multi, FALSE, s, ev_bitmask, running_handles);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002730 if(CURLM_OK >= result)
Alex Deymoe3149cc2016-10-05 11:18:42 -07002731 update_timer(multi);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002732 return result;
2733}
2734
Alex Deymoe3149cc2016-10-05 11:18:42 -07002735CURLMcode curl_multi_socket_all(struct Curl_multi *multi, int *running_handles)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002736
2737{
Elliott Hughescac39802018-04-27 16:19:43 -07002738 CURLMcode result;
2739 if(multi->in_callback)
2740 return CURLM_RECURSIVE_API_CALL;
2741 result = multi_socket(multi, TRUE, CURL_SOCKET_BAD, 0, running_handles);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002742 if(CURLM_OK >= result)
Alex Deymoe3149cc2016-10-05 11:18:42 -07002743 update_timer(multi);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002744 return result;
2745}
2746
2747static CURLMcode multi_timeout(struct Curl_multi *multi,
2748 long *timeout_ms)
2749{
Elliott Hughes82be86d2017-09-20 17:00:17 -07002750 static struct curltime tv_zero = {0, 0};
Kristian Monsen5ab50182010-05-14 18:53:44 +01002751
2752 if(multi->timetree) {
2753 /* we have a tree of expire times */
Alex Deymo486467e2017-12-19 19:04:07 +01002754 struct curltime now = Curl_now();
Kristian Monsen5ab50182010-05-14 18:53:44 +01002755
2756 /* splay the lowest to the bottom */
2757 multi->timetree = Curl_splay(tv_zero, multi->timetree);
2758
2759 if(Curl_splaycomparekeys(multi->timetree->key, now) > 0) {
2760 /* some time left before expiration */
Alex Deymo486467e2017-12-19 19:04:07 +01002761 timediff_t diff = Curl_timediff(multi->timetree->key, now);
2762 if(diff <= 0)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002763 /*
2764 * Since we only provide millisecond resolution on the returned value
2765 * and the diff might be less than one millisecond here, we don't
2766 * return zero as that may cause short bursts of busyloops on fast
2767 * processors while the diff is still present but less than one
2768 * millisecond! instead we return 1 until the time is ripe.
2769 */
Alex Deymo486467e2017-12-19 19:04:07 +01002770 *timeout_ms = 1;
2771 else
2772 /* this should be safe even on 64 bit archs, as we don't use that
2773 overly long timeouts */
2774 *timeout_ms = (long)diff;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002775 }
2776 else
2777 /* 0 means immediately */
2778 *timeout_ms = 0;
2779 }
2780 else
2781 *timeout_ms = -1;
2782
2783 return CURLM_OK;
2784}
2785
Alex Deymoe3149cc2016-10-05 11:18:42 -07002786CURLMcode curl_multi_timeout(struct Curl_multi *multi,
Kristian Monsen5ab50182010-05-14 18:53:44 +01002787 long *timeout_ms)
2788{
Kristian Monsen5ab50182010-05-14 18:53:44 +01002789 /* First, make some basic checks that the CURLM handle is a good handle */
2790 if(!GOOD_MULTI_HANDLE(multi))
2791 return CURLM_BAD_HANDLE;
2792
Elliott Hughescac39802018-04-27 16:19:43 -07002793 if(multi->in_callback)
2794 return CURLM_RECURSIVE_API_CALL;
2795
Kristian Monsen5ab50182010-05-14 18:53:44 +01002796 return multi_timeout(multi, timeout_ms);
2797}
2798
2799/*
2800 * Tell the application it should update its timers, if it subscribes to the
2801 * update timer callback.
2802 */
2803static int update_timer(struct Curl_multi *multi)
2804{
2805 long timeout_ms;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002806
Kristian Monsen5ab50182010-05-14 18:53:44 +01002807 if(!multi->timer_cb)
2808 return 0;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002809 if(multi_timeout(multi, &timeout_ms)) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01002810 return -1;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002811 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002812 if(timeout_ms < 0) {
Alex Deymo486467e2017-12-19 19:04:07 +01002813 static const struct curltime none = {0, 0};
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002814 if(Curl_splaycomparekeys(none, multi->timer_lastcall)) {
2815 multi->timer_lastcall = none;
2816 /* there's no timeout now but there was one previously, tell the app to
2817 disable it */
Alex Deymoe3149cc2016-10-05 11:18:42 -07002818 return multi->timer_cb(multi, -1, multi->timer_userp);
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002819 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01002820 return 0;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002821 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01002822
2823 /* When multi_timeout() is done, multi->timetree points to the node with the
2824 * timeout we got the (relative) time-out time for. We can thus easily check
2825 * if this is the same (fixed) time as we got in a previous call and then
2826 * avoid calling the callback again. */
2827 if(Curl_splaycomparekeys(multi->timetree->key, multi->timer_lastcall) == 0)
2828 return 0;
2829
2830 multi->timer_lastcall = multi->timetree->key;
2831
Alex Deymoe3149cc2016-10-05 11:18:42 -07002832 return multi->timer_cb(multi, timeout_ms, multi->timer_userp);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002833}
2834
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002835/*
Elliott Hughes82be86d2017-09-20 17:00:17 -07002836 * multi_deltimeout()
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002837 *
Elliott Hughes82be86d2017-09-20 17:00:17 -07002838 * Remove a given timestamp from the list of timeouts.
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002839 */
Elliott Hughes82be86d2017-09-20 17:00:17 -07002840static void
2841multi_deltimeout(struct Curl_easy *data, expire_id eid)
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002842{
Elliott Hughes82be86d2017-09-20 17:00:17 -07002843 struct curl_llist_element *e;
2844 struct curl_llist *timeoutlist = &data->state.timeoutlist;
2845 /* find and remove the specific node from the list */
2846 for(e = timeoutlist->head; e; e = e->next) {
2847 struct time_node *n = (struct time_node *)e->ptr;
2848 if(n->eid == eid) {
2849 Curl_llist_remove(timeoutlist, e, NULL);
2850 return;
2851 }
2852 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002853}
2854
2855/*
2856 * multi_addtimeout()
2857 *
2858 * Add a timestamp to the list of timeouts. Keep the list sorted so that head
2859 * of list is always the timeout nearest in time.
2860 *
2861 */
2862static CURLMcode
Elliott Hughes82be86d2017-09-20 17:00:17 -07002863multi_addtimeout(struct Curl_easy *data,
2864 struct curltime *stamp,
2865 expire_id eid)
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002866{
2867 struct curl_llist_element *e;
Elliott Hughes82be86d2017-09-20 17:00:17 -07002868 struct time_node *node;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002869 struct curl_llist_element *prev = NULL;
Elliott Hughes82be86d2017-09-20 17:00:17 -07002870 size_t n;
2871 struct curl_llist *timeoutlist = &data->state.timeoutlist;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002872
Elliott Hughes82be86d2017-09-20 17:00:17 -07002873 node = &data->state.expires[eid];
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002874
Elliott Hughes82be86d2017-09-20 17:00:17 -07002875 /* copy the timestamp and id */
2876 memcpy(&node->time, stamp, sizeof(*stamp));
2877 node->eid = eid; /* also marks it as in use */
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002878
Elliott Hughes82be86d2017-09-20 17:00:17 -07002879 n = Curl_llist_count(timeoutlist);
2880 if(n) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002881 /* find the correct spot in the list */
2882 for(e = timeoutlist->head; e; e = e->next) {
Elliott Hughes82be86d2017-09-20 17:00:17 -07002883 struct time_node *check = (struct time_node *)e->ptr;
Alex Deymo486467e2017-12-19 19:04:07 +01002884 timediff_t diff = Curl_timediff(check->time, node->time);
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002885 if(diff > 0)
2886 break;
2887 prev = e;
2888 }
2889
2890 }
2891 /* else
2892 this is the first timeout on the list */
2893
Elliott Hughes82be86d2017-09-20 17:00:17 -07002894 Curl_llist_insert_next(timeoutlist, prev, node, &node->list);
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002895 return CURLM_OK;
2896}
2897
2898/*
2899 * Curl_expire()
2900 *
2901 * given a number of milliseconds from now to use to set the 'act before
2902 * this'-time for the transfer, to be extracted by curl_multi_timeout()
2903 *
Elliott Hughescee03382017-06-23 12:17:18 -07002904 * The timeout will be added to a queue of timeouts if it defines a moment in
2905 * time that is later than the current head of queue.
Elliott Hughes82be86d2017-09-20 17:00:17 -07002906 *
2907 * Expire replaces a former timeout using the same id if already set.
Elliott Hughescee03382017-06-23 12:17:18 -07002908 */
Elliott Hughes82be86d2017-09-20 17:00:17 -07002909void Curl_expire(struct Curl_easy *data, time_t milli, expire_id id)
Kristian Monsen5ab50182010-05-14 18:53:44 +01002910{
2911 struct Curl_multi *multi = data->multi;
Elliott Hughes82be86d2017-09-20 17:00:17 -07002912 struct curltime *nowp = &data->state.expiretime;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002913 int rc;
Elliott Hughes82be86d2017-09-20 17:00:17 -07002914 struct curltime set;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002915
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002916 /* this is only interesting while there is still an associated multi struct
2917 remaining! */
Kristian Monsen5ab50182010-05-14 18:53:44 +01002918 if(!multi)
2919 return;
2920
Elliott Hughes82be86d2017-09-20 17:00:17 -07002921 DEBUGASSERT(id < EXPIRE_LAST);
2922
Alex Deymo486467e2017-12-19 19:04:07 +01002923 set = Curl_now();
Elliott Hughes82be86d2017-09-20 17:00:17 -07002924 set.tv_sec += milli/1000;
2925 set.tv_usec += (unsigned int)(milli%1000)*1000;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002926
Elliott Hughescee03382017-06-23 12:17:18 -07002927 if(set.tv_usec >= 1000000) {
2928 set.tv_sec++;
2929 set.tv_usec -= 1000000;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002930 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01002931
Elliott Hughes82be86d2017-09-20 17:00:17 -07002932 /* Remove any timer with the same id just in case. */
2933 multi_deltimeout(data, id);
2934
2935 /* Add it to the timer list. It must stay in the list until it has expired
2936 in case we need to recompute the minimum timer later. */
2937 multi_addtimeout(data, &set, id);
2938
Elliott Hughescee03382017-06-23 12:17:18 -07002939 if(nowp->tv_sec || nowp->tv_usec) {
2940 /* This means that the struct is added as a node in the splay tree.
2941 Compare if the new time is earlier, and only remove-old/add-new if it
2942 is. */
Alex Deymo486467e2017-12-19 19:04:07 +01002943 timediff_t diff = Curl_timediff(set, *nowp);
Elliott Hughes82be86d2017-09-20 17:00:17 -07002944
Elliott Hughescee03382017-06-23 12:17:18 -07002945 if(diff > 0) {
Elliott Hughes82be86d2017-09-20 17:00:17 -07002946 /* The current splay tree entry is sooner than this new expiry time.
2947 We don't need to update our splay tree entry. */
Elliott Hughescee03382017-06-23 12:17:18 -07002948 return;
Kristian Monsen5ab50182010-05-14 18:53:44 +01002949 }
2950
Elliott Hughescee03382017-06-23 12:17:18 -07002951 /* Since this is an updated time, we must remove the previous entry from
2952 the splay tree first and then re-add the new value */
2953 rc = Curl_splayremovebyaddr(multi->timetree,
2954 &data->state.timenode,
2955 &multi->timetree);
2956 if(rc)
2957 infof(data, "Internal error removing splay node = %d\n", rc);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002958 }
Elliott Hughescee03382017-06-23 12:17:18 -07002959
Elliott Hughes82be86d2017-09-20 17:00:17 -07002960 /* Indicate that we are in the splay tree and insert the new timer expiry
2961 value since it is our local minimum. */
Elliott Hughescee03382017-06-23 12:17:18 -07002962 *nowp = set;
2963 data->state.timenode.payload = data;
2964 multi->timetree = Curl_splayinsert(*nowp, multi->timetree,
2965 &data->state.timenode);
Kristian Monsen5ab50182010-05-14 18:53:44 +01002966}
2967
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002968/*
Elliott Hughes82be86d2017-09-20 17:00:17 -07002969 * Curl_expire_done()
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002970 *
Elliott Hughes82be86d2017-09-20 17:00:17 -07002971 * Removes the expire timer. Marks it as done.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002972 *
2973 */
Elliott Hughes82be86d2017-09-20 17:00:17 -07002974void Curl_expire_done(struct Curl_easy *data, expire_id id)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002975{
Elliott Hughes82be86d2017-09-20 17:00:17 -07002976 /* remove the timer, if there */
2977 multi_deltimeout(data, id);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07002978}
2979
Elliott Hughescee03382017-06-23 12:17:18 -07002980/*
2981 * Curl_expire_clear()
2982 *
2983 * Clear ALL timeout values for this handle.
2984 */
2985void Curl_expire_clear(struct Curl_easy *data)
2986{
2987 struct Curl_multi *multi = data->multi;
Elliott Hughes82be86d2017-09-20 17:00:17 -07002988 struct curltime *nowp = &data->state.expiretime;
Elliott Hughescee03382017-06-23 12:17:18 -07002989 int rc;
2990
2991 /* this is only interesting while there is still an associated multi struct
2992 remaining! */
2993 if(!multi)
2994 return;
2995
2996 if(nowp->tv_sec || nowp->tv_usec) {
2997 /* Since this is an cleared time, we must remove the previous entry from
2998 the splay tree */
Elliott Hughes82be86d2017-09-20 17:00:17 -07002999 struct curl_llist *list = &data->state.timeoutlist;
Elliott Hughescee03382017-06-23 12:17:18 -07003000
3001 rc = Curl_splayremovebyaddr(multi->timetree,
3002 &data->state.timenode,
3003 &multi->timetree);
3004 if(rc)
3005 infof(data, "Internal error clearing splay node = %d\n", rc);
3006
3007 /* flush the timeout list too */
Elliott Hughes82be86d2017-09-20 17:00:17 -07003008 while(list->size > 0) {
Elliott Hughescee03382017-06-23 12:17:18 -07003009 Curl_llist_remove(list, list->tail, NULL);
Elliott Hughes82be86d2017-09-20 17:00:17 -07003010 }
Elliott Hughescee03382017-06-23 12:17:18 -07003011
3012#ifdef DEBUGBUILD
3013 infof(data, "Expire cleared\n");
3014#endif
3015 nowp->tv_sec = 0;
3016 nowp->tv_usec = 0;
3017 }
3018}
3019
3020
3021
3022
Alex Deymoe3149cc2016-10-05 11:18:42 -07003023CURLMcode curl_multi_assign(struct Curl_multi *multi, curl_socket_t s,
3024 void *hashp)
Kristian Monsen5ab50182010-05-14 18:53:44 +01003025{
3026 struct Curl_sh_entry *there = NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003027
Elliott Hughescac39802018-04-27 16:19:43 -07003028 if(multi->in_callback)
3029 return CURLM_RECURSIVE_API_CALL;
3030
Alex Deymod15eaac2016-06-28 14:49:26 -07003031 there = sh_getentry(&multi->sockhash, s);
Kristian Monsen5ab50182010-05-14 18:53:44 +01003032
3033 if(!there)
3034 return CURLM_BAD_SOCKET;
3035
3036 there->socketp = hashp;
3037
3038 return CURLM_OK;
3039}
3040
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003041size_t Curl_multi_max_host_connections(struct Curl_multi *multi)
Kristian Monsen5ab50182010-05-14 18:53:44 +01003042{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003043 return multi ? multi->max_host_connections : 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003044}
3045
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003046size_t Curl_multi_max_total_connections(struct Curl_multi *multi)
Kristian Monsen5ab50182010-05-14 18:53:44 +01003047{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003048 return multi ? multi->max_total_connections : 0;
3049}
Kristian Monsen5ab50182010-05-14 18:53:44 +01003050
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003051curl_off_t Curl_multi_content_length_penalty_size(struct Curl_multi *multi)
3052{
3053 return multi ? multi->content_length_penalty_size : 0;
3054}
Kristian Monsen5ab50182010-05-14 18:53:44 +01003055
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003056curl_off_t Curl_multi_chunk_length_penalty_size(struct Curl_multi *multi)
3057{
3058 return multi ? multi->chunk_length_penalty_size : 0;
3059}
3060
3061struct curl_llist *Curl_multi_pipelining_site_bl(struct Curl_multi *multi)
3062{
Elliott Hughes82be86d2017-09-20 17:00:17 -07003063 return &multi->pipelining_site_bl;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003064}
3065
3066struct curl_llist *Curl_multi_pipelining_server_bl(struct Curl_multi *multi)
3067{
Elliott Hughes82be86d2017-09-20 17:00:17 -07003068 return &multi->pipelining_server_bl;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003069}
3070
3071void Curl_multi_process_pending_handles(struct Curl_multi *multi)
3072{
Elliott Hughes82be86d2017-09-20 17:00:17 -07003073 struct curl_llist_element *e = multi->pending.head;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003074
3075 while(e) {
Alex Deymoe3149cc2016-10-05 11:18:42 -07003076 struct Curl_easy *data = e->ptr;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003077 struct curl_llist_element *next = e->next;
3078
3079 if(data->mstate == CURLM_STATE_CONNECT_PEND) {
3080 multistate(data, CURLM_STATE_CONNECT);
3081
3082 /* Remove this node from the list */
Elliott Hughes82be86d2017-09-20 17:00:17 -07003083 Curl_llist_remove(&multi->pending, e, NULL);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003084
3085 /* Make sure that the handle will be processed soonish. */
Elliott Hughes82be86d2017-09-20 17:00:17 -07003086 Curl_expire(data, 0, EXPIRE_RUN_NOW);
Kristian Monsen5ab50182010-05-14 18:53:44 +01003087 }
3088
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003089 e = next; /* operate on next handle */
Kristian Monsen5ab50182010-05-14 18:53:44 +01003090 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01003091}
3092
Elliott Hughescac39802018-04-27 16:19:43 -07003093void Curl_set_in_callback(struct Curl_easy *easy, bool value)
3094{
3095 if(easy->multi_easy)
3096 easy->multi_easy->in_callback = value;
3097 else if(easy->multi)
3098 easy->multi->in_callback = value;
3099}
3100
3101bool Curl_is_in_callback(struct Curl_easy *easy)
3102{
3103 return ((easy->multi && easy->multi->in_callback) ||
3104 (easy->multi_easy && easy->multi_easy->in_callback));
3105}
3106
Kristian Monsen5ab50182010-05-14 18:53:44 +01003107#ifdef DEBUGBUILD
Alex Deymoe3149cc2016-10-05 11:18:42 -07003108void Curl_multi_dump(struct Curl_multi *multi)
Kristian Monsen5ab50182010-05-14 18:53:44 +01003109{
Alex Deymoe3149cc2016-10-05 11:18:42 -07003110 struct Curl_easy *data;
Kristian Monsen5ab50182010-05-14 18:53:44 +01003111 int i;
3112 fprintf(stderr, "* Multi status: %d handles, %d alive\n",
3113 multi->num_easy, multi->num_alive);
Alex Deymo486467e2017-12-19 19:04:07 +01003114 for(data = multi->easyp; data; data = data->next) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003115 if(data->mstate < CURLM_STATE_COMPLETED) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01003116 /* only display handles that are not completed */
3117 fprintf(stderr, "handle %p, state %s, %d sockets\n",
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003118 (void *)data,
3119 statename[data->mstate], data->numsocks);
Alex Deymo486467e2017-12-19 19:04:07 +01003120 for(i = 0; i < data->numsocks; i++) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003121 curl_socket_t s = data->sockets[i];
Alex Deymod15eaac2016-06-28 14:49:26 -07003122 struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s);
Kristian Monsen5ab50182010-05-14 18:53:44 +01003123
3124 fprintf(stderr, "%d ", (int)s);
3125 if(!entry) {
3126 fprintf(stderr, "INTERNAL CONFUSION\n");
3127 continue;
3128 }
3129 fprintf(stderr, "[%s %s] ",
3130 entry->action&CURL_POLL_IN?"RECVING":"",
3131 entry->action&CURL_POLL_OUT?"SENDING":"");
3132 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07003133 if(data->numsocks)
Kristian Monsen5ab50182010-05-14 18:53:44 +01003134 fprintf(stderr, "\n");
3135 }
3136 }
3137}
3138#endif