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 sv_close(struct irs_sv
*);
51 static struct servent
* sv_next(struct irs_sv
*);
52 static struct servent
* sv_byname(struct irs_sv
*, const char *,
54 static struct servent
* sv_byport(struct irs_sv
*, int, const char *);
55 static void sv_rewind(struct irs_sv
*);
56 static void sv_minimize(struct irs_sv
*);
57 static struct __res_state
* sv_res_get(struct irs_sv
*);
58 static void sv_res_set(struct irs_sv
*,
65 irs_gen_sv(struct irs_acc
*this) {
66 struct gen_p
*accpvt
= (struct gen_p
*)this->private;
70 if (!(sv
= memget(sizeof *sv
))) {
74 memset(sv
, 0x5e, sizeof *sv
);
75 if (!(pvt
= memget(sizeof *pvt
))) {
76 memput(sv
, sizeof *sv
);
80 memset(pvt
, 0, sizeof *pvt
);
81 pvt
->rules
= accpvt
->map_rules
[irs_sv
];
82 pvt
->rule
= pvt
->rules
;
86 sv
->byname
= sv_byname
;
87 sv
->byport
= sv_byport
;
88 sv
->rewind
= sv_rewind
;
89 sv
->minimize
= sv_minimize
;
90 sv
->res_get
= sv_res_get
;
91 sv
->res_set
= sv_res_set
;
98 sv_close(struct irs_sv
*this) {
99 struct pvt
*pvt
= (struct pvt
*)this->private;
101 memput(pvt
, sizeof *pvt
);
102 memput(this, sizeof *this);
105 static struct servent
*
106 sv_next(struct irs_sv
*this) {
107 struct pvt
*pvt
= (struct pvt
*)this->private;
108 struct servent
*rval
;
112 sv
= pvt
->rule
->inst
->sv
;
113 rval
= (*sv
->next
)(sv
);
116 if (!(pvt
->rule
->flags
& IRS_CONTINUE
))
118 pvt
->rule
= pvt
->rule
->next
;
120 sv
= pvt
->rule
->inst
->sv
;
127 static struct servent
*
128 sv_byname(struct irs_sv
*this, const char *name
, const char *proto
) {
129 struct pvt
*pvt
= (struct pvt
*)this->private;
130 struct irs_rule
*rule
;
131 struct servent
*rval
;
135 for (rule
= pvt
->rules
; rule
; rule
= rule
->next
) {
137 rval
= (*sv
->byname
)(sv
, name
, proto
);
138 if (rval
|| !(rule
->flags
& IRS_CONTINUE
))
144 static struct servent
*
145 sv_byport(struct irs_sv
*this, int port
, const char *proto
) {
146 struct pvt
*pvt
= (struct pvt
*)this->private;
147 struct irs_rule
*rule
;
148 struct servent
*rval
;
152 for (rule
= pvt
->rules
; rule
; rule
= rule
->next
) {
154 rval
= (*sv
->byport
)(sv
, port
, proto
);
155 if (rval
|| !(rule
->flags
& IRS_CONTINUE
))
162 sv_rewind(struct irs_sv
*this) {
163 struct pvt
*pvt
= (struct pvt
*)this->private;
166 pvt
->rule
= pvt
->rules
;
168 sv
= pvt
->rule
->inst
->sv
;
174 sv_minimize(struct irs_sv
*this) {
175 struct pvt
*pvt
= (struct pvt
*)this->private;
176 struct irs_rule
*rule
;
178 for (rule
= pvt
->rules
; rule
!= NULL
; rule
= rule
->next
) {
179 struct irs_sv
*sv
= rule
->inst
->sv
;
185 static struct __res_state
*
186 sv_res_get(struct irs_sv
*this) {
187 struct pvt
*pvt
= (struct pvt
*)this->private;
190 struct __res_state
*res
;
191 res
= (struct __res_state
*)malloc(sizeof *res
);
196 memset(res
, 0, sizeof *res
);
197 sv_res_set(this, res
, free
);
204 sv_res_set(struct irs_sv
*this, struct __res_state
*res
,
205 void (*free_res
)(void *)) {
206 struct pvt
*pvt
= (struct pvt
*)this->private;
207 struct irs_rule
*rule
;
209 if (pvt
->res
&& pvt
->free_res
) {
210 res_nclose(pvt
->res
);
211 (*pvt
->free_res
)(pvt
->res
);
215 pvt
->free_res
= free_res
;
217 for (rule
= pvt
->rules
; rule
!= NULL
; rule
= rule
->next
) {
218 struct irs_sv
*sv
= rule
->inst
->sv
;
221 (*sv
->res_set
)(sv
, pvt
->res
, NULL
);