Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / dhcp / minires / res_query.c
blob9b2befafbf524b4808e7ef14f5827d225e0211de
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.
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) 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.
67 * 950 Charter Street
68 * Redwood City, CA 94063
69 * <info@isc.org>
70 * http://www.isc.org/
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>
82 #include <ctype.h>
83 #include <errno.h>
84 #include <netdb.h>
85 #include <stdio.h>
86 #include <stdlib.h>
87 #include <string.h>
88 #include <sys/socket.h>
90 #include "minires/minires.h"
91 #include "arpa/nameser.h"
93 /* Options. Leave them on. */
94 #define DEBUG
96 #if PACKETSZ > 1024
97 #define MAXPACKET PACKETSZ
98 #else
99 #define MAXPACKET 1024
100 #endif
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.
112 isc_result_t
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 */
117 unsigned anslen,
118 unsigned *ansret) /* size of answer buffer */
120 double buf[MAXPACKET / sizeof (double)];
121 HEADER *hp = (HEADER *) answer;
122 unsigned n;
123 isc_result_t rcode;
125 hp->rcode = NOERROR; /* default */
127 #ifdef DEBUG
128 if (statp->options & RES_DEBUG)
129 printf(";; res_query(%s, %d, %d)\n", name, class, type);
130 #endif
132 rcode = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
133 buf, sizeof(buf), &n);
134 if (rcode != ISC_R_SUCCESS) {
135 #ifdef DEBUG
136 if (statp->options & RES_DEBUG)
137 printf(";; res_query: mkquery failed\n");
138 #endif
139 RES_SET_H_ERRNO(statp, NO_RECOVERY);
140 return rcode;
142 rcode = res_nsend(statp, buf, n, answer, anslen, &n);
143 if (rcode != ISC_R_SUCCESS) {
144 #ifdef DEBUG
145 if (statp->options & RES_DEBUG)
146 printf(";; res_query: send error\n");
147 #endif
148 RES_SET_H_ERRNO(statp, TRY_AGAIN);
149 return rcode;
152 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
153 #ifdef DEBUG
154 if (statp->options & RES_DEBUG)
155 printf(";; rcode = %d, ancount=%d\n", hp->rcode,
156 ntohs(hp->ancount));
157 #endif
158 switch (hp->rcode) {
159 case NXDOMAIN:
160 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
161 break;
162 case SERVFAIL:
163 RES_SET_H_ERRNO(statp, TRY_AGAIN);
164 break;
165 case NOERROR:
166 RES_SET_H_ERRNO(statp, NO_DATA);
167 break;
168 case FORMERR:
169 case NOTIMP:
170 case REFUSED:
171 default:
172 RES_SET_H_ERRNO(statp, NO_RECOVERY);
173 break;
175 return ns_rcode_to_isc (hp -> rcode);
177 *ansret = n;
178 return ISC_R_SUCCESS;
181 #if 0
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.
188 isc_result_t
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 */
193 unsigned anslen,
194 unsigned *ansret) /* size of answer */
196 const char *cp, * const *domain;
197 HEADER *hp = (HEADER *) answer;
198 char tmp[NS_MAXDNAME];
199 u_int dots;
200 int trailing_dot, ret;
201 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
202 isc_result_t rcode;
204 errno = 0;
205 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */
207 dots = 0;
208 for (cp = name; *cp != '\0'; cp++)
209 dots += (*cp == '.');
210 trailing_dot = 0;
211 if (cp > name && *--cp == '.')
212 trailing_dot++;
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)) {
235 int done = 0;
237 for (domain = (const char * const *)statp->dnsrch;
238 *domain && !done;
239 domain++) {
241 if (domain[0][0] == '\0' ||
242 (domain[0][0] == '.' && domain[0][1] == '\0'))
243 root_on_list++;
245 rcode = res_nquerydomain(statp, name, *domain,
246 class, type,
247 answer, anslen, &ret);
248 if (rcode == ISC_R_SUCCESS && ret > 0) {
249 *ansret = ret;
250 return rcode;
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
264 * fully-qualified.
266 if (errno == ECONNREFUSED) {
267 RES_SET_H_ERRNO(statp, TRY_AGAIN);
268 return ISC_R_CONNREFUSED;
271 switch (statp->res_h_errno) {
272 case NO_DATA:
273 got_nodata++;
274 /* FALLTHROUGH */
275 case HOST_NOT_FOUND:
276 /* keep trying */
277 break;
278 case TRY_AGAIN:
279 if (hp->rcode == SERVFAIL) {
280 /* try next search element, if any */
281 got_servfail++;
282 break;
284 /* FALLTHROUGH */
285 default:
286 /* anything else implies that we're done */
287 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)
294 done++;
299 * If the name has any dots at all, and "." is not on the search
300 * list, then try an as-is query now.
302 if (statp->ndots) {
303 rcode = res_nquerydomain(statp, name, NULL, class, type,
304 answer, anslen, &ret);
305 if (rcode == ISC_R_SUCCESS && ret > 0) {
306 *ansret = ret;
307 return rcode;
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 (got_nodata) {
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;
327 #endif
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.
333 isc_result_t
334 res_nquerydomain(res_state statp,
335 const char *name,
336 const char *domain,
337 ns_class class, ns_type type,
338 double *answer,
339 unsigned anslen,
340 unsigned *ansret)
342 char nbuf[MAXDNAME];
343 const char *longname = nbuf;
344 int n, d;
346 #ifdef DEBUG
347 if (statp->options & RES_DEBUG)
348 printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
349 name, domain?domain:"<Nil>", class, type);
350 #endif
351 if (domain == NULL) {
353 * Check for trailing '.';
354 * copy without '.' if present.
356 n = strlen(name);
357 if (n >= MAXDNAME) {
358 RES_SET_H_ERRNO(statp, NO_RECOVERY);
359 return ISC_R_NOSPACE;
361 n--;
362 if (n >= 0 && name[n] == '.') {
363 strncpy(nbuf, name, (unsigned)n);
364 nbuf[n] = '\0';
365 } else
366 longname = name;
367 } else {
368 n = strlen(name);
369 d = strlen(domain);
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);
380 const char *
381 res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
382 char *file, *cp1, *cp2;
383 char buf[BUFSIZ];
384 FILE *fp;
386 if (statp->options & RES_NOALIASES)
387 return (NULL);
388 file = getenv("HOSTALIASES");
389 if (file == NULL || (fp = fopen(file, "r")) == NULL)
390 return (NULL);
391 setbuf(fp, NULL);
392 buf[sizeof(buf) - 1] = '\0';
393 while (fgets(buf, sizeof(buf), fp)) {
394 for (cp1 = buf; *cp1 && !isspace((unsigned char)*cp1); ++cp1)
396 if (!*cp1)
397 break;
398 *cp1 = '\0';
399 if (ns_samename(buf, name) == 1) {
400 while (isspace((unsigned char)*++cp1))
402 if (!*cp1)
403 break;
404 for (cp2 = cp1 + 1; *cp2 && !isspace((unsigned char)*cp2); ++cp2)
406 *cp2 = '\0';
407 strncpy(dst, cp1, siz - 1);
408 dst[siz - 1] = '\0';
409 fclose(fp);
410 return (dst);
413 fclose(fp);
414 return (NULL);