gen_nw.c revision 1.1.1.2 1 1.1 christos /* $NetBSD: gen_nw.c,v 1.1.1.2 2012/09/09 16:07:55 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos * Copyright (c) 1996,1999 by Internet Software Consortium.
6 1.1 christos *
7 1.1 christos * Permission to use, copy, modify, and distribute this software for any
8 1.1 christos * purpose with or without fee is hereby granted, provided that the above
9 1.1 christos * copyright notice and this permission notice appear in all copies.
10 1.1 christos *
11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 christos */
19 1.1 christos
20 1.1 christos #if !defined(LINT) && !defined(CODECENTER)
21 1.1.1.2 christos static const char rcsid[] = "Id: gen_nw.c,v 1.4 2005/04/27 04:56:23 sra Exp ";
22 1.1 christos #endif
23 1.1 christos
24 1.1 christos /* Imports */
25 1.1 christos
26 1.1 christos #include "port_before.h"
27 1.1 christos
28 1.1 christos #include <sys/types.h>
29 1.1 christos
30 1.1 christos #include <netinet/in.h>
31 1.1 christos #include <arpa/nameser.h>
32 1.1 christos
33 1.1 christos #include <errno.h>
34 1.1 christos #include <resolv.h>
35 1.1 christos #include <stdlib.h>
36 1.1 christos #include <string.h>
37 1.1 christos
38 1.1 christos #include <isc/memcluster.h>
39 1.1 christos #include <irs.h>
40 1.1 christos
41 1.1 christos #include "port_after.h"
42 1.1 christos
43 1.1 christos #include "irs_p.h"
44 1.1 christos #include "gen_p.h"
45 1.1 christos
46 1.1 christos /* Types */
47 1.1 christos
48 1.1 christos struct pvt {
49 1.1 christos struct irs_rule * rules;
50 1.1 christos struct irs_rule * rule;
51 1.1 christos struct __res_state * res;
52 1.1 christos void (*free_res)(void *);
53 1.1 christos };
54 1.1 christos
55 1.1 christos /* Forward */
56 1.1 christos
57 1.1 christos static void nw_close(struct irs_nw*);
58 1.1 christos static struct nwent * nw_next(struct irs_nw *);
59 1.1 christos static struct nwent * nw_byname(struct irs_nw *, const char *, int);
60 1.1 christos static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int);
61 1.1 christos static void nw_rewind(struct irs_nw *);
62 1.1 christos static void nw_minimize(struct irs_nw *);
63 1.1 christos static struct __res_state * nw_res_get(struct irs_nw *this);
64 1.1 christos static void nw_res_set(struct irs_nw *this,
65 1.1 christos struct __res_state *res,
66 1.1 christos void (*free_res)(void *));
67 1.1 christos
68 1.1 christos static int init(struct irs_nw *this);
69 1.1 christos
70 1.1 christos /* Public */
71 1.1 christos
72 1.1 christos struct irs_nw *
73 1.1 christos irs_gen_nw(struct irs_acc *this) {
74 1.1 christos struct gen_p *accpvt = (struct gen_p *)this->private;
75 1.1 christos struct irs_nw *nw;
76 1.1 christos struct pvt *pvt;
77 1.1 christos
78 1.1 christos if (!(pvt = memget(sizeof *pvt))) {
79 1.1 christos errno = ENOMEM;
80 1.1 christos return (NULL);
81 1.1 christos }
82 1.1 christos memset(pvt, 0, sizeof *pvt);
83 1.1 christos if (!(nw = memget(sizeof *nw))) {
84 1.1 christos memput(pvt, sizeof *pvt);
85 1.1 christos errno = ENOMEM;
86 1.1 christos return (NULL);
87 1.1 christos }
88 1.1 christos memset(nw, 0x5e, sizeof *nw);
89 1.1 christos pvt->rules = accpvt->map_rules[irs_nw];
90 1.1 christos pvt->rule = pvt->rules;
91 1.1 christos nw->private = pvt;
92 1.1 christos nw->close = nw_close;
93 1.1 christos nw->next = nw_next;
94 1.1 christos nw->byname = nw_byname;
95 1.1 christos nw->byaddr = nw_byaddr;
96 1.1 christos nw->rewind = nw_rewind;
97 1.1 christos nw->minimize = nw_minimize;
98 1.1 christos nw->res_get = nw_res_get;
99 1.1 christos nw->res_set = nw_res_set;
100 1.1 christos return (nw);
101 1.1 christos }
102 1.1 christos
103 1.1 christos /* Methods */
104 1.1 christos
105 1.1 christos static void
106 1.1 christos nw_close(struct irs_nw *this) {
107 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
108 1.1 christos
109 1.1 christos nw_minimize(this);
110 1.1 christos
111 1.1 christos if (pvt->res && pvt->free_res)
112 1.1 christos (*pvt->free_res)(pvt->res);
113 1.1 christos
114 1.1 christos memput(pvt, sizeof *pvt);
115 1.1 christos memput(this, sizeof *this);
116 1.1 christos }
117 1.1 christos
118 1.1 christos static struct nwent *
119 1.1 christos nw_next(struct irs_nw *this) {
120 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
121 1.1 christos struct nwent *rval;
122 1.1 christos struct irs_nw *nw;
123 1.1 christos
124 1.1 christos if (init(this) == -1)
125 1.1 christos return(NULL);
126 1.1 christos
127 1.1 christos while (pvt->rule) {
128 1.1 christos nw = pvt->rule->inst->nw;
129 1.1 christos rval = (*nw->next)(nw);
130 1.1 christos if (rval)
131 1.1 christos return (rval);
132 1.1 christos if (!(pvt->rules->flags & IRS_CONTINUE))
133 1.1 christos break;
134 1.1 christos pvt->rule = pvt->rule->next;
135 1.1 christos if (pvt->rule) {
136 1.1 christos nw = pvt->rule->inst->nw;
137 1.1 christos (*nw->rewind)(nw);
138 1.1 christos }
139 1.1 christos }
140 1.1 christos return (NULL);
141 1.1 christos }
142 1.1 christos
143 1.1 christos static struct nwent *
144 1.1 christos nw_byname(struct irs_nw *this, const char *name, int type) {
145 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
146 1.1 christos struct irs_rule *rule;
147 1.1 christos struct nwent *rval;
148 1.1 christos struct irs_nw *nw;
149 1.1 christos
150 1.1 christos if (init(this) == -1)
151 1.1 christos return(NULL);
152 1.1 christos
153 1.1 christos for (rule = pvt->rules; rule; rule = rule->next) {
154 1.1 christos nw = rule->inst->nw;
155 1.1 christos RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
156 1.1 christos rval = (*nw->byname)(nw, name, type);
157 1.1 christos if (rval != NULL)
158 1.1 christos return (rval);
159 1.1 christos if (pvt->res->res_h_errno != TRY_AGAIN &&
160 1.1 christos !(rule->flags & IRS_CONTINUE))
161 1.1 christos break;
162 1.1 christos }
163 1.1 christos return (NULL);
164 1.1 christos }
165 1.1 christos
166 1.1 christos static struct nwent *
167 1.1 christos nw_byaddr(struct irs_nw *this, void *net, int length, int type) {
168 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
169 1.1 christos struct irs_rule *rule;
170 1.1 christos struct nwent *rval;
171 1.1 christos struct irs_nw *nw;
172 1.1 christos
173 1.1 christos if (init(this) == -1)
174 1.1 christos return(NULL);
175 1.1 christos
176 1.1 christos for (rule = pvt->rules; rule; rule = rule->next) {
177 1.1 christos nw = rule->inst->nw;
178 1.1 christos RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
179 1.1 christos rval = (*nw->byaddr)(nw, net, length, type);
180 1.1 christos if (rval != NULL)
181 1.1 christos return (rval);
182 1.1 christos if (pvt->res->res_h_errno != TRY_AGAIN &&
183 1.1 christos !(rule->flags & IRS_CONTINUE))
184 1.1 christos break;
185 1.1 christos }
186 1.1 christos return (NULL);
187 1.1 christos }
188 1.1 christos
189 1.1 christos static void
190 1.1 christos nw_rewind(struct irs_nw *this) {
191 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
192 1.1 christos struct irs_nw *nw;
193 1.1 christos
194 1.1 christos pvt->rule = pvt->rules;
195 1.1 christos if (pvt->rule) {
196 1.1 christos nw = pvt->rule->inst->nw;
197 1.1 christos (*nw->rewind)(nw);
198 1.1 christos }
199 1.1 christos }
200 1.1 christos
201 1.1 christos static void
202 1.1 christos nw_minimize(struct irs_nw *this) {
203 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
204 1.1 christos struct irs_rule *rule;
205 1.1 christos
206 1.1 christos if (pvt->res)
207 1.1 christos res_nclose(pvt->res);
208 1.1 christos for (rule = pvt->rules; rule != NULL; rule = rule->next) {
209 1.1 christos struct irs_nw *nw = rule->inst->nw;
210 1.1 christos
211 1.1 christos (*nw->minimize)(nw);
212 1.1 christos }
213 1.1 christos }
214 1.1 christos
215 1.1 christos static struct __res_state *
216 1.1 christos nw_res_get(struct irs_nw *this) {
217 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
218 1.1 christos
219 1.1 christos if (!pvt->res) {
220 1.1 christos struct __res_state *res;
221 1.1 christos res = (struct __res_state *)malloc(sizeof *res);
222 1.1 christos if (!res) {
223 1.1 christos errno = ENOMEM;
224 1.1 christos return (NULL);
225 1.1 christos }
226 1.1 christos memset(res, 0, sizeof *res);
227 1.1 christos nw_res_set(this, res, free);
228 1.1 christos }
229 1.1 christos
230 1.1 christos return (pvt->res);
231 1.1 christos }
232 1.1 christos
233 1.1 christos static void
234 1.1 christos nw_res_set(struct irs_nw *this, struct __res_state *res,
235 1.1 christos void (*free_res)(void *)) {
236 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
237 1.1 christos struct irs_rule *rule;
238 1.1 christos
239 1.1 christos if (pvt->res && pvt->free_res) {
240 1.1 christos res_nclose(pvt->res);
241 1.1 christos (*pvt->free_res)(pvt->res);
242 1.1 christos }
243 1.1 christos
244 1.1 christos pvt->res = res;
245 1.1 christos pvt->free_res = free_res;
246 1.1 christos
247 1.1 christos for (rule = pvt->rules; rule != NULL; rule = rule->next) {
248 1.1 christos struct irs_nw *nw = rule->inst->nw;
249 1.1 christos
250 1.1 christos (*nw->res_set)(nw, pvt->res, NULL);
251 1.1 christos }
252 1.1 christos }
253 1.1 christos
254 1.1 christos static int
255 1.1 christos init(struct irs_nw *this) {
256 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
257 1.1 christos
258 1.1 christos if (!pvt->res && !nw_res_get(this))
259 1.1 christos return (-1);
260 1.1 christos if (((pvt->res->options & RES_INIT) == 0U) &&
261 1.1 christos res_ninit(pvt->res) == -1)
262 1.1 christos return (-1);
263 1.1 christos return (0);
264 1.1 christos }
265 1.1 christos
266 1.1 christos /*! \file */
267