asyn-ares.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://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 "setup.h"
  23. #ifdef HAVE_LIMITS_H
  24. #include <limits.h>
  25. #endif
  26. #ifdef HAVE_SYS_SOCKET_H
  27. #include <sys/socket.h>
  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_UNISTD_H
  39. #include <unistd.h> /* for the close() proto */
  40. #endif
  41. #ifdef __VMS
  42. #include <in.h>
  43. #include <inet.h>
  44. #endif
  45. #ifdef HAVE_PROCESS_H
  46. #include <process.h>
  47. #endif
  48. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  49. #undef in_addr_t
  50. #define in_addr_t unsigned long
  51. #endif
  52. /***********************************************************************
  53. * Only for ares-enabled builds
  54. * And only for functions that fulfill the asynch resolver backend API
  55. * as defined in asyn.h, nothing else belongs in this file!
  56. **********************************************************************/
  57. #ifdef CURLRES_ARES
  58. #include "urldata.h"
  59. #include "sendf.h"
  60. #include "hostip.h"
  61. #include "hash.h"
  62. #include "share.h"
  63. #include "strerror.h"
  64. #include "url.h"
  65. #include "multiif.h"
  66. #include "inet_pton.h"
  67. #include "connect.h"
  68. #include "select.h"
  69. #include "progress.h"
  70. #define _MPRINTF_REPLACE /* use our functions only */
  71. #include <curl/mprintf.h>
  72. # if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && \
  73. (defined(WIN32) || defined(_WIN32) || defined(__SYMBIAN32__))
  74. # define CARES_STATICLIB
  75. # endif
  76. # include <ares.h>
  77. # include <ares_version.h> /* really old c-ares didn't include this by
  78. itself */
  79. #if ARES_VERSION >= 0x010500
  80. /* c-ares 1.5.0 or later, the callback proto is modified */
  81. #define HAVE_CARES_CALLBACK_TIMEOUTS 1
  82. #endif
  83. #include "curl_memory.h"
  84. /* The last #include file should be: */
  85. #include "memdebug.h"
  86. struct ResolverResults {
  87. int num_pending; /* number of ares_gethostbyname() requests */
  88. Curl_addrinfo *temp_ai; /* intermediary result while fetching c-ares parts */
  89. int last_status;
  90. };
  91. /*
  92. * Curl_resolver_global_init() - the generic low-level asynchronous name
  93. * resolve API. Called from curl_global_init() to initialize global resolver
  94. * environment. Initializes ares library.
  95. */
  96. int Curl_resolver_global_init(void)
  97. {
  98. #ifdef CARES_HAVE_ARES_LIBRARY_INIT
  99. if(ares_library_init(ARES_LIB_INIT_ALL)) {
  100. return CURLE_FAILED_INIT;
  101. }
  102. #endif
  103. return CURLE_OK;
  104. }
  105. /*
  106. * Curl_resolver_global_cleanup()
  107. *
  108. * Called from curl_global_cleanup() to destroy global resolver environment.
  109. * Deinitializes ares library.
  110. */
  111. void Curl_resolver_global_cleanup(void)
  112. {
  113. #ifdef CARES_HAVE_ARES_LIBRARY_CLEANUP
  114. ares_library_cleanup();
  115. #endif
  116. }
  117. /*
  118. * Curl_resolver_init()
  119. *
  120. * Called from curl_easy_init() -> Curl_open() to initialize resolver
  121. * URL-state specific environment ('resolver' member of the UrlState
  122. * structure). Fills the passed pointer by the initialized ares_channel.
  123. */
  124. CURLcode Curl_resolver_init(void **resolver)
  125. {
  126. int status = ares_init((ares_channel*)resolver);
  127. if(status != ARES_SUCCESS) {
  128. if(status == ARES_ENOMEM)
  129. return CURLE_OUT_OF_MEMORY;
  130. else
  131. return CURLE_FAILED_INIT;
  132. }
  133. return CURLE_OK;
  134. /* make sure that all other returns from this function should destroy the
  135. ares channel before returning error! */
  136. }
  137. /*
  138. * Curl_resolver_cleanup()
  139. *
  140. * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
  141. * URL-state specific environment ('resolver' member of the UrlState
  142. * structure). Destroys the ares channel.
  143. */
  144. void Curl_resolver_cleanup(void *resolver)
  145. {
  146. ares_destroy((ares_channel)resolver);
  147. }
  148. /*
  149. * Curl_resolver_duphandle()
  150. *
  151. * Called from curl_easy_duphandle() to duplicate resolver URL-state specific
  152. * environment ('resolver' member of the UrlState structure). Duplicates the
  153. * 'from' ares channel and passes the resulting channel to the 'to' pointer.
  154. */
  155. int Curl_resolver_duphandle(void **to, void *from)
  156. {
  157. /* Clone the ares channel for the new handle */
  158. if(ARES_SUCCESS != ares_dup((ares_channel*)to,(ares_channel)from))
  159. return CURLE_FAILED_INIT;
  160. return CURLE_OK;
  161. }
  162. static void destroy_async_data (struct Curl_async *async);
  163. /*
  164. * Cancel all possibly still on-going resolves for this connection.
  165. */
  166. void Curl_resolver_cancel(struct connectdata *conn)
  167. {
  168. if(conn && conn->data && conn->data->state.resolver)
  169. ares_cancel((ares_channel)conn->data->state.resolver);
  170. destroy_async_data(&conn->async);
  171. }
  172. /*
  173. * destroy_async_data() cleans up async resolver data.
  174. */
  175. static void destroy_async_data (struct Curl_async *async)
  176. {
  177. if(async->hostname)
  178. free(async->hostname);
  179. if(async->os_specific) {
  180. struct ResolverResults *res = (struct ResolverResults *)async->os_specific;
  181. if(res) {
  182. if(res->temp_ai) {
  183. Curl_freeaddrinfo(res->temp_ai);
  184. res->temp_ai = NULL;
  185. }
  186. free(res);
  187. }
  188. async->os_specific = NULL;
  189. }
  190. async->hostname = NULL;
  191. }
  192. /*
  193. * Curl_resolver_fdset() is called when someone from the outside world (using
  194. * curl_multi_fdset()) wants to get our fd_set setup and we're talking with
  195. * ares. The caller must make sure that this function is only called when we
  196. * have a working ares channel.
  197. *
  198. * Returns: CURLE_OK always!
  199. */
  200. int Curl_resolver_getsock(struct connectdata *conn,
  201. curl_socket_t *socks,
  202. int numsocks)
  203. {
  204. struct timeval maxtime;
  205. struct timeval timebuf;
  206. struct timeval *timeout;
  207. long milli;
  208. int max = ares_getsock((ares_channel)conn->data->state.resolver,
  209. (ares_socket_t *)socks, numsocks);
  210. maxtime.tv_sec = CURL_TIMEOUT_RESOLVE;
  211. maxtime.tv_usec = 0;
  212. timeout = ares_timeout((ares_channel)conn->data->state.resolver, &maxtime,
  213. &timebuf);
  214. milli = (timeout->tv_sec * 1000) + (timeout->tv_usec/1000);
  215. if(milli == 0)
  216. milli += 10;
  217. Curl_expire(conn->data, milli);
  218. return max;
  219. }
  220. /*
  221. * waitperform()
  222. *
  223. * 1) Ask ares what sockets it currently plays with, then
  224. * 2) wait for the timeout period to check for action on ares' sockets.
  225. * 3) tell ares to act on all the sockets marked as "with action"
  226. *
  227. * return number of sockets it worked on
  228. */
  229. static int waitperform(struct connectdata *conn, int timeout_ms)
  230. {
  231. struct SessionHandle *data = conn->data;
  232. int nfds;
  233. int bitmask;
  234. ares_socket_t socks[ARES_GETSOCK_MAXNUM];
  235. struct pollfd pfd[ARES_GETSOCK_MAXNUM];
  236. int i;
  237. int num = 0;
  238. bitmask = ares_getsock((ares_channel)data->state.resolver, socks,
  239. ARES_GETSOCK_MAXNUM);
  240. for(i=0; i < ARES_GETSOCK_MAXNUM; i++) {
  241. pfd[i].events = 0;
  242. pfd[i].revents = 0;
  243. if(ARES_GETSOCK_READABLE(bitmask, i)) {
  244. pfd[i].fd = socks[i];
  245. pfd[i].events |= POLLRDNORM|POLLIN;
  246. }
  247. if(ARES_GETSOCK_WRITABLE(bitmask, i)) {
  248. pfd[i].fd = socks[i];
  249. pfd[i].events |= POLLWRNORM|POLLOUT;
  250. }
  251. if(pfd[i].events != 0)
  252. num++;
  253. else
  254. break;
  255. }
  256. if(num)
  257. nfds = Curl_poll(pfd, num, timeout_ms);
  258. else
  259. nfds = 0;
  260. if(!nfds)
  261. /* Call ares_process() unconditonally here, even if we simply timed out
  262. above, as otherwise the ares name resolve won't timeout! */
  263. ares_process_fd((ares_channel)data->state.resolver, ARES_SOCKET_BAD,
  264. ARES_SOCKET_BAD);
  265. else {
  266. /* move through the descriptors and ask for processing on them */
  267. for(i=0; i < num; i++)
  268. ares_process_fd((ares_channel)data->state.resolver,
  269. pfd[i].revents & (POLLRDNORM|POLLIN)?
  270. pfd[i].fd:ARES_SOCKET_BAD,
  271. pfd[i].revents & (POLLWRNORM|POLLOUT)?
  272. pfd[i].fd:ARES_SOCKET_BAD);
  273. }
  274. return nfds;
  275. }
  276. /*
  277. * Curl_resolver_is_resolved() is called repeatedly to check if a previous
  278. * name resolve request has completed. It should also make sure to time-out if
  279. * the operation seems to take too long.
  280. *
  281. * Returns normal CURLcode errors.
  282. */
  283. CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
  284. struct Curl_dns_entry **dns)
  285. {
  286. struct SessionHandle *data = conn->data;
  287. struct ResolverResults *res = (struct ResolverResults *)
  288. conn->async.os_specific;
  289. *dns = NULL;
  290. waitperform(conn, 0);
  291. if(res && !res->num_pending) {
  292. (void)Curl_addrinfo_callback(conn, res->last_status, res->temp_ai);
  293. /* temp_ai ownership is moved to the connection, so we need not free-up
  294. them */
  295. res->temp_ai = NULL;
  296. destroy_async_data(&conn->async);
  297. if(!conn->async.dns) {
  298. failf(data, "Could not resolve %s: %s (%s)",
  299. conn->bits.proxy?"proxy":"host",
  300. conn->host.dispname,
  301. ares_strerror(conn->async.status));
  302. return conn->bits.proxy?CURLE_COULDNT_RESOLVE_PROXY:
  303. CURLE_COULDNT_RESOLVE_HOST;
  304. }
  305. *dns = conn->async.dns;
  306. }
  307. return CURLE_OK;
  308. }
  309. /*
  310. * Curl_resolver_wait_resolv()
  311. *
  312. * waits for a resolve to finish. This function should be avoided since using
  313. * this risk getting the multi interface to "hang".
  314. *
  315. * If 'entry' is non-NULL, make it point to the resolved dns entry
  316. *
  317. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved, and
  318. * CURLE_OPERATION_TIMEDOUT if a time-out occurred.
  319. */
  320. CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
  321. struct Curl_dns_entry **entry)
  322. {
  323. CURLcode rc=CURLE_OK;
  324. struct SessionHandle *data = conn->data;
  325. long timeout;
  326. struct timeval now = Curl_tvnow();
  327. struct Curl_dns_entry *temp_entry;
  328. timeout = Curl_timeleft(data, &now, TRUE);
  329. if(!timeout)
  330. timeout = CURL_TIMEOUT_RESOLVE * 1000; /* default name resolve timeout */
  331. /* Wait for the name resolve query to complete. */
  332. for(;;) {
  333. struct timeval *tvp, tv, store;
  334. long timediff;
  335. int itimeout;
  336. int timeout_ms;
  337. itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
  338. store.tv_sec = itimeout/1000;
  339. store.tv_usec = (itimeout%1000)*1000;
  340. tvp = ares_timeout((ares_channel)data->state.resolver, &store, &tv);
  341. /* use the timeout period ares returned to us above if less than one
  342. second is left, otherwise just use 1000ms to make sure the progress
  343. callback gets called frequent enough */
  344. if(!tvp->tv_sec)
  345. timeout_ms = (int)(tvp->tv_usec/1000);
  346. else
  347. timeout_ms = 1000;
  348. waitperform(conn, timeout_ms);
  349. Curl_resolver_is_resolved(conn,&temp_entry);
  350. if(conn->async.done)
  351. break;
  352. if(Curl_pgrsUpdate(conn)) {
  353. rc = CURLE_ABORTED_BY_CALLBACK;
  354. timeout = -1; /* trigger the cancel below */
  355. }
  356. else {
  357. struct timeval now2 = Curl_tvnow();
  358. timediff = Curl_tvdiff(now2, now); /* spent time */
  359. timeout -= timediff?timediff:1; /* always deduct at least 1 */
  360. now = now2; /* for next loop */
  361. }
  362. if(timeout < 0) {
  363. /* our timeout, so we cancel the ares operation */
  364. ares_cancel((ares_channel)data->state.resolver);
  365. break;
  366. }
  367. }
  368. /* Operation complete, if the lookup was successful we now have the entry
  369. in the cache. */
  370. if(entry)
  371. *entry = conn->async.dns;
  372. if(!conn->async.dns) {
  373. /* a name was not resolved */
  374. if((timeout < 0) || (conn->async.status == ARES_ETIMEOUT)) {
  375. if(conn->bits.proxy) {
  376. failf(data, "Resolving proxy timed out: %s", conn->proxy.dispname);
  377. rc = CURLE_COULDNT_RESOLVE_PROXY;
  378. }
  379. else {
  380. failf(data, "Resolving host timed out: %s", conn->host.dispname);
  381. rc = CURLE_COULDNT_RESOLVE_HOST;
  382. }
  383. }
  384. else if(conn->async.done) {
  385. if(conn->bits.proxy) {
  386. failf(data, "Could not resolve proxy: %s (%s)", conn->proxy.dispname,
  387. ares_strerror(conn->async.status));
  388. rc = CURLE_COULDNT_RESOLVE_PROXY;
  389. }
  390. else {
  391. failf(data, "Could not resolve host: %s (%s)", conn->host.dispname,
  392. ares_strerror(conn->async.status));
  393. rc = CURLE_COULDNT_RESOLVE_HOST;
  394. }
  395. }
  396. else
  397. rc = CURLE_OPERATION_TIMEDOUT;
  398. /* close the connection, since we can't return failure here without
  399. cleaning up this connection properly */
  400. conn->bits.close = TRUE;
  401. }
  402. return rc;
  403. }
  404. /* Connects results to the list */
  405. static void compound_results(struct ResolverResults *res,
  406. Curl_addrinfo *ai)
  407. {
  408. Curl_addrinfo *ai_tail;
  409. if(!ai)
  410. return;
  411. ai_tail = ai;
  412. while(ai_tail->ai_next)
  413. ai_tail = ai_tail->ai_next;
  414. /* Add the new results to the list of old results. */
  415. ai_tail->ai_next = res->temp_ai;
  416. res->temp_ai = ai;
  417. }
  418. /*
  419. * ares_query_completed_cb() is the callback that ares will call when
  420. * the host query initiated by ares_gethostbyname() from Curl_getaddrinfo(),
  421. * when using ares, is completed either successfully or with failure.
  422. */
  423. static void query_completed_cb(void *arg, /* (struct connectdata *) */
  424. int status,
  425. #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
  426. int timeouts,
  427. #endif
  428. struct hostent *hostent)
  429. {
  430. struct connectdata *conn = (struct connectdata *)arg;
  431. struct ResolverResults *res;
  432. #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
  433. (void)timeouts; /* ignored */
  434. #endif
  435. if(ARES_EDESTRUCTION == status)
  436. /* when this ares handle is getting destroyed, the 'arg' pointer may not
  437. be valid so only defer it when we know the 'status' says its fine! */
  438. return;
  439. res = (struct ResolverResults *)conn->async.os_specific;
  440. res->num_pending--;
  441. if(CURL_ASYNC_SUCCESS == status) {
  442. Curl_addrinfo *ai = Curl_he2ai(hostent, conn->async.port);
  443. if(ai) {
  444. compound_results(res, ai);
  445. }
  446. }
  447. /* A successful result overwrites any previous error */
  448. if(res->last_status != ARES_SUCCESS)
  449. res->last_status = status;
  450. }
  451. /*
  452. * Curl_resolver_getaddrinfo() - when using ares
  453. *
  454. * Returns name information about the given hostname and port number. If
  455. * successful, the 'hostent' is returned and the forth argument will point to
  456. * memory we need to free after use. That memory *MUST* be freed with
  457. * Curl_freeaddrinfo(), nothing else.
  458. */
  459. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  460. const char *hostname,
  461. int port,
  462. int *waitp)
  463. {
  464. char *bufp;
  465. struct SessionHandle *data = conn->data;
  466. struct in_addr in;
  467. int family = PF_INET;
  468. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  469. struct in6_addr in6;
  470. #endif /* CURLRES_IPV6 */
  471. *waitp = 0; /* default to synchronous response */
  472. /* First check if this is an IPv4 address string */
  473. if(Curl_inet_pton(AF_INET, hostname, &in) > 0) {
  474. /* This is a dotted IP address 123.123.123.123-style */
  475. return Curl_ip2addr(AF_INET, &in, hostname, port);
  476. }
  477. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  478. /* Otherwise, check if this is an IPv6 address string */
  479. if(Curl_inet_pton (AF_INET6, hostname, &in6) > 0)
  480. /* This must be an IPv6 address literal. */
  481. return Curl_ip2addr(AF_INET6, &in6, hostname, port);
  482. switch(conn->ip_version) {
  483. default:
  484. #if ARES_VERSION >= 0x010601
  485. family = PF_UNSPEC; /* supported by c-ares since 1.6.1, so for older
  486. c-ares versions this just falls through and defaults
  487. to PF_INET */
  488. break;
  489. #endif
  490. case CURL_IPRESOLVE_V4:
  491. family = PF_INET;
  492. break;
  493. case CURL_IPRESOLVE_V6:
  494. family = PF_INET6;
  495. break;
  496. }
  497. #endif /* CURLRES_IPV6 */
  498. bufp = strdup(hostname);
  499. if(bufp) {
  500. struct ResolverResults *res = NULL;
  501. Curl_safefree(conn->async.hostname);
  502. conn->async.hostname = bufp;
  503. conn->async.port = port;
  504. conn->async.done = FALSE; /* not done */
  505. conn->async.status = 0; /* clear */
  506. conn->async.dns = NULL; /* clear */
  507. res = calloc(sizeof(struct ResolverResults),1);
  508. if(!res) {
  509. Curl_safefree(conn->async.hostname);
  510. conn->async.hostname = NULL;
  511. return NULL;
  512. }
  513. conn->async.os_specific = res;
  514. /* initial status - failed */
  515. res->last_status = ARES_ENOTFOUND;
  516. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  517. if(family == PF_UNSPEC) {
  518. if(Curl_ipv6works()) {
  519. res->num_pending = 2;
  520. /* areschannel is already setup in the Curl_open() function */
  521. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  522. PF_INET, query_completed_cb, conn);
  523. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  524. PF_INET6, query_completed_cb, conn);
  525. }
  526. else {
  527. res->num_pending = 1;
  528. /* areschannel is already setup in the Curl_open() function */
  529. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  530. PF_INET, query_completed_cb, conn);
  531. }
  532. }
  533. else
  534. #endif /* CURLRES_IPV6 */
  535. {
  536. res->num_pending = 1;
  537. /* areschannel is already setup in the Curl_open() function */
  538. ares_gethostbyname((ares_channel)data->state.resolver, hostname, family,
  539. query_completed_cb, conn);
  540. }
  541. *waitp = 1; /* expect asynchronous response */
  542. }
  543. return NULL; /* no struct yet */
  544. }
  545. CURLcode Curl_set_dns_servers(struct SessionHandle *data,
  546. char *servers)
  547. {
  548. CURLcode result = CURLE_NOT_BUILT_IN;
  549. #if (ARES_VERSION >= 0x010704)
  550. int ares_result = ares_set_servers_csv(data->state.resolver, servers);
  551. switch(ares_result) {
  552. case ARES_SUCCESS:
  553. result = CURLE_OK;
  554. break;
  555. case ARES_ENOMEM:
  556. result = CURLE_OUT_OF_MEMORY;
  557. break;
  558. case ARES_ENOTINITIALIZED:
  559. case ARES_ENODATA:
  560. case ARES_EBADSTR:
  561. default:
  562. result = CURLE_BAD_FUNCTION_ARGUMENT;
  563. break;
  564. }
  565. #else /* too old c-ares version! */
  566. (void)data;
  567. (void)servers;
  568. #endif
  569. return result;
  570. }
  571. #endif /* CURLRES_ARES */