2 * services/modstack.c - stack of modules
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains functions to help maintain a stack of modules.
43 #include "services/modstack.h"
44 #include "util/module.h"
45 #include "util/fptr_wlist.h"
46 #include "dns64/dns64.h"
47 #include "iterator/iterator.h"
48 #include "validator/validator.h"
49 #include "respip/respip.h"
51 #ifdef WITH_PYTHONMODULE
52 #include "pythonmod/pythonmod.h"
54 #ifdef WITH_DYNLIBMODULE
55 #include "dynlibmod/dynlibmod.h"
58 #include "cachedb/cachedb.h"
61 #include "ipsecmod/ipsecmod.h"
64 #include "edns-subnet/subnetmod.h"
67 #include "ipset/ipset.h"
70 /** count number of modules (words) in the string */
72 count_modules(const char* s)
79 while(*s && isspace((unsigned char)*s))
81 if(*s && !isspace((unsigned char)*s)) {
84 while(*s && !isspace((unsigned char)*s))
92 modstack_init(struct module_stack* stack)
99 modstack_config(struct module_stack* stack, const char* module_conf)
102 verbose(VERB_QUERY, "module config: \"%s\"", module_conf);
103 stack->num = count_modules(module_conf);
104 if(stack->num == 0) {
105 log_err("error: no modules specified");
108 if(stack->num > MAX_MODULE) {
109 log_err("error: too many modules (%d max %d)",
110 stack->num, MAX_MODULE);
113 stack->mod = (struct module_func_block**)calloc((size_t)
114 stack->num, sizeof(struct module_func_block*));
116 log_err("out of memory");
119 for(i=0; i<stack->num; i++) {
120 stack->mod[i] = module_factory(&module_conf);
123 snprintf(md, sizeof(md), "%s", module_conf);
124 if(strchr(md, ' ')) *(strchr(md, ' ')) = 0;
125 if(strchr(md, '\t')) *(strchr(md, '\t')) = 0;
126 log_err("Unknown value in module-config, module: '%s'."
127 " This module is not present (not compiled in),"
128 " See the list of linked modules with unbound -h",
136 /** The list of module names */
138 module_list_avail(void)
140 /* these are the modules available */
141 static const char* names[] = {
143 #ifdef WITH_PYTHONMODULE
146 #ifdef WITH_DYNLIBMODULE
168 /** func block get function type */
169 typedef struct module_func_block* (*fbgetfunctype)(void);
171 /** The list of module func blocks */
172 static fbgetfunctype*
173 module_funcs_avail(void)
175 static struct module_func_block* (*fb[])(void) = {
176 &dns64_get_funcblock,
177 #ifdef WITH_PYTHONMODULE
178 &pythonmod_get_funcblock,
180 #ifdef WITH_DYNLIBMODULE
181 &dynlibmod_get_funcblock,
184 &cachedb_get_funcblock,
187 &ipsecmod_get_funcblock,
190 &subnetmod_get_funcblock,
193 &ipset_get_funcblock,
195 &respip_get_funcblock,
203 module_func_block* module_factory(const char** str)
206 const char* s = *str;
207 const char** names = module_list_avail();
208 fbgetfunctype* fb = module_funcs_avail();
209 while(*s && isspace((unsigned char)*s))
212 if(strncmp(names[i], s, strlen(names[i])) == 0) {
213 s += strlen(names[i]);
223 modstack_setup(struct module_stack* stack, const char* module_conf,
224 struct module_env* env)
228 modstack_desetup(stack, env);
229 /* fixed setup of the modules */
230 if(!modstack_config(stack, module_conf)) {
233 env->need_to_validate = 0; /* set by module init below */
234 for(i=0; i<stack->num; i++) {
235 verbose(VERB_OPS, "init module %d: %s",
236 i, stack->mod[i]->name);
237 fptr_ok(fptr_whitelist_mod_init(stack->mod[i]->init));
238 if(!(*stack->mod[i]->init)(env, i)) {
239 log_err("module init for module %s failed",
240 stack->mod[i]->name);
248 modstack_desetup(struct module_stack* stack, struct module_env* env)
251 for(i=0; i<stack->num; i++) {
252 fptr_ok(fptr_whitelist_mod_deinit(stack->mod[i]->deinit));
253 (*stack->mod[i]->deinit)(env, i);
261 modstack_find(struct module_stack* stack, const char* name)
264 for(i=0; i<stack->num; i++) {
265 if(strcmp(stack->mod[i]->name, name) == 0)
272 mod_get_mem(struct module_env* env, const char* name)
274 int m = modstack_find(&env->mesh->mods, name);
276 fptr_ok(fptr_whitelist_mod_get_mem(env->mesh->
277 mods.mod[m]->get_mem));
278 return (*env->mesh->mods.mod[m]->get_mem)(env, m);