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