Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / gen_ng.c
blob11f7117974d7836712ef97d049ab1861faeda098
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 #if !defined(LINT) && !defined(CODECENTER)
19 static const char rcsid[] = "$Id: gen_ng.c,v 1.1.2.1 2004/03/09 09:17:29 marka Exp $";
20 #endif
22 /* Imports */
24 #include "port_before.h"
26 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
30 #include <resolv.h>
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include <isc/memcluster.h>
37 #include <irs.h>
39 #include "port_after.h"
41 #include "irs_p.h"
42 #include "gen_p.h"
44 /* Types */
46 struct pvt {
47 struct irs_rule * rules;
48 struct irs_rule * rule;
49 char * curgroup;
52 /* Forward */
54 static void ng_close(struct irs_ng *);
55 static int ng_next(struct irs_ng *, const char **,
56 const char **, const char **);
57 static int ng_test(struct irs_ng *, const char *,
58 const char *, const char *,
59 const char *);
60 static void ng_rewind(struct irs_ng *, const char *);
61 static void ng_minimize(struct irs_ng *);
63 /* Public */
65 struct irs_ng *
66 irs_gen_ng(struct irs_acc *this) {
67 struct gen_p *accpvt = (struct gen_p *)this->private;
68 struct irs_ng *ng;
69 struct pvt *pvt;
71 if (!(ng = memget(sizeof *ng))) {
72 errno = ENOMEM;
73 return (NULL);
75 memset(ng, 0x5e, sizeof *ng);
76 if (!(pvt = memget(sizeof *pvt))) {
77 memput(ng, sizeof *ng);
78 errno = ENOMEM;
79 return (NULL);
81 memset(pvt, 0, sizeof *pvt);
82 pvt->rules = accpvt->map_rules[irs_ng];
83 pvt->rule = pvt->rules;
84 ng->private = pvt;
85 ng->close = ng_close;
86 ng->next = ng_next;
87 ng->test = ng_test;
88 ng->rewind = ng_rewind;
89 ng->minimize = ng_minimize;
90 return (ng);
93 /* Methods */
95 static void
96 ng_close(struct irs_ng *this) {
97 struct pvt *pvt = (struct pvt *)this->private;
99 ng_minimize(this);
100 if (pvt->curgroup)
101 free(pvt->curgroup);
102 memput(pvt, sizeof *pvt);
103 memput(this, sizeof *this);
106 static int
107 ng_next(struct irs_ng *this, const char **host, const char **user,
108 const char **domain)
110 struct pvt *pvt = (struct pvt *)this->private;
111 struct irs_ng *ng;
113 while (pvt->rule) {
114 ng = pvt->rule->inst->ng;
115 if ((*ng->next)(ng, host, user, domain) == 1)
116 return (1);
117 if (!(pvt->rule->flags & IRS_CONTINUE))
118 break;
119 pvt->rule = pvt->rule->next;
120 if (pvt->rule) {
121 ng = pvt->rule->inst->ng;
122 (*ng->rewind)(ng, pvt->curgroup);
125 return (0);
128 static int
129 ng_test(struct irs_ng *this, const char *name,
130 const char *user, const char *host, const char *domain)
132 struct pvt *pvt = (struct pvt *)this->private;
133 struct irs_rule *rule;
134 struct irs_ng *ng;
135 int rval;
137 rval = 0;
138 for (rule = pvt->rules; rule; rule = rule->next) {
139 ng = rule->inst->ng;
140 rval = (*ng->test)(ng, name, user, host, domain);
141 if (rval || !(rule->flags & IRS_CONTINUE))
142 break;
144 return (rval);
147 static void
148 ng_rewind(struct irs_ng *this, const char *group) {
149 struct pvt *pvt = (struct pvt *)this->private;
150 struct irs_ng *ng;
152 pvt->rule = pvt->rules;
153 if (pvt->rule) {
154 if (pvt->curgroup)
155 free(pvt->curgroup);
156 pvt->curgroup = strdup(group);
157 ng = pvt->rule->inst->ng;
158 (*ng->rewind)(ng, pvt->curgroup);
162 static void
163 ng_minimize(struct irs_ng *this) {
164 struct pvt *pvt = (struct pvt *)this->private;
165 struct irs_rule *rule;
167 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
168 struct irs_ng *ng = rule->inst->ng;
170 (*ng->minimize)(ng);