1238106Sdes/*
2238106Sdes * services/modstack.h - stack of modules
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24266114Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25266114Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26266114Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27266114Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28266114Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29266114Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30266114Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31266114Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32266114Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33266114Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file contains functions to help maintain a stack of modules.
40238106Sdes */
41238106Sdes
42238106Sdes#ifndef SERVICES_MODSTACK_H
43238106Sdes#define SERVICES_MODSTACK_H
44238106Sdesstruct module_func_block;
45238106Sdesstruct module_env;
46238106Sdes
47238106Sdes/**
48238106Sdes * Stack of modules.
49238106Sdes */
50238106Sdesstruct module_stack {
51238106Sdes	/** the number of modules */
52238106Sdes	int num;
53238106Sdes	/** the module callbacks, array of num_modules length (ref only) */
54238106Sdes	struct module_func_block** mod;
55238106Sdes};
56238106Sdes
57238106Sdes/**
58238106Sdes * Init a stack of modules
59238106Sdes * @param stack: initialised as empty.
60238106Sdes */
61238106Sdesvoid modstack_init(struct module_stack* stack);
62238106Sdes
63238106Sdes/**
64238106Sdes * Read config file module settings and set up the modfunc block
65238106Sdes * @param stack: the stack of modules (empty before call).
66238106Sdes * @param module_conf: string what modules to insert.
67238106Sdes * @return false on error
68238106Sdes */
69238106Sdesint modstack_config(struct module_stack* stack, const char* module_conf);
70238106Sdes
71238106Sdes/**
72238106Sdes * Get funcblock for module name
73238106Sdes * @param str: string with module name. Advanced to next value on success.
74238106Sdes *	The string is assumed whitespace separated list of module names.
75238106Sdes * @return funcblock or NULL on error.
76238106Sdes */
77238106Sdesstruct module_func_block* module_factory(const char** str);
78238106Sdes
79238106Sdes/**
80238106Sdes * Get list of modules available.
81238106Sdes * @return list of modules available. Static strings, ends with NULL.
82238106Sdes */
83238106Sdesconst char** module_list_avail(void);
84238106Sdes
85238106Sdes/**
86238106Sdes * Setup modules. Assigns ids and calls module_init.
87238106Sdes * @param stack: if not empty beforehand, it will be desetup()ed.
88238106Sdes *	It is then modstack_configged().
89238106Sdes * @param module_conf: string what modules to insert.
90238106Sdes * @param env: module environment which is inited by the modules.
91238106Sdes *	environment should have a superalloc, cfg,
92238106Sdes *	env.need_to_validate is set by the modules.
93238106Sdes * @return on false a module init failed.
94238106Sdes */
95238106Sdesint modstack_setup(struct module_stack* stack, const char* module_conf,
96238106Sdes	struct module_env* env);
97238106Sdes
98238106Sdes/**
99238106Sdes * Desetup the modules, deinit, delete.
100238106Sdes * @param stack: made empty.
101238106Sdes * @param env: module env for module deinit() calls.
102238106Sdes */
103238106Sdesvoid modstack_desetup(struct module_stack* stack, struct module_env* env);
104238106Sdes
105238106Sdes/**
106238106Sdes * Find index of module by name.
107238106Sdes * @param stack: to look in
108238106Sdes * @param name: the name to look for
109238106Sdes * @return -1 on failure, otherwise index number.
110238106Sdes */
111238106Sdesint modstack_find(struct module_stack* stack, const char* name);
112238106Sdes
113356345Scy/** fetch memory for a module by name, returns 0 if module not there */
114356345Scysize_t mod_get_mem(struct module_env* env, const char* name);
115356345Scy
116238106Sdes#endif /* SERVICES_MODSTACK_H */
117