2
0

easy.c 33 KB

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