2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
18 #if !defined(LINT) && !defined(CODECENTER)
19 static const char rcsid[] = "$Id: gen_nw.c,v 1.1.206.2 2004/03/17 01:49:40 marka Exp $";
24 #include "port_before.h"
26 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
36 #include <isc/memcluster.h>
39 #include "port_after.h"
47 struct irs_rule * rules;
48 struct irs_rule * rule;
49 struct __res_state * res;
50 void (*free_res)(void *);
55 static void nw_close(struct irs_nw*);
56 static struct nwent * nw_next(struct irs_nw *);
57 static struct nwent * nw_byname(struct irs_nw *, const char *, int);
58 static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int);
59 static void nw_rewind(struct irs_nw *);
60 static void nw_minimize(struct irs_nw *);
61 static struct __res_state * nw_res_get(struct irs_nw *this);
62 static void nw_res_set(struct irs_nw *this,
63 struct __res_state *res,
64 void (*free_res)(void *));
66 static int init(struct irs_nw *this);
71 irs_gen_nw(struct irs_acc *this) {
72 struct gen_p *accpvt = (struct gen_p *)this->private;
76 if (!(pvt = memget(sizeof *pvt))) {
80 memset(pvt, 0, sizeof *pvt);
81 if (!(nw = memget(sizeof *nw))) {
82 memput(pvt, sizeof *pvt);
86 memset(nw, 0x5e, sizeof *nw);
87 pvt->rules = accpvt->map_rules[irs_nw];
88 pvt->rule = pvt->rules;
92 nw->byname = nw_byname;
93 nw->byaddr = nw_byaddr;
94 nw->rewind = nw_rewind;
95 nw->minimize = nw_minimize;
96 nw->res_get = nw_res_get;
97 nw->res_set = nw_res_set;
104 nw_close(struct irs_nw *this) {
105 struct pvt *pvt = (struct pvt *)this->private;
109 if (pvt->res && pvt->free_res)
110 (*pvt->free_res)(pvt->res);
112 memput(pvt, sizeof *pvt);
113 memput(this, sizeof *this);
116 static struct nwent *
117 nw_next(struct irs_nw *this) {
118 struct pvt *pvt = (struct pvt *)this->private;
122 if (init(this) == -1)
126 nw = pvt->rule->inst->nw;
127 rval = (*nw->next)(nw);
130 if (!(pvt->rules->flags & IRS_CONTINUE))
132 pvt->rule = pvt->rule->next;
134 nw = pvt->rule->inst->nw;
141 static struct nwent *
142 nw_byname(struct irs_nw *this, const char *name, int type) {
143 struct pvt *pvt = (struct pvt *)this->private;
144 struct irs_rule *rule;
148 if (init(this) == -1)
151 for (rule = pvt->rules; rule; rule = rule->next) {
153 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
154 rval = (*nw->byname)(nw, name, type);
157 if (pvt->res->res_h_errno != TRY_AGAIN &&
158 !(rule->flags & IRS_CONTINUE))
164 static struct nwent *
165 nw_byaddr(struct irs_nw *this, void *net, int length, int type) {
166 struct pvt *pvt = (struct pvt *)this->private;
167 struct irs_rule *rule;
171 if (init(this) == -1)
174 for (rule = pvt->rules; rule; rule = rule->next) {
176 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
177 rval = (*nw->byaddr)(nw, net, length, type);
180 if (pvt->res->res_h_errno != TRY_AGAIN &&
181 !(rule->flags & IRS_CONTINUE))
188 nw_rewind(struct irs_nw *this) {
189 struct pvt *pvt = (struct pvt *)this->private;
192 pvt->rule = pvt->rules;
194 nw = pvt->rule->inst->nw;
200 nw_minimize(struct irs_nw *this) {
201 struct pvt *pvt = (struct pvt *)this->private;
202 struct irs_rule *rule;
205 res_nclose(pvt->res);
206 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
207 struct irs_nw *nw = rule->inst->nw;
213 static struct __res_state *
214 nw_res_get(struct irs_nw *this) {
215 struct pvt *pvt = (struct pvt *)this->private;
218 struct __res_state *res;
219 res = (struct __res_state *)malloc(sizeof *res);
224 memset(res, 0, sizeof *res);
225 nw_res_set(this, res, free);
232 nw_res_set(struct irs_nw *this, struct __res_state *res,
233 void (*free_res)(void *)) {
234 struct pvt *pvt = (struct pvt *)this->private;
235 struct irs_rule *rule;
237 if (pvt->res && pvt->free_res) {
238 res_nclose(pvt->res);
239 (*pvt->free_res)(pvt->res);
243 pvt->free_res = free_res;
245 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
246 struct irs_nw *nw = rule->inst->nw;
248 (*nw->res_set)(nw, pvt->res, NULL);
253 init(struct irs_nw *this) {
254 struct pvt *pvt = (struct pvt *)this->private;
256 if (!pvt->res && !nw_res_get(this))
258 if (((pvt->res->options & RES_INIT) == 0U) &&
259 res_ninit(pvt->res) == -1)