modstack.c revision 285206
142660Smarkm/*
293139Sru * services/modstack.c - stack of modules
321495Sjmacd *
493139Sru * Copyright (c) 2007, NLnet Labs. All rights reserved.
521495Sjmacd *
621495Sjmacd * This software is open source.
721495Sjmacd *
821495Sjmacd * Redistribution and use in source and binary forms, with or without
921495Sjmacd * modification, are permitted provided that the following conditions
1021495Sjmacd * are met:
1121495Sjmacd *
1221495Sjmacd * Redistributions of source code must retain the above copyright notice,
1321495Sjmacd * this list of conditions and the following disclaimer.
1421495Sjmacd *
1521495Sjmacd * Redistributions in binary form must reproduce the above copyright notice,
1621495Sjmacd * this list of conditions and the following disclaimer in the documentation
1721495Sjmacd * and/or other materials provided with the distribution.
1821495Sjmacd *
1921495Sjmacd * Neither the name of the NLNET LABS nor the names of its contributors may
2021495Sjmacd * be used to endorse or promote products derived from this software without
2121495Sjmacd * specific prior written permission.
2221495Sjmacd *
2321495Sjmacd * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2421495Sjmacd * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2521495Sjmacd * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2621495Sjmacd * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2721495Sjmacd * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2821495Sjmacd * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2921495Sjmacd * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3021495Sjmacd * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3142660Smarkm * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3221495Sjmacd * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3321495Sjmacd * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3421495Sjmacd */
3521495Sjmacd
3621495Sjmacd/**
3721495Sjmacd * \file
3821495Sjmacd *
3921495Sjmacd * This file contains functions to help maintain a stack of modules.
4021495Sjmacd */
4121495Sjmacd#include "config.h"
4221495Sjmacd#include <ctype.h>
4321495Sjmacd#include "services/modstack.h"
4421495Sjmacd#include "util/module.h"
4521495Sjmacd#include "util/fptr_wlist.h"
4621495Sjmacd#include "dns64/dns64.h"
4756160Sru#include "iterator/iterator.h"
4856160Sru#include "validator/validator.h"
4956160Sru
5056160Sru#ifdef WITH_PYTHONMODULE
5156160Sru#include "pythonmod/pythonmod.h"
5256160Sru#endif
5356160Sru
5456160Sru/** count number of modules (words) in the string */
5521495Sjmacdstatic int
5621495Sjmacdcount_modules(const char* s)
5721495Sjmacd{
5821495Sjmacd        int num = 0;
5921495Sjmacd        if(!s)
6021495Sjmacd                return 0;
6121495Sjmacd        while(*s) {
6221495Sjmacd                /* skip whitespace */
6321495Sjmacd                while(*s && isspace((unsigned char)*s))
6421495Sjmacd                        s++;
6521495Sjmacd                if(*s && !isspace((unsigned char)*s)) {
6621495Sjmacd                        /* skip identifier */
6721495Sjmacd                        num++;
6821495Sjmacd                        while(*s && !isspace((unsigned char)*s))
6921495Sjmacd                                s++;
7021495Sjmacd                }
7121495Sjmacd        }
7221495Sjmacd        return num;
7321495Sjmacd}
7421495Sjmacd
7521495Sjmacdvoid
7621495Sjmacdmodstack_init(struct module_stack* stack)
7721495Sjmacd{
7821495Sjmacd	stack->num = 0;
7921495Sjmacd	stack->mod = NULL;
8021495Sjmacd}
8121495Sjmacd
8221495Sjmacdint
8342660Smarkmmodstack_config(struct module_stack* stack, const char* module_conf)
8442660Smarkm{
8542660Smarkm        int i;
8642660Smarkm        verbose(VERB_QUERY, "module config: \"%s\"", module_conf);
8756160Sru        stack->num = count_modules(module_conf);
8821495Sjmacd        if(stack->num == 0) {
8942660Smarkm                log_err("error: no modules specified");
9042660Smarkm                return 0;
9142660Smarkm        }
9242660Smarkm        if(stack->num > MAX_MODULE) {
9342660Smarkm                log_err("error: too many modules (%d max %d)",
9442660Smarkm                        stack->num, MAX_MODULE);
9542660Smarkm                return 0;
9642660Smarkm        }
9742660Smarkm        stack->mod = (struct module_func_block**)calloc((size_t)
9842660Smarkm                stack->num, sizeof(struct module_func_block*));
9942660Smarkm        if(!stack->mod) {
10042660Smarkm                log_err("out of memory");
10156160Sru                return 0;
10242660Smarkm        }
10342660Smarkm        for(i=0; i<stack->num; i++) {
10442660Smarkm                stack->mod[i] = module_factory(&module_conf);
10542660Smarkm                if(!stack->mod[i]) {
10642660Smarkm                        log_err("Unknown value for next module: '%s'",
10756160Sru                                module_conf);
10856160Sru                        return 0;
10956160Sru                }
11056160Sru        }
11156160Sru        return 1;
11256160Sru}
11356160Sru
11456160Sru/** The list of module names */
11556160Sruconst char**
11656160Srumodule_list_avail(void)
11756160Sru{
11856160Sru        /* these are the modules available */
11956160Sru        static const char* names[] = {
12056160Sru		"dns64",
12156160Sru#ifdef WITH_PYTHONMODULE
12256160Sru		"python",
12356160Sru#endif
12456160Sru		"validator",
12556160Sru		"iterator",
12656160Sru		NULL};
12756160Sru	return names;
12856160Sru}
12956160Sru
13056160Sru/** func block get function type */
13156160Srutypedef struct module_func_block* (*fbgetfunctype)(void);
13256160Sru
13356160Sru/** The list of module func blocks */
13456160Srustatic fbgetfunctype*
13556160Srumodule_funcs_avail(void)
13656160Sru{
13756160Sru        static struct module_func_block* (*fb[])(void) = {
13856160Sru		&dns64_get_funcblock,
13956160Sru#ifdef WITH_PYTHONMODULE
14056160Sru		&pythonmod_get_funcblock,
14156160Sru#endif
14256160Sru		&val_get_funcblock,
14356160Sru		&iter_get_funcblock,
14442660Smarkm		NULL};
14521495Sjmacd	return fb;
14621495Sjmacd}
14721495Sjmacd
14821495Sjmacdstruct
14921495Sjmacdmodule_func_block* module_factory(const char** str)
15021495Sjmacd{
15121495Sjmacd        int i = 0;
15221495Sjmacd        const char* s = *str;
15321495Sjmacd	const char** names = module_list_avail();
15421495Sjmacd	fbgetfunctype* fb = module_funcs_avail();
15542660Smarkm        while(*s && isspace((unsigned char)*s))
15642660Smarkm                s++;
15742660Smarkm	while(names[i]) {
15821495Sjmacd                if(strncmp(names[i], s, strlen(names[i])) == 0) {
15921495Sjmacd                        s += strlen(names[i]);
16021495Sjmacd                        *str = s;
16121495Sjmacd                        return (*fb[i])();
16221495Sjmacd                }
16321495Sjmacd		i++;
16421495Sjmacd        }
16521495Sjmacd        return NULL;
16621495Sjmacd}
16721495Sjmacd
16821495Sjmacdint
16921495Sjmacdmodstack_setup(struct module_stack* stack, const char* module_conf,
17021495Sjmacd	struct module_env* env)
17121495Sjmacd{
17221495Sjmacd        int i;
17321495Sjmacd        if(stack->num != 0)
17421495Sjmacd                modstack_desetup(stack, env);
17521495Sjmacd        /* fixed setup of the modules */
17621495Sjmacd        if(!modstack_config(stack, module_conf)) {
17721495Sjmacd		return 0;
17821495Sjmacd        }
17942660Smarkm        env->need_to_validate = 0; /* set by module init below */
18021495Sjmacd        for(i=0; i<stack->num; i++) {
18121495Sjmacd                verbose(VERB_OPS, "init module %d: %s",
18256160Sru                        i, stack->mod[i]->name);
18356160Sru                fptr_ok(fptr_whitelist_mod_init(stack->mod[i]->init));
18421495Sjmacd                if(!(*stack->mod[i]->init)(env, i)) {
18521495Sjmacd                        log_err("module init for module %s failed",
18621495Sjmacd                                stack->mod[i]->name);
18742660Smarkm			return 0;
18842660Smarkm                }
18921495Sjmacd        }
19042660Smarkm	return 1;
19142660Smarkm}
19242660Smarkm
19342660Smarkmvoid
19421495Sjmacdmodstack_desetup(struct module_stack* stack, struct module_env* env)
19556160Sru{
19621495Sjmacd        int i;
19756160Sru        for(i=0; i<stack->num; i++) {
19842660Smarkm                fptr_ok(fptr_whitelist_mod_deinit(stack->mod[i]->deinit));
19921495Sjmacd                (*stack->mod[i]->deinit)(env, i);
20056160Sru        }
20121495Sjmacd        stack->num = 0;
20221495Sjmacd        free(stack->mod);
20321495Sjmacd        stack->mod = NULL;
20456160Sru}
20556160Sru
20656160Sruint
20756160Srumodstack_find(struct module_stack* stack, const char* name)
20856160Sru{
20956160Sru	int i;
21056160Sru        for(i=0; i<stack->num; i++) {
21121495Sjmacd		if(strcmp(stack->mod[i]->name, name) == 0)
21256160Sru			return i;
21356160Sru	}
21456160Sru	return -1;
21556160Sru}
21656160Sru