Unleashed v1.4
[unleashed.git] / usr / src / lib / libresolv2 / common / irs / gen_pr.c
blob792cdd69a27f4bfe6df5c8f0a50cc3de824889c6
1 /*
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 /* Imports */
20 #include "port_before.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
26 #include <errno.h>
27 #include <resolv.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include <isc/memcluster.h>
32 #include <irs.h>
34 #include "port_after.h"
36 #include "irs_p.h"
37 #include "gen_p.h"
39 /* Types */
41 struct pvt {
42 struct irs_rule * rules;
43 struct irs_rule * rule;
44 struct __res_state * res;
45 void (*free_res)(void *);
48 /* Forward */
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 *,
58 struct __res_state *,
59 void (*)(void *));
61 /* Public */
63 struct irs_pr *
64 irs_gen_pr(struct irs_acc *this) {
65 struct gen_p *accpvt = (struct gen_p *)this->private;
66 struct irs_pr *pr;
67 struct pvt *pvt;
69 if (!(pr = memget(sizeof *pr))) {
70 errno = ENOMEM;
71 return (NULL);
73 memset(pr, 0x5e, sizeof *pr);
74 if (!(pvt = memget(sizeof *pvt))) {
75 memput(pr, sizeof *pr);
76 errno = ENOMEM;
77 return (NULL);
79 memset(pvt, 0, sizeof *pvt);
80 pvt->rules = accpvt->map_rules[irs_pr];
81 pvt->rule = pvt->rules;
82 pr->private = pvt;
83 pr->close = pr_close;
84 pr->next = pr_next;
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;
91 return (pr);
94 /* Methods */
96 static void
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;
108 struct irs_pr *pr;
110 while (pvt->rule) {
111 pr = pvt->rule->inst->pr;
112 rval = (*pr->next)(pr);
113 if (rval)
114 return (rval);
115 if (!(pvt->rules->flags & IRS_CONTINUE))
116 break;
117 pvt->rule = pvt->rule->next;
118 if (pvt->rule) {
119 pr = pvt->rule->inst->pr;
120 (*pr->rewind)(pr);
123 return (NULL);
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;
131 struct irs_pr *pr;
133 rval = NULL;
134 for (rule = pvt->rules; rule; rule = rule->next) {
135 pr = rule->inst->pr;
136 rval = (*pr->byname)(pr, name);
137 if (rval || !(rule->flags & IRS_CONTINUE))
138 break;
140 return (rval);
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;
148 struct irs_pr *pr;
150 rval = NULL;
151 for (rule = pvt->rules; rule; rule = rule->next) {
152 pr = rule->inst->pr;
153 rval = (*pr->bynumber)(pr, proto);
154 if (rval || !(rule->flags & IRS_CONTINUE))
155 break;
157 return (rval);
160 static void
161 pr_rewind(struct irs_pr *this) {
162 struct pvt *pvt = (struct pvt *)this->private;
163 struct irs_pr *pr;
165 pvt->rule = pvt->rules;
166 if (pvt->rule) {
167 pr = pvt->rule->inst->pr;
168 (*pr->rewind)(pr);
172 static void
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;
180 (*pr->minimize)(pr);
184 static struct __res_state *
185 pr_res_get(struct irs_pr *this) {
186 struct pvt *pvt = (struct pvt *)this->private;
188 if (!pvt->res) {
189 struct __res_state *res;
190 res = (struct __res_state *)malloc(sizeof *res);
191 if (!res) {
192 errno = ENOMEM;
193 return (NULL);
195 memset(res, 0, sizeof *res);
196 pr_res_set(this, res, free);
199 return (pvt->res);
202 static void
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);
213 pvt->res = 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;
219 if (pr->res_set)
220 (*pr->res_set)(pr, pvt->res, NULL);
224 /*! \file */