xref: /freebsd/contrib/unbound/util/module.h (revision 24e365220007c415f495cf8dcb228ece6002b8b7)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * util/module.h - DNS handling module interface
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  *
39b7579f77SDag-Erling Smørgrav  * This file contains the interface for DNS handling modules.
4017d15b25SDag-Erling Smørgrav  *
4117d15b25SDag-Erling Smørgrav  * The module interface uses the DNS modules as state machines.  The
4217d15b25SDag-Erling Smørgrav  * state machines are activated in sequence to operate on queries.  Once
4317d15b25SDag-Erling Smørgrav  * they are done, the reply is passed back.  In the usual setup the mesh
4417d15b25SDag-Erling Smørgrav  * is the caller of the state machines and once things are done sends replies
4517d15b25SDag-Erling Smørgrav  * and invokes result callbacks.
4617d15b25SDag-Erling Smørgrav  *
4717d15b25SDag-Erling Smørgrav  * The module provides a number of functions, listed in the module_func_block.
4817d15b25SDag-Erling Smørgrav  * The module is inited and destroyed and memory usage queries, for the
4917d15b25SDag-Erling Smørgrav  * module as a whole, for entire-module state (such as a cache).  And per-query
5017d15b25SDag-Erling Smørgrav  * functions are called, operate to move the state machine and cleanup of
5117d15b25SDag-Erling Smørgrav  * the per-query state.
5217d15b25SDag-Erling Smørgrav  *
5317d15b25SDag-Erling Smørgrav  * Most per-query state should simply be allocated in the query region.
5417d15b25SDag-Erling Smørgrav  * This is destroyed at the end of the query.
5517d15b25SDag-Erling Smørgrav  *
5617d15b25SDag-Erling Smørgrav  * The module environment contains services and information and caches
5717d15b25SDag-Erling Smørgrav  * shared by the modules and the rest of the system.  It also contains
5817d15b25SDag-Erling Smørgrav  * function pointers for module-specific tasks (like sending queries).
5917d15b25SDag-Erling Smørgrav  *
6017d15b25SDag-Erling Smørgrav  * *** Example module calls for a normal query
6117d15b25SDag-Erling Smørgrav  *
6217d15b25SDag-Erling Smørgrav  * In this example, the query does not need recursion, all the other data
6317d15b25SDag-Erling Smørgrav  * can be found in the cache.  This makes the example shorter.
6417d15b25SDag-Erling Smørgrav  *
6517d15b25SDag-Erling Smørgrav  * At the start of the program the iterator module is initialised.
6617d15b25SDag-Erling Smørgrav  * The iterator module sets up its global state, such as donotquery lists
6717d15b25SDag-Erling Smørgrav  * and private address trees.
6817d15b25SDag-Erling Smørgrav  *
6917d15b25SDag-Erling Smørgrav  * A query comes in, and a mesh entry is created for it.  The mesh
7017d15b25SDag-Erling Smørgrav  * starts the resolution process.  The validator module is the first
7117d15b25SDag-Erling Smørgrav  * in the list of modules, and it is started on this new query.  The
7217d15b25SDag-Erling Smørgrav  * operate() function is called.  The validator decides it needs not do
7317d15b25SDag-Erling Smørgrav  * anything yet until there is a result and returns wait_module, that
7417d15b25SDag-Erling Smørgrav  * causes the next module in the list to be started.
7517d15b25SDag-Erling Smørgrav  *
7617d15b25SDag-Erling Smørgrav  * The next module is the iterator.  It is started on the passed query and
7717d15b25SDag-Erling Smørgrav  * decides to perform a lookup.  For this simple example, the delegation
7817d15b25SDag-Erling Smørgrav  * point information is available, and all the iterator wants to do is
7917d15b25SDag-Erling Smørgrav  * send a UDP query.  The iterator uses env.send_query() to send the
8017d15b25SDag-Erling Smørgrav  * query.  Then the iterator suspends (returns from the operate call).
8117d15b25SDag-Erling Smørgrav  *
8217d15b25SDag-Erling Smørgrav  * When the UDP reply comes back (and on errors and timeouts), the
8317d15b25SDag-Erling Smørgrav  * operate function is called for the query, on the iterator module,
8417d15b25SDag-Erling Smørgrav  * with the event that there is a reply.  The iterator decides that this
8517d15b25SDag-Erling Smørgrav  * is enough, the work is done.  It returns the value finished from the
8617d15b25SDag-Erling Smørgrav  * operate call, which causes the previous module to be started.
8717d15b25SDag-Erling Smørgrav  *
8817d15b25SDag-Erling Smørgrav  * The previous module, the validator module, is started with the event
8917d15b25SDag-Erling Smørgrav  * that the iterator module is done.  The validator decides to validate
9017d15b25SDag-Erling Smørgrav  * the query.  Once it is done (which could take recursive lookups, but
9117d15b25SDag-Erling Smørgrav  * in this example no recursive lookups are needed), it returns from the
9217d15b25SDag-Erling Smørgrav  * operate function with finished.
9317d15b25SDag-Erling Smørgrav  *
9417d15b25SDag-Erling Smørgrav  * There is no previous module from the validator module, and the mesh
9517d15b25SDag-Erling Smørgrav  * takes this to mean that the query is finally done.  The mesh invokes
9617d15b25SDag-Erling Smørgrav  * callbacks and sends packets to queriers.
9717d15b25SDag-Erling Smørgrav  *
9817d15b25SDag-Erling Smørgrav  * If other modules had been waiting (recursively) on the answer to this
9917d15b25SDag-Erling Smørgrav  * query, then the mesh will tell them about it.  It calls the inform_super
10017d15b25SDag-Erling Smørgrav  * routine on all the waiting modules, and once that is done it calls all of
10117d15b25SDag-Erling Smørgrav  * them with the operate() call.  During inform_super the query that is done
10217d15b25SDag-Erling Smørgrav  * still exists and information can be copied from it (but the module should
10317d15b25SDag-Erling Smørgrav  * not really re-entry codepoints and services).  During the operate call
10417d15b25SDag-Erling Smørgrav  * the modules can use stored state to continue operation with the results.
10517d15b25SDag-Erling Smørgrav  * (network buffers are used to contain the answer packet during the
10617d15b25SDag-Erling Smørgrav  * inform_super phase, but after that the network buffers will be cleared
10717d15b25SDag-Erling Smørgrav  * of their contents so that other tasks can be performed).
10817d15b25SDag-Erling Smørgrav  *
10917d15b25SDag-Erling Smørgrav  * *** Example module calls for recursion
11017d15b25SDag-Erling Smørgrav  *
11117d15b25SDag-Erling Smørgrav  * A module is called in operate, and it decides that it wants to perform
11217d15b25SDag-Erling Smørgrav  * recursion.  That is, it wants the full state-machine-list to operate on
11317d15b25SDag-Erling Smørgrav  * a different query.  It calls env.attach_sub() to create a new query state.
11417d15b25SDag-Erling Smørgrav  * The routine returns the newly created state, and potentially the module
11517d15b25SDag-Erling Smørgrav  * can edit the module-states for the newly created query (i.e. pass along
11617d15b25SDag-Erling Smørgrav  * some information, like delegation points).  The module then suspends,
11717d15b25SDag-Erling Smørgrav  * returns from the operate routine.
11817d15b25SDag-Erling Smørgrav  *
11917d15b25SDag-Erling Smørgrav  * The mesh meanwhile will have the newly created query (or queries) on
12017d15b25SDag-Erling Smørgrav  * a waiting list, and will call operate() on this query (or queries).
12117d15b25SDag-Erling Smørgrav  * It starts again at the start of the module list for them.  The query
12217d15b25SDag-Erling Smørgrav  * (or queries) continue to operate their state machines, until they are
12317d15b25SDag-Erling Smørgrav  * done.  When they are done the mesh calls inform_super on the module that
12417d15b25SDag-Erling Smørgrav  * wanted the recursion.  After that the mesh calls operate() on the module
12517d15b25SDag-Erling Smørgrav  * that wanted to do the recursion, and during this phase the module could,
12617d15b25SDag-Erling Smørgrav  * for example, decide to create more recursions.
12717d15b25SDag-Erling Smørgrav  *
12817d15b25SDag-Erling Smørgrav  * If the module decides it no longer wants the recursive information
12917d15b25SDag-Erling Smørgrav  * it can call detach_subs.  Those queries will still run to completion,
13017d15b25SDag-Erling Smørgrav  * potentially filling the cache with information.  Inform_super is not
13117d15b25SDag-Erling Smørgrav  * called any more.
13217d15b25SDag-Erling Smørgrav  *
13317d15b25SDag-Erling Smørgrav  * The iterator module will fetch items from the cache, so a recursion
13417d15b25SDag-Erling Smørgrav  * attempt may complete very quickly if the item is in cache.  The calling
13517d15b25SDag-Erling Smørgrav  * module has to wait for completion or eventual timeout.  A recursive query
13617d15b25SDag-Erling Smørgrav  * that times out returns a servfail rcode (servfail is also returned for
13717d15b25SDag-Erling Smørgrav  * other errors during the lookup).
13817d15b25SDag-Erling Smørgrav  *
13917d15b25SDag-Erling Smørgrav  * Results are passed in the qstate, the rcode member is used to pass
14017d15b25SDag-Erling Smørgrav  * errors without requiring memory allocation, so that the code can continue
14117d15b25SDag-Erling Smørgrav  * in out-of-memory conditions.  If the rcode member is 0 (NOERROR) then
14217d15b25SDag-Erling Smørgrav  * the dns_msg entry contains a filled out message.  This message may
14317d15b25SDag-Erling Smørgrav  * also contain an rcode that is nonzero, but in this case additional
14417d15b25SDag-Erling Smørgrav  * information (query, additional) can be passed along.
14517d15b25SDag-Erling Smørgrav  *
14617d15b25SDag-Erling Smørgrav  * The rcode and dns_msg are used to pass the result from the the rightmost
14717d15b25SDag-Erling Smørgrav  * module towards the leftmost modules and then towards the user.
14817d15b25SDag-Erling Smørgrav  *
14917d15b25SDag-Erling Smørgrav  * If you want to avoid recursion-cycles where queries need other queries
15017d15b25SDag-Erling Smørgrav  * that need the first one, use detect_cycle() to see if that will happen.
15117d15b25SDag-Erling Smørgrav  *
152b7579f77SDag-Erling Smørgrav  */
153b7579f77SDag-Erling Smørgrav 
154b7579f77SDag-Erling Smørgrav #ifndef UTIL_MODULE_H
155b7579f77SDag-Erling Smørgrav #define UTIL_MODULE_H
156b7579f77SDag-Erling Smørgrav #include "util/storage/lruhash.h"
157b7579f77SDag-Erling Smørgrav #include "util/data/msgreply.h"
158b7579f77SDag-Erling Smørgrav #include "util/data/msgparse.h"
15917d15b25SDag-Erling Smørgrav struct sldns_buffer;
160b7579f77SDag-Erling Smørgrav struct alloc_cache;
161b7579f77SDag-Erling Smørgrav struct rrset_cache;
162b7579f77SDag-Erling Smørgrav struct key_cache;
163b7579f77SDag-Erling Smørgrav struct config_file;
164b7579f77SDag-Erling Smørgrav struct slabhash;
165b7579f77SDag-Erling Smørgrav struct query_info;
166b7579f77SDag-Erling Smørgrav struct edns_data;
167b7579f77SDag-Erling Smørgrav struct regional;
168b7579f77SDag-Erling Smørgrav struct worker;
16957bddd21SDag-Erling Smørgrav struct comm_base;
17057bddd21SDag-Erling Smørgrav struct auth_zones;
17157bddd21SDag-Erling Smørgrav struct outside_network;
172b7579f77SDag-Erling Smørgrav struct module_qstate;
173b7579f77SDag-Erling Smørgrav struct ub_randstate;
174b7579f77SDag-Erling Smørgrav struct mesh_area;
175b7579f77SDag-Erling Smørgrav struct mesh_state;
176b7579f77SDag-Erling Smørgrav struct val_anchors;
177b7579f77SDag-Erling Smørgrav struct val_neg_cache;
178b7579f77SDag-Erling Smørgrav struct iter_forwards;
179b7579f77SDag-Erling Smørgrav struct iter_hints;
18065b390aaSDag-Erling Smørgrav struct respip_set;
18165b390aaSDag-Erling Smørgrav struct respip_client_info;
18265b390aaSDag-Erling Smørgrav struct respip_addr_info;
183b7579f77SDag-Erling Smørgrav 
184b7579f77SDag-Erling Smørgrav /** Maximum number of modules in operation */
1853005e0a3SDag-Erling Smørgrav #define MAX_MODULE 16
186b7579f77SDag-Erling Smørgrav 
187bc892140SDag-Erling Smørgrav /** Maximum number of known edns options */
188bc892140SDag-Erling Smørgrav #define MAX_KNOWN_EDNS_OPTS 256
189bc892140SDag-Erling Smørgrav 
190bc892140SDag-Erling Smørgrav enum inplace_cb_list_type {
191bc892140SDag-Erling Smørgrav 	/* Inplace callbacks for when a resolved reply is ready to be sent to the
192bc892140SDag-Erling Smørgrav 	 * front.*/
193bc892140SDag-Erling Smørgrav 	inplace_cb_reply = 0,
194bc892140SDag-Erling Smørgrav 	/* Inplace callbacks for when a reply is given from the cache. */
195bc892140SDag-Erling Smørgrav 	inplace_cb_reply_cache,
196bc892140SDag-Erling Smørgrav 	/* Inplace callbacks for when a reply is given with local data
197bc892140SDag-Erling Smørgrav 	 * (or Chaos reply). */
198bc892140SDag-Erling Smørgrav 	inplace_cb_reply_local,
199bc892140SDag-Erling Smørgrav 	/* Inplace callbacks for when the reply is servfail. */
200bc892140SDag-Erling Smørgrav 	inplace_cb_reply_servfail,
201bc892140SDag-Erling Smørgrav 	/* Inplace callbacks for when a query is ready to be sent to the back.*/
202bc892140SDag-Erling Smørgrav 	inplace_cb_query,
20365b390aaSDag-Erling Smørgrav 	/* Inplace callback for when a reply is received from the back. */
20465b390aaSDag-Erling Smørgrav 	inplace_cb_query_response,
20565b390aaSDag-Erling Smørgrav 	/* Inplace callback for when EDNS is parsed on a reply received from the
20665b390aaSDag-Erling Smørgrav 	 * back. */
20765b390aaSDag-Erling Smørgrav 	inplace_cb_edns_back_parsed,
208bc892140SDag-Erling Smørgrav 	/* Total number of types. Used for array initialization.
209bc892140SDag-Erling Smørgrav 	 * Should always be last. */
210bc892140SDag-Erling Smørgrav 	inplace_cb_types_total
211bc892140SDag-Erling Smørgrav };
212bc892140SDag-Erling Smørgrav 
213bc892140SDag-Erling Smørgrav 
214bc892140SDag-Erling Smørgrav /** Known edns option. Can be populated during modules' init. */
215bc892140SDag-Erling Smørgrav struct edns_known_option {
216bc892140SDag-Erling Smørgrav 	/** type of this edns option */
217bc892140SDag-Erling Smørgrav 	uint16_t opt_code;
218bc892140SDag-Erling Smørgrav 	/** whether the option needs to bypass the cache stage */
219bc892140SDag-Erling Smørgrav 	int bypass_cache_stage;
220bc892140SDag-Erling Smørgrav 	/** whether the option needs mesh aggregation */
221bc892140SDag-Erling Smørgrav 	int no_aggregation;
222bc892140SDag-Erling Smørgrav };
223bc892140SDag-Erling Smørgrav 
224bc892140SDag-Erling Smørgrav /**
22565b390aaSDag-Erling Smørgrav  * Inplace callback list of registered routines to be called.
22665b390aaSDag-Erling Smørgrav  */
22765b390aaSDag-Erling Smørgrav struct inplace_cb {
22865b390aaSDag-Erling Smørgrav 	/** next in list */
22965b390aaSDag-Erling Smørgrav 	struct inplace_cb* next;
23065b390aaSDag-Erling Smørgrav 	/** Inplace callback routine */
23165b390aaSDag-Erling Smørgrav 	void* cb;
23265b390aaSDag-Erling Smørgrav 	void* cb_arg;
23365b390aaSDag-Erling Smørgrav 	/** module id */
23465b390aaSDag-Erling Smørgrav 	int id;
23565b390aaSDag-Erling Smørgrav };
23665b390aaSDag-Erling Smørgrav 
23765b390aaSDag-Erling Smørgrav /**
238bc892140SDag-Erling Smørgrav  * Inplace callback function called before replying.
2394c75e3aaSDag-Erling Smørgrav  * Called as func(qinfo, qstate, rep, rcode, edns, opt_list_out, repinfo,
2404c75e3aaSDag-Erling Smørgrav  *                region, id, python_callback)
241bc892140SDag-Erling Smørgrav  * Where:
242bc892140SDag-Erling Smørgrav  *	qinfo: the query info.
243bc892140SDag-Erling Smørgrav  *	qstate: the module state. NULL when calling before the query reaches the
244bc892140SDag-Erling Smørgrav  *		mesh states.
245bc892140SDag-Erling Smørgrav  *	rep: reply_info. Could be NULL.
246bc892140SDag-Erling Smørgrav  *	rcode: the return code.
247bc892140SDag-Erling Smørgrav  *	edns: the edns_data of the reply. When qstate is NULL, it is also used as
248bc892140SDag-Erling Smørgrav  *		the edns input.
249bc892140SDag-Erling Smørgrav  *	opt_list_out: the edns options list for the reply.
2504c75e3aaSDag-Erling Smørgrav  *	repinfo: reply information for a communication point. NULL when calling
2514c75e3aaSDag-Erling Smørgrav  *		during the mesh states; the same could be found from
2524c75e3aaSDag-Erling Smørgrav  *		qstate->mesh_info->reply_list.
253bc892140SDag-Erling Smørgrav  *	region: region to store data.
2544c75e3aaSDag-Erling Smørgrav  *	id: module id.
255bc892140SDag-Erling Smørgrav  *	python_callback: only used for registering a python callback function.
256bc892140SDag-Erling Smørgrav  */
2573005e0a3SDag-Erling Smørgrav typedef int inplace_cb_reply_func_type(struct query_info* qinfo,
258bc892140SDag-Erling Smørgrav 	struct module_qstate* qstate, struct reply_info* rep, int rcode,
259bc892140SDag-Erling Smørgrav 	struct edns_data* edns, struct edns_option** opt_list_out,
260f44e67d1SCy Schubert 	struct comm_reply* repinfo, struct regional* region,
261f44e67d1SCy Schubert 	struct timeval* start_time, int id, void* callback);
262bc892140SDag-Erling Smørgrav 
263bc892140SDag-Erling Smørgrav /**
264bc892140SDag-Erling Smørgrav  * Inplace callback function called before sending the query to a nameserver.
265bc892140SDag-Erling Smørgrav  * Called as func(qinfo, flags, qstate, addr, addrlen, zone, zonelen, region,
2664c75e3aaSDag-Erling Smørgrav  *                id, python_callback)
267bc892140SDag-Erling Smørgrav  * Where:
268bc892140SDag-Erling Smørgrav  *	qinfo: query info.
269bc892140SDag-Erling Smørgrav  *	flags: flags of the query.
270bc892140SDag-Erling Smørgrav  *	qstate: query state.
271bc892140SDag-Erling Smørgrav  *	addr: to which server to send the query.
272bc892140SDag-Erling Smørgrav  *	addrlen: length of addr.
273bc892140SDag-Erling Smørgrav  *	zone: name of the zone of the delegation point. wireformat dname.
274bc892140SDag-Erling Smørgrav  *		This is the delegation point name for which the server is deemed
275bc892140SDag-Erling Smørgrav  *		authoritative.
276bc892140SDag-Erling Smørgrav  *	zonelen: length of zone.
277bc892140SDag-Erling Smørgrav  *	region: region to store data.
2784c75e3aaSDag-Erling Smørgrav  *	id: module id.
279bc892140SDag-Erling Smørgrav  *	python_callback: only used for registering a python callback function.
280bc892140SDag-Erling Smørgrav  */
2813005e0a3SDag-Erling Smørgrav typedef int inplace_cb_query_func_type(struct query_info* qinfo, uint16_t flags,
282bc892140SDag-Erling Smørgrav 	struct module_qstate* qstate, struct sockaddr_storage* addr,
283bc892140SDag-Erling Smørgrav 	socklen_t addrlen, uint8_t* zone, size_t zonelen, struct regional* region,
28465b390aaSDag-Erling Smørgrav 	int id, void* callback);
285bc892140SDag-Erling Smørgrav 
286bc892140SDag-Erling Smørgrav /**
28765b390aaSDag-Erling Smørgrav  * Inplace callback function called after parsing edns on query reply.
2884c75e3aaSDag-Erling Smørgrav  * Called as func(qstate, id, cb_args)
28965b390aaSDag-Erling Smørgrav  * Where:
2904c75e3aaSDag-Erling Smørgrav  *	qstate: the query state.
2914c75e3aaSDag-Erling Smørgrav  *	id: module id.
29265b390aaSDag-Erling Smørgrav  *	cb_args: argument passed when registering callback.
293bc892140SDag-Erling Smørgrav  */
29465b390aaSDag-Erling Smørgrav typedef int inplace_cb_edns_back_parsed_func_type(struct module_qstate* qstate,
29565b390aaSDag-Erling Smørgrav 	int id, void* cb_args);
29665b390aaSDag-Erling Smørgrav 
297bc892140SDag-Erling Smørgrav /**
29865b390aaSDag-Erling Smørgrav  * Inplace callback function called after parsing query response.
2994c75e3aaSDag-Erling Smørgrav  * Called as func(qstate, response, id, cb_args)
30065b390aaSDag-Erling Smørgrav  * Where:
3014c75e3aaSDag-Erling Smørgrav  *	qstate: the query state.
3024c75e3aaSDag-Erling Smørgrav  *	response: query response.
3034c75e3aaSDag-Erling Smørgrav  *	id: module id.
30465b390aaSDag-Erling Smørgrav  *	cb_args: argument passed when registering callback.
305bc892140SDag-Erling Smørgrav  */
30665b390aaSDag-Erling Smørgrav typedef int inplace_cb_query_response_func_type(struct module_qstate* qstate,
30765b390aaSDag-Erling Smørgrav 	struct dns_msg* response, int id, void* cb_args);
308bc892140SDag-Erling Smørgrav 
309b7579f77SDag-Erling Smørgrav /**
310091e9e46SCy Schubert  * Function called when looking for (expired) cached answers during the serve
311091e9e46SCy Schubert  * expired logic.
312091e9e46SCy Schubert  * Called as func(qstate, lookup_qinfo)
313091e9e46SCy Schubert  * Where:
314091e9e46SCy Schubert  *	qstate: the query state.
315091e9e46SCy Schubert  *	lookup_qinfo: the qinfo to lookup for.
316091e9e46SCy Schubert  */
317091e9e46SCy Schubert typedef struct dns_msg* serve_expired_lookup_func_type(
318091e9e46SCy Schubert 	struct module_qstate* qstate, struct query_info* lookup_qinfo);
319091e9e46SCy Schubert 
320091e9e46SCy Schubert /**
321b7579f77SDag-Erling Smørgrav  * Module environment.
322b7579f77SDag-Erling Smørgrav  * Services and data provided to the module.
323b7579f77SDag-Erling Smørgrav  */
324b7579f77SDag-Erling Smørgrav struct module_env {
325b7579f77SDag-Erling Smørgrav 	/* --- data --- */
326b7579f77SDag-Erling Smørgrav 	/** config file with config options */
327b7579f77SDag-Erling Smørgrav 	struct config_file* cfg;
328b7579f77SDag-Erling Smørgrav 	/** shared message cache */
329b7579f77SDag-Erling Smørgrav 	struct slabhash* msg_cache;
330b7579f77SDag-Erling Smørgrav 	/** shared rrset cache */
331b7579f77SDag-Erling Smørgrav 	struct rrset_cache* rrset_cache;
332b7579f77SDag-Erling Smørgrav 	/** shared infrastructure cache (edns, lameness) */
333b7579f77SDag-Erling Smørgrav 	struct infra_cache* infra_cache;
334b7579f77SDag-Erling Smørgrav 	/** shared key cache */
335b7579f77SDag-Erling Smørgrav 	struct key_cache* key_cache;
336b7579f77SDag-Erling Smørgrav 
337b7579f77SDag-Erling Smørgrav 	/* --- services --- */
338b7579f77SDag-Erling Smørgrav 	/**
339b7579f77SDag-Erling Smørgrav 	 * Send serviced DNS query to server. UDP/TCP and EDNS is handled.
340b7579f77SDag-Erling Smørgrav 	 * operate() should return with wait_reply. Later on a callback
341b7579f77SDag-Erling Smørgrav 	 * will cause operate() to be called with event timeout or reply.
342b7579f77SDag-Erling Smørgrav 	 * The time until a timeout is calculated from roundtrip timing,
343b7579f77SDag-Erling Smørgrav 	 * several UDP retries are attempted.
344bc892140SDag-Erling Smørgrav 	 * @param qinfo: query info.
345b7579f77SDag-Erling Smørgrav 	 * @param flags: host order flags word, with opcode and CD bit.
346b7579f77SDag-Erling Smørgrav 	 * @param dnssec: if set, EDNS record will have bits set.
347b7579f77SDag-Erling Smørgrav 	 *	If EDNS_DO bit is set, DO bit is set in EDNS records.
348b7579f77SDag-Erling Smørgrav 	 *	If BIT_CD is set, CD bit is set in queries with EDNS records.
349b7579f77SDag-Erling Smørgrav 	 * @param want_dnssec: if set, the validator wants DNSSEC.  Without
350b7579f77SDag-Erling Smørgrav 	 * 	EDNS, the answer is likely to be useless for this domain.
351ff825849SDag-Erling Smørgrav 	 * @param nocaps: do not use caps_for_id, use the qname as given.
352ff825849SDag-Erling Smørgrav 	 *	(ignored if caps_for_id is disabled).
353b7579f77SDag-Erling Smørgrav 	 * @param addr: where to.
354b7579f77SDag-Erling Smørgrav 	 * @param addrlen: length of addr.
355b7579f77SDag-Erling Smørgrav 	 * @param zone: delegation point name.
356b7579f77SDag-Erling Smørgrav 	 * @param zonelen: length of zone name.
357*24e36522SCy Schubert 	 * @param tcp_upstream: use TCP for upstream queries.
358bc892140SDag-Erling Smørgrav 	 * @param ssl_upstream: use SSL for upstream queries.
3590fb34990SDag-Erling Smørgrav 	 * @param tls_auth_name: if ssl_upstream, use this name with TLS
3600fb34990SDag-Erling Smørgrav 	 * 	authentication.
361*24e36522SCy Schubert 	 * @param q: which query state to reactivate upon return.
362b7579f77SDag-Erling Smørgrav 	 * @return: false on failure (memory or socket related). no query was
363b7579f77SDag-Erling Smørgrav 	 *	sent. Or returns an outbound entry with qsent and qstate set.
364b7579f77SDag-Erling Smørgrav 	 *	This outbound_entry will be used on later module invocations
365b7579f77SDag-Erling Smørgrav 	 *	that involve this query (timeout, error or reply).
366b7579f77SDag-Erling Smørgrav 	 */
367bc892140SDag-Erling Smørgrav 	struct outbound_entry* (*send_query)(struct query_info* qinfo,
368bc892140SDag-Erling Smørgrav 		uint16_t flags, int dnssec, int want_dnssec, int nocaps,
369e2d15004SDag-Erling Smørgrav 		struct sockaddr_storage* addr, socklen_t addrlen,
370*24e36522SCy Schubert 		uint8_t* zone, size_t zonelen, int tcp_upstream, int ssl_upstream,
3710fb34990SDag-Erling Smørgrav 		char* tls_auth_name, struct module_qstate* q);
372b7579f77SDag-Erling Smørgrav 
373b7579f77SDag-Erling Smørgrav 	/**
374b7579f77SDag-Erling Smørgrav 	 * Detach-subqueries.
375b7579f77SDag-Erling Smørgrav 	 * Remove all sub-query references from this query state.
376b7579f77SDag-Erling Smørgrav 	 * Keeps super-references of those sub-queries correct.
377b7579f77SDag-Erling Smørgrav 	 * Updates stat items in mesh_area structure.
378b7579f77SDag-Erling Smørgrav 	 * @param qstate: used to find mesh state.
379b7579f77SDag-Erling Smørgrav 	 */
380b7579f77SDag-Erling Smørgrav 	void (*detach_subs)(struct module_qstate* qstate);
381b7579f77SDag-Erling Smørgrav 
382b7579f77SDag-Erling Smørgrav 	/**
383b7579f77SDag-Erling Smørgrav 	 * Attach subquery.
384b7579f77SDag-Erling Smørgrav 	 * Creates it if it does not exist already.
385b7579f77SDag-Erling Smørgrav 	 * Keeps sub and super references correct.
386b7579f77SDag-Erling Smørgrav 	 * Updates stat items in mesh_area structure.
387b7579f77SDag-Erling Smørgrav 	 * Pass if it is priming query or not.
388b7579f77SDag-Erling Smørgrav 	 * return:
389b7579f77SDag-Erling Smørgrav 	 * o if error (malloc) happened.
390b7579f77SDag-Erling Smørgrav 	 * o need to initialise the new state (module init; it is a new state).
391b7579f77SDag-Erling Smørgrav 	 *   so that the next run of the query with this module is successful.
392b7579f77SDag-Erling Smørgrav 	 * o no init needed, attachment successful.
393b7579f77SDag-Erling Smørgrav 	 *
394b7579f77SDag-Erling Smørgrav 	 * @param qstate: the state to find mesh state, and that wants to
395b7579f77SDag-Erling Smørgrav 	 * 	receive the results from the new subquery.
396b7579f77SDag-Erling Smørgrav 	 * @param qinfo: what to query for (copied).
397b7579f77SDag-Erling Smørgrav 	 * @param qflags: what flags to use (RD, CD flag or not).
398b7579f77SDag-Erling Smørgrav 	 * @param prime: if it is a (stub) priming query.
399ff825849SDag-Erling Smørgrav 	 * @param valrec: validation lookup recursion, does not need validation
400b7579f77SDag-Erling Smørgrav 	 * @param newq: If the new subquery needs initialisation, it is
401b7579f77SDag-Erling Smørgrav 	 * 	returned, otherwise NULL is returned.
402b7579f77SDag-Erling Smørgrav 	 * @return: false on error, true if success (and init may be needed).
403b7579f77SDag-Erling Smørgrav 	 */
404b7579f77SDag-Erling Smørgrav 	int (*attach_sub)(struct module_qstate* qstate,
405b7579f77SDag-Erling Smørgrav 		struct query_info* qinfo, uint16_t qflags, int prime,
406ff825849SDag-Erling Smørgrav 		int valrec, struct module_qstate** newq);
407b7579f77SDag-Erling Smørgrav 
408b7579f77SDag-Erling Smørgrav 	/**
409c7f4d7adSDag-Erling Smørgrav 	 * Add detached query.
410c7f4d7adSDag-Erling Smørgrav 	 * Creates it if it does not exist already.
411c7f4d7adSDag-Erling Smørgrav 	 * Does not make super/sub references.
412c7f4d7adSDag-Erling Smørgrav 	 * Performs a cycle detection - for double check - and fails if there is
413c7f4d7adSDag-Erling Smørgrav 	 * 	one.
414c7f4d7adSDag-Erling Smørgrav 	 * Updates stat items in mesh_area structure.
415c7f4d7adSDag-Erling Smørgrav 	 * Pass if it is priming query or not.
416c7f4d7adSDag-Erling Smørgrav 	 * return:
417c7f4d7adSDag-Erling Smørgrav 	 * 	o if error (malloc) happened.
418c7f4d7adSDag-Erling Smørgrav 	 * 	o need to initialise the new state (module init; it is a new state).
419c7f4d7adSDag-Erling Smørgrav 	 * 	  so that the next run of the query with this module is successful.
420c7f4d7adSDag-Erling Smørgrav 	 * 	o no init needed, attachment successful.
421c7f4d7adSDag-Erling Smørgrav 	 * 	o added subquery, created if it did not exist already.
422c7f4d7adSDag-Erling Smørgrav 	 *
423c7f4d7adSDag-Erling Smørgrav 	 * @param qstate: the state to find mesh state, and that wants to receive
424c7f4d7adSDag-Erling Smørgrav 	 * 	the results from the new subquery.
425c7f4d7adSDag-Erling Smørgrav 	 * @param qinfo: what to query for (copied).
426c7f4d7adSDag-Erling Smørgrav 	 * @param qflags: what flags to use (RD / CD flag or not).
427c7f4d7adSDag-Erling Smørgrav 	 * @param prime: if it is a (stub) priming query.
428c7f4d7adSDag-Erling Smørgrav 	 * @param valrec: if it is a validation recursion query (lookup of key, DS).
429c7f4d7adSDag-Erling Smørgrav 	 * @param newq: If the new subquery needs initialisation, it is returned,
430c7f4d7adSDag-Erling Smørgrav 	 * 	otherwise NULL is returned.
431c7f4d7adSDag-Erling Smørgrav 	 * @param sub: The added mesh state, created if it did not exist already.
432c7f4d7adSDag-Erling Smørgrav 	 * @return: false on error, true if success (and init may be needed).
433c7f4d7adSDag-Erling Smørgrav 	 */
434c7f4d7adSDag-Erling Smørgrav 	int (*add_sub)(struct module_qstate* qstate,
435c7f4d7adSDag-Erling Smørgrav 		struct query_info* qinfo, uint16_t qflags, int prime,
436c7f4d7adSDag-Erling Smørgrav 		int valrec, struct module_qstate** newq,
437c7f4d7adSDag-Erling Smørgrav 		struct mesh_state** sub);
438c7f4d7adSDag-Erling Smørgrav 
439c7f4d7adSDag-Erling Smørgrav 	/**
440b7579f77SDag-Erling Smørgrav 	 * Kill newly attached sub. If attach_sub returns newq for
441b7579f77SDag-Erling Smørgrav 	 * initialisation, but that fails, then this routine will cleanup and
4428a384985SDag-Erling Smørgrav 	 * delete the freshly created sub.
443b7579f77SDag-Erling Smørgrav 	 * @param newq: the new subquery that is no longer needed.
444b7579f77SDag-Erling Smørgrav 	 * 	It is removed.
445b7579f77SDag-Erling Smørgrav 	 */
446b7579f77SDag-Erling Smørgrav 	void (*kill_sub)(struct module_qstate* newq);
447b7579f77SDag-Erling Smørgrav 
448b7579f77SDag-Erling Smørgrav 	/**
449b7579f77SDag-Erling Smørgrav 	 * Detect if adding a dependency for qstate on name,type,class will
450b7579f77SDag-Erling Smørgrav 	 * create a dependency cycle.
451b7579f77SDag-Erling Smørgrav 	 * @param qstate: given mesh querystate.
452b7579f77SDag-Erling Smørgrav 	 * @param qinfo: query info for dependency.
453b7579f77SDag-Erling Smørgrav 	 * @param flags: query flags of dependency, RD/CD flags.
454b7579f77SDag-Erling Smørgrav 	 * @param prime: if dependency is a priming query or not.
455ff825849SDag-Erling Smørgrav 	 * @param valrec: validation lookup recursion, does not need validation
456b7579f77SDag-Erling Smørgrav 	 * @return true if the name,type,class exists and the given
457b7579f77SDag-Erling Smørgrav 	 * 	qstate mesh exists as a dependency of that name. Thus
458b7579f77SDag-Erling Smørgrav 	 * 	if qstate becomes dependent on name,type,class then a
459b7579f77SDag-Erling Smørgrav 	 * 	cycle is created.
460b7579f77SDag-Erling Smørgrav 	 */
461b7579f77SDag-Erling Smørgrav 	int (*detect_cycle)(struct module_qstate* qstate,
462ff825849SDag-Erling Smørgrav 		struct query_info* qinfo, uint16_t flags, int prime,
463ff825849SDag-Erling Smørgrav 		int valrec);
464b7579f77SDag-Erling Smørgrav 
465b7579f77SDag-Erling Smørgrav 	/** region for temporary usage. May be cleared after operate() call. */
466b7579f77SDag-Erling Smørgrav 	struct regional* scratch;
467b7579f77SDag-Erling Smørgrav 	/** buffer for temporary usage. May be cleared after operate() call. */
46817d15b25SDag-Erling Smørgrav 	struct sldns_buffer* scratch_buffer;
469b7579f77SDag-Erling Smørgrav 	/** internal data for daemon - worker thread. */
470b7579f77SDag-Erling Smørgrav 	struct worker* worker;
47157bddd21SDag-Erling Smørgrav 	/** the worker event base */
47257bddd21SDag-Erling Smørgrav 	struct comm_base* worker_base;
47357bddd21SDag-Erling Smørgrav 	/** the outside network */
47457bddd21SDag-Erling Smørgrav 	struct outside_network* outnet;
475b7579f77SDag-Erling Smørgrav 	/** mesh area with query state dependencies */
476b7579f77SDag-Erling Smørgrav 	struct mesh_area* mesh;
477b7579f77SDag-Erling Smørgrav 	/** allocation service */
478b7579f77SDag-Erling Smørgrav 	struct alloc_cache* alloc;
479b7579f77SDag-Erling Smørgrav 	/** random table to generate random numbers */
480b7579f77SDag-Erling Smørgrav 	struct ub_randstate* rnd;
481b7579f77SDag-Erling Smørgrav 	/** time in seconds, converted to integer */
48217d15b25SDag-Erling Smørgrav 	time_t* now;
483b7579f77SDag-Erling Smørgrav 	/** time in microseconds. Relatively recent. */
484b7579f77SDag-Erling Smørgrav 	struct timeval* now_tv;
485b7579f77SDag-Erling Smørgrav 	/** is validation required for messages, controls client-facing
486b7579f77SDag-Erling Smørgrav 	 * validation status (AD bits) and servfails */
487b7579f77SDag-Erling Smørgrav 	int need_to_validate;
488b7579f77SDag-Erling Smørgrav 	/** trusted key storage; these are the configured keys, if not NULL,
489b7579f77SDag-Erling Smørgrav 	 * otherwise configured by validator. These are the trust anchors,
490b7579f77SDag-Erling Smørgrav 	 * and are not primed and ready for validation, but on the bright
491b7579f77SDag-Erling Smørgrav 	 * side, they are read only memory, thus no locks and fast. */
492b7579f77SDag-Erling Smørgrav 	struct val_anchors* anchors;
493b7579f77SDag-Erling Smørgrav 	/** negative cache, configured by the validator. if not NULL,
494b7579f77SDag-Erling Smørgrav 	 * contains NSEC record lookup trees. */
495b7579f77SDag-Erling Smørgrav 	struct val_neg_cache* neg_cache;
496b7579f77SDag-Erling Smørgrav 	/** the 5011-probe timer (if any) */
497b7579f77SDag-Erling Smørgrav 	struct comm_timer* probe_timer;
49857bddd21SDag-Erling Smørgrav 	/** auth zones */
49957bddd21SDag-Erling Smørgrav 	struct auth_zones* auth_zones;
500b7579f77SDag-Erling Smørgrav 	/** Mapping of forwarding zones to targets.
501b7579f77SDag-Erling Smørgrav 	 * iterator forwarder information. per-thread, created by worker */
502b7579f77SDag-Erling Smørgrav 	struct iter_forwards* fwds;
503b7579f77SDag-Erling Smørgrav 	/**
504b7579f77SDag-Erling Smørgrav 	 * iterator forwarder information. per-thread, created by worker.
505b7579f77SDag-Erling Smørgrav 	 * The hints -- these aren't stored in the cache because they don't
506b7579f77SDag-Erling Smørgrav 	 * expire. The hints are always used to "prime" the cache. Note
507b7579f77SDag-Erling Smørgrav 	 * that both root hints and stub zone "hints" are stored in this
508b7579f77SDag-Erling Smørgrav 	 * data structure.
509b7579f77SDag-Erling Smørgrav 	 */
510b7579f77SDag-Erling Smørgrav 	struct iter_hints* hints;
511b7579f77SDag-Erling Smørgrav 	/** module specific data. indexed by module id. */
512b7579f77SDag-Erling Smørgrav 	void* modinfo[MAX_MODULE];
513bc892140SDag-Erling Smørgrav 
514bc892140SDag-Erling Smørgrav 	/* Shared linked list of inplace callback functions */
51565b390aaSDag-Erling Smørgrav 	struct inplace_cb* inplace_cb_lists[inplace_cb_types_total];
516bc892140SDag-Erling Smørgrav 
517bc892140SDag-Erling Smørgrav 	/**
518bc892140SDag-Erling Smørgrav 	 * Shared array of known edns options (size MAX_KNOWN_EDNS_OPTS).
519bc892140SDag-Erling Smørgrav 	 * Filled by edns literate modules during init.
520bc892140SDag-Erling Smørgrav 	 */
521bc892140SDag-Erling Smørgrav 	struct edns_known_option* edns_known_options;
522bc892140SDag-Erling Smørgrav 	/* Number of known edns options */
523bc892140SDag-Erling Smørgrav 	size_t edns_known_options_num;
524369c6923SCy Schubert 	/** EDNS client string information */
525369c6923SCy Schubert 	struct edns_strings* edns_strings;
52665b390aaSDag-Erling Smørgrav 
52765b390aaSDag-Erling Smørgrav 	/* Make every mesh state unique, do not aggregate mesh states. */
52865b390aaSDag-Erling Smørgrav 	int unique_mesh;
529b7579f77SDag-Erling Smørgrav };
530b7579f77SDag-Erling Smørgrav 
531b7579f77SDag-Erling Smørgrav /**
532b7579f77SDag-Erling Smørgrav  * External visible states of the module state machine
533b7579f77SDag-Erling Smørgrav  * Modules may also have an internal state.
534b7579f77SDag-Erling Smørgrav  * Modules are supposed to run to completion or until blocked.
535b7579f77SDag-Erling Smørgrav  */
536b7579f77SDag-Erling Smørgrav enum module_ext_state {
537b7579f77SDag-Erling Smørgrav 	/** initial state - new query */
538b7579f77SDag-Erling Smørgrav 	module_state_initial = 0,
539b7579f77SDag-Erling Smørgrav 	/** waiting for reply to outgoing network query */
540b7579f77SDag-Erling Smørgrav 	module_wait_reply,
541b7579f77SDag-Erling Smørgrav 	/** module is waiting for another module */
542b7579f77SDag-Erling Smørgrav 	module_wait_module,
543b7579f77SDag-Erling Smørgrav 	/** module is waiting for another module; that other is restarted */
544b7579f77SDag-Erling Smørgrav 	module_restart_next,
545b7579f77SDag-Erling Smørgrav 	/** module is waiting for sub-query */
546b7579f77SDag-Erling Smørgrav 	module_wait_subquery,
547b7579f77SDag-Erling Smørgrav 	/** module could not finish the query */
548b7579f77SDag-Erling Smørgrav 	module_error,
549b7579f77SDag-Erling Smørgrav 	/** module is finished with query */
550b7579f77SDag-Erling Smørgrav 	module_finished
551b7579f77SDag-Erling Smørgrav };
552b7579f77SDag-Erling Smørgrav 
553b7579f77SDag-Erling Smørgrav /**
554b7579f77SDag-Erling Smørgrav  * Events that happen to modules, that start or wakeup modules.
555b7579f77SDag-Erling Smørgrav  */
556b7579f77SDag-Erling Smørgrav enum module_ev {
557b7579f77SDag-Erling Smørgrav 	/** new query */
558b7579f77SDag-Erling Smørgrav 	module_event_new = 0,
559b7579f77SDag-Erling Smørgrav 	/** query passed by other module */
560b7579f77SDag-Erling Smørgrav 	module_event_pass,
561b7579f77SDag-Erling Smørgrav 	/** reply inbound from server */
562b7579f77SDag-Erling Smørgrav 	module_event_reply,
563b7579f77SDag-Erling Smørgrav 	/** no reply, timeout or other error */
564b7579f77SDag-Erling Smørgrav 	module_event_noreply,
565b7579f77SDag-Erling Smørgrav 	/** reply is there, but capitalisation check failed */
566b7579f77SDag-Erling Smørgrav 	module_event_capsfail,
567b7579f77SDag-Erling Smørgrav 	/** next module is done, and its reply is awaiting you */
568b7579f77SDag-Erling Smørgrav 	module_event_moddone,
569b7579f77SDag-Erling Smørgrav 	/** error */
570b7579f77SDag-Erling Smørgrav 	module_event_error
571b7579f77SDag-Erling Smørgrav };
572b7579f77SDag-Erling Smørgrav 
573b7579f77SDag-Erling Smørgrav /**
574b7579f77SDag-Erling Smørgrav  * Linked list of sockaddrs
575b7579f77SDag-Erling Smørgrav  * May be allocated such that only 'len' bytes of addr exist for the structure.
576b7579f77SDag-Erling Smørgrav  */
577b7579f77SDag-Erling Smørgrav struct sock_list {
578b7579f77SDag-Erling Smørgrav 	/** next in list */
579b7579f77SDag-Erling Smørgrav 	struct sock_list* next;
580b7579f77SDag-Erling Smørgrav 	/** length of addr */
581b7579f77SDag-Erling Smørgrav 	socklen_t len;
582b7579f77SDag-Erling Smørgrav 	/** sockaddr */
583b7579f77SDag-Erling Smørgrav 	struct sockaddr_storage addr;
584b7579f77SDag-Erling Smørgrav };
585b7579f77SDag-Erling Smørgrav 
58665b390aaSDag-Erling Smørgrav struct respip_action_info;
58765b390aaSDag-Erling Smørgrav 
588b7579f77SDag-Erling Smørgrav /**
589091e9e46SCy Schubert  * Struct to hold relevant data for serve expired
590091e9e46SCy Schubert  */
591091e9e46SCy Schubert struct serve_expired_data {
592091e9e46SCy Schubert 	struct comm_timer* timer;
593091e9e46SCy Schubert 	serve_expired_lookup_func_type* get_cached_answer;
594091e9e46SCy Schubert };
595091e9e46SCy Schubert 
596091e9e46SCy Schubert /**
597b7579f77SDag-Erling Smørgrav  * Module state, per query.
598b7579f77SDag-Erling Smørgrav  */
599b7579f77SDag-Erling Smørgrav struct module_qstate {
600b7579f77SDag-Erling Smørgrav 	/** which query is being answered: name, type, class */
601b7579f77SDag-Erling Smørgrav 	struct query_info qinfo;
602b7579f77SDag-Erling Smørgrav 	/** flags uint16 from query */
603b7579f77SDag-Erling Smørgrav 	uint16_t query_flags;
604b7579f77SDag-Erling Smørgrav 	/** if this is a (stub or root) priming query (with hints) */
605b7579f77SDag-Erling Smørgrav 	int is_priming;
606ff825849SDag-Erling Smørgrav 	/** if this is a validation recursion query that does not get
607ff825849SDag-Erling Smørgrav 	 * validation itself */
608ff825849SDag-Erling Smørgrav 	int is_valrec;
609b7579f77SDag-Erling Smørgrav 
610b7579f77SDag-Erling Smørgrav 	/** comm_reply contains server replies */
611b7579f77SDag-Erling Smørgrav 	struct comm_reply* reply;
612b7579f77SDag-Erling Smørgrav 	/** the reply message, with message for client and calling module */
613b7579f77SDag-Erling Smørgrav 	struct dns_msg* return_msg;
614b7579f77SDag-Erling Smørgrav 	/** the rcode, in case of error, instead of a reply message */
615b7579f77SDag-Erling Smørgrav 	int return_rcode;
616b7579f77SDag-Erling Smørgrav 	/** origin of the reply (can be NULL from cache, list for cnames) */
617b7579f77SDag-Erling Smørgrav 	struct sock_list* reply_origin;
618b7579f77SDag-Erling Smørgrav 	/** IP blacklist for queries */
619b7579f77SDag-Erling Smørgrav 	struct sock_list* blacklist;
620b7579f77SDag-Erling Smørgrav 	/** region for this query. Cleared when query process finishes. */
621b7579f77SDag-Erling Smørgrav 	struct regional* region;
622b7579f77SDag-Erling Smørgrav 	/** failure reason information if val-log-level is high */
623b7579f77SDag-Erling Smørgrav 	struct config_strlist* errinf;
624b7579f77SDag-Erling Smørgrav 
625b7579f77SDag-Erling Smørgrav 	/** which module is executing */
626b7579f77SDag-Erling Smørgrav 	int curmod;
627b7579f77SDag-Erling Smørgrav 	/** module states */
628b7579f77SDag-Erling Smørgrav 	enum module_ext_state ext_state[MAX_MODULE];
629b7579f77SDag-Erling Smørgrav 	/** module specific data for query. indexed by module id. */
630b7579f77SDag-Erling Smørgrav 	void* minfo[MAX_MODULE];
631b7579f77SDag-Erling Smørgrav 	/** environment for this query */
632b7579f77SDag-Erling Smørgrav 	struct module_env* env;
633b7579f77SDag-Erling Smørgrav 	/** mesh related information for this query */
634b7579f77SDag-Erling Smørgrav 	struct mesh_state* mesh_info;
635b7579f77SDag-Erling Smørgrav 	/** how many seconds before expiry is this prefetched (0 if not) */
63617d15b25SDag-Erling Smørgrav 	time_t prefetch_leeway;
637091e9e46SCy Schubert 	/** serve expired data */
638091e9e46SCy Schubert 	struct serve_expired_data* serve_expired_data;
639bc892140SDag-Erling Smørgrav 
640bc892140SDag-Erling Smørgrav 	/** incoming edns options from the front end */
641bc892140SDag-Erling Smørgrav 	struct edns_option* edns_opts_front_in;
642bc892140SDag-Erling Smørgrav 	/** outgoing edns options to the back end */
643bc892140SDag-Erling Smørgrav 	struct edns_option* edns_opts_back_out;
644bc892140SDag-Erling Smørgrav 	/** incoming edns options from the back end */
645bc892140SDag-Erling Smørgrav 	struct edns_option* edns_opts_back_in;
646bc892140SDag-Erling Smørgrav 	/** outgoing edns options to the front end */
647bc892140SDag-Erling Smørgrav 	struct edns_option* edns_opts_front_out;
648bc892140SDag-Erling Smørgrav 	/** whether modules should answer from the cache */
649bc892140SDag-Erling Smørgrav 	int no_cache_lookup;
650bc892140SDag-Erling Smørgrav 	/** whether modules should store answer in the cache */
651bc892140SDag-Erling Smørgrav 	int no_cache_store;
6528a384985SDag-Erling Smørgrav 	/** whether to refetch a fresh answer on finishing this state*/
6538a384985SDag-Erling Smørgrav 	int need_refetch;
6544c75e3aaSDag-Erling Smørgrav 	/** whether the query (or a subquery) was ratelimited */
6554c75e3aaSDag-Erling Smørgrav 	int was_ratelimited;
65665b390aaSDag-Erling Smørgrav 
65765b390aaSDag-Erling Smørgrav 	/**
65865b390aaSDag-Erling Smørgrav 	 * Attributes of clients that share the qstate that may affect IP-based
65965b390aaSDag-Erling Smørgrav 	 * actions.
66065b390aaSDag-Erling Smørgrav 	 */
66165b390aaSDag-Erling Smørgrav 	struct respip_client_info* client_info;
66265b390aaSDag-Erling Smørgrav 
66365b390aaSDag-Erling Smørgrav 	/** Extended result of response-ip action processing, mainly
66465b390aaSDag-Erling Smørgrav 	 *  for logging purposes. */
66565b390aaSDag-Erling Smørgrav 	struct respip_action_info* respip_action_info;
66665b390aaSDag-Erling Smørgrav 
66765b390aaSDag-Erling Smørgrav 	/** whether the reply should be dropped */
66865b390aaSDag-Erling Smørgrav 	int is_drop;
669b7579f77SDag-Erling Smørgrav };
670b7579f77SDag-Erling Smørgrav 
671b7579f77SDag-Erling Smørgrav /**
672b7579f77SDag-Erling Smørgrav  * Module functionality block
673b7579f77SDag-Erling Smørgrav  */
674b7579f77SDag-Erling Smørgrav struct module_func_block {
675b7579f77SDag-Erling Smørgrav 	/** text string name of module */
676b7579f77SDag-Erling Smørgrav 	const char* name;
677b7579f77SDag-Erling Smørgrav 
678b7579f77SDag-Erling Smørgrav 	/**
679b7579f77SDag-Erling Smørgrav 	 * init the module. Called once for the global state.
680b7579f77SDag-Erling Smørgrav 	 * This is the place to apply settings from the config file.
681b7579f77SDag-Erling Smørgrav 	 * @param env: module environment.
682b7579f77SDag-Erling Smørgrav 	 * @param id: module id number.
683b7579f77SDag-Erling Smørgrav 	 * return: 0 on error
684b7579f77SDag-Erling Smørgrav 	 */
685b7579f77SDag-Erling Smørgrav 	int (*init)(struct module_env* env, int id);
686b7579f77SDag-Erling Smørgrav 
687b7579f77SDag-Erling Smørgrav 	/**
688b7579f77SDag-Erling Smørgrav 	 * de-init, delete, the module. Called once for the global state.
689b7579f77SDag-Erling Smørgrav 	 * @param env: module environment.
690b7579f77SDag-Erling Smørgrav 	 * @param id: module id number.
691b7579f77SDag-Erling Smørgrav 	 */
692b7579f77SDag-Erling Smørgrav 	void (*deinit)(struct module_env* env, int id);
693b7579f77SDag-Erling Smørgrav 
694b7579f77SDag-Erling Smørgrav 	/**
695b7579f77SDag-Erling Smørgrav 	 * accept a new query, or work further on existing query.
696b7579f77SDag-Erling Smørgrav 	 * Changes the qstate->ext_state to be correct on exit.
697b7579f77SDag-Erling Smørgrav 	 * @param ev: event that causes the module state machine to
698b7579f77SDag-Erling Smørgrav 	 *	(re-)activate.
699b7579f77SDag-Erling Smørgrav 	 * @param qstate: the query state.
700b7579f77SDag-Erling Smørgrav 	 *	Note that this method is not allowed to change the
701b7579f77SDag-Erling Smørgrav 	 *	query state 'identity', that is query info, qflags,
702b7579f77SDag-Erling Smørgrav 	 *	and priming status.
703b7579f77SDag-Erling Smørgrav 	 *	Attach a subquery to get results to a different query.
704b7579f77SDag-Erling Smørgrav 	 * @param id: module id number that operate() is called on.
705b7579f77SDag-Erling Smørgrav 	 * @param outbound: if not NULL this event is due to the reply/timeout
706b7579f77SDag-Erling Smørgrav 	 *	or error on this outbound query.
707b7579f77SDag-Erling Smørgrav 	 * @return: if at exit the ext_state is:
708b7579f77SDag-Erling Smørgrav 	 *	o wait_module: next module is started. (with pass event).
709b7579f77SDag-Erling Smørgrav 	 *	o error or finished: previous module is resumed.
710b7579f77SDag-Erling Smørgrav 	 *	o otherwise it waits until that event happens (assumes
711b7579f77SDag-Erling Smørgrav 	 *	  the service routine to make subrequest or send message
712b7579f77SDag-Erling Smørgrav 	 *	  have been called.
713b7579f77SDag-Erling Smørgrav 	 */
714b7579f77SDag-Erling Smørgrav 	void (*operate)(struct module_qstate* qstate, enum module_ev event,
715b7579f77SDag-Erling Smørgrav 		int id, struct outbound_entry* outbound);
716b7579f77SDag-Erling Smørgrav 
717b7579f77SDag-Erling Smørgrav 	/**
718b7579f77SDag-Erling Smørgrav 	 * inform super querystate about the results from this subquerystate.
719b7579f77SDag-Erling Smørgrav 	 * Is called when the querystate is finished.  The method invoked is
720b7579f77SDag-Erling Smørgrav 	 * the one from the current module active in the super querystate.
721b7579f77SDag-Erling Smørgrav 	 * @param qstate: the query state that is finished.
722b7579f77SDag-Erling Smørgrav 	 *	Examine return_rcode and return_reply in the qstate.
723b7579f77SDag-Erling Smørgrav 	 * @param id: module id for this module.
724b7579f77SDag-Erling Smørgrav 	 *	This coincides with the current module for the super qstate.
725b7579f77SDag-Erling Smørgrav 	 * @param super: the super querystate that needs to be informed.
726b7579f77SDag-Erling Smørgrav 	 */
727b7579f77SDag-Erling Smørgrav 	void (*inform_super)(struct module_qstate* qstate, int id,
728b7579f77SDag-Erling Smørgrav 		struct module_qstate* super);
729b7579f77SDag-Erling Smørgrav 
730b7579f77SDag-Erling Smørgrav 	/**
731b7579f77SDag-Erling Smørgrav 	 * clear module specific data
732b7579f77SDag-Erling Smørgrav 	 */
733b7579f77SDag-Erling Smørgrav 	void (*clear)(struct module_qstate* qstate, int id);
734b7579f77SDag-Erling Smørgrav 
735b7579f77SDag-Erling Smørgrav 	/**
736b7579f77SDag-Erling Smørgrav 	 * How much memory is the module specific data using.
737b7579f77SDag-Erling Smørgrav 	 * @param env: module environment.
738b7579f77SDag-Erling Smørgrav 	 * @param id: the module id.
739b7579f77SDag-Erling Smørgrav 	 * @return the number of bytes that are alloced.
740b7579f77SDag-Erling Smørgrav 	 */
741b7579f77SDag-Erling Smørgrav 	size_t (*get_mem)(struct module_env* env, int id);
742b7579f77SDag-Erling Smørgrav };
743b7579f77SDag-Erling Smørgrav 
744b7579f77SDag-Erling Smørgrav /**
745b7579f77SDag-Erling Smørgrav  * Debug utility: module external qstate to string
746b7579f77SDag-Erling Smørgrav  * @param s: the state value.
747b7579f77SDag-Erling Smørgrav  * @return descriptive string.
748b7579f77SDag-Erling Smørgrav  */
749b7579f77SDag-Erling Smørgrav const char* strextstate(enum module_ext_state s);
750b7579f77SDag-Erling Smørgrav 
751b7579f77SDag-Erling Smørgrav /**
752b7579f77SDag-Erling Smørgrav  * Debug utility: module event to string
753b7579f77SDag-Erling Smørgrav  * @param e: the module event value.
754b7579f77SDag-Erling Smørgrav  * @return descriptive string.
755b7579f77SDag-Erling Smørgrav  */
756b7579f77SDag-Erling Smørgrav const char* strmodulevent(enum module_ev e);
757b7579f77SDag-Erling Smørgrav 
758bc892140SDag-Erling Smørgrav /**
759bc892140SDag-Erling Smørgrav  * Initialize the edns known options by allocating the required space.
760bc892140SDag-Erling Smørgrav  * @param env: the module environment.
761bc892140SDag-Erling Smørgrav  * @return false on failure (no memory).
762bc892140SDag-Erling Smørgrav  */
763bc892140SDag-Erling Smørgrav int edns_known_options_init(struct module_env* env);
764bc892140SDag-Erling Smørgrav 
765bc892140SDag-Erling Smørgrav /**
766bc892140SDag-Erling Smørgrav  * Free the allocated space for the known edns options.
767bc892140SDag-Erling Smørgrav  * @param env: the module environment.
768bc892140SDag-Erling Smørgrav  */
769bc892140SDag-Erling Smørgrav void edns_known_options_delete(struct module_env* env);
770bc892140SDag-Erling Smørgrav 
771bc892140SDag-Erling Smørgrav /**
772bc892140SDag-Erling Smørgrav  * Register a known edns option. Overwrite the flags if it is already
773bc892140SDag-Erling Smørgrav  * registered. Used before creating workers to register known edns options.
774bc892140SDag-Erling Smørgrav  * @param opt_code: the edns option code.
775bc892140SDag-Erling Smørgrav  * @param bypass_cache_stage: whether the option interacts with the cache.
776bc892140SDag-Erling Smørgrav  * @param no_aggregation: whether the option implies more specific
777bc892140SDag-Erling Smørgrav  *	aggregation.
778bc892140SDag-Erling Smørgrav  * @param env: the module environment.
779bc892140SDag-Erling Smørgrav  * @return true on success, false on failure (registering more options than
780bc892140SDag-Erling Smørgrav  *	allowed or trying to register after the environment is copied to the
781bc892140SDag-Erling Smørgrav  *	threads.)
782bc892140SDag-Erling Smørgrav  */
783bc892140SDag-Erling Smørgrav int edns_register_option(uint16_t opt_code, int bypass_cache_stage,
784bc892140SDag-Erling Smørgrav 	int no_aggregation, struct module_env* env);
785bc892140SDag-Erling Smørgrav 
786bc892140SDag-Erling Smørgrav /**
78765b390aaSDag-Erling Smørgrav  * Register an inplace callback function.
788bc892140SDag-Erling Smørgrav  * @param cb: pointer to the callback function.
78965b390aaSDag-Erling Smørgrav  * @param type: inplace callback type.
79065b390aaSDag-Erling Smørgrav  * @param cbarg: argument for the callback function, or NULL.
791bc892140SDag-Erling Smørgrav  * @param env: the module environment.
79265b390aaSDag-Erling Smørgrav  * @param id: module id.
793bc892140SDag-Erling Smørgrav  * @return true on success, false on failure (out of memory or trying to
794bc892140SDag-Erling Smørgrav  *	register after the environment is copied to the threads.)
795bc892140SDag-Erling Smørgrav  */
79665b390aaSDag-Erling Smørgrav int
79765b390aaSDag-Erling Smørgrav inplace_cb_register(void* cb, enum inplace_cb_list_type type, void* cbarg,
79865b390aaSDag-Erling Smørgrav 	struct module_env* env, int id);
799bc892140SDag-Erling Smørgrav 
800bc892140SDag-Erling Smørgrav /**
80165b390aaSDag-Erling Smørgrav  * Delete callback for specified type and module id.
802bc892140SDag-Erling Smørgrav  * @param env: the module environment.
80365b390aaSDag-Erling Smørgrav  * @param type: inplace callback type.
80465b390aaSDag-Erling Smørgrav  * @param id: module id.
805bc892140SDag-Erling Smørgrav  */
80665b390aaSDag-Erling Smørgrav void
80765b390aaSDag-Erling Smørgrav inplace_cb_delete(struct module_env* env, enum inplace_cb_list_type type,
80865b390aaSDag-Erling Smørgrav 	int id);
809bc892140SDag-Erling Smørgrav 
810bc892140SDag-Erling Smørgrav /**
811bc892140SDag-Erling Smørgrav  * Delete all the inplace callback linked lists.
812bc892140SDag-Erling Smørgrav  * @param env: the module environment.
813bc892140SDag-Erling Smørgrav  */
814bc892140SDag-Erling Smørgrav void inplace_cb_lists_delete(struct module_env* env);
815bc892140SDag-Erling Smørgrav 
816bc892140SDag-Erling Smørgrav /**
817bc892140SDag-Erling Smørgrav  * Check if an edns option is known.
818bc892140SDag-Erling Smørgrav  * @param opt_code: the edns option code.
819bc892140SDag-Erling Smørgrav  * @param env: the module environment.
820bc892140SDag-Erling Smørgrav  * @return pointer to registered option if the edns option is known,
821bc892140SDag-Erling Smørgrav  *	NULL otherwise.
822bc892140SDag-Erling Smørgrav  */
823bc892140SDag-Erling Smørgrav struct edns_known_option* edns_option_is_known(uint16_t opt_code,
824bc892140SDag-Erling Smørgrav 	struct module_env* env);
825bc892140SDag-Erling Smørgrav 
826bc892140SDag-Erling Smørgrav /**
827bc892140SDag-Erling Smørgrav  * Check if an edns option needs to bypass the reply from cache stage.
828bc892140SDag-Erling Smørgrav  * @param list: the edns options.
829bc892140SDag-Erling Smørgrav  * @param env: the module environment.
830bc892140SDag-Erling Smørgrav  * @return true if an edns option needs to bypass the cache stage,
831bc892140SDag-Erling Smørgrav  *	false otherwise.
832bc892140SDag-Erling Smørgrav  */
833bc892140SDag-Erling Smørgrav int edns_bypass_cache_stage(struct edns_option* list,
834bc892140SDag-Erling Smørgrav 	struct module_env* env);
835bc892140SDag-Erling Smørgrav 
836bc892140SDag-Erling Smørgrav /**
83765b390aaSDag-Erling Smørgrav  * Check if an unique mesh state is required. Might be triggered by EDNS option
83865b390aaSDag-Erling Smørgrav  * or set for the complete env.
839bc892140SDag-Erling Smørgrav  * @param list: the edns options.
840bc892140SDag-Erling Smørgrav  * @param env: the module environment.
841bc892140SDag-Erling Smørgrav  * @return true if an edns option needs a unique mesh state,
842bc892140SDag-Erling Smørgrav  *	false otherwise.
843bc892140SDag-Erling Smørgrav  */
84465b390aaSDag-Erling Smørgrav int unique_mesh_state(struct edns_option* list, struct module_env* env);
845bc892140SDag-Erling Smørgrav 
846bc892140SDag-Erling Smørgrav /**
847bc892140SDag-Erling Smørgrav  * Log the known edns options.
848bc892140SDag-Erling Smørgrav  * @param level: the desired verbosity level.
849bc892140SDag-Erling Smørgrav  * @param env: the module environment.
850bc892140SDag-Erling Smørgrav  */
851bc892140SDag-Erling Smørgrav void log_edns_known_options(enum verbosity_value level,
852bc892140SDag-Erling Smørgrav 	struct module_env* env);
853bc892140SDag-Erling Smørgrav 
8544c75e3aaSDag-Erling Smørgrav /**
8554c75e3aaSDag-Erling Smørgrav  * Copy state that may have happened in the subquery and is always relevant to
8564c75e3aaSDag-Erling Smørgrav  * the super.
8574c75e3aaSDag-Erling Smørgrav  * @param qstate: query state that finished.
8584c75e3aaSDag-Erling Smørgrav  * @param id: module id.
8594c75e3aaSDag-Erling Smørgrav  * @param super: the qstate to inform.
8604c75e3aaSDag-Erling Smørgrav  */
8614c75e3aaSDag-Erling Smørgrav void copy_state_to_super(struct module_qstate* qstate, int id,
8624c75e3aaSDag-Erling Smørgrav 	struct module_qstate* super);
8634c75e3aaSDag-Erling Smørgrav 
864b7579f77SDag-Erling Smørgrav #endif /* UTIL_MODULE_H */
865