easy.c 36 KB

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