easy.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, Daniel Stenberg, <[email protected]>, 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. #include "curl_setup.h"
  23. /*
  24. * See comment in curl_memory.h for the explanation of this sanity check.
  25. */
  26. #ifdef CURLX_NO_MEMORY_CALLBACKS
  27. #error "libcurl shall not ever be built with CURLX_NO_MEMORY_CALLBACKS defined"
  28. #endif
  29. #ifdef HAVE_NETINET_IN_H
  30. #include <netinet/in.h>
  31. #endif
  32. #ifdef HAVE_NETDB_H
  33. #include <netdb.h>
  34. #endif
  35. #ifdef HAVE_ARPA_INET_H
  36. #include <arpa/inet.h>
  37. #endif
  38. #ifdef HAVE_NET_IF_H
  39. #include <net/if.h>
  40. #endif
  41. #ifdef HAVE_SYS_IOCTL_H
  42. #include <sys/ioctl.h>
  43. #endif
  44. #ifdef HAVE_SYS_PARAM_H
  45. #include <sys/param.h>
  46. #endif
  47. #include "urldata.h"
  48. #include <curl/curl.h>
  49. #include "transfer.h"
  50. #include "vtls/vtls.h"
  51. #include "url.h"
  52. #include "getinfo.h"
  53. #include "hostip.h"
  54. #include "share.h"
  55. #include "strdup.h"
  56. #include "progress.h"
  57. #include "easyif.h"
  58. #include "multiif.h"
  59. #include "select.h"
  60. #include "sendf.h" /* for failf function prototype */
  61. #include "connect.h" /* for Curl_getconnectinfo */
  62. #include "slist.h"
  63. #include "mime.h"
  64. #include "amigaos.h"
  65. #include "non-ascii.h"
  66. #include "warnless.h"
  67. #include "multiif.h"
  68. #include "sigpipe.h"
  69. #include "ssh.h"
  70. #include "setopt.h"
  71. #include "http_digest.h"
  72. /* The last 3 #include files should be in this order */
  73. #include "curl_printf.h"
  74. #include "curl_memory.h"
  75. #include "memdebug.h"
  76. void Curl_version_init(void);
  77. /* win32_cleanup() is for win32 socket cleanup functionality, the opposite
  78. of win32_init() */
  79. static void win32_cleanup(void)
  80. {
  81. #ifdef USE_WINSOCK
  82. WSACleanup();
  83. #endif
  84. #ifdef USE_WINDOWS_SSPI
  85. Curl_sspi_global_cleanup();
  86. #endif
  87. }
  88. /* win32_init() performs win32 socket initialization to properly setup the
  89. stack to allow networking */
  90. static CURLcode win32_init(void)
  91. {
  92. #ifdef USE_WINSOCK
  93. WORD wVersionRequested;
  94. WSADATA wsaData;
  95. int res;
  96. #if defined(ENABLE_IPV6) && (USE_WINSOCK < 2)
  97. Error IPV6_requires_winsock2
  98. #endif
  99. wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
  100. res = WSAStartup(wVersionRequested, &wsaData);
  101. if(res != 0)
  102. /* Tell the user that we couldn't find a useable */
  103. /* winsock.dll. */
  104. return CURLE_FAILED_INIT;
  105. /* Confirm that the Windows Sockets DLL supports what we need.*/
  106. /* Note that if the DLL supports versions greater */
  107. /* than wVersionRequested, it will still return */
  108. /* wVersionRequested in wVersion. wHighVersion contains the */
  109. /* highest supported version. */
  110. if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
  111. HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) {
  112. /* Tell the user that we couldn't find a useable */
  113. /* winsock.dll. */
  114. WSACleanup();
  115. return CURLE_FAILED_INIT;
  116. }
  117. /* The Windows Sockets DLL is acceptable. Proceed. */
  118. #elif defined(USE_LWIPSOCK)
  119. lwip_init();
  120. #endif
  121. #ifdef USE_WINDOWS_SSPI
  122. {
  123. CURLcode result = Curl_sspi_global_init();
  124. if(result)
  125. return result;
  126. }
  127. #endif
  128. return CURLE_OK;
  129. }
  130. /* true globals -- for curl_global_init() and curl_global_cleanup() */
  131. static unsigned int initialized;
  132. static long init_flags;
  133. /*
  134. * strdup (and other memory functions) is redefined in complicated
  135. * ways, but at this point it must be defined as the system-supplied strdup
  136. * so the callback pointer is initialized correctly.
  137. */
  138. #if defined(_WIN32_WCE)
  139. #define system_strdup _strdup
  140. #elif !defined(HAVE_STRDUP)
  141. #define system_strdup curlx_strdup
  142. #else
  143. #define system_strdup strdup
  144. #endif
  145. #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
  146. # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
  147. #endif
  148. #ifndef __SYMBIAN32__
  149. /*
  150. * If a memory-using function (like curl_getenv) is used before
  151. * curl_global_init() is called, we need to have these pointers set already.
  152. */
  153. curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
  154. curl_free_callback Curl_cfree = (curl_free_callback)free;
  155. curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
  156. curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
  157. curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
  158. #if defined(WIN32) && defined(UNICODE)
  159. curl_wcsdup_callback Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
  160. #endif
  161. #else
  162. /*
  163. * Symbian OS doesn't support initialization to code in writable static data.
  164. * Initialization will occur in the curl_global_init() call.
  165. */
  166. curl_malloc_callback Curl_cmalloc;
  167. curl_free_callback Curl_cfree;
  168. curl_realloc_callback Curl_crealloc;
  169. curl_strdup_callback Curl_cstrdup;
  170. curl_calloc_callback Curl_ccalloc;
  171. #endif
  172. #if defined(_MSC_VER) && defined(_DLL) && !defined(__POCC__)
  173. # pragma warning(default:4232) /* MSVC extension, dllimport identity */
  174. #endif
  175. /**
  176. * curl_global_init() globally initializes curl given a bitwise set of the
  177. * different features of what to initialize.
  178. */
  179. static CURLcode global_init(long flags, bool memoryfuncs)
  180. {
  181. if(initialized++)
  182. return CURLE_OK;
  183. if(memoryfuncs) {
  184. /* Setup the default memory functions here (again) */
  185. Curl_cmalloc = (curl_malloc_callback)malloc;
  186. Curl_cfree = (curl_free_callback)free;
  187. Curl_crealloc = (curl_realloc_callback)realloc;
  188. Curl_cstrdup = (curl_strdup_callback)system_strdup;
  189. Curl_ccalloc = (curl_calloc_callback)calloc;
  190. #if defined(WIN32) && defined(UNICODE)
  191. Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
  192. #endif
  193. }
  194. if(!Curl_ssl_init()) {
  195. DEBUGF(fprintf(stderr, "Error: Curl_ssl_init failed\n"));
  196. return CURLE_FAILED_INIT;
  197. }
  198. if(flags & CURL_GLOBAL_WIN32)
  199. if(win32_init()) {
  200. DEBUGF(fprintf(stderr, "Error: win32_init failed\n"));
  201. return CURLE_FAILED_INIT;
  202. }
  203. #ifdef __AMIGA__
  204. if(!Curl_amiga_init()) {
  205. DEBUGF(fprintf(stderr, "Error: Curl_amiga_init failed\n"));
  206. return CURLE_FAILED_INIT;
  207. }
  208. #endif
  209. #ifdef NETWARE
  210. if(netware_init()) {
  211. DEBUGF(fprintf(stderr, "Warning: LONG namespace not available\n"));
  212. }
  213. #endif
  214. if(Curl_resolver_global_init()) {
  215. DEBUGF(fprintf(stderr, "Error: resolver_global_init failed\n"));
  216. return CURLE_FAILED_INIT;
  217. }
  218. (void)Curl_ipv6works();
  219. #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_INIT)
  220. if(libssh2_init(0)) {
  221. DEBUGF(fprintf(stderr, "Error: libssh2_init failed\n"));
  222. return CURLE_FAILED_INIT;
  223. }
  224. #endif
  225. #if defined(USE_LIBSSH)
  226. if(ssh_init()) {
  227. DEBUGF(fprintf(stderr, "Error: libssh_init failed\n"));
  228. return CURLE_FAILED_INIT;
  229. }
  230. #endif
  231. if(flags & CURL_GLOBAL_ACK_EINTR)
  232. Curl_ack_eintr = 1;
  233. init_flags = flags;
  234. Curl_version_init();
  235. return CURLE_OK;
  236. }
  237. /**
  238. * curl_global_init() globally initializes curl given a bitwise set of the
  239. * different features of what to initialize.
  240. */
  241. CURLcode curl_global_init(long flags)
  242. {
  243. return global_init(flags, TRUE);
  244. }
  245. /*
  246. * curl_global_init_mem() globally initializes curl and also registers the
  247. * user provided callback routines.
  248. */
  249. CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
  250. curl_free_callback f, curl_realloc_callback r,
  251. curl_strdup_callback s, curl_calloc_callback c)
  252. {
  253. /* Invalid input, return immediately */
  254. if(!m || !f || !r || !s || !c)
  255. return CURLE_FAILED_INIT;
  256. if(initialized) {
  257. /* Already initialized, don't do it again, but bump the variable anyway to
  258. work like curl_global_init() and require the same amount of cleanup
  259. calls. */
  260. initialized++;
  261. return CURLE_OK;
  262. }
  263. /* set memory functions before global_init() in case it wants memory
  264. functions */
  265. Curl_cmalloc = m;
  266. Curl_cfree = f;
  267. Curl_cstrdup = s;
  268. Curl_crealloc = r;
  269. Curl_ccalloc = c;
  270. /* Call the actual init function, but without setting */
  271. return global_init(flags, FALSE);
  272. }
  273. /**
  274. * curl_global_cleanup() globally cleanups curl, uses the value of
  275. * "init_flags" to determine what needs to be cleaned up and what doesn't.
  276. */
  277. void curl_global_cleanup(void)
  278. {
  279. if(!initialized)
  280. return;
  281. if(--initialized)
  282. return;
  283. Curl_global_host_cache_dtor();
  284. Curl_ssl_cleanup();
  285. Curl_resolver_global_cleanup();
  286. if(init_flags & CURL_GLOBAL_WIN32)
  287. win32_cleanup();
  288. Curl_amiga_cleanup();
  289. #if defined(USE_LIBSSH2) && defined(HAVE_LIBSSH2_EXIT)
  290. (void)libssh2_exit();
  291. #endif
  292. #if defined(USE_LIBSSH)
  293. (void)ssh_finalize();
  294. #endif
  295. init_flags = 0;
  296. }
  297. /*
  298. * curl_easy_init() is the external interface to alloc, setup and init an
  299. * easy handle that is returned. If anything goes wrong, NULL is returned.
  300. */
  301. struct Curl_easy *curl_easy_init(void)
  302. {
  303. CURLcode result;
  304. struct Curl_easy *data;
  305. /* Make sure we inited the global SSL stuff */
  306. if(!initialized) {
  307. result = curl_global_init(CURL_GLOBAL_DEFAULT);
  308. if(result) {
  309. /* something in the global init failed, return nothing */
  310. DEBUGF(fprintf(stderr, "Error: curl_global_init failed\n"));
  311. return NULL;
  312. }
  313. }
  314. /* We use curl_open() with undefined URL so far */
  315. result = Curl_open(&data);
  316. if(result) {
  317. DEBUGF(fprintf(stderr, "Error: Curl_open failed\n"));
  318. return NULL;
  319. }
  320. return data;
  321. }
  322. #ifdef CURLDEBUG
  323. struct socketmonitor {
  324. struct socketmonitor *next; /* the next node in the list or NULL */
  325. struct pollfd socket; /* socket info of what to monitor */
  326. };
  327. struct events {
  328. long ms; /* timeout, run the timeout function when reached */
  329. bool msbump; /* set TRUE when timeout is set by callback */
  330. int num_sockets; /* number of nodes in the monitor list */
  331. struct socketmonitor *list; /* list of sockets to monitor */
  332. int running_handles; /* store the returned number */
  333. };
  334. /* events_timer
  335. *
  336. * Callback that gets called with a new value when the timeout should be
  337. * updated.
  338. */
  339. static int events_timer(struct Curl_multi *multi, /* multi handle */
  340. long timeout_ms, /* see above */
  341. void *userp) /* private callback pointer */
  342. {
  343. struct events *ev = userp;
  344. (void)multi;
  345. if(timeout_ms == -1)
  346. /* timeout removed */
  347. timeout_ms = 0;
  348. else if(timeout_ms == 0)
  349. /* timeout is already reached! */
  350. timeout_ms = 1; /* trigger asap */
  351. ev->ms = timeout_ms;
  352. ev->msbump = TRUE;
  353. return 0;
  354. }
  355. /* poll2cselect
  356. *
  357. * convert from poll() bit definitions to libcurl's CURL_CSELECT_* ones
  358. */
  359. static int poll2cselect(int pollmask)
  360. {
  361. int omask = 0;
  362. if(pollmask & POLLIN)
  363. omask |= CURL_CSELECT_IN;
  364. if(pollmask & POLLOUT)
  365. omask |= CURL_CSELECT_OUT;
  366. if(pollmask & POLLERR)
  367. omask |= CURL_CSELECT_ERR;
  368. return omask;
  369. }
  370. /* socketcb2poll
  371. *
  372. * convert from libcurl' CURL_POLL_* bit definitions to poll()'s
  373. */
  374. static short socketcb2poll(int pollmask)
  375. {
  376. short omask = 0;
  377. if(pollmask & CURL_POLL_IN)
  378. omask |= POLLIN;
  379. if(pollmask & CURL_POLL_OUT)
  380. omask |= POLLOUT;
  381. return omask;
  382. }
  383. /* events_socket
  384. *
  385. * Callback that gets called with information about socket activity to
  386. * monitor.
  387. */
  388. static int events_socket(struct Curl_easy *easy, /* easy handle */
  389. curl_socket_t s, /* socket */
  390. int what, /* see above */
  391. void *userp, /* private callback
  392. pointer */
  393. void *socketp) /* private socket
  394. pointer */
  395. {
  396. struct events *ev = userp;
  397. struct socketmonitor *m;
  398. struct socketmonitor *prev = NULL;
  399. #if defined(CURL_DISABLE_VERBOSE_STRINGS)
  400. (void) easy;
  401. #endif
  402. (void)socketp;
  403. m = ev->list;
  404. while(m) {
  405. if(m->socket.fd == s) {
  406. if(what == CURL_POLL_REMOVE) {
  407. struct socketmonitor *nxt = m->next;
  408. /* remove this node from the list of monitored sockets */
  409. if(prev)
  410. prev->next = nxt;
  411. else
  412. ev->list = nxt;
  413. free(m);
  414. m = nxt;
  415. infof(easy, "socket cb: socket %d REMOVED\n", s);
  416. }
  417. else {
  418. /* The socket 's' is already being monitored, update the activity
  419. mask. Convert from libcurl bitmask to the poll one. */
  420. m->socket.events = socketcb2poll(what);
  421. infof(easy, "socket cb: socket %d UPDATED as %s%s\n", s,
  422. what&CURL_POLL_IN?"IN":"",
  423. what&CURL_POLL_OUT?"OUT":"");
  424. }
  425. break;
  426. }
  427. prev = m;
  428. m = m->next; /* move to next node */
  429. }
  430. if(!m) {
  431. if(what == CURL_POLL_REMOVE) {
  432. /* this happens a bit too often, libcurl fix perhaps? */
  433. /* fprintf(stderr,
  434. "%s: socket %d asked to be REMOVED but not present!\n",
  435. __func__, s); */
  436. }
  437. else {
  438. m = malloc(sizeof(struct socketmonitor));
  439. if(m) {
  440. m->next = ev->list;
  441. m->socket.fd = s;
  442. m->socket.events = socketcb2poll(what);
  443. m->socket.revents = 0;
  444. ev->list = m;
  445. infof(easy, "socket cb: socket %d ADDED as %s%s\n", s,
  446. what&CURL_POLL_IN?"IN":"",
  447. what&CURL_POLL_OUT?"OUT":"");
  448. }
  449. else
  450. return CURLE_OUT_OF_MEMORY;
  451. }
  452. }
  453. return 0;
  454. }
  455. /*
  456. * events_setup()
  457. *
  458. * Do the multi handle setups that only event-based transfers need.
  459. */
  460. static void events_setup(struct Curl_multi *multi, struct events *ev)
  461. {
  462. /* timer callback */
  463. curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, events_timer);
  464. curl_multi_setopt(multi, CURLMOPT_TIMERDATA, ev);
  465. /* socket callback */
  466. curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, events_socket);
  467. curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev);
  468. }
  469. /* wait_or_timeout()
  470. *
  471. * waits for activity on any of the given sockets, or the timeout to trigger.
  472. */
  473. static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
  474. {
  475. bool done = FALSE;
  476. CURLMcode mcode = CURLM_OK;
  477. CURLcode result = CURLE_OK;
  478. while(!done) {
  479. CURLMsg *msg;
  480. struct socketmonitor *m;
  481. struct pollfd *f;
  482. struct pollfd fds[4];
  483. int numfds = 0;
  484. int pollrc;
  485. int i;
  486. struct curltime before;
  487. struct curltime after;
  488. /* populate the fds[] array */
  489. for(m = ev->list, f = &fds[0]; m; m = m->next) {
  490. f->fd = m->socket.fd;
  491. f->events = m->socket.events;
  492. f->revents = 0;
  493. /* fprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd); */
  494. f++;
  495. numfds++;
  496. }
  497. /* get the time stamp to use to figure out how long poll takes */
  498. before = Curl_now();
  499. /* wait for activity or timeout */
  500. pollrc = Curl_poll(fds, numfds, (int)ev->ms);
  501. after = Curl_now();
  502. ev->msbump = FALSE; /* reset here */
  503. if(0 == pollrc) {
  504. /* timeout! */
  505. ev->ms = 0;
  506. /* fprintf(stderr, "call curl_multi_socket_action(TIMEOUT)\n"); */
  507. mcode = curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0,
  508. &ev->running_handles);
  509. }
  510. else if(pollrc > 0) {
  511. /* loop over the monitored sockets to see which ones had activity */
  512. for(i = 0; i< numfds; i++) {
  513. if(fds[i].revents) {
  514. /* socket activity, tell libcurl */
  515. int act = poll2cselect(fds[i].revents); /* convert */
  516. infof(multi->easyp, "call curl_multi_socket_action(socket %d)\n",
  517. fds[i].fd);
  518. mcode = curl_multi_socket_action(multi, fds[i].fd, act,
  519. &ev->running_handles);
  520. }
  521. }
  522. if(!ev->msbump) {
  523. /* If nothing updated the timeout, we decrease it by the spent time.
  524. * If it was updated, it has the new timeout time stored already.
  525. */
  526. timediff_t timediff = Curl_timediff(after, before);
  527. if(timediff > 0) {
  528. if(timediff > ev->ms)
  529. ev->ms = 0;
  530. else
  531. ev->ms -= (long)timediff;
  532. }
  533. }
  534. }
  535. else
  536. return CURLE_RECV_ERROR;
  537. if(mcode)
  538. return CURLE_URL_MALFORMAT; /* TODO: return a proper error! */
  539. /* we don't really care about the "msgs_in_queue" value returned in the
  540. second argument */
  541. msg = curl_multi_info_read(multi, &pollrc);
  542. if(msg) {
  543. result = msg->data.result;
  544. done = TRUE;
  545. }
  546. }
  547. return result;
  548. }
  549. /* easy_events()
  550. *
  551. * Runs a transfer in a blocking manner using the events-based API
  552. */
  553. static CURLcode easy_events(struct Curl_multi *multi)
  554. {
  555. /* this struct is made static to allow it to be used after this function
  556. returns and curl_multi_remove_handle() is called */
  557. static struct events evs = {2, FALSE, 0, NULL, 0};
  558. /* if running event-based, do some further multi inits */
  559. events_setup(multi, &evs);
  560. return wait_or_timeout(multi, &evs);
  561. }
  562. #else /* CURLDEBUG */
  563. /* when not built with debug, this function doesn't exist */
  564. #define easy_events(x) CURLE_NOT_BUILT_IN
  565. #endif
  566. static CURLcode easy_transfer(struct Curl_multi *multi)
  567. {
  568. bool done = FALSE;
  569. CURLMcode mcode = CURLM_OK;
  570. CURLcode result = CURLE_OK;
  571. struct curltime before;
  572. int without_fds = 0; /* count number of consecutive returns from
  573. curl_multi_wait() without any filedescriptors */
  574. while(!done && !mcode) {
  575. int still_running = 0;
  576. int rc;
  577. before = Curl_now();
  578. mcode = curl_multi_wait(multi, NULL, 0, 1000, &rc);
  579. if(!mcode) {
  580. if(!rc) {
  581. struct curltime after = Curl_now();
  582. /* If it returns without any filedescriptor instantly, we need to
  583. avoid busy-looping during periods where it has nothing particular
  584. to wait for */
  585. if(Curl_timediff(after, before) <= 10) {
  586. without_fds++;
  587. if(without_fds > 2) {
  588. int sleep_ms = without_fds < 10 ? (1 << (without_fds - 1)) : 1000;
  589. Curl_wait_ms(sleep_ms);
  590. }
  591. }
  592. else
  593. /* it wasn't "instant", restart counter */
  594. without_fds = 0;
  595. }
  596. else
  597. /* got file descriptor, restart counter */
  598. without_fds = 0;
  599. mcode = curl_multi_perform(multi, &still_running);
  600. }
  601. /* only read 'still_running' if curl_multi_perform() return OK */
  602. if(!mcode && !still_running) {
  603. CURLMsg *msg = curl_multi_info_read(multi, &rc);
  604. if(msg) {
  605. result = msg->data.result;
  606. done = TRUE;
  607. }
  608. }
  609. }
  610. /* Make sure to return some kind of error if there was a multi problem */
  611. if(mcode) {
  612. result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
  613. /* The other multi errors should never happen, so return
  614. something suitably generic */
  615. CURLE_BAD_FUNCTION_ARGUMENT;
  616. }
  617. return result;
  618. }
  619. /*
  620. * easy_perform() is the external interface that performs a blocking
  621. * transfer as previously setup.
  622. *
  623. * CONCEPT: This function creates a multi handle, adds the easy handle to it,
  624. * runs curl_multi_perform() until the transfer is done, then detaches the
  625. * easy handle, destroys the multi handle and returns the easy handle's return
  626. * code.
  627. *
  628. * REALITY: it can't just create and destroy the multi handle that easily. It
  629. * needs to keep it around since if this easy handle is used again by this
  630. * function, the same multi handle must be re-used so that the same pools and
  631. * caches can be used.
  632. *
  633. * DEBUG: if 'events' is set TRUE, this function will use a replacement engine
  634. * instead of curl_multi_perform() and use curl_multi_socket_action().
  635. */
  636. static CURLcode easy_perform(struct Curl_easy *data, bool events)
  637. {
  638. struct Curl_multi *multi;
  639. CURLMcode mcode;
  640. CURLcode result = CURLE_OK;
  641. SIGPIPE_VARIABLE(pipe_st);
  642. if(!data)
  643. return CURLE_BAD_FUNCTION_ARGUMENT;
  644. if(data->set.errorbuffer)
  645. /* clear this as early as possible */
  646. data->set.errorbuffer[0] = 0;
  647. if(data->multi) {
  648. failf(data, "easy handle already used in multi handle");
  649. return CURLE_FAILED_INIT;
  650. }
  651. if(data->multi_easy)
  652. multi = data->multi_easy;
  653. else {
  654. /* this multi handle will only ever have a single easy handled attached
  655. to it, so make it use minimal hashes */
  656. multi = Curl_multi_handle(1, 3);
  657. if(!multi)
  658. return CURLE_OUT_OF_MEMORY;
  659. data->multi_easy = multi;
  660. }
  661. if(multi->in_callback)
  662. return CURLE_RECURSIVE_API_CALL;
  663. /* Copy the MAXCONNECTS option to the multi handle */
  664. curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, data->set.maxconnects);
  665. mcode = curl_multi_add_handle(multi, data);
  666. if(mcode) {
  667. curl_multi_cleanup(multi);
  668. if(mcode == CURLM_OUT_OF_MEMORY)
  669. return CURLE_OUT_OF_MEMORY;
  670. return CURLE_FAILED_INIT;
  671. }
  672. sigpipe_ignore(data, &pipe_st);
  673. /* assign this after curl_multi_add_handle() since that function checks for
  674. it and rejects this handle otherwise */
  675. data->multi = multi;
  676. /* run the transfer */
  677. result = events ? easy_events(multi) : easy_transfer(multi);
  678. /* ignoring the return code isn't nice, but atm we can't really handle
  679. a failure here, room for future improvement! */
  680. (void)curl_multi_remove_handle(multi, data);
  681. sigpipe_restore(&pipe_st);
  682. /* The multi handle is kept alive, owned by the easy handle */
  683. return result;
  684. }
  685. /*
  686. * curl_easy_perform() is the external interface that performs a blocking
  687. * transfer as previously setup.
  688. */
  689. CURLcode curl_easy_perform(struct Curl_easy *data)
  690. {
  691. return easy_perform(data, FALSE);
  692. }
  693. #ifdef CURLDEBUG
  694. /*
  695. * curl_easy_perform_ev() is the external interface that performs a blocking
  696. * transfer using the event-based API internally.
  697. */
  698. CURLcode curl_easy_perform_ev(struct Curl_easy *data)
  699. {
  700. return easy_perform(data, TRUE);
  701. }
  702. #endif
  703. /*
  704. * curl_easy_cleanup() is the external interface to cleaning/freeing the given
  705. * easy handle.
  706. */
  707. void curl_easy_cleanup(struct Curl_easy *data)
  708. {
  709. SIGPIPE_VARIABLE(pipe_st);
  710. if(!data)
  711. return;
  712. sigpipe_ignore(data, &pipe_st);
  713. Curl_close(data);
  714. sigpipe_restore(&pipe_st);
  715. }
  716. /*
  717. * curl_easy_getinfo() is an external interface that allows an app to retrieve
  718. * information from a performed transfer and similar.
  719. */
  720. #undef curl_easy_getinfo
  721. CURLcode curl_easy_getinfo(struct Curl_easy *data, CURLINFO info, ...)
  722. {
  723. va_list arg;
  724. void *paramp;
  725. CURLcode result;
  726. va_start(arg, info);
  727. paramp = va_arg(arg, void *);
  728. result = Curl_getinfo(data, info, paramp);
  729. va_end(arg);
  730. return result;
  731. }
  732. static CURLcode dupset(struct Curl_easy *dst, struct Curl_easy *src)
  733. {
  734. CURLcode result = CURLE_OK;
  735. enum dupstring i;
  736. /* Copy src->set into dst->set first, then deal with the strings
  737. afterwards */
  738. dst->set = src->set;
  739. Curl_mime_initpart(&dst->set.mimepost, dst);
  740. /* clear all string pointers first */
  741. memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
  742. /* duplicate all strings */
  743. for(i = (enum dupstring)0; i< STRING_LASTZEROTERMINATED; i++) {
  744. result = Curl_setstropt(&dst->set.str[i], src->set.str[i]);
  745. if(result)
  746. return result;
  747. }
  748. /* duplicate memory areas pointed to */
  749. i = STRING_COPYPOSTFIELDS;
  750. if(src->set.postfieldsize && src->set.str[i]) {
  751. /* postfieldsize is curl_off_t, Curl_memdup() takes a size_t ... */
  752. dst->set.str[i] = Curl_memdup(src->set.str[i],
  753. curlx_sotouz(src->set.postfieldsize));
  754. if(!dst->set.str[i])
  755. return CURLE_OUT_OF_MEMORY;
  756. /* point to the new copy */
  757. dst->set.postfields = dst->set.str[i];
  758. }
  759. /* Duplicate mime data. */
  760. result = Curl_mime_duppart(&dst->set.mimepost, &src->set.mimepost);
  761. if(src->set.resolve)
  762. dst->change.resolve = dst->set.resolve;
  763. return result;
  764. }
  765. /*
  766. * curl_easy_duphandle() is an external interface to allow duplication of a
  767. * given input easy handle. The returned handle will be a new working handle
  768. * with all options set exactly as the input source handle.
  769. */
  770. struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
  771. {
  772. struct Curl_easy *outcurl = calloc(1, sizeof(struct Curl_easy));
  773. if(NULL == outcurl)
  774. goto fail;
  775. /*
  776. * We setup a few buffers we need. We should probably make them
  777. * get setup on-demand in the code, as that would probably decrease
  778. * the likeliness of us forgetting to init a buffer here in the future.
  779. */
  780. outcurl->set.buffer_size = data->set.buffer_size;
  781. outcurl->state.buffer = malloc(outcurl->set.buffer_size + 1);
  782. if(!outcurl->state.buffer)
  783. goto fail;
  784. outcurl->state.headerbuff = malloc(HEADERSIZE);
  785. if(!outcurl->state.headerbuff)
  786. goto fail;
  787. outcurl->state.headersize = HEADERSIZE;
  788. /* copy all userdefined values */
  789. if(dupset(outcurl, data))
  790. goto fail;
  791. /* the connection cache is setup on demand */
  792. outcurl->state.conn_cache = NULL;
  793. outcurl->state.lastconnect = NULL;
  794. outcurl->progress.flags = data->progress.flags;
  795. outcurl->progress.callback = data->progress.callback;
  796. if(data->cookies) {
  797. /* If cookies are enabled in the parent handle, we enable them
  798. in the clone as well! */
  799. outcurl->cookies = Curl_cookie_init(data,
  800. data->cookies->filename,
  801. outcurl->cookies,
  802. data->set.cookiesession);
  803. if(!outcurl->cookies)
  804. goto fail;
  805. }
  806. /* duplicate all values in 'change' */
  807. if(data->change.cookielist) {
  808. outcurl->change.cookielist =
  809. Curl_slist_duplicate(data->change.cookielist);
  810. if(!outcurl->change.cookielist)
  811. goto fail;
  812. }
  813. if(data->change.url) {
  814. outcurl->change.url = strdup(data->change.url);
  815. if(!outcurl->change.url)
  816. goto fail;
  817. outcurl->change.url_alloc = TRUE;
  818. }
  819. if(data->change.referer) {
  820. outcurl->change.referer = strdup(data->change.referer);
  821. if(!outcurl->change.referer)
  822. goto fail;
  823. outcurl->change.referer_alloc = TRUE;
  824. }
  825. /* Clone the resolver handle, if present, for the new handle */
  826. if(Curl_resolver_duphandle(&outcurl->state.resolver,
  827. data->state.resolver))
  828. goto fail;
  829. Curl_convert_setup(outcurl);
  830. Curl_initinfo(outcurl);
  831. outcurl->magic = CURLEASY_MAGIC_NUMBER;
  832. /* we reach this point and thus we are OK */
  833. return outcurl;
  834. fail:
  835. if(outcurl) {
  836. curl_slist_free_all(outcurl->change.cookielist);
  837. outcurl->change.cookielist = NULL;
  838. Curl_safefree(outcurl->state.buffer);
  839. Curl_safefree(outcurl->state.headerbuff);
  840. Curl_safefree(outcurl->change.url);
  841. Curl_safefree(outcurl->change.referer);
  842. Curl_freeset(outcurl);
  843. free(outcurl);
  844. }
  845. return NULL;
  846. }
  847. /*
  848. * curl_easy_reset() is an external interface that allows an app to re-
  849. * initialize a session handle to the default values.
  850. */
  851. void curl_easy_reset(struct Curl_easy *data)
  852. {
  853. Curl_safefree(data->state.pathbuffer);
  854. data->state.path = NULL;
  855. Curl_free_request_state(data);
  856. /* zero out UserDefined data: */
  857. Curl_freeset(data);
  858. memset(&data->set, 0, sizeof(struct UserDefined));
  859. (void)Curl_init_userdefined(data);
  860. /* zero out Progress data: */
  861. memset(&data->progress, 0, sizeof(struct Progress));
  862. /* zero out PureInfo data: */
  863. Curl_initinfo(data);
  864. data->progress.flags |= PGRS_HIDE;
  865. data->state.current_speed = -1; /* init to negative == impossible */
  866. /* zero out authentication data: */
  867. memset(&data->state.authhost, 0, sizeof(struct auth));
  868. memset(&data->state.authproxy, 0, sizeof(struct auth));
  869. Curl_digest_cleanup(data);
  870. }
  871. /*
  872. * curl_easy_pause() allows an application to pause or unpause a specific
  873. * transfer and direction. This function sets the full new state for the
  874. * current connection this easy handle operates on.
  875. *
  876. * NOTE: if you have the receiving paused and you call this function to remove
  877. * the pausing, you may get your write callback called at this point.
  878. *
  879. * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
  880. *
  881. * NOTE: This is one of few API functions that are allowed to be called from
  882. * within a callback.
  883. */
  884. CURLcode curl_easy_pause(struct Curl_easy *data, int action)
  885. {
  886. struct SingleRequest *k = &data->req;
  887. CURLcode result = CURLE_OK;
  888. /* first switch off both pause bits */
  889. int newstate = k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
  890. /* set the new desired pause bits */
  891. newstate |= ((action & CURLPAUSE_RECV)?KEEP_RECV_PAUSE:0) |
  892. ((action & CURLPAUSE_SEND)?KEEP_SEND_PAUSE:0);
  893. /* put it back in the keepon */
  894. k->keepon = newstate;
  895. if(!(newstate & KEEP_RECV_PAUSE) && data->state.tempcount) {
  896. /* there are buffers for sending that can be delivered as the receive
  897. pausing is lifted! */
  898. unsigned int i;
  899. unsigned int count = data->state.tempcount;
  900. struct tempbuf writebuf[3]; /* there can only be three */
  901. struct connectdata *conn = data->easy_conn;
  902. struct Curl_easy *saved_data = NULL;
  903. /* copy the structs to allow for immediate re-pausing */
  904. for(i = 0; i < data->state.tempcount; i++) {
  905. writebuf[i] = data->state.tempwrite[i];
  906. data->state.tempwrite[i].buf = NULL;
  907. }
  908. data->state.tempcount = 0;
  909. /* set the connection's current owner */
  910. if(conn->data != data) {
  911. saved_data = conn->data;
  912. conn->data = data;
  913. }
  914. for(i = 0; i < count; i++) {
  915. /* even if one function returns error, this loops through and frees all
  916. buffers */
  917. if(!result)
  918. result = Curl_client_write(conn, writebuf[i].type, writebuf[i].buf,
  919. writebuf[i].len);
  920. free(writebuf[i].buf);
  921. }
  922. /* recover previous owner of the connection */
  923. if(saved_data)
  924. conn->data = saved_data;
  925. if(result)
  926. return result;
  927. }
  928. /* if there's no error and we're not pausing both directions, we want
  929. to have this handle checked soon */
  930. if(!result &&
  931. ((newstate&(KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) !=
  932. (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) )
  933. Curl_expire(data, 0, EXPIRE_RUN_NOW); /* get this handle going again */
  934. /* This transfer may have been moved in or out of the bundle, update
  935. the corresponding socket callback, if used */
  936. Curl_updatesocket(data);
  937. return result;
  938. }
  939. static CURLcode easy_connection(struct Curl_easy *data,
  940. curl_socket_t *sfd,
  941. struct connectdata **connp)
  942. {
  943. if(data == NULL)
  944. return CURLE_BAD_FUNCTION_ARGUMENT;
  945. /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
  946. if(!data->set.connect_only) {
  947. failf(data, "CONNECT_ONLY is required!");
  948. return CURLE_UNSUPPORTED_PROTOCOL;
  949. }
  950. *sfd = Curl_getconnectinfo(data, connp);
  951. if(*sfd == CURL_SOCKET_BAD) {
  952. failf(data, "Failed to get recent socket");
  953. return CURLE_UNSUPPORTED_PROTOCOL;
  954. }
  955. return CURLE_OK;
  956. }
  957. /*
  958. * Receives data from the connected socket. Use after successful
  959. * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
  960. * Returns CURLE_OK on success, error code on error.
  961. */
  962. CURLcode curl_easy_recv(struct Curl_easy *data, void *buffer, size_t buflen,
  963. size_t *n)
  964. {
  965. curl_socket_t sfd;
  966. CURLcode result;
  967. ssize_t n1;
  968. struct connectdata *c;
  969. if(Curl_is_in_callback(data))
  970. return CURLE_RECURSIVE_API_CALL;
  971. result = easy_connection(data, &sfd, &c);
  972. if(result)
  973. return result;
  974. *n = 0;
  975. result = Curl_read(c, sfd, buffer, buflen, &n1);
  976. if(result)
  977. return result;
  978. *n = (size_t)n1;
  979. return CURLE_OK;
  980. }
  981. /*
  982. * Sends data over the connected socket. Use after successful
  983. * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
  984. */
  985. CURLcode curl_easy_send(struct Curl_easy *data, const void *buffer,
  986. size_t buflen, size_t *n)
  987. {
  988. curl_socket_t sfd;
  989. CURLcode result;
  990. ssize_t n1;
  991. struct connectdata *c = NULL;
  992. if(Curl_is_in_callback(data))
  993. return CURLE_RECURSIVE_API_CALL;
  994. result = easy_connection(data, &sfd, &c);
  995. if(result)
  996. return result;
  997. *n = 0;
  998. result = Curl_write(c, sfd, buffer, buflen, &n1);
  999. if(n1 == -1)
  1000. return CURLE_SEND_ERROR;
  1001. /* detect EAGAIN */
  1002. if(!result && !n1)
  1003. return CURLE_AGAIN;
  1004. *n = (size_t)n1;
  1005. return result;
  1006. }