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