1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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>
55 #include <curl/curl.h>
57 #include "vtls/vtls.h"
66 #include "sendf.h" /* for failf function prototype */
67 #include "connect.h" /* for Curl_getconnectinfo */
70 #include "non-ascii.h"
72 #include "conncache.h"
76 #include "curl_printf.h"
78 /* The last #include files should be: */
79 #include "curl_memory.h"
82 void Curl_version_init(void);
84 /* win32_cleanup() is for win32 socket cleanup functionality, the opposite
86 static void win32_cleanup(void)
91 #ifdef USE_WINDOWS_SSPI
92 Curl_sspi_global_cleanup();
96 /* win32_init() performs win32 socket initialization to properly setup the
97 stack to allow networking */
98 static CURLcode win32_init(void)
101 WORD wVersionRequested;
105 #if defined(ENABLE_IPV6) && (USE_WINSOCK < 2)
106 Error IPV6_requires_winsock2
109 wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
111 res = WSAStartup(wVersionRequested, &wsaData);
114 /* Tell the user that we couldn't find a useable */
116 return CURLE_FAILED_INIT;
118 /* Confirm that the Windows Sockets DLL supports what we need.*/
119 /* Note that if the DLL supports versions greater */
120 /* than wVersionRequested, it will still return */
121 /* wVersionRequested in wVersion. wHighVersion contains the */
122 /* highest supported version. */
124 if(LOBYTE( wsaData.wVersion ) != LOBYTE(wVersionRequested) ||
125 HIBYTE( wsaData.wVersion ) != HIBYTE(wVersionRequested) ) {
126 /* Tell the user that we couldn't find a useable */
130 return CURLE_FAILED_INIT;
132 /* The Windows Sockets DLL is acceptable. Proceed. */
133 #elif defined(USE_LWIPSOCK)
137 #ifdef USE_WINDOWS_SSPI
139 CURLcode result = Curl_sspi_global_init();
150 * Initialise use of IDNA library.
151 * It falls back to ASCII if $CHARSET isn't defined. This doesn't work for
152 * idna_to_ascii_lz().
154 static void idna_init (void)
160 if(!getenv("CHARSET") && cp > 0) {
161 snprintf(buf, sizeof(buf), "CHARSET=cp%u", cp);
168 #endif /* USE_LIBIDN */
170 /* true globals -- for curl_global_init() and curl_global_cleanup() */
171 static unsigned int initialized;
172 static long init_flags;
175 * strdup (and other memory functions) is redefined in complicated
176 * ways, but at this point it must be defined as the system-supplied strdup
177 * so the callback pointer is initialized correctly.
179 #if defined(_WIN32_WCE)
180 #define system_strdup _strdup
181 #elif !defined(HAVE_STRDUP)
182 #define system_strdup curlx_strdup
184 #define system_strdup strdup
187 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
188 # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
191 #ifndef __SYMBIAN32__
193 * If a memory-using function (like curl_getenv) is used before
194 * curl_global_init() is called, we need to have these pointers set already.
196 curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
197 curl_free_callback Curl_cfree = (curl_free_callback)free;
198 curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
199 curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
200 curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
201 #if defined(WIN32) && defined(UNICODE)
202 curl_wcsdup_callback Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
206 * Symbian OS doesn't support initialization to code in writeable static data.
207 * Initialization will occur in the curl_global_init() call.
209 curl_malloc_callback Curl_cmalloc;
210 curl_free_callback Curl_cfree;
211 curl_realloc_callback Curl_crealloc;
212 curl_strdup_callback Curl_cstrdup;
213 curl_calloc_callback Curl_ccalloc;
216 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
217 # pragma warning(default:4232) /* MSVC extension, dllimport identity */
221 * curl_global_init() globally initializes cURL given a bitwise set of the
222 * different features of what to initialize.
224 static CURLcode global_init(long flags, bool memoryfuncs)
230 /* Setup the default memory functions here (again) */
231 Curl_cmalloc = (curl_malloc_callback)malloc;
232 Curl_cfree = (curl_free_callback)free;
233 Curl_crealloc = (curl_realloc_callback)realloc;
234 Curl_cstrdup = (curl_strdup_callback)system_strdup;
235 Curl_ccalloc = (curl_calloc_callback)calloc;
236 #if defined(WIN32) && defined(UNICODE)
237 Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
241 if(flags & CURL_GLOBAL_SSL)
242 if(!Curl_ssl_init()) {
243 DEBUGF(fprintf(stderr, "Error: Curl_ssl_init failed\n"));
244 return CURLE_FAILED_INIT;
247 if(flags & CURL_GLOBAL_WIN32)
249 DEBUGF(fprintf(stderr, "Error: win32_init failed\n"));
250 return CURLE_FAILED_INIT;
254 if(!Curl_amiga_init()) {
255 DEBUGF(fprintf(stderr, "Error: Curl_amiga_init failed\n"));
256 return CURLE_FAILED_INIT;
262 DEBUGF(fprintf(stderr, "Warning: LONG namespace not available\n"));
270 if(Curl_resolver_global_init()) {
271 DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n"));
272 return CURLE_FAILED_INIT;
275 #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_INIT)
276 if(libssh2_init(0)) {
277 DEBUGF(fprintf(stderr, "Error: libssh2_init failed\n"));
278 return CURLE_FAILED_INIT;
282 if(flags & CURL_GLOBAL_ACK_EINTR)
294 * curl_global_init() globally initializes cURL given a bitwise set of the
295 * different features of what to initialize.
297 CURLcode curl_global_init(long flags)
299 return global_init(flags, TRUE);
303 * curl_global_init_mem() globally initializes cURL and also registers the
304 * user provided callback routines.
306 CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
307 curl_free_callback f, curl_realloc_callback r,
308 curl_strdup_callback s, curl_calloc_callback c)
310 /* Invalid input, return immediately */
311 if(!m || !f || !r || !s || !c)
312 return CURLE_FAILED_INIT;
315 /* Already initialized, don't do it again, but bump the variable anyway to
316 work like curl_global_init() and require the same amount of cleanup
322 /* set memory functions before global_init() in case it wants memory
330 /* Call the actual init function, but without setting */
331 return global_init(flags, FALSE);
335 * curl_global_cleanup() globally cleanups cURL, uses the value of
336 * "init_flags" to determine what needs to be cleaned up and what doesn't.
338 void curl_global_cleanup(void)
346 Curl_global_host_cache_dtor();
348 if(init_flags & CURL_GLOBAL_SSL)
351 Curl_resolver_global_cleanup();
353 if(init_flags & CURL_GLOBAL_WIN32)
356 Curl_amiga_cleanup();
358 #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_EXIT)
359 (void)libssh2_exit();
366 * curl_easy_init() is the external interface to alloc, setup and init an
367 * easy handle that is returned. If anything goes wrong, NULL is returned.
369 CURL *curl_easy_init(void)
372 struct SessionHandle *data;
374 /* Make sure we inited the global SSL stuff */
376 result = curl_global_init(CURL_GLOBAL_DEFAULT);
378 /* something in the global init failed, return nothing */
379 DEBUGF(fprintf(stderr, "Error: curl_global_init failed\n"));
384 /* We use curl_open() with undefined URL so far */
385 result = Curl_open(&data);
387 DEBUGF(fprintf(stderr, "Error: Curl_open failed\n"));
395 * curl_easy_setopt() is the external interface for setting options on an
399 #undef curl_easy_setopt
400 CURLcode curl_easy_setopt(CURL *curl, CURLoption tag, ...)
403 struct SessionHandle *data = curl;
407 return CURLE_BAD_FUNCTION_ARGUMENT;
411 result = Curl_setopt(data, tag, arg);
419 struct socketmonitor {
420 struct socketmonitor *next; /* the next node in the list or NULL */
421 struct pollfd socket; /* socket info of what to monitor */
425 long ms; /* timeout, run the timeout function when reached */
426 bool msbump; /* set TRUE when timeout is set by callback */
427 int num_sockets; /* number of nodes in the monitor list */
428 struct socketmonitor *list; /* list of sockets to monitor */
429 int running_handles; /* store the returned number */
434 * Callback that gets called with a new value when the timeout should be
438 static int events_timer(CURLM *multi, /* multi handle */
439 long timeout_ms, /* see above */
440 void *userp) /* private callback pointer */
442 struct events *ev = userp;
445 /* timeout removed */
447 else if(timeout_ms == 0)
448 /* timeout is already reached! */
449 timeout_ms = 1; /* trigger asap */
459 * convert from poll() bit definitions to libcurl's CURL_CSELECT_* ones
461 static int poll2cselect(int pollmask)
464 if(pollmask & POLLIN)
465 omask |= CURL_CSELECT_IN;
466 if(pollmask & POLLOUT)
467 omask |= CURL_CSELECT_OUT;
468 if(pollmask & POLLERR)
469 omask |= CURL_CSELECT_ERR;
476 * convert from libcurl' CURL_POLL_* bit definitions to poll()'s
478 static short socketcb2poll(int pollmask)
481 if(pollmask & CURL_POLL_IN)
483 if(pollmask & CURL_POLL_OUT)
490 * Callback that gets called with information about socket activity to
493 static int events_socket(CURL *easy, /* easy handle */
494 curl_socket_t s, /* socket */
495 int what, /* see above */
496 void *userp, /* private callback
498 void *socketp) /* private socket
501 struct events *ev = userp;
502 struct socketmonitor *m;
503 struct socketmonitor *prev=NULL;
505 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
512 if(m->socket.fd == s) {
514 if(what == CURL_POLL_REMOVE) {
515 struct socketmonitor *nxt = m->next;
516 /* remove this node from the list of monitored sockets */
523 infof(easy, "socket cb: socket %d REMOVED\n", s);
526 /* The socket 's' is already being monitored, update the activity
527 mask. Convert from libcurl bitmask to the poll one. */
528 m->socket.events = socketcb2poll(what);
529 infof(easy, "socket cb: socket %d UPDATED as %s%s\n", s,
530 what&CURL_POLL_IN?"IN":"",
531 what&CURL_POLL_OUT?"OUT":"");
536 m = m->next; /* move to next node */
539 if(what == CURL_POLL_REMOVE) {
540 /* this happens a bit too often, libcurl fix perhaps? */
542 "%s: socket %d asked to be REMOVED but not present!\n",
546 m = malloc(sizeof(struct socketmonitor));
550 m->socket.events = socketcb2poll(what);
551 m->socket.revents = 0;
553 infof(easy, "socket cb: socket %d ADDED as %s%s\n", s,
554 what&CURL_POLL_IN?"IN":"",
555 what&CURL_POLL_OUT?"OUT":"");
558 return CURLE_OUT_OF_MEMORY;
569 * Do the multi handle setups that only event-based transfers need.
571 static void events_setup(CURLM *multi, struct events *ev)
574 curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, events_timer);
575 curl_multi_setopt(multi, CURLMOPT_TIMERDATA, ev);
577 /* socket callback */
578 curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, events_socket);
579 curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev);
585 * waits for activity on any of the given sockets, or the timeout to trigger.
588 static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
592 CURLcode result = CURLE_OK;
596 struct socketmonitor *m;
598 struct pollfd fds[4];
602 struct timeval before;
603 struct timeval after;
605 /* populate the fds[] array */
606 for(m = ev->list, f=&fds[0]; m; m = m->next) {
607 f->fd = m->socket.fd;
608 f->events = m->socket.events;
610 /* fprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd); */
615 /* get the time stamp to use to figure out how long poll takes */
616 before = curlx_tvnow();
618 /* wait for activity or timeout */
619 pollrc = Curl_poll(fds, numfds, (int)ev->ms);
621 after = curlx_tvnow();
623 ev->msbump = FALSE; /* reset here */
628 /* fprintf(stderr, "call curl_multi_socket_action( TIMEOUT )\n"); */
629 mcode = curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0,
630 &ev->running_handles);
632 else if(pollrc > 0) {
633 /* loop over the monitored sockets to see which ones had activity */
634 for(i = 0; i< numfds; i++) {
636 /* socket activity, tell libcurl */
637 int act = poll2cselect(fds[i].revents); /* convert */
638 infof(multi->easyp, "call curl_multi_socket_action( socket %d )\n",
640 mcode = curl_multi_socket_action(multi, fds[i].fd, act,
641 &ev->running_handles);
646 /* If nothing updated the timeout, we decrease it by the spent time.
647 * If it was updated, it has the new timeout time stored already.
649 ev->ms += curlx_tvdiff(after, before);
653 return CURLE_RECV_ERROR;
656 return CURLE_URL_MALFORMAT; /* TODO: return a proper error! */
658 /* we don't really care about the "msgs_in_queue" value returned in the
660 msg = curl_multi_info_read(multi, &pollrc);
662 result = msg->data.result;
673 * Runs a transfer in a blocking manner using the events-based API
675 static CURLcode easy_events(CURLM *multi)
677 struct events evs= {2, FALSE, 0, NULL, 0};
679 /* if running event-based, do some further multi inits */
680 events_setup(multi, &evs);
682 return wait_or_timeout(multi, &evs);
684 #else /* CURLDEBUG */
685 /* when not built with debug, this function doesn't exist */
686 #define easy_events(x) CURLE_NOT_BUILT_IN
689 static CURLcode easy_transfer(CURLM *multi)
692 CURLMcode mcode = CURLM_OK;
693 CURLcode result = CURLE_OK;
694 struct timeval before;
695 int without_fds = 0; /* count number of consecutive returns from
696 curl_multi_wait() without any filedescriptors */
698 while(!done && !mcode) {
699 int still_running = 0;
702 before = curlx_tvnow();
703 mcode = curl_multi_wait(multi, NULL, 0, 1000, &rc);
707 struct timeval after = curlx_tvnow();
709 /* If it returns without any filedescriptor instantly, we need to
710 avoid busy-looping during periods where it has nothing particular
712 if(curlx_tvdiff(after, before) <= 10) {
714 if(without_fds > 2) {
715 int sleep_ms = without_fds < 10 ? (1 << (without_fds - 1)) : 1000;
716 Curl_wait_ms(sleep_ms);
720 /* it wasn't "instant", restart counter */
724 /* got file descriptor, restart counter */
727 mcode = curl_multi_perform(multi, &still_running);
730 /* only read 'still_running' if curl_multi_perform() return OK */
731 if(!mcode && !still_running) {
732 CURLMsg *msg = curl_multi_info_read(multi, &rc);
734 result = msg->data.result;
740 /* Make sure to return some kind of error if there was a multi problem */
742 result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
743 /* The other multi errors should never happen, so return
744 something suitably generic */
745 CURLE_BAD_FUNCTION_ARGUMENT;
753 * easy_perform() is the external interface that performs a blocking
754 * transfer as previously setup.
756 * CONCEPT: This function creates a multi handle, adds the easy handle to it,
757 * runs curl_multi_perform() until the transfer is done, then detaches the
758 * easy handle, destroys the multi handle and returns the easy handle's return
761 * REALITY: it can't just create and destroy the multi handle that easily. It
762 * needs to keep it around since if this easy handle is used again by this
763 * function, the same multi handle must be re-used so that the same pools and
764 * caches can be used.
766 * DEBUG: if 'events' is set TRUE, this function will use a replacement engine
767 * instead of curl_multi_perform() and use curl_multi_socket_action().
769 static CURLcode easy_perform(struct SessionHandle *data, bool events)
773 CURLcode result = CURLE_OK;
774 SIGPIPE_VARIABLE(pipe_st);
777 return CURLE_BAD_FUNCTION_ARGUMENT;
780 failf(data, "easy handle already used in multi handle");
781 return CURLE_FAILED_INIT;
785 multi = data->multi_easy;
787 /* this multi handle will only ever have a single easy handled attached
788 to it, so make it use minimal hashes */
789 multi = Curl_multi_handle(1, 3);
791 return CURLE_OUT_OF_MEMORY;
792 data->multi_easy = multi;
795 /* Copy the MAXCONNECTS option to the multi handle */
796 curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, data->set.maxconnects);
798 mcode = curl_multi_add_handle(multi, data);
800 curl_multi_cleanup(multi);
801 if(mcode == CURLM_OUT_OF_MEMORY)
802 return CURLE_OUT_OF_MEMORY;
804 return CURLE_FAILED_INIT;
807 sigpipe_ignore(data, &pipe_st);
809 /* assign this after curl_multi_add_handle() since that function checks for
810 it and rejects this handle otherwise */
813 /* run the transfer */
814 result = events ? easy_events(multi) : easy_transfer(multi);
816 /* ignoring the return code isn't nice, but atm we can't really handle
817 a failure here, room for future improvement! */
818 (void)curl_multi_remove_handle(multi, data);
820 sigpipe_restore(&pipe_st);
822 /* The multi handle is kept alive, owned by the easy handle */
828 * curl_easy_perform() is the external interface that performs a blocking
829 * transfer as previously setup.
831 CURLcode curl_easy_perform(CURL *easy)
833 return easy_perform(easy, FALSE);
838 * curl_easy_perform_ev() is the external interface that performs a blocking
839 * transfer using the event-based API internally.
841 CURLcode curl_easy_perform_ev(CURL *easy)
843 return easy_perform(easy, TRUE);
849 * curl_easy_cleanup() is the external interface to cleaning/freeing the given
852 void curl_easy_cleanup(CURL *curl)
854 struct SessionHandle *data = (struct SessionHandle *)curl;
855 SIGPIPE_VARIABLE(pipe_st);
860 sigpipe_ignore(data, &pipe_st);
862 sigpipe_restore(&pipe_st);
866 * curl_easy_getinfo() is an external interface that allows an app to retrieve
867 * information from a performed transfer and similar.
869 #undef curl_easy_getinfo
870 CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...)
875 struct SessionHandle *data = (struct SessionHandle *)curl;
878 paramp = va_arg(arg, void *);
880 result = Curl_getinfo(data, info, paramp);
887 * curl_easy_duphandle() is an external interface to allow duplication of a
888 * given input easy handle. The returned handle will be a new working handle
889 * with all options set exactly as the input source handle.
891 CURL *curl_easy_duphandle(CURL *incurl)
893 struct SessionHandle *data=(struct SessionHandle *)incurl;
895 struct SessionHandle *outcurl = calloc(1, sizeof(struct SessionHandle));
900 * We setup a few buffers we need. We should probably make them
901 * get setup on-demand in the code, as that would probably decrease
902 * the likeliness of us forgetting to init a buffer here in the future.
904 outcurl->state.headerbuff = malloc(HEADERSIZE);
905 if(!outcurl->state.headerbuff)
907 outcurl->state.headersize = HEADERSIZE;
909 /* copy all userdefined values */
910 if(Curl_dupset(outcurl, data))
913 /* the connection cache is setup on demand */
914 outcurl->state.conn_cache = NULL;
916 outcurl->state.lastconnect = NULL;
918 outcurl->progress.flags = data->progress.flags;
919 outcurl->progress.callback = data->progress.callback;
922 /* If cookies are enabled in the parent handle, we enable them
923 in the clone as well! */
924 outcurl->cookies = Curl_cookie_init(data,
925 data->cookies->filename,
927 data->set.cookiesession);
928 if(!outcurl->cookies)
932 /* duplicate all values in 'change' */
933 if(data->change.cookielist) {
934 outcurl->change.cookielist =
935 Curl_slist_duplicate(data->change.cookielist);
936 if(!outcurl->change.cookielist)
940 if(data->change.url) {
941 outcurl->change.url = strdup(data->change.url);
942 if(!outcurl->change.url)
944 outcurl->change.url_alloc = TRUE;
947 if(data->change.referer) {
948 outcurl->change.referer = strdup(data->change.referer);
949 if(!outcurl->change.referer)
951 outcurl->change.referer_alloc = TRUE;
954 /* Clone the resolver handle, if present, for the new handle */
955 if(Curl_resolver_duphandle(&outcurl->state.resolver,
956 data->state.resolver))
959 Curl_convert_setup(outcurl);
961 outcurl->magic = CURLEASY_MAGIC_NUMBER;
963 /* we reach this point and thus we are OK */
970 curl_slist_free_all(outcurl->change.cookielist);
971 outcurl->change.cookielist = NULL;
972 Curl_safefree(outcurl->state.headerbuff);
973 Curl_safefree(outcurl->change.url);
974 Curl_safefree(outcurl->change.referer);
975 Curl_freeset(outcurl);
983 * curl_easy_reset() is an external interface that allows an app to re-
984 * initialize a session handle to the default values.
986 void curl_easy_reset(CURL *curl)
988 struct SessionHandle *data = (struct SessionHandle *)curl;
990 Curl_safefree(data->state.pathbuffer);
992 data->state.path = NULL;
994 Curl_free_request_state(data);
996 /* zero out UserDefined data: */
998 memset(&data->set, 0, sizeof(struct UserDefined));
999 (void)Curl_init_userdefined(&data->set);
1001 /* zero out Progress data: */
1002 memset(&data->progress, 0, sizeof(struct Progress));
1004 data->progress.flags |= PGRS_HIDE;
1005 data->state.current_speed = -1; /* init to negative == impossible */
1009 * curl_easy_pause() allows an application to pause or unpause a specific
1010 * transfer and direction. This function sets the full new state for the
1011 * current connection this easy handle operates on.
1013 * NOTE: if you have the receiving paused and you call this function to remove
1014 * the pausing, you may get your write callback called at this point.
1016 * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
1018 CURLcode curl_easy_pause(CURL *curl, int action)
1020 struct SessionHandle *data = (struct SessionHandle *)curl;
1021 struct SingleRequest *k = &data->req;
1022 CURLcode result = CURLE_OK;
1024 /* first switch off both pause bits */
1025 int newstate = k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
1027 /* set the new desired pause bits */
1028 newstate |= ((action & CURLPAUSE_RECV)?KEEP_RECV_PAUSE:0) |
1029 ((action & CURLPAUSE_SEND)?KEEP_SEND_PAUSE:0);
1031 /* put it back in the keepon */
1032 k->keepon = newstate;
1034 if(!(newstate & KEEP_RECV_PAUSE) && data->state.tempwrite) {
1035 /* we have a buffer for sending that we now seem to be able to deliver
1036 since the receive pausing is lifted! */
1038 /* get the pointer in local copy since the function may return PAUSE
1039 again and then we'll get a new copy allocted and stored in
1040 the tempwrite variables */
1041 char *tempwrite = data->state.tempwrite;
1043 data->state.tempwrite = NULL;
1044 result = Curl_client_chop_write(data->easy_conn, data->state.tempwritetype,
1045 tempwrite, data->state.tempwritesize);
1049 /* if there's no error and we're not pausing both directions, we want
1050 to have this handle checked soon */
1052 ((newstate&(KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) !=
1053 (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) )
1054 Curl_expire(data, 1); /* get this handle going again */
1060 static CURLcode easy_connection(struct SessionHandle *data,
1062 struct connectdata **connp)
1065 return CURLE_BAD_FUNCTION_ARGUMENT;
1067 /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
1068 if(!data->set.connect_only) {
1069 failf(data, "CONNECT_ONLY is required!");
1070 return CURLE_UNSUPPORTED_PROTOCOL;
1073 *sfd = Curl_getconnectinfo(data, connp);
1075 if(*sfd == CURL_SOCKET_BAD) {
1076 failf(data, "Failed to get recent socket");
1077 return CURLE_UNSUPPORTED_PROTOCOL;
1084 * Receives data from the connected socket. Use after successful
1085 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1086 * Returns CURLE_OK on success, error code on error.
1088 CURLcode curl_easy_recv(CURL *curl, void *buffer, size_t buflen, size_t *n)
1093 struct connectdata *c;
1094 struct SessionHandle *data = (struct SessionHandle *)curl;
1096 result = easy_connection(data, &sfd, &c);
1101 result = Curl_read(c, sfd, buffer, buflen, &n1);
1112 * Sends data over the connected socket. Use after successful
1113 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1115 CURLcode curl_easy_send(CURL *curl, const void *buffer, size_t buflen,
1121 struct connectdata *c = NULL;
1122 struct SessionHandle *data = (struct SessionHandle *)curl;
1124 result = easy_connection(data, &sfd, &c);
1129 result = Curl_write(c, sfd, buffer, buflen, &n1);
1132 return CURLE_SEND_ERROR;