]> sourceware.org Git - glibc.git/blob - resolv/res_query.c
Update.
[glibc.git] / resolv / res_query.c
1 /*
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
32 *
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47 * SOFTWARE.
48 */
49
50 /*
51 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
52 *
53 * Permission to use, copy, modify, and distribute this software for any
54 * purpose with or without fee is hereby granted, provided that the above
55 * copyright notice and this permission notice appear in all copies.
56 *
57 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
58 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
60 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
61 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
62 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
63 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
64 * SOFTWARE.
65 */
66
67 #if defined(LIBC_SCCS) && !defined(lint)
68 static const char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93";
69 static const char rcsid[] = "$BINDId: res_query.c,v 8.20 2000/02/29 05:39:12 vixie Exp $";
70 #endif /* LIBC_SCCS and not lint */
71
72 #include <sys/types.h>
73 #include <sys/param.h>
74 #include <netinet/in.h>
75 #include <arpa/inet.h>
76 #include <arpa/nameser.h>
77 #include <ctype.h>
78 #include <errno.h>
79 #include <netdb.h>
80 #include <resolv.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84
85 /* Options. Leave them on. */
86 /* #undef DEBUG */
87
88 #if PACKETSZ > 65536
89 #define MAXPACKET PACKETSZ
90 #else
91 #define MAXPACKET 65536
92 #endif
93
94 /*
95 * Formulate a normal query, send, and await answer.
96 * Returned answer is placed in supplied buffer "answer".
97 * Perform preliminary check of answer, returning success only
98 * if no error is indicated and the answer count is nonzero.
99 * Return the size of the response on success, -1 on error.
100 * Error number is left in H_ERRNO.
101 *
102 * Caller must parse answer and determine whether it answers the question.
103 */
104 int
105 res_nquery(res_state statp,
106 const char *name, /* domain name */
107 int class, int type, /* class and type of query */
108 u_char *answer, /* buffer to put answer */
109 int anslen) /* size of answer buffer */
110 {
111 u_char *buf;
112 HEADER *hp = (HEADER *) answer;
113 int n, use_malloc = 0;
114
115 hp->rcode = NOERROR; /* default */
116
117 if (!__libc_use_alloca (MAXPACKET)) {
118 buf = malloc (MAXPACKET);
119 if (buf == NULL) {
120 __set_h_errno (NETDB_INTERNAL);
121 return -1;
122 }
123 use_malloc = 1;
124 } else
125 buf = alloca (MAXPACKET);
126
127 #ifdef DEBUG
128 if (statp->options & RES_DEBUG)
129 printf(";; res_query(%s, %d, %d)\n", name, class, type);
130 #endif
131
132 n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
133 buf, MAXPACKET);
134 if (n <= 0) {
135 #ifdef DEBUG
136 if (statp->options & RES_DEBUG)
137 printf(";; res_query: mkquery failed\n");
138 #endif
139 RES_SET_H_ERRNO(statp, NO_RECOVERY);
140 if (use_malloc)
141 free (buf);
142 return (n);
143 }
144 n = res_nsend(statp, buf, n, answer, anslen);
145 if (use_malloc)
146 free (buf);
147 if (n < 0) {
148 #ifdef DEBUG
149 if (statp->options & RES_DEBUG)
150 printf(";; res_query: send error\n");
151 #endif
152 RES_SET_H_ERRNO(statp, TRY_AGAIN);
153 return (n);
154 }
155
156 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
157 #ifdef DEBUG
158 if (statp->options & RES_DEBUG)
159 printf(";; rcode = %d, ancount=%d\n", hp->rcode,
160 ntohs(hp->ancount));
161 #endif
162 switch (hp->rcode) {
163 case NXDOMAIN:
164 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
165 break;
166 case SERVFAIL:
167 RES_SET_H_ERRNO(statp, TRY_AGAIN);
168 break;
169 case NOERROR:
170 RES_SET_H_ERRNO(statp, NO_DATA);
171 break;
172 case FORMERR:
173 case NOTIMP:
174 case REFUSED:
175 default:
176 RES_SET_H_ERRNO(statp, NO_RECOVERY);
177 break;
178 }
179 return (-1);
180 }
181 return (n);
182 }
183
184 /*
185 * Formulate a normal query, send, and retrieve answer in supplied buffer.
186 * Return the size of the response on success, -1 on error.
187 * If enabled, implement search rules until answer or unrecoverable failure
188 * is detected. Error code, if any, is left in H_ERRNO.
189 */
190 int
191 res_nsearch(res_state statp,
192 const char *name, /* domain name */
193 int class, int type, /* class and type of query */
194 u_char *answer, /* buffer to put answer */
195 int anslen) /* size of answer */
196 {
197 const char *cp, * const *domain;
198 HEADER *hp = (HEADER *) answer;
199 char tmp[NS_MAXDNAME];
200 u_int dots;
201 int trailing_dot, ret, saved_herrno;
202 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
203 int tried_as_is = 0;
204
205 __set_errno (0);
206 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */
207
208 dots = 0;
209 for (cp = name; *cp != '\0'; cp++)
210 dots += (*cp == '.');
211 trailing_dot = 0;
212 if (cp > name && *--cp == '.')
213 trailing_dot++;
214
215 /* If there aren't any dots, it could be a user-level alias. */
216 if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
217 return (res_nquery(statp, cp, class, type, answer, anslen));
218
219 /*
220 * If there are enough dots in the name, let's just give it a
221 * try 'as is'. The threshold can be set with the "ndots" option.
222 * Also, query 'as is', if there is a trailing dot in the name.
223 */
224 saved_herrno = -1;
225 if (dots >= statp->ndots || trailing_dot) {
226 ret = res_nquerydomain(statp, name, NULL, class, type,
227 answer, anslen);
228 if (ret > 0 || trailing_dot)
229 return (ret);
230 saved_herrno = h_errno;
231 tried_as_is++;
232 }
233
234 /*
235 * We do at least one level of search if
236 * - there is no dot and RES_DEFNAME is set, or
237 * - there is at least one dot, there is no trailing dot,
238 * and RES_DNSRCH is set.
239 */
240 if ((!dots && (statp->options & RES_DEFNAMES) != 0) ||
241 (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0)) {
242 int done = 0;
243
244 for (domain = (const char * const *)statp->dnsrch;
245 *domain && !done;
246 domain++) {
247
248 if (domain[0][0] == '\0' ||
249 (domain[0][0] == '.' && domain[0][1] == '\0'))
250 root_on_list++;
251
252 ret = res_nquerydomain(statp, name, *domain,
253 class, type,
254 answer, anslen);
255 if (ret > 0)
256 return (ret);
257
258 /*
259 * If no server present, give up.
260 * If name isn't found in this domain,
261 * keep trying higher domains in the search list
262 * (if that's enabled).
263 * On a NO_DATA error, keep trying, otherwise
264 * a wildcard entry of another type could keep us
265 * from finding this entry higher in the domain.
266 * If we get some other error (negative answer or
267 * server failure), then stop searching up,
268 * but try the input name below in case it's
269 * fully-qualified.
270 */
271 if (errno == ECONNREFUSED) {
272 RES_SET_H_ERRNO(statp, TRY_AGAIN);
273 return (-1);
274 }
275
276 switch (statp->res_h_errno) {
277 case NO_DATA:
278 got_nodata++;
279 /* FALLTHROUGH */
280 case HOST_NOT_FOUND:
281 /* keep trying */
282 break;
283 case TRY_AGAIN:
284 if (hp->rcode == SERVFAIL) {
285 /* try next search element, if any */
286 got_servfail++;
287 break;
288 }
289 /* FALLTHROUGH */
290 default:
291 /* anything else implies that we're done */
292 done++;
293 }
294
295 /* if we got here for some reason other than DNSRCH,
296 * we only wanted one iteration of the loop, so stop.
297 */
298 if ((statp->options & RES_DNSRCH) == 0)
299 done++;
300 }
301 }
302
303 /*
304 * If the name has any dots at all, and no earlier 'as-is' query
305 * for the name, and "." is not on the search list, then try an as-is
306 * query now.
307 */
308 if (statp->ndots && !(tried_as_is || root_on_list)) {
309 ret = res_nquerydomain(statp, name, NULL, class, type,
310 answer, anslen);
311 if (ret > 0)
312 return (ret);
313 }
314
315 /* if we got here, we didn't satisfy the search.
316 * if we did an initial full query, return that query's H_ERRNO
317 * (note that we wouldn't be here if that query had succeeded).
318 * else if we ever got a nodata, send that back as the reason.
319 * else send back meaningless H_ERRNO, that being the one from
320 * the last DNSRCH we did.
321 */
322 if (saved_herrno != -1)
323 RES_SET_H_ERRNO(statp, saved_herrno);
324 else if (got_nodata)
325 RES_SET_H_ERRNO(statp, NO_DATA);
326 else if (got_servfail)
327 RES_SET_H_ERRNO(statp, TRY_AGAIN);
328 return (-1);
329 }
330
331 /*
332 * Perform a call on res_query on the concatenation of name and domain,
333 * removing a trailing dot from name if domain is NULL.
334 */
335 int
336 res_nquerydomain(res_state statp,
337 const char *name,
338 const char *domain,
339 int class, int type, /* class and type of query */
340 u_char *answer, /* buffer to put answer */
341 int anslen) /* size of answer */
342 {
343 char nbuf[MAXDNAME];
344 const char *longname = nbuf;
345 int n, d;
346
347 #ifdef DEBUG
348 if (statp->options & RES_DEBUG)
349 printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
350 name, domain?domain:"<Nil>", class, type);
351 #endif
352 if (domain == NULL) {
353 /*
354 * Check for trailing '.';
355 * copy without '.' if present.
356 */
357 n = strlen(name);
358 if (n >= MAXDNAME) {
359 RES_SET_H_ERRNO(statp, NO_RECOVERY);
360 return (-1);
361 }
362 n--;
363 if (n >= 0 && name[n] == '.') {
364 strncpy(nbuf, name, n);
365 nbuf[n] = '\0';
366 } else
367 longname = name;
368 } else {
369 n = strlen(name);
370 d = strlen(domain);
371 if (n + d + 1 >= MAXDNAME) {
372 RES_SET_H_ERRNO(statp, NO_RECOVERY);
373 return (-1);
374 }
375 sprintf(nbuf, "%s.%s", name, domain);
376 }
377 return (res_nquery(statp, longname, class, type, answer, anslen));
378 }
379
380 const char *
381 res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
382 char *file, *cp1, *cp2;
383 char buf[BUFSIZ];
384 FILE *fp;
385
386 if (statp->options & RES_NOALIASES)
387 return (NULL);
388 file = getenv("HOSTALIASES");
389 if (file == NULL || (fp = fopen(file, "r")) == NULL)
390 return (NULL);
391 setbuf(fp, NULL);
392 buf[sizeof(buf) - 1] = '\0';
393 while (fgets(buf, sizeof(buf), fp)) {
394 for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)
395 ;
396 if (!*cp1)
397 break;
398 *cp1 = '\0';
399 if (ns_samename(buf, name) == 1) {
400 while (isspace(*++cp1))
401 ;
402 if (!*cp1)
403 break;
404 for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)
405 ;
406 *cp2 = '\0';
407 strncpy(dst, cp1, siz - 1);
408 dst[siz - 1] = '\0';
409 fclose(fp);
410 return (dst);
411 }
412 }
413 fclose(fp);
414 return (NULL);
415 }
This page took 0.062446 seconds and 6 git commands to generate.