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