10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM * Portions Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
3*11038SRao.Shoaib@Sun.COM * Portions Copyright (C) 1996-2001, 2003 Internet Software Consortium.
4*11038SRao.Shoaib@Sun.COM *
5*11038SRao.Shoaib@Sun.COM * Permission to use, copy, modify, and/or distribute this software for any
6*11038SRao.Shoaib@Sun.COM * purpose with or without fee is hereby granted, provided that the above
7*11038SRao.Shoaib@Sun.COM * copyright notice and this permission notice appear in all copies.
8*11038SRao.Shoaib@Sun.COM *
9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*11038SRao.Shoaib@Sun.COM * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*11038SRao.Shoaib@Sun.COM * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*11038SRao.Shoaib@Sun.COM * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*11038SRao.Shoaib@Sun.COM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*11038SRao.Shoaib@Sun.COM * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*11038SRao.Shoaib@Sun.COM * PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate */
170Sstevel@tonic-gate
180Sstevel@tonic-gate /*
190Sstevel@tonic-gate * Copyright (c) 1988, 1993
200Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
21*11038SRao.Shoaib@Sun.COM *
220Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
230Sstevel@tonic-gate * modification, are permitted provided that the following conditions
240Sstevel@tonic-gate * are met:
250Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
260Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
270Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
280Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
290Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
300Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
310Sstevel@tonic-gate * must display the following acknowledgement:
320Sstevel@tonic-gate * This product includes software developed by the University of
330Sstevel@tonic-gate * California, Berkeley and its contributors.
340Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
350Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
360Sstevel@tonic-gate * without specific prior written permission.
37*11038SRao.Shoaib@Sun.COM *
380Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
390Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
400Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
410Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
420Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
430Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
440Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
450Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
460Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
470Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
480Sstevel@tonic-gate * SUCH DAMAGE.
490Sstevel@tonic-gate */
500Sstevel@tonic-gate
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate * Portions Copyright (c) 1993 by Digital Equipment Corporation.
53*11038SRao.Shoaib@Sun.COM *
540Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
550Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
560Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies, and that
570Sstevel@tonic-gate * the name of Digital Equipment Corporation not be used in advertising or
580Sstevel@tonic-gate * publicity pertaining to distribution of the document or software without
590Sstevel@tonic-gate * specific, written prior permission.
60*11038SRao.Shoaib@Sun.COM *
610Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
620Sstevel@tonic-gate * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
630Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
640Sstevel@tonic-gate * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
650Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
660Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
670Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
680Sstevel@tonic-gate * SOFTWARE.
690Sstevel@tonic-gate */
700Sstevel@tonic-gate
710Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
720Sstevel@tonic-gate static const char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93";
73*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: res_query.c,v 1.11 2008/11/14 02:36:51 marka Exp $";
740Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
750Sstevel@tonic-gate
760Sstevel@tonic-gate #include "port_before.h"
770Sstevel@tonic-gate #include <sys/types.h>
780Sstevel@tonic-gate #include <sys/param.h>
790Sstevel@tonic-gate #include <netinet/in.h>
800Sstevel@tonic-gate #include <arpa/inet.h>
810Sstevel@tonic-gate #include <arpa/nameser.h>
820Sstevel@tonic-gate #include <ctype.h>
830Sstevel@tonic-gate #include <errno.h>
840Sstevel@tonic-gate #include <netdb.h>
850Sstevel@tonic-gate #include <resolv.h>
860Sstevel@tonic-gate #include <stdio.h>
870Sstevel@tonic-gate #include <stdlib.h>
880Sstevel@tonic-gate #include <string.h>
890Sstevel@tonic-gate #include "port_after.h"
900Sstevel@tonic-gate
910Sstevel@tonic-gate /* Options. Leave them on. */
920Sstevel@tonic-gate #define DEBUG
930Sstevel@tonic-gate
940Sstevel@tonic-gate #if PACKETSZ > 1024
950Sstevel@tonic-gate #define MAXPACKET PACKETSZ
960Sstevel@tonic-gate #else
970Sstevel@tonic-gate #define MAXPACKET 1024
980Sstevel@tonic-gate #endif
990Sstevel@tonic-gate
100*11038SRao.Shoaib@Sun.COM /*%
1010Sstevel@tonic-gate * Formulate a normal query, send, and await answer.
1020Sstevel@tonic-gate * Returned answer is placed in supplied buffer "answer".
1030Sstevel@tonic-gate * Perform preliminary check of answer, returning success only
1040Sstevel@tonic-gate * if no error is indicated and the answer count is nonzero.
1050Sstevel@tonic-gate * Return the size of the response on success, -1 on error.
1060Sstevel@tonic-gate * Error number is left in H_ERRNO.
1070Sstevel@tonic-gate *
1080Sstevel@tonic-gate * Caller must parse answer and determine whether it answers the question.
1090Sstevel@tonic-gate */
1100Sstevel@tonic-gate int
res_nquery(res_state statp,const char * name,int class,int type,u_char * answer,int anslen)1110Sstevel@tonic-gate res_nquery(res_state statp,
112*11038SRao.Shoaib@Sun.COM const char *name, /*%< domain name */
113*11038SRao.Shoaib@Sun.COM int class, int type, /*%< class and type of query */
114*11038SRao.Shoaib@Sun.COM u_char *answer, /*%< buffer to put answer */
115*11038SRao.Shoaib@Sun.COM int anslen) /*%< size of answer buffer */
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate u_char buf[MAXPACKET];
1180Sstevel@tonic-gate HEADER *hp = (HEADER *) answer;
119*11038SRao.Shoaib@Sun.COM u_int oflags;
120*11038SRao.Shoaib@Sun.COM u_char *rdata;
1210Sstevel@tonic-gate int n;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate oflags = statp->_flags;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate again:
126*11038SRao.Shoaib@Sun.COM hp->rcode = NOERROR; /*%< default */
1270Sstevel@tonic-gate #ifdef DEBUG
1280Sstevel@tonic-gate if (statp->options & RES_DEBUG)
1290Sstevel@tonic-gate printf(";; res_query(%s, %d, %d)\n", name, class, type);
1300Sstevel@tonic-gate #endif
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
1330Sstevel@tonic-gate buf, sizeof(buf));
1340Sstevel@tonic-gate #ifdef RES_USE_EDNS0
1350Sstevel@tonic-gate if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 &&
136*11038SRao.Shoaib@Sun.COM (statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC|RES_NSID))) {
1370Sstevel@tonic-gate n = res_nopt(statp, n, buf, sizeof(buf), anslen);
138*11038SRao.Shoaib@Sun.COM rdata = &buf[n];
139*11038SRao.Shoaib@Sun.COM if (n > 0 && (statp->options & RES_NSID) != 0U) {
140*11038SRao.Shoaib@Sun.COM n = res_nopt_rdata(statp, n, buf, sizeof(buf), rdata,
141*11038SRao.Shoaib@Sun.COM NS_OPT_NSID, 0, NULL);
142*11038SRao.Shoaib@Sun.COM }
143*11038SRao.Shoaib@Sun.COM }
1440Sstevel@tonic-gate #endif
1450Sstevel@tonic-gate if (n <= 0) {
1460Sstevel@tonic-gate #ifdef DEBUG
1470Sstevel@tonic-gate if (statp->options & RES_DEBUG)
1480Sstevel@tonic-gate printf(";; res_query: mkquery failed\n");
1490Sstevel@tonic-gate #endif
1500Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_RECOVERY);
1510Sstevel@tonic-gate return (n);
1520Sstevel@tonic-gate }
153*11038SRao.Shoaib@Sun.COM
1540Sstevel@tonic-gate n = res_nsend(statp, buf, n, answer, anslen);
1550Sstevel@tonic-gate if (n < 0) {
1560Sstevel@tonic-gate #ifdef RES_USE_EDNS0
1570Sstevel@tonic-gate /* if the query choked with EDNS0, retry without EDNS0 */
158*11038SRao.Shoaib@Sun.COM if ((statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U &&
1590Sstevel@tonic-gate ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
1600Sstevel@tonic-gate statp->_flags |= RES_F_EDNS0ERR;
1610Sstevel@tonic-gate if (statp->options & RES_DEBUG)
1620Sstevel@tonic-gate printf(";; res_nquery: retry without EDNS0\n");
1630Sstevel@tonic-gate goto again;
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate #endif
1660Sstevel@tonic-gate #ifdef DEBUG
1670Sstevel@tonic-gate if (statp->options & RES_DEBUG)
1680Sstevel@tonic-gate printf(";; res_query: send error\n");
1690Sstevel@tonic-gate #endif
1700Sstevel@tonic-gate RES_SET_H_ERRNO(statp, TRY_AGAIN);
1710Sstevel@tonic-gate return (n);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1750Sstevel@tonic-gate #ifdef DEBUG
1760Sstevel@tonic-gate if (statp->options & RES_DEBUG)
1770Sstevel@tonic-gate printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n",
1780Sstevel@tonic-gate p_rcode(hp->rcode),
1790Sstevel@tonic-gate ntohs(hp->ancount),
1800Sstevel@tonic-gate ntohs(hp->nscount),
1810Sstevel@tonic-gate ntohs(hp->arcount));
1820Sstevel@tonic-gate #endif
1830Sstevel@tonic-gate switch (hp->rcode) {
1840Sstevel@tonic-gate case NXDOMAIN:
1850Sstevel@tonic-gate RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
1860Sstevel@tonic-gate break;
1870Sstevel@tonic-gate case SERVFAIL:
1880Sstevel@tonic-gate RES_SET_H_ERRNO(statp, TRY_AGAIN);
1890Sstevel@tonic-gate break;
1900Sstevel@tonic-gate case NOERROR:
1910Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_DATA);
1920Sstevel@tonic-gate break;
1930Sstevel@tonic-gate case FORMERR:
1940Sstevel@tonic-gate case NOTIMP:
1950Sstevel@tonic-gate case REFUSED:
1960Sstevel@tonic-gate default:
1970Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_RECOVERY);
1980Sstevel@tonic-gate break;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate return (-1);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate return (n);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
205*11038SRao.Shoaib@Sun.COM /*%
2060Sstevel@tonic-gate * Formulate a normal query, send, and retrieve answer in supplied buffer.
2070Sstevel@tonic-gate * Return the size of the response on success, -1 on error.
2080Sstevel@tonic-gate * If enabled, implement search rules until answer or unrecoverable failure
2090Sstevel@tonic-gate * is detected. Error code, if any, is left in H_ERRNO.
2100Sstevel@tonic-gate */
2110Sstevel@tonic-gate int
res_nsearch(res_state statp,const char * name,int class,int type,u_char * answer,int anslen)2120Sstevel@tonic-gate res_nsearch(res_state statp,
213*11038SRao.Shoaib@Sun.COM const char *name, /*%< domain name */
214*11038SRao.Shoaib@Sun.COM int class, int type, /*%< class and type of query */
215*11038SRao.Shoaib@Sun.COM u_char *answer, /*%< buffer to put answer */
216*11038SRao.Shoaib@Sun.COM int anslen) /*%< size of answer */
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate const char *cp, * const *domain;
2190Sstevel@tonic-gate HEADER *hp = (HEADER *) answer;
2200Sstevel@tonic-gate char tmp[NS_MAXDNAME];
2210Sstevel@tonic-gate u_int dots;
2220Sstevel@tonic-gate int trailing_dot, ret, saved_herrno;
2230Sstevel@tonic-gate int got_nodata = 0, got_servfail = 0, root_on_list = 0;
2240Sstevel@tonic-gate int tried_as_is = 0;
2250Sstevel@tonic-gate int searched = 0;
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate errno = 0;
228*11038SRao.Shoaib@Sun.COM RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /*%< True if we never query. */
2290Sstevel@tonic-gate dots = 0;
2300Sstevel@tonic-gate for (cp = name; *cp != '\0'; cp++)
2310Sstevel@tonic-gate dots += (*cp == '.');
2320Sstevel@tonic-gate trailing_dot = 0;
2330Sstevel@tonic-gate if (cp > name && *--cp == '.')
2340Sstevel@tonic-gate trailing_dot++;
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate /* If there aren't any dots, it could be a user-level alias. */
2370Sstevel@tonic-gate if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
2380Sstevel@tonic-gate return (res_nquery(statp, cp, class, type, answer, anslen));
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate * If there are enough dots in the name, let's just give it a
2420Sstevel@tonic-gate * try 'as is'. The threshold can be set with the "ndots" option.
2430Sstevel@tonic-gate * Also, query 'as is', if there is a trailing dot in the name.
2440Sstevel@tonic-gate */
2450Sstevel@tonic-gate saved_herrno = -1;
2460Sstevel@tonic-gate if (dots >= statp->ndots || trailing_dot) {
2470Sstevel@tonic-gate ret = res_nquerydomain(statp, name, NULL, class, type,
2480Sstevel@tonic-gate answer, anslen);
2490Sstevel@tonic-gate if (ret > 0 || trailing_dot)
2500Sstevel@tonic-gate return (ret);
2510Sstevel@tonic-gate saved_herrno = statp->res_h_errno;
2520Sstevel@tonic-gate tried_as_is++;
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate * We do at least one level of search if
2570Sstevel@tonic-gate * - there is no dot and RES_DEFNAME is set, or
2580Sstevel@tonic-gate * - there is at least one dot, there is no trailing dot,
2590Sstevel@tonic-gate * and RES_DNSRCH is set.
2600Sstevel@tonic-gate */
261*11038SRao.Shoaib@Sun.COM if ((!dots && (statp->options & RES_DEFNAMES) != 0U) ||
262*11038SRao.Shoaib@Sun.COM (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0U)) {
2630Sstevel@tonic-gate int done = 0;
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate for (domain = (const char * const *)statp->dnsrch;
2660Sstevel@tonic-gate *domain && !done;
2670Sstevel@tonic-gate domain++) {
2680Sstevel@tonic-gate searched = 1;
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate if (domain[0][0] == '\0' ||
2710Sstevel@tonic-gate (domain[0][0] == '.' && domain[0][1] == '\0'))
2720Sstevel@tonic-gate root_on_list++;
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate ret = res_nquerydomain(statp, name, *domain,
2750Sstevel@tonic-gate class, type,
2760Sstevel@tonic-gate answer, anslen);
2770Sstevel@tonic-gate if (ret > 0)
2780Sstevel@tonic-gate return (ret);
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate * If no server present, give up.
2820Sstevel@tonic-gate * If name isn't found in this domain,
2830Sstevel@tonic-gate * keep trying higher domains in the search list
2840Sstevel@tonic-gate * (if that's enabled).
2850Sstevel@tonic-gate * On a NO_DATA error, keep trying, otherwise
2860Sstevel@tonic-gate * a wildcard entry of another type could keep us
2870Sstevel@tonic-gate * from finding this entry higher in the domain.
2880Sstevel@tonic-gate * If we get some other error (negative answer or
2890Sstevel@tonic-gate * server failure), then stop searching up,
2900Sstevel@tonic-gate * but try the input name below in case it's
2910Sstevel@tonic-gate * fully-qualified.
2920Sstevel@tonic-gate */
2930Sstevel@tonic-gate if (errno == ECONNREFUSED) {
2940Sstevel@tonic-gate RES_SET_H_ERRNO(statp, TRY_AGAIN);
2950Sstevel@tonic-gate return (-1);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate switch (statp->res_h_errno) {
2990Sstevel@tonic-gate case NO_DATA:
3000Sstevel@tonic-gate got_nodata++;
3010Sstevel@tonic-gate /* FALLTHROUGH */
3020Sstevel@tonic-gate case HOST_NOT_FOUND:
3030Sstevel@tonic-gate /* keep trying */
3040Sstevel@tonic-gate break;
3050Sstevel@tonic-gate case TRY_AGAIN:
3060Sstevel@tonic-gate if (hp->rcode == SERVFAIL) {
3070Sstevel@tonic-gate /* try next search element, if any */
3080Sstevel@tonic-gate got_servfail++;
3090Sstevel@tonic-gate break;
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate /* FALLTHROUGH */
3120Sstevel@tonic-gate default:
3130Sstevel@tonic-gate /* anything else implies that we're done */
3140Sstevel@tonic-gate done++;
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate /* if we got here for some reason other than DNSRCH,
3180Sstevel@tonic-gate * we only wanted one iteration of the loop, so stop.
3190Sstevel@tonic-gate */
320*11038SRao.Shoaib@Sun.COM if ((statp->options & RES_DNSRCH) == 0U)
3210Sstevel@tonic-gate done++;
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate * If the query has not already been tried as is then try it
3270Sstevel@tonic-gate * unless RES_NOTLDQUERY is set and there were no dots.
3280Sstevel@tonic-gate */
329*11038SRao.Shoaib@Sun.COM if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) &&
3300Sstevel@tonic-gate !(tried_as_is || root_on_list)) {
3310Sstevel@tonic-gate ret = res_nquerydomain(statp, name, NULL, class, type,
3320Sstevel@tonic-gate answer, anslen);
3330Sstevel@tonic-gate if (ret > 0)
3340Sstevel@tonic-gate return (ret);
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate /* if we got here, we didn't satisfy the search.
3380Sstevel@tonic-gate * if we did an initial full query, return that query's H_ERRNO
3390Sstevel@tonic-gate * (note that we wouldn't be here if that query had succeeded).
3400Sstevel@tonic-gate * else if we ever got a nodata, send that back as the reason.
3410Sstevel@tonic-gate * else send back meaningless H_ERRNO, that being the one from
3420Sstevel@tonic-gate * the last DNSRCH we did.
3430Sstevel@tonic-gate */
3440Sstevel@tonic-gate if (saved_herrno != -1)
3450Sstevel@tonic-gate RES_SET_H_ERRNO(statp, saved_herrno);
3460Sstevel@tonic-gate else if (got_nodata)
3470Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_DATA);
3480Sstevel@tonic-gate else if (got_servfail)
3490Sstevel@tonic-gate RES_SET_H_ERRNO(statp, TRY_AGAIN);
3500Sstevel@tonic-gate return (-1);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate
353*11038SRao.Shoaib@Sun.COM /*%
3540Sstevel@tonic-gate * Perform a call on res_query on the concatenation of name and domain,
3550Sstevel@tonic-gate * removing a trailing dot from name if domain is NULL.
3560Sstevel@tonic-gate */
3570Sstevel@tonic-gate int
res_nquerydomain(res_state statp,const char * name,const char * domain,int class,int type,u_char * answer,int anslen)3580Sstevel@tonic-gate res_nquerydomain(res_state statp,
3590Sstevel@tonic-gate const char *name,
3600Sstevel@tonic-gate const char *domain,
361*11038SRao.Shoaib@Sun.COM int class, int type, /*%< class and type of query */
362*11038SRao.Shoaib@Sun.COM u_char *answer, /*%< buffer to put answer */
363*11038SRao.Shoaib@Sun.COM int anslen) /*%< size of answer */
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate char nbuf[MAXDNAME];
3660Sstevel@tonic-gate const char *longname = nbuf;
3670Sstevel@tonic-gate int n, d;
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate #ifdef DEBUG
3700Sstevel@tonic-gate if (statp->options & RES_DEBUG)
3710Sstevel@tonic-gate printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
3720Sstevel@tonic-gate name, domain?domain:"<Nil>", class, type);
3730Sstevel@tonic-gate #endif
3740Sstevel@tonic-gate if (domain == NULL) {
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate * Check for trailing '.';
3770Sstevel@tonic-gate * copy without '.' if present.
3780Sstevel@tonic-gate */
3790Sstevel@tonic-gate n = strlen(name);
3800Sstevel@tonic-gate if (n >= MAXDNAME) {
3810Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_RECOVERY);
3820Sstevel@tonic-gate return (-1);
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate n--;
3850Sstevel@tonic-gate if (n >= 0 && name[n] == '.') {
3860Sstevel@tonic-gate strncpy(nbuf, name, n);
3870Sstevel@tonic-gate nbuf[n] = '\0';
3880Sstevel@tonic-gate } else
3890Sstevel@tonic-gate longname = name;
3900Sstevel@tonic-gate } else {
3910Sstevel@tonic-gate n = strlen(name);
3920Sstevel@tonic-gate d = strlen(domain);
3930Sstevel@tonic-gate if (n + d + 1 >= MAXDNAME) {
3940Sstevel@tonic-gate RES_SET_H_ERRNO(statp, NO_RECOVERY);
3950Sstevel@tonic-gate return (-1);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate sprintf(nbuf, "%s.%s", name, domain);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate return (res_nquery(statp, longname, class, type, answer, anslen));
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate const char *
res_hostalias(const res_state statp,const char * name,char * dst,size_t siz)4030Sstevel@tonic-gate res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
4040Sstevel@tonic-gate char *file, *cp1, *cp2;
4050Sstevel@tonic-gate char buf[BUFSIZ];
4060Sstevel@tonic-gate FILE *fp;
4070Sstevel@tonic-gate
4080Sstevel@tonic-gate if (statp->options & RES_NOALIASES)
4090Sstevel@tonic-gate return (NULL);
4100Sstevel@tonic-gate file = getenv("HOSTALIASES");
411*11038SRao.Shoaib@Sun.COM if (file == NULL || (fp = fopen(file, "r")) == NULL)
4120Sstevel@tonic-gate return (NULL);
4130Sstevel@tonic-gate setbuf(fp, NULL);
4140Sstevel@tonic-gate buf[sizeof(buf) - 1] = '\0';
4150Sstevel@tonic-gate while (fgets(buf, sizeof(buf), fp)) {
4160Sstevel@tonic-gate for (cp1 = buf; *cp1 && !isspace((unsigned char)*cp1); ++cp1)
4170Sstevel@tonic-gate ;
4180Sstevel@tonic-gate if (!*cp1)
4190Sstevel@tonic-gate break;
4200Sstevel@tonic-gate *cp1 = '\0';
4210Sstevel@tonic-gate if (ns_samename(buf, name) == 1) {
4220Sstevel@tonic-gate while (isspace((unsigned char)*++cp1))
4230Sstevel@tonic-gate ;
4240Sstevel@tonic-gate if (!*cp1)
4250Sstevel@tonic-gate break;
4260Sstevel@tonic-gate for (cp2 = cp1 + 1; *cp2 &&
4270Sstevel@tonic-gate !isspace((unsigned char)*cp2); ++cp2)
4280Sstevel@tonic-gate ;
4290Sstevel@tonic-gate *cp2 = '\0';
4300Sstevel@tonic-gate strncpy(dst, cp1, siz - 1);
4310Sstevel@tonic-gate dst[siz - 1] = '\0';
4320Sstevel@tonic-gate fclose(fp);
4330Sstevel@tonic-gate return (dst);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate fclose(fp);
4370Sstevel@tonic-gate return (NULL);
4380Sstevel@tonic-gate }
439*11038SRao.Shoaib@Sun.COM
440*11038SRao.Shoaib@Sun.COM /*! \file */
441