2
0

easy.c 35 KB

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