Remove tm.h and xm.h handling, as it wasn't used. Use nm.h only when needed.
[dragonfly.git] / lib / libc / net / res_query.c
blob8f753fe23125401fea93f1e08f1082361290d6c2
1 /*
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
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 * 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
27 * SUCH DAMAGE.
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
52 * SOFTWARE.
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
69 * SOFTWARE.
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 #include "res_config.h"
87 #if PACKETSZ > 1024
88 #define MAXPACKET PACKETSZ
89 #else
90 #define MAXPACKET 1024
91 #endif
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;
112 int n;
114 hp->rcode = NOERROR; /* default */
116 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
117 h_errno = NETDB_INTERNAL;
118 return (-1);
120 #ifdef DEBUG
121 if (_res.options & RES_DEBUG)
122 printf(";; res_query(%s, %d, %d)\n", name, class, type);
123 #endif
125 n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,
126 buf, sizeof(buf));
127 if (n > 0 && (_res.options & RES_USE_EDNS0) != 0)
128 n = res_opt(n, buf, sizeof(buf), anslen);
129 if (n <= 0) {
130 #ifdef DEBUG
131 if (_res.options & RES_DEBUG)
132 printf(";; res_query: mkquery failed\n");
133 #endif
134 h_errno = NO_RECOVERY;
135 return (n);
137 n = res_send(buf, n, answer, anslen);
138 if (n < 0) {
139 #ifdef DEBUG
140 if (_res.options & RES_DEBUG)
141 printf(";; res_query: send error\n");
142 #endif
143 h_errno = TRY_AGAIN;
144 return (n);
147 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
148 #ifdef DEBUG
149 if (_res.options & RES_DEBUG)
150 printf(";; rcode = %d, ancount=%d\n", hp->rcode,
151 ntohs(hp->ancount));
152 #endif
153 switch (hp->rcode) {
154 case NXDOMAIN:
155 h_errno = HOST_NOT_FOUND;
156 break;
157 case SERVFAIL:
158 h_errno = TRY_AGAIN;
159 break;
160 case NOERROR:
161 h_errno = NO_DATA;
162 break;
163 case FORMERR:
164 case NOTIMP:
165 case REFUSED:
166 default:
167 h_errno = NO_RECOVERY;
168 break;
170 return (-1);
172 return (n);
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;
189 u_int dots;
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;
195 return (-1);
197 errno = 0;
198 h_errno = HOST_NOT_FOUND; /* default, if we never query */
199 dots = 0;
200 for (cp = name; *cp; cp++)
201 dots += (*cp == '.');
202 trailing_dot = 0;
203 if (cp > name && *--cp == '.')
204 trailing_dot++;
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.
214 saved_herrno = -1;
215 if (dots >= _res.ndots) {
216 ret = res_querydomain(name, NULL, class, type, answer, anslen);
217 if (ret > 0)
218 return (ret);
219 saved_herrno = h_errno;
220 tried_as_is++;
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))) {
231 int done = 0;
233 for (domain = (const char * const *)_res.dnsrch;
234 *domain && !done;
235 domain++) {
237 ret = res_querydomain(name, *domain, class, type,
238 answer, anslen);
239 if (ret > 0)
240 return (ret);
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
253 * fully-qualified.
255 if (errno == ECONNREFUSED) {
256 h_errno = TRY_AGAIN;
257 return (-1);
260 switch (h_errno) {
261 case NO_DATA:
262 got_nodata++;
263 /* FALLTHROUGH */
264 case HOST_NOT_FOUND:
265 /* keep trying */
266 break;
267 case TRY_AGAIN:
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
273 * case of a timeout.
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.'.
285 ++got_servfail;
286 /* FALLTHROUGH */
287 default:
288 /* anything else implies that we're done */
289 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))
296 done++;
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);
307 if (ret > 0)
308 return (ret);
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;
320 else if (got_nodata)
321 h_errno = NO_DATA;
322 else if (got_servfail)
323 h_errno = TRY_AGAIN;
324 return (-1);
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,
333 const char *domain,
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 */
339 char nbuf[MAXDNAME];
340 const char *longname = nbuf;
341 int n, d;
343 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
344 h_errno = NETDB_INTERNAL;
345 return (-1);
347 #ifdef DEBUG
348 if (_res.options & RES_DEBUG)
349 printf(";; res_querydomain(%s, %s, %d, %d)\n",
350 name, domain?domain:"<Nil>", class, type);
351 #endif
352 if (domain == NULL) {
354 * Check for trailing '.';
355 * copy without '.' if present.
357 n = strlen(name);
358 if (n >= MAXDNAME) {
359 h_errno = NO_RECOVERY;
360 return (-1);
362 n--;
363 if (n >= 0 && name[n] == '.') {
364 strncpy(nbuf, name, n);
365 nbuf[n] = '\0';
366 } else
367 longname = name;
368 } else {
369 n = strlen(name);
370 d = strlen(domain);
371 if (n + d + 1 >= MAXDNAME) {
372 h_errno = NO_RECOVERY;
373 return (-1);
375 sprintf(nbuf, "%s.%s", name, domain);
377 return (res_query(longname, class, type, answer, anslen));
380 const char *
381 hostalias(const char *name)
383 char *cp1, *cp2;
384 FILE *fp;
385 char *file;
386 char buf[BUFSIZ];
387 static char abuf[MAXDNAME];
389 if (_res.options & RES_NOALIASES)
390 return (NULL);
391 if (issetugid())
392 return (NULL);
393 file = getenv("HOSTALIASES");
394 if (file == NULL || (fp = fopen(file, "r")) == NULL)
395 return (NULL);
396 setbuf(fp, NULL);
397 buf[sizeof(buf) - 1] = '\0';
398 while (fgets(buf, sizeof(buf), fp)) {
399 for (cp1 = buf; *cp1 && !isspace((unsigned char)*cp1); ++cp1)
401 if (!*cp1)
402 break;
403 *cp1 = '\0';
404 if (!strcasecmp(buf, name)) {
405 while (isspace((unsigned char)*++cp1))
407 if (!*cp1)
408 break;
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);
413 fclose(fp);
414 return (abuf);
417 fclose(fp);
418 return (NULL);
422 * Weak aliases for applications that use certain private entry points,
423 * and fail to include <resolv.h>.
425 #undef res_query
426 __weak_reference(__res_query, res_query);
427 #undef res_search
428 __weak_reference(__res_search, res_search);
429 #undef res_querydomain
430 __weak_reference(__res_querydomain, res_querydomain);