Coverage Report

Created: 2026-02-26 06:32

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