Unleashed v1.4
[unleashed.git] / usr / src / lib / libresolv2 / common / irs / gen_ng.c
blob8d544e8322619f6644bf8b1e0b63765e6ea923d9
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>
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
26 #include <resolv.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include <isc/memcluster.h>
33 #include <irs.h>
35 #include "port_after.h"
37 #include "irs_p.h"
38 #include "gen_p.h"
40 /* Types */
42 struct pvt {
43 struct irs_rule * rules;
44 struct irs_rule * rule;
45 char * curgroup;
48 /* Forward */
50 static void ng_close(struct irs_ng *);
51 static int ng_next(struct irs_ng *, const char **,
52 const char **, const char **);
53 static int ng_test(struct irs_ng *, const char *,
54 const char *, const char *,
55 const char *);
56 static void ng_rewind(struct irs_ng *, const char *);
57 static void ng_minimize(struct irs_ng *);
59 /* Public */
61 struct irs_ng *
62 irs_gen_ng(struct irs_acc *this) {
63 struct gen_p *accpvt = (struct gen_p *)this->private;
64 struct irs_ng *ng;
65 struct pvt *pvt;
67 if (!(ng = memget(sizeof *ng))) {
68 errno = ENOMEM;
69 return (NULL);
71 memset(ng, 0x5e, sizeof *ng);
72 if (!(pvt = memget(sizeof *pvt))) {
73 memput(ng, sizeof *ng);
74 errno = ENOMEM;
75 return (NULL);
77 memset(pvt, 0, sizeof *pvt);
78 pvt->rules = accpvt->map_rules[irs_ng];
79 pvt->rule = pvt->rules;
80 ng->private = pvt;
81 ng->close = ng_close;
82 ng->next = ng_next;
83 ng->test = ng_test;
84 ng->rewind = ng_rewind;
85 ng->minimize = ng_minimize;
86 return (ng);
89 /* Methods */
91 static void
92 ng_close(struct irs_ng *this) {
93 struct pvt *pvt = (struct pvt *)this->private;
95 ng_minimize(this);
96 free(pvt->curgroup);
97 memput(pvt, sizeof *pvt);
98 memput(this, sizeof *this);
101 static int
102 ng_next(struct irs_ng *this, const char **host, const char **user,
103 const char **domain)
105 struct pvt *pvt = (struct pvt *)this->private;
106 struct irs_ng *ng;
108 while (pvt->rule) {
109 ng = pvt->rule->inst->ng;
110 if ((*ng->next)(ng, host, user, domain) == 1)
111 return (1);
112 if (!(pvt->rule->flags & IRS_CONTINUE))
113 break;
114 pvt->rule = pvt->rule->next;
115 if (pvt->rule) {
116 ng = pvt->rule->inst->ng;
117 (*ng->rewind)(ng, pvt->curgroup);
120 return (0);
123 static int
124 ng_test(struct irs_ng *this, const char *name,
125 const char *user, const char *host, const char *domain)
127 struct pvt *pvt = (struct pvt *)this->private;
128 struct irs_rule *rule;
129 struct irs_ng *ng;
130 int rval;
132 rval = 0;
133 for (rule = pvt->rules; rule; rule = rule->next) {
134 ng = rule->inst->ng;
135 rval = (*ng->test)(ng, name, user, host, domain);
136 if (rval || !(rule->flags & IRS_CONTINUE))
137 break;
139 return (rval);
142 static void
143 ng_rewind(struct irs_ng *this, const char *group) {
144 struct pvt *pvt = (struct pvt *)this->private;
145 struct irs_ng *ng;
147 pvt->rule = pvt->rules;
148 if (pvt->rule) {
149 free(pvt->curgroup);
150 pvt->curgroup = strdup(group);
151 ng = pvt->rule->inst->ng;
152 (*ng->rewind)(ng, pvt->curgroup);
156 static void
157 ng_minimize(struct irs_ng *this) {
158 struct pvt *pvt = (struct pvt *)this->private;
159 struct irs_rule *rule;
161 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
162 struct irs_ng *ng = rule->inst->ng;
164 (*ng->minimize)(ng);
168 /*! \file */