2 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
17 #include "ares_setup.h"
24 #include "ares_private.h"
27 /* Arguments passed to ares_search */
29 char *name; /* copied into an allocated buffer */
32 ares_callback callback;
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? */
42 static void search_callback(void *arg, int status, int timeouts,
43 unsigned char *abuf, int alen);
44 static void end_squery(struct search_query *squery, int status,
45 unsigned char *abuf, int alen);
46 static int cat_domain(const char *name, const char *domain, char **s);
47 STATIC_TESTABLE int single_domain(ares_channel channel, const char *name, char **s);
49 void ares_search(ares_channel channel, const char *name, int dnsclass,
50 int type, ares_callback callback, void *arg)
52 struct search_query *squery;
57 /* Per RFC 7686, reject queries for ".onion" domain names with NXDOMAIN. */
58 if (ares__is_onion_domain(name))
60 callback(arg, ARES_ENOTFOUND, 0, NULL, 0);
64 /* If name only yields one domain to search, then we don't have
65 * to keep extra state, so just do an ares_query().
67 status = single_domain(channel, name, &s);
68 if (status != ARES_SUCCESS)
70 callback(arg, status, 0, NULL, 0);
75 ares_query(channel, s, dnsclass, type, callback, arg);
80 /* Allocate a search_query structure to hold the state necessary for
81 * doing multiple lookups.
83 squery = ares_malloc(sizeof(struct search_query));
86 callback(arg, ARES_ENOMEM, 0, NULL, 0);
89 squery->channel = channel;
90 squery->name = ares_strdup(name);
94 callback(arg, ARES_ENOMEM, 0, NULL, 0);
97 squery->dnsclass = dnsclass;
99 squery->status_as_is = -1;
100 squery->callback = callback;
102 squery->timeouts = 0;
103 squery->ever_got_nodata = 0;
105 /* Count the number of dots in name. */
107 for (p = name; *p; p++)
113 /* If ndots is at least the channel ndots threshold (usually 1),
114 * then we try the name as-is first. Otherwise, we try the name
117 if (ndots >= channel->ndots)
119 /* Try the name as-is first. */
120 squery->next_domain = 0;
121 squery->trying_as_is = 1;
122 ares_query(channel, name, dnsclass, type, search_callback, squery);
126 /* Try the name as-is last; start with the first search domain. */
127 squery->next_domain = 1;
128 squery->trying_as_is = 0;
129 status = cat_domain(name, channel->domains[0], &s);
130 if (status == ARES_SUCCESS)
132 ares_query(channel, s, dnsclass, type, search_callback, squery);
137 /* failed, free the malloc()ed memory */
138 ares_free(squery->name);
140 callback(arg, status, 0, NULL, 0);
145 static void search_callback(void *arg, int status, int timeouts,
146 unsigned char *abuf, int alen)
148 struct search_query *squery = (struct search_query *) arg;
149 ares_channel channel = squery->channel;
152 squery->timeouts += timeouts;
154 /* Stop searching unless we got a non-fatal error. */
155 if (status != ARES_ENODATA && status != ARES_ESERVFAIL
156 && status != ARES_ENOTFOUND)
157 end_squery(squery, status, abuf, alen);
160 /* Save the status if we were trying as-is. */
161 if (squery->trying_as_is)
162 squery->status_as_is = status;
165 * If we ever get ARES_ENODATA along the way, record that; if the search
166 * should run to the very end and we got at least one ARES_ENODATA,
167 * then callers like ares_gethostbyname() may want to try a T_A search
168 * even if the last domain we queried for T_AAAA resource records
169 * returned ARES_ENOTFOUND.
171 if (status == ARES_ENODATA)
172 squery->ever_got_nodata = 1;
174 if (squery->next_domain < channel->ndomains)
176 /* Try the next domain. */
177 status = cat_domain(squery->name,
178 channel->domains[squery->next_domain], &s);
179 if (status != ARES_SUCCESS)
180 end_squery(squery, status, NULL, 0);
183 squery->trying_as_is = 0;
184 squery->next_domain++;
185 ares_query(channel, s, squery->dnsclass, squery->type,
186 search_callback, squery);
190 else if (squery->status_as_is == -1)
192 /* Try the name as-is at the end. */
193 squery->trying_as_is = 1;
194 ares_query(channel, squery->name, squery->dnsclass, squery->type,
195 search_callback, squery);
198 if (squery->status_as_is == ARES_ENOTFOUND && squery->ever_got_nodata) {
199 end_squery(squery, ARES_ENODATA, NULL, 0);
202 end_squery(squery, squery->status_as_is, NULL, 0);
207 static void end_squery(struct search_query *squery, int status,
208 unsigned char *abuf, int alen)
210 squery->callback(squery->arg, status, squery->timeouts, abuf, alen);
211 ares_free(squery->name);
215 /* Concatenate two domains. */
216 static int cat_domain(const char *name, const char *domain, char **s)
218 size_t nlen = strlen(name);
219 size_t dlen = strlen(domain);
221 *s = ares_malloc(nlen + 1 + dlen + 1);
224 memcpy(*s, name, nlen);
226 memcpy(*s + nlen + 1, domain, dlen);
227 (*s)[nlen + 1 + dlen] = 0;
231 /* Determine if this name only yields one query. If it does, set *s to
232 * the string we should query, in an allocated buffer. If not, set *s
235 STATIC_TESTABLE int single_domain(ares_channel channel, const char *name, char **s)
237 size_t len = strlen(name);
238 const char *hostaliases;
246 /* If the name contains a trailing dot, then the single query is the name
247 * sans the trailing dot.
249 if ((len > 0) && (name[len - 1] == '.'))
251 *s = ares_strdup(name);
252 return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
255 if (!(channel->flags & ARES_FLAG_NOALIASES) && !strchr(name, '.'))
257 /* The name might be a host alias. */
258 hostaliases = getenv("HOSTALIASES");
261 fp = fopen(hostaliases, "r");
264 while ((status = ares__read_line(fp, &line, &linesize))
267 if (strncasecmp(line, name, len) != 0 ||
276 while (*q && !ISSPACE(*q))
278 *s = ares_malloc(q - p + 1);
281 memcpy(*s, p, q - p);
286 return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
291 if (status != ARES_SUCCESS && status != ARES_EOF)
303 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
304 error, strerror(error)));
305 DEBUGF(fprintf(stderr, "Error opening file: %s\n",
314 if (channel->flags & ARES_FLAG_NOSEARCH || channel->ndomains == 0)
316 /* No domain search to do; just try the name as-is. */
317 *s = ares_strdup(name);
318 return (*s) ? ARES_SUCCESS : ARES_ENOMEM;