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
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 * 3. 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
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
51 * Portions Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
52 * Portions Copyright (c) 1996-2003 by Internet Software Consortium
54 * Permission to use, copy, modify, and distribute this software for any
55 * purpose with or without fee is hereby granted, provided that the above
56 * copyright notice and this permission notice appear in all copies.
58 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
59 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
60 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
61 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
62 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
63 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
64 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
66 * Internet Systems Consortium, Inc.
68 * Redwood City, CA 94063
73 #if defined(LIBC_SCCS) && !defined(lint)
74 static const char sccsid
[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93";
75 static const char rcsid
[] = "$Id: res_query.c,v 1.4 2005/08/11 17:13:26 drochner Exp $";
76 #endif /* LIBC_SCCS and not lint */
78 #include <sys/types.h>
79 #include <sys/param.h>
80 #include <netinet/in.h>
81 #include <arpa/inet.h>
88 #include <sys/socket.h>
90 #include "minires/minires.h"
91 #include "arpa/nameser.h"
93 /* Options. Leave them on. */
97 #define MAXPACKET PACKETSZ
99 #define MAXPACKET 1024
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 res_nquery(res_state statp
,
114 const char *name
, /* domain name */
115 ns_class
class, ns_type type
, /* class and type of query */
116 double *answer
, /* buffer to put answer */
118 unsigned *ansret
) /* size of answer buffer */
120 double buf
[MAXPACKET
/ sizeof (double)];
121 HEADER
*hp
= (HEADER
*) answer
;
125 hp
->rcode
= NOERROR
; /* default */
128 if (statp
->options
& RES_DEBUG
)
129 printf(";; res_query(%s, %d, %d)\n", name
, class, type
);
132 rcode
= res_nmkquery(statp
, QUERY
, name
, class, type
, NULL
, 0, NULL
,
133 buf
, sizeof(buf
), &n
);
134 if (rcode
!= ISC_R_SUCCESS
) {
136 if (statp
->options
& RES_DEBUG
)
137 printf(";; res_query: mkquery failed\n");
139 RES_SET_H_ERRNO(statp
, NO_RECOVERY
);
142 rcode
= res_nsend(statp
, buf
, n
, answer
, anslen
, &n
);
143 if (rcode
!= ISC_R_SUCCESS
) {
145 if (statp
->options
& RES_DEBUG
)
146 printf(";; res_query: send error\n");
148 RES_SET_H_ERRNO(statp
, TRY_AGAIN
);
152 if (hp
->rcode
!= NOERROR
|| ntohs(hp
->ancount
) == 0) {
154 if (statp
->options
& RES_DEBUG
)
155 printf(";; rcode = %d, ancount=%d\n", hp
->rcode
,
160 RES_SET_H_ERRNO(statp
, HOST_NOT_FOUND
);
163 RES_SET_H_ERRNO(statp
, TRY_AGAIN
);
166 RES_SET_H_ERRNO(statp
, NO_DATA
);
172 RES_SET_H_ERRNO(statp
, NO_RECOVERY
);
175 return ns_rcode_to_isc (hp
-> rcode
);
178 return ISC_R_SUCCESS
;
183 * Formulate a normal query, send, and retrieve answer in supplied buffer.
184 * Return the size of the response on success, -1 on error.
185 * If enabled, implement search rules until answer or unrecoverable failure
186 * is detected. Error code, if any, is left in H_ERRNO.
189 res_nsearch(res_state statp
,
190 const char *name
, /* domain name */
191 ns_class
class, ns_type type
, /* class and type of query */
192 double *answer
, /* buffer to put answer */
194 unsigned *ansret
) /* size of answer */
196 const char *cp
, * const *domain
;
197 HEADER
*hp
= (HEADER
*) answer
;
198 char tmp
[NS_MAXDNAME
];
200 int trailing_dot
, ret
;
201 int got_nodata
= 0, got_servfail
= 0, root_on_list
= 0;
205 RES_SET_H_ERRNO(statp
, HOST_NOT_FOUND
); /* True if we never query. */
208 for (cp
= name
; *cp
!= '\0'; cp
++)
209 dots
+= (*cp
== '.');
211 if (cp
> name
&& *--cp
== '.')
214 /* If there aren't any dots, it could be a user-level alias. */
215 if (!dots
&& (cp
= res_hostalias(statp
, name
, tmp
, sizeof tmp
))!= NULL
)
216 return res_nquery(statp
, cp
, class, type
,
217 answer
, anslen
, ansret
);
220 * If there are enough dots in the name, do no searching.
221 * (The threshold can be set with the "ndots" option.)
223 if (dots
>= statp
->ndots
|| trailing_dot
)
224 return res_nquerydomain(statp
, name
, NULL
, class, type
,
225 answer
, anslen
, ansret
);
228 * We do at least one level of search if
229 * - there is no dot and RES_DEFNAME is set, or
230 * - there is at least one dot, there is no trailing dot,
231 * and RES_DNSRCH is set.
233 if ((!dots
&& (statp
->options
& RES_DEFNAMES
) != 0) ||
234 (dots
&& !trailing_dot
&& (statp
->options
& RES_DNSRCH
) != 0)) {
237 for (domain
= (const char * const *)statp
->dnsrch
;
241 if (domain
[0][0] == '\0' ||
242 (domain
[0][0] == '.' && domain
[0][1] == '\0'))
245 rcode
= res_nquerydomain(statp
, name
, *domain
,
247 answer
, anslen
, &ret
);
248 if (rcode
== ISC_R_SUCCESS
&& ret
> 0) {
254 * If no server present, give up.
255 * If name isn't found in this domain,
256 * keep trying higher domains in the search list
257 * (if that's enabled).
258 * On a NO_DATA error, keep trying, otherwise
259 * a wildcard entry of another type could keep us
260 * from finding this entry higher in the domain.
261 * If we get some other error (negative answer or
262 * server failure), then stop searching up,
263 * but try the input name below in case it's
266 if (errno
== ECONNREFUSED
) {
267 RES_SET_H_ERRNO(statp
, TRY_AGAIN
);
268 return ISC_R_CONNREFUSED
;
271 switch (statp
->res_h_errno
) {
279 if (hp
->rcode
== SERVFAIL
) {
280 /* try next search element, if any */
286 /* anything else implies that we're done */
290 /* if we got here for some reason other than DNSRCH,
291 * we only wanted one iteration of the loop, so stop.
293 if ((statp
->options
& RES_DNSRCH
) == 0)
299 * If the name has any dots at all, and "." is not on the search
300 * list, then try an as-is query now.
303 rcode
= res_nquerydomain(statp
, name
, NULL
, class, type
,
304 answer
, anslen
, &ret
);
305 if (rcode
== ISC_R_SUCCESS
&& ret
> 0) {
311 /* if we got here, we didn't satisfy the search.
312 * if we did an initial full query, return that query's H_ERRNO
313 * (note that we wouldn't be here if that query had succeeded).
314 * else if we ever got a nodata, send that back as the reason.
315 * else send back meaningless H_ERRNO, that being the one from
316 * the last DNSRCH we did.
319 RES_SET_H_ERRNO(statp
, NO_DATA
);
320 return ISC_R_NOTFOUND
;
321 } else if (got_servfail
) {
322 RES_SET_H_ERRNO(statp
, TRY_AGAIN
);
323 return ISC_R_TIMEDOUT
;
325 return ISC_R_UNEXPECTED
;
330 * Perform a call on res_query on the concatenation of name and domain,
331 * removing a trailing dot from name if domain is NULL.
334 res_nquerydomain(res_state statp
,
337 ns_class
class, ns_type type
,
343 const char *longname
= nbuf
;
347 if (statp
->options
& RES_DEBUG
)
348 printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
349 name
, domain
?domain
:"<Nil>", class, type
);
351 if (domain
== NULL
) {
353 * Check for trailing '.';
354 * copy without '.' if present.
358 RES_SET_H_ERRNO(statp
, NO_RECOVERY
);
359 return ISC_R_NOSPACE
;
362 if (n
>= 0 && name
[n
] == '.') {
363 strncpy(nbuf
, name
, (unsigned)n
);
370 if (n
+ d
+ 1 >= MAXDNAME
) {
371 RES_SET_H_ERRNO(statp
, NO_RECOVERY
);
372 return ISC_R_NOSPACE
;
374 sprintf(nbuf
, "%s.%s", name
, domain
);
376 return res_nquery(statp
,
377 longname
, class, type
, answer
, anslen
, ansret
);
381 res_hostalias(const res_state statp
, const char *name
, char *dst
, size_t siz
) {
382 char *file
, *cp1
, *cp2
;
386 if (statp
->options
& RES_NOALIASES
)
388 file
= getenv("HOSTALIASES");
389 if (file
== NULL
|| (fp
= fopen(file
, "r")) == NULL
)
392 buf
[sizeof(buf
) - 1] = '\0';
393 while (fgets(buf
, sizeof(buf
), fp
)) {
394 for (cp1
= buf
; *cp1
&& !isspace((unsigned char)*cp1
); ++cp1
)
399 if (ns_samename(buf
, name
) == 1) {
400 while (isspace((unsigned char)*++cp1
))
404 for (cp2
= cp1
+ 1; *cp2
&& !isspace((unsigned char)*cp2
); ++cp2
)
407 strncpy(dst
, cp1
, siz
- 1);