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