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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
55 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
57 * Permission to use, copy, modify, and distribute this software for any
58 * purpose with or without fee is hereby granted, provided that the above
59 * copyright notice and this permission notice appear in all copies.
61 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
62 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
63 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
64 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
65 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
66 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
67 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
71 #if defined(LIBC_SCCS) && !defined(lint)
72 static const char sccsid
[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93";
73 static const char rcsid
[] = "$Id: res_query.c,v 1.1 2006/03/01 19:01:38 majka Exp $";
74 #endif /* LIBC_SCCS and not lint */
77 #include "port_before.h"
79 #include <sys/types.h>
80 #include <sys/param.h>
81 #include <netinet/in.h>
82 #include <arpa/inet.h>
83 #include <arpa/nameser.h>
91 #include "res_private.h"
93 #include <sys/event.h>
100 #include "port_after.h"
103 /* interrupt mechanism is implemented in res_send.c */
104 extern int interrupt_pipe_enabled
;
105 extern pthread_key_t interrupt_pipe_key
;
107 /* Options. Leave them on. */
111 #define MAXPACKET PACKETSZ
113 #define MAXPACKET 1024
116 #define BILLION 1000000000
118 /* length of a reverse DNS IPv6 address query name, e.g. "9.4.a.f.c.e.e.f.e.e.1.5.4.1.4.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa" */
119 #define IPv6_REVERSE_LEN 72
121 /* index of the trailing char that must be "8", "9", "A", "a", "b", or "B" */
122 #define IPv6_REVERSE_LINK_LOCAL_TRAILING_CHAR 58
124 /* index of low-order nibble of embedded scope id */
125 #define IPv6_REVERSE_LINK_LOCAL_SCOPE_ID_LOW 48
127 const static uint8_t hexval
[128] =
129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 15 */
130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16 - 31 */
131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 32 - 47 */
132 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 48 - 63 */
133 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 64 - 79 */
134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 95 */
135 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 96 - 111 */
136 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 112 - 127 */
139 struct res_query_context
144 uint16_t lastanstype
;
147 DNSServiceFlags flags
;
148 DNSServiceErrorType error
;
152 res_query_callback(DNSServiceRef sdRef
, DNSServiceFlags flags
, uint32_t ifIndex
, DNSServiceErrorType errorCode
, const char *fullname
, uint16_t rrtype
, uint16_t rrclass
, uint16_t rdlen
, const void *rdata
, uint32_t ttl
, void *ctx
)
154 struct res_query_context
*context
;
157 u_char
*dnlist
[2], *cp
;
161 context
= (struct res_query_context
*)ctx
;
163 context
->flags
= flags
;
164 context
->error
= errorCode
;
166 if (errorCode
!= kDNSServiceErr_NoError
)
168 if (context
->res_flags
& RES_DEBUG
) printf(";; res_query_mDNSResponder callback [%s %hu %hu]: error %u\n", fullname
, rrtype
, rrclass
, errorCode
);
172 buflen
= context
->ansmaxlen
- context
->anslen
;
173 if (buflen
< NS_HFIXEDSZ
)
175 if (context
->res_flags
& RES_DEBUG
) printf(";; res_query_mDNSResponder callback [%s %hu %hu]: malformed reply\n", fullname
, rrtype
, rrclass
);
179 dnlist
[0] = context
->answer
+ NS_HFIXEDSZ
;
182 cp
= context
->answer
+ context
->anslen
;
184 n
= dn_comp((char *)fullname
, cp
, buflen
, dnlist
, &dnlist
[1]);
187 if (context
->res_flags
& RES_DEBUG
) printf(";; res_query_mDNSResponder callback [%s %hu %hu]: name mismatch\n", fullname
, rrtype
, rrclass
);
192 * Check that there is enough space in the buffer for the resource name (n),
193 * the resource record data (rdlen) and the resource record header (10).
195 if (buflen
< n
+ rdlen
+ 10)
197 if (context
->res_flags
& RES_DEBUG
) printf(";; res_query_mDNSResponder callback [%s %hu %hu]: insufficient buffer space for reply\n", fullname
, rrtype
, rrclass
);
201 if (context
->res_flags
& RES_DEBUG
) printf(";; res_query_mDNSResponder callback for %s %hu %hu\n", fullname
, rrtype
, rrclass
);
206 putshort(rrtype
, cp
);
207 cp
+= sizeof(uint16_t);
209 putshort(rrclass
, cp
);
210 cp
+= sizeof(uint16_t);
213 cp
+= sizeof(uint32_t);
216 cp
+= sizeof(uint16_t);
218 memcpy(cp
, rdata
, rdlen
);
221 ans
= (HEADER
*)context
->answer
;
222 ans
->ancount
= htons(ntohs(ans
->ancount
) + 1);
224 context
->anslen
= (size_t)(cp
- context
->answer
);
226 context
->lastanstype
= rrtype
;
229 * Save the interface number for the first AAAA record for link-local addresses.
230 * It's used by getaddrinfo to set the scope id.
232 if ((context
->ifnum
== 0) && (rrtype
== ns_t_aaaa
))
234 memset(&a6
, 0, sizeof(struct in6_addr
));
235 memcpy(&a6
, rdata
, sizeof(struct in6_addr
));
236 if (IN6_IS_ADDR_LINKLOCAL(&a6
)) context
->ifnum
= ifIndex
;
241 h_errno_for_dnssd_err(DNSServiceErrorType dnssd_err
, int *h_errno_err
)
245 case kDNSServiceErr_NoError
:
246 *h_errno_err
= NETDB_SUCCESS
;
248 case kDNSServiceErr_Unknown
:
249 *h_errno_err
= NO_RECOVERY
;
251 case kDNSServiceErr_NoSuchRecord
:
252 *h_errno_err
= NO_DATA
;
254 case kDNSServiceErr_NoSuchName
:
255 *h_errno_err
= HOST_NOT_FOUND
;
257 case kDNSServiceErr_NoMemory
:
259 *h_errno_err
= NETDB_INTERNAL
;
265 _is_rev_link_local(const char *name
)
269 if (name
== NULL
) return 0;
272 if (len
== 0) return 0;
274 /* check for trailing '.' */
275 if (name
[len
- 1] == '.') len
--;
277 if (len
!= IPv6_REVERSE_LEN
) return 0;
279 i
= IPv6_REVERSE_LINK_LOCAL_TRAILING_CHAR
;
280 if ((name
[i
] != '8') && (name
[i
] != '9') && (name
[i
] != 'A') && (name
[i
] != 'a') && (name
[i
] != 'B') && (name
[i
] != 'b')) return 0;
282 i
= IPv6_REVERSE_LINK_LOCAL_TRAILING_CHAR
+ 1;
283 if (strncasecmp(name
+ i
, ".e.f.ip6.arpa", 13)) return 0;
285 for (i
= 0; i
< IPv6_REVERSE_LINK_LOCAL_TRAILING_CHAR
; i
+= 2)
287 if (name
[i
] < '0') return 0;
288 if ((name
[i
] > '9') && (name
[i
] < 'A')) return 0;
289 if ((name
[i
] > 'F') && (name
[i
] < 'a')) return 0;
290 if (name
[i
] > 'f') return 0;
291 if (name
[i
+ 1] != '.') return 0;
298 res_query_mDNSResponder(res_state statp
, const char *name
, int class, int type
, u_char
*answer
, int anslen
, struct sockaddr
*from
, uint32_t *fromlen
)
301 DNSServiceErrorType result
;
302 struct res_query_context context
;
303 int i
, kq
, n
, wait
, cancelled
, notify_token
, status
;
304 struct kevent mevent
, ievent
, event
;
306 struct timespec now
, finish
, timeout
;
310 char *qname
, *notify_name
;
312 uint64_t exit_requested
;
314 interrupt_pipe
= NULL
;
317 ans
= (HEADER
*)answer
;
321 memset(&context
, 0, sizeof(struct res_query_context
));
323 /* Build a dummy DNS header with question for the answer */
324 context
.res_flags
= statp
->options
;
325 context
.answer
= answer
;
326 context
.ansmaxlen
= anslen
;
327 context
.anslen
= res_nmkquery(statp
, ns_o_query
, name
, class, type
, NULL
, 0, NULL
, answer
, anslen
);
328 if (context
.anslen
<= 0) return 0;
330 /* Mark DNS packet as a response */
332 ans
->qr
= htons(ans
->qr
);
334 /* Pull out Scope ID in link-local reverse queries */
335 qname
= (char *)name
;
337 if (_is_rev_link_local(name
))
339 /* _is_rev_link_local rejects chars > 127 so it's safe to index into hexval */
340 i
= IPv6_REVERSE_LINK_LOCAL_SCOPE_ID_LOW
;
341 nibble
= hexval
[(uint32_t)name
[i
]];
345 nibble
= hexval
[(uint32_t)name
[i
]];
346 iface
+= (nibble
<< 4);
349 nibble
= hexval
[(uint32_t)name
[i
]];
350 iface
+= (nibble
<< 8);
353 nibble
= hexval
[(uint32_t)name
[i
]];
354 iface
+= (nibble
<< 12);
358 qname
= strdup(name
);
361 h_errno
= NO_RECOVERY
;
366 i
= IPv6_REVERSE_LINK_LOCAL_SCOPE_ID_LOW
;
374 if (statp
->options
& RES_DEBUG
) printf(";; res_query_mDNSResponder\n");
376 result
= DNSServiceQueryRecord(&sdRef
, kDNSServiceFlagsReturnIntermediates
, iface
, qname
, type
, class, res_query_callback
, &context
);
377 if (iface
!= 0) free(qname
);
379 if (result
!= 0) return 0;
381 /* Use a kqueue to wait for a response from mDNSResponder */
384 /* determine the maximum time to wait for a result */
385 gettimeofday(&ctv
, NULL
);
387 /* N.B. statp->retrans is actually the total timeount in seconds */
388 timeout
.tv_sec
= statp
->retrans
;
391 finish
.tv_sec
= ctv
.tv_sec
+ statp
->retrans
;
392 finish
.tv_nsec
= ctv
.tv_usec
* 1000;
394 /* add mdns reply FD to kqueue */
395 EV_SET(&mevent
, DNSServiceRefSockFD(sdRef
), EVFILT_READ
, EV_ADD
, 0, 0, 0);
396 n
= kevent(kq
, &mevent
, 1, NULL
, 0, NULL
);
397 if (n
!= 0) return 0;
399 /* add interrupt pipe to kqueue if interrupt is enabled */
400 if (interrupt_pipe_enabled
!= 0)
402 interrupt_pipe
= pthread_getspecific(interrupt_pipe_key
);
403 if (interrupt_pipe
!= NULL
)
405 if (interrupt_pipe
[0] >= 0)
407 EV_SET(&ievent
, interrupt_pipe
[0], EVFILT_READ
, EV_ADD
, 0, 0, (void *)name
);
408 /* allow this to fail silently (should never happen, but it would only break interrupts */
409 n
= kevent(kq
, &ievent
, 1, NULL
, 0, NULL
);
415 * Get notification token
416 * we use a self-notification token to allow a caller
417 * to signal the thread doing this DNS query to quit.
422 asprintf(¬ify_name
, "self.thread.%lu", (unsigned long)pthread_self());
423 if (notify_name
!= NULL
)
425 status
= notify_register_plain(notify_name
, ¬ify_token
);
432 memset(&event
, 0, sizeof(struct kevent
));
433 n
= kevent(kq
, NULL
, 0, &event
, 1, &timeout
);
435 if (notify_token
!= -1)
438 status
= notify_get_state(notify_token
, &exit_requested
);
439 if (exit_requested
== ThreadStateExitRequested
)
442 if (statp
->options
& RES_DEBUG
) printf(";; cancelled\n");
450 if (errno
== EINTR
) goto keep_waiting
;
451 h_errno
= NO_RECOVERY
;
454 else if ((n
== 0) && (ans
->ancount
== 0))
459 else if (event
.udata
== (void *)name
)
462 if (statp
->options
& RES_DEBUG
) printf(";; cancelled\n");
468 result
= DNSServiceProcessResult(sdRef
);
469 if ((result
!= 0) || (context
.error
!= 0))
471 if (result
== 0) result
= context
.error
;
472 h_errno_for_dnssd_err(result
, &h_errno
);
476 if ((ans
->ancount
> 0) && ((context
.flags
& kDNSServiceFlagsMoreComing
) == 0) && ((context
.lastanstype
!= ns_t_cname
) || (type
== ns_t_cname
))) wait
= 0;
483 /* calculate remaining timeout */
484 gettimeofday(&ctv
, NULL
);
486 now
.tv_sec
= ctv
.tv_sec
;
487 now
.tv_nsec
= ctv
.tv_usec
* 1000;
489 timeout
.tv_sec
= finish
.tv_sec
- now
.tv_sec
;
490 if (finish
.tv_nsec
>= now
.tv_nsec
)
492 timeout
.tv_nsec
= finish
.tv_nsec
- now
.tv_nsec
;
496 timeout
.tv_nsec
= BILLION
- now
.tv_nsec
+ finish
.tv_nsec
;
502 if (notify_token
!= -1) notify_cancel(notify_token
);
503 DNSServiceRefDeallocate(sdRef
);
506 if ((ans
->ancount
== 0) || (cancelled
== 1)) context
.anslen
= -1;
508 if ((from
!= NULL
) && (fromlen
!= NULL
) && (context
.ifnum
!= 0))
510 ((struct sockaddr_in6
*)from
)->sin6_len
= sizeof(struct sockaddr_in6
);
511 ((struct sockaddr_in6
*)from
)->sin6_family
= AF_INET6
;
512 ((struct sockaddr_in6
*)from
)->sin6_addr
.__u6_addr
.__u6_addr8
[15] = 1;
513 ((struct sockaddr_in6
*)from
)->sin6_scope_id
= context
.ifnum
;
514 *fromlen
= sizeof(struct sockaddr_in6
);
517 return context
.anslen
;
521 res_soa_minimum(const u_char
*msg
, int len
)
534 if (msg
+ NS_INT16SZ
> eom
) return -1;
535 NS_GET16(handle
._id
, msg
);
537 if (msg
+ NS_INT16SZ
> eom
) return -1;
538 NS_GET16(handle
._flags
, msg
);
540 for (i
= 0; i
< ns_s_max
; i
++)
542 if (msg
+ NS_INT16SZ
> eom
) return -1;
543 NS_GET16(handle
._counts
[i
], msg
);
546 if (handle
._counts
[ns_s_ns
] == 0) return -1;
548 /* Skip forward to nameserver section */
549 for (i
= 0; i
< ns_s_ns
; i
++)
551 if (handle
._counts
[i
] == 0) handle
._sections
[i
] = NULL
;
554 b
= ns_skiprr(msg
, eom
, (ns_sect
)i
, handle
._counts
[i
]);
555 if (b
< 0) return -1;
557 handle
._sections
[i
] = msg
;
563 for (i
= 0; i
< handle
._counts
[ns_s_ns
]; i
++)
565 b
= ns_skiprr(msg
, eom
, ns_s_ns
, 1);
566 if (b
< 0) return -1;
568 memcpy(&soa_min
, msg
+ b
- sizeof(int32_t), sizeof(int32_t));
569 soa_min
= ntohl(soa_min
);
570 if ((i
== 0) || (soa_min
< min
)) min
= soa_min
;
578 * Formulate a normal query, send, and await answer.
579 * Returned answer is placed in supplied buffer "answer".
580 * Perform preliminary check of answer, returning success only
581 * if no error is indicated and the answer count is nonzero.
582 * Return the size of the response on success, -1 on error.
583 * Error number is left in H_ERRNO.
585 * Caller must parse answer and determine whether it answers the question.
587 __attribute__((__visibility__("hidden"))) int
588 res_nquery_soa_min(res_state statp
, const char *name
, int class, int type
, u_char
*answer
, int anslen
, struct sockaddr
*from
, int *fromlen
, int *min
)
590 u_char buf
[MAXPACKET
];
591 HEADER
*hp
= (HEADER
*) answer
;
595 if (min
!= NULL
) *min
= -1;
597 oflags
= statp
->_flags
;
600 __h_errno_set(statp
, 0);
601 hp
->rcode
= ns_r_noerror
; /* default */
604 if (statp
->options
& RES_DEBUG
) printf(";; res_query(%s, %d, %d)\n", name
, class, type
);
607 n
= res_nmkquery(statp
, ns_o_query
, name
, class, type
, NULL
, 0, NULL
, buf
, sizeof(buf
));
609 if (n
> 0 && (statp
->_flags
& RES_F_EDNS0ERR
) == 0 && (statp
->options
& (RES_USE_EDNS0
|RES_USE_DNSSEC
)) != 0)
610 n
= res_nopt(statp
, n
, buf
, sizeof(buf
), anslen
);
615 if (statp
->options
& RES_DEBUG
) printf(";; res_query: mkquery failed\n");
617 __h_errno_set(statp
, NO_RECOVERY
);
621 n
= res_nsend_2(statp
, buf
, n
, answer
, anslen
, from
, fromlen
);
625 /* if the query choked with EDNS0, retry without EDNS0 */
626 if ((statp
->options
& (RES_USE_EDNS0
|RES_USE_DNSSEC
)) != 0 && ((oflags
^ statp
->_flags
) & RES_F_EDNS0ERR
) != 0)
628 statp
->_flags
|= RES_F_EDNS0ERR
;
629 if (statp
->options
& RES_DEBUG
) printf(";; res_nquery: retry without EDNS0\n");
634 if (statp
->options
& RES_DEBUG
) printf(";; res_query: send error\n");
636 __h_errno_set(statp
, TRY_AGAIN
);
640 if ((hp
->rcode
== ns_r_nxdomain
) || ((hp
->rcode
== ns_r_noerror
) && (ntohs(hp
->ancount
) == 0)))
644 *min
= res_soa_minimum(answer
, anslen
);
645 if (statp
->options
& RES_DEBUG
) printf(";; res_nquery: SOA minimum TTL = %d\n", *min
);
649 if (hp
->rcode
!= ns_r_noerror
|| ntohs(hp
->ancount
) == 0)
652 if (statp
->options
& RES_DEBUG
) printf(";; rcode = %d, ancount=%d\n", hp
->rcode
, ntohs(hp
->ancount
));
657 __h_errno_set(statp
, HOST_NOT_FOUND
);
660 __h_errno_set(statp
, TRY_AGAIN
);
663 __h_errno_set(statp
, NO_DATA
);
669 __h_errno_set(statp
, NO_RECOVERY
);
679 __attribute__((__visibility__("hidden")))
681 res_nquery_2(res_state statp
, const char *name
, int class, int type
, u_char
*answer
, int anslen
, struct sockaddr
*from
, int *fromlen
)
685 return res_nquery_soa_min(statp
, name
, class, type
, answer
, anslen
, from
, fromlen
, &unused
);
689 res_nquery(res_state statp
, const char *name
, int class, int type
, u_char
*answer
, int anslen
)
691 struct sockaddr_storage f
;
694 l
= sizeof(struct sockaddr_storage
);
696 return res_nquery_2(statp
, name
, class, type
, answer
, anslen
, (struct sockaddr
*)&f
, &l
);
700 * Perform a call on res_query on the concatenation of name and domain,
701 * removing a trailing dot from name if domain is NULL.
703 __attribute__((__visibility__("hidden")))
705 res_nquerydomain_2(res_state statp
, const char *name
, const char *domain
, int class, int type
, u_char
*answer
, int anslen
, struct sockaddr
*from
, int *fromlen
)
707 char nbuf
[NS_MAXDNAME
];
708 const char *longname
= nbuf
;
712 if (statp
->options
& RES_DEBUG
) printf(";; res_nquerydomain(%s, %s, %d, %d)\n", name
, domain
?domain
:"<Nil>", class, type
);
717 * Check for trailing '.';
718 * copy without '.' if present.
721 if (n
>= NS_MAXDNAME
)
723 __h_errno_set(statp
, NO_RECOVERY
);
728 if (n
>= 0 && name
[n
] == '.')
730 strncpy(nbuf
, name
, n
);
742 if (n
+ d
+ 1 >= NS_MAXDNAME
)
744 __h_errno_set(statp
, NO_RECOVERY
);
748 sprintf(nbuf
, "%s.%s", name
, domain
);
751 return (res_nquery_2(statp
, longname
, class, type
, answer
, anslen
, from
, fromlen
));
755 res_nquerydomain(res_state statp
, const char *name
, const char *domain
, int class, int type
, u_char
*answer
, int anslen
)
757 struct sockaddr_storage f
;
760 l
= sizeof(struct sockaddr_storage
);
762 return res_nquerydomain_2(statp
, name
, domain
, class, type
, answer
, anslen
, (struct sockaddr
*)&f
, &l
);
766 * Formulate a normal query, send, and retrieve answer in supplied buffer.
767 * Return the size of the response on success, -1 on error.
768 * If enabled, implement search rules until answer or unrecoverable failure
769 * is detected. Error code, if any, is left in H_ERRNO.
771 __attribute__((__visibility__("hidden")))
773 res_nsearch_2(res_state statp
, const char *name
, int class, int type
, u_char
*answer
, int anslen
, struct sockaddr
*from
, int *fromlen
)
775 const char *cp
, * const *domain
;
776 HEADER
*hp
= (HEADER
*) answer
;
777 char tmp
[NS_MAXDNAME
];
779 int trailing_dot
, ret
, saved_herrno
;
780 int got_nodata
= 0, got_servfail
= 0, root_on_list
= 0;
785 __h_errno_set(statp
, HOST_NOT_FOUND
); /* True if we never query. */
788 for (cp
= name
; *cp
!= '\0'; cp
++) dots
+= (*cp
== '.');
791 if (cp
> name
&& *--cp
== '.') trailing_dot
++;
793 /* If there aren't any dots, it could be a user-level alias. */
794 if (!dots
&& (cp
= res_hostalias(statp
, name
, tmp
, sizeof tmp
))!= NULL
)
795 return (res_nquery(statp
, cp
, class, type
, answer
, anslen
));
798 * If there are enough dots in the name, let's just give it a
799 * try 'as is'. The threshold can be set with the "ndots" option.
800 * Also, query 'as is', if there is a trailing dot in the name.
803 if (dots
>= statp
->ndots
|| trailing_dot
)
805 ret
= res_nquerydomain_2(statp
, name
, NULL
, class, type
, answer
, anslen
, from
, fromlen
);
806 if (ret
> 0 || trailing_dot
) return (ret
);
807 saved_herrno
= h_errno
;
812 * We do at least one level of search if
813 * - there is no dot and RES_DEFNAME is set, or
814 * - there is at least one dot, there is no trailing dot,
815 * and RES_DNSRCH is set.
817 if ((!dots
&& (statp
->options
& RES_DEFNAMES
) != 0) || (dots
&& !trailing_dot
&& (statp
->options
& RES_DNSRCH
) != 0))
821 for (domain
= (const char * const *)statp
->dnsrch
; *domain
&& !done
; domain
++)
825 if (domain
[0][0] == '\0' || (domain
[0][0] == '.' && domain
[0][1] == '\0')) root_on_list
++;
827 ret
= res_nquerydomain_2(statp
, name
, *domain
, class, type
, answer
, anslen
, from
, fromlen
);
828 if (ret
> 0) return (ret
);
831 * If no server present, give up.
832 * If name isn't found in this domain,
833 * keep trying higher domains in the search list
834 * (if that's enabled).
835 * On a NO_DATA error, keep trying, otherwise
836 * a wildcard entry of another type could keep us
837 * from finding this entry higher in the domain.
838 * If we get some other error (negative answer or
839 * server failure), then stop searching up,
840 * but try the input name below in case it's
843 if (errno
== ECONNREFUSED
)
845 __h_errno_set(statp
, TRY_AGAIN
);
849 switch (statp
->res_h_errno
)
858 if (hp
->rcode
== ns_r_refused
)
860 /* try next search element, if any */
866 /* anything else implies that we're done */
870 /* if we got here for some reason other than DNSRCH,
871 * we only wanted one iteration of the loop, so stop.
873 if ((statp
->options
& RES_DNSRCH
) == 0) done
++;
878 * If the query has not already been tried as is then try it
879 * unless RES_NOTLDQUERY is set and there were no dots.
881 if ((dots
|| !searched
|| (statp
->options
& RES_NOTLDQUERY
) == 0) && !(tried_as_is
|| root_on_list
))
883 ret
= res_nquerydomain_2(statp
, name
, NULL
, class, type
, answer
, anslen
, from
, fromlen
);
884 if (ret
> 0) return (ret
);
887 /* if we got here, we didn't satisfy the search.
888 * if we did an initial full query, return that query's H_ERRNO
889 * (note that we wouldn't be here if that query had succeeded).
890 * else if we ever got a nodata, send that back as the reason.
891 * else send back meaningless H_ERRNO, that being the one from
892 * the last DNSRCH we did.
894 if (saved_herrno
!= -1)
895 __h_errno_set(statp
, saved_herrno
);
897 __h_errno_set(statp
, NO_DATA
);
898 else if (got_servfail
)
899 __h_errno_set(statp
, TRY_AGAIN
);
903 __attribute__((__visibility__("hidden")))
905 __res_nsearch_list_2(res_state statp
, const char *name
, int class, int type
, u_char
*answer
, int anslen
, struct sockaddr
*from
, int *fromlen
, int nsearch
, char **search
)
907 const char *cp
, *domain
;
908 HEADER
*hp
= (HEADER
*) answer
;
909 char tmp
[NS_MAXDNAME
];
911 int trailing_dot
, ret
, saved_herrno
, i
;
912 int got_nodata
= 0, got_servfail
= 0, root_on_list
= 0;
917 __h_errno_set(statp
, HOST_NOT_FOUND
); /* True if we never query. */
920 for (cp
= name
; *cp
!= '\0'; cp
++) dots
+= (*cp
== '.');
923 if (cp
> name
&& *--cp
== '.') trailing_dot
++;
925 /* If there aren't any dots, it could be a user-level alias. */
926 if (!dots
&& (cp
= res_hostalias(statp
, name
, tmp
, sizeof tmp
)) != NULL
)
927 return (res_nquery(statp
, cp
, class, type
, answer
, anslen
));
930 * If there are enough dots in the name, let's just give it a
931 * try 'as is'. The threshold can be set with the "ndots" option.
932 * Also, query 'as is', if there is a trailing dot in the name.
935 if (dots
>= statp
->ndots
|| trailing_dot
)
937 ret
= res_nquerydomain(statp
, name
, NULL
, class, type
, answer
, anslen
);
938 if (ret
> 0 || trailing_dot
) return ret
;
939 saved_herrno
= h_errno
;
944 * We do at least one level of search if
945 * - there is no dot and RES_DEFNAME is set, or
946 * - there is at least one dot, there is no trailing dot,
947 * and RES_DNSRCH is set.
949 if ((!dots
&& (statp
->options
& RES_DEFNAMES
) != 0) || (dots
&& !trailing_dot
&& (statp
->options
& RES_DNSRCH
) != 0))
953 for (i
= 0; i
< nsearch
; i
++)
958 if (domain
[0] == '\0' || (domain
[0] == '.' && domain
[1] == '\0')) root_on_list
++;
960 ret
= res_nquerydomain_2(statp
, name
, domain
, class, type
, answer
, anslen
, from
, fromlen
);
961 if (ret
> 0) return ret
;
964 * If no server present, give up.
965 * If name isn't found in this domain,
966 * keep trying higher domains in the search list
967 * (if that's enabled).
968 * On a NO_DATA error, keep trying, otherwise
969 * a wildcard entry of another type could keep us
970 * from finding this entry higher in the domain.
971 * If we get some other error (negative answer or
972 * server failure), then stop searching up,
973 * but try the input name below in case it's
976 if (errno
== ECONNREFUSED
)
978 __h_errno_set(statp
, TRY_AGAIN
);
982 switch (statp
->res_h_errno
)
991 if (hp
->rcode
== ns_r_refused
)
993 /* try next search element, if any */
999 /* anything else implies that we're done */
1004 * if we got here for some reason other than DNSRCH,
1005 * we only wanted one iteration of the loop, so stop.
1007 if ((statp
->options
& RES_DNSRCH
) == 0) done
++;
1012 * If the query has not already been tried as is then try it
1013 * unless RES_NOTLDQUERY is set and there were no dots.
1015 if ((dots
|| !searched
|| (statp
->options
& RES_NOTLDQUERY
) == 0) && !(tried_as_is
|| root_on_list
))
1017 ret
= res_nquerydomain_2(statp
, name
, NULL
, class, type
, answer
, anslen
, from
, fromlen
);
1018 if (ret
> 0) return ret
;
1022 * we got here, we didn't satisfy the search.
1023 * if we did an initial full query, return that query's H_ERRNO
1024 * (note that we wouldn't be here if that query had succeeded).
1025 * else if we ever got a nodata, send that back as the reason.
1026 * else send back meaningless H_ERRNO, that being the one from
1027 * the last DNSRCH we did.
1029 if (saved_herrno
!= -1) __h_errno_set(statp
, saved_herrno
);
1030 else if (got_nodata
) __h_errno_set(statp
, NO_DATA
);
1031 else if (got_servfail
) __h_errno_set(statp
, TRY_AGAIN
);
1036 res_nsearch(res_state statp
, const char *name
, int class, int type
, u_char
*answer
, int anslen
)
1038 struct sockaddr_storage f
;
1041 l
= sizeof(struct sockaddr_storage
);
1043 return res_nsearch_2(statp
, name
, class, type
, answer
, anslen
, (struct sockaddr
*)&f
, &l
);
1047 res_hostalias(const res_state statp
, const char *name
, char *dst
, size_t siz
)
1049 char *file
, *cp1
, *cp2
;
1053 if (statp
->options
& RES_NOALIASES
) return (NULL
);
1055 file
= getenv("HOSTALIASES");
1056 if (file
== NULL
|| (fp
= fopen(file
, "r")) == NULL
) return (NULL
);
1059 buf
[sizeof(buf
) - 1] = '\0';
1060 while (fgets(buf
, sizeof(buf
), fp
))
1062 for (cp1
= buf
; *cp1
&& !isspace((unsigned char)*cp1
); ++cp1
) ;
1067 if (ns_samename(buf
, name
) == 1)
1069 while (isspace((unsigned char)*++cp1
)) ;
1073 for (cp2
= cp1
+ 1; *cp2
&& !isspace((unsigned char)*cp2
); ++cp2
) ;
1076 strncpy(dst
, cp1
, siz
- 1);
1077 dst
[siz
- 1] = '\0';