xref: /freebsd/contrib/unbound/services/modstack.h (revision b7579f77d18196a58ff700756c84dc9a302a7f67)
1*b7579f77SDag-Erling Smørgrav /*
2*b7579f77SDag-Erling Smørgrav  * services/modstack.h - stack of modules
3*b7579f77SDag-Erling Smørgrav  *
4*b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5*b7579f77SDag-Erling Smørgrav  *
6*b7579f77SDag-Erling Smørgrav  * This software is open source.
7*b7579f77SDag-Erling Smørgrav  *
8*b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9*b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10*b7579f77SDag-Erling Smørgrav  * are met:
11*b7579f77SDag-Erling Smørgrav  *
12*b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13*b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14*b7579f77SDag-Erling Smørgrav  *
15*b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16*b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17*b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18*b7579f77SDag-Erling Smørgrav  *
19*b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20*b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21*b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22*b7579f77SDag-Erling Smørgrav  *
23*b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*b7579f77SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25*b7579f77SDag-Erling Smørgrav  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26*b7579f77SDag-Erling Smørgrav  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27*b7579f77SDag-Erling Smørgrav  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28*b7579f77SDag-Erling Smørgrav  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29*b7579f77SDag-Erling Smørgrav  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30*b7579f77SDag-Erling Smørgrav  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31*b7579f77SDag-Erling Smørgrav  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32*b7579f77SDag-Erling Smørgrav  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33*b7579f77SDag-Erling Smørgrav  * POSSIBILITY OF SUCH DAMAGE.
34*b7579f77SDag-Erling Smørgrav  */
35*b7579f77SDag-Erling Smørgrav 
36*b7579f77SDag-Erling Smørgrav /**
37*b7579f77SDag-Erling Smørgrav  * \file
38*b7579f77SDag-Erling Smørgrav  *
39*b7579f77SDag-Erling Smørgrav  * This file contains functions to help maintain a stack of modules.
40*b7579f77SDag-Erling Smørgrav  */
41*b7579f77SDag-Erling Smørgrav 
42*b7579f77SDag-Erling Smørgrav #ifndef SERVICES_MODSTACK_H
43*b7579f77SDag-Erling Smørgrav #define SERVICES_MODSTACK_H
44*b7579f77SDag-Erling Smørgrav struct module_func_block;
45*b7579f77SDag-Erling Smørgrav struct module_env;
46*b7579f77SDag-Erling Smørgrav 
47*b7579f77SDag-Erling Smørgrav /**
48*b7579f77SDag-Erling Smørgrav  * Stack of modules.
49*b7579f77SDag-Erling Smørgrav  */
50*b7579f77SDag-Erling Smørgrav struct module_stack {
51*b7579f77SDag-Erling Smørgrav 	/** the number of modules */
52*b7579f77SDag-Erling Smørgrav 	int num;
53*b7579f77SDag-Erling Smørgrav 	/** the module callbacks, array of num_modules length (ref only) */
54*b7579f77SDag-Erling Smørgrav 	struct module_func_block** mod;
55*b7579f77SDag-Erling Smørgrav };
56*b7579f77SDag-Erling Smørgrav 
57*b7579f77SDag-Erling Smørgrav /**
58*b7579f77SDag-Erling Smørgrav  * Init a stack of modules
59*b7579f77SDag-Erling Smørgrav  * @param stack: initialised as empty.
60*b7579f77SDag-Erling Smørgrav  */
61*b7579f77SDag-Erling Smørgrav void modstack_init(struct module_stack* stack);
62*b7579f77SDag-Erling Smørgrav 
63*b7579f77SDag-Erling Smørgrav /**
64*b7579f77SDag-Erling Smørgrav  * Read config file module settings and set up the modfunc block
65*b7579f77SDag-Erling Smørgrav  * @param stack: the stack of modules (empty before call).
66*b7579f77SDag-Erling Smørgrav  * @param module_conf: string what modules to insert.
67*b7579f77SDag-Erling Smørgrav  * @return false on error
68*b7579f77SDag-Erling Smørgrav  */
69*b7579f77SDag-Erling Smørgrav int modstack_config(struct module_stack* stack, const char* module_conf);
70*b7579f77SDag-Erling Smørgrav 
71*b7579f77SDag-Erling Smørgrav /**
72*b7579f77SDag-Erling Smørgrav  * Get funcblock for module name
73*b7579f77SDag-Erling Smørgrav  * @param str: string with module name. Advanced to next value on success.
74*b7579f77SDag-Erling Smørgrav  *	The string is assumed whitespace separated list of module names.
75*b7579f77SDag-Erling Smørgrav  * @return funcblock or NULL on error.
76*b7579f77SDag-Erling Smørgrav  */
77*b7579f77SDag-Erling Smørgrav struct module_func_block* module_factory(const char** str);
78*b7579f77SDag-Erling Smørgrav 
79*b7579f77SDag-Erling Smørgrav /**
80*b7579f77SDag-Erling Smørgrav  * Get list of modules available.
81*b7579f77SDag-Erling Smørgrav  * @return list of modules available. Static strings, ends with NULL.
82*b7579f77SDag-Erling Smørgrav  */
83*b7579f77SDag-Erling Smørgrav const char** module_list_avail(void);
84*b7579f77SDag-Erling Smørgrav 
85*b7579f77SDag-Erling Smørgrav /**
86*b7579f77SDag-Erling Smørgrav  * Setup modules. Assigns ids and calls module_init.
87*b7579f77SDag-Erling Smørgrav  * @param stack: if not empty beforehand, it will be desetup()ed.
88*b7579f77SDag-Erling Smørgrav  *	It is then modstack_configged().
89*b7579f77SDag-Erling Smørgrav  * @param module_conf: string what modules to insert.
90*b7579f77SDag-Erling Smørgrav  * @param env: module environment which is inited by the modules.
91*b7579f77SDag-Erling Smørgrav  *	environment should have a superalloc, cfg,
92*b7579f77SDag-Erling Smørgrav  *	env.need_to_validate is set by the modules.
93*b7579f77SDag-Erling Smørgrav  * @return on false a module init failed.
94*b7579f77SDag-Erling Smørgrav  */
95*b7579f77SDag-Erling Smørgrav int modstack_setup(struct module_stack* stack, const char* module_conf,
96*b7579f77SDag-Erling Smørgrav 	struct module_env* env);
97*b7579f77SDag-Erling Smørgrav 
98*b7579f77SDag-Erling Smørgrav /**
99*b7579f77SDag-Erling Smørgrav  * Desetup the modules, deinit, delete.
100*b7579f77SDag-Erling Smørgrav  * @param stack: made empty.
101*b7579f77SDag-Erling Smørgrav  * @param env: module env for module deinit() calls.
102*b7579f77SDag-Erling Smørgrav  */
103*b7579f77SDag-Erling Smørgrav void modstack_desetup(struct module_stack* stack, struct module_env* env);
104*b7579f77SDag-Erling Smørgrav 
105*b7579f77SDag-Erling Smørgrav /**
106*b7579f77SDag-Erling Smørgrav  * Find index of module by name.
107*b7579f77SDag-Erling Smørgrav  * @param stack: to look in
108*b7579f77SDag-Erling Smørgrav  * @param name: the name to look for
109*b7579f77SDag-Erling Smørgrav  * @return -1 on failure, otherwise index number.
110*b7579f77SDag-Erling Smørgrav  */
111*b7579f77SDag-Erling Smørgrav int modstack_find(struct module_stack* stack, const char* name);
112*b7579f77SDag-Erling Smørgrav 
113*b7579f77SDag-Erling Smørgrav #endif /* SERVICES_MODSTACK_H */
114