easy.c 29 KB

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