Add nss-crypt entry. Rename pic_default to libc_cv_pic_default.
[glibc.git] / resolv / res_query.c
blob6ac33ce64cc00072610d25b9ea86c91e0aaece15
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,
101 int *resplen2);
104 * Formulate a normal query, send, and await answer.
105 * Returned answer is placed in supplied buffer "answer".
106 * Perform preliminary check of answer, returning success only
107 * if no error is indicated and the answer count is nonzero.
108 * Return the size of the response on success, -1 on error.
109 * Error number is left in H_ERRNO.
111 * Caller must parse answer and determine whether it answers the question.
114 __libc_res_nquery(res_state statp,
115 const char *name, /* domain name */
116 int class, int type, /* class and type of query */
117 u_char *answer, /* buffer to put answer */
118 int anslen, /* size of answer buffer */
119 u_char **answerp, /* if buffer needs to be enlarged */
120 u_char **answerp2,
121 int *nanswerp2,
122 int *resplen2)
124 HEADER *hp = (HEADER *) answer;
125 int n, use_malloc = 0;
126 u_int oflags = statp->_flags;
128 size_t bufsize = (type == T_UNSPEC ? 2 : 1) * QUERYSIZE;
129 u_char *buf = alloca (bufsize);
130 u_char *query1 = buf;
131 int nquery1 = -1;
132 u_char *query2 = NULL;
133 int nquery2 = 0;
135 again:
136 hp->rcode = NOERROR; /* default */
138 #ifdef DEBUG
139 if (statp->options & RES_DEBUG)
140 printf(";; res_query(%s, %d, %d)\n", name, class, type);
141 #endif
143 if (type == T_UNSPEC)
145 n = res_nmkquery(statp, QUERY, name, class, T_A, NULL, 0, NULL,
146 query1, bufsize);
147 if (n > 0)
149 if ((oflags & RES_F_EDNS0ERR) == 0
150 && (statp->options & RES_USE_EDNS0) != 0)
152 n = __res_nopt(statp, n, query1, bufsize, anslen / 2);
153 if (n < 0)
154 goto unspec_nomem;
157 nquery1 = n;
158 /* Align the buffer. */
159 int npad = ((nquery1 + __alignof__ (HEADER) - 1)
160 & ~(__alignof__ (HEADER) - 1)) - nquery1;
161 if (n > bufsize - npad)
163 n = -1;
164 goto unspec_nomem;
166 int nused = n + npad;
167 query2 = buf + nused;
168 n = res_nmkquery(statp, QUERY, name, class, T_AAAA, NULL, 0,
169 NULL, query2, bufsize - nused);
170 if (n > 0
171 && (oflags & RES_F_EDNS0ERR) == 0
172 && (statp->options & RES_USE_EDNS0) != 0)
173 n = __res_nopt(statp, n, query2, bufsize - nused - n,
174 anslen / 2);
175 nquery2 = n;
178 unspec_nomem:;
180 else
182 n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
183 query1, bufsize);
185 if (n > 0
186 && (oflags & RES_F_EDNS0ERR) == 0
187 && (statp->options & RES_USE_EDNS0) != 0)
188 n = __res_nopt(statp, n, query1, bufsize, anslen);
190 nquery1 = n;
193 if (__builtin_expect (n <= 0, 0) && !use_malloc) {
194 /* Retry just in case res_nmkquery failed because of too
195 short buffer. Shouldn't happen. */
196 bufsize = (type == T_UNSPEC ? 2 : 1) * MAXPACKET;
197 buf = malloc (bufsize);
198 if (buf != NULL) {
199 query1 = buf;
200 use_malloc = 1;
201 goto again;
204 if (__builtin_expect (n <= 0, 0)) {
205 /* If the query choked with EDNS0, retry without EDNS0. */
206 if ((statp->options & RES_USE_EDNS0) != 0
207 && ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
208 statp->_flags |= RES_F_EDNS0ERR;
209 #ifdef DEBUG
210 if (statp->options & RES_DEBUG)
211 printf(";; res_nquery: retry without EDNS0\n");
212 #endif
213 goto again;
215 #ifdef DEBUG
216 if (statp->options & RES_DEBUG)
217 printf(";; res_query: mkquery failed\n");
218 #endif
219 RES_SET_H_ERRNO(statp, NO_RECOVERY);
220 if (use_malloc)
221 free (buf);
222 return (n);
224 assert (answerp == NULL || (void *) *answerp == (void *) answer);
225 n = __libc_res_nsend(statp, query1, nquery1, query2, nquery2, answer,
226 anslen, answerp, answerp2, nanswerp2, resplen2);
227 if (use_malloc)
228 free (buf);
229 if (n < 0) {
230 #ifdef DEBUG
231 if (statp->options & RES_DEBUG)
232 printf(";; res_query: send error\n");
233 #endif
234 RES_SET_H_ERRNO(statp, TRY_AGAIN);
235 return (n);
238 if (answerp != NULL)
239 /* __libc_res_nsend might have reallocated the buffer. */
240 hp = (HEADER *) *answerp;
242 /* We simplify the following tests by assigning HP to HP2. It
243 is easy to verify that this is the same as ignoring all
244 tests of HP2. */
245 HEADER *hp2 = answerp2 ? (HEADER *) *answerp2 : hp;
247 if (n < (int) sizeof (HEADER) && answerp2 != NULL
248 && *resplen2 > (int) sizeof (HEADER))
250 /* Special case of partial answer. */
251 assert (hp != hp2);
252 hp = hp2;
254 else if (answerp2 != NULL && *resplen2 < (int) sizeof (HEADER)
255 && n > (int) sizeof (HEADER))
257 /* Special case of partial answer. */
258 assert (hp != hp2);
259 hp2 = hp;
262 if ((hp->rcode != NOERROR || ntohs(hp->ancount) == 0)
263 && (hp2->rcode != NOERROR || ntohs(hp2->ancount) == 0)) {
264 #ifdef DEBUG
265 if (statp->options & RES_DEBUG) {
266 printf(";; rcode = %d, ancount=%d\n", hp->rcode,
267 ntohs(hp->ancount));
268 if (hp != hp2)
269 printf(";; rcode2 = %d, ancount2=%d\n", hp2->rcode,
270 ntohs(hp2->ancount));
272 #endif
273 switch (hp->rcode == NOERROR ? hp2->rcode : hp->rcode) {
274 case NXDOMAIN:
275 if ((hp->rcode == NOERROR && ntohs (hp->ancount) != 0)
276 || (hp2->rcode == NOERROR
277 && ntohs (hp2->ancount) != 0))
278 goto success;
279 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
280 break;
281 case SERVFAIL:
282 RES_SET_H_ERRNO(statp, TRY_AGAIN);
283 break;
284 case NOERROR:
285 if (ntohs (hp->ancount) != 0
286 || ntohs (hp2->ancount) != 0)
287 goto success;
288 RES_SET_H_ERRNO(statp, NO_DATA);
289 break;
290 case FORMERR:
291 case NOTIMP:
292 case REFUSED:
293 default:
294 RES_SET_H_ERRNO(statp, NO_RECOVERY);
295 break;
297 return (-1);
299 success:
300 return (n);
302 libresolv_hidden_def (__libc_res_nquery)
305 res_nquery(res_state statp,
306 const char *name, /* domain name */
307 int class, int type, /* class and type of query */
308 u_char *answer, /* buffer to put answer */
309 int anslen) /* size of answer buffer */
311 return __libc_res_nquery(statp, name, class, type, answer, anslen,
312 NULL, NULL, NULL, NULL);
314 libresolv_hidden_def (res_nquery)
317 * Formulate a normal query, send, and retrieve answer in supplied buffer.
318 * Return the size of the response on success, -1 on error.
319 * If enabled, implement search rules until answer or unrecoverable failure
320 * is detected. Error code, if any, is left in H_ERRNO.
323 __libc_res_nsearch(res_state statp,
324 const char *name, /* domain name */
325 int class, int type, /* class and type of query */
326 u_char *answer, /* buffer to put answer */
327 int anslen, /* size of answer */
328 u_char **answerp,
329 u_char **answerp2,
330 int *nanswerp2,
331 int *resplen2)
333 const char *cp, * const *domain;
334 HEADER *hp = (HEADER *) answer;
335 char tmp[NS_MAXDNAME];
336 u_int dots;
337 int trailing_dot, ret, saved_herrno;
338 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
339 int tried_as_is = 0;
341 __set_errno (0);
342 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */
344 dots = 0;
345 for (cp = name; *cp != '\0'; cp++)
346 dots += (*cp == '.');
347 trailing_dot = 0;
348 if (cp > name && *--cp == '.')
349 trailing_dot++;
351 /* If there aren't any dots, it could be a user-level alias. */
352 if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
353 return (__libc_res_nquery(statp, cp, class, type, answer,
354 anslen, answerp, answerp2,
355 nanswerp2, resplen2));
357 #ifdef DEBUG
358 if (statp->options & RES_DEBUG)
359 printf("dots=%d, statp->ndots=%d, trailing_dot=%d, name=%s\n",
360 (int)dots,(int)statp->ndots,(int)trailing_dot,name);
361 #endif
364 * If there are enough dots in the name, let's just give it a
365 * try 'as is'. The threshold can be set with the "ndots" option.
366 * Also, query 'as is', if there is a trailing dot in the name.
368 saved_herrno = -1;
369 if (dots >= statp->ndots || trailing_dot) {
370 ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
371 answer, anslen, answerp,
372 answerp2, nanswerp2, resplen2);
373 if (ret > 0 || trailing_dot)
374 return (ret);
375 saved_herrno = h_errno;
376 tried_as_is++;
377 if (answerp && *answerp != answer) {
378 answer = *answerp;
379 anslen = MAXPACKET;
381 if (answerp2
382 && (*answerp2 < answer || *answerp2 >= answer + anslen))
384 free (*answerp2);
385 *answerp2 = NULL;
390 * We do at least one level of search if
391 * - there is no dot and RES_DEFNAME is set, or
392 * - there is at least one dot, there is no trailing dot,
393 * and RES_DNSRCH is set.
395 if ((!dots && (statp->options & RES_DEFNAMES) != 0) ||
396 (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0)) {
397 int done = 0;
399 for (domain = (const char * const *)statp->dnsrch;
400 *domain && !done;
401 domain++) {
403 if (domain[0][0] == '\0' ||
404 (domain[0][0] == '.' && domain[0][1] == '\0'))
405 root_on_list++;
407 ret = __libc_res_nquerydomain(statp, name, *domain,
408 class, type,
409 answer, anslen, answerp,
410 answerp2, nanswerp2,
411 resplen2);
412 if (ret > 0)
413 return (ret);
415 if (answerp && *answerp != answer) {
416 answer = *answerp;
417 anslen = MAXPACKET;
419 if (answerp2
420 && (*answerp2 < answer
421 || *answerp2 >= answer + anslen))
423 free (*answerp2);
424 *answerp2 = NULL;
428 * If no server present, give up.
429 * If name isn't found in this domain,
430 * keep trying higher domains in the search list
431 * (if that's enabled).
432 * On a NO_DATA error, keep trying, otherwise
433 * a wildcard entry of another type could keep us
434 * from finding this entry higher in the domain.
435 * If we get some other error (negative answer or
436 * server failure), then stop searching up,
437 * but try the input name below in case it's
438 * fully-qualified.
440 if (errno == ECONNREFUSED) {
441 RES_SET_H_ERRNO(statp, TRY_AGAIN);
442 return (-1);
445 switch (statp->res_h_errno) {
446 case NO_DATA:
447 got_nodata++;
448 /* FALLTHROUGH */
449 case HOST_NOT_FOUND:
450 /* keep trying */
451 break;
452 case TRY_AGAIN:
453 if (hp->rcode == SERVFAIL) {
454 /* try next search element, if any */
455 got_servfail++;
456 break;
458 /* FALLTHROUGH */
459 default:
460 /* anything else implies that we're done */
461 done++;
464 /* if we got here for some reason other than DNSRCH,
465 * we only wanted one iteration of the loop, so stop.
467 if ((statp->options & RES_DNSRCH) == 0)
468 done++;
473 * If the name has any dots at all, and no earlier 'as-is' query
474 * for the name, and "." is not on the search list, then try an as-is
475 * query now.
477 if (dots && !(tried_as_is || root_on_list)) {
478 ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
479 answer, anslen, answerp,
480 answerp2, nanswerp2, resplen2);
481 if (ret > 0)
482 return (ret);
485 /* if we got here, we didn't satisfy the search.
486 * if we did an initial full query, return that query's H_ERRNO
487 * (note that we wouldn't be here if that query had succeeded).
488 * else if we ever got a nodata, send that back as the reason.
489 * else send back meaningless H_ERRNO, that being the one from
490 * the last DNSRCH we did.
492 if (answerp2 && (*answerp2 < answer || *answerp2 >= answer + anslen))
494 free (*answerp2);
495 *answerp2 = NULL;
497 if (saved_herrno != -1)
498 RES_SET_H_ERRNO(statp, saved_herrno);
499 else if (got_nodata)
500 RES_SET_H_ERRNO(statp, NO_DATA);
501 else if (got_servfail)
502 RES_SET_H_ERRNO(statp, TRY_AGAIN);
503 return (-1);
505 libresolv_hidden_def (__libc_res_nsearch)
508 res_nsearch(res_state statp,
509 const char *name, /* domain name */
510 int class, int type, /* class and type of query */
511 u_char *answer, /* buffer to put answer */
512 int anslen) /* size of answer */
514 return __libc_res_nsearch(statp, name, class, type, answer,
515 anslen, NULL, NULL, NULL, NULL);
517 libresolv_hidden_def (res_nsearch)
520 * Perform a call on res_query on the concatenation of name and domain,
521 * removing a trailing dot from name if domain is NULL.
523 static int
524 __libc_res_nquerydomain(res_state statp,
525 const char *name,
526 const char *domain,
527 int class, int type, /* class and type of query */
528 u_char *answer, /* buffer to put answer */
529 int anslen, /* size of answer */
530 u_char **answerp,
531 u_char **answerp2,
532 int *nanswerp2,
533 int *resplen2)
535 char nbuf[MAXDNAME];
536 const char *longname = nbuf;
537 int n, d;
539 #ifdef DEBUG
540 if (statp->options & RES_DEBUG)
541 printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
542 name, domain?domain:"<Nil>", class, type);
543 #endif
544 if (domain == NULL) {
546 * Check for trailing '.';
547 * copy without '.' if present.
549 n = strlen(name);
550 if (n >= MAXDNAME) {
551 RES_SET_H_ERRNO(statp, NO_RECOVERY);
552 return (-1);
554 n--;
555 if (n >= 0 && name[n] == '.') {
556 strncpy(nbuf, name, n);
557 nbuf[n] = '\0';
558 } else
559 longname = name;
560 } else {
561 n = strlen(name);
562 d = strlen(domain);
563 if (n + d + 1 >= MAXDNAME) {
564 RES_SET_H_ERRNO(statp, NO_RECOVERY);
565 return (-1);
567 sprintf(nbuf, "%s.%s", name, domain);
569 return (__libc_res_nquery(statp, longname, class, type, answer,
570 anslen, answerp, answerp2, nanswerp2,
571 resplen2));
575 res_nquerydomain(res_state statp,
576 const char *name,
577 const char *domain,
578 int class, int type, /* class and type of query */
579 u_char *answer, /* buffer to put answer */
580 int anslen) /* size of answer */
582 return __libc_res_nquerydomain(statp, name, domain, class, type,
583 answer, anslen, NULL, NULL, NULL, NULL);
585 libresolv_hidden_def (res_nquerydomain)
587 const char *
588 res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
589 char *file, *cp1, *cp2;
590 char buf[BUFSIZ];
591 FILE *fp;
593 if (statp->options & RES_NOALIASES)
594 return (NULL);
595 file = getenv("HOSTALIASES");
596 if (file == NULL || (fp = fopen(file, "r")) == NULL)
597 return (NULL);
598 setbuf(fp, NULL);
599 buf[sizeof(buf) - 1] = '\0';
600 while (fgets(buf, sizeof(buf), fp)) {
601 for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)
603 if (!*cp1)
604 break;
605 *cp1 = '\0';
606 if (ns_samename(buf, name) == 1) {
607 while (isspace(*++cp1))
609 if (!*cp1)
610 break;
611 for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)
613 *cp2 = '\0';
614 strncpy(dst, cp1, siz - 1);
615 dst[siz - 1] = '\0';
616 fclose(fp);
617 return (dst);
620 fclose(fp);
621 return (NULL);
623 libresolv_hidden_def (res_hostalias)