1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 1997-2002 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*
7*0Sstevel@tonic-gate  * Copyright (c) 1996-1999 by Internet Software Consortium.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
10*0Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
11*0Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14*0Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16*0Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17*0Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18*0Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19*0Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20*0Sstevel@tonic-gate  * SOFTWARE.
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
26*0Sstevel@tonic-gate static const char rcsid[] = "$Id: dns.c,v 1.16 2001/05/29 05:48:26 marka Exp $";
27*0Sstevel@tonic-gate #endif
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * dns.c --- this is the top-level accessor function for the dns
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include "port_before.h"
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <errno.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include <sys/types.h>
40*0Sstevel@tonic-gate #include <netinet/in.h>
41*0Sstevel@tonic-gate #include <arpa/nameser.h>
42*0Sstevel@tonic-gate #include <resolv.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include <resolv.h>
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #include <isc/memcluster.h>
47*0Sstevel@tonic-gate #include <irs.h>
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #include "port_after.h"
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include "irs_p.h"
52*0Sstevel@tonic-gate #include "hesiod.h"
53*0Sstevel@tonic-gate #include "dns_p.h"
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate /* forward */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate static void		dns_close(struct irs_acc *);
58*0Sstevel@tonic-gate static struct __res_state *	dns_res_get(struct irs_acc *);
59*0Sstevel@tonic-gate static void		dns_res_set(struct irs_acc *, struct __res_state *,
60*0Sstevel@tonic-gate 				void (*)(void *));
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /* public */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate struct irs_acc *
65*0Sstevel@tonic-gate irs_dns_acc(const char *options) {
66*0Sstevel@tonic-gate 	struct irs_acc *acc;
67*0Sstevel@tonic-gate 	struct dns_p *dns;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	UNUSED(options);
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	if (!(acc = memget(sizeof *acc))) {
72*0Sstevel@tonic-gate 		errno = ENOMEM;
73*0Sstevel@tonic-gate 		return (NULL);
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 	memset(acc, 0x5e, sizeof *acc);
76*0Sstevel@tonic-gate 	if (!(dns = memget(sizeof *dns))) {
77*0Sstevel@tonic-gate 		errno = ENOMEM;
78*0Sstevel@tonic-gate 		memput(acc, sizeof *acc);
79*0Sstevel@tonic-gate 		return (NULL);
80*0Sstevel@tonic-gate 	}
81*0Sstevel@tonic-gate 	memset(dns, 0x5e, sizeof *dns);
82*0Sstevel@tonic-gate 	dns->res = NULL;
83*0Sstevel@tonic-gate 	dns->free_res = NULL;
84*0Sstevel@tonic-gate 	if (hesiod_init(&dns->hes_ctx) < 0) {
85*0Sstevel@tonic-gate 		/*
86*0Sstevel@tonic-gate 		 * We allow the dns accessor class to initialize
87*0Sstevel@tonic-gate 		 * despite hesiod failing to initialize correctly,
88*0Sstevel@tonic-gate 		 * since dns host queries don't depend on hesiod.
89*0Sstevel@tonic-gate 		 */
90*0Sstevel@tonic-gate 		dns->hes_ctx = NULL;
91*0Sstevel@tonic-gate 	}
92*0Sstevel@tonic-gate 	acc->private = dns;
93*0Sstevel@tonic-gate #ifdef WANT_IRS_GR
94*0Sstevel@tonic-gate 	acc->gr_map = irs_dns_gr;
95*0Sstevel@tonic-gate #else
96*0Sstevel@tonic-gate 	acc->gr_map = NULL;
97*0Sstevel@tonic-gate #endif
98*0Sstevel@tonic-gate #ifdef WANT_IRS_PW
99*0Sstevel@tonic-gate 	acc->pw_map = irs_dns_pw;
100*0Sstevel@tonic-gate #else
101*0Sstevel@tonic-gate 	acc->pw_map = NULL;
102*0Sstevel@tonic-gate #endif
103*0Sstevel@tonic-gate 	acc->sv_map = irs_dns_sv;
104*0Sstevel@tonic-gate 	acc->pr_map = irs_dns_pr;
105*0Sstevel@tonic-gate 	acc->ho_map = irs_dns_ho;
106*0Sstevel@tonic-gate 	acc->nw_map = irs_dns_nw;
107*0Sstevel@tonic-gate 	acc->ng_map = irs_nul_ng;
108*0Sstevel@tonic-gate 	acc->res_get = dns_res_get;
109*0Sstevel@tonic-gate 	acc->res_set = dns_res_set;
110*0Sstevel@tonic-gate 	acc->close = dns_close;
111*0Sstevel@tonic-gate 	return (acc);
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate /* methods */
115*0Sstevel@tonic-gate static struct __res_state *
116*0Sstevel@tonic-gate dns_res_get(struct irs_acc *this) {
117*0Sstevel@tonic-gate 	struct dns_p *dns = (struct dns_p *)this->private;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	if (dns->res == NULL) {
120*0Sstevel@tonic-gate 		struct __res_state *res;
121*0Sstevel@tonic-gate 		res = (struct __res_state *)malloc(sizeof *res);
122*0Sstevel@tonic-gate 		if (res == NULL)
123*0Sstevel@tonic-gate 			return (NULL);
124*0Sstevel@tonic-gate 		memset(dns->res, 0, sizeof *dns->res);
125*0Sstevel@tonic-gate 		dns_res_set(this, res, free);
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	if ((dns->res->options & RES_INIT) == 0 &&
129*0Sstevel@tonic-gate 	    res_ninit(dns->res) < 0)
130*0Sstevel@tonic-gate 		return (NULL);
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	return (dns->res);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate static void
136*0Sstevel@tonic-gate dns_res_set(struct irs_acc *this, struct __res_state *res,
137*0Sstevel@tonic-gate 	    void (*free_res)(void *)) {
138*0Sstevel@tonic-gate 	struct dns_p *dns = (struct dns_p *)this->private;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	if (dns->res && dns->free_res) {
141*0Sstevel@tonic-gate 		res_nclose(dns->res);
142*0Sstevel@tonic-gate 		(*dns->free_res)(dns->res);
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate 	dns->res = res;
145*0Sstevel@tonic-gate 	dns->free_res = free_res;
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate static void
149*0Sstevel@tonic-gate dns_close(struct irs_acc *this) {
150*0Sstevel@tonic-gate 	struct dns_p *dns;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	dns = (struct dns_p *)this->private;
153*0Sstevel@tonic-gate 	if (dns->res && dns->free_res)
154*0Sstevel@tonic-gate 		(*dns->free_res)(dns->res);
155*0Sstevel@tonic-gate 	if (dns->hes_ctx)
156*0Sstevel@tonic-gate 		hesiod_end(dns->hes_ctx);
157*0Sstevel@tonic-gate 	memput(dns, sizeof *dns);
158*0Sstevel@tonic-gate 	memput(this, sizeof *this);
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate 
161