Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / gen_nw.c
blob96f4be59903834e66f59ae64e5135c2b3bb728fd
1 /*
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.2.2 2004/03/17 01:54:20 marka Exp $";
20 #endif
22 /* Imports */
24 #include "port_before.h"
26 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
31 #include <errno.h>
32 #include <resolv.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include <isc/memcluster.h>
37 #include <irs.h>
39 #include "port_after.h"
41 #include "irs_p.h"
42 #include "gen_p.h"
44 /* Types */
46 struct pvt {
47 struct irs_rule * rules;
48 struct irs_rule * rule;
49 struct __res_state * res;
50 void (*free_res)(void *);
53 /* Forward */
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);
68 /* Public */
70 struct irs_nw *
71 irs_gen_nw(struct irs_acc *this) {
72 struct gen_p *accpvt = (struct gen_p *)this->private;
73 struct irs_nw *nw;
74 struct pvt *pvt;
76 if (!(pvt = memget(sizeof *pvt))) {
77 errno = ENOMEM;
78 return (NULL);
80 memset(pvt, 0, sizeof *pvt);
81 if (!(nw = memget(sizeof *nw))) {
82 memput(pvt, sizeof *pvt);
83 errno = ENOMEM;
84 return (NULL);
86 memset(nw, 0x5e, sizeof *nw);
87 pvt->rules = accpvt->map_rules[irs_nw];
88 pvt->rule = pvt->rules;
89 nw->private = pvt;
90 nw->close = nw_close;
91 nw->next = nw_next;
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;
98 return (nw);
101 /* Methods */
103 static void
104 nw_close(struct irs_nw *this) {
105 struct pvt *pvt = (struct pvt *)this->private;
107 nw_minimize(this);
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;
119 struct nwent *rval;
120 struct irs_nw *nw;
122 if (init(this) == -1)
123 return(NULL);
125 while (pvt->rule) {
126 nw = pvt->rule->inst->nw;
127 rval = (*nw->next)(nw);
128 if (rval)
129 return (rval);
130 if (!(pvt->rules->flags & IRS_CONTINUE))
131 break;
132 pvt->rule = pvt->rule->next;
133 if (pvt->rule) {
134 nw = pvt->rule->inst->nw;
135 (*nw->rewind)(nw);
138 return (NULL);
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;
145 struct nwent *rval;
146 struct irs_nw *nw;
148 if (init(this) == -1)
149 return(NULL);
151 for (rule = pvt->rules; rule; rule = rule->next) {
152 nw = rule->inst->nw;
153 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
154 rval = (*nw->byname)(nw, name, type);
155 if (rval != NULL)
156 return (rval);
157 if (pvt->res->res_h_errno != TRY_AGAIN &&
158 !(rule->flags & IRS_CONTINUE))
159 break;
161 return (NULL);
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;
168 struct nwent *rval;
169 struct irs_nw *nw;
171 if (init(this) == -1)
172 return(NULL);
174 for (rule = pvt->rules; rule; rule = rule->next) {
175 nw = rule->inst->nw;
176 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
177 rval = (*nw->byaddr)(nw, net, length, type);
178 if (rval != NULL)
179 return (rval);
180 if (pvt->res->res_h_errno != TRY_AGAIN &&
181 !(rule->flags & IRS_CONTINUE))
182 break;
184 return (NULL);
187 static void
188 nw_rewind(struct irs_nw *this) {
189 struct pvt *pvt = (struct pvt *)this->private;
190 struct irs_nw *nw;
192 pvt->rule = pvt->rules;
193 if (pvt->rule) {
194 nw = pvt->rule->inst->nw;
195 (*nw->rewind)(nw);
199 static void
200 nw_minimize(struct irs_nw *this) {
201 struct pvt *pvt = (struct pvt *)this->private;
202 struct irs_rule *rule;
204 if (pvt->res)
205 res_nclose(pvt->res);
206 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
207 struct irs_nw *nw = rule->inst->nw;
209 (*nw->minimize)(nw);
213 static struct __res_state *
214 nw_res_get(struct irs_nw *this) {
215 struct pvt *pvt = (struct pvt *)this->private;
217 if (!pvt->res) {
218 struct __res_state *res;
219 res = (struct __res_state *)malloc(sizeof *res);
220 if (!res) {
221 errno = ENOMEM;
222 return (NULL);
224 memset(res, 0, sizeof *res);
225 nw_res_set(this, res, free);
228 return (pvt->res);
231 static void
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);
242 pvt->res = 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);
252 static int
253 init(struct irs_nw *this) {
254 struct pvt *pvt = (struct pvt *)this->private;
256 if (!pvt->res && !nw_res_get(this))
257 return (-1);
258 if (((pvt->res->options & RES_INIT) == 0U) &&
259 res_ninit(pvt->res) == -1)
260 return (-1);
261 return (0);