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