easy.c 29 KB

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