easy.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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 "vtls/vtls_scache.h"
  48. #include "vquic/vquic.h"
  49. #include "url.h"
  50. #include "getinfo.h"
  51. #include "hostip.h"
  52. #include "share.h"
  53. #include "strdup.h"
  54. #include "progress.h"
  55. #include "easyif.h"
  56. #include "multiif.h"
  57. #include "select.h"
  58. #include "cfilters.h"
  59. #include "sendf.h" /* for failf function prototype */
  60. #include "connect.h" /* for Curl_getconnectinfo */
  61. #include "slist.h"
  62. #include "mime.h"
  63. #include "amigaos.h"
  64. #include "macos.h"
  65. #include "curlx/warnless.h"
  66. #include "curlx/wait.h"
  67. #include "sigpipe.h"
  68. #include "vssh/ssh.h"
  69. #include "setopt.h"
  70. #include "http_digest.h"
  71. #include "system_win32.h"
  72. #include "http2.h"
  73. #include "curlx/dynbuf.h"
  74. #include "altsvc.h"
  75. #include "hsts.h"
  76. #include "easy_lock.h"
  77. /* The last 2 #include files should be in this order */
  78. #include "curl_memory.h"
  79. #include "memdebug.h"
  80. /* true globals -- for curl_global_init() and curl_global_cleanup() */
  81. static unsigned int initialized;
  82. static long easy_init_flags;
  83. #ifdef GLOBAL_INIT_IS_THREADSAFE
  84. static curl_simple_lock s_lock = CURL_SIMPLE_LOCK_INIT;
  85. #define global_init_lock() curl_simple_lock_lock(&s_lock)
  86. #define global_init_unlock() curl_simple_lock_unlock(&s_lock)
  87. #else
  88. #define global_init_lock()
  89. #define global_init_unlock()
  90. #endif
  91. /*
  92. * strdup (and other memory functions) is redefined in complicated
  93. * ways, but at this point it must be defined as the system-supplied strdup
  94. * so the callback pointer is initialized correctly.
  95. */
  96. #ifdef UNDER_CE
  97. #define system_strdup _strdup
  98. #elif !defined(HAVE_STRDUP)
  99. #define system_strdup Curl_strdup
  100. #else
  101. #define system_strdup strdup
  102. #endif
  103. #if defined(_MSC_VER) && defined(_DLL)
  104. # pragma warning(push)
  105. # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
  106. #endif
  107. /*
  108. * If a memory-using function (like curl_getenv) is used before
  109. * curl_global_init() is called, we need to have these pointers set already.
  110. */
  111. curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
  112. curl_free_callback Curl_cfree = (curl_free_callback)free;
  113. curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
  114. curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
  115. curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
  116. #if defined(_MSC_VER) && defined(_DLL)
  117. # pragma warning(pop)
  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. }
  138. if(Curl_trc_init()) {
  139. DEBUGF(curl_mfprintf(stderr, "Error: Curl_trc_init failed\n"));
  140. goto fail;
  141. }
  142. if(!Curl_ssl_init()) {
  143. DEBUGF(curl_mfprintf(stderr, "Error: Curl_ssl_init failed\n"));
  144. goto fail;
  145. }
  146. if(!Curl_vquic_init()) {
  147. DEBUGF(curl_mfprintf(stderr, "Error: Curl_vquic_init failed\n"));
  148. goto fail;
  149. }
  150. if(Curl_win32_init(flags)) {
  151. DEBUGF(curl_mfprintf(stderr, "Error: win32_init failed\n"));
  152. goto fail;
  153. }
  154. if(Curl_amiga_init()) {
  155. DEBUGF(curl_mfprintf(stderr, "Error: Curl_amiga_init failed\n"));
  156. goto fail;
  157. }
  158. if(Curl_macos_init()) {
  159. DEBUGF(curl_mfprintf(stderr, "Error: Curl_macos_init failed\n"));
  160. goto fail;
  161. }
  162. if(Curl_async_global_init()) {
  163. DEBUGF(curl_mfprintf(stderr, "Error: resolver_global_init failed\n"));
  164. goto fail;
  165. }
  166. if(Curl_ssh_init()) {
  167. DEBUGF(curl_mfprintf(stderr, "Error: Curl_ssh_init failed\n"));
  168. goto fail;
  169. }
  170. easy_init_flags = flags;
  171. #ifdef DEBUGBUILD
  172. if(getenv("CURL_GLOBAL_INIT"))
  173. /* alloc data that will leak if *cleanup() is not called! */
  174. leakpointer = malloc(1);
  175. #endif
  176. return CURLE_OK;
  177. fail:
  178. initialized--; /* undo the increase */
  179. return CURLE_FAILED_INIT;
  180. }
  181. /**
  182. * curl_global_init() globally initializes curl given a bitwise set of the
  183. * different features of what to initialize.
  184. */
  185. CURLcode curl_global_init(long flags)
  186. {
  187. CURLcode result;
  188. global_init_lock();
  189. result = global_init(flags, TRUE);
  190. global_init_unlock();
  191. return result;
  192. }
  193. /*
  194. * curl_global_init_mem() globally initializes curl and also registers the
  195. * user provided callback routines.
  196. */
  197. CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
  198. curl_free_callback f, curl_realloc_callback r,
  199. curl_strdup_callback s, curl_calloc_callback c)
  200. {
  201. CURLcode result;
  202. /* Invalid input, return immediately */
  203. if(!m || !f || !r || !s || !c)
  204. return CURLE_FAILED_INIT;
  205. global_init_lock();
  206. if(initialized) {
  207. /* Already initialized, do not do it again, but bump the variable anyway to
  208. work like curl_global_init() and require the same amount of cleanup
  209. calls. */
  210. initialized++;
  211. global_init_unlock();
  212. return CURLE_OK;
  213. }
  214. /* set memory functions before global_init() in case it wants memory
  215. functions */
  216. Curl_cmalloc = m;
  217. Curl_cfree = f;
  218. Curl_cstrdup = s;
  219. Curl_crealloc = r;
  220. Curl_ccalloc = c;
  221. /* Call the actual init function, but without setting */
  222. result = global_init(flags, FALSE);
  223. global_init_unlock();
  224. return result;
  225. }
  226. /**
  227. * curl_global_cleanup() globally cleanups curl, uses the value of
  228. * "easy_init_flags" to determine what needs to be cleaned up and what does
  229. * not.
  230. */
  231. void curl_global_cleanup(void)
  232. {
  233. global_init_lock();
  234. if(!initialized) {
  235. global_init_unlock();
  236. return;
  237. }
  238. if(--initialized) {
  239. global_init_unlock();
  240. return;
  241. }
  242. Curl_ssl_cleanup();
  243. Curl_async_global_cleanup();
  244. #ifdef _WIN32
  245. Curl_win32_cleanup(easy_init_flags);
  246. #endif
  247. Curl_amiga_cleanup();
  248. Curl_ssh_cleanup();
  249. #ifdef DEBUGBUILD
  250. free(leakpointer);
  251. #endif
  252. easy_init_flags = 0;
  253. global_init_unlock();
  254. }
  255. /**
  256. * curl_global_trace() globally initializes curl logging.
  257. */
  258. CURLcode curl_global_trace(const char *config)
  259. {
  260. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  261. CURLcode result;
  262. global_init_lock();
  263. result = Curl_trc_opt(config);
  264. global_init_unlock();
  265. return result;
  266. #else
  267. (void)config;
  268. return CURLE_OK;
  269. #endif
  270. }
  271. /*
  272. * curl_global_sslset() globally initializes the SSL backend to use.
  273. */
  274. CURLsslset curl_global_sslset(curl_sslbackend id, const char *name,
  275. const curl_ssl_backend ***avail)
  276. {
  277. CURLsslset rc;
  278. global_init_lock();
  279. rc = Curl_init_sslset_nolock(id, name, avail);
  280. global_init_unlock();
  281. return rc;
  282. }
  283. /*
  284. * curl_easy_init() is the external interface to alloc, setup and init an
  285. * easy handle that is returned. If anything goes wrong, NULL is returned.
  286. */
  287. CURL *curl_easy_init(void)
  288. {
  289. CURLcode result;
  290. struct Curl_easy *data;
  291. /* Make sure we inited the global SSL stuff */
  292. global_init_lock();
  293. if(!initialized) {
  294. result = global_init(CURL_GLOBAL_DEFAULT, TRUE);
  295. if(result) {
  296. /* something in the global init failed, return nothing */
  297. DEBUGF(curl_mfprintf(stderr, "Error: curl_global_init failed\n"));
  298. global_init_unlock();
  299. return NULL;
  300. }
  301. }
  302. global_init_unlock();
  303. /* We use curl_open() with undefined URL so far */
  304. result = Curl_open(&data);
  305. if(result) {
  306. DEBUGF(curl_mfprintf(stderr, "Error: Curl_open failed\n"));
  307. return NULL;
  308. }
  309. return data;
  310. }
  311. #ifdef DEBUGBUILD
  312. struct socketmonitor {
  313. struct socketmonitor *next; /* the next node in the list or NULL */
  314. struct pollfd socket; /* socket info of what to monitor */
  315. };
  316. struct events {
  317. long ms; /* timeout, run the timeout function when reached */
  318. bool msbump; /* set TRUE when timeout is set by callback */
  319. int num_sockets; /* number of nodes in the monitor list */
  320. struct socketmonitor *list; /* list of sockets to monitor */
  321. int running_handles; /* store the returned number */
  322. };
  323. #define DEBUG_EV_POLL 0
  324. /* events_timer
  325. *
  326. * Callback that gets called with a new value when the timeout should be
  327. * updated.
  328. */
  329. static int events_timer(CURLM *multi, /* multi handle */
  330. long timeout_ms, /* see above */
  331. void *userp) /* private callback pointer */
  332. {
  333. struct events *ev = userp;
  334. (void)multi;
  335. #if DEBUG_EV_POLL
  336. curl_mfprintf(stderr, "events_timer: set timeout %ldms\n", timeout_ms);
  337. #endif
  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(CURL *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. bool found = FALSE;
  387. struct Curl_easy *data = easy;
  388. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  389. (void)easy;
  390. #endif
  391. (void)socketp;
  392. m = ev->list;
  393. while(m) {
  394. if(m->socket.fd == s) {
  395. found = TRUE;
  396. if(what == CURL_POLL_REMOVE) {
  397. struct socketmonitor *nxt = m->next;
  398. /* remove this node from the list of monitored sockets */
  399. if(prev)
  400. prev->next = nxt;
  401. else
  402. ev->list = nxt;
  403. free(m);
  404. infof(data, "socket cb: socket %" FMT_SOCKET_T " REMOVED", s);
  405. }
  406. else {
  407. /* The socket 's' is already being monitored, update the activity
  408. mask. Convert from libcurl bitmask to the poll one. */
  409. m->socket.events = socketcb2poll(what);
  410. infof(data, "socket cb: socket %" FMT_SOCKET_T
  411. " UPDATED as %s%s", s,
  412. (what&CURL_POLL_IN) ? "IN" : "",
  413. (what&CURL_POLL_OUT) ? "OUT" : "");
  414. }
  415. break;
  416. }
  417. prev = m;
  418. m = m->next; /* move to next node */
  419. }
  420. if(!found) {
  421. if(what == CURL_POLL_REMOVE) {
  422. /* should not happen if our logic is correct, but is no drama. */
  423. DEBUGF(infof(data, "socket cb: asked to REMOVE socket %"
  424. FMT_SOCKET_T "but not present!", s));
  425. DEBUGASSERT(0);
  426. }
  427. else {
  428. m = malloc(sizeof(struct socketmonitor));
  429. if(m) {
  430. m->next = ev->list;
  431. m->socket.fd = s;
  432. m->socket.events = socketcb2poll(what);
  433. m->socket.revents = 0;
  434. ev->list = m;
  435. infof(data, "socket cb: socket %" FMT_SOCKET_T " ADDED as %s%s", s,
  436. (what&CURL_POLL_IN) ? "IN" : "",
  437. (what&CURL_POLL_OUT) ? "OUT" : "");
  438. }
  439. else
  440. return CURLE_OUT_OF_MEMORY;
  441. }
  442. }
  443. return 0;
  444. }
  445. /*
  446. * events_setup()
  447. *
  448. * Do the multi handle setups that only event-based transfers need.
  449. */
  450. static void events_setup(struct Curl_multi *multi, struct events *ev)
  451. {
  452. /* timer callback */
  453. curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, events_timer);
  454. curl_multi_setopt(multi, CURLMOPT_TIMERDATA, ev);
  455. /* socket callback */
  456. curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, events_socket);
  457. curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev);
  458. }
  459. /* populate_fds()
  460. *
  461. * populate the fds[] array
  462. */
  463. static unsigned int populate_fds(struct pollfd *fds, struct events *ev)
  464. {
  465. unsigned int numfds = 0;
  466. struct pollfd *f;
  467. struct socketmonitor *m;
  468. f = &fds[0];
  469. for(m = ev->list; m; m = m->next) {
  470. f->fd = m->socket.fd;
  471. f->events = m->socket.events;
  472. f->revents = 0;
  473. #if DEBUG_EV_POLL
  474. curl_mfprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd);
  475. #endif
  476. f++;
  477. numfds++;
  478. }
  479. return numfds;
  480. }
  481. /* poll_fds()
  482. *
  483. * poll the fds[] array
  484. */
  485. static CURLcode poll_fds(struct events *ev,
  486. struct pollfd *fds,
  487. const unsigned int numfds,
  488. int *pollrc)
  489. {
  490. if(numfds) {
  491. /* wait for activity or timeout */
  492. #if DEBUG_EV_POLL
  493. curl_mfprintf(stderr, "poll(numfds=%u, timeout=%ldms)\n", numfds, ev->ms);
  494. #endif
  495. *pollrc = Curl_poll(fds, numfds, ev->ms);
  496. #if DEBUG_EV_POLL
  497. curl_mfprintf(stderr, "poll(numfds=%u, timeout=%ldms) -> %d\n",
  498. numfds, ev->ms, *pollrc);
  499. #endif
  500. if(*pollrc < 0)
  501. return CURLE_UNRECOVERABLE_POLL;
  502. }
  503. else {
  504. #if DEBUG_EV_POLL
  505. curl_mfprintf(stderr, "poll, but no fds, wait timeout=%ldms\n", ev->ms);
  506. #endif
  507. *pollrc = 0;
  508. if(ev->ms > 0)
  509. curlx_wait_ms(ev->ms);
  510. }
  511. return CURLE_OK;
  512. }
  513. /* wait_or_timeout()
  514. *
  515. * waits for activity on any of the given sockets, or the timeout to trigger.
  516. */
  517. static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
  518. {
  519. bool done = FALSE;
  520. CURLMcode mcode = CURLM_OK;
  521. CURLcode result = CURLE_OK;
  522. while(!done) {
  523. CURLMsg *msg;
  524. struct pollfd fds[4];
  525. int pollrc;
  526. struct curltime before;
  527. const unsigned int numfds = populate_fds(fds, ev);
  528. /* get the time stamp to use to figure out how long poll takes */
  529. before = curlx_now();
  530. result = poll_fds(ev, fds, numfds, &pollrc);
  531. if(result)
  532. return result;
  533. ev->msbump = FALSE; /* reset here */
  534. if(!pollrc) {
  535. /* timeout! */
  536. ev->ms = 0;
  537. /* curl_mfprintf(stderr, "call curl_multi_socket_action(TIMEOUT)\n"); */
  538. mcode = curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0,
  539. &ev->running_handles);
  540. }
  541. else {
  542. /* here pollrc is > 0 */
  543. /* loop over the monitored sockets to see which ones had activity */
  544. unsigned int i;
  545. for(i = 0; i < numfds; i++) {
  546. if(fds[i].revents) {
  547. /* socket activity, tell libcurl */
  548. int act = poll2cselect(fds[i].revents); /* convert */
  549. /* sending infof "randomly" to the first easy handle */
  550. infof(multi->admin, "call curl_multi_socket_action(socket "
  551. "%" FMT_SOCKET_T ")", (curl_socket_t)fds[i].fd);
  552. mcode = curl_multi_socket_action(multi, fds[i].fd, act,
  553. &ev->running_handles);
  554. }
  555. }
  556. if(!ev->msbump && ev->ms >= 0) {
  557. /* If nothing updated the timeout, we decrease it by the spent time.
  558. * If it was updated, it has the new timeout time stored already.
  559. */
  560. timediff_t timediff = curlx_timediff(curlx_now(), before);
  561. if(timediff > 0) {
  562. #if DEBUG_EV_POLL
  563. curl_mfprintf(stderr, "poll timeout %ldms not updated, decrease by "
  564. "time spent %ldms\n", ev->ms, (long)timediff);
  565. #endif
  566. if(timediff > ev->ms)
  567. ev->ms = 0;
  568. else
  569. ev->ms -= (long)timediff;
  570. }
  571. }
  572. }
  573. if(mcode)
  574. return CURLE_URL_MALFORMAT;
  575. /* we do not really care about the "msgs_in_queue" value returned in the
  576. second argument */
  577. msg = curl_multi_info_read(multi, &pollrc);
  578. if(msg) {
  579. result = msg->data.result;
  580. done = TRUE;
  581. }
  582. }
  583. return result;
  584. }
  585. /* easy_events()
  586. *
  587. * Runs a transfer in a blocking manner using the events-based API
  588. */
  589. static CURLcode easy_events(struct Curl_multi *multi)
  590. {
  591. /* this struct is made static to allow it to be used after this function
  592. returns and curl_multi_remove_handle() is called */
  593. static struct events evs = {-1, FALSE, 0, NULL, 0};
  594. /* if running event-based, do some further multi inits */
  595. events_setup(multi, &evs);
  596. return wait_or_timeout(multi, &evs);
  597. }
  598. #else /* DEBUGBUILD */
  599. /* when not built with debug, this function does not exist */
  600. #define easy_events(x) CURLE_NOT_BUILT_IN
  601. #endif
  602. static CURLcode easy_transfer(struct Curl_multi *multi)
  603. {
  604. bool done = FALSE;
  605. CURLMcode mcode = CURLM_OK;
  606. CURLcode result = CURLE_OK;
  607. while(!done && !mcode) {
  608. int still_running = 0;
  609. mcode = curl_multi_poll(multi, NULL, 0, 1000, NULL);
  610. if(!mcode)
  611. mcode = curl_multi_perform(multi, &still_running);
  612. /* only read 'still_running' if curl_multi_perform() return OK */
  613. if(!mcode && !still_running) {
  614. int rc;
  615. CURLMsg *msg = curl_multi_info_read(multi, &rc);
  616. if(msg) {
  617. result = msg->data.result;
  618. done = TRUE;
  619. }
  620. }
  621. }
  622. /* Make sure to return some kind of error if there was a multi problem */
  623. if(mcode) {
  624. result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
  625. /* The other multi errors should never happen, so return
  626. something suitably generic */
  627. CURLE_BAD_FUNCTION_ARGUMENT;
  628. }
  629. return result;
  630. }
  631. /*
  632. * easy_perform() is the internal interface that performs a blocking
  633. * transfer as previously setup.
  634. *
  635. * CONCEPT: This function creates a multi handle, adds the easy handle to it,
  636. * runs curl_multi_perform() until the transfer is done, then detaches the
  637. * easy handle, destroys the multi handle and returns the easy handle's return
  638. * code.
  639. *
  640. * REALITY: it cannot just create and destroy the multi handle that easily. It
  641. * needs to keep it around since if this easy handle is used again by this
  642. * function, the same multi handle must be reused so that the same pools and
  643. * caches can be used.
  644. *
  645. * DEBUG: if 'events' is set TRUE, this function will use a replacement engine
  646. * instead of curl_multi_perform() and use curl_multi_socket_action().
  647. */
  648. static CURLcode easy_perform(struct Curl_easy *data, bool events)
  649. {
  650. struct Curl_multi *multi;
  651. CURLMcode mcode;
  652. CURLcode result = CURLE_OK;
  653. SIGPIPE_VARIABLE(pipe_st);
  654. if(!data)
  655. return CURLE_BAD_FUNCTION_ARGUMENT;
  656. if(data->set.errorbuffer)
  657. /* clear this as early as possible */
  658. data->set.errorbuffer[0] = 0;
  659. data->state.os_errno = 0;
  660. if(data->multi) {
  661. failf(data, "easy handle already used in multi handle");
  662. return CURLE_FAILED_INIT;
  663. }
  664. /* if the handle has a connection still attached (it is/was a connect-only
  665. handle) then disconnect before performing */
  666. if(data->conn) {
  667. struct connectdata *c;
  668. curl_socket_t s;
  669. Curl_detach_connection(data);
  670. s = Curl_getconnectinfo(data, &c);
  671. if((s != CURL_SOCKET_BAD) && c) {
  672. Curl_conn_terminate(data, c, TRUE);
  673. }
  674. DEBUGASSERT(!data->conn);
  675. }
  676. if(data->multi_easy)
  677. multi = data->multi_easy;
  678. else {
  679. /* this multi handle will only ever have a single easy handle attached to
  680. it, so make it use minimal hash sizes */
  681. multi = Curl_multi_handle(16, 1, 3, 7, 3);
  682. if(!multi)
  683. return CURLE_OUT_OF_MEMORY;
  684. }
  685. if(multi->in_callback)
  686. return CURLE_RECURSIVE_API_CALL;
  687. /* Copy the MAXCONNECTS option to the multi handle */
  688. curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, (long)data->set.maxconnects);
  689. data->multi_easy = NULL; /* pretend it does not exist */
  690. mcode = curl_multi_add_handle(multi, data);
  691. if(mcode) {
  692. curl_multi_cleanup(multi);
  693. if(mcode == CURLM_OUT_OF_MEMORY)
  694. return CURLE_OUT_OF_MEMORY;
  695. return CURLE_FAILED_INIT;
  696. }
  697. /* assign this after curl_multi_add_handle() */
  698. data->multi_easy = multi;
  699. sigpipe_init(&pipe_st);
  700. sigpipe_apply(data, &pipe_st);
  701. /* run the transfer */
  702. result = events ? easy_events(multi) : easy_transfer(multi);
  703. /* ignoring the return code is not nice, but atm we cannot really handle
  704. a failure here, room for future improvement! */
  705. (void)curl_multi_remove_handle(multi, data);
  706. sigpipe_restore(&pipe_st);
  707. /* The multi handle is kept alive, owned by the easy handle */
  708. return result;
  709. }
  710. /*
  711. * curl_easy_perform() is the external interface that performs a blocking
  712. * transfer as previously setup.
  713. */
  714. CURLcode curl_easy_perform(CURL *data)
  715. {
  716. return easy_perform(data, FALSE);
  717. }
  718. #ifdef DEBUGBUILD
  719. /*
  720. * curl_easy_perform_ev() is the external interface that performs a blocking
  721. * transfer using the event-based API internally.
  722. */
  723. CURLcode curl_easy_perform_ev(struct Curl_easy *data)
  724. {
  725. return easy_perform(data, TRUE);
  726. }
  727. #endif
  728. /*
  729. * curl_easy_cleanup() is the external interface to cleaning/freeing the given
  730. * easy handle.
  731. */
  732. void curl_easy_cleanup(CURL *ptr)
  733. {
  734. struct Curl_easy *data = ptr;
  735. if(GOOD_EASY_HANDLE(data)) {
  736. SIGPIPE_VARIABLE(pipe_st);
  737. sigpipe_ignore(data, &pipe_st);
  738. Curl_close(&data);
  739. sigpipe_restore(&pipe_st);
  740. }
  741. }
  742. /*
  743. * curl_easy_getinfo() is an external interface that allows an app to retrieve
  744. * information from a performed transfer and similar.
  745. */
  746. #undef curl_easy_getinfo
  747. CURLcode curl_easy_getinfo(CURL *easy, CURLINFO info, ...)
  748. {
  749. struct Curl_easy *data = easy;
  750. va_list arg;
  751. void *paramp;
  752. CURLcode result;
  753. if(!GOOD_EASY_HANDLE(data))
  754. return CURLE_BAD_FUNCTION_ARGUMENT;
  755. va_start(arg, info);
  756. paramp = va_arg(arg, void *);
  757. result = Curl_getinfo(data, info, paramp);
  758. va_end(arg);
  759. return result;
  760. }
  761. static CURLcode dupset(struct Curl_easy *dst, struct Curl_easy *src)
  762. {
  763. CURLcode result = CURLE_OK;
  764. enum dupstring i;
  765. enum dupblob j;
  766. /* Copy src->set into dst->set first, then deal with the strings
  767. afterwards */
  768. dst->set = src->set;
  769. Curl_mime_initpart(&dst->set.mimepost);
  770. /* clear all dest string and blob pointers first, in case we error out
  771. mid-function */
  772. memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
  773. memset(dst->set.blobs, 0, BLOB_LAST * sizeof(struct curl_blob *));
  774. /* duplicate all strings */
  775. for(i = (enum dupstring)0; i < STRING_LASTZEROTERMINATED; i++) {
  776. result = Curl_setstropt(&dst->set.str[i], src->set.str[i]);
  777. if(result)
  778. return result;
  779. }
  780. /* duplicate all blobs */
  781. for(j = (enum dupblob)0; j < BLOB_LAST; j++) {
  782. result = Curl_setblobopt(&dst->set.blobs[j], src->set.blobs[j]);
  783. if(result)
  784. return result;
  785. }
  786. /* duplicate memory areas pointed to */
  787. i = STRING_COPYPOSTFIELDS;
  788. if(src->set.str[i]) {
  789. if(src->set.postfieldsize == -1)
  790. dst->set.str[i] = strdup(src->set.str[i]);
  791. else
  792. /* postfieldsize is curl_off_t, Curl_memdup() takes a size_t ... */
  793. dst->set.str[i] = Curl_memdup(src->set.str[i],
  794. curlx_sotouz(src->set.postfieldsize));
  795. if(!dst->set.str[i])
  796. return CURLE_OUT_OF_MEMORY;
  797. /* point to the new copy */
  798. dst->set.postfields = dst->set.str[i];
  799. }
  800. /* Duplicate mime data. */
  801. result = Curl_mime_duppart(dst, &dst->set.mimepost, &src->set.mimepost);
  802. if(src->set.resolve)
  803. dst->state.resolve = dst->set.resolve;
  804. return result;
  805. }
  806. static void dupeasy_meta_freeentry(void *p)
  807. {
  808. (void)p;
  809. /* Will always be FALSE. Cannot use a 0 assert here since compilers
  810. * are not in agreement if they then want a NORETURN attribute or
  811. * not. *sigh* */
  812. DEBUGASSERT(p == NULL);
  813. }
  814. /*
  815. * curl_easy_duphandle() is an external interface to allow duplication of a
  816. * given input easy handle. The returned handle will be a new working handle
  817. * with all options set exactly as the input source handle.
  818. */
  819. CURL *curl_easy_duphandle(CURL *d)
  820. {
  821. struct Curl_easy *data = d;
  822. struct Curl_easy *outcurl = NULL;
  823. if(!GOOD_EASY_HANDLE(data))
  824. goto fail;
  825. outcurl = calloc(1, sizeof(struct Curl_easy));
  826. if(!outcurl)
  827. goto fail;
  828. /*
  829. * We setup a few buffers we need. We should probably make them
  830. * get setup on-demand in the code, as that would probably decrease
  831. * the likeliness of us forgetting to init a buffer here in the future.
  832. */
  833. outcurl->set.buffer_size = data->set.buffer_size;
  834. Curl_hash_init(&outcurl->meta_hash, 23,
  835. Curl_hash_str, curlx_str_key_compare, dupeasy_meta_freeentry);
  836. curlx_dyn_init(&outcurl->state.headerb, CURL_MAX_HTTP_HEADER);
  837. Curl_netrc_init(&outcurl->state.netrc);
  838. /* the connection pool is setup on demand */
  839. outcurl->state.lastconnect_id = -1;
  840. outcurl->state.recent_conn_id = -1;
  841. outcurl->id = -1;
  842. outcurl->mid = UINT_MAX;
  843. outcurl->master_mid = UINT_MAX;
  844. #ifndef CURL_DISABLE_HTTP
  845. Curl_llist_init(&outcurl->state.httphdrs, NULL);
  846. #endif
  847. Curl_initinfo(outcurl);
  848. /* copy all userdefined values */
  849. if(dupset(outcurl, data))
  850. goto fail;
  851. outcurl->progress.hide = data->progress.hide;
  852. outcurl->progress.callback = data->progress.callback;
  853. #ifndef CURL_DISABLE_COOKIES
  854. outcurl->state.cookielist = NULL;
  855. if(data->cookies && data->state.cookie_engine) {
  856. /* If cookies are enabled in the parent handle, we enable them
  857. in the clone as well! */
  858. outcurl->cookies = Curl_cookie_init(outcurl, NULL, outcurl->cookies,
  859. data->set.cookiesession);
  860. if(!outcurl->cookies)
  861. goto fail;
  862. }
  863. if(data->state.cookielist) {
  864. outcurl->state.cookielist = Curl_slist_duplicate(data->state.cookielist);
  865. if(!outcurl->state.cookielist)
  866. goto fail;
  867. }
  868. #endif
  869. if(data->state.url) {
  870. outcurl->state.url = strdup(data->state.url);
  871. if(!outcurl->state.url)
  872. goto fail;
  873. outcurl->state.url_alloc = TRUE;
  874. }
  875. if(data->state.referer) {
  876. outcurl->state.referer = strdup(data->state.referer);
  877. if(!outcurl->state.referer)
  878. goto fail;
  879. outcurl->state.referer_alloc = TRUE;
  880. }
  881. /* Reinitialize an SSL engine for the new handle
  882. * note: the engine name has already been copied by dupset */
  883. if(outcurl->set.str[STRING_SSL_ENGINE]) {
  884. if(Curl_ssl_set_engine(outcurl, outcurl->set.str[STRING_SSL_ENGINE]))
  885. goto fail;
  886. }
  887. #ifndef CURL_DISABLE_ALTSVC
  888. if(data->asi) {
  889. outcurl->asi = Curl_altsvc_init();
  890. if(!outcurl->asi)
  891. goto fail;
  892. if(outcurl->set.str[STRING_ALTSVC])
  893. (void)Curl_altsvc_load(outcurl->asi, outcurl->set.str[STRING_ALTSVC]);
  894. }
  895. #endif
  896. #ifndef CURL_DISABLE_HSTS
  897. if(data->hsts) {
  898. outcurl->hsts = Curl_hsts_init();
  899. if(!outcurl->hsts)
  900. goto fail;
  901. if(outcurl->set.str[STRING_HSTS])
  902. (void)Curl_hsts_loadfile(outcurl,
  903. outcurl->hsts, outcurl->set.str[STRING_HSTS]);
  904. (void)Curl_hsts_loadcb(outcurl, outcurl->hsts);
  905. }
  906. #endif
  907. outcurl->magic = CURLEASY_MAGIC_NUMBER;
  908. /* we reach this point and thus we are OK */
  909. return outcurl;
  910. fail:
  911. if(outcurl) {
  912. #ifndef CURL_DISABLE_COOKIES
  913. free(outcurl->cookies);
  914. #endif
  915. curlx_dyn_free(&outcurl->state.headerb);
  916. Curl_altsvc_cleanup(&outcurl->asi);
  917. Curl_hsts_cleanup(&outcurl->hsts);
  918. Curl_freeset(outcurl);
  919. free(outcurl);
  920. }
  921. return NULL;
  922. }
  923. /*
  924. * curl_easy_reset() is an external interface that allows an app to re-
  925. * initialize a session handle to the default values.
  926. */
  927. void curl_easy_reset(CURL *d)
  928. {
  929. struct Curl_easy *data = d;
  930. if(!GOOD_EASY_HANDLE(data))
  931. return;
  932. Curl_req_hard_reset(&data->req, data);
  933. Curl_hash_clean(&data->meta_hash);
  934. /* clear all meta data */
  935. Curl_meta_reset(data);
  936. /* clear any resolve data */
  937. Curl_async_shutdown(data);
  938. Curl_resolv_unlink(data, &data->state.dns[0]);
  939. Curl_resolv_unlink(data, &data->state.dns[1]);
  940. /* zero out UserDefined data: */
  941. Curl_freeset(data);
  942. memset(&data->set, 0, sizeof(struct UserDefined));
  943. Curl_init_userdefined(data);
  944. /* zero out Progress data: */
  945. memset(&data->progress, 0, sizeof(struct Progress));
  946. /* zero out PureInfo data: */
  947. Curl_initinfo(data);
  948. data->progress.hide = TRUE;
  949. data->state.current_speed = -1; /* init to negative == impossible */
  950. data->state.recent_conn_id = -1; /* clear remembered connection id */
  951. /* zero out authentication data: */
  952. memset(&data->state.authhost, 0, sizeof(struct auth));
  953. memset(&data->state.authproxy, 0, sizeof(struct auth));
  954. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_DIGEST_AUTH)
  955. Curl_http_auth_cleanup_digest(data);
  956. #endif
  957. data->master_mid = UINT_MAX;
  958. }
  959. /*
  960. * curl_easy_pause() allows an application to pause or unpause a specific
  961. * transfer and direction. This function sets the full new state for the
  962. * current connection this easy handle operates on.
  963. *
  964. * NOTE: if you have the receiving paused and you call this function to remove
  965. * the pausing, you may get your write callback called at this point.
  966. *
  967. * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
  968. *
  969. * NOTE: This is one of few API functions that are allowed to be called from
  970. * within a callback.
  971. */
  972. CURLcode curl_easy_pause(CURL *d, int action)
  973. {
  974. CURLcode result = CURLE_OK;
  975. bool recursive = FALSE;
  976. bool changed = FALSE;
  977. struct Curl_easy *data = d;
  978. bool recv_paused, recv_paused_new;
  979. bool send_paused, send_paused_new;
  980. if(!GOOD_EASY_HANDLE(data) || !data->conn)
  981. /* crazy input, do not continue */
  982. return CURLE_BAD_FUNCTION_ARGUMENT;
  983. if(Curl_is_in_callback(data))
  984. recursive = TRUE;
  985. recv_paused = Curl_xfer_recv_is_paused(data);
  986. recv_paused_new = (action & CURLPAUSE_RECV);
  987. send_paused = Curl_xfer_send_is_paused(data);
  988. send_paused_new = (action & CURLPAUSE_SEND);
  989. if((send_paused != send_paused_new) ||
  990. (send_paused_new != Curl_creader_is_paused(data))) {
  991. changed = TRUE;
  992. result = Curl_1st_err(result, Curl_xfer_pause_send(data, send_paused_new));
  993. }
  994. if(recv_paused != recv_paused_new) {
  995. changed = TRUE;
  996. result = Curl_1st_err(result, Curl_xfer_pause_recv(data, recv_paused_new));
  997. }
  998. /* If not completely pausing both directions now, run again in any case. */
  999. if(!Curl_xfer_is_blocked(data)) {
  1000. /* reset the too-slow time keeper */
  1001. data->state.keeps_speed.tv_sec = 0;
  1002. if(data->multi) {
  1003. Curl_multi_mark_dirty(data); /* make it run */
  1004. /* On changes, tell application to update its timers. */
  1005. if(changed) {
  1006. if(Curl_update_timer(data->multi) && !result)
  1007. result = CURLE_ABORTED_BY_CALLBACK;
  1008. }
  1009. }
  1010. }
  1011. if(!result && changed && !data->state.done && data->multi)
  1012. /* pause/unpausing may result in multi event changes */
  1013. if(Curl_multi_ev_assess_xfer(data->multi, data) && !result)
  1014. result = CURLE_ABORTED_BY_CALLBACK;
  1015. if(recursive)
  1016. /* this might have called a callback recursively which might have set this
  1017. to false again on exit */
  1018. Curl_set_in_callback(data, TRUE);
  1019. return result;
  1020. }
  1021. static CURLcode easy_connection(struct Curl_easy *data,
  1022. struct connectdata **connp)
  1023. {
  1024. curl_socket_t sfd;
  1025. if(!data)
  1026. return CURLE_BAD_FUNCTION_ARGUMENT;
  1027. /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
  1028. if(!data->set.connect_only) {
  1029. failf(data, "CONNECT_ONLY is required");
  1030. return CURLE_UNSUPPORTED_PROTOCOL;
  1031. }
  1032. sfd = Curl_getconnectinfo(data, connp);
  1033. if(sfd == CURL_SOCKET_BAD) {
  1034. failf(data, "Failed to get recent socket");
  1035. return CURLE_UNSUPPORTED_PROTOCOL;
  1036. }
  1037. return CURLE_OK;
  1038. }
  1039. /*
  1040. * Receives data from the connected socket. Use after successful
  1041. * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
  1042. * Returns CURLE_OK on success, error code on error.
  1043. */
  1044. CURLcode curl_easy_recv(CURL *d, void *buffer, size_t buflen, size_t *n)
  1045. {
  1046. CURLcode result;
  1047. struct connectdata *c;
  1048. struct Curl_easy *data = d;
  1049. if(!GOOD_EASY_HANDLE(data))
  1050. return CURLE_BAD_FUNCTION_ARGUMENT;
  1051. if(Curl_is_in_callback(data))
  1052. return CURLE_RECURSIVE_API_CALL;
  1053. result = easy_connection(data, &c);
  1054. if(result)
  1055. return result;
  1056. if(!data->conn)
  1057. /* on first invoke, the transfer has been detached from the connection and
  1058. needs to be reattached */
  1059. Curl_attach_connection(data, c);
  1060. *n = 0;
  1061. return Curl_conn_recv(data, FIRSTSOCKET, buffer, buflen, n);
  1062. }
  1063. #ifndef CURL_DISABLE_WEBSOCKETS
  1064. CURLcode Curl_connect_only_attach(struct Curl_easy *data)
  1065. {
  1066. CURLcode result;
  1067. struct connectdata *c = NULL;
  1068. result = easy_connection(data, &c);
  1069. if(result)
  1070. return result;
  1071. if(!data->conn)
  1072. /* on first invoke, the transfer has been detached from the connection and
  1073. needs to be reattached */
  1074. Curl_attach_connection(data, c);
  1075. return CURLE_OK;
  1076. }
  1077. #endif /* !CURL_DISABLE_WEBSOCKETS */
  1078. /*
  1079. * Sends data over the connected socket.
  1080. *
  1081. * This is the private internal version of curl_easy_send()
  1082. */
  1083. CURLcode Curl_senddata(struct Curl_easy *data, const void *buffer,
  1084. size_t buflen, size_t *n)
  1085. {
  1086. CURLcode result;
  1087. struct connectdata *c = NULL;
  1088. SIGPIPE_VARIABLE(pipe_st);
  1089. *n = 0;
  1090. result = easy_connection(data, &c);
  1091. if(result)
  1092. return result;
  1093. if(!data->conn)
  1094. /* on first invoke, the transfer has been detached from the connection and
  1095. needs to be reattached */
  1096. Curl_attach_connection(data, c);
  1097. sigpipe_ignore(data, &pipe_st);
  1098. result = Curl_conn_send(data, FIRSTSOCKET, buffer, buflen, FALSE, n);
  1099. sigpipe_restore(&pipe_st);
  1100. if(result && result != CURLE_AGAIN)
  1101. return CURLE_SEND_ERROR;
  1102. return result;
  1103. }
  1104. /*
  1105. * Sends data over the connected socket. Use after successful
  1106. * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
  1107. */
  1108. CURLcode curl_easy_send(CURL *d, const void *buffer, size_t buflen, size_t *n)
  1109. {
  1110. size_t written = 0;
  1111. CURLcode result;
  1112. struct Curl_easy *data = d;
  1113. if(!GOOD_EASY_HANDLE(data))
  1114. return CURLE_BAD_FUNCTION_ARGUMENT;
  1115. if(Curl_is_in_callback(data))
  1116. return CURLE_RECURSIVE_API_CALL;
  1117. result = Curl_senddata(data, buffer, buflen, &written);
  1118. *n = written;
  1119. return result;
  1120. }
  1121. /*
  1122. * Performs connection upkeep for the given session handle.
  1123. */
  1124. CURLcode curl_easy_upkeep(CURL *d)
  1125. {
  1126. struct Curl_easy *data = d;
  1127. /* Verify that we got an easy handle we can work with. */
  1128. if(!GOOD_EASY_HANDLE(data))
  1129. return CURLE_BAD_FUNCTION_ARGUMENT;
  1130. if(Curl_is_in_callback(data))
  1131. return CURLE_RECURSIVE_API_CALL;
  1132. /* Use the common function to keep connections alive. */
  1133. return Curl_cpool_upkeep(data);
  1134. }
  1135. CURLcode curl_easy_ssls_import(CURL *d, const char *session_key,
  1136. const unsigned char *shmac, size_t shmac_len,
  1137. const unsigned char *sdata, size_t sdata_len)
  1138. {
  1139. #if defined(USE_SSL) && defined(USE_SSLS_EXPORT)
  1140. struct Curl_easy *data = d;
  1141. if(!GOOD_EASY_HANDLE(data))
  1142. return CURLE_BAD_FUNCTION_ARGUMENT;
  1143. return Curl_ssl_session_import(data, session_key,
  1144. shmac, shmac_len, sdata, sdata_len);
  1145. #else
  1146. (void)d;
  1147. (void)session_key;
  1148. (void)shmac;
  1149. (void)shmac_len;
  1150. (void)sdata;
  1151. (void)sdata_len;
  1152. return CURLE_NOT_BUILT_IN;
  1153. #endif
  1154. }
  1155. CURLcode curl_easy_ssls_export(CURL *d,
  1156. curl_ssls_export_cb *export_fn,
  1157. void *userptr)
  1158. {
  1159. #if defined(USE_SSL) && defined(USE_SSLS_EXPORT)
  1160. struct Curl_easy *data = d;
  1161. if(!GOOD_EASY_HANDLE(data))
  1162. return CURLE_BAD_FUNCTION_ARGUMENT;
  1163. return Curl_ssl_session_export(data, export_fn, userptr);
  1164. #else
  1165. (void)d;
  1166. (void)export_fn;
  1167. (void)userptr;
  1168. return CURLE_NOT_BUILT_IN;
  1169. #endif
  1170. }
  1171. CURLcode Curl_meta_set(struct Curl_easy *data, const char *key,
  1172. void *meta_data, Curl_meta_dtor *meta_dtor)
  1173. {
  1174. DEBUGASSERT(meta_data); /* never set to NULL */
  1175. if(!Curl_hash_add2(&data->meta_hash, CURL_UNCONST(key), strlen(key) + 1,
  1176. meta_data, meta_dtor)) {
  1177. meta_dtor(CURL_UNCONST(key), strlen(key) + 1, meta_data);
  1178. return CURLE_OUT_OF_MEMORY;
  1179. }
  1180. return CURLE_OK;
  1181. }
  1182. void Curl_meta_remove(struct Curl_easy *data, const char *key)
  1183. {
  1184. Curl_hash_delete(&data->meta_hash, CURL_UNCONST(key), strlen(key) + 1);
  1185. }
  1186. void *Curl_meta_get(struct Curl_easy *data, const char *key)
  1187. {
  1188. return Curl_hash_pick(&data->meta_hash, CURL_UNCONST(key), strlen(key) + 1);
  1189. }
  1190. void Curl_meta_reset(struct Curl_easy *data)
  1191. {
  1192. Curl_hash_clean(&data->meta_hash);
  1193. }