Home | History | Annotate | Line # | Download | only in services
      1 /*
      2  * services/mesh.h - deal with mesh of query states and handle events for that.
      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 functions to assist in dealing with a mesh of
     40  * query states. This mesh is supposed to be thread-specific.
     41  * It consists of query states (per qname, qtype, qclass) and connections
     42  * between query states and the super and subquery states, and replies to
     43  * send back to clients.
     44  */
     45 
     46 #ifndef SERVICES_MESH_H
     47 #define SERVICES_MESH_H
     48 
     49 #include "util/rbtree.h"
     50 #include "util/netevent.h"
     51 #include "util/data/msgparse.h"
     52 #include "util/module.h"
     53 #include "services/modstack.h"
     54 #include "services/rpz.h"
     55 #include "libunbound/unbound.h"
     56 struct sldns_buffer;
     57 struct mesh_state;
     58 struct mesh_reply;
     59 struct mesh_cb;
     60 struct query_info;
     61 struct reply_info;
     62 struct outbound_entry;
     63 struct timehist;
     64 struct respip_client_info;
     65 
     66 /**
     67  * Maximum number of mesh state activations. Any more is likely an
     68  * infinite loop in the module. It is then terminated.
     69  */
     70 #define MESH_MAX_ACTIVATION 10000
     71 
     72 /**
     73  * Max number of references-to-references-to-references.. search size.
     74  * Any more is treated like 'too large', and the creation of a new
     75  * dependency is failed (so that no loops can be created).
     76  */
     77 #define MESH_MAX_SUBSUB 1024
     78 
     79 /**
     80  * Mesh of query states
     81  */
     82 struct mesh_area {
     83 	/** active module stack */
     84 	struct module_stack mods;
     85 	/** environment for new states */
     86 	struct module_env* env;
     87 
     88 	/** set of runnable queries (mesh_state.run_node) */
     89 	rbtree_type run;
     90 	/** rbtree of all current queries (mesh_state.node)*/
     91 	rbtree_type all;
     92 
     93 	/** number of queries for unbound's auth_zones, upstream query */
     94 	size_t num_query_authzone_up;
     95 	/** number of queries for unbound's auth_zones, downstream answers */
     96 	size_t num_query_authzone_down;
     97 
     98 	/** count of the total number of mesh_reply entries */
     99 	size_t num_reply_addrs;
    100 	/** count of the number of mesh_states that have mesh_replies
    101 	 * Because a state can send results to multiple reply addresses,
    102 	 * this number must be equal or lower than num_reply_addrs. */
    103 	size_t num_reply_states;
    104 	/** number of mesh_states that have no mesh_replies, and also
    105 	 * an empty set of super-states, thus are 'toplevel' or detached
    106 	 * internal opportunistic queries */
    107 	size_t num_detached_states;
    108 	/** number of reply states in the forever list */
    109 	size_t num_forever_states;
    110 
    111 	/** max total number of reply states to have */
    112 	size_t max_reply_states;
    113 	/** max forever number of reply states to have */
    114 	size_t max_forever_states;
    115 
    116 	/** stats, cumulative number of reply states jostled out */
    117 	size_t stats_jostled;
    118 	/** stats, cumulative number of incoming client msgs dropped */
    119 	size_t stats_dropped;
    120 	/** stats, number of expired replies sent */
    121 	size_t ans_expired;
    122 	/** stats, number of cached replies from cachedb */
    123 	size_t ans_cachedb;
    124 	/** number of replies sent */
    125 	size_t replies_sent;
    126 	/** sum of waiting times for the replies */
    127 	struct timeval replies_sum_wait;
    128 	/** histogram of time values */
    129 	struct timehist* histogram;
    130 	/** (extended stats) secure replies */
    131 	size_t ans_secure;
    132 	/** (extended stats) bogus replies */
    133 	size_t ans_bogus;
    134 	/** (extended stats) number of validation operations */
    135 	size_t val_ops;
    136 	/** (extended stats) rcodes in replies */
    137 	size_t ans_rcode[UB_STATS_RCODE_NUM];
    138 	/** (extended stats) rcode nodata in replies */
    139 	size_t ans_nodata;
    140 	/** (extended stats) type of applied RPZ action */
    141 	size_t rpz_action[UB_STATS_RPZ_ACTION_NUM];
    142 	/** stats, number of queries removed due to discard-timeout */
    143 	size_t num_queries_discard_timeout;
    144 	/** stats, number of queries removed due to replyaddr limit */
    145 	size_t num_queries_replyaddr_limit;
    146 	/** stats, number of queries removed due to wait-limit */
    147 	size_t num_queries_wait_limit;
    148 	/** stats, number of dns error reports generated */
    149 	size_t num_dns_error_reports;
    150 
    151 	/** backup of query if other operations recurse and need the
    152 	 * network buffers */
    153 	struct sldns_buffer* qbuf_bak;
    154 
    155 	/** double linked list of the run-to-completion query states.
    156 	 * These are query states with a reply */
    157 	struct mesh_state* forever_first;
    158 	/** last entry in run forever list */
    159 	struct mesh_state* forever_last;
    160 
    161 	/** double linked list of the query states that can be jostled out
    162 	 * by new queries if too old.  These are query states with a reply */
    163 	struct mesh_state* jostle_first;
    164 	/** last entry in jostle list - this is the entry that is newest */
    165 	struct mesh_state* jostle_last;
    166 	/** timeout for jostling. if age is lower, it does not get jostled. */
    167 	struct timeval jostle_max;
    168 
    169 	/** If we need to use response ip (value passed from daemon)*/
    170 	int use_response_ip;
    171 	/** If we need to use RPZ (value passed from daemon) */
    172 	int use_rpz;
    173 };
    174 
    175 /**
    176  * A mesh query state
    177  * Unique per qname, qtype, qclass (from the qstate).
    178  * And RD / CD flag; in case a client turns it off.
    179  * And priming queries are different from ordinary queries (because of hints).
    180  *
    181  * The entire structure is allocated in a region, this region is the qstate
    182  * region. All parts (rbtree nodes etc) are also allocated in the region.
    183  */
    184 struct mesh_state {
    185 	/** node in mesh_area all tree, key is this struct. Must be first. */
    186 	rbnode_type node;
    187 	/** node in mesh_area runnable tree, key is this struct */
    188 	rbnode_type run_node;
    189 	/** the query state. Note that the qinfo and query_flags
    190 	 * may not change. */
    191 	struct module_qstate s;
    192 	/** the list of replies to clients for the results */
    193 	struct mesh_reply* reply_list;
    194 	/** if it has a first reply time */
    195 	int has_first_reply_time;
    196 	/** wall-clock time the first client reply was attached;
    197 	 *  used by mesh_make_new_space() so duplicate retransmits
    198 	 *  cannot reset jostle aging. */
    199 	struct timeval first_reply_time;
    200 	/** the list of callbacks for the results */
    201 	struct mesh_cb* cb_list;
    202 	/** set of superstates (that want this state's result)
    203 	 * contains struct mesh_state_ref* */
    204 	rbtree_type super_set;
    205 	/** set of substates (that this state needs to continue)
    206 	 * contains struct mesh_state_ref* */
    207 	rbtree_type sub_set;
    208 	/** number of activations for the mesh state */
    209 	size_t num_activated;
    210 
    211 	/** previous in linked list for reply states */
    212 	struct mesh_state* prev;
    213 	/** next in linked list for reply states */
    214 	struct mesh_state* next;
    215 	/** if this state is in the forever list, jostle list, or neither */
    216 	enum mesh_list_select { mesh_no_list, mesh_forever_list,
    217 		mesh_jostle_list } list_select;
    218 	/** pointer to this state for uniqueness or NULL */
    219 	struct mesh_state* unique;
    220 
    221 	/** true if replies have been sent out (at end for alignment) */
    222 	uint8_t replies_sent;
    223 };
    224 
    225 /**
    226  * Rbtree reference to a mesh_state.
    227  * Used in super_set and sub_set.
    228  */
    229 struct mesh_state_ref {
    230 	/** node in rbtree for set, key is this structure */
    231 	rbnode_type node;
    232 	/** the mesh state */
    233 	struct mesh_state* s;
    234 };
    235 
    236 /**
    237  * Reply to a client
    238  */
    239 struct mesh_reply {
    240 	/** next in reply list */
    241 	struct mesh_reply* next;
    242 	/** the query reply destination, packet buffer and where to send. */
    243 	struct comm_reply query_reply;
    244 	/** edns data from query */
    245 	struct edns_data edns;
    246 	/** the time when request was entered */
    247 	struct timeval start_time;
    248 	/** id of query, in network byteorder. */
    249 	uint16_t qid;
    250 	/** flags of query, for reply flags */
    251 	uint16_t qflags;
    252 	/** qname from this query. len same as mesh qinfo. */
    253 	uint8_t* qname;
    254 	/** same as that in query_info. */
    255 	struct local_rrset* local_alias;
    256 	/** send query to this http2 stream, if set */
    257 	struct http2_stream* h2_stream;
    258 };
    259 
    260 /**
    261  * Mesh result callback func.
    262  * called as func(cb_arg, rcode, buffer_with_reply, security, why_bogus,
    263  *		was_ratelimited);
    264  */
    265 typedef void (*mesh_cb_func_type)(void* cb_arg, int rcode, struct sldns_buffer*,
    266 	enum sec_status, char* why_bogus, int was_ratelimited);
    267 
    268 /**
    269  * Callback to result routine
    270  */
    271 struct mesh_cb {
    272 	/** next in list */
    273 	struct mesh_cb* next;
    274 	/** edns data from query */
    275 	struct edns_data edns;
    276 	/** id of query, in network byteorder. */
    277 	uint16_t qid;
    278 	/** flags of query, for reply flags */
    279 	uint16_t qflags;
    280 	/** buffer for reply */
    281 	struct sldns_buffer* buf;
    282 	/** callback routine for results. if rcode != 0 buf has message.
    283 	 * called as cb(cb_arg, rcode, buf, sec_state, why_bogus, was_ratelimited);
    284 	 */
    285 	mesh_cb_func_type cb;
    286 	/** user arg for callback */
    287 	void* cb_arg;
    288 };
    289 
    290 /* ------------------- Functions for worker -------------------- */
    291 
    292 /**
    293  * Allocate mesh, to empty.
    294  * @param stack: module stack to activate, copied (as readonly reference).
    295  * @param env: environment for new queries.
    296  * @return mesh: the new mesh or NULL on error.
    297  */
    298 struct mesh_area* mesh_create(struct module_stack* stack,
    299 	struct module_env* env);
    300 
    301 /**
    302  * Delete mesh, and all query states and replies in it.
    303  * @param mesh: the mesh to delete.
    304  */
    305 void mesh_delete(struct mesh_area* mesh);
    306 
    307 /**
    308  * New query incoming from clients. Create new query state if needed, and
    309  * add mesh_reply to it. Returns error to client on malloc failures.
    310  * Will run the mesh area queries to process if a new query state is created.
    311  *
    312  * @param mesh: the mesh.
    313  * @param qinfo: query from client.
    314  * @param cinfo: additional information associated with the query client.
    315  * 	'cinfo' itself is ephemeral but data pointed to by its members
    316  *      can be assumed to be valid and unchanged until the query processing is
    317  *      completed.
    318  * @param qflags: flags from client query.
    319  * @param edns: edns data from client query.
    320  * @param rep: where to reply to.
    321  * @param qid: query id to reply with.
    322  * @param rpz_passthru: if true, the rpz passthru was previously found and
    323  * 	further rpz processing is stopped.
    324  */
    325 void mesh_new_client(struct mesh_area* mesh, struct query_info* qinfo,
    326 	struct respip_client_info* cinfo, uint16_t qflags,
    327 	struct edns_data* edns, struct comm_reply* rep, uint16_t qid,
    328 	int rpz_passthru);
    329 
    330 /**
    331  * New query with callback. Create new query state if needed, and
    332  * add mesh_cb to it.
    333  * Will run the mesh area queries to process if a new query state is created.
    334  *
    335  * @param mesh: the mesh.
    336  * @param qinfo: query from client.
    337  * @param qflags: flags from client query.
    338  * @param edns: edns data from client query.
    339  * @param buf: buffer for reply contents.
    340  * @param qid: query id to reply with.
    341  * @param cb: callback function.
    342  * @param cb_arg: callback user arg.
    343  * @param rpz_passthru: if true, the rpz passthru was previously found and
    344  * 	further rpz processing is stopped.
    345  * @return 0 on error.
    346  */
    347 int mesh_new_callback(struct mesh_area* mesh, struct query_info* qinfo,
    348 	uint16_t qflags, struct edns_data* edns, struct sldns_buffer* buf,
    349 	uint16_t qid, mesh_cb_func_type cb, void* cb_arg, int rpz_passthru);
    350 
    351 /**
    352  * New prefetch message. Create new query state if needed.
    353  * Will run the mesh area queries to process if a new query state is created.
    354  *
    355  * @param mesh: the mesh.
    356  * @param qinfo: query from client.
    357  * @param qflags: flags from client query.
    358  * @param leeway: TTL leeway what to expire earlier for this update.
    359  * @param rpz_passthru: if true, the rpz passthru was previously found and
    360  * 	further rpz processing is stopped.
    361  * @param addr: sockaddr_storage for the client; to be used with subnet.
    362  * @param opt_list: edns opt_list from the client; to be used when subnet is
    363  *	enabled.
    364  */
    365 void mesh_new_prefetch(struct mesh_area* mesh, struct query_info* qinfo,
    366 	uint16_t qflags, time_t leeway, int rpz_passthru,
    367 	struct sockaddr_storage* addr, struct edns_option* opt_list);
    368 
    369 /**
    370  * Handle new event from the wire. A serviced query has returned.
    371  * The query state will be made runnable, and the mesh_area will process
    372  * query states until processing is complete.
    373  *
    374  * @param mesh: the query mesh.
    375  * @param e: outbound entry, with query state to run and reply pointer.
    376  * @param reply: the comm point reply info.
    377  * @param what: NETEVENT_* error code (if not 0, what is wrong, TIMEOUT).
    378  */
    379 void mesh_report_reply(struct mesh_area* mesh, struct outbound_entry* e,
    380 	struct comm_reply* reply, int what);
    381 
    382 /* ------------------- Functions for module environment --------------- */
    383 
    384 /**
    385  * Detach-subqueries.
    386  * Remove all sub-query references from this query state.
    387  * Keeps super-references of those sub-queries correct.
    388  * Updates stat items in mesh_area structure.
    389  * @param qstate: used to find mesh state.
    390  */
    391 void mesh_detach_subs(struct module_qstate* qstate);
    392 
    393 /**
    394  * Attach subquery.
    395  * Creates it if it does not exist already.
    396  * Keeps sub and super references correct.
    397  * Performs a cycle detection - for double check - and fails if there is one.
    398  * Also fails if the sub-sub-references become too large.
    399  * Updates stat items in mesh_area structure.
    400  * Pass if it is priming query or not.
    401  * return:
    402  * 	o if error (malloc) happened.
    403  * 	o need to initialise the new state (module init; it is a new state).
    404  * 	  so that the next run of the query with this module is successful.
    405  * 	o no init needed, attachment successful.
    406  *
    407  * @param qstate: the state to find mesh state, and that wants to receive
    408  * 	the results from the new subquery.
    409  * @param qinfo: what to query for (copied).
    410  * @param cinfo: if non-NULL client specific info that may affect IP-based
    411  * 	actions that apply to the query result. It is copied.
    412  * @param qflags: what flags to use (RD / CD flag or not).
    413  * @param prime: if it is a (stub) priming query.
    414  * @param valrec: if it is a validation recursion query (lookup of key, DS).
    415  * @param newq: If the new subquery needs initialisation, it is returned,
    416  * 	otherwise NULL is returned.
    417  * @return: false on error, true if success (and init may be needed).
    418  */
    419 int mesh_attach_sub(struct module_qstate* qstate, struct query_info* qinfo,
    420 	struct respip_client_info* cinfo, uint16_t qflags, int prime,
    421 	int valrec, struct module_qstate** newq);
    422 
    423 /**
    424  * Add detached query.
    425  * Creates it if it does not exist already.
    426  * Does not make super/sub references.
    427  * Performs a cycle detection - for double check - and fails if there is one.
    428  * Updates stat items in mesh_area structure.
    429  * Pass if it is priming query or not.
    430  * return:
    431  * 	o if error (malloc) happened.
    432  * 	o need to initialise the new state (module init; it is a new state).
    433  * 	  so that the next run of the query with this module is successful.
    434  * 	o no init needed, attachment successful.
    435  * 	o added subquery, created if it did not exist already.
    436  *
    437  * @param qstate: the state to find mesh state, and that wants to receive
    438  * 	the results from the new subquery.
    439  * @param qinfo: what to query for (copied).
    440  * @param cinfo: if non-NULL client specific info that may affect IP-based
    441  * 	actions that apply to the query result. It is copied.
    442  * @param qflags: what flags to use (RD / CD flag or not).
    443  * @param prime: if it is a (stub) priming query.
    444  * @param valrec: if it is a validation recursion query (lookup of key, DS).
    445  * @param newq: If the new subquery needs initialisation, it is returned,
    446  * 	otherwise NULL is returned.
    447  * @param sub: The added mesh state, created if it did not exist already.
    448  * @return: false on error, true if success (and init may be needed).
    449  */
    450 int mesh_add_sub(struct module_qstate* qstate, struct query_info* qinfo,
    451 	struct respip_client_info* cinfo, uint16_t qflags, int prime,
    452 	int valrec, struct module_qstate** newq, struct mesh_state** sub);
    453 
    454 /**
    455  * Query state is done, send messages to reply entries.
    456  * Encode messages using reply entry values and the querystate (with original
    457  * qinfo), using given reply_info.
    458  * Pass errcode != 0 if an error reply is needed.
    459  * If no reply entries, nothing is done.
    460  * Must be called before a module can module_finished or return module_error.
    461  * The module must handle the super query states itself as well.
    462  *
    463  * @param mstate: mesh state that is done. return_rcode and return_msg
    464  * 	are used for replies.
    465  * 	return_rcode: if not 0 (NOERROR) an error is sent back (and
    466  * 		return_msg is ignored).
    467  * 	return_msg: reply to encode and send back to clients.
    468  */
    469 void mesh_query_done(struct mesh_state* mstate);
    470 
    471 /**
    472  * Call inform_super for the super query states that are interested in the
    473  * results from this query state. These can then be changed for error
    474  * or results.
    475  * Called when a module is module_finished or returns module_error.
    476  * The super query states become runnable with event module_event_pass,
    477  * it calls the current module for the super with the inform_super event.
    478  *
    479  * @param mesh: mesh area to add newly runnable modules to.
    480  * @param mstate: the state that has results, used to find mesh state.
    481  */
    482 void mesh_walk_supers(struct mesh_area* mesh, struct mesh_state* mstate);
    483 
    484 /**
    485  * Delete mesh state, cleanup and also rbtrees and so on.
    486  * Will detach from all super/subnodes.
    487  * @param qstate: to remove.
    488  */
    489 void mesh_state_delete(struct module_qstate* qstate);
    490 
    491 /* ------------------- Functions for mesh -------------------- */
    492 
    493 /**
    494  * Create and initialize a new mesh state and its query state
    495  * Does not put the mesh state into rbtrees and so on.
    496  * @param env: module environment to set.
    497  * @param qinfo: query info that the mesh is for.
    498  * @param cinfo: control info for the query client (can be NULL).
    499  * @param qflags: flags for query (RD / CD flag).
    500  * @param prime: if true, it is a priming query, set is_priming on mesh state.
    501  * @param valrec: if true, it is a validation recursion query, and sets
    502  * 	is_valrec on the mesh state.
    503  * @return: new mesh state or NULL on allocation error.
    504  */
    505 struct mesh_state* mesh_state_create(struct module_env* env,
    506 	struct query_info* qinfo, struct respip_client_info* cinfo,
    507 	uint16_t qflags, int prime, int valrec);
    508 
    509 /**
    510  * Make a mesh state unique.
    511  * A unique mesh state uses it's unique member to point to itself.
    512  * @param mstate: mesh state to check.
    513  */
    514 void mesh_state_make_unique(struct mesh_state* mstate);
    515 
    516 /**
    517  * Cleanup a mesh state and its query state. Does not do rbtree or
    518  * reference cleanup.
    519  * @param mstate: mesh state to cleanup. Its pointer may no longer be used
    520  * 	afterwards. Cleanup rbtrees before calling this function.
    521  */
    522 void mesh_state_cleanup(struct mesh_state* mstate);
    523 
    524 /**
    525  * Delete all mesh states from the mesh.
    526  * @param mesh: the mesh area to clear
    527  */
    528 void mesh_delete_all(struct mesh_area* mesh);
    529 
    530 /**
    531  * Find a mesh state in the mesh area. Pass relevant flags.
    532  *
    533  * @param mesh: the mesh area to look in.
    534  * @param cinfo: if non-NULL client specific info that may affect IP-based
    535  * 	actions that apply to the query result.
    536  * @param qinfo: what query
    537  * @param qflags: if RD / CD bit is set or not.
    538  * @param prime: if it is a priming query.
    539  * @param valrec: if it is a validation-recursion query.
    540  * @return: mesh state or NULL if not found.
    541  */
    542 struct mesh_state* mesh_area_find(struct mesh_area* mesh,
    543 	struct respip_client_info* cinfo, struct query_info* qinfo,
    544 	uint16_t qflags, int prime, int valrec);
    545 
    546 /**
    547  * Setup attachment super/sub relation between super and sub mesh state.
    548  * The relation must not be present when calling the function.
    549  * Does not update stat items in mesh_area.
    550  * @param super: super state.
    551  * @param sub: sub state.
    552  * @return: 0 on alloc error.
    553  */
    554 int mesh_state_attachment(struct mesh_state* super, struct mesh_state* sub);
    555 
    556 /**
    557  * Create new reply structure and attach it to a mesh state.
    558  * Does not update stat items in mesh area.
    559  * @param s: the mesh state.
    560  * @param edns: edns data for reply (bufsize).
    561  * @param rep: comm point reply info.
    562  * @param qid: ID of reply.
    563  * @param qflags: original query flags.
    564  * @param qinfo: original query info.
    565  * @return: 0 on alloc error.
    566  */
    567 int mesh_state_add_reply(struct mesh_state* s, struct edns_data* edns,
    568 	struct comm_reply* rep, uint16_t qid, uint16_t qflags,
    569 	const struct query_info* qinfo);
    570 
    571 /**
    572  * Create new callback structure and attach it to a mesh state.
    573  * Does not update stat items in mesh area.
    574  * @param s: the mesh state.
    575  * @param edns: edns data for reply (bufsize).
    576  * @param buf: buffer for reply
    577  * @param cb: callback to call with results.
    578  * @param cb_arg: callback user arg.
    579  * @param qid: ID of reply.
    580  * @param qflags: original query flags.
    581  * @return: 0 on alloc error.
    582  */
    583 int mesh_state_add_cb(struct mesh_state* s, struct edns_data* edns,
    584         struct sldns_buffer* buf, mesh_cb_func_type cb, void* cb_arg,
    585 	uint16_t qid, uint16_t qflags);
    586 
    587 /**
    588  * Run the mesh. Run all runnable mesh states. Which can create new
    589  * runnable mesh states. Until completion. Automatically called by
    590  * mesh_report_reply and mesh_new_client as needed.
    591  * @param mesh: mesh area.
    592  * @param mstate: first mesh state to run.
    593  * @param ev: event the mstate. Others get event_pass.
    594  * @param e: if a reply, its outbound entry.
    595  */
    596 void mesh_run(struct mesh_area* mesh, struct mesh_state* mstate,
    597 	enum module_ev ev, struct outbound_entry* e);
    598 
    599 /**
    600  * Print some stats about the mesh to the log.
    601  * @param mesh: the mesh to print it for.
    602  * @param str: descriptive string to go with it.
    603  */
    604 void mesh_stats(struct mesh_area* mesh, const char* str);
    605 
    606 /**
    607  * Clear the stats that the mesh keeps (number of queries serviced)
    608  * @param mesh: the mesh
    609  */
    610 void mesh_stats_clear(struct mesh_area* mesh);
    611 
    612 /**
    613  * Print all the states in the mesh to the log.
    614  * @param mesh: the mesh to print all states of.
    615  */
    616 void mesh_log_list(struct mesh_area* mesh);
    617 
    618 /**
    619  * Calculate memory size in use by mesh and all queries inside it.
    620  * @param mesh: the mesh to examine.
    621  * @return size in bytes.
    622  */
    623 size_t mesh_get_mem(struct mesh_area* mesh);
    624 
    625 /**
    626  * Find cycle; see if the given mesh is in the targets sub, or sub-sub, ...
    627  * trees.
    628  * If the sub-sub structure is too large, it returns 'a cycle'=2.
    629  * @param qstate: given mesh querystate.
    630  * @param qinfo: query info for dependency.
    631  * @param flags: query flags of dependency.
    632  * @param prime: if dependency is a priming query or not.
    633  * @param valrec: if it is a validation recursion query (lookup of key, DS).
    634  * @return true if the name,type,class exists and the given qstate mesh exists
    635  * 	as a dependency of that name. Thus if qstate becomes dependent on
    636  * 	name,type,class then a cycle is created, this is return value 1.
    637  * 	Too large to search is value 2 (also true).
    638  */
    639 int mesh_detect_cycle(struct module_qstate* qstate, struct query_info* qinfo,
    640 	uint16_t flags, int prime, int valrec);
    641 
    642 /** compare two mesh_states */
    643 int mesh_state_compare(const void* ap, const void* bp);
    644 
    645 /** compare two mesh references */
    646 int mesh_state_ref_compare(const void* ap, const void* bp);
    647 
    648 /**
    649  * Make space for another recursion state for a reply in the mesh
    650  * @param mesh: mesh area
    651  * @param qbuf: query buffer to save if recursion is invoked to make space.
    652  *    This buffer is necessary, because the following sequence in calls
    653  *    can result in an overwrite of the incoming query:
    654  *    delete_other_mesh_query - iter_clean - serviced_delete - waiting
    655  *    udp query is sent - on error callback - callback sends SERVFAIL reply
    656  *    over the same network channel, and shared UDP buffer is overwritten.
    657  *    You can pass NULL if there is no buffer that must be backed up.
    658  * @return false if no space is available.
    659  */
    660 int mesh_make_new_space(struct mesh_area* mesh, struct sldns_buffer* qbuf);
    661 
    662 /**
    663  * Insert mesh state into a double linked list.  Inserted at end.
    664  * @param m: mesh state.
    665  * @param fp: pointer to the first-elem-pointer of the list.
    666  * @param lp: pointer to the last-elem-pointer of the list.
    667  */
    668 void mesh_list_insert(struct mesh_state* m, struct mesh_state** fp,
    669 	struct mesh_state** lp);
    670 
    671 /**
    672  * Remove mesh state from a double linked list.  Remove from any position.
    673  * @param m: mesh state.
    674  * @param fp: pointer to the first-elem-pointer of the list.
    675  * @param lp: pointer to the last-elem-pointer of the list.
    676  */
    677 void mesh_list_remove(struct mesh_state* m, struct mesh_state** fp,
    678 	struct mesh_state** lp);
    679 
    680 /**
    681  * Remove mesh reply entry from the reply entry list.  Searches for
    682  * the comm_point pointer.
    683  * @param mesh: to update the counters.
    684  * @param m: the mesh state.
    685  * @param cp: the comm_point to remove from the list.
    686  */
    687 void mesh_state_remove_reply(struct mesh_area* mesh, struct mesh_state* m,
    688 	struct comm_point* cp);
    689 
    690 /** Callback for when the serve expired client timer has run out.  Tries to
    691  * find an expired answer in the cache and reply that to the client.
    692  * @param arg: the argument passed to the callback.
    693  */
    694 void mesh_serve_expired_callback(void* arg);
    695 
    696 /**
    697  * Try to get a (expired) cached answer.
    698  * This needs to behave like the worker's answer_from_cache() in order to have
    699  * the same behavior as when replying from cache.
    700  * @param qstate: the module qstate.
    701  * @param lookup_qinfo: the query info to look for in the cache.
    702  * @param is_expired: set if the cached answer is expired.
    703  * @return dns_msg if a cached answer was found, otherwise NULL.
    704  */
    705 struct dns_msg*
    706 mesh_serve_expired_lookup(struct module_qstate* qstate,
    707 	struct query_info* lookup_qinfo, int* is_expired);
    708 
    709 /**
    710  * See if the mesh has space for more queries. You can allocate queries
    711  * anyway, but this checks for the allocated space.
    712  * @param mesh: mesh area.
    713  * @return true if the query list is full.
    714  * 	It checks the number of all queries, not just number of reply states,
    715  * 	that have a client address. So that spawned queries count too,
    716  * 	that were created by the iterator, or other modules.
    717  */
    718 int mesh_jostle_exceeded(struct mesh_area* mesh);
    719 
    720 /**
    721  * Give the serve expired responses.
    722  * @param mstate: mesh state for query that has serve_expired_data.
    723  */
    724 void mesh_respond_serve_expired(struct mesh_state* mstate);
    725 
    726 /**
    727  * Remove callback from mesh. Removes the callback from the state.
    728  * The state itself is left to run. Searches for the pointer values.
    729  *
    730  * @param mesh: the mesh.
    731  * @param qinfo: query from client.
    732  * @param qflags: flags from client query.
    733  * @param cb: callback function.
    734  * @param cb_arg: callback user arg.
    735  */
    736 void mesh_remove_callback(struct mesh_area* mesh, struct query_info* qinfo,
    737 	uint16_t qflags, mesh_cb_func_type cb, void* cb_arg);
    738 
    739 #endif /* SERVICES_MESH_H */
    740