ares_gethostbyname.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /* $Id$ */
  2. /* Copyright 1998 by the Massachusetts Institute of Technology.
  3. *
  4. * Permission to use, copy, modify, and distribute this
  5. * software and its documentation for any purpose and without
  6. * fee is hereby granted, provided that the above copyright
  7. * notice appear in all copies and that both that copyright
  8. * notice and this permission notice appear in supporting
  9. * documentation, and that the name of M.I.T. not be used in
  10. * advertising or publicity pertaining to distribution of the
  11. * software without specific, written prior permission.
  12. * M.I.T. makes no representations about the suitability of
  13. * this software for any purpose. It is provided "as is"
  14. * without express or implied warranty.
  15. */
  16. #include "setup.h"
  17. #ifdef HAVE_SYS_SOCKET_H
  18. # include <sys/socket.h>
  19. #endif
  20. #ifdef HAVE_NETINET_IN_H
  21. # include <netinet/in.h>
  22. #endif
  23. #ifdef HAVE_NETDB_H
  24. # include <netdb.h>
  25. #endif
  26. #ifdef HAVE_ARPA_INET_H
  27. # include <arpa/inet.h>
  28. #endif
  29. #ifdef HAVE_ARPA_NAMESER_H
  30. # include <arpa/nameser.h>
  31. #else
  32. # include "nameser.h"
  33. #endif
  34. #ifdef HAVE_ARPA_NAMESER_COMPAT_H
  35. # include <arpa/nameser_compat.h>
  36. #endif
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <ctype.h>
  41. #ifdef HAVE_STRINGS_H
  42. #include <strings.h>
  43. #endif
  44. #include "ares.h"
  45. #include "inet_net_pton.h"
  46. #include "bitncmp.h"
  47. #include "ares_private.h"
  48. #ifdef WATT32
  49. #undef WIN32
  50. #endif
  51. struct host_query {
  52. /* Arguments passed to ares_gethostbyname() */
  53. ares_channel channel;
  54. char *name;
  55. ares_host_callback callback;
  56. void *arg;
  57. int sent_family; /* this family is what was is being used */
  58. int want_family; /* this family is what is asked for in the API */
  59. const char *remaining_lookups;
  60. int timeouts;
  61. };
  62. static void next_lookup(struct host_query *hquery, int status_code);
  63. static void host_callback(void *arg, int status, int timeouts,
  64. unsigned char *abuf, int alen);
  65. static void end_hquery(struct host_query *hquery, int status,
  66. struct hostent *host);
  67. static int fake_hostent(const char *name, int family,
  68. ares_host_callback callback, void *arg);
  69. static int file_lookup(const char *name, int family, struct hostent **host);
  70. static void sort_addresses(struct hostent *host,
  71. const struct apattern *sortlist, int nsort);
  72. static void sort6_addresses(struct hostent *host,
  73. const struct apattern *sortlist, int nsort);
  74. static int get_address_index(const struct in_addr *addr,
  75. const struct apattern *sortlist, int nsort);
  76. static int get6_address_index(const struct in6_addr *addr,
  77. const struct apattern *sortlist, int nsort);
  78. void ares_gethostbyname(ares_channel channel, const char *name, int family,
  79. ares_host_callback callback, void *arg)
  80. {
  81. struct host_query *hquery;
  82. /* Right now we only know how to look up Internet addresses - and unspec
  83. means try both basically. */
  84. switch (family) {
  85. case AF_INET:
  86. case AF_INET6:
  87. case AF_UNSPEC:
  88. break;
  89. default:
  90. callback(arg, ARES_ENOTIMP, 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 = 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 = strdup(name);
  104. hquery->want_family = family;
  105. hquery->sent_family = -1; /* nothing is sent yet */
  106. if (!hquery->name) {
  107. 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, host_callback,
  140. 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. /* The query returned something but either there were no AAAA records (e.g. just CNAME)
  180. or the response was malformed. Try looking up A instead.
  181. We should possibly limit this attempt-next logic to AF_UNSPEC lookups only. */
  182. hquery->sent_family = AF_INET;
  183. ares_search(hquery->channel, hquery->name, C_IN, T_A,
  184. host_callback, hquery);
  185. return;
  186. }
  187. if (host && channel->nsort)
  188. sort6_addresses(host, channel->sortlist, channel->nsort);
  189. }
  190. end_hquery(hquery, status, host);
  191. }
  192. else if ((status == ARES_ENODATA || status == ARES_EBADRESP || status == ARES_ETIMEOUT) && hquery->sent_family == AF_INET6)
  193. {
  194. /* The AAAA query yielded no useful result. Now look up an A instead.
  195. We should possibly limit this attempt-next logic to AF_UNSPEC lookups only. */
  196. hquery->sent_family = AF_INET;
  197. ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
  198. hquery);
  199. }
  200. else if (status == ARES_EDESTRUCTION)
  201. end_hquery(hquery, status, NULL);
  202. else
  203. next_lookup(hquery, status);
  204. }
  205. static void end_hquery(struct host_query *hquery, int status,
  206. struct hostent *host)
  207. {
  208. hquery->callback(hquery->arg, status, hquery->timeouts, host);
  209. if (host)
  210. ares_free_hostent(host);
  211. free(hquery->name);
  212. free(hquery);
  213. }
  214. /* If the name looks like an IP address, fake up a host entry, end the
  215. * query immediately, and return true. Otherwise return false.
  216. */
  217. static int fake_hostent(const char *name, int family, ares_host_callback callback,
  218. void *arg)
  219. {
  220. struct hostent hostent;
  221. char *aliases[1] = { NULL };
  222. char *addrs[2];
  223. int result = 0;
  224. struct in_addr in;
  225. struct in6_addr in6;
  226. if (family == AF_INET)
  227. {
  228. /* It only looks like an IP address if it's all numbers and dots. */
  229. int numdots = 0;
  230. const char *p;
  231. for (p = name; *p; p++)
  232. {
  233. if (!ISDIGIT(*p) && *p != '.') {
  234. return 0;
  235. } else if (*p == '.') {
  236. numdots++;
  237. }
  238. }
  239. /* if we don't have 3 dots, it is illegal
  240. * (although inet_addr doesn't think so).
  241. */
  242. if (numdots != 3)
  243. result = 0;
  244. else
  245. result = ((in.s_addr = inet_addr(name)) == INADDR_NONE ? 0 : 1);
  246. }
  247. else if (family == AF_INET6)
  248. result = (ares_inet_pton(AF_INET6, name, &in6) < 1 ? 0 : 1);
  249. if (!result)
  250. return 0;
  251. if (family == AF_INET)
  252. {
  253. hostent.h_length = (int)sizeof(struct in_addr);
  254. addrs[0] = (char *)&in;
  255. }
  256. else if (family == AF_INET6)
  257. {
  258. hostent.h_length = (int)sizeof(struct in6_addr);
  259. addrs[0] = (char *)&in6;
  260. }
  261. /* Duplicate the name, to avoid a constness violation. */
  262. hostent.h_name = strdup(name);
  263. if (!hostent.h_name)
  264. {
  265. callback(arg, ARES_ENOMEM, 0, NULL);
  266. return 1;
  267. }
  268. /* Fill in the rest of the host structure and terminate the query. */
  269. addrs[1] = NULL;
  270. hostent.h_aliases = aliases;
  271. hostent.h_addrtype = family;
  272. hostent.h_addr_list = addrs;
  273. callback(arg, ARES_SUCCESS, 0, &hostent);
  274. free((char *)(hostent.h_name));
  275. return 1;
  276. }
  277. /* This is an API method */
  278. int ares_gethostbyname_file(ares_channel channel, const char *name,
  279. int family, struct hostent **host)
  280. {
  281. int result;
  282. /* We only take the channel to ensure that ares_init() been called. */
  283. if(channel == NULL)
  284. {
  285. /* Anything will do, really. This seems fine, and is consistent with
  286. other error cases. */
  287. *host = NULL;
  288. return ARES_ENOTFOUND;
  289. }
  290. /* Just chain to the internal implementation we use here; it's exactly
  291. * what we want.
  292. */
  293. result = file_lookup(name, family, host);
  294. if(result != ARES_SUCCESS)
  295. {
  296. /* We guarantee a NULL hostent on failure. */
  297. *host = NULL;
  298. }
  299. return result;
  300. }
  301. static int file_lookup(const char *name, int family, struct hostent **host)
  302. {
  303. FILE *fp;
  304. char **alias;
  305. int status;
  306. int error;
  307. #ifdef WIN32
  308. char PATH_HOSTS[MAX_PATH];
  309. if (IS_NT()) {
  310. char tmp[MAX_PATH];
  311. HKEY hkeyHosts;
  312. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ, &hkeyHosts)
  313. == ERROR_SUCCESS)
  314. {
  315. DWORD dwLength = MAX_PATH;
  316. RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
  317. &dwLength);
  318. ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
  319. RegCloseKey(hkeyHosts);
  320. }
  321. }
  322. else
  323. GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
  324. strcat(PATH_HOSTS, WIN_PATH_HOSTS);
  325. #elif defined(WATT32)
  326. extern const char *_w32_GetHostsFile (void);
  327. const char *PATH_HOSTS = _w32_GetHostsFile();
  328. if (!PATH_HOSTS)
  329. return ARES_ENOTFOUND;
  330. #endif
  331. fp = fopen(PATH_HOSTS, "r");
  332. if (!fp)
  333. {
  334. error = ERRNO;
  335. switch(error)
  336. {
  337. case ENOENT:
  338. case ESRCH:
  339. return ARES_ENOTFOUND;
  340. default:
  341. DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
  342. error, strerror(error)));
  343. DEBUGF(fprintf(stderr, "Error opening file: %s\n",
  344. PATH_HOSTS));
  345. *host = NULL;
  346. return ARES_EFILE;
  347. }
  348. }
  349. while ((status = ares__get_hostent(fp, family, host)) == ARES_SUCCESS)
  350. {
  351. if (strcasecmp((*host)->h_name, name) == 0)
  352. break;
  353. for (alias = (*host)->h_aliases; *alias; alias++)
  354. {
  355. if (strcasecmp(*alias, name) == 0)
  356. break;
  357. }
  358. if (*alias)
  359. break;
  360. ares_free_hostent(*host);
  361. }
  362. fclose(fp);
  363. if (status == ARES_EOF)
  364. status = ARES_ENOTFOUND;
  365. if (status != ARES_SUCCESS)
  366. *host = NULL;
  367. return status;
  368. }
  369. static void sort_addresses(struct hostent *host, const struct apattern *sortlist,
  370. int nsort)
  371. {
  372. struct in_addr a1, a2;
  373. int i1, i2, ind1, ind2;
  374. /* This is a simple insertion sort, not optimized at all. i1 walks
  375. * through the address list, with the loop invariant that everything
  376. * to the left of i1 is sorted. In the loop body, the value at i1 is moved
  377. * back through the list (via i2) until it is in sorted order.
  378. */
  379. for (i1 = 0; host->h_addr_list[i1]; i1++)
  380. {
  381. memcpy(&a1, host->h_addr_list[i1], sizeof(struct in_addr));
  382. ind1 = get_address_index(&a1, sortlist, nsort);
  383. for (i2 = i1 - 1; i2 >= 0; i2--)
  384. {
  385. memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
  386. ind2 = get_address_index(&a2, sortlist, nsort);
  387. if (ind2 <= ind1)
  388. break;
  389. memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
  390. }
  391. memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in_addr));
  392. }
  393. }
  394. /* Find the first entry in sortlist which matches addr. Return nsort
  395. * if none of them match.
  396. */
  397. static int get_address_index(const struct in_addr *addr,
  398. const struct apattern *sortlist,
  399. int nsort)
  400. {
  401. int i;
  402. for (i = 0; i < nsort; i++)
  403. {
  404. if (sortlist[i].family != AF_INET)
  405. continue;
  406. if (sortlist[i].type == PATTERN_MASK)
  407. {
  408. if ((addr->s_addr & sortlist[i].mask.addr4.s_addr)
  409. == sortlist[i].addrV4.s_addr)
  410. break;
  411. }
  412. else
  413. {
  414. if (!ares_bitncmp(&addr->s_addr, &sortlist[i].addrV4.s_addr,
  415. sortlist[i].mask.bits))
  416. break;
  417. }
  418. }
  419. return i;
  420. }
  421. static void sort6_addresses(struct hostent *host, const struct apattern *sortlist,
  422. int nsort)
  423. {
  424. struct in6_addr a1, a2;
  425. int i1, i2, ind1, ind2;
  426. /* This is a simple insertion sort, not optimized at all. i1 walks
  427. * through the address list, with the loop invariant that everything
  428. * to the left of i1 is sorted. In the loop body, the value at i1 is moved
  429. * back through the list (via i2) until it is in sorted order.
  430. */
  431. for (i1 = 0; host->h_addr_list[i1]; i1++)
  432. {
  433. memcpy(&a1, host->h_addr_list[i1], sizeof(struct in6_addr));
  434. ind1 = get6_address_index(&a1, sortlist, nsort);
  435. for (i2 = i1 - 1; i2 >= 0; i2--)
  436. {
  437. memcpy(&a2, host->h_addr_list[i2], sizeof(struct in6_addr));
  438. ind2 = get6_address_index(&a2, sortlist, nsort);
  439. if (ind2 <= ind1)
  440. break;
  441. memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in6_addr));
  442. }
  443. memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in6_addr));
  444. }
  445. }
  446. /* Find the first entry in sortlist which matches addr. Return nsort
  447. * if none of them match.
  448. */
  449. static int get6_address_index(const struct in6_addr *addr,
  450. const struct apattern *sortlist,
  451. int nsort)
  452. {
  453. int i;
  454. for (i = 0; i < nsort; i++)
  455. {
  456. if (sortlist[i].family != AF_INET6)
  457. continue;
  458. if (!ares_bitncmp(&addr->s6_addr, &sortlist[i].addrV6.s6_addr, sortlist[i].mask.bits))
  459. break;
  460. }
  461. return i;
  462. }