Coverage Report

Created: 2026-10-22 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/multi.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#include "urldata.h"
27
#include "transfer.h"
28
#include "url.h"
29
#include "cfilters.h"
30
#include "connect.h"
31
#include "progress.h"
32
#include "curl_share.h"
33
#include "psl.h"
34
#include "multiif.h"
35
#include "multi_ev.h"
36
#include "sendf.h"
37
#include "curl_trc.h"
38
#include "http.h"
39
#include "select.h"
40
#include "curlx/wait.h"
41
#include "conncache.h"
42
#include "multihandle.h"
43
#include "sigpipe.h"
44
#include "vtls/vtls.h"
45
#include "vtls/vtls_scache.h"
46
#include "http_proxy.h"
47
#include "http2.h"
48
#include "socketpair.h"
49
#include "bufref.h"
50
#include "curlx/strparse.h"
51
52
/* initial multi->xfers table size for a full multi */
53
22.5k
#define CURL_XFER_TABLE_SIZE 128
54
55
/* CURL_SOCKET_HASH_TABLE_SIZE should be a prime number. Increasing it from 97
56
   to 911 takes on a 32-bit machine 4 x 804 = 3211 more bytes. Still, every
57
   curl handle takes 6K memory, therefore this 3K are not significant. */
58
#ifndef CURL_SOCKET_HASH_TABLE_SIZE
59
11.2k
#define CURL_SOCKET_HASH_TABLE_SIZE 911
60
#endif
61
62
#ifndef CURL_CONNECTION_HASH_SIZE
63
11.2k
#define CURL_CONNECTION_HASH_SIZE 97
64
#endif
65
66
#ifndef CURL_DNS_HASH_SIZE
67
11.2k
#define CURL_DNS_HASH_SIZE 71
68
#endif
69
70
#ifndef CURL_TLS_SESSION_SIZE
71
11.2k
#define CURL_TLS_SESSION_SIZE 25
72
#endif
73
74
static void move_pending_to_connect(struct Curl_multi *multi,
75
                                    struct Curl_easy *data);
76
static CURLMcode add_next_timeout(const struct curltime *pnow,
77
                                  struct Curl_multi *multi,
78
                                  struct Curl_easy *data);
79
static void multi_timeout(struct Curl_multi *multi,
80
                          timediff_t *pexire_offset_us,
81
                          int *timeout_ms);
82
static void multi_schedule_pending(struct Curl_multi *multi);
83
static void multi_xfer_bufs_free(struct Curl_multi *multi);
84
#ifdef DEBUGBUILD
85
static void multi_xfer_tbl_dump(struct Curl_multi *multi);
86
#endif
87
/* Get the # of transfers current in process/pending. */
88
static uint32_t multi_xfers_running(struct Curl_multi *multi);
89
90
/* function pointer called once when entering a state */
91
typedef void (*mstate_enter_func)(struct Curl_easy *data,
92
                                  CURLMstate from_state);
93
94
static void mstate_enter_connect(struct Curl_easy *data,
95
                                 CURLMstate from_state)
96
8.04k
{
97
8.04k
  (void)from_state;
98
8.04k
  Curl_init_CONNECT(data);
99
8.04k
}
100
101
static void mstate_enter_did(struct Curl_easy *data,
102
                             CURLMstate from_state)
103
135
{
104
135
  (void)from_state;
105
135
  data->req.chunk = FALSE;
106
135
  Curl_pgrsTime(data, TIMER_PRETRANSFER);
107
135
  if(!CURL_REQ_WANT_SEND(data))
108
135
    Curl_pgrsTime(data, TIMER_POSTRANSFER);
109
135
}
110
111
static void mstate_enter_done(struct Curl_easy *data,
112
                              CURLMstate from_state)
113
141
{
114
141
  (void)from_state;
115
141
  CURLM_NTFY(data, CURLMNOTIFY_EASY_DONE);
116
141
}
117
118
static void mstate_enter_completed(struct Curl_easy *data,
119
                                   CURLMstate from_state)
120
10.8k
{
121
  /* we sometimes directly jump to COMPLETED, trigger things
122
   * we then missed. */
123
10.8k
  if(from_state < MSTATE_DID) {
124
10.6k
    Curl_pgrsTime(data, TIMER_PRETRANSFER);
125
10.6k
    Curl_pgrsTime(data, TIMER_POSTRANSFER);
126
10.6k
    Curl_pgrsTime(data, TIMER_STARTTRANSFER);
127
10.6k
  }
128
  /* multi_done() should have run and detached any connection already */
129
10.8k
  DEBUGASSERT(!data->conn);
130
10.8k
  Curl_pgrsCompleted(data);
131
10.8k
  if(from_state < MSTATE_DONE)
132
10.6k
    CURLM_NTFY(data, CURLMNOTIFY_EASY_DONE);
133
  /* changing to COMPLETED means it is in process and needs to go */
134
10.8k
  DEBUGASSERT(Curl_uint32_bset_contains(&data->multi->process, data->mid));
135
10.8k
  Curl_uint32_bset_remove(&data->multi->process, data->mid);
136
10.8k
  Curl_uint32_bset_remove(&data->multi->pending, data->mid); /* to be sure */
137
138
10.8k
  if(Curl_uint32_bset_empty(&data->multi->process)) {
139
    /* free the transfer buffer when we have no more active transfers */
140
0
    multi_xfer_bufs_free(data->multi);
141
0
  }
142
  /* Important: reset the conn pointer so that we do not point to memory
143
     that could be freed anytime */
144
10.8k
  Curl_detach_connection(data);
145
10.8k
  Curl_expire_clear_all(data); /* stop all timers */
146
10.8k
}
147
148
/* always use this function to change state, to make debugging easier */
149
static void mstate(struct Curl_easy *data, CURLMstate state
150
#ifdef DEBUGBUILD
151
                   , int lineno
152
#endif
153
)
154
59.7k
{
155
59.7k
  CURLMstate oldstate = data->mstate;
156
59.7k
  static const mstate_enter_func state_enter[MSTATE_LAST] = {
157
59.7k
    NULL,                      /* INIT */
158
59.7k
    NULL,                      /* PENDING */
159
59.7k
    NULL,                      /* SETUP */
160
59.7k
    mstate_enter_connect,      /* CONNECT */
161
59.7k
    NULL,                      /* CONNECTING */
162
59.7k
    NULL,                      /* PROTOCONNECT */
163
59.7k
    NULL,                      /* PROTOCONNECTING */
164
59.7k
    NULL,                      /* DO */
165
59.7k
    NULL,                      /* DOING */
166
59.7k
    NULL,                      /* DOING_MORE */
167
59.7k
    mstate_enter_did,          /* DID */
168
59.7k
    NULL,                      /* PERFORMING */
169
59.7k
    NULL,                      /* RATELIMITING */
170
59.7k
    mstate_enter_done,         /* DONE */
171
59.7k
    mstate_enter_completed,    /* COMPLETED */
172
    NULL                       /* MSGSENT */
173
59.7k
  };
174
175
59.7k
  if(oldstate == state)
176
    /* do not bother when the new state is the same as the old state */
177
11.2k
    return;
178
179
48.5k
#ifdef DEBUGBUILD
180
48.5k
  NOVERBOSE((void)lineno);
181
48.5k
  CURL_TRC_M(data, "-> [%s] (line %d)", CURL_MSTATE_NAME(state), lineno);
182
#else
183
  CURL_TRC_M(data, "-> [%s]", CURL_MSTATE_NAME(state));
184
#endif
185
186
  /* really switching state */
187
48.5k
  data->mstate = state;
188
48.5k
  if(state_enter[state])
189
19.1k
    state_enter[state](data, oldstate);
190
48.5k
}
191
192
#ifndef DEBUGBUILD
193
#define multistate(x, y) mstate(x, y)
194
#else
195
59.7k
#define multistate(x, y) mstate(x, y, __LINE__)
196
#endif
197
198
/* multi->proto_hash destructor. Should never be called as elements
199
 * MUST be added with their own destructor */
200
static void ph_freeentry(void *p)
201
0
{
202
0
  (void)p;
203
  /* Always FALSE. Cannot use a 0 assert here since compilers
204
   * are not in agreement if they then want a NORETURN attribute or
205
   * not. *sigh* */
206
0
  DEBUGASSERT(!p);
207
0
}
208
209
/*
210
 * multi_addmsg()
211
 *
212
 * Called when a transfer is completed. Marks its message as unread.
213
 */
214
static void multi_addmsg(struct Curl_multi *multi, struct Curl_easy *data)
215
10.8k
{
216
10.8k
  if(Curl_uint32_bset_empty(&multi->msgsent))
217
10.8k
    CURLM_NTFY(multi->admin, CURLMNOTIFY_INFO_READ);
218
10.8k
  Curl_uint32_bset_add(&multi->msgsent, data->mid);
219
10.8k
}
220
221
static void multi_timeouts_init(struct Curl_easy *data);
222
223
struct Curl_multi *Curl_multi_handle(uint32_t xfer_table_size,
224
                                     size_t ev_hashsize,  /* event hash */
225
                                     size_t chashsize, /* connection hash */
226
                                     size_t dnssize,   /* dns hash */
227
                                     size_t sesssize)  /* TLS session cache */
228
11.2k
{
229
11.2k
  struct curltime created;
230
11.2k
  struct Curl_multi *multi = curlx_calloc(1, sizeof(struct Curl_multi));
231
232
11.2k
  if(!multi)
233
0
    return NULL;
234
235
11.2k
  multi->magic = CURLMULTI_MAGIC_NUMBER;
236
237
  /* Initialisation order is important here!
238
   * easy_init() does a lazy check on curl_global_init() which sets
239
   * up platform specific things we need. For example calling curlx_pnow()
240
   * before this is not safe. */
241
11.2k
  multi->admin = curl_easy_init();
242
11.2k
  if(!multi->admin) {
243
0
    curlx_free(multi);
244
0
    return NULL;
245
0
  }
246
11.2k
  multi->admin->multi = multi;
247
11.2k
  multi->admin->state.internal = TRUE;
248
249
  /* Now we can use curlx_* things safely */
250
11.2k
  curlx_pnow(&created);
251
11.2k
  Curl_timeouts_init(&multi->timeouts, &created);
252
11.2k
  multi_timeouts_init(multi->admin);
253
254
11.2k
  Curl_dnscache_init(&multi->dnscache, dnssize);
255
11.2k
  Curl_mntfy_init(multi);
256
11.2k
  Curl_multi_ev_init(multi, ev_hashsize);
257
11.2k
  Curl_uint32_tbl_init(&multi->xfers);
258
11.2k
  Curl_uint32_bset_init(&multi->process);
259
11.2k
  Curl_uint32_bset_init(&multi->dirty);
260
11.2k
  Curl_uint32_bset_init(&multi->pending);
261
11.2k
  Curl_uint32_bset_init(&multi->msgsent);
262
11.2k
  Curl_hash_init(&multi->proto_hash, 23, CURL_HASH_TYPE_BYTES, ph_freeentry);
263
11.2k
  Curl_cshutdn_init(&multi->cshutdn);
264
265
11.2k
  multi->multiplexing = TRUE;
266
11.2k
  multi->max_concurrent_streams = 100;
267
11.2k
#ifdef ENABLE_WAKEUP
268
11.2k
  multi->wakeup_pair[0] = CURL_SOCKET_BAD;
269
11.2k
  multi->wakeup_pair[1] = CURL_SOCKET_BAD;
270
11.2k
#endif
271
11.2k
#ifdef ENABLE_INTERNAL_WAKEUP
272
11.2k
  multi->wakeup_internal[0] = CURL_SOCKET_BAD;
273
11.2k
  multi->wakeup_internal[1] = CURL_SOCKET_BAD;
274
11.2k
#endif
275
276
11.2k
  if(Curl_uint32_bset_resize(&multi->process, xfer_table_size) ||
277
11.2k
     Curl_uint32_bset_resize(&multi->pending, xfer_table_size) ||
278
11.2k
     Curl_uint32_bset_resize(&multi->dirty, xfer_table_size) ||
279
11.2k
     Curl_uint32_bset_resize(&multi->msgsent, xfer_table_size) ||
280
11.2k
     Curl_uint32_tbl_resize(&multi->xfers, xfer_table_size))
281
0
    goto error;
282
283
11.2k
#ifdef DEBUGBUILD
284
11.2k
  {
285
11.2k
    const char *p;
286
11.2k
    if(getenv("CURL_DEBUG"))
287
0
      multi->admin->set.verbose = TRUE;
288
11.2k
    p = getenv("CURL_DBG_MAX_TOTAL_CONNECTIONS");
289
11.2k
    if(p) {
290
0
      curl_off_t l;
291
0
      if(!curlx_str_number(&p, &l, UINT32_MAX)) {
292
0
        multi->max_total_connections = (uint32_t)l;
293
0
      }
294
0
    }
295
11.2k
  }
296
11.2k
#endif
297
11.2k
  Curl_uint32_tbl_add(&multi->xfers, multi->admin, &multi->admin->mid);
298
11.2k
  Curl_uint32_bset_add(&multi->process, multi->admin->mid);
299
300
11.2k
  Curl_cpool_init(&multi->cpool, NULL, chashsize);
301
302
11.2k
#ifdef USE_SSL
303
11.2k
  if(Curl_ssl_scache_create(sesssize, 2, &multi->ssl_scache))
304
0
    goto error;
305
#else
306
  (void)sesssize;
307
#endif
308
309
#ifdef USE_WINSOCK
310
  multi->wsa_event = WSACreateEvent();
311
  if(multi->wsa_event == WSA_INVALID_EVENT)
312
    goto error;
313
#endif
314
11.2k
#ifdef ENABLE_WAKEUP
315
  /* When enabled, rely on this to work. We ignore this in previous
316
   * versions, but that seems an unnecessary complication. */
317
11.2k
  if(Curl_wakeup_init(multi->wakeup_pair, TRUE) < 0)
318
0
    goto error;
319
11.2k
#endif
320
11.2k
#ifdef ENABLE_INTERNAL_WAKEUP
321
11.2k
  if(Curl_wakeup_init(multi->wakeup_internal, TRUE) < 0)
322
0
    goto error;
323
11.2k
#endif
324
325
11.2k
  if(Curl_probeipv6(multi))
326
0
    goto error;
327
328
11.2k
#ifdef USE_RESOLV_THREADED
329
11.2k
  if(xfer_table_size < CURL_XFER_TABLE_SIZE) { /* easy multi */
330
0
    if(Curl_async_thrdd_multi_init(multi, 0, 2, 10))
331
0
      goto error;
332
0
  }
333
11.2k
  else { /* real multi handle */
334
11.2k
    if(Curl_async_thrdd_multi_init(multi, 0, 20, 2000))
335
0
      goto error;
336
11.2k
  }
337
11.2k
#endif
338
339
11.2k
  return multi;
340
341
0
error:
342
343
0
#ifdef USE_RESOLV_THREADED
344
0
  Curl_async_thrdd_multi_destroy(multi, TRUE);
345
0
#endif
346
0
  Curl_multi_ev_cleanup(multi);
347
0
  Curl_hash_destroy(&multi->proto_hash);
348
0
  Curl_dnscache_destroy(&multi->dnscache);
349
0
  Curl_cpool_destroy(&multi->cpool, multi->admin);
350
0
  Curl_cshutdn_destroy(&multi->cshutdn, multi->admin);
351
0
#ifdef USE_SSL
352
0
  Curl_ssl_scache_destroy(multi->ssl_scache);
353
0
#endif
354
0
  if(multi->admin) {
355
0
    Curl_multi_ev_xfer_done(multi, multi->admin);
356
0
    multi->admin->multi = NULL;
357
0
    Curl_close(&multi->admin);
358
0
  }
359
0
  Curl_mntfy_cleanup(multi);
360
361
0
  Curl_uint32_bset_destroy(&multi->process);
362
0
  Curl_uint32_bset_destroy(&multi->dirty);
363
0
  Curl_uint32_bset_destroy(&multi->pending);
364
0
  Curl_uint32_bset_destroy(&multi->msgsent);
365
0
  Curl_uint32_tbl_destroy(&multi->xfers);
366
0
#ifdef ENABLE_WAKEUP
367
0
  Curl_wakeup_destroy(multi->wakeup_pair);
368
0
#endif
369
0
#ifdef ENABLE_INTERNAL_WAKEUP
370
0
  Curl_wakeup_destroy(multi->wakeup_internal);
371
0
#endif
372
373
0
  curlx_free(multi);
374
0
  return NULL;
375
11.2k
}
376
377
CURLM *curl_multi_init(void)
378
11.2k
{
379
11.2k
  return Curl_multi_handle(CURL_XFER_TABLE_SIZE,
380
11.2k
                           CURL_SOCKET_HASH_TABLE_SIZE,
381
11.2k
                           CURL_CONNECTION_HASH_SIZE,
382
11.2k
                           CURL_DNS_HASH_SIZE,
383
11.2k
                           CURL_TLS_SESSION_SIZE);
384
11.2k
}
385
386
#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
387
static void multi_warn_debug(struct Curl_multi *multi, struct Curl_easy *data)
388
37.6k
{
389
37.6k
  if(!multi->warned) {
390
11.2k
    infof(data, "!!! WARNING !!!");
391
11.2k
    infof(data, "This is a debug build of libcurl, "
392
11.2k
                "do not use in production.");
393
11.2k
    multi->warned = TRUE;
394
11.2k
  }
395
37.6k
}
396
#else
397
#define multi_warn_debug(x, y) Curl_nop_stmt
398
#endif
399
400
bool Curl_is_connecting(struct Curl_easy *data)
401
44.8k
{
402
44.8k
  return data->mstate < MSTATE_DO;
403
44.8k
}
404
405
static CURLMcode multi_assess_wakeup(struct Curl_multi *multi)
406
44.6k
{
407
44.6k
#ifdef ENABLE_INTERNAL_WAKEUP
408
44.6k
  if(multi->socket_cb)
409
0
    return Curl_multi_ev_assess_xfer(multi, multi->admin);
410
#else
411
  (void)multi;
412
#endif
413
44.6k
  return CURLM_OK;
414
44.6k
}
415
416
static CURLMcode multi_xfers_add(struct Curl_multi *multi,
417
                                 struct Curl_easy *data)
418
11.2k
{
419
11.2k
  uint32_t capacity = Curl_uint32_tbl_capacity(&multi->xfers);
420
11.2k
  uint32_t new_size = 0;
421
  /* Prepare to make this into a CURLMOPT_MAX_TRANSFERS, because some
422
   * applications may want to prevent a run-away of their memory use. */
423
  /* UINT_MAX is our "invalid" id, do not let the table grow up to that. */
424
11.2k
  const uint32_t max_capacity = UINT_MAX - 1;
425
426
11.2k
  if(capacity < max_capacity) {
427
    /* We want `multi->xfers` to have "sufficient" free rows, so that we do
428
     * not have to reuse the `mid` from a removed easy right away.
429
     * Check if an 8th of the capacity is still free */
430
11.2k
    uint32_t used = Curl_uint32_tbl_count(&multi->xfers);
431
11.2k
    uint32_t unused = capacity - used;
432
11.2k
    uint32_t min_unused = CURLMAX(capacity >> 3, 4);
433
11.2k
    if(unused < min_unused) {
434
      /* Grow by 50% of current capacity, in range of [128, 2048],
435
       * which means the table grows max by 16kb on 64-bit arch. */
436
0
      uint32_t growth = CURLMIN(CURLMAX(capacity >> 1, 128), 2048);
437
      /* Make sure the uint arithmetic here works on the corner
438
       * cases where we are close to max_capacity or UINT_MAX */
439
0
      if((max_capacity - growth) <= capacity)
440
0
        new_size = max_capacity;
441
0
      else
442
0
        new_size = capacity + growth;
443
0
    }
444
11.2k
  }
445
446
11.2k
  if(new_size > capacity) {
447
    /* Grow the bitsets first. Should one fail, we do not need
448
     * to downsize the already resized ones. The sets continue
449
     * to work properly when larger than the table, but not
450
     * the other way around. */
451
0
    CURL_TRC_M(data, "increasing xfer table size to %u", new_size);
452
0
    if(Curl_uint32_bset_resize(&multi->process, new_size) ||
453
0
       Curl_uint32_bset_resize(&multi->dirty, new_size) ||
454
0
       Curl_uint32_bset_resize(&multi->pending, new_size) ||
455
0
       Curl_uint32_bset_resize(&multi->msgsent, new_size) ||
456
0
       Curl_uint32_tbl_resize(&multi->xfers, new_size))
457
0
      return CURLM_OUT_OF_MEMORY;
458
0
  }
459
460
  /* Insert the easy into the table now */
461
11.2k
  if(!Curl_uint32_tbl_add(&multi->xfers, data, &data->mid)) {
462
    /* MUST only happen when table is full */
463
0
    DEBUGASSERT(Curl_uint32_tbl_capacity(&multi->xfers) <=
464
0
                Curl_uint32_tbl_count(&multi->xfers));
465
0
    return CURLM_OUT_OF_MEMORY;
466
0
  }
467
11.2k
  return CURLM_OK;
468
11.2k
}
469
470
CURLMcode Curl_multi_add_handle(struct Curl_multi *multi,
471
                                struct Curl_easy *data)
472
11.2k
{
473
11.2k
  CURLMcode mresult;
474
475
  /* Prevent users from adding same easy handle more than once and prevent
476
     adding to more than one multi stack */
477
11.2k
  if(data->multi)
478
0
    return CURLM_ADDED_ALREADY;
479
480
11.2k
  if(multi->dead) {
481
    /* a "dead" handle cannot get added transfers while any existing easy
482
       handles are still alive - but if there are none alive anymore, it is
483
       fine to start over and unmark the "deadness" of this handle.
484
       This means only the admin handle MUST be present. */
485
0
    if((Curl_uint32_tbl_count(&multi->xfers) != 1) ||
486
0
       !Curl_uint32_tbl_contains(&multi->xfers, 0))
487
0
      return CURLM_ABORTED_BY_CALLBACK;
488
0
    multi->dead = FALSE;
489
0
    Curl_uint32_bset_clear(&multi->process);
490
0
    Curl_uint32_bset_clear(&multi->dirty);
491
0
    Curl_uint32_bset_clear(&multi->pending);
492
0
    Curl_uint32_bset_clear(&multi->msgsent);
493
0
  }
494
495
11.2k
  if(data->multi_easy) {
496
    /* if this easy handle was previously used for curl_easy_perform(), there
497
       is a private multi handle here that we can kill */
498
0
    curl_multi_cleanup(data->multi_easy);
499
0
    data->multi_easy = NULL;
500
0
  }
501
502
  /* Insert the easy into the multi->xfers table, assigning it a `mid`. */
503
11.2k
  if(multi_xfers_add(multi, data))
504
0
    return CURLM_OUT_OF_MEMORY;
505
506
  /* Initialize timeouts for this handle */
507
11.2k
  multi_timeouts_init(data);
508
509
  /*
510
   * No failure allowed in this function beyond this point. No modification
511
   * of easy nor multi handle allowed before this except for potential
512
   * multi's connection pool growing which will not be undone in this
513
   * function no matter what.
514
   */
515
11.2k
  if(data->set.errorbuffer)
516
0
    data->set.errorbuffer[0] = 0;
517
518
11.2k
  data->state.os_errno = 0;
519
520
  /* make the Curl_easy refer back to this multi handle - before
521
     Curl_expire() is called. */
522
11.2k
  data->multi = multi;
523
524
  /* set the easy handle */
525
11.2k
  multistate(data, MSTATE_INIT);
526
  /* not yet passed INIT state */
527
11.2k
  data->state.really_alive = FALSE;
528
529
#ifdef USE_LIBPSL
530
  /* Do the same for PSL. */
531
  if(data->share && (data->share->specifier & (1 << CURL_LOCK_DATA_PSL)))
532
    data->psl = &data->share->psl;
533
  else
534
    data->psl = &multi->psl;
535
#endif
536
537
  /* add the easy handle to the process set */
538
11.2k
  Curl_uint32_bset_add(&multi->process, data->mid);
539
11.2k
  ++multi->xfers_alive;
540
11.2k
  ++multi->xfers_total_ever;
541
542
11.2k
  Curl_cpool_xfer_init(data);
543
11.2k
  multi_warn_debug(multi, data);
544
545
  /* Make sure the new handle will run */
546
11.2k
  Curl_multi_mark_dirty(data);
547
548
  /* Necessary in event based processing, where dirty handles trigger
549
   * a timeout callback invocation. */
550
11.2k
  mresult = Curl_update_timer(multi);
551
11.2k
  if(mresult) {
552
    /* undo the additions above */
553
0
    Curl_uint32_bset_remove(&multi->process, data->mid);
554
0
    Curl_uint32_bset_remove(&multi->dirty, data->mid);
555
0
    --multi->xfers_alive;
556
0
    data->multi = NULL; /* not anymore */
557
0
    Curl_uint32_tbl_remove(&multi->xfers, data->mid);
558
0
    data->mid = UINT32_MAX;
559
0
    return mresult;
560
0
  }
561
562
  /* The admin handle only ever has default timeouts set. To improve the
563
     state somewhat we clone the timeouts from each added handle so that the
564
     admin handle always has the same timeouts as the most recently added
565
     easy handle. */
566
11.2k
  multi->admin->set.timeout = data->set.timeout;
567
11.2k
  multi->admin->set.server_response_timeout =
568
11.2k
    data->set.server_response_timeout;
569
11.2k
  multi->admin->set.no_signal = data->set.no_signal;
570
571
11.2k
  CURL_TRC_M(data, "added to multi, mid=%u, running=%u, total=%u",
572
11.2k
             data->mid, multi_xfers_running(multi),
573
11.2k
             Curl_uint32_tbl_count(&multi->xfers));
574
11.2k
  return CURLM_OK;
575
11.2k
}
576
577
CURLMcode curl_multi_add_handle(CURLM *m, CURL *curl)
578
11.2k
{
579
11.2k
  struct Curl_mapi_guard guard;
580
11.2k
  CURLMcode mresult;
581
582
11.2k
  if(CURL_MAPI_ENTER(&guard, m, multi_add_handle, &mresult)) {
583
11.2k
    struct Curl_easy *data = curl;
584
    /* Verify that we got a somewhat good easy handle too */
585
11.2k
    if(!GOOD_EASY_HANDLE(data) || data->state.internal)
586
0
      mresult = CURLM_BAD_EASY_HANDLE;
587
11.2k
    else
588
11.2k
      mresult = Curl_multi_add_handle(m, data);
589
11.2k
  }
590
11.2k
  CURL_MAPI_LEAVE(&guard);
591
11.2k
  return mresult;
592
11.2k
}
593
594
struct multi_done_ctx {
595
  BIT(premature);
596
};
597
598
static bool multi_conn_should_close(struct connectdata *conn,
599
                                    struct Curl_easy *data,
600
                                    bool premature)
601
4.28k
{
602
  /* if conn->bits.close is TRUE, it means that the connection should be
603
     closed in spite of everything else. */
604
4.28k
  if(conn->bits.close)
605
382
    return TRUE;
606
607
  /* if data->set.reuse_forbid is TRUE, it means the libcurl client has
608
     forced us to close this connection. This is ignored for requests taking
609
     place in a NTLM/NEGOTIATE authentication handshake. */
610
3.90k
  if(data->set.reuse_forbid
611
#ifdef USE_NTLM
612
     && !(conn->http_ntlm_state == NTLMSTATE_TYPE2 ||
613
          conn->proxy_ntlm_state == NTLMSTATE_TYPE2)
614
#endif
615
#ifdef USE_SPNEGO
616
     && !(conn->http_negotiate_state == CURL_NEGO_AUTH_RECV ||
617
          conn->proxy_negotiate_state == CURL_NEGO_AUTH_RECV)
618
#endif
619
3.90k
    )
620
8
    return TRUE;
621
622
  /* Unless this connection is for a "connect-only" transfer, it
623
   * needs to be closed if the protocol handler does not support reuse. */
624
3.89k
  if(!data->set.connect_only && conn->scheme &&
625
3.87k
     !(conn->scheme->flags & PROTOPT_CONN_REUSE))
626
3.87k
    return TRUE;
627
628
  /* if premature is TRUE, it means this connection was said to be DONE before
629
     the entire request operation is complete and thus we cannot know in what
630
     state it is for reusing, so we are forced to close it. In a perfect world
631
     we can add code that keep track of if we really must close it here or not,
632
     but currently we have no such detail knowledge. */
633
18
  if(premature && !Curl_conn_is_multiplex(conn, FIRSTSOCKET))
634
5
    return TRUE;
635
636
13
  return FALSE;
637
18
}
638
639
static cpool_do_result multi_done_locked(struct Curl_easy *data,
640
                                         struct connectdata *conn,
641
                                         void *userdata,
642
                                         const struct curltime *pnow)
643
4.28k
{
644
4.28k
  struct multi_done_ctx *mdctx = userdata;
645
646
4.28k
  (void)pnow;
647
4.28k
  CURL_TRC_M(data, "multi_done_locked, in use=%u", conn->attached_xfers);
648
4.28k
  if(CONN_INUSE(conn)) {
649
    /* Stop if still used. */
650
0
    CURL_TRC_M(data, "Connection still in use %u, no more multi_done now!",
651
0
               conn->attached_xfers);
652
0
    return CPOOL_DO_KEEP;
653
0
  }
654
655
4.28k
  if(multi_conn_should_close(conn, data, (bool)mdctx->premature)) {
656
4.27k
    CURL_TRC_M(data, "multi_done, terminating conn #%" FMT_OFF_T " to %s:%u, "
657
4.27k
               "forbid=%d, close=%d, premature=%d, conn_multiplex=%d",
658
4.27k
               conn->connection_id, conn->origin->user_hostname,
659
4.27k
               conn->origin->port,
660
4.27k
               data->set.reuse_forbid, conn->bits.close, mdctx->premature,
661
4.27k
               Curl_conn_is_multiplex(conn, FIRSTSOCKET));
662
4.27k
    connclose(conn);
663
4.27k
    return mdctx->premature ? CPOOL_DO_TERMINATE : CPOOL_DO_CLOSE;
664
4.27k
  }
665
13
  else if(!Curl_conn_get_max_concurrent(data, conn, FIRSTSOCKET)) {
666
0
    CURL_TRC_M(data, "multi_done, conn #%" FMT_OFF_T " to %s:%u was shutdown"
667
0
               " by server, not reusing", conn->connection_id,
668
0
               conn->origin->user_hostname, conn->origin->port);
669
0
    connclose(conn);
670
0
    return mdctx->premature ? CPOOL_DO_TERMINATE : CPOOL_DO_CLOSE;
671
0
  }
672
13
  else {
673
13
    return CPOOL_DO_IDLE;
674
13
  }
675
4.28k
}
676
677
static CURLcode multi_done(struct Curl_easy *data,
678
                           CURLcode status,  /* an error if this is called
679
                                                after an error was detected */
680
                           bool premature)
681
4.28k
{
682
4.28k
  CURLcode result;
683
4.28k
  struct connectdata *conn = data->conn;
684
4.28k
  const struct curltime *pnow = Curl_pgrs_now(data);
685
686
4.28k
  CURL_TRC_M(data, "multi_done: status: %d prem: %d done: %d",
687
4.28k
             (int)status, (int)premature, data->state.done);
688
689
4.28k
  if(data->state.done) {
690
    /* Stop if multi_done() has already been called */
691
0
    return CURLE_OK;
692
0
  }
693
4.28k
  data->state.done = TRUE; /* called now! */
694
695
  /* Shut down any ongoing async resolver operation. */
696
4.28k
  Curl_resolv_shutdown_all(data);
697
698
  /* Cleanup possible redirect junk */
699
4.28k
  curlx_safefree(data->req.newurl);
700
4.28k
  curlx_safefree(data->req.location);
701
702
4.28k
  switch(status) {
703
0
  case CURLE_ABORTED_BY_CALLBACK:
704
0
  case CURLE_READ_ERROR:
705
0
  case CURLE_WRITE_ERROR:
706
    /* When we are aborted due to a callback return code it has to be counted
707
       as premature as there is trouble ahead if we do not. We have many
708
       callbacks and protocols work differently, we could potentially do this
709
       more fine-grained in the future. */
710
0
    premature = TRUE;
711
0
    FALLTHROUGH();
712
4.28k
  default:
713
4.28k
    break;
714
4.28k
  }
715
716
  /* this calls the protocol-specific function pointer previously set */
717
4.28k
  if(conn && conn->scheme->run->done && (data->mstate >= MSTATE_PROTOCONNECT))
718
2.05k
    result = conn->scheme->run->done(data, status, premature);
719
2.23k
  else
720
2.23k
    result = status;
721
722
4.28k
  if(data->mstate > MSTATE_CONNECTING &&
723
2.05k
     (result != CURLE_ABORTED_BY_CALLBACK)) {
724
    /* avoid this if
725
     * - the transfer has not connected
726
     * - we already aborted by callback to avoid this calling another callback
727
     */
728
2.05k
    int rc = Curl_pgrsDone(data, pnow);
729
2.05k
    if(!result && rc)
730
0
      result = CURLE_ABORTED_BY_CALLBACK;
731
2.05k
  }
732
733
  /* Make sure that transfer client writes are really done now. */
734
4.28k
  result = Curl_1st_fatal(result, Curl_xfer_write_done(data, premature));
735
736
4.28k
  if(!result)
737
2.14k
    result = Curl_req_done(&data->req, data, premature);
738
739
4.28k
  if(conn) {
740
    /* Detach connection and decide what to do with it. */
741
4.28k
    struct multi_done_ctx mdctx;
742
743
    /* Inform connection filters that this transfer is done and they
744
     * need to give up any references to `data`. */
745
4.28k
    Curl_conn_ev_data_done(data, premature);
746
4.28k
    Curl_detach_connection(data);
747
4.28k
    Curl_dnscache_prune(data, pnow);
748
749
4.28k
    memset(&mdctx, 0, sizeof(mdctx));
750
4.28k
    mdctx.premature = premature;
751
4.28k
    Curl_cpool_return(data, conn, multi_done_locked, &mdctx, pnow);
752
753
4.28k
    if(data->multi)
754
4.28k
      multi_schedule_pending(data->multi);
755
4.28k
  }
756
757
  /* flush the netrc cache */
758
4.28k
  Curl_netrc_cleanup(&data->state.netrc);
759
4.28k
  return result;
760
4.28k
}
761
762
CURLMcode Curl_multi_remove_handle(struct Curl_multi *multi,
763
                                   struct Curl_easy *data)
764
11.2k
{
765
11.2k
  CURLMcode mresult;
766
11.2k
  bool premature;
767
11.2k
  uint32_t mid;
768
769
  /* Prevent users from trying to remove same easy handle more than once */
770
11.2k
  if(!data->multi)
771
0
    return CURLM_OK; /* it is already removed so let's say it is fine! */
772
773
  /* Prevent users from trying to remove an easy handle from the wrong multi */
774
11.2k
  if(data->multi != multi)
775
0
    return CURLM_BAD_EASY_HANDLE;
776
777
11.2k
  if(data->mid == UINT32_MAX) {
778
0
    DEBUGASSERT(0);
779
0
    return CURLM_INTERNAL_ERROR;
780
0
  }
781
11.2k
  if(Curl_uint32_tbl_get(&multi->xfers, data->mid) != data) {
782
0
    DEBUGASSERT(0);
783
0
    return CURLM_INTERNAL_ERROR;
784
0
  }
785
786
11.2k
  premature = (data->mstate < MSTATE_COMPLETED);
787
788
11.2k
  if(data->conn) {
789
    /* If the 'state' is not INIT or COMPLETED, we might need to do something
790
       nice to put the easy_handle in a good known state when this returns. */
791
473
    if(premature && (data->mstate > MSTATE_DO))
792
381
      streamclose(data->conn);
793
794
    /* multi_done() clears the association between the easy handle and the
795
       connection.
796
       Note that this ignores the return code because there is
797
       nothing really useful to do with it anyway! */
798
473
    (void)multi_done(data, data->result, premature);
799
473
  }
800
801
  /* The timer must be shut down before data->multi is set to NULL, else
802
     data's splaynode would remain in the splay tree after curl_easy_cleanup is
803
     called. Do it after multi_done() in case that sets another time! */
804
11.2k
  Curl_expire_clear_all(data);
805
806
  /* In MSGSENT, it was deducted from `multi->xfers_alive` already. */
807
11.2k
  if(data->mstate != MSTATE_MSGSENT)
808
473
    --multi->xfers_alive;
809
810
11.2k
  if(data->state.really_alive) {
811
473
    data->state.really_alive = FALSE;
812
473
    --multi->xfers_really_alive;
813
473
    if(!multi->xfers_really_alive)
814
473
      (void)multi_assess_wakeup(multi);
815
473
  }
816
817
11.2k
  Curl_wildcard_dtor(&data->wildcard);
818
819
11.2k
  data->mstate = MSTATE_COMPLETED;
820
821
  /* Remove the association between the connection and the handle */
822
11.2k
  Curl_detach_connection(data);
823
824
  /* Tell event handling that this transfer is definitely going away */
825
11.2k
  Curl_multi_ev_xfer_done(multi, data);
826
827
11.2k
  if(data->set.connect_only) {
828
23
    if(data->multi_easy) {
829
0
      if(data->state.last_conn_id != -1) {
830
        /* Mark any connect-only connection for closure */
831
0
        struct connectdata *conn;
832
0
        (void)Curl_getconnectinfo(data, &conn);
833
0
        if(conn && conn->bits.connect_only)
834
0
          connclose(conn);
835
0
      }
836
0
    }
837
23
    else {
838
      /* This removes a handle that was part the multi interface that used
839
         CONNECT_ONLY, that connection is now left alive but since this handle
840
         has bits.close set nothing can use that connection anymore and it is
841
         forbidden from reuse. This easy handle cannot find the connection
842
         anymore once removed from the multi handle
843
844
         Better close the connection here, at once. */
845
23
      struct connectdata *conn;
846
23
      (void)Curl_getconnectinfo(data, &conn);
847
23
      if(conn)
848
13
        Curl_conn_close(data, conn, TRUE);
849
23
    }
850
23
  }
851
852
#ifdef USE_LIBPSL
853
  /* Remove the PSL association. */
854
  if(data->psl == &multi->psl)
855
    data->psl = NULL;
856
#endif
857
858
  /* clear the association to this multi handle */
859
11.2k
  mid = data->mid;
860
11.2k
  DEBUGASSERT(Curl_uint32_tbl_contains(&multi->xfers, mid));
861
11.2k
  Curl_uint32_tbl_remove(&multi->xfers, mid);
862
11.2k
  Curl_uint32_bset_remove(&multi->process, mid);
863
11.2k
  Curl_uint32_bset_remove(&multi->dirty, mid);
864
11.2k
  Curl_uint32_bset_remove(&multi->pending, mid);
865
11.2k
  Curl_uint32_bset_remove(&multi->msgsent, mid);
866
11.2k
  data->multi = NULL;
867
11.2k
  data->mid = UINT32_MAX;
868
11.2k
  data->master_mid = UINT32_MAX;
869
870
  /* A pending transfer *might* be able to run now. */
871
11.2k
  multi_schedule_pending(multi);
872
11.2k
  mresult = Curl_update_timer(multi);
873
11.2k
  if(mresult)
874
0
    return mresult;
875
876
11.2k
  mresult = multi_assess_wakeup(multi);
877
11.2k
  if(mresult) {
878
0
    failf(data, "error enabling wakeup listening: %d", mresult);
879
0
    return mresult;
880
0
  }
881
882
11.2k
  CURL_TRC_M(data, "removed from multi, mid=%u, running=%u, total=%u",
883
11.2k
             mid, multi_xfers_running(multi),
884
11.2k
             Curl_uint32_tbl_count(&multi->xfers));
885
11.2k
  return CURLM_OK;
886
11.2k
}
887
888
CURLMcode curl_multi_remove_handle(CURLM *m, CURL *curl)
889
11.2k
{
890
11.2k
  struct Curl_mapi_guard guard;
891
11.2k
  CURLMcode mresult;
892
893
11.2k
  if(CURL_MAPI_ENTER(&guard, m, multi_remove_handle, &mresult)) {
894
11.2k
    struct Curl_easy *data = curl;
895
11.2k
    if(!GOOD_EASY_HANDLE(data) || data->state.internal)
896
0
      mresult = CURLM_BAD_EASY_HANDLE;
897
11.2k
    else
898
11.2k
      mresult = Curl_multi_remove_handle(m, data);
899
11.2k
  }
900
11.2k
  CURL_MAPI_LEAVE(&guard);
901
11.2k
  return mresult;
902
11.2k
}
903
904
/* Return TRUE if the application asked for multiplexing */
905
bool Curl_multiplex_wanted(const struct Curl_multi *multi)
906
0
{
907
0
  return multi && multi->multiplexing;
908
0
}
909
910
/*
911
 * Curl_detach_connection() removes the given transfer from the connection.
912
 *
913
 * This is the only function that should clear data->conn. This will
914
 * occasionally be called with the data->conn pointer already cleared.
915
 */
916
void Curl_detach_connection(struct Curl_easy *data)
917
56.5k
{
918
56.5k
  struct connectdata *conn = data->conn;
919
56.5k
  if(conn) {
920
    /* this should never happen, prevent underflow */
921
10.2k
    DEBUGASSERT(conn->attached_xfers);
922
10.2k
    if(conn->attached_xfers) {
923
10.2k
      conn->attached_xfers--;
924
10.2k
      if(!conn->attached_xfers)
925
10.2k
        conn->attached_multi = NULL;
926
10.2k
    }
927
10.2k
  }
928
56.5k
  data->conn = NULL;
929
56.5k
}
930
931
/*
932
 * Curl_attach_connection() attaches this transfer to this connection.
933
 *
934
 * This is the only function that should assign data->conn.
935
 * `matched == TRUE` means the transfer's properties match this
936
 * connection and it is not a temporary attach for maintenance.
937
 */
938
void Curl_attach_connection(struct Curl_easy *data,
939
                            struct connectdata *conn,
940
                            bool matched)
941
10.2k
{
942
10.2k
  DEBUGASSERT(data);
943
10.2k
  DEBUGASSERT(!data->conn);
944
10.2k
  DEBUGASSERT(conn);
945
10.2k
  DEBUGASSERT(conn->attached_xfers < UINT32_MAX);
946
10.2k
  data->conn = conn;
947
10.2k
  if(matched) {
948
4.28k
    data->state.last_conn_id = conn->connection_id;
949
4.28k
    data->state.last_cpid = conn->cpid;
950
4.28k
  }
951
5.94k
  else
952
10.2k
    DEBUGASSERT(!data->mid); /* admin handle */
953
10.2k
  conn->attached_xfers++;
954
  /* all attached transfers must be from the same multi */
955
10.2k
  if(!conn->attached_multi)
956
10.2k
    conn->attached_multi = data->multi;
957
10.2k
  DEBUGASSERT(conn->attached_multi == data->multi);
958
959
10.2k
  if(conn->scheme && conn->scheme->run->attach)
960
0
    conn->scheme->run->attach(data, conn);
961
10.2k
}
962
963
/* adjust pollset for rate limits/pauses */
964
static CURLcode multi_adjust_pollset(struct Curl_easy *data,
965
                                     struct easy_pollset *ps)
966
2.40k
{
967
2.40k
  CURLcode result = CURLE_OK;
968
969
2.40k
  if(ps->n) {
970
2.40k
    bool send_blocked, recv_blocked;
971
972
2.40k
    recv_blocked = (Curl_rlimit_avail(&data->progress.dl.rlimit, NULL) <= 0);
973
2.40k
    send_blocked = (Curl_rlimit_avail(&data->progress.ul.rlimit, NULL) <= 0);
974
2.40k
    if(send_blocked || recv_blocked) {
975
37
      int i;
976
111
      for(i = 0; i <= SECONDARYSOCKET; ++i) {
977
74
        curl_socket_t sock = data->conn->sock[i];
978
74
        if(sock == CURL_SOCKET_BAD)
979
37
          continue;
980
37
        if(recv_blocked && Curl_pollset_want_recv(data, ps, sock)) {
981
37
          result = Curl_pollset_remove_in(data, ps, sock);
982
37
          if(result)
983
0
            break;
984
37
        }
985
37
        if(send_blocked && Curl_pollset_want_send(data, ps, sock)) {
986
0
          result = Curl_pollset_remove_out(data, ps, sock);
987
0
          if(result)
988
0
            break;
989
0
        }
990
37
      }
991
37
    }
992
993
    /* Not blocked and wanting to receive. If there is data pending
994
     * in the connection filters, make transfer run again. */
995
2.40k
    if(!recv_blocked &&
996
2.36k
       ((Curl_pollset_want_recv(data, ps, data->conn->sock[FIRSTSOCKET]) &&
997
1.65k
         Curl_conn_data_pending(data, FIRSTSOCKET)) ||
998
2.36k
        (Curl_pollset_want_recv(data, ps, data->conn->sock[SECONDARYSOCKET]) &&
999
0
         Curl_conn_data_pending(data, SECONDARYSOCKET)))) {
1000
0
      CURL_TRC_M(data, "pollset[] has POLLIN, but there is still "
1001
0
                 "buffered input -> mark as dirty");
1002
0
      Curl_multi_mark_dirty(data);
1003
0
    }
1004
2.40k
  }
1005
2.40k
  return result;
1006
2.40k
}
1007
1008
static CURLcode mstate_connecting_pollset(struct Curl_easy *data,
1009
                                          struct easy_pollset *ps)
1010
643
{
1011
643
  struct connectdata *conn = data->conn;
1012
643
  curl_socket_t sockfd;
1013
643
  CURLcode result = CURLE_OK;
1014
1015
643
  if(Curl_xfer_recv_is_paused(data))
1016
0
    return CURLE_OK;
1017
  /* If a socket is set, receiving is default. If the socket
1018
   * has not been determined yet (eyeballing), always ask the
1019
   * connection filters for what to monitor. */
1020
643
  sockfd = Curl_conn_get_first_socket(data);
1021
643
  if(sockfd != CURL_SOCKET_BAD) {
1022
643
    result = Curl_pollset_change(data, ps, sockfd, CURL_POLL_IN, 0);
1023
643
    if(!result)
1024
643
      result = multi_adjust_pollset(data, ps);
1025
643
  }
1026
643
  if(!result)
1027
643
    result = Curl_conn_adjust_pollset(data, conn, ps);
1028
643
  return result;
1029
643
}
1030
1031
static CURLcode mstate_protocol_pollset(struct Curl_easy *data,
1032
                                        struct easy_pollset *ps)
1033
0
{
1034
0
  struct connectdata *conn = data->conn;
1035
0
  CURLcode result = CURLE_OK;
1036
1037
0
  if(conn->scheme->run->proto_pollset)
1038
0
    result = conn->scheme->run->proto_pollset(data, ps);
1039
0
  else {
1040
0
    curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
1041
0
    if(sockfd != CURL_SOCKET_BAD) {
1042
      /* Default is to wait to something from the server */
1043
0
      result = Curl_pollset_change(data, ps, sockfd, CURL_POLL_IN, 0);
1044
0
    }
1045
0
  }
1046
0
  if(!result)
1047
0
    result = multi_adjust_pollset(data, ps);
1048
0
  if(!result)
1049
0
    result = Curl_conn_adjust_pollset(data, conn, ps);
1050
0
  return result;
1051
0
}
1052
1053
static CURLcode mstate_do_pollset(struct Curl_easy *data,
1054
                                  struct easy_pollset *ps)
1055
1.75k
{
1056
1.75k
  struct connectdata *conn = data->conn;
1057
1.75k
  CURLcode result = CURLE_OK;
1058
1059
1.75k
  if(conn->scheme->run->doing_pollset)
1060
1.75k
    result = conn->scheme->run->doing_pollset(data, ps);
1061
0
  else if(CONN_SOCK_IDX_VALID(conn->send_idx)) {
1062
    /* Default is that we want to send something to the server */
1063
0
    result = Curl_pollset_add_out(data, ps, conn->sock[conn->send_idx]);
1064
0
  }
1065
1.75k
  if(!result)
1066
1.75k
    result = multi_adjust_pollset(data, ps);
1067
1.75k
  if(!result)
1068
1.75k
    result = Curl_conn_adjust_pollset(data, conn, ps);
1069
1.75k
  return result;
1070
1.75k
}
1071
1072
static CURLcode mstate_domore_pollset(struct Curl_easy *data,
1073
                                      struct easy_pollset *ps)
1074
0
{
1075
0
  struct connectdata *conn = data->conn;
1076
0
  CURLcode result = CURLE_OK;
1077
1078
0
  if(conn->scheme->run->domore_pollset)
1079
0
    result = conn->scheme->run->domore_pollset(data, ps);
1080
0
  else if(CONN_SOCK_IDX_VALID(conn->send_idx)) {
1081
    /* Default is that we want to send something to the server */
1082
0
    result = Curl_pollset_add_out(data, ps, conn->sock[conn->send_idx]);
1083
0
  }
1084
0
  if(!result)
1085
0
    result = multi_adjust_pollset(data, ps);
1086
0
  if(!result)
1087
0
    result = Curl_conn_adjust_pollset(data, conn, ps);
1088
0
  return result;
1089
0
}
1090
1091
static CURLcode mstate_perform_pollset(struct Curl_easy *data,
1092
                                       struct easy_pollset *ps)
1093
0
{
1094
0
  struct connectdata *conn = data->conn;
1095
0
  CURLcode result = CURLE_OK;
1096
1097
0
  if(conn->scheme->run->perform_pollset)
1098
0
    result = conn->scheme->run->perform_pollset(data, ps);
1099
0
  else {
1100
    /* Default is to obey the request flags for send/recv */
1101
0
    if(Curl_req_want_recv(data) && CONN_SOCK_IDX_VALID(conn->recv_idx)) {
1102
0
      result = Curl_pollset_add_in(data, ps, conn->sock[conn->recv_idx]);
1103
0
    }
1104
0
    if(!result && Curl_req_want_send(data) &&
1105
0
       CONN_SOCK_IDX_VALID(conn->send_idx)) {
1106
0
      result = Curl_pollset_add_out(data, ps, conn->sock[conn->send_idx]);
1107
0
    }
1108
0
  }
1109
0
  if(!result)
1110
0
    result = multi_adjust_pollset(data, ps);
1111
0
  if(!result)
1112
0
    result = Curl_conn_adjust_pollset(data, conn, ps);
1113
0
  return result;
1114
0
}
1115
1116
#ifdef CURLVERBOSE
1117
static size_t multi_timeouts_count(struct expire_timers *timeouts)
1118
0
{
1119
0
  size_t n = 0;
1120
0
  uint8_t eid = timeouts->first;
1121
0
  for(; eid < EXPIRE_LAST; eid = timeouts->next[eid])
1122
0
    ++n;
1123
0
  return n;
1124
0
}
1125
#endif
1126
1127
/* Initializes `poll_set` with the current socket poll actions needed
1128
 * for transfer `data`. */
1129
CURLMcode Curl_multi_pollset(struct Curl_easy *data,
1130
                             struct easy_pollset *ps)
1131
4.80k
{
1132
4.80k
  CURLcode result = CURLE_OK;
1133
1134
4.80k
  Curl_pollset_reset(ps);
1135
4.80k
#ifdef ENABLE_INTERNAL_WAKEUP
1136
  /* The admin handle always listens on the wakeup socket when there
1137
   * are transfers alive. */
1138
4.80k
  if(data->multi && (data == data->multi->admin) &&
1139
2.40k
     data->multi->xfers_really_alive) {
1140
2.40k
    CURL_TRC_M(data, "adding wakeup, %u xfers really alive",
1141
2.40k
               data->multi->xfers_really_alive);
1142
2.40k
    result = Curl_pollset_add_in(data, ps, data->multi->wakeup_internal[0]);
1143
2.40k
  }
1144
4.80k
#endif
1145
  /* If the transfer has no connection, this is fine. Happens when
1146
     called via curl_multi_remove_handle() => Curl_multi_ev_assess() =>
1147
     Curl_multi_pollset(). */
1148
4.80k
  if(!result && data->conn) {
1149
2.40k
    switch(data->mstate) {
1150
0
    case MSTATE_INIT:
1151
0
    case MSTATE_PENDING:
1152
0
    case MSTATE_SETUP:
1153
0
    case MSTATE_CONNECT:
1154
      /* nothing to poll for yet */
1155
0
      break;
1156
1157
643
    case MSTATE_CONNECTING:
1158
643
      result = mstate_connecting_pollset(data, ps);
1159
643
      break;
1160
1161
0
    case MSTATE_PROTOCONNECT:
1162
0
    case MSTATE_PROTOCONNECTING:
1163
0
      result = mstate_protocol_pollset(data, ps);
1164
0
      break;
1165
1166
0
    case MSTATE_DO:
1167
1.75k
    case MSTATE_DOING:
1168
1.75k
      result = mstate_do_pollset(data, ps);
1169
1.75k
      break;
1170
1171
0
    case MSTATE_DOING_MORE:
1172
0
      result = mstate_domore_pollset(data, ps);
1173
0
      break;
1174
1175
0
    case MSTATE_DID: /* same as PERFORMING in regard to polling */
1176
0
    case MSTATE_PERFORMING:
1177
0
      result = mstate_perform_pollset(data, ps);
1178
0
      break;
1179
1180
0
    case MSTATE_RATELIMITING:
1181
      /* we need to let time pass, ignore socket(s) */
1182
0
      break;
1183
1184
0
    case MSTATE_DONE:
1185
0
    case MSTATE_COMPLETED:
1186
0
    case MSTATE_MSGSENT:
1187
      /* nothing more to poll for */
1188
0
      break;
1189
1190
0
    default:
1191
0
      failf(data, "multi_getsock: unexpected multi state %d",
1192
0
            (int)data->mstate);
1193
0
      DEBUGASSERT(0);
1194
0
      break;
1195
2.40k
    }
1196
2.40k
  }
1197
1198
4.80k
  if(result) {
1199
0
    if(result == CURLE_OUT_OF_MEMORY)
1200
0
      return CURLM_OUT_OF_MEMORY;
1201
0
    failf(data, "error determining pollset: %d", (int)result);
1202
0
    return CURLM_INTERNAL_ERROR;
1203
0
  }
1204
1205
4.80k
#ifdef CURLVERBOSE
1206
4.80k
  if(CURL_TRC_M_is_verbose(data)) {
1207
0
    size_t timeout_count = multi_timeouts_count(&data->state.timeouts);
1208
0
    switch(ps->n) {
1209
0
    case 0:
1210
0
      CURL_TRC_M(data, "pollset[], timeouts=%zu, paused %d/%d (r/w)",
1211
0
                 timeout_count,
1212
0
                 Curl_xfer_send_is_paused(data),
1213
0
                 Curl_xfer_recv_is_paused(data));
1214
0
      break;
1215
0
    case 1:
1216
0
      CURL_TRC_M(data, "pollset[fd=%" FMT_SOCKET_T " %s%s], timeouts=%zu",
1217
0
                 ps->sockets[0],
1218
0
                 (ps->actions[0] & CURL_POLL_IN) ? "IN" : "",
1219
0
                 (ps->actions[0] & CURL_POLL_OUT) ? "OUT" : "",
1220
0
                 timeout_count);
1221
0
      break;
1222
0
    case 2:
1223
0
      CURL_TRC_M(data, "pollset[fd=%" FMT_SOCKET_T " %s%s, "
1224
0
                 "fd=%" FMT_SOCKET_T " %s%s], timeouts=%zu",
1225
0
                 ps->sockets[0],
1226
0
                 (ps->actions[0] & CURL_POLL_IN) ? "IN" : "",
1227
0
                 (ps->actions[0] & CURL_POLL_OUT) ? "OUT" : "",
1228
0
                 ps->sockets[1],
1229
0
                 (ps->actions[1] & CURL_POLL_IN) ? "IN" : "",
1230
0
                 (ps->actions[1] & CURL_POLL_OUT) ? "OUT" : "",
1231
0
                 timeout_count);
1232
0
      break;
1233
0
    default:
1234
0
      CURL_TRC_M(data, "pollset[fds=%u], timeouts=%zu", ps->n, timeout_count);
1235
0
      break;
1236
0
    }
1237
0
    CURL_TRC_EASY_TIMERS(data);
1238
0
  }
1239
4.80k
#endif
1240
1241
4.80k
  return CURLM_OK;
1242
4.80k
}
1243
1244
CURLMcode curl_multi_fdset(CURLM *m,
1245
                           fd_set *read_fd_set, fd_set *write_fd_set,
1246
                           fd_set *exc_fd_set, int *max_fd)
1247
2.40k
{
1248
2.40k
  struct Curl_mapi_guard guard;
1249
2.40k
  CURLMcode mresult;
1250
1251
2.40k
  if(CURL_MAPI_ENTER(&guard, m, multi_fdset, &mresult)) {
1252
    /* Scan through all the easy handles to get the file descriptors set.
1253
       Some easy handles may not have connected to the remote host yet,
1254
       and then we must make sure that is done. */
1255
2.40k
    struct Curl_multi *multi = m;
1256
2.40k
    struct easy_pollset ps;
1257
2.40k
    int this_max_fd = -1;
1258
2.40k
    unsigned int i;
1259
2.40k
    uint32_t mid;
1260
2.40k
    (void)exc_fd_set;
1261
1262
2.40k
    Curl_pollset_init(&ps);
1263
2.40k
    if(Curl_uint32_bset_first(&multi->process, &mid)) {
1264
4.80k
      do {
1265
4.80k
        struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
1266
1267
4.80k
        if(!data) {
1268
0
          DEBUGASSERT(0);
1269
0
          continue;
1270
0
        }
1271
1272
4.80k
        Curl_multi_pollset(data, &ps);
1273
9.56k
        for(i = 0; i < ps.n; i++) {
1274
4.76k
          if(!FDSET_SOCK(ps.sockets[i]))
1275
            /* pretend it does not exist */
1276
0
            continue;
1277
4.76k
          if(ps.actions[i] & CURL_POLL_IN)
1278
4.76k
            FD_SET(ps.sockets[i], read_fd_set);
1279
4.76k
          if(ps.actions[i] & CURL_POLL_OUT)
1280
4.76k
            FD_SET(ps.sockets[i], write_fd_set);
1281
4.76k
          if((int)ps.sockets[i] > this_max_fd)
1282
4.76k
            this_max_fd = (int)ps.sockets[i];
1283
4.76k
        }
1284
4.80k
      } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
1285
2.40k
    }
1286
1287
2.40k
    Curl_cshutdn_setfds(&multi->cshutdn, multi->admin,
1288
2.40k
                        read_fd_set, write_fd_set, &this_max_fd);
1289
1290
2.40k
    *max_fd = this_max_fd;
1291
2.40k
    Curl_pollset_cleanup(&ps);
1292
1293
2.40k
    mresult = CURLM_OK;
1294
2.40k
  }
1295
2.40k
  CURL_MAPI_LEAVE(&guard);
1296
2.40k
  return mresult;
1297
2.40k
}
1298
1299
CURLMcode curl_multi_waitfds(CURLM *m,
1300
                             struct curl_waitfd *ufds,
1301
                             unsigned int size,
1302
                             unsigned int *fd_count)
1303
0
{
1304
0
  struct Curl_mapi_guard guard;
1305
0
  CURLMcode mresult;
1306
1307
0
  if(CURL_MAPI_ENTER(&guard, m, multi_waitfds, &mresult)) {
1308
0
    struct Curl_waitfds cwfds;
1309
0
    struct Curl_multi *multi = m;
1310
0
    struct easy_pollset ps;
1311
0
    unsigned int need = 0;
1312
0
    uint32_t mid;
1313
1314
0
    if(!ufds && (size || !fd_count)) {
1315
0
      mresult = CURLM_BAD_FUNCTION_ARGUMENT;
1316
0
      goto out;
1317
0
    }
1318
1319
0
    Curl_pollset_init(&ps);
1320
0
    Curl_waitfds_init(&cwfds, ufds, size);
1321
0
    mresult = CURLM_OK;
1322
0
    if(Curl_uint32_bset_first(&multi->process, &mid)) {
1323
0
      do {
1324
0
        struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
1325
0
        if(!data) {
1326
0
          DEBUGASSERT(0);
1327
0
          Curl_uint32_bset_remove(&multi->process, mid);
1328
0
          Curl_uint32_bset_remove(&multi->dirty, mid);
1329
0
          continue;
1330
0
        }
1331
0
        Curl_multi_pollset(data, &ps);
1332
0
        need += Curl_waitfds_add_ps(&cwfds, &ps);
1333
0
      } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
1334
0
    }
1335
1336
0
    need += Curl_cshutdn_add_waitfds(&multi->cshutdn, multi->admin, &cwfds);
1337
1338
0
    if(need != cwfds.n && ufds)
1339
0
      mresult = CURLM_OUT_OF_MEMORY;
1340
1341
0
    if(fd_count)
1342
0
      *fd_count = need;
1343
0
    Curl_pollset_cleanup(&ps);
1344
0
  }
1345
0
out:
1346
0
  CURL_MAPI_LEAVE(&guard);
1347
0
  return mresult;
1348
0
}
1349
1350
#ifdef USE_WINSOCK
1351
/* Reset FD_WRITE for TCP sockets. Nothing is actually sent. UDP sockets cannot
1352
 * be reset this way because an empty datagram would be sent. #9203
1353
 *
1354
 * "On Windows the internal state of FD_WRITE as returned from
1355
 * WSAEnumNetworkEvents is only reset after successful send()."
1356
 */
1357
static void reset_socket_fdwrite(curl_socket_t s)
1358
{
1359
  int t;
1360
  int l = (int)sizeof(t);
1361
  if(!getsockopt(s, SOL_SOCKET, SO_TYPE, (char *)&t, &l) && t == SOCK_STREAM)
1362
    swrite(s, NULL, 0);
1363
}
1364
1365
static CURLMcode multi_winsock_select(struct Curl_multi *multi,
1366
                                      struct curl_pollfds *cpfds,
1367
                                      unsigned int curl_nfds,
1368
                                      struct curl_waitfd extra_fds[],
1369
                                      unsigned int extra_nfds,
1370
                                      int timeout_ms,
1371
                                      bool extrawait,
1372
                                      int *pnevents)
1373
{
1374
  CURLMcode mresult = CURLM_OK;
1375
  WSANETWORKEVENTS wsa_events;
1376
  int nevents = 0;
1377
  size_t i;
1378
1379
  DEBUGASSERT(multi->wsa_event != WSA_INVALID_EVENT);
1380
1381
  /* Set the WSA events based on the collected pollds */
1382
  for(i = 0; i < cpfds->n; i++) {
1383
    long mask = 0;
1384
    if(cpfds->pfds[i].events & POLLIN)
1385
      mask |= FD_READ | FD_ACCEPT | FD_CLOSE;
1386
    if(cpfds->pfds[i].events & POLLPRI)
1387
      mask |= FD_OOB;
1388
    if(cpfds->pfds[i].events & POLLOUT) {
1389
      mask |= FD_WRITE | FD_CONNECT | FD_CLOSE;
1390
      reset_socket_fdwrite(cpfds->pfds[i].fd);
1391
    }
1392
    if(mask && WSAEventSelect(cpfds->pfds[i].fd, multi->wsa_event, mask)) {
1393
      mresult = CURLM_OUT_OF_MEMORY;
1394
      goto out;
1395
    }
1396
  }
1397
1398
  if(cpfds->n || extrawait) {
1399
    int pollrc = 0;
1400
1401
    if(cpfds->n) {         /* pre-check with Winsock */
1402
      pollrc = Curl_poll(cpfds->pfds, cpfds->n, 0);
1403
      if(pollrc < 0) {
1404
        mresult = CURLM_UNRECOVERABLE_POLL;
1405
        goto out;
1406
      }
1407
      nevents = pollrc;
1408
    }
1409
1410
    if(!nevents) {
1411
      /* now wait... if not ready during the pre-check (pollrc == 0) */
1412
      WSAWaitForMultipleEvents(1, &multi->wsa_event, FALSE, (DWORD)timeout_ms,
1413
                               FALSE);
1414
    }
1415
1416
    /* With Winsock, we have to run the following section unconditionally
1417
       to call WSAEventSelect(fd, event, 0) on all the sockets */
1418
    /* copy revents results from the poll to the curl_multi_wait poll
1419
       struct, the bit values of the actual underlying poll() implementation
1420
       may not be the same as the ones in the public libcurl API! */
1421
    for(i = 0; i < extra_nfds; i++) {
1422
      unsigned short mask = 0;
1423
      curl_socket_t s = extra_fds[i].fd;
1424
1425
      wsa_events.lNetworkEvents = 0;
1426
      if(WSAEnumNetworkEvents(s, NULL, &wsa_events) == 0) {
1427
        if(wsa_events.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
1428
          mask |= CURL_WAIT_POLLIN;
1429
        if(wsa_events.lNetworkEvents & (FD_WRITE | FD_CONNECT | FD_CLOSE))
1430
          mask |= CURL_WAIT_POLLOUT;
1431
        if(wsa_events.lNetworkEvents & FD_OOB)
1432
          mask |= CURL_WAIT_POLLPRI;
1433
        if(!pollrc && wsa_events.lNetworkEvents)
1434
          nevents++;
1435
      }
1436
      WSAEventSelect(s, multi->wsa_event, 0);
1437
      if(!pollrc) {
1438
        extra_fds[i].revents = (short)mask;
1439
        continue;
1440
      }
1441
      else {
1442
        unsigned r = (unsigned)cpfds->pfds[curl_nfds + i].revents;
1443
        if(r & POLLIN)
1444
          mask |= CURL_WAIT_POLLIN;
1445
        if(r & POLLOUT)
1446
          mask |= CURL_WAIT_POLLOUT;
1447
        if(r & POLLPRI)
1448
          mask |= CURL_WAIT_POLLPRI;
1449
        extra_fds[i].revents = (short)mask;
1450
      }
1451
    }
1452
1453
    /* Count up all our own sockets that had activity,
1454
       and remove them from the event. */
1455
    for(i = 0; i < curl_nfds; ++i) {
1456
      wsa_events.lNetworkEvents = 0;
1457
      if(WSAEnumNetworkEvents(cpfds->pfds[i].fd, NULL, &wsa_events) == 0) {
1458
        if(!pollrc && wsa_events.lNetworkEvents)
1459
          nevents++;
1460
      }
1461
      WSAEventSelect(cpfds->pfds[i].fd, multi->wsa_event, 0);
1462
    }
1463
    WSAResetEvent(multi->wsa_event);
1464
  }
1465
1466
out:
1467
  *pnevents = nevents;
1468
  return mresult;
1469
}
1470
1471
#else /* USE_WINSOCK */
1472
1473
static CURLMcode multi_posix_poll(struct Curl_multi *multi,
1474
                                  struct curl_pollfds *cpfds,
1475
                                  unsigned int curl_nfds,
1476
                                  struct curl_waitfd extra_fds[],
1477
                                  unsigned int extra_nfds,
1478
                                  int timeout_ms,
1479
                                  bool extrawait,
1480
                                  int *pnevents)
1481
0
{
1482
0
  CURLMcode mresult = CURLM_OK;
1483
0
  int nevents = 0;
1484
0
  size_t i;
1485
1486
0
  (void)multi;
1487
0
  if(cpfds->n) {
1488
0
    int pollrc = Curl_poll(cpfds->pfds, cpfds->n, timeout_ms); /* wait... */
1489
0
    if(pollrc < 0) {
1490
0
      mresult = CURLM_UNRECOVERABLE_POLL;
1491
0
      goto out;
1492
0
    }
1493
0
    nevents = pollrc;
1494
1495
    /* copy revents results from the poll to the curl_multi_wait poll
1496
       struct, the bit values of the actual underlying poll() implementation
1497
       may not be the same as the ones in the public libcurl API! */
1498
0
    for(i = 0; i < extra_nfds; i++) {
1499
0
      unsigned r = (unsigned)cpfds->pfds[curl_nfds + i].revents;
1500
0
      unsigned short mask = 0;
1501
0
      if(r & POLLIN)
1502
0
        mask |= CURL_WAIT_POLLIN;
1503
0
      if(r & POLLOUT)
1504
0
        mask |= CURL_WAIT_POLLOUT;
1505
0
      if(r & POLLPRI)
1506
0
        mask |= CURL_WAIT_POLLPRI;
1507
0
      extra_fds[i].revents = (short)mask;
1508
0
    }
1509
0
  }
1510
0
  else if(extrawait) {
1511
    /* No fds to poll, but asked to obey timeout_ms anyway. We cannot
1512
     * use Curl_poll() as it, on some platforms, returns immediately
1513
     * without fds. */
1514
0
    curlx_wait_ms(timeout_ms);
1515
0
  }
1516
1517
0
out:
1518
0
  *pnevents = nevents;
1519
0
  return mresult;
1520
0
}
1521
1522
#endif /* !USE_WINSOCK */
1523
1524
0
#define NUM_POLLS_ON_STACK 10
1525
1526
static CURLMcode multi_wait(struct Curl_multi *multi,
1527
                            struct curl_waitfd extra_fds[],
1528
                            unsigned int extra_nfds,
1529
                            int timeout_ms,
1530
                            int *ret,
1531
                            bool extrawait)  /* when no socket, wait */
1532
0
{
1533
0
  size_t i;
1534
0
  int timeout_internal;
1535
0
  int nevents = 0;
1536
0
  struct easy_pollset ps;
1537
0
  struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];
1538
0
  struct curl_pollfds cpfds;
1539
0
  unsigned int curl_nfds = 0; /* how many pfds are for curl transfers */
1540
0
  struct Curl_easy *data = NULL;
1541
0
  CURLMcode mresult = CURLM_OK;
1542
0
  uint32_t mid;
1543
0
#ifdef ENABLE_WAKEUP
1544
0
  int wakeup_idx = -1;
1545
0
#endif
1546
1547
0
  if(timeout_ms < 0)
1548
0
    return CURLM_BAD_FUNCTION_ARGUMENT;
1549
1550
0
  Curl_pollset_init(&ps);
1551
0
  Curl_pollfds_init(&cpfds, a_few_on_stack, NUM_POLLS_ON_STACK);
1552
1553
  /* Add the curl handles to our pollfds first */
1554
0
  if(Curl_uint32_bset_first(&multi->process, &mid)) {
1555
0
    do {
1556
0
      data = Curl_multi_get_easy(multi, mid);
1557
0
      if(!data) {
1558
0
        DEBUGASSERT(0);
1559
0
        Curl_uint32_bset_remove(&multi->process, mid);
1560
0
        Curl_uint32_bset_remove(&multi->dirty, mid);
1561
0
        continue;
1562
0
      }
1563
0
      Curl_multi_pollset(data, &ps);
1564
0
      if(Curl_pollfds_add_ps(&cpfds, &ps)) {
1565
0
        mresult = CURLM_OUT_OF_MEMORY;
1566
0
        goto out;
1567
0
      }
1568
0
    } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
1569
0
  }
1570
1571
0
  if(Curl_cshutdn_add_pollfds(&multi->cshutdn, multi->admin, &cpfds)) {
1572
0
    mresult = CURLM_OUT_OF_MEMORY;
1573
0
    goto out;
1574
0
  }
1575
1576
0
#ifdef ENABLE_WAKEUP
1577
  /* If `extrawait` is TRUE *or* we have `extra_fds`to poll *or* we
1578
   * have transfer sockets to poll, we obey `timeout_ms`.
1579
   * Then we need to also monitor the multi's wakeup
1580
   * socket to catch calls to `curl_multi_wakeup()` during the wait. */
1581
0
  if(extrawait || cpfds.n || extra_nfds) {
1582
0
    wakeup_idx = cpfds.n;
1583
0
    if(Curl_pollfds_add_sock(&cpfds, multi->wakeup_pair[0], POLLIN)) {
1584
0
      mresult = CURLM_OUT_OF_MEMORY;
1585
0
      goto out;
1586
0
    }
1587
0
  }
1588
0
#endif
1589
1590
0
  curl_nfds = cpfds.n; /* what curl internally uses in cpfds */
1591
  /* Add external file descriptions from poll-like struct curl_waitfd */
1592
0
  for(i = 0; i < extra_nfds; i++) {
1593
0
    unsigned short events = 0;
1594
0
    if(extra_fds[i].events & CURL_WAIT_POLLIN)
1595
0
      events |= POLLIN;
1596
0
    if(extra_fds[i].events & CURL_WAIT_POLLPRI)
1597
0
      events |= POLLPRI;
1598
0
    if(extra_fds[i].events & CURL_WAIT_POLLOUT)
1599
0
      events |= POLLOUT;
1600
0
    if(Curl_pollfds_add_sock(&cpfds, extra_fds[i].fd, events)) {
1601
0
      mresult = CURLM_OUT_OF_MEMORY;
1602
0
      goto out;
1603
0
    }
1604
0
  }
1605
1606
  /* We check the internal timeout *AFTER* we collected all sockets to
1607
   * poll. Collecting the sockets may install new timers by protocols
1608
   * and connection filters.
1609
   * Use the shorter one of the internal and the caller requested timeout.
1610
   * If we are called with `!extrawait` and multi_timeout() reports no
1611
   * timeouts exist, do not wait. */
1612
0
  multi_timeout(multi, NULL, &timeout_internal);
1613
0
  if((timeout_internal >= 0) && (timeout_internal < timeout_ms))
1614
0
    timeout_ms = timeout_internal;
1615
1616
0
  if(data)
1617
0
    CURL_TRC_M(data, "multi_wait(fds=%u, timeout=%d) tinternal=%d",
1618
0
               cpfds.n, timeout_ms, timeout_internal);
1619
1620
#ifdef USE_WINSOCK
1621
  mresult = multi_winsock_select(multi, &cpfds, curl_nfds,
1622
                                 extra_fds, extra_nfds,
1623
                                 timeout_ms, extrawait, &nevents);
1624
#else
1625
0
  mresult = multi_posix_poll(multi, &cpfds, curl_nfds,
1626
0
                             extra_fds, extra_nfds,
1627
0
                             timeout_ms, extrawait, &nevents);
1628
0
#endif
1629
1630
0
#ifdef ENABLE_WAKEUP
1631
0
    if(nevents && (wakeup_idx >= 0)) {
1632
0
      if(cpfds.pfds[wakeup_idx].revents & POLLIN) {
1633
0
        (void)Curl_wakeup_consume(multi->wakeup_pair, TRUE);
1634
        /* do not count the wakeup socket into the returned value */
1635
0
        nevents--;
1636
0
      }
1637
0
    }
1638
0
#endif
1639
1640
0
out:
1641
0
  Curl_pollset_cleanup(&ps);
1642
0
  Curl_pollfds_cleanup(&cpfds);
1643
0
  if(ret)
1644
0
    *ret = nevents;
1645
0
  return mresult;
1646
0
}
1647
1648
CURLMcode curl_multi_wait(CURLM *m,
1649
                          struct curl_waitfd extra_fds[],
1650
                          unsigned int extra_nfds,
1651
                          int timeout_ms,
1652
                          int *ret)
1653
0
{
1654
0
  struct Curl_mapi_guard guard;
1655
0
  CURLMcode mresult;
1656
1657
0
  if(CURL_MAPI_ENTER(&guard, m, multi_wait, &mresult)) {
1658
0
    mresult = multi_wait(m, extra_fds, extra_nfds, timeout_ms, ret, FALSE);
1659
0
  }
1660
0
  CURL_MAPI_LEAVE(&guard);
1661
0
  return mresult;
1662
0
}
1663
1664
CURLMcode curl_multi_poll(CURLM *m,
1665
                          struct curl_waitfd extra_fds[],
1666
                          unsigned int extra_nfds,
1667
                          int timeout_ms,
1668
                          int *ret)
1669
0
{
1670
0
  struct Curl_mapi_guard guard;
1671
0
  CURLMcode mresult;
1672
1673
0
  if(CURL_MAPI_ENTER(&guard, m, multi_poll, &mresult)) {
1674
0
    mresult = multi_wait(m, extra_fds, extra_nfds, timeout_ms, ret, TRUE);
1675
0
  }
1676
0
  CURL_MAPI_LEAVE(&guard);
1677
0
  return mresult;
1678
0
}
1679
1680
CURLMcode curl_multi_wakeup(CURLM *m)
1681
0
{
1682
  /* this function is usually called from another thread,
1683
     it has to be careful only to access parts of the
1684
     Curl_multi struct that are constant */
1685
0
  struct Curl_multi *multi = m;
1686
0
  CURLMcode mresult = CURLM_WAKEUP_FAILURE;
1687
1688
  /* GOOD_MULTI_HANDLE can be safely called */
1689
0
  if(!GOOD_MULTI_HANDLE(multi))
1690
0
    return CURLM_BAD_HANDLE;
1691
1692
0
#ifdef ENABLE_WAKEUP
1693
  /* the wakeup_pair variable is only written during init and cleanup,
1694
     making it safe to access from another thread after the init part
1695
     and before cleanup */
1696
0
  if(!Curl_wakeup_signal(multi->wakeup_pair))
1697
0
    mresult = CURLM_OK;
1698
0
#endif
1699
#ifdef USE_WINSOCK
1700
  if(WSASetEvent(multi->wsa_event))
1701
    mresult = CURLM_OK;
1702
#endif
1703
0
  return mresult;
1704
0
}
1705
1706
#ifdef ENABLE_INTERNAL_WAKEUP
1707
void Curl_multi_wakeup_internal(struct Curl_multi *multi)
1708
0
{
1709
  /* This is expected to be invocable from another thread which
1710
   * does NOT outlive the multi handle. Check for sanity. */
1711
0
  if(GOOD_MULTI_HANDLE(multi))
1712
0
    Curl_wakeup_signal(multi->wakeup_internal);
1713
0
  else
1714
0
    DEBUGASSERT(0);
1715
0
}
1716
#endif
1717
1718
/*
1719
 * multi_ischanged() is called
1720
 *
1721
 * Returns TRUE/FALSE whether the state is changed to trigger a CONNECT_PEND
1722
 * => CONNECT action.
1723
 *
1724
 * Set 'clear' to TRUE to have it also clear the state variable.
1725
 */
1726
static bool multi_ischanged(struct Curl_multi *multi, bool clear)
1727
62.2k
{
1728
62.2k
  bool retval = FALSE;
1729
62.2k
  DEBUGASSERT(multi);
1730
62.2k
  if(multi) {
1731
62.2k
    retval = (bool)multi->recheckstate;
1732
62.2k
    if(clear)
1733
53.3k
      multi->recheckstate = FALSE;
1734
62.2k
  }
1735
62.2k
  return retval;
1736
62.2k
}
1737
1738
/*
1739
 * Curl_multi_connchanged() is called to tell that there is a connection in
1740
 * this multi handle that has changed state (multiplexing become possible, the
1741
 * number of allowed streams changed or similar), and a subsequent use of this
1742
 * multi handle should move CONNECT_PEND handles back to CONNECT to have them
1743
 * retry.
1744
 */
1745
void Curl_multi_connchanged(struct Curl_multi *multi)
1746
4.28k
{
1747
  /* May be invoked for a `data->multi` when no multi is present. */
1748
4.28k
  if(multi)
1749
4.28k
    multi->recheckstate = TRUE;
1750
4.28k
}
1751
1752
CURLMcode Curl_multi_add_perform(struct Curl_multi *multi,
1753
                                 struct Curl_easy *data,
1754
                                 struct connectdata *conn)
1755
0
{
1756
0
  CURLMcode mresult;
1757
1758
0
  mresult = Curl_multi_add_handle(multi, data);
1759
0
  if(!mresult) {
1760
0
    CURLcode result;
1761
1762
    /* pass in NULL for 'conn' here since we do not want to init the
1763
       connection, only this transfer */
1764
0
    result = Curl_init_transfer(data, NULL);
1765
0
    if(result) {
1766
0
      Curl_multi_remove_handle(multi, data);
1767
0
      return CURLM_INTERNAL_ERROR;
1768
0
    }
1769
1770
    /* take this handle to the perform state right away */
1771
0
    multistate(data, MSTATE_PERFORMING);
1772
0
    Curl_attach_connection(data, conn, TRUE);
1773
0
    CURL_REQ_SET_RECV(data);
1774
0
  }
1775
0
  return mresult;
1776
0
}
1777
1778
static CURLcode multi_do(struct Curl_easy *data, bool *done)
1779
2.04k
{
1780
2.04k
  CURLcode result = CURLE_OK;
1781
2.04k
  struct connectdata *conn = data->conn;
1782
1783
2.04k
  DEBUGASSERT(conn);
1784
2.04k
  DEBUGASSERT(conn->scheme);
1785
1786
2.04k
  if(conn->scheme->run->do_it)
1787
2.04k
    result = conn->scheme->run->do_it(data, done);
1788
1789
2.04k
  return result;
1790
2.04k
}
1791
1792
/*
1793
 * multi_do_more() is called during the DO_MORE multi state. It is a second
1794
 * stage DO state which (wrongly) was introduced to support FTP's second
1795
 * connection.
1796
 *
1797
 * 'complete' can return DOMORE_INCOMPLETE, DOMORE_DONE or DOMORE_GOBACK
1798
 * (to DOING state when there is more work to do)
1799
 */
1800
1801
static CURLcode multi_do_more(struct Curl_easy *data, domore *complete)
1802
0
{
1803
0
  CURLcode result = CURLE_OK;
1804
0
  struct connectdata *conn = data->conn;
1805
1806
0
  *complete = DOMORE_INCOMPLETE;
1807
1808
0
  if(conn->scheme->run->do_more)
1809
0
    result = conn->scheme->run->do_more(data, complete);
1810
1811
0
  return result;
1812
0
}
1813
1814
/*
1815
 * Check whether a timeout occurred, and handle it if it did
1816
 */
1817
static bool multi_handle_timeout(struct Curl_easy *data,
1818
                                 const struct curltime *pnow,
1819
                                 bool *stream_error,
1820
                                 CURLcode *result)
1821
25.2k
{
1822
25.2k
  timediff_t timeout_ms;
1823
1824
25.2k
  timeout_ms = Curl_timeleft_now_ms(data, pnow);
1825
25.2k
  if(timeout_ms < 0) {
1826
    /* Handle timed out */
1827
23
    timerid base_timer = Curl_is_connecting(data) ?
1828
23
                         TIMER_STARTSINGLE : TIMER_STARTOP;
1829
23
    timediff_t elapsed_ms = Curl_pgrs_since_ms(data, NULL, base_timer);
1830
23
    if(data->mstate == MSTATE_CONNECTING) {
1831
21
      failf(data, "%s timed out after %" FMT_TIMEDIFF_T " milliseconds",
1832
21
            data->conn->bits.dns_resolved ? "Connection" : "Resolving",
1833
21
            elapsed_ms);
1834
      /* keep what we know about a partly done connect, e.g. TCP done
1835
       * but TLS handshake still ongoing */
1836
21
      Curl_conn_report_failed(data, FIRSTSOCKET);
1837
21
    }
1838
2
    else {
1839
2
      struct SingleRequest *k = &data->req;
1840
2
      if(k->size != -1) {
1841
0
        failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
1842
0
              " milliseconds with %" FMT_OFF_T " out of %"
1843
0
              FMT_OFF_T " bytes received",
1844
0
              elapsed_ms, k->bytecount, k->size);
1845
0
      }
1846
2
      else {
1847
2
        failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
1848
2
              " milliseconds with %" FMT_OFF_T " bytes received",
1849
2
              elapsed_ms, k->bytecount);
1850
2
      }
1851
2
    }
1852
23
    *result = CURLE_OPERATION_TIMEDOUT;
1853
23
    if(data->conn) {
1854
      /* Force connection closed if the connection has indeed been used */
1855
21
      if(data->mstate > MSTATE_DO) {
1856
0
        streamclose(data->conn);
1857
0
        *stream_error = TRUE;
1858
0
      }
1859
21
      (void)multi_done(data, *result, TRUE);
1860
21
    }
1861
23
    return TRUE;
1862
23
  }
1863
1864
25.1k
  return FALSE;
1865
25.2k
}
1866
1867
/*
1868
 * We are doing protocol-specific connecting and this is being called over and
1869
 * over from the multi interface until the connection phase is done on
1870
 * protocol layer.
1871
 */
1872
1873
static CURLcode protocol_connecting(struct Curl_easy *data, bool *done)
1874
0
{
1875
0
  CURLcode result = CURLE_OK;
1876
0
  struct connectdata *conn = data->conn;
1877
1878
0
  if(conn && conn->scheme->run->connecting) {
1879
0
    *done = FALSE;
1880
0
    result = conn->scheme->run->connecting(data, done);
1881
0
  }
1882
0
  else
1883
0
    *done = TRUE;
1884
1885
0
  return result;
1886
0
}
1887
1888
/*
1889
 * We are DOING this is being called over and over from the multi interface
1890
 * until the DOING phase is done on protocol layer.
1891
 */
1892
1893
static CURLcode protocol_doing(struct Curl_easy *data, bool *done)
1894
3.41k
{
1895
3.41k
  CURLcode result = CURLE_OK;
1896
3.41k
  struct connectdata *conn = data->conn;
1897
1898
3.41k
  if(conn && conn->scheme->run->doing) {
1899
3.41k
    *done = FALSE;
1900
3.41k
    result = conn->scheme->run->doing(data, done);
1901
3.41k
  }
1902
0
  else
1903
0
    *done = TRUE;
1904
1905
3.41k
  return result;
1906
3.41k
}
1907
1908
/*
1909
 * We have discovered that the TCP connection has been successful, we can now
1910
 * proceed with some action.
1911
 *
1912
 */
1913
static CURLcode protocol_connect(struct Curl_easy *data, bool *protocol_done)
1914
2.05k
{
1915
2.05k
  struct connectdata *conn = data->conn;
1916
2.05k
  CURLcode result = CURLE_OK;
1917
1918
2.05k
  DEBUGASSERT(conn);
1919
2.05k
  DEBUGASSERT(protocol_done);
1920
2.05k
  DEBUGASSERT(Curl_conn_is_connected(conn, FIRSTSOCKET));
1921
1922
2.05k
  *protocol_done = FALSE;
1923
2.05k
  if(!conn->bits.protoconnstart) {
1924
2.05k
    if(conn->scheme->run->connect_it) {
1925
      /* Call the protocol-specific connect function */
1926
0
      result = conn->scheme->run->connect_it(data, protocol_done);
1927
0
      if(result)
1928
0
        return result;
1929
0
    }
1930
2.05k
    conn->bits.protoconnstart = TRUE;
1931
2.05k
  }
1932
1933
  /* Unless this protocol does not have any protocol-connect callback, as
1934
     then we know we are done. */
1935
2.05k
  if(!conn->scheme->run->connecting)
1936
2.05k
    *protocol_done = TRUE;
1937
2.05k
  return CURLE_OK;
1938
2.05k
}
1939
1940
/*
1941
 * posttransfer() is called immediately after a transfer ends
1942
 */
1943
static void multi_posttransfer(struct Curl_easy *data)
1944
7.54k
{
1945
#if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(MSG_NOSIGNAL)
1946
  /* restore the signal handler for SIGPIPE before we get back */
1947
  if(!data->set.no_signal)
1948
    signal(SIGPIPE, data->state.prev_signal);
1949
#else
1950
7.54k
  (void)data;
1951
7.54k
#endif
1952
7.54k
}
1953
1954
/*
1955
 * multi_follow() handles the URL redirect magic. Pass in the 'newurl' string
1956
 * as given by the remote server and set up the new URL to request.
1957
 *
1958
 * This function DOES NOT FREE the given URL.
1959
 */
1960
static CURLcode multi_follow(struct Curl_easy *data,
1961
                             const struct Curl_scheme *handler,
1962
                             const char *newurl, /* the Location: string */
1963
                             followtype type) /* see transfer.h */
1964
0
{
1965
0
  if(handler && handler->run->follow)
1966
0
    return handler->run->follow(data, newurl, type);
1967
1968
0
  if(type == FOLLOW_RETRY)
1969
    /* Retries are generic and do not require protocol-specific redirect
1970
       handling. */
1971
0
    return CURLE_OK;
1972
1973
0
  return CURLE_TOO_MANY_REDIRECTS;
1974
0
}
1975
1976
static CURLcode mspeed_check(struct Curl_easy *data)
1977
135
{
1978
135
  if(Curl_rlimit_active(&data->progress.dl.rlimit) ||
1979
92
     Curl_rlimit_active(&data->progress.ul.rlimit)) {
1980
    /* check if our send/recv limits require idle waits */
1981
66
    const struct curltime *pnow = Curl_pgrs_now(data);
1982
66
    timediff_t recv_ms, send_ms;
1983
1984
66
    send_ms = Curl_rlimit_wait_ms(&data->progress.ul.rlimit, pnow);
1985
66
    recv_ms = Curl_rlimit_wait_ms(&data->progress.dl.rlimit, pnow);
1986
1987
66
    if(send_ms || recv_ms) {
1988
0
      if(data->mstate != MSTATE_RATELIMITING) {
1989
0
        multistate(data, MSTATE_RATELIMITING);
1990
0
      }
1991
0
      Curl_expire_set(data, EXPIRE_TOOFAST, CURLMAX(send_ms, recv_ms), pnow);
1992
0
      Curl_multi_clear_dirty(data);
1993
0
      CURL_TRC_M(data, "[RLIMIT] waiting %" FMT_TIMEDIFF_T "ms",
1994
0
                 CURLMAX(send_ms, recv_ms));
1995
0
      return CURLE_AGAIN;
1996
0
    }
1997
66
    else {
1998
      /* when will the rate limits increase next? The transfer needs
1999
       * to run again at that time or it may stall. */
2000
66
      send_ms = Curl_rlimit_next_step_ms(&data->progress.ul.rlimit, pnow);
2001
66
      recv_ms = Curl_rlimit_next_step_ms(&data->progress.dl.rlimit, pnow);
2002
66
      if(send_ms || recv_ms) {
2003
66
        timediff_t next_ms = CURLMIN(send_ms, recv_ms);
2004
66
        if(!next_ms)
2005
62
          next_ms = CURLMAX(send_ms, recv_ms);
2006
66
        Curl_expire_set(data, EXPIRE_TOOFAST, next_ms, pnow);
2007
66
        CURL_TRC_M(data, "[RLIMIT] next token update in %" FMT_TIMEDIFF_T "ms",
2008
66
                   next_ms);
2009
66
      }
2010
66
    }
2011
66
  }
2012
2013
135
  if(data->mstate != MSTATE_PERFORMING) {
2014
0
    CURL_TRC_M(data, "[RLIMIT] wait over, continue");
2015
0
    multistate(data, MSTATE_PERFORMING);
2016
0
  }
2017
135
  return CURLE_OK;
2018
135
}
2019
2020
static CURLMcode multistate_performing(struct Curl_easy *data,
2021
                                       bool *stream_errorp,
2022
                                       CURLcode *resultp)
2023
135
{
2024
135
  char *newurl = NULL;
2025
135
  bool retry = FALSE;
2026
135
  CURLMcode mresult = CURLM_OK;
2027
135
  CURLcode result = *resultp = CURLE_OK;
2028
135
  *stream_errorp = FALSE;
2029
2030
135
  if(mspeed_check(data) == CURLE_AGAIN)
2031
0
    return CURLM_OK;
2032
2033
  /* read/write data if it is ready to do so */
2034
135
  result = Curl_sendrecv(data);
2035
2036
135
  if(data->req.done || (result == CURLE_RECV_ERROR)) {
2037
    /* If CURLE_RECV_ERROR happens early enough, we assume it was a race
2038
     * condition and the server closed the reused connection exactly when we
2039
     * wanted to use it, so figure out if that is indeed the case.
2040
     */
2041
135
    CURLcode ret = Curl_retry_request(data, &newurl);
2042
135
    if(!ret)
2043
135
      retry = !!newurl;
2044
0
    else if(!result)
2045
0
      result = ret;
2046
2047
135
    if(retry) {
2048
      /* if we are to retry, set the result to OK and consider the
2049
         request as done */
2050
0
      result = CURLE_OK;
2051
0
      data->req.done = TRUE;
2052
0
    }
2053
135
  }
2054
0
#ifndef CURL_DISABLE_HTTP
2055
0
  else if((result == CURLE_HTTP2_STREAM) &&
2056
0
          Curl_h2_http_1_1_error(data)) {
2057
0
    CURLcode ret = Curl_retry_request(data, &newurl);
2058
2059
0
    if(!ret) {
2060
0
      infof(data, "Downgrades to HTTP/1.1");
2061
0
      streamclose(data->conn);
2062
0
      data->state.http_neg.wanted = CURL_HTTP_V1x;
2063
0
      data->state.http_neg.allowed = CURL_HTTP_V1x;
2064
      /* clear the error message bit too as we ignore the one we got */
2065
0
      data->state.errorbuf = FALSE;
2066
0
      if(!newurl)
2067
        /* typically for HTTP_1_1_REQUIRED error on first flight */
2068
0
        newurl = Curl_bufref_dup(&data->state.url);
2069
0
      if(!newurl) {
2070
0
        result = CURLE_OUT_OF_MEMORY;
2071
0
      }
2072
0
      else {
2073
        /* if we are to retry, set the result to OK and consider the request
2074
          as done */
2075
0
        retry = TRUE;
2076
0
        result = CURLE_OK;
2077
0
        data->req.done = TRUE;
2078
0
      }
2079
0
    }
2080
0
    else
2081
0
      result = ret;
2082
0
  }
2083
135
#endif
2084
2085
135
  if(result) {
2086
    /*
2087
     * The transfer phase returned error, we mark the connection to get closed
2088
     * to prevent being reused. This is because we cannot possibly know if the
2089
     * connection is in a good shape or not now. Unless it is a protocol which
2090
     * uses two "channels" like FTP, as then the error happened in the data
2091
     * connection.
2092
     */
2093
2094
0
    if(!(data->conn->scheme->flags & PROTOPT_DUAL) &&
2095
0
       result != CURLE_HTTP2_STREAM)
2096
0
      streamclose(data->conn);
2097
2098
0
    multi_posttransfer(data);
2099
0
    multi_done(data, result, TRUE);
2100
0
  }
2101
135
  else if(data->req.done && !Curl_cwriter_is_paused(data)) {
2102
135
    const struct Curl_scheme *handler = data->conn->scheme;
2103
2104
    /* call this even if the readwrite function returned error */
2105
135
    multi_posttransfer(data);
2106
2107
    /* When we follow redirects or is set to retry the connection, we must to
2108
       go back to the CONNECT state */
2109
135
    if(data->req.newurl || retry) {
2110
0
      followtype follow = FOLLOW_NONE;
2111
0
      if(!retry) {
2112
        /* if the URL is a follow-location and not a retried request then
2113
           figure out the URL here */
2114
0
        curlx_free(newurl);
2115
0
        newurl = data->req.newurl;
2116
0
        data->req.newurl = NULL;
2117
0
        follow = FOLLOW_REDIR;
2118
0
      }
2119
0
      else
2120
0
        follow = FOLLOW_RETRY;
2121
0
      (void)multi_done(data, CURLE_OK, FALSE);
2122
      /* multi_done() might return CURLE_GOT_NOTHING */
2123
0
      result = multi_follow(data, handler, newurl, follow);
2124
0
      if(!result) {
2125
0
        multistate(data, MSTATE_SETUP);
2126
0
        mresult = CURLM_CALL_MULTI_PERFORM;
2127
0
      }
2128
0
    }
2129
135
    else {
2130
      /* after the transfer is done, go DONE */
2131
2132
      /* but first check to see if we got a location info even though we are
2133
         not following redirects */
2134
135
      if(data->req.location) {
2135
0
        curlx_free(newurl);
2136
0
        newurl = data->req.location;
2137
0
        data->req.location = NULL;
2138
0
        result = multi_follow(data, handler, newurl, FOLLOW_FAKE);
2139
0
        if(result) {
2140
0
          *stream_errorp = TRUE;
2141
0
          result = multi_done(data, result, TRUE);
2142
0
        }
2143
0
      }
2144
2145
135
      if(!result) {
2146
135
        multistate(data, MSTATE_DONE);
2147
135
        mresult = CURLM_CALL_MULTI_PERFORM;
2148
135
      }
2149
135
    }
2150
135
  }
2151
0
  else { /* not errored, not done */
2152
0
    mspeed_check(data);
2153
0
  }
2154
135
  curlx_free(newurl);
2155
135
  *resultp = result;
2156
135
  return mresult;
2157
135
}
2158
2159
static CURLMcode multistate_do(struct Curl_easy *data,
2160
                               bool *stream_errorp,
2161
                               CURLcode *resultp)
2162
2.05k
{
2163
2.05k
  CURLMcode mresult = CURLM_OK;
2164
2.05k
  CURLcode result = CURLE_OK;
2165
2.05k
  if(data->set.fprereq) {
2166
0
    struct Curl_mapi_guard guard;
2167
0
    int prereq_rc;
2168
2169
    /* call the prerequest callback function */
2170
0
    CURL_CBAPI_START(&guard, data, easy_fprereq);
2171
0
    prereq_rc = data->set.fprereq(data->set.prereq_userp,
2172
0
                                  data->info.primary.remote_ip,
2173
0
                                  data->info.primary.local_ip,
2174
0
                                  data->info.primary.remote_port,
2175
0
                                  data->info.primary.local_port);
2176
0
    CURL_CBAPI_END(&guard);
2177
0
    if(prereq_rc != CURL_PREREQFUNC_OK) {
2178
0
      failf(data, "operation aborted by pre-request callback");
2179
      /* failure in pre-request callback - do not do any other processing */
2180
0
      result = CURLE_ABORTED_BY_CALLBACK;
2181
0
      multi_posttransfer(data);
2182
0
      multi_done(data, result, FALSE);
2183
0
      *stream_errorp = TRUE;
2184
0
      goto end;
2185
0
    }
2186
0
  }
2187
2188
2.05k
  if(data->set.connect_only && !data->set.connect_only_ws) {
2189
6
    multistate(data, MSTATE_DONE);
2190
6
    mresult = CURLM_CALL_MULTI_PERFORM;
2191
6
  }
2192
2.04k
  else {
2193
2.04k
    bool dophase_done = FALSE;
2194
    /* Perform the protocol's DO action */
2195
2.04k
    result = multi_do(data, &dophase_done);
2196
2197
2.04k
    if(!result) {
2198
2.03k
      if(!dophase_done) {
2199
2.03k
#ifndef CURL_DISABLE_FTP
2200
        /* some steps needed for wildcard matching */
2201
2.03k
        if(data->state.wildcardmatch) {
2202
0
          struct WildcardData *wc = data->wildcard;
2203
0
          if(wc->state == CURLWC_DONE || wc->state == CURLWC_SKIP) {
2204
            /* skip some states if it is important */
2205
0
            multi_done(data, CURLE_OK, FALSE);
2206
2207
            /* if there is no connection left, skip the DONE state */
2208
0
            multistate(data, data->conn ? MSTATE_DONE : MSTATE_COMPLETED);
2209
0
            mresult = CURLM_CALL_MULTI_PERFORM;
2210
0
            goto end;
2211
0
          }
2212
0
        }
2213
2.03k
#endif
2214
        /* DO was not completed in one function call, we must continue
2215
           DOING... */
2216
2.03k
        multistate(data, MSTATE_DOING);
2217
2.03k
        mresult = CURLM_CALL_MULTI_PERFORM;
2218
2.03k
      }
2219
2220
      /* after DO, go DO_DONE... or DO_MORE */
2221
0
      else if(data->conn->bits.do_more) {
2222
        /* we are supposed to do more, but we need to sit down, relax and wait
2223
           a little while first */
2224
0
        multistate(data, MSTATE_DOING_MORE);
2225
0
        mresult = CURLM_CALL_MULTI_PERFORM;
2226
0
      }
2227
0
      else {
2228
        /* we are done with the DO, now DID */
2229
0
        multistate(data, MSTATE_DID);
2230
0
        mresult = CURLM_CALL_MULTI_PERFORM;
2231
0
      }
2232
2.03k
    }
2233
9
    else if((result == CURLE_SEND_ERROR) &&
2234
0
            data->conn->bits.reuse) {
2235
      /*
2236
       * In this situation, a connection that we were trying to use may have
2237
       * unexpectedly died. If possible, send the connection back to the
2238
       * CONNECT phase so we can try again.
2239
       */
2240
0
      const struct Curl_scheme *handler = data->conn->scheme;
2241
0
      char *newurl = NULL;
2242
0
      followtype follow = FOLLOW_NONE;
2243
0
      CURLcode drc;
2244
2245
0
      drc = Curl_retry_request(data, &newurl);
2246
0
      if(drc) {
2247
        /* a failure here pretty much implies an out of memory */
2248
0
        result = drc;
2249
0
        *stream_errorp = TRUE;
2250
0
      }
2251
2252
0
      multi_posttransfer(data);
2253
0
      drc = multi_done(data, result, FALSE);
2254
2255
      /* When set to retry the connection, we must go back to the CONNECT
2256
       * state */
2257
0
      if(newurl) {
2258
0
        if(!drc || (drc == CURLE_SEND_ERROR)) {
2259
0
          follow = FOLLOW_RETRY;
2260
0
          drc = multi_follow(data, handler, newurl, follow);
2261
0
          if(!drc) {
2262
0
            multistate(data, MSTATE_SETUP);
2263
0
            mresult = CURLM_CALL_MULTI_PERFORM;
2264
0
            result = CURLE_OK;
2265
0
          }
2266
0
          else {
2267
            /* Follow failed */
2268
0
            result = drc;
2269
0
          }
2270
0
        }
2271
0
        else {
2272
          /* done did not return OK or SEND_ERROR */
2273
0
          result = drc;
2274
0
        }
2275
0
      }
2276
0
      else {
2277
        /* Have error handler disconnect conn if we cannot retry */
2278
0
        *stream_errorp = TRUE;
2279
0
      }
2280
0
      curlx_free(newurl);
2281
0
    }
2282
9
    else {
2283
      /* failure detected */
2284
9
      multi_posttransfer(data);
2285
9
      if(data->conn)
2286
9
        multi_done(data, result, FALSE);
2287
9
      *stream_errorp = TRUE;
2288
9
    }
2289
2.04k
  }
2290
2.05k
end:
2291
2.05k
  *resultp = result;
2292
2.05k
  return mresult;
2293
2.05k
}
2294
2295
static CURLMcode multistate_ratelimiting(struct Curl_easy *data,
2296
                                         CURLcode *resultp)
2297
0
{
2298
0
  CURLcode result = CURLE_OK;
2299
0
  CURLMcode mresult = CURLM_OK;
2300
0
  DEBUGASSERT(data->conn);
2301
  /* if both rates are within spec, resume transfer */
2302
0
  result = Curl_pgrsCheck(data);
2303
2304
0
  if(result) {
2305
0
    if(!(data->conn->scheme->flags & PROTOPT_DUAL) &&
2306
0
       result != CURLE_HTTP2_STREAM)
2307
0
      streamclose(data->conn);
2308
2309
0
    multi_posttransfer(data);
2310
0
    multi_done(data, result, TRUE);
2311
0
  }
2312
0
  else {
2313
0
    if(!mspeed_check(data))
2314
0
      mresult = CURLM_CALL_MULTI_PERFORM;
2315
0
  }
2316
0
  *resultp = result;
2317
0
  return mresult;
2318
0
}
2319
2320
static CURLMcode multistate_connect(struct Curl_multi *multi,
2321
                                    struct Curl_easy *data,
2322
                                    CURLcode *resultp)
2323
8.04k
{
2324
  /* Connect. We want to get a connection identifier filled in. This state can
2325
     be entered from SETUP and from PENDING. */
2326
8.04k
  bool connected;
2327
8.04k
  CURLMcode mresult = CURLM_OK;
2328
8.04k
  CURLcode result = Curl_connect(data, &connected);
2329
8.04k
  if(result == CURLE_NO_CONNECTION_AVAILABLE) {
2330
    /* There was no connection available. We will go to the pending state and
2331
       wait for an available connection. */
2332
0
    multistate(data, MSTATE_PENDING);
2333
    /* move from process to pending set */
2334
0
    Curl_uint32_bset_remove(&multi->process, data->mid);
2335
0
    Curl_uint32_bset_remove(&multi->dirty, data->mid);
2336
0
    Curl_uint32_bset_add(&multi->pending, data->mid);
2337
0
    *resultp = CURLE_OK;
2338
0
    return mresult;
2339
0
  }
2340
8.04k
  else
2341
8.04k
    multi_schedule_pending(data->multi);
2342
2343
8.04k
  if(!result) {
2344
    /* after the connect has been sent off, go WAITCONNECT unless the
2345
       protocol connect is already done and we can go directly to WAITDO or
2346
       DO! */
2347
4.28k
    mresult = CURLM_CALL_MULTI_PERFORM;
2348
2349
4.28k
    if(connected) {
2350
0
      if(!data->conn->bits.reuse &&
2351
0
         Curl_conn_is_multiplex(data->conn, FIRSTSOCKET)) {
2352
        /* new connection, can multiplex, wake pending handles */
2353
0
        multi_schedule_pending(data->multi);
2354
0
      }
2355
0
      multistate(data, MSTATE_PROTOCONNECT);
2356
0
    }
2357
4.28k
    else {
2358
4.28k
      multistate(data, MSTATE_CONNECTING);
2359
4.28k
    }
2360
4.28k
  }
2361
8.04k
  *resultp = result;
2362
8.04k
  return mresult;
2363
8.04k
}
2364
2365
/* returns the possibly updated result */
2366
static CURLcode is_finished(struct Curl_multi *multi,
2367
                            struct Curl_easy *data,
2368
                            const struct curltime *pnow,
2369
                            bool stream_error,
2370
                            CURLcode result)
2371
40.1k
{
2372
40.1k
  if(data->mstate < MSTATE_COMPLETED) {
2373
39.9k
    if(result) {
2374
      /*
2375
       * If an error was returned, and we are not in completed state now,
2376
       * then we go to completed and consider this transfer aborted.
2377
       */
2378
2379
      /* No attempt to disconnect connections must be made before this -
2380
         connection detach and termination happens only here */
2381
2382
      /* Check if we can move pending requests to send pipe */
2383
10.6k
      multi_schedule_pending(multi); /* connection */
2384
2385
10.6k
      if(data->conn) {
2386
0
        if(stream_error) {
2387
          /* Do not attempt to send data over a connection that timed out */
2388
0
          bool dead_connection = result == CURLE_OPERATION_TIMEDOUT;
2389
0
          struct connectdata *conn = data->conn;
2390
2391
          /* This is where we make sure that the conn pointer is reset.
2392
             We do not have to do this in every case block above where a
2393
             failure is detected */
2394
0
          Curl_detach_connection(data);
2395
0
          Curl_conn_close(data, conn, dead_connection);
2396
0
        }
2397
0
      }
2398
10.6k
      else if(data->mstate == MSTATE_CONNECT) {
2399
        /* Curl_connect() failed */
2400
3.75k
        multi_posttransfer(data);
2401
3.75k
        Curl_pgrsUpdate_nometer(data);
2402
3.75k
      }
2403
2404
10.6k
      multistate(data, MSTATE_COMPLETED);
2405
10.6k
      return result;
2406
10.6k
    }
2407
    /* if there is still a connection to use, call the progress function */
2408
29.3k
    else if(data->conn && Curl_conn_is_connected(data->conn, FIRSTSOCKET)) {
2409
8.31k
      result = Curl_pgrsUpdateX(data, pnow);
2410
8.31k
      if(result) {
2411
        /* aborted due to progress callback return code must close the
2412
           connection */
2413
0
        streamclose(data->conn);
2414
2415
        /* if not yet in DONE state, go there. */
2416
0
        if(data->mstate < MSTATE_DONE)
2417
0
          multistate(data, MSTATE_DONE);
2418
0
        return result;
2419
0
      }
2420
8.31k
    }
2421
39.9k
  }
2422
29.4k
  return result;
2423
40.1k
}
2424
2425
static void handle_completed(struct Curl_multi *multi,
2426
                             struct Curl_easy *data,
2427
                             CURLcode result)
2428
10.8k
{
2429
10.8k
  bool msg_to_app = data->master_mid == UINT32_MAX;
2430
2431
10.8k
  if(!msg_to_app) {
2432
    /* A sub transfer, not reported to the application. Is anyone still
2433
     * interested in processing its results? */
2434
0
    if(data->sub_xfer_done) {
2435
0
      struct Curl_easy *master = Curl_multi_get_easy(multi, data->master_mid);
2436
2437
0
      CURL_TRC_M(data, "sub xfer done for master %u", data->master_mid);
2438
0
      if(master)
2439
0
        data->sub_xfer_done(data, master, result);
2440
0
      else
2441
0
        CURL_TRC_M(data, "master easy %u already gone.", data->master_mid);
2442
0
    }
2443
0
  }
2444
10.8k
  else {
2445
    /* now fill in the CURLMsg with this info */
2446
10.8k
    struct CURLMsg *msg = &data->msg;
2447
2448
10.8k
    msg->msg = CURLMSG_DONE;
2449
10.8k
    msg->easy_handle = data;
2450
10.8k
    msg->data.result = result;
2451
2452
10.8k
    DEBUGASSERT(!data->conn);
2453
10.8k
  }
2454
10.8k
  multistate(data, MSTATE_MSGSENT);
2455
2456
  /* remove from the other sets */
2457
10.8k
  Curl_uint32_bset_remove(&multi->process, data->mid);
2458
10.8k
  Curl_uint32_bset_remove(&multi->dirty, data->mid);
2459
10.8k
  Curl_uint32_bset_remove(&multi->pending, data->mid);
2460
10.8k
  if(msg_to_app)
2461
10.8k
    multi_addmsg(multi, data);
2462
10.8k
  if(data->state.really_alive) {
2463
10.8k
    data->state.really_alive = FALSE;
2464
10.8k
    --multi->xfers_really_alive;
2465
10.8k
    if(!multi->xfers_really_alive)
2466
10.8k
      (void)multi_assess_wakeup(multi);
2467
10.8k
  }
2468
10.8k
  --multi->xfers_alive;
2469
10.8k
  if(!multi->xfers_alive)
2470
10.8k
    multi_assess_wakeup(multi);
2471
10.8k
}
2472
2473
static CURLMcode multistate_init(struct Curl_easy *data, CURLcode *result)
2474
11.2k
{
2475
11.2k
  if(!data->state.really_alive) {
2476
11.2k
    data->state.really_alive = TRUE;
2477
11.2k
    ++data->multi->xfers_really_alive;
2478
11.2k
    if(data->multi->xfers_really_alive == 1) {
2479
11.2k
      CURLMcode mresult = multi_assess_wakeup(data->multi);
2480
11.2k
      if(mresult) {
2481
0
        failf(data, "error enabling wakeup listening: %d", mresult);
2482
0
        return mresult;
2483
0
      }
2484
11.2k
    }
2485
11.2k
  }
2486
2487
11.2k
  *result = Curl_pretransfer(data);
2488
11.2k
  if(*result)
2489
3.23k
    return CURLM_OK;
2490
2491
  /* after init, go SETUP */
2492
8.04k
  multistate(data, MSTATE_SETUP);
2493
8.04k
  Curl_pgrsTime(data, TIMER_STARTOP);
2494
8.04k
  return CURLM_CALL_MULTI_PERFORM;
2495
11.2k
}
2496
2497
static CURLMcode multistate_setup(struct Curl_easy *data)
2498
8.04k
{
2499
8.04k
  const struct curltime *pnow = Curl_pgrs_now(data);
2500
8.04k
  Curl_pgrsTimeWas(data, TIMER_STARTSINGLE, *pnow);
2501
8.04k
  if(data->set.timeout)
2502
8.04k
    Curl_expire_set(data, EXPIRE_TIMEOUT, data->set.timeout, pnow);
2503
8.04k
  if(data->set.connecttimeout)
2504
    /* Since a connection might go to pending and back to CONNECT several
2505
       times before it actually takes off, we need to set the timeout once
2506
       in SETUP before we enter CONNECT the first time. */
2507
224
    Curl_expire_set(data, EXPIRE_CONNECTTIMEOUT,
2508
224
                    data->set.connecttimeout, pnow);
2509
2510
8.04k
  multistate(data, MSTATE_CONNECT);
2511
8.04k
  return CURLM_CALL_MULTI_PERFORM;
2512
8.04k
}
2513
2514
static CURLMcode multistate_connecting(struct Curl_easy *data,
2515
                                       bool *stream_error,
2516
                                       CURLcode *result)
2517
4.83k
{
2518
4.83k
  bool connected;
2519
2520
4.83k
  if(!data->conn) {
2521
0
    DEBUGASSERT(0);
2522
0
    *result = CURLE_FAILED_INIT;
2523
0
    return CURLM_OK;
2524
0
  }
2525
4.83k
  if(!Curl_xfer_recv_is_paused(data)) {
2526
4.83k
    *result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &connected);
2527
4.83k
    if(connected && !*result) {
2528
2.05k
      if(!data->conn->bits.reuse &&
2529
2.05k
         Curl_conn_is_multiplex(data->conn, FIRSTSOCKET)) {
2530
        /* new connection, can multiplex, wake pending handles */
2531
0
        multi_schedule_pending(data->multi);
2532
0
      }
2533
2.05k
      multistate(data, MSTATE_PROTOCONNECT);
2534
2.05k
      return CURLM_CALL_MULTI_PERFORM;
2535
2.05k
    }
2536
2.78k
    else if(*result) {
2537
      /* failure detected */
2538
2.12k
      CURL_TRC_M(data, "connect failed -> %d", (int)*result);
2539
2.12k
      multi_posttransfer(data);
2540
2.12k
      multi_done(data, *result, TRUE);
2541
2.12k
      *stream_error = TRUE;
2542
2.12k
      return CURLM_OK;
2543
2.12k
    }
2544
4.83k
  }
2545
658
  return CURLM_OK;
2546
4.83k
}
2547
2548
static CURLMcode multistate_protoconnect(struct Curl_easy *data,
2549
                                         bool *stream_error,
2550
                                         CURLcode *result)
2551
2.05k
{
2552
2.05k
  bool protocol_connected = FALSE;
2553
2554
2.05k
  if(!*result && data->conn->bits.reuse) {
2555
    /* ftp seems to hang when protoconnect on reused connection since we
2556
     * handle PROTOCONNECT in general inside the filters, it seems wrong to
2557
     * restart this on a reused connection.
2558
     */
2559
0
    multistate(data, MSTATE_DO);
2560
0
    return CURLM_CALL_MULTI_PERFORM;
2561
0
  }
2562
2.05k
  if(!*result)
2563
2.05k
    *result = protocol_connect(data, &protocol_connected);
2564
2.05k
  if(!*result && !protocol_connected) {
2565
    /* switch to waiting state */
2566
0
    multistate(data, MSTATE_PROTOCONNECTING);
2567
0
    return CURLM_CALL_MULTI_PERFORM;
2568
0
  }
2569
2.05k
  else if(!*result) {
2570
    /* protocol connect has completed, go WAITDO or DO */
2571
2.05k
    multistate(data, MSTATE_DO);
2572
2.05k
    return CURLM_CALL_MULTI_PERFORM;
2573
2.05k
  }
2574
2575
  /* failure detected */
2576
0
  multi_posttransfer(data);
2577
0
  multi_done(data, *result, TRUE);
2578
0
  *stream_error = TRUE;
2579
0
  return CURLM_OK;
2580
2.05k
}
2581
2582
static CURLMcode multistate_protoconnecting(struct Curl_easy *data,
2583
                                            bool *stream_error,
2584
                                            CURLcode *result)
2585
0
{
2586
0
  bool protocol_connected = FALSE;
2587
2588
  /* protocol-specific connect phase */
2589
0
  *result = protocol_connecting(data, &protocol_connected);
2590
0
  if(!*result && protocol_connected) {
2591
    /* after the connect has completed, go WAITDO or DO */
2592
0
    multistate(data, MSTATE_DO);
2593
0
    return CURLM_CALL_MULTI_PERFORM;
2594
0
  }
2595
0
  else if(*result) {
2596
    /* failure detected */
2597
0
    multi_posttransfer(data);
2598
0
    multi_done(data, *result, TRUE);
2599
0
    *stream_error = TRUE;
2600
0
  }
2601
0
  return CURLM_OK;
2602
0
}
2603
2604
static CURLMcode multistate_doing(struct Curl_easy *data,
2605
                                  bool *stream_error,
2606
                                  CURLcode *result)
2607
3.41k
{
2608
3.41k
  bool dophase_done = FALSE;
2609
2610
  /* we continue DOING until the DO phase is complete */
2611
3.41k
  DEBUGASSERT(data->conn);
2612
3.41k
  *result = protocol_doing(data, &dophase_done);
2613
3.41k
  if(!*result) {
2614
1.89k
    if(dophase_done) {
2615
      /* after DO, go DO_DONE or DO_MORE */
2616
135
      multistate(data, data->conn->bits.do_more ?
2617
135
                 MSTATE_DOING_MORE : MSTATE_DID);
2618
135
      return CURLM_CALL_MULTI_PERFORM;
2619
135
    } /* dophase_done */
2620
1.89k
  }
2621
1.52k
  else {
2622
    /* failure detected */
2623
1.52k
    multi_posttransfer(data);
2624
1.52k
    multi_done(data, *result, FALSE);
2625
1.52k
    *stream_error = TRUE;
2626
1.52k
  }
2627
3.27k
  return CURLM_OK;
2628
3.41k
}
2629
2630
static CURLMcode multistate_doing_more(struct Curl_easy *data,
2631
                                       bool *stream_error,
2632
                                       CURLcode *result)
2633
0
{
2634
0
  domore control;
2635
2636
  /*
2637
   * When we are connected, DOING MORE and then go DID
2638
   */
2639
0
  DEBUGASSERT(data->conn);
2640
0
  *result = multi_do_more(data, &control);
2641
2642
0
  if(!*result) {
2643
0
    if(control != DOMORE_INCOMPLETE) {
2644
      /* if DONE, advance to DO_DONE
2645
         if GOBACK, go back to DOING */
2646
0
      multistate(data, control == DOMORE_DONE ? MSTATE_DID : MSTATE_DOING);
2647
0
      return CURLM_CALL_MULTI_PERFORM;
2648
0
    }
2649
    /* else
2650
       stay in DO_MORE */
2651
0
  }
2652
0
  else {
2653
    /* failure detected */
2654
0
    multi_posttransfer(data);
2655
0
    multi_done(data, *result, FALSE);
2656
0
    *stream_error = TRUE;
2657
0
  }
2658
0
  return CURLM_OK;
2659
0
}
2660
2661
static CURLMcode multistate_did(struct Curl_multi *multi,
2662
                                struct Curl_easy *data)
2663
135
{
2664
135
  DEBUGASSERT(data->conn);
2665
135
  if(data->conn->bits.multiplex)
2666
    /* Check if we can move pending requests to send pipe */
2667
0
    multi_schedule_pending(multi); /* multiplexed */
2668
2669
  /* Only perform the transfer if there is a good socket to work with.
2670
     Having both BAD is a signal to skip immediately to DONE */
2671
135
  if(CONN_SOCK_IDX_VALID(data->conn->recv_idx) ||
2672
135
     CONN_SOCK_IDX_VALID(data->conn->send_idx)) {
2673
135
    multistate(data, MSTATE_PERFORMING);
2674
    /* Do not return CURLM_CALL_MULTI_PERFORM to give other transfers
2675
     * a chance to send off their requests.
2676
     * Note: Some SFTP handlers do not seem to like this.
2677
     *       Restrict it to HTTP families. */
2678
135
    return ((multi->xfers_alive > 1) &&
2679
0
            (data->conn->scheme->protocol & PROTO_FAMILY_HTTP)) ?
2680
135
           CURLM_OK : CURLM_CALL_MULTI_PERFORM;
2681
135
  }
2682
0
  else {
2683
0
#ifndef CURL_DISABLE_FTP
2684
0
    if(data->state.wildcardmatch &&
2685
0
       ((data->conn->scheme->flags & PROTOPT_WILDCARD) == 0)) {
2686
0
      data->wildcard->state = CURLWC_DONE;
2687
0
    }
2688
0
#endif
2689
0
    multistate(data, MSTATE_DONE);
2690
0
    return CURLM_CALL_MULTI_PERFORM;
2691
0
  }
2692
135
}
2693
2694
static CURLMcode multistate_done(struct Curl_easy *data, CURLcode *presult)
2695
141
{
2696
141
  if(data->conn) {
2697
141
    CURLcode result;
2698
2699
    /* post-transfer command */
2700
141
    result = multi_done(data, *presult, FALSE);
2701
2702
    /* allow a previously set error code take precedence */
2703
141
    if(!(*presult))
2704
141
      *presult = result;
2705
141
  }
2706
2707
141
#ifndef CURL_DISABLE_FTP
2708
141
  if(data->state.wildcardmatch) {
2709
0
    if(data->wildcard->state != CURLWC_DONE) {
2710
      /* if a wildcard is set and we are not ending -> lets start again
2711
         with MSTATE_INIT */
2712
0
      multistate(data, MSTATE_INIT);
2713
0
      return CURLM_CALL_MULTI_PERFORM;
2714
0
    }
2715
0
  }
2716
141
#endif
2717
  /* after we have DONE what we are supposed to do, go COMPLETED, and
2718
     it does not matter what the multi_done() returned! */
2719
141
  multistate(data, MSTATE_COMPLETED);
2720
141
  return CURLM_CALL_MULTI_PERFORM;
2721
141
}
2722
2723
static CURLMcode multi_runsingle(struct Curl_multi *multi,
2724
                                 struct Curl_easy *data,
2725
                                 struct Curl_sigpipe_ctx *sigpipe_ctx)
2726
26.4k
{
2727
26.4k
  CURLMcode mresult = CURLM_OK;
2728
26.4k
  CURLcode result = CURLE_OK;
2729
26.4k
  const struct curltime *pnow = NULL;
2730
2731
26.4k
  if(multi->dead) {
2732
    /* a multi-level callback returned error before, meaning every individual
2733
     transfer now has failed */
2734
0
    result = CURLE_ABORTED_BY_CALLBACK;
2735
0
    multi_posttransfer(data);
2736
0
    multi_done(data, result, FALSE);
2737
0
    multistate(data, MSTATE_COMPLETED);
2738
0
  }
2739
2740
26.4k
  multi_warn_debug(multi, data);
2741
2742
  /* transfer runs now, clear the dirty bit. This may be set
2743
   * again during processing, triggering a re-run later. */
2744
26.4k
  Curl_uint32_bset_remove(&multi->dirty, data->mid);
2745
2746
26.4k
  if(data == multi->admin) {
2747
13.2k
#ifdef ENABLE_INTERNAL_WAKEUP
2748
    /* Consume any pending wakeup signals before processing.
2749
     * This is necessary for event based processing. See #21547 */
2750
13.2k
    (void)Curl_wakeup_consume(multi->wakeup_internal, TRUE);
2751
13.2k
#endif
2752
13.2k
#ifdef USE_RESOLV_THREADED
2753
13.2k
    Curl_async_thrdd_multi_process(multi);
2754
13.2k
#endif
2755
13.2k
    Curl_cshutdn_perform(&multi->cshutdn, multi->admin, sigpipe_ctx);
2756
13.2k
    goto out;
2757
13.2k
  }
2758
2759
13.2k
  sigpipe_apply(data, sigpipe_ctx);
2760
40.1k
  do {
2761
    /* A "stream" here is a logical stream if the protocol can handle that
2762
       (HTTP/2), or the full connection for older protocols */
2763
40.1k
    bool stream_error = FALSE;
2764
40.1k
    mresult = CURLM_OK;
2765
40.1k
    pnow = NULL;
2766
2767
40.1k
    if(multi_ischanged(multi, TRUE)) {
2768
0
      CURL_TRC_M(data, "multi changed, check CONNECT_PEND queue");
2769
0
      multi_schedule_pending(multi); /* multiplexed */
2770
0
    }
2771
2772
40.1k
    if(data->mstate > MSTATE_CONNECT &&
2773
12.7k
       data->mstate < MSTATE_COMPLETED) {
2774
      /* Make sure we set the connection's current owner */
2775
12.7k
      DEBUGASSERT(data->conn);
2776
12.7k
      if(!data->conn) {
2777
0
        mresult = CURLM_INTERNAL_ERROR;
2778
0
        goto out;
2779
0
      }
2780
12.7k
    }
2781
2782
    /* Wait for the connect state as only then is the start time stored, but
2783
       we must not check already completed handles */
2784
40.1k
    if((data->mstate >= MSTATE_CONNECT) && (data->mstate < MSTATE_COMPLETED)) {
2785
20.8k
      pnow = Curl_pgrs_now(data);
2786
20.8k
      if(multi_handle_timeout(data, pnow, &stream_error, &result))
2787
        /* Skip the statemachine and go directly to error handling section. */
2788
6
        goto statemachine_end;
2789
20.8k
      pnow = NULL;
2790
20.8k
    }
2791
2792
40.1k
    switch(data->mstate) {
2793
11.2k
    case MSTATE_INIT:
2794
      /* Transitional state. init this transfer. A handle never comes back to
2795
         this state. */
2796
11.2k
      mresult = multistate_init(data, &result);
2797
11.2k
      break;
2798
2799
8.04k
    case MSTATE_SETUP:
2800
      /* Transitional state. Setup things for a new transfer. The handle
2801
         can come back to this state on a redirect. */
2802
8.04k
      mresult = multistate_setup(data);
2803
8.04k
      break;
2804
2805
8.04k
    case MSTATE_CONNECT:
2806
8.04k
      mresult = multistate_connect(multi, data, &result);
2807
8.04k
      break;
2808
2809
4.83k
    case MSTATE_CONNECTING:
2810
      /* awaiting a completion of an asynch TCP connect */
2811
4.83k
      mresult = multistate_connecting(data, &stream_error, &result);
2812
4.83k
      break;
2813
2814
2.05k
    case MSTATE_PROTOCONNECT:
2815
2.05k
      mresult = multistate_protoconnect(data, &stream_error, &result);
2816
2.05k
      break;
2817
2818
0
    case MSTATE_PROTOCONNECTING:
2819
      /* protocol-specific connect phase */
2820
0
      mresult = multistate_protoconnecting(data, &stream_error, &result);
2821
0
      break;
2822
2823
2.05k
    case MSTATE_DO:
2824
2.05k
      mresult = multistate_do(data, &stream_error, &result);
2825
2.05k
      break;
2826
2827
3.41k
    case MSTATE_DOING:
2828
      /* we continue DOING until the DO phase is complete */
2829
3.41k
      mresult = multistate_doing(data, &stream_error, &result);
2830
3.41k
      break;
2831
2832
0
    case MSTATE_DOING_MORE:
2833
      /*
2834
       * When we are connected, DOING MORE and then go DID
2835
       */
2836
0
      mresult = multistate_doing_more(data, &stream_error, &result);
2837
0
      break;
2838
2839
135
    case MSTATE_DID:
2840
135
      mresult = multistate_did(multi, data);
2841
135
      break;
2842
2843
0
    case MSTATE_RATELIMITING: /* limit-rate exceeded in either direction */
2844
0
      mresult = multistate_ratelimiting(data, &result);
2845
0
      break;
2846
2847
135
    case MSTATE_PERFORMING:
2848
135
      mresult = multistate_performing(data, &stream_error, &result);
2849
135
      break;
2850
2851
141
    case MSTATE_DONE:
2852
141
      mresult = multistate_done(data, &result);
2853
141
      break;
2854
2855
0
    case MSTATE_COMPLETED:
2856
0
      break;
2857
2858
0
    case MSTATE_PENDING:
2859
0
    case MSTATE_MSGSENT:
2860
      /* handles in these states should NOT be in this list */
2861
0
      break;
2862
2863
0
    default:
2864
0
      mresult = CURLM_INTERNAL_ERROR;
2865
0
      goto out;
2866
40.1k
    }
2867
2868
40.1k
    if(data->mstate >= MSTATE_CONNECT &&
2869
28.8k
       data->mstate < MSTATE_DO &&
2870
20.9k
       mresult != CURLM_CALL_MULTI_PERFORM &&
2871
6.53k
       !multi_ischanged(multi, FALSE)) {
2872
      /* We now handle stream timeouts if and only if this will be the last
2873
       * loop iteration. We only check this on the last iteration to ensure
2874
       * that if we know we have additional work to do immediately
2875
       * (i.e. CURLM_CALL_MULTI_PERFORM == TRUE) then we should do that before
2876
       * declaring the connection timed out as we may almost have a completed
2877
       * connection. */
2878
4.41k
      pnow = Curl_pgrs_now(data);
2879
4.41k
      multi_handle_timeout(data, pnow, &stream_error, &result);
2880
4.41k
    }
2881
2882
40.1k
statemachine_end:
2883
40.1k
    if(!pnow)
2884
35.7k
      pnow = Curl_pgrs_now(data);
2885
40.1k
    result = is_finished(multi, data, pnow, stream_error, result);
2886
40.1k
    if(result)
2887
10.6k
      mresult = CURLM_CALL_MULTI_PERFORM;
2888
2889
40.1k
    if(MSTATE_COMPLETED == data->mstate) {
2890
10.8k
      handle_completed(multi, data, result);
2891
10.8k
      mresult = CURLM_OK;
2892
10.8k
      goto out;
2893
10.8k
    }
2894
40.1k
  } while((mresult == CURLM_CALL_MULTI_PERFORM) ||
2895
2.40k
          multi_ischanged(multi, FALSE));
2896
2897
26.4k
out:
2898
26.4k
  data->result = result;
2899
26.4k
  return mresult;
2900
13.2k
}
2901
2902
static CURLMcode multi_perform(struct Curl_multi *multi,
2903
                               int *running_handles)
2904
13.2k
{
2905
13.2k
  CURLMcode returncode = CURLM_OK;
2906
13.2k
  struct curltime start;
2907
13.2k
  uint32_t mid;
2908
13.2k
  struct Curl_sigpipe_ctx sigpipe_ctx;
2909
2910
13.2k
  curlx_pnow(&start);
2911
13.2k
  sigpipe_init(&sigpipe_ctx);
2912
2913
13.2k
  if(Curl_uint32_bset_first(&multi->process, &mid)) {
2914
13.2k
    CURL_TRC_M(multi->admin, "multi_perform(running=%u)",
2915
13.2k
               multi_xfers_running(multi));
2916
26.4k
    do {
2917
26.4k
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
2918
26.4k
      CURLMcode mresult;
2919
26.4k
      if(!data) {
2920
0
        DEBUGASSERT(0);
2921
0
        Curl_uint32_bset_remove(&multi->process, mid);
2922
0
        Curl_uint32_bset_remove(&multi->dirty, mid);
2923
0
        continue;
2924
0
      }
2925
26.4k
      mresult = multi_runsingle(multi, data, &sigpipe_ctx);
2926
26.4k
      if(mresult)
2927
0
        returncode = mresult;
2928
26.4k
    } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
2929
13.2k
  }
2930
13.2k
  sigpipe_restore(&sigpipe_ctx);
2931
2932
13.2k
  if(multi_ischanged(multi, TRUE))
2933
3.80k
    multi_schedule_pending(multi);
2934
2935
13.2k
  if(!returncode && CURL_MNTFY_HAS_ENTRIES(multi))
2936
0
    returncode = Curl_mntfy_dispatch_all(multi);
2937
2938
  /*
2939
   * Remove all expired timers from the splay since handles are dealt
2940
   * with unconditionally by this function and curl_multi_timeout() requires
2941
   * that already passed/handled expire times are removed from the splay.
2942
   *
2943
   * It is important that the 'now' value is set at the entry of this function
2944
   * and not for the current time as it may have ticked a little while since
2945
   * then and then we risk this loop to remove timers that actually have not
2946
   * been handled!
2947
   */
2948
13.2k
  while(Curl_timeouts_remove_expired(&multi->timeouts, &start, &mid)) {
2949
    /* the removed may have another timeout in queue */
2950
11
    struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
2951
11
    if(!data) {
2952
0
      DEBUGASSERT(0);
2953
0
      continue;
2954
0
    }
2955
11
    (void)add_next_timeout(&start, multi, data);
2956
11
    if(data->mstate == MSTATE_PENDING) {
2957
0
      bool stream_unused;
2958
0
      CURLcode result_unused;
2959
0
      if(multi_handle_timeout(data, Curl_pgrs_now(data),
2960
0
                              &stream_unused, &result_unused)) {
2961
0
        infof(data, "PENDING handle timeout");
2962
0
        move_pending_to_connect(multi, data);
2963
0
      }
2964
0
    }
2965
11
  }
2966
2967
13.2k
  if(running_handles) {
2968
13.2k
    uint32_t running = multi_xfers_running(multi);
2969
13.2k
    *running_handles = (running < INT_MAX) ? (int)running : INT_MAX;
2970
13.2k
  }
2971
2972
13.2k
  if(CURLM_OK >= returncode)
2973
13.2k
    returncode = Curl_update_timer(multi);
2974
2975
13.2k
  return returncode;
2976
13.2k
}
2977
2978
CURLMcode curl_multi_perform(CURLM *m, int *running_handles)
2979
13.2k
{
2980
13.2k
  struct Curl_mapi_guard guard;
2981
13.2k
  CURLMcode mresult;
2982
2983
13.2k
  if(CURL_MAPI_ENTER(&guard, m, multi_perform, &mresult)) {
2984
13.2k
    mresult = multi_perform(m, running_handles);
2985
13.2k
  }
2986
13.2k
  CURL_MAPI_LEAVE(&guard);
2987
13.2k
  return mresult;
2988
13.2k
}
2989
2990
CURLMcode curl_multi_cleanup(CURLM *m)
2991
11.2k
{
2992
11.2k
  struct Curl_mapi_guard guard;
2993
11.2k
  CURLMcode mresult;
2994
2995
11.2k
  if(CURL_MAPI_ENTER(&guard, m, multi_cleanup, &mresult)) {
2996
11.2k
    struct Curl_multi *multi = m;
2997
11.2k
    void *entry;
2998
11.2k
    uint32_t mid;
2999
3000
    /* First remove all remaining easy handles,
3001
     * close internal ones. admin handle is special */
3002
11.2k
    if(Curl_uint32_tbl_first(&multi->xfers, &mid, &entry)) {
3003
11.2k
      do {
3004
11.2k
        struct Curl_easy *data = entry;
3005
11.2k
        if(!GOOD_EASY_HANDLE(data)) {
3006
0
          mresult = CURLM_BAD_HANDLE;
3007
0
          goto out;
3008
0
        }
3009
3010
11.2k
#ifdef DEBUGBUILD
3011
11.2k
        if(mid != data->mid) {
3012
0
          CURL_TRC_M(data, "multi_cleanup: still present with mid=%u, "
3013
0
                     "but unexpected data->mid=%u\n", mid, data->mid);
3014
0
          DEBUGASSERT(0);
3015
0
        }
3016
11.2k
#endif
3017
3018
11.2k
        if(data == multi->admin)
3019
11.2k
          continue;
3020
3021
0
        if(!data->state.done && data->conn)
3022
          /* if DONE was never called for this handle */
3023
0
          (void)multi_done(data, CURLE_OK, TRUE);
3024
3025
0
        data->multi = NULL; /* clear the association */
3026
0
        Curl_uint32_tbl_remove(&multi->xfers, mid);
3027
0
        data->mid = UINT32_MAX;
3028
3029
#ifdef USE_LIBPSL
3030
        if(data->psl == &multi->psl)
3031
          data->psl = NULL;
3032
#endif
3033
0
        if(data->state.internal)
3034
0
          Curl_close(&data);
3035
11.2k
      } while(Curl_uint32_tbl_next(&multi->xfers, mid, &mid, &entry));
3036
11.2k
    }
3037
3038
11.2k
#ifdef USE_RESOLV_THREADED
3039
11.2k
    Curl_async_thrdd_multi_destroy(multi, !multi->quick_exit);
3040
11.2k
#endif
3041
11.2k
    Curl_cpool_destroy(&multi->cpool, multi->admin);
3042
11.2k
    Curl_cshutdn_destroy(&multi->cshutdn, multi->admin);
3043
11.2k
    if(multi->admin) {
3044
11.2k
      CURL_TRC_M(multi->admin, "multi_cleanup, closing admin handle, done");
3045
11.2k
      multi->admin->multi = NULL;
3046
11.2k
      Curl_uint32_tbl_remove(&multi->xfers, multi->admin->mid);
3047
11.2k
      Curl_close(&multi->admin);
3048
11.2k
    }
3049
3050
11.2k
    multi->magic = 0; /* not good anymore */
3051
3052
11.2k
    Curl_multi_ev_cleanup(multi);
3053
11.2k
    Curl_hash_destroy(&multi->proto_hash);
3054
11.2k
    Curl_dnscache_destroy(&multi->dnscache);
3055
11.2k
    Curl_psl_destroy(&multi->psl);
3056
11.2k
#ifdef USE_SSL
3057
11.2k
    Curl_ssl_scache_destroy(multi->ssl_scache);
3058
11.2k
#endif
3059
3060
#ifdef USE_WINSOCK
3061
    WSACloseEvent(multi->wsa_event);
3062
#endif
3063
11.2k
#ifdef ENABLE_WAKEUP
3064
11.2k
  Curl_wakeup_destroy(multi->wakeup_pair);
3065
11.2k
#endif
3066
11.2k
#ifdef ENABLE_INTERNAL_WAKEUP
3067
11.2k
  Curl_wakeup_destroy(multi->wakeup_internal);
3068
11.2k
#endif
3069
3070
11.2k
    multi_xfer_bufs_free(multi);
3071
11.2k
    Curl_mntfy_cleanup(multi);
3072
11.2k
#ifdef DEBUGBUILD
3073
11.2k
    if(Curl_uint32_tbl_count(&multi->xfers)) {
3074
0
      multi_xfer_tbl_dump(multi);
3075
0
      DEBUGASSERT(0);
3076
0
    }
3077
11.2k
#endif
3078
11.2k
    Curl_uint32_bset_destroy(&multi->process);
3079
11.2k
    Curl_uint32_bset_destroy(&multi->dirty);
3080
11.2k
    Curl_uint32_bset_destroy(&multi->pending);
3081
11.2k
    Curl_uint32_bset_destroy(&multi->msgsent);
3082
11.2k
    Curl_uint32_tbl_destroy(&multi->xfers);
3083
11.2k
    curlx_memzero(multi, sizeof(*multi));
3084
11.2k
    curlx_free(multi);
3085
3086
11.2k
    mresult = CURLM_OK;
3087
11.2k
  }
3088
11.2k
out:
3089
11.2k
  CURL_MAPI_LEAVE(&guard);
3090
11.2k
  return mresult;
3091
11.2k
}
3092
3093
/*
3094
 * curl_multi_info_read()
3095
 *
3096
 * This function is the primary way for a multi/multi_socket application to
3097
 * figure out if a transfer has ended.
3098
 */
3099
3100
CURLMsg *curl_multi_info_read(CURLM *m, int *msgs_in_queue)
3101
0
{
3102
0
  struct Curl_mapi_guard guard;
3103
0
  CURLMsg *msg_result = NULL;
3104
3105
0
  *msgs_in_queue = 0; /* default to none */
3106
0
  if(CURL_MAPI_ENTER(&guard, m, multi_info_read, NULL)) {
3107
0
    struct Curl_multi *multi = m;
3108
0
    uint32_t mid;
3109
0
    if(Curl_uint32_bset_first(&multi->msgsent, &mid)) {
3110
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3111
3112
0
      DEBUGASSERT(data);
3113
0
      Curl_uint32_bset_remove(&multi->msgsent, mid);
3114
0
      *msgs_in_queue =
3115
0
        curlx_uztosi(Curl_uint32_bset_count(&multi->msgsent));
3116
0
      if(data)
3117
0
        msg_result = &data->msg;
3118
0
    }
3119
0
  }
3120
0
  CURL_MAPI_LEAVE(&guard);
3121
0
  return msg_result;
3122
0
}
3123
3124
void Curl_multi_will_close(struct Curl_easy *data, curl_socket_t s)
3125
4.25k
{
3126
4.25k
  if(data) {
3127
4.25k
    struct Curl_multi *multi = data->multi;
3128
4.25k
    if(multi) {
3129
4.25k
      CURL_TRC_M(data, "Curl_multi_will_close fd=%" FMT_SOCKET_T, s);
3130
4.25k
      Curl_multi_ev_socket_done(multi, data, s);
3131
4.25k
    }
3132
4.25k
  }
3133
4.25k
}
3134
3135
static void multi_timeouts_init(struct Curl_easy *data)
3136
30.5k
{
3137
30.5k
  data->state.timeouts.first = EXPIRE_LAST;
3138
30.5k
  data->state.timeouts.splaynode.registered = FALSE;
3139
30.5k
}
3140
3141
/*
3142
 * Each Curl_easy has a list of timeouts. The add_next_timeout() is called
3143
 * when it has been removed from the splay tree because the timeout has
3144
 * expired. This function is then to advance in the list to pick the next
3145
 * timeout to use (skip the already expired ones) and add this node back to
3146
 * the splay tree again.
3147
 *
3148
 * The splay tree only has each Curl_easy as a single node and the nearest
3149
 * timeout is used to sort it on.
3150
 */
3151
static CURLMcode add_next_timeout(const struct curltime *pnow,
3152
                                  struct Curl_multi *multi,
3153
                                  struct Curl_easy *data)
3154
11
{
3155
11
  struct expire_timers *timeouts = &data->state.timeouts;
3156
11
  timediff_t now_us = Curl_timeouts_offset_us(&multi->timeouts, pnow);
3157
3158
22
  while(timeouts->first < EXPIRE_LAST) {
3159
22
    if(timeouts->offset_us[timeouts->first] <= now_us)  /* already expired */
3160
11
      timeouts->first = timeouts->next[timeouts->first];
3161
11
    else /* timeouts are sorted, first is first in the future now */
3162
11
      break;
3163
22
  }
3164
3165
11
  if(timeouts->first < EXPIRE_LAST) {
3166
    /* Insert this node again into the splay. Keep the timer in the list in
3167
       case we need to recompute future timers. */
3168
11
    Curl_timeouts_add(&multi->timeouts, data,
3169
11
                      timeouts->offset_us[timeouts->first]);
3170
11
  }
3171
11
  return CURLM_OK;
3172
11
}
3173
3174
static void multi_mark_expired_as_dirty(struct Curl_multi *multi,
3175
                                        const struct curltime *ts)
3176
0
{
3177
0
  struct Curl_easy *data = NULL;
3178
0
  uint32_t mid;
3179
3180
  /*
3181
   * The loop following here will go on as long as there are expire-times left
3182
   * to process (compared to `ts`) in the splay and 'data' will be
3183
   * re-assigned for every expired handle we deal with.
3184
   */
3185
0
  while(Curl_timeouts_remove_expired(&multi->timeouts, ts, &mid)) {
3186
    /* Check if there is one (more) expired timer to deal with! This function
3187
       extracts a matching node if there is one */
3188
0
    data = Curl_multi_get_easy(multi, mid);
3189
0
    if(!data) {
3190
0
      DEBUGASSERT(0);
3191
0
      continue;
3192
0
    }
3193
0
#ifdef CURLVERBOSE
3194
0
    if(CURL_TRC_TIMER_is_verbose(data)) {
3195
0
      if(data->state.timeouts.first < EXPIRE_LAST) {
3196
0
        CURL_TRC_TIMER(data, data->state.timeouts.first, "has expired");
3197
0
      }
3198
0
    }
3199
0
#endif
3200
0
    (void)add_next_timeout(ts, multi, data);
3201
0
    Curl_multi_mark_dirty(data);
3202
0
  }
3203
0
}
3204
3205
static CURLMcode multi_run_dirty(struct Curl_multi *multi,
3206
                                 struct Curl_sigpipe_ctx *sigpipe_ctx,
3207
                                 uint32_t *pnum)
3208
0
{
3209
0
  CURLMcode mresult = CURLM_OK;
3210
0
  uint32_t mid;
3211
3212
0
  *pnum = 0;
3213
0
  if(Curl_uint32_bset_first(&multi->dirty, &mid)) {
3214
0
    do {
3215
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3216
0
      if(data) {
3217
0
        CURL_TRC_M(data, "multi_run_dirty");
3218
3219
0
        if(!Curl_uint32_bset_contains(&multi->process, mid)) {
3220
0
          if(Curl_uint32_bset_contains(&multi->pending, mid)) {
3221
            /* Something happened on the pending transfer, act on it. */
3222
0
            move_pending_to_connect(multi, data);
3223
0
          }
3224
0
          else {
3225
            /* We are no longer processing this transfer */
3226
0
            Curl_uint32_bset_remove(&multi->dirty, mid);
3227
0
            continue;
3228
0
          }
3229
0
        }
3230
3231
0
        (*pnum)++;
3232
        /* runsingle() clears the dirty mid */
3233
0
        mresult = multi_runsingle(multi, data, sigpipe_ctx);
3234
3235
0
        if(CURLM_OK >= mresult) {
3236
          /* reassess event handling of data */
3237
0
          mresult = Curl_multi_ev_assess_xfer(multi, data);
3238
0
          if(mresult)
3239
0
            goto out;
3240
0
        }
3241
0
      }
3242
0
      else {
3243
0
        CURL_TRC_M(multi->admin, "multi_run_dirty, %u no longer found", mid);
3244
0
        Curl_uint32_bset_remove(&multi->dirty, mid);
3245
0
      }
3246
0
    } while(Curl_uint32_bset_next(&multi->dirty, mid, &mid));
3247
0
  }
3248
3249
0
out:
3250
0
  return mresult;
3251
0
}
3252
3253
static CURLMcode multi_socket(struct Curl_multi *multi,
3254
                              bool checkall,
3255
                              curl_socket_t s,
3256
                              int ev_bitmask,
3257
                              int *running_handles)
3258
0
{
3259
0
  CURLMcode mresult = CURLM_OK;
3260
0
  struct Curl_sigpipe_ctx pipe_ctx;
3261
0
  struct curltime now;
3262
0
  uint32_t run_xfers;
3263
3264
0
  (void)ev_bitmask;
3265
0
  sigpipe_init(&pipe_ctx);
3266
3267
0
  if(checkall) {
3268
    /* *perform() deals with running_handles on its own */
3269
0
    mresult = multi_perform(multi, running_handles);
3270
3271
0
    if(mresult != CURLM_BAD_HANDLE) {
3272
      /* Reassess event status of all active transfers, but keep any error
3273
         from multi_perform() */
3274
0
      CURLMcode mresult2 =
3275
0
        Curl_multi_ev_assess_xfer_bset(multi, &multi->process);
3276
0
      if(!mresult)
3277
0
        mresult = mresult2;
3278
0
    }
3279
0
    goto out;
3280
0
  }
3281
3282
0
  if(s != CURL_SOCKET_TIMEOUT) {
3283
    /* Mark all transfers of that socket as dirty */
3284
0
    Curl_multi_ev_dirty_xfers(multi, s);
3285
0
  }
3286
0
  else {
3287
    /* Asked to run due to time-out. Clear the 'last_expire_ts' variable to
3288
       force Curl_update_timer() to trigger a callback to the app again even
3289
       if the same timeout is still the one to run after this call. That
3290
       handles the case when the application asks libcurl to run the timeout
3291
       prematurely. */
3292
0
    multi->last_expire_offset_us = 0;
3293
3294
    /* Applications may set `socket_cb` *after* having added transfers
3295
     * first. *Then* kick off processing with a
3296
     * curl_multi_socket_action(TIMEOUT) afterwards. Make sure our
3297
     * admin handle registers its pollset with the callbacks present. */
3298
0
    mresult = multi_assess_wakeup(multi);
3299
0
    if(mresult)
3300
0
      goto out;
3301
0
  }
3302
3303
0
  curlx_pnow(&now);
3304
0
  multi_mark_expired_as_dirty(multi, &now);
3305
0
  mresult = multi_run_dirty(multi, &pipe_ctx, &run_xfers);
3306
0
  if(mresult)
3307
0
    goto out;
3308
3309
0
  if(run_xfers) {
3310
    /* Running transfers takes time. With a new timestamp, we might catch
3311
     * other expires which are due now. Instead of telling the application
3312
     * to set a 0 timeout and call us again, we run them here.
3313
     * Do that only once or it might be unfair to transfers on other
3314
     * sockets. */
3315
0
    curlx_pnow(&now);
3316
0
    multi_mark_expired_as_dirty(multi, &now);
3317
0
    mresult = multi_run_dirty(multi, &pipe_ctx, &run_xfers);
3318
0
  }
3319
3320
0
out:
3321
0
  sigpipe_restore(&pipe_ctx);
3322
3323
0
  if(multi_ischanged(multi, TRUE))
3324
0
    multi_schedule_pending(multi);
3325
3326
0
  if(!mresult && CURL_MNTFY_HAS_ENTRIES(multi))
3327
0
    mresult = Curl_mntfy_dispatch_all(multi);
3328
3329
0
  if(running_handles) {
3330
0
    uint32_t running = multi_xfers_running(multi);
3331
0
    *running_handles = (running < INT_MAX) ? (int)running : INT_MAX;
3332
0
  }
3333
3334
0
  if(CURLM_OK >= mresult)
3335
0
    mresult = Curl_update_timer(multi);
3336
0
  return mresult;
3337
0
}
3338
3339
#undef curl_multi_setopt
3340
CURLMcode curl_multi_setopt(CURLM *m, CURLMoption option, ...)
3341
0
{
3342
0
  struct Curl_mapi_guard guard;
3343
0
  CURLMcode mresult = CURLM_OK;
3344
3345
0
  if(CURL_MAPI_ENTER(&guard, m, multi_setopt, &mresult)) {
3346
0
    struct Curl_multi *multi = m;
3347
0
    va_list param;
3348
0
    unsigned long uarg;
3349
0
    size_t szarg;
3350
3351
0
    va_start(param, option);
3352
3353
0
    switch(option) {
3354
0
    case CURLMOPT_SOCKETFUNCTION:
3355
0
      multi->socket_cb = va_arg(param, curl_socket_callback);
3356
0
      break;
3357
0
    case CURLMOPT_SOCKETDATA:
3358
0
      multi->socket_userp = va_arg(param, void *);
3359
0
      break;
3360
0
    case CURLMOPT_PUSHFUNCTION:
3361
0
      multi->push_cb = va_arg(param, curl_push_callback);
3362
0
      break;
3363
0
    case CURLMOPT_PUSHDATA:
3364
0
      multi->push_userp = va_arg(param, void *);
3365
0
      break;
3366
0
    case CURLMOPT_PIPELINING:
3367
0
      multi->multiplexing = va_arg(param, long) & CURLPIPE_MULTIPLEX ? 1 : 0;
3368
0
      break;
3369
0
    case CURLMOPT_TIMERFUNCTION:
3370
0
      multi->timer_cb = va_arg(param, curl_multi_timer_callback);
3371
0
      break;
3372
0
    case CURLMOPT_TIMERDATA:
3373
0
      multi->timer_userp = va_arg(param, void *);
3374
0
      break;
3375
0
    case CURLMOPT_MAXCONNECTS:
3376
0
      uarg = va_arg(param, unsigned long);
3377
0
      if(uarg <= UINT32_MAX)
3378
0
        multi->maxconnects = (uint32_t)uarg;
3379
0
      break;
3380
0
    case CURLMOPT_MAX_HOST_CONNECTIONS:
3381
0
      if(!curlx_sltouz(va_arg(param, long), &szarg))
3382
0
        mresult = CURLM_BAD_FUNCTION_ARGUMENT;
3383
0
      multi->max_host_connections = (szarg < UINT32_MAX) ?
3384
0
                                    (uint32_t)szarg : UINT32_MAX;
3385
0
      break;
3386
0
    case CURLMOPT_MAX_TOTAL_CONNECTIONS:
3387
0
      if(!curlx_sltouz(va_arg(param, long), &szarg))
3388
0
        mresult = CURLM_BAD_FUNCTION_ARGUMENT;
3389
0
      multi->max_total_connections = (szarg < UINT32_MAX) ?
3390
0
                                     (uint32_t)szarg : UINT32_MAX;
3391
0
      break;
3392
      /* options formerly used for pipelining */
3393
0
    case CURLMOPT_MAX_PIPELINE_LENGTH:
3394
0
      break;
3395
0
    case CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE:
3396
0
      break;
3397
0
    case CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE:
3398
0
      break;
3399
0
    case CURLMOPT_PIPELINING_SITE_BL:
3400
0
      break;
3401
0
    case CURLMOPT_PIPELINING_SERVER_BL:
3402
0
      break;
3403
0
    case CURLMOPT_MAX_CONCURRENT_STREAMS: {
3404
0
      if(!curlx_sltouz(va_arg(param, long), &szarg) ||
3405
0
         !szarg || (szarg > (size_t)INT_MAX)) /* preserve previous cutoff */
3406
0
        multi->max_concurrent_streams = 100;
3407
0
      else
3408
0
        multi->max_concurrent_streams = (szarg < UINT32_MAX) ?
3409
0
                                       (uint32_t)szarg : UINT32_MAX;
3410
0
      break;
3411
0
    }
3412
0
    case CURLMOPT_NETWORK_CHANGED: {
3413
0
      long val = va_arg(param, long);
3414
0
      if(val & CURLMNWC_CLEAR_ALL)
3415
        /* In the beginning, all values available to set were 1 by mistake. We
3416
           converted this to mean "all", thus setting all the bits
3417
           automatically */
3418
0
        val = CURLMNWC_CLEAR_DNS | CURLMNWC_CLEAR_CONNS;
3419
0
      if(val & CURLMNWC_CLEAR_DNS) {
3420
0
        Curl_dnscache_clear(multi->admin);
3421
0
      }
3422
0
      if(val & CURLMNWC_CLEAR_CONNS) {
3423
0
        Curl_cpool_nw_changed(&multi->cpool, multi->admin);
3424
0
      }
3425
0
      break;
3426
0
    }
3427
0
    case CURLMOPT_NOTIFYFUNCTION:
3428
0
      multi->ntfy.ntfy_cb = va_arg(param, curl_notify_callback);
3429
0
      break;
3430
0
    case CURLMOPT_NOTIFYDATA:
3431
0
      multi->ntfy.ntfy_cb_data = va_arg(param, void *);
3432
0
      break;
3433
0
    case CURLMOPT_RESOLVE_THREADS_MAX:
3434
0
#ifdef USE_RESOLV_THREADED
3435
0
      uarg = va_arg(param, long);
3436
0
      if((uarg <= 0) || (uarg > UINT32_MAX))
3437
0
        mresult = CURLM_BAD_FUNCTION_ARGUMENT;
3438
0
      else {
3439
0
        CURLcode result = Curl_async_thrdd_multi_set_props(
3440
0
          multi, 0, (uint32_t)uarg, 2000);
3441
0
        switch(result) {
3442
0
        case CURLE_OK:
3443
0
          mresult = CURLM_OK;
3444
0
          break;
3445
0
        case CURLE_BAD_FUNCTION_ARGUMENT:
3446
0
          mresult = CURLM_BAD_FUNCTION_ARGUMENT;
3447
0
          break;
3448
0
        case CURLE_OUT_OF_MEMORY:
3449
0
          mresult = CURLM_OUT_OF_MEMORY;
3450
0
          break;
3451
0
        default:
3452
0
          mresult = CURLM_INTERNAL_ERROR;
3453
0
          break;
3454
0
        }
3455
0
      }
3456
0
#endif
3457
0
      break;
3458
0
    case CURLMOPT_QUICK_EXIT:
3459
0
      multi->quick_exit = va_arg(param, long) ? 1 : 0;
3460
0
      break;
3461
0
    default:
3462
0
      mresult = CURLM_UNKNOWN_OPTION;
3463
0
      break;
3464
0
    }
3465
0
    va_end(param);
3466
0
  }
3467
0
  CURL_MAPI_LEAVE(&guard);
3468
0
  return mresult;
3469
0
}
3470
3471
/* we define curl_multi_socket() in the public multi.h header */
3472
#undef curl_multi_socket
3473
3474
CURLMcode curl_multi_socket(CURLM *m, curl_socket_t s, int *running_handles)
3475
0
{
3476
0
  struct Curl_mapi_guard guard;
3477
0
  CURLMcode mresult;
3478
3479
0
  if(CURL_MAPI_ENTER(&guard, m, multi_socket, &mresult)) {
3480
0
    mresult = multi_socket(m, FALSE, s, 0, running_handles);
3481
0
  }
3482
0
  CURL_MAPI_LEAVE(&guard);
3483
0
  return mresult;
3484
0
}
3485
3486
CURLMcode curl_multi_socket_action(CURLM *m, curl_socket_t s,
3487
                                   int ev_bitmask, int *running_handles)
3488
0
{
3489
0
  struct Curl_mapi_guard guard;
3490
0
  CURLMcode mresult;
3491
3492
0
  if(CURL_MAPI_ENTER(&guard, m, multi_socket_action, &mresult)) {
3493
0
    mresult = multi_socket(m, FALSE, s, ev_bitmask, running_handles);
3494
0
  }
3495
0
  CURL_MAPI_LEAVE(&guard);
3496
0
  return mresult;
3497
0
}
3498
3499
CURLMcode curl_multi_socket_all(CURLM *m, int *running_handles)
3500
0
{
3501
0
  struct Curl_mapi_guard guard;
3502
0
  CURLMcode mresult;
3503
3504
0
  if(CURL_MAPI_ENTER(&guard, m, multi_socket_all, &mresult)) {
3505
0
    mresult = multi_socket(m, TRUE, CURL_SOCKET_BAD, 0, running_handles);
3506
0
  }
3507
0
  CURL_MAPI_LEAVE(&guard);
3508
0
  return mresult;
3509
0
}
3510
3511
static bool multi_has_dirties(struct Curl_multi *multi)
3512
0
{
3513
0
  uint32_t mid;
3514
0
  if(Curl_uint32_bset_first(&multi->dirty, &mid)) {
3515
0
    do {
3516
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3517
0
      if(data) {
3518
0
        if(Curl_uint32_bset_contains(&multi->process, mid))
3519
0
          return TRUE;
3520
        /* We are no longer processing this transfer */
3521
0
        Curl_uint32_bset_remove(&multi->dirty, mid);
3522
0
      }
3523
0
      else {
3524
0
        CURL_TRC_M(multi->admin, "dirty transfer %u no longer found", mid);
3525
0
        Curl_uint32_bset_remove(&multi->dirty, mid);
3526
0
      }
3527
0
    } while(Curl_uint32_bset_next(&multi->dirty, mid, &mid));
3528
0
  }
3529
0
  return FALSE;
3530
0
}
3531
3532
static void multi_timeout(struct Curl_multi *multi,
3533
                          timediff_t *pexire_offset_us,
3534
                          int *timeout_ms)
3535
0
{
3536
0
  if(multi->dead) {
3537
0
    if(pexire_offset_us)
3538
0
      *pexire_offset_us = 0;
3539
0
    *timeout_ms = 0;
3540
0
    return;
3541
0
  }
3542
3543
0
  if(multi_has_dirties(multi)) {
3544
0
    if(pexire_offset_us) {
3545
0
      struct curltime now;
3546
0
      curlx_pnow(&now);
3547
0
      *pexire_offset_us = Curl_timeouts_offset_us(&multi->timeouts, &now);
3548
0
    }
3549
0
    *timeout_ms = 0;
3550
0
    return;
3551
0
  }
3552
0
  else {
3553
0
    struct curltime now;
3554
0
    uint32_t mid;
3555
3556
0
    curlx_pnow(&now);
3557
0
    *timeout_ms = Curl_timeouts_next_ms(&multi->timeouts, &now,
3558
0
                                        pexire_offset_us, &mid);
3559
0
#ifdef CURLVERBOSE
3560
0
    if(mid != UINT32_MAX) {
3561
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3562
0
      if(data && CURL_TRC_TIMER_is_verbose(data) &&
3563
0
         (data->state.timeouts.first < EXPIRE_LAST)) {
3564
0
        CURL_TRC_TIMER(data, data->state.timeouts.first,
3565
0
                       "gives multi timeout in %dms", *timeout_ms);
3566
0
      }
3567
0
    }
3568
0
#endif
3569
0
  }
3570
0
}
3571
3572
CURLMcode curl_multi_timeout(CURLM *m,
3573
                             long *timeout_ms)
3574
0
{
3575
0
  struct Curl_mapi_guard guard;
3576
0
  CURLMcode mresult;
3577
3578
0
  if(CURL_MAPI_ENTER(&guard, m, multi_timeout, &mresult)) {
3579
0
    int itimeout_ms;
3580
3581
0
    multi_timeout(m, NULL, &itimeout_ms);
3582
0
    *timeout_ms = (long)itimeout_ms;
3583
0
    mresult = CURLM_OK;
3584
0
  }
3585
0
  CURL_MAPI_LEAVE(&guard);
3586
0
  return mresult;
3587
0
}
3588
3589
/*
3590
 * Tell the application it should update its timers, if it subscribes to the
3591
 * update timer callback.
3592
 */
3593
CURLMcode Curl_update_timer(struct Curl_multi *multi)
3594
35.7k
{
3595
35.7k
  timediff_t timeouts_offset_us = 0;
3596
35.7k
  int timeout_ms;
3597
35.7k
  int rc;
3598
35.7k
  bool set_value = FALSE;
3599
3600
35.7k
  if(!multi->timer_cb || multi->dead)
3601
35.7k
    return CURLM_OK;
3602
0
  multi_timeout(multi, &timeouts_offset_us, &timeout_ms);
3603
3604
0
  if(timeout_ms < 0 && !multi->last_timeout_set) {
3605
    /* nothing to do */
3606
0
  }
3607
0
  else if(timeout_ms < 0) {
3608
    /* there is no timeout now but there was one previously */
3609
0
    CURL_TRC_M(multi->admin, "[TIMER] clear");
3610
0
    timeout_ms = -1; /* normalize */
3611
0
    set_value = TRUE;
3612
0
  }
3613
0
  else if(!multi->last_timeout_set) {
3614
0
    CURL_TRC_M(multi->admin, "[TIMER] set %dms, none before", timeout_ms);
3615
0
    set_value = TRUE;
3616
0
  }
3617
0
  else if(multi->last_expire_offset_us != timeouts_offset_us) {
3618
    /* We had a timeout before and have one now, the absolute timestamp
3619
     * differs. The relative timeout_ms may be the same, but the starting
3620
     * point differs. Let the application restart its timer. */
3621
0
    CURL_TRC_M(multi->admin, "[TIMER] set %dms, replace previous",
3622
0
               timeout_ms);
3623
0
    set_value = TRUE;
3624
0
  }
3625
0
  else {
3626
    /* We have same expire time as previously. Our relative 'timeout_ms'
3627
     * may be different now, but the application has the timer running
3628
     * and we do not to tell it to start this again. */
3629
0
  }
3630
3631
0
  if(set_value) {
3632
0
    struct Curl_mapi_guard guard;
3633
3634
0
    multi->last_expire_offset_us = timeouts_offset_us;
3635
0
    multi->last_timeout_set = timeout_ms >= 0;
3636
0
    CURL_CBAPI_MULTI_START(&guard, multi, multi_timer_cb);
3637
0
    rc = multi->timer_cb(multi, timeout_ms, multi->timer_userp);
3638
0
    CURL_CBAPI_MULTI_END(&guard);
3639
0
    if(rc == -1) {
3640
0
      multi->dead = TRUE;
3641
0
      return CURLM_ABORTED_BY_CALLBACK;
3642
0
    }
3643
0
  }
3644
0
  return CURLM_OK;
3645
0
}
3646
3647
#ifdef DEBUGBUILD
3648
static bool multi_timeouts_check(struct Curl_easy *data)
3649
20.8k
{
3650
20.8k
  struct expire_timers *timeouts = &data->state.timeouts;
3651
20.8k
  uint8_t id;
3652
20.8k
  int i = 0;
3653
34.0k
  for(id = timeouts->first; id < EXPIRE_LAST; id = timeouts->next[id]) {
3654
13.1k
    if(++i >= EXPIRE_LAST) {
3655
0
      failf(data, "expire timeouts looped: %d iterations and no end", i);
3656
0
      return FALSE;
3657
0
    }
3658
13.1k
    if(id == timeouts->next[id]) {
3659
0
      failf(data, "expire timeouts wrong: %d points to itself", (int)id);
3660
0
      return FALSE;
3661
0
    }
3662
13.1k
    if((timeouts->next[id] < EXPIRE_LAST) &&
3663
409
       (timeouts->offset_us[id] > timeouts->offset_us[timeouts->next[id]])) {
3664
0
      failf(data, "expire timeouts not sorted: %d happens after %d but "
3665
0
            "is listed before", (int)id, (int)timeouts->next[id]);
3666
0
      return FALSE;
3667
0
    }
3668
13.1k
  }
3669
20.8k
  return TRUE;
3670
20.8k
}
3671
#endif
3672
3673
/*
3674
 * Remove a given timestamp from the list of timeouts.
3675
 */
3676
static void multi_clear_timeout(struct Curl_easy *data, expire_id eid)
3677
12.4k
{
3678
12.4k
  struct expire_timers *timeouts = &data->state.timeouts;
3679
12.4k
  uint8_t orig_first = timeouts->first;
3680
12.4k
  uint8_t *anchor = &timeouts->first;
3681
12.4k
  uint8_t id = (uint8_t)eid;
3682
3683
12.4k
  if((unsigned)eid >= EXPIRE_LAST) {
3684
0
    DEBUGASSERT(0);
3685
0
    return;
3686
0
  }
3687
3688
17.0k
  while(*anchor < EXPIRE_LAST) {
3689
4.56k
    if(*anchor == id) {
3690
0
      *anchor = timeouts->next[id];
3691
0
      break;
3692
0
    }
3693
4.56k
    anchor = &timeouts->next[*anchor];
3694
4.56k
  }
3695
12.4k
  DEBUGASSERT(multi_timeouts_check(data));
3696
12.4k
  if(Curl_timeouts_has(data)) {
3697
4.45k
    struct Curl_multi *multi = data->multi;
3698
3699
4.45k
    if(!multi) {
3700
0
      DEBUGASSERT(0);
3701
0
      return;
3702
0
    }
3703
4.45k
    if((timeouts->first >= EXPIRE_LAST) || /* no more timeouts */
3704
4.45k
       (timeouts->first != orig_first)) {  /* active timeout changed */
3705
0
      Curl_timeouts_remove(&multi->timeouts, data);
3706
0
    }
3707
4.45k
    if((timeouts->first < EXPIRE_LAST) && !Curl_timeouts_has(data)) {
3708
0
      Curl_timeouts_add(&multi->timeouts, data,
3709
0
                        timeouts->offset_us[timeouts->first]);
3710
0
    }
3711
4.45k
  }
3712
12.4k
}
3713
3714
/*
3715
 * Add a timestamp to the list of timeouts. Keep the list sorted so that head
3716
 * of list is always the timeout nearest in time.
3717
 */
3718
static CURLMcode multi_set_timeout(struct Curl_easy *data,
3719
                                   const struct curltime *stamp,
3720
                                   expire_id eid)
3721
8.33k
{
3722
8.33k
  struct expire_timers *timeouts = &data->state.timeouts;
3723
8.33k
  uint8_t *anchor = &timeouts->first;
3724
8.33k
  uint8_t id = (uint8_t)eid;
3725
3726
8.33k
  if((unsigned)eid >= EXPIRE_LAST) {
3727
0
    DEBUGASSERT(0);
3728
0
    return CURLM_BAD_FUNCTION_ARGUMENT;
3729
0
  }
3730
  /* remove from list, store time and re-insert */
3731
8.33k
  multi_clear_timeout(data, eid);
3732
8.33k
  timeouts->offset_us[id] =
3733
8.33k
    Curl_timeouts_offset_us(&data->multi->timeouts, stamp);
3734
3735
8.49k
  while(*anchor < EXPIRE_LAST) {
3736
295
    if(timeouts->offset_us[*anchor] > timeouts->offset_us[id])
3737
130
      break;
3738
165
    anchor = &timeouts->next[*anchor];
3739
165
  }
3740
8.33k
  timeouts->next[eid] = *anchor;
3741
8.33k
  timeouts->next[eid] = *anchor;
3742
8.33k
  *anchor = id;
3743
8.33k
  DEBUGASSERT(multi_timeouts_check(data));
3744
8.33k
  CURL_TRC_TIMER(data, eid, "set for %" FMT_TIMEDIFF_T "us",
3745
8.33k
                 curlx_ptimediff_us(stamp, Curl_pgrs_now(data)));
3746
8.33k
  return CURLM_OK;
3747
8.33k
}
3748
3749
/*
3750
 * given a number of milliseconds from now to use to set the 'act before
3751
 * this'-time for the transfer, to be extracted by curl_multi_timeout()
3752
 *
3753
 * The timeout will be added to a queue of timeouts if it defines a moment in
3754
 * time that is later than the current head of queue.
3755
 *
3756
 * Expire replaces a former timeout using the same id if already set.
3757
 */
3758
void Curl_expire_set(struct Curl_easy *data,
3759
                     expire_id eid, timediff_t ms,
3760
                     const struct curltime *pnow)
3761
8.33k
{
3762
8.33k
  struct Curl_multi *multi = data->multi;
3763
8.33k
  struct expire_timers *timeouts = &data->state.timeouts;
3764
8.33k
  uint8_t prev_id = timeouts->first;
3765
8.33k
  struct curltime set;
3766
3767
  /* this is only interesting while there is still an associated multi struct
3768
     remaining! */
3769
8.33k
  if(!multi)
3770
0
    return;
3771
8.33k
  DEBUGASSERT(eid < EXPIRE_LAST);
3772
8.33k
  if(ms > INT_MAX)
3773
    /* Cap ridiculous timeouts, 31-bit ms is still 3.5 weeks. When the time
3774
       goes to the user, it must fit in this size. */
3775
50
    ms = INT_MAX;
3776
3777
8.33k
  set = *pnow;
3778
8.33k
  set.tv_sec += (time_t)(ms / 1000); /* may be a 64 to 32-bit conversion */
3779
8.33k
  set.tv_usec += (int)(ms % 1000) * 1000;
3780
8.33k
  if(set.tv_usec >= 1000000) {
3781
1.65k
    set.tv_sec++;
3782
1.65k
    set.tv_usec -= 1000000;
3783
1.65k
  }
3784
3785
  /* Add the timeout, will replace any previous value for this timer. */
3786
8.33k
  multi_set_timeout(data, &set, eid);
3787
8.33k
  DEBUGASSERT(timeouts->first < EXPIRE_LAST);
3788
3789
8.33k
  if(Curl_timeouts_has(data)) {
3790
    /* data has already a timeout registered. If the first timer
3791
     * was NOT the one we just set AND is still the first one,
3792
     * nothing changed from the timeouts point of view. The
3793
     * set timer triggers after the one already registered. Leave. */
3794
290
    if((prev_id != eid) && (prev_id == timeouts->first))
3795
162
      return;
3796
3797
    /* Since this is an updated time, we must remove data from
3798
     * timeouts and then add it again. */
3799
128
    Curl_timeouts_remove(&multi->timeouts, data);
3800
128
  }
3801
3802
  /* Insert the new timer expiry since it is our local minimum. */
3803
8.16k
  Curl_timeouts_add(&multi->timeouts, data,
3804
8.16k
                    timeouts->offset_us[timeouts->first]);
3805
8.16k
}
3806
3807
void Curl_expire(struct Curl_easy *data,
3808
                 timediff_t milli, expire_id eid)
3809
0
{
3810
0
  Curl_expire_set(data, eid, milli, Curl_pgrs_now(data));
3811
0
}
3812
3813
/*
3814
 * Removes the expire timer. Marks it as done.
3815
 */
3816
void Curl_expire_clear(struct Curl_easy *data, expire_id eid)
3817
4.16k
{
3818
  /* remove the timer, if there */
3819
4.16k
  multi_clear_timeout(data, eid);
3820
4.16k
  CURL_TRC_TIMER(data, eid, "cleared");
3821
4.16k
}
3822
3823
/*
3824
 * Clear ALL timeout values for this handle.
3825
 */
3826
void Curl_expire_clear_all(struct Curl_easy *data)
3827
46.3k
{
3828
46.3k
  struct Curl_multi *multi = data->multi;
3829
3830
  /* this is only interesting while there is still an associated multi struct
3831
     remaining! */
3832
46.3k
  if(!multi)
3833
24.2k
    return;
3834
3835
22.0k
  if(Curl_timeouts_remove(&multi->timeouts, data)) {
3836
    /* Since this is an cleared time, we must remove the previous entry from
3837
       the splay tree */
3838
8.04k
    multi_timeouts_init(data);
3839
3840
8.04k
    if(data->id >= 0)
3841
8.04k
      CURL_TRC_M(data, "[TIMEOUT] all cleared");
3842
8.04k
  }
3843
22.0k
}
3844
3845
CURLMcode curl_multi_assign(CURLM *m, curl_socket_t sockfd,
3846
                            void *sockp)
3847
0
{
3848
0
  struct Curl_mapi_guard guard;
3849
0
  CURLMcode mresult;
3850
3851
0
  if(CURL_MAPI_ENTER(&guard, m, multi_assign, &mresult)) {
3852
0
    mresult = Curl_multi_ev_assign(m, sockfd, sockp);
3853
0
  }
3854
0
  CURL_MAPI_LEAVE(&guard);
3855
0
  return mresult;
3856
0
}
3857
3858
static void move_pending_to_connect(struct Curl_multi *multi,
3859
                                    struct Curl_easy *data)
3860
0
{
3861
0
  DEBUGASSERT(data->mstate == MSTATE_PENDING);
3862
3863
  /* Remove this node from the pending set, add into process set */
3864
0
  Curl_uint32_bset_remove(&multi->pending, data->mid);
3865
0
  Curl_uint32_bset_add(&multi->process, data->mid);
3866
3867
0
  multistate(data, MSTATE_CONNECT);
3868
0
  Curl_multi_mark_dirty(data); /* make it run */
3869
0
}
3870
3871
/* multi_schedule_pending() moves a handle from PENDING back into the process
3872
   list and change state to CONNECT.
3873
3874
   We do not move all transfers because that can be a significant amount.
3875
   Since this is tried every now and then doing too many too often becomes a
3876
   performance problem.
3877
3878
   When there is a change for connection limits like max host connections etc,
3879
   this likely only allows one new transfer. When there is a pipewait change,
3880
   it can potentially allow hundreds of new transfers.
3881
3882
   We could consider an improvement where we store the queue reason and allow
3883
   more pipewait rechecks than others. */
3884
static void multi_schedule_pending(struct Curl_multi *multi)
3885
38.0k
{
3886
38.0k
  uint32_t mid = multi->last_pending_mid;
3887
3888
38.0k
  if(mid) {
3889
0
    while(Curl_uint32_bset_next(&multi->pending, mid, &mid)) {
3890
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3891
0
      if(data) {
3892
0
        move_pending_to_connect(multi, data);
3893
0
        multi->last_pending_mid = mid;
3894
0
        return;
3895
0
      }
3896
      /* transfer no longer known, should not happen */
3897
0
      Curl_uint32_bset_remove(&multi->pending, mid);
3898
0
      DEBUGASSERT(0);
3899
0
    }
3900
    /* found no pending transfers with `mid` larger than `last_pending_mid`.
3901
     * Start at the beginning of the pending set again. */
3902
0
    multi->last_pending_mid = 0;
3903
0
  }
3904
3905
38.0k
  if(Curl_uint32_bset_first(&multi->pending, &mid)) {
3906
0
    do {
3907
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3908
0
      if(data) {
3909
0
        move_pending_to_connect(multi, data);
3910
0
        multi->last_pending_mid = mid;
3911
0
        return;
3912
0
      }
3913
      /* transfer no longer known, should not happen */
3914
0
      Curl_uint32_bset_remove(&multi->pending, mid);
3915
0
      DEBUGASSERT(0);
3916
0
    } while(Curl_uint32_bset_next(&multi->pending, mid, &mid));
3917
0
  }
3918
38.0k
}
3919
3920
uint32_t Curl_multi_max_concurrent_streams(struct Curl_multi *multi)
3921
0
{
3922
0
  DEBUGASSERT(multi);
3923
0
  return multi->max_concurrent_streams;
3924
0
}
3925
3926
CURL **curl_multi_get_handles(CURLM *m)
3927
0
{
3928
0
  struct Curl_mapi_guard guard;
3929
0
  CURL **a = NULL;
3930
3931
0
  if(CURL_MAPI_ENTER(&guard, m, multi_get_handles, NULL)) {
3932
0
    struct Curl_multi *multi = m;
3933
0
    void *entry;
3934
0
    size_t count = Curl_uint32_tbl_count(&multi->xfers);
3935
3936
0
    a = curlx_malloc(sizeof(struct Curl_easy *) * (count + 1));
3937
0
    if(a) {
3938
0
      unsigned int i = 0;
3939
0
      uint32_t mid;
3940
3941
0
      if(Curl_uint32_tbl_first(&multi->xfers, &mid, &entry)) {
3942
0
        do {
3943
0
          struct Curl_easy *data = entry;
3944
0
          DEBUGASSERT(i < count);
3945
0
          if(!data->state.internal)
3946
0
            a[i++] = data;
3947
0
        } while(Curl_uint32_tbl_next(&multi->xfers, mid, &mid, &entry));
3948
0
      }
3949
0
      a[i] = NULL; /* last entry is a NULL */
3950
0
    }
3951
0
  }
3952
0
  CURL_MAPI_LEAVE(&guard);
3953
0
  return a;
3954
0
}
3955
3956
CURLMcode curl_multi_get_offt(CURLM *m,
3957
                              CURLMinfo_offt info,
3958
                              curl_off_t *pvalue)
3959
0
{
3960
0
  struct Curl_mapi_guard guard;
3961
0
  CURLMcode mresult = CURLM_OK;
3962
3963
0
  if(CURL_MAPI_ENTER(&guard, m, multi_get_offt, &mresult)) {
3964
0
    struct Curl_multi *multi = m;
3965
0
    uint32_t n;
3966
3967
0
    if(!pvalue) {
3968
0
      mresult = CURLM_BAD_FUNCTION_ARGUMENT;
3969
0
      goto out;
3970
0
    }
3971
3972
0
    switch(info) {
3973
0
    case CURLMINFO_XFERS_CURRENT:
3974
0
      n = Curl_uint32_tbl_count(&multi->xfers);
3975
0
      if(n && multi->admin)
3976
0
        --n;
3977
0
      *pvalue = (curl_off_t)n;
3978
0
      break;
3979
0
    case CURLMINFO_XFERS_RUNNING:
3980
0
      n = Curl_uint32_bset_count(&multi->process);
3981
0
      if(n && Curl_uint32_bset_contains(&multi->process, multi->admin->mid))
3982
0
        --n;
3983
0
      *pvalue = (curl_off_t)n;
3984
0
      break;
3985
0
    case CURLMINFO_XFERS_PENDING:
3986
0
      *pvalue = (curl_off_t)Curl_uint32_bset_count(&multi->pending);
3987
0
      break;
3988
0
    case CURLMINFO_XFERS_DONE:
3989
0
      *pvalue = (curl_off_t)Curl_uint32_bset_count(&multi->msgsent);
3990
0
      break;
3991
0
    case CURLMINFO_XFERS_ADDED:
3992
0
      *pvalue = multi->xfers_total_ever;
3993
0
      break;
3994
0
    default:
3995
0
      *pvalue = -1;
3996
0
      mresult = CURLM_UNKNOWN_OPTION;
3997
0
      break;
3998
0
    }
3999
0
  }
4000
0
out:
4001
0
  CURL_MAPI_LEAVE(&guard);
4002
0
  return mresult;
4003
0
}
4004
4005
static struct Curl_fixed_buf *fixed_buf_create(size_t len)
4006
0
{
4007
0
  struct Curl_fixed_buf *fbuf;
4008
0
  if((SIZE_MAX - sizeof(*fbuf)) < len)
4009
0
    return NULL; /* too large */
4010
0
  fbuf = curlx_malloc(len + sizeof(*fbuf));
4011
0
  if(fbuf)
4012
0
    fbuf->len = len;
4013
0
  return fbuf;
4014
0
}
4015
4016
CURLcode Curl_multi_xfer_buf_borrow(struct Curl_easy *data,
4017
                                    char **pbuf, size_t *pbuflen)
4018
0
{
4019
0
  DEBUGASSERT(data);
4020
0
  DEBUGASSERT(data->multi);
4021
0
  *pbuf = NULL;
4022
0
  *pbuflen = 0;
4023
0
  if(!data->multi) {
4024
0
    failf(data, "transfer has no multi handle");
4025
0
    return CURLE_FAILED_INIT;
4026
0
  }
4027
0
  if(!data->set.buffer_size) {
4028
0
    failf(data, "transfer buffer size is 0");
4029
0
    return CURLE_FAILED_INIT;
4030
0
  }
4031
0
  if(data->multi->xfer_buf_borrowed) {
4032
0
    failf(data, "attempt to borrow xfer_buf when already borrowed");
4033
0
    return CURLE_AGAIN;
4034
0
  }
4035
4036
0
  if(data->multi->xfer_buf &&
4037
0
     data->set.buffer_size > data->multi->xfer_buf->len) {
4038
    /* not large enough, get a new one */
4039
0
    curlx_safefree(data->multi->xfer_buf);
4040
0
  }
4041
4042
0
  if(!data->multi->xfer_buf) {
4043
0
    data->multi->xfer_buf =
4044
0
      fixed_buf_create(curlx_uitouz(data->set.buffer_size));
4045
0
    if(!data->multi->xfer_buf) {
4046
0
      failf(data, "could not allocate xfer_buf of %u bytes",
4047
0
            data->set.buffer_size);
4048
0
      return CURLE_OUT_OF_MEMORY;
4049
0
    }
4050
0
  }
4051
4052
0
  data->multi->xfer_buf_borrowed = TRUE;
4053
0
  *pbuf = data->multi->xfer_buf->data;
4054
0
  *pbuflen = data->multi->xfer_buf->len;
4055
0
  return CURLE_OK;
4056
0
}
4057
4058
void Curl_multi_xfer_buf_release(struct Curl_easy *data, char *buf)
4059
0
{
4060
0
  (void)buf;
4061
0
  DEBUGASSERT(data);
4062
0
  DEBUGASSERT(data->multi);
4063
0
  DEBUGASSERT(!buf || (data->multi->xfer_buf &&
4064
0
                       data->multi->xfer_buf->data == buf));
4065
0
  data->multi->xfer_buf_borrowed = FALSE;
4066
0
}
4067
4068
CURLcode Curl_multi_xfer_ulbuf_borrow(struct Curl_easy *data,
4069
                                      char **pbuf, size_t *pbuflen)
4070
0
{
4071
0
  DEBUGASSERT(data);
4072
0
  DEBUGASSERT(data->multi);
4073
0
  *pbuf = NULL;
4074
0
  *pbuflen = 0;
4075
0
  if(!data->multi) {
4076
0
    failf(data, "transfer has no multi handle");
4077
0
    return CURLE_FAILED_INIT;
4078
0
  }
4079
0
  if(data->multi->xfer_ulbuf_borrowed) {
4080
0
    failf(data, "attempt to borrow xfer_ulbuf when already borrowed");
4081
0
    return CURLE_AGAIN;
4082
0
  }
4083
4084
0
  if(data->multi->xfer_ulbuf &&
4085
0
     data->set.upload_buffer_size > data->multi->xfer_ulbuf->len) {
4086
    /* not large enough, get a new one */
4087
0
    curlx_safefree(data->multi->xfer_ulbuf);
4088
0
  }
4089
4090
0
  if(!data->multi->xfer_ulbuf) {
4091
0
    data->multi->xfer_ulbuf =
4092
0
      fixed_buf_create(curlx_uitouz(data->set.upload_buffer_size));
4093
0
    if(!data->multi->xfer_ulbuf) {
4094
0
      failf(data, "could not allocate xfer_ulbuf of %u bytes",
4095
0
            data->set.upload_buffer_size);
4096
0
      return CURLE_OUT_OF_MEMORY;
4097
0
    }
4098
0
  }
4099
4100
0
  data->multi->xfer_ulbuf_borrowed = TRUE;
4101
0
  *pbuf = data->multi->xfer_ulbuf->data;
4102
0
  *pbuflen = data->multi->xfer_ulbuf->len;
4103
0
  return CURLE_OK;
4104
0
}
4105
4106
void Curl_multi_xfer_ulbuf_release(struct Curl_easy *data, char *buf)
4107
0
{
4108
0
  (void)buf;
4109
0
  DEBUGASSERT(data);
4110
0
  DEBUGASSERT(data->multi);
4111
0
  DEBUGASSERT(!buf || (data->multi->xfer_ulbuf &&
4112
0
                       data->multi->xfer_ulbuf->data == buf));
4113
0
  data->multi->xfer_ulbuf_borrowed = FALSE;
4114
0
}
4115
4116
CURLcode Curl_multi_xfer_sockbuf_borrow(struct Curl_easy *data,
4117
                                        size_t blen, char **pbuf)
4118
0
{
4119
0
  DEBUGASSERT(data);
4120
0
  *pbuf = NULL;
4121
0
  if(!data->multi) {
4122
    /* When a SHARE gets destroyed and has a connection pool, we get
4123
     * call with share->admin which does not have a multi handle. */
4124
0
    *pbuf = curlx_malloc(blen);
4125
0
    return *pbuf ? CURLE_OK : CURLE_OUT_OF_MEMORY;
4126
0
  }
4127
0
  if(data->multi->xfer_sockbuf_borrowed) {
4128
0
    failf(data, "attempt to borrow xfer_sockbuf when already borrowed");
4129
0
    return CURLE_AGAIN;
4130
0
  }
4131
4132
0
  if(data->multi->xfer_sockbuf && blen > data->multi->xfer_sockbuf->len) {
4133
    /* not large enough, get a new one */
4134
0
    curlx_safefree(data->multi->xfer_sockbuf);
4135
0
  }
4136
4137
0
  if(!data->multi->xfer_sockbuf) {
4138
0
    data->multi->xfer_sockbuf = fixed_buf_create(blen);
4139
0
    if(!data->multi->xfer_sockbuf) {
4140
0
      failf(data, "could not allocate xfer_sockbuf of %zu bytes", blen);
4141
0
      return CURLE_OUT_OF_MEMORY;
4142
0
    }
4143
0
  }
4144
4145
0
  data->multi->xfer_sockbuf_borrowed = TRUE;
4146
0
  *pbuf = data->multi->xfer_sockbuf->data;
4147
0
  return CURLE_OK;
4148
0
}
4149
4150
void Curl_multi_xfer_sockbuf_release(struct Curl_easy *data, char *buf)
4151
0
{
4152
0
  DEBUGASSERT(data);
4153
0
  if(!data->multi) {
4154
    /* When a SHARE gets destroyed and has a connection pool, we get
4155
     * call with share->admin which does not have a multi handle. */
4156
0
    curlx_free(buf);
4157
0
  }
4158
0
  else {
4159
0
    DEBUGASSERT(!buf || (data->multi->xfer_sockbuf &&
4160
0
                         data->multi->xfer_sockbuf->data == buf));
4161
0
    data->multi->xfer_sockbuf_borrowed = FALSE;
4162
0
  }
4163
0
}
4164
4165
static void multi_xfer_bufs_free(struct Curl_multi *multi)
4166
11.2k
{
4167
11.2k
  DEBUGASSERT(multi);
4168
11.2k
  curlx_safefree(multi->xfer_buf);
4169
11.2k
  multi->xfer_buf_borrowed = FALSE;
4170
11.2k
  curlx_safefree(multi->xfer_ulbuf);
4171
11.2k
  multi->xfer_ulbuf_borrowed = FALSE;
4172
11.2k
  curlx_safefree(multi->xfer_sockbuf);
4173
11.2k
  multi->xfer_sockbuf_borrowed = FALSE;
4174
11.2k
}
4175
4176
struct Curl_easy *Curl_multi_get_easy(struct Curl_multi *multi,
4177
                                      uint32_t mid)
4178
31.2k
{
4179
31.2k
  struct Curl_easy *data = Curl_uint32_tbl_get(&multi->xfers, mid);
4180
31.2k
  if(GOOD_EASY_HANDLE(data))
4181
31.2k
    return data;
4182
0
  CURL_TRC_M(multi->admin, "invalid easy handle in xfer table for mid=%u",
4183
0
             mid);
4184
0
  Curl_uint32_tbl_remove(&multi->xfers, mid);
4185
0
  return NULL;
4186
31.2k
}
4187
4188
bool Curl_multi_knows_easy(struct Curl_multi *multi, struct Curl_easy *data)
4189
0
{
4190
0
  return Curl_uint32_tbl_get(&multi->xfers, data->mid) == data;
4191
0
}
4192
4193
static uint32_t multi_xfers_running(struct Curl_multi *multi)
4194
13.2k
{
4195
13.2k
  if(!multi) {
4196
0
    DEBUGASSERT(0);
4197
0
    return 0;
4198
0
  }
4199
13.2k
  return multi->xfers_alive;
4200
13.2k
}
4201
4202
uint32_t Curl_multi_xfers_attached(struct Curl_multi *multi)
4203
13
{
4204
13
  if(!multi || !multi->admin) {
4205
0
    DEBUGASSERT(0);
4206
0
    return 0;
4207
0
  }
4208
  /* Discount the admin handle */
4209
13
  return Curl_uint32_tbl_count(&multi->xfers) - 1;
4210
13
}
4211
4212
void Curl_multi_mark_dirty(struct Curl_easy *data)
4213
11.3k
{
4214
11.3k
  if(data->multi && data->mid != UINT32_MAX)
4215
11.3k
    Curl_uint32_bset_add(&data->multi->dirty, data->mid);
4216
11.3k
}
4217
4218
void Curl_multi_clear_dirty(struct Curl_easy *data)
4219
0
{
4220
0
  if(data->multi && data->mid != UINT32_MAX)
4221
0
    Curl_uint32_bset_remove(&data->multi->dirty, data->mid);
4222
0
}
4223
4224
CURLMcode curl_multi_notify_enable(CURLM *m, unsigned int notification)
4225
0
{
4226
0
  struct Curl_mapi_guard guard;
4227
0
  CURLMcode mresult = CURLM_OK;
4228
4229
0
  if(CURL_MAPI_ENTER(&guard, m, multi_notify_enable, &mresult)) {
4230
0
    mresult = Curl_mntfy_enable(m, notification);
4231
0
  }
4232
0
  CURL_MAPI_LEAVE(&guard);
4233
0
  return mresult;
4234
0
}
4235
4236
CURLMcode curl_multi_notify_disable(CURLM *m, unsigned int notification)
4237
0
{
4238
0
  struct Curl_mapi_guard guard;
4239
0
  CURLMcode mresult = CURLM_OK;
4240
4241
0
  if(CURL_MAPI_ENTER(&guard, m, multi_notify_disable, &mresult)) {
4242
0
    mresult = Curl_mntfy_disable(m, notification);
4243
0
  }
4244
0
  CURL_MAPI_LEAVE(&guard);
4245
0
  return mresult;
4246
0
}
4247
4248
#ifdef DEBUGBUILD
4249
static void multi_xfer_dump(struct Curl_multi *multi, uint32_t mid,
4250
                            void *entry)
4251
0
{
4252
0
  struct Curl_easy *data = entry;
4253
4254
0
  (void)multi;
4255
0
  if(!data) {
4256
0
    curl_mfprintf(stderr, "mid=%u, entry=NULL, bug in xfer table?\n", mid);
4257
0
  }
4258
0
  else {
4259
0
    curl_mfprintf(stderr, "mid=%u, magic=%s, p=%p, id=%" FMT_OFF_T
4260
0
                  ", url=%s\n",
4261
0
                  mid,
4262
0
                  (data->magic == CURLEASY_MAGIC_NUMBER) ? "GOOD" : "BAD!",
4263
0
                  (void *)data, data->id, Curl_bufref_ptr(&data->state.url));
4264
0
  }
4265
0
}
4266
4267
static void multi_xfer_tbl_dump(struct Curl_multi *multi)
4268
0
{
4269
0
  uint32_t mid;
4270
0
  void *entry;
4271
0
  curl_mfprintf(stderr, "=== multi xfer table (count=%u, capacity=%u\n",
4272
0
                Curl_uint32_tbl_count(&multi->xfers),
4273
0
                Curl_uint32_tbl_capacity(&multi->xfers));
4274
0
  if(Curl_uint32_tbl_first(&multi->xfers, &mid, &entry)) {
4275
0
    multi_xfer_dump(multi, mid, entry);
4276
0
    while(Curl_uint32_tbl_next(&multi->xfers, mid, &mid, &entry))
4277
0
      multi_xfer_dump(multi, mid, entry);
4278
0
  }
4279
0
  curl_mfprintf(stderr, "===\n");
4280
  fflush(stderr);
4281
0
}
4282
#endif /* DEBUGBUILD */