2 * libunbound/context.c - 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 LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
39 * This file contains the validator context structure.
42 #include "libunbound/context.h"
43 #include "util/module.h"
44 #include "util/config_file.h"
45 #include "util/net_help.h"
46 #include "services/modstack.h"
47 #include "services/localzone.h"
48 #include "services/cache/rrset.h"
49 #include "services/cache/infra.h"
50 #include "util/data/msgreply.h"
51 #include "util/storage/slabhash.h"
54 context_finalize(struct ub_ctx* ctx)
56 struct config_file* cfg = ctx->env->cfg;
57 verbosity = cfg->verbosity;
58 if(ctx->logfile_override)
59 log_file(ctx->log_out);
60 else log_init(cfg->logfile, cfg->use_syslog, NULL);
62 if(!modstack_setup(&ctx->mods, cfg->module_conf, ctx->env))
64 ctx->local_zones = local_zones_create();
67 if(!local_zones_apply_cfg(ctx->local_zones, cfg))
69 if(!ctx->env->msg_cache ||
70 cfg->msg_cache_size != slabhash_get_size(ctx->env->msg_cache) ||
71 cfg->msg_cache_slabs != ctx->env->msg_cache->size) {
72 slabhash_delete(ctx->env->msg_cache);
73 ctx->env->msg_cache = slabhash_create(cfg->msg_cache_slabs,
74 HASH_DEFAULT_STARTARRAY, cfg->msg_cache_size,
75 msgreply_sizefunc, query_info_compare,
76 query_entry_delete, reply_info_delete, NULL);
77 if(!ctx->env->msg_cache)
80 ctx->env->rrset_cache = rrset_cache_adjust(ctx->env->rrset_cache,
81 ctx->env->cfg, ctx->env->alloc);
82 if(!ctx->env->rrset_cache)
84 ctx->env->infra_cache = infra_adjust(ctx->env->infra_cache, cfg);
85 if(!ctx->env->infra_cache)
91 int context_query_cmp(const void* a, const void* b)
93 if( *(int*)a < *(int*)b )
95 if( *(int*)a > *(int*)b )
101 context_query_delete(struct ctx_query* q)
104 ub_resolve_free(q->res);
109 /** How many times to try to find an unused query-id-number for async */
110 #define NUM_ID_TRIES 100000
111 /** find next useful id number of 0 on error */
113 find_id(struct ub_ctx* ctx, int* id)
116 ctx->next_querynum++;
117 while(rbtree_search(&ctx->queries, &ctx->next_querynum)) {
118 ctx->next_querynum++; /* numerical wraparound is fine */
119 if(tries++ > NUM_ID_TRIES)
122 *id = ctx->next_querynum;
127 context_new(struct ub_ctx* ctx, const char* name, int rrtype, int rrclass,
128 ub_callback_t cb, void* cbarg)
130 struct ctx_query* q = (struct ctx_query*)calloc(1, sizeof(*q));
132 lock_basic_lock(&ctx->cfglock);
133 if(!find_id(ctx, &q->querynum)) {
134 lock_basic_unlock(&ctx->cfglock);
138 lock_basic_unlock(&ctx->cfglock);
139 q->node.key = &q->querynum;
140 q->async = (cb != NULL);
143 q->res = (struct ub_result*)calloc(1, sizeof(*q->res));
148 q->res->qname = strdup(name);
154 q->res->qtype = rrtype;
155 q->res->qclass = rrclass;
157 /* add to query list */
158 lock_basic_lock(&ctx->cfglock);
161 (void)rbtree_insert(&ctx->queries, &q->node);
162 lock_basic_unlock(&ctx->cfglock);
167 context_obtain_alloc(struct ub_ctx* ctx, int locking)
169 struct alloc_cache* a;
172 lock_basic_lock(&ctx->cfglock);
176 ctx->alloc_list = a->super; /* snip off list */
177 else tnum = ctx->thr_next_num++;
179 lock_basic_unlock(&ctx->cfglock);
182 a->super = &ctx->superalloc;
185 a = (struct alloc_cache*)calloc(1, sizeof(*a));
188 alloc_init(a, &ctx->superalloc, tnum);
193 context_release_alloc(struct ub_ctx* ctx, struct alloc_cache* alloc,
199 lock_basic_lock(&ctx->cfglock);
201 alloc->super = ctx->alloc_list;
202 ctx->alloc_list = alloc;
204 lock_basic_unlock(&ctx->cfglock);
209 context_serialize_new_query(struct ctx_query* q, uint32_t* len)
211 /* format for new query is
216 * o rest queryname (string)
219 size_t slen = strlen(q->res->qname) + 1/*end of string*/;
220 *len = sizeof(uint32_t)*4 + slen;
221 p = (uint8_t*)malloc(*len);
223 ldns_write_uint32(p, UB_LIBCMD_NEWQUERY);
224 ldns_write_uint32(p+sizeof(uint32_t), (uint32_t)q->querynum);
225 ldns_write_uint32(p+2*sizeof(uint32_t), (uint32_t)q->res->qtype);
226 ldns_write_uint32(p+3*sizeof(uint32_t), (uint32_t)q->res->qclass);
227 memmove(p+4*sizeof(uint32_t), q->res->qname, slen);
232 context_deserialize_new_query(struct ub_ctx* ctx, uint8_t* p, uint32_t len)
234 struct ctx_query* q = (struct ctx_query*)calloc(1, sizeof(*q));
236 if(len < 4*sizeof(uint32_t)+1) {
240 log_assert( ldns_read_uint32(p) == UB_LIBCMD_NEWQUERY);
241 q->querynum = (int)ldns_read_uint32(p+sizeof(uint32_t));
242 q->node.key = &q->querynum;
244 q->res = (struct ub_result*)calloc(1, sizeof(*q->res));
249 q->res->qtype = (int)ldns_read_uint32(p+2*sizeof(uint32_t));
250 q->res->qclass = (int)ldns_read_uint32(p+3*sizeof(uint32_t));
251 q->res->qname = strdup((char*)(p+4*sizeof(uint32_t)));
258 /** add to query list */
260 (void)rbtree_insert(&ctx->queries, &q->node);
265 context_lookup_new_query(struct ub_ctx* ctx, uint8_t* p, uint32_t len)
269 if(len < 4*sizeof(uint32_t)+1) {
272 log_assert( ldns_read_uint32(p) == UB_LIBCMD_NEWQUERY);
273 querynum = (int)ldns_read_uint32(p+sizeof(uint32_t));
274 q = (struct ctx_query*)rbtree_search(&ctx->queries, &querynum);
278 log_assert(q->async);
283 context_serialize_answer(struct ctx_query* q, int err, ldns_buffer* pkt,
289 * o uint32 error_code
290 * o uint32 msg_security
291 * o uint32 length of why_bogus string (+1 for eos); 0 absent.
293 * o the remainder is the answer msg from resolver lookup.
294 * remainder can be length 0.
296 size_t pkt_len = pkt?ldns_buffer_remaining(pkt):0;
297 size_t wlen = (pkt&&q->res->why_bogus)?strlen(q->res->why_bogus)+1:0;
299 *len = sizeof(uint32_t)*5 + pkt_len + wlen;
300 p = (uint8_t*)malloc(*len);
302 ldns_write_uint32(p, UB_LIBCMD_ANSWER);
303 ldns_write_uint32(p+sizeof(uint32_t), (uint32_t)q->querynum);
304 ldns_write_uint32(p+2*sizeof(uint32_t), (uint32_t)err);
305 ldns_write_uint32(p+3*sizeof(uint32_t), (uint32_t)q->msg_security);
306 ldns_write_uint32(p+4*sizeof(uint32_t), (uint32_t)wlen);
308 memmove(p+5*sizeof(uint32_t), q->res->why_bogus, wlen);
310 memmove(p+5*sizeof(uint32_t)+wlen,
311 ldns_buffer_begin(pkt), pkt_len);
316 context_deserialize_answer(struct ub_ctx* ctx,
317 uint8_t* p, uint32_t len, int* err)
319 struct ctx_query* q = NULL ;
322 if(len < 5*sizeof(uint32_t)) return NULL;
323 log_assert( ldns_read_uint32(p) == UB_LIBCMD_ANSWER);
324 id = (int)ldns_read_uint32(p+sizeof(uint32_t));
325 q = (struct ctx_query*)rbtree_search(&ctx->queries, &id);
327 *err = (int)ldns_read_uint32(p+2*sizeof(uint32_t));
328 q->msg_security = ldns_read_uint32(p+3*sizeof(uint32_t));
329 wlen = (size_t)ldns_read_uint32(p+4*sizeof(uint32_t));
330 if(len > 5*sizeof(uint32_t) && wlen > 0) {
331 if(len >= 5*sizeof(uint32_t)+wlen)
332 q->res->why_bogus = (char*)memdup(
333 p+5*sizeof(uint32_t), wlen);
334 if(!q->res->why_bogus) {
335 /* pass malloc failure to the user callback */
340 q->res->why_bogus[wlen-1] = 0; /* zero terminated for sure */
342 if(len > 5*sizeof(uint32_t)+wlen) {
343 q->msg_len = len - 5*sizeof(uint32_t) - wlen;
344 q->msg = (uint8_t*)memdup(p+5*sizeof(uint32_t)+wlen,
347 /* pass malloc failure to the user callback */
357 context_serialize_cancel(struct ctx_query* q, uint32_t* len)
361 * o uint32 async-id */
362 uint8_t* p = (uint8_t*)malloc(2*sizeof(uint32_t));
364 *len = 2*sizeof(uint32_t);
365 ldns_write_uint32(p, UB_LIBCMD_CANCEL);
366 ldns_write_uint32(p+sizeof(uint32_t), (uint32_t)q->querynum);
370 struct ctx_query* context_deserialize_cancel(struct ub_ctx* ctx,
371 uint8_t* p, uint32_t len)
375 if(len != 2*sizeof(uint32_t)) return NULL;
376 log_assert( ldns_read_uint32(p) == UB_LIBCMD_CANCEL);
377 id = (int)ldns_read_uint32(p+sizeof(uint32_t));
378 q = (struct ctx_query*)rbtree_search(&ctx->queries, &id);
383 context_serialize_quit(uint32_t* len)
385 uint8_t* p = (uint8_t*)malloc(sizeof(uint32_t));
388 *len = sizeof(uint32_t);
389 ldns_write_uint32(p, UB_LIBCMD_QUIT);
393 enum ub_ctx_cmd context_serial_getcmd(uint8_t* p, uint32_t len)
396 if((size_t)len < sizeof(v))
397 return UB_LIBCMD_QUIT;
398 v = ldns_read_uint32(p);