easy.c 33 KB

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