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