Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / gen_pr.c
blob8e2530473e648e4e53c730d9dc0c3db0c95451c1
1 /* $NetBSD$ */
3 /*
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_pr.c,v 1.3 2005/04/27 04:56:24 sra Exp";
22 #endif
24 /* Imports */
26 #include "port_before.h"
28 #include <sys/types.h>
29 #include <netinet/in.h>
30 #include <arpa/nameser.h>
32 #include <errno.h>
33 #include <resolv.h>
34 #include <stdlib.h>
35 #include <string.h>
37 #include <isc/memcluster.h>
38 #include <irs.h>
40 #include "port_after.h"
42 #include "irs_p.h"
43 #include "gen_p.h"
45 /* Types */
47 struct pvt {
48 struct irs_rule * rules;
49 struct irs_rule * rule;
50 struct __res_state * res;
51 void (*free_res)(void *);
54 /* Forward */
56 static void pr_close(struct irs_pr*);
57 static struct protoent * pr_next(struct irs_pr *);
58 static struct protoent * pr_byname(struct irs_pr *, const char *);
59 static struct protoent * pr_bynumber(struct irs_pr *, int);
60 static void pr_rewind(struct irs_pr *);
61 static void pr_minimize(struct irs_pr *);
62 static struct __res_state * pr_res_get(struct irs_pr *);
63 static void pr_res_set(struct irs_pr *,
64 struct __res_state *,
65 void (*)(void *));
67 /* Public */
69 struct irs_pr *
70 irs_gen_pr(struct irs_acc *this) {
71 struct gen_p *accpvt = (struct gen_p *)this->private;
72 struct irs_pr *pr;
73 struct pvt *pvt;
75 if (!(pr = memget(sizeof *pr))) {
76 errno = ENOMEM;
77 return (NULL);
79 memset(pr, 0x5e, sizeof *pr);
80 if (!(pvt = memget(sizeof *pvt))) {
81 memput(pr, sizeof *pr);
82 errno = ENOMEM;
83 return (NULL);
85 memset(pvt, 0, sizeof *pvt);
86 pvt->rules = accpvt->map_rules[irs_pr];
87 pvt->rule = pvt->rules;
88 pr->private = pvt;
89 pr->close = pr_close;
90 pr->next = pr_next;
91 pr->byname = pr_byname;
92 pr->bynumber = pr_bynumber;
93 pr->rewind = pr_rewind;
94 pr->minimize = pr_minimize;
95 pr->res_get = pr_res_get;
96 pr->res_set = pr_res_set;
97 return (pr);
100 /* Methods */
102 static void
103 pr_close(struct irs_pr *this) {
104 struct pvt *pvt = (struct pvt *)this->private;
106 memput(pvt, sizeof *pvt);
107 memput(this, sizeof *this);
110 static struct protoent *
111 pr_next(struct irs_pr *this) {
112 struct pvt *pvt = (struct pvt *)this->private;
113 struct protoent *rval;
114 struct irs_pr *pr;
116 while (pvt->rule) {
117 pr = pvt->rule->inst->pr;
118 rval = (*pr->next)(pr);
119 if (rval)
120 return (rval);
121 if (!(pvt->rules->flags & IRS_CONTINUE))
122 break;
123 pvt->rule = pvt->rule->next;
124 if (pvt->rule) {
125 pr = pvt->rule->inst->pr;
126 (*pr->rewind)(pr);
129 return (NULL);
132 static struct protoent *
133 pr_byname(struct irs_pr *this, const char *name) {
134 struct pvt *pvt = (struct pvt *)this->private;
135 struct irs_rule *rule;
136 struct protoent *rval;
137 struct irs_pr *pr;
139 rval = NULL;
140 for (rule = pvt->rules; rule; rule = rule->next) {
141 pr = rule->inst->pr;
142 rval = (*pr->byname)(pr, name);
143 if (rval || !(rule->flags & IRS_CONTINUE))
144 break;
146 return (rval);
149 static struct protoent *
150 pr_bynumber(struct irs_pr *this, int proto) {
151 struct pvt *pvt = (struct pvt *)this->private;
152 struct irs_rule *rule;
153 struct protoent *rval;
154 struct irs_pr *pr;
156 rval = NULL;
157 for (rule = pvt->rules; rule; rule = rule->next) {
158 pr = rule->inst->pr;
159 rval = (*pr->bynumber)(pr, proto);
160 if (rval || !(rule->flags & IRS_CONTINUE))
161 break;
163 return (rval);
166 static void
167 pr_rewind(struct irs_pr *this) {
168 struct pvt *pvt = (struct pvt *)this->private;
169 struct irs_pr *pr;
171 pvt->rule = pvt->rules;
172 if (pvt->rule) {
173 pr = pvt->rule->inst->pr;
174 (*pr->rewind)(pr);
178 static void
179 pr_minimize(struct irs_pr *this) {
180 struct pvt *pvt = (struct pvt *)this->private;
181 struct irs_rule *rule;
183 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
184 struct irs_pr *pr = rule->inst->pr;
186 (*pr->minimize)(pr);
190 static struct __res_state *
191 pr_res_get(struct irs_pr *this) {
192 struct pvt *pvt = (struct pvt *)this->private;
194 if (!pvt->res) {
195 struct __res_state *res;
196 res = (struct __res_state *)malloc(sizeof *res);
197 if (!res) {
198 errno = ENOMEM;
199 return (NULL);
201 memset(res, 0, sizeof *res);
202 pr_res_set(this, res, free);
205 return (pvt->res);
208 static void
209 pr_res_set(struct irs_pr *this, struct __res_state *res,
210 void (*free_res)(void *)) {
211 struct pvt *pvt = (struct pvt *)this->private;
212 struct irs_rule *rule;
214 if (pvt->res && pvt->free_res) {
215 res_nclose(pvt->res);
216 (*pvt->free_res)(pvt->res);
219 pvt->res = res;
220 pvt->free_res = free_res;
222 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
223 struct irs_pr *pr = rule->inst->pr;
225 if (pr->res_set)
226 (*pr->res_set)(pr, pvt->res, NULL);
230 /*! \file */