xref: /freebsd/contrib/unbound/libunbound/libworker.c (revision c9dbb1cc52b063bbd9ab078a7afc89a8696da659)
1 /*
2  * libunbound/worker.c - worker thread or process that resolves
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 worker process or thread that performs
40  * the DNS resolving and validation. The worker is called by a procedure
41  * and if in the background continues until exit, if in the foreground
42  * returns from the procedure when done.
43  */
44 #include "config.h"
45 #ifdef HAVE_SSL
46 #include <openssl/ssl.h>
47 #endif
48 #include "libunbound/libworker.h"
49 #include "libunbound/context.h"
50 #include "libunbound/unbound.h"
51 #include "libunbound/worker.h"
52 #include "libunbound/unbound-event.h"
53 #include "services/outside_network.h"
54 #include "services/mesh.h"
55 #include "services/localzone.h"
56 #include "services/cache/rrset.h"
57 #include "services/outbound_list.h"
58 #include "util/fptr_wlist.h"
59 #include "util/module.h"
60 #include "util/regional.h"
61 #include "util/random.h"
62 #include "util/config_file.h"
63 #include "util/netevent.h"
64 #include "util/storage/lookup3.h"
65 #include "util/storage/slabhash.h"
66 #include "util/net_help.h"
67 #include "util/data/dname.h"
68 #include "util/data/msgreply.h"
69 #include "util/data/msgencode.h"
70 #include "util/tube.h"
71 #include "iterator/iter_fwd.h"
72 #include "iterator/iter_hints.h"
73 #include "sldns/sbuffer.h"
74 #include "sldns/str2wire.h"
75 
76 /** handle new query command for bg worker */
77 static void handle_newq(struct libworker* w, uint8_t* buf, uint32_t len);
78 
79 /** delete libworker env */
80 static void
81 libworker_delete_env(struct libworker* w)
82 {
83 	if(w->env) {
84 		outside_network_quit_prepare(w->back);
85 		mesh_delete(w->env->mesh);
86 		context_release_alloc(w->ctx, w->env->alloc,
87 			!w->is_bg || w->is_bg_thread);
88 		sldns_buffer_free(w->env->scratch_buffer);
89 		regional_destroy(w->env->scratch);
90 		forwards_delete(w->env->fwds);
91 		hints_delete(w->env->hints);
92 		ub_randfree(w->env->rnd);
93 		free(w->env);
94 	}
95 #ifdef HAVE_SSL
96 	SSL_CTX_free(w->sslctx);
97 #endif
98 	outside_network_delete(w->back);
99 }
100 
101 /** delete libworker struct */
102 static void
103 libworker_delete(struct libworker* w)
104 {
105 	if(!w) return;
106 	libworker_delete_env(w);
107 	comm_base_delete(w->base);
108 	free(w);
109 }
110 
111 void
112 libworker_delete_event(struct libworker* w)
113 {
114 	if(!w) return;
115 	libworker_delete_env(w);
116 	comm_base_delete_no_base(w->base);
117 	free(w);
118 }
119 
120 /** setup fresh libworker struct */
121 static struct libworker*
122 libworker_setup(struct ub_ctx* ctx, int is_bg, struct event_base* eb)
123 {
124 	unsigned int seed;
125 	struct libworker* w = (struct libworker*)calloc(1, sizeof(*w));
126 	struct config_file* cfg = ctx->env->cfg;
127 	int* ports;
128 	int numports;
129 	if(!w) return NULL;
130 	w->is_bg = is_bg;
131 	w->ctx = ctx;
132 	w->env = (struct module_env*)malloc(sizeof(*w->env));
133 	if(!w->env) {
134 		free(w);
135 		return NULL;
136 	}
137 	*w->env = *ctx->env;
138 	w->env->alloc = context_obtain_alloc(ctx, !w->is_bg || w->is_bg_thread);
139 	if(!w->env->alloc) {
140 		libworker_delete(w);
141 		return NULL;
142 	}
143 	w->thread_num = w->env->alloc->thread_num;
144 	alloc_set_id_cleanup(w->env->alloc, &libworker_alloc_cleanup, w);
145 	if(!w->is_bg || w->is_bg_thread) {
146 		lock_basic_lock(&ctx->cfglock);
147 	}
148 	w->env->scratch = regional_create_custom(cfg->msg_buffer_size);
149 	w->env->scratch_buffer = sldns_buffer_new(cfg->msg_buffer_size);
150 	w->env->fwds = forwards_create();
151 	if(w->env->fwds && !forwards_apply_cfg(w->env->fwds, cfg)) {
152 		forwards_delete(w->env->fwds);
153 		w->env->fwds = NULL;
154 	}
155 	w->env->hints = hints_create();
156 	if(w->env->hints && !hints_apply_cfg(w->env->hints, cfg)) {
157 		hints_delete(w->env->hints);
158 		w->env->hints = NULL;
159 	}
160 	if(cfg->ssl_upstream) {
161 		w->sslctx = connect_sslctx_create(NULL, NULL, NULL);
162 		if(!w->sslctx) {
163 			/* to make the setup fail after unlock */
164 			hints_delete(w->env->hints);
165 			w->env->hints = NULL;
166 		}
167 	}
168 	if(!w->is_bg || w->is_bg_thread) {
169 		lock_basic_unlock(&ctx->cfglock);
170 	}
171 	if(!w->env->scratch || !w->env->scratch_buffer || !w->env->fwds ||
172 		!w->env->hints) {
173 		libworker_delete(w);
174 		return NULL;
175 	}
176 	w->env->worker = (struct worker*)w;
177 	w->env->probe_timer = NULL;
178 	seed = (unsigned int)time(NULL) ^ (unsigned int)getpid() ^
179 		(((unsigned int)w->thread_num)<<17);
180 	seed ^= (unsigned int)w->env->alloc->next_id;
181 	if(!w->is_bg || w->is_bg_thread) {
182 		lock_basic_lock(&ctx->cfglock);
183 	}
184 	if(!(w->env->rnd = ub_initstate(seed, ctx->seed_rnd))) {
185 		if(!w->is_bg || w->is_bg_thread) {
186 			lock_basic_unlock(&ctx->cfglock);
187 		}
188 		seed = 0;
189 		libworker_delete(w);
190 		return NULL;
191 	}
192 	if(!w->is_bg || w->is_bg_thread) {
193 		lock_basic_unlock(&ctx->cfglock);
194 	}
195 	if(1) {
196 		/* primitive lockout for threading: if it overwrites another
197 		 * thread it is like wiping the cache (which is likely empty
198 		 * at the start) */
199 		/* note we are holding the ctx lock in normal threaded
200 		 * cases so that is solved properly, it is only for many ctx
201 		 * in different threads that this may clash */
202 		static int done_raninit = 0;
203 		if(!done_raninit) {
204 			done_raninit = 1;
205 			hash_set_raninit((uint32_t)ub_random(w->env->rnd));
206 		}
207 	}
208 	seed = 0;
209 
210 	if(eb)
211 		w->base = comm_base_create_event(eb);
212 	else	w->base = comm_base_create(0);
213 	if(!w->base) {
214 		libworker_delete(w);
215 		return NULL;
216 	}
217 	if(!w->is_bg || w->is_bg_thread) {
218 		lock_basic_lock(&ctx->cfglock);
219 	}
220 	numports = cfg_condense_ports(cfg, &ports);
221 	if(numports == 0) {
222 		int locked = !w->is_bg || w->is_bg_thread;
223 		libworker_delete(w);
224 		if(locked) {
225 			lock_basic_unlock(&ctx->cfglock);
226 		}
227 		return NULL;
228 	}
229 	w->back = outside_network_create(w->base, cfg->msg_buffer_size,
230 		(size_t)cfg->outgoing_num_ports, cfg->out_ifs,
231 		cfg->num_out_ifs, cfg->do_ip4, cfg->do_ip6,
232 		cfg->do_tcp?cfg->outgoing_num_tcp:0,
233 		w->env->infra_cache, w->env->rnd, cfg->use_caps_bits_for_id,
234 		ports, numports, cfg->unwanted_threshold,
235 		&libworker_alloc_cleanup, w, cfg->do_udp, w->sslctx,
236 		cfg->delay_close, NULL);
237 	if(!w->is_bg || w->is_bg_thread) {
238 		lock_basic_unlock(&ctx->cfglock);
239 	}
240 	free(ports);
241 	if(!w->back) {
242 		libworker_delete(w);
243 		return NULL;
244 	}
245 	w->env->mesh = mesh_create(&ctx->mods, w->env);
246 	if(!w->env->mesh) {
247 		libworker_delete(w);
248 		return NULL;
249 	}
250 	w->env->send_query = &libworker_send_query;
251 	w->env->detach_subs = &mesh_detach_subs;
252 	w->env->attach_sub = &mesh_attach_sub;
253 	w->env->kill_sub = &mesh_state_delete;
254 	w->env->detect_cycle = &mesh_detect_cycle;
255 	comm_base_timept(w->base, &w->env->now, &w->env->now_tv);
256 	return w;
257 }
258 
259 struct libworker* libworker_create_event(struct ub_ctx* ctx,
260 	struct event_base* eb)
261 {
262 	return libworker_setup(ctx, 0, eb);
263 }
264 
265 /** handle cancel command for bg worker */
266 static void
267 handle_cancel(struct libworker* w, uint8_t* buf, uint32_t len)
268 {
269 	struct ctx_query* q;
270 	if(w->is_bg_thread) {
271 		lock_basic_lock(&w->ctx->cfglock);
272 		q = context_deserialize_cancel(w->ctx, buf, len);
273 		lock_basic_unlock(&w->ctx->cfglock);
274 	} else {
275 		q = context_deserialize_cancel(w->ctx, buf, len);
276 	}
277 	if(!q) {
278 		/* probably simply lookup failed, i.e. the message had been
279 		 * processed and answered before the cancel arrived */
280 		return;
281 	}
282 	q->cancelled = 1;
283 	free(buf);
284 }
285 
286 /** do control command coming into bg server */
287 static void
288 libworker_do_cmd(struct libworker* w, uint8_t* msg, uint32_t len)
289 {
290 	switch(context_serial_getcmd(msg, len)) {
291 		default:
292 		case UB_LIBCMD_ANSWER:
293 			log_err("unknown command for bg worker %d",
294 				(int)context_serial_getcmd(msg, len));
295 			/* and fall through to quit */
296 		case UB_LIBCMD_QUIT:
297 			free(msg);
298 			comm_base_exit(w->base);
299 			break;
300 		case UB_LIBCMD_NEWQUERY:
301 			handle_newq(w, msg, len);
302 			break;
303 		case UB_LIBCMD_CANCEL:
304 			handle_cancel(w, msg, len);
305 			break;
306 	}
307 }
308 
309 /** handle control command coming into server */
310 void
311 libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
312 	uint8_t* msg, size_t len, int err, void* arg)
313 {
314 	struct libworker* w = (struct libworker*)arg;
315 
316 	if(err != 0) {
317 		free(msg);
318 		/* it is of no use to go on, exit */
319 		comm_base_exit(w->base);
320 		return;
321 	}
322 	libworker_do_cmd(w, msg, len); /* also frees the buf */
323 }
324 
325 /** the background thread func */
326 static void*
327 libworker_dobg(void* arg)
328 {
329 	/* setup */
330 	uint32_t m;
331 	struct libworker* w = (struct libworker*)arg;
332 	struct ub_ctx* ctx;
333 	if(!w) {
334 		log_err("libunbound bg worker init failed, nomem");
335 		return NULL;
336 	}
337 	ctx = w->ctx;
338 	log_thread_set(&w->thread_num);
339 #ifdef THREADS_DISABLED
340 	/* we are forked */
341 	w->is_bg_thread = 0;
342 	/* close non-used parts of the pipes */
343 	tube_close_write(ctx->qq_pipe);
344 	tube_close_read(ctx->rr_pipe);
345 #endif
346 	if(!tube_setup_bg_listen(ctx->qq_pipe, w->base,
347 		libworker_handle_control_cmd, w)) {
348 		log_err("libunbound bg worker init failed, no bglisten");
349 		return NULL;
350 	}
351 	if(!tube_setup_bg_write(ctx->rr_pipe, w->base)) {
352 		log_err("libunbound bg worker init failed, no bgwrite");
353 		return NULL;
354 	}
355 
356 	/* do the work */
357 	comm_base_dispatch(w->base);
358 
359 	/* cleanup */
360 	m = UB_LIBCMD_QUIT;
361 	tube_remove_bg_listen(w->ctx->qq_pipe);
362 	tube_remove_bg_write(w->ctx->rr_pipe);
363 	libworker_delete(w);
364 	(void)tube_write_msg(ctx->rr_pipe, (uint8_t*)&m,
365 		(uint32_t)sizeof(m), 0);
366 #ifdef THREADS_DISABLED
367 	/* close pipes from forked process before exit */
368 	tube_close_read(ctx->qq_pipe);
369 	tube_close_write(ctx->rr_pipe);
370 #endif
371 	return NULL;
372 }
373 
374 int libworker_bg(struct ub_ctx* ctx)
375 {
376 	struct libworker* w;
377 	/* fork or threadcreate */
378 	lock_basic_lock(&ctx->cfglock);
379 	if(ctx->dothread) {
380 		lock_basic_unlock(&ctx->cfglock);
381 		w = libworker_setup(ctx, 1, NULL);
382 		if(!w) return UB_NOMEM;
383 		w->is_bg_thread = 1;
384 #ifdef ENABLE_LOCK_CHECKS
385 		w->thread_num = 1; /* for nicer DEBUG checklocks */
386 #endif
387 		ub_thread_create(&ctx->bg_tid, libworker_dobg, w);
388 	} else {
389 		lock_basic_unlock(&ctx->cfglock);
390 #ifndef HAVE_FORK
391 		/* no fork on windows */
392 		return UB_FORKFAIL;
393 #else /* HAVE_FORK */
394 		switch((ctx->bg_pid=fork())) {
395 			case 0:
396 				w = libworker_setup(ctx, 1, NULL);
397 				if(!w) fatal_exit("out of memory");
398 				/* close non-used parts of the pipes */
399 				tube_close_write(ctx->qq_pipe);
400 				tube_close_read(ctx->rr_pipe);
401 				(void)libworker_dobg(w);
402 				exit(0);
403 				break;
404 			case -1:
405 				return UB_FORKFAIL;
406 			default:
407 				/* close non-used parts, so that the worker
408 				 * bgprocess gets 'pipe closed' when the
409 				 * main process exits */
410 				tube_close_read(ctx->qq_pipe);
411 				tube_close_write(ctx->rr_pipe);
412 				break;
413 		}
414 #endif /* HAVE_FORK */
415 	}
416 	return UB_NOERROR;
417 }
418 
419 /** get msg reply struct (in temp region) */
420 static struct reply_info*
421 parse_reply(sldns_buffer* pkt, struct regional* region, struct query_info* qi)
422 {
423 	struct reply_info* rep;
424 	struct msg_parse* msg;
425 	if(!(msg = regional_alloc(region, sizeof(*msg)))) {
426 		return NULL;
427 	}
428 	memset(msg, 0, sizeof(*msg));
429 	sldns_buffer_set_position(pkt, 0);
430 	if(parse_packet(pkt, msg, region) != 0)
431 		return 0;
432 	if(!parse_create_msg(pkt, msg, NULL, qi, &rep, region)) {
433 		return 0;
434 	}
435 	return rep;
436 }
437 
438 /** insert canonname */
439 static int
440 fill_canon(struct ub_result* res, uint8_t* s)
441 {
442 	char buf[255+2];
443 	dname_str(s, buf);
444 	res->canonname = strdup(buf);
445 	return res->canonname != 0;
446 }
447 
448 /** fill data into result */
449 static int
450 fill_res(struct ub_result* res, struct ub_packed_rrset_key* answer,
451 	uint8_t* finalcname, struct query_info* rq, struct reply_info* rep)
452 {
453 	size_t i;
454 	struct packed_rrset_data* data;
455 	res->ttl = 0;
456 	if(!answer) {
457 		if(finalcname) {
458 			if(!fill_canon(res, finalcname))
459 				return 0; /* out of memory */
460 		}
461 		if(rep->rrset_count != 0)
462 			res->ttl = (int)rep->ttl;
463 		res->data = (char**)calloc(1, sizeof(char*));
464 		res->len = (int*)calloc(1, sizeof(int));
465 		return (res->data && res->len);
466 	}
467 	data = (struct packed_rrset_data*)answer->entry.data;
468 	if(query_dname_compare(rq->qname, answer->rk.dname) != 0) {
469 		if(!fill_canon(res, answer->rk.dname))
470 			return 0; /* out of memory */
471 	} else	res->canonname = NULL;
472 	res->data = (char**)calloc(data->count+1, sizeof(char*));
473 	res->len = (int*)calloc(data->count+1, sizeof(int));
474 	if(!res->data || !res->len)
475 		return 0; /* out of memory */
476 	for(i=0; i<data->count; i++) {
477 		/* remove rdlength from rdata */
478 		res->len[i] = (int)(data->rr_len[i] - 2);
479 		res->data[i] = memdup(data->rr_data[i]+2, (size_t)res->len[i]);
480 		if(!res->data[i])
481 			return 0; /* out of memory */
482 	}
483 	/* ttl for positive answers, from CNAME and answer RRs */
484 	if(data->count != 0) {
485 		size_t j;
486 		res->ttl = (int)data->ttl;
487 		for(j=0; j<rep->an_numrrsets; j++) {
488 			struct packed_rrset_data* d =
489 				(struct packed_rrset_data*)rep->rrsets[j]->
490 				entry.data;
491 			if((int)d->ttl < res->ttl)
492 				res->ttl = (int)d->ttl;
493 		}
494 	}
495 	/* ttl for negative answers */
496 	if(data->count == 0 && rep->rrset_count != 0)
497 		res->ttl = (int)rep->ttl;
498 	res->data[data->count] = NULL;
499 	res->len[data->count] = 0;
500 	return 1;
501 }
502 
503 /** fill result from parsed message, on error fills servfail */
504 void
505 libworker_enter_result(struct ub_result* res, sldns_buffer* buf,
506 	struct regional* temp, enum sec_status msg_security)
507 {
508 	struct query_info rq;
509 	struct reply_info* rep;
510 	res->rcode = LDNS_RCODE_SERVFAIL;
511 	rep = parse_reply(buf, temp, &rq);
512 	if(!rep) {
513 		log_err("cannot parse buf");
514 		return; /* error parsing buf, or out of memory */
515 	}
516 	if(!fill_res(res, reply_find_answer_rrset(&rq, rep),
517 		reply_find_final_cname_target(&rq, rep), &rq, rep))
518 		return; /* out of memory */
519 	/* rcode, havedata, nxdomain, secure, bogus */
520 	res->rcode = (int)FLAGS_GET_RCODE(rep->flags);
521 	if(res->data && res->data[0])
522 		res->havedata = 1;
523 	if(res->rcode == LDNS_RCODE_NXDOMAIN)
524 		res->nxdomain = 1;
525 	if(msg_security == sec_status_secure)
526 		res->secure = 1;
527 	if(msg_security == sec_status_bogus)
528 		res->bogus = 1;
529 }
530 
531 /** fillup fg results */
532 static void
533 libworker_fillup_fg(struct ctx_query* q, int rcode, sldns_buffer* buf,
534 	enum sec_status s, char* why_bogus)
535 {
536 	if(why_bogus)
537 		q->res->why_bogus = strdup(why_bogus);
538 	if(rcode != 0) {
539 		q->res->rcode = rcode;
540 		q->msg_security = s;
541 		return;
542 	}
543 
544 	q->res->rcode = LDNS_RCODE_SERVFAIL;
545 	q->msg_security = 0;
546 	q->msg = memdup(sldns_buffer_begin(buf), sldns_buffer_limit(buf));
547 	q->msg_len = sldns_buffer_limit(buf);
548 	if(!q->msg) {
549 		return; /* the error is in the rcode */
550 	}
551 
552 	/* canonname and results */
553 	q->msg_security = s;
554 	libworker_enter_result(q->res, buf, q->w->env->scratch, s);
555 }
556 
557 void
558 libworker_fg_done_cb(void* arg, int rcode, sldns_buffer* buf, enum sec_status s,
559 	char* why_bogus)
560 {
561 	struct ctx_query* q = (struct ctx_query*)arg;
562 	/* fg query is done; exit comm base */
563 	comm_base_exit(q->w->base);
564 
565 	libworker_fillup_fg(q, rcode, buf, s, why_bogus);
566 }
567 
568 /** setup qinfo and edns */
569 static int
570 setup_qinfo_edns(struct libworker* w, struct ctx_query* q,
571 	struct query_info* qinfo, struct edns_data* edns)
572 {
573 	qinfo->qtype = (uint16_t)q->res->qtype;
574 	qinfo->qclass = (uint16_t)q->res->qclass;
575 	qinfo->qname = sldns_str2wire_dname(q->res->qname, &qinfo->qname_len);
576 	if(!qinfo->qname) {
577 		return 0;
578 	}
579 	edns->edns_present = 1;
580 	edns->ext_rcode = 0;
581 	edns->edns_version = 0;
582 	edns->bits = EDNS_DO;
583 	if(sldns_buffer_capacity(w->back->udp_buff) < 65535)
584 		edns->udp_size = (uint16_t)sldns_buffer_capacity(
585 			w->back->udp_buff);
586 	else	edns->udp_size = 65535;
587 	return 1;
588 }
589 
590 int libworker_fg(struct ub_ctx* ctx, struct ctx_query* q)
591 {
592 	struct libworker* w = libworker_setup(ctx, 0, NULL);
593 	uint16_t qflags, qid;
594 	struct query_info qinfo;
595 	struct edns_data edns;
596 	if(!w)
597 		return UB_INITFAIL;
598 	if(!setup_qinfo_edns(w, q, &qinfo, &edns)) {
599 		libworker_delete(w);
600 		return UB_SYNTAX;
601 	}
602 	qid = 0;
603 	qflags = BIT_RD;
604 	q->w = w;
605 	/* see if there is a fixed answer */
606 	sldns_buffer_write_u16_at(w->back->udp_buff, 0, qid);
607 	sldns_buffer_write_u16_at(w->back->udp_buff, 2, qflags);
608 	if(local_zones_answer(ctx->local_zones, &qinfo, &edns,
609 		w->back->udp_buff, w->env->scratch, NULL)) {
610 		regional_free_all(w->env->scratch);
611 		libworker_fillup_fg(q, LDNS_RCODE_NOERROR,
612 			w->back->udp_buff, sec_status_insecure, NULL);
613 		libworker_delete(w);
614 		free(qinfo.qname);
615 		return UB_NOERROR;
616 	}
617 	/* process new query */
618 	if(!mesh_new_callback(w->env->mesh, &qinfo, qflags, &edns,
619 		w->back->udp_buff, qid, libworker_fg_done_cb, q)) {
620 		free(qinfo.qname);
621 		return UB_NOMEM;
622 	}
623 	free(qinfo.qname);
624 
625 	/* wait for reply */
626 	comm_base_dispatch(w->base);
627 
628 	libworker_delete(w);
629 	return UB_NOERROR;
630 }
631 
632 void
633 libworker_event_done_cb(void* arg, int rcode, sldns_buffer* buf,
634 	enum sec_status s, char* why_bogus)
635 {
636 	struct ctx_query* q = (struct ctx_query*)arg;
637 	ub_event_callback_t cb = (ub_event_callback_t)q->cb;
638 	void* cb_arg = q->cb_arg;
639 	int cancelled = q->cancelled;
640 
641 	/* delete it now */
642 	struct ub_ctx* ctx = q->w->ctx;
643 	lock_basic_lock(&ctx->cfglock);
644 	(void)rbtree_delete(&ctx->queries, q->node.key);
645 	ctx->num_async--;
646 	context_query_delete(q);
647 	lock_basic_unlock(&ctx->cfglock);
648 
649 	if(!cancelled) {
650 		/* call callback */
651 		int sec = 0;
652 		if(s == sec_status_bogus)
653 			sec = 1;
654 		else if(s == sec_status_secure)
655 			sec = 2;
656 		(*cb)(cb_arg, rcode, (void*)sldns_buffer_begin(buf),
657 			(int)sldns_buffer_limit(buf), sec, why_bogus);
658 	}
659 }
660 
661 int libworker_attach_mesh(struct ub_ctx* ctx, struct ctx_query* q,
662 	int* async_id)
663 {
664 	struct libworker* w = ctx->event_worker;
665 	uint16_t qflags, qid;
666 	struct query_info qinfo;
667 	struct edns_data edns;
668 	if(!w)
669 		return UB_INITFAIL;
670 	if(!setup_qinfo_edns(w, q, &qinfo, &edns))
671 		return UB_SYNTAX;
672 	qid = 0;
673 	qflags = BIT_RD;
674 	q->w = w;
675 	/* see if there is a fixed answer */
676 	sldns_buffer_write_u16_at(w->back->udp_buff, 0, qid);
677 	sldns_buffer_write_u16_at(w->back->udp_buff, 2, qflags);
678 	if(local_zones_answer(ctx->local_zones, &qinfo, &edns,
679 		w->back->udp_buff, w->env->scratch, NULL)) {
680 		regional_free_all(w->env->scratch);
681 		free(qinfo.qname);
682 		libworker_event_done_cb(q, LDNS_RCODE_NOERROR,
683 			w->back->udp_buff, sec_status_insecure, NULL);
684 		return UB_NOERROR;
685 	}
686 	/* process new query */
687 	if(async_id)
688 		*async_id = q->querynum;
689 	if(!mesh_new_callback(w->env->mesh, &qinfo, qflags, &edns,
690 		w->back->udp_buff, qid, libworker_event_done_cb, q)) {
691 		free(qinfo.qname);
692 		return UB_NOMEM;
693 	}
694 	free(qinfo.qname);
695 	return UB_NOERROR;
696 }
697 
698 /** add result to the bg worker result queue */
699 static void
700 add_bg_result(struct libworker* w, struct ctx_query* q, sldns_buffer* pkt,
701 	int err, char* reason)
702 {
703 	uint8_t* msg = NULL;
704 	uint32_t len = 0;
705 
706 	/* serialize and delete unneeded q */
707 	if(w->is_bg_thread) {
708 		lock_basic_lock(&w->ctx->cfglock);
709 		if(reason)
710 			q->res->why_bogus = strdup(reason);
711 		if(pkt) {
712 			q->msg_len = sldns_buffer_remaining(pkt);
713 			q->msg = memdup(sldns_buffer_begin(pkt), q->msg_len);
714 			if(!q->msg)
715 				msg = context_serialize_answer(q, UB_NOMEM,
716 				NULL, &len);
717 			else	msg = context_serialize_answer(q, err,
718 				NULL, &len);
719 		} else msg = context_serialize_answer(q, err, NULL, &len);
720 		lock_basic_unlock(&w->ctx->cfglock);
721 	} else {
722 		if(reason)
723 			q->res->why_bogus = strdup(reason);
724 		msg = context_serialize_answer(q, err, pkt, &len);
725 		(void)rbtree_delete(&w->ctx->queries, q->node.key);
726 		w->ctx->num_async--;
727 		context_query_delete(q);
728 	}
729 
730 	if(!msg) {
731 		log_err("out of memory for async answer");
732 		return;
733 	}
734 	if(!tube_queue_item(w->ctx->rr_pipe, msg, len)) {
735 		log_err("out of memory for async answer");
736 		return;
737 	}
738 }
739 
740 void
741 libworker_bg_done_cb(void* arg, int rcode, sldns_buffer* buf, enum sec_status s,
742 	char* why_bogus)
743 {
744 	struct ctx_query* q = (struct ctx_query*)arg;
745 
746 	if(q->cancelled) {
747 		if(q->w->is_bg_thread) {
748 			/* delete it now */
749 			struct ub_ctx* ctx = q->w->ctx;
750 			lock_basic_lock(&ctx->cfglock);
751 			(void)rbtree_delete(&ctx->queries, q->node.key);
752 			ctx->num_async--;
753 			context_query_delete(q);
754 			lock_basic_unlock(&ctx->cfglock);
755 		}
756 		/* cancelled, do not give answer */
757 		return;
758 	}
759 	q->msg_security = s;
760 	if(!buf)
761 		buf = q->w->env->scratch_buffer;
762 	if(rcode != 0) {
763 		error_encode(buf, rcode, NULL, 0, BIT_RD, NULL);
764 	}
765 	add_bg_result(q->w, q, buf, UB_NOERROR, why_bogus);
766 }
767 
768 
769 /** handle new query command for bg worker */
770 static void
771 handle_newq(struct libworker* w, uint8_t* buf, uint32_t len)
772 {
773 	uint16_t qflags, qid;
774 	struct query_info qinfo;
775 	struct edns_data edns;
776 	struct ctx_query* q;
777 	if(w->is_bg_thread) {
778 		lock_basic_lock(&w->ctx->cfglock);
779 		q = context_lookup_new_query(w->ctx, buf, len);
780 		lock_basic_unlock(&w->ctx->cfglock);
781 	} else {
782 		q = context_deserialize_new_query(w->ctx, buf, len);
783 	}
784 	free(buf);
785 	if(!q) {
786 		log_err("failed to deserialize newq");
787 		return;
788 	}
789 	if(!setup_qinfo_edns(w, q, &qinfo, &edns)) {
790 		add_bg_result(w, q, NULL, UB_SYNTAX, NULL);
791 		return;
792 	}
793 	qid = 0;
794 	qflags = BIT_RD;
795 	/* see if there is a fixed answer */
796 	sldns_buffer_write_u16_at(w->back->udp_buff, 0, qid);
797 	sldns_buffer_write_u16_at(w->back->udp_buff, 2, qflags);
798 	if(local_zones_answer(w->ctx->local_zones, &qinfo, &edns,
799 		w->back->udp_buff, w->env->scratch, NULL)) {
800 		regional_free_all(w->env->scratch);
801 		q->msg_security = sec_status_insecure;
802 		add_bg_result(w, q, w->back->udp_buff, UB_NOERROR, NULL);
803 		free(qinfo.qname);
804 		return;
805 	}
806 	q->w = w;
807 	/* process new query */
808 	if(!mesh_new_callback(w->env->mesh, &qinfo, qflags, &edns,
809 		w->back->udp_buff, qid, libworker_bg_done_cb, q)) {
810 		add_bg_result(w, q, NULL, UB_NOMEM, NULL);
811 	}
812 	free(qinfo.qname);
813 }
814 
815 void libworker_alloc_cleanup(void* arg)
816 {
817 	struct libworker* w = (struct libworker*)arg;
818 	slabhash_clear(&w->env->rrset_cache->table);
819         slabhash_clear(w->env->msg_cache);
820 }
821 
822 struct outbound_entry* libworker_send_query(uint8_t* qname, size_t qnamelen,
823         uint16_t qtype, uint16_t qclass, uint16_t flags, int dnssec,
824 	int want_dnssec, int nocaps, struct sockaddr_storage* addr,
825 	socklen_t addrlen, uint8_t* zone, size_t zonelen,
826 	struct module_qstate* q)
827 {
828 	struct libworker* w = (struct libworker*)q->env->worker;
829 	struct outbound_entry* e = (struct outbound_entry*)regional_alloc(
830 		q->region, sizeof(*e));
831 	if(!e)
832 		return NULL;
833 	e->qstate = q;
834 	e->qsent = outnet_serviced_query(w->back, qname,
835 		qnamelen, qtype, qclass, flags, dnssec, want_dnssec, nocaps,
836 		q->env->cfg->tcp_upstream, q->env->cfg->ssl_upstream, addr,
837 		addrlen, zone, zonelen, libworker_handle_service_reply, e,
838 		w->back->udp_buff);
839 	if(!e->qsent) {
840 		return NULL;
841 	}
842 	return e;
843 }
844 
845 int
846 libworker_handle_reply(struct comm_point* c, void* arg, int error,
847         struct comm_reply* reply_info)
848 {
849 	struct module_qstate* q = (struct module_qstate*)arg;
850 	struct libworker* lw = (struct libworker*)q->env->worker;
851 	struct outbound_entry e;
852 	e.qstate = q;
853 	e.qsent = NULL;
854 
855 	if(error != 0) {
856 		mesh_report_reply(lw->env->mesh, &e, reply_info, error);
857 		return 0;
858 	}
859 	/* sanity check. */
860 	if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
861 		|| LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) !=
862 			LDNS_PACKET_QUERY
863 		|| LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
864 		/* error becomes timeout for the module as if this reply
865 		 * never arrived. */
866 		mesh_report_reply(lw->env->mesh, &e, reply_info,
867 			NETEVENT_TIMEOUT);
868 		return 0;
869 	}
870 	mesh_report_reply(lw->env->mesh, &e, reply_info, NETEVENT_NOERROR);
871 	return 0;
872 }
873 
874 int
875 libworker_handle_service_reply(struct comm_point* c, void* arg, int error,
876         struct comm_reply* reply_info)
877 {
878 	struct outbound_entry* e = (struct outbound_entry*)arg;
879 	struct libworker* lw = (struct libworker*)e->qstate->env->worker;
880 
881 	if(error != 0) {
882 		mesh_report_reply(lw->env->mesh, e, reply_info, error);
883 		return 0;
884 	}
885 	/* sanity check. */
886 	if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
887 		|| LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) !=
888 			LDNS_PACKET_QUERY
889 		|| LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
890 		/* error becomes timeout for the module as if this reply
891 		 * never arrived. */
892 		mesh_report_reply(lw->env->mesh, e, reply_info,
893 			NETEVENT_TIMEOUT);
894 		return 0;
895 	}
896 	mesh_report_reply(lw->env->mesh,  e, reply_info, NETEVENT_NOERROR);
897 	return 0;
898 }
899 
900 /* --- fake callbacks for fptr_wlist to work --- */
901 void worker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
902 	uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
903 	int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
904 {
905 	log_assert(0);
906 }
907 
908 int worker_handle_request(struct comm_point* ATTR_UNUSED(c),
909 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
910         struct comm_reply* ATTR_UNUSED(repinfo))
911 {
912 	log_assert(0);
913 	return 0;
914 }
915 
916 int worker_handle_reply(struct comm_point* ATTR_UNUSED(c),
917 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
918         struct comm_reply* ATTR_UNUSED(reply_info))
919 {
920 	log_assert(0);
921 	return 0;
922 }
923 
924 int worker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
925 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
926         struct comm_reply* ATTR_UNUSED(reply_info))
927 {
928 	log_assert(0);
929 	return 0;
930 }
931 
932 int remote_accept_callback(struct comm_point* ATTR_UNUSED(c),
933 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
934         struct comm_reply* ATTR_UNUSED(repinfo))
935 {
936 	log_assert(0);
937 	return 0;
938 }
939 
940 int remote_control_callback(struct comm_point* ATTR_UNUSED(c),
941 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
942         struct comm_reply* ATTR_UNUSED(repinfo))
943 {
944 	log_assert(0);
945 	return 0;
946 }
947 
948 void worker_sighandler(int ATTR_UNUSED(sig), void* ATTR_UNUSED(arg))
949 {
950 	log_assert(0);
951 }
952 
953 struct outbound_entry* worker_send_query(uint8_t* ATTR_UNUSED(qname),
954 	size_t ATTR_UNUSED(qnamelen), uint16_t ATTR_UNUSED(qtype),
955 	uint16_t ATTR_UNUSED(qclass), uint16_t ATTR_UNUSED(flags),
956 	int ATTR_UNUSED(dnssec), int ATTR_UNUSED(want_dnssec),
957 	int ATTR_UNUSED(nocaps), struct sockaddr_storage* ATTR_UNUSED(addr),
958 	socklen_t ATTR_UNUSED(addrlen), uint8_t* ATTR_UNUSED(zone),
959 	size_t ATTR_UNUSED(zonelen), struct module_qstate* ATTR_UNUSED(q))
960 {
961 	log_assert(0);
962 	return 0;
963 }
964 
965 void
966 worker_alloc_cleanup(void* ATTR_UNUSED(arg))
967 {
968 	log_assert(0);
969 }
970 
971 void worker_stat_timer_cb(void* ATTR_UNUSED(arg))
972 {
973 	log_assert(0);
974 }
975 
976 void worker_probe_timer_cb(void* ATTR_UNUSED(arg))
977 {
978 	log_assert(0);
979 }
980 
981 void worker_start_accept(void* ATTR_UNUSED(arg))
982 {
983 	log_assert(0);
984 }
985 
986 void worker_stop_accept(void* ATTR_UNUSED(arg))
987 {
988 	log_assert(0);
989 }
990 
991 int order_lock_cmp(const void* ATTR_UNUSED(e1), const void* ATTR_UNUSED(e2))
992 {
993 	log_assert(0);
994 	return 0;
995 }
996 
997 int
998 codeline_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
999 {
1000 	log_assert(0);
1001 	return 0;
1002 }
1003 
1004 int replay_var_compare(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1005 {
1006         log_assert(0);
1007         return 0;
1008 }
1009 
1010 void remote_get_opt_ssl(char* ATTR_UNUSED(str), void* ATTR_UNUSED(arg))
1011 {
1012         log_assert(0);
1013 }
1014 
1015 #ifdef UB_ON_WINDOWS
1016 void
1017 worker_win_stop_cb(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev), void*
1018         ATTR_UNUSED(arg)) {
1019         log_assert(0);
1020 }
1021 
1022 void
1023 wsvc_cron_cb(void* ATTR_UNUSED(arg))
1024 {
1025         log_assert(0);
1026 }
1027 #endif /* UB_ON_WINDOWS */
1028