]>
git.saurik.com Git - apple/libinfo.git/blob - dns.subproj/res_query.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
25 * ++Copyright++ 1988, 1993
27 * Copyright (c) 1988, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
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.
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
58 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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
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 */
84 #include <sys/param.h>
85 #include <netinet/in.h>
86 #include <arpa/inet.h>
93 #include "nameser8_compat.h"
94 #include "resolv8_compat.h"
96 #if defined(BSD) && (BSD >= 199306)
100 # include "portability.h"
103 #if defined(USE_OPTIONS_H)
104 # include "options.h"
108 #define MAXPACKET PACKETSZ
110 #define MAXPACKET 1024
113 char *__hostalias
__P((const char *));
114 #if defined(__APPLE__)
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.
127 * Caller must parse answer and determine whether it answers the question.
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 */
136 u_char buf
[MAXPACKET
];
137 register HEADER
*hp
= (HEADER
*) answer
;
140 hp
->rcode
= NOERROR
; /* default */
142 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
143 h_errno
= NETDB_INTERNAL
;
147 if (_res
.options
& RES_DEBUG
)
148 printf(";; res_query(%s, %d, %d)\n", name
, class, type
);
151 n
= res_mkquery(QUERY
, name
, class, type
, NULL
, 0, NULL
,
155 if (_res
.options
& RES_DEBUG
)
156 printf(";; res_query: mkquery failed\n");
158 h_errno
= NO_RECOVERY
;
161 n
= res_send(buf
, n
, answer
, anslen
);
164 if (_res
.options
& RES_DEBUG
)
165 printf(";; res_query: send error\n");
171 if (hp
->rcode
!= NOERROR
|| ntohs(hp
->ancount
) == 0) {
173 if (_res
.options
& RES_DEBUG
)
174 printf(";; rcode = %d, ancount=%d\n", hp
->rcode
,
179 h_errno
= HOST_NOT_FOUND
;
191 h_errno
= NO_RECOVERY
;
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.
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 */
212 register const char *cp
, * const *domain
;
213 HEADER
*hp
= (HEADER
*) answer
;
215 int trailing_dot
, ret
, saved_herrno
;
216 int got_nodata
= 0, got_servfail
= 0, tried_as_is
= 0;
218 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
219 h_errno
= NETDB_INTERNAL
;
223 h_errno
= HOST_NOT_FOUND
; /* default, if we never query */
225 for (cp
= name
; *cp
; cp
++)
226 dots
+= (*cp
== '.');
228 if (cp
> name
&& *--cp
== '.')
232 * if there aren't any dots, it could be a user-level alias
234 if (!dots
&& (cp
= __hostalias(name
)) != NULL
)
235 return (res_query(cp
, class, type
, answer
, anslen
));
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.
242 if (dots
>= _res
.ndots
) {
243 ret
= res_querydomain(name
, NULL
, class, type
, answer
, anslen
);
246 saved_herrno
= h_errno
;
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.
256 if ((!dots
&& (_res
.options
& RES_DEFNAMES
)) ||
257 (dots
&& !trailing_dot
&& (_res
.options
& RES_DNSRCH
))) {
260 for (domain
= (const char * const *)_res
.dnsrch
;
264 ret
= res_querydomain(name
, *domain
, class, type
,
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
282 if (errno
== ECONNREFUSED
) {
295 if (hp
->rcode
== SERVFAIL
) {
296 /* try next search element, if any */
302 /* anything else implies that we're done */
306 /* if we got here for some reason other than DNSRCH,
307 * we only wanted one iteration of the loop, so stop.
309 if (!(_res
.options
& RES_DNSRCH
))
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.
319 ret
= res_querydomain(name
, NULL
, class, type
, answer
, anslen
);
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.
331 if (saved_herrno
!= -1)
332 h_errno
= saved_herrno
;
335 else if (got_servfail
)
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.
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 */
351 char nbuf
[2*MAXDNAME
+2];
352 const char *longname
= nbuf
;
355 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
356 h_errno
= NETDB_INTERNAL
;
360 if (_res
.options
& RES_DEBUG
)
361 printf(";; res_querydomain(%s, %s, %d, %d)\n",
362 name
, domain
?domain
:"<Nil>", class, type
);
364 if (domain
== NULL
) {
366 * Check for trailing '.';
367 * copy without '.' if present.
369 n
= strlen(name
) - 1;
370 if (n
!= (0 - 1) && name
[n
] == '.' && n
< sizeof(nbuf
) - 1) {
371 bcopy(name
, nbuf
, n
);
376 sprintf(nbuf
, "%.*s.%.*s", MAXDNAME
, name
, MAXDNAME
, domain
);
378 return (res_query(longname
, class, type
, answer
, anslen
));
383 register const char *name
;
385 register char *cp1
, *cp2
;
389 static char abuf
[MAXDNAME
];
391 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(*cp1
); ++cp1
)
404 if (!strcasecmp(buf
, name
)) {
405 while (isspace(*++cp1
))
409 for (cp2
= cp1
+ 1; *cp2
&& !isspace(*cp2
); ++cp2
)
411 abuf
[sizeof(abuf
) - 1] = *cp2
= '\0';
412 strncpy(abuf
, cp1
, sizeof(abuf
) - 1);