easy.c 31 KB

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