ares_search.c 9.5 KB

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