Home | History | Annotate | Line # | Download | only in libunbound
      1      1.1  christos /*
      2      1.1  christos  * libunbound/context.c - validating context for unbound internal use
      3      1.1  christos  *
      4      1.1  christos  * Copyright (c) 2007, NLnet Labs. All rights reserved.
      5      1.1  christos  *
      6      1.1  christos  * This software is open source.
      7      1.1  christos  *
      8      1.1  christos  * Redistribution and use in source and binary forms, with or without
      9      1.1  christos  * modification, are permitted provided that the following conditions
     10      1.1  christos  * are met:
     11      1.1  christos  *
     12      1.1  christos  * Redistributions of source code must retain the above copyright notice,
     13      1.1  christos  * this list of conditions and the following disclaimer.
     14      1.1  christos  *
     15      1.1  christos  * Redistributions in binary form must reproduce the above copyright notice,
     16      1.1  christos  * this list of conditions and the following disclaimer in the documentation
     17      1.1  christos  * and/or other materials provided with the distribution.
     18      1.1  christos  *
     19      1.1  christos  * Neither the name of the NLNET LABS nor the names of its contributors may
     20      1.1  christos  * be used to endorse or promote products derived from this software without
     21      1.1  christos  * specific prior written permission.
     22      1.1  christos  *
     23      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24      1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25      1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26      1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27      1.1  christos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28      1.1  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29      1.1  christos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     30      1.1  christos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31      1.1  christos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32      1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33      1.1  christos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34      1.1  christos  */
     35      1.1  christos 
     36      1.1  christos /**
     37      1.1  christos  * \file
     38      1.1  christos  *
     39      1.1  christos  * This file contains the validator context structure.
     40      1.1  christos  */
     41      1.1  christos #include "config.h"
     42      1.1  christos #include "libunbound/context.h"
     43      1.1  christos #include "util/module.h"
     44      1.1  christos #include "util/config_file.h"
     45      1.1  christos #include "util/net_help.h"
     46      1.1  christos #include "services/modstack.h"
     47      1.1  christos #include "services/localzone.h"
     48      1.1  christos #include "services/cache/rrset.h"
     49      1.1  christos #include "services/cache/infra.h"
     50  1.1.1.3  christos #include "services/authzone.h"
     51  1.1.1.7  christos #include "services/listen_dnsport.h"
     52      1.1  christos #include "util/data/msgreply.h"
     53      1.1  christos #include "util/storage/slabhash.h"
     54  1.1.1.6  christos #include "util/edns.h"
     55      1.1  christos #include "sldns/sbuffer.h"
     56  1.1.1.9  christos #include "iterator/iter_fwd.h"
     57  1.1.1.9  christos #include "iterator/iter_hints.h"
     58      1.1  christos 
     59      1.1  christos int
     60      1.1  christos context_finalize(struct ub_ctx* ctx)
     61      1.1  christos {
     62  1.1.1.6  christos 	int is_rpz = 0;
     63      1.1  christos 	struct config_file* cfg = ctx->env->cfg;
     64      1.1  christos 	verbosity = cfg->verbosity;
     65  1.1.1.5  christos 	if(ctx_logfile_overridden && !ctx->logfile_override) {
     66  1.1.1.5  christos 		log_file(NULL); /* clear that override */
     67  1.1.1.5  christos 		ctx_logfile_overridden = 0;
     68  1.1.1.5  christos 	}
     69  1.1.1.5  christos 	if(ctx->logfile_override) {
     70  1.1.1.5  christos 		ctx_logfile_overridden = 1;
     71      1.1  christos 		log_file(ctx->log_out);
     72  1.1.1.5  christos 	} else {
     73  1.1.1.5  christos 		log_init(cfg->logfile, cfg->use_syslog, NULL);
     74  1.1.1.5  christos 	}
     75  1.1.1.8  christos 	ctx->pipe_pid = getpid();
     76  1.1.1.7  christos 	cfg_apply_local_port_policy(cfg, 65536);
     77      1.1  christos 	config_apply(cfg);
     78  1.1.1.9  christos 	if(!modstack_call_startup(&ctx->mods, cfg->module_conf, ctx->env))
     79  1.1.1.9  christos 		return UB_INITFAIL;
     80  1.1.1.9  christos 	if(!modstack_call_init(&ctx->mods, cfg->module_conf, ctx->env))
     81      1.1  christos 		return UB_INITFAIL;
     82  1.1.1.7  christos 	listen_setup_locks();
     83  1.1.1.2  christos 	log_edns_known_options(VERB_ALGO, ctx->env);
     84      1.1  christos 	ctx->local_zones = local_zones_create();
     85      1.1  christos 	if(!ctx->local_zones)
     86      1.1  christos 		return UB_NOMEM;
     87      1.1  christos 	if(!local_zones_apply_cfg(ctx->local_zones, cfg))
     88      1.1  christos 		return UB_INITFAIL;
     89  1.1.1.7  christos 	if(!auth_zones_apply_cfg(ctx->env->auth_zones, cfg, 1, &is_rpz,
     90  1.1.1.7  christos 		ctx->env, &ctx->mods))
     91  1.1.1.6  christos 		return UB_INITFAIL;
     92  1.1.1.9  christos 	if(!(ctx->env->fwds = forwards_create()) ||
     93  1.1.1.9  christos 		!forwards_apply_cfg(ctx->env->fwds, cfg))
     94  1.1.1.9  christos 		return UB_INITFAIL;
     95  1.1.1.9  christos 	if(!(ctx->env->hints = hints_create()) ||
     96  1.1.1.9  christos 		!hints_apply_cfg(ctx->env->hints, cfg))
     97  1.1.1.9  christos 		return UB_INITFAIL;
     98  1.1.1.6  christos 	if(!edns_strings_apply_cfg(ctx->env->edns_strings, cfg))
     99  1.1.1.3  christos 		return UB_INITFAIL;
    100  1.1.1.4  christos 	if(!slabhash_is_size(ctx->env->msg_cache, cfg->msg_cache_size,
    101  1.1.1.4  christos 		cfg->msg_cache_slabs)) {
    102      1.1  christos 		slabhash_delete(ctx->env->msg_cache);
    103      1.1  christos 		ctx->env->msg_cache = slabhash_create(cfg->msg_cache_slabs,
    104      1.1  christos 			HASH_DEFAULT_STARTARRAY, cfg->msg_cache_size,
    105      1.1  christos 			msgreply_sizefunc, query_info_compare,
    106      1.1  christos 			query_entry_delete, reply_info_delete, NULL);
    107      1.1  christos 		if(!ctx->env->msg_cache)
    108      1.1  christos 			return UB_NOMEM;
    109      1.1  christos 	}
    110      1.1  christos 	ctx->env->rrset_cache = rrset_cache_adjust(ctx->env->rrset_cache,
    111      1.1  christos 		ctx->env->cfg, ctx->env->alloc);
    112      1.1  christos 	if(!ctx->env->rrset_cache)
    113      1.1  christos 		return UB_NOMEM;
    114      1.1  christos 	ctx->env->infra_cache = infra_adjust(ctx->env->infra_cache, cfg);
    115      1.1  christos 	if(!ctx->env->infra_cache)
    116      1.1  christos 		return UB_NOMEM;
    117      1.1  christos 	ctx->finalized = 1;
    118      1.1  christos 	return UB_NOERROR;
    119      1.1  christos }
    120      1.1  christos 
    121      1.1  christos int context_query_cmp(const void* a, const void* b)
    122      1.1  christos {
    123      1.1  christos 	if( *(int*)a < *(int*)b )
    124      1.1  christos 		return -1;
    125      1.1  christos 	if( *(int*)a > *(int*)b )
    126      1.1  christos 		return 1;
    127      1.1  christos 	return 0;
    128      1.1  christos }
    129      1.1  christos 
    130      1.1  christos void
    131      1.1  christos context_query_delete(struct ctx_query* q)
    132      1.1  christos {
    133      1.1  christos 	if(!q) return;
    134      1.1  christos 	ub_resolve_free(q->res);
    135      1.1  christos 	free(q->msg);
    136      1.1  christos 	free(q);
    137      1.1  christos }
    138      1.1  christos 
    139      1.1  christos /** How many times to try to find an unused query-id-number for async */
    140      1.1  christos #define NUM_ID_TRIES 100000
    141      1.1  christos /** find next useful id number of 0 on error */
    142      1.1  christos static int
    143      1.1  christos find_id(struct ub_ctx* ctx, int* id)
    144      1.1  christos {
    145      1.1  christos 	size_t tries = 0;
    146      1.1  christos 	ctx->next_querynum++;
    147      1.1  christos 	while(rbtree_search(&ctx->queries, &ctx->next_querynum)) {
    148      1.1  christos 		ctx->next_querynum++; /* numerical wraparound is fine */
    149      1.1  christos 		if(tries++ > NUM_ID_TRIES)
    150      1.1  christos 			return 0;
    151      1.1  christos 	}
    152      1.1  christos 	*id = ctx->next_querynum;
    153      1.1  christos 	return 1;
    154      1.1  christos }
    155      1.1  christos 
    156      1.1  christos struct ctx_query*
    157      1.1  christos context_new(struct ub_ctx* ctx, const char* name, int rrtype, int rrclass,
    158  1.1.1.3  christos 	ub_callback_type cb, ub_event_callback_type cb_event, void* cbarg)
    159      1.1  christos {
    160      1.1  christos 	struct ctx_query* q = (struct ctx_query*)calloc(1, sizeof(*q));
    161      1.1  christos 	if(!q) return NULL;
    162      1.1  christos 	lock_basic_lock(&ctx->cfglock);
    163      1.1  christos 	if(!find_id(ctx, &q->querynum)) {
    164      1.1  christos 		lock_basic_unlock(&ctx->cfglock);
    165      1.1  christos 		free(q);
    166      1.1  christos 		return NULL;
    167      1.1  christos 	}
    168      1.1  christos 	lock_basic_unlock(&ctx->cfglock);
    169      1.1  christos 	q->node.key = &q->querynum;
    170  1.1.1.3  christos 	q->async = (cb != NULL || cb_event != NULL);
    171      1.1  christos 	q->cb = cb;
    172  1.1.1.3  christos 	q->cb_event = cb_event;
    173      1.1  christos 	q->cb_arg = cbarg;
    174      1.1  christos 	q->res = (struct ub_result*)calloc(1, sizeof(*q->res));
    175      1.1  christos 	if(!q->res) {
    176      1.1  christos 		free(q);
    177      1.1  christos 		return NULL;
    178      1.1  christos 	}
    179      1.1  christos 	q->res->qname = strdup(name);
    180      1.1  christos 	if(!q->res->qname) {
    181      1.1  christos 		free(q->res);
    182      1.1  christos 		free(q);
    183      1.1  christos 		return NULL;
    184      1.1  christos 	}
    185      1.1  christos 	q->res->qtype = rrtype;
    186      1.1  christos 	q->res->qclass = rrclass;
    187      1.1  christos 
    188      1.1  christos 	/* add to query list */
    189      1.1  christos 	lock_basic_lock(&ctx->cfglock);
    190      1.1  christos 	if(q->async)
    191      1.1  christos 		ctx->num_async ++;
    192      1.1  christos 	(void)rbtree_insert(&ctx->queries, &q->node);
    193      1.1  christos 	lock_basic_unlock(&ctx->cfglock);
    194      1.1  christos 	return q;
    195      1.1  christos }
    196      1.1  christos 
    197      1.1  christos struct alloc_cache*
    198      1.1  christos context_obtain_alloc(struct ub_ctx* ctx, int locking)
    199      1.1  christos {
    200      1.1  christos 	struct alloc_cache* a;
    201      1.1  christos 	int tnum = 0;
    202      1.1  christos 	if(locking) {
    203      1.1  christos 		lock_basic_lock(&ctx->cfglock);
    204      1.1  christos 	}
    205      1.1  christos 	a = ctx->alloc_list;
    206      1.1  christos 	if(a)
    207      1.1  christos 		ctx->alloc_list = a->super; /* snip off list */
    208      1.1  christos 	else	tnum = ctx->thr_next_num++;
    209      1.1  christos 	if(locking) {
    210      1.1  christos 		lock_basic_unlock(&ctx->cfglock);
    211      1.1  christos 	}
    212      1.1  christos 	if(a) {
    213      1.1  christos 		a->super = &ctx->superalloc;
    214      1.1  christos 		return a;
    215      1.1  christos 	}
    216      1.1  christos 	a = (struct alloc_cache*)calloc(1, sizeof(*a));
    217      1.1  christos 	if(!a)
    218      1.1  christos 		return NULL;
    219      1.1  christos 	alloc_init(a, &ctx->superalloc, tnum);
    220      1.1  christos 	return a;
    221      1.1  christos }
    222      1.1  christos 
    223      1.1  christos void
    224      1.1  christos context_release_alloc(struct ub_ctx* ctx, struct alloc_cache* alloc,
    225      1.1  christos 	int locking)
    226      1.1  christos {
    227      1.1  christos 	if(!ctx || !alloc)
    228      1.1  christos 		return;
    229      1.1  christos 	if(locking) {
    230      1.1  christos 		lock_basic_lock(&ctx->cfglock);
    231      1.1  christos 	}
    232      1.1  christos 	alloc->super = ctx->alloc_list;
    233      1.1  christos 	ctx->alloc_list = alloc;
    234      1.1  christos 	if(locking) {
    235      1.1  christos 		lock_basic_unlock(&ctx->cfglock);
    236      1.1  christos 	}
    237      1.1  christos }
    238      1.1  christos 
    239      1.1  christos uint8_t*
    240      1.1  christos context_serialize_new_query(struct ctx_query* q, uint32_t* len)
    241      1.1  christos {
    242      1.1  christos 	/* format for new query is
    243      1.1  christos 	 * 	o uint32 cmd
    244      1.1  christos 	 * 	o uint32 id
    245      1.1  christos 	 * 	o uint32 type
    246      1.1  christos 	 * 	o uint32 class
    247      1.1  christos 	 * 	o rest queryname (string)
    248      1.1  christos 	 */
    249      1.1  christos 	uint8_t* p;
    250      1.1  christos 	size_t slen = strlen(q->res->qname) + 1/*end of string*/;
    251      1.1  christos 	*len = sizeof(uint32_t)*4 + slen;
    252      1.1  christos 	p = (uint8_t*)malloc(*len);
    253      1.1  christos 	if(!p) return NULL;
    254      1.1  christos 	sldns_write_uint32(p, UB_LIBCMD_NEWQUERY);
    255      1.1  christos 	sldns_write_uint32(p+sizeof(uint32_t), (uint32_t)q->querynum);
    256      1.1  christos 	sldns_write_uint32(p+2*sizeof(uint32_t), (uint32_t)q->res->qtype);
    257      1.1  christos 	sldns_write_uint32(p+3*sizeof(uint32_t), (uint32_t)q->res->qclass);
    258      1.1  christos 	memmove(p+4*sizeof(uint32_t), q->res->qname, slen);
    259      1.1  christos 	return p;
    260      1.1  christos }
    261      1.1  christos 
    262      1.1  christos struct ctx_query*
    263      1.1  christos context_deserialize_new_query(struct ub_ctx* ctx, uint8_t* p, uint32_t len)
    264      1.1  christos {
    265      1.1  christos 	struct ctx_query* q = (struct ctx_query*)calloc(1, sizeof(*q));
    266      1.1  christos 	if(!q) return NULL;
    267      1.1  christos 	if(len < 4*sizeof(uint32_t)+1) {
    268      1.1  christos 		free(q);
    269      1.1  christos 		return NULL;
    270      1.1  christos 	}
    271      1.1  christos 	log_assert( sldns_read_uint32(p) == UB_LIBCMD_NEWQUERY);
    272      1.1  christos 	q->querynum = (int)sldns_read_uint32(p+sizeof(uint32_t));
    273      1.1  christos 	q->node.key = &q->querynum;
    274      1.1  christos 	q->async = 1;
    275      1.1  christos 	q->res = (struct ub_result*)calloc(1, sizeof(*q->res));
    276      1.1  christos 	if(!q->res) {
    277      1.1  christos 		free(q);
    278      1.1  christos 		return NULL;
    279      1.1  christos 	}
    280      1.1  christos 	q->res->qtype = (int)sldns_read_uint32(p+2*sizeof(uint32_t));
    281      1.1  christos 	q->res->qclass = (int)sldns_read_uint32(p+3*sizeof(uint32_t));
    282      1.1  christos 	q->res->qname = strdup((char*)(p+4*sizeof(uint32_t)));
    283      1.1  christos 	if(!q->res->qname) {
    284      1.1  christos 		free(q->res);
    285      1.1  christos 		free(q);
    286      1.1  christos 		return NULL;
    287      1.1  christos 	}
    288      1.1  christos 
    289      1.1  christos 	/** add to query list */
    290      1.1  christos 	ctx->num_async++;
    291      1.1  christos 	(void)rbtree_insert(&ctx->queries, &q->node);
    292      1.1  christos 	return q;
    293      1.1  christos }
    294      1.1  christos 
    295      1.1  christos struct ctx_query*
    296      1.1  christos context_lookup_new_query(struct ub_ctx* ctx, uint8_t* p, uint32_t len)
    297      1.1  christos {
    298      1.1  christos 	struct ctx_query* q;
    299      1.1  christos 	int querynum;
    300      1.1  christos 	if(len < 4*sizeof(uint32_t)+1) {
    301      1.1  christos 		return NULL;
    302      1.1  christos 	}
    303      1.1  christos 	log_assert( sldns_read_uint32(p) == UB_LIBCMD_NEWQUERY);
    304      1.1  christos 	querynum = (int)sldns_read_uint32(p+sizeof(uint32_t));
    305      1.1  christos 	q = (struct ctx_query*)rbtree_search(&ctx->queries, &querynum);
    306      1.1  christos 	if(!q) {
    307      1.1  christos 		return NULL;
    308      1.1  christos 	}
    309      1.1  christos 	log_assert(q->async);
    310      1.1  christos 	return q;
    311      1.1  christos }
    312      1.1  christos 
    313      1.1  christos uint8_t*
    314      1.1  christos context_serialize_answer(struct ctx_query* q, int err, sldns_buffer* pkt,
    315      1.1  christos 	uint32_t* len)
    316      1.1  christos {
    317      1.1  christos 	/* answer format
    318      1.1  christos 	 * 	o uint32 cmd
    319      1.1  christos 	 * 	o uint32 id
    320      1.1  christos 	 * 	o uint32 error_code
    321      1.1  christos 	 * 	o uint32 msg_security
    322  1.1.1.4  christos 	 * 	o uint32 was_ratelimited
    323      1.1  christos 	 * 	o uint32 length of why_bogus string (+1 for eos); 0 absent.
    324      1.1  christos 	 * 	o why_bogus_string
    325      1.1  christos 	 * 	o the remainder is the answer msg from resolver lookup.
    326      1.1  christos 	 * 	  remainder can be length 0.
    327      1.1  christos 	 */
    328  1.1.1.4  christos 	size_t size_of_uint32s = 6 * sizeof(uint32_t);
    329      1.1  christos 	size_t pkt_len = pkt?sldns_buffer_remaining(pkt):0;
    330      1.1  christos 	size_t wlen = (pkt&&q->res->why_bogus)?strlen(q->res->why_bogus)+1:0;
    331      1.1  christos 	uint8_t* p;
    332  1.1.1.4  christos 	*len = size_of_uint32s + pkt_len + wlen;
    333      1.1  christos 	p = (uint8_t*)malloc(*len);
    334      1.1  christos 	if(!p) return NULL;
    335      1.1  christos 	sldns_write_uint32(p, UB_LIBCMD_ANSWER);
    336      1.1  christos 	sldns_write_uint32(p+sizeof(uint32_t), (uint32_t)q->querynum);
    337      1.1  christos 	sldns_write_uint32(p+2*sizeof(uint32_t), (uint32_t)err);
    338      1.1  christos 	sldns_write_uint32(p+3*sizeof(uint32_t), (uint32_t)q->msg_security);
    339  1.1.1.4  christos 	sldns_write_uint32(p+4*sizeof(uint32_t), (uint32_t)q->res->was_ratelimited);
    340  1.1.1.4  christos 	sldns_write_uint32(p+5*sizeof(uint32_t), (uint32_t)wlen);
    341      1.1  christos 	if(wlen > 0)
    342  1.1.1.4  christos 		memmove(p+size_of_uint32s, q->res->why_bogus, wlen);
    343      1.1  christos 	if(pkt_len > 0)
    344  1.1.1.4  christos 		memmove(p+size_of_uint32s+wlen,
    345      1.1  christos 			sldns_buffer_begin(pkt), pkt_len);
    346      1.1  christos 	return p;
    347      1.1  christos }
    348      1.1  christos 
    349      1.1  christos struct ctx_query*
    350      1.1  christos context_deserialize_answer(struct ub_ctx* ctx,
    351      1.1  christos         uint8_t* p, uint32_t len, int* err)
    352      1.1  christos {
    353  1.1.1.4  christos 	size_t size_of_uint32s = 6 * sizeof(uint32_t);
    354      1.1  christos 	struct ctx_query* q = NULL ;
    355      1.1  christos 	int id;
    356      1.1  christos 	size_t wlen;
    357  1.1.1.4  christos 	if(len < size_of_uint32s) return NULL;
    358      1.1  christos 	log_assert( sldns_read_uint32(p) == UB_LIBCMD_ANSWER);
    359      1.1  christos 	id = (int)sldns_read_uint32(p+sizeof(uint32_t));
    360      1.1  christos 	q = (struct ctx_query*)rbtree_search(&ctx->queries, &id);
    361      1.1  christos 	if(!q) return NULL;
    362      1.1  christos 	*err = (int)sldns_read_uint32(p+2*sizeof(uint32_t));
    363      1.1  christos 	q->msg_security = sldns_read_uint32(p+3*sizeof(uint32_t));
    364  1.1.1.4  christos 	q->res->was_ratelimited = (int)sldns_read_uint32(p+4*sizeof(uint32_t));
    365  1.1.1.4  christos 	wlen = (size_t)sldns_read_uint32(p+5*sizeof(uint32_t));
    366  1.1.1.4  christos 	if(len > size_of_uint32s && wlen > 0) {
    367  1.1.1.4  christos 		if(len >= size_of_uint32s+wlen)
    368      1.1  christos 			q->res->why_bogus = (char*)memdup(
    369  1.1.1.4  christos 				p+size_of_uint32s, wlen);
    370      1.1  christos 		if(!q->res->why_bogus) {
    371      1.1  christos 			/* pass malloc failure to the user callback */
    372      1.1  christos 			q->msg_len = 0;
    373      1.1  christos 			*err = UB_NOMEM;
    374      1.1  christos 			return q;
    375      1.1  christos 		}
    376      1.1  christos 		q->res->why_bogus[wlen-1] = 0; /* zero terminated for sure */
    377      1.1  christos 	}
    378  1.1.1.4  christos 	if(len > size_of_uint32s+wlen) {
    379  1.1.1.4  christos 		q->msg_len = len - size_of_uint32s - wlen;
    380  1.1.1.4  christos 		q->msg = (uint8_t*)memdup(p+size_of_uint32s+wlen,
    381      1.1  christos 			q->msg_len);
    382      1.1  christos 		if(!q->msg) {
    383      1.1  christos 			/* pass malloc failure to the user callback */
    384      1.1  christos 			q->msg_len = 0;
    385      1.1  christos 			*err = UB_NOMEM;
    386      1.1  christos 			return q;
    387      1.1  christos 		}
    388      1.1  christos 	}
    389      1.1  christos 	return q;
    390      1.1  christos }
    391      1.1  christos 
    392      1.1  christos uint8_t*
    393      1.1  christos context_serialize_cancel(struct ctx_query* q, uint32_t* len)
    394      1.1  christos {
    395      1.1  christos 	/* format of cancel:
    396      1.1  christos 	 * 	o uint32 cmd
    397      1.1  christos 	 * 	o uint32 async-id */
    398  1.1.1.9  christos 	uint8_t* p = (uint8_t*)reallocarray(NULL, 2, sizeof(uint32_t));
    399      1.1  christos 	if(!p) return NULL;
    400      1.1  christos 	*len = 2*sizeof(uint32_t);
    401      1.1  christos 	sldns_write_uint32(p, UB_LIBCMD_CANCEL);
    402      1.1  christos 	sldns_write_uint32(p+sizeof(uint32_t), (uint32_t)q->querynum);
    403      1.1  christos 	return p;
    404      1.1  christos }
    405      1.1  christos 
    406      1.1  christos struct ctx_query* context_deserialize_cancel(struct ub_ctx* ctx,
    407      1.1  christos         uint8_t* p, uint32_t len)
    408      1.1  christos {
    409      1.1  christos 	struct ctx_query* q;
    410      1.1  christos 	int id;
    411      1.1  christos 	if(len != 2*sizeof(uint32_t)) return NULL;
    412      1.1  christos 	log_assert( sldns_read_uint32(p) == UB_LIBCMD_CANCEL);
    413      1.1  christos 	id = (int)sldns_read_uint32(p+sizeof(uint32_t));
    414      1.1  christos 	q = (struct ctx_query*)rbtree_search(&ctx->queries, &id);
    415      1.1  christos 	return q;
    416      1.1  christos }
    417      1.1  christos 
    418      1.1  christos uint8_t*
    419      1.1  christos context_serialize_quit(uint32_t* len)
    420      1.1  christos {
    421  1.1.1.4  christos 	uint32_t* p = (uint32_t*)malloc(sizeof(uint32_t));
    422      1.1  christos 	if(!p)
    423      1.1  christos 		return NULL;
    424      1.1  christos 	*len = sizeof(uint32_t);
    425      1.1  christos 	sldns_write_uint32(p, UB_LIBCMD_QUIT);
    426  1.1.1.4  christos 	return (uint8_t*)p;
    427      1.1  christos }
    428      1.1  christos 
    429      1.1  christos enum ub_ctx_cmd context_serial_getcmd(uint8_t* p, uint32_t len)
    430      1.1  christos {
    431      1.1  christos 	uint32_t v;
    432      1.1  christos 	if((size_t)len < sizeof(v))
    433      1.1  christos 		return UB_LIBCMD_QUIT;
    434      1.1  christos 	v = sldns_read_uint32(p);
    435      1.1  christos 	return v;
    436      1.1  christos }
    437