easy.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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 writable 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 = CURLM_OK;
  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. time_t timediff = curlx_tvdiff(after, before);
  533. if(timediff > 0) {
  534. if(timediff > ev->ms)
  535. ev->ms = 0;
  536. else
  537. ev->ms -= (long)timediff;
  538. }
  539. }
  540. }
  541. else
  542. return CURLE_RECV_ERROR;
  543. if(mcode)
  544. return CURLE_URL_MALFORMAT; /* TODO: return a proper error! */
  545. /* we don't really care about the "msgs_in_queue" value returned in the
  546. second argument */
  547. msg = curl_multi_info_read(multi, &pollrc);
  548. if(msg) {
  549. result = msg->data.result;
  550. done = TRUE;
  551. }
  552. }
  553. return result;
  554. }
  555. /* easy_events()
  556. *
  557. * Runs a transfer in a blocking manner using the events-based API
  558. */
  559. static CURLcode easy_events(struct Curl_multi *multi)
  560. {
  561. struct events evs= {2, FALSE, 0, NULL, 0};
  562. /* if running event-based, do some further multi inits */
  563. events_setup(multi, &evs);
  564. return wait_or_timeout(multi, &evs);
  565. }
  566. #else /* CURLDEBUG */
  567. /* when not built with debug, this function doesn't exist */
  568. #define easy_events(x) CURLE_NOT_BUILT_IN
  569. #endif
  570. static CURLcode easy_transfer(struct Curl_multi *multi)
  571. {
  572. bool done = FALSE;
  573. CURLMcode mcode = CURLM_OK;
  574. CURLcode result = CURLE_OK;
  575. struct timeval before;
  576. int without_fds = 0; /* count number of consecutive returns from
  577. curl_multi_wait() without any filedescriptors */
  578. while(!done && !mcode) {
  579. int still_running = 0;
  580. int rc;
  581. before = curlx_tvnow();
  582. mcode = curl_multi_wait(multi, NULL, 0, 1000, &rc);
  583. if(!mcode) {
  584. if(!rc) {
  585. struct timeval after = curlx_tvnow();
  586. /* If it returns without any filedescriptor instantly, we need to
  587. avoid busy-looping during periods where it has nothing particular
  588. to wait for */
  589. if(curlx_tvdiff(after, before) <= 10) {
  590. without_fds++;
  591. if(without_fds > 2) {
  592. int sleep_ms = without_fds < 10 ? (1 << (without_fds - 1)) : 1000;
  593. Curl_wait_ms(sleep_ms);
  594. }
  595. }
  596. else
  597. /* it wasn't "instant", restart counter */
  598. without_fds = 0;
  599. }
  600. else
  601. /* got file descriptor, restart counter */
  602. without_fds = 0;
  603. mcode = curl_multi_perform(multi, &still_running);
  604. }
  605. /* only read 'still_running' if curl_multi_perform() return OK */
  606. if(!mcode && !still_running) {
  607. CURLMsg *msg = curl_multi_info_read(multi, &rc);
  608. if(msg) {
  609. result = msg->data.result;
  610. done = TRUE;
  611. }
  612. }
  613. }
  614. /* Make sure to return some kind of error if there was a multi problem */
  615. if(mcode) {
  616. result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
  617. /* The other multi errors should never happen, so return
  618. something suitably generic */
  619. CURLE_BAD_FUNCTION_ARGUMENT;
  620. }
  621. return result;
  622. }
  623. /*
  624. * easy_perform() is the external interface that performs a blocking
  625. * transfer as previously setup.
  626. *
  627. * CONCEPT: This function creates a multi handle, adds the easy handle to it,
  628. * runs curl_multi_perform() until the transfer is done, then detaches the
  629. * easy handle, destroys the multi handle and returns the easy handle's return
  630. * code.
  631. *
  632. * REALITY: it can't just create and destroy the multi handle that easily. It
  633. * needs to keep it around since if this easy handle is used again by this
  634. * function, the same multi handle must be re-used so that the same pools and
  635. * caches can be used.
  636. *
  637. * DEBUG: if 'events' is set TRUE, this function will use a replacement engine
  638. * instead of curl_multi_perform() and use curl_multi_socket_action().
  639. */
  640. static CURLcode easy_perform(struct Curl_easy *data, bool events)
  641. {
  642. struct Curl_multi *multi;
  643. CURLMcode mcode;
  644. CURLcode result = CURLE_OK;
  645. SIGPIPE_VARIABLE(pipe_st);
  646. if(!data)
  647. return CURLE_BAD_FUNCTION_ARGUMENT;
  648. if(data->multi) {
  649. failf(data, "easy handle already used in multi handle");
  650. return CURLE_FAILED_INIT;
  651. }
  652. if(data->multi_easy)
  653. multi = data->multi_easy;
  654. else {
  655. /* this multi handle will only ever have a single easy handled attached
  656. to it, so make it use minimal hashes */
  657. multi = Curl_multi_handle(1, 3);
  658. if(!multi)
  659. return CURLE_OUT_OF_MEMORY;
  660. data->multi_easy = multi;
  661. }
  662. /* Copy the MAXCONNECTS option to the multi handle */
  663. curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, data->set.maxconnects);
  664. mcode = curl_multi_add_handle(multi, data);
  665. if(mcode) {
  666. curl_multi_cleanup(multi);
  667. if(mcode == CURLM_OUT_OF_MEMORY)
  668. return CURLE_OUT_OF_MEMORY;
  669. return CURLE_FAILED_INIT;
  670. }
  671. sigpipe_ignore(data, &pipe_st);
  672. /* assign this after curl_multi_add_handle() since that function checks for
  673. it and rejects this handle otherwise */
  674. data->multi = multi;
  675. /* run the transfer */
  676. result = events ? easy_events(multi) : easy_transfer(multi);
  677. /* ignoring the return code isn't nice, but atm we can't really handle
  678. a failure here, room for future improvement! */
  679. (void)curl_multi_remove_handle(multi, data);
  680. sigpipe_restore(&pipe_st);
  681. /* The multi handle is kept alive, owned by the easy handle */
  682. return result;
  683. }
  684. /*
  685. * curl_easy_perform() is the external interface that performs a blocking
  686. * transfer as previously setup.
  687. */
  688. CURLcode curl_easy_perform(struct Curl_easy *data)
  689. {
  690. return easy_perform(data, FALSE);
  691. }
  692. #ifdef CURLDEBUG
  693. /*
  694. * curl_easy_perform_ev() is the external interface that performs a blocking
  695. * transfer using the event-based API internally.
  696. */
  697. CURLcode curl_easy_perform_ev(struct Curl_easy *data)
  698. {
  699. return easy_perform(data, TRUE);
  700. }
  701. #endif
  702. /*
  703. * curl_easy_cleanup() is the external interface to cleaning/freeing the given
  704. * easy handle.
  705. */
  706. void curl_easy_cleanup(struct Curl_easy *data)
  707. {
  708. SIGPIPE_VARIABLE(pipe_st);
  709. if(!data)
  710. return;
  711. sigpipe_ignore(data, &pipe_st);
  712. Curl_close(data);
  713. sigpipe_restore(&pipe_st);
  714. }
  715. /*
  716. * curl_easy_getinfo() is an external interface that allows an app to retrieve
  717. * information from a performed transfer and similar.
  718. */
  719. #undef curl_easy_getinfo
  720. CURLcode curl_easy_getinfo(struct Curl_easy *data, CURLINFO info, ...)
  721. {
  722. va_list arg;
  723. void *paramp;
  724. CURLcode result;
  725. va_start(arg, info);
  726. paramp = va_arg(arg, void *);
  727. result = Curl_getinfo(data, info, paramp);
  728. va_end(arg);
  729. return result;
  730. }
  731. /*
  732. * curl_easy_duphandle() is an external interface to allow duplication of a
  733. * given input easy handle. The returned handle will be a new working handle
  734. * with all options set exactly as the input source handle.
  735. */
  736. struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
  737. {
  738. struct Curl_easy *outcurl = calloc(1, sizeof(struct Curl_easy));
  739. if(NULL == outcurl)
  740. goto fail;
  741. /*
  742. * We setup a few buffers we need. We should probably make them
  743. * get setup on-demand in the code, as that would probably decrease
  744. * the likeliness of us forgetting to init a buffer here in the future.
  745. */
  746. outcurl->set.buffer_size = data->set.buffer_size;
  747. outcurl->state.buffer = malloc(outcurl->set.buffer_size + 1);
  748. if(!outcurl->state.buffer)
  749. goto fail;
  750. outcurl->state.headerbuff = malloc(HEADERSIZE);
  751. if(!outcurl->state.headerbuff)
  752. goto fail;
  753. outcurl->state.headersize = HEADERSIZE;
  754. /* copy all userdefined values */
  755. if(Curl_dupset(outcurl, data))
  756. goto fail;
  757. /* the connection cache is setup on demand */
  758. outcurl->state.conn_cache = NULL;
  759. outcurl->state.lastconnect = NULL;
  760. outcurl->progress.flags = data->progress.flags;
  761. outcurl->progress.callback = data->progress.callback;
  762. if(data->cookies) {
  763. /* If cookies are enabled in the parent handle, we enable them
  764. in the clone as well! */
  765. outcurl->cookies = Curl_cookie_init(data,
  766. data->cookies->filename,
  767. outcurl->cookies,
  768. data->set.cookiesession);
  769. if(!outcurl->cookies)
  770. goto fail;
  771. }
  772. /* duplicate all values in 'change' */
  773. if(data->change.cookielist) {
  774. outcurl->change.cookielist =
  775. Curl_slist_duplicate(data->change.cookielist);
  776. if(!outcurl->change.cookielist)
  777. goto fail;
  778. }
  779. if(data->change.url) {
  780. outcurl->change.url = strdup(data->change.url);
  781. if(!outcurl->change.url)
  782. goto fail;
  783. outcurl->change.url_alloc = TRUE;
  784. }
  785. if(data->change.referer) {
  786. outcurl->change.referer = strdup(data->change.referer);
  787. if(!outcurl->change.referer)
  788. goto fail;
  789. outcurl->change.referer_alloc = TRUE;
  790. }
  791. /* Clone the resolver handle, if present, for the new handle */
  792. if(Curl_resolver_duphandle(&outcurl->state.resolver,
  793. data->state.resolver))
  794. goto fail;
  795. Curl_convert_setup(outcurl);
  796. Curl_initinfo(outcurl);
  797. outcurl->magic = CURLEASY_MAGIC_NUMBER;
  798. /* we reach this point and thus we are OK */
  799. return outcurl;
  800. fail:
  801. if(outcurl) {
  802. curl_slist_free_all(outcurl->change.cookielist);
  803. outcurl->change.cookielist = NULL;
  804. Curl_safefree(outcurl->state.buffer);
  805. Curl_safefree(outcurl->state.headerbuff);
  806. Curl_safefree(outcurl->change.url);
  807. Curl_safefree(outcurl->change.referer);
  808. Curl_freeset(outcurl);
  809. free(outcurl);
  810. }
  811. return NULL;
  812. }
  813. /*
  814. * curl_easy_reset() is an external interface that allows an app to re-
  815. * initialize a session handle to the default values.
  816. */
  817. void curl_easy_reset(struct Curl_easy *data)
  818. {
  819. Curl_safefree(data->state.pathbuffer);
  820. data->state.path = NULL;
  821. Curl_free_request_state(data);
  822. /* zero out UserDefined data: */
  823. Curl_freeset(data);
  824. memset(&data->set, 0, sizeof(struct UserDefined));
  825. (void)Curl_init_userdefined(&data->set);
  826. /* zero out Progress data: */
  827. memset(&data->progress, 0, sizeof(struct Progress));
  828. /* zero out PureInfo data: */
  829. Curl_initinfo(data);
  830. data->progress.flags |= PGRS_HIDE;
  831. data->state.current_speed = -1; /* init to negative == impossible */
  832. /* zero out authentication data: */
  833. memset(&data->state.authhost, 0, sizeof(struct auth));
  834. memset(&data->state.authproxy, 0, sizeof(struct auth));
  835. }
  836. /*
  837. * curl_easy_pause() allows an application to pause or unpause a specific
  838. * transfer and direction. This function sets the full new state for the
  839. * current connection this easy handle operates on.
  840. *
  841. * NOTE: if you have the receiving paused and you call this function to remove
  842. * the pausing, you may get your write callback called at this point.
  843. *
  844. * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h
  845. */
  846. CURLcode curl_easy_pause(struct Curl_easy *data, int action)
  847. {
  848. struct SingleRequest *k = &data->req;
  849. CURLcode result = CURLE_OK;
  850. /* first switch off both pause bits */
  851. int newstate = k->keepon &~ (KEEP_RECV_PAUSE| KEEP_SEND_PAUSE);
  852. /* set the new desired pause bits */
  853. newstate |= ((action & CURLPAUSE_RECV)?KEEP_RECV_PAUSE:0) |
  854. ((action & CURLPAUSE_SEND)?KEEP_SEND_PAUSE:0);
  855. /* put it back in the keepon */
  856. k->keepon = newstate;
  857. if(!(newstate & KEEP_RECV_PAUSE) && data->state.tempcount) {
  858. /* there are buffers for sending that can be delivered as the receive
  859. pausing is lifted! */
  860. unsigned int i;
  861. unsigned int count = data->state.tempcount;
  862. struct tempbuf writebuf[3]; /* there can only be three */
  863. /* copy the structs to allow for immediate re-pausing */
  864. for(i=0; i < data->state.tempcount; i++) {
  865. writebuf[i] = data->state.tempwrite[i];
  866. data->state.tempwrite[i].buf = NULL;
  867. }
  868. data->state.tempcount = 0;
  869. for(i=0; i < count; i++) {
  870. /* even if one function returns error, this loops through and frees all
  871. buffers */
  872. if(!result)
  873. result = Curl_client_chop_write(data->easy_conn,
  874. writebuf[i].type,
  875. writebuf[i].buf,
  876. writebuf[i].len);
  877. free(writebuf[i].buf);
  878. }
  879. if(result)
  880. return result;
  881. }
  882. /* if there's no error and we're not pausing both directions, we want
  883. to have this handle checked soon */
  884. if(!result &&
  885. ((newstate&(KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) !=
  886. (KEEP_RECV_PAUSE|KEEP_SEND_PAUSE)) )
  887. Curl_expire(data, 0, EXPIRE_RUN_NOW); /* get this handle going again */
  888. return result;
  889. }
  890. static CURLcode easy_connection(struct Curl_easy *data,
  891. curl_socket_t *sfd,
  892. struct connectdata **connp)
  893. {
  894. if(data == NULL)
  895. return CURLE_BAD_FUNCTION_ARGUMENT;
  896. /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */
  897. if(!data->set.connect_only) {
  898. failf(data, "CONNECT_ONLY is required!");
  899. return CURLE_UNSUPPORTED_PROTOCOL;
  900. }
  901. *sfd = Curl_getconnectinfo(data, connp);
  902. if(*sfd == CURL_SOCKET_BAD) {
  903. failf(data, "Failed to get recent socket");
  904. return CURLE_UNSUPPORTED_PROTOCOL;
  905. }
  906. return CURLE_OK;
  907. }
  908. /*
  909. * Receives data from the connected socket. Use after successful
  910. * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
  911. * Returns CURLE_OK on success, error code on error.
  912. */
  913. CURLcode curl_easy_recv(struct Curl_easy *data, void *buffer, size_t buflen,
  914. size_t *n)
  915. {
  916. curl_socket_t sfd;
  917. CURLcode result;
  918. ssize_t n1;
  919. struct connectdata *c;
  920. result = easy_connection(data, &sfd, &c);
  921. if(result)
  922. return result;
  923. *n = 0;
  924. result = Curl_read(c, sfd, buffer, buflen, &n1);
  925. if(result)
  926. return result;
  927. *n = (size_t)n1;
  928. return CURLE_OK;
  929. }
  930. /*
  931. * Sends data over the connected socket. Use after successful
  932. * curl_easy_perform() with CURLOPT_CONNECT_ONLY option.
  933. */
  934. CURLcode curl_easy_send(struct Curl_easy *data, const void *buffer,
  935. size_t buflen, size_t *n)
  936. {
  937. curl_socket_t sfd;
  938. CURLcode result;
  939. ssize_t n1;
  940. struct connectdata *c = NULL;
  941. result = easy_connection(data, &sfd, &c);
  942. if(result)
  943. return result;
  944. *n = 0;
  945. result = Curl_write(c, sfd, buffer, buflen, &n1);
  946. if(n1 == -1)
  947. return CURLE_SEND_ERROR;
  948. /* detect EAGAIN */
  949. if(!result && !n1)
  950. return CURLE_AGAIN;
  951. *n = (size_t)n1;
  952. return result;
  953. }