2 * libunbound/context.h - validating context for unbound internal use
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains the validator context structure.
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"
55 /** store that the logfile has a debug override */
56 extern int ctx_logfile_overridden;
59 * The context structure
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.
67 /** mutex on query write pipe */
68 lock_basic_type qqpipe_lock;
69 /** the query write pipe */
71 /** mutex on result read pipe */
72 lock_basic_type rrpipe_lock;
73 /** the result read pipe */
76 /* --- shared data --- */
77 /** mutex for access to env.cfg, finalized and dothread */
78 lock_basic_type cfglock;
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.
86 /** is bg worker created yet ? */
88 /** pid of bg worker process */
90 /** tid of bg worker thread */
91 ub_thread_type bg_tid;
93 /** do threading (instead of forking) for async resolution */
95 /** next thread number for new threads */
97 /** if logfile is overridden */
99 /** what logfile to use instead */
102 * List of alloc-cache-id points per threadnum for notinuse threads.
103 * Simply the entire struct alloc_cache with the 'super' member used
104 * to link a simply linked list. Reset super member to the superalloc
107 struct alloc_cache* alloc_list;
109 /** shared caches, and so on */
110 struct alloc_cache superalloc;
111 /** module env master value */
112 struct module_env* env;
114 struct module_stack mods;
115 /** local authority zones */
116 struct local_zones* local_zones;
117 /** random state used to seed new random state structures */
118 struct ub_randstate* seed_rnd;
120 /** event base for event oriented interface */
121 struct ub_event_base* event_base;
122 /** true if the event_base is a pluggable base that is malloced
123 * with a user event base inside, if so, clean up the pluggable alloc*/
124 int event_base_malloced;
125 /** libworker for event based interface */
126 struct libworker* event_worker;
128 /** next query number (to try) to use */
130 /** number of async queries outstanding */
133 * Tree of outstanding queries. Indexed by querynum
134 * Used when results come in for async to lookup.
135 * Used when cancel is done for lookup (and delete).
136 * Used to see if querynum is free for use.
137 * Content of type ctx_query.
143 * The queries outstanding for the libunbound resolver.
144 * These are outstanding for async resolution.
145 * But also, outstanding for sync resolution by one of the threads that
146 * has joined the threadpool.
149 /** node in rbtree, must be first entry, key is ptr to the querynum */
150 struct rbnode_type node;
151 /** query id number, key for node */
153 /** was this an async query? */
155 /** was this query cancelled (for bg worker) */
158 /** for async query, the callback function of type ub_callback_type */
160 /** for event callbacks the type is ub_event_callback_type */
161 ub_event_callback_type cb_event;
162 /** for async query, the callback user arg */
165 /** answer message, result from resolver lookup. */
167 /** resulting message length. */
169 /** validation status on security */
170 enum sec_status msg_security;
171 /** store libworker that is handling this query */
174 /** result structure, also contains original query, type, class.
175 * malloced ptr ready to hand to the client. */
176 struct ub_result* res;
180 * The error constants
185 /** socket operation. Set to -1, so that if an error from _fd() is
186 * passed (-1) it gives a socket error. */
192 /** DNS service failed */
196 /** cfg change after finalize() */
198 /** initialization failed (bad settings) */
200 /** error in pipe communication with async bg worker */
202 /** error reading from file (resolv.conf) */
204 /** error async_id does not exist or result already been delivered */
209 * Command codes for libunbound pipe.
211 * Serialization looks like this:
212 * o length (of remainder) uint32.
213 * o uint32 command code.
214 * o per command format.
219 /** New query, sent to bg worker */
221 /** Cancel query, sent to bg worker */
223 /** Query result, originates from bg worker */
228 * finalize a context.
229 * @param ctx: context to finalize. creates shared data.
230 * @return 0 if OK, or errcode.
232 int context_finalize(struct ub_ctx* ctx);
234 /** compare two ctx_query elements */
235 int context_query_cmp(const void* a, const void* b);
238 * delete context query
239 * @param q: query to delete, including message packet and prealloc result
241 void context_query_delete(struct ctx_query* q);
244 * Create new query in context, add to querynum list.
245 * @param ctx: context
246 * @param name: query name
247 * @param rrtype: type
248 * @param rrclass: class
249 * @param cb: callback for async, or NULL for sync.
250 * @param cb_event: event callback for async, or NULL for sync.
251 * @param cbarg: user arg for async queries.
252 * @return new ctx_query or NULL for malloc failure.
254 struct ctx_query* context_new(struct ub_ctx* ctx, const char* name, int rrtype,
255 int rrclass, ub_callback_type cb, ub_event_callback_type cb_event,
259 * Get a new alloc. Creates a new one or uses a cached one.
260 * @param ctx: context
261 * @param locking: if true, cfglock is locked while getting alloc.
262 * @return an alloc, or NULL on mem error.
264 struct alloc_cache* context_obtain_alloc(struct ub_ctx* ctx, int locking);
267 * Release an alloc. Puts it into the cache.
268 * @param ctx: context
269 * @param locking: if true, cfglock is locked while releasing alloc.
270 * @param alloc: alloc to relinquish.
272 void context_release_alloc(struct ub_ctx* ctx, struct alloc_cache* alloc,
276 * Serialize a context query that questions data.
277 * This serializes the query name, type, ...
278 * As well as command code 'new_query'.
279 * @param q: context query
280 * @param len: the length of the allocation is returned.
281 * @return: an alloc, or NULL on mem error.
283 uint8_t* context_serialize_new_query(struct ctx_query* q, uint32_t* len);
286 * Serialize a context_query result to hand back to user.
287 * This serializes the query name, type, ..., and result.
288 * As well as command code 'answer'.
289 * @param q: context query
290 * @param err: error code to pass to client.
291 * @param pkt: the packet to add, can be NULL.
292 * @param len: the length of the allocation is returned.
293 * @return: an alloc, or NULL on mem error.
295 uint8_t* context_serialize_answer(struct ctx_query* q, int err,
296 struct sldns_buffer* pkt, uint32_t* len);
299 * Serialize a query cancellation. Serializes query async id
300 * as well as command code 'cancel'
301 * @param q: context query
302 * @param len: the length of the allocation is returned.
303 * @return: an alloc, or NULL on mem error.
305 uint8_t* context_serialize_cancel(struct ctx_query* q, uint32_t* len);
308 * Serialize a 'quit' command.
309 * @param len: the length of the allocation is returned.
310 * @return: an alloc, or NULL on mem error.
312 uint8_t* context_serialize_quit(uint32_t* len);
315 * Obtain command code from serialized buffer
316 * @param p: buffer serialized.
317 * @param len: length of buffer.
318 * @return command code or QUIT on error.
320 enum ub_ctx_cmd context_serial_getcmd(uint8_t* p, uint32_t len);
323 * Lookup query from new_query buffer.
324 * @param ctx: context
325 * @param p: buffer serialized.
326 * @param len: length of buffer.
327 * @return looked up ctx_query or NULL for malloc failure.
329 struct ctx_query* context_lookup_new_query(struct ub_ctx* ctx,
330 uint8_t* p, uint32_t len);
333 * Deserialize a new_query buffer.
334 * @param ctx: context
335 * @param p: buffer serialized.
336 * @param len: length of buffer.
337 * @return new ctx_query or NULL for malloc failure.
339 struct ctx_query* context_deserialize_new_query(struct ub_ctx* ctx,
340 uint8_t* p, uint32_t len);
343 * Deserialize an answer buffer.
344 * @param ctx: context
345 * @param p: buffer serialized.
346 * @param len: length of buffer.
347 * @param err: error code to be returned to client is passed.
348 * @return ctx_query with answer added or NULL for malloc failure.
350 struct ctx_query* context_deserialize_answer(struct ub_ctx* ctx,
351 uint8_t* p, uint32_t len, int* err);
354 * Deserialize a cancel buffer.
355 * @param ctx: context
356 * @param p: buffer serialized.
357 * @param len: length of buffer.
358 * @return ctx_query to cancel or NULL for failure.
360 struct ctx_query* context_deserialize_cancel(struct ub_ctx* ctx,
361 uint8_t* p, uint32_t len);
363 #endif /* LIBUNBOUND_CONTEXT_H */