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 * 4. 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
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
51 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
53 * Permission to use, copy, modify, and distribute this software for any
54 * purpose with or without fee is hereby granted, provided that the above
55 * copyright notice and this permission notice appear in all copies.
57 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
58 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
60 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
61 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
62 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
63 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
68 #include <sys/types.h>
69 #include <sys/param.h>
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
72 #include <arpa/nameser.h>
77 #include <resolv/resolv-internal.h>
78 #include <resolv/resolv_context.h>
82 #include <shlib-compat.h>
85 #define MAXPACKET PACKETSZ
87 #define MAXPACKET 65536
90 #define QUERYSIZE (HFIXEDSZ + QFIXEDSZ + MAXCDNAME + 1)
93 __res_context_querydomain (struct resolv_context
*,
94 const char *name
, const char *domain
,
95 int class, int type
, unsigned char *answer
, int anslen
,
96 unsigned char **answerp
, unsigned char **answerp2
, int *nanswerp2
,
97 int *resplen2
, int *answerp2_malloced
);
99 /* Formulate a normal query, send, and await answer. Returned answer
100 is placed in supplied buffer ANSWER. Perform preliminary check of
101 answer, returning success only if no error is indicated and the
102 answer count is nonzero. Return the size of the response on
103 success, -1 on error. Error number is left in h_errno.
105 Caller must parse answer and determine whether it answers the
108 __res_context_query (struct resolv_context
*ctx
, const char *name
,
110 unsigned char *answer
, int anslen
,
111 unsigned char **answerp
, unsigned char **answerp2
,
112 int *nanswerp2
, int *resplen2
, int *answerp2_malloced
)
114 struct __res_state
*statp
= ctx
->resp
;
115 UHEADER
*hp
= (UHEADER
*) answer
;
117 int n
, use_malloc
= 0;
119 size_t bufsize
= (type
== T_QUERY_A_AND_AAAA
? 2 : 1) * QUERYSIZE
;
120 u_char
*buf
= alloca (bufsize
);
121 u_char
*query1
= buf
;
123 u_char
*query2
= NULL
;
127 hp
->rcode
= NOERROR
; /* default */
129 if (type
== T_QUERY_A_AND_AAAA
)
131 n
= __res_context_mkquery (ctx
, QUERY
, name
, class, T_A
, NULL
,
135 if ((statp
->options
& (RES_USE_EDNS0
|RES_USE_DNSSEC
)) != 0)
137 /* Use RESOLV_EDNS_BUFFER_SIZE because the receive
138 buffer can be reallocated. */
139 n
= __res_nopt (ctx
, n
, query1
, bufsize
,
140 RESOLV_EDNS_BUFFER_SIZE
);
146 /* Align the buffer. */
147 int npad
= ((nquery1
+ __alignof__ (HEADER
) - 1)
148 & ~(__alignof__ (HEADER
) - 1)) - nquery1
;
149 if (n
> bufsize
- npad
)
154 int nused
= n
+ npad
;
155 query2
= buf
+ nused
;
156 n
= __res_context_mkquery (ctx
, QUERY
, name
, class, T_AAAA
,
157 NULL
, query2
, bufsize
- nused
);
159 && (statp
->options
& (RES_USE_EDNS0
|RES_USE_DNSSEC
)) != 0)
160 /* Use RESOLV_EDNS_BUFFER_SIZE because the receive
161 buffer can be reallocated. */
162 n
= __res_nopt (ctx
, n
, query2
, bufsize
,
163 RESOLV_EDNS_BUFFER_SIZE
);
171 n
= __res_context_mkquery (ctx
, QUERY
, name
, class, type
, NULL
,
175 && (statp
->options
& (RES_USE_EDNS0
|RES_USE_DNSSEC
)) != 0)
177 /* Use RESOLV_EDNS_BUFFER_SIZE if the receive buffer
178 can be reallocated. */
183 advertise
= RESOLV_EDNS_BUFFER_SIZE
;
184 n
= __res_nopt (ctx
, n
, query1
, bufsize
, advertise
);
190 if (__glibc_unlikely (n
<= 0) && !use_malloc
) {
191 /* Retry just in case res_nmkquery failed because of too
192 short buffer. Shouldn't happen. */
193 bufsize
= (type
== T_QUERY_A_AND_AAAA
? 2 : 1) * MAXPACKET
;
194 buf
= malloc (bufsize
);
201 if (__glibc_unlikely (n
<= 0)) {
202 RES_SET_H_ERRNO(statp
, NO_RECOVERY
);
207 assert (answerp
== NULL
|| (void *) *answerp
== (void *) answer
);
208 n
= __res_context_send (ctx
, query1
, nquery1
, query2
, nquery2
, answer
,
209 anslen
, answerp
, answerp2
, nanswerp2
, resplen2
,
214 RES_SET_H_ERRNO(statp
, TRY_AGAIN
);
219 /* __res_context_send might have reallocated the buffer. */
220 hp
= (UHEADER
*) *answerp
;
222 /* We simplify the following tests by assigning HP to HP2 or
223 vice versa. It is easy to verify that this is the same as
224 ignoring all tests of HP or HP2. */
225 if (answerp2
== NULL
|| *resplen2
< (int) sizeof (HEADER
))
231 hp2
= (UHEADER
*) *answerp2
;
232 if (n
< (int) sizeof (HEADER
))
238 /* Make sure both hp and hp2 are defined */
239 assert((hp
!= NULL
) && (hp2
!= NULL
));
241 if ((hp
->rcode
!= NOERROR
|| ntohs(hp
->ancount
) == 0)
242 && (hp2
->rcode
!= NOERROR
|| ntohs(hp2
->ancount
) == 0)) {
243 switch (hp
->rcode
== NOERROR
? hp2
->rcode
: hp
->rcode
) {
245 if ((hp
->rcode
== NOERROR
&& ntohs (hp
->ancount
) != 0)
246 || (hp2
->rcode
== NOERROR
247 && ntohs (hp2
->ancount
) != 0))
249 RES_SET_H_ERRNO(statp
, HOST_NOT_FOUND
);
252 RES_SET_H_ERRNO(statp
, TRY_AGAIN
);
255 if (ntohs (hp
->ancount
) != 0
256 || ntohs (hp2
->ancount
) != 0)
258 RES_SET_H_ERRNO(statp
, NO_DATA
);
262 /* Servers must not reply to AAAA queries with
263 NOTIMP etc but some of them do. */
264 if ((hp
->rcode
== NOERROR
&& ntohs (hp
->ancount
) != 0)
265 || (hp2
->rcode
== NOERROR
266 && ntohs (hp2
->ancount
) != 0))
271 RES_SET_H_ERRNO(statp
, NO_RECOVERY
);
279 libc_hidden_def (__res_context_query
)
281 /* Common part of res_nquery and res_query. */
283 context_query_common (struct resolv_context
*ctx
,
284 const char *name
, int class, int type
,
285 unsigned char *answer
, int anslen
)
289 RES_SET_H_ERRNO (&_res
, NETDB_INTERNAL
);
292 int result
= __res_context_query (ctx
, name
, class, type
, answer
, anslen
,
293 NULL
, NULL
, NULL
, NULL
, NULL
);
294 __resolv_context_put (ctx
);
299 ___res_nquery (res_state statp
,
300 const char *name
, /* Domain name. */
301 int class, int type
, /* Class and type of query. */
302 unsigned char *answer
, /* Buffer to put answer. */
303 int anslen
) /* Size of answer buffer. */
305 return context_query_common
306 (__resolv_context_get_override (statp
), name
, class, type
, answer
, anslen
);
308 versioned_symbol (libc
, ___res_nquery
, res_nquery
, GLIBC_2_34
);
309 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_2, GLIBC_2_34)
310 compat_symbol (libresolv
, ___res_nquery
, __res_nquery
, GLIBC_2_2
);
314 ___res_query (const char *name
, int class, int type
,
315 unsigned char *answer
, int anslen
)
317 return context_query_common
318 (__resolv_context_get (), name
, class, type
, answer
, anslen
);
320 versioned_symbol (libc
, ___res_query
, res_query
, GLIBC_2_34
);
321 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_2)
322 compat_symbol (libresolv
, ___res_query
, res_query
, GLIBC_2_0
);
324 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_2, GLIBC_2_34)
325 compat_symbol (libresolv
, ___res_query
, __res_query
, GLIBC_2_2
);
328 /* Formulate a normal query, send, and retrieve answer in supplied
329 buffer. Return the size of the response on success, -1 on error.
330 If enabled, implement search rules until answer or unrecoverable
331 failure is detected. Error code, if any, is left in h_errno. */
333 __res_context_search (struct resolv_context
*ctx
,
334 const char *name
, int class, int type
,
335 unsigned char *answer
, int anslen
,
336 unsigned char **answerp
, unsigned char **answerp2
,
337 int *nanswerp2
, int *resplen2
, int *answerp2_malloced
)
339 struct __res_state
*statp
= ctx
->resp
;
341 UHEADER
*hp
= (UHEADER
*) answer
;
342 char tmp
[NS_MAXDNAME
];
344 int trailing_dot
, ret
, saved_herrno
;
345 int got_nodata
= 0, got_servfail
= 0, root_on_list
= 0;
350 RES_SET_H_ERRNO(statp
, HOST_NOT_FOUND
); /* True if we never query. */
353 for (cp
= name
; *cp
!= '\0'; cp
++)
354 dots
+= (*cp
== '.');
356 if (cp
> name
&& *--cp
== '.')
359 /* If there aren't any dots, it could be a user-level alias. */
360 if (!dots
&& (cp
= __res_context_hostalias
361 (ctx
, name
, tmp
, sizeof tmp
))!= NULL
)
362 return __res_context_query (ctx
, cp
, class, type
, answer
,
363 anslen
, answerp
, answerp2
,
364 nanswerp2
, resplen2
, answerp2_malloced
);
367 * If there are enough dots in the name, let's just give it a
368 * try 'as is'. The threshold can be set with the "ndots" option.
369 * Also, query 'as is', if there is a trailing dot in the name.
372 if (dots
>= statp
->ndots
|| trailing_dot
) {
373 ret
= __res_context_querydomain (ctx
, name
, NULL
, class, type
,
374 answer
, anslen
, answerp
,
375 answerp2
, nanswerp2
, resplen2
,
377 if (ret
> 0 || trailing_dot
378 /* If the second response is valid then we use that. */
379 || (ret
== 0 && resplen2
!= NULL
&& *resplen2
> 0))
381 saved_herrno
= h_errno
;
383 if (answerp
&& *answerp
!= answer
) {
387 if (answerp2
&& *answerp2_malloced
)
392 *answerp2_malloced
= 0;
397 * We do at least one level of search if
398 * - there is no dot and RES_DEFNAME is set, or
399 * - there is at least one dot, there is no trailing dot,
400 * and RES_DNSRCH is set.
402 if ((!dots
&& (statp
->options
& RES_DEFNAMES
) != 0) ||
403 (dots
&& !trailing_dot
&& (statp
->options
& RES_DNSRCH
) != 0)) {
406 for (size_t domain_index
= 0; !done
; ++domain_index
) {
407 const char *dname
= __resolv_context_search_list
413 /* __res_context_querydoman concatenates name
414 with dname with a "." in between. If we
415 pass it in dname the "." we got from the
416 configured default search path, we'll end
417 up with "name..", which won't resolve.
418 OTOH, passing it "" will result in "name.",
419 which has the intended effect for both
420 possible representations of the root
424 if (dname
[0] == '\0')
427 ret
= __res_context_querydomain
428 (ctx
, name
, dname
, class, type
,
429 answer
, anslen
, answerp
, answerp2
, nanswerp2
,
430 resplen2
, answerp2_malloced
);
431 if (ret
> 0 || (ret
== 0 && resplen2
!= NULL
435 if (answerp
&& *answerp
!= answer
) {
439 if (answerp2
&& *answerp2_malloced
)
444 *answerp2_malloced
= 0;
448 * If no server present, give up.
449 * If name isn't found in this domain,
450 * keep trying higher domains in the search list
451 * (if that's enabled).
452 * On a NO_DATA error, keep trying, otherwise
453 * a wildcard entry of another type could keep us
454 * from finding this entry higher in the domain.
455 * If we get some other error (negative answer or
456 * server failure), then stop searching up,
457 * but try the input name below in case it's
460 if (errno
== ECONNREFUSED
) {
461 RES_SET_H_ERRNO(statp
, TRY_AGAIN
);
465 switch (statp
->res_h_errno
) {
473 if (hp
->rcode
== SERVFAIL
) {
474 /* try next search element, if any */
480 /* anything else implies that we're done */
484 /* if we got here for some reason other than DNSRCH,
485 * we only wanted one iteration of the loop, so stop.
487 if ((statp
->options
& RES_DNSRCH
) == 0)
493 * If the query has not already been tried as is then try it
494 * unless RES_NOTLDQUERY is set and there were no dots.
496 if ((dots
|| !searched
|| (statp
->options
& RES_NOTLDQUERY
) == 0)
497 && !(tried_as_is
|| root_on_list
)) {
498 ret
= __res_context_querydomain
499 (ctx
, name
, NULL
, class, type
,
500 answer
, anslen
, answerp
, answerp2
, nanswerp2
,
501 resplen2
, answerp2_malloced
);
502 if (ret
> 0 || (ret
== 0 && resplen2
!= NULL
507 /* if we got here, we didn't satisfy the search.
508 * if we did an initial full query, return that query's H_ERRNO
509 * (note that we wouldn't be here if that query had succeeded).
510 * else if we ever got a nodata, send that back as the reason.
511 * else send back meaningless H_ERRNO, that being the one from
512 * the last DNSRCH we did.
514 if (answerp2
&& *answerp2_malloced
)
519 *answerp2_malloced
= 0;
521 if (saved_herrno
!= -1)
522 RES_SET_H_ERRNO(statp
, saved_herrno
);
524 RES_SET_H_ERRNO(statp
, NO_DATA
);
525 else if (got_servfail
)
526 RES_SET_H_ERRNO(statp
, TRY_AGAIN
);
529 libc_hidden_def (__res_context_search
)
531 /* Common part of res_nsearch and res_search. */
533 context_search_common (struct resolv_context
*ctx
,
534 const char *name
, int class, int type
,
535 unsigned char *answer
, int anslen
)
539 RES_SET_H_ERRNO (&_res
, NETDB_INTERNAL
);
542 int result
= __res_context_search (ctx
, name
, class, type
, answer
, anslen
,
543 NULL
, NULL
, NULL
, NULL
, NULL
);
544 __resolv_context_put (ctx
);
549 ___res_nsearch (res_state statp
,
550 const char *name
, /* Domain name. */
551 int class, int type
, /* Class and type of query. */
552 unsigned char *answer
, /* Buffer to put answer. */
553 int anslen
) /* Size of answer. */
555 return context_search_common
556 (__resolv_context_get_override (statp
), name
, class, type
, answer
, anslen
);
558 versioned_symbol (libc
, ___res_nsearch
, res_nsearch
, GLIBC_2_34
);
559 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_2, GLIBC_2_34)
560 compat_symbol (libresolv
, ___res_nsearch
, __res_nsearch
, GLIBC_2_2
);
564 ___res_search (const char *name
, int class, int type
,
565 unsigned char *answer
, int anslen
)
567 return context_search_common
568 (__resolv_context_get (), name
, class, type
, answer
, anslen
);
570 versioned_symbol (libc
, ___res_search
, res_search
, GLIBC_2_34
);
571 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_2)
572 compat_symbol (libresolv
, ___res_search
, res_search
, GLIBC_2_0
);
574 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_2, GLIBC_2_34)
575 compat_symbol (libresolv
, ___res_search
, __res_search
, GLIBC_2_2
);
578 /* Perform a call on res_query on the concatenation of name and
581 __res_context_querydomain (struct resolv_context
*ctx
,
582 const char *name
, const char *domain
,
584 unsigned char *answer
, int anslen
,
585 unsigned char **answerp
, unsigned char **answerp2
,
586 int *nanswerp2
, int *resplen2
,
587 int *answerp2_malloced
)
589 struct __res_state
*statp
= ctx
->resp
;
591 const char *longname
= nbuf
;
594 if (domain
== NULL
) {
597 /* Decrement N prior to checking it against MAXDNAME
598 so that we detect a wrap to SIZE_MAX and return
599 a reasonable error. */
601 if (n
>= MAXDNAME
- 1) {
602 RES_SET_H_ERRNO(statp
, NO_RECOVERY
);
609 if (n
+ d
+ 1 >= MAXDNAME
) {
610 RES_SET_H_ERRNO(statp
, NO_RECOVERY
);
613 char *p
= __stpcpy (nbuf
, name
);
617 return __res_context_query (ctx
, longname
, class, type
, answer
,
618 anslen
, answerp
, answerp2
, nanswerp2
,
619 resplen2
, answerp2_malloced
);
622 /* Common part of res_nquerydomain and res_querydomain. */
624 context_querydomain_common (struct resolv_context
*ctx
,
625 const char *name
, const char *domain
,
627 unsigned char *answer
, int anslen
)
631 RES_SET_H_ERRNO (&_res
, NETDB_INTERNAL
);
634 int result
= __res_context_querydomain (ctx
, name
, domain
, class, type
,
636 NULL
, NULL
, NULL
, NULL
, NULL
);
637 __resolv_context_put (ctx
);
642 ___res_nquerydomain (res_state statp
,
645 int class, int type
, /* Class and type of query. */
646 unsigned char *answer
, /* Buffer to put answer. */
647 int anslen
) /* Size of answer. */
649 return context_querydomain_common
650 (__resolv_context_get_override (statp
),
651 name
, domain
, class, type
, answer
, anslen
);
653 versioned_symbol (libc
, ___res_nquerydomain
, res_nquerydomain
, GLIBC_2_34
);
654 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_2, GLIBC_2_34)
655 compat_symbol (libresolv
, ___res_nquerydomain
, __res_nquerydomain
, GLIBC_2_2
);
659 ___res_querydomain (const char *name
, const char *domain
, int class, int type
,
660 unsigned char *answer
, int anslen
)
662 return context_querydomain_common
663 (__resolv_context_get (), name
, domain
, class, type
, answer
, anslen
);
665 versioned_symbol (libc
, ___res_querydomain
, res_querydomain
, GLIBC_2_34
);
666 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_2)
667 compat_symbol (libresolv
, ___res_querydomain
, res_querydomain
, GLIBC_2_0
);
669 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_2, GLIBC_2_34)
670 compat_symbol (libresolv
, ___res_querydomain
, __res_querydomain
, GLIBC_2_2
);