]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - daemon/worker.c
Vendor import of Unbound 1.6.4.
[FreeBSD/FreeBSD.git] / daemon / worker.c
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 "respip/respip.h"
73 #include "libunbound/context.h"
74 #include "libunbound/libworker.h"
75 #include "sldns/sbuffer.h"
76 #include "sldns/wire2str.h"
77 #include "util/shm_side/shm_main.h"
78 #include "dnscrypt/dnscrypt.h"
79
80 #ifdef HAVE_SYS_TYPES_H
81 #  include <sys/types.h>
82 #endif
83 #ifdef HAVE_NETDB_H
84 #include <netdb.h>
85 #endif
86 #include <signal.h>
87 #ifdef UB_ON_WINDOWS
88 #include "winrc/win_svc.h"
89 #endif
90
91 /** Size of an UDP datagram */
92 #define NORMAL_UDP_SIZE 512 /* bytes */
93 /** ratelimit for error responses */
94 #define ERROR_RATELIMIT 100 /* qps */
95
96 /** 
97  * seconds to add to prefetch leeway.  This is a TTL that expires old rrsets
98  * earlier than they should in order to put the new update into the cache.
99  * This additional value is to make sure that if not all TTLs are equal in
100  * the message to be updated(and replaced), that rrsets with up to this much
101  * extra TTL are also replaced.  This means that the resulting new message
102  * will have (most likely) this TTL at least, avoiding very small 'split
103  * second' TTLs due to operators choosing relative primes for TTLs (or so).
104  * Also has to be at least one to break ties (and overwrite cached entry).
105  */
106 #define PREFETCH_EXPIRY_ADD 60
107
108 /** Report on memory usage by this thread and global */
109 static void
110 worker_mem_report(struct worker* ATTR_UNUSED(worker), 
111         struct serviced_query* ATTR_UNUSED(cur_serv))
112 {
113 #ifdef UNBOUND_ALLOC_STATS
114         /* measure memory leakage */
115         extern size_t unbound_mem_alloc, unbound_mem_freed;
116         /* debug func in validator module */
117         size_t total, front, back, mesh, msg, rrset, infra, ac, superac;
118         size_t me, iter, val, anch;
119         int i;
120 #ifdef CLIENT_SUBNET
121         size_t subnet = 0;
122 #endif /* CLIENT_SUBNET */
123         if(verbosity < VERB_ALGO) 
124                 return;
125         front = listen_get_mem(worker->front);
126         back = outnet_get_mem(worker->back);
127         msg = slabhash_get_mem(worker->env.msg_cache);
128         rrset = slabhash_get_mem(&worker->env.rrset_cache->table);
129         infra = infra_get_mem(worker->env.infra_cache);
130         mesh = mesh_get_mem(worker->env.mesh);
131         ac = alloc_get_mem(&worker->alloc);
132         superac = alloc_get_mem(&worker->daemon->superalloc);
133         anch = anchors_get_mem(worker->env.anchors);
134         iter = 0;
135         val = 0;
136         for(i=0; i<worker->env.mesh->mods.num; i++) {
137                 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
138                         mods.mod[i]->get_mem));
139                 if(strcmp(worker->env.mesh->mods.mod[i]->name, "validator")==0)
140                         val += (*worker->env.mesh->mods.mod[i]->get_mem)
141                                 (&worker->env, i);
142 #ifdef CLIENT_SUBNET
143                 else if(strcmp(worker->env.mesh->mods.mod[i]->name,
144                         "subnet")==0)
145                         subnet += (*worker->env.mesh->mods.mod[i]->get_mem)
146                                 (&worker->env, i);
147 #endif /* CLIENT_SUBNET */
148                 else    iter += (*worker->env.mesh->mods.mod[i]->get_mem)
149                                 (&worker->env, i);
150         }
151         me = sizeof(*worker) + sizeof(*worker->base) + sizeof(*worker->comsig)
152                 + comm_point_get_mem(worker->cmd_com) 
153                 + sizeof(worker->rndstate) 
154                 + regional_get_mem(worker->scratchpad) 
155                 + sizeof(*worker->env.scratch_buffer) 
156                 + sldns_buffer_capacity(worker->env.scratch_buffer)
157                 + forwards_get_mem(worker->env.fwds)
158                 + hints_get_mem(worker->env.hints);
159         if(worker->thread_num == 0)
160                 me += acl_list_get_mem(worker->daemon->acl);
161         if(cur_serv) {
162                 me += serviced_get_mem(cur_serv);
163         }
164         total = front+back+mesh+msg+rrset+infra+iter+val+ac+superac+me;
165 #ifdef CLIENT_SUBNET
166         total += subnet;
167         log_info("Memory conditions: %u front=%u back=%u mesh=%u msg=%u "
168                 "rrset=%u infra=%u iter=%u val=%u subnet=%u anchors=%u "
169                 "alloccache=%u globalalloccache=%u me=%u",
170                 (unsigned)total, (unsigned)front, (unsigned)back, 
171                 (unsigned)mesh, (unsigned)msg, (unsigned)rrset, (unsigned)infra,
172                 (unsigned)iter, (unsigned)val,
173                 (unsigned)subnet, (unsigned)anch, (unsigned)ac,
174                 (unsigned)superac, (unsigned)me);
175 #else /* no CLIENT_SUBNET */
176         log_info("Memory conditions: %u front=%u back=%u mesh=%u msg=%u "
177                 "rrset=%u infra=%u iter=%u val=%u anchors=%u "
178                 "alloccache=%u globalalloccache=%u me=%u",
179                 (unsigned)total, (unsigned)front, (unsigned)back, 
180                 (unsigned)mesh, (unsigned)msg, (unsigned)rrset, 
181                 (unsigned)infra, (unsigned)iter, (unsigned)val, (unsigned)anch,
182                 (unsigned)ac, (unsigned)superac, (unsigned)me);
183 #endif /* CLIENT_SUBNET */
184         log_info("Total heap memory estimate: %u  total-alloc: %u  "
185                 "total-free: %u", (unsigned)total, 
186                 (unsigned)unbound_mem_alloc, (unsigned)unbound_mem_freed);
187 #else /* no UNBOUND_ALLOC_STATS */
188         size_t val = 0;
189 #ifdef CLIENT_SUBNET
190         size_t subnet = 0;
191 #endif /* CLIENT_SUBNET */
192         int i;
193         if(verbosity < VERB_QUERY)
194                 return;
195         for(i=0; i<worker->env.mesh->mods.num; i++) {
196                 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
197                         mods.mod[i]->get_mem));
198                 if(strcmp(worker->env.mesh->mods.mod[i]->name, "validator")==0)
199                         val += (*worker->env.mesh->mods.mod[i]->get_mem)
200                                 (&worker->env, i);
201 #ifdef CLIENT_SUBNET
202                 else if(strcmp(worker->env.mesh->mods.mod[i]->name,
203                         "subnet")==0)
204                         subnet += (*worker->env.mesh->mods.mod[i]->get_mem)
205                                 (&worker->env, i);
206 #endif /* CLIENT_SUBNET */
207         }
208 #ifdef CLIENT_SUBNET
209         verbose(VERB_QUERY, "cache memory msg=%u rrset=%u infra=%u val=%u "
210                 "subnet=%u",
211                 (unsigned)slabhash_get_mem(worker->env.msg_cache),
212                 (unsigned)slabhash_get_mem(&worker->env.rrset_cache->table),
213                 (unsigned)infra_get_mem(worker->env.infra_cache),
214                 (unsigned)val, (unsigned)subnet);
215 #else /* no CLIENT_SUBNET */
216         verbose(VERB_QUERY, "cache memory msg=%u rrset=%u infra=%u val=%u",
217                 (unsigned)slabhash_get_mem(worker->env.msg_cache),
218                 (unsigned)slabhash_get_mem(&worker->env.rrset_cache->table),
219                 (unsigned)infra_get_mem(worker->env.infra_cache),
220                 (unsigned)val);
221 #endif /* CLIENT_SUBNET */
222 #endif /* UNBOUND_ALLOC_STATS */
223 }
224
225 void 
226 worker_send_cmd(struct worker* worker, enum worker_commands cmd)
227 {
228         uint32_t c = (uint32_t)htonl(cmd);
229         if(!tube_write_msg(worker->cmd, (uint8_t*)&c, sizeof(c), 0)) {
230                 log_err("worker send cmd %d failed", (int)cmd);
231         }
232 }
233
234 int 
235 worker_handle_reply(struct comm_point* c, void* arg, int error, 
236         struct comm_reply* reply_info)
237 {
238         struct module_qstate* q = (struct module_qstate*)arg;
239         struct worker* worker = q->env->worker;
240         struct outbound_entry e;
241         e.qstate = q;
242         e.qsent = NULL;
243
244         if(error != 0) {
245                 mesh_report_reply(worker->env.mesh, &e, reply_info, error);
246                 worker_mem_report(worker, NULL);
247                 return 0;
248         }
249         /* sanity check. */
250         if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
251                 || LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) != 
252                         LDNS_PACKET_QUERY
253                 || LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
254                 /* error becomes timeout for the module as if this reply
255                  * never arrived. */
256                 mesh_report_reply(worker->env.mesh, &e, reply_info, 
257                         NETEVENT_TIMEOUT);
258                 worker_mem_report(worker, NULL);
259                 return 0;
260         }
261         mesh_report_reply(worker->env.mesh, &e, reply_info, NETEVENT_NOERROR);
262         worker_mem_report(worker, NULL);
263         return 0;
264 }
265
266 int 
267 worker_handle_service_reply(struct comm_point* c, void* arg, int error, 
268         struct comm_reply* reply_info)
269 {
270         struct outbound_entry* e = (struct outbound_entry*)arg;
271         struct worker* worker = e->qstate->env->worker;
272         struct serviced_query *sq = e->qsent;
273
274         verbose(VERB_ALGO, "worker svcd callback for qstate %p", e->qstate);
275         if(error != 0) {
276                 mesh_report_reply(worker->env.mesh, e, reply_info, error);
277                 worker_mem_report(worker, sq);
278                 return 0;
279         }
280         /* sanity check. */
281         if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
282                 || LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) != 
283                         LDNS_PACKET_QUERY
284                 || LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
285                 /* error becomes timeout for the module as if this reply
286                  * never arrived. */
287                 verbose(VERB_ALGO, "worker: bad reply handled as timeout");
288                 mesh_report_reply(worker->env.mesh, e, reply_info, 
289                         NETEVENT_TIMEOUT);
290                 worker_mem_report(worker, sq);
291                 return 0;
292         }
293         mesh_report_reply(worker->env.mesh, e, reply_info, NETEVENT_NOERROR);
294         worker_mem_report(worker, sq);
295         return 0;
296 }
297
298 /** ratelimit error replies
299  * @param worker: the worker struct with ratelimit counter
300  * @param err: error code that would be wanted.
301  * @return value of err if okay, or -1 if it should be discarded instead.
302  */
303 static int
304 worker_err_ratelimit(struct worker* worker, int err)
305 {
306         if(worker->err_limit_time == *worker->env.now) {
307                 /* see if limit is exceeded for this second */
308                 if(worker->err_limit_count++ > ERROR_RATELIMIT)
309                         return -1;
310         } else {
311                 /* new second, new limits */
312                 worker->err_limit_time = *worker->env.now;
313                 worker->err_limit_count = 1;
314         }
315         return err;
316 }
317
318 /** check request sanity.
319  * @param pkt: the wire packet to examine for sanity.
320  * @param worker: parameters for checking.
321  * @return error code, 0 OK, or -1 discard.
322 */
323 static int 
324 worker_check_request(sldns_buffer* pkt, struct worker* worker)
325 {
326         if(sldns_buffer_limit(pkt) < LDNS_HEADER_SIZE) {
327                 verbose(VERB_QUERY, "request too short, discarded");
328                 return -1;
329         }
330         if(sldns_buffer_limit(pkt) > NORMAL_UDP_SIZE && 
331                 worker->daemon->cfg->harden_large_queries) {
332                 verbose(VERB_QUERY, "request too large, discarded");
333                 return -1;
334         }
335         if(LDNS_QR_WIRE(sldns_buffer_begin(pkt))) {
336                 verbose(VERB_QUERY, "request has QR bit on, discarded");
337                 return -1;
338         }
339         if(LDNS_TC_WIRE(sldns_buffer_begin(pkt))) {
340                 LDNS_TC_CLR(sldns_buffer_begin(pkt));
341                 verbose(VERB_QUERY, "request bad, has TC bit on");
342                 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
343         }
344         if(LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)) != LDNS_PACKET_QUERY) {
345                 verbose(VERB_QUERY, "request unknown opcode %d", 
346                         LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)));
347                 return worker_err_ratelimit(worker, LDNS_RCODE_NOTIMPL);
348         }
349         if(LDNS_QDCOUNT(sldns_buffer_begin(pkt)) != 1) {
350                 verbose(VERB_QUERY, "request wrong nr qd=%d", 
351                         LDNS_QDCOUNT(sldns_buffer_begin(pkt)));
352                 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
353         }
354         if(LDNS_ANCOUNT(sldns_buffer_begin(pkt)) != 0) {
355                 verbose(VERB_QUERY, "request wrong nr an=%d", 
356                         LDNS_ANCOUNT(sldns_buffer_begin(pkt)));
357                 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
358         }
359         if(LDNS_NSCOUNT(sldns_buffer_begin(pkt)) != 0) {
360                 verbose(VERB_QUERY, "request wrong nr ns=%d", 
361                         LDNS_NSCOUNT(sldns_buffer_begin(pkt)));
362                 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
363         }
364         if(LDNS_ARCOUNT(sldns_buffer_begin(pkt)) > 1) {
365                 verbose(VERB_QUERY, "request wrong nr ar=%d", 
366                         LDNS_ARCOUNT(sldns_buffer_begin(pkt)));
367                 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
368         }
369         return 0;
370 }
371
372 void 
373 worker_handle_control_cmd(struct tube* ATTR_UNUSED(tube), uint8_t* msg,
374         size_t len, int error, void* arg)
375 {
376         struct worker* worker = (struct worker*)arg;
377         enum worker_commands cmd;
378         if(error != NETEVENT_NOERROR) {
379                 free(msg);
380                 if(error == NETEVENT_CLOSED)
381                         comm_base_exit(worker->base);
382                 else    log_info("control event: %d", error);
383                 return;
384         }
385         if(len != sizeof(uint32_t)) {
386                 fatal_exit("bad control msg length %d", (int)len);
387         }
388         cmd = sldns_read_uint32(msg);
389         free(msg);
390         switch(cmd) {
391         case worker_cmd_quit:
392                 verbose(VERB_ALGO, "got control cmd quit");
393                 comm_base_exit(worker->base);
394                 break;
395         case worker_cmd_stats:
396                 verbose(VERB_ALGO, "got control cmd stats");
397                 server_stats_reply(worker, 1);
398                 break;
399         case worker_cmd_stats_noreset:
400                 verbose(VERB_ALGO, "got control cmd stats_noreset");
401                 server_stats_reply(worker, 0);
402                 break;
403         case worker_cmd_remote:
404                 verbose(VERB_ALGO, "got control cmd remote");
405                 daemon_remote_exec(worker);
406                 break;
407         default:
408                 log_err("bad command %d", (int)cmd);
409                 break;
410         }
411 }
412
413 /** check if a delegation is secure */
414 static enum sec_status
415 check_delegation_secure(struct reply_info *rep) 
416 {
417         /* return smallest security status */
418         size_t i;
419         enum sec_status sec = sec_status_secure;
420         enum sec_status s;
421         size_t num = rep->an_numrrsets + rep->ns_numrrsets;
422         /* check if answer and authority are OK */
423         for(i=0; i<num; i++) {
424                 s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
425                         ->security;
426                 if(s < sec)
427                         sec = s;
428         }
429         /* in additional, only unchecked triggers revalidation */
430         for(i=num; i<rep->rrset_count; i++) {
431                 s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
432                         ->security;
433                 if(s == sec_status_unchecked)
434                         return s;
435         }
436         return sec;
437 }
438
439 /** remove nonsecure from a delegation referral additional section */
440 static void
441 deleg_remove_nonsecure_additional(struct reply_info* rep)
442 {
443         /* we can simply edit it, since we are working in the scratch region */
444         size_t i;
445         enum sec_status s;
446
447         for(i = rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
448                 s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
449                         ->security;
450                 if(s != sec_status_secure) {
451                         memmove(rep->rrsets+i, rep->rrsets+i+1, 
452                                 sizeof(struct ub_packed_rrset_key*)* 
453                                 (rep->rrset_count - i - 1));
454                         rep->ar_numrrsets--; 
455                         rep->rrset_count--;
456                         i--;
457                 }
458         }
459 }
460
461 /** answer nonrecursive query from the cache */
462 static int
463 answer_norec_from_cache(struct worker* worker, struct query_info* qinfo,
464         uint16_t id, uint16_t flags, struct comm_reply* repinfo, 
465         struct edns_data* edns)
466 {
467         /* for a nonrecursive query return either:
468          *      o an error (servfail; we try to avoid this)
469          *      o a delegation (closest we have; this routine tries that)
470          *      o the answer (checked by answer_from_cache) 
471          *
472          * So, grab a delegation from the rrset cache. 
473          * Then check if it needs validation, if so, this routine fails,
474          * so that iterator can prime and validator can verify rrsets.
475          */
476         uint16_t udpsize = edns->udp_size;
477         int secure = 0;
478         time_t timenow = *worker->env.now;
479         int must_validate = (!(flags&BIT_CD) || worker->env.cfg->ignore_cd)
480                 && worker->env.need_to_validate;
481         struct dns_msg *msg = NULL;
482         struct delegpt *dp;
483
484         dp = dns_cache_find_delegation(&worker->env, qinfo->qname, 
485                 qinfo->qname_len, qinfo->qtype, qinfo->qclass,
486                 worker->scratchpad, &msg, timenow);
487         if(!dp) { /* no delegation, need to reprime */
488                 return 0;
489         }
490         /* In case we have a local alias, copy it into the delegation message.
491          * Shallow copy should be fine, as we'll be done with msg in this
492          * function. */
493         msg->qinfo.local_alias = qinfo->local_alias;
494         if(must_validate) {
495                 switch(check_delegation_secure(msg->rep)) {
496                 case sec_status_unchecked:
497                         /* some rrsets have not been verified yet, go and 
498                          * let validator do that */
499                         return 0;
500                 case sec_status_bogus:
501                         /* some rrsets are bogus, reply servfail */
502                         edns->edns_version = EDNS_ADVERTISED_VERSION;
503                         edns->udp_size = EDNS_ADVERTISED_SIZE;
504                         edns->ext_rcode = 0;
505                         edns->bits &= EDNS_DO;
506                         if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL,
507                                 msg->rep, LDNS_RCODE_SERVFAIL, edns, worker->scratchpad))
508                                         return 0;
509                         error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL, 
510                                 &msg->qinfo, id, flags, edns);
511                         if(worker->stats.extended) {
512                                 worker->stats.ans_bogus++;
513                                 worker->stats.ans_rcode[LDNS_RCODE_SERVFAIL]++;
514                         }
515                         return 1;
516                 case sec_status_secure:
517                         /* all rrsets are secure */
518                         /* remove non-secure rrsets from the add. section*/
519                         if(worker->env.cfg->val_clean_additional)
520                                 deleg_remove_nonsecure_additional(msg->rep);
521                         secure = 1;
522                         break;
523                 case sec_status_indeterminate:
524                 case sec_status_insecure:
525                 default:
526                         /* not secure */
527                         secure = 0;
528                         break;
529                 }
530         }
531         /* return this delegation from the cache */
532         edns->edns_version = EDNS_ADVERTISED_VERSION;
533         edns->udp_size = EDNS_ADVERTISED_SIZE;
534         edns->ext_rcode = 0;
535         edns->bits &= EDNS_DO;
536         if(!inplace_cb_reply_cache_call(&worker->env, qinfo, NULL, msg->rep,
537                 (int)(flags&LDNS_RCODE_MASK), edns, worker->scratchpad))
538                         return 0;
539         msg->rep->flags |= BIT_QR|BIT_RA;
540         if(!reply_info_answer_encode(&msg->qinfo, msg->rep, id, flags, 
541                 repinfo->c->buffer, 0, 1, worker->scratchpad,
542                 udpsize, edns, (int)(edns->bits & EDNS_DO), secure)) {
543                 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL, NULL,
544                         LDNS_RCODE_SERVFAIL, edns, worker->scratchpad))
545                                 edns->opt_list = NULL;
546                 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL, 
547                         &msg->qinfo, id, flags, edns);
548         }
549         if(worker->stats.extended) {
550                 if(secure) worker->stats.ans_secure++;
551                 server_stats_insrcode(&worker->stats, repinfo->c->buffer);
552         }
553         return 1;
554 }
555
556 /** Apply, if applicable, a response IP action to a cached answer.
557  * If the answer is rewritten as a result of an action, '*encode_repp' will
558  * point to the reply info containing the modified answer.  '*encode_repp' will
559  * be intact otherwise.
560  * It returns 1 on success, 0 otherwise. */
561 static int
562 apply_respip_action(struct worker* worker, const struct query_info* qinfo,
563         struct respip_client_info* cinfo, struct reply_info* rep,
564         struct comm_reply* repinfo, struct ub_packed_rrset_key** alias_rrset,
565         struct reply_info** encode_repp)
566 {
567         struct respip_action_info actinfo = {respip_none, NULL};
568
569         if(qinfo->qtype != LDNS_RR_TYPE_A &&
570                 qinfo->qtype != LDNS_RR_TYPE_AAAA &&
571                 qinfo->qtype != LDNS_RR_TYPE_ANY)
572                 return 1;
573
574         if(!respip_rewrite_reply(qinfo, cinfo, rep, encode_repp, &actinfo,
575                 alias_rrset, 0, worker->scratchpad))
576                 return 0;
577
578         /* xxx_deny actions mean dropping the reply, unless the original reply
579          * was redirected to response-ip data. */
580         if((actinfo.action == respip_deny ||
581                 actinfo.action == respip_inform_deny) &&
582                 *encode_repp == rep)
583                 *encode_repp = NULL;
584
585         /* If address info is returned, it means the action should be an
586          * 'inform' variant and the information should be logged. */
587         if(actinfo.addrinfo) {
588                 respip_inform_print(actinfo.addrinfo, qinfo->qname,
589                         qinfo->qtype, qinfo->qclass, qinfo->local_alias,
590                         repinfo);
591         }
592
593         return 1;
594 }
595
596 /** answer query from the cache.
597  * Normally, the answer message will be built in repinfo->c->buffer; if the
598  * answer is supposed to be suppressed or the answer is supposed to be an
599  * incomplete CNAME chain, the buffer is explicitly cleared to signal the
600  * caller as such.  In the latter case *partial_rep will point to the incomplete
601  * reply, and this function is (possibly) supposed to be called again with that
602  * *partial_rep value to complete the chain.  In addition, if the query should
603  * be completely dropped, '*need_drop' will be set to 1. */
604 static int
605 answer_from_cache(struct worker* worker, struct query_info* qinfo,
606         struct respip_client_info* cinfo, int* need_drop,
607         struct ub_packed_rrset_key** alias_rrset,
608         struct reply_info** partial_repp,
609         struct reply_info* rep, uint16_t id, uint16_t flags, 
610         struct comm_reply* repinfo, struct edns_data* edns)
611 {
612         time_t timenow = *worker->env.now;
613         uint16_t udpsize = edns->udp_size;
614         struct reply_info* encode_rep = rep;
615         struct reply_info* partial_rep = *partial_repp;
616         int secure;
617         int must_validate = (!(flags&BIT_CD) || worker->env.cfg->ignore_cd)
618                 && worker->env.need_to_validate;
619         *partial_repp = NULL;   /* avoid accidental further pass */
620         if(worker->env.cfg->serve_expired) {
621                 /* always lock rrsets, rep->ttl is ignored */
622                 if(!rrset_array_lock(rep->ref, rep->rrset_count, 0))
623                         return 0;
624                 /* below, rrsets with ttl before timenow become TTL 0 in
625                  * the response */
626                 /* This response was served with zero TTL */
627                 if (timenow >= rep->ttl) {
628                         worker->stats.zero_ttl_responses++;
629                 }
630         } else {
631                 /* see if it is possible */
632                 if(rep->ttl < timenow) {
633                         /* the rrsets may have been updated in the meantime.
634                          * we will refetch the message format from the
635                          * authoritative server 
636                          */
637                         return 0;
638                 }
639                 if(!rrset_array_lock(rep->ref, rep->rrset_count, timenow))
640                         return 0;
641                 /* locked and ids and ttls are OK. */
642         }
643         /* check CNAME chain (if any) */
644         if(rep->an_numrrsets > 0 && (rep->rrsets[0]->rk.type == 
645                 htons(LDNS_RR_TYPE_CNAME) || rep->rrsets[0]->rk.type == 
646                 htons(LDNS_RR_TYPE_DNAME))) {
647                 if(!reply_check_cname_chain(qinfo, rep)) {
648                         /* cname chain invalid, redo iterator steps */
649                         verbose(VERB_ALGO, "Cache reply: cname chain broken");
650                 bail_out:
651                         rrset_array_unlock_touch(worker->env.rrset_cache, 
652                                 worker->scratchpad, rep->ref, rep->rrset_count);
653                         return 0;
654                 }
655         }
656         /* check security status of the cached answer */
657         if( rep->security == sec_status_bogus && must_validate) {
658                 /* BAD cached */
659                 edns->edns_version = EDNS_ADVERTISED_VERSION;
660                 edns->udp_size = EDNS_ADVERTISED_SIZE;
661                 edns->ext_rcode = 0;
662                 edns->bits &= EDNS_DO;
663                 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL, rep,
664                         LDNS_RCODE_SERVFAIL, edns, worker->scratchpad))
665                         goto bail_out;
666                 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL, 
667                         qinfo, id, flags, edns);
668                 rrset_array_unlock_touch(worker->env.rrset_cache, 
669                         worker->scratchpad, rep->ref, rep->rrset_count);
670                 if(worker->stats.extended) {
671                         worker->stats.ans_bogus ++;
672                         worker->stats.ans_rcode[LDNS_RCODE_SERVFAIL] ++;
673                 }
674                 return 1;
675         } else if( rep->security == sec_status_unchecked && must_validate) {
676                 verbose(VERB_ALGO, "Cache reply: unchecked entry needs "
677                         "validation");
678                 goto bail_out; /* need to validate cache entry first */
679         } else if(rep->security == sec_status_secure) {
680                 if(reply_all_rrsets_secure(rep))
681                         secure = 1;
682                 else    {
683                         if(must_validate) {
684                                 verbose(VERB_ALGO, "Cache reply: secure entry"
685                                         " changed status");
686                                 goto bail_out; /* rrset changed, re-verify */
687                         }
688                         secure = 0;
689                 }
690         } else  secure = 0;
691
692         edns->edns_version = EDNS_ADVERTISED_VERSION;
693         edns->udp_size = EDNS_ADVERTISED_SIZE;
694         edns->ext_rcode = 0;
695         edns->bits &= EDNS_DO;
696         if(!inplace_cb_reply_cache_call(&worker->env, qinfo, NULL, rep,
697                 (int)(flags&LDNS_RCODE_MASK), edns, worker->scratchpad))
698                 goto bail_out;
699         *alias_rrset = NULL; /* avoid confusion if caller set it to non-NULL */
700         if(worker->daemon->use_response_ip && !partial_rep &&
701            !apply_respip_action(worker, qinfo, cinfo, rep, repinfo, alias_rrset,
702                         &encode_rep)) {
703                 goto bail_out;
704         } else if(partial_rep &&
705                 !respip_merge_cname(partial_rep, qinfo, rep, cinfo,
706                 must_validate, &encode_rep, worker->scratchpad)) {
707                 goto bail_out;
708         }
709         if(encode_rep != rep)
710                 secure = 0; /* if rewritten, it can't be considered "secure" */
711         if(!encode_rep || *alias_rrset) {
712                 sldns_buffer_clear(repinfo->c->buffer);
713                 sldns_buffer_flip(repinfo->c->buffer);
714                 if(!encode_rep)
715                         *need_drop = 1;
716                 else {
717                         /* If a partial CNAME chain is found, we first need to
718                          * make a copy of the reply in the scratchpad so we
719                          * can release the locks and lookup the cache again. */
720                         *partial_repp = reply_info_copy(encode_rep, NULL,
721                                 worker->scratchpad);
722                         if(!*partial_repp)
723                                 goto bail_out;
724                 }
725         } else if(!reply_info_answer_encode(qinfo, encode_rep, id, flags,
726                 repinfo->c->buffer, timenow, 1, worker->scratchpad,
727                 udpsize, edns, (int)(edns->bits & EDNS_DO), secure)) {
728                 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL, NULL,
729                         LDNS_RCODE_SERVFAIL, edns, worker->scratchpad))
730                                 edns->opt_list = NULL;
731                 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL, 
732                         qinfo, id, flags, edns);
733         }
734         /* cannot send the reply right now, because blocking network syscall
735          * is bad while holding locks. */
736         rrset_array_unlock_touch(worker->env.rrset_cache, worker->scratchpad,
737                 rep->ref, rep->rrset_count);
738         if(worker->stats.extended) {
739                 if(secure) worker->stats.ans_secure++;
740                 server_stats_insrcode(&worker->stats, repinfo->c->buffer);
741         }
742         /* go and return this buffer to the client */
743         return 1;
744 }
745
746 /** Reply to client and perform prefetch to keep cache up to date.
747  * If the buffer for the reply is empty, it indicates that only prefetch is
748  * necessary and the reply should be suppressed (because it's dropped or
749  * being deferred). */
750 static void
751 reply_and_prefetch(struct worker* worker, struct query_info* qinfo, 
752         uint16_t flags, struct comm_reply* repinfo, time_t leeway)
753 {
754         /* first send answer to client to keep its latency 
755          * as small as a cachereply */
756         if(sldns_buffer_limit(repinfo->c->buffer) != 0)
757                 comm_point_send_reply(repinfo);
758         server_stats_prefetch(&worker->stats, worker);
759         
760         /* create the prefetch in the mesh as a normal lookup without
761          * client addrs waiting, which has the cache blacklisted (to bypass
762          * the cache and go to the network for the data). */
763         /* this (potentially) runs the mesh for the new query */
764         mesh_new_prefetch(worker->env.mesh, qinfo, flags, leeway + 
765                 PREFETCH_EXPIRY_ADD);
766 }
767
768 /**
769  * Fill CH class answer into buffer. Keeps query.
770  * @param pkt: buffer
771  * @param str: string to put into text record (<255).
772  *      array of strings, every string becomes a text record.
773  * @param num: number of strings in array.
774  * @param edns: edns reply information.
775  * @param worker: worker with scratch region.
776  */
777 static void
778 chaos_replystr(sldns_buffer* pkt, char** str, int num, struct edns_data* edns,
779         struct worker* worker)
780 {
781         int i;
782         unsigned int rd = LDNS_RD_WIRE(sldns_buffer_begin(pkt));
783         unsigned int cd = LDNS_CD_WIRE(sldns_buffer_begin(pkt));
784         sldns_buffer_clear(pkt);
785         sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip id */
786         sldns_buffer_write_u16(pkt, (uint16_t)(BIT_QR|BIT_RA));
787         if(rd) LDNS_RD_SET(sldns_buffer_begin(pkt));
788         if(cd) LDNS_CD_SET(sldns_buffer_begin(pkt));
789         sldns_buffer_write_u16(pkt, 1); /* qdcount */
790         sldns_buffer_write_u16(pkt, (uint16_t)num); /* ancount */
791         sldns_buffer_write_u16(pkt, 0); /* nscount */
792         sldns_buffer_write_u16(pkt, 0); /* arcount */
793         (void)query_dname_len(pkt); /* skip qname */
794         sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qtype */
795         sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qclass */
796         for(i=0; i<num; i++) {
797                 size_t len = strlen(str[i]);
798                 if(len>255) len=255; /* cap size of TXT record */
799                 sldns_buffer_write_u16(pkt, 0xc00c); /* compr ptr to query */
800                 sldns_buffer_write_u16(pkt, LDNS_RR_TYPE_TXT);
801                 sldns_buffer_write_u16(pkt, LDNS_RR_CLASS_CH);
802                 sldns_buffer_write_u32(pkt, 0); /* TTL */
803                 sldns_buffer_write_u16(pkt, sizeof(uint8_t) + len);
804                 sldns_buffer_write_u8(pkt, len);
805                 sldns_buffer_write(pkt, str[i], len);
806         }
807         sldns_buffer_flip(pkt);
808         edns->edns_version = EDNS_ADVERTISED_VERSION;
809         edns->udp_size = EDNS_ADVERTISED_SIZE;
810         edns->bits &= EDNS_DO;
811         if(!inplace_cb_reply_local_call(&worker->env, NULL, NULL, NULL,
812                 LDNS_RCODE_NOERROR, edns, worker->scratchpad))
813                         edns->opt_list = NULL;
814         if(sldns_buffer_capacity(pkt) >=
815                 sldns_buffer_limit(pkt)+calc_edns_field_size(edns))
816                 attach_edns_record(pkt, edns);
817 }
818
819 /** Reply with one string */
820 static void
821 chaos_replyonestr(sldns_buffer* pkt, const char* str, struct edns_data* edns,
822         struct worker* worker)
823 {
824         chaos_replystr(pkt, (char**)&str, 1, edns, worker);
825 }
826
827 /**
828  * Create CH class trustanchor answer.
829  * @param pkt: buffer
830  * @param edns: edns reply information.
831  * @param w: worker with scratch region.
832  */
833 static void
834 chaos_trustanchor(sldns_buffer* pkt, struct edns_data* edns, struct worker* w)
835 {
836 #define TA_RESPONSE_MAX_TXT 16 /* max number of TXT records */
837 #define TA_RESPONSE_MAX_TAGS 32 /* max number of tags printed per zone */
838         char* str_array[TA_RESPONSE_MAX_TXT];
839         uint16_t tags[TA_RESPONSE_MAX_TAGS];
840         int num = 0;
841         struct trust_anchor* ta;
842
843         if(!w->env.need_to_validate) {
844                 /* no validator module, reply no trustanchors */
845                 chaos_replystr(pkt, NULL, 0, edns, w);
846                 return;
847         }
848
849         /* fill the string with contents */
850         lock_basic_lock(&w->env.anchors->lock);
851         RBTREE_FOR(ta, struct trust_anchor*, w->env.anchors->tree) {
852                 char* str;
853                 size_t i, numtag, str_len = 255;
854                 if(num == TA_RESPONSE_MAX_TXT) continue;
855                 str = (char*)regional_alloc(w->scratchpad, str_len);
856                 if(!str) continue;
857                 lock_basic_lock(&ta->lock);
858                 numtag = anchor_list_keytags(ta, tags, TA_RESPONSE_MAX_TAGS);
859                 if(numtag == 0) {
860                         /* empty, insecure point */
861                         lock_basic_unlock(&ta->lock);
862                         continue;
863                 }
864                 str_array[num] = str;
865                 num++;
866
867                 /* spool name of anchor */
868                 (void)sldns_wire2str_dname_buf(ta->name, ta->namelen, str, str_len);
869                 str_len -= strlen(str); str += strlen(str);
870                 /* spool tags */
871                 for(i=0; i<numtag; i++) {
872                         snprintf(str, str_len, " %u", (unsigned)tags[i]);
873                         str_len -= strlen(str); str += strlen(str);
874                 }
875                 lock_basic_unlock(&ta->lock);
876         }
877         lock_basic_unlock(&w->env.anchors->lock);
878
879         chaos_replystr(pkt, str_array, num, edns, w);
880         regional_free_all(w->scratchpad);
881 }
882
883 /**
884  * Answer CH class queries.
885  * @param w: worker
886  * @param qinfo: query info. Pointer into packet buffer.
887  * @param edns: edns info from query.
888  * @param pkt: packet buffer.
889  * @return: true if a reply is to be sent.
890  */
891 static int
892 answer_chaos(struct worker* w, struct query_info* qinfo, 
893         struct edns_data* edns, sldns_buffer* pkt)
894 {
895         struct config_file* cfg = w->env.cfg;
896         if(qinfo->qtype != LDNS_RR_TYPE_ANY && qinfo->qtype != LDNS_RR_TYPE_TXT)
897                 return 0;
898         if(query_dname_compare(qinfo->qname, 
899                 (uint8_t*)"\002id\006server") == 0 ||
900                 query_dname_compare(qinfo->qname, 
901                 (uint8_t*)"\010hostname\004bind") == 0)
902         {
903                 if(cfg->hide_identity) 
904                         return 0;
905                 if(cfg->identity==NULL || cfg->identity[0]==0) {
906                         char buf[MAXHOSTNAMELEN+1];
907                         if (gethostname(buf, MAXHOSTNAMELEN) == 0) {
908                                 buf[MAXHOSTNAMELEN] = 0;
909                                 chaos_replyonestr(pkt, buf, edns, w);
910                         } else  {
911                                 log_err("gethostname: %s", strerror(errno));
912                                 chaos_replyonestr(pkt, "no hostname", edns, w);
913                         }
914                 }
915                 else    chaos_replyonestr(pkt, cfg->identity, edns, w);
916                 return 1;
917         }
918         if(query_dname_compare(qinfo->qname, 
919                 (uint8_t*)"\007version\006server") == 0 ||
920                 query_dname_compare(qinfo->qname, 
921                 (uint8_t*)"\007version\004bind") == 0)
922         {
923                 if(cfg->hide_version) 
924                         return 0;
925                 if(cfg->version==NULL || cfg->version[0]==0)
926                         chaos_replyonestr(pkt, PACKAGE_STRING, edns, w);
927                 else    chaos_replyonestr(pkt, cfg->version, edns, w);
928                 return 1;
929         }
930         if(query_dname_compare(qinfo->qname,
931                 (uint8_t*)"\013trustanchor\007unbound") == 0)
932         {
933                 if(cfg->hide_trustanchor)
934                         return 0;
935                 chaos_trustanchor(pkt, edns, w);
936                 return 1;
937         }
938
939         return 0;
940 }
941
942 static int
943 deny_refuse(struct comm_point* c, enum acl_access acl,
944         enum acl_access deny, enum acl_access refuse,
945         struct worker* worker, struct comm_reply* repinfo)
946 {
947         if(acl == deny) {
948                 comm_point_drop_reply(repinfo);
949                 if(worker->stats.extended)
950                         worker->stats.unwanted_queries++;
951                 return 0;
952         } else if(acl == refuse) {
953                 log_addr(VERB_ALGO, "refused query from",
954                         &repinfo->addr, repinfo->addrlen);
955                 log_buf(VERB_ALGO, "refuse", c->buffer);
956                 if(worker->stats.extended)
957                         worker->stats.unwanted_queries++;
958                 if(worker_check_request(c->buffer, worker) == -1) {
959                         comm_point_drop_reply(repinfo);
960                         return 0; /* discard this */
961                 }
962                 sldns_buffer_set_limit(c->buffer, LDNS_HEADER_SIZE);
963                 sldns_buffer_write_at(c->buffer, 4, 
964                         (uint8_t*)"\0\0\0\0\0\0\0\0", 8);
965                 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
966                 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 
967                         LDNS_RCODE_REFUSED);
968                 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
969                 sldns_buffer_flip(c->buffer);
970                 return 1;
971         }
972
973         return -1;
974 }
975
976 static int
977 deny_refuse_all(struct comm_point* c, enum acl_access acl,
978         struct worker* worker, struct comm_reply* repinfo)
979 {
980         return deny_refuse(c, acl, acl_deny, acl_refuse, worker, repinfo);
981 }
982
983 static int
984 deny_refuse_non_local(struct comm_point* c, enum acl_access acl,
985         struct worker* worker, struct comm_reply* repinfo)
986 {
987         return deny_refuse(c, acl, acl_deny_non_local, acl_refuse_non_local, worker, repinfo);
988 }
989
990 int 
991 worker_handle_request(struct comm_point* c, void* arg, int error,
992         struct comm_reply* repinfo)
993 {
994         struct worker* worker = (struct worker*)arg;
995         int ret;
996         hashvalue_type h;
997         struct lruhash_entry* e;
998         struct query_info qinfo;
999         struct edns_data edns;
1000         enum acl_access acl;
1001         struct acl_addr* acladdr;
1002         int rc = 0;
1003         int need_drop = 0;
1004         /* We might have to chase a CNAME chain internally, in which case
1005          * we'll have up to two replies and combine them to build a complete
1006          * answer.  These variables control this case. */
1007         struct ub_packed_rrset_key* alias_rrset = NULL;
1008         struct reply_info* partial_rep = NULL;
1009         struct query_info* lookup_qinfo = &qinfo;
1010         struct query_info qinfo_tmp; /* placeholdoer for lookup_qinfo */
1011         struct respip_client_info* cinfo = NULL, cinfo_tmp;
1012
1013         if(error != NETEVENT_NOERROR) {
1014                 /* some bad tcp query DNS formats give these error calls */
1015                 verbose(VERB_ALGO, "handle request called with err=%d", error);
1016                 return 0;
1017         }
1018 #ifdef USE_DNSCRYPT
1019         repinfo->max_udp_size = worker->daemon->cfg->max_udp_size;
1020         if(!dnsc_handle_curved_request(worker->daemon->dnscenv, repinfo)) {
1021                 worker->stats.num_query_dnscrypt_crypted_malformed++;
1022                 return 0;
1023         }
1024         if(c->dnscrypt && !repinfo->is_dnscrypted) {
1025                 char buf[LDNS_MAX_DOMAINLEN+1];
1026                 /* Check if this is unencrypted and asking for certs */
1027                 if(worker_check_request(c->buffer, worker) != 0) {
1028                         verbose(VERB_ALGO,
1029                                 "dnscrypt: worker check request: bad query.");
1030                         log_addr(VERB_CLIENT,"from",&repinfo->addr,
1031                                 repinfo->addrlen);
1032                         comm_point_drop_reply(repinfo);
1033                         return 0;
1034                 }
1035                 if(!query_info_parse(&qinfo, c->buffer)) {
1036                         verbose(VERB_ALGO,
1037                                 "dnscrypt: worker parse request: formerror.");
1038                         log_addr(VERB_CLIENT, "from", &repinfo->addr,
1039                                 repinfo->addrlen);
1040                         comm_point_drop_reply(repinfo);
1041                         return 0;
1042                 }
1043                 dname_str(qinfo.qname, buf);
1044                 if(!(qinfo.qtype == LDNS_RR_TYPE_TXT &&
1045                         strcasecmp(buf,
1046                         worker->daemon->dnscenv->provider_name) == 0)) {
1047                         verbose(VERB_ALGO,
1048                                 "dnscrypt: not TXT %s. Receive: %s %s",
1049                                 worker->daemon->dnscenv->provider_name,
1050                                 sldns_rr_descript(qinfo.qtype)->_name,
1051                                 buf);
1052                         comm_point_drop_reply(repinfo);
1053                         worker->stats.num_query_dnscrypt_cleartext++;
1054                         return 0;
1055                 }
1056                 worker->stats.num_query_dnscrypt_cert++;
1057                 sldns_buffer_rewind(c->buffer);
1058         } else if(c->dnscrypt && repinfo->is_dnscrypted) {
1059                 worker->stats.num_query_dnscrypt_crypted++;
1060         }
1061 #endif
1062 #ifdef USE_DNSTAP
1063         if(worker->dtenv.log_client_query_messages)
1064                 dt_msg_send_client_query(&worker->dtenv, &repinfo->addr, c->type,
1065                         c->buffer);
1066 #endif
1067         acladdr = acl_addr_lookup(worker->daemon->acl, &repinfo->addr, 
1068                 repinfo->addrlen);
1069         acl = acl_get_control(acladdr);
1070         if((ret=deny_refuse_all(c, acl, worker, repinfo)) != -1)
1071         {
1072                 if(ret == 1)
1073                         goto send_reply;
1074                 return ret;
1075         }
1076         if((ret=worker_check_request(c->buffer, worker)) != 0) {
1077                 verbose(VERB_ALGO, "worker check request: bad query.");
1078                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1079                 if(ret != -1) {
1080                         LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1081                         LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
1082                         return 1;
1083                 }
1084                 comm_point_drop_reply(repinfo);
1085                 return 0;
1086         }
1087
1088         worker->stats.num_queries++;
1089
1090         /* check if this query should be dropped based on source ip rate limiting */
1091         if(!infra_ip_ratelimit_inc(worker->env.infra_cache, repinfo,
1092                         *worker->env.now)) {
1093                 /* See if we are passed through with slip factor */
1094                 if(worker->env.cfg->ip_ratelimit_factor != 0 &&
1095                         ub_random_max(worker->env.rnd,
1096                                                   worker->env.cfg->ip_ratelimit_factor) == 1) {
1097
1098                         char addrbuf[128];
1099                         addr_to_str(&repinfo->addr, repinfo->addrlen,
1100                                                 addrbuf, sizeof(addrbuf));
1101                   verbose(VERB_OPS, "ip_ratelimit allowed through for ip address %s ",
1102                                   addrbuf);
1103                 } else {
1104                         worker->stats.num_queries_ip_ratelimited++;
1105                         comm_point_drop_reply(repinfo);
1106                         return 0;
1107                 }
1108         }
1109
1110         /* see if query is in the cache */
1111         if(!query_info_parse(&qinfo, c->buffer)) {
1112                 verbose(VERB_ALGO, "worker parse request: formerror.");
1113                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1114                 if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) {
1115                         comm_point_drop_reply(repinfo);
1116                         return 0;
1117                 }
1118                 sldns_buffer_rewind(c->buffer);
1119                 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1120                 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 
1121                         LDNS_RCODE_FORMERR);
1122                 server_stats_insrcode(&worker->stats, c->buffer);
1123                 goto send_reply;
1124         }
1125         if(worker->env.cfg->log_queries) {
1126                 char ip[128];
1127                 addr_to_str(&repinfo->addr, repinfo->addrlen, ip, sizeof(ip));
1128                 log_nametypeclass(0, ip, qinfo.qname, qinfo.qtype, qinfo.qclass);
1129         }
1130         if(qinfo.qtype == LDNS_RR_TYPE_AXFR || 
1131                 qinfo.qtype == LDNS_RR_TYPE_IXFR) {
1132                 verbose(VERB_ALGO, "worker request: refused zone transfer.");
1133                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1134                 sldns_buffer_rewind(c->buffer);
1135                 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1136                 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 
1137                         LDNS_RCODE_REFUSED);
1138                 if(worker->stats.extended) {
1139                         worker->stats.qtype[qinfo.qtype]++;
1140                         server_stats_insrcode(&worker->stats, c->buffer);
1141                 }
1142                 goto send_reply;
1143         }
1144         if(qinfo.qtype == LDNS_RR_TYPE_OPT || 
1145                 qinfo.qtype == LDNS_RR_TYPE_TSIG ||
1146                 qinfo.qtype == LDNS_RR_TYPE_TKEY ||
1147                 qinfo.qtype == LDNS_RR_TYPE_MAILA ||
1148                 qinfo.qtype == LDNS_RR_TYPE_MAILB ||
1149                 (qinfo.qtype >= 128 && qinfo.qtype <= 248)) {
1150                 verbose(VERB_ALGO, "worker request: formerror for meta-type.");
1151                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1152                 if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) {
1153                         comm_point_drop_reply(repinfo);
1154                         return 0;
1155                 }
1156                 sldns_buffer_rewind(c->buffer);
1157                 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1158                 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 
1159                         LDNS_RCODE_FORMERR);
1160                 if(worker->stats.extended) {
1161                         worker->stats.qtype[qinfo.qtype]++;
1162                         server_stats_insrcode(&worker->stats, c->buffer);
1163                 }
1164                 goto send_reply;
1165         }
1166         if((ret=parse_edns_from_pkt(c->buffer, &edns, worker->scratchpad)) != 0) {
1167                 struct edns_data reply_edns;
1168                 verbose(VERB_ALGO, "worker parse edns: formerror.");
1169                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1170                 memset(&reply_edns, 0, sizeof(reply_edns));
1171                 reply_edns.edns_present = 1;
1172                 reply_edns.udp_size = EDNS_ADVERTISED_SIZE;
1173                 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
1174                 error_encode(c->buffer, ret, &qinfo,
1175                         *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1176                         sldns_buffer_read_u16_at(c->buffer, 2), &reply_edns);
1177                 regional_free_all(worker->scratchpad);
1178                 server_stats_insrcode(&worker->stats, c->buffer);
1179                 goto send_reply;
1180         }
1181         if(edns.edns_present && edns.edns_version != 0) {
1182                 edns.ext_rcode = (uint8_t)(EDNS_RCODE_BADVERS>>4);
1183                 edns.edns_version = EDNS_ADVERTISED_VERSION;
1184                 edns.udp_size = EDNS_ADVERTISED_SIZE;
1185                 edns.bits &= EDNS_DO;
1186                 edns.opt_list = NULL;
1187                 verbose(VERB_ALGO, "query with bad edns version.");
1188                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1189                 error_encode(c->buffer, EDNS_RCODE_BADVERS&0xf, &qinfo,
1190                         *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1191                         sldns_buffer_read_u16_at(c->buffer, 2), NULL);
1192                 if(sldns_buffer_capacity(c->buffer) >=
1193                         sldns_buffer_limit(c->buffer)+calc_edns_field_size(&edns))
1194                         attach_edns_record(c->buffer, &edns);
1195                 regional_free_all(worker->scratchpad);
1196                 goto send_reply;
1197         }
1198         if(edns.edns_present && edns.udp_size < NORMAL_UDP_SIZE &&
1199                 worker->daemon->cfg->harden_short_bufsize) {
1200                 verbose(VERB_QUERY, "worker request: EDNS bufsize %d ignored",
1201                         (int)edns.udp_size);
1202                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1203                 edns.udp_size = NORMAL_UDP_SIZE;
1204         }
1205         if(edns.udp_size > worker->daemon->cfg->max_udp_size &&
1206                 c->type == comm_udp) {
1207                 verbose(VERB_QUERY,
1208                         "worker request: max UDP reply size modified"
1209                         " (%d to max-udp-size)", (int)edns.udp_size);
1210                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1211                 edns.udp_size = worker->daemon->cfg->max_udp_size;
1212         }
1213         if(edns.udp_size < LDNS_HEADER_SIZE) {
1214                 verbose(VERB_ALGO, "worker request: edns is too small.");
1215                 log_addr(VERB_CLIENT, "from", &repinfo->addr, repinfo->addrlen);
1216                 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1217                 LDNS_TC_SET(sldns_buffer_begin(c->buffer));
1218                 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 
1219                         LDNS_RCODE_SERVFAIL);
1220                 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
1221                 sldns_buffer_write_at(c->buffer, 4, 
1222                         (uint8_t*)"\0\0\0\0\0\0\0\0", 8);
1223                 sldns_buffer_flip(c->buffer);
1224                 regional_free_all(worker->scratchpad);
1225                 goto send_reply;
1226         }
1227         if(worker->stats.extended)
1228                 server_stats_insquery(&worker->stats, c, qinfo.qtype,
1229                         qinfo.qclass, &edns, repinfo);
1230         if(c->type != comm_udp)
1231                 edns.udp_size = 65535; /* max size for TCP replies */
1232         if(qinfo.qclass == LDNS_RR_CLASS_CH && answer_chaos(worker, &qinfo,
1233                 &edns, c->buffer)) {
1234                 server_stats_insrcode(&worker->stats, c->buffer);
1235                 regional_free_all(worker->scratchpad);
1236                 goto send_reply;
1237         }
1238         if(local_zones_answer(worker->daemon->local_zones, &worker->env, &qinfo,
1239                 &edns, c->buffer, worker->scratchpad, repinfo, acladdr->taglist,
1240                 acladdr->taglen, acladdr->tag_actions,
1241                 acladdr->tag_actions_size, acladdr->tag_datas,
1242                 acladdr->tag_datas_size, worker->daemon->cfg->tagname,
1243                 worker->daemon->cfg->num_tags, acladdr->view)) {
1244                 regional_free_all(worker->scratchpad);
1245                 if(sldns_buffer_limit(c->buffer) == 0) {
1246                         comm_point_drop_reply(repinfo);
1247                         return 0;
1248                 }
1249                 server_stats_insrcode(&worker->stats, c->buffer);
1250                 goto send_reply;
1251         }
1252
1253         /* We've looked in our local zones. If the answer isn't there, we
1254          * might need to bail out based on ACLs now. */
1255         if((ret=deny_refuse_non_local(c, acl, worker, repinfo)) != -1)
1256         {
1257                 regional_free_all(worker->scratchpad);
1258                 if(ret == 1)
1259                         goto send_reply;
1260                 return ret;
1261         }
1262
1263         /* If this request does not have the recursion bit set, verify
1264          * ACLs allow the snooping. */
1265         if(!(LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) &&
1266                 acl != acl_allow_snoop ) {
1267                 sldns_buffer_set_limit(c->buffer, LDNS_HEADER_SIZE);
1268                 sldns_buffer_write_at(c->buffer, 4, 
1269                         (uint8_t*)"\0\0\0\0\0\0\0\0", 8);
1270                 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1271                 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 
1272                         LDNS_RCODE_REFUSED);
1273                 sldns_buffer_flip(c->buffer);
1274                 regional_free_all(worker->scratchpad);
1275                 server_stats_insrcode(&worker->stats, c->buffer);
1276                 log_addr(VERB_ALGO, "refused nonrec (cache snoop) query from",
1277                         &repinfo->addr, repinfo->addrlen);
1278                 goto send_reply;
1279         }
1280
1281         /* If we've found a local alias, replace the qname with the alias
1282          * target before resolving it. */
1283         if(qinfo.local_alias) {
1284                 struct ub_packed_rrset_key* rrset = qinfo.local_alias->rrset;
1285                 struct packed_rrset_data* d = rrset->entry.data;
1286
1287                 /* Sanity check: our current implementation only supports
1288                  * a single CNAME RRset as a local alias. */
1289                 if(qinfo.local_alias->next ||
1290                         rrset->rk.type != htons(LDNS_RR_TYPE_CNAME) ||
1291                         d->count != 1) {
1292                         log_err("assumption failure: unexpected local alias");
1293                         regional_free_all(worker->scratchpad);
1294                         return 0; /* drop it */
1295                 }
1296                 qinfo.qname = d->rr_data[0] + 2;
1297                 qinfo.qname_len = d->rr_len[0] - 2;
1298         }
1299
1300         /* If we may apply IP-based actions to the answer, build the client
1301          * information.  As this can be expensive, skip it if there is
1302          * absolutely no possibility of it. */
1303         if(worker->daemon->use_response_ip &&
1304                 (qinfo.qtype == LDNS_RR_TYPE_A ||
1305                 qinfo.qtype == LDNS_RR_TYPE_AAAA ||
1306                 qinfo.qtype == LDNS_RR_TYPE_ANY)) {
1307                 cinfo_tmp.taglist = acladdr->taglist;
1308                 cinfo_tmp.taglen = acladdr->taglen;
1309                 cinfo_tmp.tag_actions = acladdr->tag_actions;
1310                 cinfo_tmp.tag_actions_size = acladdr->tag_actions_size;
1311                 cinfo_tmp.tag_datas = acladdr->tag_datas;
1312                 cinfo_tmp.tag_datas_size = acladdr->tag_datas_size;
1313                 cinfo_tmp.view = acladdr->view;
1314                 cinfo_tmp.respip_set = worker->daemon->respip_set;
1315                 cinfo = &cinfo_tmp;
1316         }
1317
1318 lookup_cache:
1319         /* Lookup the cache.  In case we chase an intermediate CNAME chain
1320          * this is a two-pass operation, and lookup_qinfo is different for
1321          * each pass.  We should still pass the original qinfo to
1322          * answer_from_cache(), however, since it's used to build the reply. */
1323         if(!edns_bypass_cache_stage(edns.opt_list, &worker->env)) {
1324                 h = query_info_hash(lookup_qinfo, sldns_buffer_read_u16_at(c->buffer, 2));
1325                 if((e=slabhash_lookup(worker->env.msg_cache, h, lookup_qinfo, 0))) {
1326                         /* answer from cache - we have acquired a readlock on it */
1327                         if(answer_from_cache(worker, &qinfo, 
1328                                 cinfo, &need_drop, &alias_rrset, &partial_rep,
1329                                 (struct reply_info*)e->data, 
1330                                 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer), 
1331                                 sldns_buffer_read_u16_at(c->buffer, 2), repinfo, 
1332                                 &edns)) {
1333                                 /* prefetch it if the prefetch TTL expired.
1334                                  * Note that if there is more than one pass
1335                                  * its qname must be that used for cache
1336                                  * lookup. */
1337                                 if((worker->env.cfg->prefetch || worker->env.cfg->serve_expired)
1338                                         && *worker->env.now >=
1339                                         ((struct reply_info*)e->data)->prefetch_ttl) {
1340                                         time_t leeway = ((struct reply_info*)e->
1341                                                 data)->ttl - *worker->env.now;
1342                                         if(((struct reply_info*)e->data)->ttl
1343                                                 < *worker->env.now)
1344                                                 leeway = 0;
1345                                         lock_rw_unlock(&e->lock);
1346                                         reply_and_prefetch(worker, lookup_qinfo,
1347                                                 sldns_buffer_read_u16_at(c->buffer, 2),
1348                                                 repinfo, leeway);
1349                                         if(!partial_rep) {
1350                                                 rc = 0;
1351                                                 regional_free_all(worker->scratchpad);
1352                                                 goto send_reply_rc;
1353                                         }
1354                                 } else if(!partial_rep) {
1355                                         lock_rw_unlock(&e->lock);
1356                                         regional_free_all(worker->scratchpad);
1357                                         goto send_reply;
1358                                 }
1359                                 /* We've found a partial reply ending with an
1360                                  * alias.  Replace the lookup qinfo for the
1361                                  * alias target and lookup the cache again to
1362                                  * (possibly) complete the reply.  As we're
1363                                  * passing the "base" reply, there will be no
1364                                  * more alias chasing. */
1365                                 lock_rw_unlock(&e->lock);
1366                                 memset(&qinfo_tmp, 0, sizeof(qinfo_tmp));
1367                                 get_cname_target(alias_rrset, &qinfo_tmp.qname,
1368                                         &qinfo_tmp.qname_len);
1369                                 if(!qinfo_tmp.qname) {
1370                                         log_err("unexpected: invalid answer alias");
1371                                         regional_free_all(worker->scratchpad);
1372                                         return 0; /* drop query */
1373                                 }
1374                                 qinfo_tmp.qtype = qinfo.qtype;
1375                                 qinfo_tmp.qclass = qinfo.qclass;
1376                                 lookup_qinfo = &qinfo_tmp;
1377                                 goto lookup_cache;
1378                         }
1379                         verbose(VERB_ALGO, "answer from the cache failed");
1380                         lock_rw_unlock(&e->lock);
1381                 }
1382                 if(!LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) {
1383                         if(answer_norec_from_cache(worker, &qinfo,
1384                                 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer), 
1385                                 sldns_buffer_read_u16_at(c->buffer, 2), repinfo, 
1386                                 &edns)) {
1387                                 regional_free_all(worker->scratchpad);
1388                                 goto send_reply;
1389                         }
1390                         verbose(VERB_ALGO, "answer norec from cache -- "
1391                                 "need to validate or not primed");
1392                 }
1393         }
1394         sldns_buffer_rewind(c->buffer);
1395         server_stats_querymiss(&worker->stats, worker);
1396
1397         if(verbosity >= VERB_CLIENT) {
1398                 if(c->type == comm_udp)
1399                         log_addr(VERB_CLIENT, "udp request from",
1400                                 &repinfo->addr, repinfo->addrlen);
1401                 else    log_addr(VERB_CLIENT, "tcp request from",
1402                                 &repinfo->addr, repinfo->addrlen);
1403         }
1404
1405         /* grab a work request structure for this new request */
1406         mesh_new_client(worker->env.mesh, &qinfo, cinfo,
1407                 sldns_buffer_read_u16_at(c->buffer, 2),
1408                 &edns, repinfo, *(uint16_t*)(void *)sldns_buffer_begin(c->buffer));
1409         regional_free_all(worker->scratchpad);
1410         worker_mem_report(worker, NULL);
1411         return 0;
1412
1413 send_reply:
1414         rc = 1;
1415 send_reply_rc:
1416         if(need_drop) {
1417                 comm_point_drop_reply(repinfo);
1418                 return 0;
1419         }
1420 #ifdef USE_DNSTAP
1421         if(worker->dtenv.log_client_response_messages)
1422                 dt_msg_send_client_response(&worker->dtenv, &repinfo->addr,
1423                         c->type, c->buffer);
1424 #endif
1425         if(worker->env.cfg->log_replies)
1426         {
1427                 struct timeval tv = {0, 0};
1428                 log_reply_info(0, &qinfo, &repinfo->addr, repinfo->addrlen,
1429                         tv, 1, c->buffer);
1430         }
1431 #ifdef USE_DNSCRYPT
1432         if(!dnsc_handle_uncurved_request(repinfo)) {
1433                 return 0;
1434         }
1435 #endif
1436         return rc;
1437 }
1438
1439 void 
1440 worker_sighandler(int sig, void* arg)
1441 {
1442         /* note that log, print, syscalls here give race conditions. 
1443          * And cause hangups if the log-lock is held by the application. */
1444         struct worker* worker = (struct worker*)arg;
1445         switch(sig) {
1446 #ifdef SIGHUP
1447                 case SIGHUP:
1448                         comm_base_exit(worker->base);
1449                         break;
1450 #endif
1451                 case SIGINT:
1452                         worker->need_to_exit = 1;
1453                         comm_base_exit(worker->base);
1454                         break;
1455 #ifdef SIGQUIT
1456                 case SIGQUIT:
1457                         worker->need_to_exit = 1;
1458                         comm_base_exit(worker->base);
1459                         break;
1460 #endif
1461                 case SIGTERM:
1462                         worker->need_to_exit = 1;
1463                         comm_base_exit(worker->base);
1464                         break;
1465                 default:
1466                         /* unknown signal, ignored */
1467                         break;
1468         }
1469 }
1470
1471 /** restart statistics timer for worker, if enabled */
1472 static void
1473 worker_restart_timer(struct worker* worker)
1474 {
1475         if(worker->env.cfg->stat_interval > 0) {
1476                 struct timeval tv;
1477 #ifndef S_SPLINT_S
1478                 tv.tv_sec = worker->env.cfg->stat_interval;
1479                 tv.tv_usec = 0;
1480 #endif
1481                 comm_timer_set(worker->stat_timer, &tv);
1482         }
1483 }
1484
1485 void worker_stat_timer_cb(void* arg)
1486 {
1487         struct worker* worker = (struct worker*)arg;
1488         server_stats_log(&worker->stats, worker, worker->thread_num);
1489         mesh_stats(worker->env.mesh, "mesh has");
1490         worker_mem_report(worker, NULL);
1491         /* SHM is enabled, process data to SHM */
1492         if (worker->daemon->cfg->shm_enable) {
1493                 shm_main_run(worker);
1494         }
1495         if(!worker->daemon->cfg->stat_cumulative) {
1496                 worker_stats_clear(worker);
1497         }
1498         /* start next timer */
1499         worker_restart_timer(worker);
1500 }
1501
1502 void worker_probe_timer_cb(void* arg)
1503 {
1504         struct worker* worker = (struct worker*)arg;
1505         struct timeval tv;
1506 #ifndef S_SPLINT_S
1507         tv.tv_sec = (time_t)autr_probe_timer(&worker->env);
1508         tv.tv_usec = 0;
1509 #endif
1510         if(tv.tv_sec != 0)
1511                 comm_timer_set(worker->env.probe_timer, &tv);
1512 }
1513
1514 struct worker* 
1515 worker_create(struct daemon* daemon, int id, int* ports, int n)
1516 {
1517         unsigned int seed;
1518         struct worker* worker = (struct worker*)calloc(1, 
1519                 sizeof(struct worker));
1520         if(!worker) 
1521                 return NULL;
1522         worker->numports = n;
1523         worker->ports = (int*)memdup(ports, sizeof(int)*n);
1524         if(!worker->ports) {
1525                 free(worker);
1526                 return NULL;
1527         }
1528         worker->daemon = daemon;
1529         worker->thread_num = id;
1530         if(!(worker->cmd = tube_create())) {
1531                 free(worker->ports);
1532                 free(worker);
1533                 return NULL;
1534         }
1535         /* create random state here to avoid locking trouble in RAND_bytes */
1536         seed = (unsigned int)time(NULL) ^ (unsigned int)getpid() ^
1537                 (((unsigned int)worker->thread_num)<<17);
1538                 /* shift thread_num so it does not match out pid bits */
1539         if(!(worker->rndstate = ub_initstate(seed, daemon->rand))) {
1540                 seed = 0;
1541                 log_err("could not init random numbers.");
1542                 tube_delete(worker->cmd);
1543                 free(worker->ports);
1544                 free(worker);
1545                 return NULL;
1546         }
1547         seed = 0;
1548 #ifdef USE_DNSTAP
1549         if(daemon->cfg->dnstap) {
1550                 log_assert(daemon->dtenv != NULL);
1551                 memcpy(&worker->dtenv, daemon->dtenv, sizeof(struct dt_env));
1552                 if(!dt_init(&worker->dtenv))
1553                         fatal_exit("dt_init failed");
1554         }
1555 #endif
1556         return worker;
1557 }
1558
1559 int
1560 worker_init(struct worker* worker, struct config_file *cfg, 
1561         struct listen_port* ports, int do_sigs)
1562 {
1563 #ifdef USE_DNSTAP
1564         struct dt_env* dtenv = &worker->dtenv;
1565 #else
1566         void* dtenv = NULL;
1567 #endif
1568         worker->need_to_exit = 0;
1569         worker->base = comm_base_create(do_sigs);
1570         if(!worker->base) {
1571                 log_err("could not create event handling base");
1572                 worker_delete(worker);
1573                 return 0;
1574         }
1575         comm_base_set_slow_accept_handlers(worker->base, &worker_stop_accept,
1576                 &worker_start_accept, worker);
1577         if(do_sigs) {
1578 #ifdef SIGHUP
1579                 ub_thread_sig_unblock(SIGHUP);
1580 #endif
1581                 ub_thread_sig_unblock(SIGINT);
1582 #ifdef SIGQUIT
1583                 ub_thread_sig_unblock(SIGQUIT);
1584 #endif
1585                 ub_thread_sig_unblock(SIGTERM);
1586 #ifndef LIBEVENT_SIGNAL_PROBLEM
1587                 worker->comsig = comm_signal_create(worker->base, 
1588                         worker_sighandler, worker);
1589                 if(!worker->comsig 
1590 #ifdef SIGHUP
1591                         || !comm_signal_bind(worker->comsig, SIGHUP)
1592 #endif
1593 #ifdef SIGQUIT
1594                         || !comm_signal_bind(worker->comsig, SIGQUIT)
1595 #endif
1596                         || !comm_signal_bind(worker->comsig, SIGTERM)
1597                         || !comm_signal_bind(worker->comsig, SIGINT)) {
1598                         log_err("could not create signal handlers");
1599                         worker_delete(worker);
1600                         return 0;
1601                 }
1602 #endif /* LIBEVENT_SIGNAL_PROBLEM */
1603                 if(!daemon_remote_open_accept(worker->daemon->rc, 
1604                         worker->daemon->rc_ports, worker)) {
1605                         worker_delete(worker);
1606                         return 0;
1607                 }
1608 #ifdef UB_ON_WINDOWS
1609                 wsvc_setup_worker(worker);
1610 #endif /* UB_ON_WINDOWS */
1611         } else { /* !do_sigs */
1612                 worker->comsig = NULL;
1613         }
1614         worker->front = listen_create(worker->base, ports,
1615                 cfg->msg_buffer_size, (int)cfg->incoming_num_tcp, 
1616                 worker->daemon->listen_sslctx, dtenv, worker_handle_request,
1617                 worker);
1618         if(!worker->front) {
1619                 log_err("could not create listening sockets");
1620                 worker_delete(worker);
1621                 return 0;
1622         }
1623         worker->back = outside_network_create(worker->base,
1624                 cfg->msg_buffer_size, (size_t)cfg->outgoing_num_ports, 
1625                 cfg->out_ifs, cfg->num_out_ifs, cfg->do_ip4, cfg->do_ip6, 
1626                 cfg->do_tcp?cfg->outgoing_num_tcp:0, 
1627                 worker->daemon->env->infra_cache, worker->rndstate,
1628                 cfg->use_caps_bits_for_id, worker->ports, worker->numports,
1629                 cfg->unwanted_threshold, cfg->outgoing_tcp_mss,
1630                 &worker_alloc_cleanup, worker,
1631                 cfg->do_udp, worker->daemon->connect_sslctx, cfg->delay_close,
1632                 dtenv);
1633         if(!worker->back) {
1634                 log_err("could not create outgoing sockets");
1635                 worker_delete(worker);
1636                 return 0;
1637         }
1638         /* start listening to commands */
1639         if(!tube_setup_bg_listen(worker->cmd, worker->base,
1640                 &worker_handle_control_cmd, worker)) {
1641                 log_err("could not create control compt.");
1642                 worker_delete(worker);
1643                 return 0;
1644         }
1645         worker->stat_timer = comm_timer_create(worker->base, 
1646                 worker_stat_timer_cb, worker);
1647         if(!worker->stat_timer) {
1648                 log_err("could not create statistics timer");
1649         }
1650
1651         /* we use the msg_buffer_size as a good estimate for what the 
1652          * user wants for memory usage sizes */
1653         worker->scratchpad = regional_create_custom(cfg->msg_buffer_size);
1654         if(!worker->scratchpad) {
1655                 log_err("malloc failure");
1656                 worker_delete(worker);
1657                 return 0;
1658         }
1659
1660         server_stats_init(&worker->stats, cfg);
1661         alloc_init(&worker->alloc, &worker->daemon->superalloc, 
1662                 worker->thread_num);
1663         alloc_set_id_cleanup(&worker->alloc, &worker_alloc_cleanup, worker);
1664         worker->env = *worker->daemon->env;
1665         comm_base_timept(worker->base, &worker->env.now, &worker->env.now_tv);
1666         if(worker->thread_num == 0)
1667                 log_set_time(worker->env.now);
1668         worker->env.worker = worker;
1669         worker->env.send_query = &worker_send_query;
1670         worker->env.alloc = &worker->alloc;
1671         worker->env.rnd = worker->rndstate;
1672         worker->env.scratch = worker->scratchpad;
1673         worker->env.mesh = mesh_create(&worker->daemon->mods, &worker->env);
1674         worker->env.detach_subs = &mesh_detach_subs;
1675         worker->env.attach_sub = &mesh_attach_sub;
1676         worker->env.add_sub = &mesh_add_sub;
1677         worker->env.kill_sub = &mesh_state_delete;
1678         worker->env.detect_cycle = &mesh_detect_cycle;
1679         worker->env.scratch_buffer = sldns_buffer_new(cfg->msg_buffer_size);
1680         if(!(worker->env.fwds = forwards_create()) ||
1681                 !forwards_apply_cfg(worker->env.fwds, cfg)) {
1682                 log_err("Could not set forward zones");
1683                 worker_delete(worker);
1684                 return 0;
1685         }
1686         if(!(worker->env.hints = hints_create()) ||
1687                 !hints_apply_cfg(worker->env.hints, cfg)) {
1688                 log_err("Could not set root or stub hints");
1689                 worker_delete(worker);
1690                 return 0;
1691         }
1692         /* one probe timer per process -- if we have 5011 anchors */
1693         if(autr_get_num_anchors(worker->env.anchors) > 0
1694 #ifndef THREADS_DISABLED
1695                 && worker->thread_num == 0
1696 #endif
1697                 ) {
1698                 struct timeval tv;
1699                 tv.tv_sec = 0;
1700                 tv.tv_usec = 0;
1701                 worker->env.probe_timer = comm_timer_create(worker->base,
1702                         worker_probe_timer_cb, worker);
1703                 if(!worker->env.probe_timer) {
1704                         log_err("could not create 5011-probe timer");
1705                 } else {
1706                         /* let timer fire, then it can reset itself */
1707                         comm_timer_set(worker->env.probe_timer, &tv);
1708                 }
1709         }
1710         if(!worker->env.mesh || !worker->env.scratch_buffer) {
1711                 worker_delete(worker);
1712                 return 0;
1713         }
1714         worker_mem_report(worker, NULL);
1715         /* if statistics enabled start timer */
1716         if(worker->env.cfg->stat_interval > 0) {
1717                 verbose(VERB_ALGO, "set statistics interval %d secs", 
1718                         worker->env.cfg->stat_interval);
1719                 worker_restart_timer(worker);
1720         }
1721         return 1;
1722 }
1723
1724 void 
1725 worker_work(struct worker* worker)
1726 {
1727         comm_base_dispatch(worker->base);
1728 }
1729
1730 void 
1731 worker_delete(struct worker* worker)
1732 {
1733         if(!worker) 
1734                 return;
1735         if(worker->env.mesh && verbosity >= VERB_OPS) {
1736                 server_stats_log(&worker->stats, worker, worker->thread_num);
1737                 mesh_stats(worker->env.mesh, "mesh has");
1738                 worker_mem_report(worker, NULL);
1739         }
1740         outside_network_quit_prepare(worker->back);
1741         mesh_delete(worker->env.mesh);
1742         sldns_buffer_free(worker->env.scratch_buffer);
1743         forwards_delete(worker->env.fwds);
1744         hints_delete(worker->env.hints);
1745         listen_delete(worker->front);
1746         outside_network_delete(worker->back);
1747         comm_signal_delete(worker->comsig);
1748         tube_delete(worker->cmd);
1749         comm_timer_delete(worker->stat_timer);
1750         comm_timer_delete(worker->env.probe_timer);
1751         free(worker->ports);
1752         if(worker->thread_num == 0) {
1753                 log_set_time(NULL);
1754 #ifdef UB_ON_WINDOWS
1755                 wsvc_desetup_worker(worker);
1756 #endif /* UB_ON_WINDOWS */
1757         }
1758         comm_base_delete(worker->base);
1759         ub_randfree(worker->rndstate);
1760         alloc_clear(&worker->alloc);
1761         regional_destroy(worker->scratchpad);
1762         free(worker);
1763 }
1764
1765 struct outbound_entry*
1766 worker_send_query(struct query_info* qinfo, uint16_t flags, int dnssec,
1767         int want_dnssec, int nocaps, struct sockaddr_storage* addr,
1768         socklen_t addrlen, uint8_t* zone, size_t zonelen, int ssl_upstream,
1769         struct module_qstate* q)
1770 {
1771         struct worker* worker = q->env->worker;
1772         struct outbound_entry* e = (struct outbound_entry*)regional_alloc(
1773                 q->region, sizeof(*e));
1774         if(!e) 
1775                 return NULL;
1776         e->qstate = q;
1777         e->qsent = outnet_serviced_query(worker->back, qinfo, flags, dnssec,
1778                 want_dnssec, nocaps, q->env->cfg->tcp_upstream,
1779                 ssl_upstream, addr, addrlen, zone, zonelen, q,
1780                 worker_handle_service_reply, e, worker->back->udp_buff, q->env);
1781         if(!e->qsent) {
1782                 return NULL;
1783         }
1784         return e;
1785 }
1786
1787 void 
1788 worker_alloc_cleanup(void* arg)
1789 {
1790         struct worker* worker = (struct worker*)arg;
1791         slabhash_clear(&worker->env.rrset_cache->table);
1792         slabhash_clear(worker->env.msg_cache);
1793 }
1794
1795 void worker_stats_clear(struct worker* worker)
1796 {
1797         server_stats_init(&worker->stats, worker->env.cfg);
1798         mesh_stats_clear(worker->env.mesh);
1799         worker->back->unwanted_replies = 0;
1800         worker->back->num_tcp_outgoing = 0;
1801 }
1802
1803 void worker_start_accept(void* arg)
1804 {
1805         struct worker* worker = (struct worker*)arg;
1806         listen_start_accept(worker->front);
1807         if(worker->thread_num == 0)
1808                 daemon_remote_start_accept(worker->daemon->rc);
1809 }
1810
1811 void worker_stop_accept(void* arg)
1812 {
1813         struct worker* worker = (struct worker*)arg;
1814         listen_stop_accept(worker->front);
1815         if(worker->thread_num == 0)
1816                 daemon_remote_stop_accept(worker->daemon->rc);
1817 }
1818
1819 /* --- fake callbacks for fptr_wlist to work --- */
1820 struct outbound_entry* libworker_send_query(
1821         struct query_info* ATTR_UNUSED(qinfo),
1822         uint16_t ATTR_UNUSED(flags), int ATTR_UNUSED(dnssec),
1823         int ATTR_UNUSED(want_dnssec), int ATTR_UNUSED(nocaps),
1824         struct sockaddr_storage* ATTR_UNUSED(addr), socklen_t ATTR_UNUSED(addrlen),
1825         uint8_t* ATTR_UNUSED(zone), size_t ATTR_UNUSED(zonelen),
1826         int ATTR_UNUSED(ssl_upstream), struct module_qstate* ATTR_UNUSED(q))
1827 {
1828         log_assert(0);
1829         return 0;
1830 }
1831
1832 int libworker_handle_reply(struct comm_point* ATTR_UNUSED(c), 
1833         void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1834         struct comm_reply* ATTR_UNUSED(reply_info))
1835 {
1836         log_assert(0);
1837         return 0;
1838 }
1839
1840 int libworker_handle_service_reply(struct comm_point* ATTR_UNUSED(c), 
1841         void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1842         struct comm_reply* ATTR_UNUSED(reply_info))
1843 {
1844         log_assert(0);
1845         return 0;
1846 }
1847
1848 void libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
1849         uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
1850         int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
1851 {
1852         log_assert(0);
1853 }
1854
1855 void libworker_fg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1856         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1857         char* ATTR_UNUSED(why_bogus))
1858 {
1859         log_assert(0);
1860 }
1861
1862 void libworker_bg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1863         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1864         char* ATTR_UNUSED(why_bogus))
1865 {
1866         log_assert(0);
1867 }
1868
1869 void libworker_event_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1870         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1871         char* ATTR_UNUSED(why_bogus))
1872 {
1873         log_assert(0);
1874 }
1875
1876 int context_query_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1877 {
1878         log_assert(0);
1879         return 0;
1880 }
1881
1882 int order_lock_cmp(const void* ATTR_UNUSED(e1), const void* ATTR_UNUSED(e2))
1883 {
1884         log_assert(0);
1885         return 0;
1886 }
1887
1888 int codeline_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1889 {
1890         log_assert(0);
1891         return 0;
1892 }
1893