Coverage Report

Created: 2026-12-01 06:33

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
12.9k
#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
6.54k
#define CURL_SOCKET_HASH_TABLE_SIZE 911
60
#endif
61
62
#ifndef CURL_CONNECTION_HASH_SIZE
63
6.54k
#define CURL_CONNECTION_HASH_SIZE 97
64
#endif
65
66
#ifndef CURL_DNS_HASH_SIZE
67
6.54k
#define CURL_DNS_HASH_SIZE 71
68
#endif
69
70
#ifndef CURL_TLS_SESSION_SIZE
71
6.54k
#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
6.39k
{
97
6.39k
  (void)from_state;
98
6.39k
  Curl_init_CONNECT(data);
99
6.39k
}
100
101
static void mstate_enter_did(struct Curl_easy *data,
102
                             CURLMstate from_state)
103
1.93k
{
104
1.93k
  (void)from_state;
105
1.93k
  data->req.chunk = FALSE;
106
1.93k
  Curl_pgrsTime(data, TIMER_PRETRANSFER);
107
1.93k
  if(!CURL_REQ_WANT_SEND(data))
108
252
    Curl_pgrsTime(data, TIMER_POSTRANSFER);
109
1.93k
}
110
111
static void mstate_enter_done(struct Curl_easy *data,
112
                              CURLMstate from_state)
113
1.32k
{
114
1.32k
  (void)from_state;
115
1.32k
  CURLM_NTFY(data, CURLMNOTIFY_EASY_DONE);
116
1.32k
}
117
118
static void mstate_enter_completed(struct Curl_easy *data,
119
                                   CURLMstate from_state)
120
2.87k
{
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
2.87k
  if((from_state >= MSTATE_DO) && (from_state < MSTATE_DID)) {
125
471
    Curl_pgrsTime(data, TIMER_PRETRANSFER);
126
471
    Curl_pgrsTime(data, TIMER_POSTRANSFER);
127
471
    Curl_pgrsTime(data, TIMER_STARTTRANSFER);
128
471
  }
129
  /* multi_done() should have run and detached any connection already */
130
2.87k
  DEBUGASSERT(!data->conn);
131
2.87k
  Curl_pgrsCompleted(data);
132
2.87k
  if(from_state < MSTATE_DONE)
133
1.54k
    CURLM_NTFY(data, CURLMNOTIFY_EASY_DONE);
134
  /* changing to COMPLETED means it is in process and needs to go */
135
2.87k
  DEBUGASSERT(Curl_uint32_bset_contains(&data->multi->process, data->mid));
136
2.87k
  Curl_uint32_bset_remove(&data->multi->process, data->mid);
137
2.87k
  Curl_uint32_bset_remove(&data->multi->pending, data->mid); /* to be sure */
138
139
2.87k
  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
2.87k
  Curl_detach_connection(data);
146
2.87k
  Curl_expire_clear_all(data); /* stop all timers */
147
2.87k
}
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
53.5k
{
156
53.5k
  uint8_t oldstate = data->mstate;
157
53.5k
  static const mstate_enter_func state_enter[MSTATE_LAST] = {
158
53.5k
    NULL,                      /* INIT */
159
53.5k
    NULL,                      /* PENDING */
160
53.5k
    NULL,                      /* SETUP */
161
53.5k
    mstate_enter_connect,      /* CONNECT */
162
53.5k
    NULL,                      /* CONNECTING */
163
53.5k
    NULL,                      /* PROTOCONNECT */
164
53.5k
    NULL,                      /* PROTOCONNECTING */
165
53.5k
    NULL,                      /* DO */
166
53.5k
    NULL,                      /* DOING */
167
53.5k
    NULL,                      /* DOING_MORE */
168
53.5k
    mstate_enter_did,          /* DID */
169
53.5k
    NULL,                      /* PERFORMING */
170
53.5k
    NULL,                      /* RATELIMITING */
171
53.5k
    mstate_enter_done,         /* DONE */
172
53.5k
    mstate_enter_completed,    /* COMPLETED */
173
    NULL                       /* MSGSENT */
174
53.5k
  };
175
176
53.5k
  if(oldstate == state)
177
    /* do not bother when the new state is the same as the old state */
178
6.43k
    return;
179
180
47.1k
#ifdef DEBUGBUILD
181
47.1k
  NOVERBOSE((void)lineno);
182
47.1k
  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
47.1k
  data->mstate = (uint8_t)state;
189
47.1k
  if(state_enter[state])
190
12.5k
    state_enter[state](data, (CURLMstate)oldstate);
191
47.1k
}
192
193
#ifndef DEBUGBUILD
194
#define multistate(x, y) mstate(x, y)
195
#else
196
53.5k
#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
2.87k
{
217
2.87k
  if(Curl_uint32_bset_empty(&multi->msgsent))
218
2.87k
    CURLM_NTFY(multi->admin, CURLMNOTIFY_INFO_READ);
219
2.87k
  Curl_uint32_bset_add(&multi->msgsent, data->mid);
220
2.87k
}
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
6.54k
{
230
6.54k
  struct curltime created;
231
6.54k
  struct Curl_multi *multi = curlx_calloc(1, sizeof(struct Curl_multi));
232
233
6.54k
  if(!multi)
234
35
    return NULL;
235
236
6.50k
  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
6.50k
  multi->admin = curl_easy_init();
243
6.50k
  if(!multi->admin) {
244
21
    curlx_free(multi);
245
21
    return NULL;
246
21
  }
247
6.48k
  multi->admin->multi = multi;
248
6.48k
  multi->admin->state.internal = TRUE;
249
250
  /* Now we can use curlx_* things safely */
251
6.48k
  curlx_pnow(&created);
252
6.48k
  Curl_timeouts_init(&multi->timeouts, &created);
253
6.48k
  multi_timeouts_init(multi->admin);
254
255
6.48k
  Curl_dnscache_init(&multi->dnscache, dnssize);
256
6.48k
  Curl_mntfy_init(multi);
257
6.48k
  Curl_multi_ev_init(multi, ev_hashsize);
258
6.48k
  Curl_uint32_tbl_init(&multi->xfers);
259
6.48k
  Curl_uint32_bset_init(&multi->process);
260
6.48k
  Curl_uint32_bset_init(&multi->dirty);
261
6.48k
  Curl_uint32_bset_init(&multi->pending);
262
6.48k
  Curl_uint32_bset_init(&multi->msgsent);
263
6.48k
  Curl_hash_init(&multi->proto_hash, 23, CURL_HASH_TYPE_BYTES, ph_freeentry);
264
6.48k
  Curl_cshutdn_init(&multi->cshutdn);
265
266
6.48k
  multi->multiplexing = TRUE;
267
6.48k
  multi->max_concurrent_streams = 100;
268
6.48k
#ifdef ENABLE_WAKEUP
269
6.48k
  multi->wakeup_pair[0] = CURL_SOCKET_BAD;
270
6.48k
  multi->wakeup_pair[1] = CURL_SOCKET_BAD;
271
6.48k
#endif
272
6.48k
#ifdef ENABLE_INTERNAL_WAKEUP
273
6.48k
  multi->wakeup_internal[0] = CURL_SOCKET_BAD;
274
6.48k
  multi->wakeup_internal[1] = CURL_SOCKET_BAD;
275
6.48k
#endif
276
277
6.48k
  if(Curl_uint32_bset_resize(&multi->process, xfer_table_size) ||
278
6.47k
     Curl_uint32_bset_resize(&multi->pending, xfer_table_size) ||
279
6.46k
     Curl_uint32_bset_resize(&multi->dirty, xfer_table_size) ||
280
6.45k
     Curl_uint32_bset_resize(&multi->msgsent, xfer_table_size) ||
281
6.45k
     Curl_uint32_tbl_resize(&multi->xfers, xfer_table_size))
282
34
    goto error;
283
284
6.45k
#ifdef DEBUGBUILD
285
6.45k
  {
286
6.45k
    const char *p;
287
6.45k
    if(getenv("CURL_DEBUG"))
288
0
      multi->admin->set.verbose = TRUE;
289
6.45k
    p = getenv("CURL_DBG_MAX_TOTAL_CONNECTIONS");
290
6.45k
    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
6.45k
  }
297
6.45k
#endif
298
6.45k
  Curl_uint32_tbl_add(&multi->xfers, multi->admin, &multi->admin->mid);
299
6.45k
  Curl_uint32_bset_add(&multi->process, multi->admin->mid);
300
301
6.45k
  Curl_cpool_init(&multi->cpool, NULL, chashsize);
302
303
6.45k
#ifdef USE_SSL
304
6.45k
  if(Curl_ssl_scache_create(sesssize, 2, &multi->ssl_scache))
305
5
    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
6.44k
#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
6.44k
  if(Curl_wakeup_init(multi->wakeup_pair, TRUE) < 0)
319
0
    goto error;
320
6.44k
#endif
321
6.44k
#ifdef ENABLE_INTERNAL_WAKEUP
322
6.44k
  if(Curl_wakeup_init(multi->wakeup_internal, TRUE) < 0)
323
0
    goto error;
324
6.44k
#endif
325
326
6.44k
  if(Curl_probeipv6(multi))
327
0
    goto error;
328
329
6.44k
#ifdef USE_RESOLV_THREADED
330
6.44k
  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
6.44k
  else { /* real multi handle */
335
6.44k
    if(Curl_async_thrdd_multi_init(multi, 0, 20, 2000))
336
12
      goto error;
337
6.44k
  }
338
6.43k
#endif
339
340
6.43k
  return multi;
341
342
51
error:
343
344
51
#ifdef USE_RESOLV_THREADED
345
51
  Curl_async_thrdd_multi_destroy(multi, TRUE);
346
51
#endif
347
51
  Curl_multi_ev_cleanup(multi);
348
51
  Curl_hash_destroy(&multi->proto_hash);
349
51
  Curl_dnscache_destroy(&multi->dnscache);
350
51
  Curl_cpool_destroy(&multi->cpool, multi->admin);
351
51
  Curl_cshutdn_destroy(&multi->cshutdn, multi->admin);
352
51
#ifdef USE_SSL
353
51
  Curl_ssl_scache_destroy(multi->ssl_scache);
354
51
#endif
355
51
  if(multi->admin) {
356
51
    Curl_multi_ev_xfer_done(multi, multi->admin);
357
51
    multi->admin->multi = NULL;
358
51
    Curl_close(&multi->admin);
359
51
  }
360
51
  Curl_mntfy_cleanup(multi);
361
362
51
  Curl_uint32_bset_destroy(&multi->process);
363
51
  Curl_uint32_bset_destroy(&multi->dirty);
364
51
  Curl_uint32_bset_destroy(&multi->pending);
365
51
  Curl_uint32_bset_destroy(&multi->msgsent);
366
51
  Curl_uint32_tbl_destroy(&multi->xfers);
367
51
#ifdef ENABLE_WAKEUP
368
51
  Curl_wakeup_destroy(multi->wakeup_pair);
369
51
#endif
370
51
#ifdef ENABLE_INTERNAL_WAKEUP
371
51
  Curl_wakeup_destroy(multi->wakeup_internal);
372
51
#endif
373
374
51
  curlx_free(multi);
375
51
  return NULL;
376
6.44k
}
377
378
CURLM *curl_multi_init(void)
379
6.54k
{
380
6.54k
  return Curl_multi_handle(CURL_XFER_TABLE_SIZE,
381
6.54k
                           CURL_SOCKET_HASH_TABLE_SIZE,
382
6.54k
                           CURL_CONNECTION_HASH_SIZE,
383
6.54k
                           CURL_DNS_HASH_SIZE,
384
6.54k
                           CURL_TLS_SESSION_SIZE);
385
6.54k
}
386
387
#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
388
static void multi_warn_debug(struct Curl_multi *multi, struct Curl_easy *data)
389
187k
{
390
187k
  if(!multi->warned) {
391
6.43k
    infof(data, "!!! WARNING !!!");
392
6.43k
    infof(data, "This is a debug build of libcurl, "
393
6.43k
                "do not use in production.");
394
6.43k
    multi->warned = TRUE;
395
6.43k
  }
396
187k
}
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
23.7M
{
403
23.7M
  return (data->mstate < MSTATE_DO) || data->state.still_connecting;
404
23.7M
}
405
406
static CURLMcode multi_assess_wakeup(struct Curl_multi *multi)
407
22.1k
{
408
22.1k
#ifdef ENABLE_INTERNAL_WAKEUP
409
22.1k
  if(multi->socket_cb)
410
0
    return Curl_multi_ev_assess_xfer(multi, multi->admin);
411
#else
412
  (void)multi;
413
#endif
414
22.1k
  return CURLM_OK;
415
22.1k
}
416
417
static CURLMcode multi_xfers_add(struct Curl_multi *multi,
418
                                 struct Curl_easy *data)
419
6.43k
{
420
6.43k
  uint32_t capacity = Curl_uint32_tbl_capacity(&multi->xfers);
421
6.43k
  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
6.43k
  const uint32_t max_capacity = UINT_MAX - 1;
426
427
6.43k
  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
6.43k
    uint32_t used = Curl_uint32_tbl_count(&multi->xfers);
432
6.43k
    uint32_t unused = capacity - used;
433
6.43k
    uint32_t min_unused = CURLMAX(capacity >> 3, 4);
434
6.43k
    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
6.43k
  }
446
447
6.43k
  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
6.43k
  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
6.43k
  return CURLM_OK;
469
6.43k
}
470
471
CURLMcode Curl_multi_add_handle(struct Curl_multi *multi,
472
                                struct Curl_easy *data)
473
6.43k
{
474
6.43k
  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
6.43k
  if(data->multi)
479
0
    return CURLM_ADDED_ALREADY;
480
481
6.43k
  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
6.43k
  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
6.43k
  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
6.43k
  if(multi_xfers_add(multi, data))
513
0
    return CURLM_OUT_OF_MEMORY;
514
515
  /* Initialize timeouts for this handle */
516
6.43k
  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
6.43k
  if(data->set.errorbuffer)
525
0
    data->set.errorbuffer[0] = 0;
526
527
6.43k
  data->state.os_errno = 0;
528
529
  /* make the Curl_easy refer back to this multi handle - before
530
     Curl_expire() is called. */
531
6.43k
  data->multi = multi;
532
533
  /* set the easy handle */
534
6.43k
  multistate(data, MSTATE_INIT);
535
  /* not yet passed INIT state */
536
6.43k
  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
6.43k
  Curl_uint32_bset_add(&multi->process, data->mid);
548
6.43k
  ++multi->xfers_alive;
549
6.43k
  ++multi->xfers_total_ever;
550
551
6.43k
  Curl_cpool_xfer_init(data);
552
6.43k
  multi_warn_debug(multi, data);
553
554
  /* Make sure the new handle will run */
555
6.43k
  Curl_multi_mark_dirty(data);
556
557
  /* Necessary in event based processing, where dirty handles trigger
558
   * a timeout callback invocation. */
559
6.43k
  mresult = Curl_update_timer(multi);
560
6.43k
  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
6.43k
  multi->admin->set.timeout = data->set.timeout;
576
6.43k
  multi->admin->set.server_response_timeout =
577
6.43k
    data->set.server_response_timeout;
578
6.43k
  multi->admin->set.no_signal = data->set.no_signal;
579
580
6.43k
  CURL_TRC_M(data, "added to multi, mid=%u, running=%u, total=%u",
581
6.43k
             data->mid, multi_xfers_running(multi),
582
6.43k
             Curl_uint32_tbl_count(&multi->xfers));
583
6.43k
  return CURLM_OK;
584
6.43k
}
585
586
CURLMcode curl_multi_add_handle(CURLM *m, CURL *curl)
587
6.43k
{
588
6.43k
  struct Curl_mapi_guard guard;
589
6.43k
  CURLMcode mresult;
590
591
6.43k
  if(CURL_MAPI_ENTER(&guard, m, multi_add_handle, &mresult)) {
592
6.43k
    struct Curl_easy *data = curl;
593
    /* Verify that we got a somewhat good easy handle too */
594
6.43k
    if(!GOOD_EASY_HANDLE(data) || data->state.internal)
595
0
      mresult = CURLM_BAD_EASY_HANDLE;
596
6.43k
    else
597
6.43k
      mresult = Curl_multi_add_handle(m, data);
598
6.43k
  }
599
6.43k
  CURL_MAPI_LEAVE(&guard);
600
6.43k
  return mresult;
601
6.43k
}
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
6.16k
{
611
  /* if conn->bits.close is TRUE, it means that the connection should be
612
     closed in spite of everything else. */
613
6.16k
  if(conn->bits.close)
614
4.69k
    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
1.47k
  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
1.47k
    )
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
1.47k
  if(!data->set.connect_only && conn->scheme &&
630
1.47k
     !(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
1.47k
  if(premature && !Curl_conn_is_multiplex(conn))
639
148
    return TRUE;
640
641
1.32k
  return FALSE;
642
1.47k
}
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
6.16k
{
649
6.16k
  struct multi_done_ctx *mdctx = userdata;
650
651
6.16k
  (void)pnow;
652
6.16k
  CURL_TRC_M(data, "multi_done_locked, in use=%u", conn->attached_xfers);
653
6.16k
  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
6.16k
  if(multi_conn_should_close(conn, data, (bool)mdctx->premature)) {
661
4.84k
    CURL_TRC_M(data, "multi_done, terminating conn #%" FMT_OFF_T " to %s:%u, "
662
4.84k
               "forbid=%d, close=%d, premature=%d, conn_multiplex=%d",
663
4.84k
               conn->connection_id, conn->origin->user_hostname,
664
4.84k
               conn->origin->port,
665
4.84k
               data->set.reuse_forbid, conn->bits.close, mdctx->premature,
666
4.84k
               Curl_conn_is_multiplex(conn));
667
4.84k
    connclose(conn);
668
4.84k
    return mdctx->premature ? CPOOL_DO_TERMINATE : CPOOL_DO_CLOSE;
669
4.84k
  }
670
1.32k
  else if(!Curl_conn_get_max_concurrent(data, conn)) {
671
0
    CURL_TRC_M(data, "multi_done, conn #%" FMT_OFF_T " to %s:%u was shutdown"
672
0
               " by server, not reusing", conn->connection_id,
673
0
               conn->origin->user_hostname, conn->origin->port);
674
0
    connclose(conn);
675
0
    return mdctx->premature ? CPOOL_DO_TERMINATE : CPOOL_DO_CLOSE;
676
0
  }
677
1.32k
  else {
678
1.32k
    return CPOOL_DO_IDLE;
679
1.32k
  }
680
6.16k
}
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
6.16k
{
687
6.16k
  CURLcode result;
688
6.16k
  struct connectdata *conn = data->conn;
689
6.16k
  const struct curltime *pnow = Curl_pgrs_now(data);
690
691
6.16k
  CURL_TRC_M(data, "multi_done: status: %d prem: %d done: %d",
692
6.16k
             (int)status, (int)premature, data->state.done);
693
694
6.16k
  if(data->state.done) {
695
    /* Stop if multi_done() has already been called */
696
0
    return CURLE_OK;
697
0
  }
698
6.16k
  data->state.done = TRUE; /* called now! */
699
700
  /* Shut down any ongoing async resolver operation. */
701
6.16k
  Curl_resolv_shutdown_all(data);
702
703
  /* Cleanup possible redirect junk */
704
6.16k
  curlx_safefree(data->req.newurl);
705
6.16k
  curlx_safefree(data->req.location);
706
707
6.16k
  switch(status) {
708
8
  case CURLE_ABORTED_BY_CALLBACK:
709
457
  case CURLE_READ_ERROR:
710
457
  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
457
    premature = TRUE;
716
457
    FALLTHROUGH();
717
6.16k
  default:
718
6.16k
    break;
719
6.16k
  }
720
721
  /* this calls the protocol-specific function pointer previously set */
722
6.16k
  if(conn && conn->scheme->run->done && (data->mstate >= MSTATE_PROTOCONNECT))
723
6.01k
    result = conn->scheme->run->done(data, status, premature);
724
148
  else
725
148
    result = status;
726
727
6.16k
  if(data->mstate > MSTATE_CONNECTING &&
728
6.01k
     (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
6.01k
    int rc = Curl_pgrsDone(data, pnow);
734
6.01k
    if(!result && rc)
735
0
      result = CURLE_ABORTED_BY_CALLBACK;
736
6.01k
  }
737
738
  /* Make sure that transfer client writes are really done now. */
739
6.16k
  result = Curl_1st_fatal(result, Curl_xfer_write_done(data, premature));
740
741
6.16k
  if(!result)
742
3.66k
    result = Curl_req_done(&data->req, data, premature);
743
744
6.16k
  if(conn) {
745
    /* Detach connection and decide what to do with it. */
746
6.16k
    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
6.16k
    Curl_conn_ev_data_done(data, premature);
751
6.16k
    Curl_detach_connection(data);
752
6.16k
    Curl_dnscache_prune(data, pnow);
753
754
6.16k
    memset(&mdctx, 0, sizeof(mdctx));
755
6.16k
    mdctx.premature = premature;
756
6.16k
    Curl_cpool_return(data, conn, multi_done_locked, &mdctx, pnow);
757
758
6.16k
    if(data->multi)
759
6.16k
      multi_schedule_pending(data->multi);
760
6.16k
  }
761
762
  /* flush the netrc cache */
763
6.16k
  Curl_netrc_cleanup(&data->state.netrc);
764
6.16k
  return result;
765
6.16k
}
766
767
CURLMcode Curl_multi_remove_handle(struct Curl_multi *multi,
768
                                   struct Curl_easy *data)
769
6.43k
{
770
6.43k
  CURLMcode mresult;
771
6.43k
  uint32_t mid;
772
773
  /* Prevent users from trying to remove same easy handle more than once */
774
6.43k
  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
6.43k
  if(data->multi != multi)
779
0
    return CURLM_BAD_EASY_HANDLE;
780
781
6.43k
  if(data->mid == UINT32_MAX) {
782
0
    DEBUGASSERT(0);
783
0
    return CURLM_INTERNAL_ERROR;
784
0
  }
785
6.43k
  if(Curl_uint32_tbl_get(&multi->xfers, data->mid) != data) {
786
0
    DEBUGASSERT(0);
787
0
    return CURLM_INTERNAL_ERROR;
788
0
  }
789
790
6.43k
  if(data->conn) {
791
3.56k
    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
3.56k
    if(premature && (data->mstate > MSTATE_DO))
796
759
      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
3.56k
    (void)multi_done(data, (CURLcode)data->done, premature);
803
3.56k
  }
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
6.43k
  Curl_expire_clear_all(data);
809
810
  /* In MSGSENT, it was deducted from `multi->xfers_alive` already. */
811
6.43k
  if(data->mstate != MSTATE_MSGSENT)
812
3.56k
    --multi->xfers_alive;
813
814
6.43k
  if(data->state.really_alive) {
815
3.56k
    data->state.really_alive = FALSE;
816
3.56k
    --multi->xfers_really_alive;
817
3.56k
    if(!multi->xfers_really_alive)
818
3.56k
      (void)multi_assess_wakeup(multi);
819
3.56k
  }
820
821
6.43k
  Curl_wildcard_dtor(&data->wildcard);
822
823
6.43k
  data->mstate = MSTATE_COMPLETED;
824
825
  /* Remove the association between the connection and the handle */
826
6.43k
  Curl_detach_connection(data);
827
828
  /* Tell event handling that this transfer is definitely going away */
829
6.43k
  Curl_multi_ev_xfer_done(multi, data);
830
  /* Remove it from any outstanding notifications */
831
6.43k
  Curl_mntfy_remove(data);
832
833
6.43k
  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
6.43k
  mid = data->mid;
866
6.43k
  DEBUGASSERT(Curl_uint32_tbl_contains(&multi->xfers, mid));
867
6.43k
  Curl_uint32_tbl_remove(&multi->xfers, mid);
868
6.43k
  Curl_uint32_bset_remove(&multi->process, mid);
869
6.43k
  Curl_uint32_bset_remove(&multi->dirty, mid);
870
6.43k
  Curl_uint32_bset_remove(&multi->pending, mid);
871
6.43k
  Curl_uint32_bset_remove(&multi->msgsent, mid);
872
6.43k
  data->multi = NULL;
873
6.43k
  data->mid = UINT32_MAX;
874
6.43k
  data->master_mid = UINT32_MAX;
875
876
  /* A pending transfer *might* be able to run now. */
877
6.43k
  multi_schedule_pending(multi);
878
6.43k
  mresult = Curl_update_timer(multi);
879
6.43k
  if(mresult)
880
0
    return mresult;
881
882
6.43k
  mresult = multi_assess_wakeup(multi);
883
6.43k
  if(mresult) {
884
0
    failf(data, "error enabling wakeup listening: %d", mresult);
885
0
    return mresult;
886
0
  }
887
888
6.43k
  CURL_TRC_M(data, "removed from multi, mid=%u, running=%u, total=%u",
889
6.43k
             mid, multi_xfers_running(multi),
890
6.43k
             Curl_uint32_tbl_count(&multi->xfers));
891
6.43k
  return CURLM_OK;
892
6.43k
}
893
894
CURLMcode curl_multi_remove_handle(CURLM *m, CURL *curl)
895
6.43k
{
896
6.43k
  struct Curl_mapi_guard guard;
897
6.43k
  CURLMcode mresult;
898
899
6.43k
  if(CURL_MAPI_ENTER(&guard, m, multi_remove_handle, &mresult)) {
900
6.43k
    struct Curl_easy *data = curl;
901
6.43k
    if(!GOOD_EASY_HANDLE(data) || data->state.internal)
902
0
      mresult = CURLM_BAD_EASY_HANDLE;
903
6.43k
    else
904
6.43k
      mresult = Curl_multi_remove_handle(m, data);
905
6.43k
  }
906
6.43k
  CURL_MAPI_LEAVE(&guard);
907
6.43k
  return mresult;
908
6.43k
}
909
910
/* Return TRUE if the application asked for multiplexing */
911
bool Curl_multiplex_wanted(const struct Curl_multi *multi)
912
0
{
913
0
  return multi && multi->multiplexing;
914
0
}
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
36.7k
{
924
36.7k
  struct connectdata *conn = data->conn;
925
36.7k
  if(conn) {
926
    /* this should never happen, prevent underflow */
927
14.4k
    DEBUGASSERT(conn->attached_xfers);
928
14.4k
    if(conn->attached_xfers) {
929
14.4k
      conn->attached_xfers--;
930
14.4k
      if(!conn->attached_xfers)
931
14.4k
        conn->attached_multi = NULL;
932
14.4k
    }
933
14.4k
  }
934
36.7k
  data->conn = NULL;
935
36.7k
}
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
14.4k
{
948
14.4k
  DEBUGASSERT(data);
949
14.4k
  DEBUGASSERT(!data->conn);
950
14.4k
  DEBUGASSERT(conn);
951
14.4k
  DEBUGASSERT(conn->attached_xfers < UINT32_MAX);
952
14.4k
  data->conn = conn;
953
14.4k
  if(matched) {
954
6.17k
    data->state.last_conn_id = conn->connection_id;
955
6.17k
    data->state.last_cpid = conn->cpid;
956
6.17k
  }
957
8.27k
  else
958
14.4k
    DEBUGASSERT(!data->mid); /* admin handle */
959
14.4k
  conn->attached_xfers++;
960
  /* all attached transfers must be from the same multi */
961
14.4k
  if(!conn->attached_multi)
962
14.4k
    conn->attached_multi = data->multi;
963
14.4k
  DEBUGASSERT(conn->attached_multi == data->multi);
964
965
14.4k
  if(conn->scheme && conn->scheme->run->attach)
966
0
    conn->scheme->run->attach(data, conn);
967
14.4k
}
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
24.2k
{
996
24.2k
  struct connectdata *conn = data->conn;
997
24.2k
  bool send_blocked, recv_blocked;
998
24.2k
  CURLcode result = CURLE_OK;
999
1000
24.2k
  if(!ps->n)
1001
0
    return CURLE_OK;
1002
1003
24.2k
  recv_blocked = (Curl_rlimit_avail(&data->progress.dl.rlimit, NULL) <= 0);
1004
24.2k
  send_blocked = (Curl_rlimit_avail(&data->progress.ul.rlimit, NULL) <= 0);
1005
24.2k
  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
24.2k
  if(!recv_blocked && (data->mstate != MSTATE_RATELIMITING) &&
1013
24.2k
     Curl_pollset_want_recv(data, ps, conn->chain.sock) &&
1014
4.35k
     Curl_conn_data_pending(data)) {
1015
715
    CURL_TRC_M(data, "pollset[] has POLLIN, but there is still "
1016
715
               "buffered input -> mark as dirty");
1017
715
    Curl_multi_mark_dirty(data);
1018
715
  }
1019
24.2k
  return result;
1020
24.2k
}
1021
1022
static CURLcode mstate_connecting_pollset(struct Curl_easy *data,
1023
                                          struct easy_pollset *ps)
1024
393
{
1025
393
  struct connectdata *conn = data->conn;
1026
393
  CURLcode result = CURLE_OK;
1027
1028
393
  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
393
  if(conn->chain.sock != CURL_SOCKET_BAD) {
1034
393
    result = Curl_pollset_change(data, ps, conn->chain.sock, CURL_POLL_IN, 0);
1035
393
    if(!result)
1036
393
      result = multi_adjust_pollset(data, ps);
1037
393
  }
1038
393
  if(!result)
1039
393
    result = Curl_conn_adjust_pollset(data, conn, ps);
1040
393
  return result;
1041
393
}
1042
1043
static CURLcode mstate_protocol_pollset(struct Curl_easy *data,
1044
                                        struct easy_pollset *ps)
1045
2.79k
{
1046
2.79k
  struct connectdata *conn = data->conn;
1047
2.79k
  CURLcode result = CURLE_OK;
1048
1049
2.79k
  if(conn->scheme->run->proto_pollset)
1050
2.79k
    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
2.79k
  if(!result)
1059
2.79k
    result = multi_adjust_pollset(data, ps);
1060
2.79k
  if(!result)
1061
2.79k
    result = Curl_conn_adjust_pollset(data, conn, ps);
1062
2.79k
  return result;
1063
2.79k
}
1064
1065
static CURLcode mstate_do_pollset(struct Curl_easy *data,
1066
                                  struct easy_pollset *ps)
1067
1.13k
{
1068
1.13k
  struct connectdata *conn = data->conn;
1069
1.13k
  CURLcode result = CURLE_OK;
1070
1071
1.13k
  if(conn->scheme->run->doing_pollset)
1072
1.13k
    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
1.13k
  if(!result)
1078
1.13k
    result = multi_adjust_pollset(data, ps);
1079
1.13k
  if(!result)
1080
1.13k
    result = Curl_conn_adjust_pollset(data, conn, ps);
1081
1.13k
  return result;
1082
1.13k
}
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
19.9k
{
1106
19.9k
  struct connectdata *conn = data->conn;
1107
19.9k
  CURLcode result = CURLE_OK;
1108
1109
19.9k
  if(conn->scheme->run->perform_pollset)
1110
0
    result = conn->scheme->run->perform_pollset(data, ps);
1111
19.9k
  else {
1112
    /* Default is to obey the request flags for send/recv */
1113
19.9k
    if(Curl_req_want_recv(data) && conn->bits.want_recv) {
1114
56
      result = Curl_pollset_add_in(data, ps, conn->chain.sock);
1115
56
    }
1116
19.9k
    if(!result && Curl_req_want_send(data) && conn->bits.want_send) {
1117
19.8k
      result = Curl_pollset_add_out(data, ps, conn->chain.sock);
1118
19.8k
    }
1119
19.9k
  }
1120
19.9k
  if(!result)
1121
19.9k
    result = multi_adjust_pollset(data, ps);
1122
19.9k
  if(!result)
1123
19.9k
    result = Curl_conn_adjust_pollset(data, conn, ps);
1124
19.9k
  return result;
1125
19.9k
}
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
0
{
1149
0
  size_t n = 0;
1150
0
  uint8_t eid = timeouts->first;
1151
0
  for(; eid < EXPIRE_LAST; eid = timeouts->next[eid])
1152
0
    ++n;
1153
0
  return n;
1154
0
}
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
48.5k
{
1162
48.5k
  CURLcode result = CURLE_OK;
1163
1164
48.5k
  Curl_pollset_reset(ps);
1165
48.5k
#ifdef ENABLE_INTERNAL_WAKEUP
1166
  /* The admin handle always listens on the wakeup socket when there
1167
   * are transfers alive. */
1168
48.5k
  if(data->multi && (data == data->multi->admin) &&
1169
24.2k
     data->multi->xfers_really_alive) {
1170
24.2k
    CURL_TRC_M(data, "adding wakeup, %u xfers really alive",
1171
24.2k
               data->multi->xfers_really_alive);
1172
24.2k
    result = Curl_pollset_add_in(data, ps, data->multi->wakeup_internal[0]);
1173
24.2k
  }
1174
48.5k
#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
48.5k
  if(!result && data->conn) {
1179
24.2k
    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
393
    case MSTATE_CONNECTING:
1188
393
      result = mstate_connecting_pollset(data, ps);
1189
393
      break;
1190
1191
0
    case MSTATE_PROTOCONNECT:
1192
2.79k
    case MSTATE_PROTOCONNECTING:
1193
2.79k
      result = mstate_protocol_pollset(data, ps);
1194
2.79k
      break;
1195
1196
0
    case MSTATE_DO:
1197
1.13k
    case MSTATE_DOING:
1198
1.13k
      result = mstate_do_pollset(data, ps);
1199
1.13k
      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
19.9k
    case MSTATE_PERFORMING:
1207
19.9k
      result = mstate_perform_pollset(data, ps);
1208
19.9k
      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
24.2k
    }
1226
24.2k
  }
1227
1228
48.5k
  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
48.5k
#ifdef CURLVERBOSE
1236
48.5k
  if(CURL_TRC_M_is_verbose(data)) {
1237
0
    size_t timeout_count = multi_timeouts_count(&data->state.timeouts);
1238
0
    switch(ps->n) {
1239
0
    case 0:
1240
0
      CURL_TRC_M(data, "pollset[], timeouts=%zu, paused %d/%d (r/w)",
1241
0
                 timeout_count,
1242
0
                 Curl_xfer_send_is_paused(data),
1243
0
                 Curl_xfer_recv_is_paused(data));
1244
0
      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
0
    }
1267
0
    CURL_TRC_EASY_TIMERS(data);
1268
0
  }
1269
48.5k
#endif
1270
1271
48.5k
  return CURLM_OK;
1272
48.5k
}
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
6.93k
{
1278
6.93k
  struct Curl_mapi_guard guard;
1279
6.93k
  CURLMcode mresult;
1280
1281
6.93k
  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
6.93k
    struct Curl_multi *multi = m;
1286
6.93k
    struct easy_pollset ps;
1287
6.93k
    int this_max_fd = -1;
1288
6.93k
    unsigned int i;
1289
6.93k
    uint32_t mid;
1290
6.93k
    (void)exc_fd_set;
1291
1292
6.93k
    Curl_pollset_init(&ps);
1293
6.93k
    if(Curl_uint32_bset_first(&multi->process, &mid)) {
1294
13.8k
      do {
1295
13.8k
        struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
1296
1297
13.8k
        if(!data) {
1298
0
          DEBUGASSERT(0);
1299
0
          continue;
1300
0
        }
1301
1302
13.8k
        Curl_multi_pollset(data, &ps);
1303
27.7k
        for(i = 0; i < ps.n; i++) {
1304
13.8k
          if(!FDSET_SOCK(ps.sockets[i]))
1305
            /* pretend it does not exist */
1306
0
            continue;
1307
13.8k
          if(ps.actions[i] & CURL_POLL_IN)
1308
13.8k
            FD_SET(ps.sockets[i], read_fd_set);
1309
13.8k
          if(ps.actions[i] & CURL_POLL_OUT)
1310
13.8k
            FD_SET(ps.sockets[i], write_fd_set);
1311
13.8k
          if((int)ps.sockets[i] > this_max_fd)
1312
13.8k
            this_max_fd = (int)ps.sockets[i];
1313
13.8k
        }
1314
13.8k
      } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
1315
6.93k
    }
1316
1317
6.93k
    Curl_cshutdn_setfds(&multi->cshutdn, multi->admin,
1318
6.93k
                        read_fd_set, write_fd_set, &this_max_fd);
1319
1320
6.93k
    *max_fd = this_max_fd;
1321
6.93k
    Curl_pollset_cleanup(&ps);
1322
1323
6.93k
    mresult = CURLM_OK;
1324
6.93k
  }
1325
6.93k
  CURL_MAPI_LEAVE(&guard);
1326
6.93k
  return mresult;
1327
6.93k
}
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
17.3k
{
1512
17.3k
  CURLMcode mresult = CURLM_OK;
1513
17.3k
  int nevents = 0;
1514
17.3k
  size_t i;
1515
1516
17.3k
  (void)multi;
1517
17.3k
  if(cpfds->n) {
1518
17.3k
    int pollrc = Curl_poll(cpfds->pfds, cpfds->n, timeout_ms); /* wait... */
1519
17.3k
    if(pollrc < 0) {
1520
0
      mresult = CURLM_UNRECOVERABLE_POLL;
1521
0
      goto out;
1522
0
    }
1523
17.3k
    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
17.3k
    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
17.3k
  }
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
17.3k
out:
1548
17.3k
  *pnevents = nevents;
1549
17.3k
  return mresult;
1550
17.3k
}
1551
1552
#endif /* !USE_WINSOCK */
1553
1554
17.3k
#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
17.3k
{
1563
17.3k
  size_t i;
1564
17.3k
  int timeout_internal;
1565
17.3k
  int nevents = 0;
1566
17.3k
  struct easy_pollset ps;
1567
17.3k
  struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];
1568
17.3k
  struct curl_pollfds cpfds;
1569
17.3k
  unsigned int curl_nfds = 0; /* how many pfds are for curl transfers */
1570
17.3k
  struct Curl_easy *data = NULL;
1571
17.3k
  CURLMcode mresult = CURLM_OK;
1572
17.3k
  uint32_t mid;
1573
17.3k
#ifdef ENABLE_WAKEUP
1574
17.3k
  int wakeup_idx = -1;
1575
17.3k
#endif
1576
1577
17.3k
  if(timeout_ms < 0)
1578
0
    return CURLM_BAD_FUNCTION_ARGUMENT;
1579
1580
17.3k
  Curl_pollset_init(&ps);
1581
17.3k
  Curl_pollfds_init(&cpfds, a_few_on_stack, NUM_POLLS_ON_STACK);
1582
1583
  /* Add the curl handles to our pollfds first */
1584
17.3k
  if(Curl_uint32_bset_first(&multi->process, &mid)) {
1585
34.6k
    do {
1586
34.6k
      data = Curl_multi_get_easy(multi, mid);
1587
34.6k
      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
34.6k
      Curl_multi_pollset(data, &ps);
1594
34.6k
      if(Curl_pollfds_add_ps(&cpfds, &ps)) {
1595
0
        mresult = CURLM_OUT_OF_MEMORY;
1596
0
        goto out;
1597
0
      }
1598
34.6k
    } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
1599
17.3k
  }
1600
1601
17.3k
  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
17.3k
#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
17.3k
  if(extrawait || cpfds.n || extra_nfds) {
1612
17.3k
    wakeup_idx = cpfds.n;
1613
17.3k
    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
17.3k
  }
1618
17.3k
#endif
1619
1620
17.3k
  curl_nfds = cpfds.n; /* what curl internally uses in cpfds */
1621
  /* Add external file descriptions from poll-like struct curl_waitfd */
1622
17.3k
  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
17.3k
  multi_timeout(multi, NULL, &timeout_internal);
1643
17.3k
  if((timeout_internal >= 0) && (timeout_internal < timeout_ms))
1644
0
    timeout_ms = timeout_internal;
1645
1646
17.3k
  if(data)
1647
17.3k
    CURL_TRC_M(data, "multi_wait(fds=%u, timeout=%d) tinternal=%d",
1648
17.3k
               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
17.3k
  mresult = multi_posix_poll(multi, &cpfds, curl_nfds,
1656
17.3k
                             extra_fds, extra_nfds,
1657
17.3k
                             timeout_ms, extrawait, &nevents);
1658
17.3k
#endif
1659
1660
17.3k
#ifdef ENABLE_WAKEUP
1661
17.3k
    if(nevents && (wakeup_idx >= 0)) {
1662
5.74k
      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
5.74k
    }
1668
17.3k
#endif
1669
1670
17.3k
out:
1671
17.3k
  Curl_pollset_cleanup(&ps);
1672
17.3k
  Curl_pollfds_cleanup(&cpfds);
1673
17.3k
  if(ret)
1674
17.3k
    *ret = nevents;
1675
17.3k
  return mresult;
1676
17.3k
}
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
17.3k
{
1700
17.3k
  struct Curl_mapi_guard guard;
1701
17.3k
  CURLMcode mresult;
1702
1703
17.3k
  if(CURL_MAPI_ENTER(&guard, m, multi_poll, &mresult)) {
1704
17.3k
    mresult = multi_wait(m, extra_fds, extra_nfds, timeout_ms, ret, TRUE);
1705
17.3k
  }
1706
17.3k
  CURL_MAPI_LEAVE(&guard);
1707
17.3k
  return mresult;
1708
17.3k
}
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
0
{
1739
  /* This is expected to be invocable from another thread which
1740
   * does NOT outlive the multi handle. Check for sanity. */
1741
0
  if(GOOD_MULTI_HANDLE(multi))
1742
0
    Curl_wakeup_signal(multi->wakeup_internal);
1743
0
  else
1744
0
    DEBUGASSERT(0);
1745
0
}
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
348k
{
1758
348k
  bool retval = FALSE;
1759
348k
  DEBUGASSERT(multi);
1760
348k
  if(multi) {
1761
348k
    retval = (bool)multi->recheckstate;
1762
348k
    if(clear)
1763
222k
      multi->recheckstate = FALSE;
1764
348k
  }
1765
348k
  return retval;
1766
348k
}
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
6.17k
{
1777
  /* May be invoked for a `data->multi` when no multi is present. */
1778
6.17k
  if(multi)
1779
6.17k
    multi->recheckstate = TRUE;
1780
6.17k
}
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
3.04k
{
1810
3.04k
  CURLcode result = CURLE_OK;
1811
3.04k
  struct connectdata *conn = data->conn;
1812
1813
3.04k
  DEBUGASSERT(conn);
1814
3.04k
  DEBUGASSERT(conn->scheme);
1815
1816
3.04k
  if(conn->scheme->run->do_it)
1817
3.04k
    result = conn->scheme->run->do_it(data, done);
1818
1819
3.04k
  return result;
1820
3.04k
}
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
156k
{
1853
156k
  timediff_t timeout_ms;
1854
1855
156k
  timeout_ms = connect_timeout ? Curl_timeleft_now_ms(data, pnow) :
1856
156k
               Curl_timeleft_total_now_ms(data, pnow);
1857
156k
  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
156k
  return FALSE;
1897
156k
}
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
36.3k
{
1907
36.3k
  CURLcode result = CURLE_OK;
1908
36.3k
  struct connectdata *conn = data->conn;
1909
1910
36.3k
  if(conn && conn->scheme->run->connecting) {
1911
36.3k
    *done = FALSE;
1912
36.3k
    result = conn->scheme->run->connecting(data, done);
1913
36.3k
  }
1914
0
  else
1915
0
    *done = TRUE;
1916
1917
36.3k
  return result;
1918
36.3k
}
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
11.0k
{
1927
11.0k
  CURLcode result = CURLE_OK;
1928
11.0k
  struct connectdata *conn = data->conn;
1929
1930
11.0k
  if(conn && conn->scheme->run->doing) {
1931
11.0k
    *done = FALSE;
1932
11.0k
    result = conn->scheme->run->doing(data, done);
1933
11.0k
  }
1934
0
  else
1935
0
    *done = TRUE;
1936
1937
11.0k
  return result;
1938
11.0k
}
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
6.01k
{
1947
6.01k
  struct connectdata *conn = data->conn;
1948
6.01k
  CURLcode result = CURLE_OK;
1949
1950
6.01k
  DEBUGASSERT(conn);
1951
6.01k
  DEBUGASSERT(protocol_done);
1952
6.01k
  DEBUGASSERT(Curl_conn_is_connected(conn));
1953
1954
6.01k
  *protocol_done = FALSE;
1955
6.01k
  if(!conn->bits.protoconnstart) {
1956
6.01k
    if(conn->scheme->run->connect_it) {
1957
      /* Call the protocol-specific connect function */
1958
6.01k
      result = conn->scheme->run->connect_it(data, protocol_done);
1959
6.01k
      if(result)
1960
115
        return result;
1961
6.01k
    }
1962
5.90k
    conn->bits.protoconnstart = TRUE;
1963
5.90k
  }
1964
1965
  /* Unless this protocol does not have any protocol-connect callback, as
1966
     then we know we are done. */
1967
5.90k
  if(!conn->scheme->run->connecting)
1968
0
    *protocol_done = TRUE;
1969
5.90k
  return CURLE_OK;
1970
6.01k
}
1971
1972
/*
1973
 * posttransfer() is called immediately after a transfer ends
1974
 */
1975
static void multi_posttransfer(struct Curl_easy *data)
1976
2.74k
{
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
2.74k
  (void)data;
1983
2.74k
#endif
1984
2.74k
}
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
0
{
1997
0
  if(handler && handler->run->follow)
1998
0
    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
83.3k
{
2010
83.3k
  if(Curl_rlimit_active(&data->progress.dl.rlimit) ||
2011
83.3k
     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
83.3k
  if(data->mstate != MSTATE_PERFORMING) {
2046
0
    CURL_TRC_M(data, "[RLIMIT] wait over, continue");
2047
0
    multistate(data, MSTATE_PERFORMING);
2048
0
  }
2049
83.3k
  return CURLE_OK;
2050
83.3k
}
2051
2052
static CURLMcode multistate_performing(struct Curl_easy *data,
2053
                                       bool *stream_errorp,
2054
                                       CURLcode *resultp)
2055
42.5k
{
2056
42.5k
  char *newurl = NULL;
2057
42.5k
  bool retry = FALSE;
2058
42.5k
  CURLMcode mresult = CURLM_OK;
2059
42.5k
  CURLcode result = *resultp = CURLE_OK;
2060
42.5k
  *stream_errorp = FALSE;
2061
2062
42.5k
  if(mspeed_check(data) == CURLE_AGAIN)
2063
0
    return CURLM_OK;
2064
2065
  /* read/write data if it is ready to do so */
2066
42.5k
  result = Curl_sendrecv(data);
2067
2068
42.5k
  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
1.22k
    CURLcode ret = Curl_retry_request(data, &newurl);
2074
1.22k
    if(!ret)
2075
1.22k
      retry = !!newurl;
2076
0
    else if(!result)
2077
0
      result = ret;
2078
2079
1.22k
    if(retry) {
2080
      /* if we are to retry, set the result to OK and consider the
2081
         request as done */
2082
0
      result = CURLE_OK;
2083
0
      data->req.done = TRUE;
2084
0
    }
2085
1.22k
  }
2086
41.3k
#ifndef CURL_DISABLE_HTTP
2087
41.3k
  else if(!data->set.never_retry &&
2088
41.3k
          (result == CURLE_HTTP2_STREAM) &&
2089
0
          Curl_h2_http_1_1_error(data)) {
2090
0
    CURLcode ret = Curl_retry_request(data, &newurl);
2091
2092
0
    if(!ret) {
2093
0
      infof(data, "Downgrades to HTTP/1.1");
2094
0
      streamclose(data->conn);
2095
0
      data->state.http_neg.wanted = CURL_HTTP_V1x;
2096
0
      data->state.http_neg.allowed = CURL_HTTP_V1x;
2097
      /* clear the error message bit too as we ignore the one we got */
2098
0
      data->state.errorbuf = FALSE;
2099
0
      if(!newurl)
2100
        /* typically for HTTP_1_1_REQUIRED error on first flight */
2101
0
        newurl = Curl_bufref_dup(&data->state.url);
2102
0
      if(!newurl) {
2103
0
        result = CURLE_OUT_OF_MEMORY;
2104
0
      }
2105
0
      else {
2106
        /* if we are to retry, set the result to OK and consider the request
2107
          as done */
2108
0
        retry = TRUE;
2109
0
        result = CURLE_OK;
2110
0
        data->req.done = TRUE;
2111
0
      }
2112
0
    }
2113
0
    else
2114
0
      result = ret;
2115
0
  }
2116
42.5k
#endif
2117
2118
42.5k
  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
488
    if(!(data->conn->scheme->flags & PROTOPT_DUAL) &&
2128
488
       result != CURLE_HTTP2_STREAM)
2129
488
      streamclose(data->conn);
2130
2131
488
    multi_posttransfer(data);
2132
488
    multi_done(data, result, TRUE);
2133
488
  }
2134
42.0k
  else if(data->req.done && !Curl_cwriter_is_paused(data)) {
2135
1.22k
    const struct Curl_scheme *handler = data->conn->scheme;
2136
2137
    /* call this even if the readwrite function returned error */
2138
1.22k
    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
1.22k
    if(data->req.newurl || retry) {
2143
0
      followtype follow = FOLLOW_NONE;
2144
0
      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
0
      else
2153
0
        follow = FOLLOW_RETRY;
2154
0
      (void)multi_done(data, CURLE_OK, FALSE);
2155
      /* multi_done() might return CURLE_GOT_NOTHING */
2156
0
      result = multi_follow(data, handler, newurl, follow);
2157
0
      if(!result) {
2158
0
        multistate(data, MSTATE_SETUP);
2159
0
        mresult = CURLM_CALL_MULTI_PERFORM;
2160
0
      }
2161
0
    }
2162
1.22k
    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
1.22k
      if(data->req.location) {
2168
0
        curlx_free(newurl);
2169
0
        newurl = data->req.location;
2170
0
        data->req.location = NULL;
2171
0
        result = multi_follow(data, handler, newurl, FOLLOW_FAKE);
2172
0
        if(result) {
2173
0
          *stream_errorp = TRUE;
2174
0
          result = multi_done(data, result, TRUE);
2175
0
        }
2176
0
      }
2177
2178
1.22k
      if(!result) {
2179
1.22k
        multistate(data, MSTATE_DONE);
2180
1.22k
        mresult = CURLM_CALL_MULTI_PERFORM;
2181
1.22k
      }
2182
1.22k
    }
2183
1.22k
  }
2184
40.8k
  else { /* not errored, not done */
2185
40.8k
    mspeed_check(data);
2186
40.8k
  }
2187
42.5k
  curlx_free(newurl);
2188
42.5k
  *resultp = result;
2189
42.5k
  return mresult;
2190
42.5k
}
2191
2192
static CURLMcode multistate_do(struct Curl_easy *data,
2193
                               bool *stream_errorp,
2194
                               CURLcode *resultp)
2195
3.04k
{
2196
3.04k
  CURLMcode mresult = CURLM_OK;
2197
3.04k
  CURLcode result = CURLE_OK;
2198
3.04k
  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
3.04k
  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
3.04k
  else {
2226
3.04k
    bool dophase_done = FALSE;
2227
    /* Perform the protocol's DO action */
2228
3.04k
    result = multi_do(data, &dophase_done);
2229
2230
3.04k
    if(!result) {
2231
2.79k
      if(!dophase_done) {
2232
2.40k
#ifndef CURL_DISABLE_FTP
2233
        /* some steps needed for wildcard matching */
2234
2.40k
        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
2.40k
#endif
2247
        /* DO was not completed in one function call, we must continue
2248
           DOING... */
2249
2.40k
        multistate(data, MSTATE_DOING);
2250
2.40k
        mresult = CURLM_CALL_MULTI_PERFORM;
2251
2.40k
      }
2252
2253
      /* after DO, go DO_DONE... or DO_MORE */
2254
388
      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
388
      else {
2261
        /* we are done with the DO, now DID */
2262
388
        multistate(data, MSTATE_DID);
2263
388
        mresult = CURLM_CALL_MULTI_PERFORM;
2264
388
      }
2265
2.79k
    }
2266
244
    else if((result == CURLE_SEND_ERROR) &&
2267
0
            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
244
    else {
2316
      /* failure detected */
2317
244
      multi_posttransfer(data);
2318
244
      if(data->conn)
2319
244
        multi_done(data, result, FALSE);
2320
244
      *stream_errorp = TRUE;
2321
244
    }
2322
3.04k
  }
2323
3.04k
end:
2324
3.04k
  *resultp = result;
2325
3.04k
  return mresult;
2326
3.04k
}
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
6.39k
{
2369
  /* Connect. We want to get a connection identifier filled in. This state can
2370
     be entered from SETUP and from PENDING. */
2371
6.39k
  bool connected;
2372
6.39k
  CURLMcode mresult = CURLM_OK;
2373
6.39k
  CURLcode result = Curl_connect(data, &connected);
2374
6.39k
  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
0
    multistate(data, MSTATE_PENDING);
2378
    /* move from process to pending set */
2379
0
    Curl_uint32_bset_remove(&multi->process, data->mid);
2380
0
    Curl_uint32_bset_remove(&multi->dirty, data->mid);
2381
0
    Curl_uint32_bset_add(&multi->pending, data->mid);
2382
0
    *resultp = CURLE_OK;
2383
0
    return mresult;
2384
0
  }
2385
6.39k
  else
2386
6.39k
    multi_schedule_pending(data->multi);
2387
2388
6.39k
  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
6.16k
    mresult = CURLM_CALL_MULTI_PERFORM;
2393
2394
6.16k
    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
6.16k
    else {
2402
6.16k
      multistate(data, MSTATE_CONNECTING);
2403
6.16k
    }
2404
6.16k
  }
2405
6.39k
  *resultp = result;
2406
6.39k
  return mresult;
2407
6.39k
}
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
131k
{
2416
131k
  if(data->mstate < MSTATE_COMPLETED) {
2417
130k
    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
1.54k
      multi_schedule_pending(multi); /* connection */
2428
2429
1.54k
      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
1.54k
      else if(data->mstate == MSTATE_CONNECT) {
2443
        /* Curl_connect() failed */
2444
230
        multi_posttransfer(data);
2445
230
        Curl_pgrsUpdate_nometer(data);
2446
230
      }
2447
2448
1.54k
      multistate(data, MSTATE_COMPLETED);
2449
1.54k
      return result;
2450
1.54k
    }
2451
    /* if there is still a connection to use, call the progress function */
2452
129k
    else if(data->conn && Curl_conn_is_connected(data->conn)) {
2453
105k
      result = Curl_pgrsUpdateX(data, pnow);
2454
105k
      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
105k
    }
2465
130k
  }
2466
130k
  return result;
2467
131k
}
2468
2469
static void handle_completed(struct Curl_multi *multi,
2470
                             struct Curl_easy *data,
2471
                             CURLcode result)
2472
2.87k
{
2473
2.87k
  bool msg_to_app = data->master_mid == UINT32_MAX;
2474
2475
2.87k
  if(!msg_to_app) {
2476
    /* A sub transfer, not reported to the application. Is anyone still
2477
     * interested in processing its results? */
2478
0
    if(data->sub_xfer_done) {
2479
0
      struct Curl_easy *master = Curl_multi_get_easy(multi, data->master_mid);
2480
2481
0
      CURL_TRC_M(data, "sub xfer done for master %u", data->master_mid);
2482
0
      if(master)
2483
0
        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
0
    }
2487
0
  }
2488
2.87k
  else {
2489
    /* now fill in the CURLMsg with this info */
2490
2.87k
    struct CURLMsg *msg = &data->msg;
2491
2492
2.87k
    msg->msg = CURLMSG_DONE;
2493
2.87k
    msg->easy_handle = data;
2494
2.87k
    msg->data.result = result;
2495
2496
2.87k
    DEBUGASSERT(!data->conn);
2497
2.87k
  }
2498
2.87k
  multistate(data, MSTATE_MSGSENT);
2499
2500
  /* remove from the other sets */
2501
2.87k
  Curl_uint32_bset_remove(&multi->process, data->mid);
2502
2.87k
  Curl_uint32_bset_remove(&multi->dirty, data->mid);
2503
2.87k
  Curl_uint32_bset_remove(&multi->pending, data->mid);
2504
2.87k
  if(msg_to_app)
2505
2.87k
    multi_addmsg(multi, data);
2506
2.87k
  if(data->state.really_alive) {
2507
2.87k
    data->state.really_alive = FALSE;
2508
2.87k
    --multi->xfers_really_alive;
2509
2.87k
    if(!multi->xfers_really_alive)
2510
2.87k
      (void)multi_assess_wakeup(multi);
2511
2.87k
  }
2512
2.87k
  --multi->xfers_alive;
2513
2.87k
  if(!multi->xfers_alive)
2514
2.87k
    multi_assess_wakeup(multi);
2515
2.87k
}
2516
2517
static CURLMcode multistate_init(struct Curl_easy *data, CURLcode *result)
2518
6.43k
{
2519
6.43k
  if(!data->state.really_alive) {
2520
6.43k
    data->state.really_alive = TRUE;
2521
6.43k
    ++data->multi->xfers_really_alive;
2522
6.43k
    if(data->multi->xfers_really_alive == 1) {
2523
6.43k
      CURLMcode mresult = multi_assess_wakeup(data->multi);
2524
6.43k
      if(mresult) {
2525
0
        failf(data, "error enabling wakeup listening: %d", mresult);
2526
0
        return mresult;
2527
0
      }
2528
6.43k
    }
2529
6.43k
  }
2530
2531
6.43k
  *result = Curl_pretransfer(data);
2532
6.43k
  if(*result)
2533
36
    return CURLM_OK;
2534
2535
  /* after init, go SETUP */
2536
6.39k
  multistate(data, MSTATE_SETUP);
2537
6.39k
  Curl_pgrsTime(data, TIMER_STARTOP);
2538
6.39k
  return CURLM_CALL_MULTI_PERFORM;
2539
6.43k
}
2540
2541
static CURLMcode multistate_setup(struct Curl_easy *data)
2542
6.39k
{
2543
6.39k
  const struct curltime *pnow = Curl_pgrs_now(data);
2544
6.39k
  timediff_t ctimeout_ms = (data->set.connecttimeout > 0) ?
2545
6.39k
    data->set.connecttimeout : DEFAULT_CONNECT_TIMEOUT;
2546
2547
6.39k
  Curl_pgrsTimeWas(data, TIMER_STARTSINGLE, *pnow);
2548
6.39k
  if(data->set.timeout)
2549
6.39k
    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
6.39k
  Curl_expire_set(data, EXPIRE_CONNECTTIMEOUT, ctimeout_ms, pnow);
2554
2555
6.39k
  multistate(data, MSTATE_CONNECT);
2556
6.39k
  return CURLM_CALL_MULTI_PERFORM;
2557
6.39k
}
2558
2559
static CURLMcode multistate_connecting(struct Curl_easy *data,
2560
                                       bool *stream_error,
2561
                                       CURLcode *result)
2562
10.3k
{
2563
10.3k
  bool connected;
2564
2565
10.3k
  if(!data->conn) {
2566
0
    DEBUGASSERT(0);
2567
0
    *result = CURLE_FAILED_INIT;
2568
0
    return CURLM_OK;
2569
0
  }
2570
10.3k
  if(!Curl_xfer_recv_is_paused(data)) {
2571
10.3k
    *result = Curl_conn_connect(data, FALSE, &connected);
2572
10.3k
    if(connected && !*result) {
2573
6.01k
      if(!data->conn->bits.reuse && Curl_conn_is_multiplex(data->conn)) {
2574
        /* new connection, can multiplex, wake pending handles */
2575
0
        multi_schedule_pending(data->multi);
2576
0
      }
2577
6.01k
      multistate(data, MSTATE_PROTOCONNECT);
2578
6.01k
      return CURLM_CALL_MULTI_PERFORM;
2579
6.01k
    }
2580
4.37k
    else if(*result) {
2581
      /* failure detected */
2582
65
      CURL_TRC_M(data, "connect failed -> %d", (int)*result);
2583
65
      multi_posttransfer(data);
2584
65
      multi_done(data, *result, TRUE);
2585
65
      *stream_error = TRUE;
2586
65
      return CURLM_OK;
2587
65
    }
2588
10.3k
  }
2589
4.31k
  return CURLM_OK;
2590
10.3k
}
2591
2592
static CURLMcode multistate_protoconnect(struct Curl_easy *data,
2593
                                         bool *stream_error,
2594
                                         CURLcode *result)
2595
6.01k
{
2596
6.01k
  bool protocol_connected = FALSE;
2597
2598
6.01k
  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
6.01k
  if(!*result)
2607
6.01k
    *result = protocol_connect(data, &protocol_connected);
2608
6.01k
  if(!*result && !protocol_connected) {
2609
    /* switch to waiting state */
2610
5.89k
    multistate(data, MSTATE_PROTOCONNECTING);
2611
5.89k
    return CURLM_CALL_MULTI_PERFORM;
2612
5.89k
  }
2613
120
  else if(!*result) {
2614
    /* protocol connect has completed, go WAITDO or DO */
2615
5
    multistate(data, MSTATE_DO);
2616
5
    return CURLM_CALL_MULTI_PERFORM;
2617
5
  }
2618
2619
  /* failure detected */
2620
115
  multi_posttransfer(data);
2621
115
  multi_done(data, *result, TRUE);
2622
115
  *stream_error = TRUE;
2623
115
  return CURLM_OK;
2624
6.01k
}
2625
2626
static CURLMcode multistate_protoconnecting(struct Curl_easy *data,
2627
                                            bool *stream_error,
2628
                                            CURLcode *result)
2629
36.3k
{
2630
36.3k
  bool protocol_connected = FALSE;
2631
2632
  /* protocol-specific connect phase */
2633
36.3k
  *result = protocol_connecting(data, &protocol_connected);
2634
36.3k
  if(!*result && protocol_connected) {
2635
    /* after the connect has completed, go WAITDO or DO */
2636
3.03k
    multistate(data, MSTATE_DO);
2637
3.03k
    return CURLM_CALL_MULTI_PERFORM;
2638
3.03k
  }
2639
33.3k
  else if(*result) {
2640
    /* failure detected */
2641
143
    multi_posttransfer(data);
2642
143
    multi_done(data, *result, TRUE);
2643
143
    *stream_error = TRUE;
2644
143
  }
2645
33.3k
  return CURLM_OK;
2646
36.3k
}
2647
2648
static CURLMcode multistate_doing(struct Curl_easy *data,
2649
                                  bool *stream_error,
2650
                                  CURLcode *result)
2651
11.0k
{
2652
11.0k
  bool dophase_done = FALSE;
2653
2654
  /* we continue DOING until the DO phase is complete */
2655
11.0k
  DEBUGASSERT(data->conn);
2656
11.0k
  *result = protocol_doing(data, &dophase_done);
2657
11.0k
  if(!*result) {
2658
10.8k
    if(dophase_done) {
2659
      /* after DO, go DO_DONE or DO_MORE */
2660
1.54k
      multistate(data, data->conn->bits.do_more ?
2661
1.54k
                 MSTATE_DOING_MORE : MSTATE_DID);
2662
1.54k
      return CURLM_CALL_MULTI_PERFORM;
2663
1.54k
    } /* dophase_done */
2664
10.8k
  }
2665
227
  else {
2666
    /* failure detected */
2667
227
    multi_posttransfer(data);
2668
227
    multi_done(data, *result, FALSE);
2669
227
    *stream_error = TRUE;
2670
227
  }
2671
9.48k
  return CURLM_OK;
2672
11.0k
}
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
1.93k
{
2708
1.93k
  DEBUGASSERT(data->conn);
2709
1.93k
  if(data->conn->bits.multiplex)
2710
    /* Check if we can move pending requests to send pipe */
2711
0
    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
1.93k
  if(data->conn->bits.want_recv || data->conn->bits.want_send) {
2716
1.83k
    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
1.83k
    return ((multi->xfers_alive > 1) &&
2722
0
            (data->conn->scheme->protocol & PROTO_FAMILY_HTTP)) ?
2723
1.83k
           CURLM_OK : CURLM_CALL_MULTI_PERFORM;
2724
1.83k
  }
2725
94
  else {
2726
94
#ifndef CURL_DISABLE_FTP
2727
94
    if(data->state.wildcardmatch &&
2728
0
       ((data->conn->scheme->flags & PROTOPT_WILDCARD) == 0)) {
2729
0
      data->wildcard->state = CURLWC_DONE;
2730
0
    }
2731
94
#endif
2732
94
    multistate(data, MSTATE_DONE);
2733
94
    return CURLM_CALL_MULTI_PERFORM;
2734
94
  }
2735
1.93k
}
2736
2737
static CURLMcode multistate_done(struct Curl_easy *data, CURLcode *presult)
2738
1.32k
{
2739
1.32k
  if(data->conn) {
2740
1.32k
    CURLcode result;
2741
2742
    /* post-transfer command */
2743
1.32k
    result = multi_done(data, *presult, FALSE);
2744
2745
    /* allow a previously set error code take precedence */
2746
1.32k
    if(!(*presult))
2747
1.32k
      *presult = result;
2748
1.32k
  }
2749
2750
1.32k
#ifndef CURL_DISABLE_FTP
2751
1.32k
  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
1.32k
#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
1.32k
  multistate(data, MSTATE_COMPLETED);
2763
1.32k
  return CURLM_CALL_MULTI_PERFORM;
2764
1.32k
}
2765
2766
static CURLMcode multi_runsingle(struct Curl_multi *multi,
2767
                                 struct Curl_easy *data,
2768
                                 struct Curl_sigpipe_ctx *sigpipe_ctx)
2769
180k
{
2770
180k
  CURLMcode mresult = CURLM_OK;
2771
180k
  CURLcode result = CURLE_OK;
2772
180k
  const struct curltime *pnow = NULL;
2773
2774
180k
  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
180k
  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
180k
  Curl_uint32_bset_remove(&multi->dirty, data->mid);
2788
2789
180k
  if(data == multi->admin) {
2790
90.4k
#ifdef ENABLE_INTERNAL_WAKEUP
2791
    /* Consume any pending wakeup signals before processing.
2792
     * This is necessary for event based processing. See #21547 */
2793
90.4k
    (void)Curl_wakeup_consume(multi->wakeup_internal, TRUE);
2794
90.4k
#endif
2795
90.4k
#ifdef USE_RESOLV_THREADED
2796
90.4k
    Curl_async_thrdd_multi_process(multi);
2797
90.4k
#endif
2798
90.4k
    Curl_cshutdn_perform(&multi->cshutdn, multi->admin, sigpipe_ctx);
2799
90.4k
    goto out;
2800
90.4k
  }
2801
2802
90.4k
  sigpipe_apply(data, sigpipe_ctx);
2803
131k
  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
131k
    bool stream_error = FALSE;
2807
131k
    mresult = CURLM_OK;
2808
131k
    pnow = NULL;
2809
2810
131k
    if(multi_ischanged(multi, TRUE)) {
2811
0
      CURL_TRC_M(data, "multi changed, check CONNECT_PEND queue");
2812
0
      multi_schedule_pending(multi); /* multiplexed */
2813
0
    }
2814
2815
131k
    if(data->mstate > MSTATE_CONNECT &&
2816
112k
       data->mstate < MSTATE_COMPLETED) {
2817
      /* Make sure we set the connection's current owner */
2818
112k
      DEBUGASSERT(data->conn);
2819
112k
      if(!data->conn) {
2820
0
        mresult = CURLM_INTERNAL_ERROR;
2821
0
        goto out;
2822
0
      }
2823
112k
    }
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
131k
    if((data->mstate >= MSTATE_CONNECT) && (data->mstate < MSTATE_COMPLETED)) {
2840
119k
      pnow = Curl_pgrs_now(data);
2841
119k
      if(multi_handle_timeout(data, pnow, &stream_error, &result,
2842
119k
                              data->mstate == MSTATE_CONNECT))
2843
        /* Skip the statemachine and go directly to error handling section. */
2844
0
        goto statemachine_end;
2845
119k
      pnow = NULL;
2846
119k
    }
2847
2848
131k
    switch(data->mstate) {
2849
6.43k
    case MSTATE_INIT:
2850
      /* Transitional state. init this transfer. A handle never comes back to
2851
         this state. */
2852
6.43k
      mresult = multistate_init(data, &result);
2853
6.43k
      break;
2854
2855
6.39k
    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
6.39k
      mresult = multistate_setup(data);
2859
6.39k
      break;
2860
2861
6.39k
    case MSTATE_CONNECT:
2862
6.39k
      mresult = multistate_connect(multi, data, &result);
2863
6.39k
      break;
2864
2865
10.3k
    case MSTATE_CONNECTING:
2866
      /* awaiting a completion of an asynch TCP connect */
2867
10.3k
      mresult = multistate_connecting(data, &stream_error, &result);
2868
10.3k
      break;
2869
2870
6.01k
    case MSTATE_PROTOCONNECT:
2871
6.01k
      mresult = multistate_protoconnect(data, &stream_error, &result);
2872
6.01k
      break;
2873
2874
36.3k
    case MSTATE_PROTOCONNECTING:
2875
      /* protocol-specific connect phase */
2876
36.3k
      mresult = multistate_protoconnecting(data, &stream_error, &result);
2877
36.3k
      break;
2878
2879
3.04k
    case MSTATE_DO:
2880
3.04k
      mresult = multistate_do(data, &stream_error, &result);
2881
3.04k
      break;
2882
2883
11.0k
    case MSTATE_DOING:
2884
      /* we continue DOING until the DO phase is complete */
2885
11.0k
      mresult = multistate_doing(data, &stream_error, &result);
2886
11.0k
      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
1.93k
    case MSTATE_DID:
2896
1.93k
      mresult = multistate_did(multi, data);
2897
1.93k
      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
42.5k
    case MSTATE_PERFORMING:
2904
42.5k
      mresult = multistate_performing(data, &stream_error, &result);
2905
42.5k
      break;
2906
2907
1.32k
    case MSTATE_DONE:
2908
1.32k
      mresult = multistate_done(data, &result);
2909
1.32k
      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
131k
    }
2923
2924
131k
    if(data->mstate >= MSTATE_CONNECT &&
2925
125k
       data->mstate < MSTATE_COMPLETED &&
2926
124k
       (Curl_is_connecting(data) ||
2927
61.5k
        (data->conn && data->conn->connect_start_ms >= 0)) &&
2928
62.5k
       mresult != CURLM_CALL_MULTI_PERFORM &&
2929
38.0k
       !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
37.7k
      pnow = Curl_pgrs_now(data);
2940
37.7k
      multi_handle_timeout(data, pnow, &stream_error, &result, TRUE);
2941
37.7k
    }
2942
2943
131k
statemachine_end:
2944
131k
    if(!pnow)
2945
94.1k
      pnow = Curl_pgrs_now(data);
2946
131k
    result = is_finished(multi, data, pnow, stream_error, result);
2947
131k
    if(result)
2948
2.76k
      mresult = CURLM_CALL_MULTI_PERFORM;
2949
2950
131k
    if(MSTATE_COMPLETED == data->mstate) {
2951
2.87k
      handle_completed(multi, data, result);
2952
2.87k
      mresult = CURLM_OK;
2953
2.87k
      goto out;
2954
2.87k
    }
2955
131k
  } while((mresult == CURLM_CALL_MULTI_PERFORM) ||
2956
87.6k
          multi_ischanged(multi, FALSE));
2957
2958
180k
out:
2959
180k
  data->done = (uint8_t)result;
2960
180k
  return mresult;
2961
90.4k
}
2962
2963
static CURLMcode multi_perform(struct Curl_multi *multi,
2964
                               int *running_handles)
2965
90.4k
{
2966
90.4k
  CURLMcode returncode = CURLM_OK;
2967
90.4k
  struct curltime start;
2968
90.4k
  uint32_t mid;
2969
90.4k
  struct Curl_sigpipe_ctx sigpipe_ctx;
2970
2971
90.4k
  curlx_pnow(&start);
2972
90.4k
  sigpipe_init(&sigpipe_ctx);
2973
2974
90.4k
  if(Curl_uint32_bset_first(&multi->process, &mid)) {
2975
90.4k
    CURL_TRC_M(multi->admin, "multi_perform(running=%u)",
2976
90.4k
               multi_xfers_running(multi));
2977
180k
    do {
2978
180k
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
2979
180k
      CURLMcode mresult;
2980
180k
      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
180k
      mresult = multi_runsingle(multi, data, &sigpipe_ctx);
2987
180k
      if(mresult)
2988
0
        returncode = mresult;
2989
180k
    } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
2990
90.4k
  }
2991
90.4k
  sigpipe_restore(&sigpipe_ctx);
2992
2993
90.4k
  if(multi_ischanged(multi, TRUE))
2994
1.01k
    multi_schedule_pending(multi);
2995
2996
90.4k
  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
90.4k
  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
90.4k
  if(running_handles) {
3029
90.4k
    uint32_t running = multi_xfers_running(multi);
3030
90.4k
    *running_handles = (running < INT_MAX) ? (int)running : INT_MAX;
3031
90.4k
  }
3032
3033
90.4k
  if(CURLM_OK >= returncode)
3034
90.4k
    returncode = Curl_update_timer(multi);
3035
3036
90.4k
  return returncode;
3037
90.4k
}
3038
3039
CURLMcode curl_multi_perform(CURLM *m, int *running_handles)
3040
90.4k
{
3041
90.4k
  struct Curl_mapi_guard guard;
3042
90.4k
  CURLMcode mresult;
3043
3044
90.4k
  if(CURL_MAPI_ENTER(&guard, m, multi_perform, &mresult)) {
3045
90.4k
    mresult = multi_perform(m, running_handles);
3046
90.4k
  }
3047
90.4k
  CURL_MAPI_LEAVE(&guard);
3048
90.4k
  return mresult;
3049
90.4k
}
3050
3051
CURLMcode curl_multi_cleanup(CURLM *m)
3052
6.43k
{
3053
6.43k
  struct Curl_mapi_guard guard;
3054
6.43k
  CURLMcode mresult;
3055
3056
6.43k
  if(CURL_MAPI_ENTER(&guard, m, multi_cleanup, &mresult)) {
3057
6.43k
    struct Curl_multi *multi = m;
3058
6.43k
    void *entry;
3059
6.43k
    uint32_t mid;
3060
3061
    /* First remove all remaining easy handles,
3062
     * close internal ones. admin handle is special */
3063
6.43k
    if(Curl_uint32_tbl_first(&multi->xfers, &mid, &entry)) {
3064
6.43k
      do {
3065
6.43k
        struct Curl_easy *data = entry;
3066
6.43k
        if(!GOOD_EASY_HANDLE(data)) {
3067
0
          mresult = CURLM_BAD_HANDLE;
3068
0
          goto out;
3069
0
        }
3070
3071
6.43k
#ifdef DEBUGBUILD
3072
6.43k
        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
6.43k
#endif
3078
3079
6.43k
        if(data == multi->admin)
3080
6.43k
          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
6.43k
      } while(Curl_uint32_tbl_next(&multi->xfers, mid, &mid, &entry));
3097
6.43k
    }
3098
3099
6.43k
#ifdef USE_RESOLV_THREADED
3100
6.43k
    Curl_async_thrdd_multi_destroy(multi, !multi->quick_exit);
3101
6.43k
#endif
3102
6.43k
    Curl_cpool_destroy(&multi->cpool, multi->admin);
3103
6.43k
    Curl_cshutdn_destroy(&multi->cshutdn, multi->admin);
3104
6.43k
    if(multi->admin) {
3105
6.43k
      CURL_TRC_M(multi->admin, "multi_cleanup, closing admin handle, done");
3106
6.43k
      multi->admin->multi = NULL;
3107
6.43k
      Curl_uint32_tbl_remove(&multi->xfers, multi->admin->mid);
3108
6.43k
      Curl_close(&multi->admin);
3109
6.43k
    }
3110
3111
6.43k
    multi->magic = 0; /* not good anymore */
3112
3113
6.43k
    Curl_multi_ev_cleanup(multi);
3114
6.43k
    Curl_hash_destroy(&multi->proto_hash);
3115
6.43k
    Curl_dnscache_destroy(&multi->dnscache);
3116
6.43k
    Curl_psl_destroy(&multi->psl);
3117
6.43k
#ifdef USE_SSL
3118
6.43k
    Curl_ssl_scache_destroy(multi->ssl_scache);
3119
6.43k
#endif
3120
3121
#ifdef USE_WINSOCK
3122
    WSACloseEvent(multi->wsa_event);
3123
#endif
3124
6.43k
#ifdef ENABLE_WAKEUP
3125
6.43k
  Curl_wakeup_destroy(multi->wakeup_pair);
3126
6.43k
#endif
3127
6.43k
#ifdef ENABLE_INTERNAL_WAKEUP
3128
6.43k
  Curl_wakeup_destroy(multi->wakeup_internal);
3129
6.43k
#endif
3130
3131
6.43k
    multi_xfer_bufs_free(multi);
3132
6.43k
    Curl_mntfy_cleanup(multi);
3133
6.43k
#ifdef DEBUGBUILD
3134
6.43k
    if(Curl_uint32_tbl_count(&multi->xfers)) {
3135
0
      multi_xfer_tbl_dump(multi);
3136
0
      DEBUGASSERT(0);
3137
0
    }
3138
6.43k
#endif
3139
6.43k
    Curl_uint32_bset_destroy(&multi->process);
3140
6.43k
    Curl_uint32_bset_destroy(&multi->dirty);
3141
6.43k
    Curl_uint32_bset_destroy(&multi->pending);
3142
6.43k
    Curl_uint32_bset_destroy(&multi->msgsent);
3143
6.43k
    Curl_uint32_tbl_destroy(&multi->xfers);
3144
6.43k
    curlx_memzero(multi, sizeof(*multi));
3145
6.43k
    curlx_free(multi);
3146
3147
6.43k
    mresult = CURLM_OK;
3148
6.43k
  }
3149
6.43k
out:
3150
6.43k
  CURL_MAPI_LEAVE(&guard);
3151
6.43k
  return mresult;
3152
6.43k
}
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
9.30k
{
3163
9.30k
  struct Curl_mapi_guard guard;
3164
9.30k
  CURLMsg *msg_result = NULL;
3165
3166
9.30k
  *msgs_in_queue = 0; /* default to none */
3167
9.30k
  if(CURL_MAPI_ENTER(&guard, m, multi_info_read, NULL)) {
3168
9.30k
    struct Curl_multi *multi = m;
3169
9.30k
    uint32_t mid;
3170
9.30k
    if(Curl_uint32_bset_first(&multi->msgsent, &mid)) {
3171
2.87k
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3172
3173
2.87k
      DEBUGASSERT(data);
3174
2.87k
      Curl_uint32_bset_remove(&multi->msgsent, mid);
3175
2.87k
      *msgs_in_queue =
3176
2.87k
        curlx_uztosi(Curl_uint32_bset_count(&multi->msgsent));
3177
2.87k
      if(data)
3178
2.87k
        msg_result = &data->msg;
3179
2.87k
    }
3180
9.30k
  }
3181
9.30k
  CURL_MAPI_LEAVE(&guard);
3182
9.30k
  return msg_result;
3183
9.30k
}
3184
3185
void Curl_multi_will_close(struct Curl_easy *data, curl_socket_t s)
3186
6.03k
{
3187
6.03k
  if(data) {
3188
6.03k
    struct Curl_multi *multi = data->multi;
3189
6.03k
    if(multi) {
3190
6.03k
      CURL_TRC_M(data, "Curl_multi_will_close fd=%" FMT_SOCKET_T, s);
3191
6.03k
      Curl_multi_ev_socket_done(multi, data, s);
3192
6.03k
    }
3193
6.03k
  }
3194
6.03k
}
3195
3196
static void multi_timeouts_init(struct Curl_easy *data)
3197
19.3k
{
3198
19.3k
  data->state.timeouts.first = EXPIRE_LAST;
3199
19.3k
  data->state.timeouts.splaynode.registered = FALSE;
3200
19.3k
}
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
0
{
3403
0
  struct Curl_mapi_guard guard;
3404
0
  CURLMcode mresult = CURLM_OK;
3405
3406
0
  if(CURL_MAPI_ENTER(&guard, m, multi_setopt, &mresult)) {
3407
0
    struct Curl_multi *multi = m;
3408
0
    va_list param;
3409
0
    unsigned long uarg;
3410
0
    size_t szarg;
3411
3412
0
    va_start(param, option);
3413
3414
0
    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
0
    case CURLMOPT_PUSHFUNCTION:
3422
0
      multi->push_cb = va_arg(param, curl_push_callback);
3423
0
      break;
3424
0
    case CURLMOPT_PUSHDATA:
3425
0
      multi->push_userp = va_arg(param, void *);
3426
0
      break;
3427
0
    case CURLMOPT_PIPELINING:
3428
0
      multi->multiplexing = va_arg(param, long) & CURLPIPE_MULTIPLEX ? 1 : 0;
3429
0
      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
0
    case CURLMOPT_RESOLVE_THREADS_MAX:
3495
0
#ifdef USE_RESOLV_THREADED
3496
0
      uarg = va_arg(param, long);
3497
0
      if((uarg <= 0) || (uarg > UINT32_MAX))
3498
0
        mresult = CURLM_BAD_FUNCTION_ARGUMENT;
3499
0
      else {
3500
0
        CURLcode result = Curl_async_thrdd_multi_set_props(
3501
0
          multi, 0, (uint32_t)uarg, 2000);
3502
0
        switch(result) {
3503
0
        case CURLE_OK:
3504
0
          mresult = CURLM_OK;
3505
0
          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
0
        }
3516
0
      }
3517
0
#endif
3518
0
      break;
3519
0
    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
0
    }
3526
0
    va_end(param);
3527
0
  }
3528
0
  CURL_MAPI_LEAVE(&guard);
3529
0
  return mresult;
3530
0
}
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
17.3k
{
3574
17.3k
  uint32_t mid;
3575
17.3k
  if(Curl_uint32_bset_first(&multi->dirty, &mid)) {
3576
714
    do {
3577
714
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3578
714
      if(data) {
3579
714
        if(Curl_uint32_bset_contains(&multi->process, mid))
3580
714
          return TRUE;
3581
        /* We are no longer processing this transfer */
3582
0
        Curl_uint32_bset_remove(&multi->dirty, mid);
3583
0
      }
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
714
    } while(Curl_uint32_bset_next(&multi->dirty, mid, &mid));
3589
714
  }
3590
16.6k
  return FALSE;
3591
17.3k
}
3592
3593
static void multi_timeout(struct Curl_multi *multi,
3594
                          timediff_t *pexire_offset_us,
3595
                          int *timeout_ms)
3596
17.3k
{
3597
17.3k
  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
17.3k
  if(multi_has_dirties(multi)) {
3605
714
    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
714
    *timeout_ms = 0;
3611
714
    return;
3612
714
  }
3613
16.6k
  else {
3614
16.6k
    struct curltime now;
3615
16.6k
    uint32_t mid;
3616
3617
16.6k
    curlx_pnow(&now);
3618
16.6k
    *timeout_ms = Curl_timeouts_next_ms(&multi->timeouts, &now,
3619
16.6k
                                        pexire_offset_us, &mid);
3620
16.6k
#ifdef CURLVERBOSE
3621
16.6k
    if(mid != UINT32_MAX) {
3622
16.6k
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3623
16.6k
      if(data && CURL_TRC_TIMER_is_verbose(data) &&
3624
0
         (data->state.timeouts.first < EXPIRE_LAST)) {
3625
0
        CURL_TRC_TIMER(data, data->state.timeouts.first,
3626
0
                       "gives multi timeout in %dms", *timeout_ms);
3627
0
      }
3628
16.6k
    }
3629
16.6k
#endif
3630
16.6k
  }
3631
17.3k
}
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
103k
{
3656
103k
  timediff_t timeouts_offset_us = 0;
3657
103k
  int timeout_ms;
3658
103k
  int rc;
3659
103k
  bool set_value = FALSE;
3660
3661
103k
  if(!multi->timer_cb || multi->dead)
3662
103k
    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
30.1M
{
3711
30.1M
  struct expire_timers *timeouts = &data->state.timeouts;
3712
30.1M
  uint8_t id;
3713
30.1M
  int i = 0;
3714
105M
  for(id = timeouts->first; id < EXPIRE_LAST; id = timeouts->next[id]) {
3715
75.2M
    if(++i >= EXPIRE_LAST) {
3716
0
      failf(data, "expire timeouts looped: %d iterations and no end", i);
3717
0
      return FALSE;
3718
0
    }
3719
75.2M
    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
75.2M
    if((timeouts->next[id] < EXPIRE_LAST) &&
3724
45.1M
       (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
75.2M
  }
3730
30.1M
  return TRUE;
3731
30.1M
}
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
15.0M
{
3739
15.0M
  struct expire_timers *timeouts = &data->state.timeouts;
3740
15.0M
  uint8_t orig_first = timeouts->first;
3741
15.0M
  uint8_t *anchor = &timeouts->first;
3742
15.0M
  uint8_t id = (uint8_t)eid;
3743
3744
15.0M
  if((unsigned)eid >= EXPIRE_LAST) {
3745
0
    DEBUGASSERT(0);
3746
0
    return;
3747
0
  }
3748
3749
45.1M
  while(*anchor < EXPIRE_LAST) {
3750
45.1M
    if(*anchor == id) {
3751
15.0M
      *anchor = timeouts->next[id];
3752
15.0M
      break;
3753
15.0M
    }
3754
30.1M
    anchor = &timeouts->next[*anchor];
3755
30.1M
  }
3756
15.0M
  DEBUGASSERT(multi_timeouts_check(data));
3757
15.0M
  if(Curl_timeouts_has(data)) {
3758
15.0M
    struct Curl_multi *multi = data->multi;
3759
3760
15.0M
    if(!multi) {
3761
0
      DEBUGASSERT(0);
3762
0
      return;
3763
0
    }
3764
15.0M
    if((timeouts->first >= EXPIRE_LAST) || /* no more timeouts */
3765
15.0M
       (timeouts->first != orig_first)) {  /* active timeout changed */
3766
7.21k
      Curl_timeouts_remove(&multi->timeouts, data);
3767
7.21k
    }
3768
15.0M
    if((timeouts->first < EXPIRE_LAST) && !Curl_timeouts_has(data)) {
3769
7.08k
      Curl_timeouts_add(&multi->timeouts, data,
3770
7.08k
                        timeouts->offset_us[timeouts->first]);
3771
7.08k
    }
3772
15.0M
  }
3773
15.0M
}
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
15.0M
{
3783
15.0M
  struct expire_timers *timeouts = &data->state.timeouts;
3784
15.0M
  uint8_t *anchor = &timeouts->first;
3785
15.0M
  uint8_t id = (uint8_t)eid;
3786
3787
15.0M
  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
15.0M
  multi_clear_timeout(data, eid);
3793
15.0M
  timeouts->offset_us[id] =
3794
15.0M
    Curl_timeouts_offset_us(&data->multi->timeouts, stamp);
3795
3796
45.1M
  while(*anchor < EXPIRE_LAST) {
3797
30.0M
    if(timeouts->offset_us[*anchor] > timeouts->offset_us[id])
3798
7.16k
      break;
3799
30.0M
    anchor = &timeouts->next[*anchor];
3800
30.0M
  }
3801
15.0M
  timeouts->next[eid] = *anchor;
3802
15.0M
  *anchor = id;
3803
15.0M
  DEBUGASSERT(multi_timeouts_check(data));
3804
15.0M
  CURL_TRC_TIMER(data, eid, "set for %" FMT_TIMEDIFF_T "us",
3805
15.0M
                 curlx_ptimediff_us(stamp, Curl_pgrs_now(data)));
3806
15.0M
  return CURLM_OK;
3807
15.0M
}
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
15.0M
{
3822
15.0M
  struct Curl_multi *multi = data->multi;
3823
15.0M
  struct expire_timers *timeouts = &data->state.timeouts;
3824
15.0M
  uint8_t prev_id = timeouts->first;
3825
15.0M
  struct curltime set;
3826
3827
  /* this is only interesting while there is still an associated multi struct
3828
     remaining! */
3829
15.0M
  if(!multi)
3830
0
    return;
3831
15.0M
  DEBUGASSERT(eid < EXPIRE_LAST);
3832
15.0M
  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
15.0M
  set = *pnow;
3838
15.0M
  set.tv_sec += (time_t)(ms / 1000); /* may be a 64 to 32-bit conversion */
3839
15.0M
  set.tv_usec += (int)(ms % 1000) * 1000;
3840
15.0M
  if(set.tv_usec >= 1000000) {
3841
1.68M
    set.tv_sec++;
3842
1.68M
    set.tv_usec -= 1000000;
3843
1.68M
  }
3844
3845
  /* Add the timeout, will replace any previous value for this timer. */
3846
15.0M
  multi_set_timeout(data, &set, eid);
3847
15.0M
  DEBUGASSERT(timeouts->first < EXPIRE_LAST);
3848
3849
15.0M
  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
15.0M
    if((prev_id != eid) && (prev_id == timeouts->first))
3855
15.0M
      return;
3856
3857
    /* Since this is an updated time, we must remove data from
3858
     * timeouts and then add it again. */
3859
13.6k
    Curl_timeouts_remove(&multi->timeouts, data);
3860
13.6k
  }
3861
3862
  /* Insert the new timer expiry since it is our local minimum. */
3863
21.5k
  Curl_timeouts_add(&multi->timeouts, data,
3864
21.5k
                    timeouts->offset_us[timeouts->first]);
3865
21.5k
}
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
15.0M
{
3897
15.0M
  Curl_expire_set(data, eid, milli, Curl_pgrs_now(data));
3898
15.0M
}
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
6.03k
{
3905
  /* remove the timer, if there */
3906
6.03k
  multi_clear_timeout(data, eid);
3907
6.03k
  CURL_TRC_TIMER(data, eid, "cleared");
3908
6.03k
}
3909
3910
/*
3911
 * Clear ALL timeout values for this handle.
3912
 */
3913
void Curl_expire_clear_all(struct Curl_easy *data)
3914
22.3k
{
3915
22.3k
  struct Curl_multi *multi = data->multi;
3916
3917
  /* this is only interesting while there is still an associated multi struct
3918
     remaining! */
3919
22.3k
  if(!multi)
3920
13.0k
    return;
3921
3922
9.30k
  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
6.39k
    multi_timeouts_init(data);
3926
3927
6.39k
    if(data->id >= 0)
3928
6.39k
      CURL_TRC_M(data, "[TIMEOUT] all cleared");
3929
6.39k
  }
3930
9.30k
}
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
0
{
3948
0
  DEBUGASSERT(data->mstate == MSTATE_PENDING);
3949
3950
  /* Remove this node from the pending set, add into process set */
3951
0
  Curl_uint32_bset_remove(&multi->pending, data->mid);
3952
0
  Curl_uint32_bset_add(&multi->process, data->mid);
3953
3954
0
  multistate(data, MSTATE_CONNECT);
3955
0
  Curl_multi_mark_dirty(data); /* make it run */
3956
0
}
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
21.5k
{
3973
21.5k
  uint32_t mid = multi->last_pending_mid;
3974
3975
21.5k
  if(mid) {
3976
0
    while(Curl_uint32_bset_next(&multi->pending, mid, &mid)) {
3977
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3978
0
      if(data) {
3979
0
        move_pending_to_connect(multi, data);
3980
0
        multi->last_pending_mid = mid;
3981
0
        return;
3982
0
      }
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
0
    multi->last_pending_mid = 0;
3990
0
  }
3991
3992
21.5k
  if(Curl_uint32_bset_first(&multi->pending, &mid)) {
3993
0
    do {
3994
0
      struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
3995
0
      if(data) {
3996
0
        move_pending_to_connect(multi, data);
3997
0
        multi->last_pending_mid = mid;
3998
0
        return;
3999
0
      }
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
0
  }
4005
21.5k
}
4006
4007
uint32_t Curl_multi_max_concurrent_streams(struct Curl_multi *multi)
4008
0
{
4009
0
  DEBUGASSERT(multi);
4010
0
  return multi->max_concurrent_streams;
4011
0
}
4012
4013
CURL **curl_multi_get_handles(CURLM *m)
4014
6.43k
{
4015
6.43k
  struct Curl_mapi_guard guard;
4016
6.43k
  CURL **a = NULL;
4017
4018
6.43k
  if(CURL_MAPI_ENTER(&guard, m, multi_get_handles, NULL)) {
4019
6.43k
    struct Curl_multi *multi = m;
4020
6.43k
    void *entry;
4021
6.43k
    size_t count = Curl_uint32_tbl_count(&multi->xfers);
4022
4023
6.43k
    a = curlx_malloc(sizeof(struct Curl_easy *) * (count + 1));
4024
6.43k
    if(a) {
4025
6.43k
      unsigned int i = 0;
4026
6.43k
      uint32_t mid;
4027
4028
6.43k
      if(Curl_uint32_tbl_first(&multi->xfers, &mid, &entry)) {
4029
12.8k
        do {
4030
12.8k
          struct Curl_easy *data = entry;
4031
12.8k
          DEBUGASSERT(i < count);
4032
12.8k
          if(!data->state.internal)
4033
6.43k
            a[i++] = data;
4034
12.8k
        } while(Curl_uint32_tbl_next(&multi->xfers, mid, &mid, &entry));
4035
6.43k
      }
4036
6.43k
      a[i] = NULL; /* last entry is a NULL */
4037
6.43k
    }
4038
6.43k
  }
4039
6.43k
  CURL_MAPI_LEAVE(&guard);
4040
6.43k
  return a;
4041
6.43k
}
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
158
{
4094
158
  struct Curl_fixed_buf *fbuf;
4095
158
  if((SIZE_MAX - sizeof(*fbuf)) < len)
4096
0
    return NULL; /* too large */
4097
158
  fbuf = curlx_malloc(len + sizeof(*fbuf));
4098
158
  if(fbuf)
4099
158
    fbuf->len = len;
4100
158
  return fbuf;
4101
158
}
4102
4103
CURLcode Curl_multi_xfer_buf_borrow(struct Curl_easy *data,
4104
                                    char **pbuf, size_t *pbuflen)
4105
869
{
4106
869
  DEBUGASSERT(data);
4107
869
  DEBUGASSERT(data->multi);
4108
869
  *pbuf = NULL;
4109
869
  *pbuflen = 0;
4110
869
  if(!data->multi) {
4111
0
    failf(data, "transfer has no multi handle");
4112
0
    return CURLE_FAILED_INIT;
4113
0
  }
4114
869
  if(!data->set.buffer_size) {
4115
0
    failf(data, "transfer buffer size is 0");
4116
0
    return CURLE_FAILED_INIT;
4117
0
  }
4118
869
  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
869
  if(data->multi->xfer_buf &&
4124
711
     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
869
  if(!data->multi->xfer_buf) {
4130
158
    data->multi->xfer_buf =
4131
158
      fixed_buf_create(curlx_uitouz(data->set.buffer_size));
4132
158
    if(!data->multi->xfer_buf) {
4133
0
      failf(data, "could not allocate xfer_buf of %u bytes",
4134
0
            data->set.buffer_size);
4135
0
      return CURLE_OUT_OF_MEMORY;
4136
0
    }
4137
158
  }
4138
4139
869
  data->multi->xfer_buf_borrowed = TRUE;
4140
869
  *pbuf = data->multi->xfer_buf->data;
4141
869
  *pbuflen = data->multi->xfer_buf->len;
4142
869
  return CURLE_OK;
4143
869
}
4144
4145
void Curl_multi_xfer_buf_release(struct Curl_easy *data, char *buf)
4146
869
{
4147
869
  (void)buf;
4148
869
  DEBUGASSERT(data);
4149
869
  DEBUGASSERT(data->multi);
4150
869
  DEBUGASSERT(!buf || (data->multi->xfer_buf &&
4151
869
                       data->multi->xfer_buf->data == buf));
4152
869
  data->multi->xfer_buf_borrowed = FALSE;
4153
869
}
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
6.43k
{
4254
6.43k
  DEBUGASSERT(multi);
4255
6.43k
  curlx_safefree(multi->xfer_buf);
4256
6.43k
  multi->xfer_buf_borrowed = FALSE;
4257
6.43k
  curlx_safefree(multi->xfer_ulbuf);
4258
6.43k
  multi->xfer_ulbuf_borrowed = FALSE;
4259
6.43k
  curlx_safefree(multi->xfer_sockbuf);
4260
6.43k
  multi->xfer_sockbuf_borrowed = FALSE;
4261
6.43k
}
4262
4263
struct Curl_easy *Curl_multi_get_easy(struct Curl_multi *multi,
4264
                                      uint32_t mid)
4265
249k
{
4266
249k
  struct Curl_easy *data = Curl_uint32_tbl_get(&multi->xfers, mid);
4267
249k
  if(GOOD_EASY_HANDLE(data))
4268
249k
    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
249k
}
4274
4275
bool Curl_multi_knows_easy(struct Curl_multi *multi, struct Curl_easy *data)
4276
0
{
4277
0
  return Curl_uint32_tbl_get(&multi->xfers, data->mid) == data;
4278
0
}
4279
4280
static uint32_t multi_xfers_running(struct Curl_multi *multi)
4281
90.4k
{
4282
90.4k
  if(!multi) {
4283
0
    DEBUGASSERT(0);
4284
0
    return 0;
4285
0
  }
4286
90.4k
  return multi->xfers_alive;
4287
90.4k
}
4288
4289
uint32_t Curl_multi_xfers_attached(struct Curl_multi *multi)
4290
1.32k
{
4291
1.32k
  if(!multi || !multi->admin) {
4292
0
    DEBUGASSERT(0);
4293
0
    return 0;
4294
0
  }
4295
  /* Discount the admin handle */
4296
1.32k
  return Curl_uint32_tbl_count(&multi->xfers) - 1;
4297
1.32k
}
4298
4299
void Curl_multi_mark_dirty(struct Curl_easy *data)
4300
7.46k
{
4301
7.46k
  if(data->multi && data->mid != UINT32_MAX)
4302
7.46k
    Curl_uint32_bset_add(&data->multi->dirty, data->mid);
4303
7.46k
}
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 */