Coverage Report

Created: 2026-08-31 06:47

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