* resolv/nss_dns/dns-host.c (gaih_getanswer_slice): Also log and
[glibc.git] / resolv / res_query.c
blob3d2f2fe3a996412164e4b1782bf4f73795fc0376
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)
149 n = __res_nopt(statp, n, query1, bufsize, anslen / 2);
151 nquery1 = n;
152 query2 = buf + nquery1;
153 n = res_nmkquery(statp, QUERY, name, class, T_AAAA, NULL, 0,
154 NULL, query2, bufsize - n);
155 if (n > 0
156 && (oflags & RES_F_EDNS0ERR) == 0
157 && (statp->options & RES_USE_EDNS0) != 0)
158 n = __res_nopt(statp, n, query2, bufsize - n, anslen / 2);
159 nquery2 = n;
162 else
164 n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
165 query1, bufsize);
167 if (n > 0
168 && (oflags & RES_F_EDNS0ERR) == 0
169 && (statp->options & RES_USE_EDNS0) != 0)
170 n = __res_nopt(statp, n, query1, bufsize, anslen);
172 nquery1 = n;
175 if (__builtin_expect (n <= 0, 0) && !use_malloc) {
176 /* Retry just in case res_nmkquery failed because of too
177 short buffer. Shouldn't happen. */
178 bufsize = (type == T_UNSPEC ? 2 : 1) * MAXPACKET;
179 buf = malloc (bufsize);
180 if (buf != NULL) {
181 query1 = buf;
182 use_malloc = 1;
183 goto again;
186 if (__builtin_expect (n <= 0, 0)) {
187 /* If the query choked with EDNS0, retry without EDNS0. */
188 if ((statp->options & RES_USE_EDNS0) != 0
189 && ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
190 statp->_flags |= RES_F_EDNS0ERR;
191 if (statp->options & RES_DEBUG)
192 printf(";; res_nquery: retry without EDNS0\n");
193 goto again;
195 #ifdef DEBUG
196 if (statp->options & RES_DEBUG)
197 printf(";; res_query: mkquery failed\n");
198 #endif
199 RES_SET_H_ERRNO(statp, NO_RECOVERY);
200 if (use_malloc)
201 free (buf);
202 return (n);
204 assert (answerp == NULL || (void *) *answerp == (void *) answer);
205 n = __libc_res_nsend(statp, query1, nquery1, query2, nquery2, answer,
206 anslen, answerp, answerp2, nanswerp2);
207 if (use_malloc)
208 free (buf);
209 if (n < 0) {
210 #ifdef DEBUG
211 if (statp->options & RES_DEBUG)
212 printf(";; res_query: send error\n");
213 #endif
214 RES_SET_H_ERRNO(statp, TRY_AGAIN);
215 return (n);
218 if (answerp != NULL)
219 /* __libc_res_nsend might have reallocated the buffer. */
220 hp = (HEADER *) *answerp;
222 /* We simplify the following tests by assigning HP to HP2. It
223 is easy to verify that this is the same as ignoring all
224 tests of HP2. */
225 HEADER *hp2 = answerp2 ? (HEADER *) *answerp2 : hp;
227 if (n < sizeof (HEADER) && nanswerp2 != NULL
228 && *nanswerp2 > sizeof (HEADER))
230 /* Special case of partial answer. */
231 assert (hp != hp2);
232 hp = hp2;
234 else if (nanswerp2 != NULL
235 && *nanswerp2 < sizeof (HEADER) && n > sizeof (HEADER))
237 /* Special case of partial answer. */
238 assert (hp != hp2);
239 hp2 = hp;
242 if ((hp->rcode != NOERROR || ntohs(hp->ancount) == 0)
243 && (hp2->rcode != NOERROR || ntohs(hp2->ancount) == 0)) {
244 #ifdef DEBUG
245 if (statp->options & RES_DEBUG) {
246 printf(";; rcode = %d, ancount=%d\n", hp->rcode,
247 ntohs(hp->ancount));
248 if (hp != hp2)
249 printf(";; rcode2 = %d, ancount2=%d\n", hp2->rcode,
250 ntohs(hp2->ancount));
252 #endif
253 switch (hp->rcode == NOERROR ? hp2->rcode : hp->rcode) {
254 case NXDOMAIN:
255 if ((hp->rcode == NOERROR && ntohs (hp->ancount) != 0)
256 || (hp2->rcode == NOERROR
257 && ntohs (hp2->ancount) != 0))
258 goto success;
259 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
260 break;
261 case SERVFAIL:
262 RES_SET_H_ERRNO(statp, TRY_AGAIN);
263 break;
264 case NOERROR:
265 if (ntohs (hp->ancount) != 0
266 || ntohs (hp2->ancount) != 0)
267 goto success;
268 RES_SET_H_ERRNO(statp, NO_DATA);
269 break;
270 case FORMERR:
271 case NOTIMP:
272 case REFUSED:
273 default:
274 RES_SET_H_ERRNO(statp, NO_RECOVERY);
275 break;
277 return (-1);
279 success:
280 return (n);
282 libresolv_hidden_def (__libc_res_nquery)
285 res_nquery(res_state statp,
286 const char *name, /* domain name */
287 int class, int type, /* class and type of query */
288 u_char *answer, /* buffer to put answer */
289 int anslen) /* size of answer buffer */
291 return __libc_res_nquery(statp, name, class, type, answer, anslen,
292 NULL, NULL, NULL);
294 libresolv_hidden_def (res_nquery)
297 * Formulate a normal query, send, and retrieve answer in supplied buffer.
298 * Return the size of the response on success, -1 on error.
299 * If enabled, implement search rules until answer or unrecoverable failure
300 * is detected. Error code, if any, is left in H_ERRNO.
303 __libc_res_nsearch(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 */
308 u_char **answerp,
309 u_char **answerp2,
310 int *nanswerp2)
312 const char *cp, * const *domain;
313 HEADER *hp = (HEADER *) answer;
314 char tmp[NS_MAXDNAME];
315 u_int dots;
316 int trailing_dot, ret, saved_herrno;
317 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
318 int tried_as_is = 0;
320 __set_errno (0);
321 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */
323 dots = 0;
324 for (cp = name; *cp != '\0'; cp++)
325 dots += (*cp == '.');
326 trailing_dot = 0;
327 if (cp > name && *--cp == '.')
328 trailing_dot++;
330 /* If there aren't any dots, it could be a user-level alias. */
331 if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
332 return (__libc_res_nquery(statp, cp, class, type, answer,
333 anslen, answerp, answerp2,
334 nanswerp2));
336 #ifdef DEBUG
337 if (statp->options & RES_DEBUG)
338 printf("dots=%d, statp->ndots=%d, trailing_dot=%d, name=%s\n",
339 (int)dots,(int)statp->ndots,(int)trailing_dot,name);
340 #endif
343 * If there are enough dots in the name, let's just give it a
344 * try 'as is'. The threshold can be set with the "ndots" option.
345 * Also, query 'as is', if there is a trailing dot in the name.
347 saved_herrno = -1;
348 if (dots >= statp->ndots || trailing_dot) {
349 ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
350 answer, anslen, answerp,
351 answerp2, nanswerp2);
352 if (ret > 0 || trailing_dot)
353 return (ret);
354 saved_herrno = h_errno;
355 tried_as_is++;
356 if (answerp && *answerp != answer) {
357 answer = *answerp;
358 anslen = MAXPACKET;
360 if (answerp2
361 && (*answerp2 < answer || *answerp2 >= answer + anslen))
363 free (*answerp2);
364 *answerp2 = NULL;
369 * We do at least one level of search if
370 * - there is no dot and RES_DEFNAME is set, or
371 * - there is at least one dot, there is no trailing dot,
372 * and RES_DNSRCH is set.
374 if ((!dots && (statp->options & RES_DEFNAMES) != 0) ||
375 (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0)) {
376 int done = 0;
378 for (domain = (const char * const *)statp->dnsrch;
379 *domain && !done;
380 domain++) {
382 if (domain[0][0] == '\0' ||
383 (domain[0][0] == '.' && domain[0][1] == '\0'))
384 root_on_list++;
386 ret = __libc_res_nquerydomain(statp, name, *domain,
387 class, type,
388 answer, anslen, answerp,
389 answerp2, nanswerp2);
390 if (ret > 0)
391 return (ret);
393 if (answerp && *answerp != answer) {
394 answer = *answerp;
395 anslen = MAXPACKET;
397 if (answerp2
398 && (*answerp2 < answer
399 || *answerp2 >= answer + anslen))
401 free (*answerp2);
402 *answerp2 = NULL;
406 * If no server present, give up.
407 * If name isn't found in this domain,
408 * keep trying higher domains in the search list
409 * (if that's enabled).
410 * On a NO_DATA error, keep trying, otherwise
411 * a wildcard entry of another type could keep us
412 * from finding this entry higher in the domain.
413 * If we get some other error (negative answer or
414 * server failure), then stop searching up,
415 * but try the input name below in case it's
416 * fully-qualified.
418 if (errno == ECONNREFUSED) {
419 RES_SET_H_ERRNO(statp, TRY_AGAIN);
420 return (-1);
423 switch (statp->res_h_errno) {
424 case NO_DATA:
425 got_nodata++;
426 /* FALLTHROUGH */
427 case HOST_NOT_FOUND:
428 /* keep trying */
429 break;
430 case TRY_AGAIN:
431 if (hp->rcode == SERVFAIL) {
432 /* try next search element, if any */
433 got_servfail++;
434 break;
436 /* FALLTHROUGH */
437 default:
438 /* anything else implies that we're done */
439 done++;
442 /* if we got here for some reason other than DNSRCH,
443 * we only wanted one iteration of the loop, so stop.
445 if ((statp->options & RES_DNSRCH) == 0)
446 done++;
451 * If the name has any dots at all, and no earlier 'as-is' query
452 * for the name, and "." is not on the search list, then try an as-is
453 * query now.
455 if (dots && !(tried_as_is || root_on_list)) {
456 ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
457 answer, anslen, answerp,
458 answerp2, nanswerp2);
459 if (ret > 0)
460 return (ret);
463 /* if we got here, we didn't satisfy the search.
464 * if we did an initial full query, return that query's H_ERRNO
465 * (note that we wouldn't be here if that query had succeeded).
466 * else if we ever got a nodata, send that back as the reason.
467 * else send back meaningless H_ERRNO, that being the one from
468 * the last DNSRCH we did.
470 if (answerp2 && (*answerp2 < answer || *answerp2 >= answer + anslen))
472 free (*answerp2);
473 *answerp2 = NULL;
475 if (saved_herrno != -1)
476 RES_SET_H_ERRNO(statp, saved_herrno);
477 else if (got_nodata)
478 RES_SET_H_ERRNO(statp, NO_DATA);
479 else if (got_servfail)
480 RES_SET_H_ERRNO(statp, TRY_AGAIN);
481 return (-1);
483 libresolv_hidden_def (__libc_res_nsearch)
486 res_nsearch(res_state statp,
487 const char *name, /* domain name */
488 int class, int type, /* class and type of query */
489 u_char *answer, /* buffer to put answer */
490 int anslen) /* size of answer */
492 return __libc_res_nsearch(statp, name, class, type, answer,
493 anslen, NULL, NULL, NULL);
495 libresolv_hidden_def (res_nsearch)
498 * Perform a call on res_query on the concatenation of name and domain,
499 * removing a trailing dot from name if domain is NULL.
501 static int
502 __libc_res_nquerydomain(res_state statp,
503 const char *name,
504 const char *domain,
505 int class, int type, /* class and type of query */
506 u_char *answer, /* buffer to put answer */
507 int anslen, /* size of answer */
508 u_char **answerp,
509 u_char **answerp2,
510 int *nanswerp2)
512 char nbuf[MAXDNAME];
513 const char *longname = nbuf;
514 int n, d;
516 #ifdef DEBUG
517 if (statp->options & RES_DEBUG)
518 printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
519 name, domain?domain:"<Nil>", class, type);
520 #endif
521 if (domain == NULL) {
523 * Check for trailing '.';
524 * copy without '.' if present.
526 n = strlen(name);
527 if (n >= MAXDNAME) {
528 RES_SET_H_ERRNO(statp, NO_RECOVERY);
529 return (-1);
531 n--;
532 if (n >= 0 && name[n] == '.') {
533 strncpy(nbuf, name, n);
534 nbuf[n] = '\0';
535 } else
536 longname = name;
537 } else {
538 n = strlen(name);
539 d = strlen(domain);
540 if (n + d + 1 >= MAXDNAME) {
541 RES_SET_H_ERRNO(statp, NO_RECOVERY);
542 return (-1);
544 sprintf(nbuf, "%s.%s", name, domain);
546 return (__libc_res_nquery(statp, longname, class, type, answer,
547 anslen, answerp, answerp2, nanswerp2));
551 res_nquerydomain(res_state statp,
552 const char *name,
553 const char *domain,
554 int class, int type, /* class and type of query */
555 u_char *answer, /* buffer to put answer */
556 int anslen) /* size of answer */
558 return __libc_res_nquerydomain(statp, name, domain, class, type,
559 answer, anslen, NULL, NULL, NULL);
561 libresolv_hidden_def (res_nquerydomain)
563 const char *
564 res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
565 char *file, *cp1, *cp2;
566 char buf[BUFSIZ];
567 FILE *fp;
569 if (statp->options & RES_NOALIASES)
570 return (NULL);
571 file = getenv("HOSTALIASES");
572 if (file == NULL || (fp = fopen(file, "r")) == NULL)
573 return (NULL);
574 setbuf(fp, NULL);
575 buf[sizeof(buf) - 1] = '\0';
576 while (fgets(buf, sizeof(buf), fp)) {
577 for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)
579 if (!*cp1)
580 break;
581 *cp1 = '\0';
582 if (ns_samename(buf, name) == 1) {
583 while (isspace(*++cp1))
585 if (!*cp1)
586 break;
587 for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)
589 *cp2 = '\0';
590 strncpy(dst, cp1, siz - 1);
591 dst[siz - 1] = '\0';
592 fclose(fp);
593 return (dst);
596 fclose(fp);
597 return (NULL);
599 libresolv_hidden_def (res_hostalias)