ares_gethostbyname.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /* Copyright 1998, 2011, 2013 by the Massachusetts Institute of Technology.
  2. *
  3. * Permission to use, copy, modify, and distribute this
  4. * software and its documentation for any purpose and without
  5. * fee is hereby granted, provided that the above copyright
  6. * notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting
  8. * documentation, and that the name of M.I.T. not be used in
  9. * advertising or publicity pertaining to distribution of the
  10. * software without specific, written prior permission.
  11. * M.I.T. makes no representations about the suitability of
  12. * this software for any purpose. It is provided "as is"
  13. * without express or implied warranty.
  14. */
  15. #include "ares_setup.h"
  16. #ifdef HAVE_NETINET_IN_H
  17. # include <netinet/in.h>
  18. #endif
  19. #ifdef HAVE_NETDB_H
  20. # include <netdb.h>
  21. #endif
  22. #ifdef HAVE_ARPA_INET_H
  23. # include <arpa/inet.h>
  24. #endif
  25. #ifdef HAVE_ARPA_NAMESER_H
  26. # include <arpa/nameser.h>
  27. #else
  28. # include "nameser.h"
  29. #endif
  30. #ifdef HAVE_ARPA_NAMESER_COMPAT_H
  31. # include <arpa/nameser_compat.h>
  32. #endif
  33. #ifdef HAVE_STRINGS_H
  34. #include <strings.h>
  35. #endif
  36. #include "ares.h"
  37. #include "ares_inet_net_pton.h"
  38. #include "bitncmp.h"
  39. #include "ares_platform.h"
  40. #include "ares_nowarn.h"
  41. #include "ares_private.h"
  42. #ifdef WATT32
  43. #undef WIN32
  44. #endif
  45. struct host_query {
  46. /* Arguments passed to ares_gethostbyname() */
  47. ares_channel channel;
  48. char *name;
  49. ares_host_callback callback;
  50. void *arg;
  51. int sent_family; /* this family is what was is being used */
  52. int want_family; /* this family is what is asked for in the API */
  53. const char *remaining_lookups;
  54. int timeouts;
  55. };
  56. static void next_lookup(struct host_query *hquery, int status_code);
  57. static void host_callback(void *arg, int status, int timeouts,
  58. unsigned char *abuf, int alen);
  59. static void end_hquery(struct host_query *hquery, int status,
  60. struct hostent *host);
  61. static int fake_hostent(const char *name, int family,
  62. ares_host_callback callback, void *arg);
  63. static int file_lookup(const char *name, int family, struct hostent **host);
  64. static void sort_addresses(struct hostent *host,
  65. const struct apattern *sortlist, int nsort);
  66. static void sort6_addresses(struct hostent *host,
  67. const struct apattern *sortlist, int nsort);
  68. static int get_address_index(const struct in_addr *addr,
  69. const struct apattern *sortlist, int nsort);
  70. static int get6_address_index(const struct ares_in6_addr *addr,
  71. const struct apattern *sortlist, int nsort);
  72. void ares_gethostbyname(ares_channel channel, const char *name, int family,
  73. ares_host_callback callback, void *arg)
  74. {
  75. struct host_query *hquery;
  76. /* Right now we only know how to look up Internet addresses - and unspec
  77. means try both basically. */
  78. switch (family) {
  79. case AF_INET:
  80. case AF_INET6:
  81. case AF_UNSPEC:
  82. break;
  83. default:
  84. callback(arg, ARES_ENOTIMP, 0, NULL);
  85. return;
  86. }
  87. /* Per RFC 7686, reject queries for ".onion" domain names with NXDOMAIN. */
  88. if (ares__is_onion_domain(name))
  89. {
  90. callback(arg, ARES_ENOTFOUND, 0, NULL);
  91. return;
  92. }
  93. if (fake_hostent(name, family, callback, arg))
  94. return;
  95. /* Allocate and fill in the host query structure. */
  96. hquery = ares_malloc(sizeof(struct host_query));
  97. if (!hquery)
  98. {
  99. callback(arg, ARES_ENOMEM, 0, NULL);
  100. return;
  101. }
  102. hquery->channel = channel;
  103. hquery->name = ares_strdup(name);
  104. hquery->want_family = family;
  105. hquery->sent_family = -1; /* nothing is sent yet */
  106. if (!hquery->name) {
  107. ares_free(hquery);
  108. callback(arg, ARES_ENOMEM, 0, NULL);
  109. return;
  110. }
  111. hquery->callback = callback;
  112. hquery->arg = arg;
  113. hquery->remaining_lookups = channel->lookups;
  114. hquery->timeouts = 0;
  115. /* Start performing lookups according to channel->lookups. */
  116. next_lookup(hquery, ARES_ECONNREFUSED /* initial error code */);
  117. }
  118. static void next_lookup(struct host_query *hquery, int status_code)
  119. {
  120. const char *p;
  121. struct hostent *host;
  122. int status = status_code;
  123. for (p = hquery->remaining_lookups; *p; p++)
  124. {
  125. switch (*p)
  126. {
  127. case 'b':
  128. /* DNS lookup */
  129. hquery->remaining_lookups = p + 1;
  130. if ((hquery->want_family == AF_INET6) ||
  131. (hquery->want_family == AF_UNSPEC)) {
  132. /* if inet6 or unspec, start out with AAAA */
  133. hquery->sent_family = AF_INET6;
  134. ares_search(hquery->channel, hquery->name, C_IN, T_AAAA,
  135. host_callback, hquery);
  136. }
  137. else {
  138. hquery->sent_family = AF_INET;
  139. ares_search(hquery->channel, hquery->name, C_IN, T_A,
  140. host_callback, hquery);
  141. }
  142. return;
  143. case 'f':
  144. /* Host file lookup */
  145. status = file_lookup(hquery->name, hquery->want_family, &host);
  146. /* this status check below previously checked for !ARES_ENOTFOUND,
  147. but we should not assume that this single error code is the one
  148. that can occur, as that is in fact no longer the case */
  149. if (status == ARES_SUCCESS)
  150. {
  151. end_hquery(hquery, status, host);
  152. return;
  153. }
  154. status = status_code; /* Use original status code */
  155. break;
  156. }
  157. }
  158. end_hquery(hquery, status, NULL);
  159. }
  160. static void host_callback(void *arg, int status, int timeouts,
  161. unsigned char *abuf, int alen)
  162. {
  163. struct host_query *hquery = (struct host_query *) arg;
  164. ares_channel channel = hquery->channel;
  165. struct hostent *host = NULL;
  166. hquery->timeouts += timeouts;
  167. if (status == ARES_SUCCESS)
  168. {
  169. if (hquery->sent_family == AF_INET)
  170. {
  171. status = ares_parse_a_reply(abuf, alen, &host, NULL, NULL);
  172. if (host && channel->nsort)
  173. sort_addresses(host, channel->sortlist, channel->nsort);
  174. }
  175. else if (hquery->sent_family == AF_INET6)
  176. {
  177. status = ares_parse_aaaa_reply(abuf, alen, &host, NULL, NULL);
  178. if ((status == ARES_ENODATA || status == ARES_EBADRESP ||
  179. (status == ARES_SUCCESS && host && host->h_addr_list[0] == NULL)) &&
  180. hquery->want_family == AF_UNSPEC) {
  181. /* The query returned something but either there were no AAAA
  182. records (e.g. just CNAME) or the response was malformed. Try
  183. looking up A instead. */
  184. if (host)
  185. ares_free_hostent(host);
  186. hquery->sent_family = AF_INET;
  187. ares_search(hquery->channel, hquery->name, C_IN, T_A,
  188. host_callback, hquery);
  189. return;
  190. }
  191. if (host && channel->nsort)
  192. sort6_addresses(host, channel->sortlist, channel->nsort);
  193. }
  194. if (status == ARES_SUCCESS && host && host->h_addr_list[0] == NULL)
  195. {
  196. /* The query returned something but had no A/AAAA record
  197. (even after potentially retrying AAAA with A)
  198. so we should treat this as an error */
  199. status = ARES_ENODATA;
  200. }
  201. end_hquery(hquery, status, host);
  202. }
  203. else if ((status == ARES_ENODATA || status == ARES_EBADRESP ||
  204. status == ARES_ETIMEOUT) && (hquery->sent_family == AF_INET6 &&
  205. hquery->want_family == AF_UNSPEC))
  206. {
  207. /* The AAAA query yielded no useful result. Now look up an A instead. */
  208. hquery->sent_family = AF_INET;
  209. ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
  210. hquery);
  211. }
  212. else if (status == ARES_EDESTRUCTION)
  213. end_hquery(hquery, status, NULL);
  214. else
  215. next_lookup(hquery, status);
  216. }
  217. static void end_hquery(struct host_query *hquery, int status,
  218. struct hostent *host)
  219. {
  220. hquery->callback(hquery->arg, status, hquery->timeouts, host);
  221. if (host)
  222. ares_free_hostent(host);
  223. ares_free(hquery->name);
  224. ares_free(hquery);
  225. }
  226. /* If the name looks like an IP address, fake up a host entry, end the
  227. * query immediately, and return true. Otherwise return false.
  228. */
  229. static int fake_hostent(const char *name, int family,
  230. ares_host_callback callback, void *arg)
  231. {
  232. struct hostent hostent;
  233. char *aliases[1] = { NULL };
  234. char *addrs[2];
  235. int result = 0;
  236. struct in_addr in;
  237. struct ares_in6_addr in6;
  238. if (family == AF_INET || family == AF_INET6)
  239. {
  240. /* It only looks like an IP address if it's all numbers and dots. */
  241. int numdots = 0, valid = 1;
  242. const char *p;
  243. for (p = name; *p; p++)
  244. {
  245. if (!ISDIGIT(*p) && *p != '.') {
  246. valid = 0;
  247. break;
  248. } else if (*p == '.') {
  249. numdots++;
  250. }
  251. }
  252. /* if we don't have 3 dots, it is illegal
  253. * (although inet_pton doesn't think so).
  254. */
  255. if (numdots != 3 || !valid)
  256. result = 0;
  257. else
  258. result = (ares_inet_pton(AF_INET, name, &in) < 1 ? 0 : 1);
  259. if (result)
  260. family = AF_INET;
  261. }
  262. if (family == AF_INET6)
  263. result = (ares_inet_pton(AF_INET6, name, &in6) < 1 ? 0 : 1);
  264. if (!result)
  265. return 0;
  266. if (family == AF_INET)
  267. {
  268. hostent.h_length = (int)sizeof(struct in_addr);
  269. addrs[0] = (char *)&in;
  270. }
  271. else if (family == AF_INET6)
  272. {
  273. hostent.h_length = (int)sizeof(struct ares_in6_addr);
  274. addrs[0] = (char *)&in6;
  275. }
  276. /* Duplicate the name, to avoid a constness violation. */
  277. hostent.h_name = ares_strdup(name);
  278. if (!hostent.h_name)
  279. {
  280. callback(arg, ARES_ENOMEM, 0, NULL);
  281. return 1;
  282. }
  283. /* Fill in the rest of the host structure and terminate the query. */
  284. addrs[1] = NULL;
  285. hostent.h_aliases = aliases;
  286. hostent.h_addrtype = aresx_sitoss(family);
  287. hostent.h_addr_list = addrs;
  288. callback(arg, ARES_SUCCESS, 0, &hostent);
  289. ares_free((char *)(hostent.h_name));
  290. return 1;
  291. }
  292. /* This is an API method */
  293. int ares_gethostbyname_file(ares_channel channel, const char *name,
  294. int family, struct hostent **host)
  295. {
  296. int result;
  297. /* We only take the channel to ensure that ares_init() been called. */
  298. if(channel == NULL)
  299. {
  300. /* Anything will do, really. This seems fine, and is consistent with
  301. other error cases. */
  302. *host = NULL;
  303. return ARES_ENOTFOUND;
  304. }
  305. /* Just chain to the internal implementation we use here; it's exactly
  306. * what we want.
  307. */
  308. result = file_lookup(name, family, host);
  309. if(result != ARES_SUCCESS)
  310. {
  311. /* We guarantee a NULL hostent on failure. */
  312. *host = NULL;
  313. }
  314. return result;
  315. }
  316. static int file_lookup(const char *name, int family, struct hostent **host)
  317. {
  318. FILE *fp;
  319. char **alias;
  320. int status;
  321. int error;
  322. #ifdef WIN32
  323. char PATH_HOSTS[MAX_PATH];
  324. win_platform platform;
  325. PATH_HOSTS[0] = '\0';
  326. platform = ares__getplatform();
  327. if (platform == WIN_NT) {
  328. char tmp[MAX_PATH];
  329. HKEY hkeyHosts;
  330. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ,
  331. &hkeyHosts) == ERROR_SUCCESS)
  332. {
  333. DWORD dwLength = MAX_PATH;
  334. RegQueryValueExA(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
  335. &dwLength);
  336. ExpandEnvironmentStringsA(tmp, PATH_HOSTS, MAX_PATH);
  337. RegCloseKey(hkeyHosts);
  338. }
  339. }
  340. else if (platform == WIN_9X)
  341. GetWindowsDirectoryA(PATH_HOSTS, MAX_PATH);
  342. else
  343. return ARES_ENOTFOUND;
  344. strcat(PATH_HOSTS, WIN_PATH_HOSTS);
  345. #elif defined(WATT32)
  346. extern const char *_w32_GetHostsFile (void);
  347. const char *PATH_HOSTS = _w32_GetHostsFile();
  348. if (!PATH_HOSTS)
  349. return ARES_ENOTFOUND;
  350. #endif
  351. /* Per RFC 7686, reject queries for ".onion" domain names with NXDOMAIN. */
  352. if (ares__is_onion_domain(name))
  353. return ARES_ENOTFOUND;
  354. fp = fopen(PATH_HOSTS, "r");
  355. if (!fp)
  356. {
  357. error = ERRNO;
  358. switch(error)
  359. {
  360. case ENOENT:
  361. case ESRCH:
  362. return ARES_ENOTFOUND;
  363. default:
  364. DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
  365. error, strerror(error)));
  366. DEBUGF(fprintf(stderr, "Error opening file: %s\n",
  367. PATH_HOSTS));
  368. *host = NULL;
  369. return ARES_EFILE;
  370. }
  371. }
  372. while ((status = ares__get_hostent(fp, family, host)) == ARES_SUCCESS)
  373. {
  374. if (strcasecmp((*host)->h_name, name) == 0)
  375. break;
  376. for (alias = (*host)->h_aliases; *alias; alias++)
  377. {
  378. if (strcasecmp(*alias, name) == 0)
  379. break;
  380. }
  381. if (*alias)
  382. break;
  383. ares_free_hostent(*host);
  384. }
  385. fclose(fp);
  386. if (status == ARES_EOF)
  387. status = ARES_ENOTFOUND;
  388. if (status != ARES_SUCCESS)
  389. *host = NULL;
  390. return status;
  391. }
  392. static void sort_addresses(struct hostent *host,
  393. const struct apattern *sortlist, int nsort)
  394. {
  395. struct in_addr a1, a2;
  396. int i1, i2, ind1, ind2;
  397. /* This is a simple insertion sort, not optimized at all. i1 walks
  398. * through the address list, with the loop invariant that everything
  399. * to the left of i1 is sorted. In the loop body, the value at i1 is moved
  400. * back through the list (via i2) until it is in sorted order.
  401. */
  402. for (i1 = 0; host->h_addr_list[i1]; i1++)
  403. {
  404. memcpy(&a1, host->h_addr_list[i1], sizeof(struct in_addr));
  405. ind1 = get_address_index(&a1, sortlist, nsort);
  406. for (i2 = i1 - 1; i2 >= 0; i2--)
  407. {
  408. memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
  409. ind2 = get_address_index(&a2, sortlist, nsort);
  410. if (ind2 <= ind1)
  411. break;
  412. memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
  413. }
  414. memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in_addr));
  415. }
  416. }
  417. /* Find the first entry in sortlist which matches addr. Return nsort
  418. * if none of them match.
  419. */
  420. static int get_address_index(const struct in_addr *addr,
  421. const struct apattern *sortlist,
  422. int nsort)
  423. {
  424. int i;
  425. for (i = 0; i < nsort; i++)
  426. {
  427. if (sortlist[i].family != AF_INET)
  428. continue;
  429. if (sortlist[i].type == PATTERN_MASK)
  430. {
  431. if ((addr->s_addr & sortlist[i].mask.addr4.s_addr)
  432. == sortlist[i].addrV4.s_addr)
  433. break;
  434. }
  435. else
  436. {
  437. if (!ares__bitncmp(&addr->s_addr, &sortlist[i].addrV4.s_addr,
  438. sortlist[i].mask.bits))
  439. break;
  440. }
  441. }
  442. return i;
  443. }
  444. static void sort6_addresses(struct hostent *host,
  445. const struct apattern *sortlist, int nsort)
  446. {
  447. struct ares_in6_addr a1, a2;
  448. int i1, i2, ind1, ind2;
  449. /* This is a simple insertion sort, not optimized at all. i1 walks
  450. * through the address list, with the loop invariant that everything
  451. * to the left of i1 is sorted. In the loop body, the value at i1 is moved
  452. * back through the list (via i2) until it is in sorted order.
  453. */
  454. for (i1 = 0; host->h_addr_list[i1]; i1++)
  455. {
  456. memcpy(&a1, host->h_addr_list[i1], sizeof(struct ares_in6_addr));
  457. ind1 = get6_address_index(&a1, sortlist, nsort);
  458. for (i2 = i1 - 1; i2 >= 0; i2--)
  459. {
  460. memcpy(&a2, host->h_addr_list[i2], sizeof(struct ares_in6_addr));
  461. ind2 = get6_address_index(&a2, sortlist, nsort);
  462. if (ind2 <= ind1)
  463. break;
  464. memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct ares_in6_addr));
  465. }
  466. memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct ares_in6_addr));
  467. }
  468. }
  469. /* Find the first entry in sortlist which matches addr. Return nsort
  470. * if none of them match.
  471. */
  472. static int get6_address_index(const struct ares_in6_addr *addr,
  473. const struct apattern *sortlist,
  474. int nsort)
  475. {
  476. int i;
  477. for (i = 0; i < nsort; i++)
  478. {
  479. if (sortlist[i].family != AF_INET6)
  480. continue;
  481. if (!ares__bitncmp(addr, &sortlist[i].addrV6, sortlist[i].mask.bits))
  482. break;
  483. }
  484. return i;
  485. }