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_sv.c,v 1.3 2005/04/27 04:56:24 sra 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 sv_close(struct irs_sv
*);
55 static struct servent
* sv_next(struct irs_sv
*);
56 static struct servent
* sv_byname(struct irs_sv
*, const char *,
58 static struct servent
* sv_byport(struct irs_sv
*, int, const char *);
59 static void sv_rewind(struct irs_sv
*);
60 static void sv_minimize(struct irs_sv
*);
61 static struct __res_state
* sv_res_get(struct irs_sv
*);
62 static void sv_res_set(struct irs_sv
*,
69 irs_gen_sv(struct irs_acc
*this) {
70 struct gen_p
*accpvt
= (struct gen_p
*)this->private;
74 if (!(sv
= memget(sizeof *sv
))) {
78 memset(sv
, 0x5e, sizeof *sv
);
79 if (!(pvt
= memget(sizeof *pvt
))) {
80 memput(sv
, sizeof *sv
);
84 memset(pvt
, 0, sizeof *pvt
);
85 pvt
->rules
= accpvt
->map_rules
[irs_sv
];
86 pvt
->rule
= pvt
->rules
;
90 sv
->byname
= sv_byname
;
91 sv
->byport
= sv_byport
;
92 sv
->rewind
= sv_rewind
;
93 sv
->minimize
= sv_minimize
;
94 sv
->res_get
= sv_res_get
;
95 sv
->res_set
= sv_res_set
;
102 sv_close(struct irs_sv
*this) {
103 struct pvt
*pvt
= (struct pvt
*)this->private;
105 memput(pvt
, sizeof *pvt
);
106 memput(this, sizeof *this);
109 static struct servent
*
110 sv_next(struct irs_sv
*this) {
111 struct pvt
*pvt
= (struct pvt
*)this->private;
112 struct servent
*rval
;
116 sv
= pvt
->rule
->inst
->sv
;
117 rval
= (*sv
->next
)(sv
);
120 if (!(pvt
->rule
->flags
& IRS_CONTINUE
))
122 pvt
->rule
= pvt
->rule
->next
;
124 sv
= pvt
->rule
->inst
->sv
;
131 static struct servent
*
132 sv_byname(struct irs_sv
*this, const char *name
, const char *proto
) {
133 struct pvt
*pvt
= (struct pvt
*)this->private;
134 struct irs_rule
*rule
;
135 struct servent
*rval
;
139 for (rule
= pvt
->rules
; rule
; rule
= rule
->next
) {
141 rval
= (*sv
->byname
)(sv
, name
, proto
);
142 if (rval
|| !(rule
->flags
& IRS_CONTINUE
))
148 static struct servent
*
149 sv_byport(struct irs_sv
*this, int port
, const char *proto
) {
150 struct pvt
*pvt
= (struct pvt
*)this->private;
151 struct irs_rule
*rule
;
152 struct servent
*rval
;
156 for (rule
= pvt
->rules
; rule
; rule
= rule
->next
) {
158 rval
= (*sv
->byport
)(sv
, port
, proto
);
159 if (rval
|| !(rule
->flags
& IRS_CONTINUE
))
166 sv_rewind(struct irs_sv
*this) {
167 struct pvt
*pvt
= (struct pvt
*)this->private;
170 pvt
->rule
= pvt
->rules
;
172 sv
= pvt
->rule
->inst
->sv
;
178 sv_minimize(struct irs_sv
*this) {
179 struct pvt
*pvt
= (struct pvt
*)this->private;
180 struct irs_rule
*rule
;
182 for (rule
= pvt
->rules
; rule
!= NULL
; rule
= rule
->next
) {
183 struct irs_sv
*sv
= rule
->inst
->sv
;
189 static struct __res_state
*
190 sv_res_get(struct irs_sv
*this) {
191 struct pvt
*pvt
= (struct pvt
*)this->private;
194 struct __res_state
*res
;
195 res
= (struct __res_state
*)malloc(sizeof *res
);
200 memset(res
, 0, sizeof *res
);
201 sv_res_set(this, res
, free
);
208 sv_res_set(struct irs_sv
*this, struct __res_state
*res
,
209 void (*free_res
)(void *)) {
210 struct pvt
*pvt
= (struct pvt
*)this->private;
211 struct irs_rule
*rule
;
213 if (pvt
->res
&& pvt
->free_res
) {
214 res_nclose(pvt
->res
);
215 (*pvt
->free_res
)(pvt
->res
);
219 pvt
->free_res
= free_res
;
221 for (rule
= pvt
->rules
; rule
!= NULL
; rule
= rule
->next
) {
222 struct irs_sv
*sv
= rule
->inst
->sv
;
225 (*sv
->res_set
)(sv
, pvt
->res
, NULL
);