xref: /openbsd-src/usr.sbin/unbound/util/module.h (revision ae3cb403620ab940fbaabb3055fac045a63d56b7)
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 struct respip_set;
178 struct respip_client_info;
179 struct respip_addr_info;
180 
181 /** Maximum number of modules in operation */
182 #define MAX_MODULE 16
183 
184 /** Maximum number of known edns options */
185 #define MAX_KNOWN_EDNS_OPTS 256
186 
187 enum inplace_cb_list_type {
188 	/* Inplace callbacks for when a resolved reply is ready to be sent to the
189 	 * front.*/
190 	inplace_cb_reply = 0,
191 	/* Inplace callbacks for when a reply is given from the cache. */
192 	inplace_cb_reply_cache,
193 	/* Inplace callbacks for when a reply is given with local data
194 	 * (or Chaos reply). */
195 	inplace_cb_reply_local,
196 	/* Inplace callbacks for when the reply is servfail. */
197 	inplace_cb_reply_servfail,
198 	/* Inplace callbacks for when a query is ready to be sent to the back.*/
199 	inplace_cb_query,
200 	/* Inplace callback for when a reply is received from the back. */
201 	inplace_cb_query_response,
202 	/* Inplace callback for when EDNS is parsed on a reply received from the
203 	 * back. */
204 	inplace_cb_edns_back_parsed,
205 	/* Total number of types. Used for array initialization.
206 	 * Should always be last. */
207 	inplace_cb_types_total
208 };
209 
210 
211 /** Known edns option. Can be populated during modules' init. */
212 struct edns_known_option {
213 	/** type of this edns option */
214 	uint16_t opt_code;
215 	/** whether the option needs to bypass the cache stage */
216 	int bypass_cache_stage;
217 	/** whether the option needs mesh aggregation */
218 	int no_aggregation;
219 };
220 
221 /**
222  * Inplace callback list of registered routines to be called.
223  */
224 struct inplace_cb {
225 	/** next in list */
226 	struct inplace_cb* next;
227 	/** Inplace callback routine */
228 	void* cb;
229 	void* cb_arg;
230 	/** module id */
231 	int id;
232 };
233 
234 /**
235  * Inplace callback function called before replying.
236  * Called as func(edns, qstate, opt_list_out, qinfo, reply_info, rcode,
237  *                region, python_callback)
238  * Where:
239  *	qinfo: the query info.
240  *	qstate: the module state. NULL when calling before the query reaches the
241  *		mesh states.
242  *	rep: reply_info. Could be NULL.
243  *	rcode: the return code.
244  *	edns: the edns_data of the reply. When qstate is NULL, it is also used as
245  *		the edns input.
246  *	opt_list_out: the edns options list for the reply.
247  *	region: region to store data.
248  *	python_callback: only used for registering a python callback function.
249  */
250 typedef int inplace_cb_reply_func_type(struct query_info* qinfo,
251 	struct module_qstate* qstate, struct reply_info* rep, int rcode,
252 	struct edns_data* edns, struct edns_option** opt_list_out,
253 	struct regional* region, int id, void* callback);
254 
255 /**
256  * Inplace callback function called before sending the query to a nameserver.
257  * Called as func(qinfo, flags, qstate, addr, addrlen, zone, zonelen, region,
258  *                python_callback)
259  * Where:
260  *	qinfo: query info.
261  *	flags: flags of the query.
262  *	qstate: query state.
263  *	addr: to which server to send the query.
264  *	addrlen: length of addr.
265  *	zone: name of the zone of the delegation point. wireformat dname.
266  *		This is the delegation point name for which the server is deemed
267  *		authoritative.
268  *	zonelen: length of zone.
269  *	region: region to store data.
270  *	python_callback: only used for registering a python callback function.
271  */
272 typedef int inplace_cb_query_func_type(struct query_info* qinfo, uint16_t flags,
273 	struct module_qstate* qstate, struct sockaddr_storage* addr,
274 	socklen_t addrlen, uint8_t* zone, size_t zonelen, struct regional* region,
275 	int id, void* callback);
276 
277 /**
278  * Inplace callback function called after parsing edns on query reply.
279  * Called as func(qstate, cb_args)
280  * Where:
281  *	qstate: the query state
282  *	id: module id
283  *	cb_args: argument passed when registering callback.
284  */
285 typedef int inplace_cb_edns_back_parsed_func_type(struct module_qstate* qstate,
286 	int id, void* cb_args);
287 
288 /**
289  * Inplace callback function called after parsing query response.
290  * Called as func(qstate, id, cb_args)
291  * Where:
292  *	qstate: the query state
293  *	response: query response
294  *	id: module id
295  *	cb_args: argument passed when registering callback.
296  */
297 typedef int inplace_cb_query_response_func_type(struct module_qstate* qstate,
298 	struct dns_msg* response, int id, void* cb_args);
299 
300 /**
301  * Module environment.
302  * Services and data provided to the module.
303  */
304 struct module_env {
305 	/* --- data --- */
306 	/** config file with config options */
307 	struct config_file* cfg;
308 	/** shared message cache */
309 	struct slabhash* msg_cache;
310 	/** shared rrset cache */
311 	struct rrset_cache* rrset_cache;
312 	/** shared infrastructure cache (edns, lameness) */
313 	struct infra_cache* infra_cache;
314 	/** shared key cache */
315 	struct key_cache* key_cache;
316 
317 	/* --- services --- */
318 	/**
319 	 * Send serviced DNS query to server. UDP/TCP and EDNS is handled.
320 	 * operate() should return with wait_reply. Later on a callback
321 	 * will cause operate() to be called with event timeout or reply.
322 	 * The time until a timeout is calculated from roundtrip timing,
323 	 * several UDP retries are attempted.
324 	 * @param qinfo: query info.
325 	 * @param flags: host order flags word, with opcode and CD bit.
326 	 * @param dnssec: if set, EDNS record will have bits set.
327 	 *	If EDNS_DO bit is set, DO bit is set in EDNS records.
328 	 *	If BIT_CD is set, CD bit is set in queries with EDNS records.
329 	 * @param want_dnssec: if set, the validator wants DNSSEC.  Without
330 	 * 	EDNS, the answer is likely to be useless for this domain.
331 	 * @param nocaps: do not use caps_for_id, use the qname as given.
332 	 *	(ignored if caps_for_id is disabled).
333 	 * @param addr: where to.
334 	 * @param addrlen: length of addr.
335 	 * @param zone: delegation point name.
336 	 * @param zonelen: length of zone name.
337 	 * @param ssl_upstream: use SSL for upstream queries.
338 	 * @param q: wich query state to reactivate upon return.
339 	 * @return: false on failure (memory or socket related). no query was
340 	 *	sent. Or returns an outbound entry with qsent and qstate set.
341 	 *	This outbound_entry will be used on later module invocations
342 	 *	that involve this query (timeout, error or reply).
343 	 */
344 	struct outbound_entry* (*send_query)(struct query_info* qinfo,
345 		uint16_t flags, int dnssec, int want_dnssec, int nocaps,
346 		struct sockaddr_storage* addr, socklen_t addrlen,
347 		uint8_t* zone, size_t zonelen, int ssl_upstream,
348 		struct module_qstate* q);
349 
350 	/**
351 	 * Detach-subqueries.
352 	 * Remove all sub-query references from this query state.
353 	 * Keeps super-references of those sub-queries correct.
354 	 * Updates stat items in mesh_area structure.
355 	 * @param qstate: used to find mesh state.
356 	 */
357 	void (*detach_subs)(struct module_qstate* qstate);
358 
359 	/**
360 	 * Attach subquery.
361 	 * Creates it if it does not exist already.
362 	 * Keeps sub and super references correct.
363 	 * Updates stat items in mesh_area structure.
364 	 * Pass if it is priming query or not.
365 	 * return:
366 	 * o if error (malloc) happened.
367 	 * o need to initialise the new state (module init; it is a new state).
368 	 *   so that the next run of the query with this module is successful.
369 	 * o no init needed, attachment successful.
370 	 *
371 	 * @param qstate: the state to find mesh state, and that wants to
372 	 * 	receive the results from the new subquery.
373 	 * @param qinfo: what to query for (copied).
374 	 * @param qflags: what flags to use (RD, CD flag or not).
375 	 * @param prime: if it is a (stub) priming query.
376 	 * @param valrec: validation lookup recursion, does not need validation
377 	 * @param newq: If the new subquery needs initialisation, it is
378 	 * 	returned, otherwise NULL is returned.
379 	 * @return: false on error, true if success (and init may be needed).
380 	 */
381 	int (*attach_sub)(struct module_qstate* qstate,
382 		struct query_info* qinfo, uint16_t qflags, int prime,
383 		int valrec, struct module_qstate** newq);
384 
385 	/**
386 	 * Add detached query.
387 	 * Creates it if it does not exist already.
388 	 * Does not make super/sub references.
389 	 * Performs a cycle detection - for double check - and fails if there is
390 	 * 	one.
391 	 * Updates stat items in mesh_area structure.
392 	 * Pass if it is priming query or not.
393 	 * return:
394 	 * 	o if error (malloc) happened.
395 	 * 	o need to initialise the new state (module init; it is a new state).
396 	 * 	  so that the next run of the query with this module is successful.
397 	 * 	o no init needed, attachment successful.
398 	 * 	o added subquery, created if it did not exist already.
399 	 *
400 	 * @param qstate: the state to find mesh state, and that wants to receive
401 	 * 	the results from the new subquery.
402 	 * @param qinfo: what to query for (copied).
403 	 * @param qflags: what flags to use (RD / CD flag or not).
404 	 * @param prime: if it is a (stub) priming query.
405 	 * @param valrec: if it is a validation recursion query (lookup of key, DS).
406 	 * @param newq: If the new subquery needs initialisation, it is returned,
407 	 * 	otherwise NULL is returned.
408 	 * @param sub: The added mesh state, created if it did not exist already.
409 	 * @return: false on error, true if success (and init may be needed).
410 	 */
411 	int (*add_sub)(struct module_qstate* qstate,
412 		struct query_info* qinfo, uint16_t qflags, int prime,
413 		int valrec, struct module_qstate** newq,
414 		struct mesh_state** sub);
415 
416 	/**
417 	 * Kill newly attached sub. If attach_sub returns newq for
418 	 * initialisation, but that fails, then this routine will cleanup and
419 	 * delete the fresly created sub.
420 	 * @param newq: the new subquery that is no longer needed.
421 	 * 	It is removed.
422 	 */
423 	void (*kill_sub)(struct module_qstate* newq);
424 
425 	/**
426 	 * Detect if adding a dependency for qstate on name,type,class will
427 	 * create a dependency cycle.
428 	 * @param qstate: given mesh querystate.
429 	 * @param qinfo: query info for dependency.
430 	 * @param flags: query flags of dependency, RD/CD flags.
431 	 * @param prime: if dependency is a priming query or not.
432 	 * @param valrec: validation lookup recursion, does not need validation
433 	 * @return true if the name,type,class exists and the given
434 	 * 	qstate mesh exists as a dependency of that name. Thus
435 	 * 	if qstate becomes dependent on name,type,class then a
436 	 * 	cycle is created.
437 	 */
438 	int (*detect_cycle)(struct module_qstate* qstate,
439 		struct query_info* qinfo, uint16_t flags, int prime,
440 		int valrec);
441 
442 	/** region for temporary usage. May be cleared after operate() call. */
443 	struct regional* scratch;
444 	/** buffer for temporary usage. May be cleared after operate() call. */
445 	struct sldns_buffer* scratch_buffer;
446 	/** internal data for daemon - worker thread. */
447 	struct worker* worker;
448 	/** mesh area with query state dependencies */
449 	struct mesh_area* mesh;
450 	/** allocation service */
451 	struct alloc_cache* alloc;
452 	/** random table to generate random numbers */
453 	struct ub_randstate* rnd;
454 	/** time in seconds, converted to integer */
455 	time_t* now;
456 	/** time in microseconds. Relatively recent. */
457 	struct timeval* now_tv;
458 	/** is validation required for messages, controls client-facing
459 	 * validation status (AD bits) and servfails */
460 	int need_to_validate;
461 	/** trusted key storage; these are the configured keys, if not NULL,
462 	 * otherwise configured by validator. These are the trust anchors,
463 	 * and are not primed and ready for validation, but on the bright
464 	 * side, they are read only memory, thus no locks and fast. */
465 	struct val_anchors* anchors;
466 	/** negative cache, configured by the validator. if not NULL,
467 	 * contains NSEC record lookup trees. */
468 	struct val_neg_cache* neg_cache;
469 	/** the 5011-probe timer (if any) */
470 	struct comm_timer* probe_timer;
471 	/** Mapping of forwarding zones to targets.
472 	 * iterator forwarder information. per-thread, created by worker */
473 	struct iter_forwards* fwds;
474 	/**
475 	 * iterator forwarder information. per-thread, created by worker.
476 	 * The hints -- these aren't stored in the cache because they don't
477 	 * expire. The hints are always used to "prime" the cache. Note
478 	 * that both root hints and stub zone "hints" are stored in this
479 	 * data structure.
480 	 */
481 	struct iter_hints* hints;
482 	/** module specific data. indexed by module id. */
483 	void* modinfo[MAX_MODULE];
484 
485 	/* Shared linked list of inplace callback functions */
486 	struct inplace_cb* inplace_cb_lists[inplace_cb_types_total];
487 
488 	/**
489 	 * Shared array of known edns options (size MAX_KNOWN_EDNS_OPTS).
490 	 * Filled by edns literate modules during init.
491 	 */
492 	struct edns_known_option* edns_known_options;
493 	/* Number of known edns options */
494 	size_t edns_known_options_num;
495 
496 	/* Make every mesh state unique, do not aggregate mesh states. */
497 	int unique_mesh;
498 };
499 
500 /**
501  * External visible states of the module state machine
502  * Modules may also have an internal state.
503  * Modules are supposed to run to completion or until blocked.
504  */
505 enum module_ext_state {
506 	/** initial state - new query */
507 	module_state_initial = 0,
508 	/** waiting for reply to outgoing network query */
509 	module_wait_reply,
510 	/** module is waiting for another module */
511 	module_wait_module,
512 	/** module is waiting for another module; that other is restarted */
513 	module_restart_next,
514 	/** module is waiting for sub-query */
515 	module_wait_subquery,
516 	/** module could not finish the query */
517 	module_error,
518 	/** module is finished with query */
519 	module_finished
520 };
521 
522 /**
523  * Events that happen to modules, that start or wakeup modules.
524  */
525 enum module_ev {
526 	/** new query */
527 	module_event_new = 0,
528 	/** query passed by other module */
529 	module_event_pass,
530 	/** reply inbound from server */
531 	module_event_reply,
532 	/** no reply, timeout or other error */
533 	module_event_noreply,
534 	/** reply is there, but capitalisation check failed */
535 	module_event_capsfail,
536 	/** next module is done, and its reply is awaiting you */
537 	module_event_moddone,
538 	/** error */
539 	module_event_error
540 };
541 
542 /**
543  * Linked list of sockaddrs
544  * May be allocated such that only 'len' bytes of addr exist for the structure.
545  */
546 struct sock_list {
547 	/** next in list */
548 	struct sock_list* next;
549 	/** length of addr */
550 	socklen_t len;
551 	/** sockaddr */
552 	struct sockaddr_storage addr;
553 };
554 
555 struct respip_action_info;
556 
557 /**
558  * Module state, per query.
559  */
560 struct module_qstate {
561 	/** which query is being answered: name, type, class */
562 	struct query_info qinfo;
563 	/** flags uint16 from query */
564 	uint16_t query_flags;
565 	/** if this is a (stub or root) priming query (with hints) */
566 	int is_priming;
567 	/** if this is a validation recursion query that does not get
568 	 * validation itself */
569 	int is_valrec;
570 
571 	/** comm_reply contains server replies */
572 	struct comm_reply* reply;
573 	/** the reply message, with message for client and calling module */
574 	struct dns_msg* return_msg;
575 	/** the rcode, in case of error, instead of a reply message */
576 	int return_rcode;
577 	/** origin of the reply (can be NULL from cache, list for cnames) */
578 	struct sock_list* reply_origin;
579 	/** IP blacklist for queries */
580 	struct sock_list* blacklist;
581 	/** region for this query. Cleared when query process finishes. */
582 	struct regional* region;
583 	/** failure reason information if val-log-level is high */
584 	struct config_strlist* errinf;
585 
586 	/** which module is executing */
587 	int curmod;
588 	/** module states */
589 	enum module_ext_state ext_state[MAX_MODULE];
590 	/** module specific data for query. indexed by module id. */
591 	void* minfo[MAX_MODULE];
592 	/** environment for this query */
593 	struct module_env* env;
594 	/** mesh related information for this query */
595 	struct mesh_state* mesh_info;
596 	/** how many seconds before expiry is this prefetched (0 if not) */
597 	time_t prefetch_leeway;
598 
599 	/** incoming edns options from the front end */
600 	struct edns_option* edns_opts_front_in;
601 	/** outgoing edns options to the back end */
602 	struct edns_option* edns_opts_back_out;
603 	/** incoming edns options from the back end */
604 	struct edns_option* edns_opts_back_in;
605 	/** outgoing edns options to the front end */
606 	struct edns_option* edns_opts_front_out;
607 	/** whether modules should answer from the cache */
608 	int no_cache_lookup;
609 	/** whether modules should store answer in the cache */
610 	int no_cache_store;
611 
612 	/**
613 	 * Attributes of clients that share the qstate that may affect IP-based
614 	 * actions.
615 	 */
616 	struct respip_client_info* client_info;
617 
618 	/** Extended result of response-ip action processing, mainly
619 	 *  for logging purposes. */
620 	struct respip_action_info* respip_action_info;
621 
622 	/** whether the reply should be dropped */
623 	int is_drop;
624 };
625 
626 /**
627  * Module functionality block
628  */
629 struct module_func_block {
630 	/** text string name of module */
631 	const char* name;
632 
633 	/**
634 	 * init the module. Called once for the global state.
635 	 * This is the place to apply settings from the config file.
636 	 * @param env: module environment.
637 	 * @param id: module id number.
638 	 * return: 0 on error
639 	 */
640 	int (*init)(struct module_env* env, int id);
641 
642 	/**
643 	 * de-init, delete, the module. Called once for the global state.
644 	 * @param env: module environment.
645 	 * @param id: module id number.
646 	 */
647 	void (*deinit)(struct module_env* env, int id);
648 
649 	/**
650 	 * accept a new query, or work further on existing query.
651 	 * Changes the qstate->ext_state to be correct on exit.
652 	 * @param ev: event that causes the module state machine to
653 	 *	(re-)activate.
654 	 * @param qstate: the query state.
655 	 *	Note that this method is not allowed to change the
656 	 *	query state 'identity', that is query info, qflags,
657 	 *	and priming status.
658 	 *	Attach a subquery to get results to a different query.
659 	 * @param id: module id number that operate() is called on.
660 	 * @param outbound: if not NULL this event is due to the reply/timeout
661 	 *	or error on this outbound query.
662 	 * @return: if at exit the ext_state is:
663 	 *	o wait_module: next module is started. (with pass event).
664 	 *	o error or finished: previous module is resumed.
665 	 *	o otherwise it waits until that event happens (assumes
666 	 *	  the service routine to make subrequest or send message
667 	 *	  have been called.
668 	 */
669 	void (*operate)(struct module_qstate* qstate, enum module_ev event,
670 		int id, struct outbound_entry* outbound);
671 
672 	/**
673 	 * inform super querystate about the results from this subquerystate.
674 	 * Is called when the querystate is finished.  The method invoked is
675 	 * the one from the current module active in the super querystate.
676 	 * @param qstate: the query state that is finished.
677 	 *	Examine return_rcode and return_reply in the qstate.
678 	 * @param id: module id for this module.
679 	 *	This coincides with the current module for the super qstate.
680 	 * @param super: the super querystate that needs to be informed.
681 	 */
682 	void (*inform_super)(struct module_qstate* qstate, int id,
683 		struct module_qstate* super);
684 
685 	/**
686 	 * clear module specific data
687 	 */
688 	void (*clear)(struct module_qstate* qstate, int id);
689 
690 	/**
691 	 * How much memory is the module specific data using.
692 	 * @param env: module environment.
693 	 * @param id: the module id.
694 	 * @return the number of bytes that are alloced.
695 	 */
696 	size_t (*get_mem)(struct module_env* env, int id);
697 };
698 
699 /**
700  * Debug utility: module external qstate to string
701  * @param s: the state value.
702  * @return descriptive string.
703  */
704 const char* strextstate(enum module_ext_state s);
705 
706 /**
707  * Debug utility: module event to string
708  * @param e: the module event value.
709  * @return descriptive string.
710  */
711 const char* strmodulevent(enum module_ev e);
712 
713 /**
714  * Initialize the edns known options by allocating the required space.
715  * @param env: the module environment.
716  * @return false on failure (no memory).
717  */
718 int edns_known_options_init(struct module_env* env);
719 
720 /**
721  * Free the allocated space for the known edns options.
722  * @param env: the module environment.
723  */
724 void edns_known_options_delete(struct module_env* env);
725 
726 /**
727  * Register a known edns option. Overwrite the flags if it is already
728  * registered. Used before creating workers to register known edns options.
729  * @param opt_code: the edns option code.
730  * @param bypass_cache_stage: whether the option interacts with the cache.
731  * @param no_aggregation: whether the option implies more specific
732  *	aggregation.
733  * @param env: the module environment.
734  * @return true on success, false on failure (registering more options than
735  *	allowed or trying to register after the environment is copied to the
736  *	threads.)
737  */
738 int edns_register_option(uint16_t opt_code, int bypass_cache_stage,
739 	int no_aggregation, struct module_env* env);
740 
741 /**
742  * Register an inplace callback function.
743  * @param cb: pointer to the callback function.
744  * @param type: inplace callback type.
745  * @param cbarg: argument for the callback function, or NULL.
746  * @param env: the module environment.
747  * @param id: module id.
748  * @return true on success, false on failure (out of memory or trying to
749  *	register after the environment is copied to the threads.)
750  */
751 int
752 inplace_cb_register(void* cb, enum inplace_cb_list_type type, void* cbarg,
753 	struct module_env* env, int id);
754 
755 /**
756  * Delete callback for specified type and module id.
757  * @param env: the module environment.
758  * @param type: inplace callback type.
759  * @param id: module id.
760  */
761 void
762 inplace_cb_delete(struct module_env* env, enum inplace_cb_list_type type,
763 	int id);
764 
765 /**
766  * Delete all the inplace callback linked lists.
767  * @param env: the module environment.
768  */
769 void inplace_cb_lists_delete(struct module_env* env);
770 
771 /**
772  * Check if an edns option is known.
773  * @param opt_code: the edns option code.
774  * @param env: the module environment.
775  * @return pointer to registered option if the edns option is known,
776  *	NULL otherwise.
777  */
778 struct edns_known_option* edns_option_is_known(uint16_t opt_code,
779 	struct module_env* env);
780 
781 /**
782  * Check if an edns option needs to bypass the reply from cache stage.
783  * @param list: the edns options.
784  * @param env: the module environment.
785  * @return true if an edns option needs to bypass the cache stage,
786  *	false otherwise.
787  */
788 int edns_bypass_cache_stage(struct edns_option* list,
789 	struct module_env* env);
790 
791 /**
792  * Check if an unique mesh state is required. Might be triggered by EDNS option
793  * or set for the complete env.
794  * @param list: the edns options.
795  * @param env: the module environment.
796  * @return true if an edns option needs a unique mesh state,
797  *	false otherwise.
798  */
799 int unique_mesh_state(struct edns_option* list, struct module_env* env);
800 
801 /**
802  * Log the known edns options.
803  * @param level: the desired verbosity level.
804  * @param env: the module environment.
805  */
806 void log_edns_known_options(enum verbosity_value level,
807 	struct module_env* env);
808 
809 #endif /* UTIL_MODULE_H */
810