easy.c 36 KB

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