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_pr.c,v 1.1.2.1 2004/03/09 09:17:29 marka Exp $";
24 #include "port_before.h"
26 #include <sys/types.h>
27 #include <netinet/in.h>
28 #include <arpa/nameser.h>
35 #include <isc/memcluster.h>
38 #include "port_after.h"
46 struct irs_rule * rules;
47 struct irs_rule * rule;
48 struct __res_state * res;
49 void (*free_res)(void *);
54 static void pr_close(struct irs_pr*);
55 static struct protoent * pr_next(struct irs_pr *);
56 static struct protoent * pr_byname(struct irs_pr *, const char *);
57 static struct protoent * pr_bynumber(struct irs_pr *, int);
58 static void pr_rewind(struct irs_pr *);
59 static void pr_minimize(struct irs_pr *);
60 static struct __res_state * pr_res_get(struct irs_pr *);
61 static void pr_res_set(struct irs_pr *,
68 irs_gen_pr(struct irs_acc *this) {
69 struct gen_p *accpvt = (struct gen_p *)this->private;
73 if (!(pr = memget(sizeof *pr))) {
77 memset(pr, 0x5e, sizeof *pr);
78 if (!(pvt = memget(sizeof *pvt))) {
79 memput(pr, sizeof *pr);
83 memset(pvt, 0, sizeof *pvt);
84 pvt->rules = accpvt->map_rules[irs_pr];
85 pvt->rule = pvt->rules;
89 pr->byname = pr_byname;
90 pr->bynumber = pr_bynumber;
91 pr->rewind = pr_rewind;
92 pr->minimize = pr_minimize;
93 pr->res_get = pr_res_get;
94 pr->res_set = pr_res_set;
101 pr_close(struct irs_pr *this) {
102 struct pvt *pvt = (struct pvt *)this->private;
104 memput(pvt, sizeof *pvt);
105 memput(this, sizeof *this);
108 static struct protoent *
109 pr_next(struct irs_pr *this) {
110 struct pvt *pvt = (struct pvt *)this->private;
111 struct protoent *rval;
115 pr = pvt->rule->inst->pr;
116 rval = (*pr->next)(pr);
119 if (!(pvt->rules->flags & IRS_CONTINUE))
121 pvt->rule = pvt->rule->next;
123 pr = pvt->rule->inst->pr;
130 static struct protoent *
131 pr_byname(struct irs_pr *this, const char *name) {
132 struct pvt *pvt = (struct pvt *)this->private;
133 struct irs_rule *rule;
134 struct protoent *rval;
138 for (rule = pvt->rules; rule; rule = rule->next) {
140 rval = (*pr->byname)(pr, name);
141 if (rval || !(rule->flags & IRS_CONTINUE))
147 static struct protoent *
148 pr_bynumber(struct irs_pr *this, int proto) {
149 struct pvt *pvt = (struct pvt *)this->private;
150 struct irs_rule *rule;
151 struct protoent *rval;
155 for (rule = pvt->rules; rule; rule = rule->next) {
157 rval = (*pr->bynumber)(pr, proto);
158 if (rval || !(rule->flags & IRS_CONTINUE))
165 pr_rewind(struct irs_pr *this) {
166 struct pvt *pvt = (struct pvt *)this->private;
169 pvt->rule = pvt->rules;
171 pr = pvt->rule->inst->pr;
177 pr_minimize(struct irs_pr *this) {
178 struct pvt *pvt = (struct pvt *)this->private;
179 struct irs_rule *rule;
181 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
182 struct irs_pr *pr = rule->inst->pr;
188 static struct __res_state *
189 pr_res_get(struct irs_pr *this) {
190 struct pvt *pvt = (struct pvt *)this->private;
193 struct __res_state *res;
194 res = (struct __res_state *)malloc(sizeof *res);
199 memset(res, 0, sizeof *res);
200 pr_res_set(this, res, free);
207 pr_res_set(struct irs_pr *this, struct __res_state *res,
208 void (*free_res)(void *)) {
209 struct pvt *pvt = (struct pvt *)this->private;
210 struct irs_rule *rule;
212 if (pvt->res && pvt->free_res) {
213 res_nclose(pvt->res);
214 (*pvt->free_res)(pvt->res);
218 pvt->free_res = free_res;
220 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
221 struct irs_pr *pr = rule->inst->pr;
224 (*pr->res_set)(pr, pvt->res, NULL);