xref: /freebsd/contrib/unbound/services/mesh.h (revision c0caa2e24e9a8c64aa01e4265e8b989ba74b9702)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * services/mesh.h - deal with mesh of query states and handle events for that.
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  *
39b7579f77SDag-Erling Smørgrav  * This file contains functions to assist in dealing with a mesh of
40b7579f77SDag-Erling Smørgrav  * query states. This mesh is supposed to be thread-specific.
41b7579f77SDag-Erling Smørgrav  * It consists of query states (per qname, qtype, qclass) and connections
42b7579f77SDag-Erling Smørgrav  * between query states and the super and subquery states, and replies to
43b7579f77SDag-Erling Smørgrav  * send back to clients.
44b7579f77SDag-Erling Smørgrav  */
45b7579f77SDag-Erling Smørgrav 
46b7579f77SDag-Erling Smørgrav #ifndef SERVICES_MESH_H
47b7579f77SDag-Erling Smørgrav #define SERVICES_MESH_H
48b7579f77SDag-Erling Smørgrav 
49b7579f77SDag-Erling Smørgrav #include "util/rbtree.h"
50b7579f77SDag-Erling Smørgrav #include "util/netevent.h"
51b7579f77SDag-Erling Smørgrav #include "util/data/msgparse.h"
52b7579f77SDag-Erling Smørgrav #include "util/module.h"
53b7579f77SDag-Erling Smørgrav #include "services/modstack.h"
54091e9e46SCy Schubert #include "services/rpz.h"
55091e9e46SCy Schubert #include "libunbound/unbound.h"
5617d15b25SDag-Erling Smørgrav struct sldns_buffer;
57b7579f77SDag-Erling Smørgrav struct mesh_state;
58b7579f77SDag-Erling Smørgrav struct mesh_reply;
59b7579f77SDag-Erling Smørgrav struct mesh_cb;
60b7579f77SDag-Erling Smørgrav struct query_info;
61b7579f77SDag-Erling Smørgrav struct reply_info;
62b7579f77SDag-Erling Smørgrav struct outbound_entry;
63b7579f77SDag-Erling Smørgrav struct timehist;
6465b390aaSDag-Erling Smørgrav struct respip_client_info;
65b7579f77SDag-Erling Smørgrav 
66b7579f77SDag-Erling Smørgrav /**
67b7579f77SDag-Erling Smørgrav  * Maximum number of mesh state activations. Any more is likely an
68b7579f77SDag-Erling Smørgrav  * infinite loop in the module. It is then terminated.
69b7579f77SDag-Erling Smørgrav  */
70e86b9096SDag-Erling Smørgrav #define MESH_MAX_ACTIVATION 10000
71b7579f77SDag-Erling Smørgrav 
72b7579f77SDag-Erling Smørgrav /**
73b7579f77SDag-Erling Smørgrav  * Max number of references-to-references-to-references.. search size.
74b7579f77SDag-Erling Smørgrav  * Any more is treated like 'too large', and the creation of a new
75b7579f77SDag-Erling Smørgrav  * dependency is failed (so that no loops can be created).
76b7579f77SDag-Erling Smørgrav  */
77b7579f77SDag-Erling Smørgrav #define MESH_MAX_SUBSUB 1024
78b7579f77SDag-Erling Smørgrav 
79b7579f77SDag-Erling Smørgrav /**
80b7579f77SDag-Erling Smørgrav  * Mesh of query states
81b7579f77SDag-Erling Smørgrav  */
82b7579f77SDag-Erling Smørgrav struct mesh_area {
83b7579f77SDag-Erling Smørgrav 	/** active module stack */
84b7579f77SDag-Erling Smørgrav 	struct module_stack mods;
85b7579f77SDag-Erling Smørgrav 	/** environment for new states */
86b7579f77SDag-Erling Smørgrav 	struct module_env* env;
87b7579f77SDag-Erling Smørgrav 
88b7579f77SDag-Erling Smørgrav 	/** set of runnable queries (mesh_state.run_node) */
893005e0a3SDag-Erling Smørgrav 	rbtree_type run;
90b7579f77SDag-Erling Smørgrav 	/** rbtree of all current queries (mesh_state.node)*/
913005e0a3SDag-Erling Smørgrav 	rbtree_type all;
92b7579f77SDag-Erling Smørgrav 
93b7579f77SDag-Erling Smørgrav 	/** count of the total number of mesh_reply entries */
94b7579f77SDag-Erling Smørgrav 	size_t num_reply_addrs;
95b7579f77SDag-Erling Smørgrav 	/** count of the number of mesh_states that have mesh_replies
96b7579f77SDag-Erling Smørgrav 	 * Because a state can send results to multiple reply addresses,
97b7579f77SDag-Erling Smørgrav 	 * this number must be equal or lower than num_reply_addrs. */
98b7579f77SDag-Erling Smørgrav 	size_t num_reply_states;
99b7579f77SDag-Erling Smørgrav 	/** number of mesh_states that have no mesh_replies, and also
100b7579f77SDag-Erling Smørgrav 	 * an empty set of super-states, thus are 'toplevel' or detached
101b7579f77SDag-Erling Smørgrav 	 * internal opportunistic queries */
102b7579f77SDag-Erling Smørgrav 	size_t num_detached_states;
103b7579f77SDag-Erling Smørgrav 	/** number of reply states in the forever list */
104b7579f77SDag-Erling Smørgrav 	size_t num_forever_states;
105b7579f77SDag-Erling Smørgrav 
106b7579f77SDag-Erling Smørgrav 	/** max total number of reply states to have */
107b7579f77SDag-Erling Smørgrav 	size_t max_reply_states;
108b7579f77SDag-Erling Smørgrav 	/** max forever number of reply states to have */
109b7579f77SDag-Erling Smørgrav 	size_t max_forever_states;
110b7579f77SDag-Erling Smørgrav 
111b7579f77SDag-Erling Smørgrav 	/** stats, cumulative number of reply states jostled out */
112b7579f77SDag-Erling Smørgrav 	size_t stats_jostled;
113b7579f77SDag-Erling Smørgrav 	/** stats, cumulative number of incoming client msgs dropped */
114b7579f77SDag-Erling Smørgrav 	size_t stats_dropped;
115091e9e46SCy Schubert 	/** stats, number of expired replies sent */
116091e9e46SCy Schubert 	size_t ans_expired;
117b7579f77SDag-Erling Smørgrav 	/** number of replies sent */
118b7579f77SDag-Erling Smørgrav 	size_t replies_sent;
119b7579f77SDag-Erling Smørgrav 	/** sum of waiting times for the replies */
120b7579f77SDag-Erling Smørgrav 	struct timeval replies_sum_wait;
121b7579f77SDag-Erling Smørgrav 	/** histogram of time values */
122b7579f77SDag-Erling Smørgrav 	struct timehist* histogram;
123b7579f77SDag-Erling Smørgrav 	/** (extended stats) secure replies */
124b7579f77SDag-Erling Smørgrav 	size_t ans_secure;
125b7579f77SDag-Erling Smørgrav 	/** (extended stats) bogus replies */
126b7579f77SDag-Erling Smørgrav 	size_t ans_bogus;
127b7579f77SDag-Erling Smørgrav 	/** (extended stats) rcodes in replies */
128091e9e46SCy Schubert 	size_t ans_rcode[UB_STATS_RCODE_NUM];
129b7579f77SDag-Erling Smørgrav 	/** (extended stats) rcode nodata in replies */
130b7579f77SDag-Erling Smørgrav 	size_t ans_nodata;
131091e9e46SCy Schubert 	/** (extended stats) type of applied RPZ action */
132091e9e46SCy Schubert 	size_t rpz_action[UB_STATS_RPZ_ACTION_NUM];
133b7579f77SDag-Erling Smørgrav 
134b7579f77SDag-Erling Smørgrav 	/** backup of query if other operations recurse and need the
135b7579f77SDag-Erling Smørgrav 	 * network buffers */
13617d15b25SDag-Erling Smørgrav 	struct sldns_buffer* qbuf_bak;
137b7579f77SDag-Erling Smørgrav 
138b7579f77SDag-Erling Smørgrav 	/** double linked list of the run-to-completion query states.
139b7579f77SDag-Erling Smørgrav 	 * These are query states with a reply */
140b7579f77SDag-Erling Smørgrav 	struct mesh_state* forever_first;
141b7579f77SDag-Erling Smørgrav 	/** last entry in run forever list */
142b7579f77SDag-Erling Smørgrav 	struct mesh_state* forever_last;
143b7579f77SDag-Erling Smørgrav 
144b7579f77SDag-Erling Smørgrav 	/** double linked list of the query states that can be jostled out
145b7579f77SDag-Erling Smørgrav 	 * by new queries if too old.  These are query states with a reply */
146b7579f77SDag-Erling Smørgrav 	struct mesh_state* jostle_first;
147b7579f77SDag-Erling Smørgrav 	/** last entry in jostle list - this is the entry that is newest */
148b7579f77SDag-Erling Smørgrav 	struct mesh_state* jostle_last;
149b7579f77SDag-Erling Smørgrav 	/** timeout for jostling. if age is lower, it does not get jostled. */
150b7579f77SDag-Erling Smørgrav 	struct timeval jostle_max;
151091e9e46SCy Schubert 
152091e9e46SCy Schubert 	/** If we need to use response ip (value passed from daemon)*/
153091e9e46SCy Schubert 	int use_response_ip;
154091e9e46SCy Schubert 	/** If we need to use RPZ (value passed from daemon) */
155091e9e46SCy Schubert 	int use_rpz;
156b7579f77SDag-Erling Smørgrav };
157b7579f77SDag-Erling Smørgrav 
158b7579f77SDag-Erling Smørgrav /**
159b7579f77SDag-Erling Smørgrav  * A mesh query state
160b7579f77SDag-Erling Smørgrav  * Unique per qname, qtype, qclass (from the qstate).
161b7579f77SDag-Erling Smørgrav  * And RD / CD flag; in case a client turns it off.
162b7579f77SDag-Erling Smørgrav  * And priming queries are different from ordinary queries (because of hints).
163b7579f77SDag-Erling Smørgrav  *
164b7579f77SDag-Erling Smørgrav  * The entire structure is allocated in a region, this region is the qstate
165b7579f77SDag-Erling Smørgrav  * region. All parts (rbtree nodes etc) are also allocated in the region.
166b7579f77SDag-Erling Smørgrav  */
167b7579f77SDag-Erling Smørgrav struct mesh_state {
168b7579f77SDag-Erling Smørgrav 	/** node in mesh_area all tree, key is this struct. Must be first. */
1693005e0a3SDag-Erling Smørgrav 	rbnode_type node;
170b7579f77SDag-Erling Smørgrav 	/** node in mesh_area runnable tree, key is this struct */
1713005e0a3SDag-Erling Smørgrav 	rbnode_type run_node;
172b7579f77SDag-Erling Smørgrav 	/** the query state. Note that the qinfo and query_flags
173b7579f77SDag-Erling Smørgrav 	 * may not change. */
174b7579f77SDag-Erling Smørgrav 	struct module_qstate s;
175b7579f77SDag-Erling Smørgrav 	/** the list of replies to clients for the results */
176b7579f77SDag-Erling Smørgrav 	struct mesh_reply* reply_list;
177b7579f77SDag-Erling Smørgrav 	/** the list of callbacks for the results */
178b7579f77SDag-Erling Smørgrav 	struct mesh_cb* cb_list;
179b7579f77SDag-Erling Smørgrav 	/** set of superstates (that want this state's result)
180b7579f77SDag-Erling Smørgrav 	 * contains struct mesh_state_ref* */
1813005e0a3SDag-Erling Smørgrav 	rbtree_type super_set;
182b7579f77SDag-Erling Smørgrav 	/** set of substates (that this state needs to continue)
183b7579f77SDag-Erling Smørgrav 	 * contains struct mesh_state_ref* */
1843005e0a3SDag-Erling Smørgrav 	rbtree_type sub_set;
185b7579f77SDag-Erling Smørgrav 	/** number of activations for the mesh state */
186b7579f77SDag-Erling Smørgrav 	size_t num_activated;
187b7579f77SDag-Erling Smørgrav 
188b7579f77SDag-Erling Smørgrav 	/** previous in linked list for reply states */
189b7579f77SDag-Erling Smørgrav 	struct mesh_state* prev;
190b7579f77SDag-Erling Smørgrav 	/** next in linked list for reply states */
191b7579f77SDag-Erling Smørgrav 	struct mesh_state* next;
192b7579f77SDag-Erling Smørgrav 	/** if this state is in the forever list, jostle list, or neither */
193b7579f77SDag-Erling Smørgrav 	enum mesh_list_select { mesh_no_list, mesh_forever_list,
194b7579f77SDag-Erling Smørgrav 		mesh_jostle_list } list_select;
195bc892140SDag-Erling Smørgrav 	/** pointer to this state for uniqueness or NULL */
196bc892140SDag-Erling Smørgrav 	struct mesh_state* unique;
197b7579f77SDag-Erling Smørgrav 
198b7579f77SDag-Erling Smørgrav 	/** true if replies have been sent out (at end for alignment) */
199b7579f77SDag-Erling Smørgrav 	uint8_t replies_sent;
200b7579f77SDag-Erling Smørgrav };
201b7579f77SDag-Erling Smørgrav 
202b7579f77SDag-Erling Smørgrav /**
203b7579f77SDag-Erling Smørgrav  * Rbtree reference to a mesh_state.
204b7579f77SDag-Erling Smørgrav  * Used in super_set and sub_set.
205b7579f77SDag-Erling Smørgrav  */
206b7579f77SDag-Erling Smørgrav struct mesh_state_ref {
207b7579f77SDag-Erling Smørgrav 	/** node in rbtree for set, key is this structure */
2083005e0a3SDag-Erling Smørgrav 	rbnode_type node;
209b7579f77SDag-Erling Smørgrav 	/** the mesh state */
210b7579f77SDag-Erling Smørgrav 	struct mesh_state* s;
211b7579f77SDag-Erling Smørgrav };
212b7579f77SDag-Erling Smørgrav 
213b7579f77SDag-Erling Smørgrav /**
214b7579f77SDag-Erling Smørgrav  * Reply to a client
215b7579f77SDag-Erling Smørgrav  */
216b7579f77SDag-Erling Smørgrav struct mesh_reply {
217b7579f77SDag-Erling Smørgrav 	/** next in reply list */
218b7579f77SDag-Erling Smørgrav 	struct mesh_reply* next;
219b7579f77SDag-Erling Smørgrav 	/** the query reply destination, packet buffer and where to send. */
220b7579f77SDag-Erling Smørgrav 	struct comm_reply query_reply;
221b7579f77SDag-Erling Smørgrav 	/** edns data from query */
222b7579f77SDag-Erling Smørgrav 	struct edns_data edns;
223b7579f77SDag-Erling Smørgrav 	/** the time when request was entered */
224b7579f77SDag-Erling Smørgrav 	struct timeval start_time;
225b7579f77SDag-Erling Smørgrav 	/** id of query, in network byteorder. */
226b7579f77SDag-Erling Smørgrav 	uint16_t qid;
227b7579f77SDag-Erling Smørgrav 	/** flags of query, for reply flags */
228b7579f77SDag-Erling Smørgrav 	uint16_t qflags;
229b7579f77SDag-Erling Smørgrav 	/** qname from this query. len same as mesh qinfo. */
230b7579f77SDag-Erling Smørgrav 	uint8_t* qname;
231bc892140SDag-Erling Smørgrav 	/** same as that in query_info. */
232bc892140SDag-Erling Smørgrav 	struct local_rrset* local_alias;
233*c0caa2e2SCy Schubert 	/** send query to this http2 stream, if set */
234*c0caa2e2SCy Schubert 	struct http2_stream* h2_stream;
235b7579f77SDag-Erling Smørgrav };
236b7579f77SDag-Erling Smørgrav 
237b7579f77SDag-Erling Smørgrav /**
238b7579f77SDag-Erling Smørgrav  * Mesh result callback func.
2394c75e3aaSDag-Erling Smørgrav  * called as func(cb_arg, rcode, buffer_with_reply, security, why_bogus,
2404c75e3aaSDag-Erling Smørgrav  *		was_ratelimited);
241b7579f77SDag-Erling Smørgrav  */
2424c75e3aaSDag-Erling Smørgrav typedef void (*mesh_cb_func_type)(void* cb_arg, int rcode, struct sldns_buffer*,
2434c75e3aaSDag-Erling Smørgrav 	enum sec_status, char* why_bogus, int was_ratelimited);
244b7579f77SDag-Erling Smørgrav 
245b7579f77SDag-Erling Smørgrav /**
246b7579f77SDag-Erling Smørgrav  * Callback to result routine
247b7579f77SDag-Erling Smørgrav  */
248b7579f77SDag-Erling Smørgrav struct mesh_cb {
249b7579f77SDag-Erling Smørgrav 	/** next in list */
250b7579f77SDag-Erling Smørgrav 	struct mesh_cb* next;
251b7579f77SDag-Erling Smørgrav 	/** edns data from query */
252b7579f77SDag-Erling Smørgrav 	struct edns_data edns;
253b7579f77SDag-Erling Smørgrav 	/** id of query, in network byteorder. */
254b7579f77SDag-Erling Smørgrav 	uint16_t qid;
255b7579f77SDag-Erling Smørgrav 	/** flags of query, for reply flags */
256b7579f77SDag-Erling Smørgrav 	uint16_t qflags;
257b7579f77SDag-Erling Smørgrav 	/** buffer for reply */
25817d15b25SDag-Erling Smørgrav 	struct sldns_buffer* buf;
259b7579f77SDag-Erling Smørgrav 	/** callback routine for results. if rcode != 0 buf has message.
2604c75e3aaSDag-Erling Smørgrav 	 * called as cb(cb_arg, rcode, buf, sec_state, why_bogus, was_ratelimited);
261b7579f77SDag-Erling Smørgrav 	 */
2623005e0a3SDag-Erling Smørgrav 	mesh_cb_func_type cb;
263b7579f77SDag-Erling Smørgrav 	/** user arg for callback */
264b7579f77SDag-Erling Smørgrav 	void* cb_arg;
265b7579f77SDag-Erling Smørgrav };
266b7579f77SDag-Erling Smørgrav 
267b7579f77SDag-Erling Smørgrav /* ------------------- Functions for worker -------------------- */
268b7579f77SDag-Erling Smørgrav 
269b7579f77SDag-Erling Smørgrav /**
270b7579f77SDag-Erling Smørgrav  * Allocate mesh, to empty.
271b7579f77SDag-Erling Smørgrav  * @param stack: module stack to activate, copied (as readonly reference).
272b7579f77SDag-Erling Smørgrav  * @param env: environment for new queries.
273b7579f77SDag-Erling Smørgrav  * @return mesh: the new mesh or NULL on error.
274b7579f77SDag-Erling Smørgrav  */
275b7579f77SDag-Erling Smørgrav struct mesh_area* mesh_create(struct module_stack* stack,
276b7579f77SDag-Erling Smørgrav 	struct module_env* env);
277b7579f77SDag-Erling Smørgrav 
278b7579f77SDag-Erling Smørgrav /**
279b7579f77SDag-Erling Smørgrav  * Delete mesh, and all query states and replies in it.
280b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh to delete.
281b7579f77SDag-Erling Smørgrav  */
282b7579f77SDag-Erling Smørgrav void mesh_delete(struct mesh_area* mesh);
283b7579f77SDag-Erling Smørgrav 
284b7579f77SDag-Erling Smørgrav /**
285b7579f77SDag-Erling Smørgrav  * New query incoming from clients. Create new query state if needed, and
286b7579f77SDag-Erling Smørgrav  * add mesh_reply to it. Returns error to client on malloc failures.
287b7579f77SDag-Erling Smørgrav  * Will run the mesh area queries to process if a new query state is created.
288b7579f77SDag-Erling Smørgrav  *
289b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh.
290b7579f77SDag-Erling Smørgrav  * @param qinfo: query from client.
29165b390aaSDag-Erling Smørgrav  * @param cinfo: additional information associated with the query client.
29265b390aaSDag-Erling Smørgrav  * 	'cinfo' itself is ephemeral but data pointed to by its members
29365b390aaSDag-Erling Smørgrav  *      can be assumed to be valid and unchanged until the query processing is
29465b390aaSDag-Erling Smørgrav  *      completed.
295b7579f77SDag-Erling Smørgrav  * @param qflags: flags from client query.
296b7579f77SDag-Erling Smørgrav  * @param edns: edns data from client query.
297b7579f77SDag-Erling Smørgrav  * @param rep: where to reply to.
298b7579f77SDag-Erling Smørgrav  * @param qid: query id to reply with.
299b7579f77SDag-Erling Smørgrav  */
300b7579f77SDag-Erling Smørgrav void mesh_new_client(struct mesh_area* mesh, struct query_info* qinfo,
30165b390aaSDag-Erling Smørgrav 	struct respip_client_info* cinfo, uint16_t qflags,
30265b390aaSDag-Erling Smørgrav 	struct edns_data* edns, struct comm_reply* rep, uint16_t qid);
303b7579f77SDag-Erling Smørgrav 
304b7579f77SDag-Erling Smørgrav /**
305b7579f77SDag-Erling Smørgrav  * New query with callback. Create new query state if needed, and
306b7579f77SDag-Erling Smørgrav  * add mesh_cb to it.
307b7579f77SDag-Erling Smørgrav  * Will run the mesh area queries to process if a new query state is created.
308b7579f77SDag-Erling Smørgrav  *
309b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh.
310b7579f77SDag-Erling Smørgrav  * @param qinfo: query from client.
311b7579f77SDag-Erling Smørgrav  * @param qflags: flags from client query.
312b7579f77SDag-Erling Smørgrav  * @param edns: edns data from client query.
313b7579f77SDag-Erling Smørgrav  * @param buf: buffer for reply contents.
314b7579f77SDag-Erling Smørgrav  * @param qid: query id to reply with.
315b7579f77SDag-Erling Smørgrav  * @param cb: callback function.
316b7579f77SDag-Erling Smørgrav  * @param cb_arg: callback user arg.
317b7579f77SDag-Erling Smørgrav  * @return 0 on error.
318b7579f77SDag-Erling Smørgrav  */
319b7579f77SDag-Erling Smørgrav int mesh_new_callback(struct mesh_area* mesh, struct query_info* qinfo,
32017d15b25SDag-Erling Smørgrav 	uint16_t qflags, struct edns_data* edns, struct sldns_buffer* buf,
3213005e0a3SDag-Erling Smørgrav 	uint16_t qid, mesh_cb_func_type cb, void* cb_arg);
322b7579f77SDag-Erling Smørgrav 
323b7579f77SDag-Erling Smørgrav /**
324b7579f77SDag-Erling Smørgrav  * New prefetch message. Create new query state if needed.
325b7579f77SDag-Erling Smørgrav  * Will run the mesh area queries to process if a new query state is created.
326b7579f77SDag-Erling Smørgrav  *
327b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh.
328b7579f77SDag-Erling Smørgrav  * @param qinfo: query from client.
329b7579f77SDag-Erling Smørgrav  * @param qflags: flags from client query.
330b7579f77SDag-Erling Smørgrav  * @param leeway: TTL leeway what to expire earlier for this update.
331b7579f77SDag-Erling Smørgrav  */
332b7579f77SDag-Erling Smørgrav void mesh_new_prefetch(struct mesh_area* mesh, struct query_info* qinfo,
33317d15b25SDag-Erling Smørgrav 	uint16_t qflags, time_t leeway);
334b7579f77SDag-Erling Smørgrav 
335b7579f77SDag-Erling Smørgrav /**
336b7579f77SDag-Erling Smørgrav  * Handle new event from the wire. A serviced query has returned.
337b7579f77SDag-Erling Smørgrav  * The query state will be made runnable, and the mesh_area will process
338b7579f77SDag-Erling Smørgrav  * query states until processing is complete.
339b7579f77SDag-Erling Smørgrav  *
340b7579f77SDag-Erling Smørgrav  * @param mesh: the query mesh.
341b7579f77SDag-Erling Smørgrav  * @param e: outbound entry, with query state to run and reply pointer.
342b7579f77SDag-Erling Smørgrav  * @param reply: the comm point reply info.
343b7579f77SDag-Erling Smørgrav  * @param what: NETEVENT_* error code (if not 0, what is wrong, TIMEOUT).
344b7579f77SDag-Erling Smørgrav  */
345b7579f77SDag-Erling Smørgrav void mesh_report_reply(struct mesh_area* mesh, struct outbound_entry* e,
346b7579f77SDag-Erling Smørgrav 	struct comm_reply* reply, int what);
347b7579f77SDag-Erling Smørgrav 
348b7579f77SDag-Erling Smørgrav /* ------------------- Functions for module environment --------------- */
349b7579f77SDag-Erling Smørgrav 
350b7579f77SDag-Erling Smørgrav /**
351b7579f77SDag-Erling Smørgrav  * Detach-subqueries.
352b7579f77SDag-Erling Smørgrav  * Remove all sub-query references from this query state.
353b7579f77SDag-Erling Smørgrav  * Keeps super-references of those sub-queries correct.
354b7579f77SDag-Erling Smørgrav  * Updates stat items in mesh_area structure.
355b7579f77SDag-Erling Smørgrav  * @param qstate: used to find mesh state.
356b7579f77SDag-Erling Smørgrav  */
357b7579f77SDag-Erling Smørgrav void mesh_detach_subs(struct module_qstate* qstate);
358b7579f77SDag-Erling Smørgrav 
359b7579f77SDag-Erling Smørgrav /**
360b7579f77SDag-Erling Smørgrav  * Attach subquery.
361b7579f77SDag-Erling Smørgrav  * Creates it if it does not exist already.
362b7579f77SDag-Erling Smørgrav  * Keeps sub and super references correct.
363b7579f77SDag-Erling Smørgrav  * Performs a cycle detection - for double check - and fails if there is one.
364b7579f77SDag-Erling Smørgrav  * Also fails if the sub-sub-references become too large.
365b7579f77SDag-Erling Smørgrav  * Updates stat items in mesh_area structure.
366b7579f77SDag-Erling Smørgrav  * Pass if it is priming query or not.
367b7579f77SDag-Erling Smørgrav  * return:
368b7579f77SDag-Erling Smørgrav  * 	o if error (malloc) happened.
369b7579f77SDag-Erling Smørgrav  * 	o need to initialise the new state (module init; it is a new state).
370b7579f77SDag-Erling Smørgrav  * 	  so that the next run of the query with this module is successful.
371b7579f77SDag-Erling Smørgrav  * 	o no init needed, attachment successful.
372b7579f77SDag-Erling Smørgrav  *
373b7579f77SDag-Erling Smørgrav  * @param qstate: the state to find mesh state, and that wants to receive
374b7579f77SDag-Erling Smørgrav  * 	the results from the new subquery.
375b7579f77SDag-Erling Smørgrav  * @param qinfo: what to query for (copied).
376b7579f77SDag-Erling Smørgrav  * @param qflags: what flags to use (RD / CD flag or not).
377b7579f77SDag-Erling Smørgrav  * @param prime: if it is a (stub) priming query.
378ff825849SDag-Erling Smørgrav  * @param valrec: if it is a validation recursion query (lookup of key, DS).
379b7579f77SDag-Erling Smørgrav  * @param newq: If the new subquery needs initialisation, it is returned,
380b7579f77SDag-Erling Smørgrav  * 	otherwise NULL is returned.
381b7579f77SDag-Erling Smørgrav  * @return: false on error, true if success (and init may be needed).
382b7579f77SDag-Erling Smørgrav  */
383b7579f77SDag-Erling Smørgrav int mesh_attach_sub(struct module_qstate* qstate, struct query_info* qinfo,
384ff825849SDag-Erling Smørgrav 	uint16_t qflags, int prime, int valrec, struct module_qstate** newq);
385b7579f77SDag-Erling Smørgrav 
386b7579f77SDag-Erling Smørgrav /**
387c7f4d7adSDag-Erling Smørgrav  * Add detached query.
388c7f4d7adSDag-Erling Smørgrav  * Creates it if it does not exist already.
389c7f4d7adSDag-Erling Smørgrav  * Does not make super/sub references.
390c7f4d7adSDag-Erling Smørgrav  * Performs a cycle detection - for double check - and fails if there is one.
391c7f4d7adSDag-Erling Smørgrav  * Updates stat items in mesh_area structure.
392c7f4d7adSDag-Erling Smørgrav  * Pass if it is priming query or not.
393c7f4d7adSDag-Erling Smørgrav  * return:
394c7f4d7adSDag-Erling Smørgrav  * 	o if error (malloc) happened.
395c7f4d7adSDag-Erling Smørgrav  * 	o need to initialise the new state (module init; it is a new state).
396c7f4d7adSDag-Erling Smørgrav  * 	  so that the next run of the query with this module is successful.
397c7f4d7adSDag-Erling Smørgrav  * 	o no init needed, attachment successful.
398c7f4d7adSDag-Erling Smørgrav  * 	o added subquery, created if it did not exist already.
399c7f4d7adSDag-Erling Smørgrav  *
400c7f4d7adSDag-Erling Smørgrav  * @param qstate: the state to find mesh state, and that wants to receive
401c7f4d7adSDag-Erling Smørgrav  * 	the results from the new subquery.
402c7f4d7adSDag-Erling Smørgrav  * @param qinfo: what to query for (copied).
403c7f4d7adSDag-Erling Smørgrav  * @param qflags: what flags to use (RD / CD flag or not).
404c7f4d7adSDag-Erling Smørgrav  * @param prime: if it is a (stub) priming query.
405c7f4d7adSDag-Erling Smørgrav  * @param valrec: if it is a validation recursion query (lookup of key, DS).
406c7f4d7adSDag-Erling Smørgrav  * @param newq: If the new subquery needs initialisation, it is returned,
407c7f4d7adSDag-Erling Smørgrav  * 	otherwise NULL is returned.
408c7f4d7adSDag-Erling Smørgrav  * @param sub: The added mesh state, created if it did not exist already.
409c7f4d7adSDag-Erling Smørgrav  * @return: false on error, true if success (and init may be needed).
410c7f4d7adSDag-Erling Smørgrav  */
411c7f4d7adSDag-Erling Smørgrav int mesh_add_sub(struct module_qstate* qstate, struct query_info* qinfo,
412c7f4d7adSDag-Erling Smørgrav         uint16_t qflags, int prime, int valrec, struct module_qstate** newq,
413c7f4d7adSDag-Erling Smørgrav 	struct mesh_state** sub);
414c7f4d7adSDag-Erling Smørgrav 
415c7f4d7adSDag-Erling Smørgrav /**
416b7579f77SDag-Erling Smørgrav  * Query state is done, send messages to reply entries.
417b7579f77SDag-Erling Smørgrav  * Encode messages using reply entry values and the querystate (with original
418b7579f77SDag-Erling Smørgrav  * qinfo), using given reply_info.
419b7579f77SDag-Erling Smørgrav  * Pass errcode != 0 if an error reply is needed.
420b7579f77SDag-Erling Smørgrav  * If no reply entries, nothing is done.
421b7579f77SDag-Erling Smørgrav  * Must be called before a module can module_finished or return module_error.
422b7579f77SDag-Erling Smørgrav  * The module must handle the super query states itself as well.
423b7579f77SDag-Erling Smørgrav  *
424b7579f77SDag-Erling Smørgrav  * @param mstate: mesh state that is done. return_rcode and return_msg
425b7579f77SDag-Erling Smørgrav  * 	are used for replies.
426b7579f77SDag-Erling Smørgrav  * 	return_rcode: if not 0 (NOERROR) an error is sent back (and
427b7579f77SDag-Erling Smørgrav  * 		return_msg is ignored).
428b7579f77SDag-Erling Smørgrav  * 	return_msg: reply to encode and send back to clients.
429b7579f77SDag-Erling Smørgrav  */
430b7579f77SDag-Erling Smørgrav void mesh_query_done(struct mesh_state* mstate);
431b7579f77SDag-Erling Smørgrav 
432b7579f77SDag-Erling Smørgrav /**
433b7579f77SDag-Erling Smørgrav  * Call inform_super for the super query states that are interested in the
434b7579f77SDag-Erling Smørgrav  * results from this query state. These can then be changed for error
435b7579f77SDag-Erling Smørgrav  * or results.
436b7579f77SDag-Erling Smørgrav  * Called when a module is module_finished or returns module_error.
437b7579f77SDag-Erling Smørgrav  * The super query states become runnable with event module_event_pass,
438b7579f77SDag-Erling Smørgrav  * it calls the current module for the super with the inform_super event.
439b7579f77SDag-Erling Smørgrav  *
440b7579f77SDag-Erling Smørgrav  * @param mesh: mesh area to add newly runnable modules to.
441b7579f77SDag-Erling Smørgrav  * @param mstate: the state that has results, used to find mesh state.
442b7579f77SDag-Erling Smørgrav  */
443b7579f77SDag-Erling Smørgrav void mesh_walk_supers(struct mesh_area* mesh, struct mesh_state* mstate);
444b7579f77SDag-Erling Smørgrav 
445b7579f77SDag-Erling Smørgrav /**
446b7579f77SDag-Erling Smørgrav  * Delete mesh state, cleanup and also rbtrees and so on.
447b7579f77SDag-Erling Smørgrav  * Will detach from all super/subnodes.
448b7579f77SDag-Erling Smørgrav  * @param qstate: to remove.
449b7579f77SDag-Erling Smørgrav  */
450b7579f77SDag-Erling Smørgrav void mesh_state_delete(struct module_qstate* qstate);
451b7579f77SDag-Erling Smørgrav 
452b7579f77SDag-Erling Smørgrav /* ------------------- Functions for mesh -------------------- */
453b7579f77SDag-Erling Smørgrav 
454b7579f77SDag-Erling Smørgrav /**
455b7579f77SDag-Erling Smørgrav  * Create and initialize a new mesh state and its query state
456b7579f77SDag-Erling Smørgrav  * Does not put the mesh state into rbtrees and so on.
457b7579f77SDag-Erling Smørgrav  * @param env: module environment to set.
458b7579f77SDag-Erling Smørgrav  * @param qinfo: query info that the mesh is for.
45965b390aaSDag-Erling Smørgrav  * @param cinfo: control info for the query client (can be NULL).
460b7579f77SDag-Erling Smørgrav  * @param qflags: flags for query (RD / CD flag).
461b7579f77SDag-Erling Smørgrav  * @param prime: if true, it is a priming query, set is_priming on mesh state.
462ff825849SDag-Erling Smørgrav  * @param valrec: if true, it is a validation recursion query, and sets
463ff825849SDag-Erling Smørgrav  * 	is_valrec on the mesh state.
464b7579f77SDag-Erling Smørgrav  * @return: new mesh state or NULL on allocation error.
465b7579f77SDag-Erling Smørgrav  */
466b7579f77SDag-Erling Smørgrav struct mesh_state* mesh_state_create(struct module_env* env,
46765b390aaSDag-Erling Smørgrav 	struct query_info* qinfo, struct respip_client_info* cinfo,
46865b390aaSDag-Erling Smørgrav 	uint16_t qflags, int prime, int valrec);
469b7579f77SDag-Erling Smørgrav 
470b7579f77SDag-Erling Smørgrav /**
471bc892140SDag-Erling Smørgrav  * Check if the mesh state is unique.
472bc892140SDag-Erling Smørgrav  * A unique mesh state uses it's unique member to point to itself, else NULL.
473bc892140SDag-Erling Smørgrav  * @param mstate: mesh state to check.
474bc892140SDag-Erling Smørgrav  * @return true if the mesh state is unique, false otherwise.
475bc892140SDag-Erling Smørgrav  */
476bc892140SDag-Erling Smørgrav int mesh_state_is_unique(struct mesh_state* mstate);
477bc892140SDag-Erling Smørgrav 
478bc892140SDag-Erling Smørgrav /**
479bc892140SDag-Erling Smørgrav  * Make a mesh state unique.
480bc892140SDag-Erling Smørgrav  * A unique mesh state uses it's unique member to point to itself.
481bc892140SDag-Erling Smørgrav  * @param mstate: mesh state to check.
482bc892140SDag-Erling Smørgrav  */
483bc892140SDag-Erling Smørgrav void mesh_state_make_unique(struct mesh_state* mstate);
484bc892140SDag-Erling Smørgrav 
485bc892140SDag-Erling Smørgrav /**
486b7579f77SDag-Erling Smørgrav  * Cleanup a mesh state and its query state. Does not do rbtree or
487b7579f77SDag-Erling Smørgrav  * reference cleanup.
488b7579f77SDag-Erling Smørgrav  * @param mstate: mesh state to cleanup. Its pointer may no longer be used
489b7579f77SDag-Erling Smørgrav  * 	afterwards. Cleanup rbtrees before calling this function.
490b7579f77SDag-Erling Smørgrav  */
491b7579f77SDag-Erling Smørgrav void mesh_state_cleanup(struct mesh_state* mstate);
492b7579f77SDag-Erling Smørgrav 
493b7579f77SDag-Erling Smørgrav /**
494b7579f77SDag-Erling Smørgrav  * Delete all mesh states from the mesh.
495b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh area to clear
496b7579f77SDag-Erling Smørgrav  */
497b7579f77SDag-Erling Smørgrav void mesh_delete_all(struct mesh_area* mesh);
498b7579f77SDag-Erling Smørgrav 
499b7579f77SDag-Erling Smørgrav /**
500b7579f77SDag-Erling Smørgrav  * Find a mesh state in the mesh area. Pass relevant flags.
501b7579f77SDag-Erling Smørgrav  *
502b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh area to look in.
50365b390aaSDag-Erling Smørgrav  * @param cinfo: if non-NULL client specific info that may affect IP-based
50465b390aaSDag-Erling Smørgrav  * 	actions that apply to the query result.
505b7579f77SDag-Erling Smørgrav  * @param qinfo: what query
506b7579f77SDag-Erling Smørgrav  * @param qflags: if RD / CD bit is set or not.
507b7579f77SDag-Erling Smørgrav  * @param prime: if it is a priming query.
508ff825849SDag-Erling Smørgrav  * @param valrec: if it is a validation-recursion query.
509b7579f77SDag-Erling Smørgrav  * @return: mesh state or NULL if not found.
510b7579f77SDag-Erling Smørgrav  */
511b7579f77SDag-Erling Smørgrav struct mesh_state* mesh_area_find(struct mesh_area* mesh,
51265b390aaSDag-Erling Smørgrav 	struct respip_client_info* cinfo, struct query_info* qinfo,
51365b390aaSDag-Erling Smørgrav 	uint16_t qflags, int prime, int valrec);
514b7579f77SDag-Erling Smørgrav 
515b7579f77SDag-Erling Smørgrav /**
516b7579f77SDag-Erling Smørgrav  * Setup attachment super/sub relation between super and sub mesh state.
517b7579f77SDag-Erling Smørgrav  * The relation must not be present when calling the function.
518b7579f77SDag-Erling Smørgrav  * Does not update stat items in mesh_area.
519b7579f77SDag-Erling Smørgrav  * @param super: super state.
520b7579f77SDag-Erling Smørgrav  * @param sub: sub state.
521b7579f77SDag-Erling Smørgrav  * @return: 0 on alloc error.
522b7579f77SDag-Erling Smørgrav  */
523b7579f77SDag-Erling Smørgrav int mesh_state_attachment(struct mesh_state* super, struct mesh_state* sub);
524b7579f77SDag-Erling Smørgrav 
525b7579f77SDag-Erling Smørgrav /**
526b7579f77SDag-Erling Smørgrav  * Create new reply structure and attach it to a mesh state.
527b7579f77SDag-Erling Smørgrav  * Does not update stat items in mesh area.
528b7579f77SDag-Erling Smørgrav  * @param s: the mesh state.
529b7579f77SDag-Erling Smørgrav  * @param edns: edns data for reply (bufsize).
530b7579f77SDag-Erling Smørgrav  * @param rep: comm point reply info.
531b7579f77SDag-Erling Smørgrav  * @param qid: ID of reply.
532b7579f77SDag-Erling Smørgrav  * @param qflags: original query flags.
533bc892140SDag-Erling Smørgrav  * @param qinfo: original query info.
534b7579f77SDag-Erling Smørgrav  * @return: 0 on alloc error.
535b7579f77SDag-Erling Smørgrav  */
536b7579f77SDag-Erling Smørgrav int mesh_state_add_reply(struct mesh_state* s, struct edns_data* edns,
537bc892140SDag-Erling Smørgrav 	struct comm_reply* rep, uint16_t qid, uint16_t qflags,
538bc892140SDag-Erling Smørgrav 	const struct query_info* qinfo);
539b7579f77SDag-Erling Smørgrav 
540b7579f77SDag-Erling Smørgrav /**
541b7579f77SDag-Erling Smørgrav  * Create new callback structure and attach it to a mesh state.
542b7579f77SDag-Erling Smørgrav  * Does not update stat items in mesh area.
543b7579f77SDag-Erling Smørgrav  * @param s: the mesh state.
544b7579f77SDag-Erling Smørgrav  * @param edns: edns data for reply (bufsize).
545b7579f77SDag-Erling Smørgrav  * @param buf: buffer for reply
546b7579f77SDag-Erling Smørgrav  * @param cb: callback to call with results.
547b7579f77SDag-Erling Smørgrav  * @param cb_arg: callback user arg.
548b7579f77SDag-Erling Smørgrav  * @param qid: ID of reply.
549b7579f77SDag-Erling Smørgrav  * @param qflags: original query flags.
550b7579f77SDag-Erling Smørgrav  * @return: 0 on alloc error.
551b7579f77SDag-Erling Smørgrav  */
552b7579f77SDag-Erling Smørgrav int mesh_state_add_cb(struct mesh_state* s, struct edns_data* edns,
5533005e0a3SDag-Erling Smørgrav         struct sldns_buffer* buf, mesh_cb_func_type cb, void* cb_arg,
5543005e0a3SDag-Erling Smørgrav 	uint16_t qid, uint16_t qflags);
555b7579f77SDag-Erling Smørgrav 
556b7579f77SDag-Erling Smørgrav /**
557b7579f77SDag-Erling Smørgrav  * Run the mesh. Run all runnable mesh states. Which can create new
558b7579f77SDag-Erling Smørgrav  * runnable mesh states. Until completion. Automatically called by
559b7579f77SDag-Erling Smørgrav  * mesh_report_reply and mesh_new_client as needed.
560b7579f77SDag-Erling Smørgrav  * @param mesh: mesh area.
561b7579f77SDag-Erling Smørgrav  * @param mstate: first mesh state to run.
562b7579f77SDag-Erling Smørgrav  * @param ev: event the mstate. Others get event_pass.
563b7579f77SDag-Erling Smørgrav  * @param e: if a reply, its outbound entry.
564b7579f77SDag-Erling Smørgrav  */
565b7579f77SDag-Erling Smørgrav void mesh_run(struct mesh_area* mesh, struct mesh_state* mstate,
566b7579f77SDag-Erling Smørgrav 	enum module_ev ev, struct outbound_entry* e);
567b7579f77SDag-Erling Smørgrav 
568b7579f77SDag-Erling Smørgrav /**
569b7579f77SDag-Erling Smørgrav  * Print some stats about the mesh to the log.
570b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh to print it for.
571b7579f77SDag-Erling Smørgrav  * @param str: descriptive string to go with it.
572b7579f77SDag-Erling Smørgrav  */
573b7579f77SDag-Erling Smørgrav void mesh_stats(struct mesh_area* mesh, const char* str);
574b7579f77SDag-Erling Smørgrav 
575b7579f77SDag-Erling Smørgrav /**
576b7579f77SDag-Erling Smørgrav  * Clear the stats that the mesh keeps (number of queries serviced)
577b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh
578b7579f77SDag-Erling Smørgrav  */
579b7579f77SDag-Erling Smørgrav void mesh_stats_clear(struct mesh_area* mesh);
580b7579f77SDag-Erling Smørgrav 
581b7579f77SDag-Erling Smørgrav /**
582b7579f77SDag-Erling Smørgrav  * Print all the states in the mesh to the log.
583b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh to print all states of.
584b7579f77SDag-Erling Smørgrav  */
585b7579f77SDag-Erling Smørgrav void mesh_log_list(struct mesh_area* mesh);
586b7579f77SDag-Erling Smørgrav 
587b7579f77SDag-Erling Smørgrav /**
588b7579f77SDag-Erling Smørgrav  * Calculate memory size in use by mesh and all queries inside it.
589b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh to examine.
590b7579f77SDag-Erling Smørgrav  * @return size in bytes.
591b7579f77SDag-Erling Smørgrav  */
592b7579f77SDag-Erling Smørgrav size_t mesh_get_mem(struct mesh_area* mesh);
593b7579f77SDag-Erling Smørgrav 
594b7579f77SDag-Erling Smørgrav /**
595b7579f77SDag-Erling Smørgrav  * Find cycle; see if the given mesh is in the targets sub, or sub-sub, ...
596b7579f77SDag-Erling Smørgrav  * trees.
597b7579f77SDag-Erling Smørgrav  * If the sub-sub structure is too large, it returns 'a cycle'=2.
598b7579f77SDag-Erling Smørgrav  * @param qstate: given mesh querystate.
599b7579f77SDag-Erling Smørgrav  * @param qinfo: query info for dependency.
600b7579f77SDag-Erling Smørgrav  * @param flags: query flags of dependency.
601b7579f77SDag-Erling Smørgrav  * @param prime: if dependency is a priming query or not.
602ff825849SDag-Erling Smørgrav  * @param valrec: if it is a validation recursion query (lookup of key, DS).
603b7579f77SDag-Erling Smørgrav  * @return true if the name,type,class exists and the given qstate mesh exists
604b7579f77SDag-Erling Smørgrav  * 	as a dependency of that name. Thus if qstate becomes dependent on
605b7579f77SDag-Erling Smørgrav  * 	name,type,class then a cycle is created, this is return value 1.
606b7579f77SDag-Erling Smørgrav  * 	Too large to search is value 2 (also true).
607b7579f77SDag-Erling Smørgrav  */
608b7579f77SDag-Erling Smørgrav int mesh_detect_cycle(struct module_qstate* qstate, struct query_info* qinfo,
609ff825849SDag-Erling Smørgrav 	uint16_t flags, int prime, int valrec);
610b7579f77SDag-Erling Smørgrav 
611b7579f77SDag-Erling Smørgrav /** compare two mesh_states */
612b7579f77SDag-Erling Smørgrav int mesh_state_compare(const void* ap, const void* bp);
613b7579f77SDag-Erling Smørgrav 
614b7579f77SDag-Erling Smørgrav /** compare two mesh references */
615b7579f77SDag-Erling Smørgrav int mesh_state_ref_compare(const void* ap, const void* bp);
616b7579f77SDag-Erling Smørgrav 
617b7579f77SDag-Erling Smørgrav /**
618b7579f77SDag-Erling Smørgrav  * Make space for another recursion state for a reply in the mesh
619b7579f77SDag-Erling Smørgrav  * @param mesh: mesh area
620b7579f77SDag-Erling Smørgrav  * @param qbuf: query buffer to save if recursion is invoked to make space.
621b7579f77SDag-Erling Smørgrav  *    This buffer is necessary, because the following sequence in calls
622b7579f77SDag-Erling Smørgrav  *    can result in an overwrite of the incoming query:
623b7579f77SDag-Erling Smørgrav  *    delete_other_mesh_query - iter_clean - serviced_delete - waiting
624b7579f77SDag-Erling Smørgrav  *    udp query is sent - on error callback - callback sends SERVFAIL reply
625b7579f77SDag-Erling Smørgrav  *    over the same network channel, and shared UDP buffer is overwritten.
626b7579f77SDag-Erling Smørgrav  *    You can pass NULL if there is no buffer that must be backed up.
627b7579f77SDag-Erling Smørgrav  * @return false if no space is available.
628b7579f77SDag-Erling Smørgrav  */
62917d15b25SDag-Erling Smørgrav int mesh_make_new_space(struct mesh_area* mesh, struct sldns_buffer* qbuf);
630b7579f77SDag-Erling Smørgrav 
631b7579f77SDag-Erling Smørgrav /**
632b7579f77SDag-Erling Smørgrav  * Insert mesh state into a double linked list.  Inserted at end.
633b7579f77SDag-Erling Smørgrav  * @param m: mesh state.
634b7579f77SDag-Erling Smørgrav  * @param fp: pointer to the first-elem-pointer of the list.
635b7579f77SDag-Erling Smørgrav  * @param lp: pointer to the last-elem-pointer of the list.
636b7579f77SDag-Erling Smørgrav  */
637b7579f77SDag-Erling Smørgrav void mesh_list_insert(struct mesh_state* m, struct mesh_state** fp,
638b7579f77SDag-Erling Smørgrav 	struct mesh_state** lp);
639b7579f77SDag-Erling Smørgrav 
640b7579f77SDag-Erling Smørgrav /**
641b7579f77SDag-Erling Smørgrav  * Remove mesh state from a double linked list.  Remove from any position.
642b7579f77SDag-Erling Smørgrav  * @param m: mesh state.
643b7579f77SDag-Erling Smørgrav  * @param fp: pointer to the first-elem-pointer of the list.
644b7579f77SDag-Erling Smørgrav  * @param lp: pointer to the last-elem-pointer of the list.
645b7579f77SDag-Erling Smørgrav  */
646b7579f77SDag-Erling Smørgrav void mesh_list_remove(struct mesh_state* m, struct mesh_state** fp,
647b7579f77SDag-Erling Smørgrav 	struct mesh_state** lp);
648b7579f77SDag-Erling Smørgrav 
649e86b9096SDag-Erling Smørgrav /**
650e86b9096SDag-Erling Smørgrav  * Remove mesh reply entry from the reply entry list.  Searches for
651e86b9096SDag-Erling Smørgrav  * the comm_point pointer.
652e86b9096SDag-Erling Smørgrav  * @param mesh: to update the counters.
653e86b9096SDag-Erling Smørgrav  * @param m: the mesh state.
654e86b9096SDag-Erling Smørgrav  * @param cp: the comm_point to remove from the list.
655e86b9096SDag-Erling Smørgrav  */
656e86b9096SDag-Erling Smørgrav void mesh_state_remove_reply(struct mesh_area* mesh, struct mesh_state* m,
657e86b9096SDag-Erling Smørgrav 	struct comm_point* cp);
658e86b9096SDag-Erling Smørgrav 
659091e9e46SCy Schubert /** Callback for when the serve expired client timer has run out.  Tries to
660091e9e46SCy Schubert  * find an expired answer in the cache and reply that to the client.
661091e9e46SCy Schubert  * @param arg: the argument passed to the callback.
662091e9e46SCy Schubert  */
663091e9e46SCy Schubert void mesh_serve_expired_callback(void* arg);
664091e9e46SCy Schubert 
665091e9e46SCy Schubert /**
666091e9e46SCy Schubert  * Try to get a (expired) cached answer.
667091e9e46SCy Schubert  * This needs to behave like the worker's answer_from_cache() in order to have
668091e9e46SCy Schubert  * the same behavior as when replying from cache.
669091e9e46SCy Schubert  * @param qstate: the module qstate.
670091e9e46SCy Schubert  * @param lookup_qinfo: the query info to look for in the cache.
671091e9e46SCy Schubert  * @return dns_msg if a cached answer was found, otherwise NULL.
672091e9e46SCy Schubert  */
673091e9e46SCy Schubert struct dns_msg*
674091e9e46SCy Schubert mesh_serve_expired_lookup(struct module_qstate* qstate,
675091e9e46SCy Schubert 	struct query_info* lookup_qinfo);
676091e9e46SCy Schubert 
677b7579f77SDag-Erling Smørgrav #endif /* SERVICES_MESH_H */
678