Coverage Report

Created: 2026-07-16 06:34

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