17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib * Portions Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
3*9525b14bSRao Shoaib * Portions Copyright (C) 1996-2001, 2003 Internet Software Consortium.
4*9525b14bSRao Shoaib *
5*9525b14bSRao Shoaib * Permission to use, copy, modify, and/or distribute this software for any
6*9525b14bSRao Shoaib * purpose with or without fee is hereby granted, provided that the above
7*9525b14bSRao Shoaib * copyright notice and this permission notice appear in all copies.
8*9525b14bSRao Shoaib *
9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*9525b14bSRao Shoaib * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*9525b14bSRao Shoaib * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*9525b14bSRao Shoaib * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*9525b14bSRao Shoaib * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*9525b14bSRao Shoaib * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*9525b14bSRao Shoaib * PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate /*
197c478bd9Sstevel@tonic-gate * Copyright (c) 1988, 1993
207c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
217c478bd9Sstevel@tonic-gate *
227c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
237c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
247c478bd9Sstevel@tonic-gate * are met:
257c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
267c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
277c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
287c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
297c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
307c478bd9Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
317c478bd9Sstevel@tonic-gate * must display the following acknowledgement:
327c478bd9Sstevel@tonic-gate * This product includes software developed by the University of
337c478bd9Sstevel@tonic-gate * California, Berkeley and its contributors.
347c478bd9Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
357c478bd9Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
367c478bd9Sstevel@tonic-gate * without specific prior written permission.
377c478bd9Sstevel@tonic-gate *
387c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
397c478bd9Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
407c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
417c478bd9Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
427c478bd9Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
437c478bd9Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
447c478bd9Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
457c478bd9Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
467c478bd9Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
477c478bd9Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
487c478bd9Sstevel@tonic-gate * SUCH DAMAGE.
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate * Portions Copyright (c) 1993 by Digital Equipment Corporation.
537c478bd9Sstevel@tonic-gate *
547c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
557c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
567c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies, and that
577c478bd9Sstevel@tonic-gate * the name of Digital Equipment Corporation not be used in advertising or
587c478bd9Sstevel@tonic-gate * publicity pertaining to distribution of the document or software without
597c478bd9Sstevel@tonic-gate * specific, written prior permission.
607c478bd9Sstevel@tonic-gate *
617c478bd9Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
627c478bd9Sstevel@tonic-gate * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
637c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
647c478bd9Sstevel@tonic-gate * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
657c478bd9Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
667c478bd9Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
677c478bd9Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
687c478bd9Sstevel@tonic-gate * SOFTWARE.
697c478bd9Sstevel@tonic-gate */
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
727c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93";
73*9525b14bSRao Shoaib static const char rcsid[] = "$Id: res_query.c,v 1.11 2008/11/14 02:36:51 marka Exp $";
747c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate #include "port_before.h"
777c478bd9Sstevel@tonic-gate #include <sys/types.h>
787c478bd9Sstevel@tonic-gate #include <sys/param.h>
797c478bd9Sstevel@tonic-gate #include <netinet/in.h>
807c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
817c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
827c478bd9Sstevel@tonic-gate #include <ctype.h>
837c478bd9Sstevel@tonic-gate #include <errno.h>
847c478bd9Sstevel@tonic-gate #include <netdb.h>
857c478bd9Sstevel@tonic-gate #include <resolv.h>
867c478bd9Sstevel@tonic-gate #include <stdio.h>
877c478bd9Sstevel@tonic-gate #include <stdlib.h>
887c478bd9Sstevel@tonic-gate #include <string.h>
897c478bd9Sstevel@tonic-gate #include "port_after.h"
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate /* Options. Leave them on. */
927c478bd9Sstevel@tonic-gate #define DEBUG
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate #if PACKETSZ > 1024
957c478bd9Sstevel@tonic-gate #define MAXPACKET PACKETSZ
967c478bd9Sstevel@tonic-gate #else
977c478bd9Sstevel@tonic-gate #define MAXPACKET 1024
987c478bd9Sstevel@tonic-gate #endif
997c478bd9Sstevel@tonic-gate
100*9525b14bSRao Shoaib /*%
1017c478bd9Sstevel@tonic-gate * Formulate a normal query, send, and await answer.
1027c478bd9Sstevel@tonic-gate * Returned answer is placed in supplied buffer "answer".
1037c478bd9Sstevel@tonic-gate * Perform preliminary check of answer, returning success only
1047c478bd9Sstevel@tonic-gate * if no error is indicated and the answer count is nonzero.
1057c478bd9Sstevel@tonic-gate * Return the size of the response on success, -1 on error.
1067c478bd9Sstevel@tonic-gate * Error number is left in H_ERRNO.
1077c478bd9Sstevel@tonic-gate *
1087c478bd9Sstevel@tonic-gate * Caller must parse answer and determine whether it answers the question.
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate int
res_nquery(res_state statp,const char * name,int class,int type,u_char * answer,int anslen)1117c478bd9Sstevel@tonic-gate res_nquery(res_state statp,
112*9525b14bSRao Shoaib const char *name, /*%< domain name */
113*9525b14bSRao Shoaib int class, int type, /*%< class and type of query */
114*9525b14bSRao Shoaib u_char *answer, /*%< buffer to put answer */
115*9525b14bSRao Shoaib int anslen) /*%< size of answer buffer */
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate u_char buf[MAXPACKET];
1187c478bd9Sstevel@tonic-gate HEADER *hp = (HEADER *) answer;
1197c478bd9Sstevel@tonic-gate u_int oflags;
120*9525b14bSRao Shoaib u_char *rdata;
121*9525b14bSRao Shoaib int n;
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate oflags = statp->_flags;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate again:
126*9525b14bSRao Shoaib hp->rcode = NOERROR; /*%< default */
1277c478bd9Sstevel@tonic-gate #ifdef DEBUG
1287c478bd9Sstevel@tonic-gate if (statp->options & RES_DEBUG)
1297c478bd9Sstevel@tonic-gate printf(";; res_query(%s, %d, %d)\n", name, class, type);
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
1337c478bd9Sstevel@tonic-gate buf, sizeof(buf));
1347c478bd9Sstevel@tonic-gate #ifdef RES_USE_EDNS0
1357c478bd9Sstevel@tonic-gate if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 &&
136*9525b14bSRao Shoaib (statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC|RES_NSID))) {
1377c478bd9Sstevel@tonic-gate n = res_nopt(statp, n, buf, sizeof(buf), anslen);
138*9525b14bSRao Shoaib rdata = &buf[n];
139*9525b14bSRao Shoaib if (n > 0 && (statp->options & RES_NSID) != 0U) {
140*9525b14bSRao Shoaib n = res_nopt_rdata(statp, n, buf, sizeof(buf), rdata,
141*9525b14bSRao Shoaib NS_OPT_NSID, 0, NULL);
142*9525b14bSRao Shoaib }
143*9525b14bSRao Shoaib }
1447c478bd9Sstevel@tonic-gate #endif
1457c478bd9Sstevel@tonic-gate if (n <= 0) {
1467c478bd9Sstevel@tonic-gate #ifdef DEBUG
1477c478bd9Sstevel@tonic-gate if (statp->options & RES_DEBUG)
1487c478bd9Sstevel@tonic-gate printf(";; res_query: mkquery failed\n");
1497c478bd9Sstevel@tonic-gate #endif
1507c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_RECOVERY);
1517c478bd9Sstevel@tonic-gate return (n);
1527c478bd9Sstevel@tonic-gate }
153*9525b14bSRao Shoaib
1547c478bd9Sstevel@tonic-gate n = res_nsend(statp, buf, n, answer, anslen);
1557c478bd9Sstevel@tonic-gate if (n < 0) {
1567c478bd9Sstevel@tonic-gate #ifdef RES_USE_EDNS0
1577c478bd9Sstevel@tonic-gate /* if the query choked with EDNS0, retry without EDNS0 */
158*9525b14bSRao Shoaib if ((statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U &&
1597c478bd9Sstevel@tonic-gate ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
1607c478bd9Sstevel@tonic-gate statp->_flags |= RES_F_EDNS0ERR;
1617c478bd9Sstevel@tonic-gate if (statp->options & RES_DEBUG)
1627c478bd9Sstevel@tonic-gate printf(";; res_nquery: retry without EDNS0\n");
1637c478bd9Sstevel@tonic-gate goto again;
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate #endif
1667c478bd9Sstevel@tonic-gate #ifdef DEBUG
1677c478bd9Sstevel@tonic-gate if (statp->options & RES_DEBUG)
1687c478bd9Sstevel@tonic-gate printf(";; res_query: send error\n");
1697c478bd9Sstevel@tonic-gate #endif
1707c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(statp, TRY_AGAIN);
1717c478bd9Sstevel@tonic-gate return (n);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1757c478bd9Sstevel@tonic-gate #ifdef DEBUG
1767c478bd9Sstevel@tonic-gate if (statp->options & RES_DEBUG)
1777c478bd9Sstevel@tonic-gate printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n",
1787c478bd9Sstevel@tonic-gate p_rcode(hp->rcode),
1797c478bd9Sstevel@tonic-gate ntohs(hp->ancount),
1807c478bd9Sstevel@tonic-gate ntohs(hp->nscount),
1817c478bd9Sstevel@tonic-gate ntohs(hp->arcount));
1827c478bd9Sstevel@tonic-gate #endif
1837c478bd9Sstevel@tonic-gate switch (hp->rcode) {
1847c478bd9Sstevel@tonic-gate case NXDOMAIN:
1857c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
1867c478bd9Sstevel@tonic-gate break;
1877c478bd9Sstevel@tonic-gate case SERVFAIL:
1887c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(statp, TRY_AGAIN);
1897c478bd9Sstevel@tonic-gate break;
1907c478bd9Sstevel@tonic-gate case NOERROR:
1917c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_DATA);
1927c478bd9Sstevel@tonic-gate break;
1937c478bd9Sstevel@tonic-gate case FORMERR:
1947c478bd9Sstevel@tonic-gate case NOTIMP:
1957c478bd9Sstevel@tonic-gate case REFUSED:
1967c478bd9Sstevel@tonic-gate default:
1977c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_RECOVERY);
1987c478bd9Sstevel@tonic-gate break;
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate return (-1);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate return (n);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate
205*9525b14bSRao Shoaib /*%
2067c478bd9Sstevel@tonic-gate * Formulate a normal query, send, and retrieve answer in supplied buffer.
2077c478bd9Sstevel@tonic-gate * Return the size of the response on success, -1 on error.
2087c478bd9Sstevel@tonic-gate * If enabled, implement search rules until answer or unrecoverable failure
2097c478bd9Sstevel@tonic-gate * is detected. Error code, if any, is left in H_ERRNO.
2107c478bd9Sstevel@tonic-gate */
2117c478bd9Sstevel@tonic-gate int
res_nsearch(res_state statp,const char * name,int class,int type,u_char * answer,int anslen)2127c478bd9Sstevel@tonic-gate res_nsearch(res_state statp,
213*9525b14bSRao Shoaib const char *name, /*%< domain name */
214*9525b14bSRao Shoaib int class, int type, /*%< class and type of query */
215*9525b14bSRao Shoaib u_char *answer, /*%< buffer to put answer */
216*9525b14bSRao Shoaib int anslen) /*%< size of answer */
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate const char *cp, * const *domain;
2197c478bd9Sstevel@tonic-gate HEADER *hp = (HEADER *) answer;
2207c478bd9Sstevel@tonic-gate char tmp[NS_MAXDNAME];
2217c478bd9Sstevel@tonic-gate u_int dots;
2227c478bd9Sstevel@tonic-gate int trailing_dot, ret, saved_herrno;
2237c478bd9Sstevel@tonic-gate int got_nodata = 0, got_servfail = 0, root_on_list = 0;
2247c478bd9Sstevel@tonic-gate int tried_as_is = 0;
2257c478bd9Sstevel@tonic-gate int searched = 0;
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate errno = 0;
228*9525b14bSRao Shoaib RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /*%< True if we never query. */
2297c478bd9Sstevel@tonic-gate dots = 0;
2307c478bd9Sstevel@tonic-gate for (cp = name; *cp != '\0'; cp++)
2317c478bd9Sstevel@tonic-gate dots += (*cp == '.');
2327c478bd9Sstevel@tonic-gate trailing_dot = 0;
2337c478bd9Sstevel@tonic-gate if (cp > name && *--cp == '.')
2347c478bd9Sstevel@tonic-gate trailing_dot++;
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate /* If there aren't any dots, it could be a user-level alias. */
2377c478bd9Sstevel@tonic-gate if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
2387c478bd9Sstevel@tonic-gate return (res_nquery(statp, cp, class, type, answer, anslen));
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate * If there are enough dots in the name, let's just give it a
2427c478bd9Sstevel@tonic-gate * try 'as is'. The threshold can be set with the "ndots" option.
2437c478bd9Sstevel@tonic-gate * Also, query 'as is', if there is a trailing dot in the name.
2447c478bd9Sstevel@tonic-gate */
2457c478bd9Sstevel@tonic-gate saved_herrno = -1;
2467c478bd9Sstevel@tonic-gate if (dots >= statp->ndots || trailing_dot) {
2477c478bd9Sstevel@tonic-gate ret = res_nquerydomain(statp, name, NULL, class, type,
2487c478bd9Sstevel@tonic-gate answer, anslen);
2497c478bd9Sstevel@tonic-gate if (ret > 0 || trailing_dot)
2507c478bd9Sstevel@tonic-gate return (ret);
2517c478bd9Sstevel@tonic-gate saved_herrno = statp->res_h_errno;
2527c478bd9Sstevel@tonic-gate tried_as_is++;
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate * We do at least one level of search if
2577c478bd9Sstevel@tonic-gate * - there is no dot and RES_DEFNAME is set, or
2587c478bd9Sstevel@tonic-gate * - there is at least one dot, there is no trailing dot,
2597c478bd9Sstevel@tonic-gate * and RES_DNSRCH is set.
2607c478bd9Sstevel@tonic-gate */
261*9525b14bSRao Shoaib if ((!dots && (statp->options & RES_DEFNAMES) != 0U) ||
262*9525b14bSRao Shoaib (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0U)) {
2637c478bd9Sstevel@tonic-gate int done = 0;
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate for (domain = (const char * const *)statp->dnsrch;
2667c478bd9Sstevel@tonic-gate *domain && !done;
2677c478bd9Sstevel@tonic-gate domain++) {
2687c478bd9Sstevel@tonic-gate searched = 1;
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate if (domain[0][0] == '\0' ||
2717c478bd9Sstevel@tonic-gate (domain[0][0] == '.' && domain[0][1] == '\0'))
2727c478bd9Sstevel@tonic-gate root_on_list++;
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate ret = res_nquerydomain(statp, name, *domain,
2757c478bd9Sstevel@tonic-gate class, type,
2767c478bd9Sstevel@tonic-gate answer, anslen);
2777c478bd9Sstevel@tonic-gate if (ret > 0)
2787c478bd9Sstevel@tonic-gate return (ret);
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate * If no server present, give up.
2827c478bd9Sstevel@tonic-gate * If name isn't found in this domain,
2837c478bd9Sstevel@tonic-gate * keep trying higher domains in the search list
2847c478bd9Sstevel@tonic-gate * (if that's enabled).
2857c478bd9Sstevel@tonic-gate * On a NO_DATA error, keep trying, otherwise
2867c478bd9Sstevel@tonic-gate * a wildcard entry of another type could keep us
2877c478bd9Sstevel@tonic-gate * from finding this entry higher in the domain.
2887c478bd9Sstevel@tonic-gate * If we get some other error (negative answer or
2897c478bd9Sstevel@tonic-gate * server failure), then stop searching up,
2907c478bd9Sstevel@tonic-gate * but try the input name below in case it's
2917c478bd9Sstevel@tonic-gate * fully-qualified.
2927c478bd9Sstevel@tonic-gate */
2937c478bd9Sstevel@tonic-gate if (errno == ECONNREFUSED) {
2947c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(statp, TRY_AGAIN);
2957c478bd9Sstevel@tonic-gate return (-1);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate switch (statp->res_h_errno) {
2997c478bd9Sstevel@tonic-gate case NO_DATA:
3007c478bd9Sstevel@tonic-gate got_nodata++;
3017c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
3027c478bd9Sstevel@tonic-gate case HOST_NOT_FOUND:
3037c478bd9Sstevel@tonic-gate /* keep trying */
3047c478bd9Sstevel@tonic-gate break;
3057c478bd9Sstevel@tonic-gate case TRY_AGAIN:
3067c478bd9Sstevel@tonic-gate if (hp->rcode == SERVFAIL) {
3077c478bd9Sstevel@tonic-gate /* try next search element, if any */
3087c478bd9Sstevel@tonic-gate got_servfail++;
3097c478bd9Sstevel@tonic-gate break;
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
3127c478bd9Sstevel@tonic-gate default:
3137c478bd9Sstevel@tonic-gate /* anything else implies that we're done */
3147c478bd9Sstevel@tonic-gate done++;
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate /* if we got here for some reason other than DNSRCH,
3187c478bd9Sstevel@tonic-gate * we only wanted one iteration of the loop, so stop.
3197c478bd9Sstevel@tonic-gate */
320*9525b14bSRao Shoaib if ((statp->options & RES_DNSRCH) == 0U)
3217c478bd9Sstevel@tonic-gate done++;
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate * If the query has not already been tried as is then try it
3277c478bd9Sstevel@tonic-gate * unless RES_NOTLDQUERY is set and there were no dots.
3287c478bd9Sstevel@tonic-gate */
329*9525b14bSRao Shoaib if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) &&
3307c478bd9Sstevel@tonic-gate !(tried_as_is || root_on_list)) {
3317c478bd9Sstevel@tonic-gate ret = res_nquerydomain(statp, name, NULL, class, type,
3327c478bd9Sstevel@tonic-gate answer, anslen);
3337c478bd9Sstevel@tonic-gate if (ret > 0)
3347c478bd9Sstevel@tonic-gate return (ret);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate /* if we got here, we didn't satisfy the search.
3387c478bd9Sstevel@tonic-gate * if we did an initial full query, return that query's H_ERRNO
3397c478bd9Sstevel@tonic-gate * (note that we wouldn't be here if that query had succeeded).
3407c478bd9Sstevel@tonic-gate * else if we ever got a nodata, send that back as the reason.
3417c478bd9Sstevel@tonic-gate * else send back meaningless H_ERRNO, that being the one from
3427c478bd9Sstevel@tonic-gate * the last DNSRCH we did.
3437c478bd9Sstevel@tonic-gate */
3447c478bd9Sstevel@tonic-gate if (saved_herrno != -1)
3457c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(statp, saved_herrno);
3467c478bd9Sstevel@tonic-gate else if (got_nodata)
3477c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_DATA);
3487c478bd9Sstevel@tonic-gate else if (got_servfail)
3497c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(statp, TRY_AGAIN);
3507c478bd9Sstevel@tonic-gate return (-1);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate
353*9525b14bSRao Shoaib /*%
3547c478bd9Sstevel@tonic-gate * Perform a call on res_query on the concatenation of name and domain,
3557c478bd9Sstevel@tonic-gate * removing a trailing dot from name if domain is NULL.
3567c478bd9Sstevel@tonic-gate */
3577c478bd9Sstevel@tonic-gate int
res_nquerydomain(res_state statp,const char * name,const char * domain,int class,int type,u_char * answer,int anslen)3587c478bd9Sstevel@tonic-gate res_nquerydomain(res_state statp,
3597c478bd9Sstevel@tonic-gate const char *name,
3607c478bd9Sstevel@tonic-gate const char *domain,
361*9525b14bSRao Shoaib int class, int type, /*%< class and type of query */
362*9525b14bSRao Shoaib u_char *answer, /*%< buffer to put answer */
363*9525b14bSRao Shoaib int anslen) /*%< size of answer */
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate char nbuf[MAXDNAME];
3667c478bd9Sstevel@tonic-gate const char *longname = nbuf;
3677c478bd9Sstevel@tonic-gate int n, d;
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate #ifdef DEBUG
3707c478bd9Sstevel@tonic-gate if (statp->options & RES_DEBUG)
3717c478bd9Sstevel@tonic-gate printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
3727c478bd9Sstevel@tonic-gate name, domain?domain:"<Nil>", class, type);
3737c478bd9Sstevel@tonic-gate #endif
3747c478bd9Sstevel@tonic-gate if (domain == NULL) {
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate * Check for trailing '.';
3777c478bd9Sstevel@tonic-gate * copy without '.' if present.
3787c478bd9Sstevel@tonic-gate */
3797c478bd9Sstevel@tonic-gate n = strlen(name);
3807c478bd9Sstevel@tonic-gate if (n >= MAXDNAME) {
3817c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_RECOVERY);
3827c478bd9Sstevel@tonic-gate return (-1);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate n--;
3857c478bd9Sstevel@tonic-gate if (n >= 0 && name[n] == '.') {
3867c478bd9Sstevel@tonic-gate strncpy(nbuf, name, n);
3877c478bd9Sstevel@tonic-gate nbuf[n] = '\0';
3887c478bd9Sstevel@tonic-gate } else
3897c478bd9Sstevel@tonic-gate longname = name;
3907c478bd9Sstevel@tonic-gate } else {
3917c478bd9Sstevel@tonic-gate n = strlen(name);
3927c478bd9Sstevel@tonic-gate d = strlen(domain);
3937c478bd9Sstevel@tonic-gate if (n + d + 1 >= MAXDNAME) {
3947c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_RECOVERY);
3957c478bd9Sstevel@tonic-gate return (-1);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate sprintf(nbuf, "%s.%s", name, domain);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate return (res_nquery(statp, longname, class, type, answer, anslen));
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate const char *
res_hostalias(const res_state statp,const char * name,char * dst,size_t siz)4037c478bd9Sstevel@tonic-gate res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
4047c478bd9Sstevel@tonic-gate char *file, *cp1, *cp2;
4057c478bd9Sstevel@tonic-gate char buf[BUFSIZ];
4067c478bd9Sstevel@tonic-gate FILE *fp;
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate if (statp->options & RES_NOALIASES)
4097c478bd9Sstevel@tonic-gate return (NULL);
4107c478bd9Sstevel@tonic-gate file = getenv("HOSTALIASES");
4117c478bd9Sstevel@tonic-gate if (file == NULL || (fp = fopen(file, "r")) == NULL)
4127c478bd9Sstevel@tonic-gate return (NULL);
4137c478bd9Sstevel@tonic-gate setbuf(fp, NULL);
4147c478bd9Sstevel@tonic-gate buf[sizeof(buf) - 1] = '\0';
4157c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof(buf), fp)) {
4167c478bd9Sstevel@tonic-gate for (cp1 = buf; *cp1 && !isspace((unsigned char)*cp1); ++cp1)
4177c478bd9Sstevel@tonic-gate ;
4187c478bd9Sstevel@tonic-gate if (!*cp1)
4197c478bd9Sstevel@tonic-gate break;
4207c478bd9Sstevel@tonic-gate *cp1 = '\0';
4217c478bd9Sstevel@tonic-gate if (ns_samename(buf, name) == 1) {
4227c478bd9Sstevel@tonic-gate while (isspace((unsigned char)*++cp1))
4237c478bd9Sstevel@tonic-gate ;
4247c478bd9Sstevel@tonic-gate if (!*cp1)
4257c478bd9Sstevel@tonic-gate break;
4267c478bd9Sstevel@tonic-gate for (cp2 = cp1 + 1; *cp2 &&
4277c478bd9Sstevel@tonic-gate !isspace((unsigned char)*cp2); ++cp2)
4287c478bd9Sstevel@tonic-gate ;
4297c478bd9Sstevel@tonic-gate *cp2 = '\0';
4307c478bd9Sstevel@tonic-gate strncpy(dst, cp1, siz - 1);
4317c478bd9Sstevel@tonic-gate dst[siz - 1] = '\0';
4327c478bd9Sstevel@tonic-gate fclose(fp);
4337c478bd9Sstevel@tonic-gate return (dst);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate fclose(fp);
4377c478bd9Sstevel@tonic-gate return (NULL);
4387c478bd9Sstevel@tonic-gate }
439*9525b14bSRao Shoaib
440*9525b14bSRao Shoaib /*! \file */
441