xref: /illumos-gate/usr/src/lib/libresolv2/common/resolv/res_findzonecut.c (revision 55fea89dcaa64928bed4327112404dcb3e07b79f)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate /* Import. */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include "port_before.h"
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <sys/param.h>
237c478bd9Sstevel@tonic-gate #include <sys/socket.h>
247c478bd9Sstevel@tonic-gate #include <sys/time.h>
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <netinet/in.h>
277c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
287c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <limits.h>
327c478bd9Sstevel@tonic-gate #include <netdb.h>
337c478bd9Sstevel@tonic-gate #include <stdarg.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <isc/list.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "port_after.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <resolv.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /* Data structures. */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate typedef struct rr_a {
477c478bd9Sstevel@tonic-gate 	LINK(struct rr_a)	link;
487c478bd9Sstevel@tonic-gate 	union res_sockaddr_union addr;
497c478bd9Sstevel@tonic-gate } rr_a;
507c478bd9Sstevel@tonic-gate typedef LIST(rr_a) rrset_a;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate typedef struct rr_ns {
537c478bd9Sstevel@tonic-gate 	LINK(struct rr_ns)	link;
547c478bd9Sstevel@tonic-gate 	const char *		name;
557c478bd9Sstevel@tonic-gate 	unsigned int		flags;
567c478bd9Sstevel@tonic-gate 	rrset_a			addrs;
577c478bd9Sstevel@tonic-gate } rr_ns;
587c478bd9Sstevel@tonic-gate typedef LIST(rr_ns) rrset_ns;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	RR_NS_HAVE_V4		0x01
617c478bd9Sstevel@tonic-gate #define	RR_NS_HAVE_V6		0x02
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* Forward. */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static int	satisfy(res_state, const char *, rrset_ns *,
667c478bd9Sstevel@tonic-gate 			union res_sockaddr_union *, int);
677c478bd9Sstevel@tonic-gate static int	add_addrs(res_state, rr_ns *,
687c478bd9Sstevel@tonic-gate 			  union res_sockaddr_union *, int);
697c478bd9Sstevel@tonic-gate static int	get_soa(res_state, const char *, ns_class, int,
707c478bd9Sstevel@tonic-gate 			char *, size_t, char *, size_t,
717c478bd9Sstevel@tonic-gate 			rrset_ns *);
727c478bd9Sstevel@tonic-gate static int	get_ns(res_state, const char *, ns_class, int, rrset_ns *);
737c478bd9Sstevel@tonic-gate static int	get_glue(res_state, ns_class, int, rrset_ns *);
747c478bd9Sstevel@tonic-gate static int	save_ns(res_state, ns_msg *, ns_sect,
757c478bd9Sstevel@tonic-gate 			const char *, ns_class, int, rrset_ns *);
767c478bd9Sstevel@tonic-gate static int	save_a(res_state, ns_msg *, ns_sect,
777c478bd9Sstevel@tonic-gate 		       const char *, ns_class, int, rr_ns *);
787c478bd9Sstevel@tonic-gate static void	free_nsrrset(rrset_ns *);
797c478bd9Sstevel@tonic-gate static void	free_nsrr(rrset_ns *, rr_ns *);
807c478bd9Sstevel@tonic-gate static rr_ns *	find_ns(rrset_ns *, const char *);
817c478bd9Sstevel@tonic-gate static int	do_query(res_state, const char *, ns_class, ns_type,
827c478bd9Sstevel@tonic-gate 			 u_char *, ns_msg *);
837c478bd9Sstevel@tonic-gate static void	res_dprintf(const char *, ...) ISC_FORMAT_PRINTF(1, 2);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /* Macros. */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define DPRINTF(x) do {\
887c478bd9Sstevel@tonic-gate 		int save_errno = errno; \
89*9525b14bSRao Shoaib 		if ((statp->options & RES_DEBUG) != 0U) res_dprintf x; \
907c478bd9Sstevel@tonic-gate 		errno = save_errno; \
917c478bd9Sstevel@tonic-gate 	} while (0)
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /* Public. */
947c478bd9Sstevel@tonic-gate 
95*9525b14bSRao Shoaib /*%
967c478bd9Sstevel@tonic-gate  *	find enclosing zone for a <dname,class>, and some server addresses
97*9525b14bSRao Shoaib  *
987c478bd9Sstevel@tonic-gate  * parameters:
99*9525b14bSRao Shoaib  *\li	res - resolver context to work within (is modified)
100*9525b14bSRao Shoaib  *\li	dname - domain name whose enclosing zone is desired
101*9525b14bSRao Shoaib  *\li	class - class of dname (and its enclosing zone)
102*9525b14bSRao Shoaib  *\li	zname - found zone name
103*9525b14bSRao Shoaib  *\li	zsize - allocated size of zname
104*9525b14bSRao Shoaib  *\li	addrs - found server addresses
105*9525b14bSRao Shoaib  *\li	naddrs - max number of addrs
106*9525b14bSRao Shoaib  *
1077c478bd9Sstevel@tonic-gate  * return values:
108*9525b14bSRao Shoaib  *\li	< 0 - an error occurred (check errno)
109*9525b14bSRao Shoaib  *\li	= 0 - zname is now valid, but addrs[] wasn't changed
110*9525b14bSRao Shoaib  *\li	> 0 - zname is now valid, and return value is number of addrs[] found
111*9525b14bSRao Shoaib  *
1127c478bd9Sstevel@tonic-gate  * notes:
113*9525b14bSRao Shoaib  *\li	this function calls res_nsend() which means it depends on correctly
1147c478bd9Sstevel@tonic-gate  *	functioning recursive nameservers (usually defined in /etc/resolv.conf
1157c478bd9Sstevel@tonic-gate  *	or its local equivilent).
1167c478bd9Sstevel@tonic-gate  *
117*9525b14bSRao Shoaib  *\li	we start by asking for an SOA<dname,class>.  if we get one as an
1187c478bd9Sstevel@tonic-gate  *	answer, that just means <dname,class> is a zone top, which is fine.
1197c478bd9Sstevel@tonic-gate  *	more than likely we'll be told to go pound sand, in the form of a
1207c478bd9Sstevel@tonic-gate  *	negative answer.
1217c478bd9Sstevel@tonic-gate  *
122*9525b14bSRao Shoaib  *\li	note that we are not prepared to deal with referrals since that would
1237c478bd9Sstevel@tonic-gate  *	only come from authority servers and our correctly functioning local
1247c478bd9Sstevel@tonic-gate  *	recursive server would have followed the referral and got us something
1257c478bd9Sstevel@tonic-gate  *	more definite.
1267c478bd9Sstevel@tonic-gate  *
127*9525b14bSRao Shoaib  *\li	if the authority section contains an SOA, this SOA should also be the
1287c478bd9Sstevel@tonic-gate  *	closest enclosing zone, since any intermediary zone cuts would've been
1297c478bd9Sstevel@tonic-gate  *	returned as referrals and dealt with by our correctly functioning local
1307c478bd9Sstevel@tonic-gate  *	recursive name server.  but an SOA in the authority section should NOT
1317c478bd9Sstevel@tonic-gate  *	match our dname (since that would have been returned in the answer
1327c478bd9Sstevel@tonic-gate  *	section).  an authority section SOA has to be "above" our dname.
1337c478bd9Sstevel@tonic-gate  *
134*9525b14bSRao Shoaib  *\li	however, since authority section SOA's were once optional, it's
1357c478bd9Sstevel@tonic-gate  *	possible that we'll have to go hunting for the enclosing SOA by
1367c478bd9Sstevel@tonic-gate  *	ripping labels off the front of our dname -- this is known as "doing
1377c478bd9Sstevel@tonic-gate  *	it the hard way."
1387c478bd9Sstevel@tonic-gate  *
139*9525b14bSRao Shoaib  *\li	ultimately we want some server addresses, which are ideally the ones
1407c478bd9Sstevel@tonic-gate  *	pertaining to the SOA.MNAME, but only if there is a matching NS RR.
1417c478bd9Sstevel@tonic-gate  *	so the second phase (after we find an SOA) is to go looking for the
1427c478bd9Sstevel@tonic-gate  *	NS RRset for that SOA's zone.
1437c478bd9Sstevel@tonic-gate  *
144*9525b14bSRao Shoaib  *\li	no answer section processed by this code is allowed to contain CNAME
1457c478bd9Sstevel@tonic-gate  *	or DNAME RR's.  for the SOA query this means we strip a label and
1467c478bd9Sstevel@tonic-gate  *	keep going.  for the NS and A queries this means we just give up.
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@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)1507c478bd9Sstevel@tonic-gate res_findzonecut(res_state statp, const char *dname, ns_class class, int opts,
1517c478bd9Sstevel@tonic-gate 		char *zname, size_t zsize, struct in_addr *addrs, int naddrs)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	int result, i;
1547c478bd9Sstevel@tonic-gate 	union res_sockaddr_union *u;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	opts |= RES_IPV4ONLY;
1587c478bd9Sstevel@tonic-gate 	opts &= ~RES_IPV6ONLY;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	u = calloc(naddrs, sizeof(*u));
1617c478bd9Sstevel@tonic-gate 	if (u == NULL)
1627c478bd9Sstevel@tonic-gate 		return(-1);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	result = res_findzonecut2(statp, dname, class, opts, zname, zsize,
1657c478bd9Sstevel@tonic-gate 				  u, naddrs);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	for (i = 0; i < result; i++) {
1687c478bd9Sstevel@tonic-gate 		addrs[i] = u[i].sin.sin_addr;
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 	free(u);
1717c478bd9Sstevel@tonic-gate 	return (result);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@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)1757c478bd9Sstevel@tonic-gate res_findzonecut2(res_state statp, const char *dname, ns_class class, int opts,
1767c478bd9Sstevel@tonic-gate 		 char *zname, size_t zsize, union res_sockaddr_union *addrs,
1777c478bd9Sstevel@tonic-gate 		 int naddrs)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	char mname[NS_MAXDNAME];
1807c478bd9Sstevel@tonic-gate 	u_long save_pfcode;
1817c478bd9Sstevel@tonic-gate 	rrset_ns nsrrs;
1827c478bd9Sstevel@tonic-gate 	int n;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	DPRINTF(("START dname='%s' class=%s, zsize=%ld, naddrs=%d",
1857c478bd9Sstevel@tonic-gate 		 dname, p_class(class), (long)zsize, naddrs));
1867c478bd9Sstevel@tonic-gate 	save_pfcode = statp->pfcode;
1877c478bd9Sstevel@tonic-gate 	statp->pfcode |= RES_PRF_HEAD2 | RES_PRF_HEAD1 | RES_PRF_HEADX |
1887c478bd9Sstevel@tonic-gate 			 RES_PRF_QUES | RES_PRF_ANS |
1897c478bd9Sstevel@tonic-gate 			 RES_PRF_AUTH | RES_PRF_ADD;
1907c478bd9Sstevel@tonic-gate 	INIT_LIST(nsrrs);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	DPRINTF(("get the soa, and see if it has enough glue"));
1937c478bd9Sstevel@tonic-gate 	if ((n = get_soa(statp, dname, class, opts, zname, zsize,
1947c478bd9Sstevel@tonic-gate 			 mname, sizeof mname, &nsrrs)) < 0 ||
1957c478bd9Sstevel@tonic-gate 	    ((opts & RES_EXHAUSTIVE) == 0 &&
1967c478bd9Sstevel@tonic-gate 	     (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
1977c478bd9Sstevel@tonic-gate 		goto done;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	DPRINTF(("get the ns rrset and see if it has enough glue"));
2007c478bd9Sstevel@tonic-gate 	if ((n = get_ns(statp, zname, class, opts, &nsrrs)) < 0 ||
2017c478bd9Sstevel@tonic-gate 	    ((opts & RES_EXHAUSTIVE) == 0 &&
2027c478bd9Sstevel@tonic-gate 	     (n = satisfy(statp, mname, &nsrrs, addrs, naddrs)) > 0))
2037c478bd9Sstevel@tonic-gate 		goto done;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	DPRINTF(("get the missing glue and see if it's finally enough"));
2067c478bd9Sstevel@tonic-gate 	if ((n = get_glue(statp, class, opts, &nsrrs)) >= 0)
2077c478bd9Sstevel@tonic-gate 		n = satisfy(statp, mname, &nsrrs, addrs, naddrs);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate  done:
2107c478bd9Sstevel@tonic-gate 	DPRINTF(("FINISH n=%d (%s)", n, (n < 0) ? strerror(errno) : "OK"));
2117c478bd9Sstevel@tonic-gate 	free_nsrrset(&nsrrs);
2127c478bd9Sstevel@tonic-gate 	statp->pfcode = save_pfcode;
2137c478bd9Sstevel@tonic-gate 	return (n);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /* Private. */
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate static int
satisfy(res_state statp,const char * mname,rrset_ns * nsrrsp,union res_sockaddr_union * addrs,int naddrs)2197c478bd9Sstevel@tonic-gate satisfy(res_state statp, const char *mname, rrset_ns *nsrrsp,
2207c478bd9Sstevel@tonic-gate 	union res_sockaddr_union *addrs, int naddrs)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	rr_ns *nsrr;
2237c478bd9Sstevel@tonic-gate 	int n, x;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	n = 0;
2267c478bd9Sstevel@tonic-gate 	nsrr = find_ns(nsrrsp, mname);
2277c478bd9Sstevel@tonic-gate 	if (nsrr != NULL) {
2287c478bd9Sstevel@tonic-gate 		x = add_addrs(statp, nsrr, addrs, naddrs);
2297c478bd9Sstevel@tonic-gate 		addrs += x;
2307c478bd9Sstevel@tonic-gate 		naddrs -= x;
2317c478bd9Sstevel@tonic-gate 		n += x;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 	for (nsrr = HEAD(*nsrrsp);
2347c478bd9Sstevel@tonic-gate 	     nsrr != NULL && naddrs > 0;
2357c478bd9Sstevel@tonic-gate 	     nsrr = NEXT(nsrr, link))
2367c478bd9Sstevel@tonic-gate 		if (ns_samename(nsrr->name, mname) != 1) {
2377c478bd9Sstevel@tonic-gate 			x = add_addrs(statp, nsrr, addrs, naddrs);
2387c478bd9Sstevel@tonic-gate 			addrs += x;
2397c478bd9Sstevel@tonic-gate 			naddrs -= x;
2407c478bd9Sstevel@tonic-gate 			n += x;
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 	DPRINTF(("satisfy(%s): %d", mname, n));
2437c478bd9Sstevel@tonic-gate 	return (n);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate static int
add_addrs(res_state statp,rr_ns * nsrr,union res_sockaddr_union * addrs,int naddrs)2477c478bd9Sstevel@tonic-gate add_addrs(res_state statp, rr_ns *nsrr,
2487c478bd9Sstevel@tonic-gate 	  union res_sockaddr_union *addrs, int naddrs)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	rr_a *arr;
2517c478bd9Sstevel@tonic-gate 	int n = 0;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	for (arr = HEAD(nsrr->addrs); arr != NULL; arr = NEXT(arr, link)) {
2547c478bd9Sstevel@tonic-gate 		if (naddrs <= 0)
2557c478bd9Sstevel@tonic-gate 			return (0);
2567c478bd9Sstevel@tonic-gate 		*addrs++ = arr->addr;
2577c478bd9Sstevel@tonic-gate 		naddrs--;
2587c478bd9Sstevel@tonic-gate 		n++;
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 	DPRINTF(("add_addrs: %d", n));
2617c478bd9Sstevel@tonic-gate 	return (n);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@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)2657c478bd9Sstevel@tonic-gate get_soa(res_state statp, const char *dname, ns_class class, int opts,
2667c478bd9Sstevel@tonic-gate 	char *zname, size_t zsize, char *mname, size_t msize,
2677c478bd9Sstevel@tonic-gate 	rrset_ns *nsrrsp)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	char tname[NS_MAXDNAME];
2707c478bd9Sstevel@tonic-gate 	u_char *resp = NULL;
2717c478bd9Sstevel@tonic-gate 	int n, i, ancount, nscount;
2727c478bd9Sstevel@tonic-gate 	ns_sect sect;
2737c478bd9Sstevel@tonic-gate 	ns_msg msg;
2747c478bd9Sstevel@tonic-gate 	u_int rcode;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/*
2777c478bd9Sstevel@tonic-gate 	 * Find closest enclosing SOA, even if it's for the root zone.
2787c478bd9Sstevel@tonic-gate 	 */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	/* First canonicalize dname (exactly one unescaped trailing "."). */
2817c478bd9Sstevel@tonic-gate 	if (ns_makecanon(dname, tname, sizeof tname) < 0)
2827c478bd9Sstevel@tonic-gate 		goto cleanup;
2837c478bd9Sstevel@tonic-gate 	dname = tname;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	resp = malloc(NS_MAXMSG);
2867c478bd9Sstevel@tonic-gate 	if (resp == NULL)
2877c478bd9Sstevel@tonic-gate 		goto cleanup;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/* Now grovel the subdomains, hunting for an SOA answer or auth. */
2907c478bd9Sstevel@tonic-gate 	for (;;) {
2917c478bd9Sstevel@tonic-gate 		/* Leading or inter-label '.' are skipped here. */
2927c478bd9Sstevel@tonic-gate 		while (*dname == '.')
2937c478bd9Sstevel@tonic-gate 			dname++;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 		/* Is there an SOA? */
2967c478bd9Sstevel@tonic-gate 		n = do_query(statp, dname, class, ns_t_soa, resp, &msg);
2977c478bd9Sstevel@tonic-gate 		if (n < 0) {
2987c478bd9Sstevel@tonic-gate 			DPRINTF(("get_soa: do_query('%s', %s) failed (%d)",
2997c478bd9Sstevel@tonic-gate 				 dname, p_class(class), n));
3007c478bd9Sstevel@tonic-gate 			goto cleanup;
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 		if (n > 0) {
3037c478bd9Sstevel@tonic-gate 			DPRINTF(("get_soa: CNAME or DNAME found"));
3047c478bd9Sstevel@tonic-gate 			sect = ns_s_max, n = 0;
3057c478bd9Sstevel@tonic-gate 		} else {
3067c478bd9Sstevel@tonic-gate 			rcode = ns_msg_getflag(msg, ns_f_rcode);
3077c478bd9Sstevel@tonic-gate 			ancount = ns_msg_count(msg, ns_s_an);
3087c478bd9Sstevel@tonic-gate 			nscount = ns_msg_count(msg, ns_s_ns);
3097c478bd9Sstevel@tonic-gate 			if (ancount > 0 && rcode == ns_r_noerror)
3107c478bd9Sstevel@tonic-gate 				sect = ns_s_an, n = ancount;
3117c478bd9Sstevel@tonic-gate 			else if (nscount > 0)
3127c478bd9Sstevel@tonic-gate 				sect = ns_s_ns, n = nscount;
3137c478bd9Sstevel@tonic-gate 			else
3147c478bd9Sstevel@tonic-gate 				sect = ns_s_max, n = 0;
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 		for (i = 0; i < n; i++) {
3177c478bd9Sstevel@tonic-gate 			const char *t;
3187c478bd9Sstevel@tonic-gate 			const u_char *rdata;
3197c478bd9Sstevel@tonic-gate 			ns_rr rr;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 			if (ns_parserr(&msg, sect, i, &rr) < 0) {
3227c478bd9Sstevel@tonic-gate 				DPRINTF(("get_soa: ns_parserr(%s, %d) failed",
3237c478bd9Sstevel@tonic-gate 					 p_section(sect, ns_o_query), i));
3247c478bd9Sstevel@tonic-gate 				goto cleanup;
3257c478bd9Sstevel@tonic-gate 			}
3267c478bd9Sstevel@tonic-gate 			if (ns_rr_type(rr) == ns_t_cname ||
3277c478bd9Sstevel@tonic-gate 			    ns_rr_type(rr) == ns_t_dname)
3287c478bd9Sstevel@tonic-gate 				break;
3297c478bd9Sstevel@tonic-gate 			if (ns_rr_type(rr) != ns_t_soa ||
3307c478bd9Sstevel@tonic-gate 			    ns_rr_class(rr) != class)
3317c478bd9Sstevel@tonic-gate 				continue;
3327c478bd9Sstevel@tonic-gate 			t = ns_rr_name(rr);
3337c478bd9Sstevel@tonic-gate 			switch (sect) {
3347c478bd9Sstevel@tonic-gate 			case ns_s_an:
3357c478bd9Sstevel@tonic-gate 				if (ns_samedomain(dname, t) == 0) {
3367c478bd9Sstevel@tonic-gate 					DPRINTF(
3377c478bd9Sstevel@tonic-gate 				    ("get_soa: ns_samedomain('%s', '%s') == 0",
3387c478bd9Sstevel@tonic-gate 						dname, t)
3397c478bd9Sstevel@tonic-gate 						);
3407c478bd9Sstevel@tonic-gate 					errno = EPROTOTYPE;
3417c478bd9Sstevel@tonic-gate 					goto cleanup;
3427c478bd9Sstevel@tonic-gate 				}
3437c478bd9Sstevel@tonic-gate 				break;
3447c478bd9Sstevel@tonic-gate 			case ns_s_ns:
3457c478bd9Sstevel@tonic-gate 				if (ns_samename(dname, t) == 1 ||
3467c478bd9Sstevel@tonic-gate 				    ns_samedomain(dname, t) == 0) {
3477c478bd9Sstevel@tonic-gate 					DPRINTF(
3487c478bd9Sstevel@tonic-gate 		       ("get_soa: ns_samename() || !ns_samedomain('%s', '%s')",
3497c478bd9Sstevel@tonic-gate 						dname, t)
3507c478bd9Sstevel@tonic-gate 						);
3517c478bd9Sstevel@tonic-gate 					errno = EPROTOTYPE;
3527c478bd9Sstevel@tonic-gate 					goto cleanup;
3537c478bd9Sstevel@tonic-gate 				}
3547c478bd9Sstevel@tonic-gate 				break;
3557c478bd9Sstevel@tonic-gate 			default:
3567c478bd9Sstevel@tonic-gate 				abort();
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 			if (strlen(t) + 1 > zsize) {
359*9525b14bSRao Shoaib 				DPRINTF(("get_soa: zname(%lu) too small (%lu)",
360*9525b14bSRao Shoaib 					 (unsigned long)zsize,
361*9525b14bSRao Shoaib 					 (unsigned long)strlen(t) + 1));
3627c478bd9Sstevel@tonic-gate 				errno = EMSGSIZE;
3637c478bd9Sstevel@tonic-gate 				goto cleanup;
3647c478bd9Sstevel@tonic-gate 			}
3657c478bd9Sstevel@tonic-gate 			strcpy(zname, t);
3667c478bd9Sstevel@tonic-gate 			rdata = ns_rr_rdata(rr);
3677c478bd9Sstevel@tonic-gate 			if (ns_name_uncompress(resp, ns_msg_end(msg), rdata,
3687c478bd9Sstevel@tonic-gate 					       mname, msize) < 0) {
3697c478bd9Sstevel@tonic-gate 				DPRINTF(("get_soa: ns_name_uncompress failed")
3707c478bd9Sstevel@tonic-gate 					);
3717c478bd9Sstevel@tonic-gate 				goto cleanup;
3727c478bd9Sstevel@tonic-gate 			}
3737c478bd9Sstevel@tonic-gate 			if (save_ns(statp, &msg, ns_s_ns,
3747c478bd9Sstevel@tonic-gate 				    zname, class, opts, nsrrsp) < 0) {
3757c478bd9Sstevel@tonic-gate 				DPRINTF(("get_soa: save_ns failed"));
3767c478bd9Sstevel@tonic-gate 				goto cleanup;
3777c478bd9Sstevel@tonic-gate 			}
3787c478bd9Sstevel@tonic-gate 			free(resp);
3797c478bd9Sstevel@tonic-gate 			return (0);
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 		/* If we're out of labels, then not even "." has an SOA! */
3837c478bd9Sstevel@tonic-gate 		if (*dname == '\0')
3847c478bd9Sstevel@tonic-gate 			break;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 		/* Find label-terminating "."; top of loop will skip it. */
3877c478bd9Sstevel@tonic-gate 		while (*dname != '.') {
3887c478bd9Sstevel@tonic-gate 			if (*dname == '\\')
3897c478bd9Sstevel@tonic-gate 				if (*++dname == '\0') {
3907c478bd9Sstevel@tonic-gate 					errno = EMSGSIZE;
3917c478bd9Sstevel@tonic-gate 					goto cleanup;
3927c478bd9Sstevel@tonic-gate 				}
3937c478bd9Sstevel@tonic-gate 			dname++;
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 	DPRINTF(("get_soa: out of labels"));
3977c478bd9Sstevel@tonic-gate 	errno = EDESTADDRREQ;
3987c478bd9Sstevel@tonic-gate  cleanup:
3997c478bd9Sstevel@tonic-gate 	if (resp != NULL)
4007c478bd9Sstevel@tonic-gate 		free(resp);
4017c478bd9Sstevel@tonic-gate 	return (-1);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate static int
get_ns(res_state statp,const char * zname,ns_class class,int opts,rrset_ns * nsrrsp)4057c478bd9Sstevel@tonic-gate get_ns(res_state statp, const char *zname, ns_class class, int opts,
4067c478bd9Sstevel@tonic-gate       rrset_ns *nsrrsp)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	u_char *resp;
4097c478bd9Sstevel@tonic-gate 	ns_msg msg;
4107c478bd9Sstevel@tonic-gate 	int n;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	resp = malloc(NS_MAXMSG);
4137c478bd9Sstevel@tonic-gate 	if (resp == NULL)
4147c478bd9Sstevel@tonic-gate 		return (-1);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/* Go and get the NS RRs for this zone. */
4177c478bd9Sstevel@tonic-gate 	n = do_query(statp, zname, class, ns_t_ns, resp, &msg);
4187c478bd9Sstevel@tonic-gate 	if (n != 0) {
4197c478bd9Sstevel@tonic-gate 		DPRINTF(("get_ns: do_query('%s', %s) failed (%d)",
4207c478bd9Sstevel@tonic-gate 			 zname, p_class(class), n));
4217c478bd9Sstevel@tonic-gate 		free(resp);
4227c478bd9Sstevel@tonic-gate 		return (-1);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/* Remember the NS RRs and associated A RRs that came back. */
4267c478bd9Sstevel@tonic-gate 	if (save_ns(statp, &msg, ns_s_an, zname, class, opts, nsrrsp) < 0) {
4277c478bd9Sstevel@tonic-gate 		DPRINTF(("get_ns save_ns('%s', %s) failed",
4287c478bd9Sstevel@tonic-gate 			 zname, p_class(class)));
4297c478bd9Sstevel@tonic-gate 		free(resp);
4307c478bd9Sstevel@tonic-gate 		return (-1);
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	free(resp);
4347c478bd9Sstevel@tonic-gate 	return (0);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate static int
get_glue(res_state statp,ns_class class,int opts,rrset_ns * nsrrsp)4387c478bd9Sstevel@tonic-gate get_glue(res_state statp, ns_class class, int opts, rrset_ns *nsrrsp) {
4397c478bd9Sstevel@tonic-gate 	rr_ns *nsrr, *nsrr_n;
4407c478bd9Sstevel@tonic-gate 	u_char *resp;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	resp = malloc(NS_MAXMSG);
4437c478bd9Sstevel@tonic-gate 	if (resp == NULL)
4447c478bd9Sstevel@tonic-gate 		return(-1);
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	/* Go and get the A RRs for each empty NS RR on our list. */
4477c478bd9Sstevel@tonic-gate 	for (nsrr = HEAD(*nsrrsp); nsrr != NULL; nsrr = nsrr_n) {
4487c478bd9Sstevel@tonic-gate 		ns_msg msg;
4497c478bd9Sstevel@tonic-gate 		int n;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		nsrr_n = NEXT(nsrr, link);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 		if ((nsrr->flags & RR_NS_HAVE_V4) == 0) {
4547c478bd9Sstevel@tonic-gate 			n = do_query(statp, nsrr->name, class, ns_t_a,
4557c478bd9Sstevel@tonic-gate 				     resp, &msg);
4567c478bd9Sstevel@tonic-gate 			if (n < 0) {
4577c478bd9Sstevel@tonic-gate 				DPRINTF(
4587c478bd9Sstevel@tonic-gate 				       ("get_glue: do_query('%s', %s') failed",
4597c478bd9Sstevel@tonic-gate 					nsrr->name, p_class(class)));
4607c478bd9Sstevel@tonic-gate 				goto cleanup;
4617c478bd9Sstevel@tonic-gate 			}
4627c478bd9Sstevel@tonic-gate 			if (n > 0) {
4637c478bd9Sstevel@tonic-gate 				DPRINTF((
4647c478bd9Sstevel@tonic-gate 			"get_glue: do_query('%s', %s') CNAME or DNAME found",
4657c478bd9Sstevel@tonic-gate 					 nsrr->name, p_class(class)));
4667c478bd9Sstevel@tonic-gate 			}
4677c478bd9Sstevel@tonic-gate 			if (save_a(statp, &msg, ns_s_an, nsrr->name, class,
4687c478bd9Sstevel@tonic-gate 				   opts, nsrr) < 0) {
4697c478bd9Sstevel@tonic-gate 				DPRINTF(("get_glue: save_r('%s', %s) failed",
4707c478bd9Sstevel@tonic-gate 					 nsrr->name, p_class(class)));
4717c478bd9Sstevel@tonic-gate 				goto cleanup;
4727c478bd9Sstevel@tonic-gate 			}
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 		if ((nsrr->flags & RR_NS_HAVE_V6) == 0) {
4767c478bd9Sstevel@tonic-gate 			n = do_query(statp, nsrr->name, class, ns_t_aaaa,
4777c478bd9Sstevel@tonic-gate 				     resp, &msg);
4787c478bd9Sstevel@tonic-gate 			if (n < 0) {
4797c478bd9Sstevel@tonic-gate 				DPRINTF(
4807c478bd9Sstevel@tonic-gate 				       ("get_glue: do_query('%s', %s') failed",
4817c478bd9Sstevel@tonic-gate 					nsrr->name, p_class(class)));
4827c478bd9Sstevel@tonic-gate 				goto cleanup;
4837c478bd9Sstevel@tonic-gate 			}
4847c478bd9Sstevel@tonic-gate 			if (n > 0) {
4857c478bd9Sstevel@tonic-gate 				DPRINTF((
4867c478bd9Sstevel@tonic-gate 			"get_glue: do_query('%s', %s') CNAME or DNAME found",
4877c478bd9Sstevel@tonic-gate 					 nsrr->name, p_class(class)));
4887c478bd9Sstevel@tonic-gate 			}
4897c478bd9Sstevel@tonic-gate 			if (save_a(statp, &msg, ns_s_an, nsrr->name, class,
4907c478bd9Sstevel@tonic-gate 				   opts, nsrr) < 0) {
4917c478bd9Sstevel@tonic-gate 				DPRINTF(("get_glue: save_r('%s', %s) failed",
4927c478bd9Sstevel@tonic-gate 					 nsrr->name, p_class(class)));
4937c478bd9Sstevel@tonic-gate 				goto cleanup;
4947c478bd9Sstevel@tonic-gate 			}
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 		/* If it's still empty, it's just chaff. */
4987c478bd9Sstevel@tonic-gate 		if (EMPTY(nsrr->addrs)) {
4997c478bd9Sstevel@tonic-gate 			DPRINTF(("get_glue: removing empty '%s' NS",
5007c478bd9Sstevel@tonic-gate 				 nsrr->name));
5017c478bd9Sstevel@tonic-gate 			free_nsrr(nsrrsp, nsrr);
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	free(resp);
5057c478bd9Sstevel@tonic-gate 	return (0);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate  cleanup:
5087c478bd9Sstevel@tonic-gate 	free(resp);
5097c478bd9Sstevel@tonic-gate 	return (-1);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@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)5137c478bd9Sstevel@tonic-gate save_ns(res_state statp, ns_msg *msg, ns_sect sect,
5147c478bd9Sstevel@tonic-gate 	const char *owner, ns_class class, int opts,
5157c478bd9Sstevel@tonic-gate 	rrset_ns *nsrrsp)
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	int i;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	for (i = 0; i < ns_msg_count(*msg, sect); i++) {
5207c478bd9Sstevel@tonic-gate 		char tname[MAXDNAME];
5217c478bd9Sstevel@tonic-gate 		const u_char *rdata;
5227c478bd9Sstevel@tonic-gate 		rr_ns *nsrr;
5237c478bd9Sstevel@tonic-gate 		ns_rr rr;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		if (ns_parserr(msg, sect, i, &rr) < 0) {
5267c478bd9Sstevel@tonic-gate 			DPRINTF(("save_ns: ns_parserr(%s, %d) failed",
5277c478bd9Sstevel@tonic-gate 				 p_section(sect, ns_o_query), i));
5287c478bd9Sstevel@tonic-gate 			return (-1);
5297c478bd9Sstevel@tonic-gate 		}
5307c478bd9Sstevel@tonic-gate 		if (ns_rr_type(rr) != ns_t_ns ||
5317c478bd9Sstevel@tonic-gate 		    ns_rr_class(rr) != class ||
5327c478bd9Sstevel@tonic-gate 		    ns_samename(ns_rr_name(rr), owner) != 1)
5337c478bd9Sstevel@tonic-gate 			continue;
5347c478bd9Sstevel@tonic-gate 		nsrr = find_ns(nsrrsp, ns_rr_name(rr));
5357c478bd9Sstevel@tonic-gate 		if (nsrr == NULL) {
5367c478bd9Sstevel@tonic-gate 			nsrr = malloc(sizeof *nsrr);
5377c478bd9Sstevel@tonic-gate 			if (nsrr == NULL) {
5387c478bd9Sstevel@tonic-gate 				DPRINTF(("save_ns: malloc failed"));
5397c478bd9Sstevel@tonic-gate 				return (-1);
5407c478bd9Sstevel@tonic-gate 			}
5417c478bd9Sstevel@tonic-gate 			rdata = ns_rr_rdata(rr);
5427c478bd9Sstevel@tonic-gate 			if (ns_name_uncompress(ns_msg_base(*msg),
5437c478bd9Sstevel@tonic-gate 					       ns_msg_end(*msg), rdata,
5447c478bd9Sstevel@tonic-gate 					       tname, sizeof tname) < 0) {
5457c478bd9Sstevel@tonic-gate 				DPRINTF(("save_ns: ns_name_uncompress failed")
5467c478bd9Sstevel@tonic-gate 					);
5477c478bd9Sstevel@tonic-gate 				free(nsrr);
5487c478bd9Sstevel@tonic-gate 				return (-1);
5497c478bd9Sstevel@tonic-gate 			}
5507c478bd9Sstevel@tonic-gate 			nsrr->name = strdup(tname);
5517c478bd9Sstevel@tonic-gate 			if (nsrr->name == NULL) {
5527c478bd9Sstevel@tonic-gate 				DPRINTF(("save_ns: strdup failed"));
5537c478bd9Sstevel@tonic-gate 				free(nsrr);
5547c478bd9Sstevel@tonic-gate 				return (-1);
5557c478bd9Sstevel@tonic-gate 			}
5567c478bd9Sstevel@tonic-gate 			INIT_LINK(nsrr, link);
5577c478bd9Sstevel@tonic-gate 			INIT_LIST(nsrr->addrs);
5587c478bd9Sstevel@tonic-gate 			nsrr->flags = 0;
5597c478bd9Sstevel@tonic-gate 			APPEND(*nsrrsp, nsrr, link);
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 		if (save_a(statp, msg, ns_s_ar,
5627c478bd9Sstevel@tonic-gate 			   nsrr->name, class, opts, nsrr) < 0) {
5637c478bd9Sstevel@tonic-gate 			DPRINTF(("save_ns: save_r('%s', %s) failed",
5647c478bd9Sstevel@tonic-gate 				 nsrr->name, p_class(class)));
5657c478bd9Sstevel@tonic-gate 			return (-1);
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 	return (0);
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@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)5727c478bd9Sstevel@tonic-gate save_a(res_state statp, ns_msg *msg, ns_sect sect,
5737c478bd9Sstevel@tonic-gate        const char *owner, ns_class class, int opts,
5747c478bd9Sstevel@tonic-gate        rr_ns *nsrr)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	int i;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	for (i = 0; i < ns_msg_count(*msg, sect); i++) {
5797c478bd9Sstevel@tonic-gate 		ns_rr rr;
5807c478bd9Sstevel@tonic-gate 		rr_a *arr;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		if (ns_parserr(msg, sect, i, &rr) < 0) {
5837c478bd9Sstevel@tonic-gate 			DPRINTF(("save_a: ns_parserr(%s, %d) failed",
5847c478bd9Sstevel@tonic-gate 				 p_section(sect, ns_o_query), i));
5857c478bd9Sstevel@tonic-gate 			return (-1);
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 		if ((ns_rr_type(rr) != ns_t_a &&
5887c478bd9Sstevel@tonic-gate 		     ns_rr_type(rr) != ns_t_aaaa) ||
5897c478bd9Sstevel@tonic-gate 		    ns_rr_class(rr) != class ||
5907c478bd9Sstevel@tonic-gate 		    ns_samename(ns_rr_name(rr), owner) != 1 ||
5917c478bd9Sstevel@tonic-gate 		    ns_rr_rdlen(rr) != NS_INADDRSZ)
5927c478bd9Sstevel@tonic-gate 			continue;
5937c478bd9Sstevel@tonic-gate 		if ((opts & RES_IPV6ONLY) != 0 && ns_rr_type(rr) != ns_t_aaaa)
5947c478bd9Sstevel@tonic-gate 			continue;
5957c478bd9Sstevel@tonic-gate 		if ((opts & RES_IPV4ONLY) != 0 && ns_rr_type(rr) != ns_t_a)
5967c478bd9Sstevel@tonic-gate 			continue;
5977c478bd9Sstevel@tonic-gate 		arr = malloc(sizeof *arr);
5987c478bd9Sstevel@tonic-gate 		if (arr == NULL) {
5997c478bd9Sstevel@tonic-gate 			DPRINTF(("save_a: malloc failed"));
6007c478bd9Sstevel@tonic-gate 			return (-1);
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 		INIT_LINK(arr, link);
6037c478bd9Sstevel@tonic-gate 		memset(&arr->addr, 0, sizeof(arr->addr));
6047c478bd9Sstevel@tonic-gate 		switch (ns_rr_type(rr)) {
6057c478bd9Sstevel@tonic-gate 		case ns_t_a:
6067c478bd9Sstevel@tonic-gate 			arr->addr.sin.sin_family = AF_INET;
6077c478bd9Sstevel@tonic-gate #ifdef HAVE_SA_LEN
6087c478bd9Sstevel@tonic-gate 			arr->addr.sin.sin_len = sizeof(arr->addr.sin);
6097c478bd9Sstevel@tonic-gate #endif
6107c478bd9Sstevel@tonic-gate 			memcpy(&arr->addr.sin.sin_addr, ns_rr_rdata(rr),
6117c478bd9Sstevel@tonic-gate 			       NS_INADDRSZ);
6127c478bd9Sstevel@tonic-gate 			arr->addr.sin.sin_port = htons(NAMESERVER_PORT);
6137c478bd9Sstevel@tonic-gate 			nsrr->flags |= RR_NS_HAVE_V4;
6147c478bd9Sstevel@tonic-gate 			break;
6157c478bd9Sstevel@tonic-gate 		case ns_t_aaaa:
6167c478bd9Sstevel@tonic-gate 			arr->addr.sin6.sin6_family = AF_INET6;
6177c478bd9Sstevel@tonic-gate #ifdef HAVE_SA_LEN
6187c478bd9Sstevel@tonic-gate 			arr->addr.sin6.sin6_len = sizeof(arr->addr.sin6);
6197c478bd9Sstevel@tonic-gate #endif
6207c478bd9Sstevel@tonic-gate 			memcpy(&arr->addr.sin6.sin6_addr, ns_rr_rdata(rr), 16);
6217c478bd9Sstevel@tonic-gate 			arr->addr.sin.sin_port = htons(NAMESERVER_PORT);
6227c478bd9Sstevel@tonic-gate 			nsrr->flags |= RR_NS_HAVE_V6;
6237c478bd9Sstevel@tonic-gate 			break;
6247c478bd9Sstevel@tonic-gate 		default:
6257c478bd9Sstevel@tonic-gate 			abort();
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 		APPEND(nsrr->addrs, arr, link);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 	return (0);
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate static void
free_nsrrset(rrset_ns * nsrrsp)6337c478bd9Sstevel@tonic-gate free_nsrrset(rrset_ns *nsrrsp) {
6347c478bd9Sstevel@tonic-gate 	rr_ns *nsrr;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	while ((nsrr = HEAD(*nsrrsp)) != NULL)
6377c478bd9Sstevel@tonic-gate 		free_nsrr(nsrrsp, nsrr);
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate static void
free_nsrr(rrset_ns * nsrrsp,rr_ns * nsrr)6417c478bd9Sstevel@tonic-gate free_nsrr(rrset_ns *nsrrsp, rr_ns *nsrr) {
6427c478bd9Sstevel@tonic-gate 	rr_a *arr;
6437c478bd9Sstevel@tonic-gate 	char *tmp;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	while ((arr = HEAD(nsrr->addrs)) != NULL) {
6467c478bd9Sstevel@tonic-gate 		UNLINK(nsrr->addrs, arr, link);
6477c478bd9Sstevel@tonic-gate 		free(arr);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 	DE_CONST(nsrr->name, tmp);
6507c478bd9Sstevel@tonic-gate 	free(tmp);
6517c478bd9Sstevel@tonic-gate 	UNLINK(*nsrrsp, nsrr, link);
6527c478bd9Sstevel@tonic-gate 	free(nsrr);
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate static rr_ns *
find_ns(rrset_ns * nsrrsp,const char * dname)6567c478bd9Sstevel@tonic-gate find_ns(rrset_ns *nsrrsp, const char *dname) {
6577c478bd9Sstevel@tonic-gate 	rr_ns *nsrr;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	for (nsrr = HEAD(*nsrrsp); nsrr != NULL; nsrr = NEXT(nsrr, link))
6607c478bd9Sstevel@tonic-gate 		if (ns_samename(nsrr->name, dname) == 1)
6617c478bd9Sstevel@tonic-gate 			return (nsrr);
6627c478bd9Sstevel@tonic-gate 	return (NULL);
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate static int
do_query(res_state statp,const char * dname,ns_class class,ns_type qtype,u_char * resp,ns_msg * msg)6667c478bd9Sstevel@tonic-gate do_query(res_state statp, const char *dname, ns_class class, ns_type qtype,
6677c478bd9Sstevel@tonic-gate 	 u_char *resp, ns_msg *msg)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	u_char req[NS_PACKETSZ];
6707c478bd9Sstevel@tonic-gate 	int i, n;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	n = res_nmkquery(statp, ns_o_query, dname, class, qtype,
6737c478bd9Sstevel@tonic-gate 			 NULL, 0, NULL, req, NS_PACKETSZ);
6747c478bd9Sstevel@tonic-gate 	if (n < 0) {
6757c478bd9Sstevel@tonic-gate 		DPRINTF(("do_query: res_nmkquery failed"));
6767c478bd9Sstevel@tonic-gate 		return (-1);
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 	n = res_nsend(statp, req, n, resp, NS_MAXMSG);
6797c478bd9Sstevel@tonic-gate 	if (n < 0) {
6807c478bd9Sstevel@tonic-gate 		DPRINTF(("do_query: res_nsend failed"));
6817c478bd9Sstevel@tonic-gate 		return (-1);
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 	if (n == 0) {
6847c478bd9Sstevel@tonic-gate 		DPRINTF(("do_query: res_nsend returned 0"));
6857c478bd9Sstevel@tonic-gate 		errno = EMSGSIZE;
6867c478bd9Sstevel@tonic-gate 		return (-1);
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 	if (ns_initparse(resp, n, msg) < 0) {
6897c478bd9Sstevel@tonic-gate 		DPRINTF(("do_query: ns_initparse failed"));
6907c478bd9Sstevel@tonic-gate 		return (-1);
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 	n = 0;
6937c478bd9Sstevel@tonic-gate 	for (i = 0; i < ns_msg_count(*msg, ns_s_an); i++) {
6947c478bd9Sstevel@tonic-gate 		ns_rr rr;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 		if (ns_parserr(msg, ns_s_an, i, &rr) < 0) {
6977c478bd9Sstevel@tonic-gate 			DPRINTF(("do_query: ns_parserr failed"));
6987c478bd9Sstevel@tonic-gate 			return (-1);
6997c478bd9Sstevel@tonic-gate 		}
7007c478bd9Sstevel@tonic-gate 		n += (ns_rr_class(rr) == class &&
7017c478bd9Sstevel@tonic-gate 		      (ns_rr_type(rr) == ns_t_cname ||
7027c478bd9Sstevel@tonic-gate 		       ns_rr_type(rr) == ns_t_dname));
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 	return (n);
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate static void
res_dprintf(const char * fmt,...)7087c478bd9Sstevel@tonic-gate res_dprintf(const char *fmt, ...) {
7097c478bd9Sstevel@tonic-gate 	va_list ap;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
7127c478bd9Sstevel@tonic-gate 	fputs(";; res_findzonecut: ", stderr);
7137c478bd9Sstevel@tonic-gate 	vfprintf(stderr, fmt, ap);
7147c478bd9Sstevel@tonic-gate 	fputc('\n', stderr);
7157c478bd9Sstevel@tonic-gate 	va_end(ap);
7167c478bd9Sstevel@tonic-gate }
717*9525b14bSRao Shoaib 
718*9525b14bSRao Shoaib /*! \file */
719