If --verify loop fails to find a dynamic linker for the file don't
[glibc/history.git] / resolv / res_query.c
blob0feba6687a259542e8f31e805a196e98c530a772
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 <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>
85 /* Options. Leave them on. */
86 /* #undef DEBUG */
88 #if PACKETSZ > 65536
89 #define MAXPACKET PACKETSZ
90 #else
91 #define MAXPACKET 65536
92 #endif
94 #define QUERYSIZE (HFIXEDSZ + QFIXEDSZ + MAXCDNAME + 1)
96 static int
97 __libc_res_nquerydomain(res_state statp, const char *name, const char *domain,
98 int class, int type, u_char *answer, int anslen,
99 u_char **answerp);
102 * Formulate a normal query, send, and await answer.
103 * Returned answer is placed in supplied buffer "answer".
104 * Perform preliminary check of answer, returning success only
105 * if no error is indicated and the answer count is nonzero.
106 * Return the size of the response on success, -1 on error.
107 * Error number is left in H_ERRNO.
109 * Caller must parse answer and determine whether it answers the question.
112 __libc_res_nquery(res_state statp,
113 const char *name, /* domain name */
114 int class, int type, /* class and type of query */
115 u_char *answer, /* buffer to put answer */
116 int anslen, /* size of answer buffer */
117 u_char **answerp) /* if buffer needs to be enlarged */
119 u_char *buf;
120 HEADER *hp = (HEADER *) answer;
121 int n, use_malloc = 0;
123 hp->rcode = NOERROR; /* default */
125 buf = alloca (QUERYSIZE);
127 #ifdef DEBUG
128 if (statp->options & RES_DEBUG)
129 printf(";; res_query(%s, %d, %d)\n", name, class, type);
130 #endif
132 n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
133 buf, QUERYSIZE);
134 if (__builtin_expect (n <= 0, 0)) {
135 /* Retry just in case res_nmkquery failed because of too
136 short buffer. Shouldn't happen. */
137 buf = malloc (MAXPACKET);
138 if (buf != NULL) {
139 use_malloc = 1;
140 n = res_nmkquery(statp, QUERY, name, class, type, NULL,
141 0, NULL, buf, MAXPACKET);
144 if (__builtin_expect (n <= 0, 0)) {
145 #ifdef DEBUG
146 if (statp->options & RES_DEBUG)
147 printf(";; res_query: mkquery failed\n");
148 #endif
149 RES_SET_H_ERRNO(statp, NO_RECOVERY);
150 if (use_malloc)
151 free (buf);
152 return (n);
154 n = __libc_res_nsend(statp, buf, n, answer, anslen, answerp);
155 if (use_malloc)
156 free (buf);
157 if (n < 0) {
158 #ifdef DEBUG
159 if (statp->options & RES_DEBUG)
160 printf(";; res_query: send error\n");
161 #endif
162 RES_SET_H_ERRNO(statp, TRY_AGAIN);
163 return (n);
166 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
167 #ifdef DEBUG
168 if (statp->options & RES_DEBUG)
169 printf(";; rcode = %d, ancount=%d\n", hp->rcode,
170 ntohs(hp->ancount));
171 #endif
172 switch (hp->rcode) {
173 case NXDOMAIN:
174 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
175 break;
176 case SERVFAIL:
177 RES_SET_H_ERRNO(statp, TRY_AGAIN);
178 break;
179 case NOERROR:
180 RES_SET_H_ERRNO(statp, NO_DATA);
181 break;
182 case FORMERR:
183 case NOTIMP:
184 case REFUSED:
185 default:
186 RES_SET_H_ERRNO(statp, NO_RECOVERY);
187 break;
189 return (-1);
191 return (n);
193 libresolv_hidden_def (__libc_res_nquery)
196 res_nquery(res_state statp,
197 const char *name, /* domain name */
198 int class, int type, /* class and type of query */
199 u_char *answer, /* buffer to put answer */
200 int anslen) /* size of answer buffer */
202 return __libc_res_nquery(statp, name, class, type, answer, anslen,
203 NULL);
205 libresolv_hidden_def (res_nquery)
208 * Formulate a normal query, send, and retrieve answer in supplied buffer.
209 * Return the size of the response on success, -1 on error.
210 * If enabled, implement search rules until answer or unrecoverable failure
211 * is detected. Error code, if any, is left in H_ERRNO.
214 __libc_res_nsearch(res_state statp,
215 const char *name, /* domain name */
216 int class, int type, /* class and type of query */
217 u_char *answer, /* buffer to put answer */
218 int anslen, /* size of answer */
219 u_char **answerp)
221 const char *cp, * const *domain;
222 HEADER *hp = (HEADER *) answer;
223 char tmp[NS_MAXDNAME];
224 u_int dots;
225 int trailing_dot, ret, saved_herrno;
226 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
227 int tried_as_is = 0;
229 __set_errno (0);
230 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */
232 dots = 0;
233 for (cp = name; *cp != '\0'; cp++)
234 dots += (*cp == '.');
235 trailing_dot = 0;
236 if (cp > name && *--cp == '.')
237 trailing_dot++;
239 /* If there aren't any dots, it could be a user-level alias. */
240 if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
241 return (__libc_res_nquery(statp, cp, class, type, answer,
242 anslen, answerp));
244 #ifdef DEBUG
245 if (statp->options & RES_DEBUG)
246 printf("dots=%d, statp->ndots=%d, trailing_dot=%d, name=%s\n",
247 (int)dots,(int)statp->ndots,(int)trailing_dot,name);
248 #endif
251 * If there are enough dots in the name, let's just give it a
252 * try 'as is'. The threshold can be set with the "ndots" option.
253 * Also, query 'as is', if there is a trailing dot in the name.
255 saved_herrno = -1;
256 if (dots >= statp->ndots || trailing_dot) {
257 ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
258 answer, anslen, answerp);
259 if (ret > 0 || trailing_dot)
260 return (ret);
261 saved_herrno = h_errno;
262 tried_as_is++;
263 if (answerp && *answerp != answer) {
264 answer = *answerp;
265 anslen = MAXPACKET;
270 * We do at least one level of search if
271 * - there is no dot and RES_DEFNAME is set, or
272 * - there is at least one dot, there is no trailing dot,
273 * and RES_DNSRCH is set.
275 if ((!dots && (statp->options & RES_DEFNAMES) != 0) ||
276 (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0)) {
277 int done = 0;
279 for (domain = (const char * const *)statp->dnsrch;
280 *domain && !done;
281 domain++) {
283 if (domain[0][0] == '\0' ||
284 (domain[0][0] == '.' && domain[0][1] == '\0'))
285 root_on_list++;
287 ret = __libc_res_nquerydomain(statp, name, *domain,
288 class, type,
289 answer, anslen, answerp);
290 if (ret > 0)
291 return (ret);
293 if (answerp && *answerp != answer) {
294 answer = *answerp;
295 anslen = MAXPACKET;
299 * If no server present, give up.
300 * If name isn't found in this domain,
301 * keep trying higher domains in the search list
302 * (if that's enabled).
303 * On a NO_DATA error, keep trying, otherwise
304 * a wildcard entry of another type could keep us
305 * from finding this entry higher in the domain.
306 * If we get some other error (negative answer or
307 * server failure), then stop searching up,
308 * but try the input name below in case it's
309 * fully-qualified.
311 if (errno == ECONNREFUSED) {
312 RES_SET_H_ERRNO(statp, TRY_AGAIN);
313 return (-1);
316 switch (statp->res_h_errno) {
317 case NO_DATA:
318 got_nodata++;
319 /* FALLTHROUGH */
320 case HOST_NOT_FOUND:
321 /* keep trying */
322 break;
323 case TRY_AGAIN:
324 if (hp->rcode == SERVFAIL) {
325 /* try next search element, if any */
326 got_servfail++;
327 break;
329 /* FALLTHROUGH */
330 default:
331 /* anything else implies that we're done */
332 done++;
335 /* if we got here for some reason other than DNSRCH,
336 * we only wanted one iteration of the loop, so stop.
338 if ((statp->options & RES_DNSRCH) == 0)
339 done++;
344 * If the name has any dots at all, and no earlier 'as-is' query
345 * for the name, and "." is not on the search list, then try an as-is
346 * query now.
348 if (dots && !(tried_as_is || root_on_list)) {
349 ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
350 answer, anslen, answerp);
351 if (ret > 0)
352 return (ret);
355 /* if we got here, we didn't satisfy the search.
356 * if we did an initial full query, return that query's H_ERRNO
357 * (note that we wouldn't be here if that query had succeeded).
358 * else if we ever got a nodata, send that back as the reason.
359 * else send back meaningless H_ERRNO, that being the one from
360 * the last DNSRCH we did.
362 if (saved_herrno != -1)
363 RES_SET_H_ERRNO(statp, saved_herrno);
364 else if (got_nodata)
365 RES_SET_H_ERRNO(statp, NO_DATA);
366 else if (got_servfail)
367 RES_SET_H_ERRNO(statp, TRY_AGAIN);
368 return (-1);
370 libresolv_hidden_def (__libc_res_nsearch)
373 res_nsearch(res_state statp,
374 const char *name, /* domain name */
375 int class, int type, /* class and type of query */
376 u_char *answer, /* buffer to put answer */
377 int anslen) /* size of answer */
379 return __libc_res_nsearch(statp, name, class, type, answer,
380 anslen, NULL);
382 libresolv_hidden_def (res_nsearch)
385 * Perform a call on res_query on the concatenation of name and domain,
386 * removing a trailing dot from name if domain is NULL.
388 static int
389 __libc_res_nquerydomain(res_state statp,
390 const char *name,
391 const char *domain,
392 int class, int type, /* class and type of query */
393 u_char *answer, /* buffer to put answer */
394 int anslen, /* size of answer */
395 u_char **answerp)
397 char nbuf[MAXDNAME];
398 const char *longname = nbuf;
399 int n, d;
401 #ifdef DEBUG
402 if (statp->options & RES_DEBUG)
403 printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
404 name, domain?domain:"<Nil>", class, type);
405 #endif
406 if (domain == NULL) {
408 * Check for trailing '.';
409 * copy without '.' if present.
411 n = strlen(name);
412 if (n >= MAXDNAME) {
413 RES_SET_H_ERRNO(statp, NO_RECOVERY);
414 return (-1);
416 n--;
417 if (n >= 0 && name[n] == '.') {
418 strncpy(nbuf, name, n);
419 nbuf[n] = '\0';
420 } else
421 longname = name;
422 } else {
423 n = strlen(name);
424 d = strlen(domain);
425 if (n + d + 1 >= MAXDNAME) {
426 RES_SET_H_ERRNO(statp, NO_RECOVERY);
427 return (-1);
429 sprintf(nbuf, "%s.%s", name, domain);
431 return (__libc_res_nquery(statp, longname, class, type, answer,
432 anslen, answerp));
436 res_nquerydomain(res_state statp,
437 const char *name,
438 const char *domain,
439 int class, int type, /* class and type of query */
440 u_char *answer, /* buffer to put answer */
441 int anslen) /* size of answer */
443 return __libc_res_nquerydomain(statp, name, domain, class, type,
444 answer, anslen, NULL);
446 libresolv_hidden_def (res_nquerydomain)
448 const char *
449 res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
450 char *file, *cp1, *cp2;
451 char buf[BUFSIZ];
452 FILE *fp;
454 if (statp->options & RES_NOALIASES)
455 return (NULL);
456 file = getenv("HOSTALIASES");
457 if (file == NULL || (fp = fopen(file, "r")) == NULL)
458 return (NULL);
459 setbuf(fp, NULL);
460 buf[sizeof(buf) - 1] = '\0';
461 while (fgets(buf, sizeof(buf), fp)) {
462 for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)
464 if (!*cp1)
465 break;
466 *cp1 = '\0';
467 if (ns_samename(buf, name) == 1) {
468 while (isspace(*++cp1))
470 if (!*cp1)
471 break;
472 for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)
474 *cp2 = '\0';
475 strncpy(dst, cp1, siz - 1);
476 dst[siz - 1] = '\0';
477 fclose(fp);
478 return (dst);
481 fclose(fp);
482 return (NULL);
484 libresolv_hidden_def (res_hostalias)