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