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