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
29 * @(#)res_query.c 8.1 (Berkeley) 6/4/93
30 * $From: Id: res_query.c,v 8.14 1997/06/09 17:47:05 halley Exp $
31 * $FreeBSD: src/lib/libc/net/res_query.c,v 1.19.2.2 2002/07/07 11:34:42 robert Exp $
32 * $DragonFly: src/lib/libc/net/res_query.c,v 1.6 2005/11/13 02:04:47 swildner Exp $
36 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 * Permission to use, copy, modify, and distribute this software for any
39 * purpose with or without fee is hereby granted, provided that the above
40 * copyright notice and this permission notice appear in all copies, and that
41 * the name of Digital Equipment Corporation not be used in advertising or
42 * publicity pertaining to distribution of the document or software without
43 * specific, written prior permission.
45 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
46 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
48 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
49 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
50 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
51 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
56 * Portions Copyright (c) 1996 by Internet Software Consortium.
58 * Permission to use, copy, modify, and distribute this software for any
59 * purpose with or without fee is hereby granted, provided that the above
60 * copyright notice and this permission notice appear in all copies.
62 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
63 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
64 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
65 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
66 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
67 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
68 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
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>
85 #include "res_config.h"
88 #define MAXPACKET PACKETSZ
90 #define MAXPACKET 1024
94 * Formulate a normal query, send, and await answer.
95 * Returned answer is placed in supplied buffer "answer".
96 * Perform preliminary check of answer, returning success only
97 * if no error is indicated and the answer count is nonzero.
98 * Return the size of the response on success, -1 on error.
99 * Error number is left in h_errno.
101 * Caller must parse answer and determine whether it answers the question.
104 res_query(const char *name, /* domain name */
105 int class, /* class of query */
106 int type, /* type of query */
107 u_char *answer, /* buffer to put answer */
108 int anslen) /* size of answer buffer */
110 u_char buf[MAXPACKET];
111 HEADER *hp = (HEADER *) answer;
114 hp->rcode = NOERROR; /* default */
116 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
117 h_errno = NETDB_INTERNAL;
121 if (_res.options & RES_DEBUG)
122 printf(";; res_query(%s, %d, %d)\n", name, class, type);
125 n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,
127 if (n > 0 && (_res.options & RES_USE_EDNS0) != 0)
128 n = res_opt(n, buf, sizeof(buf), anslen);
131 if (_res.options & RES_DEBUG)
132 printf(";; res_query: mkquery failed\n");
134 h_errno = NO_RECOVERY;
137 n = res_send(buf, n, answer, anslen);
140 if (_res.options & RES_DEBUG)
141 printf(";; res_query: send error\n");
147 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
149 if (_res.options & RES_DEBUG)
150 printf(";; rcode = %d, ancount=%d\n", hp->rcode,
155 h_errno = HOST_NOT_FOUND;
167 h_errno = NO_RECOVERY;
176 * Formulate a normal query, send, and retrieve answer in supplied buffer.
177 * Return the size of the response on success, -1 on error.
178 * If enabled, implement search rules until answer or unrecoverable failure
179 * is detected. Error code, if any, is left in h_errno.
182 res_search(const char *name, /* domain name */
183 int class, /* class of query */
184 int type, /* type of query */
185 u_char *answer, /* buffer to put answer */
186 int anslen) /* size of answer */
188 const char *cp, * const *domain;
190 int trailing_dot, ret, saved_herrno;
191 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
193 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
194 h_errno = NETDB_INTERNAL;
198 h_errno = HOST_NOT_FOUND; /* default, if we never query */
200 for (cp = name; *cp; cp++)
201 dots += (*cp == '.');
203 if (cp > name && *--cp == '.')
206 /* If there aren't any dots, it could be a user-level alias */
207 if (!dots && (cp = hostalias(name)) != NULL)
208 return (res_query(cp, class, type, answer, anslen));
211 * If there are dots in the name already, let's just give it a try
212 * 'as is'. The threshold can be set with the "ndots" option.
215 if (dots >= _res.ndots) {
216 ret = res_querydomain(name, NULL, class, type, answer, anslen);
219 saved_herrno = h_errno;
224 * We do at least one level of search if
225 * - there is no dot and RES_DEFNAME is set, or
226 * - there is at least one dot, there is no trailing dot,
227 * and RES_DNSRCH is set.
229 if ((!dots && (_res.options & RES_DEFNAMES)) ||
230 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
233 for (domain = (const char * const *)_res.dnsrch;
237 ret = res_querydomain(name, *domain, class, type,
243 * If no server present, give up.
244 * If name isn't found in this domain,
245 * keep trying higher domains in the search list
246 * (if that's enabled).
247 * On a NO_DATA error, keep trying, otherwise
248 * a wildcard entry of another type could keep us
249 * from finding this entry higher in the domain.
250 * If we get some other error (negative answer or
251 * server failure), then stop searching up,
252 * but try the input name below in case it's
255 if (errno == ECONNREFUSED) {
269 * This can occur due to a server failure
270 * (that is, all listed servers have failed),
271 * or all listed servers have timed out.
272 * hp->rcode may not be set to SERVFAIL in the
275 * Either way we must terminate the search
276 * and return TRY_AGAIN in order to avoid
277 * non-deterministic return codes. For
278 * example, loaded name servers or races
279 * against network startup/validation (dhcp,
280 * ppp, etc) can cause the search to timeout
281 * on one search element, e.g. 'fu.bar.com',
282 * and return a definitive failure on the
283 * next search element, e.g. 'fu.'.
288 /* anything else implies that we're done */
292 /* if we got here for some reason other than DNSRCH,
293 * we only wanted one iteration of the loop, so stop.
295 if (!(_res.options & RES_DNSRCH))
301 * If we have not already tried the name "as is", do that now.
302 * note that we do this regardless of how many dots were in the
303 * name or whether it ends with a dot unless NOTLDQUERY is set.
305 if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) {
306 ret = res_querydomain(name, NULL, class, type, answer, anslen);
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.
318 if (saved_herrno != -1)
319 h_errno = saved_herrno;
322 else if (got_servfail)
328 * Perform a call on res_query on the concatenation of name and domain,
329 * removing a trailing dot from name if domain is NULL.
332 res_querydomain(const char *name,
334 int class, /* class of query */
335 int type, /* type of query */
336 u_char *answer, /* buffer to put answer */
337 int anslen) /* size of answer */
340 const char *longname = nbuf;
343 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
344 h_errno = NETDB_INTERNAL;
348 if (_res.options & RES_DEBUG)
349 printf(";; res_querydomain(%s, %s, %d, %d)\n",
350 name, domain?domain:"<Nil>", class, type);
352 if (domain == NULL) {
354 * Check for trailing '.';
355 * copy without '.' if present.
359 h_errno = NO_RECOVERY;
363 if (n >= 0 && name[n] == '.') {
364 strncpy(nbuf, name, n);
371 if (n + d + 1 >= MAXDNAME) {
372 h_errno = NO_RECOVERY;
375 sprintf(nbuf, "%s.%s", name, domain);
377 return (res_query(longname, class, type, answer, anslen));
381 hostalias(const char *name)
387 static char abuf[MAXDNAME];
389 if (_res.options & RES_NOALIASES)
393 file = getenv("HOSTALIASES");
394 if (file == NULL || (fp = fopen(file, "r")) == NULL)
397 buf[sizeof(buf) - 1] = '\0';
398 while (fgets(buf, sizeof(buf), fp)) {
399 for (cp1 = buf; *cp1 && !isspace((unsigned char)*cp1); ++cp1)
404 if (!strcasecmp(buf, name)) {
405 while (isspace((unsigned char)*++cp1))
409 for (cp2 = cp1 + 1; *cp2 && !isspace((unsigned char)*cp2); ++cp2)
411 abuf[sizeof(abuf) - 1] = *cp2 = '\0';
412 strncpy(abuf, cp1, sizeof(abuf) - 1);
422 * Weak aliases for applications that use certain private entry points,
423 * and fail to include <resolv.h>.
426 __weak_reference(__res_query, res_query);
428 __weak_reference(__res_search, res_search);
429 #undef res_querydomain
430 __weak_reference(__res_querydomain, res_querydomain);