xref: /onnv-gate/usr/src/lib/libresolv2/common/resolv/res_findzonecut.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate #if !defined(lint) && !defined(SABER)
2*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: res_findzonecut.c,v 1.10 2005/10/11 00:10:16 marka Exp $";
30Sstevel@tonic-gate #endif /* not lint */
40Sstevel@tonic-gate 
50Sstevel@tonic-gate /*
6*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
70Sstevel@tonic-gate  * Copyright (c) 1999 by Internet Software Consortium.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
100Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
110Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
120Sstevel@tonic-gate  *
13*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
16*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /* Import. */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include "port_before.h"
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/socket.h>
280Sstevel@tonic-gate #include <sys/time.h>
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <netinet/in.h>
310Sstevel@tonic-gate #include <arpa/inet.h>
320Sstevel@tonic-gate #include <arpa/nameser.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <limits.h>
360Sstevel@tonic-gate #include <netdb.h>
370Sstevel@tonic-gate #include <stdarg.h>
380Sstevel@tonic-gate #include <stdio.h>
390Sstevel@tonic-gate #include <stdlib.h>
400Sstevel@tonic-gate #include <string.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <isc/list.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include "port_after.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <resolv.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /* Data structures. */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate typedef struct rr_a {
510Sstevel@tonic-gate 	LINK(struct rr_a)	link;
520Sstevel@tonic-gate 	union res_sockaddr_union addr;
530Sstevel@tonic-gate } rr_a;
540Sstevel@tonic-gate typedef LIST(rr_a) rrset_a;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate typedef struct rr_ns {
570Sstevel@tonic-gate 	LINK(struct rr_ns)	link;
580Sstevel@tonic-gate 	const char *		name;
590Sstevel@tonic-gate 	unsigned int		flags;
600Sstevel@tonic-gate 	rrset_a			addrs;
610Sstevel@tonic-gate } rr_ns;
620Sstevel@tonic-gate typedef LIST(rr_ns) rrset_ns;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #define	RR_NS_HAVE_V4		0x01
650Sstevel@tonic-gate #define	RR_NS_HAVE_V6		0x02
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /* Forward. */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static int	satisfy(res_state, const char *, rrset_ns *,
700Sstevel@tonic-gate 			union res_sockaddr_union *, int);
710Sstevel@tonic-gate static int	add_addrs(res_state, rr_ns *,
720Sstevel@tonic-gate 			  union res_sockaddr_union *, int);
730Sstevel@tonic-gate static int	get_soa(res_state, const char *, ns_class, int,
740Sstevel@tonic-gate 			char *, size_t, char *, size_t,
750Sstevel@tonic-gate 			rrset_ns *);
760Sstevel@tonic-gate static int	get_ns(res_state, const char *, ns_class, int, rrset_ns *);
770Sstevel@tonic-gate static int	get_glue(res_state, ns_class, int, rrset_ns *);
780Sstevel@tonic-gate static int	save_ns(res_state, ns_msg *, ns_sect,
790Sstevel@tonic-gate 			const char *, ns_class, int, rrset_ns *);
800Sstevel@tonic-gate static int	save_a(res_state, ns_msg *, ns_sect,
810Sstevel@tonic-gate 		       const char *, ns_class, int, rr_ns *);
820Sstevel@tonic-gate static void	free_nsrrset(rrset_ns *);
830Sstevel@tonic-gate static void	free_nsrr(rrset_ns *, rr_ns *);
840Sstevel@tonic-gate static rr_ns *	find_ns(rrset_ns *, const char *);
850Sstevel@tonic-gate static int	do_query(res_state, const char *, ns_class, ns_type,
860Sstevel@tonic-gate 			 u_char *, ns_msg *);
870Sstevel@tonic-gate static void	res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /* Macros. */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #define DPRINTF(x) do {\
920Sstevel@tonic-gate 		int save_errno = errno; \
93*11038SRao.Shoaib@Sun.COM 		if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \
940Sstevel@tonic-gate 		errno = save_errno; \
950Sstevel@tonic-gate 	} while (0)
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /* Public. */
980Sstevel@tonic-gate 
99*11038SRao.Shoaib@Sun.COM /*%
1000Sstevel@tonic-gate  *	find enclosing zone for a <dname,class>, and some server addresses
101*11038SRao.Shoaib@Sun.COM  *
1020Sstevel@tonic-gate  * parameters:
103*11038SRao.Shoaib@Sun.COM  *\li	res - resolver context to work within (is modified)
104*11038SRao.Shoaib@Sun.COM  *\li	dname - domain name whose enclosing zone is desired
105*11038SRao.Shoaib@Sun.COM  *\li	class - class of dname (and its enclosing zone)
106*11038SRao.Shoaib@Sun.COM  *\li	zname - found zone name
107*11038SRao.Shoaib@Sun.COM  *\li	zsize - allocated size of zname
108*11038SRao.Shoaib@Sun.COM  *\li	addrs - found server addresses
109*11038SRao.Shoaib@Sun.COM  *\li	naddrs - max number of addrs
110*11038SRao.Shoaib@Sun.COM  *
1110Sstevel@tonic-gate  * return values:
112*11038SRao.Shoaib@Sun.COM  *\li	< 0 - an error occurred (check errno)
113*11038SRao.Shoaib@Sun.COM  *\li	= 0 - zname is now valid, but addrs[] wasn't changed
114*11038SRao.Shoaib@Sun.COM  *\li	> 0 - zname is now valid, and return value is number of addrs[] found
115*11038SRao.Shoaib@Sun.COM  *
1160Sstevel@tonic-gate  * notes:
117*11038SRao.Shoaib@Sun.COM  *\li	this function calls res_nsend() which means it depends on correctly
1180Sstevel@tonic-gate  *	functioning recursive nameservers (usually defined in /etc/resolv.conf
1190Sstevel@tonic-gate  *	or its local equivilent).
1200Sstevel@tonic-gate  *
121*11038SRao.Shoaib@Sun.COM  *\li	we start by asking for an SOA<dname,class>.  if we get one as an
1220Sstevel@tonic-gate  *	answer, that just means <dname,class> is a zone top, which is fine.
1230Sstevel@tonic-gate  *	more than likely we'll be told to go pound sand, in the form of a
1240Sstevel@tonic-gate  *	negative answer.
1250Sstevel@tonic-gate  *
126*11038SRao.Shoaib@Sun.COM  *\li	note that we are not prepared to deal with referrals since that would
1270Sstevel@tonic-gate  *	only come from authority servers and our correctly functioning local
1280Sstevel@tonic-gate  *	recursive server would have followed the referral and got us something
1290Sstevel@tonic-gate  *	more definite.
1300Sstevel@tonic-gate  *
131*11038SRao.Shoaib@Sun.COM  *\li	if the authority section contains an SOA, this SOA should also be the
1320Sstevel@tonic-gate  *	closest enclosing zone, since any intermediary zone cuts would've been
1330Sstevel@tonic-gate  *	returned as referrals and dealt with by our correctly functioning local
1340Sstevel@tonic-gate  *	recursive name server.  but an SOA in the authority section should NOT
1350Sstevel@tonic-gate  *	match our dname (since that would have been returned in the answer
1360Sstevel@tonic-gate  *	section).  an authority section SOA has to be "above" our dname.
1370Sstevel@tonic-gate  *
138*11038SRao.Shoaib@Sun.COM  *\li	however, since authority section SOA's were once optional, it's
1390Sstevel@tonic-gate  *	possible that we'll have to go hunting for the enclosing SOA by
1400Sstevel@tonic-gate  *	ripping labels off the front of our dname -- this is known as "doing
1410Sstevel@tonic-gate  *	it the hard way."
1420Sstevel@tonic-gate  *
143*11038SRao.Shoaib@Sun.COM  *\li	ultimately we want some server addresses, which are ideally the ones
1440Sstevel@tonic-gate  *	pertaining to the SOA.MNAME, but only if there is a matching NS RR.
1450Sstevel@tonic-gate  *	so the second phase (after we find an SOA) is to go looking for the
1460Sstevel@tonic-gate  *	NS RRset for that SOA's zone.
1470Sstevel@tonic-gate  *
148*11038SRao.Shoaib@Sun.COM  *\li	no answer section processed by this code is allowed to contain CNAME
1490Sstevel@tonic-gate  *	or DNAME RR's.  for the SOA query this means we strip a label and
1500Sstevel@tonic-gate  *	keep going.  for the NS and A queries this means we just give up.
1510Sstevel@tonic-gate  */
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate int
res_findzonecut(res_state statp,const char * dname,ns_class class,int opts,char * zname,size_t zsize,struct in_addr * addrs,int naddrs)1540Sstevel@tonic-gate res_findzonecut(res_state statp, const char *dname, ns_class class, int opts,
1550Sstevel@tonic-gate 		char *zname, size_t zsize, struct in_addr *addrs, int naddrs)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	int result, i;
1580Sstevel@tonic-gate 	union res_sockaddr_union *u;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	opts |= RES_IPV4ONLY;
1620Sstevel@tonic-gate 	opts &= ~RES_IPV6ONLY;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	u = calloc(naddrs, sizeof(*u));
1650Sstevel@tonic-gate 	if (u == NULL)
1660Sstevel@tonic-gate 		return(-1);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	result = res_findzonecut2(statp, dname, class, opts, zname, zsize,
1690Sstevel@tonic-gate 				  u, naddrs);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	for (i = 0; i < result; i++) {
1720Sstevel@tonic-gate 		addrs[i] = u[i].sin.sin_addr;
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 	free(u);
1750Sstevel@tonic-gate 	return (result);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate int
res_findzonecut2(res_state statp,const char * dname,ns_class class,int opts,char * zname,size_t zsize,union res_sockaddr_union * addrs,int naddrs)1790Sstevel@tonic-gate res_findzonecut2(res_state statp, const char *dname, ns_class class, int opts,
1800Sstevel@tonic-gate 		 char *zname, size_t zsize, union res_sockaddr_union *addrs,
1810Sstevel@tonic-gate 		 int naddrs)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate 	char mname[NS_MAXDNAME];
1840Sstevel@tonic-gate 	u_long save_pfcode;
1850Sstevel@tonic-gate 	rrset_ns nsrrs;
1860Sstevel@tonic-gate 	int n;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	DPRINTF(("START dname='%s' class=%s, zsize=%ld, naddrs=%d",
1890Sstevel@tonic-gate 		 dname, p_class(class), (long)zsize, naddrs));
1900Sstevel@tonic-gate 	save_pfcode = statp->pfcode;
1910Sstevel@tonic-gate 	statp->pfcode |= RES_PRF_HEAD2 | RES_PRF_HEAD1 | RES_PRF_HEADX |
1920Sstevel@tonic-gate 			 RES_PRF_QUES | RES_PRF_ANS |
1930Sstevel@tonic-gate 			 RES_PRF_AUTH | RES_PRF_ADD;
1940Sstevel@tonic-gate 	INIT_LIST(nsrrs);
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	DPRINTF(("get the soa, and see if it has enough glue"));
1970Sstevel@tonic-gate 	if ((n = get_soa(statp, dname, class, opts, zname, zsize,
1980Sstevel@tonic-gate 			 mname, sizeof mname, &nsrrs)) < 0 ||
1990Sstevel@tonic-gate 	    ((opts & RES_EXHAUSTIVE) == 0 &&
2000Sstevel@tonic-gate 	     (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
2010Sstevel@tonic-gate 		goto done;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	DPRINTF(("get the ns rrset and see if it has enough glue"));
2040Sstevel@tonic-gate 	if ((n = get_ns(statp, zname, class, opts, &nsrrs)) < 0 ||
2050Sstevel@tonic-gate 	    ((opts & RES_EXHAUSTIVE) == 0 &&
2060Sstevel@tonic-gate 	     (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
2070Sstevel@tonic-gate 		goto done;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	DPRINTF(("get the missing glue and see if it's finally enough"));
2100Sstevel@tonic-gate 	if ((n = get_glue(statp, class, opts, &nsrrs)) >= 0)
2110Sstevel@tonic-gate 		n = satisfy(statp, mname, &nsrrs, addrs, naddrs);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate  done:
2140Sstevel@tonic-gate 	DPRINTF(("FINISH n=%d (%s)", n, (n < 0) ? strerror(errno) : "OK"));
2150Sstevel@tonic-gate 	free_nsrrset(&nsrrs);
2160Sstevel@tonic-gate 	statp->pfcode = save_pfcode;
2170Sstevel@tonic-gate 	return (n);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /* Private. */
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate static int
satisfy(res_state statp,const char * mname,rrset_ns * nsrrsp,union res_sockaddr_union * addrs,int naddrs)2230Sstevel@tonic-gate satisfy(res_state statp, const char *mname, rrset_ns *nsrrsp,
2240Sstevel@tonic-gate 	union res_sockaddr_union *addrs, int naddrs)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	rr_ns *nsrr;
2270Sstevel@tonic-gate 	int n, x;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	n = 0;
2300Sstevel@tonic-gate 	nsrr = find_ns(nsrrsp, mname);
2310Sstevel@tonic-gate 	if (nsrr != NULL) {
2320Sstevel@tonic-gate 		x = add_addrs(statp, nsrr, addrs, naddrs);
2330Sstevel@tonic-gate 		addrs += x;
2340Sstevel@tonic-gate 		naddrs -= x;
2350Sstevel@tonic-gate 		n += x;
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 	for (nsrr = HEAD(*nsrrsp);
2380Sstevel@tonic-gate 	     nsrr != NULL && naddrs > 0;
2390Sstevel@tonic-gate 	     nsrr = NEXT(nsrr, link))
2400Sstevel@tonic-gate 		if (ns_samename(nsrr->name, mname) != 1) {
2410Sstevel@tonic-gate 			x = add_addrs(statp, nsrr, addrs, naddrs);
2420Sstevel@tonic-gate 			addrs += x;
2430Sstevel@tonic-gate 			naddrs -= x;
2440Sstevel@tonic-gate 			n += x;
2450Sstevel@tonic-gate 		}
2460Sstevel@tonic-gate 	DPRINTF(("satisfy(%s): %d", mname, n));
2470Sstevel@tonic-gate 	return (n);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate static int
add_addrs(res_state statp,rr_ns * nsrr,union res_sockaddr_union * addrs,int naddrs)2510Sstevel@tonic-gate add_addrs(res_state statp, rr_ns *nsrr,
2520Sstevel@tonic-gate 	  union res_sockaddr_union *addrs, int naddrs)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	rr_a *arr;
2550Sstevel@tonic-gate 	int n = 0;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	for (arr = HEAD(nsrr->addrs); arr != NULL; arr = NEXT(arr, link)) {
2580Sstevel@tonic-gate 		if (naddrs <= 0)
2590Sstevel@tonic-gate 			return (0);
2600Sstevel@tonic-gate 		*addrs++ = arr->addr;
2610Sstevel@tonic-gate 		naddrs--;
2620Sstevel@tonic-gate 		n++;
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 	DPRINTF(("add_addrs: %d", n));
2650Sstevel@tonic-gate 	return (n);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate static int
get_soa(res_state statp,const char * dname,ns_class class,int opts,char * zname,size_t zsize,char * mname,size_t msize,rrset_ns * nsrrsp)2690Sstevel@tonic-gate get_soa(res_state statp, const char *dname, ns_class class, int opts,
2700Sstevel@tonic-gate 	char *zname, size_t zsize, char *mname, size_t msize,
2710Sstevel@tonic-gate 	rrset_ns *nsrrsp)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	char tname[NS_MAXDNAME];
2740Sstevel@tonic-gate 	u_char *resp = NULL;
2750Sstevel@tonic-gate 	int n, i, ancount, nscount;
2760Sstevel@tonic-gate 	ns_sect sect;
2770Sstevel@tonic-gate 	ns_msg msg;
2780Sstevel@tonic-gate 	u_int rcode;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	/*
2810Sstevel@tonic-gate 	 * Find closest enclosing SOA, even if it's for the root zone.
2820Sstevel@tonic-gate 	 */
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	/* First canonicalize dname (exactly one unescaped trailing "."). */
2850Sstevel@tonic-gate 	if (ns_makecanon(dname, tname, sizeof tname) < 0)
2860Sstevel@tonic-gate 		goto cleanup;
2870Sstevel@tonic-gate 	dname = tname;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	resp = malloc(NS_MAXMSG);
2900Sstevel@tonic-gate 	if (resp == NULL)
2910Sstevel@tonic-gate 		goto cleanup;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	/* Now grovel the subdomains, hunting for an SOA answer or auth. */
2940Sstevel@tonic-gate 	for (;;) {
2950Sstevel@tonic-gate 		/* Leading or inter-label '.' are skipped here. */
2960Sstevel@tonic-gate 		while (*dname == '.')
2970Sstevel@tonic-gate 			dname++;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		/* Is there an SOA? */
3000Sstevel@tonic-gate 		n = do_query(statp, dname, class, ns_t_soa, resp, &msg);
3010Sstevel@tonic-gate 		if (n < 0) {
3020Sstevel@tonic-gate 			DPRINTF(("get_soa: do_query('%s', %s) failed (%d)",
3030Sstevel@tonic-gate 				 dname, p_class(class), n));
3040Sstevel@tonic-gate 			goto cleanup;
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 		if (n > 0) {
3070Sstevel@tonic-gate 			DPRINTF(("get_soa: CNAME or DNAME found"));
3080Sstevel@tonic-gate 			sect = ns_s_max, n = 0;
3090Sstevel@tonic-gate 		} else {
3100Sstevel@tonic-gate 			rcode = ns_msg_getflag(msg, ns_f_rcode);
3110Sstevel@tonic-gate 			ancount = ns_msg_count(msg, ns_s_an);
3120Sstevel@tonic-gate 			nscount = ns_msg_count(msg, ns_s_ns);
3130Sstevel@tonic-gate 			if (ancount > 0 && rcode == ns_r_noerror)
3140Sstevel@tonic-gate 				sect = ns_s_an, n = ancount;
3150Sstevel@tonic-gate 			else if (nscount > 0)
3160Sstevel@tonic-gate 				sect = ns_s_ns, n = nscount;
3170Sstevel@tonic-gate 			else
3180Sstevel@tonic-gate 				sect = ns_s_max, n = 0;
3190Sstevel@tonic-gate 		}
3200Sstevel@tonic-gate 		for (i = 0; i < n; i++) {
3210Sstevel@tonic-gate 			const char *t;
3220Sstevel@tonic-gate 			const u_char *rdata;
3230Sstevel@tonic-gate 			ns_rr rr;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 			if (ns_parserr(&msg, sect, i, &rr) < 0) {
3260Sstevel@tonic-gate 				DPRINTF(("get_soa: ns_parserr(%s, %d) failed",
3270Sstevel@tonic-gate 					 p_section(sect, ns_o_query), i));
3280Sstevel@tonic-gate 				goto cleanup;
3290Sstevel@tonic-gate 			}
3300Sstevel@tonic-gate 			if (ns_rr_type(rr) == ns_t_cname ||
3310Sstevel@tonic-gate 			    ns_rr_type(rr) == ns_t_dname)
3320Sstevel@tonic-gate 				break;
3330Sstevel@tonic-gate 			if (ns_rr_type(rr) != ns_t_soa ||
3340Sstevel@tonic-gate 			    ns_rr_class(rr) != class)
3350Sstevel@tonic-gate 				continue;
3360Sstevel@tonic-gate 			t = ns_rr_name(rr);
3370Sstevel@tonic-gate 			switch (sect) {
3380Sstevel@tonic-gate 			case ns_s_an:
3390Sstevel@tonic-gate 				if (ns_samedomain(dname, t) == 0) {
3400Sstevel@tonic-gate 					DPRINTF(
3410Sstevel@tonic-gate 				    ("get_soa: ns_samedomain('%s', '%s') == 0",
3420Sstevel@tonic-gate 						dname, t)
3430Sstevel@tonic-gate 						);
3440Sstevel@tonic-gate 					errno = EPROTOTYPE;
3450Sstevel@tonic-gate 					goto cleanup;
3460Sstevel@tonic-gate 				}
3470Sstevel@tonic-gate 				break;
3480Sstevel@tonic-gate 			case ns_s_ns:
3490Sstevel@tonic-gate 				if (ns_samename(dname, t) == 1 ||
3500Sstevel@tonic-gate 				    ns_samedomain(dname, t) == 0) {
3510Sstevel@tonic-gate 					DPRINTF(
3520Sstevel@tonic-gate 		       ("get_soa: ns_samename() || !ns_samedomain('%s', '%s')",
3530Sstevel@tonic-gate 						dname, t)
3540Sstevel@tonic-gate 						);
3550Sstevel@tonic-gate 					errno = EPROTOTYPE;
3560Sstevel@tonic-gate 					goto cleanup;
3570Sstevel@tonic-gate 				}
3580Sstevel@tonic-gate 				break;
3590Sstevel@tonic-gate 			default:
3600Sstevel@tonic-gate 				abort();
3610Sstevel@tonic-gate 			}
3620Sstevel@tonic-gate 			if (strlen(t) + 1 > zsize) {
363*11038SRao.Shoaib@Sun.COM 				DPRINTF(("get_soa: zname(%lu) too small (%lu)",
364*11038SRao.Shoaib@Sun.COM 					 (unsigned long)zsize,
365*11038SRao.Shoaib@Sun.COM 					 (unsigned long)strlen(t) + 1));
3660Sstevel@tonic-gate 				errno = EMSGSIZE;
3670Sstevel@tonic-gate 				goto cleanup;
3680Sstevel@tonic-gate 			}
3690Sstevel@tonic-gate 			strcpy(zname, t);
3700Sstevel@tonic-gate 			rdata = ns_rr_rdata(rr);
3710Sstevel@tonic-gate 			if (ns_name_uncompress(resp, ns_msg_end(msg), rdata,
3720Sstevel@tonic-gate 					       mname, msize) < 0) {
3730Sstevel@tonic-gate 				DPRINTF(("get_soa: ns_name_uncompress failed")
3740Sstevel@tonic-gate 					);
3750Sstevel@tonic-gate 				goto cleanup;
3760Sstevel@tonic-gate 			}
3770Sstevel@tonic-gate 			if (save_ns(statp, &msg, ns_s_ns,
3780Sstevel@tonic-gate 				    zname, class, opts, nsrrsp) < 0) {
3790Sstevel@tonic-gate 				DPRINTF(("get_soa: save_ns failed"));
3800Sstevel@tonic-gate 				goto cleanup;
3810Sstevel@tonic-gate 			}
3820Sstevel@tonic-gate 			free(resp);
3830Sstevel@tonic-gate 			return (0);
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 		/* If we're out of labels, then not even "." has an SOA! */
3870Sstevel@tonic-gate 		if (*dname == '\0')
3880Sstevel@tonic-gate 			break;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 		/* Find label-terminating "."; top of loop will skip it. */
3910Sstevel@tonic-gate 		while (*dname != '.') {
3920Sstevel@tonic-gate 			if (*dname == '\\')
3930Sstevel@tonic-gate 				if (*++dname == '\0') {
3940Sstevel@tonic-gate 					errno = EMSGSIZE;
3950Sstevel@tonic-gate 					goto cleanup;
3960Sstevel@tonic-gate 				}
3970Sstevel@tonic-gate 			dname++;
3980Sstevel@tonic-gate 		}
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 	DPRINTF(("get_soa: out of labels"));
4010Sstevel@tonic-gate 	errno = EDESTADDRREQ;
4020Sstevel@tonic-gate  cleanup:
4030Sstevel@tonic-gate 	if (resp != NULL)
4040Sstevel@tonic-gate 		free(resp);
4050Sstevel@tonic-gate 	return (-1);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate static int
get_ns(res_state statp,const char * zname,ns_class class,int opts,rrset_ns * nsrrsp)4090Sstevel@tonic-gate get_ns(res_state statp, const char *zname, ns_class class, int opts,
4100Sstevel@tonic-gate       rrset_ns *nsrrsp)
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate 	u_char *resp;
4130Sstevel@tonic-gate 	ns_msg msg;
4140Sstevel@tonic-gate 	int n;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	resp = malloc(NS_MAXMSG);
4170Sstevel@tonic-gate 	if (resp == NULL)
4180Sstevel@tonic-gate 		return (-1);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	/* Go and get the NS RRs for this zone. */
4210Sstevel@tonic-gate 	n = do_query(statp, zname, class, ns_t_ns, resp, &msg);
4220Sstevel@tonic-gate 	if (n != 0) {
4230Sstevel@tonic-gate 		DPRINTF(("get_ns: do_query('%s', %s) failed (%d)",
4240Sstevel@tonic-gate 			 zname, p_class(class), n));
4250Sstevel@tonic-gate 		free(resp);
4260Sstevel@tonic-gate 		return (-1);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	/* Remember the NS RRs and associated A RRs that came back. */
4300Sstevel@tonic-gate 	if (save_ns(statp, &msg, ns_s_an, zname, class, opts, nsrrsp) < 0) {
4310Sstevel@tonic-gate 		DPRINTF(("get_ns save_ns('%s', %s) failed",
4320Sstevel@tonic-gate 			 zname, p_class(class)));
4330Sstevel@tonic-gate 		free(resp);
4340Sstevel@tonic-gate 		return (-1);
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	free(resp);
4380Sstevel@tonic-gate 	return (0);
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate static int
get_glue(res_state statp,ns_class class,int opts,rrset_ns * nsrrsp)4420Sstevel@tonic-gate get_glue(res_state statp, ns_class class, int opts, rrset_ns *nsrrsp) {
4430Sstevel@tonic-gate 	rr_ns *nsrr, *nsrr_n;
4440Sstevel@tonic-gate 	u_char *resp;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	resp = malloc(NS_MAXMSG);
4470Sstevel@tonic-gate 	if (resp == NULL)
4480Sstevel@tonic-gate 		return(-1);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	/* Go and get the A RRs for each empty NS RR on our list. */
4510Sstevel@tonic-gate 	for (nsrr = HEAD(*nsrrsp); nsrr != NULL; nsrr = nsrr_n) {
4520Sstevel@tonic-gate 		ns_msg msg;
4530Sstevel@tonic-gate 		int n;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		nsrr_n = NEXT(nsrr, link);
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		if ((nsrr->flags & RR_NS_HAVE_V4) == 0) {
4580Sstevel@tonic-gate 			n = do_query(statp, nsrr->name, class, ns_t_a,
4590Sstevel@tonic-gate 				     resp, &msg);
4600Sstevel@tonic-gate 			if (n < 0) {
4610Sstevel@tonic-gate 				DPRINTF(
4620Sstevel@tonic-gate 				       ("get_glue: do_query('%s', %s') failed",
4630Sstevel@tonic-gate 					nsrr->name, p_class(class)));
4640Sstevel@tonic-gate 				goto cleanup;
4650Sstevel@tonic-gate 			}
4660Sstevel@tonic-gate 			if (n > 0) {
4670Sstevel@tonic-gate 				DPRINTF((
4680Sstevel@tonic-gate 			"get_glue: do_query('%s', %s') CNAME or DNAME found",
4690Sstevel@tonic-gate 					 nsrr->name, p_class(class)));
4700Sstevel@tonic-gate 			}
4710Sstevel@tonic-gate 			if (save_a(statp, &msg, ns_s_an, nsrr->name, class,
4720Sstevel@tonic-gate 				   opts, nsrr) < 0) {
4730Sstevel@tonic-gate 				DPRINTF(("get_glue: save_r('%s', %s) failed",
4740Sstevel@tonic-gate 					 nsrr->name, p_class(class)));
4750Sstevel@tonic-gate 				goto cleanup;
4760Sstevel@tonic-gate 			}
4770Sstevel@tonic-gate 		}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 		if ((nsrr->flags & RR_NS_HAVE_V6) == 0) {
4800Sstevel@tonic-gate 			n = do_query(statp, nsrr->name, class, ns_t_aaaa,
4810Sstevel@tonic-gate 				     resp, &msg);
4820Sstevel@tonic-gate 			if (n < 0) {
4830Sstevel@tonic-gate 				DPRINTF(
4840Sstevel@tonic-gate 				       ("get_glue: do_query('%s', %s') failed",
4850Sstevel@tonic-gate 					nsrr->name, p_class(class)));
4860Sstevel@tonic-gate 				goto cleanup;
4870Sstevel@tonic-gate 			}
4880Sstevel@tonic-gate 			if (n > 0) {
4890Sstevel@tonic-gate 				DPRINTF((
4900Sstevel@tonic-gate 			"get_glue: do_query('%s', %s') CNAME or DNAME found",
4910Sstevel@tonic-gate 					 nsrr->name, p_class(class)));
4920Sstevel@tonic-gate 			}
4930Sstevel@tonic-gate 			if (save_a(statp, &msg, ns_s_an, nsrr->name, class,
4940Sstevel@tonic-gate 				   opts, nsrr) < 0) {
4950Sstevel@tonic-gate 				DPRINTF(("get_glue: save_r('%s', %s) failed",
4960Sstevel@tonic-gate 					 nsrr->name, p_class(class)));
4970Sstevel@tonic-gate 				goto cleanup;
4980Sstevel@tonic-gate 			}
4990Sstevel@tonic-gate 		}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 		/* If it's still empty, it's just chaff. */
5020Sstevel@tonic-gate 		if (EMPTY(nsrr->addrs)) {
5030Sstevel@tonic-gate 			DPRINTF(("get_glue: removing empty '%s' NS",
5040Sstevel@tonic-gate 				 nsrr->name));
5050Sstevel@tonic-gate 			free_nsrr(nsrrsp, nsrr);
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 	free(resp);
5090Sstevel@tonic-gate 	return (0);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate  cleanup:
5120Sstevel@tonic-gate 	free(resp);
5130Sstevel@tonic-gate 	return (-1);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate static int
save_ns(res_state statp,ns_msg * msg,ns_sect sect,const char * owner,ns_class class,int opts,rrset_ns * nsrrsp)5170Sstevel@tonic-gate save_ns(res_state statp, ns_msg *msg, ns_sect sect,
5180Sstevel@tonic-gate 	const char *owner, ns_class class, int opts,
5190Sstevel@tonic-gate 	rrset_ns *nsrrsp)
5200Sstevel@tonic-gate {
5210Sstevel@tonic-gate 	int i;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	for (i = 0; i < ns_msg_count(*msg, sect); i++) {
5240Sstevel@tonic-gate 		char tname[MAXDNAME];
5250Sstevel@tonic-gate 		const u_char *rdata;
5260Sstevel@tonic-gate 		rr_ns *nsrr;
5270Sstevel@tonic-gate 		ns_rr rr;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 		if (ns_parserr(msg, sect, i, &rr) < 0) {
5300Sstevel@tonic-gate 			DPRINTF(("save_ns: ns_parserr(%s, %d) failed",
5310Sstevel@tonic-gate 				 p_section(sect, ns_o_query), i));
5320Sstevel@tonic-gate 			return (-1);
5330Sstevel@tonic-gate 		}
5340Sstevel@tonic-gate 		if (ns_rr_type(rr) != ns_t_ns ||
5350Sstevel@tonic-gate 		    ns_rr_class(rr) != class ||
5360Sstevel@tonic-gate 		    ns_samename(ns_rr_name(rr), owner) != 1)
5370Sstevel@tonic-gate 			continue;
5380Sstevel@tonic-gate 		nsrr = find_ns(nsrrsp, ns_rr_name(rr));
5390Sstevel@tonic-gate 		if (nsrr == NULL) {
5400Sstevel@tonic-gate 			nsrr = malloc(sizeof *nsrr);
5410Sstevel@tonic-gate 			if (nsrr == NULL) {
5420Sstevel@tonic-gate 				DPRINTF(("save_ns: malloc failed"));
5430Sstevel@tonic-gate 				return (-1);
5440Sstevel@tonic-gate 			}
5450Sstevel@tonic-gate 			rdata = ns_rr_rdata(rr);
5460Sstevel@tonic-gate 			if (ns_name_uncompress(ns_msg_base(*msg),
5470Sstevel@tonic-gate 					       ns_msg_end(*msg), rdata,
5480Sstevel@tonic-gate 					       tname, sizeof tname) < 0) {
5490Sstevel@tonic-gate 				DPRINTF(("save_ns: ns_name_uncompress failed")
5500Sstevel@tonic-gate 					);
5510Sstevel@tonic-gate 				free(nsrr);
5520Sstevel@tonic-gate 				return (-1);
5530Sstevel@tonic-gate 			}
5540Sstevel@tonic-gate 			nsrr->name = strdup(tname);
5550Sstevel@tonic-gate 			if (nsrr->name == NULL) {
5560Sstevel@tonic-gate 				DPRINTF(("save_ns: strdup failed"));
5570Sstevel@tonic-gate 				free(nsrr);
5580Sstevel@tonic-gate 				return (-1);
5590Sstevel@tonic-gate 			}
5600Sstevel@tonic-gate 			INIT_LINK(nsrr, link);
5610Sstevel@tonic-gate 			INIT_LIST(nsrr->addrs);
5620Sstevel@tonic-gate 			nsrr->flags = 0;
5630Sstevel@tonic-gate 			APPEND(*nsrrsp, nsrr, link);
5640Sstevel@tonic-gate 		}
5650Sstevel@tonic-gate 		if (save_a(statp, msg, ns_s_ar,
5660Sstevel@tonic-gate 			   nsrr->name, class, opts, nsrr) < 0) {
5670Sstevel@tonic-gate 			DPRINTF(("save_ns: save_r('%s', %s) failed",
5680Sstevel@tonic-gate 				 nsrr->name, p_class(class)));
5690Sstevel@tonic-gate 			return (-1);
5700Sstevel@tonic-gate 		}
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 	return (0);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate static int
save_a(res_state statp,ns_msg * msg,ns_sect sect,const char * owner,ns_class class,int opts,rr_ns * nsrr)5760Sstevel@tonic-gate save_a(res_state statp, ns_msg *msg, ns_sect sect,
5770Sstevel@tonic-gate        const char *owner, ns_class class, int opts,
5780Sstevel@tonic-gate        rr_ns *nsrr)
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate 	int i;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	for (i = 0; i < ns_msg_count(*msg, sect); i++) {
5830Sstevel@tonic-gate 		ns_rr rr;
5840Sstevel@tonic-gate 		rr_a *arr;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 		if (ns_parserr(msg, sect, i, &rr) < 0) {
5870Sstevel@tonic-gate 			DPRINTF(("save_a: ns_parserr(%s, %d) failed",
5880Sstevel@tonic-gate 				 p_section(sect, ns_o_query), i));
5890Sstevel@tonic-gate 			return (-1);
5900Sstevel@tonic-gate 		}
5910Sstevel@tonic-gate 		if ((ns_rr_type(rr) != ns_t_a &&
5920Sstevel@tonic-gate 		     ns_rr_type(rr) != ns_t_aaaa) ||
5930Sstevel@tonic-gate 		    ns_rr_class(rr) != class ||
5940Sstevel@tonic-gate 		    ns_samename(ns_rr_name(rr), owner) != 1 ||
5950Sstevel@tonic-gate 		    ns_rr_rdlen(rr) != NS_INADDRSZ)
5960Sstevel@tonic-gate 			continue;
5970Sstevel@tonic-gate 		if ((opts & RES_IPV6ONLY) != 0 && ns_rr_type(rr) != ns_t_aaaa)
5980Sstevel@tonic-gate 			continue;
5990Sstevel@tonic-gate 		if ((opts & RES_IPV4ONLY) != 0 && ns_rr_type(rr) != ns_t_a)
6000Sstevel@tonic-gate 			continue;
6010Sstevel@tonic-gate 		arr = malloc(sizeof *arr);
6020Sstevel@tonic-gate 		if (arr == NULL) {
6030Sstevel@tonic-gate 			DPRINTF(("save_a: malloc failed"));
6040Sstevel@tonic-gate 			return (-1);
6050Sstevel@tonic-gate 		}
6060Sstevel@tonic-gate 		INIT_LINK(arr, link);
6070Sstevel@tonic-gate 		memset(&arr->addr, 0, sizeof(arr->addr));
6080Sstevel@tonic-gate 		switch (ns_rr_type(rr)) {
6090Sstevel@tonic-gate 		case ns_t_a:
6100Sstevel@tonic-gate 			arr->addr.sin.sin_family = AF_INET;
6110Sstevel@tonic-gate #ifdef HAVE_SA_LEN
6120Sstevel@tonic-gate 			arr->addr.sin.sin_len = sizeof(arr->addr.sin);
6130Sstevel@tonic-gate #endif
6140Sstevel@tonic-gate 			memcpy(&arr->addr.sin.sin_addr, ns_rr_rdata(rr),
6150Sstevel@tonic-gate 			       NS_INADDRSZ);
6160Sstevel@tonic-gate 			arr->addr.sin.sin_port = htons(NAMESERVER_PORT);
6170Sstevel@tonic-gate 			nsrr->flags |= RR_NS_HAVE_V4;
6180Sstevel@tonic-gate 			break;
6190Sstevel@tonic-gate 		case ns_t_aaaa:
6200Sstevel@tonic-gate 			arr->addr.sin6.sin6_family = AF_INET6;
6210Sstevel@tonic-gate #ifdef HAVE_SA_LEN
6220Sstevel@tonic-gate 			arr->addr.sin6.sin6_len = sizeof(arr->addr.sin6);
6230Sstevel@tonic-gate #endif
6240Sstevel@tonic-gate 			memcpy(&arr->addr.sin6.sin6_addr, ns_rr_rdata(rr), 16);
6250Sstevel@tonic-gate 			arr->addr.sin.sin_port = htons(NAMESERVER_PORT);
6260Sstevel@tonic-gate 			nsrr->flags |= RR_NS_HAVE_V6;
6270Sstevel@tonic-gate 			break;
6280Sstevel@tonic-gate 		default:
6290Sstevel@tonic-gate 			abort();
6300Sstevel@tonic-gate 		}
6310Sstevel@tonic-gate 		APPEND(nsrr->addrs, arr, link);
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 	return (0);
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate static void
free_nsrrset(rrset_ns * nsrrsp)6370Sstevel@tonic-gate free_nsrrset(rrset_ns *nsrrsp) {
6380Sstevel@tonic-gate 	rr_ns *nsrr;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	while ((nsrr = HEAD(*nsrrsp)) != NULL)
6410Sstevel@tonic-gate 		free_nsrr(nsrrsp, nsrr);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate static void
free_nsrr(rrset_ns * nsrrsp,rr_ns * nsrr)6450Sstevel@tonic-gate free_nsrr(rrset_ns *nsrrsp, rr_ns *nsrr) {
6460Sstevel@tonic-gate 	rr_a *arr;
6470Sstevel@tonic-gate 	char *tmp;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	while ((arr = HEAD(nsrr->addrs)) != NULL) {
6500Sstevel@tonic-gate 		UNLINK(nsrr->addrs, arr, link);
6510Sstevel@tonic-gate 		free(arr);
6520Sstevel@tonic-gate 	}
6530Sstevel@tonic-gate 	DE_CONST(nsrr->name, tmp);
6540Sstevel@tonic-gate 	free(tmp);
6550Sstevel@tonic-gate 	UNLINK(*nsrrsp, nsrr, link);
6560Sstevel@tonic-gate 	free(nsrr);
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate static rr_ns *
find_ns(rrset_ns * nsrrsp,const char * dname)6600Sstevel@tonic-gate find_ns(rrset_ns *nsrrsp, const char *dname) {
6610Sstevel@tonic-gate 	rr_ns *nsrr;
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	for (nsrr = HEAD(*nsrrsp); nsrr != NULL; nsrr = NEXT(nsrr, link))
6640Sstevel@tonic-gate 		if (ns_samename(nsrr->name, dname) == 1)
6650Sstevel@tonic-gate 			return (nsrr);
6660Sstevel@tonic-gate 	return (NULL);
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate static int
do_query(res_state statp,const char * dname,ns_class class,ns_type qtype,u_char * resp,ns_msg * msg)6700Sstevel@tonic-gate do_query(res_state statp, const char *dname, ns_class class, ns_type qtype,
6710Sstevel@tonic-gate 	 u_char *resp, ns_msg *msg)
6720Sstevel@tonic-gate {
6730Sstevel@tonic-gate 	u_char req[NS_PACKETSZ];
6740Sstevel@tonic-gate 	int i, n;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	n = res_nmkquery(statp, ns_o_query, dname, class, qtype,
6770Sstevel@tonic-gate 			 NULL, 0, NULL, req, NS_PACKETSZ);
6780Sstevel@tonic-gate 	if (n < 0) {
6790Sstevel@tonic-gate 		DPRINTF(("do_query: res_nmkquery failed"));
6800Sstevel@tonic-gate 		return (-1);
6810Sstevel@tonic-gate 	}
6820Sstevel@tonic-gate 	n = res_nsend(statp, req, n, resp, NS_MAXMSG);
6830Sstevel@tonic-gate 	if (n < 0) {
6840Sstevel@tonic-gate 		DPRINTF(("do_query: res_nsend failed"));
6850Sstevel@tonic-gate 		return (-1);
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 	if (n == 0) {
6880Sstevel@tonic-gate 		DPRINTF(("do_query: res_nsend returned 0"));
6890Sstevel@tonic-gate 		errno = EMSGSIZE;
6900Sstevel@tonic-gate 		return (-1);
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 	if (ns_initparse(resp, n, msg) < 0) {
6930Sstevel@tonic-gate 		DPRINTF(("do_query: ns_initparse failed"));
6940Sstevel@tonic-gate 		return (-1);
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate 	n = 0;
6970Sstevel@tonic-gate 	for (i = 0; i < ns_msg_count(*msg, ns_s_an); i++) {
6980Sstevel@tonic-gate 		ns_rr rr;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 		if (ns_parserr(msg, ns_s_an, i, &rr) < 0) {
7010Sstevel@tonic-gate 			DPRINTF(("do_query: ns_parserr failed"));
7020Sstevel@tonic-gate 			return (-1);
7030Sstevel@tonic-gate 		}
7040Sstevel@tonic-gate 		n += (ns_rr_class(rr) == class &&
7050Sstevel@tonic-gate 		      (ns_rr_type(rr) == ns_t_cname ||
7060Sstevel@tonic-gate 		       ns_rr_type(rr) == ns_t_dname));
7070Sstevel@tonic-gate 	}
7080Sstevel@tonic-gate 	return (n);
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate static void
res_dprintf(const char * fmt,...)7120Sstevel@tonic-gate res_dprintf(const char *fmt, ...) {
7130Sstevel@tonic-gate 	va_list ap;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	va_start(ap, fmt);
7160Sstevel@tonic-gate 	fputs(";; res_findzonecut: ", stderr);
7170Sstevel@tonic-gate 	vfprintf(stderr, fmt, ap);
7180Sstevel@tonic-gate 	fputc('\n', stderr);
7190Sstevel@tonic-gate 	va_end(ap);
7200Sstevel@tonic-gate }
721*11038SRao.Shoaib@Sun.COM 
722*11038SRao.Shoaib@Sun.COM /*! \file */
723