easy.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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, (int)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. /* Copy src->set into dst->set first, then deal with the strings
  657. afterwards */
  658. dst->set = src->set;
  659. Curl_mime_initpart(&dst->set.mimepost, dst);
  660. /* clear all string pointers first */
  661. memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
  662. /* duplicate all strings */
  663. for(i = (enum dupstring)0; i< STRING_LASTZEROTERMINATED; i++) {
  664. result = Curl_setstropt(&dst->set.str[i], src->set.str[i]);
  665. if(result)
  666. return result;
  667. }
  668. /* duplicate memory areas pointed to */
  669. i = STRING_COPYPOSTFIELDS;
  670. if(src->set.postfieldsize && src->set.str[i]) {
  671. /* postfieldsize is curl_off_t, Curl_memdup() takes a size_t ... */
  672. dst->set.str[i] = Curl_memdup(src->set.str[i],
  673. curlx_sotouz(src->set.postfieldsize));
  674. if(!dst->set.str[i])
  675. return CURLE_OUT_OF_MEMORY;
  676. /* point to the new copy */
  677. dst->set.postfields = dst->set.str[i];
  678. }
  679. /* Duplicate mime data. */
  680. result = Curl_mime_duppart(&dst->set.mimepost, &src->set.mimepost);
  681. if(src->set.resolve)
  682. dst->change.resolve = dst->set.resolve;
  683. return result;
  684. }
  685. /*
  686. * curl_easy_duphandle() is an external interface to allow duplication of a
  687. * given input easy handle. The returned handle will be a new working handle
  688. * with all options set exactly as the input source handle.
  689. */
  690. struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
  691. {
  692. struct Curl_easy *outcurl = calloc(1, sizeof(struct Curl_easy));
  693. if(NULL == outcurl)
  694. goto fail;
  695. /*
  696. * We setup a few buffers we need. We should probably make them
  697. * get setup on-demand in the code, as that would probably decrease
  698. * the likeliness of us forgetting to init a buffer here in the future.
  699. */
  700. outcurl->set.buffer_size = data->set.buffer_size;
  701. outcurl->state.buffer = malloc(outcurl->set.buffer_size + 1);
  702. if(!outcurl->state.buffer)
  703. goto fail;
  704. /* copy all userdefined values */
  705. if(dupset(outcurl, data))
  706. goto fail;
  707. Curl_dyn_init(&outcurl->state.headerb, CURL_MAX_HTTP_HEADER);
  708. /* the connection cache is setup on demand */
  709. outcurl->state.conn_cache = NULL;
  710. outcurl->state.lastconnect = NULL;
  711. outcurl->progress.flags = data->progress.flags;
  712. outcurl->progress.callback = data->progress.callback;
  713. if(data->cookies) {
  714. /* If cookies are enabled in the parent handle, we enable them
  715. in the clone as well! */
  716. outcurl->cookies = Curl_cookie_init(data,
  717. data->cookies->filename,
  718. outcurl->cookies,
  719. data->set.cookiesession);
  720. if(!outcurl->cookies)
  721. goto fail;
  722. }
  723. /* duplicate all values in 'change' */
  724. if(data->change.cookielist) {
  725. outcurl->change.cookielist =
  726. Curl_slist_duplicate(data->change.cookielist);
  727. if(!outcurl->change.cookielist)
  728. goto fail;
  729. }
  730. if(data->change.url) {
  731. outcurl->change.url = strdup(data->change.url);
  732. if(!outcurl->change.url)
  733. goto fail;
  734. outcurl->change.url_alloc = TRUE;
  735. }
  736. if(data->change.referer) {
  737. outcurl->change.referer = strdup(data->change.referer);
  738. if(!outcurl->change.referer)
  739. goto fail;
  740. outcurl->change.referer_alloc = TRUE;
  741. }
  742. /* Reinitialize an SSL engine for the new handle
  743. * note: the engine name has already been copied by dupset */
  744. if(outcurl->set.str[STRING_SSL_ENGINE]) {
  745. if(Curl_ssl_set_engine(outcurl, outcurl->set.str[STRING_SSL_ENGINE]))
  746. goto fail;
  747. }
  748. /* Clone the resolver handle, if present, for the new handle */
  749. if(Curl_resolver_duphandle(outcurl,
  750. &outcurl->state.resolver,
  751. data->state.resolver))
  752. goto fail;
  753. #ifdef USE_ARES
  754. {
  755. CURLcode rc;
  756. rc = Curl_set_dns_servers(outcurl, data->set.str[STRING_DNS_SERVERS]);
  757. if(rc && rc != CURLE_NOT_BUILT_IN)
  758. goto fail;
  759. rc = Curl_set_dns_interface(outcurl, data->set.str[STRING_DNS_INTERFACE]);
  760. if(rc && rc != CURLE_NOT_BUILT_IN)
  761. goto fail;
  762. rc = Curl_set_dns_local_ip4(outcurl, data->set.str[STRING_DNS_LOCAL_IP4]);
  763. if(rc && rc != CURLE_NOT_BUILT_IN)
  764. goto fail;
  765. rc = Curl_set_dns_local_ip6(outcurl, data->set.str[STRING_DNS_LOCAL_IP6]);
  766. if(rc && rc != CURLE_NOT_BUILT_IN)
  767. goto fail;
  768. }
  769. #endif /* USE_ARES */
  770. Curl_convert_setup(outcurl);
  771. Curl_initinfo(outcurl);
  772. outcurl->magic = CURLEASY_MAGIC_NUMBER;
  773. /* we reach this point and thus we are OK */
  774. return outcurl;
  775. fail:
  776. if(outcurl) {
  777. curl_slist_free_all(outcurl->change.cookielist);
  778. outcurl->change.cookielist = NULL;
  779. Curl_safefree(outcurl->state.buffer);
  780. Curl_dyn_free(&outcurl->state.headerb);
  781. Curl_safefree(outcurl->change.url);
  782. Curl_safefree(outcurl->change.referer);
  783. Curl_freeset(outcurl);
  784. free(outcurl);
  785. }
  786. return NULL;
  787. }
  788. /*
  789. * curl_easy_reset() is an external interface that allows an app to re-
  790. * initialize a session handle to the default values.
  791. */
  792. void curl_easy_reset(struct Curl_easy *data)
  793. {
  794. long old_buffer_size = data->set.buffer_size;
  795. Curl_free_request_state(data);
  796. /* zero out UserDefined data: */
  797. Curl_freeset(data);
  798. memset(&data->set, 0, sizeof(struct UserDefined));
  799. (void)Curl_init_userdefined(data);
  800. /* zero out Progress data: */
  801. memset(&data->progress, 0, sizeof(struct Progress));
  802. /* zero out PureInfo data: */
  803. Curl_initinfo(data);
  804. data->progress.flags |= PGRS_HIDE;
  805. data->state.current_speed = -1; /* init to negative == impossible */
  806. /* zero out authentication data: */
  807. memset(&data->state.authhost, 0, sizeof(struct auth));
  808. memset(&data->state.authproxy, 0, sizeof(struct auth));
  809. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
  810. Curl_http_auth_cleanup_digest(data);
  811. #endif
  812. /* resize receive buffer */
  813. if(old_buffer_size != data->set.buffer_size) {
  814. char *newbuff = realloc(data->state.buffer, data->set.buffer_size + 1);
  815. if(!newbuff) {
  816. DEBUGF(fprintf(stderr, "Error: realloc of buffer failed\n"));
  817. /* nothing we can do here except use the old size */
  818. data->set.buffer_size = old_buffer_size;
  819. }
  820. else
  821. data->state.buffer = newbuff;
  822. }
  823. }
  824. /*
  825. * curl_easy_pause() allows an application to pause or unpause a specific
  826. * transfer and direction. This function sets the full new state for the
  827. * current connection this easy handle operates on.
  828. *
  829. * NOTE: if you have the receiving paused and you call this function to remove
  830. * the pausing, you may get your write callback called at this point.
  831. *
  832. * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
  833. *
  834. * NOTE: This is one of few API functions that are allowed to be called from
  835. * within a callback.
  836. */
  837. CURLcode curl_easy_pause(struct Curl_easy *data, int action)
  838. {
  839. struct SingleRequest *k;
  840. CURLcode result = CURLE_OK;
  841. int oldstate;
  842. int newstate;
  843. if(!GOOD_EASY_HANDLE(data) || !data->conn)
  844. /* crazy input, don't continue */
  845. return CURLE_BAD_FUNCTION_ARGUMENT;
  846. k = &data->req;
  847. oldstate = k->keepon & (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
  848. /* first switch off both pause bits then set the new pause bits */
  849. newstate = (k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE)) |
  850. ((action & CURLPAUSE_RECV)?KEEP_RECV_PAUSE:0) |
  851. ((action & CURLPAUSE_SEND)?KEEP_SEND_PAUSE:0);
  852. if((newstate & (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE)) == oldstate) {
  853. /* Not changing any pause state, return */
  854. DEBUGF(infof(data, "pause: no change, early return\n"));
  855. return CURLE_OK;
  856. }
  857. /* Unpause parts in active mime tree. */
  858. if((k->keepon & ~newstate & KEEP_SEND_PAUSE) &&
  859. (data->mstate == CURLM_STATE_PERFORM ||
  860. data->mstate == CURLM_STATE_TOOFAST) &&
  861. data->state.fread_func == (curl_read_callback) Curl_mime_read) {
  862. Curl_mime_unpause(data->state.in);
  863. }
  864. /* put it back in the keepon */
  865. k->keepon = newstate;
  866. if(!(newstate & KEEP_RECV_PAUSE)) {
  867. Curl_http2_stream_pause(data, FALSE);
  868. if(data->state.tempcount) {
  869. /* there are buffers for sending that can be delivered as the receive
  870. pausing is lifted! */
  871. unsigned int i;
  872. unsigned int count = data->state.tempcount;
  873. struct tempbuf writebuf[3]; /* there can only be three */
  874. struct connectdata *conn = data->conn;
  875. struct Curl_easy *saved_data = NULL;
  876. /* copy the structs to allow for immediate re-pausing */
  877. for(i = 0; i < data->state.tempcount; i++) {
  878. writebuf[i] = data->state.tempwrite[i];
  879. Curl_dyn_init(&data->state.tempwrite[i].b, DYN_PAUSE_BUFFER);
  880. }
  881. data->state.tempcount = 0;
  882. /* set the connection's current owner */
  883. if(conn->data != data) {
  884. saved_data = conn->data;
  885. conn->data = data;
  886. }
  887. for(i = 0; i < count; i++) {
  888. /* even if one function returns error, this loops through and frees
  889. all buffers */
  890. if(!result)
  891. result = Curl_client_write(conn, writebuf[i].type,
  892. Curl_dyn_ptr(&writebuf[i].b),
  893. Curl_dyn_len(&writebuf[i].b));
  894. Curl_dyn_free(&writebuf[i].b);
  895. }
  896. /* recover previous owner of the connection */
  897. if(saved_data)
  898. conn->data = saved_data;
  899. if(result)
  900. return result;
  901. }
  902. }
  903. /* if there's no error and we're not pausing both directions, we want
  904. to have this handle checked soon */
  905. if((newstate & (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) !=
  906. (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) {
  907. Curl_expire(data, 0, EXPIRE_RUN_NOW); /* get this handle going again */
  908. /* force a recv/send check of this connection, as the data might've been
  909. read off the socket already */
  910. data->conn->cselect_bits = CURL_CSELECT_IN | CURL_CSELECT_OUT;
  911. if(data->multi)
  912. Curl_update_timer(data->multi);
  913. }
  914. if(!data->state.done)
  915. /* This transfer may have been moved in or out of the bundle, update the
  916. corresponding socket callback, if used */
  917. Curl_updatesocket(data);
  918. return result;
  919. }
  920. static CURLcode easy_connection(struct Curl_easy *data,
  921. curl_socket_t *sfd,
  922. struct connectdata **connp)
  923. {
  924. if(data == NULL)
  925. return CURLE_BAD_FUNCTION_ARGUMENT;
  926. /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
  927. if(!data->set.connect_only) {
  928. failf(data, "CONNECT_ONLY is required!");
  929. return CURLE_UNSUPPORTED_PROTOCOL;
  930. }
  931. *sfd = Curl_getconnectinfo(data, connp);
  932. if(*sfd == CURL_SOCKET_BAD) {
  933. failf(data, "Failed to get recent socket");
  934. return CURLE_UNSUPPORTED_PROTOCOL;
  935. }
  936. return CURLE_OK;
  937. }
  938. /*
  939. * Receives data from the connected socket. Use after successful
  940. * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
  941. * Returns CURLE_OK on success, error code on error.
  942. */
  943. CURLcode curl_easy_recv(struct Curl_easy *data, void *buffer, size_t buflen,
  944. size_t *n)
  945. {
  946. curl_socket_t sfd;
  947. CURLcode result;
  948. ssize_t n1;
  949. struct connectdata *c;
  950. if(Curl_is_in_callback(data))
  951. return CURLE_RECURSIVE_API_CALL;
  952. result = easy_connection(data, &sfd, &c);
  953. if(result)
  954. return result;
  955. *n = 0;
  956. result = Curl_read(c, sfd, buffer, buflen, &n1);
  957. if(result)
  958. return result;
  959. *n = (size_t)n1;
  960. return CURLE_OK;
  961. }
  962. /*
  963. * Sends data over the connected socket. Use after successful
  964. * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
  965. */
  966. CURLcode curl_easy_send(struct Curl_easy *data, const void *buffer,
  967. size_t buflen, size_t *n)
  968. {
  969. curl_socket_t sfd;
  970. CURLcode result;
  971. ssize_t n1;
  972. struct connectdata *c = NULL;
  973. if(Curl_is_in_callback(data))
  974. return CURLE_RECURSIVE_API_CALL;
  975. result = easy_connection(data, &sfd, &c);
  976. if(result)
  977. return result;
  978. *n = 0;
  979. result = Curl_write(c, sfd, buffer, buflen, &n1);
  980. if(n1 == -1)
  981. return CURLE_SEND_ERROR;
  982. /* detect EAGAIN */
  983. if(!result && !n1)
  984. return CURLE_AGAIN;
  985. *n = (size_t)n1;
  986. return result;
  987. }
  988. /*
  989. * Wrapper to call functions in Curl_conncache_foreach()
  990. *
  991. * Returns always 0.
  992. */
  993. static int conn_upkeep(struct connectdata *conn,
  994. void *param)
  995. {
  996. /* Param is unused. */
  997. (void)param;
  998. if(conn->handler->connection_check) {
  999. /* Do a protocol-specific keepalive check on the connection. */
  1000. conn->handler->connection_check(conn, CONNCHECK_KEEPALIVE);
  1001. }
  1002. return 0; /* continue iteration */
  1003. }
  1004. static CURLcode upkeep(struct conncache *conn_cache, void *data)
  1005. {
  1006. /* Loop over every connection and make connection alive. */
  1007. Curl_conncache_foreach(data,
  1008. conn_cache,
  1009. data,
  1010. conn_upkeep);
  1011. return CURLE_OK;
  1012. }
  1013. /*
  1014. * Performs connection upkeep for the given session handle.
  1015. */
  1016. CURLcode curl_easy_upkeep(struct Curl_easy *data)
  1017. {
  1018. /* Verify that we got an easy handle we can work with. */
  1019. if(!GOOD_EASY_HANDLE(data))
  1020. return CURLE_BAD_FUNCTION_ARGUMENT;
  1021. if(data->multi_easy) {
  1022. /* Use the common function to keep connections alive. */
  1023. return upkeep(&data->multi_easy->conn_cache, data);
  1024. }
  1025. else {
  1026. /* No connections, so just return success */
  1027. return CURLE_OK;
  1028. }
  1029. }