2 * util/module.c - module interface
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.
37 * Implementation of module.h.
41 #include "util/module.h"
42 #include "sldns/wire2str.h"
45 strextstate(enum module_ext_state s)
48 case module_state_initial: return "module_state_initial";
49 case module_wait_reply: return "module_wait_reply";
50 case module_wait_module: return "module_wait_module";
51 case module_restart_next: return "module_restart_next";
52 case module_wait_subquery: return "module_wait_subquery";
53 case module_error: return "module_error";
54 case module_finished: return "module_finished";
56 return "bad_extstate_value";
60 strmodulevent(enum module_ev e)
63 case module_event_new: return "module_event_new";
64 case module_event_pass: return "module_event_pass";
65 case module_event_reply: return "module_event_reply";
66 case module_event_noreply: return "module_event_noreply";
67 case module_event_capsfail: return "module_event_capsfail";
68 case module_event_moddone: return "module_event_moddone";
69 case module_event_error: return "module_event_error";
71 return "bad_event_value";
75 edns_known_options_init(struct module_env* env)
77 env->edns_known_options_num = 0;
78 env->edns_known_options = (struct edns_known_option*)calloc(
79 MAX_KNOWN_EDNS_OPTS, sizeof(struct edns_known_option));
80 if(!env->edns_known_options) return 0;
85 edns_known_options_delete(struct module_env* env)
87 free(env->edns_known_options);
88 env->edns_known_options = NULL;
89 env->edns_known_options_num = 0;
93 edns_register_option(uint16_t opt_code, int bypass_cache_stage,
94 int no_aggregation, struct module_env* env)
98 log_err("invalid edns registration: "
99 "trying to register option after module init phase");
104 * Checking if we are full first is faster but it does not provide
105 * the option to change the flags when the array is full.
106 * It only impacts unbound initialization, leave it for now.
108 /* Check if the option is already registered. */
109 for(i=0; i<env->edns_known_options_num; i++)
110 if(env->edns_known_options[i].opt_code == opt_code)
112 /* If it is not yet registered check if we have space to add a new one. */
113 if(i == env->edns_known_options_num) {
114 if(env->edns_known_options_num >= MAX_KNOWN_EDNS_OPTS) {
115 log_err("invalid edns registration: maximum options reached");
118 env->edns_known_options_num++;
120 env->edns_known_options[i].opt_code = opt_code;
121 env->edns_known_options[i].bypass_cache_stage = bypass_cache_stage;
122 env->edns_known_options[i].no_aggregation = no_aggregation;
127 inplace_cb_register(void* cb, enum inplace_cb_list_type type, void* cbarg,
128 struct module_env* env, int id)
130 struct inplace_cb* callback;
131 struct inplace_cb** prevp;
133 log_err("invalid edns callback registration: "
134 "trying to register callback after module init phase");
138 callback = (struct inplace_cb*)calloc(1, sizeof(*callback));
139 if(callback == NULL) {
140 log_err("out of memory during edns callback registration.");
144 callback->next = NULL;
146 callback->cb_arg = cbarg;
148 prevp = (struct inplace_cb**) &env->inplace_cb_lists[type];
149 /* append at end of list */
150 while(*prevp != NULL)
151 prevp = &((*prevp)->next);
157 inplace_cb_delete(struct module_env* env, enum inplace_cb_list_type type,
160 struct inplace_cb* temp = env->inplace_cb_lists[type];
161 struct inplace_cb* prev = NULL;
166 env->inplace_cb_lists[type] = temp->next;
168 temp = env->inplace_cb_lists[type];
171 prev->next = temp->next;
183 struct edns_known_option*
184 edns_option_is_known(uint16_t opt_code, struct module_env* env)
187 for(i=0; i<env->edns_known_options_num; i++)
188 if(env->edns_known_options[i].opt_code == opt_code)
189 return env->edns_known_options + i;
194 edns_bypass_cache_stage(struct edns_option* list, struct module_env* env)
197 for(; list; list=list->next)
198 for(i=0; i<env->edns_known_options_num; i++)
199 if(env->edns_known_options[i].opt_code == list->opt_code &&
200 env->edns_known_options[i].bypass_cache_stage == 1)
206 unique_mesh_state(struct edns_option* list, struct module_env* env)
211 for(; list; list=list->next)
212 for(i=0; i<env->edns_known_options_num; i++)
213 if(env->edns_known_options[i].opt_code == list->opt_code &&
214 env->edns_known_options[i].no_aggregation == 1)
220 log_edns_known_options(enum verbosity_value level, struct module_env* env)
225 if(env->edns_known_options_num > 0 && verbosity >= level) {
226 verbose(level, "EDNS known options:");
227 verbose(level, " Code: Bypass_cache_stage: Aggregate_mesh:");
228 for(i=0; i<env->edns_known_options_num; i++) {
231 (void)sldns_wire2str_edns_option_code_print(&s, &slen,
232 env->edns_known_options[i].opt_code);
233 verbose(level, " %-8.8s %-19s %-15s", str,
234 env->edns_known_options[i].bypass_cache_stage?"YES":"NO",
235 env->edns_known_options[i].no_aggregation?"NO":"YES");
241 copy_state_to_super(struct module_qstate* qstate, int ATTR_UNUSED(id),
242 struct module_qstate* super)
244 /* Overwrite super's was_ratelimited only when it was not set */
245 if(!super->was_ratelimited) {
246 super->was_ratelimited = qstate->was_ratelimited;