]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - services/modstack.c
Vendor import of Unbound 1.6.3.
[FreeBSD/FreeBSD.git] / services / modstack.c
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"
47 #include "iterator/iterator.h"
48 #include "validator/validator.h"
49 #include "respip/respip.h"
50
51 #ifdef WITH_PYTHONMODULE
52 #include "pythonmod/pythonmod.h"
53 #endif
54 #ifdef USE_CACHEDB
55 #include "cachedb/cachedb.h"
56 #endif
57 #ifdef CLIENT_SUBNET
58 #include "edns-subnet/subnetmod.h"
59 #endif
60
61 /** count number of modules (words) in the string */
62 static int
63 count_modules(const char* s)
64 {
65         int num = 0;
66         if(!s)
67                 return 0;
68         while(*s) {
69                 /* skip whitespace */
70                 while(*s && isspace((unsigned char)*s))
71                         s++;
72                 if(*s && !isspace((unsigned char)*s)) {
73                         /* skip identifier */
74                         num++;
75                         while(*s && !isspace((unsigned char)*s))
76                                 s++;
77                 }
78         }
79         return num;
80 }
81
82 void 
83 modstack_init(struct module_stack* stack)
84 {
85         stack->num = 0;
86         stack->mod = NULL;
87 }
88
89 int 
90 modstack_config(struct module_stack* stack, const char* module_conf)
91 {
92         int i;
93         verbose(VERB_QUERY, "module config: \"%s\"", module_conf);
94         stack->num = count_modules(module_conf);
95         if(stack->num == 0) {
96                 log_err("error: no modules specified");
97                 return 0;
98         }
99         if(stack->num > MAX_MODULE) {
100                 log_err("error: too many modules (%d max %d)",
101                         stack->num, MAX_MODULE);
102                 return 0;
103         }
104         stack->mod = (struct module_func_block**)calloc((size_t)
105                 stack->num, sizeof(struct module_func_block*));
106         if(!stack->mod) {
107                 log_err("out of memory");
108                 return 0;
109         }
110         for(i=0; i<stack->num; i++) {
111                 stack->mod[i] = module_factory(&module_conf);
112                 if(!stack->mod[i]) {
113                         log_err("Unknown value for next module: '%s'",
114                                 module_conf);
115                         return 0;
116                 }
117         }
118         return 1;
119 }
120
121 /** The list of module names */
122 const char**
123 module_list_avail(void)
124 {
125         /* these are the modules available */
126         static const char* names[] = {
127                 "dns64",
128 #ifdef WITH_PYTHONMODULE
129                 "python", 
130 #endif
131 #ifdef USE_CACHEDB
132                 "cachedb",
133 #endif
134 #ifdef CLIENT_SUBNET
135                 "subnetcache", 
136 #endif
137                 "respip",
138                 "validator", 
139                 "iterator", 
140                 NULL};
141         return names;
142 }
143
144 /** func block get function type */
145 typedef struct module_func_block* (*fbgetfunctype)(void);
146
147 /** The list of module func blocks */
148 static fbgetfunctype*
149 module_funcs_avail(void)
150 {
151         static struct module_func_block* (*fb[])(void) = {
152                 &dns64_get_funcblock,
153 #ifdef WITH_PYTHONMODULE
154                 &pythonmod_get_funcblock, 
155 #endif
156 #ifdef USE_CACHEDB
157                 &cachedb_get_funcblock,
158 #endif
159 #ifdef CLIENT_SUBNET
160                 &subnetmod_get_funcblock, 
161 #endif
162                 &respip_get_funcblock,
163                 &val_get_funcblock, 
164                 &iter_get_funcblock, 
165                 NULL};
166         return fb;
167 }
168
169 struct 
170 module_func_block* module_factory(const char** str)
171 {
172         int i = 0;
173         const char* s = *str;
174         const char** names = module_list_avail();
175         fbgetfunctype* fb = module_funcs_avail();
176         while(*s && isspace((unsigned char)*s))
177                 s++;
178         while(names[i]) {
179                 if(strncmp(names[i], s, strlen(names[i])) == 0) {
180                         s += strlen(names[i]);
181                         *str = s;
182                         return (*fb[i])();
183                 }
184                 i++;
185         }
186         return NULL;
187 }
188
189 int 
190 modstack_setup(struct module_stack* stack, const char* module_conf,
191         struct module_env* env)
192 {
193         int i;
194         if(stack->num != 0)
195                 modstack_desetup(stack, env);
196         /* fixed setup of the modules */
197         if(!modstack_config(stack, module_conf)) {
198                 return 0;
199         }
200         env->need_to_validate = 0; /* set by module init below */
201         for(i=0; i<stack->num; i++) {
202                 verbose(VERB_OPS, "init module %d: %s",
203                         i, stack->mod[i]->name);
204                 fptr_ok(fptr_whitelist_mod_init(stack->mod[i]->init));
205                 if(!(*stack->mod[i]->init)(env, i)) {
206                         log_err("module init for module %s failed",
207                                 stack->mod[i]->name);
208                         return 0;
209                 }
210         }
211         return 1;
212 }
213
214 void 
215 modstack_desetup(struct module_stack* stack, struct module_env* env)
216 {
217         int i;
218         for(i=0; i<stack->num; i++) {
219                 fptr_ok(fptr_whitelist_mod_deinit(stack->mod[i]->deinit));
220                 (*stack->mod[i]->deinit)(env, i);
221         }
222         stack->num = 0;
223         free(stack->mod);
224         stack->mod = NULL;
225 }
226
227 int 
228 modstack_find(struct module_stack* stack, const char* name)
229 {
230         int i;
231         for(i=0; i<stack->num; i++) {
232                 if(strcmp(stack->mod[i]->name, name) == 0)
233                         return i;
234         }
235         return -1;
236 }