xref: /freebsd/contrib/unbound/services/mesh.h (revision a39a5a6905612447def27b66ffe73b9d11efd80c)
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;
233c0caa2e2SCy Schubert 	/** send query to this http2 stream, if set */
234c0caa2e2SCy 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.
299*a39a5a69SCy Schubert  * @param rpz_passthru: if true, the rpz passthru was previously found and
300*a39a5a69SCy Schubert  * 	further rpz processing is stopped.
301b7579f77SDag-Erling Smørgrav  */
302b7579f77SDag-Erling Smørgrav void mesh_new_client(struct mesh_area* mesh, struct query_info* qinfo,
30365b390aaSDag-Erling Smørgrav 	struct respip_client_info* cinfo, uint16_t qflags,
304*a39a5a69SCy Schubert 	struct edns_data* edns, struct comm_reply* rep, uint16_t qid,
305*a39a5a69SCy Schubert 	int rpz_passthru);
306b7579f77SDag-Erling Smørgrav 
307b7579f77SDag-Erling Smørgrav /**
308b7579f77SDag-Erling Smørgrav  * New query with callback. Create new query state if needed, and
309b7579f77SDag-Erling Smørgrav  * add mesh_cb to it.
310b7579f77SDag-Erling Smørgrav  * Will run the mesh area queries to process if a new query state is created.
311b7579f77SDag-Erling Smørgrav  *
312b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh.
313b7579f77SDag-Erling Smørgrav  * @param qinfo: query from client.
314b7579f77SDag-Erling Smørgrav  * @param qflags: flags from client query.
315b7579f77SDag-Erling Smørgrav  * @param edns: edns data from client query.
316b7579f77SDag-Erling Smørgrav  * @param buf: buffer for reply contents.
317b7579f77SDag-Erling Smørgrav  * @param qid: query id to reply with.
318b7579f77SDag-Erling Smørgrav  * @param cb: callback function.
319b7579f77SDag-Erling Smørgrav  * @param cb_arg: callback user arg.
320*a39a5a69SCy Schubert  * @param rpz_passthru: if true, the rpz passthru was previously found and
321*a39a5a69SCy Schubert  * 	further rpz processing is stopped.
322b7579f77SDag-Erling Smørgrav  * @return 0 on error.
323b7579f77SDag-Erling Smørgrav  */
324b7579f77SDag-Erling Smørgrav int mesh_new_callback(struct mesh_area* mesh, struct query_info* qinfo,
32517d15b25SDag-Erling Smørgrav 	uint16_t qflags, struct edns_data* edns, struct sldns_buffer* buf,
326*a39a5a69SCy Schubert 	uint16_t qid, mesh_cb_func_type cb, void* cb_arg, int rpz_passthru);
327b7579f77SDag-Erling Smørgrav 
328b7579f77SDag-Erling Smørgrav /**
329b7579f77SDag-Erling Smørgrav  * New prefetch message. Create new query state if needed.
330b7579f77SDag-Erling Smørgrav  * Will run the mesh area queries to process if a new query state is created.
331b7579f77SDag-Erling Smørgrav  *
332b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh.
333b7579f77SDag-Erling Smørgrav  * @param qinfo: query from client.
334b7579f77SDag-Erling Smørgrav  * @param qflags: flags from client query.
335b7579f77SDag-Erling Smørgrav  * @param leeway: TTL leeway what to expire earlier for this update.
336*a39a5a69SCy Schubert  * @param rpz_passthru: if true, the rpz passthru was previously found and
337*a39a5a69SCy Schubert  * 	further rpz processing is stopped.
338*a39a5a69SCy Schubert  * @param rep: comm_reply for the client; to be used when subnet is enabled.
339*a39a5a69SCy Schubert  * @param opt_list: edns opt_list from the client; to be used when subnet is
340*a39a5a69SCy Schubert  *	enabled.
341b7579f77SDag-Erling Smørgrav  */
342b7579f77SDag-Erling Smørgrav void mesh_new_prefetch(struct mesh_area* mesh, struct query_info* qinfo,
343*a39a5a69SCy Schubert 	uint16_t qflags, time_t leeway, int rpz_passthru,
344*a39a5a69SCy Schubert 	struct comm_reply* rep, struct edns_option* opt_list);
345b7579f77SDag-Erling Smørgrav 
346b7579f77SDag-Erling Smørgrav /**
347b7579f77SDag-Erling Smørgrav  * Handle new event from the wire. A serviced query has returned.
348b7579f77SDag-Erling Smørgrav  * The query state will be made runnable, and the mesh_area will process
349b7579f77SDag-Erling Smørgrav  * query states until processing is complete.
350b7579f77SDag-Erling Smørgrav  *
351b7579f77SDag-Erling Smørgrav  * @param mesh: the query mesh.
352b7579f77SDag-Erling Smørgrav  * @param e: outbound entry, with query state to run and reply pointer.
353b7579f77SDag-Erling Smørgrav  * @param reply: the comm point reply info.
354b7579f77SDag-Erling Smørgrav  * @param what: NETEVENT_* error code (if not 0, what is wrong, TIMEOUT).
355b7579f77SDag-Erling Smørgrav  */
356b7579f77SDag-Erling Smørgrav void mesh_report_reply(struct mesh_area* mesh, struct outbound_entry* e,
357b7579f77SDag-Erling Smørgrav 	struct comm_reply* reply, int what);
358b7579f77SDag-Erling Smørgrav 
359b7579f77SDag-Erling Smørgrav /* ------------------- Functions for module environment --------------- */
360b7579f77SDag-Erling Smørgrav 
361b7579f77SDag-Erling Smørgrav /**
362b7579f77SDag-Erling Smørgrav  * Detach-subqueries.
363b7579f77SDag-Erling Smørgrav  * Remove all sub-query references from this query state.
364b7579f77SDag-Erling Smørgrav  * Keeps super-references of those sub-queries correct.
365b7579f77SDag-Erling Smørgrav  * Updates stat items in mesh_area structure.
366b7579f77SDag-Erling Smørgrav  * @param qstate: used to find mesh state.
367b7579f77SDag-Erling Smørgrav  */
368b7579f77SDag-Erling Smørgrav void mesh_detach_subs(struct module_qstate* qstate);
369b7579f77SDag-Erling Smørgrav 
370b7579f77SDag-Erling Smørgrav /**
371b7579f77SDag-Erling Smørgrav  * Attach subquery.
372b7579f77SDag-Erling Smørgrav  * Creates it if it does not exist already.
373b7579f77SDag-Erling Smørgrav  * Keeps sub and super references correct.
374b7579f77SDag-Erling Smørgrav  * Performs a cycle detection - for double check - and fails if there is one.
375b7579f77SDag-Erling Smørgrav  * Also fails if the sub-sub-references become too large.
376b7579f77SDag-Erling Smørgrav  * Updates stat items in mesh_area structure.
377b7579f77SDag-Erling Smørgrav  * Pass if it is priming query or not.
378b7579f77SDag-Erling Smørgrav  * return:
379b7579f77SDag-Erling Smørgrav  * 	o if error (malloc) happened.
380b7579f77SDag-Erling Smørgrav  * 	o need to initialise the new state (module init; it is a new state).
381b7579f77SDag-Erling Smørgrav  * 	  so that the next run of the query with this module is successful.
382b7579f77SDag-Erling Smørgrav  * 	o no init needed, attachment successful.
383b7579f77SDag-Erling Smørgrav  *
384b7579f77SDag-Erling Smørgrav  * @param qstate: the state to find mesh state, and that wants to receive
385b7579f77SDag-Erling Smørgrav  * 	the results from the new subquery.
386b7579f77SDag-Erling Smørgrav  * @param qinfo: what to query for (copied).
387b7579f77SDag-Erling Smørgrav  * @param qflags: what flags to use (RD / CD flag or not).
388b7579f77SDag-Erling Smørgrav  * @param prime: if it is a (stub) priming query.
389ff825849SDag-Erling Smørgrav  * @param valrec: if it is a validation recursion query (lookup of key, DS).
390b7579f77SDag-Erling Smørgrav  * @param newq: If the new subquery needs initialisation, it is returned,
391b7579f77SDag-Erling Smørgrav  * 	otherwise NULL is returned.
392b7579f77SDag-Erling Smørgrav  * @return: false on error, true if success (and init may be needed).
393b7579f77SDag-Erling Smørgrav  */
394b7579f77SDag-Erling Smørgrav int mesh_attach_sub(struct module_qstate* qstate, struct query_info* qinfo,
395ff825849SDag-Erling Smørgrav 	uint16_t qflags, int prime, int valrec, struct module_qstate** newq);
396b7579f77SDag-Erling Smørgrav 
397b7579f77SDag-Erling Smørgrav /**
398c7f4d7adSDag-Erling Smørgrav  * Add detached query.
399c7f4d7adSDag-Erling Smørgrav  * Creates it if it does not exist already.
400c7f4d7adSDag-Erling Smørgrav  * Does not make super/sub references.
401c7f4d7adSDag-Erling Smørgrav  * Performs a cycle detection - for double check - and fails if there is one.
402c7f4d7adSDag-Erling Smørgrav  * Updates stat items in mesh_area structure.
403c7f4d7adSDag-Erling Smørgrav  * Pass if it is priming query or not.
404c7f4d7adSDag-Erling Smørgrav  * return:
405c7f4d7adSDag-Erling Smørgrav  * 	o if error (malloc) happened.
406c7f4d7adSDag-Erling Smørgrav  * 	o need to initialise the new state (module init; it is a new state).
407c7f4d7adSDag-Erling Smørgrav  * 	  so that the next run of the query with this module is successful.
408c7f4d7adSDag-Erling Smørgrav  * 	o no init needed, attachment successful.
409c7f4d7adSDag-Erling Smørgrav  * 	o added subquery, created if it did not exist already.
410c7f4d7adSDag-Erling Smørgrav  *
411c7f4d7adSDag-Erling Smørgrav  * @param qstate: the state to find mesh state, and that wants to receive
412c7f4d7adSDag-Erling Smørgrav  * 	the results from the new subquery.
413c7f4d7adSDag-Erling Smørgrav  * @param qinfo: what to query for (copied).
414c7f4d7adSDag-Erling Smørgrav  * @param qflags: what flags to use (RD / CD flag or not).
415c7f4d7adSDag-Erling Smørgrav  * @param prime: if it is a (stub) priming query.
416c7f4d7adSDag-Erling Smørgrav  * @param valrec: if it is a validation recursion query (lookup of key, DS).
417c7f4d7adSDag-Erling Smørgrav  * @param newq: If the new subquery needs initialisation, it is returned,
418c7f4d7adSDag-Erling Smørgrav  * 	otherwise NULL is returned.
419c7f4d7adSDag-Erling Smørgrav  * @param sub: The added mesh state, created if it did not exist already.
420c7f4d7adSDag-Erling Smørgrav  * @return: false on error, true if success (and init may be needed).
421c7f4d7adSDag-Erling Smørgrav  */
422c7f4d7adSDag-Erling Smørgrav int mesh_add_sub(struct module_qstate* qstate, struct query_info* qinfo,
423c7f4d7adSDag-Erling Smørgrav         uint16_t qflags, int prime, int valrec, struct module_qstate** newq,
424c7f4d7adSDag-Erling Smørgrav 	struct mesh_state** sub);
425c7f4d7adSDag-Erling Smørgrav 
426c7f4d7adSDag-Erling Smørgrav /**
427b7579f77SDag-Erling Smørgrav  * Query state is done, send messages to reply entries.
428b7579f77SDag-Erling Smørgrav  * Encode messages using reply entry values and the querystate (with original
429b7579f77SDag-Erling Smørgrav  * qinfo), using given reply_info.
430b7579f77SDag-Erling Smørgrav  * Pass errcode != 0 if an error reply is needed.
431b7579f77SDag-Erling Smørgrav  * If no reply entries, nothing is done.
432b7579f77SDag-Erling Smørgrav  * Must be called before a module can module_finished or return module_error.
433b7579f77SDag-Erling Smørgrav  * The module must handle the super query states itself as well.
434b7579f77SDag-Erling Smørgrav  *
435b7579f77SDag-Erling Smørgrav  * @param mstate: mesh state that is done. return_rcode and return_msg
436b7579f77SDag-Erling Smørgrav  * 	are used for replies.
437b7579f77SDag-Erling Smørgrav  * 	return_rcode: if not 0 (NOERROR) an error is sent back (and
438b7579f77SDag-Erling Smørgrav  * 		return_msg is ignored).
439b7579f77SDag-Erling Smørgrav  * 	return_msg: reply to encode and send back to clients.
440b7579f77SDag-Erling Smørgrav  */
441b7579f77SDag-Erling Smørgrav void mesh_query_done(struct mesh_state* mstate);
442b7579f77SDag-Erling Smørgrav 
443b7579f77SDag-Erling Smørgrav /**
444b7579f77SDag-Erling Smørgrav  * Call inform_super for the super query states that are interested in the
445b7579f77SDag-Erling Smørgrav  * results from this query state. These can then be changed for error
446b7579f77SDag-Erling Smørgrav  * or results.
447b7579f77SDag-Erling Smørgrav  * Called when a module is module_finished or returns module_error.
448b7579f77SDag-Erling Smørgrav  * The super query states become runnable with event module_event_pass,
449b7579f77SDag-Erling Smørgrav  * it calls the current module for the super with the inform_super event.
450b7579f77SDag-Erling Smørgrav  *
451b7579f77SDag-Erling Smørgrav  * @param mesh: mesh area to add newly runnable modules to.
452b7579f77SDag-Erling Smørgrav  * @param mstate: the state that has results, used to find mesh state.
453b7579f77SDag-Erling Smørgrav  */
454b7579f77SDag-Erling Smørgrav void mesh_walk_supers(struct mesh_area* mesh, struct mesh_state* mstate);
455b7579f77SDag-Erling Smørgrav 
456b7579f77SDag-Erling Smørgrav /**
457b7579f77SDag-Erling Smørgrav  * Delete mesh state, cleanup and also rbtrees and so on.
458b7579f77SDag-Erling Smørgrav  * Will detach from all super/subnodes.
459b7579f77SDag-Erling Smørgrav  * @param qstate: to remove.
460b7579f77SDag-Erling Smørgrav  */
461b7579f77SDag-Erling Smørgrav void mesh_state_delete(struct module_qstate* qstate);
462b7579f77SDag-Erling Smørgrav 
463b7579f77SDag-Erling Smørgrav /* ------------------- Functions for mesh -------------------- */
464b7579f77SDag-Erling Smørgrav 
465b7579f77SDag-Erling Smørgrav /**
466b7579f77SDag-Erling Smørgrav  * Create and initialize a new mesh state and its query state
467b7579f77SDag-Erling Smørgrav  * Does not put the mesh state into rbtrees and so on.
468b7579f77SDag-Erling Smørgrav  * @param env: module environment to set.
469b7579f77SDag-Erling Smørgrav  * @param qinfo: query info that the mesh is for.
47065b390aaSDag-Erling Smørgrav  * @param cinfo: control info for the query client (can be NULL).
471b7579f77SDag-Erling Smørgrav  * @param qflags: flags for query (RD / CD flag).
472b7579f77SDag-Erling Smørgrav  * @param prime: if true, it is a priming query, set is_priming on mesh state.
473ff825849SDag-Erling Smørgrav  * @param valrec: if true, it is a validation recursion query, and sets
474ff825849SDag-Erling Smørgrav  * 	is_valrec on the mesh state.
475b7579f77SDag-Erling Smørgrav  * @return: new mesh state or NULL on allocation error.
476b7579f77SDag-Erling Smørgrav  */
477b7579f77SDag-Erling Smørgrav struct mesh_state* mesh_state_create(struct module_env* env,
47865b390aaSDag-Erling Smørgrav 	struct query_info* qinfo, struct respip_client_info* cinfo,
47965b390aaSDag-Erling Smørgrav 	uint16_t qflags, int prime, int valrec);
480b7579f77SDag-Erling Smørgrav 
481b7579f77SDag-Erling Smørgrav /**
482bc892140SDag-Erling Smørgrav  * Check if the mesh state is unique.
483bc892140SDag-Erling Smørgrav  * A unique mesh state uses it's unique member to point to itself, else NULL.
484bc892140SDag-Erling Smørgrav  * @param mstate: mesh state to check.
485bc892140SDag-Erling Smørgrav  * @return true if the mesh state is unique, false otherwise.
486bc892140SDag-Erling Smørgrav  */
487bc892140SDag-Erling Smørgrav int mesh_state_is_unique(struct mesh_state* mstate);
488bc892140SDag-Erling Smørgrav 
489bc892140SDag-Erling Smørgrav /**
490bc892140SDag-Erling Smørgrav  * Make a mesh state unique.
491bc892140SDag-Erling Smørgrav  * A unique mesh state uses it's unique member to point to itself.
492bc892140SDag-Erling Smørgrav  * @param mstate: mesh state to check.
493bc892140SDag-Erling Smørgrav  */
494bc892140SDag-Erling Smørgrav void mesh_state_make_unique(struct mesh_state* mstate);
495bc892140SDag-Erling Smørgrav 
496bc892140SDag-Erling Smørgrav /**
497b7579f77SDag-Erling Smørgrav  * Cleanup a mesh state and its query state. Does not do rbtree or
498b7579f77SDag-Erling Smørgrav  * reference cleanup.
499b7579f77SDag-Erling Smørgrav  * @param mstate: mesh state to cleanup. Its pointer may no longer be used
500b7579f77SDag-Erling Smørgrav  * 	afterwards. Cleanup rbtrees before calling this function.
501b7579f77SDag-Erling Smørgrav  */
502b7579f77SDag-Erling Smørgrav void mesh_state_cleanup(struct mesh_state* mstate);
503b7579f77SDag-Erling Smørgrav 
504b7579f77SDag-Erling Smørgrav /**
505b7579f77SDag-Erling Smørgrav  * Delete all mesh states from the mesh.
506b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh area to clear
507b7579f77SDag-Erling Smørgrav  */
508b7579f77SDag-Erling Smørgrav void mesh_delete_all(struct mesh_area* mesh);
509b7579f77SDag-Erling Smørgrav 
510b7579f77SDag-Erling Smørgrav /**
511b7579f77SDag-Erling Smørgrav  * Find a mesh state in the mesh area. Pass relevant flags.
512b7579f77SDag-Erling Smørgrav  *
513b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh area to look in.
51465b390aaSDag-Erling Smørgrav  * @param cinfo: if non-NULL client specific info that may affect IP-based
51565b390aaSDag-Erling Smørgrav  * 	actions that apply to the query result.
516b7579f77SDag-Erling Smørgrav  * @param qinfo: what query
517b7579f77SDag-Erling Smørgrav  * @param qflags: if RD / CD bit is set or not.
518b7579f77SDag-Erling Smørgrav  * @param prime: if it is a priming query.
519ff825849SDag-Erling Smørgrav  * @param valrec: if it is a validation-recursion query.
520b7579f77SDag-Erling Smørgrav  * @return: mesh state or NULL if not found.
521b7579f77SDag-Erling Smørgrav  */
522b7579f77SDag-Erling Smørgrav struct mesh_state* mesh_area_find(struct mesh_area* mesh,
52365b390aaSDag-Erling Smørgrav 	struct respip_client_info* cinfo, struct query_info* qinfo,
52465b390aaSDag-Erling Smørgrav 	uint16_t qflags, int prime, int valrec);
525b7579f77SDag-Erling Smørgrav 
526b7579f77SDag-Erling Smørgrav /**
527b7579f77SDag-Erling Smørgrav  * Setup attachment super/sub relation between super and sub mesh state.
528b7579f77SDag-Erling Smørgrav  * The relation must not be present when calling the function.
529b7579f77SDag-Erling Smørgrav  * Does not update stat items in mesh_area.
530b7579f77SDag-Erling Smørgrav  * @param super: super state.
531b7579f77SDag-Erling Smørgrav  * @param sub: sub state.
532b7579f77SDag-Erling Smørgrav  * @return: 0 on alloc error.
533b7579f77SDag-Erling Smørgrav  */
534b7579f77SDag-Erling Smørgrav int mesh_state_attachment(struct mesh_state* super, struct mesh_state* sub);
535b7579f77SDag-Erling Smørgrav 
536b7579f77SDag-Erling Smørgrav /**
537b7579f77SDag-Erling Smørgrav  * Create new reply structure and attach it to a mesh state.
538b7579f77SDag-Erling Smørgrav  * Does not update stat items in mesh area.
539b7579f77SDag-Erling Smørgrav  * @param s: the mesh state.
540b7579f77SDag-Erling Smørgrav  * @param edns: edns data for reply (bufsize).
541b7579f77SDag-Erling Smørgrav  * @param rep: comm point reply info.
542b7579f77SDag-Erling Smørgrav  * @param qid: ID of reply.
543b7579f77SDag-Erling Smørgrav  * @param qflags: original query flags.
544bc892140SDag-Erling Smørgrav  * @param qinfo: original query info.
545b7579f77SDag-Erling Smørgrav  * @return: 0 on alloc error.
546b7579f77SDag-Erling Smørgrav  */
547b7579f77SDag-Erling Smørgrav int mesh_state_add_reply(struct mesh_state* s, struct edns_data* edns,
548bc892140SDag-Erling Smørgrav 	struct comm_reply* rep, uint16_t qid, uint16_t qflags,
549bc892140SDag-Erling Smørgrav 	const struct query_info* qinfo);
550b7579f77SDag-Erling Smørgrav 
551b7579f77SDag-Erling Smørgrav /**
552b7579f77SDag-Erling Smørgrav  * Create new callback structure and attach it to a mesh state.
553b7579f77SDag-Erling Smørgrav  * Does not update stat items in mesh area.
554b7579f77SDag-Erling Smørgrav  * @param s: the mesh state.
555b7579f77SDag-Erling Smørgrav  * @param edns: edns data for reply (bufsize).
556b7579f77SDag-Erling Smørgrav  * @param buf: buffer for reply
557b7579f77SDag-Erling Smørgrav  * @param cb: callback to call with results.
558b7579f77SDag-Erling Smørgrav  * @param cb_arg: callback user arg.
559b7579f77SDag-Erling Smørgrav  * @param qid: ID of reply.
560b7579f77SDag-Erling Smørgrav  * @param qflags: original query flags.
561b7579f77SDag-Erling Smørgrav  * @return: 0 on alloc error.
562b7579f77SDag-Erling Smørgrav  */
563b7579f77SDag-Erling Smørgrav int mesh_state_add_cb(struct mesh_state* s, struct edns_data* edns,
5643005e0a3SDag-Erling Smørgrav         struct sldns_buffer* buf, mesh_cb_func_type cb, void* cb_arg,
5653005e0a3SDag-Erling Smørgrav 	uint16_t qid, uint16_t qflags);
566b7579f77SDag-Erling Smørgrav 
567b7579f77SDag-Erling Smørgrav /**
568b7579f77SDag-Erling Smørgrav  * Run the mesh. Run all runnable mesh states. Which can create new
569b7579f77SDag-Erling Smørgrav  * runnable mesh states. Until completion. Automatically called by
570b7579f77SDag-Erling Smørgrav  * mesh_report_reply and mesh_new_client as needed.
571b7579f77SDag-Erling Smørgrav  * @param mesh: mesh area.
572b7579f77SDag-Erling Smørgrav  * @param mstate: first mesh state to run.
573b7579f77SDag-Erling Smørgrav  * @param ev: event the mstate. Others get event_pass.
574b7579f77SDag-Erling Smørgrav  * @param e: if a reply, its outbound entry.
575b7579f77SDag-Erling Smørgrav  */
576b7579f77SDag-Erling Smørgrav void mesh_run(struct mesh_area* mesh, struct mesh_state* mstate,
577b7579f77SDag-Erling Smørgrav 	enum module_ev ev, struct outbound_entry* e);
578b7579f77SDag-Erling Smørgrav 
579b7579f77SDag-Erling Smørgrav /**
580b7579f77SDag-Erling Smørgrav  * Print some stats about the mesh to the log.
581b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh to print it for.
582b7579f77SDag-Erling Smørgrav  * @param str: descriptive string to go with it.
583b7579f77SDag-Erling Smørgrav  */
584b7579f77SDag-Erling Smørgrav void mesh_stats(struct mesh_area* mesh, const char* str);
585b7579f77SDag-Erling Smørgrav 
586b7579f77SDag-Erling Smørgrav /**
587b7579f77SDag-Erling Smørgrav  * Clear the stats that the mesh keeps (number of queries serviced)
588b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh
589b7579f77SDag-Erling Smørgrav  */
590b7579f77SDag-Erling Smørgrav void mesh_stats_clear(struct mesh_area* mesh);
591b7579f77SDag-Erling Smørgrav 
592b7579f77SDag-Erling Smørgrav /**
593b7579f77SDag-Erling Smørgrav  * Print all the states in the mesh to the log.
594b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh to print all states of.
595b7579f77SDag-Erling Smørgrav  */
596b7579f77SDag-Erling Smørgrav void mesh_log_list(struct mesh_area* mesh);
597b7579f77SDag-Erling Smørgrav 
598b7579f77SDag-Erling Smørgrav /**
599b7579f77SDag-Erling Smørgrav  * Calculate memory size in use by mesh and all queries inside it.
600b7579f77SDag-Erling Smørgrav  * @param mesh: the mesh to examine.
601b7579f77SDag-Erling Smørgrav  * @return size in bytes.
602b7579f77SDag-Erling Smørgrav  */
603b7579f77SDag-Erling Smørgrav size_t mesh_get_mem(struct mesh_area* mesh);
604b7579f77SDag-Erling Smørgrav 
605b7579f77SDag-Erling Smørgrav /**
606b7579f77SDag-Erling Smørgrav  * Find cycle; see if the given mesh is in the targets sub, or sub-sub, ...
607b7579f77SDag-Erling Smørgrav  * trees.
608b7579f77SDag-Erling Smørgrav  * If the sub-sub structure is too large, it returns 'a cycle'=2.
609b7579f77SDag-Erling Smørgrav  * @param qstate: given mesh querystate.
610b7579f77SDag-Erling Smørgrav  * @param qinfo: query info for dependency.
611b7579f77SDag-Erling Smørgrav  * @param flags: query flags of dependency.
612b7579f77SDag-Erling Smørgrav  * @param prime: if dependency is a priming query or not.
613ff825849SDag-Erling Smørgrav  * @param valrec: if it is a validation recursion query (lookup of key, DS).
614b7579f77SDag-Erling Smørgrav  * @return true if the name,type,class exists and the given qstate mesh exists
615b7579f77SDag-Erling Smørgrav  * 	as a dependency of that name. Thus if qstate becomes dependent on
616b7579f77SDag-Erling Smørgrav  * 	name,type,class then a cycle is created, this is return value 1.
617b7579f77SDag-Erling Smørgrav  * 	Too large to search is value 2 (also true).
618b7579f77SDag-Erling Smørgrav  */
619b7579f77SDag-Erling Smørgrav int mesh_detect_cycle(struct module_qstate* qstate, struct query_info* qinfo,
620ff825849SDag-Erling Smørgrav 	uint16_t flags, int prime, int valrec);
621b7579f77SDag-Erling Smørgrav 
622b7579f77SDag-Erling Smørgrav /** compare two mesh_states */
623b7579f77SDag-Erling Smørgrav int mesh_state_compare(const void* ap, const void* bp);
624b7579f77SDag-Erling Smørgrav 
625b7579f77SDag-Erling Smørgrav /** compare two mesh references */
626b7579f77SDag-Erling Smørgrav int mesh_state_ref_compare(const void* ap, const void* bp);
627b7579f77SDag-Erling Smørgrav 
628b7579f77SDag-Erling Smørgrav /**
629b7579f77SDag-Erling Smørgrav  * Make space for another recursion state for a reply in the mesh
630b7579f77SDag-Erling Smørgrav  * @param mesh: mesh area
631b7579f77SDag-Erling Smørgrav  * @param qbuf: query buffer to save if recursion is invoked to make space.
632b7579f77SDag-Erling Smørgrav  *    This buffer is necessary, because the following sequence in calls
633b7579f77SDag-Erling Smørgrav  *    can result in an overwrite of the incoming query:
634b7579f77SDag-Erling Smørgrav  *    delete_other_mesh_query - iter_clean - serviced_delete - waiting
635b7579f77SDag-Erling Smørgrav  *    udp query is sent - on error callback - callback sends SERVFAIL reply
636b7579f77SDag-Erling Smørgrav  *    over the same network channel, and shared UDP buffer is overwritten.
637b7579f77SDag-Erling Smørgrav  *    You can pass NULL if there is no buffer that must be backed up.
638b7579f77SDag-Erling Smørgrav  * @return false if no space is available.
639b7579f77SDag-Erling Smørgrav  */
64017d15b25SDag-Erling Smørgrav int mesh_make_new_space(struct mesh_area* mesh, struct sldns_buffer* qbuf);
641b7579f77SDag-Erling Smørgrav 
642b7579f77SDag-Erling Smørgrav /**
643b7579f77SDag-Erling Smørgrav  * Insert mesh state into a double linked list.  Inserted at end.
644b7579f77SDag-Erling Smørgrav  * @param m: mesh state.
645b7579f77SDag-Erling Smørgrav  * @param fp: pointer to the first-elem-pointer of the list.
646b7579f77SDag-Erling Smørgrav  * @param lp: pointer to the last-elem-pointer of the list.
647b7579f77SDag-Erling Smørgrav  */
648b7579f77SDag-Erling Smørgrav void mesh_list_insert(struct mesh_state* m, struct mesh_state** fp,
649b7579f77SDag-Erling Smørgrav 	struct mesh_state** lp);
650b7579f77SDag-Erling Smørgrav 
651b7579f77SDag-Erling Smørgrav /**
652b7579f77SDag-Erling Smørgrav  * Remove mesh state from a double linked list.  Remove from any position.
653b7579f77SDag-Erling Smørgrav  * @param m: mesh state.
654b7579f77SDag-Erling Smørgrav  * @param fp: pointer to the first-elem-pointer of the list.
655b7579f77SDag-Erling Smørgrav  * @param lp: pointer to the last-elem-pointer of the list.
656b7579f77SDag-Erling Smørgrav  */
657b7579f77SDag-Erling Smørgrav void mesh_list_remove(struct mesh_state* m, struct mesh_state** fp,
658b7579f77SDag-Erling Smørgrav 	struct mesh_state** lp);
659b7579f77SDag-Erling Smørgrav 
660e86b9096SDag-Erling Smørgrav /**
661e86b9096SDag-Erling Smørgrav  * Remove mesh reply entry from the reply entry list.  Searches for
662e86b9096SDag-Erling Smørgrav  * the comm_point pointer.
663e86b9096SDag-Erling Smørgrav  * @param mesh: to update the counters.
664e86b9096SDag-Erling Smørgrav  * @param m: the mesh state.
665e86b9096SDag-Erling Smørgrav  * @param cp: the comm_point to remove from the list.
666e86b9096SDag-Erling Smørgrav  */
667e86b9096SDag-Erling Smørgrav void mesh_state_remove_reply(struct mesh_area* mesh, struct mesh_state* m,
668e86b9096SDag-Erling Smørgrav 	struct comm_point* cp);
669e86b9096SDag-Erling Smørgrav 
670091e9e46SCy Schubert /** Callback for when the serve expired client timer has run out.  Tries to
671091e9e46SCy Schubert  * find an expired answer in the cache and reply that to the client.
672091e9e46SCy Schubert  * @param arg: the argument passed to the callback.
673091e9e46SCy Schubert  */
674091e9e46SCy Schubert void mesh_serve_expired_callback(void* arg);
675091e9e46SCy Schubert 
676091e9e46SCy Schubert /**
677091e9e46SCy Schubert  * Try to get a (expired) cached answer.
678091e9e46SCy Schubert  * This needs to behave like the worker's answer_from_cache() in order to have
679091e9e46SCy Schubert  * the same behavior as when replying from cache.
680091e9e46SCy Schubert  * @param qstate: the module qstate.
681091e9e46SCy Schubert  * @param lookup_qinfo: the query info to look for in the cache.
682091e9e46SCy Schubert  * @return dns_msg if a cached answer was found, otherwise NULL.
683091e9e46SCy Schubert  */
684091e9e46SCy Schubert struct dns_msg*
685091e9e46SCy Schubert mesh_serve_expired_lookup(struct module_qstate* qstate,
686091e9e46SCy Schubert 	struct query_info* lookup_qinfo);
687091e9e46SCy Schubert 
688b7579f77SDag-Erling Smørgrav #endif /* SERVICES_MESH_H */
689