Coverage Report

Created: 2026-12-01 06:28

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