Libinfo-324.1.tar.gz
[apple/libinfo.git] / dns.subproj / res_query.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * ++Copyright++ 1988, 1993
26 * -
27 * Copyright (c) 1988, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 * -
58 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
59 *
60 * Permission to use, copy, modify, and distribute this software for any
61 * purpose with or without fee is hereby granted, provided that the above
62 * copyright notice and this permission notice appear in all copies, and that
63 * the name of Digital Equipment Corporation not be used in advertising or
64 * publicity pertaining to distribution of the document or software without
65 * specific, written prior permission.
66 *
67 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
68 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
69 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
70 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
71 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
72 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
73 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
74 * SOFTWARE.
75 * -
76 * --Copyright--
77 */
78
79 #if defined(LIBC_SCCS) && !defined(lint)
80 static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93";
81 static char rcsid[] = "$Id: res_query.c,v 1.3 2003/02/18 17:29:24 majka Exp $";
82 #endif /* LIBC_SCCS and not lint */
83
84 #include <sys/param.h>
85 #include <netinet/in.h>
86 #include <arpa/inet.h>
87
88 #include <stdio.h>
89 #include <netdb.h>
90 #include <ctype.h>
91 #include <errno.h>
92
93 #include "nameser8_compat.h"
94 #include "resolv8_compat.h"
95
96 #if defined(BSD) && (BSD >= 199306)
97 # include <stdlib.h>
98 # include <string.h>
99 #else
100 # include "portability.h"
101 #endif
102
103 #if defined(USE_OPTIONS_H)
104 # include "options.h"
105 #endif
106
107 #if PACKETSZ > 1024
108 #define MAXPACKET PACKETSZ
109 #else
110 #define MAXPACKET 1024
111 #endif
112
113 char *__hostalias __P((const char *));
114 #if defined(__APPLE__)
115 extern
116 #endif
117 int h_errno;
118
119 /*
120 * Formulate a normal query, send, and await answer.
121 * Returned answer is placed in supplied buffer "answer".
122 * Perform preliminary check of answer, returning success only
123 * if no error is indicated and the answer count is nonzero.
124 * Return the size of the response on success, -1 on error.
125 * Error number is left in h_errno.
126 *
127 * Caller must parse answer and determine whether it answers the question.
128 */
129 int
130 res_query(name, class, type, answer, anslen)
131 const char *name; /* domain name */
132 int class, type; /* class and type of query */
133 u_char *answer; /* buffer to put answer */
134 int anslen; /* size of answer buffer */
135 {
136 u_char buf[MAXPACKET];
137 register HEADER *hp = (HEADER *) answer;
138 int n;
139
140 hp->rcode = NOERROR; /* default */
141
142 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
143 h_errno = NETDB_INTERNAL;
144 return (-1);
145 }
146 #ifdef DEBUG
147 if (_res.options & RES_DEBUG)
148 printf(";; res_query(%s, %d, %d)\n", name, class, type);
149 #endif
150
151 n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,
152 buf, sizeof(buf));
153 if (n <= 0) {
154 #ifdef DEBUG
155 if (_res.options & RES_DEBUG)
156 printf(";; res_query: mkquery failed\n");
157 #endif
158 h_errno = NO_RECOVERY;
159 return (n);
160 }
161 n = res_send(buf, n, answer, anslen);
162 if (n < 0) {
163 #ifdef DEBUG
164 if (_res.options & RES_DEBUG)
165 printf(";; res_query: send error\n");
166 #endif
167 h_errno = TRY_AGAIN;
168 return (n);
169 }
170
171 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
172 #ifdef DEBUG
173 if (_res.options & RES_DEBUG)
174 printf(";; rcode = %d, ancount=%d\n", hp->rcode,
175 ntohs(hp->ancount));
176 #endif
177 switch (hp->rcode) {
178 case NXDOMAIN:
179 h_errno = HOST_NOT_FOUND;
180 break;
181 case SERVFAIL:
182 h_errno = TRY_AGAIN;
183 break;
184 case NOERROR:
185 h_errno = NO_DATA;
186 break;
187 case FORMERR:
188 case NOTIMP:
189 case REFUSED:
190 default:
191 h_errno = NO_RECOVERY;
192 break;
193 }
194 return (-1);
195 }
196 return (n);
197 }
198
199 /*
200 * Formulate a normal query, send, and retrieve answer in supplied buffer.
201 * Return the size of the response on success, -1 on error.
202 * If enabled, implement search rules until answer or unrecoverable failure
203 * is detected. Error code, if any, is left in h_errno.
204 */
205 int
206 res_search(name, class, type, answer, anslen)
207 const char *name; /* domain name */
208 int class, type; /* class and type of query */
209 u_char *answer; /* buffer to put answer */
210 int anslen; /* size of answer */
211 {
212 register const char *cp, * const *domain;
213 HEADER *hp = (HEADER *) answer;
214 u_int dots;
215 int trailing_dot, ret, saved_herrno;
216 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
217
218 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
219 h_errno = NETDB_INTERNAL;
220 return (-1);
221 }
222 errno = 0;
223 h_errno = HOST_NOT_FOUND; /* default, if we never query */
224 dots = 0;
225 for (cp = name; *cp; cp++)
226 dots += (*cp == '.');
227 trailing_dot = 0;
228 if (cp > name && *--cp == '.')
229 trailing_dot++;
230
231 /*
232 * if there aren't any dots, it could be a user-level alias
233 */
234 if (!dots && (cp = __hostalias(name)) != NULL)
235 return (res_query(cp, class, type, answer, anslen));
236
237 /*
238 * If there are dots in the name already, let's just give it a try
239 * 'as is'. The threshold can be set with the "ndots" option.
240 */
241 saved_herrno = -1;
242 if (dots >= _res.ndots) {
243 ret = res_querydomain(name, NULL, class, type, answer, anslen);
244 if (ret > 0)
245 return (ret);
246 saved_herrno = h_errno;
247 tried_as_is++;
248 }
249
250 /*
251 * We do at least one level of search if
252 * - there is no dot and RES_DEFNAME is set, or
253 * - there is at least one dot, there is no trailing dot,
254 * and RES_DNSRCH is set.
255 */
256 if ((!dots && (_res.options & RES_DEFNAMES)) ||
257 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
258 int done = 0;
259
260 for (domain = (const char * const *)_res.dnsrch;
261 *domain && !done;
262 domain++) {
263
264 ret = res_querydomain(name, *domain, class, type,
265 answer, anslen);
266 if (ret > 0)
267 return (ret);
268
269 /*
270 * If no server present, give up.
271 * If name isn't found in this domain,
272 * keep trying higher domains in the search list
273 * (if that's enabled).
274 * On a NO_DATA error, keep trying, otherwise
275 * a wildcard entry of another type could keep us
276 * from finding this entry higher in the domain.
277 * If we get some other error (negative answer or
278 * server failure), then stop searching up,
279 * but try the input name below in case it's
280 * fully-qualified.
281 */
282 if (errno == ECONNREFUSED) {
283 h_errno = TRY_AGAIN;
284 return (-1);
285 }
286
287 switch (h_errno) {
288 case NO_DATA:
289 got_nodata++;
290 /* FALLTHROUGH */
291 case HOST_NOT_FOUND:
292 /* keep trying */
293 break;
294 case TRY_AGAIN:
295 if (hp->rcode == SERVFAIL) {
296 /* try next search element, if any */
297 got_servfail++;
298 break;
299 }
300 /* FALLTHROUGH */
301 default:
302 /* anything else implies that we're done */
303 done++;
304 }
305
306 /* if we got here for some reason other than DNSRCH,
307 * we only wanted one iteration of the loop, so stop.
308 */
309 if (!(_res.options & RES_DNSRCH))
310 done++;
311 }
312 }
313
314 /* if we have not already tried the name "as is", do that now.
315 * note that we do this regardless of how many dots were in the
316 * name or whether it ends with a dot.
317 */
318 if (!tried_as_is) {
319 ret = res_querydomain(name, NULL, class, type, answer, anslen);
320 if (ret > 0)
321 return (ret);
322 }
323
324 /* if we got here, we didn't satisfy the search.
325 * if we did an initial full query, return that query's h_errno
326 * (note that we wouldn't be here if that query had succeeded).
327 * else if we ever got a nodata, send that back as the reason.
328 * else send back meaningless h_errno, that being the one from
329 * the last DNSRCH we did.
330 */
331 if (saved_herrno != -1)
332 h_errno = saved_herrno;
333 else if (got_nodata)
334 h_errno = NO_DATA;
335 else if (got_servfail)
336 h_errno = TRY_AGAIN;
337 return (-1);
338 }
339
340 /*
341 * Perform a call on res_query on the concatenation of name and domain,
342 * removing a trailing dot from name if domain is NULL.
343 */
344 int
345 res_querydomain(name, domain, class, type, answer, anslen)
346 const char *name, *domain;
347 int class, type; /* class and type of query */
348 u_char *answer; /* buffer to put answer */
349 int anslen; /* size of answer */
350 {
351 char nbuf[2*MAXDNAME+2];
352 const char *longname = nbuf;
353 int n;
354
355 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
356 h_errno = NETDB_INTERNAL;
357 return (-1);
358 }
359 #ifdef DEBUG
360 if (_res.options & RES_DEBUG)
361 printf(";; res_querydomain(%s, %s, %d, %d)\n",
362 name, domain?domain:"<Nil>", class, type);
363 #endif
364 if (domain == NULL) {
365 /*
366 * Check for trailing '.';
367 * copy without '.' if present.
368 */
369 n = strlen(name) - 1;
370 if (n != (0 - 1) && name[n] == '.' && n < sizeof(nbuf) - 1) {
371 bcopy(name, nbuf, n);
372 nbuf[n] = '\0';
373 } else
374 longname = name;
375 } else
376 sprintf(nbuf, "%.*s.%.*s", MAXDNAME, name, MAXDNAME, domain);
377
378 return (res_query(longname, class, type, answer, anslen));
379 }
380
381 char *
382 __hostalias(name)
383 register const char *name;
384 {
385 register char *cp1, *cp2;
386 FILE *fp;
387 char *file;
388 char buf[BUFSIZ];
389 static char abuf[MAXDNAME];
390
391 if (_res.options & RES_NOALIASES)
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(*cp1); ++cp1)
400 ;
401 if (!*cp1)
402 break;
403 *cp1 = '\0';
404 if (!strcasecmp(buf, name)) {
405 while (isspace(*++cp1))
406 ;
407 if (!*cp1)
408 break;
409 for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)
410 ;
411 abuf[sizeof(abuf) - 1] = *cp2 = '\0';
412 strncpy(abuf, cp1, sizeof(abuf) - 1);
413 fclose(fp);
414 return (abuf);
415 }
416 }
417 fclose(fp);
418 return (NULL);
419 }