xref: /openbsd/usr.sbin/unbound/util/module.h (revision 6f40fd34)
1 /*
2  * util/module.h - DNS handling module interface
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains the interface for DNS handling modules.
40  *
41  * The module interface uses the DNS modules as state machines.  The
42  * state machines are activated in sequence to operate on queries.  Once
43  * they are done, the reply is passed back.  In the usual setup the mesh
44  * is the caller of the state machines and once things are done sends replies
45  * and invokes result callbacks.
46  *
47  * The module provides a number of functions, listed in the module_func_block.
48  * The module is inited and destroyed and memory usage queries, for the
49  * module as a whole, for entire-module state (such as a cache).  And per-query
50  * functions are called, operate to move the state machine and cleanup of
51  * the per-query state.
52  *
53  * Most per-query state should simply be allocated in the query region.
54  * This is destroyed at the end of the query.
55  *
56  * The module environment contains services and information and caches
57  * shared by the modules and the rest of the system.  It also contains
58  * function pointers for module-specific tasks (like sending queries).
59  *
60  * *** Example module calls for a normal query
61  *
62  * In this example, the query does not need recursion, all the other data
63  * can be found in the cache.  This makes the example shorter.
64  *
65  * At the start of the program the iterator module is initialised.
66  * The iterator module sets up its global state, such as donotquery lists
67  * and private address trees.
68  *
69  * A query comes in, and a mesh entry is created for it.  The mesh
70  * starts the resolution process.  The validator module is the first
71  * in the list of modules, and it is started on this new query.  The
72  * operate() function is called.  The validator decides it needs not do
73  * anything yet until there is a result and returns wait_module, that
74  * causes the next module in the list to be started.
75  *
76  * The next module is the iterator.  It is started on the passed query and
77  * decides to perform a lookup.  For this simple example, the delegation
78  * point information is available, and all the iterator wants to do is
79  * send a UDP query.  The iterator uses env.send_query() to send the
80  * query.  Then the iterator suspends (returns from the operate call).
81  *
82  * When the UDP reply comes back (and on errors and timeouts), the
83  * operate function is called for the query, on the iterator module,
84  * with the event that there is a reply.  The iterator decides that this
85  * is enough, the work is done.  It returns the value finished from the
86  * operate call, which causes the previous module to be started.
87  *
88  * The previous module, the validator module, is started with the event
89  * that the iterator module is done.  The validator decides to validate
90  * the query.  Once it is done (which could take recursive lookups, but
91  * in this example no recursive lookups are needed), it returns from the
92  * operate function with finished.
93  *
94  * There is no previous module from the validator module, and the mesh
95  * takes this to mean that the query is finally done.  The mesh invokes
96  * callbacks and sends packets to queriers.
97  *
98  * If other modules had been waiting (recursively) on the answer to this
99  * query, then the mesh will tell them about it.  It calls the inform_super
100  * routine on all the waiting modules, and once that is done it calls all of
101  * them with the operate() call.  During inform_super the query that is done
102  * still exists and information can be copied from it (but the module should
103  * not really re-entry codepoints and services).  During the operate call
104  * the modules can use stored state to continue operation with the results.
105  * (network buffers are used to contain the answer packet during the
106  * inform_super phase, but after that the network buffers will be cleared
107  * of their contents so that other tasks can be performed).
108  *
109  * *** Example module calls for recursion
110  *
111  * A module is called in operate, and it decides that it wants to perform
112  * recursion.  That is, it wants the full state-machine-list to operate on
113  * a different query.  It calls env.attach_sub() to create a new query state.
114  * The routine returns the newly created state, and potentially the module
115  * can edit the module-states for the newly created query (i.e. pass along
116  * some information, like delegation points).  The module then suspends,
117  * returns from the operate routine.
118  *
119  * The mesh meanwhile will have the newly created query (or queries) on
120  * a waiting list, and will call operate() on this query (or queries).
121  * It starts again at the start of the module list for them.  The query
122  * (or queries) continue to operate their state machines, until they are
123  * done.  When they are done the mesh calls inform_super on the module that
124  * wanted the recursion.  After that the mesh calls operate() on the module
125  * that wanted to do the recursion, and during this phase the module could,
126  * for example, decide to create more recursions.
127  *
128  * If the module decides it no longer wants the recursive information
129  * it can call detach_subs.  Those queries will still run to completion,
130  * potentially filling the cache with information.  Inform_super is not
131  * called any more.
132  *
133  * The iterator module will fetch items from the cache, so a recursion
134  * attempt may complete very quickly if the item is in cache.  The calling
135  * module has to wait for completion or eventual timeout.  A recursive query
136  * that times out returns a servfail rcode (servfail is also returned for
137  * other errors during the lookup).
138  *
139  * Results are passed in the qstate, the rcode member is used to pass
140  * errors without requiring memory allocation, so that the code can continue
141  * in out-of-memory conditions.  If the rcode member is 0 (NOERROR) then
142  * the dns_msg entry contains a filled out message.  This message may
143  * also contain an rcode that is nonzero, but in this case additional
144  * information (query, additional) can be passed along.
145  *
146  * The rcode and dns_msg are used to pass the result from the the rightmost
147  * module towards the leftmost modules and then towards the user.
148  *
149  * If you want to avoid recursion-cycles where queries need other queries
150  * that need the first one, use detect_cycle() to see if that will happen.
151  *
152  */
153 
154 #ifndef UTIL_MODULE_H
155 #define UTIL_MODULE_H
156 #include "util/storage/lruhash.h"
157 #include "util/data/msgreply.h"
158 #include "util/data/msgparse.h"
159 struct sldns_buffer;
160 struct alloc_cache;
161 struct rrset_cache;
162 struct key_cache;
163 struct config_file;
164 struct slabhash;
165 struct query_info;
166 struct edns_data;
167 struct regional;
168 struct worker;
169 struct module_qstate;
170 struct ub_randstate;
171 struct mesh_area;
172 struct mesh_state;
173 struct val_anchors;
174 struct val_neg_cache;
175 struct iter_forwards;
176 struct iter_hints;
177 
178 /** Maximum number of modules in operation */
179 #define MAX_MODULE 16
180 
181 /** Maximum number of known edns options */
182 #define MAX_KNOWN_EDNS_OPTS 256
183 
184 enum inplace_cb_list_type {
185 	/* Inplace callbacks for when a resolved reply is ready to be sent to the
186 	 * front.*/
187 	inplace_cb_reply = 0,
188 	/* Inplace callbacks for when a reply is given from the cache. */
189 	inplace_cb_reply_cache,
190 	/* Inplace callbacks for when a reply is given with local data
191 	 * (or Chaos reply). */
192 	inplace_cb_reply_local,
193 	/* Inplace callbacks for when the reply is servfail. */
194 	inplace_cb_reply_servfail,
195 	/* Inplace callbacks for when a query is ready to be sent to the back.*/
196 	inplace_cb_query,
197 	/* Total number of types. Used for array initialization.
198 	 * Should always be last. */
199 	inplace_cb_types_total
200 };
201 
202 
203 /** Known edns option. Can be populated during modules' init. */
204 struct edns_known_option {
205 	/** type of this edns option */
206 	uint16_t opt_code;
207 	/** whether the option needs to bypass the cache stage */
208 	int bypass_cache_stage;
209 	/** whether the option needs mesh aggregation */
210 	int no_aggregation;
211 };
212 
213 /**
214  * Inplace callback function called before replying.
215  * Called as func(edns, qstate, opt_list_out, qinfo, reply_info, rcode,
216  *                region, python_callback)
217  * Where:
218  *	qinfo: the query info.
219  *	qstate: the module state. NULL when calling before the query reaches the
220  *		mesh states.
221  *	rep: reply_info. Could be NULL.
222  *	rcode: the return code.
223  *	edns: the edns_data of the reply. When qstate is NULL, it is also used as
224  *		the edns input.
225  *	opt_list_out: the edns options list for the reply.
226  *	region: region to store data.
227  *	python_callback: only used for registering a python callback function.
228  */
229 typedef int inplace_cb_reply_func_type(struct query_info* qinfo,
230 	struct module_qstate* qstate, struct reply_info* rep, int rcode,
231 	struct edns_data* edns, struct edns_option** opt_list_out,
232 	struct regional* region, void* python_callback);
233 
234 /**
235  * Inplace callback list of registered routines to be called before replying
236  * with a resolved query.
237  */
238 struct inplace_cb_reply {
239 	/** next in list */
240 	struct inplace_cb_reply* next;
241 	/**
242 	 * Inplace callback routine for cache stage response.
243 	 * called as cb(qinfo, qstate, qinfo, reply_info, rcode, edns,
244 	 *              opt_list_out, region, python_callback);
245 	 * python_callback is only used for registering a python callback function.
246 	 */
247 	inplace_cb_reply_func_type* cb;
248 	void* cb_arg;
249 };
250 
251 /**
252  * Inplace callback function called before sending the query to a nameserver.
253  * Called as func(qinfo, flags, qstate, addr, addrlen, zone, zonelen, region,
254  *                python_callback)
255  * Where:
256  *	qinfo: query info.
257  *	flags: flags of the query.
258  *	qstate: query state.
259  *	addr: to which server to send the query.
260  *	addrlen: length of addr.
261  *	zone: name of the zone of the delegation point. wireformat dname.
262  *		This is the delegation point name for which the server is deemed
263  *		authoritative.
264  *	zonelen: length of zone.
265  *	region: region to store data.
266  *	python_callback: only used for registering a python callback function.
267  */
268 typedef int inplace_cb_query_func_type(struct query_info* qinfo, uint16_t flags,
269 	struct module_qstate* qstate, struct sockaddr_storage* addr,
270 	socklen_t addrlen, uint8_t* zone, size_t zonelen, struct regional* region,
271 	void* python_callback);
272 
273 /**
274  * Inplace callback list of registered routines to be called before quering a
275  * nameserver.
276  */
277 struct inplace_cb_query {
278 	/** next in list */
279 	struct inplace_cb_query* next;
280 	/**
281 	 * Inplace callback routine for cache stage response.
282 	 * called as cb(qinfo, flags, qstate, addr, addrlen, zone, zonelen,
283 	 *              region, python_callback);
284 	 * python_callback is only used for registering a python callback function.
285 	 */
286 	inplace_cb_query_func_type* cb;
287 	void* cb_arg;
288 };
289 
290 /**
291  * Module environment.
292  * Services and data provided to the module.
293  */
294 struct module_env {
295 	/* --- data --- */
296 	/** config file with config options */
297 	struct config_file* cfg;
298 	/** shared message cache */
299 	struct slabhash* msg_cache;
300 	/** shared rrset cache */
301 	struct rrset_cache* rrset_cache;
302 	/** shared infrastructure cache (edns, lameness) */
303 	struct infra_cache* infra_cache;
304 	/** shared key cache */
305 	struct key_cache* key_cache;
306 
307 	/* --- services --- */
308 	/**
309 	 * Send serviced DNS query to server. UDP/TCP and EDNS is handled.
310 	 * operate() should return with wait_reply. Later on a callback
311 	 * will cause operate() to be called with event timeout or reply.
312 	 * The time until a timeout is calculated from roundtrip timing,
313 	 * several UDP retries are attempted.
314 	 * @param qinfo: query info.
315 	 * @param flags: host order flags word, with opcode and CD bit.
316 	 * @param dnssec: if set, EDNS record will have bits set.
317 	 *	If EDNS_DO bit is set, DO bit is set in EDNS records.
318 	 *	If BIT_CD is set, CD bit is set in queries with EDNS records.
319 	 * @param want_dnssec: if set, the validator wants DNSSEC.  Without
320 	 * 	EDNS, the answer is likely to be useless for this domain.
321 	 * @param nocaps: do not use caps_for_id, use the qname as given.
322 	 *	(ignored if caps_for_id is disabled).
323 	 * @param addr: where to.
324 	 * @param addrlen: length of addr.
325 	 * @param zone: delegation point name.
326 	 * @param zonelen: length of zone name.
327 	 * @param ssl_upstream: use SSL for upstream queries.
328 	 * @param q: wich query state to reactivate upon return.
329 	 * @return: false on failure (memory or socket related). no query was
330 	 *	sent. Or returns an outbound entry with qsent and qstate set.
331 	 *	This outbound_entry will be used on later module invocations
332 	 *	that involve this query (timeout, error or reply).
333 	 */
334 	struct outbound_entry* (*send_query)(struct query_info* qinfo,
335 		uint16_t flags, int dnssec, int want_dnssec, int nocaps,
336 		struct sockaddr_storage* addr, socklen_t addrlen,
337 		uint8_t* zone, size_t zonelen, int ssl_upstream,
338 		struct module_qstate* q);
339 
340 	/**
341 	 * Detach-subqueries.
342 	 * Remove all sub-query references from this query state.
343 	 * Keeps super-references of those sub-queries correct.
344 	 * Updates stat items in mesh_area structure.
345 	 * @param qstate: used to find mesh state.
346 	 */
347 	void (*detach_subs)(struct module_qstate* qstate);
348 
349 	/**
350 	 * Attach subquery.
351 	 * Creates it if it does not exist already.
352 	 * Keeps sub and super references correct.
353 	 * Updates stat items in mesh_area structure.
354 	 * Pass if it is priming query or not.
355 	 * return:
356 	 * o if error (malloc) happened.
357 	 * o need to initialise the new state (module init; it is a new state).
358 	 *   so that the next run of the query with this module is successful.
359 	 * o no init needed, attachment successful.
360 	 *
361 	 * @param qstate: the state to find mesh state, and that wants to
362 	 * 	receive the results from the new subquery.
363 	 * @param qinfo: what to query for (copied).
364 	 * @param qflags: what flags to use (RD, CD flag or not).
365 	 * @param prime: if it is a (stub) priming query.
366 	 * @param valrec: validation lookup recursion, does not need validation
367 	 * @param newq: If the new subquery needs initialisation, it is
368 	 * 	returned, otherwise NULL is returned.
369 	 * @return: false on error, true if success (and init may be needed).
370 	 */
371 	int (*attach_sub)(struct module_qstate* qstate,
372 		struct query_info* qinfo, uint16_t qflags, int prime,
373 		int valrec, struct module_qstate** newq);
374 
375 	/**
376 	 * Kill newly attached sub. If attach_sub returns newq for
377 	 * initialisation, but that fails, then this routine will cleanup and
378 	 * delete the fresly created sub.
379 	 * @param newq: the new subquery that is no longer needed.
380 	 * 	It is removed.
381 	 */
382 	void (*kill_sub)(struct module_qstate* newq);
383 
384 	/**
385 	 * Detect if adding a dependency for qstate on name,type,class will
386 	 * create a dependency cycle.
387 	 * @param qstate: given mesh querystate.
388 	 * @param qinfo: query info for dependency.
389 	 * @param flags: query flags of dependency, RD/CD flags.
390 	 * @param prime: if dependency is a priming query or not.
391 	 * @param valrec: validation lookup recursion, does not need validation
392 	 * @return true if the name,type,class exists and the given
393 	 * 	qstate mesh exists as a dependency of that name. Thus
394 	 * 	if qstate becomes dependent on name,type,class then a
395 	 * 	cycle is created.
396 	 */
397 	int (*detect_cycle)(struct module_qstate* qstate,
398 		struct query_info* qinfo, uint16_t flags, int prime,
399 		int valrec);
400 
401 	/** region for temporary usage. May be cleared after operate() call. */
402 	struct regional* scratch;
403 	/** buffer for temporary usage. May be cleared after operate() call. */
404 	struct sldns_buffer* scratch_buffer;
405 	/** internal data for daemon - worker thread. */
406 	struct worker* worker;
407 	/** mesh area with query state dependencies */
408 	struct mesh_area* mesh;
409 	/** allocation service */
410 	struct alloc_cache* alloc;
411 	/** random table to generate random numbers */
412 	struct ub_randstate* rnd;
413 	/** time in seconds, converted to integer */
414 	time_t* now;
415 	/** time in microseconds. Relatively recent. */
416 	struct timeval* now_tv;
417 	/** is validation required for messages, controls client-facing
418 	 * validation status (AD bits) and servfails */
419 	int need_to_validate;
420 	/** trusted key storage; these are the configured keys, if not NULL,
421 	 * otherwise configured by validator. These are the trust anchors,
422 	 * and are not primed and ready for validation, but on the bright
423 	 * side, they are read only memory, thus no locks and fast. */
424 	struct val_anchors* anchors;
425 	/** negative cache, configured by the validator. if not NULL,
426 	 * contains NSEC record lookup trees. */
427 	struct val_neg_cache* neg_cache;
428 	/** the 5011-probe timer (if any) */
429 	struct comm_timer* probe_timer;
430 	/** Mapping of forwarding zones to targets.
431 	 * iterator forwarder information. per-thread, created by worker */
432 	struct iter_forwards* fwds;
433 	/**
434 	 * iterator forwarder information. per-thread, created by worker.
435 	 * The hints -- these aren't stored in the cache because they don't
436 	 * expire. The hints are always used to "prime" the cache. Note
437 	 * that both root hints and stub zone "hints" are stored in this
438 	 * data structure.
439 	 */
440 	struct iter_hints* hints;
441 	/** module specific data. indexed by module id. */
442 	void* modinfo[MAX_MODULE];
443 
444 	/* Shared linked list of inplace callback functions */
445 	void* inplace_cb_lists[inplace_cb_types_total];
446 
447 	/**
448 	 * Shared array of known edns options (size MAX_KNOWN_EDNS_OPTS).
449 	 * Filled by edns literate modules during init.
450 	 */
451 	struct edns_known_option* edns_known_options;
452 	/* Number of known edns options */
453 	size_t edns_known_options_num;
454 };
455 
456 /**
457  * External visible states of the module state machine
458  * Modules may also have an internal state.
459  * Modules are supposed to run to completion or until blocked.
460  */
461 enum module_ext_state {
462 	/** initial state - new query */
463 	module_state_initial = 0,
464 	/** waiting for reply to outgoing network query */
465 	module_wait_reply,
466 	/** module is waiting for another module */
467 	module_wait_module,
468 	/** module is waiting for another module; that other is restarted */
469 	module_restart_next,
470 	/** module is waiting for sub-query */
471 	module_wait_subquery,
472 	/** module could not finish the query */
473 	module_error,
474 	/** module is finished with query */
475 	module_finished
476 };
477 
478 /**
479  * Events that happen to modules, that start or wakeup modules.
480  */
481 enum module_ev {
482 	/** new query */
483 	module_event_new = 0,
484 	/** query passed by other module */
485 	module_event_pass,
486 	/** reply inbound from server */
487 	module_event_reply,
488 	/** no reply, timeout or other error */
489 	module_event_noreply,
490 	/** reply is there, but capitalisation check failed */
491 	module_event_capsfail,
492 	/** next module is done, and its reply is awaiting you */
493 	module_event_moddone,
494 	/** error */
495 	module_event_error
496 };
497 
498 /**
499  * Linked list of sockaddrs
500  * May be allocated such that only 'len' bytes of addr exist for the structure.
501  */
502 struct sock_list {
503 	/** next in list */
504 	struct sock_list* next;
505 	/** length of addr */
506 	socklen_t len;
507 	/** sockaddr */
508 	struct sockaddr_storage addr;
509 };
510 
511 /**
512  * Module state, per query.
513  */
514 struct module_qstate {
515 	/** which query is being answered: name, type, class */
516 	struct query_info qinfo;
517 	/** flags uint16 from query */
518 	uint16_t query_flags;
519 	/** if this is a (stub or root) priming query (with hints) */
520 	int is_priming;
521 	/** if this is a validation recursion query that does not get
522 	 * validation itself */
523 	int is_valrec;
524 
525 	/** comm_reply contains server replies */
526 	struct comm_reply* reply;
527 	/** the reply message, with message for client and calling module */
528 	struct dns_msg* return_msg;
529 	/** the rcode, in case of error, instead of a reply message */
530 	int return_rcode;
531 	/** origin of the reply (can be NULL from cache, list for cnames) */
532 	struct sock_list* reply_origin;
533 	/** IP blacklist for queries */
534 	struct sock_list* blacklist;
535 	/** region for this query. Cleared when query process finishes. */
536 	struct regional* region;
537 	/** failure reason information if val-log-level is high */
538 	struct config_strlist* errinf;
539 
540 	/** which module is executing */
541 	int curmod;
542 	/** module states */
543 	enum module_ext_state ext_state[MAX_MODULE];
544 	/** module specific data for query. indexed by module id. */
545 	void* minfo[MAX_MODULE];
546 	/** environment for this query */
547 	struct module_env* env;
548 	/** mesh related information for this query */
549 	struct mesh_state* mesh_info;
550 	/** how many seconds before expiry is this prefetched (0 if not) */
551 	time_t prefetch_leeway;
552 
553 	/** incoming edns options from the front end */
554 	struct edns_option* edns_opts_front_in;
555 	/** outgoing edns options to the back end */
556 	struct edns_option* edns_opts_back_out;
557 	/** incoming edns options from the back end */
558 	struct edns_option* edns_opts_back_in;
559 	/** outgoing edns options to the front end */
560 	struct edns_option* edns_opts_front_out;
561 	/** whether modules should answer from the cache */
562 	int no_cache_lookup;
563 	/** whether modules should store answer in the cache */
564 	int no_cache_store;
565 };
566 
567 /**
568  * Module functionality block
569  */
570 struct module_func_block {
571 	/** text string name of module */
572 	const char* name;
573 
574 	/**
575 	 * init the module. Called once for the global state.
576 	 * This is the place to apply settings from the config file.
577 	 * @param env: module environment.
578 	 * @param id: module id number.
579 	 * return: 0 on error
580 	 */
581 	int (*init)(struct module_env* env, int id);
582 
583 	/**
584 	 * de-init, delete, the module. Called once for the global state.
585 	 * @param env: module environment.
586 	 * @param id: module id number.
587 	 */
588 	void (*deinit)(struct module_env* env, int id);
589 
590 	/**
591 	 * accept a new query, or work further on existing query.
592 	 * Changes the qstate->ext_state to be correct on exit.
593 	 * @param ev: event that causes the module state machine to
594 	 *	(re-)activate.
595 	 * @param qstate: the query state.
596 	 *	Note that this method is not allowed to change the
597 	 *	query state 'identity', that is query info, qflags,
598 	 *	and priming status.
599 	 *	Attach a subquery to get results to a different query.
600 	 * @param id: module id number that operate() is called on.
601 	 * @param outbound: if not NULL this event is due to the reply/timeout
602 	 *	or error on this outbound query.
603 	 * @return: if at exit the ext_state is:
604 	 *	o wait_module: next module is started. (with pass event).
605 	 *	o error or finished: previous module is resumed.
606 	 *	o otherwise it waits until that event happens (assumes
607 	 *	  the service routine to make subrequest or send message
608 	 *	  have been called.
609 	 */
610 	void (*operate)(struct module_qstate* qstate, enum module_ev event,
611 		int id, struct outbound_entry* outbound);
612 
613 	/**
614 	 * inform super querystate about the results from this subquerystate.
615 	 * Is called when the querystate is finished.  The method invoked is
616 	 * the one from the current module active in the super querystate.
617 	 * @param qstate: the query state that is finished.
618 	 *	Examine return_rcode and return_reply in the qstate.
619 	 * @param id: module id for this module.
620 	 *	This coincides with the current module for the super qstate.
621 	 * @param super: the super querystate that needs to be informed.
622 	 */
623 	void (*inform_super)(struct module_qstate* qstate, int id,
624 		struct module_qstate* super);
625 
626 	/**
627 	 * clear module specific data
628 	 */
629 	void (*clear)(struct module_qstate* qstate, int id);
630 
631 	/**
632 	 * How much memory is the module specific data using.
633 	 * @param env: module environment.
634 	 * @param id: the module id.
635 	 * @return the number of bytes that are alloced.
636 	 */
637 	size_t (*get_mem)(struct module_env* env, int id);
638 };
639 
640 /**
641  * Debug utility: module external qstate to string
642  * @param s: the state value.
643  * @return descriptive string.
644  */
645 const char* strextstate(enum module_ext_state s);
646 
647 /**
648  * Debug utility: module event to string
649  * @param e: the module event value.
650  * @return descriptive string.
651  */
652 const char* strmodulevent(enum module_ev e);
653 
654 /**
655  * Initialize the edns known options by allocating the required space.
656  * @param env: the module environment.
657  * @return false on failure (no memory).
658  */
659 int edns_known_options_init(struct module_env* env);
660 
661 /**
662  * Free the allocated space for the known edns options.
663  * @param env: the module environment.
664  */
665 void edns_known_options_delete(struct module_env* env);
666 
667 /**
668  * Register a known edns option. Overwrite the flags if it is already
669  * registered. Used before creating workers to register known edns options.
670  * @param opt_code: the edns option code.
671  * @param bypass_cache_stage: whether the option interacts with the cache.
672  * @param no_aggregation: whether the option implies more specific
673  *	aggregation.
674  * @param env: the module environment.
675  * @return true on success, false on failure (registering more options than
676  *	allowed or trying to register after the environment is copied to the
677  *	threads.)
678  */
679 int edns_register_option(uint16_t opt_code, int bypass_cache_stage,
680 	int no_aggregation, struct module_env* env);
681 
682 /**
683  * Register an inplace callback function called before replying with a resolved
684  * query.
685  * @param cb: pointer to the callback function.
686  * @param cb_arg: optional argument for the callback function.
687  * @param env: the module environment.
688  * @return true on success, false on failure (out of memory or trying to
689  *	register after the environment is copied to the threads.)
690  */
691 int inplace_cb_reply_register(inplace_cb_reply_func_type* cb, void* cb_arg,
692 	struct module_env* env);
693 
694 /**
695  * Register an inplace callback function called before replying from the cache.
696  * @param cb: pointer to the callback function.
697  * @param cb_arg: optional argument for the callback function.
698  * @param env: the module environment.
699  * @return true on success, false on failure (out of memory or trying to
700  *	register after the environment is copied to the threads.)
701  */
702 int inplace_cb_reply_cache_register(inplace_cb_reply_func_type* cb, void* cb_arg,
703 	struct module_env* env);
704 
705 /**
706  * Register an inplace callback function called before replying with local
707  * data or Chaos reply.
708  * @param cb: pointer to the callback function.
709  * @param cb_arg: optional argument for the callback function.
710  * @param env: the module environment.
711  * @return true on success, false on failure (out of memory or trying to
712  *	register after the environment is copied to the threads.)
713  */
714 int inplace_cb_reply_local_register(inplace_cb_reply_func_type* cb, void* cb_arg,
715 	struct module_env* env);
716 
717 /**
718  * Register an inplace callback function called before replying with servfail.
719  * @param cb: pointer to the callback function.
720  * @param cb_arg: optional argument for the callback function.
721  * @param env: the module environment.
722  * @return true on success, false on failure (out of memory or trying to
723  *	register after the environment is copied to the threads.)
724  */
725 int inplace_cb_reply_servfail_register(inplace_cb_reply_func_type* cb,
726 	void* cb_arg, struct module_env* env);
727 
728 /**
729  * Delete the inplace_cb_reply callback linked list.
730  * @param env: the module environment.
731  */
732 void inplace_cb_reply_delete(struct module_env* env);
733 
734 /**
735  * Delete the inplace_cb_reply_cache callback linked list.
736  * @param env: the module environment.
737  */
738 void inplace_cb_reply_cache_delete(struct module_env* env);
739 
740 /**
741  * Delete the inplace_cb_reply_servfail callback linked list.
742  * @param env: the module environment.
743  */
744 void inplace_cb_reply_servfail_delete(struct module_env* env);
745 
746 /**
747  * Register an inplace callback function called before quering a nameserver.
748  * @param cb: pointer to the callback function.
749  * @param cb_arg: optional argument for the callback function.
750  * @param env: the module environment.
751  * @return true on success, false on failure (out of memory or trying to
752  *	register after the environment is copied to the threads.)
753  */
754 int inplace_cb_query_register(inplace_cb_query_func_type* cb, void* cb_arg,
755 	struct module_env* env);
756 
757 /**
758  * Delete the inplace_cb_query callback linked list.
759  * @param env: the module environment.
760  */
761 void inplace_cb_query_delete(struct module_env* env);
762 
763 /**
764  * Delete all the inplace callback linked lists.
765  * @param env: the module environment.
766  */
767 void inplace_cb_lists_delete(struct module_env* env);
768 
769 /**
770  * Check if an edns option is known.
771  * @param opt_code: the edns option code.
772  * @param env: the module environment.
773  * @return pointer to registered option if the edns option is known,
774  *	NULL otherwise.
775  */
776 struct edns_known_option* edns_option_is_known(uint16_t opt_code,
777 	struct module_env* env);
778 
779 /**
780  * Check if an edns option needs to bypass the reply from cache stage.
781  * @param list: the edns options.
782  * @param env: the module environment.
783  * @return true if an edns option needs to bypass the cache stage,
784  *	false otherwise.
785  */
786 int edns_bypass_cache_stage(struct edns_option* list,
787 	struct module_env* env);
788 
789 /**
790  * Check if an edns option needs a unique mesh state.
791  * @param list: the edns options.
792  * @param env: the module environment.
793  * @return true if an edns option needs a unique mesh state,
794  *	false otherwise.
795  */
796 int edns_unique_mesh_state(struct edns_option* list, struct module_env* env);
797 
798 /**
799  * Log the known edns options.
800  * @param level: the desired verbosity level.
801  * @param env: the module environment.
802  */
803 void log_edns_known_options(enum verbosity_value level,
804 	struct module_env* env);
805 
806 #endif /* UTIL_MODULE_H */
807