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