easy.c 31 KB

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