easy.c 34 KB

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