Deleted Added
full compact
modstack.c (266114) modstack.c (276605)
1/*
2 * services/modstack.c - stack of modules
3 *
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
5 *
6 * This software is open source.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36/**
37 * \file
38 *
39 * This file contains functions to help maintain a stack of modules.
40 */
41#include "config.h"
42#include <ctype.h>
43#include "services/modstack.h"
44#include "util/module.h"
45#include "util/fptr_wlist.h"
1/*
2 * services/modstack.c - stack of modules
3 *
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
5 *
6 * This software is open source.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36/**
37 * \file
38 *
39 * This file contains functions to help maintain a stack of modules.
40 */
41#include "config.h"
42#include <ctype.h>
43#include "services/modstack.h"
44#include "util/module.h"
45#include "util/fptr_wlist.h"
46#include "dns64/dns64.h"
46#include "iterator/iterator.h"
47#include "validator/validator.h"
48
49#ifdef WITH_PYTHONMODULE
50#include "pythonmod/pythonmod.h"
51#endif
52
53/** count number of modules (words) in the string */
54static int
55count_modules(const char* s)
56{
57 int num = 0;
58 if(!s)
59 return 0;
60 while(*s) {
61 /* skip whitespace */
47#include "iterator/iterator.h"
48#include "validator/validator.h"
49
50#ifdef WITH_PYTHONMODULE
51#include "pythonmod/pythonmod.h"
52#endif
53
54/** count number of modules (words) in the string */
55static int
56count_modules(const char* s)
57{
58 int num = 0;
59 if(!s)
60 return 0;
61 while(*s) {
62 /* skip whitespace */
62 while(*s && isspace((int)*s))
63 while(*s && isspace((unsigned char)*s))
63 s++;
64 s++;
64 if(*s && !isspace((int)*s)) {
65 if(*s && !isspace((unsigned char)*s)) {
65 /* skip identifier */
66 num++;
66 /* skip identifier */
67 num++;
67 while(*s && !isspace((int)*s))
68 while(*s && !isspace((unsigned char)*s))
68 s++;
69 }
70 }
71 return num;
72}
73
74void
75modstack_init(struct module_stack* stack)
76{
77 stack->num = 0;
78 stack->mod = NULL;
79}
80
81int
82modstack_config(struct module_stack* stack, const char* module_conf)
83{
84 int i;
85 verbose(VERB_QUERY, "module config: \"%s\"", module_conf);
86 stack->num = count_modules(module_conf);
87 if(stack->num == 0) {
88 log_err("error: no modules specified");
89 return 0;
90 }
91 if(stack->num > MAX_MODULE) {
92 log_err("error: too many modules (%d max %d)",
93 stack->num, MAX_MODULE);
94 return 0;
95 }
96 stack->mod = (struct module_func_block**)calloc((size_t)
97 stack->num, sizeof(struct module_func_block*));
98 if(!stack->mod) {
99 log_err("out of memory");
100 return 0;
101 }
102 for(i=0; i<stack->num; i++) {
103 stack->mod[i] = module_factory(&module_conf);
104 if(!stack->mod[i]) {
105 log_err("Unknown value for next module: '%s'",
106 module_conf);
107 return 0;
108 }
109 }
110 return 1;
111}
112
113/** The list of module names */
114const char**
115module_list_avail(void)
116{
117 /* these are the modules available */
118 static const char* names[] = {
69 s++;
70 }
71 }
72 return num;
73}
74
75void
76modstack_init(struct module_stack* stack)
77{
78 stack->num = 0;
79 stack->mod = NULL;
80}
81
82int
83modstack_config(struct module_stack* stack, const char* module_conf)
84{
85 int i;
86 verbose(VERB_QUERY, "module config: \"%s\"", module_conf);
87 stack->num = count_modules(module_conf);
88 if(stack->num == 0) {
89 log_err("error: no modules specified");
90 return 0;
91 }
92 if(stack->num > MAX_MODULE) {
93 log_err("error: too many modules (%d max %d)",
94 stack->num, MAX_MODULE);
95 return 0;
96 }
97 stack->mod = (struct module_func_block**)calloc((size_t)
98 stack->num, sizeof(struct module_func_block*));
99 if(!stack->mod) {
100 log_err("out of memory");
101 return 0;
102 }
103 for(i=0; i<stack->num; i++) {
104 stack->mod[i] = module_factory(&module_conf);
105 if(!stack->mod[i]) {
106 log_err("Unknown value for next module: '%s'",
107 module_conf);
108 return 0;
109 }
110 }
111 return 1;
112}
113
114/** The list of module names */
115const char**
116module_list_avail(void)
117{
118 /* these are the modules available */
119 static const char* names[] = {
120 "dns64",
119#ifdef WITH_PYTHONMODULE
120 "python",
121#endif
122 "validator",
123 "iterator",
124 NULL};
125 return names;
126}
127
128/** func block get function type */
129typedef struct module_func_block* (*fbgetfunctype)(void);
130
131/** The list of module func blocks */
132static fbgetfunctype*
133module_funcs_avail(void)
134{
135 static struct module_func_block* (*fb[])(void) = {
121#ifdef WITH_PYTHONMODULE
122 "python",
123#endif
124 "validator",
125 "iterator",
126 NULL};
127 return names;
128}
129
130/** func block get function type */
131typedef struct module_func_block* (*fbgetfunctype)(void);
132
133/** The list of module func blocks */
134static fbgetfunctype*
135module_funcs_avail(void)
136{
137 static struct module_func_block* (*fb[])(void) = {
138 &dns64_get_funcblock,
136#ifdef WITH_PYTHONMODULE
137 &pythonmod_get_funcblock,
138#endif
139 &val_get_funcblock,
140 &iter_get_funcblock,
141 NULL};
142 return fb;
143}
144
145struct
146module_func_block* module_factory(const char** str)
147{
148 int i = 0;
149 const char* s = *str;
150 const char** names = module_list_avail();
151 fbgetfunctype* fb = module_funcs_avail();
139#ifdef WITH_PYTHONMODULE
140 &pythonmod_get_funcblock,
141#endif
142 &val_get_funcblock,
143 &iter_get_funcblock,
144 NULL};
145 return fb;
146}
147
148struct
149module_func_block* module_factory(const char** str)
150{
151 int i = 0;
152 const char* s = *str;
153 const char** names = module_list_avail();
154 fbgetfunctype* fb = module_funcs_avail();
152 while(*s && isspace((int)*s))
155 while(*s && isspace((unsigned char)*s))
153 s++;
154 while(names[i]) {
155 if(strncmp(names[i], s, strlen(names[i])) == 0) {
156 s += strlen(names[i]);
157 *str = s;
158 return (*fb[i])();
159 }
160 i++;
161 }
162 return NULL;
163}
164
165int
166modstack_setup(struct module_stack* stack, const char* module_conf,
167 struct module_env* env)
168{
169 int i;
170 if(stack->num != 0)
171 modstack_desetup(stack, env);
172 /* fixed setup of the modules */
173 if(!modstack_config(stack, module_conf)) {
174 return 0;
175 }
176 env->need_to_validate = 0; /* set by module init below */
177 for(i=0; i<stack->num; i++) {
178 verbose(VERB_OPS, "init module %d: %s",
179 i, stack->mod[i]->name);
180 fptr_ok(fptr_whitelist_mod_init(stack->mod[i]->init));
181 if(!(*stack->mod[i]->init)(env, i)) {
182 log_err("module init for module %s failed",
183 stack->mod[i]->name);
184 return 0;
185 }
186 }
187 return 1;
188}
189
190void
191modstack_desetup(struct module_stack* stack, struct module_env* env)
192{
193 int i;
194 for(i=0; i<stack->num; i++) {
195 fptr_ok(fptr_whitelist_mod_deinit(stack->mod[i]->deinit));
196 (*stack->mod[i]->deinit)(env, i);
197 }
198 stack->num = 0;
199 free(stack->mod);
200 stack->mod = NULL;
201}
202
203int
204modstack_find(struct module_stack* stack, const char* name)
205{
206 int i;
207 for(i=0; i<stack->num; i++) {
208 if(strcmp(stack->mod[i]->name, name) == 0)
209 return i;
210 }
211 return -1;
212}
156 s++;
157 while(names[i]) {
158 if(strncmp(names[i], s, strlen(names[i])) == 0) {
159 s += strlen(names[i]);
160 *str = s;
161 return (*fb[i])();
162 }
163 i++;
164 }
165 return NULL;
166}
167
168int
169modstack_setup(struct module_stack* stack, const char* module_conf,
170 struct module_env* env)
171{
172 int i;
173 if(stack->num != 0)
174 modstack_desetup(stack, env);
175 /* fixed setup of the modules */
176 if(!modstack_config(stack, module_conf)) {
177 return 0;
178 }
179 env->need_to_validate = 0; /* set by module init below */
180 for(i=0; i<stack->num; i++) {
181 verbose(VERB_OPS, "init module %d: %s",
182 i, stack->mod[i]->name);
183 fptr_ok(fptr_whitelist_mod_init(stack->mod[i]->init));
184 if(!(*stack->mod[i]->init)(env, i)) {
185 log_err("module init for module %s failed",
186 stack->mod[i]->name);
187 return 0;
188 }
189 }
190 return 1;
191}
192
193void
194modstack_desetup(struct module_stack* stack, struct module_env* env)
195{
196 int i;
197 for(i=0; i<stack->num; i++) {
198 fptr_ok(fptr_whitelist_mod_deinit(stack->mod[i]->deinit));
199 (*stack->mod[i]->deinit)(env, i);
200 }
201 stack->num = 0;
202 free(stack->mod);
203 stack->mod = NULL;
204}
205
206int
207modstack_find(struct module_stack* stack, const char* name)
208{
209 int i;
210 for(i=0; i<stack->num; i++) {
211 if(strcmp(stack->mod[i]->name, name) == 0)
212 return i;
213 }
214 return -1;
215}