1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 http://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"
63 #include "curl_memory.h"
67 #include "sendf.h" /* for failf function prototype */
68 #include "curl_ntlm.h"
69 #include "connect.h" /* for Curl_getconnectinfo */
72 #include "non-ascii.h"
74 #include "conncache.h"
79 #define _MPRINTF_REPLACE /* use our functions only */
80 #include <curl/mprintf.h>
82 /* The last #include file should be: */
85 /* win32_cleanup() is for win32 socket cleanup functionality, the opposite
87 static void win32_cleanup(void)
92 #ifdef USE_WINDOWS_SSPI
93 Curl_sspi_global_cleanup();
97 /* win32_init() performs win32 socket initialization to properly setup the
98 stack to allow networking */
99 static CURLcode win32_init(void)
102 WORD wVersionRequested;
106 #if defined(ENABLE_IPV6) && (USE_WINSOCK < 2)
107 Error IPV6_requires_winsock2
110 wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
112 res = WSAStartup(wVersionRequested, &wsaData);
115 /* Tell the user that we couldn't find a useable */
117 return CURLE_FAILED_INIT;
119 /* Confirm that the Windows Sockets DLL supports what we need.*/
120 /* Note that if the DLL supports versions greater */
121 /* than wVersionRequested, it will still return */
122 /* wVersionRequested in wVersion. wHighVersion contains the */
123 /* highest supported version. */
125 if(LOBYTE( wsaData.wVersion ) != LOBYTE(wVersionRequested) ||
126 HIBYTE( wsaData.wVersion ) != HIBYTE(wVersionRequested) ) {
127 /* Tell the user that we couldn't find a useable */
131 return CURLE_FAILED_INIT;
133 /* The Windows Sockets DLL is acceptable. Proceed. */
134 #elif defined(USE_LWIPSOCK)
138 #ifdef USE_WINDOWS_SSPI
140 CURLcode result = Curl_sspi_global_init();
151 * Initialise use of IDNA library.
152 * It falls back to ASCII if $CHARSET isn't defined. This doesn't work for
153 * idna_to_ascii_lz().
155 static void idna_init (void)
161 if(!getenv("CHARSET") && cp > 0) {
162 snprintf(buf, sizeof(buf), "CHARSET=cp%u", cp);
169 #endif /* USE_LIBIDN */
171 /* true globals -- for curl_global_init() and curl_global_cleanup() */
172 static unsigned int initialized;
173 static long init_flags;
176 * strdup (and other memory functions) is redefined in complicated
177 * ways, but at this point it must be defined as the system-supplied strdup
178 * so the callback pointer is initialized correctly.
180 #if defined(_WIN32_WCE)
181 #define system_strdup _strdup
182 #elif !defined(HAVE_STRDUP)
183 #define system_strdup curlx_strdup
185 #define system_strdup strdup
188 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
189 # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
192 #ifndef __SYMBIAN32__
194 * If a memory-using function (like curl_getenv) is used before
195 * curl_global_init() is called, we need to have these pointers set already.
197 curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
198 curl_free_callback Curl_cfree = (curl_free_callback)free;
199 curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
200 curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
201 curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
202 #if defined(WIN32) && defined(UNICODE)
203 curl_wcsdup_callback Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
207 * Symbian OS doesn't support initialization to code in writeable static data.
208 * Initialization will occur in the curl_global_init() call.
210 curl_malloc_callback Curl_cmalloc;
211 curl_free_callback Curl_cfree;
212 curl_realloc_callback Curl_crealloc;
213 curl_strdup_callback Curl_cstrdup;
214 curl_calloc_callback Curl_ccalloc;
217 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
218 # pragma warning(default:4232) /* MSVC extension, dllimport identity */
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 /* 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;
240 if(flags & CURL_GLOBAL_SSL)
241 if(!Curl_ssl_init()) {
242 DEBUGF(fprintf(stderr, "Error: Curl_ssl_init failed\n"));
243 return CURLE_FAILED_INIT;
246 if(flags & CURL_GLOBAL_WIN32)
248 DEBUGF(fprintf(stderr, "Error: win32_init failed\n"));
249 return CURLE_FAILED_INIT;
253 if(!Curl_amiga_init()) {
254 DEBUGF(fprintf(stderr, "Error: Curl_amiga_init failed\n"));
255 return CURLE_FAILED_INIT;
261 DEBUGF(fprintf(stderr, "Warning: LONG namespace not available\n"));
269 if(Curl_resolver_global_init()) {
270 DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n"));
271 return CURLE_FAILED_INIT;
274 #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_INIT)
275 if(libssh2_init(0)) {
276 DEBUGF(fprintf(stderr, "Error: libssh2_init failed\n"));
277 return CURLE_FAILED_INIT;
281 if(flags & CURL_GLOBAL_ACK_EINTR)
290 * curl_global_init_mem() globally initializes cURL and also registers the
291 * user provided callback routines.
293 CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
294 curl_free_callback f, curl_realloc_callback r,
295 curl_strdup_callback s, curl_calloc_callback c)
297 CURLcode result = CURLE_OK;
299 /* Invalid input, return immediately */
300 if(!m || !f || !r || !s || !c)
301 return CURLE_FAILED_INIT;
304 /* Already initialized, don't do it again, but bump the variable anyway to
305 work like curl_global_init() and require the same amount of cleanup
311 /* Call the actual init function first */
312 result = curl_global_init(flags);
325 * curl_global_cleanup() globally cleanups cURL, uses the value of
326 * "init_flags" to determine what needs to be cleaned up and what doesn't.
328 void curl_global_cleanup(void)
336 Curl_global_host_cache_dtor();
338 if(init_flags & CURL_GLOBAL_SSL)
341 Curl_resolver_global_cleanup();
343 if(init_flags & CURL_GLOBAL_WIN32)
346 Curl_amiga_cleanup();
348 #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_EXIT)
349 (void)libssh2_exit();
356 * curl_easy_init() is the external interface to alloc, setup and init an
357 * easy handle that is returned. If anything goes wrong, NULL is returned.
359 CURL *curl_easy_init(void)
362 struct SessionHandle *data;
364 /* Make sure we inited the global SSL stuff */
366 result = curl_global_init(CURL_GLOBAL_DEFAULT);
368 /* something in the global init failed, return nothing */
369 DEBUGF(fprintf(stderr, "Error: curl_global_init failed\n"));
374 /* We use curl_open() with undefined URL so far */
375 result = Curl_open(&data);
377 DEBUGF(fprintf(stderr, "Error: Curl_open failed\n"));
385 * curl_easy_setopt() is the external interface for setting options on an
389 #undef curl_easy_setopt
390 CURLcode curl_easy_setopt(CURL *curl, CURLoption tag, ...)
393 struct SessionHandle *data = curl;
397 return CURLE_BAD_FUNCTION_ARGUMENT;
401 result = Curl_setopt(data, tag, arg);
409 struct socketmonitor {
410 struct socketmonitor *next; /* the next node in the list or NULL */
411 struct pollfd socket; /* socket info of what to monitor */
415 long ms; /* timeout, run the timeout function when reached */
416 bool msbump; /* set TRUE when timeout is set by callback */
417 int num_sockets; /* number of nodes in the monitor list */
418 struct socketmonitor *list; /* list of sockets to monitor */
419 int running_handles; /* store the returned number */
424 * Callback that gets called with a new value when the timeout should be
428 static int events_timer(CURLM *multi, /* multi handle */
429 long timeout_ms, /* see above */
430 void *userp) /* private callback pointer */
432 struct events *ev = userp;
435 /* timeout removed */
437 else if(timeout_ms == 0)
438 /* timeout is already reached! */
439 timeout_ms = 1; /* trigger asap */
449 * convert from poll() bit definitions to libcurl's CURL_CSELECT_* ones
451 static int poll2cselect(int pollmask)
454 if(pollmask & POLLIN)
455 omask |= CURL_CSELECT_IN;
456 if(pollmask & POLLOUT)
457 omask |= CURL_CSELECT_OUT;
458 if(pollmask & POLLERR)
459 omask |= CURL_CSELECT_ERR;
466 * convert from libcurl' CURL_POLL_* bit definitions to poll()'s
468 static short socketcb2poll(int pollmask)
471 if(pollmask & CURL_POLL_IN)
473 if(pollmask & CURL_POLL_OUT)
480 * Callback that gets called with information about socket activity to
483 static int events_socket(CURL *easy, /* easy handle */
484 curl_socket_t s, /* socket */
485 int what, /* see above */
486 void *userp, /* private callback
488 void *socketp) /* private socket
491 struct events *ev = userp;
492 struct socketmonitor *m;
493 struct socketmonitor *prev=NULL;
495 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
502 if(m->socket.fd == s) {
504 if(what == CURL_POLL_REMOVE) {
505 struct socketmonitor *nxt = m->next;
506 /* remove this node from the list of monitored sockets */
513 infof(easy, "socket cb: socket %d REMOVED\n", s);
516 /* The socket 's' is already being monitored, update the activity
517 mask. Convert from libcurl bitmask to the poll one. */
518 m->socket.events = socketcb2poll(what);
519 infof(easy, "socket cb: socket %d UPDATED as %s%s\n", s,
520 what&CURL_POLL_IN?"IN":"",
521 what&CURL_POLL_OUT?"OUT":"");
526 m = m->next; /* move to next node */
529 if(what == CURL_POLL_REMOVE) {
530 /* this happens a bit too often, libcurl fix perhaps? */
532 "%s: socket %d asked to be REMOVED but not present!\n",
536 m = malloc(sizeof(struct socketmonitor));
539 m->socket.events = socketcb2poll(what);
540 m->socket.revents = 0;
542 infof(easy, "socket cb: socket %d ADDED as %s%s\n", s,
543 what&CURL_POLL_IN?"IN":"",
544 what&CURL_POLL_OUT?"OUT":"");
555 * Do the multi handle setups that only event-based transfers need.
557 static void events_setup(CURLM *multi, struct events *ev)
560 curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, events_timer);
561 curl_multi_setopt(multi, CURLMOPT_TIMERDATA, ev);
563 /* socket callback */
564 curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, events_socket);
565 curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev);
571 * waits for activity on any of the given sockets, or the timeout to trigger.
574 static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
578 CURLcode result = CURLE_OK;
582 struct socketmonitor *m;
584 struct pollfd fds[4];
588 struct timeval before;
589 struct timeval after;
591 /* populate the fds[] array */
592 for(m = ev->list, f=&fds[0]; m; m = m->next) {
593 f->fd = m->socket.fd;
594 f->events = m->socket.events;
596 /* fprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd); */
601 /* get the time stamp to use to figure out how long poll takes */
602 before = curlx_tvnow();
604 /* wait for activity or timeout */
605 pollrc = Curl_poll(fds, numfds, (int)ev->ms);
607 after = curlx_tvnow();
609 ev->msbump = FALSE; /* reset here */
614 /* fprintf(stderr, "call curl_multi_socket_action( TIMEOUT )\n"); */
615 mcode = curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0,
616 &ev->running_handles);
618 else if(pollrc > 0) {
619 /* loop over the monitored sockets to see which ones had activity */
620 for(i = 0; i< numfds; i++) {
622 /* socket activity, tell libcurl */
623 int act = poll2cselect(fds[i].revents); /* convert */
624 infof(multi->easyp, "call curl_multi_socket_action( socket %d )\n",
626 mcode = curl_multi_socket_action(multi, fds[i].fd, act,
627 &ev->running_handles);
632 /* If nothing updated the timeout, we decrease it by the spent time.
633 * If it was updated, it has the new timeout time stored already.
635 ev->ms += curlx_tvdiff(after, before);
639 return CURLE_RECV_ERROR;
642 return CURLE_URL_MALFORMAT; /* TODO: return a proper error! */
644 /* we don't really care about the "msgs_in_queue" value returned in the
646 msg = curl_multi_info_read(multi, &pollrc);
648 result = msg->data.result;
659 * Runs a transfer in a blocking manner using the events-based API
661 static CURLcode easy_events(CURLM *multi)
663 struct events evs= {2, FALSE, 0, NULL, 0};
665 /* if running event-based, do some further multi inits */
666 events_setup(multi, &evs);
668 return wait_or_timeout(multi, &evs);
670 #else /* CURLDEBUG */
671 /* when not built with debug, this function doesn't exist */
672 #define easy_events(x) CURLE_NOT_BUILT_IN
675 static CURLcode easy_transfer(CURLM *multi)
678 CURLMcode mcode = CURLM_OK;
679 CURLcode result = CURLE_OK;
680 struct timeval before;
681 int without_fds = 0; /* count number of consecutive returns from
682 curl_multi_wait() without any filedescriptors */
684 while(!done && !mcode) {
685 int still_running = 0;
688 before = curlx_tvnow();
689 mcode = curl_multi_wait(multi, NULL, 0, 1000, &ret);
691 if(mcode == CURLM_OK) {
693 /* poll() failed not on EINTR, indicate a network problem */
694 result = CURLE_RECV_ERROR;
698 struct timeval after = curlx_tvnow();
699 /* If it returns without any filedescriptor instantly, we need to
700 avoid busy-looping during periods where it has nothing particular
702 if(curlx_tvdiff(after, before) <= 10) {
704 if(without_fds > 2) {
705 int sleep_ms = without_fds < 10 ? (1 << (without_fds-1)): 1000;
706 Curl_wait_ms(sleep_ms);
710 /* it wasn't "instant", restart counter */
714 /* got file descriptor, restart counter */
717 mcode = curl_multi_perform(multi, &still_running);
720 /* only read 'still_running' if curl_multi_perform() return OK */
721 if((mcode == CURLM_OK) && !still_running) {
723 CURLMsg *msg = curl_multi_info_read(multi, &rc);
725 result = msg->data.result;
731 /* Make sure to return some kind of error if there was a multi problem */
733 return (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
734 /* The other multi errors should never happen, so return
735 something suitably generic */
736 CURLE_BAD_FUNCTION_ARGUMENT;
744 * easy_perform() is the external interface that performs a blocking
745 * transfer as previously setup.
747 * CONCEPT: This function creates a multi handle, adds the easy handle to it,
748 * runs curl_multi_perform() until the transfer is done, then detaches the
749 * easy handle, destroys the multi handle and returns the easy handle's return
752 * REALITY: it can't just create and destroy the multi handle that easily. It
753 * needs to keep it around since if this easy handle is used again by this
754 * function, the same multi handle must be re-used so that the same pools and
755 * caches can be used.
757 * DEBUG: if 'events' is set TRUE, this function will use a replacement engine
758 * instead of curl_multi_perform() and use curl_multi_socket_action().
760 static CURLcode easy_perform(struct SessionHandle *data, bool events)
764 CURLcode result = CURLE_OK;
765 SIGPIPE_VARIABLE(pipe_st);
768 return CURLE_BAD_FUNCTION_ARGUMENT;
771 failf(data, "easy handle already used in multi handle");
772 return CURLE_FAILED_INIT;
776 multi = data->multi_easy;
778 /* this multi handle will only ever have a single easy handled attached
779 to it, so make it use minimal hashes */
780 multi = Curl_multi_handle(1, 3);
782 return CURLE_OUT_OF_MEMORY;
783 data->multi_easy = multi;
786 /* Copy the MAXCONNECTS option to the multi handle */
787 curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, data->set.maxconnects);
789 mcode = curl_multi_add_handle(multi, data);
791 curl_multi_cleanup(multi);
792 if(mcode == CURLM_OUT_OF_MEMORY)
793 return CURLE_OUT_OF_MEMORY;
795 return CURLE_FAILED_INIT;
798 sigpipe_ignore(data, &pipe_st);
800 /* assign this after curl_multi_add_handle() since that function checks for
801 it and rejects this handle otherwise */
804 /* run the transfer */
805 result = events ? easy_events(multi) : easy_transfer(multi);
807 /* ignoring the return code isn't nice, but atm we can't really handle
808 a failure here, room for future improvement! */
809 (void)curl_multi_remove_handle(multi, data);
811 sigpipe_restore(&pipe_st);
813 /* The multi handle is kept alive, owned by the easy handle */
819 * curl_easy_perform() is the external interface that performs a blocking
820 * transfer as previously setup.
822 CURLcode curl_easy_perform(CURL *easy)
824 return easy_perform(easy, FALSE);
829 * curl_easy_perform_ev() is the external interface that performs a blocking
830 * transfer using the event-based API internally.
832 CURLcode curl_easy_perform_ev(CURL *easy)
834 return easy_perform(easy, TRUE);
840 * curl_easy_cleanup() is the external interface to cleaning/freeing the given
843 void curl_easy_cleanup(CURL *curl)
845 struct SessionHandle *data = (struct SessionHandle *)curl;
846 SIGPIPE_VARIABLE(pipe_st);
851 sigpipe_ignore(data, &pipe_st);
853 sigpipe_restore(&pipe_st);
857 * curl_easy_getinfo() is an external interface that allows an app to retrieve
858 * information from a performed transfer and similar.
860 #undef curl_easy_getinfo
861 CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...)
866 struct SessionHandle *data = (struct SessionHandle *)curl;
869 paramp = va_arg(arg, void *);
871 result = Curl_getinfo(data, info, paramp);
878 * curl_easy_duphandle() is an external interface to allow duplication of a
879 * given input easy handle. The returned handle will be a new working handle
880 * with all options set exactly as the input source handle.
882 CURL *curl_easy_duphandle(CURL *incurl)
884 struct SessionHandle *data=(struct SessionHandle *)incurl;
886 struct SessionHandle *outcurl = calloc(1, sizeof(struct SessionHandle));
891 * We setup a few buffers we need. We should probably make them
892 * get setup on-demand in the code, as that would probably decrease
893 * the likeliness of us forgetting to init a buffer here in the future.
895 outcurl->state.headerbuff = malloc(HEADERSIZE);
896 if(!outcurl->state.headerbuff)
898 outcurl->state.headersize = HEADERSIZE;
900 /* copy all userdefined values */
901 if(Curl_dupset(outcurl, data))
904 /* the connection cache is setup on demand */
905 outcurl->state.conn_cache = NULL;
907 outcurl->state.lastconnect = NULL;
909 outcurl->progress.flags = data->progress.flags;
910 outcurl->progress.callback = data->progress.callback;
913 /* If cookies are enabled in the parent handle, we enable them
914 in the clone as well! */
915 outcurl->cookies = Curl_cookie_init(data,
916 data->cookies->filename,
918 data->set.cookiesession);
919 if(!outcurl->cookies)
923 /* duplicate all values in 'change' */
924 if(data->change.cookielist) {
925 outcurl->change.cookielist =
926 Curl_slist_duplicate(data->change.cookielist);
927 if(!outcurl->change.cookielist)
931 if(data->change.url) {
932 outcurl->change.url = strdup(data->change.url);
933 if(!outcurl->change.url)
935 outcurl->change.url_alloc = TRUE;
938 if(data->change.referer) {
939 outcurl->change.referer = strdup(data->change.referer);
940 if(!outcurl->change.referer)
942 outcurl->change.referer_alloc = TRUE;
945 /* Clone the resolver handle, if present, for the new handle */
946 if(Curl_resolver_duphandle(&outcurl->state.resolver,
947 data->state.resolver))
950 Curl_convert_setup(outcurl);
952 outcurl->magic = CURLEASY_MAGIC_NUMBER;
954 /* we reach this point and thus we are OK */
961 curl_slist_free_all(outcurl->change.cookielist);
962 outcurl->change.cookielist = NULL;
963 Curl_safefree(outcurl->state.headerbuff);
964 Curl_safefree(outcurl->change.url);
965 Curl_safefree(outcurl->change.referer);
966 Curl_freeset(outcurl);
974 * curl_easy_reset() is an external interface that allows an app to re-
975 * initialize a session handle to the default values.
977 void curl_easy_reset(CURL *curl)
979 struct SessionHandle *data = (struct SessionHandle *)curl;
981 Curl_safefree(data->state.pathbuffer);
983 data->state.path = NULL;
985 Curl_free_request_state(data);
987 /* zero out UserDefined data: */
989 memset(&data->set, 0, sizeof(struct UserDefined));
990 (void)Curl_init_userdefined(&data->set);
992 /* zero out Progress data: */
993 memset(&data->progress, 0, sizeof(struct Progress));
995 data->progress.flags |= PGRS_HIDE;
996 data->state.current_speed = -1; /* init to negative == impossible */
1000 * curl_easy_pause() allows an application to pause or unpause a specific
1001 * transfer and direction. This function sets the full new state for the
1002 * current connection this easy handle operates on.
1004 * NOTE: if you have the receiving paused and you call this function to remove
1005 * the pausing, you may get your write callback called at this point.
1007 * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
1009 CURLcode curl_easy_pause(CURL *curl, int action)
1011 struct SessionHandle *data = (struct SessionHandle *)curl;
1012 struct SingleRequest *k = &data->req;
1013 CURLcode result = CURLE_OK;
1015 /* first switch off both pause bits */
1016 int newstate = k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
1018 /* set the new desired pause bits */
1019 newstate |= ((action & CURLPAUSE_RECV)?KEEP_RECV_PAUSE:0) |
1020 ((action & CURLPAUSE_SEND)?KEEP_SEND_PAUSE:0);
1022 /* put it back in the keepon */
1023 k->keepon = newstate;
1025 if(!(newstate & KEEP_RECV_PAUSE) && data->state.tempwrite) {
1026 /* we have a buffer for sending that we now seem to be able to deliver
1027 since the receive pausing is lifted! */
1029 /* get the pointer in local copy since the function may return PAUSE
1030 again and then we'll get a new copy allocted and stored in
1031 the tempwrite variables */
1032 char *tempwrite = data->state.tempwrite;
1034 data->state.tempwrite = NULL;
1035 result = Curl_client_chop_write(data->easy_conn, data->state.tempwritetype,
1036 tempwrite, data->state.tempwritesize);
1040 /* if there's no error and we're not pausing both directions, we want
1041 to have this handle checked soon */
1043 ((newstate&(KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) !=
1044 (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) )
1045 Curl_expire(data, 1); /* get this handle going again */
1051 static CURLcode easy_connection(struct SessionHandle *data,
1053 struct connectdata **connp)
1056 return CURLE_BAD_FUNCTION_ARGUMENT;
1058 /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
1059 if(!data->set.connect_only) {
1060 failf(data, "CONNECT_ONLY is required!");
1061 return CURLE_UNSUPPORTED_PROTOCOL;
1064 *sfd = Curl_getconnectinfo(data, connp);
1066 if(*sfd == CURL_SOCKET_BAD) {
1067 failf(data, "Failed to get recent socket");
1068 return CURLE_UNSUPPORTED_PROTOCOL;
1075 * Receives data from the connected socket. Use after successful
1076 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1077 * Returns CURLE_OK on success, error code on error.
1079 CURLcode curl_easy_recv(CURL *curl, void *buffer, size_t buflen, size_t *n)
1084 struct connectdata *c;
1085 struct SessionHandle *data = (struct SessionHandle *)curl;
1087 result = easy_connection(data, &sfd, &c);
1092 result = Curl_read(c, sfd, buffer, buflen, &n1);
1103 * Sends data over the connected socket. Use after successful
1104 * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1106 CURLcode curl_easy_send(CURL *curl, const void *buffer, size_t buflen,
1112 struct connectdata *c = NULL;
1113 struct SessionHandle *data = (struct SessionHandle *)curl;
1115 result = easy_connection(data, &sfd, &c);
1120 result = Curl_write(c, sfd, buffer, buflen, &n1);
1123 return CURLE_SEND_ERROR;