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