Updated to fedora-glibc-20080716T0944
[glibc.git] / resolv / res_query.c
blobb2b45acde7553006f420324236375ea7710886d4
1 /*
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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.
51 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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.
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 */
72 #include <assert.h>
73 #include <sys/types.h>
74 #include <sys/param.h>
75 #include <netinet/in.h>
76 #include <arpa/inet.h>
77 #include <arpa/nameser.h>
78 #include <ctype.h>
79 #include <errno.h>
80 #include <netdb.h>
81 #include <resolv.h>
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
86 /* Options. Leave them on. */
87 /* #undef DEBUG */
89 #if PACKETSZ > 65536
90 #define MAXPACKET PACKETSZ
91 #else
92 #define MAXPACKET 65536
93 #endif
95 #define QUERYSIZE (HFIXEDSZ + QFIXEDSZ + MAXCDNAME + 1)
97 static int
98 __libc_res_nquerydomain(res_state statp, const char *name, const char *domain,
99 int class, int type, u_char *answer, int anslen,
100 u_char **answerp, u_char **answerp2, int *nanswerp2);
103 * Formulate a normal query, send, and await answer.
104 * Returned answer is placed in supplied buffer "answer".
105 * Perform preliminary check of answer, returning success only
106 * if no error is indicated and the answer count is nonzero.
107 * Return the size of the response on success, -1 on error.
108 * Error number is left in H_ERRNO.
110 * Caller must parse answer and determine whether it answers the question.
113 __libc_res_nquery(res_state statp,
114 const char *name, /* domain name */
115 int class, int type, /* class and type of query */
116 u_char *answer, /* buffer to put answer */
117 int anslen, /* size of answer buffer */
118 u_char **answerp, /* if buffer needs to be enlarged */
119 u_char **answerp2,
120 int *nanswerp2)
122 HEADER *hp = (HEADER *) answer;
123 int n, use_malloc = 0;
124 u_int oflags = statp->_flags;
126 size_t bufsize = (type == T_UNSPEC ? 2 : 1) * QUERYSIZE;
127 u_char *buf = alloca (bufsize);
128 u_char *query1 = buf;
129 int nquery1 = -1;
130 u_char *query2 = NULL;
131 int nquery2 = 0;
133 again:
134 hp->rcode = NOERROR; /* default */
136 #ifdef DEBUG
137 if (statp->options & RES_DEBUG)
138 printf(";; res_query(%s, %d, %d)\n", name, class, type);
139 #endif
141 if (type == T_UNSPEC)
143 n = res_nmkquery(statp, QUERY, name, class, T_A, NULL, 0, NULL,
144 query1, bufsize);
145 if (n > 0)
147 if ((oflags & RES_F_EDNS0ERR) == 0
148 && (statp->options & RES_USE_EDNS0) != 0)
150 n = __res_nopt(statp, n, query1, bufsize, anslen / 2);
151 if (n < 0)
152 goto unspec_nomem;
155 nquery1 = n;
156 /* Align the buffer. */
157 int npad = ((nquery1 + __alignof__ (HEADER) - 1)
158 & ~(__alignof__ (HEADER) - 1)) - nquery1;
159 if (n > bufsize - npad)
161 n = -1;
162 goto unspec_nomem;
164 int nused = n + npad;
165 query2 = buf + nused;
166 n = res_nmkquery(statp, QUERY, name, class, T_AAAA, NULL, 0,
167 NULL, query2, bufsize - nused);
168 if (n > 0
169 && (oflags & RES_F_EDNS0ERR) == 0
170 && (statp->options & RES_USE_EDNS0) != 0)
171 n = __res_nopt(statp, n, query2, bufsize - nused - n,
172 anslen / 2);
173 nquery2 = n;
176 unspec_nomem:;
178 else
180 n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
181 query1, bufsize);
183 if (n > 0
184 && (oflags & RES_F_EDNS0ERR) == 0
185 && (statp->options & RES_USE_EDNS0) != 0)
186 n = __res_nopt(statp, n, query1, bufsize, anslen);
188 nquery1 = n;
191 if (__builtin_expect (n <= 0, 0) && !use_malloc) {
192 /* Retry just in case res_nmkquery failed because of too
193 short buffer. Shouldn't happen. */
194 bufsize = (type == T_UNSPEC ? 2 : 1) * MAXPACKET;
195 buf = malloc (bufsize);
196 if (buf != NULL) {
197 query1 = buf;
198 use_malloc = 1;
199 goto again;
202 if (__builtin_expect (n <= 0, 0)) {
203 /* If the query choked with EDNS0, retry without EDNS0. */
204 if ((statp->options & RES_USE_EDNS0) != 0
205 && ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
206 statp->_flags |= RES_F_EDNS0ERR;
207 #ifdef DEBUG
208 if (statp->options & RES_DEBUG)
209 printf(";; res_nquery: retry without EDNS0\n");
210 #endif
211 goto again;
213 #ifdef DEBUG
214 if (statp->options & RES_DEBUG)
215 printf(";; res_query: mkquery failed\n");
216 #endif
217 RES_SET_H_ERRNO(statp, NO_RECOVERY);
218 if (use_malloc)
219 free (buf);
220 return (n);
222 assert (answerp == NULL || (void *) *answerp == (void *) answer);
223 n = __libc_res_nsend(statp, query1, nquery1, query2, nquery2, answer,
224 anslen, answerp, answerp2, nanswerp2);
225 if (use_malloc)
226 free (buf);
227 if (n < 0) {
228 #ifdef DEBUG
229 if (statp->options & RES_DEBUG)
230 printf(";; res_query: send error\n");
231 #endif
232 RES_SET_H_ERRNO(statp, TRY_AGAIN);
233 return (n);
236 if (answerp != NULL)
237 /* __libc_res_nsend might have reallocated the buffer. */
238 hp = (HEADER *) *answerp;
240 /* We simplify the following tests by assigning HP to HP2. It
241 is easy to verify that this is the same as ignoring all
242 tests of HP2. */
243 HEADER *hp2 = answerp2 ? (HEADER *) *answerp2 : hp;
245 if (n < sizeof (HEADER) && nanswerp2 != NULL
246 && *nanswerp2 > sizeof (HEADER))
248 /* Special case of partial answer. */
249 assert (hp != hp2);
250 hp = hp2;
252 else if (nanswerp2 != NULL
253 && *nanswerp2 < sizeof (HEADER) && n > sizeof (HEADER))
255 /* Special case of partial answer. */
256 assert (hp != hp2);
257 hp2 = hp;
260 if ((hp->rcode != NOERROR || ntohs(hp->ancount) == 0)
261 && (hp2->rcode != NOERROR || ntohs(hp2->ancount) == 0)) {
262 #ifdef DEBUG
263 if (statp->options & RES_DEBUG) {
264 printf(";; rcode = %d, ancount=%d\n", hp->rcode,
265 ntohs(hp->ancount));
266 if (hp != hp2)
267 printf(";; rcode2 = %d, ancount2=%d\n", hp2->rcode,
268 ntohs(hp2->ancount));
270 #endif
271 switch (hp->rcode == NOERROR ? hp2->rcode : hp->rcode) {
272 case NXDOMAIN:
273 if ((hp->rcode == NOERROR && ntohs (hp->ancount) != 0)
274 || (hp2->rcode == NOERROR
275 && ntohs (hp2->ancount) != 0))
276 goto success;
277 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
278 break;
279 case SERVFAIL:
280 RES_SET_H_ERRNO(statp, TRY_AGAIN);
281 break;
282 case NOERROR:
283 if (ntohs (hp->ancount) != 0
284 || ntohs (hp2->ancount) != 0)
285 goto success;
286 RES_SET_H_ERRNO(statp, NO_DATA);
287 break;
288 case FORMERR:
289 case NOTIMP:
290 case REFUSED:
291 default:
292 RES_SET_H_ERRNO(statp, NO_RECOVERY);
293 break;
295 return (-1);
297 success:
298 return (n);
300 libresolv_hidden_def (__libc_res_nquery)
303 res_nquery(res_state statp,
304 const char *name, /* domain name */
305 int class, int type, /* class and type of query */
306 u_char *answer, /* buffer to put answer */
307 int anslen) /* size of answer buffer */
309 return __libc_res_nquery(statp, name, class, type, answer, anslen,
310 NULL, NULL, NULL);
312 libresolv_hidden_def (res_nquery)
315 * Formulate a normal query, send, and retrieve answer in supplied buffer.
316 * Return the size of the response on success, -1 on error.
317 * If enabled, implement search rules until answer or unrecoverable failure
318 * is detected. Error code, if any, is left in H_ERRNO.
321 __libc_res_nsearch(res_state statp,
322 const char *name, /* domain name */
323 int class, int type, /* class and type of query */
324 u_char *answer, /* buffer to put answer */
325 int anslen, /* size of answer */
326 u_char **answerp,
327 u_char **answerp2,
328 int *nanswerp2)
330 const char *cp, * const *domain;
331 HEADER *hp = (HEADER *) answer;
332 char tmp[NS_MAXDNAME];
333 u_int dots;
334 int trailing_dot, ret, saved_herrno;
335 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
336 int tried_as_is = 0;
338 __set_errno (0);
339 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */
341 dots = 0;
342 for (cp = name; *cp != '\0'; cp++)
343 dots += (*cp == '.');
344 trailing_dot = 0;
345 if (cp > name && *--cp == '.')
346 trailing_dot++;
348 /* If there aren't any dots, it could be a user-level alias. */
349 if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
350 return (__libc_res_nquery(statp, cp, class, type, answer,
351 anslen, answerp, answerp2,
352 nanswerp2));
354 #ifdef DEBUG
355 if (statp->options & RES_DEBUG)
356 printf("dots=%d, statp->ndots=%d, trailing_dot=%d, name=%s\n",
357 (int)dots,(int)statp->ndots,(int)trailing_dot,name);
358 #endif
361 * If there are enough dots in the name, let's just give it a
362 * try 'as is'. The threshold can be set with the "ndots" option.
363 * Also, query 'as is', if there is a trailing dot in the name.
365 saved_herrno = -1;
366 if (dots >= statp->ndots || trailing_dot) {
367 ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
368 answer, anslen, answerp,
369 answerp2, nanswerp2);
370 if (ret > 0 || trailing_dot)
371 return (ret);
372 saved_herrno = h_errno;
373 tried_as_is++;
374 if (answerp && *answerp != answer) {
375 answer = *answerp;
376 anslen = MAXPACKET;
378 if (answerp2
379 && (*answerp2 < answer || *answerp2 >= answer + anslen))
381 free (*answerp2);
382 *answerp2 = NULL;
387 * We do at least one level of search if
388 * - there is no dot and RES_DEFNAME is set, or
389 * - there is at least one dot, there is no trailing dot,
390 * and RES_DNSRCH is set.
392 if ((!dots && (statp->options & RES_DEFNAMES) != 0) ||
393 (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0)) {
394 int done = 0;
396 for (domain = (const char * const *)statp->dnsrch;
397 *domain && !done;
398 domain++) {
400 if (domain[0][0] == '\0' ||
401 (domain[0][0] == '.' && domain[0][1] == '\0'))
402 root_on_list++;
404 ret = __libc_res_nquerydomain(statp, name, *domain,
405 class, type,
406 answer, anslen, answerp,
407 answerp2, nanswerp2);
408 if (ret > 0)
409 return (ret);
411 if (answerp && *answerp != answer) {
412 answer = *answerp;
413 anslen = MAXPACKET;
415 if (answerp2
416 && (*answerp2 < answer
417 || *answerp2 >= answer + anslen))
419 free (*answerp2);
420 *answerp2 = NULL;
424 * If no server present, give up.
425 * If name isn't found in this domain,
426 * keep trying higher domains in the search list
427 * (if that's enabled).
428 * On a NO_DATA error, keep trying, otherwise
429 * a wildcard entry of another type could keep us
430 * from finding this entry higher in the domain.
431 * If we get some other error (negative answer or
432 * server failure), then stop searching up,
433 * but try the input name below in case it's
434 * fully-qualified.
436 if (errno == ECONNREFUSED) {
437 RES_SET_H_ERRNO(statp, TRY_AGAIN);
438 return (-1);
441 switch (statp->res_h_errno) {
442 case NO_DATA:
443 got_nodata++;
444 /* FALLTHROUGH */
445 case HOST_NOT_FOUND:
446 /* keep trying */
447 break;
448 case TRY_AGAIN:
449 if (hp->rcode == SERVFAIL) {
450 /* try next search element, if any */
451 got_servfail++;
452 break;
454 /* FALLTHROUGH */
455 default:
456 /* anything else implies that we're done */
457 done++;
460 /* if we got here for some reason other than DNSRCH,
461 * we only wanted one iteration of the loop, so stop.
463 if ((statp->options & RES_DNSRCH) == 0)
464 done++;
469 * If the name has any dots at all, and no earlier 'as-is' query
470 * for the name, and "." is not on the search list, then try an as-is
471 * query now.
473 if (dots && !(tried_as_is || root_on_list)) {
474 ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
475 answer, anslen, answerp,
476 answerp2, nanswerp2);
477 if (ret > 0)
478 return (ret);
481 /* if we got here, we didn't satisfy the search.
482 * if we did an initial full query, return that query's H_ERRNO
483 * (note that we wouldn't be here if that query had succeeded).
484 * else if we ever got a nodata, send that back as the reason.
485 * else send back meaningless H_ERRNO, that being the one from
486 * the last DNSRCH we did.
488 if (answerp2 && (*answerp2 < answer || *answerp2 >= answer + anslen))
490 free (*answerp2);
491 *answerp2 = NULL;
493 if (saved_herrno != -1)
494 RES_SET_H_ERRNO(statp, saved_herrno);
495 else if (got_nodata)
496 RES_SET_H_ERRNO(statp, NO_DATA);
497 else if (got_servfail)
498 RES_SET_H_ERRNO(statp, TRY_AGAIN);
499 return (-1);
501 libresolv_hidden_def (__libc_res_nsearch)
504 res_nsearch(res_state statp,
505 const char *name, /* domain name */
506 int class, int type, /* class and type of query */
507 u_char *answer, /* buffer to put answer */
508 int anslen) /* size of answer */
510 return __libc_res_nsearch(statp, name, class, type, answer,
511 anslen, NULL, NULL, NULL);
513 libresolv_hidden_def (res_nsearch)
516 * Perform a call on res_query on the concatenation of name and domain,
517 * removing a trailing dot from name if domain is NULL.
519 static int
520 __libc_res_nquerydomain(res_state statp,
521 const char *name,
522 const char *domain,
523 int class, int type, /* class and type of query */
524 u_char *answer, /* buffer to put answer */
525 int anslen, /* size of answer */
526 u_char **answerp,
527 u_char **answerp2,
528 int *nanswerp2)
530 char nbuf[MAXDNAME];
531 const char *longname = nbuf;
532 int n, d;
534 #ifdef DEBUG
535 if (statp->options & RES_DEBUG)
536 printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
537 name, domain?domain:"<Nil>", class, type);
538 #endif
539 if (domain == NULL) {
541 * Check for trailing '.';
542 * copy without '.' if present.
544 n = strlen(name);
545 if (n >= MAXDNAME) {
546 RES_SET_H_ERRNO(statp, NO_RECOVERY);
547 return (-1);
549 n--;
550 if (n >= 0 && name[n] == '.') {
551 strncpy(nbuf, name, n);
552 nbuf[n] = '\0';
553 } else
554 longname = name;
555 } else {
556 n = strlen(name);
557 d = strlen(domain);
558 if (n + d + 1 >= MAXDNAME) {
559 RES_SET_H_ERRNO(statp, NO_RECOVERY);
560 return (-1);
562 sprintf(nbuf, "%s.%s", name, domain);
564 return (__libc_res_nquery(statp, longname, class, type, answer,
565 anslen, answerp, answerp2, nanswerp2));
569 res_nquerydomain(res_state statp,
570 const char *name,
571 const char *domain,
572 int class, int type, /* class and type of query */
573 u_char *answer, /* buffer to put answer */
574 int anslen) /* size of answer */
576 return __libc_res_nquerydomain(statp, name, domain, class, type,
577 answer, anslen, NULL, NULL, NULL);
579 libresolv_hidden_def (res_nquerydomain)
581 const char *
582 res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
583 char *file, *cp1, *cp2;
584 char buf[BUFSIZ];
585 FILE *fp;
587 if (statp->options & RES_NOALIASES)
588 return (NULL);
589 file = getenv("HOSTALIASES");
590 if (file == NULL || (fp = fopen(file, "r")) == NULL)
591 return (NULL);
592 setbuf(fp, NULL);
593 buf[sizeof(buf) - 1] = '\0';
594 while (fgets(buf, sizeof(buf), fp)) {
595 for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)
597 if (!*cp1)
598 break;
599 *cp1 = '\0';
600 if (ns_samename(buf, name) == 1) {
601 while (isspace(*++cp1))
603 if (!*cp1)
604 break;
605 for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)
607 *cp2 = '\0';
608 strncpy(dst, cp1, siz - 1);
609 dst[siz - 1] = '\0';
610 fclose(fp);
611 return (dst);
614 fclose(fp);
615 return (NULL);
617 libresolv_hidden_def (res_hostalias)