Coverage Report

Created: 2026-08-13 07:18

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