Data Structures | Macros | Typedefs | Enumerations | Functions
module.h File Reference

This file contains the interface for DNS handling modules. More...

#include "util/storage/lruhash.h"
#include "util/data/msgreply.h"
#include "util/data/msgparse.h"

Data Structures

struct  edns_known_option
 Known edns option. More...
 
struct  inplace_cb
 Inplace callback list of registered routines to be called. More...
 
struct  module_env
 Module environment. More...
 
struct  sock_list
 Linked list of sockaddrs May be allocated such that only 'len' bytes of addr exist for the structure. More...
 
struct  module_qstate
 Module state, per query. More...
 
struct  module_func_block
 Module functionality block. More...
 

Macros

#define MAX_MODULE   16
 Maximum number of modules in operation.
 
#define MAX_KNOWN_EDNS_OPTS   256
 Maximum number of known edns options.
 

Typedefs

typedef int inplace_cb_reply_func_type(struct query_info *qinfo, struct module_qstate *qstate, struct reply_info *rep, int rcode, struct edns_data *edns, struct edns_option **opt_list_out, struct regional *region, int id, void *callback)
 Inplace callback function called before replying. More...
 
typedef int inplace_cb_query_func_type(struct query_info *qinfo, uint16_t flags, struct module_qstate *qstate, struct sockaddr_storage *addr, socklen_t addrlen, uint8_t *zone, size_t zonelen, struct regional *region, int id, void *callback)
 Inplace callback function called before sending the query to a nameserver. More...
 
typedef int inplace_cb_edns_back_parsed_func_type(struct module_qstate *qstate, int id, void *cb_args)
 Inplace callback function called after parsing edns on query reply. More...
 
typedef int inplace_cb_query_response_func_type(struct module_qstate *qstate, struct dns_msg *response, int id, void *cb_args)
 Inplace callback function called after parsing query response. More...
 

Enumerations

enum  inplace_cb_list_type {
  inplace_cb_reply = 0, inplace_cb_reply_cache, inplace_cb_reply_local, inplace_cb_reply_servfail,
  inplace_cb_query, inplace_cb_query_response, inplace_cb_edns_back_parsed, inplace_cb_types_total
}
 
enum  module_ext_state {
  module_state_initial = 0, module_wait_reply, module_wait_module, module_restart_next,
  module_wait_subquery, module_error, module_finished
}
 External visible states of the module state machine Modules may also have an internal state. More...
 
enum  module_ev {
  module_event_new = 0, module_event_pass, module_event_reply, module_event_noreply,
  module_event_capsfail, module_event_moddone, module_event_error
}
 Events that happen to modules, that start or wakeup modules. More...
 

Functions

const char * strextstate (enum module_ext_state s)
 Debug utility: module external qstate to string. More...
 
const char * strmodulevent (enum module_ev e)
 Debug utility: module event to string. More...
 
int edns_known_options_init (struct module_env *env)
 Initialize the edns known options by allocating the required space. More...
 
void edns_known_options_delete (struct module_env *env)
 Free the allocated space for the known edns options. More...
 
int edns_register_option (uint16_t opt_code, int bypass_cache_stage, int no_aggregation, struct module_env *env)
 Register a known edns option. More...
 
int inplace_cb_register (void *cb, enum inplace_cb_list_type type, void *cbarg, struct module_env *env, int id)
 Register an inplace callback function. More...
 
void inplace_cb_delete (struct module_env *env, enum inplace_cb_list_type type, int id)
 Delete callback for specified type and module id. More...
 
void inplace_cb_lists_delete (struct module_env *env)
 Delete all the inplace callback linked lists. More...
 
struct edns_known_optionedns_option_is_known (uint16_t opt_code, struct module_env *env)
 Check if an edns option is known. More...
 
int edns_bypass_cache_stage (struct edns_option *list, struct module_env *env)
 Check if an edns option needs to bypass the reply from cache stage. More...
 
int unique_mesh_state (struct edns_option *list, struct module_env *env)
 Check if an unique mesh state is required. More...
 
void log_edns_known_options (enum verbosity_value level, struct module_env *env)
 Log the known edns options. More...
 

Detailed Description

This file contains the interface for DNS handling modules.

The module interface uses the DNS modules as state machines. The state machines are activated in sequence to operate on queries. Once they are done, the reply is passed back. In the usual setup the mesh is the caller of the state machines and once things are done sends replies and invokes result callbacks.

The module provides a number of functions, listed in the module_func_block. The module is inited and destroyed and memory usage queries, for the module as a whole, for entire-module state (such as a cache). And per-query functions are called, operate to move the state machine and cleanup of the per-query state.

Most per-query state should simply be allocated in the query region. This is destroyed at the end of the query.

The module environment contains services and information and caches shared by the modules and the rest of the system. It also contains function pointers for module-specific tasks (like sending queries).

*** Example module calls for a normal query

In this example, the query does not need recursion, all the other data can be found in the cache. This makes the example shorter.

At the start of the program the iterator module is initialised. The iterator module sets up its global state, such as donotquery lists and private address trees.

A query comes in, and a mesh entry is created for it. The mesh starts the resolution process. The validator module is the first in the list of modules, and it is started on this new query. The operate() function is called. The validator decides it needs not do anything yet until there is a result and returns wait_module, that causes the next module in the list to be started.

The next module is the iterator. It is started on the passed query and decides to perform a lookup. For this simple example, the delegation point information is available, and all the iterator wants to do is send a UDP query. The iterator uses env.send_query() to send the query. Then the iterator suspends (returns from the operate call).

When the UDP reply comes back (and on errors and timeouts), the operate function is called for the query, on the iterator module, with the event that there is a reply. The iterator decides that this is enough, the work is done. It returns the value finished from the operate call, which causes the previous module to be started.

The previous module, the validator module, is started with the event that the iterator module is done. The validator decides to validate the query. Once it is done (which could take recursive lookups, but in this example no recursive lookups are needed), it returns from the operate function with finished.

There is no previous module from the validator module, and the mesh takes this to mean that the query is finally done. The mesh invokes callbacks and sends packets to queriers.

If other modules had been waiting (recursively) on the answer to this query, then the mesh will tell them about it. It calls the inform_super routine on all the waiting modules, and once that is done it calls all of them with the operate() call. During inform_super the query that is done still exists and information can be copied from it (but the module should not really re-entry codepoints and services). During the operate call the modules can use stored state to continue operation with the results. (network buffers are used to contain the answer packet during the inform_super phase, but after that the network buffers will be cleared of their contents so that other tasks can be performed).

*** Example module calls for recursion

A module is called in operate, and it decides that it wants to perform recursion. That is, it wants the full state-machine-list to operate on a different query. It calls env.attach_sub() to create a new query state. The routine returns the newly created state, and potentially the module can edit the module-states for the newly created query (i.e. pass along some information, like delegation points). The module then suspends, returns from the operate routine.

The mesh meanwhile will have the newly created query (or queries) on a waiting list, and will call operate() on this query (or queries). It starts again at the start of the module list for them. The query (or queries) continue to operate their state machines, until they are done. When they are done the mesh calls inform_super on the module that wanted the recursion. After that the mesh calls operate() on the module that wanted to do the recursion, and during this phase the module could, for example, decide to create more recursions.

If the module decides it no longer wants the recursive information it can call detach_subs. Those queries will still run to completion, potentially filling the cache with information. Inform_super is not called any more.

The iterator module will fetch items from the cache, so a recursion attempt may complete very quickly if the item is in cache. The calling module has to wait for completion or eventual timeout. A recursive query that times out returns a servfail rcode (servfail is also returned for other errors during the lookup).

Results are passed in the qstate, the rcode member is used to pass errors without requiring memory allocation, so that the code can continue in out-of-memory conditions. If the rcode member is 0 (NOERROR) then the dns_msg entry contains a filled out message. This message may also contain an rcode that is nonzero, but in this case additional information (query, additional) can be passed along.

The rcode and dns_msg are used to pass the result from the the rightmost module towards the leftmost modules and then towards the user.

If you want to avoid recursion-cycles where queries need other queries that need the first one, use detect_cycle() to see if that will happen.

Typedef Documentation

◆ inplace_cb_reply_func_type

typedef int inplace_cb_reply_func_type(struct query_info *qinfo, struct module_qstate *qstate, struct reply_info *rep, int rcode, struct edns_data *edns, struct edns_option **opt_list_out, struct regional *region, int id, void *callback)

Inplace callback function called before replying.

Called as func(edns, qstate, opt_list_out, qinfo, reply_info, rcode, region, python_callback) Where: qinfo: the query info. qstate: the module state. NULL when calling before the query reaches the mesh states. rep: reply_info. Could be NULL. rcode: the return code. edns: the edns_data of the reply. When qstate is NULL, it is also used as the edns input. opt_list_out: the edns options list for the reply. region: region to store data. python_callback: only used for registering a python callback function.

◆ inplace_cb_query_func_type

typedef int inplace_cb_query_func_type(struct query_info *qinfo, uint16_t flags, struct module_qstate *qstate, struct sockaddr_storage *addr, socklen_t addrlen, uint8_t *zone, size_t zonelen, struct regional *region, int id, void *callback)

Inplace callback function called before sending the query to a nameserver.

Called as func(qinfo, flags, qstate, addr, addrlen, zone, zonelen, region, python_callback) Where: qinfo: query info. flags: flags of the query. qstate: query state. addr: to which server to send the query. addrlen: length of addr. zone: name of the zone of the delegation point. wireformat dname. This is the delegation point name for which the server is deemed authoritative. zonelen: length of zone. region: region to store data. python_callback: only used for registering a python callback function.

◆ inplace_cb_edns_back_parsed_func_type

typedef int inplace_cb_edns_back_parsed_func_type(struct module_qstate *qstate, int id, void *cb_args)

Inplace callback function called after parsing edns on query reply.

Called as func(qstate, cb_args) Where: qstate: the query state id: module id cb_args: argument passed when registering callback.

◆ inplace_cb_query_response_func_type

typedef int inplace_cb_query_response_func_type(struct module_qstate *qstate, struct dns_msg *response, int id, void *cb_args)

Inplace callback function called after parsing query response.

Called as func(qstate, id, cb_args) Where: qstate: the query state response: query response id: module id cb_args: argument passed when registering callback.

Enumeration Type Documentation

◆ module_ext_state

External visible states of the module state machine Modules may also have an internal state.

Modules are supposed to run to completion or until blocked.

Enumerator
module_state_initial 

initial state - new query

module_wait_reply 

waiting for reply to outgoing network query

module_wait_module 

module is waiting for another module

module_restart_next 

module is waiting for another module; that other is restarted

module_wait_subquery 

module is waiting for sub-query

module_error 

module could not finish the query

module_finished 

module is finished with query

◆ module_ev

enum module_ev

Events that happen to modules, that start or wakeup modules.

Enumerator
module_event_new 

new query

module_event_pass 

query passed by other module

module_event_reply 

reply inbound from server

module_event_noreply 

no reply, timeout or other error

module_event_capsfail 

reply is there, but capitalisation check failed

module_event_moddone 

next module is done, and its reply is awaiting you

module_event_error 

error

Function Documentation

◆ strextstate()

const char* strextstate ( enum module_ext_state  s)

Debug utility: module external qstate to string.

Parameters
sthe state value.
Returns
descriptive string.

References module_error, module_finished, module_restart_next, module_state_initial, module_wait_module, module_wait_reply, and module_wait_subquery.

Referenced by dns64_operate(), get_mesh_status(), iter_operate(), mesh_run(), and val_operate().

◆ strmodulevent()

const char* strmodulevent ( enum module_ev  e)

Debug utility: module event to string.

Parameters
ethe module event value.
Returns
descriptive string.

References module_event_capsfail, module_event_error, module_event_moddone, module_event_new, module_event_noreply, module_event_pass, and module_event_reply.

Referenced by dns64_operate(), iter_operate(), pylib::setTTL(), and val_operate().

◆ edns_known_options_init()

int edns_known_options_init ( struct module_env env)

Initialize the edns known options by allocating the required space.

Parameters
envthe module environment.
Returns
false on failure (no memory).

References module_env::edns_known_options, and MAX_KNOWN_EDNS_OPTS.

Referenced by ub_ctx_create_nopipe().

◆ edns_known_options_delete()

void edns_known_options_delete ( struct module_env env)

Free the allocated space for the known edns options.

Parameters
envthe module environment.

References module_env::edns_known_options.

Referenced by daemon_delete(), ub_ctx_create(), and ub_ctx_delete().

◆ edns_register_option()

int edns_register_option ( uint16_t  opt_code,
int  bypass_cache_stage,
int  no_aggregation,
struct module_env env 
)

Register a known edns option.

Overwrite the flags if it is already registered. Used before creating workers to register known edns options.

Parameters
opt_codethe edns option code.
bypass_cache_stagewhether the option interacts with the cache.
no_aggregationwhether the option implies more specific aggregation.
envthe module environment.
Returns
true on success, false on failure (registering more options than allowed or trying to register after the environment is copied to the threads.)

Checking if we are full first is faster but it does not provide the option to change the flags when the array is full. It only impacts unbound initialization, leave it for now.

References edns_known_option::bypass_cache_stage, module_env::edns_known_options, log_err(), MAX_KNOWN_EDNS_OPTS, edns_known_option::no_aggregation, edns_known_option::opt_code, and module_env::worker.

◆ inplace_cb_register()

int inplace_cb_register ( void *  cb,
enum inplace_cb_list_type  type,
void *  cbarg,
struct module_env env,
int  id 
)

Register an inplace callback function.

Parameters
cbpointer to the callback function.
typeinplace callback type.
cbargargument for the callback function, or NULL.
envthe module environment.
idmodule id.
Returns
true on success, false on failure (out of memory or trying to register after the environment is copied to the threads.)

References inplace_cb::cb, inplace_cb::id, log_err(), inplace_cb::next, and module_env::worker.

◆ inplace_cb_delete()

void inplace_cb_delete ( struct module_env env,
enum inplace_cb_list_type  type,
int  id 
)

Delete callback for specified type and module id.

Parameters
envthe module environment.
typeinplace callback type.
idmodule id.

References inplace_cb::id, and inplace_cb::next.

◆ inplace_cb_lists_delete()

void inplace_cb_lists_delete ( struct module_env env)

Delete all the inplace callback linked lists.

Parameters
envthe module environment.

◆ edns_option_is_known()

struct edns_known_option* edns_option_is_known ( uint16_t  opt_code,
struct module_env env 
)

Check if an edns option is known.

Parameters
opt_codethe edns option code.
envthe module environment.
Returns
pointer to registered option if the edns option is known, NULL otherwise.

References module_env::edns_known_options, and edns_known_option::opt_code.

◆ edns_bypass_cache_stage()

int edns_bypass_cache_stage ( struct edns_option list,
struct module_env env 
)

Check if an edns option needs to bypass the reply from cache stage.

Parameters
listthe edns options.
envthe module environment.
Returns
true if an edns option needs to bypass the cache stage, false otherwise.

References edns_known_option::bypass_cache_stage, module_env::edns_known_options, edns_option::next, edns_known_option::opt_code, and edns_option::opt_code.

◆ unique_mesh_state()

int unique_mesh_state ( struct edns_option list,
struct module_env env 
)

Check if an unique mesh state is required.

Might be triggered by EDNS option or set for the complete env.

Parameters
listthe edns options.
envthe module environment.
Returns
true if an edns option needs a unique mesh state, false otherwise.

References module_env::edns_known_options, edns_option::next, edns_known_option::no_aggregation, edns_known_option::opt_code, and edns_option::opt_code.

Referenced by mesh_new_callback(), and mesh_new_client().

◆ log_edns_known_options()

void log_edns_known_options ( enum verbosity_value  level,
struct module_env env 
)

Log the known edns options.

Parameters
levelthe desired verbosity level.
envthe module environment.

References edns_known_option::bypass_cache_stage, module_env::edns_known_options, edns_known_option::no_aggregation, edns_known_option::opt_code, sldns_wire2str_edns_option_code_print(), verbose(), and verbosity.

Referenced by context_finalize().