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 */
global_init(long flags,bool memoryfuncs)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 */
curl_global_init(long flags)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 */
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)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 */
curl_global_cleanup(void)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 */
curl_global_trace(const char * config)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 */
curl_global_sslset(curl_sslbackend id,const char * name,const curl_ssl_backend *** avail)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 */
curl_easy_init(void)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 */
events_timer(CURLM * multi,long timeout_ms,void * userp)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 */
poll2cselect(int pollmask)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 */
socketcb2poll(int pollmask)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 */
events_socket(CURL * easy,curl_socket_t s,int what,void * userp,void * socketp)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 */
events_setup(struct Curl_multi * multi,struct events * ev)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
wait_or_timeout(struct Curl_multi * multi,struct events * ev)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 */
easy_events(struct Curl_multi * multi)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
easy_transfer(struct Curl_multi * multi)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 */
easy_perform(struct Curl_easy * data,bool events)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 */
curl_easy_perform(CURL * data)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 */
curl_easy_perform_ev(struct Curl_easy * data)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 */
curl_easy_cleanup(CURL * ptr)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
curl_easy_getinfo(CURL * data,CURLINFO info,...)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
dupset(struct Curl_easy * dst,struct Curl_easy * src)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 */
curl_easy_duphandle(CURL * d)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
944 /* the connection pool is setup on demand */
945 outcurl->state.lastconnect_id = -1;
946 outcurl->state.recent_conn_id = -1;
947 outcurl->id = -1;
948
949 outcurl->progress.flags = data->progress.flags;
950 outcurl->progress.callback = data->progress.callback;
951
952 #ifndef CURL_DISABLE_COOKIES
953 outcurl->state.cookielist = NULL;
954 if(data->cookies && data->state.cookie_engine) {
955 /* If cookies are enabled in the parent handle, we enable them
956 in the clone as well! */
957 outcurl->cookies = Curl_cookie_init(outcurl, NULL, outcurl->cookies,
958 data->set.cookiesession);
959 if(!outcurl->cookies)
960 goto fail;
961 }
962
963 if(data->state.cookielist) {
964 outcurl->state.cookielist = Curl_slist_duplicate(data->state.cookielist);
965 if(!outcurl->state.cookielist)
966 goto fail;
967 }
968 #endif
969
970 if(data->state.url) {
971 outcurl->state.url = strdup(data->state.url);
972 if(!outcurl->state.url)
973 goto fail;
974 outcurl->state.url_alloc = TRUE;
975 }
976
977 if(data->state.referer) {
978 outcurl->state.referer = strdup(data->state.referer);
979 if(!outcurl->state.referer)
980 goto fail;
981 outcurl->state.referer_alloc = TRUE;
982 }
983
984 /* Reinitialize an SSL engine for the new handle
985 * note: the engine name has already been copied by dupset */
986 if(outcurl->set.str[STRING_SSL_ENGINE]) {
987 if(Curl_ssl_set_engine(outcurl, outcurl->set.str[STRING_SSL_ENGINE]))
988 goto fail;
989 }
990
991 #ifndef CURL_DISABLE_ALTSVC
992 if(data->asi) {
993 outcurl->asi = Curl_altsvc_init();
994 if(!outcurl->asi)
995 goto fail;
996 if(outcurl->set.str[STRING_ALTSVC])
997 (void)Curl_altsvc_load(outcurl->asi, outcurl->set.str[STRING_ALTSVC]);
998 }
999 #endif
1000 #ifndef CURL_DISABLE_HSTS
1001 if(data->hsts) {
1002 outcurl->hsts = Curl_hsts_init();
1003 if(!outcurl->hsts)
1004 goto fail;
1005 if(outcurl->set.str[STRING_HSTS])
1006 (void)Curl_hsts_loadfile(outcurl,
1007 outcurl->hsts, outcurl->set.str[STRING_HSTS]);
1008 (void)Curl_hsts_loadcb(outcurl, outcurl->hsts);
1009 }
1010 #endif
1011
1012 #ifdef CURLRES_ASYNCH
1013 /* Clone the resolver handle, if present, for the new handle */
1014 if(Curl_resolver_duphandle(outcurl,
1015 &outcurl->state.async.resolver,
1016 data->state.async.resolver))
1017 goto fail;
1018 #endif
1019
1020 #ifdef USE_ARES
1021 {
1022 CURLcode rc;
1023
1024 rc = Curl_set_dns_servers(outcurl, data->set.str[STRING_DNS_SERVERS]);
1025 if(rc && rc != CURLE_NOT_BUILT_IN)
1026 goto fail;
1027
1028 rc = Curl_set_dns_interface(outcurl, data->set.str[STRING_DNS_INTERFACE]);
1029 if(rc && rc != CURLE_NOT_BUILT_IN)
1030 goto fail;
1031
1032 rc = Curl_set_dns_local_ip4(outcurl, data->set.str[STRING_DNS_LOCAL_IP4]);
1033 if(rc && rc != CURLE_NOT_BUILT_IN)
1034 goto fail;
1035
1036 rc = Curl_set_dns_local_ip6(outcurl, data->set.str[STRING_DNS_LOCAL_IP6]);
1037 if(rc && rc != CURLE_NOT_BUILT_IN)
1038 goto fail;
1039 }
1040 #endif /* USE_ARES */
1041 #ifndef CURL_DISABLE_HTTP
1042 Curl_llist_init(&outcurl->state.httphdrs, NULL);
1043 #endif
1044 Curl_initinfo(outcurl);
1045
1046 outcurl->magic = CURLEASY_MAGIC_NUMBER;
1047
1048 /* we reach this point and thus we are OK */
1049
1050 return outcurl;
1051
1052 fail:
1053
1054 if(outcurl) {
1055 #ifndef CURL_DISABLE_COOKIES
1056 free(outcurl->cookies);
1057 #endif
1058 Curl_dyn_free(&outcurl->state.headerb);
1059 Curl_altsvc_cleanup(&outcurl->asi);
1060 Curl_hsts_cleanup(&outcurl->hsts);
1061 Curl_freeset(outcurl);
1062 free(outcurl);
1063 }
1064
1065 return NULL;
1066 }
1067
1068 /*
1069 * curl_easy_reset() is an external interface that allows an app to re-
1070 * initialize a session handle to the default values.
1071 */
curl_easy_reset(CURL * d)1072 void curl_easy_reset(CURL *d)
1073 {
1074 struct Curl_easy *data = d;
1075 Curl_req_hard_reset(&data->req, data);
1076
1077 /* zero out UserDefined data: */
1078 Curl_freeset(data);
1079 memset(&data->set, 0, sizeof(struct UserDefined));
1080 (void)Curl_init_userdefined(data);
1081
1082 /* zero out Progress data: */
1083 memset(&data->progress, 0, sizeof(struct Progress));
1084
1085 /* zero out PureInfo data: */
1086 Curl_initinfo(data);
1087
1088 data->progress.flags |= PGRS_HIDE;
1089 data->state.current_speed = -1; /* init to negative == impossible */
1090 data->state.retrycount = 0; /* reset the retry counter */
1091
1092 /* zero out authentication data: */
1093 memset(&data->state.authhost, 0, sizeof(struct auth));
1094 memset(&data->state.authproxy, 0, sizeof(struct auth));
1095
1096 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_DIGEST_AUTH)
1097 Curl_http_auth_cleanup_digest(data);
1098 #endif
1099 }
1100
1101 /*
1102 * curl_easy_pause() allows an application to pause or unpause a specific
1103 * transfer and direction. This function sets the full new state for the
1104 * current connection this easy handle operates on.
1105 *
1106 * NOTE: if you have the receiving paused and you call this function to remove
1107 * the pausing, you may get your write callback called at this point.
1108 *
1109 * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
1110 *
1111 * NOTE: This is one of few API functions that are allowed to be called from
1112 * within a callback.
1113 */
curl_easy_pause(CURL * d,int action)1114 CURLcode curl_easy_pause(CURL *d, int action)
1115 {
1116 struct SingleRequest *k;
1117 CURLcode result = CURLE_OK;
1118 int oldstate;
1119 int newstate;
1120 bool recursive = FALSE;
1121 bool keep_changed, unpause_read, not_all_paused;
1122 struct Curl_easy *data = d;
1123
1124 if(!GOOD_EASY_HANDLE(data) || !data->conn)
1125 /* crazy input, do not continue */
1126 return CURLE_BAD_FUNCTION_ARGUMENT;
1127
1128 if(Curl_is_in_callback(data))
1129 recursive = TRUE;
1130 k = &data->req;
1131 oldstate = k->keepon & (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
1132
1133 /* first switch off both pause bits then set the new pause bits */
1134 newstate = (k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE)) |
1135 ((action & CURLPAUSE_RECV) ? KEEP_RECV_PAUSE : 0) |
1136 ((action & CURLPAUSE_SEND) ? KEEP_SEND_PAUSE : 0);
1137
1138 keep_changed = ((newstate & (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE)) != oldstate);
1139 not_all_paused = (newstate & (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) !=
1140 (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE);
1141 unpause_read = ((k->keepon & ~newstate & KEEP_SEND_PAUSE) &&
1142 (data->mstate == MSTATE_PERFORMING ||
1143 data->mstate == MSTATE_RATELIMITING));
1144 /* Unpausing writes is detected on the next run in
1145 * transfer.c:Curl_sendrecv(). This is because this may result
1146 * in a transfer error if the application's callbacks fail */
1147
1148 /* Set the new keepon state, so it takes effect no matter what error
1149 * may happen afterwards. */
1150 k->keepon = newstate;
1151
1152 /* If not completely pausing both directions now, run again in any case. */
1153 if(not_all_paused) {
1154 Curl_expire(data, 0, EXPIRE_RUN_NOW);
1155 /* reset the too-slow time keeper */
1156 data->state.keeps_speed.tv_sec = 0;
1157 /* Simulate socket events on next run for unpaused directions */
1158 if(!(newstate & KEEP_SEND_PAUSE))
1159 data->state.select_bits |= CURL_CSELECT_OUT;
1160 if(!(newstate & KEEP_RECV_PAUSE))
1161 data->state.select_bits |= CURL_CSELECT_IN;
1162 /* On changes, tell application to update its timers. */
1163 if(keep_changed && data->multi) {
1164 if(Curl_update_timer(data->multi)) {
1165 result = CURLE_ABORTED_BY_CALLBACK;
1166 goto out;
1167 }
1168 }
1169 }
1170
1171 if(unpause_read) {
1172 result = Curl_creader_unpause(data);
1173 if(result)
1174 goto out;
1175 }
1176
1177 if(!(k->keepon & KEEP_RECV_PAUSE) && Curl_cwriter_is_paused(data)) {
1178 Curl_conn_ev_data_pause(data, FALSE);
1179 result = Curl_cwriter_unpause(data);
1180 }
1181
1182 out:
1183 if(!result && !data->state.done && keep_changed)
1184 /* This transfer may have been moved in or out of the bundle, update the
1185 corresponding socket callback, if used */
1186 result = Curl_updatesocket(data);
1187
1188 if(recursive)
1189 /* this might have called a callback recursively which might have set this
1190 to false again on exit */
1191 Curl_set_in_callback(data, TRUE);
1192
1193 return result;
1194 }
1195
1196
easy_connection(struct Curl_easy * data,struct connectdata ** connp)1197 static CURLcode easy_connection(struct Curl_easy *data,
1198 struct connectdata **connp)
1199 {
1200 curl_socket_t sfd;
1201
1202 if(!data)
1203 return CURLE_BAD_FUNCTION_ARGUMENT;
1204
1205 /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
1206 if(!data->set.connect_only) {
1207 failf(data, "CONNECT_ONLY is required");
1208 return CURLE_UNSUPPORTED_PROTOCOL;
1209 }
1210
1211 sfd = Curl_getconnectinfo(data, connp);
1212
1213 if(sfd == CURL_SOCKET_BAD) {
1214 failf(data, "Failed to get recent socket");
1215 return CURLE_UNSUPPORTED_PROTOCOL;
1216 }
1217
1218 return CURLE_OK;
1219 }
1220
1221 /*
1222 * Receives data from the connected socket. Use after successful
1223 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1224 * Returns CURLE_OK on success, error code on error.
1225 */
curl_easy_recv(CURL * d,void * buffer,size_t buflen,size_t * n)1226 CURLcode curl_easy_recv(CURL *d, void *buffer, size_t buflen, size_t *n)
1227 {
1228 CURLcode result;
1229 ssize_t n1;
1230 struct connectdata *c;
1231 struct Curl_easy *data = d;
1232
1233 if(Curl_is_in_callback(data))
1234 return CURLE_RECURSIVE_API_CALL;
1235
1236 result = easy_connection(data, &c);
1237 if(result)
1238 return result;
1239
1240 if(!data->conn)
1241 /* on first invoke, the transfer has been detached from the connection and
1242 needs to be reattached */
1243 Curl_attach_connection(data, c);
1244
1245 *n = 0;
1246 result = Curl_conn_recv(data, FIRSTSOCKET, buffer, buflen, &n1);
1247
1248 if(result)
1249 return result;
1250
1251 *n = (size_t)n1;
1252 return CURLE_OK;
1253 }
1254
1255 #ifndef CURL_DISABLE_WEBSOCKETS
Curl_connect_only_attach(struct Curl_easy * data)1256 CURLcode Curl_connect_only_attach(struct Curl_easy *data)
1257 {
1258 CURLcode result;
1259 struct connectdata *c = NULL;
1260
1261 result = easy_connection(data, &c);
1262 if(result)
1263 return result;
1264
1265 if(!data->conn)
1266 /* on first invoke, the transfer has been detached from the connection and
1267 needs to be reattached */
1268 Curl_attach_connection(data, c);
1269
1270 return CURLE_OK;
1271 }
1272 #endif /* !CURL_DISABLE_WEBSOCKETS */
1273
1274 /*
1275 * Sends data over the connected socket.
1276 *
1277 * This is the private internal version of curl_easy_send()
1278 */
Curl_senddata(struct Curl_easy * data,const void * buffer,size_t buflen,size_t * n)1279 CURLcode Curl_senddata(struct Curl_easy *data, const void *buffer,
1280 size_t buflen, size_t *n)
1281 {
1282 CURLcode result;
1283 struct connectdata *c = NULL;
1284 SIGPIPE_VARIABLE(pipe_st);
1285
1286 *n = 0;
1287 result = easy_connection(data, &c);
1288 if(result)
1289 return result;
1290
1291 if(!data->conn)
1292 /* on first invoke, the transfer has been detached from the connection and
1293 needs to be reattached */
1294 Curl_attach_connection(data, c);
1295
1296 sigpipe_ignore(data, &pipe_st);
1297 result = Curl_conn_send(data, FIRSTSOCKET, buffer, buflen, FALSE, n);
1298 sigpipe_restore(&pipe_st);
1299
1300 if(result && result != CURLE_AGAIN)
1301 return CURLE_SEND_ERROR;
1302 return result;
1303 }
1304
1305 /*
1306 * Sends data over the connected socket. Use after successful
1307 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1308 */
curl_easy_send(CURL * d,const void * buffer,size_t buflen,size_t * n)1309 CURLcode curl_easy_send(CURL *d, const void *buffer, size_t buflen, size_t *n)
1310 {
1311 size_t written = 0;
1312 CURLcode result;
1313 struct Curl_easy *data = d;
1314 if(Curl_is_in_callback(data))
1315 return CURLE_RECURSIVE_API_CALL;
1316
1317 result = Curl_senddata(data, buffer, buflen, &written);
1318 *n = written;
1319 return result;
1320 }
1321
1322 /*
1323 * Performs connection upkeep for the given session handle.
1324 */
curl_easy_upkeep(CURL * d)1325 CURLcode curl_easy_upkeep(CURL *d)
1326 {
1327 struct Curl_easy *data = d;
1328 /* Verify that we got an easy handle we can work with. */
1329 if(!GOOD_EASY_HANDLE(data))
1330 return CURLE_BAD_FUNCTION_ARGUMENT;
1331
1332 if(Curl_is_in_callback(data))
1333 return CURLE_RECURSIVE_API_CALL;
1334
1335 /* Use the common function to keep connections alive. */
1336 return Curl_cpool_upkeep(data);
1337 }
1338