1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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.haxx.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"
65 #include "sendf.h" /* for failf function prototype */
66 #include "connect.h" /* for Curl_getconnectinfo */
69 #include "non-ascii.h"
71 #include "conncache.h"
75 /* The last 3 #include files should be in this order */
76 #include "curl_printf.h"
77 #include "curl_memory.h"
80 void Curl_version_init(void);
82 /* win32_cleanup() is for win32 socket cleanup functionality, the opposite
84 static void win32_cleanup(void)
89 #ifdef USE_WINDOWS_SSPI
90 Curl_sspi_global_cleanup();
94 /* win32_init() performs win32 socket initialization to properly setup the
95 stack to allow networking */
96 static CURLcode win32_init(void)
99 WORD wVersionRequested;
103 #if defined(ENABLE_IPV6) && (USE_WINSOCK < 2)
104 Error IPV6_requires_winsock2
107 wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
109 res = WSAStartup(wVersionRequested, &wsaData);
112 /* Tell the user that we couldn't find a useable */
114 return CURLE_FAILED_INIT;
116 /* Confirm that the Windows Sockets DLL supports what we need.*/
117 /* Note that if the DLL supports versions greater */
118 /* than wVersionRequested, it will still return */
119 /* wVersionRequested in wVersion. wHighVersion contains the */
120 /* highest supported version. */
122 if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
123 HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) {
124 /* Tell the user that we couldn't find a useable */
128 return CURLE_FAILED_INIT;
130 /* The Windows Sockets DLL is acceptable. Proceed. */
131 #elif defined(USE_LWIPSOCK)
135 #ifdef USE_WINDOWS_SSPI
137 CURLcode result = Curl_sspi_global_init();
146 /* true globals -- for curl_global_init() and curl_global_cleanup() */
147 static unsigned int initialized;
148 static long init_flags;
151 * strdup (and other memory functions) is redefined in complicated
152 * ways, but at this point it must be defined as the system-supplied strdup
153 * so the callback pointer is initialized correctly.
155 #if defined(_WIN32_WCE)
156 #define system_strdup _strdup
157 #elif !defined(HAVE_STRDUP)
158 #define system_strdup curlx_strdup
160 #define system_strdup strdup
163 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
164 # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
167 #ifndef __SYMBIAN32__
169 * If a memory-using function (like curl_getenv) is used before
170 * curl_global_init() is called, we need to have these pointers set already.
172 curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
173 curl_free_callback Curl_cfree = (curl_free_callback)free;
174 curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
175 curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
176 curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
177 #if defined(WIN32) && defined(UNICODE)
178 curl_wcsdup_callback Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
182 * Symbian OS doesn't support initialization to code in writable static data.
183 * Initialization will occur in the curl_global_init() call.
185 curl_malloc_callback Curl_cmalloc;
186 curl_free_callback Curl_cfree;
187 curl_realloc_callback Curl_crealloc;
188 curl_strdup_callback Curl_cstrdup;
189 curl_calloc_callback Curl_ccalloc;
192 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
193 # pragma warning(default:4232) /* MSVC extension, dllimport identity */
197 * curl_global_init() globally initializes curl given a bitwise set of the
198 * different features of what to initialize.
200 static CURLcode global_init(long flags, bool memoryfuncs)
206 /* Setup the default memory functions here (again) */
207 Curl_cmalloc = (curl_malloc_callback)malloc;
208 Curl_cfree = (curl_free_callback)free;
209 Curl_crealloc = (curl_realloc_callback)realloc;
210 Curl_cstrdup = (curl_strdup_callback)system_strdup;
211 Curl_ccalloc = (curl_calloc_callback)calloc;
212 #if defined(WIN32) && defined(UNICODE)
213 Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
217 if(flags & CURL_GLOBAL_SSL)
218 if(!Curl_ssl_init()) {
219 DEBUGF(fprintf(stderr, "Error: Curl_ssl_init failed\n"));
220 return CURLE_FAILED_INIT;
223 if(flags & CURL_GLOBAL_WIN32)
225 DEBUGF(fprintf(stderr, "Error: win32_init failed\n"));
226 return CURLE_FAILED_INIT;
230 if(!Curl_amiga_init()) {
231 DEBUGF(fprintf(stderr, "Error: Curl_amiga_init failed\n"));
232 return CURLE_FAILED_INIT;
238 DEBUGF(fprintf(stderr, "Warning: LONG namespace not available\n"));
242 if(Curl_resolver_global_init()) {
243 DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n"));
244 return CURLE_FAILED_INIT;
247 (void)Curl_ipv6works();
249 #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_INIT)
250 if(libssh2_init(0)) {
251 DEBUGF(fprintf(stderr, "Error: libssh2_init failed\n"));
252 return CURLE_FAILED_INIT;
256 if(flags & CURL_GLOBAL_ACK_EINTR)
268 * curl_global_init() globally initializes curl given a bitwise set of the
269 * different features of what to initialize.
271 CURLcode curl_global_init(long flags)
273 return global_init(flags, TRUE);
277 * curl_global_init_mem() globally initializes curl and also registers the
278 * user provided callback routines.
280 CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
281 curl_free_callback f, curl_realloc_callback r,
282 curl_strdup_callback s, curl_calloc_callback c)
284 /* Invalid input, return immediately */
285 if(!m || !f || !r || !s || !c)
286 return CURLE_FAILED_INIT;
289 /* Already initialized, don't do it again, but bump the variable anyway to
290 work like curl_global_init() and require the same amount of cleanup
296 /* set memory functions before global_init() in case it wants memory
304 /* Call the actual init function, but without setting */
305 return global_init(flags, FALSE);
309 * curl_global_cleanup() globally cleanups curl, uses the value of
310 * "init_flags" to determine what needs to be cleaned up and what doesn't.
312 void curl_global_cleanup(void)
320 Curl_global_host_cache_dtor();
322 if(init_flags & CURL_GLOBAL_SSL)
325 Curl_resolver_global_cleanup();
327 if(init_flags & CURL_GLOBAL_WIN32)
330 Curl_amiga_cleanup();
332 #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_EXIT)
333 (void)libssh2_exit();
340 * curl_easy_init() is the external interface to alloc, setup and init an
341 * easy handle that is returned. If anything goes wrong, NULL is returned.
343 struct Curl_easy *curl_easy_init(void)
346 struct Curl_easy *data;
348 /* Make sure we inited the global SSL stuff */
350 result = curl_global_init(CURL_GLOBAL_DEFAULT);
352 /* something in the global init failed, return nothing */
353 DEBUGF(fprintf(stderr, "Error: curl_global_init failed\n"));
358 /* We use curl_open() with undefined URL so far */
359 result = Curl_open(&data);
361 DEBUGF(fprintf(stderr, "Error: Curl_open failed\n"));
369 * curl_easy_setopt() is the external interface for setting options on an
373 #undef curl_easy_setopt
374 CURLcode curl_easy_setopt(struct Curl_easy *data, CURLoption tag, ...)
380 return CURLE_BAD_FUNCTION_ARGUMENT;
384 result = Curl_setopt(data, tag, arg);
392 struct socketmonitor {
393 struct socketmonitor *next; /* the next node in the list or NULL */
394 struct pollfd socket; /* socket info of what to monitor */
398 long ms; /* timeout, run the timeout function when reached */
399 bool msbump; /* set TRUE when timeout is set by callback */
400 int num_sockets; /* number of nodes in the monitor list */
401 struct socketmonitor *list; /* list of sockets to monitor */
402 int running_handles; /* store the returned number */
407 * Callback that gets called with a new value when the timeout should be
411 static int events_timer(struct Curl_multi *multi, /* multi handle */
412 long timeout_ms, /* see above */
413 void *userp) /* private callback pointer */
415 struct events *ev = userp;
418 /* timeout removed */
420 else if(timeout_ms == 0)
421 /* timeout is already reached! */
422 timeout_ms = 1; /* trigger asap */
432 * convert from poll() bit definitions to libcurl's CURL_CSELECT_* ones
434 static int poll2cselect(int pollmask)
437 if(pollmask & POLLIN)
438 omask |= CURL_CSELECT_IN;
439 if(pollmask & POLLOUT)
440 omask |= CURL_CSELECT_OUT;
441 if(pollmask & POLLERR)
442 omask |= CURL_CSELECT_ERR;
449 * convert from libcurl' CURL_POLL_* bit definitions to poll()'s
451 static short socketcb2poll(int pollmask)
454 if(pollmask & CURL_POLL_IN)
456 if(pollmask & CURL_POLL_OUT)
463 * Callback that gets called with information about socket activity to
466 static int events_socket(struct Curl_easy *easy, /* easy handle */
467 curl_socket_t s, /* socket */
468 int what, /* see above */
469 void *userp, /* private callback
471 void *socketp) /* private socket
474 struct events *ev = userp;
475 struct socketmonitor *m;
476 struct socketmonitor *prev = NULL;
478 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
485 if(m->socket.fd == s) {
487 if(what == CURL_POLL_REMOVE) {
488 struct socketmonitor *nxt = m->next;
489 /* remove this node from the list of monitored sockets */
496 infof(easy, "socket cb: socket %d REMOVED\n", s);
499 /* The socket 's' is already being monitored, update the activity
500 mask. Convert from libcurl bitmask to the poll one. */
501 m->socket.events = socketcb2poll(what);
502 infof(easy, "socket cb: socket %d UPDATED as %s%s\n", s,
503 what&CURL_POLL_IN?"IN":"",
504 what&CURL_POLL_OUT?"OUT":"");
509 m = m->next; /* move to next node */
512 if(what == CURL_POLL_REMOVE) {
513 /* this happens a bit too often, libcurl fix perhaps? */
515 "%s: socket %d asked to be REMOVED but not present!\n",
519 m = malloc(sizeof(struct socketmonitor));
523 m->socket.events = socketcb2poll(what);
524 m->socket.revents = 0;
526 infof(easy, "socket cb: socket %d ADDED as %s%s\n", s,
527 what&CURL_POLL_IN?"IN":"",
528 what&CURL_POLL_OUT?"OUT":"");
531 return CURLE_OUT_OF_MEMORY;
542 * Do the multi handle setups that only event-based transfers need.
544 static void events_setup(struct Curl_multi *multi, struct events *ev)
547 curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, events_timer);
548 curl_multi_setopt(multi, CURLMOPT_TIMERDATA, ev);
550 /* socket callback */
551 curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, events_socket);
552 curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev);
558 * waits for activity on any of the given sockets, or the timeout to trigger.
561 static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
564 CURLMcode mcode = CURLM_OK;
565 CURLcode result = CURLE_OK;
569 struct socketmonitor *m;
571 struct pollfd fds[4];
575 struct curltime before;
576 struct curltime after;
578 /* populate the fds[] array */
579 for(m = ev->list, f = &fds[0]; m; m = m->next) {
580 f->fd = m->socket.fd;
581 f->events = m->socket.events;
583 /* fprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd); */
588 /* get the time stamp to use to figure out how long poll takes */
589 before = curlx_tvnow();
591 /* wait for activity or timeout */
592 pollrc = Curl_poll(fds, numfds, (int)ev->ms);
594 after = curlx_tvnow();
596 ev->msbump = FALSE; /* reset here */
601 /* fprintf(stderr, "call curl_multi_socket_action(TIMEOUT)\n"); */
602 mcode = curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0,
603 &ev->running_handles);
605 else if(pollrc > 0) {
606 /* loop over the monitored sockets to see which ones had activity */
607 for(i = 0; i< numfds; i++) {
609 /* socket activity, tell libcurl */
610 int act = poll2cselect(fds[i].revents); /* convert */
611 infof(multi->easyp, "call curl_multi_socket_action(socket %d)\n",
613 mcode = curl_multi_socket_action(multi, fds[i].fd, act,
614 &ev->running_handles);
619 /* If nothing updated the timeout, we decrease it by the spent time.
620 * If it was updated, it has the new timeout time stored already.
622 time_t timediff = curlx_tvdiff(after, before);
624 if(timediff > ev->ms)
627 ev->ms -= (long)timediff;
632 return CURLE_RECV_ERROR;
635 return CURLE_URL_MALFORMAT; /* TODO: return a proper error! */
637 /* we don't really care about the "msgs_in_queue" value returned in the
639 msg = curl_multi_info_read(multi, &pollrc);
641 result = msg->data.result;
652 * Runs a transfer in a blocking manner using the events-based API
654 static CURLcode easy_events(struct Curl_multi *multi)
656 /* this struct is made static to allow it to be used after this function
657 returns and curl_multi_remove_handle() is called */
658 static struct events evs = {2, FALSE, 0, NULL, 0};
660 /* if running event-based, do some further multi inits */
661 events_setup(multi, &evs);
663 return wait_or_timeout(multi, &evs);
665 #else /* CURLDEBUG */
666 /* when not built with debug, this function doesn't exist */
667 #define easy_events(x) CURLE_NOT_BUILT_IN
670 static CURLcode easy_transfer(struct Curl_multi *multi)
673 CURLMcode mcode = CURLM_OK;
674 CURLcode result = CURLE_OK;
675 struct curltime before;
676 int without_fds = 0; /* count number of consecutive returns from
677 curl_multi_wait() without any filedescriptors */
679 while(!done && !mcode) {
680 int still_running = 0;
683 before = curlx_tvnow();
684 mcode = curl_multi_wait(multi, NULL, 0, 1000, &rc);
688 struct curltime after = curlx_tvnow();
690 /* If it returns without any filedescriptor instantly, we need to
691 avoid busy-looping during periods where it has nothing particular
693 if(curlx_tvdiff(after, before) <= 10) {
695 if(without_fds > 2) {
696 int sleep_ms = without_fds < 10 ? (1 << (without_fds - 1)) : 1000;
697 Curl_wait_ms(sleep_ms);
701 /* it wasn't "instant", restart counter */
705 /* got file descriptor, restart counter */
708 mcode = curl_multi_perform(multi, &still_running);
711 /* only read 'still_running' if curl_multi_perform() return OK */
712 if(!mcode && !still_running) {
713 CURLMsg *msg = curl_multi_info_read(multi, &rc);
715 result = msg->data.result;
721 /* Make sure to return some kind of error if there was a multi problem */
723 result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
724 /* The other multi errors should never happen, so return
725 something suitably generic */
726 CURLE_BAD_FUNCTION_ARGUMENT;
734 * easy_perform() is the external interface that performs a blocking
735 * transfer as previously setup.
737 * CONCEPT: This function creates a multi handle, adds the easy handle to it,
738 * runs curl_multi_perform() until the transfer is done, then detaches the
739 * easy handle, destroys the multi handle and returns the easy handle's return
742 * REALITY: it can't just create and destroy the multi handle that easily. It
743 * needs to keep it around since if this easy handle is used again by this
744 * function, the same multi handle must be re-used so that the same pools and
745 * caches can be used.
747 * DEBUG: if 'events' is set TRUE, this function will use a replacement engine
748 * instead of curl_multi_perform() and use curl_multi_socket_action().
750 static CURLcode easy_perform(struct Curl_easy *data, bool events)
752 struct Curl_multi *multi;
754 CURLcode result = CURLE_OK;
755 SIGPIPE_VARIABLE(pipe_st);
758 return CURLE_BAD_FUNCTION_ARGUMENT;
761 failf(data, "easy handle already used in multi handle");
762 return CURLE_FAILED_INIT;
766 multi = data->multi_easy;
768 /* this multi handle will only ever have a single easy handled attached
769 to it, so make it use minimal hashes */
770 multi = Curl_multi_handle(1, 3);
772 return CURLE_OUT_OF_MEMORY;
773 data->multi_easy = multi;
776 /* Copy the MAXCONNECTS option to the multi handle */
777 curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, data->set.maxconnects);
779 mcode = curl_multi_add_handle(multi, data);
781 curl_multi_cleanup(multi);
782 if(mcode == CURLM_OUT_OF_MEMORY)
783 return CURLE_OUT_OF_MEMORY;
784 return CURLE_FAILED_INIT;
787 sigpipe_ignore(data, &pipe_st);
789 /* assign this after curl_multi_add_handle() since that function checks for
790 it and rejects this handle otherwise */
793 /* run the transfer */
794 result = events ? easy_events(multi) : easy_transfer(multi);
796 /* ignoring the return code isn't nice, but atm we can't really handle
797 a failure here, room for future improvement! */
798 (void)curl_multi_remove_handle(multi, data);
800 sigpipe_restore(&pipe_st);
802 /* The multi handle is kept alive, owned by the easy handle */
808 * curl_easy_perform() is the external interface that performs a blocking
809 * transfer as previously setup.
811 CURLcode curl_easy_perform(struct Curl_easy *data)
813 return easy_perform(data, FALSE);
818 * curl_easy_perform_ev() is the external interface that performs a blocking
819 * transfer using the event-based API internally.
821 CURLcode curl_easy_perform_ev(struct Curl_easy *data)
823 return easy_perform(data, TRUE);
829 * curl_easy_cleanup() is the external interface to cleaning/freeing the given
832 void curl_easy_cleanup(struct Curl_easy *data)
834 SIGPIPE_VARIABLE(pipe_st);
839 sigpipe_ignore(data, &pipe_st);
841 sigpipe_restore(&pipe_st);
845 * curl_easy_getinfo() is an external interface that allows an app to retrieve
846 * information from a performed transfer and similar.
848 #undef curl_easy_getinfo
849 CURLcode curl_easy_getinfo(struct Curl_easy *data, CURLINFO info, ...)
856 paramp = va_arg(arg, void *);
858 result = Curl_getinfo(data, info, paramp);
865 * curl_easy_duphandle() is an external interface to allow duplication of a
866 * given input easy handle. The returned handle will be a new working handle
867 * with all options set exactly as the input source handle.
869 struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
871 struct Curl_easy *outcurl = calloc(1, sizeof(struct Curl_easy));
876 * We setup a few buffers we need. We should probably make them
877 * get setup on-demand in the code, as that would probably decrease
878 * the likeliness of us forgetting to init a buffer here in the future.
880 outcurl->set.buffer_size = data->set.buffer_size;
881 outcurl->state.buffer = malloc(outcurl->set.buffer_size + 1);
882 if(!outcurl->state.buffer)
885 outcurl->state.headerbuff = malloc(HEADERSIZE);
886 if(!outcurl->state.headerbuff)
888 outcurl->state.headersize = HEADERSIZE;
890 /* copy all userdefined values */
891 if(Curl_dupset(outcurl, data))
894 /* the connection cache is setup on demand */
895 outcurl->state.conn_cache = NULL;
897 outcurl->state.lastconnect = NULL;
899 outcurl->progress.flags = data->progress.flags;
900 outcurl->progress.callback = data->progress.callback;
903 /* If cookies are enabled in the parent handle, we enable them
904 in the clone as well! */
905 outcurl->cookies = Curl_cookie_init(data,
906 data->cookies->filename,
908 data->set.cookiesession);
909 if(!outcurl->cookies)
913 /* duplicate all values in 'change' */
914 if(data->change.cookielist) {
915 outcurl->change.cookielist =
916 Curl_slist_duplicate(data->change.cookielist);
917 if(!outcurl->change.cookielist)
921 if(data->change.url) {
922 outcurl->change.url = strdup(data->change.url);
923 if(!outcurl->change.url)
925 outcurl->change.url_alloc = TRUE;
928 if(data->change.referer) {
929 outcurl->change.referer = strdup(data->change.referer);
930 if(!outcurl->change.referer)
932 outcurl->change.referer_alloc = TRUE;
935 /* Clone the resolver handle, if present, for the new handle */
936 if(Curl_resolver_duphandle(&outcurl->state.resolver,
937 data->state.resolver))
940 Curl_convert_setup(outcurl);
942 Curl_initinfo(outcurl);
944 outcurl->magic = CURLEASY_MAGIC_NUMBER;
946 /* we reach this point and thus we are OK */
953 curl_slist_free_all(outcurl->change.cookielist);
954 outcurl->change.cookielist = NULL;
955 Curl_safefree(outcurl->state.buffer);
956 Curl_safefree(outcurl->state.headerbuff);
957 Curl_safefree(outcurl->change.url);
958 Curl_safefree(outcurl->change.referer);
959 Curl_freeset(outcurl);
967 * curl_easy_reset() is an external interface that allows an app to re-
968 * initialize a session handle to the default values.
970 void curl_easy_reset(struct Curl_easy *data)
972 Curl_safefree(data->state.pathbuffer);
974 data->state.path = NULL;
976 Curl_free_request_state(data);
978 /* zero out UserDefined data: */
980 memset(&data->set, 0, sizeof(struct UserDefined));
981 (void)Curl_init_userdefined(&data->set);
983 /* zero out Progress data: */
984 memset(&data->progress, 0, sizeof(struct Progress));
986 /* zero out PureInfo data: */
989 data->progress.flags |= PGRS_HIDE;
990 data->state.current_speed = -1; /* init to negative == impossible */
992 /* zero out authentication data: */
993 memset(&data->state.authhost, 0, sizeof(struct auth));
994 memset(&data->state.authproxy, 0, sizeof(struct auth));
998 * curl_easy_pause() allows an application to pause or unpause a specific
999 * transfer and direction. This function sets the full new state for the
1000 * current connection this easy handle operates on.
1002 * NOTE: if you have the receiving paused and you call this function to remove
1003 * the pausing, you may get your write callback called at this point.
1005 * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
1007 CURLcode curl_easy_pause(struct Curl_easy *data, int action)
1009 struct SingleRequest *k = &data->req;
1010 CURLcode result = CURLE_OK;
1012 /* first switch off both pause bits */
1013 int newstate = k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
1015 /* set the new desired pause bits */
1016 newstate |= ((action & CURLPAUSE_RECV)?KEEP_RECV_PAUSE:0) |
1017 ((action & CURLPAUSE_SEND)?KEEP_SEND_PAUSE:0);
1019 /* put it back in the keepon */
1020 k->keepon = newstate;
1022 if(!(newstate & KEEP_RECV_PAUSE) && data->state.tempcount) {
1023 /* there are buffers for sending that can be delivered as the receive
1024 pausing is lifted! */
1026 unsigned int count = data->state.tempcount;
1027 struct tempbuf writebuf[3]; /* there can only be three */
1029 /* copy the structs to allow for immediate re-pausing */
1030 for(i = 0; i < data->state.tempcount; i++) {
1031 writebuf[i] = data->state.tempwrite[i];
1032 data->state.tempwrite[i].buf = NULL;
1034 data->state.tempcount = 0;
1036 for(i = 0; i < count; i++) {
1037 /* even if one function returns error, this loops through and frees all
1040 result = Curl_client_chop_write(data->easy_conn,
1044 free(writebuf[i].buf);
1050 /* if there's no error and we're not pausing both directions, we want
1051 to have this handle checked soon */
1053 ((newstate&(KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) !=
1054 (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) )
1055 Curl_expire(data, 0, EXPIRE_RUN_NOW); /* get this handle going again */
1061 static CURLcode easy_connection(struct Curl_easy *data,
1063 struct connectdata **connp)
1066 return CURLE_BAD_FUNCTION_ARGUMENT;
1068 /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
1069 if(!data->set.connect_only) {
1070 failf(data, "CONNECT_ONLY is required!");
1071 return CURLE_UNSUPPORTED_PROTOCOL;
1074 *sfd = Curl_getconnectinfo(data, connp);
1076 if(*sfd == CURL_SOCKET_BAD) {
1077 failf(data, "Failed to get recent socket");
1078 return CURLE_UNSUPPORTED_PROTOCOL;
1085 * Receives data from the connected socket. Use after successful
1086 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1087 * Returns CURLE_OK on success, error code on error.
1089 CURLcode curl_easy_recv(struct Curl_easy *data, void *buffer, size_t buflen,
1095 struct connectdata *c;
1097 result = easy_connection(data, &sfd, &c);
1102 result = Curl_read(c, sfd, buffer, buflen, &n1);
1113 * Sends data over the connected socket. Use after successful
1114 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1116 CURLcode curl_easy_send(struct Curl_easy *data, const void *buffer,
1117 size_t buflen, size_t *n)
1122 struct connectdata *c = NULL;
1124 result = easy_connection(data, &sfd, &c);
1129 result = Curl_write(c, sfd, buffer, buflen, &n1);
1132 return CURLE_SEND_ERROR;