Home | History | Annotate | Line # | Download | only in util
      1      1.1  christos /*
      2      1.1  christos  * util/module.h - DNS handling module interface
      3      1.1  christos  *
      4      1.1  christos  * Copyright (c) 2007, NLnet Labs. All rights reserved.
      5      1.1  christos  *
      6      1.1  christos  * This software is open source.
      7      1.1  christos  *
      8      1.1  christos  * Redistribution and use in source and binary forms, with or without
      9      1.1  christos  * modification, are permitted provided that the following conditions
     10      1.1  christos  * are met:
     11      1.1  christos  *
     12      1.1  christos  * Redistributions of source code must retain the above copyright notice,
     13      1.1  christos  * this list of conditions and the following disclaimer.
     14      1.1  christos  *
     15      1.1  christos  * Redistributions in binary form must reproduce the above copyright notice,
     16      1.1  christos  * this list of conditions and the following disclaimer in the documentation
     17      1.1  christos  * and/or other materials provided with the distribution.
     18      1.1  christos  *
     19      1.1  christos  * Neither the name of the NLNET LABS nor the names of its contributors may
     20      1.1  christos  * be used to endorse or promote products derived from this software without
     21      1.1  christos  * specific prior written permission.
     22      1.1  christos  *
     23      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24      1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25      1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26      1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27      1.1  christos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28      1.1  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29      1.1  christos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     30      1.1  christos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31      1.1  christos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32      1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33      1.1  christos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34      1.1  christos  */
     35      1.1  christos 
     36      1.1  christos /**
     37      1.1  christos  * \file
     38      1.1  christos  *
     39      1.1  christos  * This file contains the interface for DNS handling modules.
     40      1.1  christos  *
     41      1.1  christos  * The module interface uses the DNS modules as state machines.  The
     42      1.1  christos  * state machines are activated in sequence to operate on queries.  Once
     43      1.1  christos  * they are done, the reply is passed back.  In the usual setup the mesh
     44      1.1  christos  * is the caller of the state machines and once things are done sends replies
     45      1.1  christos  * and invokes result callbacks.
     46      1.1  christos  *
     47      1.1  christos  * The module provides a number of functions, listed in the module_func_block.
     48      1.1  christos  * The module is inited and destroyed and memory usage queries, for the
     49      1.1  christos  * module as a whole, for entire-module state (such as a cache).  And per-query
     50      1.1  christos  * functions are called, operate to move the state machine and cleanup of
     51      1.1  christos  * the per-query state.
     52      1.1  christos  *
     53      1.1  christos  * Most per-query state should simply be allocated in the query region.
     54      1.1  christos  * This is destroyed at the end of the query.
     55      1.1  christos  *
     56      1.1  christos  * The module environment contains services and information and caches
     57      1.1  christos  * shared by the modules and the rest of the system.  It also contains
     58      1.1  christos  * function pointers for module-specific tasks (like sending queries).
     59      1.1  christos  *
     60      1.1  christos  * *** Example module calls for a normal query
     61      1.1  christos  *
     62      1.1  christos  * In this example, the query does not need recursion, all the other data
     63      1.1  christos  * can be found in the cache.  This makes the example shorter.
     64      1.1  christos  *
     65      1.1  christos  * At the start of the program the iterator module is initialised.
     66      1.1  christos  * The iterator module sets up its global state, such as donotquery lists
     67      1.1  christos  * and private address trees.
     68      1.1  christos  *
     69      1.1  christos  * A query comes in, and a mesh entry is created for it.  The mesh
     70      1.1  christos  * starts the resolution process.  The validator module is the first
     71      1.1  christos  * in the list of modules, and it is started on this new query.  The
     72      1.1  christos  * operate() function is called.  The validator decides it needs not do
     73      1.1  christos  * anything yet until there is a result and returns wait_module, that
     74      1.1  christos  * causes the next module in the list to be started.
     75      1.1  christos  *
     76      1.1  christos  * The next module is the iterator.  It is started on the passed query and
     77      1.1  christos  * decides to perform a lookup.  For this simple example, the delegation
     78      1.1  christos  * point information is available, and all the iterator wants to do is
     79      1.1  christos  * send a UDP query.  The iterator uses env.send_query() to send the
     80      1.1  christos  * query.  Then the iterator suspends (returns from the operate call).
     81      1.1  christos  *
     82      1.1  christos  * When the UDP reply comes back (and on errors and timeouts), the
     83      1.1  christos  * operate function is called for the query, on the iterator module,
     84      1.1  christos  * with the event that there is a reply.  The iterator decides that this
     85      1.1  christos  * is enough, the work is done.  It returns the value finished from the
     86      1.1  christos  * operate call, which causes the previous module to be started.
     87      1.1  christos  *
     88      1.1  christos  * The previous module, the validator module, is started with the event
     89      1.1  christos  * that the iterator module is done.  The validator decides to validate
     90      1.1  christos  * the query.  Once it is done (which could take recursive lookups, but
     91      1.1  christos  * in this example no recursive lookups are needed), it returns from the
     92      1.1  christos  * operate function with finished.
     93      1.1  christos  *
     94      1.1  christos  * There is no previous module from the validator module, and the mesh
     95      1.1  christos  * takes this to mean that the query is finally done.  The mesh invokes
     96      1.1  christos  * callbacks and sends packets to queriers.
     97      1.1  christos  *
     98      1.1  christos  * If other modules had been waiting (recursively) on the answer to this
     99      1.1  christos  * query, then the mesh will tell them about it.  It calls the inform_super
    100      1.1  christos  * routine on all the waiting modules, and once that is done it calls all of
    101      1.1  christos  * them with the operate() call.  During inform_super the query that is done
    102      1.1  christos  * still exists and information can be copied from it (but the module should
    103      1.1  christos  * not really re-entry codepoints and services).  During the operate call
    104      1.1  christos  * the modules can use stored state to continue operation with the results.
    105      1.1  christos  * (network buffers are used to contain the answer packet during the
    106      1.1  christos  * inform_super phase, but after that the network buffers will be cleared
    107      1.1  christos  * of their contents so that other tasks can be performed).
    108      1.1  christos  *
    109      1.1  christos  * *** Example module calls for recursion
    110      1.1  christos  *
    111      1.1  christos  * A module is called in operate, and it decides that it wants to perform
    112      1.1  christos  * recursion.  That is, it wants the full state-machine-list to operate on
    113      1.1  christos  * a different query.  It calls env.attach_sub() to create a new query state.
    114      1.1  christos  * The routine returns the newly created state, and potentially the module
    115      1.1  christos  * can edit the module-states for the newly created query (i.e. pass along
    116      1.1  christos  * some information, like delegation points).  The module then suspends,
    117      1.1  christos  * returns from the operate routine.
    118      1.1  christos  *
    119      1.1  christos  * The mesh meanwhile will have the newly created query (or queries) on
    120      1.1  christos  * a waiting list, and will call operate() on this query (or queries).
    121      1.1  christos  * It starts again at the start of the module list for them.  The query
    122      1.1  christos  * (or queries) continue to operate their state machines, until they are
    123      1.1  christos  * done.  When they are done the mesh calls inform_super on the module that
    124      1.1  christos  * wanted the recursion.  After that the mesh calls operate() on the module
    125      1.1  christos  * that wanted to do the recursion, and during this phase the module could,
    126      1.1  christos  * for example, decide to create more recursions.
    127      1.1  christos  *
    128      1.1  christos  * If the module decides it no longer wants the recursive information
    129      1.1  christos  * it can call detach_subs.  Those queries will still run to completion,
    130      1.1  christos  * potentially filling the cache with information.  Inform_super is not
    131      1.1  christos  * called any more.
    132      1.1  christos  *
    133      1.1  christos  * The iterator module will fetch items from the cache, so a recursion
    134      1.1  christos  * attempt may complete very quickly if the item is in cache.  The calling
    135      1.1  christos  * module has to wait for completion or eventual timeout.  A recursive query
    136      1.1  christos  * that times out returns a servfail rcode (servfail is also returned for
    137      1.1  christos  * other errors during the lookup).
    138      1.1  christos  *
    139      1.1  christos  * Results are passed in the qstate, the rcode member is used to pass
    140      1.1  christos  * errors without requiring memory allocation, so that the code can continue
    141      1.1  christos  * in out-of-memory conditions.  If the rcode member is 0 (NOERROR) then
    142      1.1  christos  * the dns_msg entry contains a filled out message.  This message may
    143      1.1  christos  * also contain an rcode that is nonzero, but in this case additional
    144      1.1  christos  * information (query, additional) can be passed along.
    145      1.1  christos  *
    146  1.1.1.8  christos  * The rcode and dns_msg are used to pass the result from the rightmost
    147      1.1  christos  * module towards the leftmost modules and then towards the user.
    148      1.1  christos  *
    149      1.1  christos  * If you want to avoid recursion-cycles where queries need other queries
    150      1.1  christos  * that need the first one, use detect_cycle() to see if that will happen.
    151      1.1  christos  *
    152      1.1  christos  */
    153      1.1  christos 
    154      1.1  christos #ifndef UTIL_MODULE_H
    155      1.1  christos #define UTIL_MODULE_H
    156      1.1  christos #include "util/storage/lruhash.h"
    157      1.1  christos #include "util/data/msgreply.h"
    158      1.1  christos #include "util/data/msgparse.h"
    159      1.1  christos struct sldns_buffer;
    160      1.1  christos struct alloc_cache;
    161      1.1  christos struct rrset_cache;
    162      1.1  christos struct key_cache;
    163      1.1  christos struct config_file;
    164      1.1  christos struct slabhash;
    165      1.1  christos struct query_info;
    166      1.1  christos struct edns_data;
    167      1.1  christos struct regional;
    168      1.1  christos struct worker;
    169  1.1.1.3  christos struct comm_base;
    170  1.1.1.3  christos struct auth_zones;
    171  1.1.1.3  christos struct outside_network;
    172      1.1  christos struct module_qstate;
    173      1.1  christos struct ub_randstate;
    174      1.1  christos struct mesh_area;
    175      1.1  christos struct mesh_state;
    176      1.1  christos struct val_anchors;
    177      1.1  christos struct val_neg_cache;
    178      1.1  christos struct iter_forwards;
    179      1.1  christos struct iter_hints;
    180  1.1.1.8  christos struct views;
    181  1.1.1.2  christos struct respip_set;
    182  1.1.1.2  christos struct respip_client_info;
    183  1.1.1.2  christos struct respip_addr_info;
    184  1.1.1.8  christos struct module_stack;
    185      1.1  christos 
    186      1.1  christos /** Maximum number of modules in operation */
    187  1.1.1.2  christos #define MAX_MODULE 16
    188  1.1.1.2  christos 
    189  1.1.1.2  christos /** Maximum number of known edns options */
    190  1.1.1.2  christos #define MAX_KNOWN_EDNS_OPTS 256
    191  1.1.1.2  christos 
    192  1.1.1.6  christos struct errinf_strlist {
    193  1.1.1.6  christos     /** next item in list */
    194  1.1.1.6  christos     struct errinf_strlist* next;
    195  1.1.1.6  christos     /** config option string */
    196  1.1.1.6  christos     char* str;
    197  1.1.1.6  christos     /** EDE code companion to the error str */
    198  1.1.1.6  christos     int reason_bogus;
    199  1.1.1.6  christos };
    200  1.1.1.6  christos 
    201  1.1.1.2  christos enum inplace_cb_list_type {
    202  1.1.1.2  christos 	/* Inplace callbacks for when a resolved reply is ready to be sent to the
    203  1.1.1.2  christos 	 * front.*/
    204  1.1.1.2  christos 	inplace_cb_reply = 0,
    205  1.1.1.2  christos 	/* Inplace callbacks for when a reply is given from the cache. */
    206  1.1.1.2  christos 	inplace_cb_reply_cache,
    207  1.1.1.2  christos 	/* Inplace callbacks for when a reply is given with local data
    208  1.1.1.2  christos 	 * (or Chaos reply). */
    209  1.1.1.2  christos 	inplace_cb_reply_local,
    210  1.1.1.2  christos 	/* Inplace callbacks for when the reply is servfail. */
    211  1.1.1.2  christos 	inplace_cb_reply_servfail,
    212  1.1.1.2  christos 	/* Inplace callbacks for when a query is ready to be sent to the back.*/
    213  1.1.1.2  christos 	inplace_cb_query,
    214  1.1.1.2  christos 	/* Inplace callback for when a reply is received from the back. */
    215  1.1.1.2  christos 	inplace_cb_query_response,
    216  1.1.1.2  christos 	/* Inplace callback for when EDNS is parsed on a reply received from the
    217  1.1.1.2  christos 	 * back. */
    218  1.1.1.2  christos 	inplace_cb_edns_back_parsed,
    219  1.1.1.2  christos 	/* Total number of types. Used for array initialization.
    220  1.1.1.2  christos 	 * Should always be last. */
    221  1.1.1.2  christos 	inplace_cb_types_total
    222  1.1.1.2  christos };
    223  1.1.1.2  christos 
    224  1.1.1.2  christos 
    225  1.1.1.2  christos /** Known edns option. Can be populated during modules' init. */
    226  1.1.1.2  christos struct edns_known_option {
    227  1.1.1.2  christos 	/** type of this edns option */
    228  1.1.1.2  christos 	uint16_t opt_code;
    229  1.1.1.2  christos 	/** whether the option needs to bypass the cache stage */
    230  1.1.1.2  christos 	int bypass_cache_stage;
    231  1.1.1.2  christos 	/** whether the option needs mesh aggregation */
    232  1.1.1.2  christos 	int no_aggregation;
    233  1.1.1.2  christos };
    234  1.1.1.2  christos 
    235  1.1.1.2  christos /**
    236  1.1.1.2  christos  * Inplace callback list of registered routines to be called.
    237  1.1.1.2  christos  */
    238  1.1.1.2  christos struct inplace_cb {
    239  1.1.1.2  christos 	/** next in list */
    240  1.1.1.2  christos 	struct inplace_cb* next;
    241  1.1.1.2  christos 	/** Inplace callback routine */
    242  1.1.1.2  christos 	void* cb;
    243  1.1.1.2  christos 	void* cb_arg;
    244  1.1.1.2  christos 	/** module id */
    245  1.1.1.2  christos 	int id;
    246  1.1.1.2  christos };
    247  1.1.1.2  christos 
    248  1.1.1.2  christos /**
    249  1.1.1.2  christos  * Inplace callback function called before replying.
    250  1.1.1.4  christos  * Called as func(qinfo, qstate, rep, rcode, edns, opt_list_out, repinfo,
    251  1.1.1.4  christos  *                region, id, python_callback)
    252  1.1.1.2  christos  * Where:
    253  1.1.1.2  christos  *	qinfo: the query info.
    254  1.1.1.2  christos  *	qstate: the module state. NULL when calling before the query reaches the
    255  1.1.1.2  christos  *		mesh states.
    256  1.1.1.2  christos  *	rep: reply_info. Could be NULL.
    257  1.1.1.2  christos  *	rcode: the return code.
    258  1.1.1.2  christos  *	edns: the edns_data of the reply. When qstate is NULL, it is also used as
    259  1.1.1.2  christos  *		the edns input.
    260  1.1.1.2  christos  *	opt_list_out: the edns options list for the reply.
    261  1.1.1.4  christos  *	repinfo: reply information for a communication point. NULL when calling
    262  1.1.1.4  christos  *		during the mesh states; the same could be found from
    263  1.1.1.4  christos  *		qstate->mesh_info->reply_list.
    264  1.1.1.2  christos  *	region: region to store data.
    265  1.1.1.4  christos  *	id: module id.
    266  1.1.1.2  christos  *	python_callback: only used for registering a python callback function.
    267  1.1.1.2  christos  */
    268  1.1.1.2  christos typedef int inplace_cb_reply_func_type(struct query_info* qinfo,
    269  1.1.1.2  christos 	struct module_qstate* qstate, struct reply_info* rep, int rcode,
    270  1.1.1.4  christos 	struct edns_data* edns, struct edns_option** opt_list_out,
    271  1.1.1.5  christos 	struct comm_reply* repinfo, struct regional* region,
    272  1.1.1.5  christos 	struct timeval* start_time, int id, void* callback);
    273  1.1.1.2  christos 
    274  1.1.1.2  christos /**
    275  1.1.1.2  christos  * Inplace callback function called before sending the query to a nameserver.
    276  1.1.1.2  christos  * Called as func(qinfo, flags, qstate, addr, addrlen, zone, zonelen, region,
    277  1.1.1.4  christos  *                id, python_callback)
    278  1.1.1.2  christos  * Where:
    279  1.1.1.2  christos  *	qinfo: query info.
    280  1.1.1.2  christos  *	flags: flags of the query.
    281  1.1.1.2  christos  *	qstate: query state.
    282  1.1.1.2  christos  *	addr: to which server to send the query.
    283  1.1.1.2  christos  *	addrlen: length of addr.
    284  1.1.1.2  christos  *	zone: name of the zone of the delegation point. wireformat dname.
    285  1.1.1.2  christos  *		This is the delegation point name for which the server is deemed
    286  1.1.1.2  christos  *		authoritative.
    287  1.1.1.2  christos  *	zonelen: length of zone.
    288  1.1.1.2  christos  *	region: region to store data.
    289  1.1.1.4  christos  *	id: module id.
    290  1.1.1.2  christos  *	python_callback: only used for registering a python callback function.
    291  1.1.1.2  christos  */
    292  1.1.1.2  christos typedef int inplace_cb_query_func_type(struct query_info* qinfo, uint16_t flags,
    293  1.1.1.2  christos 	struct module_qstate* qstate, struct sockaddr_storage* addr,
    294  1.1.1.2  christos 	socklen_t addrlen, uint8_t* zone, size_t zonelen, struct regional* region,
    295  1.1.1.2  christos 	int id, void* callback);
    296  1.1.1.2  christos 
    297  1.1.1.2  christos /**
    298  1.1.1.2  christos  * Inplace callback function called after parsing edns on query reply.
    299  1.1.1.4  christos  * Called as func(qstate, id, cb_args)
    300  1.1.1.2  christos  * Where:
    301  1.1.1.4  christos  *	qstate: the query state.
    302  1.1.1.4  christos  *	id: module id.
    303  1.1.1.2  christos  *	cb_args: argument passed when registering callback.
    304  1.1.1.2  christos  */
    305  1.1.1.2  christos typedef int inplace_cb_edns_back_parsed_func_type(struct module_qstate* qstate,
    306  1.1.1.2  christos 	int id, void* cb_args);
    307  1.1.1.2  christos 
    308  1.1.1.2  christos /**
    309  1.1.1.2  christos  * Inplace callback function called after parsing query response.
    310  1.1.1.4  christos  * Called as func(qstate, response, id, cb_args)
    311  1.1.1.2  christos  * Where:
    312  1.1.1.4  christos  *	qstate: the query state.
    313  1.1.1.4  christos  *	response: query response.
    314  1.1.1.4  christos  *	id: module id.
    315  1.1.1.2  christos  *	cb_args: argument passed when registering callback.
    316  1.1.1.2  christos  */
    317  1.1.1.2  christos typedef int inplace_cb_query_response_func_type(struct module_qstate* qstate,
    318  1.1.1.2  christos 	struct dns_msg* response, int id, void* cb_args);
    319      1.1  christos 
    320      1.1  christos /**
    321  1.1.1.5  christos  * Function called when looking for (expired) cached answers during the serve
    322  1.1.1.5  christos  * expired logic.
    323  1.1.1.8  christos  * Called as func(qstate, lookup_qinfo, &is_expired)
    324  1.1.1.5  christos  * Where:
    325  1.1.1.5  christos  *	qstate: the query state.
    326  1.1.1.5  christos  *	lookup_qinfo: the qinfo to lookup for.
    327  1.1.1.8  christos  *      is_expired: set if the cached answer is expired.
    328  1.1.1.5  christos  */
    329  1.1.1.5  christos typedef struct dns_msg* serve_expired_lookup_func_type(
    330  1.1.1.8  christos 	struct module_qstate* qstate, struct query_info* lookup_qinfo,
    331  1.1.1.8  christos 	int* is_expired);
    332  1.1.1.5  christos 
    333  1.1.1.5  christos /**
    334      1.1  christos  * Module environment.
    335      1.1  christos  * Services and data provided to the module.
    336      1.1  christos  */
    337      1.1  christos struct module_env {
    338      1.1  christos 	/* --- data --- */
    339      1.1  christos 	/** config file with config options */
    340      1.1  christos 	struct config_file* cfg;
    341      1.1  christos 	/** shared message cache */
    342      1.1  christos 	struct slabhash* msg_cache;
    343      1.1  christos 	/** shared rrset cache */
    344      1.1  christos 	struct rrset_cache* rrset_cache;
    345      1.1  christos 	/** shared infrastructure cache (edns, lameness) */
    346      1.1  christos 	struct infra_cache* infra_cache;
    347      1.1  christos 	/** shared key cache */
    348      1.1  christos 	struct key_cache* key_cache;
    349      1.1  christos 
    350      1.1  christos 	/* --- services --- */
    351      1.1  christos 	/**
    352      1.1  christos 	 * Send serviced DNS query to server. UDP/TCP and EDNS is handled.
    353      1.1  christos 	 * operate() should return with wait_reply. Later on a callback
    354      1.1  christos 	 * will cause operate() to be called with event timeout or reply.
    355      1.1  christos 	 * The time until a timeout is calculated from roundtrip timing,
    356      1.1  christos 	 * several UDP retries are attempted.
    357  1.1.1.2  christos 	 * @param qinfo: query info.
    358      1.1  christos 	 * @param flags: host order flags word, with opcode and CD bit.
    359      1.1  christos 	 * @param dnssec: if set, EDNS record will have bits set.
    360      1.1  christos 	 *	If EDNS_DO bit is set, DO bit is set in EDNS records.
    361      1.1  christos 	 *	If BIT_CD is set, CD bit is set in queries with EDNS records.
    362      1.1  christos 	 * @param want_dnssec: if set, the validator wants DNSSEC.  Without
    363      1.1  christos 	 * 	EDNS, the answer is likely to be useless for this domain.
    364      1.1  christos 	 * @param nocaps: do not use caps_for_id, use the qname as given.
    365      1.1  christos 	 *	(ignored if caps_for_id is disabled).
    366  1.1.1.6  christos 	 * @param check_ratelimit: if set, will check ratelimit before sending out.
    367      1.1  christos 	 * @param addr: where to.
    368      1.1  christos 	 * @param addrlen: length of addr.
    369      1.1  christos 	 * @param zone: delegation point name.
    370      1.1  christos 	 * @param zonelen: length of zone name.
    371  1.1.1.6  christos 	 * @param tcp_upstream: use TCP for upstream queries.
    372  1.1.1.2  christos 	 * @param ssl_upstream: use SSL for upstream queries.
    373  1.1.1.3  christos 	 * @param tls_auth_name: if ssl_upstream, use this name with TLS
    374  1.1.1.3  christos 	 * 	authentication.
    375  1.1.1.6  christos 	 * @param q: which query state to reactivate upon return.
    376  1.1.1.6  christos 	 * @param was_ratelimited: it will signal back if the query failed to pass the
    377  1.1.1.6  christos 	 *	ratelimit check.
    378      1.1  christos 	 * @return: false on failure (memory or socket related). no query was
    379      1.1  christos 	 *	sent. Or returns an outbound entry with qsent and qstate set.
    380      1.1  christos 	 *	This outbound_entry will be used on later module invocations
    381      1.1  christos 	 *	that involve this query (timeout, error or reply).
    382      1.1  christos 	 */
    383  1.1.1.2  christos 	struct outbound_entry* (*send_query)(struct query_info* qinfo,
    384  1.1.1.2  christos 		uint16_t flags, int dnssec, int want_dnssec, int nocaps,
    385  1.1.1.6  christos 		int check_ratelimit,
    386      1.1  christos 		struct sockaddr_storage* addr, socklen_t addrlen,
    387  1.1.1.6  christos 		uint8_t* zone, size_t zonelen, int tcp_upstream, int ssl_upstream,
    388  1.1.1.6  christos 		char* tls_auth_name, struct module_qstate* q, int* was_ratelimited);
    389      1.1  christos 
    390      1.1  christos 	/**
    391      1.1  christos 	 * Detach-subqueries.
    392      1.1  christos 	 * Remove all sub-query references from this query state.
    393      1.1  christos 	 * Keeps super-references of those sub-queries correct.
    394      1.1  christos 	 * Updates stat items in mesh_area structure.
    395      1.1  christos 	 * @param qstate: used to find mesh state.
    396      1.1  christos 	 */
    397      1.1  christos 	void (*detach_subs)(struct module_qstate* qstate);
    398      1.1  christos 
    399      1.1  christos 	/**
    400      1.1  christos 	 * Attach subquery.
    401      1.1  christos 	 * Creates it if it does not exist already.
    402      1.1  christos 	 * Keeps sub and super references correct.
    403      1.1  christos 	 * Updates stat items in mesh_area structure.
    404      1.1  christos 	 * Pass if it is priming query or not.
    405      1.1  christos 	 * return:
    406      1.1  christos 	 * o if error (malloc) happened.
    407      1.1  christos 	 * o need to initialise the new state (module init; it is a new state).
    408      1.1  christos 	 *   so that the next run of the query with this module is successful.
    409      1.1  christos 	 * o no init needed, attachment successful.
    410      1.1  christos 	 *
    411      1.1  christos 	 * @param qstate: the state to find mesh state, and that wants to
    412      1.1  christos 	 * 	receive the results from the new subquery.
    413      1.1  christos 	 * @param qinfo: what to query for (copied).
    414      1.1  christos 	 * @param qflags: what flags to use (RD, CD flag or not).
    415      1.1  christos 	 * @param prime: if it is a (stub) priming query.
    416      1.1  christos 	 * @param valrec: validation lookup recursion, does not need validation
    417      1.1  christos 	 * @param newq: If the new subquery needs initialisation, it is
    418      1.1  christos 	 * 	returned, otherwise NULL is returned.
    419      1.1  christos 	 * @return: false on error, true if success (and init may be needed).
    420      1.1  christos 	 */
    421      1.1  christos 	int (*attach_sub)(struct module_qstate* qstate,
    422      1.1  christos 		struct query_info* qinfo, uint16_t qflags, int prime,
    423      1.1  christos 		int valrec, struct module_qstate** newq);
    424      1.1  christos 
    425      1.1  christos 	/**
    426  1.1.1.2  christos 	 * Add detached query.
    427  1.1.1.2  christos 	 * Creates it if it does not exist already.
    428  1.1.1.2  christos 	 * Does not make super/sub references.
    429  1.1.1.2  christos 	 * Performs a cycle detection - for double check - and fails if there is
    430  1.1.1.2  christos 	 * 	one.
    431  1.1.1.2  christos 	 * Updates stat items in mesh_area structure.
    432  1.1.1.2  christos 	 * Pass if it is priming query or not.
    433  1.1.1.2  christos 	 * return:
    434  1.1.1.2  christos 	 * 	o if error (malloc) happened.
    435  1.1.1.2  christos 	 * 	o need to initialise the new state (module init; it is a new state).
    436  1.1.1.2  christos 	 * 	  so that the next run of the query with this module is successful.
    437  1.1.1.2  christos 	 * 	o no init needed, attachment successful.
    438  1.1.1.2  christos 	 * 	o added subquery, created if it did not exist already.
    439  1.1.1.2  christos 	 *
    440  1.1.1.2  christos 	 * @param qstate: the state to find mesh state, and that wants to receive
    441  1.1.1.2  christos 	 * 	the results from the new subquery.
    442  1.1.1.2  christos 	 * @param qinfo: what to query for (copied).
    443  1.1.1.2  christos 	 * @param qflags: what flags to use (RD / CD flag or not).
    444  1.1.1.2  christos 	 * @param prime: if it is a (stub) priming query.
    445  1.1.1.2  christos 	 * @param valrec: if it is a validation recursion query (lookup of key, DS).
    446  1.1.1.2  christos 	 * @param newq: If the new subquery needs initialisation, it is returned,
    447  1.1.1.2  christos 	 * 	otherwise NULL is returned.
    448  1.1.1.2  christos 	 * @param sub: The added mesh state, created if it did not exist already.
    449  1.1.1.2  christos 	 * @return: false on error, true if success (and init may be needed).
    450  1.1.1.2  christos 	 */
    451  1.1.1.2  christos 	int (*add_sub)(struct module_qstate* qstate,
    452  1.1.1.2  christos 		struct query_info* qinfo, uint16_t qflags, int prime,
    453  1.1.1.2  christos 		int valrec, struct module_qstate** newq,
    454  1.1.1.2  christos 		struct mesh_state** sub);
    455  1.1.1.2  christos 
    456  1.1.1.2  christos 	/**
    457      1.1  christos 	 * Kill newly attached sub. If attach_sub returns newq for
    458      1.1  christos 	 * initialisation, but that fails, then this routine will cleanup and
    459  1.1.1.2  christos 	 * delete the freshly created sub.
    460      1.1  christos 	 * @param newq: the new subquery that is no longer needed.
    461      1.1  christos 	 * 	It is removed.
    462      1.1  christos 	 */
    463      1.1  christos 	void (*kill_sub)(struct module_qstate* newq);
    464      1.1  christos 
    465      1.1  christos 	/**
    466      1.1  christos 	 * Detect if adding a dependency for qstate on name,type,class will
    467      1.1  christos 	 * create a dependency cycle.
    468      1.1  christos 	 * @param qstate: given mesh querystate.
    469      1.1  christos 	 * @param qinfo: query info for dependency.
    470      1.1  christos 	 * @param flags: query flags of dependency, RD/CD flags.
    471      1.1  christos 	 * @param prime: if dependency is a priming query or not.
    472      1.1  christos 	 * @param valrec: validation lookup recursion, does not need validation
    473      1.1  christos 	 * @return true if the name,type,class exists and the given
    474      1.1  christos 	 * 	qstate mesh exists as a dependency of that name. Thus
    475      1.1  christos 	 * 	if qstate becomes dependent on name,type,class then a
    476      1.1  christos 	 * 	cycle is created.
    477      1.1  christos 	 */
    478      1.1  christos 	int (*detect_cycle)(struct module_qstate* qstate,
    479      1.1  christos 		struct query_info* qinfo, uint16_t flags, int prime,
    480      1.1  christos 		int valrec);
    481      1.1  christos 
    482      1.1  christos 	/** region for temporary usage. May be cleared after operate() call. */
    483      1.1  christos 	struct regional* scratch;
    484      1.1  christos 	/** buffer for temporary usage. May be cleared after operate() call. */
    485      1.1  christos 	struct sldns_buffer* scratch_buffer;
    486      1.1  christos 	/** internal data for daemon - worker thread. */
    487      1.1  christos 	struct worker* worker;
    488  1.1.1.3  christos 	/** the worker event base */
    489  1.1.1.3  christos 	struct comm_base* worker_base;
    490  1.1.1.3  christos 	/** the outside network */
    491  1.1.1.3  christos 	struct outside_network* outnet;
    492      1.1  christos 	/** mesh area with query state dependencies */
    493      1.1  christos 	struct mesh_area* mesh;
    494      1.1  christos 	/** allocation service */
    495      1.1  christos 	struct alloc_cache* alloc;
    496      1.1  christos 	/** random table to generate random numbers */
    497      1.1  christos 	struct ub_randstate* rnd;
    498      1.1  christos 	/** time in seconds, converted to integer */
    499      1.1  christos 	time_t* now;
    500      1.1  christos 	/** time in microseconds. Relatively recent. */
    501      1.1  christos 	struct timeval* now_tv;
    502      1.1  christos 	/** is validation required for messages, controls client-facing
    503      1.1  christos 	 * validation status (AD bits) and servfails */
    504      1.1  christos 	int need_to_validate;
    505      1.1  christos 	/** trusted key storage; these are the configured keys, if not NULL,
    506      1.1  christos 	 * otherwise configured by validator. These are the trust anchors,
    507      1.1  christos 	 * and are not primed and ready for validation, but on the bright
    508      1.1  christos 	 * side, they are read only memory, thus no locks and fast. */
    509      1.1  christos 	struct val_anchors* anchors;
    510      1.1  christos 	/** negative cache, configured by the validator. if not NULL,
    511      1.1  christos 	 * contains NSEC record lookup trees. */
    512      1.1  christos 	struct val_neg_cache* neg_cache;
    513      1.1  christos 	/** the 5011-probe timer (if any) */
    514      1.1  christos 	struct comm_timer* probe_timer;
    515  1.1.1.3  christos 	/** auth zones */
    516  1.1.1.3  christos 	struct auth_zones* auth_zones;
    517      1.1  christos 	/** Mapping of forwarding zones to targets.
    518  1.1.1.8  christos 	 * iterator forwarder information. */
    519      1.1  christos 	struct iter_forwards* fwds;
    520      1.1  christos 	/**
    521  1.1.1.8  christos 	 * iterator stub information.
    522      1.1  christos 	 * The hints -- these aren't stored in the cache because they don't
    523      1.1  christos 	 * expire. The hints are always used to "prime" the cache. Note
    524      1.1  christos 	 * that both root hints and stub zone "hints" are stored in this
    525      1.1  christos 	 * data structure.
    526      1.1  christos 	 */
    527      1.1  christos 	struct iter_hints* hints;
    528  1.1.1.8  christos 	/** views structure containing view tree */
    529  1.1.1.8  christos 	struct views* views;
    530  1.1.1.8  christos 	/** response-ip set with associated actions and tags. */
    531  1.1.1.8  christos 	struct respip_set* respip_set;
    532      1.1  christos 	/** module specific data. indexed by module id. */
    533      1.1  christos 	void* modinfo[MAX_MODULE];
    534  1.1.1.2  christos 
    535  1.1.1.2  christos 	/* Shared linked list of inplace callback functions */
    536  1.1.1.2  christos 	struct inplace_cb* inplace_cb_lists[inplace_cb_types_total];
    537  1.1.1.2  christos 
    538  1.1.1.2  christos 	/**
    539  1.1.1.2  christos 	 * Shared array of known edns options (size MAX_KNOWN_EDNS_OPTS).
    540  1.1.1.2  christos 	 * Filled by edns literate modules during init.
    541  1.1.1.2  christos 	 */
    542  1.1.1.2  christos 	struct edns_known_option* edns_known_options;
    543  1.1.1.2  christos 	/* Number of known edns options */
    544  1.1.1.2  christos 	size_t edns_known_options_num;
    545  1.1.1.5  christos 	/** EDNS client string information */
    546  1.1.1.5  christos 	struct edns_strings* edns_strings;
    547  1.1.1.2  christos 
    548  1.1.1.8  christos 	/** module stack */
    549  1.1.1.8  christos 	struct module_stack* modstack;
    550  1.1.1.8  christos #ifdef USE_CACHEDB
    551  1.1.1.8  christos 	/** the cachedb enabled value, copied and stored here. */
    552  1.1.1.8  christos 	int cachedb_enabled;
    553  1.1.1.8  christos #endif
    554  1.1.1.2  christos 	/* Make every mesh state unique, do not aggregate mesh states. */
    555  1.1.1.2  christos 	int unique_mesh;
    556      1.1  christos };
    557      1.1  christos 
    558      1.1  christos /**
    559      1.1  christos  * External visible states of the module state machine
    560      1.1  christos  * Modules may also have an internal state.
    561      1.1  christos  * Modules are supposed to run to completion or until blocked.
    562      1.1  christos  */
    563      1.1  christos enum module_ext_state {
    564      1.1  christos 	/** initial state - new query */
    565      1.1  christos 	module_state_initial = 0,
    566      1.1  christos 	/** waiting for reply to outgoing network query */
    567      1.1  christos 	module_wait_reply,
    568      1.1  christos 	/** module is waiting for another module */
    569      1.1  christos 	module_wait_module,
    570      1.1  christos 	/** module is waiting for another module; that other is restarted */
    571      1.1  christos 	module_restart_next,
    572      1.1  christos 	/** module is waiting for sub-query */
    573      1.1  christos 	module_wait_subquery,
    574      1.1  christos 	/** module could not finish the query */
    575      1.1  christos 	module_error,
    576      1.1  christos 	/** module is finished with query */
    577      1.1  christos 	module_finished
    578      1.1  christos };
    579      1.1  christos 
    580      1.1  christos /**
    581      1.1  christos  * Events that happen to modules, that start or wakeup modules.
    582      1.1  christos  */
    583      1.1  christos enum module_ev {
    584      1.1  christos 	/** new query */
    585      1.1  christos 	module_event_new = 0,
    586      1.1  christos 	/** query passed by other module */
    587      1.1  christos 	module_event_pass,
    588      1.1  christos 	/** reply inbound from server */
    589      1.1  christos 	module_event_reply,
    590      1.1  christos 	/** no reply, timeout or other error */
    591      1.1  christos 	module_event_noreply,
    592      1.1  christos 	/** reply is there, but capitalisation check failed */
    593      1.1  christos 	module_event_capsfail,
    594      1.1  christos 	/** next module is done, and its reply is awaiting you */
    595      1.1  christos 	module_event_moddone,
    596      1.1  christos 	/** error */
    597      1.1  christos 	module_event_error
    598      1.1  christos };
    599      1.1  christos 
    600      1.1  christos /**
    601      1.1  christos  * Linked list of sockaddrs
    602      1.1  christos  * May be allocated such that only 'len' bytes of addr exist for the structure.
    603      1.1  christos  */
    604      1.1  christos struct sock_list {
    605      1.1  christos 	/** next in list */
    606      1.1  christos 	struct sock_list* next;
    607      1.1  christos 	/** length of addr */
    608      1.1  christos 	socklen_t len;
    609      1.1  christos 	/** sockaddr */
    610      1.1  christos 	struct sockaddr_storage addr;
    611      1.1  christos };
    612      1.1  christos 
    613  1.1.1.2  christos struct respip_action_info;
    614  1.1.1.2  christos 
    615      1.1  christos /**
    616  1.1.1.5  christos  * Struct to hold relevant data for serve expired
    617  1.1.1.5  christos  */
    618  1.1.1.5  christos struct serve_expired_data {
    619  1.1.1.5  christos 	struct comm_timer* timer;
    620  1.1.1.5  christos 	serve_expired_lookup_func_type* get_cached_answer;
    621  1.1.1.5  christos };
    622  1.1.1.5  christos 
    623  1.1.1.5  christos /**
    624      1.1  christos  * Module state, per query.
    625      1.1  christos  */
    626      1.1  christos struct module_qstate {
    627      1.1  christos 	/** which query is being answered: name, type, class */
    628      1.1  christos 	struct query_info qinfo;
    629      1.1  christos 	/** flags uint16 from query */
    630      1.1  christos 	uint16_t query_flags;
    631      1.1  christos 	/** if this is a (stub or root) priming query (with hints) */
    632      1.1  christos 	int is_priming;
    633      1.1  christos 	/** if this is a validation recursion query that does not get
    634      1.1  christos 	 * validation itself */
    635      1.1  christos 	int is_valrec;
    636  1.1.1.7  christos #ifdef CLIENT_SUBNET
    637  1.1.1.7  christos 	/** the client network address is needed for the client-subnet option
    638  1.1.1.7  christos 	 *  when prefetching, but we can't use reply_list in mesh_info, because
    639  1.1.1.7  christos 	 *  we don't want to send a reply for the internal query. */
    640  1.1.1.7  christos         struct sockaddr_storage client_addr;
    641  1.1.1.7  christos #endif
    642      1.1  christos 
    643      1.1  christos 	/** comm_reply contains server replies */
    644      1.1  christos 	struct comm_reply* reply;
    645      1.1  christos 	/** the reply message, with message for client and calling module */
    646      1.1  christos 	struct dns_msg* return_msg;
    647      1.1  christos 	/** the rcode, in case of error, instead of a reply message */
    648      1.1  christos 	int return_rcode;
    649      1.1  christos 	/** origin of the reply (can be NULL from cache, list for cnames) */
    650      1.1  christos 	struct sock_list* reply_origin;
    651      1.1  christos 	/** IP blacklist for queries */
    652      1.1  christos 	struct sock_list* blacklist;
    653      1.1  christos 	/** region for this query. Cleared when query process finishes. */
    654      1.1  christos 	struct regional* region;
    655      1.1  christos 	/** failure reason information if val-log-level is high */
    656  1.1.1.6  christos 	struct errinf_strlist* errinf;
    657      1.1  christos 	/** which module is executing */
    658      1.1  christos 	int curmod;
    659      1.1  christos 	/** module states */
    660      1.1  christos 	enum module_ext_state ext_state[MAX_MODULE];
    661      1.1  christos 	/** module specific data for query. indexed by module id. */
    662      1.1  christos 	void* minfo[MAX_MODULE];
    663      1.1  christos 	/** environment for this query */
    664      1.1  christos 	struct module_env* env;
    665      1.1  christos 	/** mesh related information for this query */
    666      1.1  christos 	struct mesh_state* mesh_info;
    667      1.1  christos 	/** how many seconds before expiry is this prefetched (0 if not) */
    668      1.1  christos 	time_t prefetch_leeway;
    669  1.1.1.5  christos 	/** serve expired data */
    670  1.1.1.5  christos 	struct serve_expired_data* serve_expired_data;
    671  1.1.1.2  christos 
    672  1.1.1.2  christos 	/** incoming edns options from the front end */
    673  1.1.1.2  christos 	struct edns_option* edns_opts_front_in;
    674  1.1.1.2  christos 	/** outgoing edns options to the back end */
    675  1.1.1.2  christos 	struct edns_option* edns_opts_back_out;
    676  1.1.1.2  christos 	/** incoming edns options from the back end */
    677  1.1.1.2  christos 	struct edns_option* edns_opts_back_in;
    678  1.1.1.2  christos 	/** outgoing edns options to the front end */
    679  1.1.1.2  christos 	struct edns_option* edns_opts_front_out;
    680  1.1.1.2  christos 	/** whether modules should answer from the cache */
    681  1.1.1.2  christos 	int no_cache_lookup;
    682  1.1.1.2  christos 	/** whether modules should store answer in the cache */
    683  1.1.1.2  christos 	int no_cache_store;
    684  1.1.1.2  christos 	/** whether to refetch a fresh answer on finishing this state*/
    685  1.1.1.2  christos 	int need_refetch;
    686  1.1.1.4  christos 	/** whether the query (or a subquery) was ratelimited */
    687  1.1.1.4  christos 	int was_ratelimited;
    688  1.1.1.6  christos 	/** time when query was started. This is when the qstate is created.
    689  1.1.1.6  christos 	 * This is used so that type NS data cannot be overwritten by them
    690  1.1.1.6  christos 	 * expiring while the lookup is in progress, using data fetched from
    691  1.1.1.6  christos 	 * those servers. By comparing expiry time with qstarttime for type NS.
    692  1.1.1.6  christos 	 */
    693  1.1.1.6  christos 	time_t qstarttime;
    694  1.1.1.7  christos 	/** whether a message from cachedb will be used for the reply */
    695  1.1.1.7  christos 	int is_cachedb_answer;
    696  1.1.1.2  christos 
    697  1.1.1.2  christos 	/**
    698  1.1.1.2  christos 	 * Attributes of clients that share the qstate that may affect IP-based
    699  1.1.1.2  christos 	 * actions.
    700  1.1.1.2  christos 	 */
    701  1.1.1.2  christos 	struct respip_client_info* client_info;
    702  1.1.1.2  christos 
    703  1.1.1.2  christos 	/** Extended result of response-ip action processing, mainly
    704  1.1.1.2  christos 	 *  for logging purposes. */
    705  1.1.1.2  christos 	struct respip_action_info* respip_action_info;
    706  1.1.1.8  christos 	/** if the query has been modified by rpz processing. */
    707  1.1.1.8  christos 	int rpz_applied;
    708  1.1.1.6  christos 	/** if the query is rpz passthru, no further rpz processing for it */
    709  1.1.1.6  christos 	int rpz_passthru;
    710  1.1.1.7  christos 	/* Flag tcp required. */
    711  1.1.1.7  christos 	int tcp_required;
    712  1.1.1.2  christos 
    713  1.1.1.2  christos 	/** whether the reply should be dropped */
    714  1.1.1.2  christos 	int is_drop;
    715      1.1  christos };
    716      1.1  christos 
    717      1.1  christos /**
    718      1.1  christos  * Module functionality block
    719      1.1  christos  */
    720      1.1  christos struct module_func_block {
    721      1.1  christos 	/** text string name of module */
    722      1.1  christos 	const char* name;
    723      1.1  christos 
    724  1.1.1.8  christos 	/**
    725  1.1.1.8  christos 	 * Set up the module for start. This is called only once at startup.
    726  1.1.1.8  christos 	 * Privileged operations like opening device files may be done here.
    727  1.1.1.8  christos 	 * The function ptr can be NULL, if it is not used.
    728  1.1.1.8  christos 	 * @param env: module environment.
    729  1.1.1.8  christos 	 * @param id: module id number.
    730  1.1.1.8  christos 	 * return: 0 on error
    731  1.1.1.8  christos 	 */
    732  1.1.1.8  christos 	int (*startup)(struct module_env* env, int id);
    733  1.1.1.8  christos 
    734  1.1.1.8  christos 	/**
    735  1.1.1.8  christos 	 * Close down the module for stop. This is called only once before
    736  1.1.1.8  christos 	 * shutdown to free resources allocated during startup().
    737  1.1.1.8  christos 	 * Closing privileged ports or files must be done here.
    738  1.1.1.8  christos 	 * The function ptr can be NULL, if it is not used.
    739  1.1.1.8  christos 	 * @param env: module environment.
    740  1.1.1.8  christos 	 * @param id: module id number.
    741  1.1.1.8  christos 	 */
    742  1.1.1.8  christos 	void (*destartup)(struct module_env* env, int id);
    743  1.1.1.8  christos 
    744  1.1.1.8  christos 	/**
    745  1.1.1.8  christos 	 * Initialise the module. Called when restarting or reloading the
    746  1.1.1.8  christos 	 * daemon.
    747      1.1  christos 	 * This is the place to apply settings from the config file.
    748      1.1  christos 	 * @param env: module environment.
    749      1.1  christos 	 * @param id: module id number.
    750      1.1  christos 	 * return: 0 on error
    751      1.1  christos 	 */
    752      1.1  christos 	int (*init)(struct module_env* env, int id);
    753      1.1  christos 
    754      1.1  christos 	/**
    755  1.1.1.8  christos 	 * Deinitialise the module, undo stuff done during init().
    756  1.1.1.8  christos 	 * Called before reloading the daemon.
    757      1.1  christos 	 * @param env: module environment.
    758      1.1  christos 	 * @param id: module id number.
    759      1.1  christos 	 */
    760      1.1  christos 	void (*deinit)(struct module_env* env, int id);
    761      1.1  christos 
    762      1.1  christos 	/**
    763      1.1  christos 	 * accept a new query, or work further on existing query.
    764      1.1  christos 	 * Changes the qstate->ext_state to be correct on exit.
    765      1.1  christos 	 * @param ev: event that causes the module state machine to
    766      1.1  christos 	 *	(re-)activate.
    767      1.1  christos 	 * @param qstate: the query state.
    768      1.1  christos 	 *	Note that this method is not allowed to change the
    769      1.1  christos 	 *	query state 'identity', that is query info, qflags,
    770      1.1  christos 	 *	and priming status.
    771      1.1  christos 	 *	Attach a subquery to get results to a different query.
    772      1.1  christos 	 * @param id: module id number that operate() is called on.
    773      1.1  christos 	 * @param outbound: if not NULL this event is due to the reply/timeout
    774      1.1  christos 	 *	or error on this outbound query.
    775      1.1  christos 	 * @return: if at exit the ext_state is:
    776      1.1  christos 	 *	o wait_module: next module is started. (with pass event).
    777      1.1  christos 	 *	o error or finished: previous module is resumed.
    778      1.1  christos 	 *	o otherwise it waits until that event happens (assumes
    779      1.1  christos 	 *	  the service routine to make subrequest or send message
    780      1.1  christos 	 *	  have been called.
    781      1.1  christos 	 */
    782      1.1  christos 	void (*operate)(struct module_qstate* qstate, enum module_ev event,
    783      1.1  christos 		int id, struct outbound_entry* outbound);
    784      1.1  christos 
    785      1.1  christos 	/**
    786      1.1  christos 	 * inform super querystate about the results from this subquerystate.
    787      1.1  christos 	 * Is called when the querystate is finished.  The method invoked is
    788      1.1  christos 	 * the one from the current module active in the super querystate.
    789      1.1  christos 	 * @param qstate: the query state that is finished.
    790      1.1  christos 	 *	Examine return_rcode and return_reply in the qstate.
    791      1.1  christos 	 * @param id: module id for this module.
    792      1.1  christos 	 *	This coincides with the current module for the super qstate.
    793      1.1  christos 	 * @param super: the super querystate that needs to be informed.
    794      1.1  christos 	 */
    795      1.1  christos 	void (*inform_super)(struct module_qstate* qstate, int id,
    796      1.1  christos 		struct module_qstate* super);
    797      1.1  christos 
    798      1.1  christos 	/**
    799      1.1  christos 	 * clear module specific data
    800      1.1  christos 	 */
    801      1.1  christos 	void (*clear)(struct module_qstate* qstate, int id);
    802      1.1  christos 
    803      1.1  christos 	/**
    804      1.1  christos 	 * How much memory is the module specific data using.
    805      1.1  christos 	 * @param env: module environment.
    806      1.1  christos 	 * @param id: the module id.
    807      1.1  christos 	 * @return the number of bytes that are alloced.
    808      1.1  christos 	 */
    809      1.1  christos 	size_t (*get_mem)(struct module_env* env, int id);
    810      1.1  christos };
    811      1.1  christos 
    812      1.1  christos /**
    813      1.1  christos  * Debug utility: module external qstate to string
    814      1.1  christos  * @param s: the state value.
    815      1.1  christos  * @return descriptive string.
    816      1.1  christos  */
    817      1.1  christos const char* strextstate(enum module_ext_state s);
    818      1.1  christos 
    819      1.1  christos /**
    820      1.1  christos  * Debug utility: module event to string
    821      1.1  christos  * @param e: the module event value.
    822      1.1  christos  * @return descriptive string.
    823      1.1  christos  */
    824      1.1  christos const char* strmodulevent(enum module_ev e);
    825      1.1  christos 
    826  1.1.1.2  christos /**
    827  1.1.1.6  christos  * Append text to the error info for validation.
    828  1.1.1.6  christos  * @param qstate: query state.
    829  1.1.1.6  christos  * @param str: copied into query region and appended.
    830  1.1.1.6  christos  * Failures to allocate are logged.
    831  1.1.1.6  christos  */
    832  1.1.1.6  christos void errinf(struct module_qstate* qstate, const char* str);
    833  1.1.1.6  christos void errinf_ede(struct module_qstate* qstate, const char* str,
    834  1.1.1.6  christos                 sldns_ede_code reason_bogus);
    835  1.1.1.6  christos 
    836  1.1.1.6  christos /**
    837  1.1.1.6  christos  * Append text to error info:  from 1.2.3.4
    838  1.1.1.6  christos  * @param qstate: query state.
    839  1.1.1.6  christos  * @param origin: sock list with origin of trouble.
    840  1.1.1.6  christos  *  Every element added.
    841  1.1.1.6  christos  *  If NULL: nothing is added.
    842  1.1.1.6  christos  *  if 0len element: 'from cache' is added.
    843  1.1.1.6  christos  */
    844  1.1.1.6  christos void errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
    845  1.1.1.6  christos 
    846  1.1.1.6  christos /**
    847  1.1.1.6  christos  * Append text to error info:  for RRset name type class
    848  1.1.1.6  christos  * @param qstate: query state.
    849  1.1.1.6  christos  * @param rr: rrset_key.
    850  1.1.1.6  christos  */
    851  1.1.1.6  christos void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
    852  1.1.1.6  christos 
    853  1.1.1.6  christos /**
    854  1.1.1.6  christos  * Append text to error info:  str dname
    855  1.1.1.6  christos  * @param qstate: query state.
    856  1.1.1.6  christos  * @param str: explanation string
    857  1.1.1.6  christos  * @param dname: the dname.
    858  1.1.1.6  christos  */
    859  1.1.1.6  christos void errinf_dname(struct module_qstate* qstate, const char* str,
    860  1.1.1.6  christos     uint8_t* dname);
    861  1.1.1.6  christos 
    862  1.1.1.6  christos /**
    863  1.1.1.6  christos  * Create error info in string.  For validation failures.
    864  1.1.1.6  christos  * @param qstate: query state.
    865  1.1.1.8  christos  * @param region: the region for the result or NULL for malloced result.
    866  1.1.1.6  christos  * @return string or NULL on malloc failure (already logged).
    867  1.1.1.8  christos  *    This string is malloced if region is NULL and has to be freed by caller.
    868  1.1.1.6  christos  */
    869  1.1.1.8  christos char* errinf_to_str_bogus(struct module_qstate* qstate, struct regional* region);
    870  1.1.1.7  christos 
    871  1.1.1.6  christos /**
    872  1.1.1.7  christos  * Check the sldns_ede_code of the qstate->errinf.
    873  1.1.1.6  christos  * @param qstate: query state.
    874  1.1.1.7  christos  * @return the latest explicitly set sldns_ede_code or LDNS_EDE_NONE.
    875  1.1.1.6  christos  */
    876  1.1.1.6  christos sldns_ede_code errinf_to_reason_bogus(struct module_qstate* qstate);
    877  1.1.1.6  christos 
    878  1.1.1.6  christos /**
    879  1.1.1.6  christos  * Create error info in string.  For other servfails.
    880  1.1.1.6  christos  * @param qstate: query state.
    881  1.1.1.6  christos  * @return string or NULL on malloc failure (already logged).
    882  1.1.1.6  christos  */
    883  1.1.1.6  christos char* errinf_to_str_servfail(struct module_qstate* qstate);
    884  1.1.1.6  christos 
    885  1.1.1.6  christos /**
    886  1.1.1.7  christos  * Create error info in string.  For misc failures that are not servfail.
    887  1.1.1.7  christos  * @param qstate: query state.
    888  1.1.1.7  christos  * @return string or NULL on malloc failure (already logged).
    889  1.1.1.7  christos  */
    890  1.1.1.7  christos char* errinf_to_str_misc(struct module_qstate* qstate);
    891  1.1.1.7  christos 
    892  1.1.1.7  christos /**
    893  1.1.1.2  christos  * Initialize the edns known options by allocating the required space.
    894  1.1.1.2  christos  * @param env: the module environment.
    895  1.1.1.2  christos  * @return false on failure (no memory).
    896  1.1.1.2  christos  */
    897  1.1.1.2  christos int edns_known_options_init(struct module_env* env);
    898  1.1.1.2  christos 
    899  1.1.1.2  christos /**
    900  1.1.1.2  christos  * Free the allocated space for the known edns options.
    901  1.1.1.2  christos  * @param env: the module environment.
    902  1.1.1.2  christos  */
    903  1.1.1.2  christos void edns_known_options_delete(struct module_env* env);
    904  1.1.1.2  christos 
    905  1.1.1.2  christos /**
    906  1.1.1.2  christos  * Register a known edns option. Overwrite the flags if it is already
    907  1.1.1.2  christos  * registered. Used before creating workers to register known edns options.
    908  1.1.1.2  christos  * @param opt_code: the edns option code.
    909  1.1.1.2  christos  * @param bypass_cache_stage: whether the option interacts with the cache.
    910  1.1.1.2  christos  * @param no_aggregation: whether the option implies more specific
    911  1.1.1.2  christos  *	aggregation.
    912  1.1.1.2  christos  * @param env: the module environment.
    913  1.1.1.2  christos  * @return true on success, false on failure (registering more options than
    914  1.1.1.2  christos  *	allowed or trying to register after the environment is copied to the
    915  1.1.1.2  christos  *	threads.)
    916  1.1.1.2  christos  */
    917  1.1.1.2  christos int edns_register_option(uint16_t opt_code, int bypass_cache_stage,
    918  1.1.1.2  christos 	int no_aggregation, struct module_env* env);
    919  1.1.1.2  christos 
    920  1.1.1.2  christos /**
    921  1.1.1.2  christos  * Register an inplace callback function.
    922  1.1.1.2  christos  * @param cb: pointer to the callback function.
    923  1.1.1.2  christos  * @param type: inplace callback type.
    924  1.1.1.2  christos  * @param cbarg: argument for the callback function, or NULL.
    925  1.1.1.2  christos  * @param env: the module environment.
    926  1.1.1.2  christos  * @param id: module id.
    927  1.1.1.2  christos  * @return true on success, false on failure (out of memory or trying to
    928  1.1.1.2  christos  *	register after the environment is copied to the threads.)
    929  1.1.1.2  christos  */
    930  1.1.1.2  christos int
    931  1.1.1.2  christos inplace_cb_register(void* cb, enum inplace_cb_list_type type, void* cbarg,
    932  1.1.1.2  christos 	struct module_env* env, int id);
    933  1.1.1.2  christos 
    934  1.1.1.2  christos /**
    935  1.1.1.2  christos  * Delete callback for specified type and module id.
    936  1.1.1.2  christos  * @param env: the module environment.
    937  1.1.1.2  christos  * @param type: inplace callback type.
    938  1.1.1.2  christos  * @param id: module id.
    939  1.1.1.2  christos  */
    940  1.1.1.2  christos void
    941  1.1.1.2  christos inplace_cb_delete(struct module_env* env, enum inplace_cb_list_type type,
    942  1.1.1.2  christos 	int id);
    943  1.1.1.2  christos 
    944  1.1.1.2  christos /**
    945  1.1.1.2  christos  * Delete all the inplace callback linked lists.
    946  1.1.1.2  christos  * @param env: the module environment.
    947  1.1.1.2  christos  */
    948  1.1.1.2  christos void inplace_cb_lists_delete(struct module_env* env);
    949  1.1.1.2  christos 
    950  1.1.1.2  christos /**
    951  1.1.1.2  christos  * Check if an edns option is known.
    952  1.1.1.2  christos  * @param opt_code: the edns option code.
    953  1.1.1.2  christos  * @param env: the module environment.
    954  1.1.1.2  christos  * @return pointer to registered option if the edns option is known,
    955  1.1.1.2  christos  *	NULL otherwise.
    956  1.1.1.2  christos  */
    957  1.1.1.2  christos struct edns_known_option* edns_option_is_known(uint16_t opt_code,
    958  1.1.1.2  christos 	struct module_env* env);
    959  1.1.1.2  christos 
    960  1.1.1.2  christos /**
    961  1.1.1.2  christos  * Check if an edns option needs to bypass the reply from cache stage.
    962  1.1.1.2  christos  * @param list: the edns options.
    963  1.1.1.2  christos  * @param env: the module environment.
    964  1.1.1.2  christos  * @return true if an edns option needs to bypass the cache stage,
    965  1.1.1.2  christos  *	false otherwise.
    966  1.1.1.2  christos  */
    967  1.1.1.2  christos int edns_bypass_cache_stage(struct edns_option* list,
    968  1.1.1.2  christos 	struct module_env* env);
    969  1.1.1.2  christos 
    970  1.1.1.2  christos /**
    971  1.1.1.2  christos  * Check if an unique mesh state is required. Might be triggered by EDNS option
    972  1.1.1.2  christos  * or set for the complete env.
    973  1.1.1.2  christos  * @param list: the edns options.
    974  1.1.1.2  christos  * @param env: the module environment.
    975  1.1.1.2  christos  * @return true if an edns option needs a unique mesh state,
    976  1.1.1.2  christos  *	false otherwise.
    977  1.1.1.2  christos  */
    978  1.1.1.2  christos int unique_mesh_state(struct edns_option* list, struct module_env* env);
    979  1.1.1.2  christos 
    980  1.1.1.2  christos /**
    981  1.1.1.2  christos  * Log the known edns options.
    982  1.1.1.2  christos  * @param level: the desired verbosity level.
    983  1.1.1.2  christos  * @param env: the module environment.
    984  1.1.1.2  christos  */
    985  1.1.1.2  christos void log_edns_known_options(enum verbosity_value level,
    986  1.1.1.2  christos 	struct module_env* env);
    987  1.1.1.2  christos 
    988  1.1.1.4  christos /**
    989  1.1.1.4  christos  * Copy state that may have happened in the subquery and is always relevant to
    990  1.1.1.4  christos  * the super.
    991  1.1.1.4  christos  * @param qstate: query state that finished.
    992  1.1.1.4  christos  * @param id: module id.
    993  1.1.1.4  christos  * @param super: the qstate to inform.
    994  1.1.1.4  christos  */
    995  1.1.1.4  christos void copy_state_to_super(struct module_qstate* qstate, int id,
    996  1.1.1.4  christos 	struct module_qstate* super);
    997  1.1.1.4  christos 
    998      1.1  christos #endif /* UTIL_MODULE_H */
    999