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.
20 #include "port_before.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
31 #include <isc/memcluster.h>
34 #include "port_after.h"
42 struct irs_rule
* rules
;
43 struct irs_rule
* rule
;
44 struct __res_state
* res
;
45 void (*free_res
)(void *);
50 static void pr_close(struct irs_pr
*);
51 static struct protoent
* pr_next(struct irs_pr
*);
52 static struct protoent
* pr_byname(struct irs_pr
*, const char *);
53 static struct protoent
* pr_bynumber(struct irs_pr
*, int);
54 static void pr_rewind(struct irs_pr
*);
55 static void pr_minimize(struct irs_pr
*);
56 static struct __res_state
* pr_res_get(struct irs_pr
*);
57 static void pr_res_set(struct irs_pr
*,
64 irs_gen_pr(struct irs_acc
*this) {
65 struct gen_p
*accpvt
= (struct gen_p
*)this->private;
69 if (!(pr
= memget(sizeof *pr
))) {
73 memset(pr
, 0x5e, sizeof *pr
);
74 if (!(pvt
= memget(sizeof *pvt
))) {
75 memput(pr
, sizeof *pr
);
79 memset(pvt
, 0, sizeof *pvt
);
80 pvt
->rules
= accpvt
->map_rules
[irs_pr
];
81 pvt
->rule
= pvt
->rules
;
85 pr
->byname
= pr_byname
;
86 pr
->bynumber
= pr_bynumber
;
87 pr
->rewind
= pr_rewind
;
88 pr
->minimize
= pr_minimize
;
89 pr
->res_get
= pr_res_get
;
90 pr
->res_set
= pr_res_set
;
97 pr_close(struct irs_pr
*this) {
98 struct pvt
*pvt
= (struct pvt
*)this->private;
100 memput(pvt
, sizeof *pvt
);
101 memput(this, sizeof *this);
104 static struct protoent
*
105 pr_next(struct irs_pr
*this) {
106 struct pvt
*pvt
= (struct pvt
*)this->private;
107 struct protoent
*rval
;
111 pr
= pvt
->rule
->inst
->pr
;
112 rval
= (*pr
->next
)(pr
);
115 if (!(pvt
->rules
->flags
& IRS_CONTINUE
))
117 pvt
->rule
= pvt
->rule
->next
;
119 pr
= pvt
->rule
->inst
->pr
;
126 static struct protoent
*
127 pr_byname(struct irs_pr
*this, const char *name
) {
128 struct pvt
*pvt
= (struct pvt
*)this->private;
129 struct irs_rule
*rule
;
130 struct protoent
*rval
;
134 for (rule
= pvt
->rules
; rule
; rule
= rule
->next
) {
136 rval
= (*pr
->byname
)(pr
, name
);
137 if (rval
|| !(rule
->flags
& IRS_CONTINUE
))
143 static struct protoent
*
144 pr_bynumber(struct irs_pr
*this, int proto
) {
145 struct pvt
*pvt
= (struct pvt
*)this->private;
146 struct irs_rule
*rule
;
147 struct protoent
*rval
;
151 for (rule
= pvt
->rules
; rule
; rule
= rule
->next
) {
153 rval
= (*pr
->bynumber
)(pr
, proto
);
154 if (rval
|| !(rule
->flags
& IRS_CONTINUE
))
161 pr_rewind(struct irs_pr
*this) {
162 struct pvt
*pvt
= (struct pvt
*)this->private;
165 pvt
->rule
= pvt
->rules
;
167 pr
= pvt
->rule
->inst
->pr
;
173 pr_minimize(struct irs_pr
*this) {
174 struct pvt
*pvt
= (struct pvt
*)this->private;
175 struct irs_rule
*rule
;
177 for (rule
= pvt
->rules
; rule
!= NULL
; rule
= rule
->next
) {
178 struct irs_pr
*pr
= rule
->inst
->pr
;
184 static struct __res_state
*
185 pr_res_get(struct irs_pr
*this) {
186 struct pvt
*pvt
= (struct pvt
*)this->private;
189 struct __res_state
*res
;
190 res
= (struct __res_state
*)malloc(sizeof *res
);
195 memset(res
, 0, sizeof *res
);
196 pr_res_set(this, res
, free
);
203 pr_res_set(struct irs_pr
*this, struct __res_state
*res
,
204 void (*free_res
)(void *)) {
205 struct pvt
*pvt
= (struct pvt
*)this->private;
206 struct irs_rule
*rule
;
208 if (pvt
->res
&& pvt
->free_res
) {
209 res_nclose(pvt
->res
);
210 (*pvt
->free_res
)(pvt
->res
);
214 pvt
->free_res
= free_res
;
216 for (rule
= pvt
->rules
; rule
!= NULL
; rule
= rule
->next
) {
217 struct irs_pr
*pr
= rule
->inst
->pr
;
220 (*pr
->res_set
)(pr
, pvt
->res
, NULL
);