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"
43 #include "util/config_file.h"
44 #include "util/regional.h"
45 #include "util/data/dname.h"
46 #include "util/net_help.h"
49 strextstate(enum module_ext_state s)
52 case module_state_initial: return "module_state_initial";
53 case module_wait_reply: return "module_wait_reply";
54 case module_wait_module: return "module_wait_module";
55 case module_restart_next: return "module_restart_next";
56 case module_wait_subquery: return "module_wait_subquery";
57 case module_error: return "module_error";
58 case module_finished: return "module_finished";
60 return "bad_extstate_value";
64 strmodulevent(enum module_ev e)
67 case module_event_new: return "module_event_new";
68 case module_event_pass: return "module_event_pass";
69 case module_event_reply: return "module_event_reply";
70 case module_event_noreply: return "module_event_noreply";
71 case module_event_capsfail: return "module_event_capsfail";
72 case module_event_moddone: return "module_event_moddone";
73 case module_event_error: return "module_event_error";
75 return "bad_event_value";
78 void errinf(struct module_qstate* qstate, const char* str)
80 errinf_ede(qstate, str, LDNS_EDE_NONE);
83 void errinf_ede(struct module_qstate* qstate,
84 const char* str, sldns_ede_code reason_bogus)
86 struct errinf_strlist* p;
87 if(!str || (qstate->env->cfg->val_log_level < 2 &&
88 !qstate->env->cfg->log_servfail)) {
91 p = (struct errinf_strlist*)regional_alloc(qstate->region, sizeof(*p));
93 log_err("malloc failure in validator-error-info string");
97 p->str = regional_strdup(qstate->region, str);
98 p->reason_bogus = reason_bogus;
100 log_err("malloc failure in validator-error-info string");
105 struct errinf_strlist* q = qstate->errinf;
109 } else qstate->errinf = p;
112 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin)
115 if(qstate->env->cfg->val_log_level < 2 && !qstate->env->cfg->log_servfail)
117 for(p=origin; p; p=p->next) {
120 snprintf(buf, sizeof(buf), "from ");
121 else snprintf(buf, sizeof(buf), "and ");
123 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf),
126 addr_to_str(&p->addr, p->len, buf+strlen(buf),
127 sizeof(buf)-strlen(buf));
132 char* errinf_to_str_bogus(struct module_qstate* qstate)
136 size_t left = sizeof(buf);
137 struct errinf_strlist* s;
138 char dname[LDNS_MAX_DOMAINLEN+1];
140 sldns_wire2str_type_buf(qstate->qinfo.qtype, t, sizeof(t));
141 sldns_wire2str_class_buf(qstate->qinfo.qclass, c, sizeof(c));
142 dname_str(qstate->qinfo.qname, dname);
143 snprintf(p, left, "validation failure <%s %s %s>:", dname, t, c);
144 left -= strlen(p); p += strlen(p);
146 snprintf(p, left, " misc failure");
147 else for(s=qstate->errinf; s; s=s->next) {
148 snprintf(p, left, " %s", s->str);
149 left -= strlen(p); p += strlen(p);
153 log_err("malloc failure in errinf_to_str");
157 /* Try to find the latest (most specific) dnssec failure */
158 sldns_ede_code errinf_to_reason_bogus(struct module_qstate* qstate)
160 struct errinf_strlist* s;
161 sldns_ede_code ede = LDNS_EDE_NONE;
162 for(s=qstate->errinf; s; s=s->next) {
163 if(s->reason_bogus == LDNS_EDE_NONE) continue;
164 if(ede != LDNS_EDE_NONE
165 && ede != LDNS_EDE_DNSSEC_BOGUS
166 && s->reason_bogus == LDNS_EDE_DNSSEC_BOGUS) continue;
167 ede = s->reason_bogus;
172 char* errinf_to_str_servfail(struct module_qstate* qstate)
176 size_t left = sizeof(buf);
177 struct errinf_strlist* s;
178 char dname[LDNS_MAX_DOMAINLEN+1];
180 sldns_wire2str_type_buf(qstate->qinfo.qtype, t, sizeof(t));
181 sldns_wire2str_class_buf(qstate->qinfo.qclass, c, sizeof(c));
182 dname_str(qstate->qinfo.qname, dname);
183 snprintf(p, left, "SERVFAIL <%s %s %s>:", dname, t, c);
184 left -= strlen(p); p += strlen(p);
186 snprintf(p, left, " misc failure");
187 else for(s=qstate->errinf; s; s=s->next) {
188 snprintf(p, left, " %s", s->str);
189 left -= strlen(p); p += strlen(p);
193 log_err("malloc failure in errinf_to_str");
197 char* errinf_to_str_misc(struct module_qstate* qstate)
201 size_t left = sizeof(buf);
202 struct errinf_strlist* s;
204 snprintf(p, left, "misc failure");
205 else for(s=qstate->errinf; s; s=s->next) {
206 snprintf(p, left, "%s%s", (s==qstate->errinf?"":" "), s->str);
207 left -= strlen(p); p += strlen(p);
211 log_err("malloc failure in errinf_to_str");
215 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr)
218 char dname[LDNS_MAX_DOMAINLEN+1];
220 if((qstate->env->cfg->val_log_level < 2 && !qstate->env->cfg->log_servfail) || !rr)
222 sldns_wire2str_type_buf(ntohs(rr->rk.type), t, sizeof(t));
223 sldns_wire2str_class_buf(ntohs(rr->rk.rrset_class), c, sizeof(c));
224 dname_str(rr->rk.dname, dname);
225 snprintf(buf, sizeof(buf), "for <%s %s %s>", dname, t, c);
229 void errinf_dname(struct module_qstate* qstate, const char* str, uint8_t* dname)
232 char buf[LDNS_MAX_DOMAINLEN+1];
233 if((qstate->env->cfg->val_log_level < 2 && !qstate->env->cfg->log_servfail) || !str || !dname)
235 dname_str(dname, buf);
236 snprintf(b, sizeof(b), "%s %s", str, buf);
241 edns_known_options_init(struct module_env* env)
243 env->edns_known_options_num = 0;
244 env->edns_known_options = (struct edns_known_option*)calloc(
245 MAX_KNOWN_EDNS_OPTS, sizeof(struct edns_known_option));
246 if(!env->edns_known_options) return 0;
251 edns_known_options_delete(struct module_env* env)
253 free(env->edns_known_options);
254 env->edns_known_options = NULL;
255 env->edns_known_options_num = 0;
259 edns_register_option(uint16_t opt_code, int bypass_cache_stage,
260 int no_aggregation, struct module_env* env)
264 log_err("invalid edns registration: "
265 "trying to register option after module init phase");
270 * Checking if we are full first is faster but it does not provide
271 * the option to change the flags when the array is full.
272 * It only impacts unbound initialization, leave it for now.
274 /* Check if the option is already registered. */
275 for(i=0; i<env->edns_known_options_num; i++)
276 if(env->edns_known_options[i].opt_code == opt_code)
278 /* If it is not yet registered check if we have space to add a new one. */
279 if(i == env->edns_known_options_num) {
280 if(env->edns_known_options_num >= MAX_KNOWN_EDNS_OPTS) {
281 log_err("invalid edns registration: maximum options reached");
284 env->edns_known_options_num++;
286 env->edns_known_options[i].opt_code = opt_code;
287 env->edns_known_options[i].bypass_cache_stage = bypass_cache_stage;
288 env->edns_known_options[i].no_aggregation = no_aggregation;
293 inplace_cb_register(void* cb, enum inplace_cb_list_type type, void* cbarg,
294 struct module_env* env, int id)
296 struct inplace_cb* callback;
297 struct inplace_cb** prevp;
299 log_err("invalid edns callback registration: "
300 "trying to register callback after module init phase");
304 callback = (struct inplace_cb*)calloc(1, sizeof(*callback));
305 if(callback == NULL) {
306 log_err("out of memory during edns callback registration.");
310 callback->next = NULL;
312 callback->cb_arg = cbarg;
314 prevp = (struct inplace_cb**) &env->inplace_cb_lists[type];
315 /* append at end of list */
316 while(*prevp != NULL)
317 prevp = &((*prevp)->next);
323 inplace_cb_delete(struct module_env* env, enum inplace_cb_list_type type,
326 struct inplace_cb* temp = env->inplace_cb_lists[type];
327 struct inplace_cb* prev = NULL;
332 env->inplace_cb_lists[type] = temp->next;
334 temp = env->inplace_cb_lists[type];
337 prev->next = temp->next;
349 struct edns_known_option*
350 edns_option_is_known(uint16_t opt_code, struct module_env* env)
353 for(i=0; i<env->edns_known_options_num; i++)
354 if(env->edns_known_options[i].opt_code == opt_code)
355 return env->edns_known_options + i;
360 edns_bypass_cache_stage(struct edns_option* list, struct module_env* env)
363 for(; list; list=list->next)
364 for(i=0; i<env->edns_known_options_num; i++)
365 if(env->edns_known_options[i].opt_code == list->opt_code &&
366 env->edns_known_options[i].bypass_cache_stage == 1)
372 unique_mesh_state(struct edns_option* list, struct module_env* env)
377 for(; list; list=list->next)
378 for(i=0; i<env->edns_known_options_num; i++)
379 if(env->edns_known_options[i].opt_code == list->opt_code &&
380 env->edns_known_options[i].no_aggregation == 1)
386 log_edns_known_options(enum verbosity_value level, struct module_env* env)
391 if(env->edns_known_options_num > 0 && verbosity >= level) {
392 verbose(level, "EDNS known options:");
393 verbose(level, " Code: Bypass_cache_stage: Aggregate_mesh:");
394 for(i=0; i<env->edns_known_options_num; i++) {
397 (void)sldns_wire2str_edns_option_code_print(&s, &slen,
398 env->edns_known_options[i].opt_code);
399 verbose(level, " %-8.8s %-19s %-15s", str,
400 env->edns_known_options[i].bypass_cache_stage?"YES":"NO",
401 env->edns_known_options[i].no_aggregation?"NO":"YES");
407 copy_state_to_super(struct module_qstate* qstate, int ATTR_UNUSED(id),
408 struct module_qstate* super)
410 /* Overwrite super's was_ratelimited only when it was not set */
411 if(!super->was_ratelimited) {
412 super->was_ratelimited = qstate->was_ratelimited;