Coverage Report

Created: 2025-11-23 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/easy.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
25
#include "curl_setup.h"
26
27
#ifdef HAVE_NETINET_IN_H
28
#include <netinet/in.h>
29
#endif
30
#ifdef HAVE_NETDB_H
31
#include <netdb.h>
32
#endif
33
#ifdef HAVE_ARPA_INET_H
34
#include <arpa/inet.h>
35
#endif
36
#ifdef HAVE_NET_IF_H
37
#include <net/if.h>
38
#endif
39
#ifdef HAVE_SYS_IOCTL_H
40
#include <sys/ioctl.h>
41
#endif
42
43
#ifdef HAVE_SYS_PARAM_H
44
#include <sys/param.h>
45
#endif
46
47
#include "urldata.h"
48
#include <curl/curl.h>
49
#include "transfer.h"
50
#include "vtls/vtls.h"
51
#include "vtls/vtls_scache.h"
52
#include "vquic/vquic.h"
53
#include "url.h"
54
#include "getinfo.h"
55
#include "hostip.h"
56
#include "share.h"
57
#include "strdup.h"
58
#include "progress.h"
59
#include "easyif.h"
60
#include "multiif.h"
61
#include "select.h"
62
#include "cfilters.h"
63
#include "sendf.h" /* for failf function prototype */
64
#include "connect.h" /* for Curl_getconnectinfo */
65
#include "slist.h"
66
#include "mime.h"
67
#include "amigaos.h"
68
#include "macos.h"
69
#include "curlx/warnless.h"
70
#include "curlx/wait.h"
71
#include "sigpipe.h"
72
#include "vssh/ssh.h"
73
#include "setopt.h"
74
#include "http_digest.h"
75
#include "system_win32.h"
76
#include "http2.h"
77
#include "curlx/dynbuf.h"
78
#include "altsvc.h"
79
#include "hsts.h"
80
81
#include "easy_lock.h"
82
83
/* The last 2 #include files should be in this order */
84
#include "curl_memory.h"
85
#include "memdebug.h"
86
87
/* true globals -- for curl_global_init() and curl_global_cleanup() */
88
static unsigned int  initialized;
89
static long          easy_init_flags;
90
91
#ifdef GLOBAL_INIT_IS_THREADSAFE
92
93
static curl_simple_lock s_lock = CURL_SIMPLE_LOCK_INIT;
94
0
#define global_init_lock() curl_simple_lock_lock(&s_lock)
95
0
#define global_init_unlock() curl_simple_lock_unlock(&s_lock)
96
97
#else
98
99
#define global_init_lock()
100
#define global_init_unlock()
101
102
#endif
103
104
/*
105
 * strdup (and other memory functions) is redefined in complicated
106
 * ways, but at this point it must be defined as the system-supplied strdup
107
 * so the callback pointer is initialized correctly.
108
 */
109
#ifdef HAVE_STRDUP
110
0
#define system_strdup strdup
111
#else
112
#define system_strdup Curl_strdup
113
#endif
114
115
#if defined(_MSC_VER) && defined(_DLL)
116
#  pragma warning(push)
117
#  pragma warning(disable:4232) /* MSVC extension, dllimport identity */
118
#endif
119
120
/*
121
 * If a memory-using function (like curl_getenv) is used before
122
 * curl_global_init() is called, we need to have these pointers set already.
123
 */
124
curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
125
curl_free_callback Curl_cfree = (curl_free_callback)free;
126
curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
127
curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
128
curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
129
130
#if defined(_MSC_VER) && defined(_DLL)
131
#  pragma warning(pop)
132
#endif
133
134
#ifdef DEBUGBUILD
135
static char *leakpointer;
136
#endif
137
138
/**
139
 * curl_global_init() globally initializes curl given a bitwise set of the
140
 * different features of what to initialize.
141
 */
142
static CURLcode global_init(long flags, bool memoryfuncs)
143
0
{
144
0
  if(initialized++)
145
0
    return CURLE_OK;
146
147
0
  if(memoryfuncs) {
148
    /* Setup the default memory functions here (again) */
149
0
    Curl_cmalloc = (curl_malloc_callback)malloc;
150
0
    Curl_cfree = (curl_free_callback)free;
151
0
    Curl_crealloc = (curl_realloc_callback)realloc;
152
0
    Curl_cstrdup = (curl_strdup_callback)system_strdup;
153
0
    Curl_ccalloc = (curl_calloc_callback)calloc;
154
0
  }
155
156
0
  if(Curl_trc_init()) {
157
0
    DEBUGF(curl_mfprintf(stderr, "Error: Curl_trc_init failed\n"));
158
0
    goto fail;
159
0
  }
160
161
0
  if(!Curl_ssl_init()) {
162
0
    DEBUGF(curl_mfprintf(stderr, "Error: Curl_ssl_init failed\n"));
163
0
    goto fail;
164
0
  }
165
166
0
  if(!Curl_vquic_init()) {
167
0
    DEBUGF(curl_mfprintf(stderr, "Error: Curl_vquic_init failed\n"));
168
0
    goto fail;
169
0
  }
170
171
0
  if(Curl_win32_init(flags)) {
172
0
    DEBUGF(curl_mfprintf(stderr, "Error: win32_init failed\n"));
173
0
    goto fail;
174
0
  }
175
176
0
  if(Curl_amiga_init()) {
177
0
    DEBUGF(curl_mfprintf(stderr, "Error: Curl_amiga_init failed\n"));
178
0
    goto fail;
179
0
  }
180
181
0
  if(Curl_macos_init()) {
182
0
    DEBUGF(curl_mfprintf(stderr, "Error: Curl_macos_init failed\n"));
183
0
    goto fail;
184
0
  }
185
186
0
  if(Curl_async_global_init()) {
187
0
    DEBUGF(curl_mfprintf(stderr, "Error: resolver_global_init failed\n"));
188
0
    goto fail;
189
0
  }
190
191
0
  if(Curl_ssh_init()) {
192
0
    DEBUGF(curl_mfprintf(stderr, "Error: Curl_ssh_init failed\n"));
193
0
    goto fail;
194
0
  }
195
196
0
  easy_init_flags = flags;
197
198
0
#ifdef DEBUGBUILD
199
0
  if(getenv("CURL_GLOBAL_INIT"))
200
    /* alloc data that will leak if *cleanup() is not called! */
201
0
    leakpointer = malloc(1);
202
0
#endif
203
204
0
  return CURLE_OK;
205
206
0
fail:
207
0
  initialized--; /* undo the increase */
208
0
  return CURLE_FAILED_INIT;
209
0
}
210
211
212
/**
213
 * curl_global_init() globally initializes curl given a bitwise set of the
214
 * different features of what to initialize.
215
 */
216
CURLcode curl_global_init(long flags)
217
0
{
218
0
  CURLcode result;
219
0
  global_init_lock();
220
221
0
  result = global_init(flags, TRUE);
222
223
0
  global_init_unlock();
224
225
0
  return result;
226
0
}
227
228
/*
229
 * curl_global_init_mem() globally initializes curl and also registers the
230
 * user provided callback routines.
231
 */
232
CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
233
                              curl_free_callback f, curl_realloc_callback r,
234
                              curl_strdup_callback s, curl_calloc_callback c)
235
0
{
236
0
  CURLcode result;
237
238
  /* Invalid input, return immediately */
239
0
  if(!m || !f || !r || !s || !c)
240
0
    return CURLE_FAILED_INIT;
241
242
0
  global_init_lock();
243
244
0
  if(initialized) {
245
    /* Already initialized, do not do it again, but bump the variable anyway to
246
       work like curl_global_init() and require the same amount of cleanup
247
       calls. */
248
0
    initialized++;
249
0
    global_init_unlock();
250
0
    return CURLE_OK;
251
0
  }
252
253
  /* set memory functions before global_init() in case it wants memory
254
     functions */
255
0
  Curl_cmalloc = m;
256
0
  Curl_cfree = f;
257
0
  Curl_cstrdup = s;
258
0
  Curl_crealloc = r;
259
0
  Curl_ccalloc = c;
260
261
  /* Call the actual init function, but without setting */
262
0
  result = global_init(flags, FALSE);
263
264
0
  global_init_unlock();
265
266
0
  return result;
267
0
}
268
269
/**
270
 * curl_global_cleanup() globally cleanups curl, uses the value of
271
 * "easy_init_flags" to determine what needs to be cleaned up and what does
272
 * not.
273
 */
274
void curl_global_cleanup(void)
275
0
{
276
0
  global_init_lock();
277
278
0
  if(!initialized) {
279
0
    global_init_unlock();
280
0
    return;
281
0
  }
282
283
0
  if(--initialized) {
284
0
    global_init_unlock();
285
0
    return;
286
0
  }
287
288
0
  Curl_ssl_cleanup();
289
0
  Curl_async_global_cleanup();
290
291
#ifdef _WIN32
292
  Curl_win32_cleanup(easy_init_flags);
293
#endif
294
295
0
  Curl_amiga_cleanup();
296
297
0
  Curl_ssh_cleanup();
298
299
0
#ifdef DEBUGBUILD
300
0
  free(leakpointer);
301
0
#endif
302
303
0
  easy_init_flags = 0;
304
305
0
  global_init_unlock();
306
0
}
307
308
/**
309
 * curl_global_trace() globally initializes curl logging.
310
 */
311
CURLcode curl_global_trace(const char *config)
312
0
{
313
0
#ifndef CURL_DISABLE_VERBOSE_STRINGS
314
0
  CURLcode result;
315
0
  global_init_lock();
316
317
0
  result = Curl_trc_opt(config);
318
319
0
  global_init_unlock();
320
321
0
  return result;
322
#else
323
  (void)config;
324
  return CURLE_OK;
325
#endif
326
0
}
327
328
/*
329
 * curl_global_sslset() globally initializes the SSL backend to use.
330
 */
331
CURLsslset curl_global_sslset(curl_sslbackend id, const char *name,
332
                              const curl_ssl_backend ***avail)
333
0
{
334
0
  CURLsslset rc;
335
336
0
  global_init_lock();
337
338
0
  rc = Curl_init_sslset_nolock(id, name, avail);
339
340
0
  global_init_unlock();
341
342
0
  return rc;
343
0
}
344
345
/*
346
 * curl_easy_init() is the external interface to alloc, setup and init an
347
 * easy handle that is returned. If anything goes wrong, NULL is returned.
348
 */
349
CURL *curl_easy_init(void)
350
0
{
351
0
  CURLcode result;
352
0
  struct Curl_easy *data;
353
354
  /* Make sure we inited the global SSL stuff */
355
0
  global_init_lock();
356
357
0
  if(!initialized) {
358
0
    result = global_init(CURL_GLOBAL_DEFAULT, TRUE);
359
0
    if(result) {
360
      /* something in the global init failed, return nothing */
361
0
      DEBUGF(curl_mfprintf(stderr, "Error: curl_global_init failed\n"));
362
0
      global_init_unlock();
363
0
      return NULL;
364
0
    }
365
0
  }
366
0
  global_init_unlock();
367
368
  /* We use curl_open() with undefined URL so far */
369
0
  result = Curl_open(&data);
370
0
  if(result) {
371
0
    DEBUGF(curl_mfprintf(stderr, "Error: Curl_open failed\n"));
372
0
    return NULL;
373
0
  }
374
375
0
  return data;
376
0
}
377
378
#ifdef DEBUGBUILD
379
380
struct socketmonitor {
381
  struct socketmonitor *next; /* the next node in the list or NULL */
382
  struct pollfd socket; /* socket info of what to monitor */
383
};
384
385
struct events {
386
  long ms;              /* timeout, run the timeout function when reached */
387
  bool msbump;          /* set TRUE when timeout is set by callback */
388
  int num_sockets;      /* number of nodes in the monitor list */
389
  struct socketmonitor *list; /* list of sockets to monitor */
390
  int running_handles;  /* store the returned number */
391
};
392
393
#define DEBUG_EV_POLL   0
394
395
/* events_timer
396
 *
397
 * Callback that gets called with a new value when the timeout should be
398
 * updated.
399
 */
400
static int events_timer(CURLM *multi,    /* multi handle */
401
                        long timeout_ms, /* see above */
402
                        void *userp)     /* private callback pointer */
403
0
{
404
0
  struct events *ev = userp;
405
0
  (void)multi;
406
#if DEBUG_EV_POLL
407
  curl_mfprintf(stderr, "events_timer: set timeout %ldms\n", timeout_ms);
408
#endif
409
0
  ev->ms = timeout_ms;
410
0
  ev->msbump = TRUE;
411
0
  return 0;
412
0
}
413
414
415
/* poll2cselect
416
 *
417
 * convert from poll() bit definitions to libcurl's CURL_CSELECT_* ones
418
 */
419
static int poll2cselect(int pollmask)
420
0
{
421
0
  int omask = 0;
422
0
  if(pollmask & POLLIN)
423
0
    omask |= CURL_CSELECT_IN;
424
0
  if(pollmask & POLLOUT)
425
0
    omask |= CURL_CSELECT_OUT;
426
0
  if(pollmask & POLLERR)
427
0
    omask |= CURL_CSELECT_ERR;
428
0
  return omask;
429
0
}
430
431
432
/* socketcb2poll
433
 *
434
 * convert from libcurl' CURL_POLL_* bit definitions to poll()'s
435
 */
436
static short socketcb2poll(int pollmask)
437
0
{
438
0
  short omask = 0;
439
0
  if(pollmask & CURL_POLL_IN)
440
0
    omask |= POLLIN;
441
0
  if(pollmask & CURL_POLL_OUT)
442
0
    omask |= POLLOUT;
443
0
  return omask;
444
0
}
445
446
/* events_socket
447
 *
448
 * Callback that gets called with information about socket activity to
449
 * monitor.
450
 */
451
static int events_socket(CURL *easy,      /* easy handle */
452
                         curl_socket_t s, /* socket */
453
                         int what,        /* see above */
454
                         void *userp,     /* private callback
455
                                             pointer */
456
                         void *socketp)   /* private socket
457
                                             pointer */
458
0
{
459
0
  struct events *ev = userp;
460
0
  struct socketmonitor *m;
461
0
  struct socketmonitor *prev = NULL;
462
0
  bool found = FALSE;
463
0
  struct Curl_easy *data = easy;
464
465
#ifdef CURL_DISABLE_VERBOSE_STRINGS
466
  (void)easy;
467
#endif
468
0
  (void)socketp;
469
470
0
  m = ev->list;
471
0
  while(m) {
472
0
    if(m->socket.fd == s) {
473
0
      found = TRUE;
474
0
      if(what == CURL_POLL_REMOVE) {
475
0
        struct socketmonitor *nxt = m->next;
476
        /* remove this node from the list of monitored sockets */
477
0
        if(prev)
478
0
          prev->next = nxt;
479
0
        else
480
0
          ev->list = nxt;
481
0
        free(m);
482
0
        infof(data, "socket cb: socket %" FMT_SOCKET_T " REMOVED", s);
483
0
      }
484
0
      else {
485
        /* The socket 's' is already being monitored, update the activity
486
           mask. Convert from libcurl bitmask to the poll one. */
487
0
        m->socket.events = socketcb2poll(what);
488
0
        infof(data, "socket cb: socket %" FMT_SOCKET_T
489
0
              " UPDATED as %s%s", s,
490
0
              (what&CURL_POLL_IN) ? "IN" : "",
491
0
              (what&CURL_POLL_OUT) ? "OUT" : "");
492
0
      }
493
0
      break;
494
0
    }
495
0
    prev = m;
496
0
    m = m->next; /* move to next node */
497
0
  }
498
499
0
  if(!found) {
500
0
    if(what == CURL_POLL_REMOVE) {
501
      /* should not happen if our logic is correct, but is no drama. */
502
0
      DEBUGF(infof(data, "socket cb: asked to REMOVE socket %"
503
0
                   FMT_SOCKET_T "but not present!", s));
504
0
      DEBUGASSERT(0);
505
0
    }
506
0
    else {
507
0
      m = malloc(sizeof(struct socketmonitor));
508
0
      if(m) {
509
0
        m->next = ev->list;
510
0
        m->socket.fd = s;
511
0
        m->socket.events = socketcb2poll(what);
512
0
        m->socket.revents = 0;
513
0
        ev->list = m;
514
0
        infof(data, "socket cb: socket %" FMT_SOCKET_T " ADDED as %s%s", s,
515
0
              (what&CURL_POLL_IN) ? "IN" : "",
516
0
              (what&CURL_POLL_OUT) ? "OUT" : "");
517
0
      }
518
0
      else
519
0
        return CURLE_OUT_OF_MEMORY;
520
0
    }
521
0
  }
522
523
0
  return 0;
524
0
}
525
526
527
/*
528
 * events_setup()
529
 *
530
 * Do the multi handle setups that only event-based transfers need.
531
 */
532
static void events_setup(struct Curl_multi *multi, struct events *ev)
533
{
534
  /* timer callback */
535
  curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, events_timer);
536
  curl_multi_setopt(multi, CURLMOPT_TIMERDATA, ev);
537
538
  /* socket callback */
539
  curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, events_socket);
540
  curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev);
541
}
542
543
/* populate_fds()
544
 *
545
 * populate the fds[] array
546
 */
547
static unsigned int populate_fds(struct pollfd *fds, struct events *ev)
548
0
{
549
0
  unsigned int numfds = 0;
550
0
  struct pollfd *f;
551
0
  struct socketmonitor *m;
552
553
0
  f = &fds[0];
554
0
  for(m = ev->list; m; m = m->next) {
555
0
    f->fd = m->socket.fd;
556
0
    f->events = m->socket.events;
557
0
    f->revents = 0;
558
#if DEBUG_EV_POLL
559
    curl_mfprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd);
560
#endif
561
0
    f++;
562
0
    numfds++;
563
0
  }
564
0
  return numfds;
565
0
}
566
567
/* poll_fds()
568
 *
569
 * poll the fds[] array
570
 */
571
static CURLcode poll_fds(struct events *ev,
572
                         struct pollfd *fds,
573
                         const unsigned int numfds,
574
                         int *pollrc)
575
0
{
576
0
  if(numfds) {
577
    /* wait for activity or timeout */
578
#if DEBUG_EV_POLL
579
    curl_mfprintf(stderr, "poll(numfds=%u, timeout=%ldms)\n", numfds, ev->ms);
580
#endif
581
0
    *pollrc = Curl_poll(fds, numfds, ev->ms);
582
#if DEBUG_EV_POLL
583
    curl_mfprintf(stderr, "poll(numfds=%u, timeout=%ldms) -> %d\n",
584
                  numfds, ev->ms, *pollrc);
585
#endif
586
0
    if(*pollrc < 0)
587
0
      return CURLE_UNRECOVERABLE_POLL;
588
0
  }
589
0
  else {
590
#if DEBUG_EV_POLL
591
    curl_mfprintf(stderr, "poll, but no fds, wait timeout=%ldms\n", ev->ms);
592
#endif
593
0
    *pollrc = 0;
594
0
    if(ev->ms > 0)
595
0
      curlx_wait_ms(ev->ms);
596
0
  }
597
0
  return CURLE_OK;
598
0
}
599
600
/* wait_or_timeout()
601
 *
602
 * waits for activity on any of the given sockets, or the timeout to trigger.
603
 */
604
static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
605
0
{
606
0
  bool done = FALSE;
607
0
  CURLMcode mcode = CURLM_OK;
608
0
  CURLcode result = CURLE_OK;
609
610
0
  while(!done) {
611
0
    CURLMsg *msg;
612
0
    struct pollfd fds[4];
613
0
    int pollrc;
614
0
    struct curltime before;
615
0
    const unsigned int numfds = populate_fds(fds, ev);
616
617
    /* get the time stamp to use to figure out how long poll takes */
618
0
    before = curlx_now();
619
620
0
    result = poll_fds(ev, fds, numfds, &pollrc);
621
0
    if(result)
622
0
      return result;
623
624
0
    ev->msbump = FALSE; /* reset here */
625
626
0
    if(!pollrc) {
627
      /* timeout! */
628
0
      ev->ms = 0;
629
      /* curl_mfprintf(stderr, "call curl_multi_socket_action(TIMEOUT)\n"); */
630
0
      mcode = curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0,
631
0
                                       &ev->running_handles);
632
0
    }
633
0
    else {
634
      /* here pollrc is > 0 */
635
      /* loop over the monitored sockets to see which ones had activity */
636
0
      unsigned int i;
637
0
      for(i = 0; i < numfds; i++) {
638
0
        if(fds[i].revents) {
639
          /* socket activity, tell libcurl */
640
0
          int act = poll2cselect(fds[i].revents); /* convert */
641
642
          /* sending infof "randomly" to the first easy handle */
643
0
          infof(multi->admin, "call curl_multi_socket_action(socket "
644
0
                "%" FMT_SOCKET_T ")", (curl_socket_t)fds[i].fd);
645
0
          mcode = curl_multi_socket_action(multi, fds[i].fd, act,
646
0
                                           &ev->running_handles);
647
0
        }
648
0
      }
649
650
651
0
      if(!ev->msbump && ev->ms >= 0) {
652
        /* If nothing updated the timeout, we decrease it by the spent time.
653
         * If it was updated, it has the new timeout time stored already.
654
         */
655
0
        timediff_t spent_ms = curlx_timediff_ms(curlx_now(), before);
656
0
        if(spent_ms > 0) {
657
#if DEBUG_EV_POLL
658
        curl_mfprintf(stderr, "poll timeout %ldms not updated, decrease by "
659
                      "time spent %ldms\n", ev->ms, (long)timediff);
660
#endif
661
0
          if(spent_ms > ev->ms)
662
0
            ev->ms = 0;
663
0
          else
664
0
            ev->ms -= (long)spent_ms;
665
0
        }
666
0
      }
667
0
    }
668
669
0
    if(mcode)
670
0
      return CURLE_URL_MALFORMAT;
671
672
    /* we do not really care about the "msgs_in_queue" value returned in the
673
       second argument */
674
0
    msg = curl_multi_info_read(multi, &pollrc);
675
0
    if(msg) {
676
0
      result = msg->data.result;
677
0
      done = TRUE;
678
0
    }
679
0
  }
680
681
0
  return result;
682
0
}
683
684
685
/* easy_events()
686
 *
687
 * Runs a transfer in a blocking manner using the events-based API
688
 */
689
static CURLcode easy_events(struct Curl_multi *multi)
690
0
{
691
  /* this struct is made static to allow it to be used after this function
692
     returns and curl_multi_remove_handle() is called */
693
0
  static struct events evs = {-1, FALSE, 0, NULL, 0};
694
695
  /* if running event-based, do some further multi inits */
696
0
  events_setup(multi, &evs);
697
698
0
  return wait_or_timeout(multi, &evs);
699
0
}
700
#else /* DEBUGBUILD */
701
/* when not built with debug, this function does not exist */
702
#define easy_events(x) CURLE_NOT_BUILT_IN
703
#endif
704
705
static CURLcode easy_transfer(struct Curl_multi *multi)
706
0
{
707
0
  bool done = FALSE;
708
0
  CURLMcode mcode = CURLM_OK;
709
0
  CURLcode result = CURLE_OK;
710
711
0
  while(!done && !mcode) {
712
0
    int still_running = 0;
713
714
0
    mcode = curl_multi_poll(multi, NULL, 0, 1000, NULL);
715
716
0
    if(!mcode)
717
0
      mcode = curl_multi_perform(multi, &still_running);
718
719
    /* only read 'still_running' if curl_multi_perform() return OK */
720
0
    if(!mcode && !still_running) {
721
0
      int rc;
722
0
      CURLMsg *msg = curl_multi_info_read(multi, &rc);
723
0
      if(msg) {
724
0
        result = msg->data.result;
725
0
        done = TRUE;
726
0
      }
727
0
    }
728
0
  }
729
730
  /* Make sure to return some kind of error if there was a multi problem */
731
0
  if(mcode) {
732
0
    result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
733
      /* The other multi errors should never happen, so return
734
         something suitably generic */
735
0
      CURLE_BAD_FUNCTION_ARGUMENT;
736
0
  }
737
738
0
  return result;
739
0
}
740
741
742
/*
743
 * easy_perform() is the internal interface that performs a blocking
744
 * transfer as previously setup.
745
 *
746
 * CONCEPT: This function creates a multi handle, adds the easy handle to it,
747
 * runs curl_multi_perform() until the transfer is done, then detaches the
748
 * easy handle, destroys the multi handle and returns the easy handle's return
749
 * code.
750
 *
751
 * REALITY: it cannot just create and destroy the multi handle that easily. It
752
 * needs to keep it around since if this easy handle is used again by this
753
 * function, the same multi handle must be reused so that the same pools and
754
 * caches can be used.
755
 *
756
 * DEBUG: if 'events' is set TRUE, this function will use a replacement engine
757
 * instead of curl_multi_perform() and use curl_multi_socket_action().
758
 */
759
static CURLcode easy_perform(struct Curl_easy *data, bool events)
760
0
{
761
0
  struct Curl_multi *multi;
762
0
  CURLMcode mcode;
763
0
  CURLcode result = CURLE_OK;
764
0
  SIGPIPE_VARIABLE(pipe_st);
765
766
0
  if(!data)
767
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
768
769
0
  if(data->set.errorbuffer)
770
    /* clear this as early as possible */
771
0
    data->set.errorbuffer[0] = 0;
772
773
0
  data->state.os_errno = 0;
774
775
0
  if(data->multi) {
776
0
    failf(data, "easy handle already used in multi handle");
777
0
    return CURLE_FAILED_INIT;
778
0
  }
779
780
  /* if the handle has a connection still attached (it is/was a connect-only
781
     handle) then disconnect before performing */
782
0
  if(data->conn) {
783
0
    struct connectdata *c;
784
0
    curl_socket_t s;
785
0
    Curl_detach_connection(data);
786
0
    s = Curl_getconnectinfo(data, &c);
787
0
    if((s != CURL_SOCKET_BAD) && c) {
788
0
      Curl_conn_terminate(data, c, TRUE);
789
0
    }
790
0
    DEBUGASSERT(!data->conn);
791
0
  }
792
793
0
  if(data->multi_easy)
794
0
    multi = data->multi_easy;
795
0
  else {
796
    /* this multi handle will only ever have a single easy handle attached to
797
       it, so make it use minimal hash sizes */
798
0
    multi = Curl_multi_handle(16, 1, 3, 7, 3);
799
0
    if(!multi)
800
0
      return CURLE_OUT_OF_MEMORY;
801
0
  }
802
803
0
  if(multi->in_callback)
804
0
    return CURLE_RECURSIVE_API_CALL;
805
806
  /* Copy the MAXCONNECTS option to the multi handle */
807
0
  curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, (long)data->set.maxconnects);
808
809
0
  data->multi_easy = NULL; /* pretend it does not exist */
810
0
  mcode = curl_multi_add_handle(multi, data);
811
0
  if(mcode) {
812
0
    curl_multi_cleanup(multi);
813
0
    if(mcode == CURLM_OUT_OF_MEMORY)
814
0
      return CURLE_OUT_OF_MEMORY;
815
0
    return CURLE_FAILED_INIT;
816
0
  }
817
818
  /* assign this after curl_multi_add_handle() */
819
0
  data->multi_easy = multi;
820
821
0
  sigpipe_init(&pipe_st);
822
0
  sigpipe_apply(data, &pipe_st);
823
824
  /* run the transfer */
825
0
  result = events ? easy_events(multi) : easy_transfer(multi);
826
827
  /* ignoring the return code is not nice, but atm we cannot really handle
828
     a failure here, room for future improvement! */
829
0
  (void)curl_multi_remove_handle(multi, data);
830
831
0
  sigpipe_restore(&pipe_st);
832
833
  /* The multi handle is kept alive, owned by the easy handle */
834
0
  return result;
835
0
}
836
837
/*
838
 * curl_easy_perform() is the external interface that performs a blocking
839
 * transfer as previously setup.
840
 */
841
CURLcode curl_easy_perform(CURL *data)
842
0
{
843
0
  return easy_perform(data, FALSE);
844
0
}
845
846
#ifdef DEBUGBUILD
847
/*
848
 * curl_easy_perform_ev() is the external interface that performs a blocking
849
 * transfer using the event-based API internally.
850
 */
851
CURLcode curl_easy_perform_ev(struct Curl_easy *data)
852
0
{
853
0
  return easy_perform(data, TRUE);
854
0
}
855
#endif
856
857
/*
858
 * curl_easy_cleanup() is the external interface to cleaning/freeing the given
859
 * easy handle.
860
 */
861
void curl_easy_cleanup(CURL *ptr)
862
0
{
863
0
  struct Curl_easy *data = ptr;
864
0
  if(GOOD_EASY_HANDLE(data)) {
865
0
    SIGPIPE_VARIABLE(pipe_st);
866
0
    sigpipe_ignore(data, &pipe_st);
867
0
    Curl_close(&data);
868
0
    sigpipe_restore(&pipe_st);
869
0
  }
870
0
}
871
872
/*
873
 * curl_easy_getinfo() is an external interface that allows an app to retrieve
874
 * information from a performed transfer and similar.
875
 */
876
#undef curl_easy_getinfo
877
CURLcode curl_easy_getinfo(CURL *easy, CURLINFO info, ...)
878
0
{
879
0
  struct Curl_easy *data = easy;
880
0
  va_list arg;
881
0
  void *paramp;
882
0
  CURLcode result;
883
884
0
  if(!GOOD_EASY_HANDLE(data))
885
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
886
887
0
  va_start(arg, info);
888
0
  paramp = va_arg(arg, void *);
889
890
0
  result = Curl_getinfo(data, info, paramp);
891
892
0
  va_end(arg);
893
0
  return result;
894
0
}
895
896
static CURLcode dupset(struct Curl_easy *dst, struct Curl_easy *src)
897
0
{
898
0
  CURLcode result = CURLE_OK;
899
0
  enum dupstring i;
900
0
  enum dupblob j;
901
902
  /* Copy src->set into dst->set first, then deal with the strings
903
     afterwards */
904
0
  dst->set = src->set;
905
0
  Curl_mime_initpart(&dst->set.mimepost);
906
907
  /* clear all dest string and blob pointers first, in case we error out
908
     mid-function */
909
0
  memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
910
0
  memset(dst->set.blobs, 0, BLOB_LAST * sizeof(struct curl_blob *));
911
912
  /* duplicate all strings */
913
0
  for(i = (enum dupstring)0; i < STRING_LASTZEROTERMINATED; i++) {
914
0
    result = Curl_setstropt(&dst->set.str[i], src->set.str[i]);
915
0
    if(result)
916
0
      return result;
917
0
  }
918
919
  /* duplicate all blobs */
920
0
  for(j = (enum dupblob)0; j < BLOB_LAST; j++) {
921
0
    result = Curl_setblobopt(&dst->set.blobs[j], src->set.blobs[j]);
922
0
    if(result)
923
0
      return result;
924
0
  }
925
926
  /* duplicate memory areas pointed to */
927
0
  i = STRING_COPYPOSTFIELDS;
928
0
  if(src->set.str[i]) {
929
0
    if(src->set.postfieldsize == -1)
930
0
      dst->set.str[i] = strdup(src->set.str[i]);
931
0
    else
932
      /* postfieldsize is curl_off_t, Curl_memdup() takes a size_t ... */
933
0
      dst->set.str[i] = Curl_memdup(src->set.str[i],
934
0
                                    curlx_sotouz(src->set.postfieldsize));
935
0
    if(!dst->set.str[i])
936
0
      return CURLE_OUT_OF_MEMORY;
937
    /* point to the new copy */
938
0
    dst->set.postfields = dst->set.str[i];
939
0
  }
940
941
  /* Duplicate mime data. */
942
0
  result = Curl_mime_duppart(dst, &dst->set.mimepost, &src->set.mimepost);
943
944
0
  if(src->set.resolve)
945
0
    dst->state.resolve = dst->set.resolve;
946
947
0
  return result;
948
0
}
949
950
static void dupeasy_meta_freeentry(void *p)
951
0
{
952
0
  (void)p;
953
  /* Will always be FALSE. Cannot use a 0 assert here since compilers
954
   * are not in agreement if they then want a NORETURN attribute or
955
   * not. *sigh* */
956
0
  DEBUGASSERT(p == NULL);
957
0
}
958
959
/*
960
 * curl_easy_duphandle() is an external interface to allow duplication of a
961
 * given input easy handle. The returned handle will be a new working handle
962
 * with all options set exactly as the input source handle.
963
 */
964
CURL *curl_easy_duphandle(CURL *d)
965
0
{
966
0
  struct Curl_easy *data = d;
967
0
  struct Curl_easy *outcurl = NULL;
968
969
0
  if(!GOOD_EASY_HANDLE(data))
970
0
    goto fail;
971
0
  outcurl = calloc(1, sizeof(struct Curl_easy));
972
0
  if(!outcurl)
973
0
    goto fail;
974
975
  /*
976
   * We setup a few buffers we need. We should probably make them
977
   * get setup on-demand in the code, as that would probably decrease
978
   * the likeliness of us forgetting to init a buffer here in the future.
979
   */
980
0
  outcurl->set.buffer_size = data->set.buffer_size;
981
982
0
  Curl_hash_init(&outcurl->meta_hash, 23,
983
0
                 Curl_hash_str, curlx_str_key_compare, dupeasy_meta_freeentry);
984
0
  curlx_dyn_init(&outcurl->state.headerb, CURL_MAX_HTTP_HEADER);
985
0
  Curl_netrc_init(&outcurl->state.netrc);
986
987
  /* the connection pool is setup on demand */
988
0
  outcurl->state.lastconnect_id = -1;
989
0
  outcurl->state.recent_conn_id = -1;
990
0
  outcurl->id = -1;
991
0
  outcurl->mid = UINT_MAX;
992
0
  outcurl->master_mid = UINT_MAX;
993
994
0
#ifndef CURL_DISABLE_HTTP
995
0
  Curl_llist_init(&outcurl->state.httphdrs, NULL);
996
0
#endif
997
0
  Curl_initinfo(outcurl);
998
999
  /* copy all userdefined values */
1000
0
  if(dupset(outcurl, data))
1001
0
    goto fail;
1002
1003
0
  outcurl->progress.hide     = data->progress.hide;
1004
0
  outcurl->progress.callback = data->progress.callback;
1005
1006
0
#ifndef CURL_DISABLE_COOKIES
1007
0
  outcurl->state.cookielist = NULL;
1008
0
  if(data->cookies && data->state.cookie_engine) {
1009
    /* If cookies are enabled in the parent handle, we enable them
1010
       in the clone as well! */
1011
0
    outcurl->cookies = Curl_cookie_init();
1012
0
    if(!outcurl->cookies)
1013
0
      goto fail;
1014
0
    outcurl->state.cookie_engine = TRUE;
1015
0
  }
1016
1017
0
  if(data->state.cookielist) {
1018
0
    outcurl->state.cookielist = Curl_slist_duplicate(data->state.cookielist);
1019
0
    if(!outcurl->state.cookielist)
1020
0
      goto fail;
1021
0
  }
1022
0
#endif
1023
1024
0
  if(data->state.url) {
1025
0
    outcurl->state.url = strdup(data->state.url);
1026
0
    if(!outcurl->state.url)
1027
0
      goto fail;
1028
0
    outcurl->state.url_alloc = TRUE;
1029
0
  }
1030
1031
0
  if(data->state.referer) {
1032
0
    outcurl->state.referer = strdup(data->state.referer);
1033
0
    if(!outcurl->state.referer)
1034
0
      goto fail;
1035
0
    outcurl->state.referer_alloc = TRUE;
1036
0
  }
1037
1038
  /* Reinitialize an SSL engine for the new handle
1039
   * note: the engine name has already been copied by dupset */
1040
0
  if(outcurl->set.str[STRING_SSL_ENGINE]) {
1041
0
    if(Curl_ssl_set_engine(outcurl, outcurl->set.str[STRING_SSL_ENGINE]))
1042
0
      goto fail;
1043
0
  }
1044
1045
0
#ifndef CURL_DISABLE_ALTSVC
1046
0
  if(data->asi) {
1047
0
    outcurl->asi = Curl_altsvc_init();
1048
0
    if(!outcurl->asi)
1049
0
      goto fail;
1050
0
    if(outcurl->set.str[STRING_ALTSVC])
1051
0
      (void)Curl_altsvc_load(outcurl->asi, outcurl->set.str[STRING_ALTSVC]);
1052
0
  }
1053
0
#endif
1054
0
#ifndef CURL_DISABLE_HSTS
1055
0
  if(data->hsts) {
1056
0
    outcurl->hsts = Curl_hsts_init();
1057
0
    if(!outcurl->hsts)
1058
0
      goto fail;
1059
0
    if(outcurl->set.str[STRING_HSTS])
1060
0
      (void)Curl_hsts_loadfile(outcurl,
1061
0
                               outcurl->hsts, outcurl->set.str[STRING_HSTS]);
1062
0
    (void)Curl_hsts_loadcb(outcurl, outcurl->hsts);
1063
0
  }
1064
0
#endif
1065
1066
0
  outcurl->magic = CURLEASY_MAGIC_NUMBER;
1067
1068
  /* we reach this point and thus we are OK */
1069
1070
0
  return outcurl;
1071
1072
0
fail:
1073
1074
0
  if(outcurl) {
1075
0
#ifndef CURL_DISABLE_COOKIES
1076
0
    free(outcurl->cookies);
1077
0
#endif
1078
0
    curlx_dyn_free(&outcurl->state.headerb);
1079
0
    Curl_altsvc_cleanup(&outcurl->asi);
1080
0
    Curl_hsts_cleanup(&outcurl->hsts);
1081
0
    Curl_freeset(outcurl);
1082
0
    free(outcurl);
1083
0
  }
1084
1085
0
  return NULL;
1086
0
}
1087
1088
/*
1089
 * curl_easy_reset() is an external interface that allows an app to re-
1090
 * initialize a session handle to the default values.
1091
 */
1092
void curl_easy_reset(CURL *d)
1093
0
{
1094
0
  struct Curl_easy *data = d;
1095
0
  if(!GOOD_EASY_HANDLE(data))
1096
0
    return;
1097
1098
0
  Curl_req_hard_reset(&data->req, data);
1099
0
  Curl_hash_clean(&data->meta_hash);
1100
1101
  /* clear all meta data */
1102
0
  Curl_meta_reset(data);
1103
  /* clear any resolve data */
1104
0
  Curl_async_shutdown(data);
1105
0
  Curl_resolv_unlink(data, &data->state.dns[0]);
1106
0
  Curl_resolv_unlink(data, &data->state.dns[1]);
1107
  /* zero out UserDefined data: */
1108
0
  Curl_freeset(data);
1109
0
  memset(&data->set, 0, sizeof(struct UserDefined));
1110
0
  Curl_init_userdefined(data);
1111
1112
  /* zero out Progress data: */
1113
0
  memset(&data->progress, 0, sizeof(struct Progress));
1114
1115
  /* zero out PureInfo data: */
1116
0
  Curl_initinfo(data);
1117
1118
0
  data->progress.hide = TRUE;
1119
0
  data->state.current_speed = -1; /* init to negative == impossible */
1120
0
  data->state.recent_conn_id = -1; /* clear remembered connection id */
1121
1122
  /* zero out authentication data: */
1123
0
  memset(&data->state.authhost, 0, sizeof(struct auth));
1124
0
  memset(&data->state.authproxy, 0, sizeof(struct auth));
1125
1126
0
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_DIGEST_AUTH)
1127
0
  Curl_http_auth_cleanup_digest(data);
1128
0
#endif
1129
0
  data->master_mid = UINT_MAX;
1130
0
}
1131
1132
/*
1133
 * curl_easy_pause() allows an application to pause or unpause a specific
1134
 * transfer and direction. This function sets the full new state for the
1135
 * current connection this easy handle operates on.
1136
 *
1137
 * NOTE: if you have the receiving paused and you call this function to remove
1138
 * the pausing, you may get your write callback called at this point.
1139
 *
1140
 * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
1141
 *
1142
 * NOTE: This is one of few API functions that are allowed to be called from
1143
 * within a callback.
1144
 */
1145
CURLcode curl_easy_pause(CURL *d, int action)
1146
0
{
1147
0
  CURLcode result = CURLE_OK;
1148
0
  bool recursive = FALSE;
1149
0
  bool changed = FALSE;
1150
0
  struct Curl_easy *data = d;
1151
0
  bool recv_paused, recv_paused_new;
1152
0
  bool send_paused, send_paused_new;
1153
1154
0
  if(!GOOD_EASY_HANDLE(data) || !data->conn)
1155
    /* crazy input, do not continue */
1156
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
1157
1158
0
  if(Curl_is_in_callback(data))
1159
0
    recursive = TRUE;
1160
1161
0
  recv_paused = Curl_xfer_recv_is_paused(data);
1162
0
  recv_paused_new = (action & CURLPAUSE_RECV);
1163
0
  send_paused = Curl_xfer_send_is_paused(data);
1164
0
  send_paused_new = (action & CURLPAUSE_SEND);
1165
1166
0
  if((send_paused != send_paused_new) ||
1167
0
     (send_paused_new != Curl_creader_is_paused(data))) {
1168
0
    changed = TRUE;
1169
0
    result = Curl_1st_err(result, Curl_xfer_pause_send(data, send_paused_new));
1170
0
  }
1171
1172
0
  if(recv_paused != recv_paused_new) {
1173
0
    changed = TRUE;
1174
0
    result = Curl_1st_err(result, Curl_xfer_pause_recv(data, recv_paused_new));
1175
0
  }
1176
1177
  /* If not completely pausing both directions now, run again in any case. */
1178
0
  if(!Curl_xfer_is_blocked(data)) {
1179
    /* reset the too-slow time keeper */
1180
0
    data->state.keeps_speed.tv_sec = 0;
1181
0
    if(data->multi) {
1182
0
      Curl_multi_mark_dirty(data); /* make it run */
1183
      /* On changes, tell application to update its timers. */
1184
0
      if(changed) {
1185
0
        if(Curl_update_timer(data->multi) && !result)
1186
0
          result = CURLE_ABORTED_BY_CALLBACK;
1187
0
      }
1188
0
    }
1189
0
  }
1190
1191
0
  if(!result && changed && !data->state.done && data->multi)
1192
    /* pause/unpausing may result in multi event changes */
1193
0
    if(Curl_multi_ev_assess_xfer(data->multi, data) && !result)
1194
0
      result = CURLE_ABORTED_BY_CALLBACK;
1195
1196
0
  if(recursive)
1197
    /* this might have called a callback recursively which might have set this
1198
       to false again on exit */
1199
0
    Curl_set_in_callback(data, TRUE);
1200
1201
0
  return result;
1202
0
}
1203
1204
1205
static CURLcode easy_connection(struct Curl_easy *data,
1206
                                struct connectdata **connp)
1207
0
{
1208
0
  curl_socket_t sfd;
1209
1210
0
  if(!data)
1211
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
1212
1213
  /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
1214
0
  if(!data->set.connect_only) {
1215
0
    failf(data, "CONNECT_ONLY is required");
1216
0
    return CURLE_UNSUPPORTED_PROTOCOL;
1217
0
  }
1218
1219
0
  sfd = Curl_getconnectinfo(data, connp);
1220
1221
0
  if(sfd == CURL_SOCKET_BAD) {
1222
0
    failf(data, "Failed to get recent socket");
1223
0
    return CURLE_UNSUPPORTED_PROTOCOL;
1224
0
  }
1225
1226
0
  return CURLE_OK;
1227
0
}
1228
1229
/*
1230
 * Receives data from the connected socket. Use after successful
1231
 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1232
 * Returns CURLE_OK on success, error code on error.
1233
 */
1234
CURLcode curl_easy_recv(CURL *d, void *buffer, size_t buflen, size_t *n)
1235
0
{
1236
0
  CURLcode result;
1237
0
  struct connectdata *c;
1238
0
  struct Curl_easy *data = d;
1239
1240
0
  if(!GOOD_EASY_HANDLE(data))
1241
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
1242
0
  if(Curl_is_in_callback(data))
1243
0
    return CURLE_RECURSIVE_API_CALL;
1244
1245
0
  result = easy_connection(data, &c);
1246
0
  if(result)
1247
0
    return result;
1248
1249
0
  if(!data->conn)
1250
    /* on first invoke, the transfer has been detached from the connection and
1251
       needs to be reattached */
1252
0
    Curl_attach_connection(data, c);
1253
1254
0
  *n = 0;
1255
0
  return Curl_conn_recv(data, FIRSTSOCKET, buffer, buflen, n);
1256
0
}
1257
1258
#ifndef CURL_DISABLE_WEBSOCKETS
1259
CURLcode Curl_connect_only_attach(struct Curl_easy *data)
1260
0
{
1261
0
  CURLcode result;
1262
0
  struct connectdata *c = NULL;
1263
1264
0
  result = easy_connection(data, &c);
1265
0
  if(result)
1266
0
    return result;
1267
1268
0
  if(!data->conn)
1269
    /* on first invoke, the transfer has been detached from the connection and
1270
       needs to be reattached */
1271
0
    Curl_attach_connection(data, c);
1272
1273
0
  return CURLE_OK;
1274
0
}
1275
#endif /* !CURL_DISABLE_WEBSOCKETS */
1276
1277
/*
1278
 * Sends data over the connected socket.
1279
 *
1280
 * This is the private internal version of curl_easy_send()
1281
 */
1282
CURLcode Curl_senddata(struct Curl_easy *data, const void *buffer,
1283
                       size_t buflen, size_t *n)
1284
0
{
1285
0
  CURLcode result;
1286
0
  struct connectdata *c = NULL;
1287
0
  SIGPIPE_VARIABLE(pipe_st);
1288
1289
0
  *n = 0;
1290
0
  result = easy_connection(data, &c);
1291
0
  if(result)
1292
0
    return result;
1293
1294
0
  if(!data->conn)
1295
    /* on first invoke, the transfer has been detached from the connection and
1296
       needs to be reattached */
1297
0
    Curl_attach_connection(data, c);
1298
1299
0
  sigpipe_ignore(data, &pipe_st);
1300
0
  result = Curl_conn_send(data, FIRSTSOCKET, buffer, buflen, FALSE, n);
1301
0
  sigpipe_restore(&pipe_st);
1302
1303
0
  if(result && result != CURLE_AGAIN)
1304
0
    return CURLE_SEND_ERROR;
1305
0
  return result;
1306
0
}
1307
1308
/*
1309
 * Sends data over the connected socket. Use after successful
1310
 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1311
 */
1312
CURLcode curl_easy_send(CURL *d, const void *buffer, size_t buflen, size_t *n)
1313
0
{
1314
0
  size_t written = 0;
1315
0
  CURLcode result;
1316
0
  struct Curl_easy *data = d;
1317
0
  if(!GOOD_EASY_HANDLE(data))
1318
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
1319
0
  if(Curl_is_in_callback(data))
1320
0
    return CURLE_RECURSIVE_API_CALL;
1321
1322
0
  result = Curl_senddata(data, buffer, buflen, &written);
1323
0
  *n = written;
1324
0
  return result;
1325
0
}
1326
1327
/*
1328
 * Performs connection upkeep for the given session handle.
1329
 */
1330
CURLcode curl_easy_upkeep(CURL *d)
1331
0
{
1332
0
  struct Curl_easy *data = d;
1333
  /* Verify that we got an easy handle we can work with. */
1334
0
  if(!GOOD_EASY_HANDLE(data))
1335
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
1336
1337
0
  if(Curl_is_in_callback(data))
1338
0
    return CURLE_RECURSIVE_API_CALL;
1339
1340
  /* Use the common function to keep connections alive. */
1341
0
  return Curl_cpool_upkeep(data);
1342
0
}
1343
1344
CURLcode curl_easy_ssls_import(CURL *d, const char *session_key,
1345
                               const unsigned char *shmac, size_t shmac_len,
1346
                               const unsigned char *sdata, size_t sdata_len)
1347
0
{
1348
#if defined(USE_SSL) && defined(USE_SSLS_EXPORT)
1349
  struct Curl_easy *data = d;
1350
  if(!GOOD_EASY_HANDLE(data))
1351
    return CURLE_BAD_FUNCTION_ARGUMENT;
1352
  return Curl_ssl_session_import(data, session_key,
1353
                                 shmac, shmac_len, sdata, sdata_len);
1354
#else
1355
0
  (void)d;
1356
0
  (void)session_key;
1357
0
  (void)shmac;
1358
0
  (void)shmac_len;
1359
0
  (void)sdata;
1360
0
  (void)sdata_len;
1361
0
  return CURLE_NOT_BUILT_IN;
1362
0
#endif
1363
0
}
1364
1365
CURLcode curl_easy_ssls_export(CURL *d,
1366
                               curl_ssls_export_cb *export_fn,
1367
                               void *userptr)
1368
0
{
1369
#if defined(USE_SSL) && defined(USE_SSLS_EXPORT)
1370
  struct Curl_easy *data = d;
1371
  if(!GOOD_EASY_HANDLE(data))
1372
    return CURLE_BAD_FUNCTION_ARGUMENT;
1373
  return Curl_ssl_session_export(data, export_fn, userptr);
1374
#else
1375
0
  (void)d;
1376
0
  (void)export_fn;
1377
0
  (void)userptr;
1378
0
  return CURLE_NOT_BUILT_IN;
1379
0
#endif
1380
0
}
1381
1382
CURLcode Curl_meta_set(struct Curl_easy *data, const char *key,
1383
                       void *meta_data, Curl_meta_dtor *meta_dtor)
1384
0
{
1385
0
  DEBUGASSERT(meta_data); /* never set to NULL */
1386
0
  if(!Curl_hash_add2(&data->meta_hash, CURL_UNCONST(key), strlen(key) + 1,
1387
0
                     meta_data, meta_dtor)) {
1388
0
    meta_dtor(CURL_UNCONST(key), strlen(key) + 1, meta_data);
1389
0
    return CURLE_OUT_OF_MEMORY;
1390
0
  }
1391
0
  return CURLE_OK;
1392
0
}
1393
1394
void Curl_meta_remove(struct Curl_easy *data, const char *key)
1395
0
{
1396
0
  Curl_hash_delete(&data->meta_hash, CURL_UNCONST(key), strlen(key) + 1);
1397
0
}
1398
1399
void *Curl_meta_get(struct Curl_easy *data, const char *key)
1400
0
{
1401
0
  return Curl_hash_pick(&data->meta_hash, CURL_UNCONST(key), strlen(key) + 1);
1402
0
}
1403
1404
void Curl_meta_reset(struct Curl_easy *data)
1405
0
{
1406
0
  Curl_hash_clean(&data->meta_hash);
1407
0
}