Coverage Report

Created: 2026-10-15 06:25

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