No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / gen_sv.c
blob3803d5160525ebd227640b297d031a41b24c86c5
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_sv.c,v 1.3 2005/04/27 04:56:24 sra Exp";
22 #endif
24 /* Imports */
26 #include "port_before.h"
28 #include <sys/types.h>
29 #include <netinet/in.h>
30 #include <arpa/nameser.h>
31 #include <resolv.h>
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <string.h>
37 #include <isc/memcluster.h>
38 #include <irs.h>
40 #include "port_after.h"
42 #include "irs_p.h"
43 #include "gen_p.h"
45 /* Types */
47 struct pvt {
48 struct irs_rule * rules;
49 struct irs_rule * rule;
50 struct __res_state * res;
51 void (*free_res)(void *);
54 /* Forward */
56 static void sv_close(struct irs_sv*);
57 static struct servent * sv_next(struct irs_sv *);
58 static struct servent * sv_byname(struct irs_sv *, const char *,
59 const char *);
60 static struct servent * sv_byport(struct irs_sv *, int, const char *);
61 static void sv_rewind(struct irs_sv *);
62 static void sv_minimize(struct irs_sv *);
63 static struct __res_state * sv_res_get(struct irs_sv *);
64 static void sv_res_set(struct irs_sv *,
65 struct __res_state *,
66 void (*)(void *));
68 /* Public */
70 struct irs_sv *
71 irs_gen_sv(struct irs_acc *this) {
72 struct gen_p *accpvt = (struct gen_p *)this->private;
73 struct irs_sv *sv;
74 struct pvt *pvt;
76 if (!(sv = memget(sizeof *sv))) {
77 errno = ENOMEM;
78 return (NULL);
80 memset(sv, 0x5e, sizeof *sv);
81 if (!(pvt = memget(sizeof *pvt))) {
82 memput(sv, sizeof *sv);
83 errno = ENOMEM;
84 return (NULL);
86 memset(pvt, 0, sizeof *pvt);
87 pvt->rules = accpvt->map_rules[irs_sv];
88 pvt->rule = pvt->rules;
89 sv->private = pvt;
90 sv->close = sv_close;
91 sv->next = sv_next;
92 sv->byname = sv_byname;
93 sv->byport = sv_byport;
94 sv->rewind = sv_rewind;
95 sv->minimize = sv_minimize;
96 sv->res_get = sv_res_get;
97 sv->res_set = sv_res_set;
98 return (sv);
101 /* Methods */
103 static void
104 sv_close(struct irs_sv *this) {
105 struct pvt *pvt = (struct pvt *)this->private;
107 memput(pvt, sizeof *pvt);
108 memput(this, sizeof *this);
111 static struct servent *
112 sv_next(struct irs_sv *this) {
113 struct pvt *pvt = (struct pvt *)this->private;
114 struct servent *rval;
115 struct irs_sv *sv;
117 while (pvt->rule) {
118 sv = pvt->rule->inst->sv;
119 rval = (*sv->next)(sv);
120 if (rval)
121 return (rval);
122 if (!(pvt->rule->flags & IRS_CONTINUE))
123 break;
124 pvt->rule = pvt->rule->next;
125 if (pvt->rule) {
126 sv = pvt->rule->inst->sv;
127 (*sv->rewind)(sv);
130 return (NULL);
133 static struct servent *
134 sv_byname(struct irs_sv *this, const char *name, const char *proto) {
135 struct pvt *pvt = (struct pvt *)this->private;
136 struct irs_rule *rule;
137 struct servent *rval;
138 struct irs_sv *sv;
140 rval = NULL;
141 for (rule = pvt->rules; rule; rule = rule->next) {
142 sv = rule->inst->sv;
143 rval = (*sv->byname)(sv, name, proto);
144 if (rval || !(rule->flags & IRS_CONTINUE))
145 break;
147 return (rval);
150 static struct servent *
151 sv_byport(struct irs_sv *this, int port, const char *proto) {
152 struct pvt *pvt = (struct pvt *)this->private;
153 struct irs_rule *rule;
154 struct servent *rval;
155 struct irs_sv *sv;
157 rval = NULL;
158 for (rule = pvt->rules; rule; rule = rule->next) {
159 sv = rule->inst->sv;
160 rval = (*sv->byport)(sv, port, proto);
161 if (rval || !(rule->flags & IRS_CONTINUE))
162 break;
164 return (rval);
167 static void
168 sv_rewind(struct irs_sv *this) {
169 struct pvt *pvt = (struct pvt *)this->private;
170 struct irs_sv *sv;
172 pvt->rule = pvt->rules;
173 if (pvt->rule) {
174 sv = pvt->rule->inst->sv;
175 (*sv->rewind)(sv);
179 static void
180 sv_minimize(struct irs_sv *this) {
181 struct pvt *pvt = (struct pvt *)this->private;
182 struct irs_rule *rule;
184 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
185 struct irs_sv *sv = rule->inst->sv;
187 (*sv->minimize)(sv);
191 static struct __res_state *
192 sv_res_get(struct irs_sv *this) {
193 struct pvt *pvt = (struct pvt *)this->private;
195 if (!pvt->res) {
196 struct __res_state *res;
197 res = (struct __res_state *)malloc(sizeof *res);
198 if (!res) {
199 errno = ENOMEM;
200 return (NULL);
202 memset(res, 0, sizeof *res);
203 sv_res_set(this, res, free);
206 return (pvt->res);
209 static void
210 sv_res_set(struct irs_sv *this, struct __res_state *res,
211 void (*free_res)(void *)) {
212 struct pvt *pvt = (struct pvt *)this->private;
213 struct irs_rule *rule;
215 if (pvt->res && pvt->free_res) {
216 res_nclose(pvt->res);
217 (*pvt->free_res)(pvt->res);
220 pvt->res = res;
221 pvt->free_res = free_res;
223 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
224 struct irs_sv *sv = rule->inst->sv;
226 if (sv->res_set)
227 (*sv->res_set)(sv, pvt->res, NULL);
231 /*! \file */