Update.
[glibc.git] / resolv / res_query.c
blob866a2bba551cacad38789edc2ef2b80b9b5d4990
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
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.
102 * Caller must parse answer and determine whether it answers the question.
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 */
111 u_char buf[MAXPACKET];
112 HEADER *hp = (HEADER *) answer;
113 int n;
115 hp->rcode = NOERROR; /* default */
117 #ifdef DEBUG
118 if (statp->options & RES_DEBUG)
119 printf(";; res_query(%s, %d, %d)\n", name, class, type);
120 #endif
122 n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
123 buf, sizeof(buf));
124 if (n <= 0) {
125 #ifdef DEBUG
126 if (statp->options & RES_DEBUG)
127 printf(";; res_query: mkquery failed\n");
128 #endif
129 RES_SET_H_ERRNO(statp, NO_RECOVERY);
130 return (n);
132 n = res_nsend(statp, buf, n, answer, anslen);
133 if (n < 0) {
134 #ifdef DEBUG
135 if (statp->options & RES_DEBUG)
136 printf(";; res_query: send error\n");
137 #endif
138 RES_SET_H_ERRNO(statp, TRY_AGAIN);
139 return (n);
142 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
143 #ifdef DEBUG
144 if (statp->options & RES_DEBUG)
145 printf(";; rcode = %d, ancount=%d\n", hp->rcode,
146 ntohs(hp->ancount));
147 #endif
148 switch (hp->rcode) {
149 case NXDOMAIN:
150 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
151 break;
152 case SERVFAIL:
153 RES_SET_H_ERRNO(statp, TRY_AGAIN);
154 break;
155 case NOERROR:
156 RES_SET_H_ERRNO(statp, NO_DATA);
157 break;
158 case FORMERR:
159 case NOTIMP:
160 case REFUSED:
161 default:
162 RES_SET_H_ERRNO(statp, NO_RECOVERY);
163 break;
165 return (-1);
167 return (n);
171 * Formulate a normal query, send, and retrieve answer in supplied buffer.
172 * Return the size of the response on success, -1 on error.
173 * If enabled, implement search rules until answer or unrecoverable failure
174 * is detected. Error code, if any, is left in H_ERRNO.
177 res_nsearch(res_state statp,
178 const char *name, /* domain name */
179 int class, int type, /* class and type of query */
180 u_char *answer, /* buffer to put answer */
181 int anslen) /* size of answer */
183 const char *cp, * const *domain;
184 HEADER *hp = (HEADER *) answer;
185 char tmp[NS_MAXDNAME];
186 u_int dots;
187 int trailing_dot, ret, saved_herrno;
188 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
189 int tried_as_is = 0;
191 __set_errno (0);
192 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */
194 dots = 0;
195 for (cp = name; *cp != '\0'; cp++)
196 dots += (*cp == '.');
197 trailing_dot = 0;
198 if (cp > name && *--cp == '.')
199 trailing_dot++;
201 /* If there aren't any dots, it could be a user-level alias. */
202 if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
203 return (res_nquery(statp, cp, class, type, answer, anslen));
206 * If there are enough dots in the name, let's just give it a
207 * try 'as is'. The threshold can be set with the "ndots" option.
208 * Also, query 'as is', if there is a trailing dot in the name.
210 saved_herrno = -1;
211 if (dots >= statp->ndots || trailing_dot) {
212 ret = res_nquerydomain(statp, name, NULL, class, type,
213 answer, anslen);
214 if (ret > 0 || trailing_dot)
215 return (ret);
216 saved_herrno = h_errno;
217 tried_as_is++;
221 * We do at least one level of search if
222 * - there is no dot and RES_DEFNAME is set, or
223 * - there is at least one dot, there is no trailing dot,
224 * and RES_DNSRCH is set.
226 if ((!dots && (statp->options & RES_DEFNAMES) != 0) ||
227 (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0)) {
228 int done = 0;
230 for (domain = (const char * const *)statp->dnsrch;
231 *domain && !done;
232 domain++) {
234 if (domain[0][0] == '\0' ||
235 (domain[0][0] == '.' && domain[0][1] == '\0'))
236 root_on_list++;
238 ret = res_nquerydomain(statp, name, *domain,
239 class, type,
240 answer, anslen);
241 if (ret > 0)
242 return (ret);
245 * If no server present, give up.
246 * If name isn't found in this domain,
247 * keep trying higher domains in the search list
248 * (if that's enabled).
249 * On a NO_DATA error, keep trying, otherwise
250 * a wildcard entry of another type could keep us
251 * from finding this entry higher in the domain.
252 * If we get some other error (negative answer or
253 * server failure), then stop searching up,
254 * but try the input name below in case it's
255 * fully-qualified.
257 if (errno == ECONNREFUSED) {
258 RES_SET_H_ERRNO(statp, TRY_AGAIN);
259 return (-1);
262 switch (statp->res_h_errno) {
263 case NO_DATA:
264 got_nodata++;
265 /* FALLTHROUGH */
266 case HOST_NOT_FOUND:
267 /* keep trying */
268 break;
269 case TRY_AGAIN:
270 if (hp->rcode == SERVFAIL) {
271 /* try next search element, if any */
272 got_servfail++;
273 break;
275 /* FALLTHROUGH */
276 default:
277 /* anything else implies that we're done */
278 done++;
281 /* if we got here for some reason other than DNSRCH,
282 * we only wanted one iteration of the loop, so stop.
284 if ((statp->options & RES_DNSRCH) == 0)
285 done++;
290 * If the name has any dots at all, and no earlier 'as-is' query
291 * for the name, and "." is not on the search list, then try an as-is
292 * query now.
294 if (statp->ndots && !(tried_as_is || root_on_list)) {
295 ret = res_nquerydomain(statp, name, NULL, class, type,
296 answer, anslen);
297 if (ret > 0)
298 return (ret);
301 /* if we got here, we didn't satisfy the search.
302 * if we did an initial full query, return that query's H_ERRNO
303 * (note that we wouldn't be here if that query had succeeded).
304 * else if we ever got a nodata, send that back as the reason.
305 * else send back meaningless H_ERRNO, that being the one from
306 * the last DNSRCH we did.
308 if (saved_herrno != -1)
309 RES_SET_H_ERRNO(statp, saved_herrno);
310 else if (got_nodata)
311 RES_SET_H_ERRNO(statp, NO_DATA);
312 else if (got_servfail)
313 RES_SET_H_ERRNO(statp, TRY_AGAIN);
314 return (-1);
318 * Perform a call on res_query on the concatenation of name and domain,
319 * removing a trailing dot from name if domain is NULL.
322 res_nquerydomain(res_state statp,
323 const char *name,
324 const char *domain,
325 int class, int type, /* class and type of query */
326 u_char *answer, /* buffer to put answer */
327 int anslen) /* size of answer */
329 char nbuf[MAXDNAME];
330 const char *longname = nbuf;
331 int n, d;
333 #ifdef DEBUG
334 if (statp->options & RES_DEBUG)
335 printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
336 name, domain?domain:"<Nil>", class, type);
337 #endif
338 if (domain == NULL) {
340 * Check for trailing '.';
341 * copy without '.' if present.
343 n = strlen(name);
344 if (n >= MAXDNAME) {
345 RES_SET_H_ERRNO(statp, NO_RECOVERY);
346 return (-1);
348 n--;
349 if (n >= 0 && name[n] == '.') {
350 strncpy(nbuf, name, n);
351 nbuf[n] = '\0';
352 } else
353 longname = name;
354 } else {
355 n = strlen(name);
356 d = strlen(domain);
357 if (n + d + 1 >= MAXDNAME) {
358 RES_SET_H_ERRNO(statp, NO_RECOVERY);
359 return (-1);
361 sprintf(nbuf, "%s.%s", name, domain);
363 return (res_nquery(statp, longname, class, type, answer, anslen));
366 const char *
367 res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
368 char *file, *cp1, *cp2;
369 char buf[BUFSIZ];
370 FILE *fp;
372 if (statp->options & RES_NOALIASES)
373 return (NULL);
374 file = getenv("HOSTALIASES");
375 if (file == NULL || (fp = fopen(file, "r")) == NULL)
376 return (NULL);
377 setbuf(fp, NULL);
378 buf[sizeof(buf) - 1] = '\0';
379 while (fgets(buf, sizeof(buf), fp)) {
380 for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)
382 if (!*cp1)
383 break;
384 *cp1 = '\0';
385 if (ns_samename(buf, name) == 1) {
386 while (isspace(*++cp1))
388 if (!*cp1)
389 break;
390 for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)
392 *cp2 = '\0';
393 strncpy(dst, cp1, siz - 1);
394 dst[siz - 1] = '\0';
395 fclose(fp);
396 return (dst);
399 fclose(fp);
400 return (NULL);