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