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 ***************************************************************************/
23 #include "curl_setup.h"
26 * See comment in curl_memory.h for the explanation of this sanity check.
29 #ifdef CURLX_NO_MEMORY_CALLBACKS
30 #error "libcurl shall not ever be built with CURLX_NO_MEMORY_CALLBACKS defined"
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
39 #ifdef HAVE_ARPA_INET_H
40 #include <arpa/inet.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
49 #ifdef HAVE_SYS_PARAM_H
50 #include <sys/param.h>
54 #include <curl/curl.h>
56 #include "vtls/vtls.h"
66 #include "sendf.h" /* for failf function prototype */
67 #include "connect.h" /* for Curl_getconnectinfo */
76 #include "http_digest.h"
77 #include "system_win32.h"
83 /* The last 3 #include files should be in this order */
84 #include "curl_printf.h"
85 #include "curl_memory.h"
88 /* true globals -- for curl_global_init() and curl_global_cleanup() */
89 static unsigned int initialized;
90 static long init_flags;
93 * strdup (and other memory functions) is redefined in complicated
94 * ways, but at this point it must be defined as the system-supplied strdup
95 * so the callback pointer is initialized correctly.
97 #if defined(_WIN32_WCE)
98 #define system_strdup _strdup
99 #elif !defined(HAVE_STRDUP)
100 #define system_strdup curlx_strdup
102 #define system_strdup strdup
105 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
106 # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
110 * If a memory-using function (like curl_getenv) is used before
111 * curl_global_init() is called, we need to have these pointers set already.
113 curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
114 curl_free_callback Curl_cfree = (curl_free_callback)free;
115 curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
116 curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
117 curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
118 #if defined(WIN32) && defined(UNICODE)
119 curl_wcsdup_callback Curl_cwcsdup = Curl_wcsdup;
122 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
123 # pragma warning(default:4232) /* MSVC extension, dllimport identity */
127 static char *leakpointer;
131 * curl_global_init() globally initializes curl given a bitwise set of the
132 * different features of what to initialize.
134 static CURLcode global_init(long flags, bool memoryfuncs)
140 /* Setup the default memory functions here (again) */
141 Curl_cmalloc = (curl_malloc_callback)malloc;
142 Curl_cfree = (curl_free_callback)free;
143 Curl_crealloc = (curl_realloc_callback)realloc;
144 Curl_cstrdup = (curl_strdup_callback)system_strdup;
145 Curl_ccalloc = (curl_calloc_callback)calloc;
146 #if defined(WIN32) && defined(UNICODE)
147 Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
151 if(!Curl_ssl_init()) {
152 DEBUGF(fprintf(stderr, "Error: Curl_ssl_init failed\n"));
157 if(Curl_win32_init(flags)) {
158 DEBUGF(fprintf(stderr, "Error: win32_init failed\n"));
164 if(!Curl_amiga_init()) {
165 DEBUGF(fprintf(stderr, "Error: Curl_amiga_init failed\n"));
170 if(Curl_resolver_global_init()) {
171 DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n"));
176 if(Curl_ssh_init()) {
182 if(WS_SUCCESS != wolfSSH_Init()) {
183 DEBUGF(fprintf(stderr, "Error: wolfSSH_Init failed\n"));
184 return CURLE_FAILED_INIT;
191 if(getenv("CURL_GLOBAL_INIT"))
192 /* alloc data that will leak if *cleanup() is not called! */
193 leakpointer = malloc(1);
199 initialized--; /* undo the increase */
200 return CURLE_FAILED_INIT;
205 * curl_global_init() globally initializes curl given a bitwise set of the
206 * different features of what to initialize.
208 CURLcode curl_global_init(long flags)
210 return global_init(flags, TRUE);
214 * curl_global_init_mem() globally initializes curl and also registers the
215 * user provided callback routines.
217 CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
218 curl_free_callback f, curl_realloc_callback r,
219 curl_strdup_callback s, curl_calloc_callback c)
221 /* Invalid input, return immediately */
222 if(!m || !f || !r || !s || !c)
223 return CURLE_FAILED_INIT;
226 /* Already initialized, don't do it again, but bump the variable anyway to
227 work like curl_global_init() and require the same amount of cleanup
233 /* set memory functions before global_init() in case it wants memory
241 /* Call the actual init function, but without setting */
242 return global_init(flags, FALSE);
246 * curl_global_cleanup() globally cleanups curl, uses the value of
247 * "init_flags" to determine what needs to be cleaned up and what doesn't.
249 void curl_global_cleanup(void)
258 Curl_resolver_global_cleanup();
261 Curl_win32_cleanup(init_flags);
264 Curl_amiga_cleanup();
269 (void)wolfSSH_Cleanup();
279 * curl_easy_init() is the external interface to alloc, setup and init an
280 * easy handle that is returned. If anything goes wrong, NULL is returned.
282 struct Curl_easy *curl_easy_init(void)
285 struct Curl_easy *data;
287 /* Make sure we inited the global SSL stuff */
289 result = curl_global_init(CURL_GLOBAL_DEFAULT);
291 /* something in the global init failed, return nothing */
292 DEBUGF(fprintf(stderr, "Error: curl_global_init failed\n"));
297 /* We use curl_open() with undefined URL so far */
298 result = Curl_open(&data);
300 DEBUGF(fprintf(stderr, "Error: Curl_open failed\n"));
309 struct socketmonitor {
310 struct socketmonitor *next; /* the next node in the list or NULL */
311 struct pollfd socket; /* socket info of what to monitor */
315 long ms; /* timeout, run the timeout function when reached */
316 bool msbump; /* set TRUE when timeout is set by callback */
317 int num_sockets; /* number of nodes in the monitor list */
318 struct socketmonitor *list; /* list of sockets to monitor */
319 int running_handles; /* store the returned number */
324 * Callback that gets called with a new value when the timeout should be
328 static int events_timer(struct Curl_multi *multi, /* multi handle */
329 long timeout_ms, /* see above */
330 void *userp) /* private callback pointer */
332 struct events *ev = userp;
335 /* timeout removed */
337 else if(timeout_ms == 0)
338 /* timeout is already reached! */
339 timeout_ms = 1; /* trigger asap */
349 * convert from poll() bit definitions to libcurl's CURL_CSELECT_* ones
351 static int poll2cselect(int pollmask)
354 if(pollmask & POLLIN)
355 omask |= CURL_CSELECT_IN;
356 if(pollmask & POLLOUT)
357 omask |= CURL_CSELECT_OUT;
358 if(pollmask & POLLERR)
359 omask |= CURL_CSELECT_ERR;
366 * convert from libcurl' CURL_POLL_* bit definitions to poll()'s
368 static short socketcb2poll(int pollmask)
371 if(pollmask & CURL_POLL_IN)
373 if(pollmask & CURL_POLL_OUT)
380 * Callback that gets called with information about socket activity to
383 static int events_socket(struct Curl_easy *easy, /* easy handle */
384 curl_socket_t s, /* socket */
385 int what, /* see above */
386 void *userp, /* private callback
388 void *socketp) /* private socket
391 struct events *ev = userp;
392 struct socketmonitor *m;
393 struct socketmonitor *prev = NULL;
395 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
402 if(m->socket.fd == s) {
404 if(what == CURL_POLL_REMOVE) {
405 struct socketmonitor *nxt = m->next;
406 /* remove this node from the list of monitored sockets */
413 infof(easy, "socket cb: socket %d REMOVED", s);
416 /* The socket 's' is already being monitored, update the activity
417 mask. Convert from libcurl bitmask to the poll one. */
418 m->socket.events = socketcb2poll(what);
419 infof(easy, "socket cb: socket %d UPDATED as %s%s", s,
420 (what&CURL_POLL_IN)?"IN":"",
421 (what&CURL_POLL_OUT)?"OUT":"");
426 m = m->next; /* move to next node */
429 if(what == CURL_POLL_REMOVE) {
430 /* this happens a bit too often, libcurl fix perhaps? */
432 "%s: socket %d asked to be REMOVED but not present!\n",
436 m = malloc(sizeof(struct socketmonitor));
440 m->socket.events = socketcb2poll(what);
441 m->socket.revents = 0;
443 infof(easy, "socket cb: socket %d ADDED as %s%s", s,
444 (what&CURL_POLL_IN)?"IN":"",
445 (what&CURL_POLL_OUT)?"OUT":"");
448 return CURLE_OUT_OF_MEMORY;
459 * Do the multi handle setups that only event-based transfers need.
461 static void events_setup(struct Curl_multi *multi, struct events *ev)
464 curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, events_timer);
465 curl_multi_setopt(multi, CURLMOPT_TIMERDATA, ev);
467 /* socket callback */
468 curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, events_socket);
469 curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev);
475 * waits for activity on any of the given sockets, or the timeout to trigger.
478 static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
481 CURLMcode mcode = CURLM_OK;
482 CURLcode result = CURLE_OK;
486 struct socketmonitor *m;
488 struct pollfd fds[4];
492 struct curltime before;
493 struct curltime after;
495 /* populate the fds[] array */
496 for(m = ev->list, f = &fds[0]; m; m = m->next) {
497 f->fd = m->socket.fd;
498 f->events = m->socket.events;
500 /* fprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd); */
505 /* get the time stamp to use to figure out how long poll takes */
508 /* wait for activity or timeout */
509 pollrc = Curl_poll(fds, numfds, ev->ms);
513 ev->msbump = FALSE; /* reset here */
518 /* fprintf(stderr, "call curl_multi_socket_action(TIMEOUT)\n"); */
519 mcode = curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0,
520 &ev->running_handles);
522 else if(pollrc > 0) {
523 /* loop over the monitored sockets to see which ones had activity */
524 for(i = 0; i< numfds; i++) {
526 /* socket activity, tell libcurl */
527 int act = poll2cselect(fds[i].revents); /* convert */
528 infof(multi->easyp, "call curl_multi_socket_action(socket %d)",
530 mcode = curl_multi_socket_action(multi, fds[i].fd, act,
531 &ev->running_handles);
536 /* If nothing updated the timeout, we decrease it by the spent time.
537 * If it was updated, it has the new timeout time stored already.
539 timediff_t timediff = Curl_timediff(after, before);
541 if(timediff > ev->ms)
544 ev->ms -= (long)timediff;
549 return CURLE_RECV_ERROR;
552 return CURLE_URL_MALFORMAT;
554 /* we don't really care about the "msgs_in_queue" value returned in the
556 msg = curl_multi_info_read(multi, &pollrc);
558 result = msg->data.result;
569 * Runs a transfer in a blocking manner using the events-based API
571 static CURLcode easy_events(struct Curl_multi *multi)
573 /* this struct is made static to allow it to be used after this function
574 returns and curl_multi_remove_handle() is called */
575 static struct events evs = {2, FALSE, 0, NULL, 0};
577 /* if running event-based, do some further multi inits */
578 events_setup(multi, &evs);
580 return wait_or_timeout(multi, &evs);
582 #else /* CURLDEBUG */
583 /* when not built with debug, this function doesn't exist */
584 #define easy_events(x) CURLE_NOT_BUILT_IN
587 static CURLcode easy_transfer(struct Curl_multi *multi)
590 CURLMcode mcode = CURLM_OK;
591 CURLcode result = CURLE_OK;
593 while(!done && !mcode) {
594 int still_running = 0;
596 mcode = curl_multi_poll(multi, NULL, 0, 1000, NULL);
599 mcode = curl_multi_perform(multi, &still_running);
601 /* only read 'still_running' if curl_multi_perform() return OK */
602 if(!mcode && !still_running) {
604 CURLMsg *msg = curl_multi_info_read(multi, &rc);
606 result = msg->data.result;
612 /* Make sure to return some kind of error if there was a multi problem */
614 result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
615 /* The other multi errors should never happen, so return
616 something suitably generic */
617 CURLE_BAD_FUNCTION_ARGUMENT;
625 * easy_perform() is the external interface that performs a blocking
626 * transfer as previously setup.
628 * CONCEPT: This function creates a multi handle, adds the easy handle to it,
629 * runs curl_multi_perform() until the transfer is done, then detaches the
630 * easy handle, destroys the multi handle and returns the easy handle's return
633 * REALITY: it can't just create and destroy the multi handle that easily. It
634 * needs to keep it around since if this easy handle is used again by this
635 * function, the same multi handle must be re-used so that the same pools and
636 * caches can be used.
638 * DEBUG: if 'events' is set TRUE, this function will use a replacement engine
639 * instead of curl_multi_perform() and use curl_multi_socket_action().
641 static CURLcode easy_perform(struct Curl_easy *data, bool events)
643 struct Curl_multi *multi;
645 CURLcode result = CURLE_OK;
646 SIGPIPE_VARIABLE(pipe_st);
649 return CURLE_BAD_FUNCTION_ARGUMENT;
651 if(data->set.errorbuffer)
652 /* clear this as early as possible */
653 data->set.errorbuffer[0] = 0;
656 failf(data, "easy handle already used in multi handle");
657 return CURLE_FAILED_INIT;
661 multi = data->multi_easy;
663 /* this multi handle will only ever have a single easy handled attached
664 to it, so make it use minimal hashes */
665 multi = Curl_multi_handle(1, 3);
667 return CURLE_OUT_OF_MEMORY;
668 data->multi_easy = multi;
671 if(multi->in_callback)
672 return CURLE_RECURSIVE_API_CALL;
674 /* Copy the MAXCONNECTS option to the multi handle */
675 curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, data->set.maxconnects);
677 mcode = curl_multi_add_handle(multi, data);
679 curl_multi_cleanup(multi);
680 data->multi_easy = NULL;
681 if(mcode == CURLM_OUT_OF_MEMORY)
682 return CURLE_OUT_OF_MEMORY;
683 return CURLE_FAILED_INIT;
686 sigpipe_ignore(data, &pipe_st);
688 /* run the transfer */
689 result = events ? easy_events(multi) : easy_transfer(multi);
691 /* ignoring the return code isn't nice, but atm we can't really handle
692 a failure here, room for future improvement! */
693 (void)curl_multi_remove_handle(multi, data);
695 sigpipe_restore(&pipe_st);
697 /* The multi handle is kept alive, owned by the easy handle */
703 * curl_easy_perform() is the external interface that performs a blocking
704 * transfer as previously setup.
706 CURLcode curl_easy_perform(struct Curl_easy *data)
708 return easy_perform(data, FALSE);
713 * curl_easy_perform_ev() is the external interface that performs a blocking
714 * transfer using the event-based API internally.
716 CURLcode curl_easy_perform_ev(struct Curl_easy *data)
718 return easy_perform(data, TRUE);
724 * curl_easy_cleanup() is the external interface to cleaning/freeing the given
727 void curl_easy_cleanup(struct Curl_easy *data)
729 SIGPIPE_VARIABLE(pipe_st);
734 sigpipe_ignore(data, &pipe_st);
736 sigpipe_restore(&pipe_st);
740 * curl_easy_getinfo() is an external interface that allows an app to retrieve
741 * information from a performed transfer and similar.
743 #undef curl_easy_getinfo
744 CURLcode curl_easy_getinfo(struct Curl_easy *data, CURLINFO info, ...)
751 paramp = va_arg(arg, void *);
753 result = Curl_getinfo(data, info, paramp);
759 static CURLcode dupset(struct Curl_easy *dst, struct Curl_easy *src)
761 CURLcode result = CURLE_OK;
765 /* Copy src->set into dst->set first, then deal with the strings
768 Curl_mime_initpart(&dst->set.mimepost, dst);
770 /* clear all string pointers first */
771 memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
773 /* duplicate all strings */
774 for(i = (enum dupstring)0; i< STRING_LASTZEROTERMINATED; i++) {
775 result = Curl_setstropt(&dst->set.str[i], src->set.str[i]);
780 /* clear all blob pointers first */
781 memset(dst->set.blobs, 0, BLOB_LAST * sizeof(struct curl_blob *));
782 /* duplicate all blobs */
783 for(j = (enum dupblob)0; j < BLOB_LAST; j++) {
784 result = Curl_setblobopt(&dst->set.blobs[j], src->set.blobs[j]);
789 /* duplicate memory areas pointed to */
790 i = STRING_COPYPOSTFIELDS;
791 if(src->set.postfieldsize && src->set.str[i]) {
792 /* postfieldsize is curl_off_t, Curl_memdup() takes a size_t ... */
793 dst->set.str[i] = Curl_memdup(src->set.str[i],
794 curlx_sotouz(src->set.postfieldsize));
796 return CURLE_OUT_OF_MEMORY;
797 /* point to the new copy */
798 dst->set.postfields = dst->set.str[i];
801 /* Duplicate mime data. */
802 result = Curl_mime_duppart(&dst->set.mimepost, &src->set.mimepost);
805 dst->state.resolve = dst->set.resolve;
811 * curl_easy_duphandle() is an external interface to allow duplication of a
812 * given input easy handle. The returned handle will be a new working handle
813 * with all options set exactly as the input source handle.
815 struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
817 struct Curl_easy *outcurl = calloc(1, sizeof(struct Curl_easy));
822 * We setup a few buffers we need. We should probably make them
823 * get setup on-demand in the code, as that would probably decrease
824 * the likeliness of us forgetting to init a buffer here in the future.
826 outcurl->set.buffer_size = data->set.buffer_size;
828 /* copy all userdefined values */
829 if(dupset(outcurl, data))
832 Curl_dyn_init(&outcurl->state.headerb, CURL_MAX_HTTP_HEADER);
834 /* the connection cache is setup on demand */
835 outcurl->state.conn_cache = NULL;
836 outcurl->state.lastconnect_id = -1;
838 outcurl->progress.flags = data->progress.flags;
839 outcurl->progress.callback = data->progress.callback;
842 /* If cookies are enabled in the parent handle, we enable them
843 in the clone as well! */
844 outcurl->cookies = Curl_cookie_init(data,
845 data->cookies->filename,
847 data->set.cookiesession);
848 if(!outcurl->cookies)
852 /* duplicate all values in 'change' */
853 if(data->state.cookielist) {
854 outcurl->state.cookielist =
855 Curl_slist_duplicate(data->state.cookielist);
856 if(!outcurl->state.cookielist)
860 if(data->state.url) {
861 outcurl->state.url = strdup(data->state.url);
862 if(!outcurl->state.url)
864 outcurl->state.url_alloc = TRUE;
867 if(data->state.referer) {
868 outcurl->state.referer = strdup(data->state.referer);
869 if(!outcurl->state.referer)
871 outcurl->state.referer_alloc = TRUE;
874 /* Reinitialize an SSL engine for the new handle
875 * note: the engine name has already been copied by dupset */
876 if(outcurl->set.str[STRING_SSL_ENGINE]) {
877 if(Curl_ssl_set_engine(outcurl, outcurl->set.str[STRING_SSL_ENGINE]))
883 outcurl->asi = Curl_altsvc_init();
886 if(outcurl->set.str[STRING_ALTSVC])
887 (void)Curl_altsvc_load(outcurl->asi, outcurl->set.str[STRING_ALTSVC]);
890 #ifndef CURL_DISABLE_HSTS
892 outcurl->hsts = Curl_hsts_init();
895 if(outcurl->set.str[STRING_HSTS])
896 (void)Curl_hsts_loadfile(outcurl,
897 outcurl->hsts, outcurl->set.str[STRING_HSTS]);
898 (void)Curl_hsts_loadcb(outcurl, outcurl->hsts);
901 /* Clone the resolver handle, if present, for the new handle */
902 if(Curl_resolver_duphandle(outcurl,
903 &outcurl->state.async.resolver,
904 data->state.async.resolver))
911 rc = Curl_set_dns_servers(outcurl, data->set.str[STRING_DNS_SERVERS]);
912 if(rc && rc != CURLE_NOT_BUILT_IN)
915 rc = Curl_set_dns_interface(outcurl, data->set.str[STRING_DNS_INTERFACE]);
916 if(rc && rc != CURLE_NOT_BUILT_IN)
919 rc = Curl_set_dns_local_ip4(outcurl, data->set.str[STRING_DNS_LOCAL_IP4]);
920 if(rc && rc != CURLE_NOT_BUILT_IN)
923 rc = Curl_set_dns_local_ip6(outcurl, data->set.str[STRING_DNS_LOCAL_IP6]);
924 if(rc && rc != CURLE_NOT_BUILT_IN)
927 #endif /* USE_ARES */
929 Curl_initinfo(outcurl);
931 outcurl->magic = CURLEASY_MAGIC_NUMBER;
933 /* we reach this point and thus we are OK */
940 curl_slist_free_all(outcurl->state.cookielist);
941 outcurl->state.cookielist = NULL;
942 Curl_safefree(outcurl->state.buffer);
943 Curl_dyn_free(&outcurl->state.headerb);
944 Curl_safefree(outcurl->state.url);
945 Curl_safefree(outcurl->state.referer);
946 Curl_altsvc_cleanup(&outcurl->asi);
947 Curl_hsts_cleanup(&outcurl->hsts);
948 Curl_freeset(outcurl);
956 * curl_easy_reset() is an external interface that allows an app to re-
957 * initialize a session handle to the default values.
959 void curl_easy_reset(struct Curl_easy *data)
961 Curl_free_request_state(data);
963 /* zero out UserDefined data: */
965 memset(&data->set, 0, sizeof(struct UserDefined));
966 (void)Curl_init_userdefined(data);
968 /* zero out Progress data: */
969 memset(&data->progress, 0, sizeof(struct Progress));
971 /* zero out PureInfo data: */
974 data->progress.flags |= PGRS_HIDE;
975 data->state.current_speed = -1; /* init to negative == impossible */
976 data->state.retrycount = 0; /* reset the retry counter */
978 /* zero out authentication data: */
979 memset(&data->state.authhost, 0, sizeof(struct auth));
980 memset(&data->state.authproxy, 0, sizeof(struct auth));
982 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
983 Curl_http_auth_cleanup_digest(data);
988 * curl_easy_pause() allows an application to pause or unpause a specific
989 * transfer and direction. This function sets the full new state for the
990 * current connection this easy handle operates on.
992 * NOTE: if you have the receiving paused and you call this function to remove
993 * the pausing, you may get your write callback called at this point.
995 * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
997 * NOTE: This is one of few API functions that are allowed to be called from
1000 CURLcode curl_easy_pause(struct Curl_easy *data, int action)
1002 struct SingleRequest *k;
1003 CURLcode result = CURLE_OK;
1007 if(!GOOD_EASY_HANDLE(data) || !data->conn)
1008 /* crazy input, don't continue */
1009 return CURLE_BAD_FUNCTION_ARGUMENT;
1012 oldstate = k->keepon & (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
1014 /* first switch off both pause bits then set the new pause bits */
1015 newstate = (k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE)) |
1016 ((action & CURLPAUSE_RECV)?KEEP_RECV_PAUSE:0) |
1017 ((action & CURLPAUSE_SEND)?KEEP_SEND_PAUSE:0);
1019 if((newstate & (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE)) == oldstate) {
1020 /* Not changing any pause state, return */
1021 DEBUGF(infof(data, "pause: no change, early return"));
1025 /* Unpause parts in active mime tree. */
1026 if((k->keepon & ~newstate & KEEP_SEND_PAUSE) &&
1027 (data->mstate == MSTATE_PERFORMING ||
1028 data->mstate == MSTATE_RATELIMITING) &&
1029 data->state.fread_func == (curl_read_callback) Curl_mime_read) {
1030 Curl_mime_unpause(data->state.in);
1033 /* put it back in the keepon */
1034 k->keepon = newstate;
1036 if(!(newstate & KEEP_RECV_PAUSE)) {
1037 Curl_http2_stream_pause(data, FALSE);
1039 if(data->state.tempcount) {
1040 /* there are buffers for sending that can be delivered as the receive
1041 pausing is lifted! */
1043 unsigned int count = data->state.tempcount;
1044 struct tempbuf writebuf[3]; /* there can only be three */
1046 /* copy the structs to allow for immediate re-pausing */
1047 for(i = 0; i < data->state.tempcount; i++) {
1048 writebuf[i] = data->state.tempwrite[i];
1049 Curl_dyn_init(&data->state.tempwrite[i].b, DYN_PAUSE_BUFFER);
1051 data->state.tempcount = 0;
1053 for(i = 0; i < count; i++) {
1054 /* even if one function returns error, this loops through and frees
1057 result = Curl_client_write(data, writebuf[i].type,
1058 Curl_dyn_ptr(&writebuf[i].b),
1059 Curl_dyn_len(&writebuf[i].b));
1060 Curl_dyn_free(&writebuf[i].b);
1068 /* if there's no error and we're not pausing both directions, we want
1069 to have this handle checked soon */
1070 if((newstate & (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) !=
1071 (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) {
1072 Curl_expire(data, 0, EXPIRE_RUN_NOW); /* get this handle going again */
1074 /* reset the too-slow time keeper */
1075 data->state.keeps_speed.tv_sec = 0;
1077 if(!data->state.tempcount)
1078 /* if not pausing again, force a recv/send check of this connection as
1079 the data might've been read off the socket already */
1080 data->conn->cselect_bits = CURL_CSELECT_IN | CURL_CSELECT_OUT;
1082 if(Curl_update_timer(data->multi))
1083 return CURLE_ABORTED_BY_CALLBACK;
1087 if(!data->state.done)
1088 /* This transfer may have been moved in or out of the bundle, update the
1089 corresponding socket callback, if used */
1090 result = Curl_updatesocket(data);
1096 static CURLcode easy_connection(struct Curl_easy *data,
1098 struct connectdata **connp)
1101 return CURLE_BAD_FUNCTION_ARGUMENT;
1103 /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
1104 if(!data->set.connect_only) {
1105 failf(data, "CONNECT_ONLY is required");
1106 return CURLE_UNSUPPORTED_PROTOCOL;
1109 *sfd = Curl_getconnectinfo(data, connp);
1111 if(*sfd == CURL_SOCKET_BAD) {
1112 failf(data, "Failed to get recent socket");
1113 return CURLE_UNSUPPORTED_PROTOCOL;
1120 * Receives data from the connected socket. Use after successful
1121 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1122 * Returns CURLE_OK on success, error code on error.
1124 CURLcode curl_easy_recv(struct Curl_easy *data, void *buffer, size_t buflen,
1130 struct connectdata *c;
1132 if(Curl_is_in_callback(data))
1133 return CURLE_RECURSIVE_API_CALL;
1135 result = easy_connection(data, &sfd, &c);
1140 /* on first invoke, the transfer has been detached from the connection and
1141 needs to be reattached */
1142 Curl_attach_connection(data, c);
1145 result = Curl_read(data, sfd, buffer, buflen, &n1);
1156 * Sends data over the connected socket. Use after successful
1157 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1159 CURLcode curl_easy_send(struct Curl_easy *data, const void *buffer,
1160 size_t buflen, size_t *n)
1165 struct connectdata *c = NULL;
1166 SIGPIPE_VARIABLE(pipe_st);
1168 if(Curl_is_in_callback(data))
1169 return CURLE_RECURSIVE_API_CALL;
1171 result = easy_connection(data, &sfd, &c);
1176 /* on first invoke, the transfer has been detached from the connection and
1177 needs to be reattached */
1178 Curl_attach_connection(data, c);
1181 sigpipe_ignore(data, &pipe_st);
1182 result = Curl_write(data, sfd, buffer, buflen, &n1);
1183 sigpipe_restore(&pipe_st);
1186 return CURLE_SEND_ERROR;
1198 * Wrapper to call functions in Curl_conncache_foreach()
1202 static int conn_upkeep(struct Curl_easy *data,
1203 struct connectdata *conn,
1206 /* Param is unused. */
1209 if(conn->handler->connection_check) {
1210 /* briefly attach the connection to this transfer for the purpose of
1212 Curl_attach_connection(data, conn);
1214 /* Do a protocol-specific keepalive check on the connection. */
1215 conn->handler->connection_check(data, conn, CONNCHECK_KEEPALIVE);
1216 /* detach the connection again */
1217 Curl_detach_connection(data);
1220 return 0; /* continue iteration */
1223 static CURLcode upkeep(struct conncache *conn_cache, void *data)
1225 /* Loop over every connection and make connection alive. */
1226 Curl_conncache_foreach(data,
1234 * Performs connection upkeep for the given session handle.
1236 CURLcode curl_easy_upkeep(struct Curl_easy *data)
1238 /* Verify that we got an easy handle we can work with. */
1239 if(!GOOD_EASY_HANDLE(data))
1240 return CURLE_BAD_FUNCTION_ARGUMENT;
1242 if(data->multi_easy) {
1243 /* Use the common function to keep connections alive. */
1244 return upkeep(&data->multi_easy->conn_cache, data);
1247 /* No connections, so just return success */