1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 1997-2000 by Sun Microsystems, Inc.
3*0Sstevel@tonic-gate  * All rights reserved.
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(LINT) && !defined(CODECENTER)
26*0Sstevel@tonic-gate static const char rcsid[] = "$Id: gen_pr.c,v 1.12 1999/10/13 16:39:30 vixie Exp $";
27*0Sstevel@tonic-gate #endif
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /* Imports */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include "port_before.h"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <netinet/in.h>
35*0Sstevel@tonic-gate #include <arpa/nameser.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <errno.h>
38*0Sstevel@tonic-gate #include <resolv.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <string.h>
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #include <isc/memcluster.h>
43*0Sstevel@tonic-gate #include <irs.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include "port_after.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #include "irs_p.h"
48*0Sstevel@tonic-gate #include "gen_p.h"
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /* Types */
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate struct pvt {
53*0Sstevel@tonic-gate 	struct irs_rule *	rules;
54*0Sstevel@tonic-gate 	struct irs_rule *	rule;
55*0Sstevel@tonic-gate 	struct __res_state *	res;
56*0Sstevel@tonic-gate 	void			(*free_res)(void *);
57*0Sstevel@tonic-gate };
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /* Forward */
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate static void			pr_close(struct irs_pr*);
62*0Sstevel@tonic-gate static struct protoent *	pr_next(struct irs_pr *);
63*0Sstevel@tonic-gate static struct protoent *	pr_byname(struct irs_pr *, const char *);
64*0Sstevel@tonic-gate static struct protoent * 	pr_bynumber(struct irs_pr *, int);
65*0Sstevel@tonic-gate static void 			pr_rewind(struct irs_pr *);
66*0Sstevel@tonic-gate static void			pr_minimize(struct irs_pr *);
67*0Sstevel@tonic-gate static struct __res_state *	pr_res_get(struct irs_pr *);
68*0Sstevel@tonic-gate static void			pr_res_set(struct irs_pr *,
69*0Sstevel@tonic-gate 					   struct __res_state *,
70*0Sstevel@tonic-gate 					   void (*)(void *));
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate /* Public */
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate struct irs_pr *
75*0Sstevel@tonic-gate irs_gen_pr(struct irs_acc *this) {
76*0Sstevel@tonic-gate 	struct gen_p *accpvt = (struct gen_p *)this->private;
77*0Sstevel@tonic-gate 	struct irs_pr *pr;
78*0Sstevel@tonic-gate 	struct pvt *pvt;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	if (!(pr = memget(sizeof *pr))) {
81*0Sstevel@tonic-gate 		errno = ENOMEM;
82*0Sstevel@tonic-gate 		return (NULL);
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 	memset(pr, 0x5e, sizeof *pr);
85*0Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
86*0Sstevel@tonic-gate 		memput(pr, sizeof *pr);
87*0Sstevel@tonic-gate 		errno = ENOMEM;
88*0Sstevel@tonic-gate 		return (NULL);
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
91*0Sstevel@tonic-gate 	pvt->rules = accpvt->map_rules[irs_pr];
92*0Sstevel@tonic-gate 	pvt->rule = pvt->rules;
93*0Sstevel@tonic-gate 	pr->private = pvt;
94*0Sstevel@tonic-gate 	pr->close = pr_close;
95*0Sstevel@tonic-gate 	pr->next = pr_next;
96*0Sstevel@tonic-gate 	pr->byname = pr_byname;
97*0Sstevel@tonic-gate 	pr->bynumber = pr_bynumber;
98*0Sstevel@tonic-gate 	pr->rewind = pr_rewind;
99*0Sstevel@tonic-gate 	pr->minimize = pr_minimize;
100*0Sstevel@tonic-gate 	pr->res_get = pr_res_get;
101*0Sstevel@tonic-gate 	pr->res_set = pr_res_set;
102*0Sstevel@tonic-gate 	return (pr);
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate /* Methods */
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate static void
108*0Sstevel@tonic-gate pr_close(struct irs_pr *this) {
109*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
112*0Sstevel@tonic-gate 	memput(this, sizeof *this);
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate static struct protoent *
116*0Sstevel@tonic-gate pr_next(struct irs_pr *this) {
117*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
118*0Sstevel@tonic-gate 	struct protoent *rval;
119*0Sstevel@tonic-gate 	struct irs_pr *pr;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	while (pvt->rule) {
122*0Sstevel@tonic-gate 		pr = pvt->rule->inst->pr;
123*0Sstevel@tonic-gate 		rval = (*pr->next)(pr);
124*0Sstevel@tonic-gate 		if (rval)
125*0Sstevel@tonic-gate 			return (rval);
126*0Sstevel@tonic-gate 		if (!(pvt->rules->flags & IRS_CONTINUE))
127*0Sstevel@tonic-gate 			break;
128*0Sstevel@tonic-gate 		pvt->rule = pvt->rule->next;
129*0Sstevel@tonic-gate 		if (pvt->rule) {
130*0Sstevel@tonic-gate 			pr = pvt->rule->inst->pr;
131*0Sstevel@tonic-gate 			(*pr->rewind)(pr);
132*0Sstevel@tonic-gate 		}
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 	return (NULL);
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate static struct protoent *
138*0Sstevel@tonic-gate pr_byname(struct irs_pr *this, const char *name) {
139*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
140*0Sstevel@tonic-gate 	struct irs_rule *rule;
141*0Sstevel@tonic-gate 	struct protoent *rval;
142*0Sstevel@tonic-gate 	struct irs_pr *pr;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	rval = NULL;
145*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
146*0Sstevel@tonic-gate 		pr = rule->inst->pr;
147*0Sstevel@tonic-gate 		rval = (*pr->byname)(pr, name);
148*0Sstevel@tonic-gate 		if (rval || !(rule->flags & IRS_CONTINUE))
149*0Sstevel@tonic-gate 			break;
150*0Sstevel@tonic-gate 	}
151*0Sstevel@tonic-gate 	return (rval);
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate static struct protoent *
155*0Sstevel@tonic-gate pr_bynumber(struct irs_pr *this, int proto) {
156*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
157*0Sstevel@tonic-gate 	struct irs_rule *rule;
158*0Sstevel@tonic-gate 	struct protoent *rval;
159*0Sstevel@tonic-gate 	struct irs_pr *pr;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	rval = NULL;
162*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
163*0Sstevel@tonic-gate 		pr = rule->inst->pr;
164*0Sstevel@tonic-gate 		rval = (*pr->bynumber)(pr, proto);
165*0Sstevel@tonic-gate 		if (rval || !(rule->flags & IRS_CONTINUE))
166*0Sstevel@tonic-gate 			break;
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate 	return (rval);
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate static void
172*0Sstevel@tonic-gate pr_rewind(struct irs_pr *this) {
173*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
174*0Sstevel@tonic-gate 	struct irs_pr *pr;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	pvt->rule = pvt->rules;
177*0Sstevel@tonic-gate 	if (pvt->rule) {
178*0Sstevel@tonic-gate 		pr = pvt->rule->inst->pr;
179*0Sstevel@tonic-gate 		(*pr->rewind)(pr);
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate static void
184*0Sstevel@tonic-gate pr_minimize(struct irs_pr *this) {
185*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
186*0Sstevel@tonic-gate 	struct irs_rule *rule;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
189*0Sstevel@tonic-gate 		struct irs_pr *pr = rule->inst->pr;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 		(*pr->minimize)(pr);
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate }
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate static struct __res_state *
196*0Sstevel@tonic-gate pr_res_get(struct irs_pr *this) {
197*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if (!pvt->res) {
200*0Sstevel@tonic-gate 		struct __res_state *res;
201*0Sstevel@tonic-gate 		res = (struct __res_state *)malloc(sizeof *res);
202*0Sstevel@tonic-gate 		if (!res) {
203*0Sstevel@tonic-gate 			errno = ENOMEM;
204*0Sstevel@tonic-gate 			return (NULL);
205*0Sstevel@tonic-gate 		}
206*0Sstevel@tonic-gate 		memset(res, 0, sizeof *res);
207*0Sstevel@tonic-gate 		pr_res_set(this, res, free);
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	return (pvt->res);
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate static void
214*0Sstevel@tonic-gate pr_res_set(struct irs_pr *this, struct __res_state *res,
215*0Sstevel@tonic-gate 		void (*free_res)(void *)) {
216*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
217*0Sstevel@tonic-gate 	struct irs_rule *rule;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	if (pvt->res && pvt->free_res) {
220*0Sstevel@tonic-gate 		res_nclose(pvt->res);
221*0Sstevel@tonic-gate 		(*pvt->free_res)(pvt->res);
222*0Sstevel@tonic-gate 	}
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	pvt->res = res;
225*0Sstevel@tonic-gate 	pvt->free_res = free_res;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
228*0Sstevel@tonic-gate 		struct irs_pr *pr = rule->inst->pr;
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 		if (pr->res_set)
231*0Sstevel@tonic-gate 			(*pr->res_set)(pr, pvt->res, NULL);
232*0Sstevel@tonic-gate 	}
233*0Sstevel@tonic-gate }
234