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