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 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 /* If name only yields one domain to search, then we don't have
58 * to keep extra state, so just do an ares_query().
60 status = single_domain(channel, name, &s);
61 if (status != ARES_SUCCESS)
63 callback(arg, status, 0, NULL, 0);
68 ares_query(channel, s, dnsclass, type, callback, arg);
73 /* Allocate a search_query structure to hold the state necessary for
74 * doing multiple lookups.
76 squery = malloc(sizeof(struct search_query));
79 callback(arg, ARES_ENOMEM, 0, NULL, 0);
82 squery->channel = channel;
83 squery->name = strdup(name);
87 callback(arg, ARES_ENOMEM, 0, NULL, 0);
90 squery->dnsclass = dnsclass;
92 squery->status_as_is = -1;
93 squery->callback = callback;
96 squery->ever_got_nodata = 0;
98 /* Count the number of dots in name. */
100 for (p = name; *p; p++)
106 /* If ndots is at least the channel ndots threshold (usually 1),
107 * then we try the name as-is first. Otherwise, we try the name
110 if (ndots >= channel->ndots)
112 /* Try the name as-is first. */
113 squery->next_domain = 0;
114 squery->trying_as_is = 1;
115 ares_query(channel, name, dnsclass, type, search_callback, squery);
119 /* Try the name as-is last; start with the first search domain. */
120 squery->next_domain = 1;
121 squery->trying_as_is = 0;
122 status = cat_domain(name, channel->domains[0], &s);
123 if (status == ARES_SUCCESS)
125 ares_query(channel, s, dnsclass, type, search_callback, squery);
130 /* failed, free the malloc()ed memory */
133 callback(arg, status, 0, NULL, 0);
138 static void search_callback(void *arg, int status, int timeouts,
139 unsigned char *abuf, int alen)
141 struct search_query *squery = (struct search_query *) arg;
142 ares_channel channel = squery->channel;
145 squery->timeouts += timeouts;
147 /* Stop searching unless we got a non-fatal error. */
148 if (status != ARES_ENODATA && status != ARES_ESERVFAIL
149 && status != ARES_ENOTFOUND)
150 end_squery(squery, status, abuf, alen);
153 /* Save the status if we were trying as-is. */
154 if (squery->trying_as_is)
155 squery->status_as_is = status;
158 * If we ever get ARES_ENODATA along the way, record that; if the search
159 * should run to the very end and we got at least one ARES_ENODATA,
160 * then callers like ares_gethostbyname() may want to try a T_A search
161 * even if the last domain we queried for T_AAAA resource records
162 * returned ARES_ENOTFOUND.
164 if (status == ARES_ENODATA)
165 squery->ever_got_nodata = 1;
167 if (squery->next_domain < channel->ndomains)
169 /* Try the next domain. */
170 status = cat_domain(squery->name,
171 channel->domains[squery->next_domain], &s);
172 if (status != ARES_SUCCESS)
173 end_squery(squery, status, NULL, 0);
176 squery->trying_as_is = 0;
177 squery->next_domain++;
178 ares_query(channel, s, squery->dnsclass, squery->type,
179 search_callback, squery);
183 else if (squery->status_as_is == -1)
185 /* Try the name as-is at the end. */
186 squery->trying_as_is = 1;
187 ares_query(channel, squery->name, squery->dnsclass, squery->type,
188 search_callback, squery);
191 if (squery->status_as_is == ARES_ENOTFOUND && squery->ever_got_nodata) {
192 end_squery(squery, ARES_ENODATA, NULL, 0);
195 end_squery(squery, squery->status_as_is, NULL, 0);
200 static void end_squery(struct search_query *squery, int status,
201 unsigned char *abuf, int alen)
203 squery->callback(squery->arg, status, squery->timeouts, abuf, alen);
208 /* Concatenate two domains. */
209 static int cat_domain(const char *name, const char *domain, char **s)
211 size_t nlen = strlen(name);
212 size_t dlen = strlen(domain);
214 *s = malloc(nlen + 1 + dlen + 1);
217 memcpy(*s, name, nlen);
219 memcpy(*s + nlen + 1, domain, dlen);
220 (*s)[nlen + 1 + dlen] = 0;
224 /* Determine if this name only yields one query. If it does, set *s to
225 * the string we should query, in an allocated buffer. If not, set *s
228 static int single_domain(ares_channel channel, const char *name, char **s)
230 size_t len = strlen(name);
231 const char *hostaliases;
239 /* If the name contains a trailing dot, then the single query is the name
240 * sans the trailing dot.
242 if (name[len - 1] == '.')
245 return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
248 if (!(channel->flags & ARES_FLAG_NOALIASES) && !strchr(name, '.'))
250 /* The name might be a host alias. */
251 hostaliases = getenv("HOSTALIASES");
254 fp = fopen(hostaliases, "r");
257 while ((status = ares__read_line(fp, &line, &linesize))
260 if (strncasecmp(line, name, len) != 0 ||
269 while (*q && !ISSPACE(*q))
271 *s = malloc(q - p + 1);
274 memcpy(*s, p, q - p);
279 return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
284 if (status != ARES_SUCCESS && status != ARES_EOF)
296 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n",
297 error, strerror(error)));
298 DEBUGF(fprintf(stderr, "Error opening file: %s\n",
307 if (channel->flags & ARES_FLAG_NOSEARCH || channel->ndomains == 0)
309 /* No domain search to do; just try the name as-is. */
311 return (*s) ? ARES_SUCCESS : ARES_ENOMEM;