1 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
16 static const char rcsid[] = "$Id: ares_search.c,v 1.3 2000/09/21 19:16:02 ghudson Exp $";
23 #include "ares_private.h"
26 /* Arguments passed to ares_search */
28 char *name; /* copied into an allocated buffer */
31 ares_callback callback;
34 int status_as_is; /* error status from trying as-is */
35 int next_domain; /* next search domain to try */
36 int trying_as_is; /* current query is for name as-is */
39 static void search_callback(void *arg, int status, unsigned char *abuf,
41 static void end_squery(struct search_query *squery, int status,
42 unsigned char *abuf, int alen);
43 static int cat_domain(const char *name, const char *domain, char **s);
44 static int single_domain(ares_channel channel, const char *name, char **s);
46 void ares_search(ares_channel channel, const char *name, int dnsclass,
47 int type, ares_callback callback, void *arg)
49 struct search_query *squery;
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().
57 status = single_domain(channel, name, &s);
58 if (status != ARES_SUCCESS)
60 callback(arg, status, NULL, 0);
65 ares_query(channel, s, dnsclass, type, callback, arg);
70 /* Allocate a search_query structure to hold the state necessary for
71 * doing multiple lookups.
73 squery = malloc(sizeof(struct search_query));
76 callback(arg, ARES_ENOMEM, NULL, 0);
79 squery->channel = channel;
80 squery->name = strdup(name);
84 callback(arg, ARES_ENOMEM, NULL, 0);
87 squery->dnsclass = dnsclass;
89 squery->status_as_is = -1;
90 squery->callback = callback;
93 /* Count the number of dots in name. */
95 for (p = name; *p; p++)
101 /* If ndots is at least the channel ndots threshold (usually 1),
102 * then we try the name as-is first. Otherwise, we try the name
105 if (ndots >= channel->ndots)
107 /* Try the name as-is first. */
108 squery->next_domain = 0;
109 squery->trying_as_is = 1;
110 ares_query(channel, name, dnsclass, type, search_callback, squery);
114 /* Try the name as-is last; start with the first search domain. */
115 squery->next_domain = 1;
116 squery->trying_as_is = 0;
117 status = cat_domain(name, channel->domains[0], &s);
118 if (status == ARES_SUCCESS)
120 ares_query(channel, s, dnsclass, type, search_callback, squery);
124 callback(arg, status, NULL, 0);
128 static void search_callback(void *arg, int status, unsigned char *abuf,
131 struct search_query *squery = (struct search_query *) arg;
132 ares_channel channel = squery->channel;
135 /* Stop searching unless we got a non-fatal error. */
136 if (status != ARES_ENODATA && status != ARES_ESERVFAIL
137 && status != ARES_ENOTFOUND)
138 end_squery(squery, status, abuf, alen);
141 /* Save the status if we were trying as-is. */
142 if (squery->trying_as_is)
143 squery->status_as_is = status;
144 if (squery->next_domain < channel->ndomains)
146 /* Try the next domain. */
147 status = cat_domain(squery->name,
148 channel->domains[squery->next_domain], &s);
149 if (status != ARES_SUCCESS)
150 end_squery(squery, status, NULL, 0);
153 squery->trying_as_is = 0;
154 squery->next_domain++;
155 ares_query(channel, s, squery->dnsclass, squery->type,
156 search_callback, squery);
160 else if (squery->status_as_is == -1)
162 /* Try the name as-is at the end. */
163 squery->trying_as_is = 1;
164 ares_query(channel, squery->name, squery->dnsclass, squery->type,
165 search_callback, squery);
168 end_squery(squery, squery->status_as_is, NULL, 0);
172 static void end_squery(struct search_query *squery, int status,
173 unsigned char *abuf, int alen)
175 squery->callback(squery->arg, status, abuf, alen);
180 /* Concatenate two domains. */
181 static int cat_domain(const char *name, const char *domain, char **s)
183 int nlen = strlen(name), dlen = strlen(domain);
185 *s = malloc(nlen + 1 + dlen + 1);
188 memcpy(*s, name, nlen);
190 memcpy(*s + nlen + 1, domain, dlen);
191 (*s)[nlen + 1 + dlen] = 0;
195 /* Determine if this name only yields one query. If it does, set *s to
196 * the string we should query, in an allocated buffer. If not, set *s
199 static int single_domain(ares_channel channel, const char *name, char **s)
201 int len = strlen(name);
202 const char *hostaliases;
205 int linesize, status;
208 /* If the name contains a trailing dot, then the single query is the name
209 * sans the trailing dot.
211 if (name[len - 1] == '.')
214 return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
217 if (!(channel->flags & ARES_FLAG_NOALIASES) && !strchr(name, '.'))
219 /* The name might be a host alias. */
220 hostaliases = getenv("HOSTALIASES");
223 fp = fopen(hostaliases, "r");
226 while ((status = ares__read_line(fp, &line, &linesize))
229 if (strncasecmp(line, name, len) != 0 ||
230 !isspace((unsigned char)line[len]))
233 while (isspace((unsigned char)*p))
238 while (*q && !isspace((unsigned char)*q))
240 *s = malloc(q - p + 1);
243 memcpy(*s, p, q - p);
248 return (*s) ? ARES_SUCCESS : ARES_ENOMEM;
253 if (status != ARES_SUCCESS)
259 if (channel->flags & ARES_FLAG_NOSEARCH || channel->ndomains == 0)
261 /* No domain search to do; just try the name as-is. */
263 return (*s) ? ARES_SUCCESS : ARES_ENOMEM;