xref: /freebsd/contrib/unbound/daemon/worker.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*
2  * daemon/worker.c - worker that handles a pending list of requests.
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 implements the worker that handles callbacks on events, for
40  * pending requests.
41  */
42 #include "config.h"
43 #include "util/log.h"
44 #include "util/net_help.h"
45 #include "util/random.h"
46 #include "daemon/worker.h"
47 #include "daemon/daemon.h"
48 #include "daemon/remote.h"
49 #include "daemon/acl_list.h"
50 #include "util/netevent.h"
51 #include "util/config_file.h"
52 #include "util/module.h"
53 #include "util/regional.h"
54 #include "util/storage/slabhash.h"
55 #include "services/listen_dnsport.h"
56 #include "services/outside_network.h"
57 #include "services/outbound_list.h"
58 #include "services/cache/rrset.h"
59 #include "services/cache/infra.h"
60 #include "services/cache/dns.h"
61 #include "services/mesh.h"
62 #include "services/localzone.h"
63 #include "util/data/msgparse.h"
64 #include "util/data/msgencode.h"
65 #include "util/data/dname.h"
66 #include "util/fptr_wlist.h"
67 #include "util/tube.h"
68 #include "iterator/iter_fwd.h"
69 #include "iterator/iter_hints.h"
70 #include "validator/autotrust.h"
71 #include "validator/val_anchor.h"
72 #include "libunbound/context.h"
73 #include "libunbound/libworker.h"
74 #include "sldns/sbuffer.h"
75 
76 #ifdef HAVE_SYS_TYPES_H
77 #  include <sys/types.h>
78 #endif
79 #ifdef HAVE_NETDB_H
80 #include <netdb.h>
81 #endif
82 #include <signal.h>
83 #ifdef UB_ON_WINDOWS
84 #include "winrc/win_svc.h"
85 #endif
86 
87 /** Size of an UDP datagram */
88 #define NORMAL_UDP_SIZE	512 /* bytes */
89 /** ratelimit for error responses */
90 #define ERROR_RATELIMIT 100 /* qps */
91 
92 /**
93  * seconds to add to prefetch leeway.  This is a TTL that expires old rrsets
94  * earlier than they should in order to put the new update into the cache.
95  * This additional value is to make sure that if not all TTLs are equal in
96  * the message to be updated(and replaced), that rrsets with up to this much
97  * extra TTL are also replaced.  This means that the resulting new message
98  * will have (most likely) this TTL at least, avoiding very small 'split
99  * second' TTLs due to operators choosing relative primes for TTLs (or so).
100  * Also has to be at least one to break ties (and overwrite cached entry).
101  */
102 #define PREFETCH_EXPIRY_ADD 60
103 
104 #ifdef UNBOUND_ALLOC_STATS
105 /** measure memory leakage */
106 static void
107 debug_memleak(size_t accounted, size_t heap,
108 	size_t total_alloc, size_t total_free)
109 {
110 	static int init = 0;
111 	static size_t base_heap, base_accounted, base_alloc, base_free;
112 	size_t base_af, cur_af, grow_af, grow_acc;
113 	if(!init) {
114 		init = 1;
115 		base_heap = heap;
116 		base_accounted = accounted;
117 		base_alloc = total_alloc;
118 		base_free = total_free;
119 	}
120 	base_af = base_alloc - base_free;
121 	cur_af = total_alloc - total_free;
122 	grow_af = cur_af - base_af;
123 	grow_acc = accounted - base_accounted;
124 	log_info("Leakage: %d leaked. growth: %u use, %u acc, %u heap",
125 		(int)(grow_af - grow_acc), (unsigned)grow_af,
126 		(unsigned)grow_acc, (unsigned)(heap - base_heap));
127 }
128 
129 /** give debug heap size indication */
130 static void
131 debug_total_mem(size_t calctotal)
132 {
133 #ifdef HAVE_SBRK
134 	extern void* unbound_start_brk;
135 	extern size_t unbound_mem_alloc, unbound_mem_freed;
136 	void* cur = sbrk(0);
137 	int total = cur-unbound_start_brk;
138 	log_info("Total heap memory estimate: %u  total-alloc: %u  "
139 		"total-free: %u", (unsigned)total,
140 		(unsigned)unbound_mem_alloc, (unsigned)unbound_mem_freed);
141 	debug_memleak(calctotal, (size_t)total,
142 		unbound_mem_alloc, unbound_mem_freed);
143 #else
144 	(void)calctotal;
145 #endif /* HAVE_SBRK */
146 }
147 #endif /* UNBOUND_ALLOC_STATS */
148 
149 /** Report on memory usage by this thread and global */
150 static void
151 worker_mem_report(struct worker* ATTR_UNUSED(worker),
152 	struct serviced_query* ATTR_UNUSED(cur_serv))
153 {
154 #ifdef UNBOUND_ALLOC_STATS
155 	/* debug func in validator module */
156 	size_t total, front, back, mesh, msg, rrset, infra, ac, superac;
157 	size_t me, iter, val, anch;
158 	int i;
159 	if(verbosity < VERB_ALGO)
160 		return;
161 	front = listen_get_mem(worker->front);
162 	back = outnet_get_mem(worker->back);
163 	msg = slabhash_get_mem(worker->env.msg_cache);
164 	rrset = slabhash_get_mem(&worker->env.rrset_cache->table);
165 	infra = infra_get_mem(worker->env.infra_cache);
166 	mesh = mesh_get_mem(worker->env.mesh);
167 	ac = alloc_get_mem(&worker->alloc);
168 	superac = alloc_get_mem(&worker->daemon->superalloc);
169 	anch = anchors_get_mem(worker->env.anchors);
170 	iter = 0;
171 	val = 0;
172 	for(i=0; i<worker->env.mesh->mods.num; i++) {
173 		fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
174 			mods.mod[i]->get_mem));
175 		if(strcmp(worker->env.mesh->mods.mod[i]->name, "validator")==0)
176 			val += (*worker->env.mesh->mods.mod[i]->get_mem)
177 				(&worker->env, i);
178 		else	iter += (*worker->env.mesh->mods.mod[i]->get_mem)
179 				(&worker->env, i);
180 	}
181 	me = sizeof(*worker) + sizeof(*worker->base) + sizeof(*worker->comsig)
182 		+ comm_point_get_mem(worker->cmd_com)
183 		+ sizeof(worker->rndstate)
184 		+ regional_get_mem(worker->scratchpad)
185 		+ sizeof(*worker->env.scratch_buffer)
186 		+ sldns_buffer_capacity(worker->env.scratch_buffer)
187 		+ forwards_get_mem(worker->env.fwds)
188 		+ hints_get_mem(worker->env.hints);
189 	if(worker->thread_num == 0)
190 		me += acl_list_get_mem(worker->daemon->acl);
191 	if(cur_serv) {
192 		me += serviced_get_mem(cur_serv);
193 	}
194 	total = front+back+mesh+msg+rrset+infra+iter+val+ac+superac+me;
195 	log_info("Memory conditions: %u front=%u back=%u mesh=%u msg=%u "
196 		"rrset=%u infra=%u iter=%u val=%u anchors=%u "
197 		"alloccache=%u globalalloccache=%u me=%u",
198 		(unsigned)total, (unsigned)front, (unsigned)back,
199 		(unsigned)mesh, (unsigned)msg, (unsigned)rrset,
200 		(unsigned)infra, (unsigned)iter, (unsigned)val, (unsigned)anch,
201 		(unsigned)ac, (unsigned)superac, (unsigned)me);
202 	debug_total_mem(total);
203 #else /* no UNBOUND_ALLOC_STATS */
204 	size_t val = 0;
205 	int i;
206 	if(verbosity < VERB_QUERY)
207 		return;
208 	for(i=0; i<worker->env.mesh->mods.num; i++) {
209 		fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
210 			mods.mod[i]->get_mem));
211 		if(strcmp(worker->env.mesh->mods.mod[i]->name, "validator")==0)
212 			val += (*worker->env.mesh->mods.mod[i]->get_mem)
213 				(&worker->env, i);
214 	}
215 	verbose(VERB_QUERY, "cache memory msg=%u rrset=%u infra=%u val=%u",
216 		(unsigned)slabhash_get_mem(worker->env.msg_cache),
217 		(unsigned)slabhash_get_mem(&worker->env.rrset_cache->table),
218 		(unsigned)infra_get_mem(worker->env.infra_cache),
219 		(unsigned)val);
220 #endif /* UNBOUND_ALLOC_STATS */
221 }
222 
223 void
224 worker_send_cmd(struct worker* worker, enum worker_commands cmd)
225 {
226 	uint32_t c = (uint32_t)htonl(cmd);
227 	if(!tube_write_msg(worker->cmd, (uint8_t*)&c, sizeof(c), 0)) {
228 		log_err("worker send cmd %d failed", (int)cmd);
229 	}
230 }
231 
232 int
233 worker_handle_reply(struct comm_point* c, void* arg, int error,
234 	struct comm_reply* reply_info)
235 {
236 	struct module_qstate* q = (struct module_qstate*)arg;
237 	struct worker* worker = q->env->worker;
238 	struct outbound_entry e;
239 	e.qstate = q;
240 	e.qsent = NULL;
241 
242 	if(error != 0) {
243 		mesh_report_reply(worker->env.mesh, &e, reply_info, error);
244 		worker_mem_report(worker, NULL);
245 		return 0;
246 	}
247 	/* sanity check. */
248 	if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
249 		|| LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) !=
250 			LDNS_PACKET_QUERY
251 		|| LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
252 		/* error becomes timeout for the module as if this reply
253 		 * never arrived. */
254 		mesh_report_reply(worker->env.mesh, &e, reply_info,
255 			NETEVENT_TIMEOUT);
256 		worker_mem_report(worker, NULL);
257 		return 0;
258 	}
259 	mesh_report_reply(worker->env.mesh, &e, reply_info, NETEVENT_NOERROR);
260 	worker_mem_report(worker, NULL);
261 	return 0;
262 }
263 
264 int
265 worker_handle_service_reply(struct comm_point* c, void* arg, int error,
266 	struct comm_reply* reply_info)
267 {
268 	struct outbound_entry* e = (struct outbound_entry*)arg;
269 	struct worker* worker = e->qstate->env->worker;
270 	struct serviced_query *sq = e->qsent;
271 
272 	verbose(VERB_ALGO, "worker svcd callback for qstate %p", e->qstate);
273 	if(error != 0) {
274 		mesh_report_reply(worker->env.mesh, e, reply_info, error);
275 		worker_mem_report(worker, sq);
276 		return 0;
277 	}
278 	/* sanity check. */
279 	if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
280 		|| LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) !=
281 			LDNS_PACKET_QUERY
282 		|| LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
283 		/* error becomes timeout for the module as if this reply
284 		 * never arrived. */
285 		verbose(VERB_ALGO, "worker: bad reply handled as timeout");
286 		mesh_report_reply(worker->env.mesh, e, reply_info,
287 			NETEVENT_TIMEOUT);
288 		worker_mem_report(worker, sq);
289 		return 0;
290 	}
291 	mesh_report_reply(worker->env.mesh, e, reply_info, NETEVENT_NOERROR);
292 	worker_mem_report(worker, sq);
293 	return 0;
294 }
295 
296 /** ratelimit error replies
297  * @param worker: the worker struct with ratelimit counter
298  * @param err: error code that would be wanted.
299  * @return value of err if okay, or -1 if it should be discarded instead.
300  */
301 static int
302 worker_err_ratelimit(struct worker* worker, int err)
303 {
304 	if(worker->err_limit_time == *worker->env.now) {
305 		/* see if limit is exceeded for this second */
306 		if(worker->err_limit_count++ > ERROR_RATELIMIT)
307 			return -1;
308 	} else {
309 		/* new second, new limits */
310 		worker->err_limit_time = *worker->env.now;
311 		worker->err_limit_count = 1;
312 	}
313 	return err;
314 }
315 
316 /** check request sanity.
317  * @param pkt: the wire packet to examine for sanity.
318  * @param worker: parameters for checking.
319  * @return error code, 0 OK, or -1 discard.
320 */
321 static int
322 worker_check_request(sldns_buffer* pkt, struct worker* worker)
323 {
324 	if(sldns_buffer_limit(pkt) < LDNS_HEADER_SIZE) {
325 		verbose(VERB_QUERY, "request too short, discarded");
326 		return -1;
327 	}
328 	if(sldns_buffer_limit(pkt) > NORMAL_UDP_SIZE &&
329 		worker->daemon->cfg->harden_large_queries) {
330 		verbose(VERB_QUERY, "request too large, discarded");
331 		return -1;
332 	}
333 	if(LDNS_QR_WIRE(sldns_buffer_begin(pkt))) {
334 		verbose(VERB_QUERY, "request has QR bit on, discarded");
335 		return -1;
336 	}
337 	if(LDNS_TC_WIRE(sldns_buffer_begin(pkt))) {
338 		LDNS_TC_CLR(sldns_buffer_begin(pkt));
339 		verbose(VERB_QUERY, "request bad, has TC bit on");
340 		return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
341 	}
342 	if(LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)) != LDNS_PACKET_QUERY) {
343 		verbose(VERB_QUERY, "request unknown opcode %d",
344 			LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)));
345 		return worker_err_ratelimit(worker, LDNS_RCODE_NOTIMPL);
346 	}
347 	if(LDNS_QDCOUNT(sldns_buffer_begin(pkt)) != 1) {
348 		verbose(VERB_QUERY, "request wrong nr qd=%d",
349 			LDNS_QDCOUNT(sldns_buffer_begin(pkt)));
350 		return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
351 	}
352 	if(LDNS_ANCOUNT(sldns_buffer_begin(pkt)) != 0) {
353 		verbose(VERB_QUERY, "request wrong nr an=%d",
354 			LDNS_ANCOUNT(sldns_buffer_begin(pkt)));
355 		return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
356 	}
357 	if(LDNS_NSCOUNT(sldns_buffer_begin(pkt)) != 0) {
358 		verbose(VERB_QUERY, "request wrong nr ns=%d",
359 			LDNS_NSCOUNT(sldns_buffer_begin(pkt)));
360 		return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
361 	}
362 	if(LDNS_ARCOUNT(sldns_buffer_begin(pkt)) > 1) {
363 		verbose(VERB_QUERY, "request wrong nr ar=%d",
364 			LDNS_ARCOUNT(sldns_buffer_begin(pkt)));
365 		return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
366 	}
367 	return 0;
368 }
369 
370 void
371 worker_handle_control_cmd(struct tube* ATTR_UNUSED(tube), uint8_t* msg,
372 	size_t len, int error, void* arg)
373 {
374 	struct worker* worker = (struct worker*)arg;
375 	enum worker_commands cmd;
376 	if(error != NETEVENT_NOERROR) {
377 		free(msg);
378 		if(error == NETEVENT_CLOSED)
379 			comm_base_exit(worker->base);
380 		else	log_info("control event: %d", error);
381 		return;
382 	}
383 	if(len != sizeof(uint32_t)) {
384 		fatal_exit("bad control msg length %d", (int)len);
385 	}
386 	cmd = sldns_read_uint32(msg);
387 	free(msg);
388 	switch(cmd) {
389 	case worker_cmd_quit:
390 		verbose(VERB_ALGO, "got control cmd quit");
391 		comm_base_exit(worker->base);
392 		break;
393 	case worker_cmd_stats:
394 		verbose(VERB_ALGO, "got control cmd stats");
395 		server_stats_reply(worker, 1);
396 		break;
397 	case worker_cmd_stats_noreset:
398 		verbose(VERB_ALGO, "got control cmd stats_noreset");
399 		server_stats_reply(worker, 0);
400 		break;
401 	case worker_cmd_remote:
402 		verbose(VERB_ALGO, "got control cmd remote");
403 		daemon_remote_exec(worker);
404 		break;
405 	default:
406 		log_err("bad command %d", (int)cmd);
407 		break;
408 	}
409 }
410 
411 /** check if a delegation is secure */
412 static enum sec_status
413 check_delegation_secure(struct reply_info *rep)
414 {
415 	/* return smallest security status */
416 	size_t i;
417 	enum sec_status sec = sec_status_secure;
418 	enum sec_status s;
419 	size_t num = rep->an_numrrsets + rep->ns_numrrsets;
420 	/* check if answer and authority are OK */
421 	for(i=0; i<num; i++) {
422 		s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
423 			->security;
424 		if(s < sec)
425 			sec = s;
426 	}
427 	/* in additional, only unchecked triggers revalidation */
428 	for(i=num; i<rep->rrset_count; i++) {
429 		s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
430 			->security;
431 		if(s == sec_status_unchecked)
432 			return s;
433 	}
434 	return sec;
435 }
436 
437 /** remove nonsecure from a delegation referral additional section */
438 static void
439 deleg_remove_nonsecure_additional(struct reply_info* rep)
440 {
441 	/* we can simply edit it, since we are working in the scratch region */
442 	size_t i;
443 	enum sec_status s;
444 
445 	for(i = rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
446 		s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
447 			->security;
448 		if(s != sec_status_secure) {
449 			memmove(rep->rrsets+i, rep->rrsets+i+1,
450 				sizeof(struct ub_packed_rrset_key*)*
451 				(rep->rrset_count - i - 1));
452 			rep->ar_numrrsets--;
453 			rep->rrset_count--;
454 			i--;
455 		}
456 	}
457 }
458 
459 /** answer nonrecursive query from the cache */
460 static int
461 answer_norec_from_cache(struct worker* worker, struct query_info* qinfo,
462 	uint16_t id, uint16_t flags, struct comm_reply* repinfo,
463 	struct edns_data* edns)
464 {
465 	/* for a nonrecursive query return either:
466 	 * 	o an error (servfail; we try to avoid this)
467 	 * 	o a delegation (closest we have; this routine tries that)
468 	 * 	o the answer (checked by answer_from_cache)
469 	 *
470 	 * So, grab a delegation from the rrset cache.
471 	 * Then check if it needs validation, if so, this routine fails,
472 	 * so that iterator can prime and validator can verify rrsets.
473 	 */
474 	uint16_t udpsize = edns->udp_size;
475 	int secure = 0;
476 	time_t timenow = *worker->env.now;
477 	int must_validate = (!(flags&BIT_CD) || worker->env.cfg->ignore_cd)
478 		&& worker->env.need_to_validate;
479 	struct dns_msg *msg = NULL;
480 	struct delegpt *dp;
481 
482 	dp = dns_cache_find_delegation(&worker->env, qinfo->qname,
483 		qinfo->qname_len, qinfo->qtype, qinfo->qclass,
484 		worker->scratchpad, &msg, timenow);
485 	if(!dp) { /* no delegation, need to reprime */
486 		regional_free_all(worker->scratchpad);
487 		return 0;
488 	}
489 	if(must_validate) {
490 		switch(check_delegation_secure(msg->rep)) {
491 		case sec_status_unchecked:
492 			/* some rrsets have not been verified yet, go and
493 			 * let validator do that */
494 			regional_free_all(worker->scratchpad);
495 			return 0;
496 		case sec_status_bogus:
497 			/* some rrsets are bogus, reply servfail */
498 			edns->edns_version = EDNS_ADVERTISED_VERSION;
499 			edns->udp_size = EDNS_ADVERTISED_SIZE;
500 			edns->ext_rcode = 0;
501 			edns->bits &= EDNS_DO;
502 			error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
503 				&msg->qinfo, id, flags, edns);
504 			regional_free_all(worker->scratchpad);
505 			if(worker->stats.extended) {
506 				worker->stats.ans_bogus++;
507 				worker->stats.ans_rcode[LDNS_RCODE_SERVFAIL]++;
508 			}
509 			return 1;
510 		case sec_status_secure:
511 			/* all rrsets are secure */
512 			/* remove non-secure rrsets from the add. section*/
513 			if(worker->env.cfg->val_clean_additional)
514 				deleg_remove_nonsecure_additional(msg->rep);
515 			secure = 1;
516 			break;
517 		case sec_status_indeterminate:
518 		case sec_status_insecure:
519 		default:
520 			/* not secure */
521 			secure = 0;
522 			break;
523 		}
524 	}
525 	/* return this delegation from the cache */
526 	edns->edns_version = EDNS_ADVERTISED_VERSION;
527 	edns->udp_size = EDNS_ADVERTISED_SIZE;
528 	edns->ext_rcode = 0;
529 	edns->bits &= EDNS_DO;
530 	msg->rep->flags |= BIT_QR|BIT_RA;
531 	if(!reply_info_answer_encode(&msg->qinfo, msg->rep, id, flags,
532 		repinfo->c->buffer, 0, 1, worker->scratchpad,
533 		udpsize, edns, (int)(edns->bits & EDNS_DO), secure)) {
534 		error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
535 			&msg->qinfo, id, flags, edns);
536 	}
537 	regional_free_all(worker->scratchpad);
538 	if(worker->stats.extended) {
539 		if(secure) worker->stats.ans_secure++;
540 		server_stats_insrcode(&worker->stats, repinfo->c->buffer);
541 	}
542 	return 1;
543 }
544 
545 /** answer query from the cache */
546 static int
547 answer_from_cache(struct worker* worker, struct query_info* qinfo,
548 	struct reply_info* rep, uint16_t id, uint16_t flags,
549 	struct comm_reply* repinfo, struct edns_data* edns)
550 {
551 	time_t timenow = *worker->env.now;
552 	uint16_t udpsize = edns->udp_size;
553 	int secure;
554 	int must_validate = (!(flags&BIT_CD) || worker->env.cfg->ignore_cd)
555 		&& worker->env.need_to_validate;
556 	/* see if it is possible */
557 	if(rep->ttl < timenow) {
558 		/* the rrsets may have been updated in the meantime.
559 		 * we will refetch the message format from the
560 		 * authoritative server
561 		 */
562 		return 0;
563 	}
564 	if(!rrset_array_lock(rep->ref, rep->rrset_count, timenow))
565 		return 0;
566 	/* locked and ids and ttls are OK. */
567 	/* check CNAME chain (if any) */
568 	if(rep->an_numrrsets > 0 && (rep->rrsets[0]->rk.type ==
569 		htons(LDNS_RR_TYPE_CNAME) || rep->rrsets[0]->rk.type ==
570 		htons(LDNS_RR_TYPE_DNAME))) {
571 		if(!reply_check_cname_chain(qinfo, rep)) {
572 			/* cname chain invalid, redo iterator steps */
573 			verbose(VERB_ALGO, "Cache reply: cname chain broken");
574 		bail_out:
575 			rrset_array_unlock_touch(worker->env.rrset_cache,
576 				worker->scratchpad, rep->ref, rep->rrset_count);
577 			regional_free_all(worker->scratchpad);
578 			return 0;
579 		}
580 	}
581 	/* check security status of the cached answer */
582 	if( rep->security == sec_status_bogus && must_validate) {
583 		/* BAD cached */
584 		edns->edns_version = EDNS_ADVERTISED_VERSION;
585 		edns->udp_size = EDNS_ADVERTISED_SIZE;
586 		edns->ext_rcode = 0;
587 		edns->bits &= EDNS_DO;
588 		error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
589 			qinfo, id, flags, edns);
590 		rrset_array_unlock_touch(worker->env.rrset_cache,
591 			worker->scratchpad, rep->ref, rep->rrset_count);
592 		regional_free_all(worker->scratchpad);
593 		if(worker->stats.extended) {
594 			worker->stats.ans_bogus ++;
595 			worker->stats.ans_rcode[LDNS_RCODE_SERVFAIL] ++;
596 		}
597 		return 1;
598 	} else if( rep->security == sec_status_unchecked && must_validate) {
599 		verbose(VERB_ALGO, "Cache reply: unchecked entry needs "
600 			"validation");
601 		goto bail_out; /* need to validate cache entry first */
602 	} else if(rep->security == sec_status_secure) {
603 		if(reply_all_rrsets_secure(rep))
604 			secure = 1;
605 		else	{
606 			if(must_validate) {
607 				verbose(VERB_ALGO, "Cache reply: secure entry"
608 					" changed status");
609 				goto bail_out; /* rrset changed, re-verify */
610 			}
611 			secure = 0;
612 		}
613 	} else	secure = 0;
614 
615 	edns->edns_version = EDNS_ADVERTISED_VERSION;
616 	edns->udp_size = EDNS_ADVERTISED_SIZE;
617 	edns->ext_rcode = 0;
618 	edns->bits &= EDNS_DO;
619 	if(!reply_info_answer_encode(qinfo, rep, id, flags,
620 		repinfo->c->buffer, timenow, 1, worker->scratchpad,
621 		udpsize, edns, (int)(edns->bits & EDNS_DO), secure)) {
622 		error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
623 			qinfo, id, flags, edns);
624 	}
625 	/* cannot send the reply right now, because blocking network syscall
626 	 * is bad while holding locks. */
627 	rrset_array_unlock_touch(worker->env.rrset_cache, worker->scratchpad,
628 		rep->ref, rep->rrset_count);
629 	regional_free_all(worker->scratchpad);
630 	if(worker->stats.extended) {
631 		if(secure) worker->stats.ans_secure++;
632 		server_stats_insrcode(&worker->stats, repinfo->c->buffer);
633 	}
634 	/* go and return this buffer to the client */
635 	return 1;
636 }
637 
638 /** Reply to client and perform prefetch to keep cache up to date */
639 static void
640 reply_and_prefetch(struct worker* worker, struct query_info* qinfo,
641 	uint16_t flags, struct comm_reply* repinfo, time_t leeway)
642 {
643 	/* first send answer to client to keep its latency
644 	 * as small as a cachereply */
645 	comm_point_send_reply(repinfo);
646 	server_stats_prefetch(&worker->stats, worker);
647 
648 	/* create the prefetch in the mesh as a normal lookup without
649 	 * client addrs waiting, which has the cache blacklisted (to bypass
650 	 * the cache and go to the network for the data). */
651 	/* this (potentially) runs the mesh for the new query */
652 	mesh_new_prefetch(worker->env.mesh, qinfo, flags, leeway +
653 		PREFETCH_EXPIRY_ADD);
654 }
655 
656 /**
657  * Fill CH class answer into buffer. Keeps query.
658  * @param pkt: buffer
659  * @param str: string to put into text record (<255).
660  * @param edns: edns reply information.
661  */
662 static void
663 chaos_replystr(sldns_buffer* pkt, const char* str, struct edns_data* edns)
664 {
665 	size_t len = strlen(str);
666 	unsigned int rd = LDNS_RD_WIRE(sldns_buffer_begin(pkt));
667 	unsigned int cd = LDNS_CD_WIRE(sldns_buffer_begin(pkt));
668 	if(len>255) len=255; /* cap size of TXT record */
669 	sldns_buffer_clear(pkt);
670 	sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip id */
671 	sldns_buffer_write_u16(pkt, (uint16_t)(BIT_QR|BIT_RA));
672 	if(rd) LDNS_RD_SET(sldns_buffer_begin(pkt));
673 	if(cd) LDNS_CD_SET(sldns_buffer_begin(pkt));
674 	sldns_buffer_write_u16(pkt, 1); /* qdcount */
675 	sldns_buffer_write_u16(pkt, 1); /* ancount */
676 	sldns_buffer_write_u16(pkt, 0); /* nscount */
677 	sldns_buffer_write_u16(pkt, 0); /* arcount */
678 	(void)query_dname_len(pkt); /* skip qname */
679 	sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qtype */
680 	sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qclass */
681 	sldns_buffer_write_u16(pkt, 0xc00c); /* compr ptr to query */
682 	sldns_buffer_write_u16(pkt, LDNS_RR_TYPE_TXT);
683 	sldns_buffer_write_u16(pkt, LDNS_RR_CLASS_CH);
684 	sldns_buffer_write_u32(pkt, 0); /* TTL */
685 	sldns_buffer_write_u16(pkt, sizeof(uint8_t) + len);
686 	sldns_buffer_write_u8(pkt, len);
687 	sldns_buffer_write(pkt, str, len);
688 	sldns_buffer_flip(pkt);
689 	edns->edns_version = EDNS_ADVERTISED_VERSION;
690 	edns->udp_size = EDNS_ADVERTISED_SIZE;
691 	edns->bits &= EDNS_DO;
692 	attach_edns_record(pkt, edns);
693 }
694 
695 /**
696  * Answer CH class queries.
697  * @param w: worker
698  * @param qinfo: query info. Pointer into packet buffer.
699  * @param edns: edns info from query.
700  * @param pkt: packet buffer.
701  * @return: true if a reply is to be sent.
702  */
703 static int
704 answer_chaos(struct worker* w, struct query_info* qinfo,
705 	struct edns_data* edns, sldns_buffer* pkt)
706 {
707 	struct config_file* cfg = w->env.cfg;
708 	if(qinfo->qtype != LDNS_RR_TYPE_ANY && qinfo->qtype != LDNS_RR_TYPE_TXT)
709 		return 0;
710 	if(query_dname_compare(qinfo->qname,
711 		(uint8_t*)"\002id\006server") == 0 ||
712 		query_dname_compare(qinfo->qname,
713 		(uint8_t*)"\010hostname\004bind") == 0)
714 	{
715 		if(cfg->hide_identity)
716 			return 0;
717 		if(cfg->identity==NULL || cfg->identity[0]==0) {
718 			char buf[MAXHOSTNAMELEN+1];
719 			if (gethostname(buf, MAXHOSTNAMELEN) == 0) {
720 				buf[MAXHOSTNAMELEN] = 0;
721 				chaos_replystr(pkt, buf, edns);
722 			} else 	{
723 				log_err("gethostname: %s", strerror(errno));
724 				chaos_replystr(pkt, "no hostname", edns);
725 			}
726 		}
727 		else 	chaos_replystr(pkt, cfg->identity, edns);
728 		return 1;
729 	}
730 	if(query_dname_compare(qinfo->qname,
731 		(uint8_t*)"\007version\006server") == 0 ||
732 		query_dname_compare(qinfo->qname,
733 		(uint8_t*)"\007version\004bind") == 0)
734 	{
735 		if(cfg->hide_version)
736 			return 0;
737 		if(cfg->version==NULL || cfg->version[0]==0)
738 			chaos_replystr(pkt, PACKAGE_STRING, edns);
739 		else 	chaos_replystr(pkt, cfg->version, edns);
740 		return 1;
741 	}
742 	return 0;
743 }
744 
745 static int
746 deny_refuse(struct comm_point* c, enum acl_access acl,
747 	enum acl_access deny, enum acl_access refuse,
748 	struct worker* worker, struct comm_reply* repinfo)
749 {
750 	if(acl == deny) {
751 		comm_point_drop_reply(repinfo);
752 		if(worker->stats.extended)
753 			worker->stats.unwanted_queries++;
754 		return 0;
755 	} else if(acl == refuse) {
756 		log_addr(VERB_ALGO, "refused query from",
757 			&repinfo->addr, repinfo->addrlen);
758 		log_buf(VERB_ALGO, "refuse", c->buffer);
759 		if(worker->stats.extended)
760 			worker->stats.unwanted_queries++;
761 		if(worker_check_request(c->buffer, worker) == -1) {
762 			comm_point_drop_reply(repinfo);
763 			return 0; /* discard this */
764 		}
765 		sldns_buffer_set_limit(c->buffer, LDNS_HEADER_SIZE);
766 		sldns_buffer_write_at(c->buffer, 4,
767 			(uint8_t*)"\0\0\0\0\0\0\0\0", 8);
768 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
769 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
770 			LDNS_RCODE_REFUSED);
771 		return 1;
772 	}
773 
774 	return -1;
775 }
776 
777 static int
778 deny_refuse_all(struct comm_point* c, enum acl_access acl,
779 	struct worker* worker, struct comm_reply* repinfo)
780 {
781 	return deny_refuse(c, acl, acl_deny, acl_refuse, worker, repinfo);
782 }
783 
784 static int
785 deny_refuse_non_local(struct comm_point* c, enum acl_access acl,
786 	struct worker* worker, struct comm_reply* repinfo)
787 {
788 	return deny_refuse(c, acl, acl_deny_non_local, acl_refuse_non_local, worker, repinfo);
789 }
790 
791 int
792 worker_handle_request(struct comm_point* c, void* arg, int error,
793 	struct comm_reply* repinfo)
794 {
795 	struct worker* worker = (struct worker*)arg;
796 	int ret;
797 	hashvalue_t h;
798 	struct lruhash_entry* e;
799 	struct query_info qinfo;
800 	struct edns_data edns;
801 	enum acl_access acl;
802 	int rc = 0;
803 
804 	if(error != NETEVENT_NOERROR) {
805 		/* some bad tcp query DNS formats give these error calls */
806 		verbose(VERB_ALGO, "handle request called with err=%d", error);
807 		return 0;
808 	}
809 #ifdef USE_DNSTAP
810 	if(worker->dtenv.log_client_query_messages)
811 		dt_msg_send_client_query(&worker->dtenv, &repinfo->addr, c->type,
812 			c->buffer);
813 #endif
814 	acl = acl_list_lookup(worker->daemon->acl, &repinfo->addr,
815 		repinfo->addrlen);
816 	if((ret=deny_refuse_all(c, acl, worker, repinfo)) != -1)
817 	{
818 		if(ret == 1)
819 			goto send_reply;
820 		return ret;
821 	}
822 	if((ret=worker_check_request(c->buffer, worker)) != 0) {
823 		verbose(VERB_ALGO, "worker check request: bad query.");
824 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
825 		if(ret != -1) {
826 			LDNS_QR_SET(sldns_buffer_begin(c->buffer));
827 			LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
828 			return 1;
829 		}
830 		comm_point_drop_reply(repinfo);
831 		return 0;
832 	}
833 	worker->stats.num_queries++;
834 	/* see if query is in the cache */
835 	if(!query_info_parse(&qinfo, c->buffer)) {
836 		verbose(VERB_ALGO, "worker parse request: formerror.");
837 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
838 		if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) {
839 			comm_point_drop_reply(repinfo);
840 			return 0;
841 		}
842 		sldns_buffer_rewind(c->buffer);
843 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
844 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
845 			LDNS_RCODE_FORMERR);
846 		server_stats_insrcode(&worker->stats, c->buffer);
847 		goto send_reply;
848 	}
849 	if(worker->env.cfg->log_queries) {
850 		char ip[128];
851 		addr_to_str(&repinfo->addr, repinfo->addrlen, ip, sizeof(ip));
852 		log_nametypeclass(0, ip, qinfo.qname, qinfo.qtype, qinfo.qclass);
853 	}
854 	if(qinfo.qtype == LDNS_RR_TYPE_AXFR ||
855 		qinfo.qtype == LDNS_RR_TYPE_IXFR) {
856 		verbose(VERB_ALGO, "worker request: refused zone transfer.");
857 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
858 		sldns_buffer_rewind(c->buffer);
859 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
860 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
861 			LDNS_RCODE_REFUSED);
862 		if(worker->stats.extended) {
863 			worker->stats.qtype[qinfo.qtype]++;
864 			server_stats_insrcode(&worker->stats, c->buffer);
865 		}
866 		goto send_reply;
867 	}
868 	if((ret=parse_edns_from_pkt(c->buffer, &edns)) != 0) {
869 		struct edns_data reply_edns;
870 		verbose(VERB_ALGO, "worker parse edns: formerror.");
871 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
872 		memset(&reply_edns, 0, sizeof(reply_edns));
873 		reply_edns.edns_present = 1;
874 		reply_edns.udp_size = EDNS_ADVERTISED_SIZE;
875 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
876 		error_encode(c->buffer, ret, &qinfo,
877 			*(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
878 			sldns_buffer_read_u16_at(c->buffer, 2), &reply_edns);
879 		server_stats_insrcode(&worker->stats, c->buffer);
880 		goto send_reply;
881 	}
882 	if(edns.edns_present && edns.edns_version != 0) {
883 		edns.ext_rcode = (uint8_t)(EDNS_RCODE_BADVERS>>4);
884 		edns.edns_version = EDNS_ADVERTISED_VERSION;
885 		edns.udp_size = EDNS_ADVERTISED_SIZE;
886 		edns.bits &= EDNS_DO;
887 		verbose(VERB_ALGO, "query with bad edns version.");
888 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
889 		error_encode(c->buffer, EDNS_RCODE_BADVERS&0xf, &qinfo,
890 			*(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
891 			sldns_buffer_read_u16_at(c->buffer, 2), NULL);
892 		attach_edns_record(c->buffer, &edns);
893 		goto send_reply;
894 	}
895 	if(edns.edns_present && edns.udp_size < NORMAL_UDP_SIZE &&
896 		worker->daemon->cfg->harden_short_bufsize) {
897 		verbose(VERB_QUERY, "worker request: EDNS bufsize %d ignored",
898 			(int)edns.udp_size);
899 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
900 		edns.udp_size = NORMAL_UDP_SIZE;
901 	}
902 	if(edns.udp_size > worker->daemon->cfg->max_udp_size &&
903 		c->type == comm_udp) {
904 		verbose(VERB_QUERY,
905 			"worker request: max UDP reply size modified"
906 			" (%d to max-udp-size)", (int)edns.udp_size);
907 		log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
908 		edns.udp_size = worker->daemon->cfg->max_udp_size;
909 	}
910 	if(edns.udp_size < LDNS_HEADER_SIZE) {
911 		verbose(VERB_ALGO, "worker request: edns is too small.");
912 		log_addr(VERB_CLIENT, "from", &repinfo->addr, repinfo->addrlen);
913 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
914 		LDNS_TC_SET(sldns_buffer_begin(c->buffer));
915 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
916 			LDNS_RCODE_SERVFAIL);
917 		sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
918 		sldns_buffer_write_at(c->buffer, 4,
919 			(uint8_t*)"\0\0\0\0\0\0\0\0", 8);
920 		sldns_buffer_flip(c->buffer);
921 		goto send_reply;
922 	}
923 	if(worker->stats.extended)
924 		server_stats_insquery(&worker->stats, c, qinfo.qtype,
925 			qinfo.qclass, &edns, repinfo);
926 	if(c->type != comm_udp)
927 		edns.udp_size = 65535; /* max size for TCP replies */
928 	if(qinfo.qclass == LDNS_RR_CLASS_CH && answer_chaos(worker, &qinfo,
929 		&edns, c->buffer)) {
930 		server_stats_insrcode(&worker->stats, c->buffer);
931 		goto send_reply;
932 	}
933 	if(local_zones_answer(worker->daemon->local_zones, &qinfo, &edns,
934 		c->buffer, worker->scratchpad, repinfo)) {
935 		regional_free_all(worker->scratchpad);
936 		if(sldns_buffer_limit(c->buffer) == 0) {
937 			comm_point_drop_reply(repinfo);
938 			return 0;
939 		}
940 		server_stats_insrcode(&worker->stats, c->buffer);
941 		goto send_reply;
942 	}
943 
944 	/* We've looked in our local zones. If the answer isn't there, we
945 	 * might need to bail out based on ACLs now. */
946 	if((ret=deny_refuse_non_local(c, acl, worker, repinfo)) != -1)
947 	{
948 		if(ret == 1)
949 			goto send_reply;
950 		return ret;
951 	}
952 
953 	/* If this request does not have the recursion bit set, verify
954 	 * ACLs allow the snooping. */
955 	if(!(LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) &&
956 		acl != acl_allow_snoop ) {
957 		sldns_buffer_set_limit(c->buffer, LDNS_HEADER_SIZE);
958 		sldns_buffer_write_at(c->buffer, 4,
959 			(uint8_t*)"\0\0\0\0\0\0\0\0", 8);
960 		LDNS_QR_SET(sldns_buffer_begin(c->buffer));
961 		LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
962 			LDNS_RCODE_REFUSED);
963 		sldns_buffer_flip(c->buffer);
964 		server_stats_insrcode(&worker->stats, c->buffer);
965 		log_addr(VERB_ALGO, "refused nonrec (cache snoop) query from",
966 			&repinfo->addr, repinfo->addrlen);
967 		goto send_reply;
968 	}
969 	h = query_info_hash(&qinfo, sldns_buffer_read_u16_at(c->buffer, 2));
970 	if((e=slabhash_lookup(worker->env.msg_cache, h, &qinfo, 0))) {
971 		/* answer from cache - we have acquired a readlock on it */
972 		if(answer_from_cache(worker, &qinfo,
973 			(struct reply_info*)e->data,
974 			*(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
975 			sldns_buffer_read_u16_at(c->buffer, 2), repinfo,
976 			&edns)) {
977 			/* prefetch it if the prefetch TTL expired */
978 			if(worker->env.cfg->prefetch && *worker->env.now >=
979 				((struct reply_info*)e->data)->prefetch_ttl) {
980 				time_t leeway = ((struct reply_info*)e->
981 					data)->ttl - *worker->env.now;
982 				lock_rw_unlock(&e->lock);
983 				reply_and_prefetch(worker, &qinfo,
984 					sldns_buffer_read_u16_at(c->buffer, 2),
985 					repinfo, leeway);
986 				rc = 0;
987 				goto send_reply_rc;
988 			}
989 			lock_rw_unlock(&e->lock);
990 			goto send_reply;
991 		}
992 		verbose(VERB_ALGO, "answer from the cache failed");
993 		lock_rw_unlock(&e->lock);
994 	}
995 	if(!LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) {
996 		if(answer_norec_from_cache(worker, &qinfo,
997 			*(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
998 			sldns_buffer_read_u16_at(c->buffer, 2), repinfo,
999 			&edns)) {
1000 			goto send_reply;
1001 		}
1002 		verbose(VERB_ALGO, "answer norec from cache -- "
1003 			"need to validate or not primed");
1004 	}
1005 	sldns_buffer_rewind(c->buffer);
1006 	server_stats_querymiss(&worker->stats, worker);
1007 
1008 	if(verbosity >= VERB_CLIENT) {
1009 		if(c->type == comm_udp)
1010 			log_addr(VERB_CLIENT, "udp request from",
1011 				&repinfo->addr, repinfo->addrlen);
1012 		else	log_addr(VERB_CLIENT, "tcp request from",
1013 				&repinfo->addr, repinfo->addrlen);
1014 	}
1015 
1016 	/* grab a work request structure for this new request */
1017 	mesh_new_client(worker->env.mesh, &qinfo,
1018 		sldns_buffer_read_u16_at(c->buffer, 2),
1019 		&edns, repinfo, *(uint16_t*)(void *)sldns_buffer_begin(c->buffer));
1020 	worker_mem_report(worker, NULL);
1021 	return 0;
1022 
1023 send_reply:
1024 	rc = 1;
1025 send_reply_rc:
1026 #ifdef USE_DNSTAP
1027 	if(worker->dtenv.log_client_response_messages)
1028 		dt_msg_send_client_response(&worker->dtenv, &repinfo->addr,
1029 			c->type, c->buffer);
1030 #endif
1031 	return rc;
1032 }
1033 
1034 void
1035 worker_sighandler(int sig, void* arg)
1036 {
1037 	/* note that log, print, syscalls here give race conditions.
1038 	 * And cause hangups if the log-lock is held by the application. */
1039 	struct worker* worker = (struct worker*)arg;
1040 	switch(sig) {
1041 #ifdef SIGHUP
1042 		case SIGHUP:
1043 			comm_base_exit(worker->base);
1044 			break;
1045 #endif
1046 		case SIGINT:
1047 			worker->need_to_exit = 1;
1048 			comm_base_exit(worker->base);
1049 			break;
1050 #ifdef SIGQUIT
1051 		case SIGQUIT:
1052 			worker->need_to_exit = 1;
1053 			comm_base_exit(worker->base);
1054 			break;
1055 #endif
1056 		case SIGTERM:
1057 			worker->need_to_exit = 1;
1058 			comm_base_exit(worker->base);
1059 			break;
1060 		default:
1061 			/* unknown signal, ignored */
1062 			break;
1063 	}
1064 }
1065 
1066 /** restart statistics timer for worker, if enabled */
1067 static void
1068 worker_restart_timer(struct worker* worker)
1069 {
1070 	if(worker->env.cfg->stat_interval > 0) {
1071 		struct timeval tv;
1072 #ifndef S_SPLINT_S
1073 		tv.tv_sec = worker->env.cfg->stat_interval;
1074 		tv.tv_usec = 0;
1075 #endif
1076 		comm_timer_set(worker->stat_timer, &tv);
1077 	}
1078 }
1079 
1080 void worker_stat_timer_cb(void* arg)
1081 {
1082 	struct worker* worker = (struct worker*)arg;
1083 	server_stats_log(&worker->stats, worker, worker->thread_num);
1084 	mesh_stats(worker->env.mesh, "mesh has");
1085 	worker_mem_report(worker, NULL);
1086 	if(!worker->daemon->cfg->stat_cumulative) {
1087 		worker_stats_clear(worker);
1088 	}
1089 	/* start next timer */
1090 	worker_restart_timer(worker);
1091 }
1092 
1093 void worker_probe_timer_cb(void* arg)
1094 {
1095 	struct worker* worker = (struct worker*)arg;
1096 	struct timeval tv;
1097 #ifndef S_SPLINT_S
1098 	tv.tv_sec = (time_t)autr_probe_timer(&worker->env);
1099 	tv.tv_usec = 0;
1100 #endif
1101 	if(tv.tv_sec != 0)
1102 		comm_timer_set(worker->env.probe_timer, &tv);
1103 }
1104 
1105 struct worker*
1106 worker_create(struct daemon* daemon, int id, int* ports, int n)
1107 {
1108 	unsigned int seed;
1109 	struct worker* worker = (struct worker*)calloc(1,
1110 		sizeof(struct worker));
1111 	if(!worker)
1112 		return NULL;
1113 	worker->numports = n;
1114 	worker->ports = (int*)memdup(ports, sizeof(int)*n);
1115 	if(!worker->ports) {
1116 		free(worker);
1117 		return NULL;
1118 	}
1119 	worker->daemon = daemon;
1120 	worker->thread_num = id;
1121 	if(!(worker->cmd = tube_create())) {
1122 		free(worker->ports);
1123 		free(worker);
1124 		return NULL;
1125 	}
1126 	/* create random state here to avoid locking trouble in RAND_bytes */
1127 	seed = (unsigned int)time(NULL) ^ (unsigned int)getpid() ^
1128 		(((unsigned int)worker->thread_num)<<17);
1129 		/* shift thread_num so it does not match out pid bits */
1130 	if(!(worker->rndstate = ub_initstate(seed, daemon->rand))) {
1131 		seed = 0;
1132 		log_err("could not init random numbers.");
1133 		tube_delete(worker->cmd);
1134 		free(worker->ports);
1135 		free(worker);
1136 		return NULL;
1137 	}
1138 	seed = 0;
1139 #ifdef USE_DNSTAP
1140 	if(daemon->cfg->dnstap) {
1141 		log_assert(daemon->dtenv != NULL);
1142 		memcpy(&worker->dtenv, daemon->dtenv, sizeof(struct dt_env));
1143 		if(!dt_init(&worker->dtenv))
1144 			fatal_exit("dt_init failed");
1145 	}
1146 #endif
1147 	return worker;
1148 }
1149 
1150 int
1151 worker_init(struct worker* worker, struct config_file *cfg,
1152 	struct listen_port* ports, int do_sigs)
1153 {
1154 #ifdef USE_DNSTAP
1155 	struct dt_env* dtenv = &worker->dtenv;
1156 #else
1157 	void* dtenv = NULL;
1158 #endif
1159 	worker->need_to_exit = 0;
1160 	worker->base = comm_base_create(do_sigs);
1161 	if(!worker->base) {
1162 		log_err("could not create event handling base");
1163 		worker_delete(worker);
1164 		return 0;
1165 	}
1166 	comm_base_set_slow_accept_handlers(worker->base, &worker_stop_accept,
1167 		&worker_start_accept, worker);
1168 	if(do_sigs) {
1169 #ifdef SIGHUP
1170 		ub_thread_sig_unblock(SIGHUP);
1171 #endif
1172 		ub_thread_sig_unblock(SIGINT);
1173 #ifdef SIGQUIT
1174 		ub_thread_sig_unblock(SIGQUIT);
1175 #endif
1176 		ub_thread_sig_unblock(SIGTERM);
1177 #ifndef LIBEVENT_SIGNAL_PROBLEM
1178 		worker->comsig = comm_signal_create(worker->base,
1179 			worker_sighandler, worker);
1180 		if(!worker->comsig
1181 #ifdef SIGHUP
1182 			|| !comm_signal_bind(worker->comsig, SIGHUP)
1183 #endif
1184 #ifdef SIGQUIT
1185 			|| !comm_signal_bind(worker->comsig, SIGQUIT)
1186 #endif
1187 			|| !comm_signal_bind(worker->comsig, SIGTERM)
1188 			|| !comm_signal_bind(worker->comsig, SIGINT)) {
1189 			log_err("could not create signal handlers");
1190 			worker_delete(worker);
1191 			return 0;
1192 		}
1193 #endif /* LIBEVENT_SIGNAL_PROBLEM */
1194 		if(!daemon_remote_open_accept(worker->daemon->rc,
1195 			worker->daemon->rc_ports, worker)) {
1196 			worker_delete(worker);
1197 			return 0;
1198 		}
1199 #ifdef UB_ON_WINDOWS
1200 		wsvc_setup_worker(worker);
1201 #endif /* UB_ON_WINDOWS */
1202 	} else { /* !do_sigs */
1203 		worker->comsig = NULL;
1204 	}
1205 	worker->front = listen_create(worker->base, ports,
1206 		cfg->msg_buffer_size, (int)cfg->incoming_num_tcp,
1207 		worker->daemon->listen_sslctx, dtenv, worker_handle_request,
1208 		worker);
1209 	if(!worker->front) {
1210 		log_err("could not create listening sockets");
1211 		worker_delete(worker);
1212 		return 0;
1213 	}
1214 	worker->back = outside_network_create(worker->base,
1215 		cfg->msg_buffer_size, (size_t)cfg->outgoing_num_ports,
1216 		cfg->out_ifs, cfg->num_out_ifs, cfg->do_ip4, cfg->do_ip6,
1217 		cfg->do_tcp?cfg->outgoing_num_tcp:0,
1218 		worker->daemon->env->infra_cache, worker->rndstate,
1219 		cfg->use_caps_bits_for_id, worker->ports, worker->numports,
1220 		cfg->unwanted_threshold, &worker_alloc_cleanup, worker,
1221 		cfg->do_udp, worker->daemon->connect_sslctx, cfg->delay_close,
1222 		dtenv);
1223 	if(!worker->back) {
1224 		log_err("could not create outgoing sockets");
1225 		worker_delete(worker);
1226 		return 0;
1227 	}
1228 	/* start listening to commands */
1229 	if(!tube_setup_bg_listen(worker->cmd, worker->base,
1230 		&worker_handle_control_cmd, worker)) {
1231 		log_err("could not create control compt.");
1232 		worker_delete(worker);
1233 		return 0;
1234 	}
1235 	worker->stat_timer = comm_timer_create(worker->base,
1236 		worker_stat_timer_cb, worker);
1237 	if(!worker->stat_timer) {
1238 		log_err("could not create statistics timer");
1239 	}
1240 
1241 	/* we use the msg_buffer_size as a good estimate for what the
1242 	 * user wants for memory usage sizes */
1243 	worker->scratchpad = regional_create_custom(cfg->msg_buffer_size);
1244 	if(!worker->scratchpad) {
1245 		log_err("malloc failure");
1246 		worker_delete(worker);
1247 		return 0;
1248 	}
1249 
1250 	server_stats_init(&worker->stats, cfg);
1251 	alloc_init(&worker->alloc, &worker->daemon->superalloc,
1252 		worker->thread_num);
1253 	alloc_set_id_cleanup(&worker->alloc, &worker_alloc_cleanup, worker);
1254 	worker->env = *worker->daemon->env;
1255 	comm_base_timept(worker->base, &worker->env.now, &worker->env.now_tv);
1256 	if(worker->thread_num == 0)
1257 		log_set_time(worker->env.now);
1258 	worker->env.worker = worker;
1259 	worker->env.send_query = &worker_send_query;
1260 	worker->env.alloc = &worker->alloc;
1261 	worker->env.rnd = worker->rndstate;
1262 	worker->env.scratch = worker->scratchpad;
1263 	worker->env.mesh = mesh_create(&worker->daemon->mods, &worker->env);
1264 	worker->env.detach_subs = &mesh_detach_subs;
1265 	worker->env.attach_sub = &mesh_attach_sub;
1266 	worker->env.kill_sub = &mesh_state_delete;
1267 	worker->env.detect_cycle = &mesh_detect_cycle;
1268 	worker->env.scratch_buffer = sldns_buffer_new(cfg->msg_buffer_size);
1269 	if(!(worker->env.fwds = forwards_create()) ||
1270 		!forwards_apply_cfg(worker->env.fwds, cfg)) {
1271 		log_err("Could not set forward zones");
1272 		worker_delete(worker);
1273 		return 0;
1274 	}
1275 	if(!(worker->env.hints = hints_create()) ||
1276 		!hints_apply_cfg(worker->env.hints, cfg)) {
1277 		log_err("Could not set root or stub hints");
1278 		worker_delete(worker);
1279 		return 0;
1280 	}
1281 	/* one probe timer per process -- if we have 5011 anchors */
1282 	if(autr_get_num_anchors(worker->env.anchors) > 0
1283 #ifndef THREADS_DISABLED
1284 		&& worker->thread_num == 0
1285 #endif
1286 		) {
1287 		struct timeval tv;
1288 		tv.tv_sec = 0;
1289 		tv.tv_usec = 0;
1290 		worker->env.probe_timer = comm_timer_create(worker->base,
1291 			worker_probe_timer_cb, worker);
1292 		if(!worker->env.probe_timer) {
1293 			log_err("could not create 5011-probe timer");
1294 		} else {
1295 			/* let timer fire, then it can reset itself */
1296 			comm_timer_set(worker->env.probe_timer, &tv);
1297 		}
1298 	}
1299 	if(!worker->env.mesh || !worker->env.scratch_buffer) {
1300 		worker_delete(worker);
1301 		return 0;
1302 	}
1303 	worker_mem_report(worker, NULL);
1304 	/* if statistics enabled start timer */
1305 	if(worker->env.cfg->stat_interval > 0) {
1306 		verbose(VERB_ALGO, "set statistics interval %d secs",
1307 			worker->env.cfg->stat_interval);
1308 		worker_restart_timer(worker);
1309 	}
1310 	return 1;
1311 }
1312 
1313 void
1314 worker_work(struct worker* worker)
1315 {
1316 	comm_base_dispatch(worker->base);
1317 }
1318 
1319 void
1320 worker_delete(struct worker* worker)
1321 {
1322 	if(!worker)
1323 		return;
1324 	if(worker->env.mesh && verbosity >= VERB_OPS) {
1325 		server_stats_log(&worker->stats, worker, worker->thread_num);
1326 		mesh_stats(worker->env.mesh, "mesh has");
1327 		worker_mem_report(worker, NULL);
1328 	}
1329 	outside_network_quit_prepare(worker->back);
1330 	mesh_delete(worker->env.mesh);
1331 	sldns_buffer_free(worker->env.scratch_buffer);
1332 	forwards_delete(worker->env.fwds);
1333 	hints_delete(worker->env.hints);
1334 	listen_delete(worker->front);
1335 	outside_network_delete(worker->back);
1336 	comm_signal_delete(worker->comsig);
1337 	tube_delete(worker->cmd);
1338 	comm_timer_delete(worker->stat_timer);
1339 	comm_timer_delete(worker->env.probe_timer);
1340 	free(worker->ports);
1341 	if(worker->thread_num == 0) {
1342 		log_set_time(NULL);
1343 #ifdef UB_ON_WINDOWS
1344 		wsvc_desetup_worker(worker);
1345 #endif /* UB_ON_WINDOWS */
1346 	}
1347 	comm_base_delete(worker->base);
1348 	ub_randfree(worker->rndstate);
1349 	alloc_clear(&worker->alloc);
1350 	regional_destroy(worker->scratchpad);
1351 	free(worker);
1352 }
1353 
1354 struct outbound_entry*
1355 worker_send_query(uint8_t* qname, size_t qnamelen, uint16_t qtype,
1356 	uint16_t qclass, uint16_t flags, int dnssec, int want_dnssec,
1357 	int nocaps, struct sockaddr_storage* addr, socklen_t addrlen,
1358 	uint8_t* zone, size_t zonelen, struct module_qstate* q)
1359 {
1360 	struct worker* worker = q->env->worker;
1361 	struct outbound_entry* e = (struct outbound_entry*)regional_alloc(
1362 		q->region, sizeof(*e));
1363 	if(!e)
1364 		return NULL;
1365 	e->qstate = q;
1366 	e->qsent = outnet_serviced_query(worker->back, qname,
1367 		qnamelen, qtype, qclass, flags, dnssec, want_dnssec, nocaps,
1368 		q->env->cfg->tcp_upstream, q->env->cfg->ssl_upstream, addr,
1369 		addrlen, zone, zonelen, worker_handle_service_reply, e,
1370 		worker->back->udp_buff);
1371 	if(!e->qsent) {
1372 		return NULL;
1373 	}
1374 	return e;
1375 }
1376 
1377 void
1378 worker_alloc_cleanup(void* arg)
1379 {
1380 	struct worker* worker = (struct worker*)arg;
1381 	slabhash_clear(&worker->env.rrset_cache->table);
1382 	slabhash_clear(worker->env.msg_cache);
1383 }
1384 
1385 void worker_stats_clear(struct worker* worker)
1386 {
1387 	server_stats_init(&worker->stats, worker->env.cfg);
1388 	mesh_stats_clear(worker->env.mesh);
1389 	worker->back->unwanted_replies = 0;
1390 	worker->back->num_tcp_outgoing = 0;
1391 }
1392 
1393 void worker_start_accept(void* arg)
1394 {
1395 	struct worker* worker = (struct worker*)arg;
1396 	listen_start_accept(worker->front);
1397 	if(worker->thread_num == 0)
1398 		daemon_remote_start_accept(worker->daemon->rc);
1399 }
1400 
1401 void worker_stop_accept(void* arg)
1402 {
1403 	struct worker* worker = (struct worker*)arg;
1404 	listen_stop_accept(worker->front);
1405 	if(worker->thread_num == 0)
1406 		daemon_remote_stop_accept(worker->daemon->rc);
1407 }
1408 
1409 /* --- fake callbacks for fptr_wlist to work --- */
1410 struct outbound_entry* libworker_send_query(uint8_t* ATTR_UNUSED(qname),
1411 	size_t ATTR_UNUSED(qnamelen), uint16_t ATTR_UNUSED(qtype),
1412 	uint16_t ATTR_UNUSED(qclass), uint16_t ATTR_UNUSED(flags),
1413 	int ATTR_UNUSED(dnssec), int ATTR_UNUSED(want_dnssec),
1414 	int ATTR_UNUSED(nocaps), struct sockaddr_storage* ATTR_UNUSED(addr),
1415 	socklen_t ATTR_UNUSED(addrlen), uint8_t* ATTR_UNUSED(zone),
1416 	size_t ATTR_UNUSED(zonelen), struct module_qstate* ATTR_UNUSED(q))
1417 {
1418 	log_assert(0);
1419 	return 0;
1420 }
1421 
1422 int libworker_handle_reply(struct comm_point* ATTR_UNUSED(c),
1423 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1424         struct comm_reply* ATTR_UNUSED(reply_info))
1425 {
1426 	log_assert(0);
1427 	return 0;
1428 }
1429 
1430 int libworker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
1431 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1432         struct comm_reply* ATTR_UNUSED(reply_info))
1433 {
1434 	log_assert(0);
1435 	return 0;
1436 }
1437 
1438 void libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
1439         uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
1440         int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
1441 {
1442 	log_assert(0);
1443 }
1444 
1445 void libworker_fg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1446         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1447 	char* ATTR_UNUSED(why_bogus))
1448 {
1449 	log_assert(0);
1450 }
1451 
1452 void libworker_bg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1453         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1454 	char* ATTR_UNUSED(why_bogus))
1455 {
1456 	log_assert(0);
1457 }
1458 
1459 void libworker_event_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1460         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1461 	char* ATTR_UNUSED(why_bogus))
1462 {
1463 	log_assert(0);
1464 }
1465 
1466 int context_query_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1467 {
1468 	log_assert(0);
1469 	return 0;
1470 }
1471 
1472 int order_lock_cmp(const void* ATTR_UNUSED(e1), const void* ATTR_UNUSED(e2))
1473 {
1474         log_assert(0);
1475         return 0;
1476 }
1477 
1478 int codeline_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1479 {
1480         log_assert(0);
1481         return 0;
1482 }
1483 
1484