Coverage Report

Created: 2026-12-01 06:26

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