Electroneum
module.h File Reference
Include dependency graph for module.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  edns_known_option
 
struct  inplace_cb
 
struct  module_env
 
struct  sock_list
 
struct  module_qstate
 
struct  module_func_block
 

Macros

#define MAX_MODULE   16
 
#define MAX_KNOWN_EDNS_OPTS   256
 

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)
 
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)
 
typedef int inplace_cb_edns_back_parsed_func_type(struct module_qstate *qstate, int id, void *cb_args)
 
typedef int inplace_cb_query_response_func_type(struct module_qstate *qstate, struct dns_msg *response, int id, void *cb_args)
 

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
}
 
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
}
 

Functions

const char * strextstate (enum module_ext_state s)
 
const char * strmodulevent (enum module_ev e)
 
int edns_known_options_init (struct module_env *env)
 
void edns_known_options_delete (struct module_env *env)
 
int edns_register_option (uint16_t opt_code, int bypass_cache_stage, int no_aggregation, struct module_env *env)
 
int inplace_cb_register (void *cb, enum inplace_cb_list_type type, void *cbarg, struct module_env *env, int id)
 
void inplace_cb_delete (struct module_env *env, enum inplace_cb_list_type type, int id)
 
void inplace_cb_lists_delete (struct module_env *env)
 
struct edns_known_optionedns_option_is_known (uint16_t opt_code, struct module_env *env)
 
int edns_bypass_cache_stage (struct edns_option *list, struct module_env *env)
 
int unique_mesh_state (struct edns_option *list, struct module_env *env)
 
void log_edns_known_options (enum verbosity_value level, struct module_env *env)
 

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.

Definition in file module.h.

Macro Definition Documentation

◆ MAX_KNOWN_EDNS_OPTS

#define MAX_KNOWN_EDNS_OPTS   256

Maximum number of known edns options

Definition at line 188 of file module.h.

◆ MAX_MODULE

#define MAX_MODULE   16

Maximum number of modules in operation

Definition at line 185 of file module.h.

Typedef Documentation

◆ 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.

Definition at line 288 of file module.h.

◆ 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.

Definition at line 275 of file module.h.

◆ 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.

Definition at line 300 of file module.h.

◆ 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.

Definition at line 253 of file module.h.

Enumeration Type Documentation

◆ inplace_cb_list_type

Enumerator
inplace_cb_reply 
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 

Definition at line 190 of file module.h.

190  {
191  /* Inplace callbacks for when a resolved reply is ready to be sent to the
192  * front.*/
193  inplace_cb_reply = 0,
194  /* Inplace callbacks for when a reply is given from the cache. */
196  /* Inplace callbacks for when a reply is given with local data
197  * (or Chaos reply). */
199  /* Inplace callbacks for when the reply is servfail. */
201  /* Inplace callbacks for when a query is ready to be sent to the back.*/
203  /* Inplace callback for when a reply is received from the back. */
205  /* Inplace callback for when EDNS is parsed on a reply received from the
206  * back. */
208  /* Total number of types. Used for array initialization.
209  * Should always be last. */
211 };

◆ 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

Definition at line 536 of file module.h.

◆ 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

Definition at line 516 of file module.h.

Function Documentation

◆ 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.

◆ 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.

◆ 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).

◆ 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.

◆ 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.)

◆ 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.

◆ inplace_cb_lists_delete()

void inplace_cb_lists_delete ( struct module_env env)

Delete all the inplace callback linked lists.

Parameters
envthe module environment.

◆ 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.)

◆ 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.

◆ strextstate()

const char* strextstate ( enum module_ext_state  s)

Debug utility: module external qstate to string

Parameters
sthe state value.
Returns
descriptive string.

◆ strmodulevent()

const char* strmodulevent ( enum module_ev  e)

Debug utility: module event to string

Parameters
ethe module event value.
Returns
descriptive string.

◆ 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.