xref: /illumos-gate/usr/src/lib/libresolv2/common/irs/gen_pr.c (revision 30165b7f6753bc3d48c52319bed7ec7b3ea36b3c)
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996,1999 by Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /* Imports */
19 
20 #include "port_before.h"
21 
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
25 
26 #include <errno.h>
27 #include <resolv.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include <isc/memcluster.h>
32 #include <irs.h>
33 
34 #include "port_after.h"
35 
36 #include "irs_p.h"
37 #include "gen_p.h"
38 
39 /* Types */
40 
41 struct pvt {
42 	struct irs_rule *	rules;
43 	struct irs_rule *	rule;
44 	struct __res_state *	res;
45 	void			(*free_res)(void *);
46 };
47 
48 /* Forward */
49 
50 static void			pr_close(struct irs_pr*);
51 static struct protoent *	pr_next(struct irs_pr *);
52 static struct protoent *	pr_byname(struct irs_pr *, const char *);
53 static struct protoent * 	pr_bynumber(struct irs_pr *, int);
54 static void 			pr_rewind(struct irs_pr *);
55 static void			pr_minimize(struct irs_pr *);
56 static struct __res_state *	pr_res_get(struct irs_pr *);
57 static void			pr_res_set(struct irs_pr *,
58 					   struct __res_state *,
59 					   void (*)(void *));
60 
61 /* Public */
62 
63 struct irs_pr *
64 irs_gen_pr(struct irs_acc *this) {
65 	struct gen_p *accpvt = (struct gen_p *)this->private;
66 	struct irs_pr *pr;
67 	struct pvt *pvt;
68 
69 	if (!(pr = memget(sizeof *pr))) {
70 		errno = ENOMEM;
71 		return (NULL);
72 	}
73 	memset(pr, 0x5e, sizeof *pr);
74 	if (!(pvt = memget(sizeof *pvt))) {
75 		memput(pr, sizeof *pr);
76 		errno = ENOMEM;
77 		return (NULL);
78 	}
79 	memset(pvt, 0, sizeof *pvt);
80 	pvt->rules = accpvt->map_rules[irs_pr];
81 	pvt->rule = pvt->rules;
82 	pr->private = pvt;
83 	pr->close = pr_close;
84 	pr->next = pr_next;
85 	pr->byname = pr_byname;
86 	pr->bynumber = pr_bynumber;
87 	pr->rewind = pr_rewind;
88 	pr->minimize = pr_minimize;
89 	pr->res_get = pr_res_get;
90 	pr->res_set = pr_res_set;
91 	return (pr);
92 }
93 
94 /* Methods */
95 
96 static void
97 pr_close(struct irs_pr *this) {
98 	struct pvt *pvt = (struct pvt *)this->private;
99 
100 	memput(pvt, sizeof *pvt);
101 	memput(this, sizeof *this);
102 }
103 
104 static struct protoent *
105 pr_next(struct irs_pr *this) {
106 	struct pvt *pvt = (struct pvt *)this->private;
107 	struct protoent *rval;
108 	struct irs_pr *pr;
109 
110 	while (pvt->rule) {
111 		pr = pvt->rule->inst->pr;
112 		rval = (*pr->next)(pr);
113 		if (rval)
114 			return (rval);
115 		if (!(pvt->rules->flags & IRS_CONTINUE))
116 			break;
117 		pvt->rule = pvt->rule->next;
118 		if (pvt->rule) {
119 			pr = pvt->rule->inst->pr;
120 			(*pr->rewind)(pr);
121 		}
122 	}
123 	return (NULL);
124 }
125 
126 static struct protoent *
127 pr_byname(struct irs_pr *this, const char *name) {
128 	struct pvt *pvt = (struct pvt *)this->private;
129 	struct irs_rule *rule;
130 	struct protoent *rval;
131 	struct irs_pr *pr;
132 
133 	rval = NULL;
134 	for (rule = pvt->rules; rule; rule = rule->next) {
135 		pr = rule->inst->pr;
136 		rval = (*pr->byname)(pr, name);
137 		if (rval || !(rule->flags & IRS_CONTINUE))
138 			break;
139 	}
140 	return (rval);
141 }
142 
143 static struct protoent *
144 pr_bynumber(struct irs_pr *this, int proto) {
145 	struct pvt *pvt = (struct pvt *)this->private;
146 	struct irs_rule *rule;
147 	struct protoent *rval;
148 	struct irs_pr *pr;
149 
150 	rval = NULL;
151 	for (rule = pvt->rules; rule; rule = rule->next) {
152 		pr = rule->inst->pr;
153 		rval = (*pr->bynumber)(pr, proto);
154 		if (rval || !(rule->flags & IRS_CONTINUE))
155 			break;
156 	}
157 	return (rval);
158 }
159 
160 static void
161 pr_rewind(struct irs_pr *this) {
162 	struct pvt *pvt = (struct pvt *)this->private;
163 	struct irs_pr *pr;
164 
165 	pvt->rule = pvt->rules;
166 	if (pvt->rule) {
167 		pr = pvt->rule->inst->pr;
168 		(*pr->rewind)(pr);
169 	}
170 }
171 
172 static void
173 pr_minimize(struct irs_pr *this) {
174 	struct pvt *pvt = (struct pvt *)this->private;
175 	struct irs_rule *rule;
176 
177 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
178 		struct irs_pr *pr = rule->inst->pr;
179 
180 		(*pr->minimize)(pr);
181 	}
182 }
183 
184 static struct __res_state *
185 pr_res_get(struct irs_pr *this) {
186 	struct pvt *pvt = (struct pvt *)this->private;
187 
188 	if (!pvt->res) {
189 		struct __res_state *res;
190 		res = (struct __res_state *)malloc(sizeof *res);
191 		if (!res) {
192 			errno = ENOMEM;
193 			return (NULL);
194 		}
195 		memset(res, 0, sizeof *res);
196 		pr_res_set(this, res, free);
197 	}
198 
199 	return (pvt->res);
200 }
201 
202 static void
203 pr_res_set(struct irs_pr *this, struct __res_state *res,
204 		void (*free_res)(void *)) {
205 	struct pvt *pvt = (struct pvt *)this->private;
206 	struct irs_rule *rule;
207 
208 	if (pvt->res && pvt->free_res) {
209 		res_nclose(pvt->res);
210 		(*pvt->free_res)(pvt->res);
211 	}
212 
213 	pvt->res = res;
214 	pvt->free_res = free_res;
215 
216 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
217 		struct irs_pr *pr = rule->inst->pr;
218 
219 		if (pr->res_set)
220 			(*pr->res_set)(pr, pvt->res, NULL);
221 	}
222 }
223 
224 /*! \file */
225