xref: /freebsd/contrib/unbound/libunbound/context.h (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /*
2  * libunbound/context.h - validating context for unbound internal use
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains the validator context structure.
40  */
41 #ifndef LIBUNBOUND_CONTEXT_H
42 #define LIBUNBOUND_CONTEXT_H
43 #include "util/locks.h"
44 #include "util/alloc.h"
45 #include "util/rbtree.h"
46 #include "services/modstack.h"
47 #include "libunbound/unbound.h"
48 #include "libunbound/unbound-event.h"
49 #include "util/data/packed_rrset.h"
50 struct libworker;
51 struct tube;
52 struct sldns_buffer;
53 struct ub_event_base;
54 
55 /** store that the logfile has a debug override */
56 extern int ctx_logfile_overridden;
57 
58 /**
59  * The context structure
60  *
61  * Contains two pipes for async service
62  *	qq : write queries to the async service pid/tid.
63  *	rr : read results from the async service pid/tid.
64  */
65 struct ub_ctx {
66 	/* --- pipes --- */
67 	/** mutex on query write pipe */
68 	lock_basic_type qqpipe_lock;
69 	/** the query write pipe */
70 	struct tube* qq_pipe;
71 	/** mutex on result read pipe */
72 	lock_basic_type rrpipe_lock;
73 	/** the result read pipe */
74 	struct tube* rr_pipe;
75 
76 	/* --- shared data --- */
77 	/** mutex for access to env.cfg, finalized and dothread */
78 	lock_basic_type cfglock;
79 	/**
80 	 * The context has been finalized
81 	 * This is after config when the first resolve is done.
82 	 * The modules are inited (module-init()) and shared caches created.
83 	 */
84 	int finalized;
85 
86 	/** is bg worker created yet ? */
87 	int created_bg;
88 	/** pid of bg worker process */
89 	pid_t bg_pid;
90 	/** tid of bg worker thread */
91 	ub_thread_type bg_tid;
92 	/** pid when pipes are created. This was the process when the
93 	 * setup was called. Helps with clean up, so we can tell after a fork
94 	 * which side of the fork the delete is on. */
95 	pid_t pipe_pid;
96 	/** when threaded, the worker that exists in the created thread. */
97 	struct libworker* thread_worker;
98 
99 	/** do threading (instead of forking) for async resolution */
100 	int dothread;
101 	/** next thread number for new threads */
102 	int thr_next_num;
103 	/** if logfile is overridden */
104 	int logfile_override;
105 	/** what logfile to use instead */
106 	FILE* log_out;
107 	/**
108 	 * List of alloc-cache-id points per threadnum for notinuse threads.
109 	 * Simply the entire struct alloc_cache with the 'super' member used
110 	 * to link a simply linked list. Reset super member to the superalloc
111 	 * before use.
112 	 */
113 	struct alloc_cache* alloc_list;
114 
115 	/** shared caches, and so on */
116 	struct alloc_cache superalloc;
117 	/** module env master value */
118 	struct module_env* env;
119 	/** module stack */
120 	struct module_stack mods;
121 	/** local authority zones */
122 	struct local_zones* local_zones;
123 	/** random state used to seed new random state structures */
124 	struct ub_randstate* seed_rnd;
125 
126 	/** event base for event oriented interface */
127 	struct ub_event_base* event_base;
128 	/** true if the event_base is a pluggable base that is malloced
129 	 * with a user event base inside, if so, clean up the pluggable alloc*/
130 	int event_base_malloced;
131 	/** libworker for event based interface */
132 	struct libworker* event_worker;
133 
134 	/** next query number (to try) to use */
135 	int next_querynum;
136 	/** number of async queries outstanding */
137 	size_t num_async;
138 	/**
139 	 * Tree of outstanding queries. Indexed by querynum
140 	 * Used when results come in for async to lookup.
141 	 * Used when cancel is done for lookup (and delete).
142 	 * Used to see if querynum is free for use.
143 	 * Content of type ctx_query.
144 	 */
145 	rbtree_type queries;
146 };
147 
148 /**
149  * The queries outstanding for the libunbound resolver.
150  * These are outstanding for async resolution.
151  * But also, outstanding for sync resolution by one of the threads that
152  * has joined the threadpool.
153  */
154 struct ctx_query {
155 	/** node in rbtree, must be first entry, key is ptr to the querynum */
156 	struct rbnode_type node;
157 	/** query id number, key for node */
158 	int querynum;
159 	/** was this an async query? */
160 	int async;
161 	/** was this query cancelled (for bg worker) */
162 	int cancelled;
163 
164 	/** for async query, the callback function of type ub_callback_type */
165 	ub_callback_type cb;
166 	/** for event callbacks the type is ub_event_callback_type */
167         ub_event_callback_type cb_event;
168 	/** for async query, the callback user arg */
169 	void* cb_arg;
170 
171 	/** answer message, result from resolver lookup. */
172 	uint8_t* msg;
173 	/** resulting message length. */
174 	size_t msg_len;
175 	/** validation status on security */
176 	enum sec_status msg_security;
177 	/** store libworker that is handling this query */
178 	struct libworker* w;
179 
180 	/** result structure, also contains original query, type, class.
181 	 * malloced ptr ready to hand to the client. */
182 	struct ub_result* res;
183 };
184 
185 /**
186  * Command codes for libunbound pipe.
187  *
188  * Serialization looks like this:
189  * 	o length (of remainder) uint32.
190  * 	o uint32 command code.
191  * 	o per command format.
192  */
193 enum ub_ctx_cmd {
194 	/** QUIT */
195 	UB_LIBCMD_QUIT = 0,
196 	/** New query, sent to bg worker */
197 	UB_LIBCMD_NEWQUERY,
198 	/** Cancel query, sent to bg worker */
199 	UB_LIBCMD_CANCEL,
200 	/** Query result, originates from bg worker */
201 	UB_LIBCMD_ANSWER
202 };
203 
204 /**
205  * finalize a context.
206  * @param ctx: context to finalize. creates shared data.
207  * @return 0 if OK, or errcode.
208  */
209 int context_finalize(struct ub_ctx* ctx);
210 
211 /** compare two ctx_query elements */
212 int context_query_cmp(const void* a, const void* b);
213 
214 /**
215  * delete context query
216  * @param q: query to delete, including message packet and prealloc result
217  */
218 void context_query_delete(struct ctx_query* q);
219 
220 /**
221  * Create new query in context, add to querynum list.
222  * @param ctx: context
223  * @param name: query name
224  * @param rrtype: type
225  * @param rrclass: class
226  * @param cb: callback for async, or NULL for sync.
227  * @param cb_event: event callback for async, or NULL for sync.
228  * @param cbarg: user arg for async queries.
229  * @return new ctx_query or NULL for malloc failure.
230  */
231 struct ctx_query* context_new(struct ub_ctx* ctx, const char* name, int rrtype,
232         int rrclass,  ub_callback_type cb, ub_event_callback_type cb_event,
233 	void* cbarg);
234 
235 /**
236  * Get a new alloc. Creates a new one or uses a cached one.
237  * @param ctx: context
238  * @param locking: if true, cfglock is locked while getting alloc.
239  * @return an alloc, or NULL on mem error.
240  */
241 struct alloc_cache* context_obtain_alloc(struct ub_ctx* ctx, int locking);
242 
243 /**
244  * Release an alloc. Puts it into the cache.
245  * @param ctx: context
246  * @param locking: if true, cfglock is locked while releasing alloc.
247  * @param alloc: alloc to relinquish.
248  */
249 void context_release_alloc(struct ub_ctx* ctx, struct alloc_cache* alloc,
250 	int locking);
251 
252 /**
253  * Serialize a context query that questions data.
254  * This serializes the query name, type, ...
255  * As well as command code 'new_query'.
256  * @param q: context query
257  * @param len: the length of the allocation is returned.
258  * @return: an alloc, or NULL on mem error.
259  */
260 uint8_t* context_serialize_new_query(struct ctx_query* q, uint32_t* len);
261 
262 /**
263  * Serialize a context_query result to hand back to user.
264  * This serializes the query name, type, ..., and result.
265  * As well as command code 'answer'.
266  * @param q: context query
267  * @param err: error code to pass to client.
268  * @param pkt: the packet to add, can be NULL.
269  * @param len: the length of the allocation is returned.
270  * @return: an alloc, or NULL on mem error.
271  */
272 uint8_t* context_serialize_answer(struct ctx_query* q, int err,
273 	struct sldns_buffer* pkt, uint32_t* len);
274 
275 /**
276  * Serialize a query cancellation. Serializes query async id
277  * as well as command code 'cancel'
278  * @param q: context query
279  * @param len: the length of the allocation is returned.
280  * @return: an alloc, or NULL on mem error.
281  */
282 uint8_t* context_serialize_cancel(struct ctx_query* q, uint32_t* len);
283 
284 /**
285  * Serialize a 'quit' command.
286  * @param len: the length of the allocation is returned.
287  * @return: an alloc, or NULL on mem error.
288  */
289 uint8_t* context_serialize_quit(uint32_t* len);
290 
291 /**
292  * Obtain command code from serialized buffer
293  * @param p: buffer serialized.
294  * @param len: length of buffer.
295  * @return command code or QUIT on error.
296  */
297 enum ub_ctx_cmd context_serial_getcmd(uint8_t* p, uint32_t len);
298 
299 /**
300  * Lookup query from new_query buffer.
301  * @param ctx: context
302  * @param p: buffer serialized.
303  * @param len: length of buffer.
304  * @return looked up ctx_query or NULL for malloc failure.
305  */
306 struct ctx_query* context_lookup_new_query(struct ub_ctx* ctx,
307 	uint8_t* p, uint32_t len);
308 
309 /**
310  * Deserialize a new_query buffer.
311  * @param ctx: context
312  * @param p: buffer serialized.
313  * @param len: length of buffer.
314  * @return new ctx_query or NULL for malloc failure.
315  */
316 struct ctx_query* context_deserialize_new_query(struct ub_ctx* ctx,
317 	uint8_t* p, uint32_t len);
318 
319 /**
320  * Deserialize an answer buffer.
321  * @param ctx: context
322  * @param p: buffer serialized.
323  * @param len: length of buffer.
324  * @param err: error code to be returned to client is passed.
325  * @return ctx_query with answer added or NULL for malloc failure.
326  */
327 struct ctx_query* context_deserialize_answer(struct ub_ctx* ctx,
328 	uint8_t* p, uint32_t len, int* err);
329 
330 /**
331  * Deserialize a cancel buffer.
332  * @param ctx: context
333  * @param p: buffer serialized.
334  * @param len: length of buffer.
335  * @return ctx_query to cancel or NULL for failure.
336  */
337 struct ctx_query* context_deserialize_cancel(struct ub_ctx* ctx,
338 	uint8_t* p, uint32_t len);
339 
340 #endif /* LIBUNBOUND_CONTEXT_H */
341