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_sv.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 sv_close(struct irs_sv
*);
57 static struct servent
* sv_next(struct irs_sv
*);
58 static struct servent
* sv_byname(struct irs_sv
*, const char *,
60 static struct servent
* sv_byport(struct irs_sv
*, int, const char *);
61 static void sv_rewind(struct irs_sv
*);
62 static void sv_minimize(struct irs_sv
*);
63 static struct __res_state
* sv_res_get(struct irs_sv
*);
64 static void sv_res_set(struct irs_sv
*,
71 irs_gen_sv(struct irs_acc
*this) {
72 struct gen_p
*accpvt
= (struct gen_p
*)this->private;
76 if (!(sv
= memget(sizeof *sv
))) {
80 memset(sv
, 0x5e, sizeof *sv
);
81 if (!(pvt
= memget(sizeof *pvt
))) {
82 memput(sv
, sizeof *sv
);
86 memset(pvt
, 0, sizeof *pvt
);
87 pvt
->rules
= accpvt
->map_rules
[irs_sv
];
88 pvt
->rule
= pvt
->rules
;
92 sv
->byname
= sv_byname
;
93 sv
->byport
= sv_byport
;
94 sv
->rewind
= sv_rewind
;
95 sv
->minimize
= sv_minimize
;
96 sv
->res_get
= sv_res_get
;
97 sv
->res_set
= sv_res_set
;
104 sv_close(struct irs_sv
*this) {
105 struct pvt
*pvt
= (struct pvt
*)this->private;
107 memput(pvt
, sizeof *pvt
);
108 memput(this, sizeof *this);
111 static struct servent
*
112 sv_next(struct irs_sv
*this) {
113 struct pvt
*pvt
= (struct pvt
*)this->private;
114 struct servent
*rval
;
118 sv
= pvt
->rule
->inst
->sv
;
119 rval
= (*sv
->next
)(sv
);
122 if (!(pvt
->rule
->flags
& IRS_CONTINUE
))
124 pvt
->rule
= pvt
->rule
->next
;
126 sv
= pvt
->rule
->inst
->sv
;
133 static struct servent
*
134 sv_byname(struct irs_sv
*this, const char *name
, const char *proto
) {
135 struct pvt
*pvt
= (struct pvt
*)this->private;
136 struct irs_rule
*rule
;
137 struct servent
*rval
;
141 for (rule
= pvt
->rules
; rule
; rule
= rule
->next
) {
143 rval
= (*sv
->byname
)(sv
, name
, proto
);
144 if (rval
|| !(rule
->flags
& IRS_CONTINUE
))
150 static struct servent
*
151 sv_byport(struct irs_sv
*this, int port
, const char *proto
) {
152 struct pvt
*pvt
= (struct pvt
*)this->private;
153 struct irs_rule
*rule
;
154 struct servent
*rval
;
158 for (rule
= pvt
->rules
; rule
; rule
= rule
->next
) {
160 rval
= (*sv
->byport
)(sv
, port
, proto
);
161 if (rval
|| !(rule
->flags
& IRS_CONTINUE
))
168 sv_rewind(struct irs_sv
*this) {
169 struct pvt
*pvt
= (struct pvt
*)this->private;
172 pvt
->rule
= pvt
->rules
;
174 sv
= pvt
->rule
->inst
->sv
;
180 sv_minimize(struct irs_sv
*this) {
181 struct pvt
*pvt
= (struct pvt
*)this->private;
182 struct irs_rule
*rule
;
184 for (rule
= pvt
->rules
; rule
!= NULL
; rule
= rule
->next
) {
185 struct irs_sv
*sv
= rule
->inst
->sv
;
191 static struct __res_state
*
192 sv_res_get(struct irs_sv
*this) {
193 struct pvt
*pvt
= (struct pvt
*)this->private;
196 struct __res_state
*res
;
197 res
= (struct __res_state
*)malloc(sizeof *res
);
202 memset(res
, 0, sizeof *res
);
203 sv_res_set(this, res
, free
);
210 sv_res_set(struct irs_sv
*this, struct __res_state
*res
,
211 void (*free_res
)(void *)) {
212 struct pvt
*pvt
= (struct pvt
*)this->private;
213 struct irs_rule
*rule
;
215 if (pvt
->res
&& pvt
->free_res
) {
216 res_nclose(pvt
->res
);
217 (*pvt
->free_res
)(pvt
->res
);
221 pvt
->free_res
= free_res
;
223 for (rule
= pvt
->rules
; rule
!= NULL
; rule
= rule
->next
) {
224 struct irs_sv
*sv
= rule
->inst
->sv
;
227 (*sv
->res_set
)(sv
, pvt
->res
, NULL
);