easy.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 
23 #include "curl_setup.h"
24 
25 /*
26  * See comment in curl_memory.h for the explanation of this sanity check.
27  */
28 
29 #ifdef CURLX_NO_MEMORY_CALLBACKS
30 #error "libcurl shall not ever be built with CURLX_NO_MEMORY_CALLBACKS defined"
31 #endif
32 
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
35 #endif
36 #ifdef HAVE_NETDB_H
37 #include <netdb.h>
38 #endif
39 #ifdef HAVE_ARPA_INET_H
40 #include <arpa/inet.h>
41 #endif
42 #ifdef HAVE_NET_IF_H
43 #include <net/if.h>
44 #endif
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 
49 #ifdef HAVE_SYS_PARAM_H
50 #include <sys/param.h>
51 #endif
52 
53 #include "urldata.h"
54 #include <curl/curl.h>
55 #include "transfer.h"
56 #include "vtls/vtls.h"
57 #include "url.h"
58 #include "getinfo.h"
59 #include "hostip.h"
60 #include "share.h"
61 #include "strdup.h"
62 #include "progress.h"
63 #include "easyif.h"
64 #include "select.h"
65 #include "sendf.h" /* for failf function prototype */
66 #include "connect.h" /* for Curl_getconnectinfo */
67 #include "slist.h"
68 #include "amigaos.h"
69 #include "non-ascii.h"
70 #include "warnless.h"
71 #include "conncache.h"
72 #include "multiif.h"
73 #include "sigpipe.h"
74 #include "ssh.h"
75 /* The last 3 #include files should be in this order */
76 #include "curl_printf.h"
77 #include "curl_memory.h"
78 #include "memdebug.h"
79 
80 void Curl_version_init(void);
81 
82 /* win32_cleanup() is for win32 socket cleanup functionality, the opposite
83  of win32_init() */
84 static void win32_cleanup(void)
85 {
86 #ifdef USE_WINSOCK
87  WSACleanup();
88 #endif
89 #ifdef USE_WINDOWS_SSPI
90  Curl_sspi_global_cleanup();
91 #endif
92 }
93 
94 /* win32_init() performs win32 socket initialization to properly setup the
95  stack to allow networking */
96 static CURLcode win32_init(void)
97 {
98 #ifdef USE_WINSOCK
99  WORD wVersionRequested;
100  WSADATA wsaData;
101  int res;
102 
103 #if defined(ENABLE_IPV6) && (USE_WINSOCK < 2)
104  Error IPV6_requires_winsock2
105 #endif
106 
107  wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
108 
109  res = WSAStartup(wVersionRequested, &wsaData);
110 
111  if(res != 0)
112  /* Tell the user that we couldn't find a useable */
113  /* winsock.dll. */
114  return CURLE_FAILED_INIT;
115 
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. */
121 
122  if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
123  HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) {
124  /* Tell the user that we couldn't find a useable */
125 
126  /* winsock.dll. */
127  WSACleanup();
128  return CURLE_FAILED_INIT;
129  }
130  /* The Windows Sockets DLL is acceptable. Proceed. */
131 #elif defined(USE_LWIPSOCK)
132  lwip_init();
133 #endif
134 
135 #ifdef USE_WINDOWS_SSPI
136  {
137  CURLcode result = Curl_sspi_global_init();
138  if(result)
139  return result;
140  }
141 #endif
142 
143  return CURLE_OK;
144 }
145 
146 /* true globals -- for curl_global_init() and curl_global_cleanup() */
147 static unsigned int initialized;
148 static long init_flags;
149 
150 /*
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.
154  */
155 #if defined(_WIN32_WCE)
156 #define system_strdup _strdup
157 #elif !defined(HAVE_STRDUP)
158 #define system_strdup curlx_strdup
159 #else
160 #define system_strdup strdup
161 #endif
162 
163 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
164 # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
165 #endif
166 
167 #ifndef __SYMBIAN32__
168 /*
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.
171  */
177 #if defined(WIN32) && defined(UNICODE)
178 curl_wcsdup_callback Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
179 #endif
180 #else
181 /*
182  * Symbian OS doesn't support initialization to code in writable static data.
183  * Initialization will occur in the curl_global_init() call.
184  */
190 #endif
191 
192 #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
193 # pragma warning(default:4232) /* MSVC extension, dllimport identity */
194 #endif
195 
200 static CURLcode global_init(long flags, bool memoryfuncs)
201 {
202  if(initialized++)
203  return CURLE_OK;
204 
205  if(memoryfuncs) {
206  /* Setup the default memory functions here (again) */
212 #if defined(WIN32) && defined(UNICODE)
213  Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
214 #endif
215  }
216 
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;
221  }
222 
223  if(flags & CURL_GLOBAL_WIN32)
224  if(win32_init()) {
225  DEBUGF(fprintf(stderr, "Error: win32_init failed\n"));
226  return CURLE_FAILED_INIT;
227  }
228 
229 #ifdef __AMIGA__
230  if(!Curl_amiga_init()) {
231  DEBUGF(fprintf(stderr, "Error: Curl_amiga_init failed\n"));
232  return CURLE_FAILED_INIT;
233  }
234 #endif
235 
236 #ifdef NETWARE
237  if(netware_init()) {
238  DEBUGF(fprintf(stderr, "Warning: LONG namespace not available\n"));
239  }
240 #endif
241 
243  DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n"));
244  return CURLE_FAILED_INIT;
245  }
246 
247  (void)Curl_ipv6works();
248 
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;
253  }
254 #endif
255 
256  if(flags & CURL_GLOBAL_ACK_EINTR)
257  Curl_ack_eintr = 1;
258 
259  init_flags = flags;
260 
262 
263  return CURLE_OK;
264 }
265 
266 
272 {
273  return global_init(flags, TRUE);
274 }
275 
276 /*
277  * curl_global_init_mem() globally initializes curl and also registers the
278  * user provided callback routines.
279  */
283 {
284  /* Invalid input, return immediately */
285  if(!m || !f || !r || !s || !c)
286  return CURLE_FAILED_INIT;
287 
288  if(initialized) {
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
291  calls. */
292  initialized++;
293  return CURLE_OK;
294  }
295 
296  /* set memory functions before global_init() in case it wants memory
297  functions */
298  Curl_cmalloc = m;
299  Curl_cfree = f;
300  Curl_cstrdup = s;
301  Curl_crealloc = r;
302  Curl_ccalloc = c;
303 
304  /* Call the actual init function, but without setting */
305  return global_init(flags, FALSE);
306 }
307 
313 {
314  if(!initialized)
315  return;
316 
317  if(--initialized)
318  return;
319 
321 
324 
326 
328  win32_cleanup();
329 
331 
332 #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_EXIT)
333  (void)libssh2_exit();
334 #endif
335 
336  init_flags = 0;
337 }
338 
339 /*
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.
342  */
344 {
346  struct Curl_easy *data;
347 
348  /* Make sure we inited the global SSL stuff */
349  if(!initialized) {
351  if(result) {
352  /* something in the global init failed, return nothing */
353  DEBUGF(fprintf(stderr, "Error: curl_global_init failed\n"));
354  return NULL;
355  }
356  }
357 
358  /* We use curl_open() with undefined URL so far */
359  result = Curl_open(&data);
360  if(result) {
361  DEBUGF(fprintf(stderr, "Error: Curl_open failed\n"));
362  return NULL;
363  }
364 
365  return data;
366 }
367 
368 /*
369  * curl_easy_setopt() is the external interface for setting options on an
370  * easy handle.
371  */
372 
373 #undef curl_easy_setopt
375 {
376  va_list arg;
378 
379  if(!data)
381 
382  va_start(arg, tag);
383 
384  result = Curl_setopt(data, tag, arg);
385 
386  va_end(arg);
387  return result;
388 }
389 
390 #ifdef CURLDEBUG
391 
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 */
395 };
396 
397 struct events {
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 */
403 };
404 
405 /* events_timer
406  *
407  * Callback that gets called with a new value when the timeout should be
408  * updated.
409  */
410 
411 static int events_timer(struct Curl_multi *multi, /* multi handle */
412  long timeout_ms, /* see above */
413  void *userp) /* private callback pointer */
414 {
415  struct events *ev = userp;
416  (void)multi;
417  if(timeout_ms == -1)
418  /* timeout removed */
419  timeout_ms = 0;
420  else if(timeout_ms == 0)
421  /* timeout is already reached! */
422  timeout_ms = 1; /* trigger asap */
423 
424  ev->ms = timeout_ms;
425  ev->msbump = TRUE;
426  return 0;
427 }
428 
429 
430 /* poll2cselect
431  *
432  * convert from poll() bit definitions to libcurl's CURL_CSELECT_* ones
433  */
434 static int poll2cselect(int pollmask)
435 {
436  int omask = 0;
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;
443  return omask;
444 }
445 
446 
447 /* socketcb2poll
448  *
449  * convert from libcurl' CURL_POLL_* bit definitions to poll()'s
450  */
451 static short socketcb2poll(int pollmask)
452 {
453  short omask = 0;
454  if(pollmask & CURL_POLL_IN)
455  omask |= POLLIN;
456  if(pollmask & CURL_POLL_OUT)
457  omask |= POLLOUT;
458  return omask;
459 }
460 
461 /* events_socket
462  *
463  * Callback that gets called with information about socket activity to
464  * monitor.
465  */
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
470  pointer */
471  void *socketp) /* private socket
472  pointer */
473 {
474  struct events *ev = userp;
475  struct socketmonitor *m;
476  struct socketmonitor *prev = NULL;
477 
478 #if defined(CURL_DISABLE_VERBOSE_STRINGS)
479  (void) easy;
480 #endif
481  (void)socketp;
482 
483  m = ev->list;
484  while(m) {
485  if(m->socket.fd == s) {
486 
487  if(what == CURL_POLL_REMOVE) {
488  struct socketmonitor *nxt = m->next;
489  /* remove this node from the list of monitored sockets */
490  if(prev)
491  prev->next = nxt;
492  else
493  ev->list = nxt;
494  free(m);
495  m = nxt;
496  infof(easy, "socket cb: socket %d REMOVED\n", s);
497  }
498  else {
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":"");
505  }
506  break;
507  }
508  prev = m;
509  m = m->next; /* move to next node */
510  }
511  if(!m) {
512  if(what == CURL_POLL_REMOVE) {
513  /* this happens a bit too often, libcurl fix perhaps? */
514  /* fprintf(stderr,
515  "%s: socket %d asked to be REMOVED but not present!\n",
516  __func__, s); */
517  }
518  else {
519  m = malloc(sizeof(struct socketmonitor));
520  if(m) {
521  m->next = ev->list;
522  m->socket.fd = s;
523  m->socket.events = socketcb2poll(what);
524  m->socket.revents = 0;
525  ev->list = m;
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":"");
529  }
530  else
531  return CURLE_OUT_OF_MEMORY;
532  }
533  }
534 
535  return 0;
536 }
537 
538 
539 /*
540  * events_setup()
541  *
542  * Do the multi handle setups that only event-based transfers need.
543  */
544 static void events_setup(struct Curl_multi *multi, struct events *ev)
545 {
546  /* timer callback */
547  curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, events_timer);
548  curl_multi_setopt(multi, CURLMOPT_TIMERDATA, ev);
549 
550  /* socket callback */
551  curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, events_socket);
552  curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev);
553 }
554 
555 
556 /* wait_or_timeout()
557  *
558  * waits for activity on any of the given sockets, or the timeout to trigger.
559  */
560 
561 static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
562 {
563  bool done = FALSE;
564  CURLMcode mcode = CURLM_OK;
566 
567  while(!done) {
568  CURLMsg *msg;
569  struct socketmonitor *m;
570  struct pollfd *f;
571  struct pollfd fds[4];
572  int numfds = 0;
573  int pollrc;
574  int i;
575  struct curltime before;
576  struct curltime after;
577 
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;
582  f->revents = 0;
583  /* fprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd); */
584  f++;
585  numfds++;
586  }
587 
588  /* get the time stamp to use to figure out how long poll takes */
589  before = curlx_tvnow();
590 
591  /* wait for activity or timeout */
592  pollrc = Curl_poll(fds, numfds, (int)ev->ms);
593 
594  after = curlx_tvnow();
595 
596  ev->msbump = FALSE; /* reset here */
597 
598  if(0 == pollrc) {
599  /* timeout! */
600  ev->ms = 0;
601  /* fprintf(stderr, "call curl_multi_socket_action(TIMEOUT)\n"); */
603  &ev->running_handles);
604  }
605  else if(pollrc > 0) {
606  /* loop over the monitored sockets to see which ones had activity */
607  for(i = 0; i< numfds; i++) {
608  if(fds[i].revents) {
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",
612  fds[i].fd);
613  mcode = curl_multi_socket_action(multi, fds[i].fd, act,
614  &ev->running_handles);
615  }
616  }
617 
618  if(!ev->msbump) {
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.
621  */
622  time_t timediff = curlx_tvdiff(after, before);
623  if(timediff > 0) {
624  if(timediff > ev->ms)
625  ev->ms = 0;
626  else
627  ev->ms -= (long)timediff;
628  }
629  }
630  }
631  else
632  return CURLE_RECV_ERROR;
633 
634  if(mcode)
635  return CURLE_URL_MALFORMAT; /* TODO: return a proper error! */
636 
637  /* we don't really care about the "msgs_in_queue" value returned in the
638  second argument */
639  msg = curl_multi_info_read(multi, &pollrc);
640  if(msg) {
641  result = msg->data.result;
642  done = TRUE;
643  }
644  }
645 
646  return result;
647 }
648 
649 
650 /* easy_events()
651  *
652  * Runs a transfer in a blocking manner using the events-based API
653  */
654 static CURLcode easy_events(struct Curl_multi *multi)
655 {
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};
659 
660  /* if running event-based, do some further multi inits */
661  events_setup(multi, &evs);
662 
663  return wait_or_timeout(multi, &evs);
664 }
665 #else /* CURLDEBUG */
666 /* when not built with debug, this function doesn't exist */
667 #define easy_events(x) CURLE_NOT_BUILT_IN
668 #endif
669 
670 static CURLcode easy_transfer(struct Curl_multi *multi)
671 {
672  bool done = FALSE;
673  CURLMcode mcode = CURLM_OK;
675  struct curltime before;
676  int without_fds = 0; /* count number of consecutive returns from
677  curl_multi_wait() without any filedescriptors */
678 
679  while(!done && !mcode) {
680  int still_running = 0;
681  int rc;
682 
683  before = curlx_tvnow();
684  mcode = curl_multi_wait(multi, NULL, 0, 1000, &rc);
685 
686  if(!mcode) {
687  if(!rc) {
688  struct curltime after = curlx_tvnow();
689 
690  /* If it returns without any filedescriptor instantly, we need to
691  avoid busy-looping during periods where it has nothing particular
692  to wait for */
693  if(curlx_tvdiff(after, before) <= 10) {
694  without_fds++;
695  if(without_fds > 2) {
696  int sleep_ms = without_fds < 10 ? (1 << (without_fds - 1)) : 1000;
697  Curl_wait_ms(sleep_ms);
698  }
699  }
700  else
701  /* it wasn't "instant", restart counter */
702  without_fds = 0;
703  }
704  else
705  /* got file descriptor, restart counter */
706  without_fds = 0;
707 
708  mcode = curl_multi_perform(multi, &still_running);
709  }
710 
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);
714  if(msg) {
715  result = msg->data.result;
716  done = TRUE;
717  }
718  }
719  }
720 
721  /* Make sure to return some kind of error if there was a multi problem */
722  if(mcode) {
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 */
727  }
728 
729  return result;
730 }
731 
732 
733 /*
734  * easy_perform() is the external interface that performs a blocking
735  * transfer as previously setup.
736  *
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
740  * code.
741  *
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.
746  *
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().
749  */
750 static CURLcode easy_perform(struct Curl_easy *data, bool events)
751 {
752  struct Curl_multi *multi;
753  CURLMcode mcode;
755  SIGPIPE_VARIABLE(pipe_st);
756 
757  if(!data)
759 
760  if(data->multi) {
761  failf(data, "easy handle already used in multi handle");
762  return CURLE_FAILED_INIT;
763  }
764 
765  if(data->multi_easy)
766  multi = data->multi_easy;
767  else {
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);
771  if(!multi)
772  return CURLE_OUT_OF_MEMORY;
773  data->multi_easy = multi;
774  }
775 
776  /* Copy the MAXCONNECTS option to the multi handle */
777  curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, data->set.maxconnects);
778 
779  mcode = curl_multi_add_handle(multi, data);
780  if(mcode) {
781  curl_multi_cleanup(multi);
782  if(mcode == CURLM_OUT_OF_MEMORY)
783  return CURLE_OUT_OF_MEMORY;
784  return CURLE_FAILED_INIT;
785  }
786 
787  sigpipe_ignore(data, &pipe_st);
788 
789  /* assign this after curl_multi_add_handle() since that function checks for
790  it and rejects this handle otherwise */
791  data->multi = multi;
792 
793  /* run the transfer */
794  result = events ? easy_events(multi) : easy_transfer(multi);
795 
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);
799 
800  sigpipe_restore(&pipe_st);
801 
802  /* The multi handle is kept alive, owned by the easy handle */
803  return result;
804 }
805 
806 
807 /*
808  * curl_easy_perform() is the external interface that performs a blocking
809  * transfer as previously setup.
810  */
812 {
813  return easy_perform(data, FALSE);
814 }
815 
816 #ifdef CURLDEBUG
817 /*
818  * curl_easy_perform_ev() is the external interface that performs a blocking
819  * transfer using the event-based API internally.
820  */
821 CURLcode curl_easy_perform_ev(struct Curl_easy *data)
822 {
823  return easy_perform(data, TRUE);
824 }
825 
826 #endif
827 
828 /*
829  * curl_easy_cleanup() is the external interface to cleaning/freeing the given
830  * easy handle.
831  */
832 void curl_easy_cleanup(struct Curl_easy *data)
833 {
834  SIGPIPE_VARIABLE(pipe_st);
835 
836  if(!data)
837  return;
838 
839  sigpipe_ignore(data, &pipe_st);
840  Curl_close(data);
841  sigpipe_restore(&pipe_st);
842 }
843 
844 /*
845  * curl_easy_getinfo() is an external interface that allows an app to retrieve
846  * information from a performed transfer and similar.
847  */
848 #undef curl_easy_getinfo
850 {
851  va_list arg;
852  void *paramp;
854 
855  va_start(arg, info);
856  paramp = va_arg(arg, void *);
857 
858  result = Curl_getinfo(data, info, paramp);
859 
860  va_end(arg);
861  return result;
862 }
863 
864 /*
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.
868  */
870 {
871  struct Curl_easy *outcurl = calloc(1, sizeof(struct Curl_easy));
872  if(NULL == outcurl)
873  goto fail;
874 
875  /*
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.
879  */
880  outcurl->set.buffer_size = data->set.buffer_size;
881  outcurl->state.buffer = malloc(outcurl->set.buffer_size + 1);
882  if(!outcurl->state.buffer)
883  goto fail;
884 
885  outcurl->state.headerbuff = malloc(HEADERSIZE);
886  if(!outcurl->state.headerbuff)
887  goto fail;
888  outcurl->state.headersize = HEADERSIZE;
889 
890  /* copy all userdefined values */
891  if(Curl_dupset(outcurl, data))
892  goto fail;
893 
894  /* the connection cache is setup on demand */
895  outcurl->state.conn_cache = NULL;
896 
897  outcurl->state.lastconnect = NULL;
898 
899  outcurl->progress.flags = data->progress.flags;
900  outcurl->progress.callback = data->progress.callback;
901 
902  if(data->cookies) {
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,
907  outcurl->cookies,
908  data->set.cookiesession);
909  if(!outcurl->cookies)
910  goto fail;
911  }
912 
913  /* duplicate all values in 'change' */
914  if(data->change.cookielist) {
915  outcurl->change.cookielist =
917  if(!outcurl->change.cookielist)
918  goto fail;
919  }
920 
921  if(data->change.url) {
922  outcurl->change.url = strdup(data->change.url);
923  if(!outcurl->change.url)
924  goto fail;
925  outcurl->change.url_alloc = TRUE;
926  }
927 
928  if(data->change.referer) {
929  outcurl->change.referer = strdup(data->change.referer);
930  if(!outcurl->change.referer)
931  goto fail;
932  outcurl->change.referer_alloc = TRUE;
933  }
934 
935  /* Clone the resolver handle, if present, for the new handle */
937  data->state.resolver))
938  goto fail;
939 
940  Curl_convert_setup(outcurl);
941 
942  Curl_initinfo(outcurl);
943 
944  outcurl->magic = CURLEASY_MAGIC_NUMBER;
945 
946  /* we reach this point and thus we are OK */
947 
948  return outcurl;
949 
950  fail:
951 
952  if(outcurl) {
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);
960  free(outcurl);
961  }
962 
963  return NULL;
964 }
965 
966 /*
967  * curl_easy_reset() is an external interface that allows an app to re-
968  * initialize a session handle to the default values.
969  */
970 void curl_easy_reset(struct Curl_easy *data)
971 {
973 
974  data->state.path = NULL;
975 
977 
978  /* zero out UserDefined data: */
979  Curl_freeset(data);
980  memset(&data->set, 0, sizeof(struct UserDefined));
981  (void)Curl_init_userdefined(&data->set);
982 
983  /* zero out Progress data: */
984  memset(&data->progress, 0, sizeof(struct Progress));
985 
986  /* zero out PureInfo data: */
987  Curl_initinfo(data);
988 
989  data->progress.flags |= PGRS_HIDE;
990  data->state.current_speed = -1; /* init to negative == impossible */
991 
992  /* zero out authentication data: */
993  memset(&data->state.authhost, 0, sizeof(struct auth));
994  memset(&data->state.authproxy, 0, sizeof(struct auth));
995 }
996 
997 /*
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.
1001  *
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.
1004  *
1005  * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
1006  */
1007 CURLcode curl_easy_pause(struct Curl_easy *data, int action)
1008 {
1009  struct SingleRequest *k = &data->req;
1011 
1012  /* first switch off both pause bits */
1013  int newstate = k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
1014 
1015  /* set the new desired pause bits */
1016  newstate |= ((action & CURLPAUSE_RECV)?KEEP_RECV_PAUSE:0) |
1017  ((action & CURLPAUSE_SEND)?KEEP_SEND_PAUSE:0);
1018 
1019  /* put it back in the keepon */
1020  k->keepon = newstate;
1021 
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! */
1025  unsigned int i;
1026  unsigned int count = data->state.tempcount;
1027  struct tempbuf writebuf[3]; /* there can only be three */
1028 
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;
1033  }
1034  data->state.tempcount = 0;
1035 
1036  for(i = 0; i < count; i++) {
1037  /* even if one function returns error, this loops through and frees all
1038  buffers */
1039  if(!result)
1040  result = Curl_client_chop_write(data->easy_conn,
1041  writebuf[i].type,
1042  writebuf[i].buf,
1043  writebuf[i].len);
1044  free(writebuf[i].buf);
1045  }
1046  if(result)
1047  return result;
1048  }
1049 
1050  /* if there's no error and we're not pausing both directions, we want
1051  to have this handle checked soon */
1052  if(!result &&
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 */
1056 
1057  return result;
1058 }
1059 
1060 
1061 static CURLcode easy_connection(struct Curl_easy *data,
1062  curl_socket_t *sfd,
1063  struct connectdata **connp)
1064 {
1065  if(data == NULL)
1067 
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!");
1072  }
1073 
1074  *sfd = Curl_getconnectinfo(data, connp);
1075 
1076  if(*sfd == CURL_SOCKET_BAD) {
1077  failf(data, "Failed to get recent socket");
1079  }
1080 
1081  return CURLE_OK;
1082 }
1083 
1084 /*
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.
1088  */
1089 CURLcode curl_easy_recv(struct Curl_easy *data, void *buffer, size_t buflen,
1090  size_t *n)
1091 {
1092  curl_socket_t sfd;
1093  CURLcode result;
1094  ssize_t n1;
1095  struct connectdata *c;
1096 
1097  result = easy_connection(data, &sfd, &c);
1098  if(result)
1099  return result;
1100 
1101  *n = 0;
1102  result = Curl_read(c, sfd, buffer, buflen, &n1);
1103 
1104  if(result)
1105  return result;
1106 
1107  *n = (size_t)n1;
1108 
1109  return CURLE_OK;
1110 }
1111 
1112 /*
1113  * Sends data over the connected socket. Use after successful
1114  * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
1115  */
1116 CURLcode curl_easy_send(struct Curl_easy *data, const void *buffer,
1117  size_t buflen, size_t *n)
1118 {
1119  curl_socket_t sfd;
1120  CURLcode result;
1121  ssize_t n1;
1122  struct connectdata *c = NULL;
1123 
1124  result = easy_connection(data, &sfd, &c);
1125  if(result)
1126  return result;
1127 
1128  *n = 0;
1129  result = Curl_write(c, sfd, buffer, buflen, &n1);
1130 
1131  if(n1 == -1)
1132  return CURLE_SEND_ERROR;
1133 
1134  /* detect EAGAIN */
1135  if(!result && !n1)
1136  return CURLE_AGAIN;
1137 
1138  *n = (size_t)n1;
1139 
1140  return result;
1141 }
#define free(ptr)
Definition: curl_memory.h:130
struct Curl_multi * Curl_multi_handle(int hashsize, int chashsize)
Definition: multi.c:305
Error
struct connectdata * lastconnect
Definition: urldata.h:1248
void *(* curl_calloc_callback)(size_t nmemb, size_t size)
Definition: curl.h:423
CURLcode Curl_open(struct Curl_easy **curl)
Curl_open()
Definition: url.c:644
int Curl_ack_eintr
Definition: select.c:56
static long init_flags
Definition: easy.c:148
struct UserDefined set
Definition: urldata.h:1762
CURLcode Curl_client_chop_write(struct connectdata *conn, int type, char *ptr, size_t len)
Definition: sendf.c:539
void curl_easy_reset(struct Curl_easy *data)
Definition: easy.c:970
CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle, CURL *curl_handle)
static CURLcode easy_connection(struct Curl_easy *data, curl_socket_t *sfd, struct connectdata **connp)
Definition: easy.c:1061
#define CURL_CSELECT_OUT
Definition: multi.h:265
char * url
Definition: urldata.h:1372
struct Curl_multi * multi_easy
Definition: urldata.h:1757
f
#define CURL_SOCKET_BAD
Definition: curl.h:131
#define sigpipe_ignore(x, y)
Definition: sigpipe.h:73
struct curltime curlx_tvnow(void)
Definition: timeval.c:106
#define failf
Definition: sendf.h:48
void *(* curl_realloc_callback)(void *ptr, size_t size)
Definition: curl.h:421
#define easy_events(x)
Definition: easy.c:667
#define strdup(ptr)
Definition: curl_memory.h:122
XmlRpcServer s
CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles)
struct Curl_multi * multi
Definition: urldata.h:1754
CURLcode
Definition: curl.h:454
static CURLcode global_init(long flags, bool memoryfuncs)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:200
bool referer_alloc
Definition: urldata.h:1375
Definition: multi.h:93
CURLcode Curl_initinfo(struct Curl_easy *data)
Definition: lib/getinfo.c:45
CURLcode curl_easy_getinfo(struct Curl_easy *data, CURLINFO info,...)
Definition: easy.c:849
CURLINFO
Definition: curl.h:2439
struct Curl_easy * curl_easy_duphandle(struct Curl_easy *data)
Definition: easy.c:869
void Curl_version_init(void)
Definition: version.c:71
static unsigned int initialized
Definition: easy.c:147
#define CURLEASY_MAGIC_NUMBER
Definition: urldata.h:146
#define POLLOUT
Definition: select.h:44
static int res
static void win32_cleanup(void)
Definition: easy.c:84
#define realloc(ptr, size)
Definition: curl_memory.h:128
#define CURLPAUSE_SEND
Definition: curl.h:2708
CURLcode curl_global_init(long flags)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:271
#define malloc(size)
Definition: curl_memory.h:124
void curl_easy_cleanup(struct Curl_easy *data)
Definition: easy.c:832
#define CURL_POLL_OUT
Definition: multi.h:258
short events
Definition: select.h:52
bool connect_only
Definition: urldata.h:1653
#define Curl_amiga_init()
Definition: amigaos.h:33
UNITTEST_START int result
Definition: unit1304.c:49
static CURLcode easy_transfer(struct Curl_multi *multi)
Definition: easy.c:670
char buffer[]
Definition: unit1308.c:48
#define KEEP_SEND_PAUSE
Definition: urldata.h:460
unsigned int i
Definition: unit1303.c:79
int flags
Definition: urldata.h:1104
curl_socket_t fd
Definition: select.h:51
struct DynamicStatic change
Definition: urldata.h:1763
static size_t num_sockets
Definition: sws.c:130
CURLcode curl_global_init_mem(long flags, curl_malloc_callback m, curl_free_callback f, curl_realloc_callback r, curl_strdup_callback s, curl_calloc_callback c)
Definition: easy.c:280
#define HEADERSIZE
Definition: urldata.h:144
void Curl_global_host_cache_dtor(void)
Definition: hostip.c:137
struct connectdata * easy_conn
Definition: urldata.h:1737
Definition: urldata.h:1179
curl_socket_t Curl_getconnectinfo(struct Curl_easy *data, struct connectdata **connp)
Definition: connect.c:1210
#define CURL_CSELECT_ERR
Definition: multi.h:266
#define sigpipe_restore(x)
Definition: sigpipe.h:74
curl_strdup_callback Curl_cstrdup
Definition: easy.c:175
int Curl_wait_ms(int timeout_ms)
Definition: select.c:75
bool cookiesession
Definition: urldata.h:1565
#define FALSE
char * buffer
Definition: urldata.h:1253
size_t len
Definition: urldata.h:1206
curl_easy_setopt expects a curl_off_t argument for this option curl_easy_setopt expects a curl_write_callback argument for this option curl_easy_setopt expects a curl_ioctl_callback argument for this option curl_easy_setopt expects a curl_opensocket_callback argument for this option curl_easy_setopt expects a curl_debug_callback argument for this option curl_easy_setopt expects a curl_conv_callback argument for this option curl_easy_setopt expects a private data pointer as argument for this option curl_easy_setopt expects a FILE *argument for this option curl_easy_setopt expects a struct curl_httppost *argument for this option curl_easy_setopt expects a struct curl_slist *argument for this option curl_easy_getinfo expects a pointer to char *for this info curl_easy_getinfo expects a pointer to double for this info curl_easy_getinfo expects a pointer to struct curl_tlssessioninfo *for this info curl_easy_getinfo expects a pointer to curl_socket_t for this info size_t
char * referer
Definition: urldata.h:1374
struct SingleRequest req
Definition: urldata.h:1761
#define curl_multi_setopt(handle, opt, param)
#define CURL_CSELECT_IN
Definition: multi.h:264
UNITTEST_START int rc
Definition: unit1301.c:31
#define CURL_POLL_REMOVE
Definition: multi.h:260
char * buf
Definition: urldata.h:1204
bool url_alloc
Definition: urldata.h:1373
#define CURL_GLOBAL_DEFAULT
Definition: curl.h:2521
#define fail(msg)
Definition: curlcheck.h:51
curl_free_callback Curl_cfree
Definition: easy.c:173
void *(* curl_malloc_callback)(size_t size)
Definition: curl.h:419
#define CURL_POLL_IN
Definition: multi.h:257
void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
UNITTEST_START struct Curl_easy data
Definition: unit1399.c:82
curl_malloc_callback Curl_cmalloc
Definition: easy.c:172
static CURLcode easy_perform(struct Curl_easy *data, bool events)
Definition: easy.c:750
Definition: select.h:49
struct tempbuf tempwrite[3]
Definition: urldata.h:1269
#define SIGPIPE_VARIABLE(x)
Definition: sigpipe.h:75
CURLcode curl_easy_send(struct Curl_easy *data, const void *buffer, size_t buflen, size_t *n)
Definition: easy.c:1116
#define Curl_amiga_cleanup()
Definition: amigaos.h:34
#define POLLERR
Definition: select.h:45
struct CookieInfo * cookies
Definition: urldata.h:1764
char * headerbuff
Definition: urldata.h:1250
struct Progress progress
Definition: urldata.h:1768
bool callback
Definition: urldata.h:1102
CURLcode Curl_write(struct connectdata *conn, curl_socket_t sockfd, const void *mem, size_t len, ssize_t *written)
Definition: sendf.c:318
char * pathbuffer
Definition: urldata.h:1328
char * path
Definition: urldata.h:1329
Definition: curl.h:455
#define Curl_resolver_global_init()
Definition: asyn.h:155
struct curl_slist * cookielist
Definition: urldata.h:1376
CURLcode Curl_init_userdefined(struct UserDefined *set)
Definition: url.c:507
#define CURL_GLOBAL_WIN32
Definition: curl.h:2518
#define Curl_safefree(ptr)
Definition: memdebug.h:170
#define Curl_ssl_cleanup()
Definition: vtls.h:252
struct UrlState state
Definition: urldata.h:1769
struct Curl_easy * easyp
Definition: multihandle.h:77
unsigned int magic
Definition: urldata.h:1780
CURLoption
Definition: curl.h:910
CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle, CURL *curl_handle)
CURLcode curl_easy_perform(struct Curl_easy *data)
Definition: easy.c:811
#define KEEP_RECV_PAUSE
Definition: urldata.h:459
int type
Definition: urldata.h:1207
CURLMcode
Definition: multi.h:61
time_t curlx_tvdiff(struct curltime newer, struct curltime older)
Definition: timeval.c:128
#define Curl_ipv6works()
Definition: hostip.h:98
#define CURL_SOCKET_TIMEOUT
Definition: multi.h:262
static CURLcode win32_init(void)
Definition: easy.c:96
#define ssize_t
Definition: config-win32.h:382
#define Curl_ssl_init()
Definition: vtls.h:251
void Curl_free_request_state(struct Curl_easy *data)
Definition: url.c:4923
struct auth authhost
Definition: urldata.h:1289
void Curl_freeset(struct Curl_easy *data)
Definition: url.c:284
CURLcode curl_easy_pause(struct Curl_easy *data, int action)
Definition: easy.c:1007
#define POLLIN
Definition: select.h:42
CURL_EXTERN CURLMcode curl_multi_wait(CURLM *multi_handle, struct curl_waitfd extra_fds[], unsigned int extra_nfds, int timeout_ms, int *ret)
CURLcode Curl_dupset(struct Curl_easy *dst, struct Curl_easy *src)
Definition: url.c:362
char buf[3]
Definition: unit1398.c:32
CURL_EXTERN CURLMsg * curl_multi_info_read(CURLM *multi_handle, int *msgs_in_queue)
CURLcode curl_easy_recv(struct Curl_easy *data, void *buffer, size_t buflen, size_t *n)
Definition: easy.c:1089
#define infof
Definition: sendf.h:44
struct Curl_easy * curl_easy_init(void)
Definition: easy.c:343
short revents
Definition: select.h:53
int Curl_resolver_duphandle(void **to, void *from)
unsigned int tempcount
Definition: urldata.h:1268
void * resolver
Definition: urldata.h:1294
CURLcode Curl_setopt(struct Curl_easy *data, CURLoption option, va_list param)
Definition: url.c:718
Definition: multi.h:64
struct curl_slist * Curl_slist_duplicate(struct curl_slist *inlist)
Definition: slist.c:109
union CURLMsg::@6 data
curl_calloc_callback Curl_ccalloc
Definition: easy.c:176
#define CURL_GLOBAL_ACK_EINTR
Definition: curl.h:2522
#define Curl_resolver_global_cleanup()
Definition: asyn.h:156
int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms)
Definition: select.c:391
char * filename
Definition: cookie.h:52
struct auth authproxy
Definition: urldata.h:1290
#define fprintf
Definition: curl_printf.h:41
static CURL * easy[MAX_EASY_HANDLES]
void Curl_expire(struct Curl_easy *data, time_t milli, expire_id id)
Definition: multi.c:2930
size_t headersize
Definition: urldata.h:1251
#define TRUE
CURLcode Curl_close(struct Curl_easy *data)
Definition: url.c:405
char *(* curl_strdup_callback)(const char *str)
Definition: curl.h:422
#define CURL_GLOBAL_SSL
Definition: curl.h:2517
void(* curl_free_callback)(void *ptr)
Definition: curl.h:420
#define system_strdup
Definition: easy.c:158
CURLcode result
Definition: multi.h:98
int curl_socket_t
Definition: curl.h:130
#define PGRS_HIDE
Definition: progress.h:69
CURL_EXTERN void curl_slist_free_all(struct curl_slist *)
Definition: slist.c:129
long buffer_size
Definition: urldata.h:1591
struct conncache * conn_cache
Definition: urldata.h:1238
curl_realloc_callback Curl_crealloc
Definition: easy.c:174
#define CURLPAUSE_RECV
Definition: curl.h:2705
CURL_EXTERN CURLMcode curl_multi_socket_action(CURLM *multi_handle, curl_socket_t s, int ev_bitmask, int *running_handles)
Definition: debug.c:29
CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle)
CURLcode curl_easy_setopt(struct Curl_easy *data, CURLoption tag,...)
Definition: easy.c:374
#define calloc(nbelem, size)
Definition: curl_memory.h:126
CURLcode Curl_getinfo(struct Curl_easy *data, CURLINFO info,...)
Definition: lib/getinfo.c:405
CURLcode Curl_read(struct connectdata *conn, curl_socket_t sockfd, char *buf, size_t sizerequested, ssize_t *n)
Definition: sendf.c:686
size_t maxconnects
Definition: urldata.h:1692
curl_off_t current_speed
Definition: urldata.h:1255
#define DEBUGF(x)
#define Curl_convert_setup(x)
Definition: non-ascii.h:54


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:09