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