ares_search.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <errno.h>
  22. #ifdef HAVE_STRINGS_H
  23. # include <strings.h>
  24. #endif
  25. #include "ares.h"
  26. #include "ares_private.h"
  27. struct search_query {
  28. /* Arguments passed to ares_search */
  29. ares_channel channel;
  30. char *name; /* copied into an allocated buffer */
  31. int dnsclass;
  32. int type;
  33. ares_callback callback;
  34. void *arg;
  35. int status_as_is; /* error status from trying as-is */
  36. int next_domain; /* next search domain to try */
  37. int trying_as_is; /* current query is for name as-is */
  38. int timeouts; /* number of timeouts we saw for this request */
  39. int ever_got_nodata; /* did we ever get ARES_ENODATA along the way? */
  40. };
  41. static void search_callback(void *arg, int status, int timeouts,
  42. unsigned char *abuf, int alen);
  43. static void end_squery(struct search_query *squery, int status,
  44. unsigned char *abuf, int alen);
  45. static int cat_domain(const char *name, const char *domain, char **s);
  46. static int single_domain(ares_channel channel, const char *name, char **s);
  47. void ares_search(ares_channel channel, const char *name, int dnsclass,
  48. int type, ares_callback callback, void *arg)
  49. {
  50. struct search_query *squery;
  51. char *s;
  52. const char *p;
  53. int status, ndots;
  54. /* If name only yields one domain to search, then we don't have
  55. * to keep extra state, so just do an ares_query().
  56. */
  57. status = single_domain(channel, name, &s);
  58. if (status != ARES_SUCCESS)
  59. {
  60. callback(arg, status, 0, NULL, 0);
  61. return;
  62. }
  63. if (s)
  64. {
  65. ares_query(channel, s, dnsclass, type, callback, arg);
  66. free(s);
  67. return;
  68. }
  69. /* Allocate a search_query structure to hold the state necessary for
  70. * doing multiple lookups.
  71. */
  72. squery = malloc(sizeof(struct search_query));
  73. if (!squery)
  74. {
  75. callback(arg, ARES_ENOMEM, 0, NULL, 0);
  76. return;
  77. }
  78. squery->channel = channel;
  79. squery->name = strdup(name);
  80. if (!squery->name)
  81. {
  82. free(squery);
  83. callback(arg, ARES_ENOMEM, 0, NULL, 0);
  84. return;
  85. }
  86. squery->dnsclass = dnsclass;
  87. squery->type = type;
  88. squery->status_as_is = -1;
  89. squery->callback = callback;
  90. squery->arg = arg;
  91. squery->timeouts = 0;
  92. squery->ever_got_nodata = 0;
  93. /* Count the number of dots in name. */
  94. ndots = 0;
  95. for (p = name; *p; p++)
  96. {
  97. if (*p == '.')
  98. ndots++;
  99. }
  100. /* If ndots is at least the channel ndots threshold (usually 1),
  101. * then we try the name as-is first. Otherwise, we try the name
  102. * as-is last.
  103. */
  104. if (ndots >= channel->ndots)
  105. {
  106. /* Try the name as-is first. */
  107. squery->next_domain = 0;
  108. squery->trying_as_is = 1;
  109. ares_query(channel, name, dnsclass, type, search_callback, squery);
  110. }
  111. else
  112. {
  113. /* Try the name as-is last; start with the first search domain. */
  114. squery->next_domain = 1;
  115. squery->trying_as_is = 0;
  116. status = cat_domain(name, channel->domains[0], &s);
  117. if (status == ARES_SUCCESS)
  118. {
  119. ares_query(channel, s, dnsclass, type, search_callback, squery);
  120. free(s);
  121. }
  122. else
  123. {
  124. /* failed, free the malloc()ed memory */
  125. free(squery->name);
  126. free(squery);
  127. callback(arg, status, 0, NULL, 0);
  128. }
  129. }
  130. }
  131. static void search_callback(void *arg, int status, int timeouts,
  132. unsigned char *abuf, int alen)
  133. {
  134. struct search_query *squery = (struct search_query *) arg;
  135. ares_channel channel = squery->channel;
  136. char *s;
  137. squery->timeouts += timeouts;
  138. /* Stop searching unless we got a non-fatal error. */
  139. if (status != ARES_ENODATA && status != ARES_ESERVFAIL
  140. && status != ARES_ENOTFOUND)
  141. end_squery(squery, status, abuf, alen);
  142. else
  143. {
  144. /* Save the status if we were trying as-is. */
  145. if (squery->trying_as_is)
  146. squery->status_as_is = status;
  147. /*
  148. * If we ever get ARES_ENODATA along the way, record that; if the search
  149. * should run to the very end and we got at least one ARES_ENODATA,
  150. * then callers like ares_gethostbyname() may want to try a T_A search
  151. * even if the last domain we queried for T_AAAA resource records
  152. * returned ARES_ENOTFOUND.
  153. */
  154. if (status == ARES_ENODATA)
  155. squery->ever_got_nodata = 1;
  156. if (squery->next_domain < channel->ndomains)
  157. {
  158. /* Try the next domain. */
  159. status = cat_domain(squery->name,
  160. channel->domains[squery->next_domain], &s);
  161. if (status != ARES_SUCCESS)
  162. end_squery(squery, status, NULL, 0);
  163. else
  164. {
  165. squery->trying_as_is = 0;
  166. squery->next_domain++;
  167. ares_query(channel, s, squery->dnsclass, squery->type,
  168. search_callback, squery);
  169. free(s);
  170. }
  171. }
  172. else if (squery->status_as_is == -1)
  173. {
  174. /* Try the name as-is at the end. */
  175. squery->trying_as_is = 1;
  176. ares_query(channel, squery->name, squery->dnsclass, squery->type,
  177. search_callback, squery);
  178. }
  179. else {
  180. if (squery->status_as_is == ARES_ENOTFOUND && squery->ever_got_nodata) {
  181. end_squery(squery, ARES_ENODATA, NULL, 0);
  182. }
  183. else
  184. end_squery(squery, squery->status_as_is, NULL, 0);
  185. }
  186. }
  187. }
  188. static void end_squery(struct search_query *squery, int status,
  189. unsigned char *abuf, int alen)
  190. {
  191. squery->callback(squery->arg, status, squery->timeouts, abuf, alen);
  192. free(squery->name);
  193. free(squery);
  194. }
  195. /* Concatenate two domains. */
  196. static int cat_domain(const char *name, const char *domain, char **s)
  197. {
  198. size_t nlen = strlen(name);
  199. size_t dlen = strlen(domain);
  200. *s = malloc(nlen + 1 + dlen + 1);
  201. if (!*s)
  202. return ARES_ENOMEM;
  203. memcpy(*s, name, nlen);
  204. (*s)[nlen] = '.';
  205. memcpy(*s + nlen + 1, domain, dlen);
  206. (*s)[nlen + 1 + dlen] = 0;
  207. return ARES_SUCCESS;
  208. }
  209. /* Determine if this name only yields one query. If it does, set *s to
  210. * the string we should query, in an allocated buffer. If not, set *s
  211. * to NULL.
  212. */
  213. static int single_domain(ares_channel channel, const char *name, char **s)
  214. {
  215. size_t len = strlen(name);
  216. const char *hostaliases;
  217. FILE *fp;
  218. char *line = NULL;
  219. int linesize, status;
  220. const char *p, *q;
  221. int error;
  222. /* If the name contains a trailing dot, then the single query is the name
  223. * sans the trailing dot.
  224. */
  225. if (name[len - 1] == '.')
  226. {
  227. *s = strdup(name);
  228. return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
  229. }
  230. if (!(channel->flags & ARES_FLAG_NOALIASES) && !strchr(name, '.'))
  231. {
  232. /* The name might be a host alias. */
  233. hostaliases = getenv("HOSTALIASES");
  234. if (hostaliases)
  235. {
  236. fp = fopen(hostaliases, "r");
  237. if (fp)
  238. {
  239. while ((status = ares__read_line(fp, &line, &linesize))
  240. == ARES_SUCCESS)
  241. {
  242. if (strncasecmp(line, name, len) != 0 ||
  243. !ISSPACE(line[len]))
  244. continue;
  245. p = line + len;
  246. while (ISSPACE(*p))
  247. p++;
  248. if (*p)
  249. {
  250. q = p + 1;
  251. while (*q && !ISSPACE(*q))
  252. q++;
  253. *s = malloc(q - p + 1);
  254. if (*s)
  255. {
  256. memcpy(*s, p, q - p);
  257. (*s)[q - p] = 0;
  258. }
  259. free(line);
  260. fclose(fp);
  261. return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
  262. }
  263. }
  264. free(line);
  265. fclose(fp);
  266. if (status != ARES_SUCCESS)
  267. return status;
  268. }
  269. else
  270. {
  271. error = errno;
  272. switch(error)
  273. {
  274. case ENOENT:
  275. case ESRCH:
  276. break;
  277. default:
  278. DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
  279. error, strerror(error)));
  280. DEBUGF(fprintf(stderr, "Error opening file: %s\n",
  281. hostaliases));
  282. *s = NULL;
  283. return ARES_EFILE;
  284. }
  285. }
  286. }
  287. }
  288. if (channel->flags & ARES_FLAG_NOSEARCH || channel->ndomains == 0)
  289. {
  290. /* No domain search to do; just try the name as-is. */
  291. *s = strdup(name);
  292. return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
  293. }
  294. *s = NULL;
  295. return ARES_SUCCESS;
  296. }