Coverage Report

Created: 2026-12-01 06:24

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