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_sv.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 #include <resolv.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include <errno.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			sv_close(struct irs_sv*);
62*0Sstevel@tonic-gate static struct servent *		sv_next(struct irs_sv *);
63*0Sstevel@tonic-gate static struct servent *		sv_byname(struct irs_sv *, const char *,
64*0Sstevel@tonic-gate 					  const char *);
65*0Sstevel@tonic-gate static struct servent *		sv_byport(struct irs_sv *, int, const char *);
66*0Sstevel@tonic-gate static void			sv_rewind(struct irs_sv *);
67*0Sstevel@tonic-gate static void			sv_minimize(struct irs_sv *);
68*0Sstevel@tonic-gate static struct __res_state *	sv_res_get(struct irs_sv *);
69*0Sstevel@tonic-gate static void			sv_res_set(struct irs_sv *,
70*0Sstevel@tonic-gate 					      struct __res_state *,
71*0Sstevel@tonic-gate 					      void (*)(void *));
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /* Public */
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate struct irs_sv *
76*0Sstevel@tonic-gate irs_gen_sv(struct irs_acc *this) {
77*0Sstevel@tonic-gate 	struct gen_p *accpvt = (struct gen_p *)this->private;
78*0Sstevel@tonic-gate 	struct irs_sv *sv;
79*0Sstevel@tonic-gate 	struct pvt *pvt;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	if (!(sv = memget(sizeof *sv))) {
82*0Sstevel@tonic-gate 		errno = ENOMEM;
83*0Sstevel@tonic-gate 		return (NULL);
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 	memset(sv, 0x5e, sizeof *sv);
86*0Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
87*0Sstevel@tonic-gate 		memput(sv, sizeof *sv);
88*0Sstevel@tonic-gate 		errno = ENOMEM;
89*0Sstevel@tonic-gate 		return (NULL);
90*0Sstevel@tonic-gate 	}
91*0Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
92*0Sstevel@tonic-gate 	pvt->rules = accpvt->map_rules[irs_sv];
93*0Sstevel@tonic-gate 	pvt->rule = pvt->rules;
94*0Sstevel@tonic-gate 	sv->private = pvt;
95*0Sstevel@tonic-gate 	sv->close = sv_close;
96*0Sstevel@tonic-gate 	sv->next = sv_next;
97*0Sstevel@tonic-gate 	sv->byname = sv_byname;
98*0Sstevel@tonic-gate 	sv->byport = sv_byport;
99*0Sstevel@tonic-gate 	sv->rewind = sv_rewind;
100*0Sstevel@tonic-gate 	sv->minimize = sv_minimize;
101*0Sstevel@tonic-gate 	sv->res_get = sv_res_get;
102*0Sstevel@tonic-gate 	sv->res_set = sv_res_set;
103*0Sstevel@tonic-gate 	return (sv);
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate /* Methods */
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate static void
109*0Sstevel@tonic-gate sv_close(struct irs_sv *this) {
110*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
113*0Sstevel@tonic-gate 	memput(this, sizeof *this);
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate static struct servent *
117*0Sstevel@tonic-gate sv_next(struct irs_sv *this) {
118*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
119*0Sstevel@tonic-gate 	struct servent *rval;
120*0Sstevel@tonic-gate 	struct irs_sv *sv;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	while (pvt->rule) {
123*0Sstevel@tonic-gate 		sv = pvt->rule->inst->sv;
124*0Sstevel@tonic-gate 		rval = (*sv->next)(sv);
125*0Sstevel@tonic-gate 		if (rval)
126*0Sstevel@tonic-gate 			return (rval);
127*0Sstevel@tonic-gate 		if (!(pvt->rule->flags & IRS_CONTINUE))
128*0Sstevel@tonic-gate 			break;
129*0Sstevel@tonic-gate 		pvt->rule = pvt->rule->next;
130*0Sstevel@tonic-gate 		if (pvt->rule) {
131*0Sstevel@tonic-gate 			sv = pvt->rule->inst->sv;
132*0Sstevel@tonic-gate 			(*sv->rewind)(sv);
133*0Sstevel@tonic-gate 		}
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate 	return (NULL);
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate static struct servent *
139*0Sstevel@tonic-gate sv_byname(struct irs_sv *this, const char *name, const char *proto) {
140*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
141*0Sstevel@tonic-gate 	struct irs_rule *rule;
142*0Sstevel@tonic-gate 	struct servent *rval;
143*0Sstevel@tonic-gate 	struct irs_sv *sv;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	rval = NULL;
146*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
147*0Sstevel@tonic-gate 		sv = rule->inst->sv;
148*0Sstevel@tonic-gate 		rval = (*sv->byname)(sv, name, proto);
149*0Sstevel@tonic-gate 		if (rval || !(rule->flags & IRS_CONTINUE))
150*0Sstevel@tonic-gate 			break;
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 	return (rval);
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate static struct servent *
156*0Sstevel@tonic-gate sv_byport(struct irs_sv *this, int port, const char *proto) {
157*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
158*0Sstevel@tonic-gate 	struct irs_rule *rule;
159*0Sstevel@tonic-gate 	struct servent *rval;
160*0Sstevel@tonic-gate 	struct irs_sv *sv;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	rval = NULL;
163*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
164*0Sstevel@tonic-gate 		sv = rule->inst->sv;
165*0Sstevel@tonic-gate 		rval = (*sv->byport)(sv, port, proto);
166*0Sstevel@tonic-gate 		if (rval || !(rule->flags & IRS_CONTINUE))
167*0Sstevel@tonic-gate 			break;
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate 	return (rval);
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate static void
173*0Sstevel@tonic-gate sv_rewind(struct irs_sv *this) {
174*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
175*0Sstevel@tonic-gate 	struct irs_sv *sv;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	pvt->rule = pvt->rules;
178*0Sstevel@tonic-gate 	if (pvt->rule) {
179*0Sstevel@tonic-gate 		sv = pvt->rule->inst->sv;
180*0Sstevel@tonic-gate 		(*sv->rewind)(sv);
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate }
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate static void
185*0Sstevel@tonic-gate sv_minimize(struct irs_sv *this) {
186*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
187*0Sstevel@tonic-gate 	struct irs_rule *rule;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
190*0Sstevel@tonic-gate 		struct irs_sv *sv = rule->inst->sv;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 		(*sv->minimize)(sv);
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate }
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate static struct __res_state *
197*0Sstevel@tonic-gate sv_res_get(struct irs_sv *this) {
198*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	if (!pvt->res) {
201*0Sstevel@tonic-gate 		struct __res_state *res;
202*0Sstevel@tonic-gate 		res = (struct __res_state *)malloc(sizeof *res);
203*0Sstevel@tonic-gate 		if (!res) {
204*0Sstevel@tonic-gate 			errno = ENOMEM;
205*0Sstevel@tonic-gate 			return (NULL);
206*0Sstevel@tonic-gate 		}
207*0Sstevel@tonic-gate 		memset(res, 0, sizeof *res);
208*0Sstevel@tonic-gate 		sv_res_set(this, res, free);
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	return (pvt->res);
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate static void
215*0Sstevel@tonic-gate sv_res_set(struct irs_sv *this, struct __res_state *res,
216*0Sstevel@tonic-gate 		void (*free_res)(void *)) {
217*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
218*0Sstevel@tonic-gate 	struct irs_rule *rule;
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	if (pvt->res && pvt->free_res) {
221*0Sstevel@tonic-gate 		res_nclose(pvt->res);
222*0Sstevel@tonic-gate 		(*pvt->free_res)(pvt->res);
223*0Sstevel@tonic-gate 	}
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	pvt->res = res;
226*0Sstevel@tonic-gate 	pvt->free_res = free_res;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
229*0Sstevel@tonic-gate 		struct irs_sv *sv = rule->inst->sv;
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 		if (sv->res_set)
232*0Sstevel@tonic-gate 			(*sv->res_set)(sv, pvt->res, NULL);
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate }
235