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