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_pr.c,v 1.3 2005/04/27 04:56:24 sra Exp";
26 #include "port_before.h"
28 #include <sys/types.h>
29 #include <netinet/in.h>
30 #include <arpa/nameser.h>
37 #include <isc/memcluster.h>
40 #include "port_after.h"
48 struct irs_rule
* rules
;
49 struct irs_rule
* rule
;
50 struct __res_state
* res
;
51 void (*free_res
)(void *);
56 static void pr_close(struct irs_pr
*);
57 static struct protoent
* pr_next(struct irs_pr
*);
58 static struct protoent
* pr_byname(struct irs_pr
*, const char *);
59 static struct protoent
* pr_bynumber(struct irs_pr
*, int);
60 static void pr_rewind(struct irs_pr
*);
61 static void pr_minimize(struct irs_pr
*);
62 static struct __res_state
* pr_res_get(struct irs_pr
*);
63 static void pr_res_set(struct irs_pr
*,
70 irs_gen_pr(struct irs_acc
*this) {
71 struct gen_p
*accpvt
= (struct gen_p
*)this->private;
75 if (!(pr
= memget(sizeof *pr
))) {
79 memset(pr
, 0x5e, sizeof *pr
);
80 if (!(pvt
= memget(sizeof *pvt
))) {
81 memput(pr
, sizeof *pr
);
85 memset(pvt
, 0, sizeof *pvt
);
86 pvt
->rules
= accpvt
->map_rules
[irs_pr
];
87 pvt
->rule
= pvt
->rules
;
91 pr
->byname
= pr_byname
;
92 pr
->bynumber
= pr_bynumber
;
93 pr
->rewind
= pr_rewind
;
94 pr
->minimize
= pr_minimize
;
95 pr
->res_get
= pr_res_get
;
96 pr
->res_set
= pr_res_set
;
103 pr_close(struct irs_pr
*this) {
104 struct pvt
*pvt
= (struct pvt
*)this->private;
106 memput(pvt
, sizeof *pvt
);
107 memput(this, sizeof *this);
110 static struct protoent
*
111 pr_next(struct irs_pr
*this) {
112 struct pvt
*pvt
= (struct pvt
*)this->private;
113 struct protoent
*rval
;
117 pr
= pvt
->rule
->inst
->pr
;
118 rval
= (*pr
->next
)(pr
);
121 if (!(pvt
->rules
->flags
& IRS_CONTINUE
))
123 pvt
->rule
= pvt
->rule
->next
;
125 pr
= pvt
->rule
->inst
->pr
;
132 static struct protoent
*
133 pr_byname(struct irs_pr
*this, const char *name
) {
134 struct pvt
*pvt
= (struct pvt
*)this->private;
135 struct irs_rule
*rule
;
136 struct protoent
*rval
;
140 for (rule
= pvt
->rules
; rule
; rule
= rule
->next
) {
142 rval
= (*pr
->byname
)(pr
, name
);
143 if (rval
|| !(rule
->flags
& IRS_CONTINUE
))
149 static struct protoent
*
150 pr_bynumber(struct irs_pr
*this, int proto
) {
151 struct pvt
*pvt
= (struct pvt
*)this->private;
152 struct irs_rule
*rule
;
153 struct protoent
*rval
;
157 for (rule
= pvt
->rules
; rule
; rule
= rule
->next
) {
159 rval
= (*pr
->bynumber
)(pr
, proto
);
160 if (rval
|| !(rule
->flags
& IRS_CONTINUE
))
167 pr_rewind(struct irs_pr
*this) {
168 struct pvt
*pvt
= (struct pvt
*)this->private;
171 pvt
->rule
= pvt
->rules
;
173 pr
= pvt
->rule
->inst
->pr
;
179 pr_minimize(struct irs_pr
*this) {
180 struct pvt
*pvt
= (struct pvt
*)this->private;
181 struct irs_rule
*rule
;
183 for (rule
= pvt
->rules
; rule
!= NULL
; rule
= rule
->next
) {
184 struct irs_pr
*pr
= rule
->inst
->pr
;
190 static struct __res_state
*
191 pr_res_get(struct irs_pr
*this) {
192 struct pvt
*pvt
= (struct pvt
*)this->private;
195 struct __res_state
*res
;
196 res
= (struct __res_state
*)malloc(sizeof *res
);
201 memset(res
, 0, sizeof *res
);
202 pr_res_set(this, res
, free
);
209 pr_res_set(struct irs_pr
*this, struct __res_state
*res
,
210 void (*free_res
)(void *)) {
211 struct pvt
*pvt
= (struct pvt
*)this->private;
212 struct irs_rule
*rule
;
214 if (pvt
->res
&& pvt
->free_res
) {
215 res_nclose(pvt
->res
);
216 (*pvt
->free_res
)(pvt
->res
);
220 pvt
->free_res
= free_res
;
222 for (rule
= pvt
->rules
; rule
!= NULL
; rule
= rule
->next
) {
223 struct irs_pr
*pr
= rule
->inst
->pr
;
226 (*pr
->res_set
)(pr
, pvt
->res
, NULL
);