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"
29 #include "ares_private.h"
32 /* Arguments passed to ares_search */
34 char *name; /* copied into an allocated buffer */
37 ares_callback callback;
40 int status_as_is; /* error status from trying as-is */
41 int next_domain; /* next search domain to try */
42 int trying_as_is; /* current query is for name as-is */
43 int timeouts; /* number of timeouts we saw for this request */
44 int ever_got_nodata; /* did we ever get ARES_ENODATA along the way? */
47 static void search_callback(void *arg, int status, int timeouts,
48 unsigned char *abuf, int alen);
49 static void end_squery(struct search_query *squery, int status,
50 unsigned char *abuf, int alen);
51 static int cat_domain(const char *name, const char *domain, char **s);
52 static int single_domain(ares_channel channel, const char *name, char **s);
54 void ares_search(ares_channel channel, const char *name, int dnsclass,
55 int type, ares_callback callback, void *arg)
57 struct search_query *squery;
62 /* If name only yields one domain to search, then we don't have
63 * to keep extra state, so just do an ares_query().
65 status = single_domain(channel, name, &s);
66 if (status != ARES_SUCCESS)
68 callback(arg, status, 0, NULL, 0);
73 ares_query(channel, s, dnsclass, type, callback, arg);
78 /* Allocate a search_query structure to hold the state necessary for
79 * doing multiple lookups.
81 squery = malloc(sizeof(struct search_query));
84 callback(arg, ARES_ENOMEM, 0, NULL, 0);
87 squery->channel = channel;
88 squery->name = strdup(name);
92 callback(arg, ARES_ENOMEM, 0, NULL, 0);
95 squery->dnsclass = dnsclass;
97 squery->status_as_is = -1;
98 squery->callback = callback;
100 squery->timeouts = 0;
101 squery->ever_got_nodata = 0;
103 /* Count the number of dots in name. */
105 for (p = name; *p; p++)
111 /* If ndots is at least the channel ndots threshold (usually 1),
112 * then we try the name as-is first. Otherwise, we try the name
115 if (ndots >= channel->ndots)
117 /* Try the name as-is first. */
118 squery->next_domain = 0;
119 squery->trying_as_is = 1;
120 ares_query(channel, name, dnsclass, type, search_callback, squery);
124 /* Try the name as-is last; start with the first search domain. */
125 squery->next_domain = 1;
126 squery->trying_as_is = 0;
127 status = cat_domain(name, channel->domains[0], &s);
128 if (status == ARES_SUCCESS)
130 ares_query(channel, s, dnsclass, type, search_callback, squery);
135 /* failed, free the malloc()ed memory */
138 callback(arg, status, 0, NULL, 0);
143 static void search_callback(void *arg, int status, int timeouts,
144 unsigned char *abuf, int alen)
146 struct search_query *squery = (struct search_query *) arg;
147 ares_channel channel = squery->channel;
150 squery->timeouts += timeouts;
152 /* Stop searching unless we got a non-fatal error. */
153 if (status != ARES_ENODATA && status != ARES_ESERVFAIL
154 && status != ARES_ENOTFOUND)
155 end_squery(squery, status, abuf, alen);
158 /* Save the status if we were trying as-is. */
159 if (squery->trying_as_is)
160 squery->status_as_is = status;
163 * If we ever get ARES_ENODATA along the way, record that; if the search
164 * should run to the very end and we got at least one ARES_ENODATA,
165 * then callers like ares_gethostbyname() may want to try a T_A search
166 * even if the last domain we queried for T_AAAA resource records
167 * returned ARES_ENOTFOUND.
169 if (status == ARES_ENODATA)
170 squery->ever_got_nodata = 1;
172 if (squery->next_domain < channel->ndomains)
174 /* Try the next domain. */
175 status = cat_domain(squery->name,
176 channel->domains[squery->next_domain], &s);
177 if (status != ARES_SUCCESS)
178 end_squery(squery, status, NULL, 0);
181 squery->trying_as_is = 0;
182 squery->next_domain++;
183 ares_query(channel, s, squery->dnsclass, squery->type,
184 search_callback, squery);
188 else if (squery->status_as_is == -1)
190 /* Try the name as-is at the end. */
191 squery->trying_as_is = 1;
192 ares_query(channel, squery->name, squery->dnsclass, squery->type,
193 search_callback, squery);
196 if (squery->status_as_is == ARES_ENOTFOUND && squery->ever_got_nodata) {
197 end_squery(squery, ARES_ENODATA, NULL, 0);
200 end_squery(squery, squery->status_as_is, NULL, 0);
205 static void end_squery(struct search_query *squery, int status,
206 unsigned char *abuf, int alen)
208 squery->callback(squery->arg, status, squery->timeouts, abuf, alen);
213 /* Concatenate two domains. */
214 static int cat_domain(const char *name, const char *domain, char **s)
216 size_t nlen = strlen(name);
217 size_t dlen = strlen(domain);
219 *s = malloc(nlen + 1 + dlen + 1);
222 memcpy(*s, name, nlen);
224 memcpy(*s + nlen + 1, domain, dlen);
225 (*s)[nlen + 1 + dlen] = 0;
229 /* Determine if this name only yields one query. If it does, set *s to
230 * the string we should query, in an allocated buffer. If not, set *s
233 static int single_domain(ares_channel channel, const char *name, char **s)
235 size_t len = strlen(name);
236 const char *hostaliases;
244 /* If the name contains a trailing dot, then the single query is the name
245 * sans the trailing dot.
247 if (name[len - 1] == '.')
250 return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
253 if (!(channel->flags & ARES_FLAG_NOALIASES) && !strchr(name, '.'))
255 /* The name might be a host alias. */
256 hostaliases = getenv("HOSTALIASES");
259 fp = fopen(hostaliases, "r");
262 while ((status = ares__read_line(fp, &line, &linesize))
265 if (strncasecmp(line, name, len) != 0 ||
274 while (*q && !ISSPACE(*q))
276 *s = malloc(q - p + 1);
279 memcpy(*s, p, q - p);
284 return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
289 if (status != ARES_SUCCESS && status != ARES_EOF)
301 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
302 error, strerror(error)));
303 DEBUGF(fprintf(stderr, "Error opening file: %s\n",
312 if (channel->flags & ARES_FLAG_NOSEARCH || channel->ndomains == 0)
314 /* No domain search to do; just try the name as-is. */
316 return (*s) ? ARES_SUCCESS : ARES_ENOMEM;