1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
28 * See comment in curl_memory.h for the explanation of this sanity check.
31 #ifdef CURLX_NO_MEMORY_CALLBACKS
32 #error "libcurl shall not ever be built with CURLX_NO_MEMORY_CALLBACKS defined"
35 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.h>
41 #ifdef HAVE_ARPA_INET_H
42 #include <arpa/inet.h>
47 #ifdef HAVE_SYS_IOCTL_H
48 #include <sys/ioctl.h>
51 #ifdef HAVE_SYS_PARAM_H
52 #include <sys/param.h>
56 #include <curl/curl.h>
58 #include "vtls/vtls.h"
68 #include "sendf.h" /* for failf function prototype */
69 #include "connect.h" /* for Curl_getconnectinfo */
78 #include "http_digest.h"
79 #include "system_win32.h"
85 #include "easy_lock.h"
87 /* The last 3 #include files should be in this order */
88 #include "curl_printf.h"
89 #include "curl_memory.h"
92 /* true globals -- for curl_global_init() and curl_global_cleanup() */
93 static unsigned int initialized;
94 static long init_flags;
96 #ifdef GLOBAL_INIT_IS_THREADSAFE
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)
104 #define global_init_lock()
105 #define global_init_unlock()
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.
114 #if defined(_WIN32_WCE)
115 #define system_strdup _strdup
116 #elif !defined(HAVE_STRDUP)
117 #define system_strdup curlx_strdup
119 #define system_strdup strdup
122 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
123 # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
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.
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;
139 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
140 # pragma warning(default:4232) /* MSVC extension, dllimport identity */
144 static char *leakpointer;
148 * curl_global_init() globally initializes curl given a bitwise set of the
149 * different features of what to initialize.
151 static CURLcode global_init(long flags, bool 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;
168 if(!Curl_ssl_init()) {
169 DEBUGF(fprintf(stderr, "Error: Curl_ssl_init failed\n"));
174 if(Curl_win32_init(flags)) {
175 DEBUGF(fprintf(stderr, "Error: win32_init failed\n"));
181 if(Curl_amiga_init()) {
182 DEBUGF(fprintf(stderr, "Error: Curl_amiga_init failed\n"));
187 if(Curl_resolver_global_init()) {
188 DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n"));
193 if(Curl_ssh_init()) {
199 if(WS_SUCCESS != wolfSSH_Init()) {
200 DEBUGF(fprintf(stderr, "Error: wolfSSH_Init failed\n"));
201 return CURLE_FAILED_INIT;
208 if(getenv("CURL_GLOBAL_INIT"))
209 /* alloc data that will leak if *cleanup() is not called! */
210 leakpointer = malloc(1);
216 initialized--; /* undo the increase */
217 return CURLE_FAILED_INIT;
222 * curl_global_init() globally initializes curl given a bitwise set of the
223 * different features of what to initialize.
225 CURLcode curl_global_init(long flags)
230 result = global_init(flags, TRUE);
232 global_init_unlock();
238 * curl_global_init_mem() globally initializes curl and also registers the
239 * user provided callback routines.
241 CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
242 curl_free_callback f, curl_realloc_callback r,
243 curl_strdup_callback s, curl_calloc_callback c)
247 /* Invalid input, return immediately */
248 if(!m || !f || !r || !s || !c)
249 return CURLE_FAILED_INIT;
254 /* Already initialized, don't do it again, but bump the variable anyway to
255 work like curl_global_init() and require the same amount of cleanup
258 global_init_unlock();
262 /* set memory functions before global_init() in case it wants memory
270 /* Call the actual init function, but without setting */
271 result = global_init(flags, FALSE);
273 global_init_unlock();
279 * curl_global_cleanup() globally cleanups curl, uses the value of
280 * "init_flags" to determine what needs to be cleaned up and what doesn't.
282 void curl_global_cleanup(void)
287 global_init_unlock();
292 global_init_unlock();
297 Curl_resolver_global_cleanup();
300 Curl_win32_cleanup(init_flags);
303 Curl_amiga_cleanup();
308 (void)wolfSSH_Cleanup();
316 global_init_unlock();
320 * curl_global_sslset() globally initializes the SSL backend to use.
322 CURLsslset curl_global_sslset(curl_sslbackend id, const char *name,
323 const curl_ssl_backend ***avail)
329 rc = Curl_init_sslset_nolock(id, name, avail);
331 global_init_unlock();
337 * curl_easy_init() is the external interface to alloc, setup and init an
338 * easy handle that is returned. If anything goes wrong, NULL is returned.
340 struct Curl_easy *curl_easy_init(void)
343 struct Curl_easy *data;
345 /* Make sure we inited the global SSL stuff */
349 result = global_init(CURL_GLOBAL_DEFAULT, TRUE);
351 /* something in the global init failed, return nothing */
352 DEBUGF(fprintf(stderr, "Error: curl_global_init failed\n"));
353 global_init_unlock();
357 global_init_unlock();
359 /* We use curl_open() with undefined URL so far */
360 result = Curl_open(&data);
362 DEBUGF(fprintf(stderr, "Error: Curl_open failed\n"));
371 struct socketmonitor {
372 struct socketmonitor *next; /* the next node in the list or NULL */
373 struct pollfd socket; /* socket info of what to monitor */
377 long ms; /* timeout, run the timeout function when reached */
378 bool msbump; /* set TRUE when timeout is set by callback */
379 int num_sockets; /* number of nodes in the monitor list */
380 struct socketmonitor *list; /* list of sockets to monitor */
381 int running_handles; /* store the returned number */
386 * Callback that gets called with a new value when the timeout should be
390 static int events_timer(struct Curl_multi *multi, /* multi handle */
391 long timeout_ms, /* see above */
392 void *userp) /* private callback pointer */
394 struct events *ev = userp;
397 /* timeout removed */
399 else if(timeout_ms == 0)
400 /* timeout is already reached! */
401 timeout_ms = 1; /* trigger asap */
411 * convert from poll() bit definitions to libcurl's CURL_CSELECT_* ones
413 static int poll2cselect(int pollmask)
416 if(pollmask & POLLIN)
417 omask |= CURL_CSELECT_IN;
418 if(pollmask & POLLOUT)
419 omask |= CURL_CSELECT_OUT;
420 if(pollmask & POLLERR)
421 omask |= CURL_CSELECT_ERR;
428 * convert from libcurl' CURL_POLL_* bit definitions to poll()'s
430 static short socketcb2poll(int pollmask)
433 if(pollmask & CURL_POLL_IN)
435 if(pollmask & CURL_POLL_OUT)
442 * Callback that gets called with information about socket activity to
445 static int events_socket(struct Curl_easy *easy, /* easy handle */
446 curl_socket_t s, /* socket */
447 int what, /* see above */
448 void *userp, /* private callback
450 void *socketp) /* private socket
453 struct events *ev = userp;
454 struct socketmonitor *m;
455 struct socketmonitor *prev = NULL;
457 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
464 if(m->socket.fd == s) {
466 if(what == CURL_POLL_REMOVE) {
467 struct socketmonitor *nxt = m->next;
468 /* remove this node from the list of monitored sockets */
475 infof(easy, "socket cb: socket %d REMOVED", s);
478 /* The socket 's' is already being monitored, update the activity
479 mask. Convert from libcurl bitmask to the poll one. */
480 m->socket.events = socketcb2poll(what);
481 infof(easy, "socket cb: socket %d UPDATED as %s%s", s,
482 (what&CURL_POLL_IN)?"IN":"",
483 (what&CURL_POLL_OUT)?"OUT":"");
488 m = m->next; /* move to next node */
491 if(what == CURL_POLL_REMOVE) {
492 /* this happens a bit too often, libcurl fix perhaps? */
494 "%s: socket %d asked to be REMOVED but not present!\n",
498 m = malloc(sizeof(struct socketmonitor));
502 m->socket.events = socketcb2poll(what);
503 m->socket.revents = 0;
505 infof(easy, "socket cb: socket %d ADDED as %s%s", s,
506 (what&CURL_POLL_IN)?"IN":"",
507 (what&CURL_POLL_OUT)?"OUT":"");
510 return CURLE_OUT_OF_MEMORY;
521 * Do the multi handle setups that only event-based transfers need.
523 static void events_setup(struct Curl_multi *multi, struct events *ev)
526 curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, events_timer);
527 curl_multi_setopt(multi, CURLMOPT_TIMERDATA, ev);
529 /* socket callback */
530 curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, events_socket);
531 curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev);
537 * waits for activity on any of the given sockets, or the timeout to trigger.
540 static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
543 CURLMcode mcode = CURLM_OK;
544 CURLcode result = CURLE_OK;
548 struct socketmonitor *m;
550 struct pollfd fds[4];
554 struct curltime before;
555 struct curltime after;
557 /* populate the fds[] array */
558 for(m = ev->list, f = &fds[0]; m; m = m->next) {
559 f->fd = m->socket.fd;
560 f->events = m->socket.events;
562 /* fprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd); */
567 /* get the time stamp to use to figure out how long poll takes */
570 /* wait for activity or timeout */
571 pollrc = Curl_poll(fds, numfds, ev->ms);
573 return CURLE_UNRECOVERABLE_POLL;
577 ev->msbump = FALSE; /* reset here */
582 /* fprintf(stderr, "call curl_multi_socket_action(TIMEOUT)\n"); */
583 mcode = curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0,
584 &ev->running_handles);
587 /* here pollrc is > 0 */
589 /* loop over the monitored sockets to see which ones had activity */
590 for(i = 0; i< numfds; i++) {
592 /* socket activity, tell libcurl */
593 int act = poll2cselect(fds[i].revents); /* convert */
594 infof(multi->easyp, "call curl_multi_socket_action(socket %d)",
596 mcode = curl_multi_socket_action(multi, fds[i].fd, act,
597 &ev->running_handles);
602 /* If nothing updated the timeout, we decrease it by the spent time.
603 * If it was updated, it has the new timeout time stored already.
605 timediff_t timediff = Curl_timediff(after, before);
607 if(timediff > ev->ms)
610 ev->ms -= (long)timediff;
616 return CURLE_URL_MALFORMAT;
618 /* we don't really care about the "msgs_in_queue" value returned in the
620 msg = curl_multi_info_read(multi, &pollrc);
622 result = msg->data.result;
633 * Runs a transfer in a blocking manner using the events-based API
635 static CURLcode easy_events(struct Curl_multi *multi)
637 /* this struct is made static to allow it to be used after this function
638 returns and curl_multi_remove_handle() is called */
639 static struct events evs = {2, FALSE, 0, NULL, 0};
641 /* if running event-based, do some further multi inits */
642 events_setup(multi, &evs);
644 return wait_or_timeout(multi, &evs);
646 #else /* CURLDEBUG */
647 /* when not built with debug, this function doesn't exist */
648 #define easy_events(x) CURLE_NOT_BUILT_IN
651 static CURLcode easy_transfer(struct Curl_multi *multi)
654 CURLMcode mcode = CURLM_OK;
655 CURLcode result = CURLE_OK;
657 while(!done && !mcode) {
658 int still_running = 0;
660 mcode = curl_multi_poll(multi, NULL, 0, 1000, NULL);
663 mcode = curl_multi_perform(multi, &still_running);
665 /* only read 'still_running' if curl_multi_perform() return OK */
666 if(!mcode && !still_running) {
668 CURLMsg *msg = curl_multi_info_read(multi, &rc);
670 result = msg->data.result;
676 /* Make sure to return some kind of error if there was a multi problem */
678 result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
679 /* The other multi errors should never happen, so return
680 something suitably generic */
681 CURLE_BAD_FUNCTION_ARGUMENT;
689 * easy_perform() is the external interface that performs a blocking
690 * transfer as previously setup.
692 * CONCEPT: This function creates a multi handle, adds the easy handle to it,
693 * runs curl_multi_perform() until the transfer is done, then detaches the
694 * easy handle, destroys the multi handle and returns the easy handle's return
697 * REALITY: it can't just create and destroy the multi handle that easily. It
698 * needs to keep it around since if this easy handle is used again by this
699 * function, the same multi handle must be re-used so that the same pools and
700 * caches can be used.
702 * DEBUG: if 'events' is set TRUE, this function will use a replacement engine
703 * instead of curl_multi_perform() and use curl_multi_socket_action().
705 static CURLcode easy_perform(struct Curl_easy *data, bool events)
707 struct Curl_multi *multi;
709 CURLcode result = CURLE_OK;
710 SIGPIPE_VARIABLE(pipe_st);
713 return CURLE_BAD_FUNCTION_ARGUMENT;
715 if(data->set.errorbuffer)
716 /* clear this as early as possible */
717 data->set.errorbuffer[0] = 0;
720 failf(data, "easy handle already used in multi handle");
721 return CURLE_FAILED_INIT;
725 multi = data->multi_easy;
727 /* this multi handle will only ever have a single easy handled attached
728 to it, so make it use minimal hashes */
729 multi = Curl_multi_handle(1, 3, 7);
731 return CURLE_OUT_OF_MEMORY;
732 data->multi_easy = multi;
735 if(multi->in_callback)
736 return CURLE_RECURSIVE_API_CALL;
738 /* Copy the MAXCONNECTS option to the multi handle */
739 curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, data->set.maxconnects);
741 mcode = curl_multi_add_handle(multi, data);
743 curl_multi_cleanup(multi);
744 data->multi_easy = NULL;
745 if(mcode == CURLM_OUT_OF_MEMORY)
746 return CURLE_OUT_OF_MEMORY;
747 return CURLE_FAILED_INIT;
750 sigpipe_ignore(data, &pipe_st);
752 /* run the transfer */
753 result = events ? easy_events(multi) : easy_transfer(multi);
755 /* ignoring the return code isn't nice, but atm we can't really handle
756 a failure here, room for future improvement! */
757 (void)curl_multi_remove_handle(multi, data);
759 sigpipe_restore(&pipe_st);
761 /* The multi handle is kept alive, owned by the easy handle */
767 * curl_easy_perform() is the external interface that performs a blocking
768 * transfer as previously setup.
770 CURLcode curl_easy_perform(struct Curl_easy *data)
772 return easy_perform(data, FALSE);
777 * curl_easy_perform_ev() is the external interface that performs a blocking
778 * transfer using the event-based API internally.
780 CURLcode curl_easy_perform_ev(struct Curl_easy *data)
782 return easy_perform(data, TRUE);
788 * curl_easy_cleanup() is the external interface to cleaning/freeing the given
791 void curl_easy_cleanup(struct Curl_easy *data)
793 SIGPIPE_VARIABLE(pipe_st);
798 sigpipe_ignore(data, &pipe_st);
800 sigpipe_restore(&pipe_st);
804 * curl_easy_getinfo() is an external interface that allows an app to retrieve
805 * information from a performed transfer and similar.
807 #undef curl_easy_getinfo
808 CURLcode curl_easy_getinfo(struct Curl_easy *data, CURLINFO info, ...)
815 paramp = va_arg(arg, void *);
817 result = Curl_getinfo(data, info, paramp);
823 static CURLcode dupset(struct Curl_easy *dst, struct Curl_easy *src)
825 CURLcode result = CURLE_OK;
829 /* Copy src->set into dst->set first, then deal with the strings
832 Curl_mime_initpart(&dst->set.mimepost, dst);
834 /* clear all string pointers first */
835 memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
837 /* duplicate all strings */
838 for(i = (enum dupstring)0; i< STRING_LASTZEROTERMINATED; i++) {
839 result = Curl_setstropt(&dst->set.str[i], src->set.str[i]);
844 /* clear all blob pointers first */
845 memset(dst->set.blobs, 0, BLOB_LAST * sizeof(struct curl_blob *));
846 /* duplicate all blobs */
847 for(j = (enum dupblob)0; j < BLOB_LAST; j++) {
848 result = Curl_setblobopt(&dst->set.blobs[j], src->set.blobs[j]);
853 /* duplicate memory areas pointed to */
854 i = STRING_COPYPOSTFIELDS;
855 if(src->set.postfieldsize && src->set.str[i]) {
856 /* postfieldsize is curl_off_t, Curl_memdup() takes a size_t ... */
857 dst->set.str[i] = Curl_memdup(src->set.str[i],
858 curlx_sotouz(src->set.postfieldsize));
860 return CURLE_OUT_OF_MEMORY;
861 /* point to the new copy */
862 dst->set.postfields = dst->set.str[i];
865 /* Duplicate mime data. */
866 result = Curl_mime_duppart(&dst->set.mimepost, &src->set.mimepost);
869 dst->state.resolve = dst->set.resolve;
875 * curl_easy_duphandle() is an external interface to allow duplication of a
876 * given input easy handle. The returned handle will be a new working handle
877 * with all options set exactly as the input source handle.
879 struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
881 struct Curl_easy *outcurl = calloc(1, sizeof(struct Curl_easy));
886 * We setup a few buffers we need. We should probably make them
887 * get setup on-demand in the code, as that would probably decrease
888 * the likeliness of us forgetting to init a buffer here in the future.
890 outcurl->set.buffer_size = data->set.buffer_size;
892 /* copy all userdefined values */
893 if(dupset(outcurl, data))
896 Curl_dyn_init(&outcurl->state.headerb, CURL_MAX_HTTP_HEADER);
898 /* the connection cache is setup on demand */
899 outcurl->state.conn_cache = NULL;
900 outcurl->state.lastconnect_id = -1;
902 outcurl->progress.flags = data->progress.flags;
903 outcurl->progress.callback = data->progress.callback;
905 #ifndef CURL_DISABLE_COOKIES
907 /* If cookies are enabled in the parent handle, we enable them
908 in the clone as well! */
909 outcurl->cookies = Curl_cookie_init(data,
910 data->cookies->filename,
912 data->set.cookiesession);
913 if(!outcurl->cookies)
917 /* duplicate all values in 'change' */
918 if(data->state.cookielist) {
919 outcurl->state.cookielist =
920 Curl_slist_duplicate(data->state.cookielist);
921 if(!outcurl->state.cookielist)
926 if(data->state.url) {
927 outcurl->state.url = strdup(data->state.url);
928 if(!outcurl->state.url)
930 outcurl->state.url_alloc = TRUE;
933 if(data->state.referer) {
934 outcurl->state.referer = strdup(data->state.referer);
935 if(!outcurl->state.referer)
937 outcurl->state.referer_alloc = TRUE;
940 /* Reinitialize an SSL engine for the new handle
941 * note: the engine name has already been copied by dupset */
942 if(outcurl->set.str[STRING_SSL_ENGINE]) {
943 if(Curl_ssl_set_engine(outcurl, outcurl->set.str[STRING_SSL_ENGINE]))
947 #ifndef CURL_DISABLE_ALTSVC
949 outcurl->asi = Curl_altsvc_init();
952 if(outcurl->set.str[STRING_ALTSVC])
953 (void)Curl_altsvc_load(outcurl->asi, outcurl->set.str[STRING_ALTSVC]);
956 #ifndef CURL_DISABLE_HSTS
958 outcurl->hsts = Curl_hsts_init();
961 if(outcurl->set.str[STRING_HSTS])
962 (void)Curl_hsts_loadfile(outcurl,
963 outcurl->hsts, outcurl->set.str[STRING_HSTS]);
964 (void)Curl_hsts_loadcb(outcurl, outcurl->hsts);
967 /* Clone the resolver handle, if present, for the new handle */
968 if(Curl_resolver_duphandle(outcurl,
969 &outcurl->state.async.resolver,
970 data->state.async.resolver))
977 rc = Curl_set_dns_servers(outcurl, data->set.str[STRING_DNS_SERVERS]);
978 if(rc && rc != CURLE_NOT_BUILT_IN)
981 rc = Curl_set_dns_interface(outcurl, data->set.str[STRING_DNS_INTERFACE]);
982 if(rc && rc != CURLE_NOT_BUILT_IN)
985 rc = Curl_set_dns_local_ip4(outcurl, data->set.str[STRING_DNS_LOCAL_IP4]);
986 if(rc && rc != CURLE_NOT_BUILT_IN)
989 rc = Curl_set_dns_local_ip6(outcurl, data->set.str[STRING_DNS_LOCAL_IP6]);
990 if(rc && rc != CURLE_NOT_BUILT_IN)
993 #endif /* USE_ARES */
995 Curl_initinfo(outcurl);
997 outcurl->magic = CURLEASY_MAGIC_NUMBER;
999 /* we reach this point and thus we are OK */
1006 #ifndef CURL_DISABLE_COOKIES
1007 curl_slist_free_all(outcurl->state.cookielist);
1008 outcurl->state.cookielist = NULL;
1010 Curl_safefree(outcurl->state.buffer);
1011 Curl_dyn_free(&outcurl->state.headerb);
1012 Curl_safefree(outcurl->state.url);
1013 Curl_safefree(outcurl->state.referer);
1014 Curl_altsvc_cleanup(&outcurl->asi);
1015 Curl_hsts_cleanup(&outcurl->hsts);
1016 Curl_freeset(outcurl);
1024 * curl_easy_reset() is an external interface that allows an app to re-
1025 * initialize a session handle to the default values.
1027 void curl_easy_reset(struct Curl_easy *data)
1029 Curl_free_request_state(data);
1031 /* zero out UserDefined data: */
1033 memset(&data->set, 0, sizeof(struct UserDefined));
1034 (void)Curl_init_userdefined(data);
1036 /* zero out Progress data: */
1037 memset(&data->progress, 0, sizeof(struct Progress));
1039 /* zero out PureInfo data: */
1040 Curl_initinfo(data);
1042 data->progress.flags |= PGRS_HIDE;
1043 data->state.current_speed = -1; /* init to negative == impossible */
1044 data->state.retrycount = 0; /* reset the retry counter */
1046 /* zero out authentication data: */
1047 memset(&data->state.authhost, 0, sizeof(struct auth));
1048 memset(&data->state.authproxy, 0, sizeof(struct auth));
1050 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
1051 Curl_http_auth_cleanup_digest(data);
1056 * curl_easy_pause() allows an application to pause or unpause a specific
1057 * transfer and direction. This function sets the full new state for the
1058 * current connection this easy handle operates on.
1060 * NOTE: if you have the receiving paused and you call this function to remove
1061 * the pausing, you may get your write callback called at this point.
1063 * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
1065 * NOTE: This is one of few API functions that are allowed to be called from
1066 * within a callback.
1068 CURLcode curl_easy_pause(struct Curl_easy *data, int action)
1070 struct SingleRequest *k;
1071 CURLcode result = CURLE_OK;
1075 if(!GOOD_EASY_HANDLE(data) || !data->conn)
1076 /* crazy input, don't continue */
1077 return CURLE_BAD_FUNCTION_ARGUMENT;
1080 oldstate = k->keepon & (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
1082 /* first switch off both pause bits then set the new pause bits */
1083 newstate = (k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE)) |
1084 ((action & CURLPAUSE_RECV)?KEEP_RECV_PAUSE:0) |
1085 ((action & CURLPAUSE_SEND)?KEEP_SEND_PAUSE:0);
1087 if((newstate & (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE)) == oldstate) {
1088 /* Not changing any pause state, return */
1089 DEBUGF(infof(data, "pause: no change, early return"));
1093 /* Unpause parts in active mime tree. */
1094 if((k->keepon & ~newstate & KEEP_SEND_PAUSE) &&
1095 (data->mstate == MSTATE_PERFORMING ||
1096 data->mstate == MSTATE_RATELIMITING) &&
1097 data->state.fread_func == (curl_read_callback) Curl_mime_read) {
1098 Curl_mime_unpause(data->state.in);
1101 /* put it back in the keepon */
1102 k->keepon = newstate;
1104 if(!(newstate & KEEP_RECV_PAUSE)) {
1105 Curl_http2_stream_pause(data, FALSE);
1107 if(data->state.tempcount) {
1108 /* there are buffers for sending that can be delivered as the receive
1109 pausing is lifted! */
1111 unsigned int count = data->state.tempcount;
1112 struct tempbuf writebuf[3]; /* there can only be three */
1114 /* copy the structs to allow for immediate re-pausing */
1115 for(i = 0; i < data->state.tempcount; i++) {
1116 writebuf[i] = data->state.tempwrite[i];
1117 Curl_dyn_init(&data->state.tempwrite[i].b, DYN_PAUSE_BUFFER);
1119 data->state.tempcount = 0;
1121 for(i = 0; i < count; i++) {
1122 /* even if one function returns error, this loops through and frees
1125 result = Curl_client_write(data, writebuf[i].type,
1126 Curl_dyn_ptr(&writebuf[i].b),
1127 Curl_dyn_len(&writebuf[i].b));
1128 Curl_dyn_free(&writebuf[i].b);
1137 if(!(newstate & KEEP_SEND_PAUSE)) {
1138 /* need to wake the send body waker */
1139 if(data->hyp.send_body_waker) {
1140 hyper_waker_wake(data->hyp.send_body_waker);
1141 data->hyp.send_body_waker = NULL;
1146 /* if there's no error and we're not pausing both directions, we want
1147 to have this handle checked soon */
1148 if((newstate & (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) !=
1149 (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) {
1150 Curl_expire(data, 0, EXPIRE_RUN_NOW); /* get this handle going again */
1152 /* reset the too-slow time keeper */
1153 data->state.keeps_speed.tv_sec = 0;
1155 if(!data->state.tempcount)
1156 /* if not pausing again, force a recv/send check of this connection as
1157 the data might've been read off the socket already */
1158 data->conn->cselect_bits = CURL_CSELECT_IN | CURL_CSELECT_OUT;
1160 if(Curl_update_timer(data->multi))
1161 return CURLE_ABORTED_BY_CALLBACK;
1165 if(!data->state.done)
1166 /* This transfer may have been moved in or out of the bundle, update the
1167 corresponding socket callback, if used */
1168 result = Curl_updatesocket(data);
1174 static CURLcode easy_connection(struct Curl_easy *data, curl_socket_t *sfd,
1175 struct connectdata **connp)
1178 return CURLE_BAD_FUNCTION_ARGUMENT;
1180 /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
1181 if(!data->set.connect_only) {
1182 failf(data, "CONNECT_ONLY is required");
1183 return CURLE_UNSUPPORTED_PROTOCOL;
1186 *sfd = Curl_getconnectinfo(data, connp);
1188 if(*sfd == CURL_SOCKET_BAD) {
1189 failf(data, "Failed to get recent socket");
1190 return CURLE_UNSUPPORTED_PROTOCOL;
1197 * Receives data from the connected socket. Use after successful
1198 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1199 * Returns CURLE_OK on success, error code on error.
1201 CURLcode curl_easy_recv(struct Curl_easy *data, void *buffer, size_t buflen,
1207 struct connectdata *c;
1209 if(Curl_is_in_callback(data))
1210 return CURLE_RECURSIVE_API_CALL;
1212 result = easy_connection(data, &sfd, &c);
1217 /* on first invoke, the transfer has been detached from the connection and
1218 needs to be reattached */
1219 Curl_attach_connection(data, c);
1222 result = Curl_read(data, sfd, buffer, buflen, &n1);
1233 * Sends data over the connected socket.
1235 * This is the private internal version of curl_easy_send()
1237 CURLcode Curl_senddata(struct Curl_easy *data, const void *buffer,
1238 size_t buflen, ssize_t *n)
1243 struct connectdata *c = NULL;
1244 SIGPIPE_VARIABLE(pipe_st);
1246 result = easy_connection(data, &sfd, &c);
1251 /* on first invoke, the transfer has been detached from the connection and
1252 needs to be reattached */
1253 Curl_attach_connection(data, c);
1256 sigpipe_ignore(data, &pipe_st);
1257 result = Curl_write(data, sfd, buffer, buflen, &n1);
1258 sigpipe_restore(&pipe_st);
1261 return CURLE_SEND_ERROR;
1273 * Sends data over the connected socket. Use after successful
1274 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1276 CURLcode curl_easy_send(struct Curl_easy *data, const void *buffer,
1277 size_t buflen, size_t *n)
1279 ssize_t written = 0;
1281 if(Curl_is_in_callback(data))
1282 return CURLE_RECURSIVE_API_CALL;
1284 result = Curl_senddata(data, buffer, buflen, &written);
1285 *n = (size_t)written;
1290 * Wrapper to call functions in Curl_conncache_foreach()
1294 static int conn_upkeep(struct Curl_easy *data,
1295 struct connectdata *conn,
1298 /* Param is unused. */
1301 if(conn->handler->connection_check) {
1302 /* briefly attach the connection to this transfer for the purpose of
1304 Curl_attach_connection(data, conn);
1306 /* Do a protocol-specific keepalive check on the connection. */
1307 conn->handler->connection_check(data, conn, CONNCHECK_KEEPALIVE);
1308 /* detach the connection again */
1309 Curl_detach_connection(data);
1312 return 0; /* continue iteration */
1315 static CURLcode upkeep(struct conncache *conn_cache, void *data)
1317 /* Loop over every connection and make connection alive. */
1318 Curl_conncache_foreach(data,
1326 * Performs connection upkeep for the given session handle.
1328 CURLcode curl_easy_upkeep(struct Curl_easy *data)
1330 /* Verify that we got an easy handle we can work with. */
1331 if(!GOOD_EASY_HANDLE(data))
1332 return CURLE_BAD_FUNCTION_ARGUMENT;
1334 if(data->multi_easy) {
1335 /* Use the common function to keep connections alive. */
1336 return upkeep(&data->multi_easy->conn_cache, data);
1339 /* No connections, so just return success */