]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/daemon/worker.c
Upgrade Unbound to 1.6.7. More to follow.
[FreeBSD/FreeBSD.git] / contrib / unbound / 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         memset(&qinfo, 0, sizeof(qinfo));
1013
1014         if(error != NETEVENT_NOERROR) {
1015                 /* some bad tcp query DNS formats give these error calls */
1016                 verbose(VERB_ALGO, "handle request called with err=%d", error);
1017                 return 0;
1018         }
1019 #ifdef USE_DNSCRYPT
1020         repinfo->max_udp_size = worker->daemon->cfg->max_udp_size;
1021         if(!dnsc_handle_curved_request(worker->daemon->dnscenv, repinfo)) {
1022                 worker->stats.num_query_dnscrypt_crypted_malformed++;
1023                 return 0;
1024         }
1025         if(c->dnscrypt && !repinfo->is_dnscrypted) {
1026                 char buf[LDNS_MAX_DOMAINLEN+1];
1027                 /* Check if this is unencrypted and asking for certs */
1028                 if(worker_check_request(c->buffer, worker) != 0) {
1029                         verbose(VERB_ALGO,
1030                                 "dnscrypt: worker check request: bad query.");
1031                         log_addr(VERB_CLIENT,"from",&repinfo->addr,
1032                                 repinfo->addrlen);
1033                         comm_point_drop_reply(repinfo);
1034                         return 0;
1035                 }
1036                 if(!query_info_parse(&qinfo, c->buffer)) {
1037                         verbose(VERB_ALGO,
1038                                 "dnscrypt: worker parse request: formerror.");
1039                         log_addr(VERB_CLIENT, "from", &repinfo->addr,
1040                                 repinfo->addrlen);
1041                         comm_point_drop_reply(repinfo);
1042                         return 0;
1043                 }
1044                 dname_str(qinfo.qname, buf);
1045                 if(!(qinfo.qtype == LDNS_RR_TYPE_TXT &&
1046                         strcasecmp(buf,
1047                         worker->daemon->dnscenv->provider_name) == 0)) {
1048                         verbose(VERB_ALGO,
1049                                 "dnscrypt: not TXT %s. Receive: %s %s",
1050                                 worker->daemon->dnscenv->provider_name,
1051                                 sldns_rr_descript(qinfo.qtype)->_name,
1052                                 buf);
1053                         comm_point_drop_reply(repinfo);
1054                         worker->stats.num_query_dnscrypt_cleartext++;
1055                         return 0;
1056                 }
1057                 worker->stats.num_query_dnscrypt_cert++;
1058                 sldns_buffer_rewind(c->buffer);
1059         } else if(c->dnscrypt && repinfo->is_dnscrypted) {
1060                 worker->stats.num_query_dnscrypt_crypted++;
1061         }
1062 #endif
1063 #ifdef USE_DNSTAP
1064         if(worker->dtenv.log_client_query_messages)
1065                 dt_msg_send_client_query(&worker->dtenv, &repinfo->addr, c->type,
1066                         c->buffer);
1067 #endif
1068         acladdr = acl_addr_lookup(worker->daemon->acl, &repinfo->addr, 
1069                 repinfo->addrlen);
1070         acl = acl_get_control(acladdr);
1071         if((ret=deny_refuse_all(c, acl, worker, repinfo)) != -1)
1072         {
1073                 if(ret == 1)
1074                         goto send_reply;
1075                 return ret;
1076         }
1077         if((ret=worker_check_request(c->buffer, worker)) != 0) {
1078                 verbose(VERB_ALGO, "worker check request: bad query.");
1079                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1080                 if(ret != -1) {
1081                         LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1082                         LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
1083                         return 1;
1084                 }
1085                 comm_point_drop_reply(repinfo);
1086                 return 0;
1087         }
1088
1089         worker->stats.num_queries++;
1090
1091         /* check if this query should be dropped based on source ip rate limiting */
1092         if(!infra_ip_ratelimit_inc(worker->env.infra_cache, repinfo,
1093                         *worker->env.now)) {
1094                 /* See if we are passed through with slip factor */
1095                 if(worker->env.cfg->ip_ratelimit_factor != 0 &&
1096                         ub_random_max(worker->env.rnd,
1097                                                   worker->env.cfg->ip_ratelimit_factor) == 1) {
1098
1099                         char addrbuf[128];
1100                         addr_to_str(&repinfo->addr, repinfo->addrlen,
1101                                                 addrbuf, sizeof(addrbuf));
1102                   verbose(VERB_OPS, "ip_ratelimit allowed through for ip address %s ",
1103                                   addrbuf);
1104                 } else {
1105                         worker->stats.num_queries_ip_ratelimited++;
1106                         comm_point_drop_reply(repinfo);
1107                         return 0;
1108                 }
1109         }
1110
1111         /* see if query is in the cache */
1112         if(!query_info_parse(&qinfo, c->buffer)) {
1113                 verbose(VERB_ALGO, "worker parse request: formerror.");
1114                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1115                 memset(&qinfo, 0, sizeof(qinfo)); /* zero qinfo.qname */
1116                 if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) {
1117                         comm_point_drop_reply(repinfo);
1118                         return 0;
1119                 }
1120                 sldns_buffer_rewind(c->buffer);
1121                 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1122                 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 
1123                         LDNS_RCODE_FORMERR);
1124                 server_stats_insrcode(&worker->stats, c->buffer);
1125                 goto send_reply;
1126         }
1127         if(worker->env.cfg->log_queries) {
1128                 char ip[128];
1129                 addr_to_str(&repinfo->addr, repinfo->addrlen, ip, sizeof(ip));
1130                 log_nametypeclass(0, ip, qinfo.qname, qinfo.qtype, qinfo.qclass);
1131         }
1132         if(qinfo.qtype == LDNS_RR_TYPE_AXFR || 
1133                 qinfo.qtype == LDNS_RR_TYPE_IXFR) {
1134                 verbose(VERB_ALGO, "worker request: refused zone transfer.");
1135                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1136                 sldns_buffer_rewind(c->buffer);
1137                 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1138                 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 
1139                         LDNS_RCODE_REFUSED);
1140                 if(worker->stats.extended) {
1141                         worker->stats.qtype[qinfo.qtype]++;
1142                         server_stats_insrcode(&worker->stats, c->buffer);
1143                 }
1144                 goto send_reply;
1145         }
1146         if(qinfo.qtype == LDNS_RR_TYPE_OPT || 
1147                 qinfo.qtype == LDNS_RR_TYPE_TSIG ||
1148                 qinfo.qtype == LDNS_RR_TYPE_TKEY ||
1149                 qinfo.qtype == LDNS_RR_TYPE_MAILA ||
1150                 qinfo.qtype == LDNS_RR_TYPE_MAILB ||
1151                 (qinfo.qtype >= 128 && qinfo.qtype <= 248)) {
1152                 verbose(VERB_ALGO, "worker request: formerror for meta-type.");
1153                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1154                 if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) {
1155                         comm_point_drop_reply(repinfo);
1156                         return 0;
1157                 }
1158                 sldns_buffer_rewind(c->buffer);
1159                 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1160                 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 
1161                         LDNS_RCODE_FORMERR);
1162                 if(worker->stats.extended) {
1163                         worker->stats.qtype[qinfo.qtype]++;
1164                         server_stats_insrcode(&worker->stats, c->buffer);
1165                 }
1166                 goto send_reply;
1167         }
1168         if((ret=parse_edns_from_pkt(c->buffer, &edns, worker->scratchpad)) != 0) {
1169                 struct edns_data reply_edns;
1170                 verbose(VERB_ALGO, "worker parse edns: formerror.");
1171                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1172                 memset(&reply_edns, 0, sizeof(reply_edns));
1173                 reply_edns.edns_present = 1;
1174                 reply_edns.udp_size = EDNS_ADVERTISED_SIZE;
1175                 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
1176                 error_encode(c->buffer, ret, &qinfo,
1177                         *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1178                         sldns_buffer_read_u16_at(c->buffer, 2), &reply_edns);
1179                 regional_free_all(worker->scratchpad);
1180                 server_stats_insrcode(&worker->stats, c->buffer);
1181                 goto send_reply;
1182         }
1183         if(edns.edns_present && edns.edns_version != 0) {
1184                 edns.ext_rcode = (uint8_t)(EDNS_RCODE_BADVERS>>4);
1185                 edns.edns_version = EDNS_ADVERTISED_VERSION;
1186                 edns.udp_size = EDNS_ADVERTISED_SIZE;
1187                 edns.bits &= EDNS_DO;
1188                 edns.opt_list = NULL;
1189                 verbose(VERB_ALGO, "query with bad edns version.");
1190                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1191                 error_encode(c->buffer, EDNS_RCODE_BADVERS&0xf, &qinfo,
1192                         *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1193                         sldns_buffer_read_u16_at(c->buffer, 2), NULL);
1194                 if(sldns_buffer_capacity(c->buffer) >=
1195                         sldns_buffer_limit(c->buffer)+calc_edns_field_size(&edns))
1196                         attach_edns_record(c->buffer, &edns);
1197                 regional_free_all(worker->scratchpad);
1198                 goto send_reply;
1199         }
1200         if(edns.edns_present && edns.udp_size < NORMAL_UDP_SIZE &&
1201                 worker->daemon->cfg->harden_short_bufsize) {
1202                 verbose(VERB_QUERY, "worker request: EDNS bufsize %d ignored",
1203                         (int)edns.udp_size);
1204                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1205                 edns.udp_size = NORMAL_UDP_SIZE;
1206         }
1207         if(edns.udp_size > worker->daemon->cfg->max_udp_size &&
1208                 c->type == comm_udp) {
1209                 verbose(VERB_QUERY,
1210                         "worker request: max UDP reply size modified"
1211                         " (%d to max-udp-size)", (int)edns.udp_size);
1212                 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
1213                 edns.udp_size = worker->daemon->cfg->max_udp_size;
1214         }
1215         if(edns.udp_size < LDNS_HEADER_SIZE) {
1216                 verbose(VERB_ALGO, "worker request: edns is too small.");
1217                 log_addr(VERB_CLIENT, "from", &repinfo->addr, repinfo->addrlen);
1218                 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1219                 LDNS_TC_SET(sldns_buffer_begin(c->buffer));
1220                 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 
1221                         LDNS_RCODE_SERVFAIL);
1222                 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
1223                 sldns_buffer_write_at(c->buffer, 4, 
1224                         (uint8_t*)"\0\0\0\0\0\0\0\0", 8);
1225                 sldns_buffer_flip(c->buffer);
1226                 regional_free_all(worker->scratchpad);
1227                 goto send_reply;
1228         }
1229         if(worker->stats.extended)
1230                 server_stats_insquery(&worker->stats, c, qinfo.qtype,
1231                         qinfo.qclass, &edns, repinfo);
1232         if(c->type != comm_udp)
1233                 edns.udp_size = 65535; /* max size for TCP replies */
1234         if(qinfo.qclass == LDNS_RR_CLASS_CH && answer_chaos(worker, &qinfo,
1235                 &edns, c->buffer)) {
1236                 server_stats_insrcode(&worker->stats, c->buffer);
1237                 regional_free_all(worker->scratchpad);
1238                 goto send_reply;
1239         }
1240         if(local_zones_answer(worker->daemon->local_zones, &worker->env, &qinfo,
1241                 &edns, c->buffer, worker->scratchpad, repinfo, acladdr->taglist,
1242                 acladdr->taglen, acladdr->tag_actions,
1243                 acladdr->tag_actions_size, acladdr->tag_datas,
1244                 acladdr->tag_datas_size, worker->daemon->cfg->tagname,
1245                 worker->daemon->cfg->num_tags, acladdr->view)) {
1246                 regional_free_all(worker->scratchpad);
1247                 if(sldns_buffer_limit(c->buffer) == 0) {
1248                         comm_point_drop_reply(repinfo);
1249                         return 0;
1250                 }
1251                 server_stats_insrcode(&worker->stats, c->buffer);
1252                 goto send_reply;
1253         }
1254
1255         /* We've looked in our local zones. If the answer isn't there, we
1256          * might need to bail out based on ACLs now. */
1257         if((ret=deny_refuse_non_local(c, acl, worker, repinfo)) != -1)
1258         {
1259                 regional_free_all(worker->scratchpad);
1260                 if(ret == 1)
1261                         goto send_reply;
1262                 return ret;
1263         }
1264
1265         /* If this request does not have the recursion bit set, verify
1266          * ACLs allow the snooping. */
1267         if(!(LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) &&
1268                 acl != acl_allow_snoop ) {
1269                 sldns_buffer_set_limit(c->buffer, LDNS_HEADER_SIZE);
1270                 sldns_buffer_write_at(c->buffer, 4, 
1271                         (uint8_t*)"\0\0\0\0\0\0\0\0", 8);
1272                 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1273                 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 
1274                         LDNS_RCODE_REFUSED);
1275                 sldns_buffer_flip(c->buffer);
1276                 regional_free_all(worker->scratchpad);
1277                 server_stats_insrcode(&worker->stats, c->buffer);
1278                 log_addr(VERB_ALGO, "refused nonrec (cache snoop) query from",
1279                         &repinfo->addr, repinfo->addrlen);
1280                 goto send_reply;
1281         }
1282
1283         /* If we've found a local alias, replace the qname with the alias
1284          * target before resolving it. */
1285         if(qinfo.local_alias) {
1286                 struct ub_packed_rrset_key* rrset = qinfo.local_alias->rrset;
1287                 struct packed_rrset_data* d = rrset->entry.data;
1288
1289                 /* Sanity check: our current implementation only supports
1290                  * a single CNAME RRset as a local alias. */
1291                 if(qinfo.local_alias->next ||
1292                         rrset->rk.type != htons(LDNS_RR_TYPE_CNAME) ||
1293                         d->count != 1) {
1294                         log_err("assumption failure: unexpected local alias");
1295                         regional_free_all(worker->scratchpad);
1296                         return 0; /* drop it */
1297                 }
1298                 qinfo.qname = d->rr_data[0] + 2;
1299                 qinfo.qname_len = d->rr_len[0] - 2;
1300         }
1301
1302         /* If we may apply IP-based actions to the answer, build the client
1303          * information.  As this can be expensive, skip it if there is
1304          * absolutely no possibility of it. */
1305         if(worker->daemon->use_response_ip &&
1306                 (qinfo.qtype == LDNS_RR_TYPE_A ||
1307                 qinfo.qtype == LDNS_RR_TYPE_AAAA ||
1308                 qinfo.qtype == LDNS_RR_TYPE_ANY)) {
1309                 cinfo_tmp.taglist = acladdr->taglist;
1310                 cinfo_tmp.taglen = acladdr->taglen;
1311                 cinfo_tmp.tag_actions = acladdr->tag_actions;
1312                 cinfo_tmp.tag_actions_size = acladdr->tag_actions_size;
1313                 cinfo_tmp.tag_datas = acladdr->tag_datas;
1314                 cinfo_tmp.tag_datas_size = acladdr->tag_datas_size;
1315                 cinfo_tmp.view = acladdr->view;
1316                 cinfo_tmp.respip_set = worker->daemon->respip_set;
1317                 cinfo = &cinfo_tmp;
1318         }
1319
1320 lookup_cache:
1321         /* Lookup the cache.  In case we chase an intermediate CNAME chain
1322          * this is a two-pass operation, and lookup_qinfo is different for
1323          * each pass.  We should still pass the original qinfo to
1324          * answer_from_cache(), however, since it's used to build the reply. */
1325         if(!edns_bypass_cache_stage(edns.opt_list, &worker->env)) {
1326                 h = query_info_hash(lookup_qinfo, sldns_buffer_read_u16_at(c->buffer, 2));
1327                 if((e=slabhash_lookup(worker->env.msg_cache, h, lookup_qinfo, 0))) {
1328                         /* answer from cache - we have acquired a readlock on it */
1329                         if(answer_from_cache(worker, &qinfo, 
1330                                 cinfo, &need_drop, &alias_rrset, &partial_rep,
1331                                 (struct reply_info*)e->data, 
1332                                 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer), 
1333                                 sldns_buffer_read_u16_at(c->buffer, 2), repinfo, 
1334                                 &edns)) {
1335                                 /* prefetch it if the prefetch TTL expired.
1336                                  * Note that if there is more than one pass
1337                                  * its qname must be that used for cache
1338                                  * lookup. */
1339                                 if((worker->env.cfg->prefetch || worker->env.cfg->serve_expired)
1340                                         && *worker->env.now >=
1341                                         ((struct reply_info*)e->data)->prefetch_ttl) {
1342                                         time_t leeway = ((struct reply_info*)e->
1343                                                 data)->ttl - *worker->env.now;
1344                                         if(((struct reply_info*)e->data)->ttl
1345                                                 < *worker->env.now)
1346                                                 leeway = 0;
1347                                         lock_rw_unlock(&e->lock);
1348                                         reply_and_prefetch(worker, lookup_qinfo,
1349                                                 sldns_buffer_read_u16_at(c->buffer, 2),
1350                                                 repinfo, leeway);
1351                                         if(!partial_rep) {
1352                                                 rc = 0;
1353                                                 regional_free_all(worker->scratchpad);
1354                                                 goto send_reply_rc;
1355                                         }
1356                                 } else if(!partial_rep) {
1357                                         lock_rw_unlock(&e->lock);
1358                                         regional_free_all(worker->scratchpad);
1359                                         goto send_reply;
1360                                 } else {
1361                                         /* Note that we've already released the
1362                                          * lock if we're here after prefetch. */
1363                                         lock_rw_unlock(&e->lock);
1364                                 }
1365                                 /* We've found a partial reply ending with an
1366                                  * alias.  Replace the lookup qinfo for the
1367                                  * alias target and lookup the cache again to
1368                                  * (possibly) complete the reply.  As we're
1369                                  * passing the "base" reply, there will be no
1370                                  * more alias chasing. */
1371                                 memset(&qinfo_tmp, 0, sizeof(qinfo_tmp));
1372                                 get_cname_target(alias_rrset, &qinfo_tmp.qname,
1373                                         &qinfo_tmp.qname_len);
1374                                 if(!qinfo_tmp.qname) {
1375                                         log_err("unexpected: invalid answer alias");
1376                                         regional_free_all(worker->scratchpad);
1377                                         return 0; /* drop query */
1378                                 }
1379                                 qinfo_tmp.qtype = qinfo.qtype;
1380                                 qinfo_tmp.qclass = qinfo.qclass;
1381                                 lookup_qinfo = &qinfo_tmp;
1382                                 goto lookup_cache;
1383                         }
1384                         verbose(VERB_ALGO, "answer from the cache failed");
1385                         lock_rw_unlock(&e->lock);
1386                 }
1387                 if(!LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) {
1388                         if(answer_norec_from_cache(worker, &qinfo,
1389                                 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer), 
1390                                 sldns_buffer_read_u16_at(c->buffer, 2), repinfo, 
1391                                 &edns)) {
1392                                 regional_free_all(worker->scratchpad);
1393                                 goto send_reply;
1394                         }
1395                         verbose(VERB_ALGO, "answer norec from cache -- "
1396                                 "need to validate or not primed");
1397                 }
1398         }
1399         sldns_buffer_rewind(c->buffer);
1400         server_stats_querymiss(&worker->stats, worker);
1401
1402         if(verbosity >= VERB_CLIENT) {
1403                 if(c->type == comm_udp)
1404                         log_addr(VERB_CLIENT, "udp request from",
1405                                 &repinfo->addr, repinfo->addrlen);
1406                 else    log_addr(VERB_CLIENT, "tcp request from",
1407                                 &repinfo->addr, repinfo->addrlen);
1408         }
1409
1410         /* grab a work request structure for this new request */
1411         mesh_new_client(worker->env.mesh, &qinfo, cinfo,
1412                 sldns_buffer_read_u16_at(c->buffer, 2),
1413                 &edns, repinfo, *(uint16_t*)(void *)sldns_buffer_begin(c->buffer));
1414         regional_free_all(worker->scratchpad);
1415         worker_mem_report(worker, NULL);
1416         return 0;
1417
1418 send_reply:
1419         rc = 1;
1420 send_reply_rc:
1421         if(need_drop) {
1422                 comm_point_drop_reply(repinfo);
1423                 return 0;
1424         }
1425 #ifdef USE_DNSTAP
1426         if(worker->dtenv.log_client_response_messages)
1427                 dt_msg_send_client_response(&worker->dtenv, &repinfo->addr,
1428                         c->type, c->buffer);
1429 #endif
1430         if(worker->env.cfg->log_replies)
1431         {
1432                 struct timeval tv = {0, 0};
1433                 log_reply_info(0, &qinfo, &repinfo->addr, repinfo->addrlen,
1434                         tv, 1, c->buffer);
1435         }
1436 #ifdef USE_DNSCRYPT
1437         if(!dnsc_handle_uncurved_request(repinfo)) {
1438                 return 0;
1439         }
1440 #endif
1441         return rc;
1442 }
1443
1444 void 
1445 worker_sighandler(int sig, void* arg)
1446 {
1447         /* note that log, print, syscalls here give race conditions. 
1448          * And cause hangups if the log-lock is held by the application. */
1449         struct worker* worker = (struct worker*)arg;
1450         switch(sig) {
1451 #ifdef SIGHUP
1452                 case SIGHUP:
1453                         comm_base_exit(worker->base);
1454                         break;
1455 #endif
1456                 case SIGINT:
1457                         worker->need_to_exit = 1;
1458                         comm_base_exit(worker->base);
1459                         break;
1460 #ifdef SIGQUIT
1461                 case SIGQUIT:
1462                         worker->need_to_exit = 1;
1463                         comm_base_exit(worker->base);
1464                         break;
1465 #endif
1466                 case SIGTERM:
1467                         worker->need_to_exit = 1;
1468                         comm_base_exit(worker->base);
1469                         break;
1470                 default:
1471                         /* unknown signal, ignored */
1472                         break;
1473         }
1474 }
1475
1476 /** restart statistics timer for worker, if enabled */
1477 static void
1478 worker_restart_timer(struct worker* worker)
1479 {
1480         if(worker->env.cfg->stat_interval > 0) {
1481                 struct timeval tv;
1482 #ifndef S_SPLINT_S
1483                 tv.tv_sec = worker->env.cfg->stat_interval;
1484                 tv.tv_usec = 0;
1485 #endif
1486                 comm_timer_set(worker->stat_timer, &tv);
1487         }
1488 }
1489
1490 void worker_stat_timer_cb(void* arg)
1491 {
1492         struct worker* worker = (struct worker*)arg;
1493         server_stats_log(&worker->stats, worker, worker->thread_num);
1494         mesh_stats(worker->env.mesh, "mesh has");
1495         worker_mem_report(worker, NULL);
1496         /* SHM is enabled, process data to SHM */
1497         if (worker->daemon->cfg->shm_enable) {
1498                 shm_main_run(worker);
1499         }
1500         if(!worker->daemon->cfg->stat_cumulative) {
1501                 worker_stats_clear(worker);
1502         }
1503         /* start next timer */
1504         worker_restart_timer(worker);
1505 }
1506
1507 void worker_probe_timer_cb(void* arg)
1508 {
1509         struct worker* worker = (struct worker*)arg;
1510         struct timeval tv;
1511 #ifndef S_SPLINT_S
1512         tv.tv_sec = (time_t)autr_probe_timer(&worker->env);
1513         tv.tv_usec = 0;
1514 #endif
1515         if(tv.tv_sec != 0)
1516                 comm_timer_set(worker->env.probe_timer, &tv);
1517 }
1518
1519 struct worker* 
1520 worker_create(struct daemon* daemon, int id, int* ports, int n)
1521 {
1522         unsigned int seed;
1523         struct worker* worker = (struct worker*)calloc(1, 
1524                 sizeof(struct worker));
1525         if(!worker) 
1526                 return NULL;
1527         worker->numports = n;
1528         worker->ports = (int*)memdup(ports, sizeof(int)*n);
1529         if(!worker->ports) {
1530                 free(worker);
1531                 return NULL;
1532         }
1533         worker->daemon = daemon;
1534         worker->thread_num = id;
1535         if(!(worker->cmd = tube_create())) {
1536                 free(worker->ports);
1537                 free(worker);
1538                 return NULL;
1539         }
1540         /* create random state here to avoid locking trouble in RAND_bytes */
1541         seed = (unsigned int)time(NULL) ^ (unsigned int)getpid() ^
1542                 (((unsigned int)worker->thread_num)<<17);
1543                 /* shift thread_num so it does not match out pid bits */
1544         if(!(worker->rndstate = ub_initstate(seed, daemon->rand))) {
1545                 seed = 0;
1546                 log_err("could not init random numbers.");
1547                 tube_delete(worker->cmd);
1548                 free(worker->ports);
1549                 free(worker);
1550                 return NULL;
1551         }
1552         seed = 0;
1553 #ifdef USE_DNSTAP
1554         if(daemon->cfg->dnstap) {
1555                 log_assert(daemon->dtenv != NULL);
1556                 memcpy(&worker->dtenv, daemon->dtenv, sizeof(struct dt_env));
1557                 if(!dt_init(&worker->dtenv))
1558                         fatal_exit("dt_init failed");
1559         }
1560 #endif
1561         return worker;
1562 }
1563
1564 int
1565 worker_init(struct worker* worker, struct config_file *cfg, 
1566         struct listen_port* ports, int do_sigs)
1567 {
1568 #ifdef USE_DNSTAP
1569         struct dt_env* dtenv = &worker->dtenv;
1570 #else
1571         void* dtenv = NULL;
1572 #endif
1573         worker->need_to_exit = 0;
1574         worker->base = comm_base_create(do_sigs);
1575         if(!worker->base) {
1576                 log_err("could not create event handling base");
1577                 worker_delete(worker);
1578                 return 0;
1579         }
1580         comm_base_set_slow_accept_handlers(worker->base, &worker_stop_accept,
1581                 &worker_start_accept, worker);
1582         if(do_sigs) {
1583 #ifdef SIGHUP
1584                 ub_thread_sig_unblock(SIGHUP);
1585 #endif
1586                 ub_thread_sig_unblock(SIGINT);
1587 #ifdef SIGQUIT
1588                 ub_thread_sig_unblock(SIGQUIT);
1589 #endif
1590                 ub_thread_sig_unblock(SIGTERM);
1591 #ifndef LIBEVENT_SIGNAL_PROBLEM
1592                 worker->comsig = comm_signal_create(worker->base, 
1593                         worker_sighandler, worker);
1594                 if(!worker->comsig 
1595 #ifdef SIGHUP
1596                         || !comm_signal_bind(worker->comsig, SIGHUP)
1597 #endif
1598 #ifdef SIGQUIT
1599                         || !comm_signal_bind(worker->comsig, SIGQUIT)
1600 #endif
1601                         || !comm_signal_bind(worker->comsig, SIGTERM)
1602                         || !comm_signal_bind(worker->comsig, SIGINT)) {
1603                         log_err("could not create signal handlers");
1604                         worker_delete(worker);
1605                         return 0;
1606                 }
1607 #endif /* LIBEVENT_SIGNAL_PROBLEM */
1608                 if(!daemon_remote_open_accept(worker->daemon->rc, 
1609                         worker->daemon->rc_ports, worker)) {
1610                         worker_delete(worker);
1611                         return 0;
1612                 }
1613 #ifdef UB_ON_WINDOWS
1614                 wsvc_setup_worker(worker);
1615 #endif /* UB_ON_WINDOWS */
1616         } else { /* !do_sigs */
1617                 worker->comsig = NULL;
1618         }
1619         worker->front = listen_create(worker->base, ports,
1620                 cfg->msg_buffer_size, (int)cfg->incoming_num_tcp, 
1621                 worker->daemon->listen_sslctx, dtenv, worker_handle_request,
1622                 worker);
1623         if(!worker->front) {
1624                 log_err("could not create listening sockets");
1625                 worker_delete(worker);
1626                 return 0;
1627         }
1628         worker->back = outside_network_create(worker->base,
1629                 cfg->msg_buffer_size, (size_t)cfg->outgoing_num_ports, 
1630                 cfg->out_ifs, cfg->num_out_ifs, cfg->do_ip4, cfg->do_ip6, 
1631                 cfg->do_tcp?cfg->outgoing_num_tcp:0, 
1632                 worker->daemon->env->infra_cache, worker->rndstate,
1633                 cfg->use_caps_bits_for_id, worker->ports, worker->numports,
1634                 cfg->unwanted_threshold, cfg->outgoing_tcp_mss,
1635                 &worker_alloc_cleanup, worker,
1636                 cfg->do_udp || cfg->udp_upstream_without_downstream,
1637                 worker->daemon->connect_sslctx, cfg->delay_close,
1638                 dtenv);
1639         if(!worker->back) {
1640                 log_err("could not create outgoing sockets");
1641                 worker_delete(worker);
1642                 return 0;
1643         }
1644         /* start listening to commands */
1645         if(!tube_setup_bg_listen(worker->cmd, worker->base,
1646                 &worker_handle_control_cmd, worker)) {
1647                 log_err("could not create control compt.");
1648                 worker_delete(worker);
1649                 return 0;
1650         }
1651         worker->stat_timer = comm_timer_create(worker->base, 
1652                 worker_stat_timer_cb, worker);
1653         if(!worker->stat_timer) {
1654                 log_err("could not create statistics timer");
1655         }
1656
1657         /* we use the msg_buffer_size as a good estimate for what the 
1658          * user wants for memory usage sizes */
1659         worker->scratchpad = regional_create_custom(cfg->msg_buffer_size);
1660         if(!worker->scratchpad) {
1661                 log_err("malloc failure");
1662                 worker_delete(worker);
1663                 return 0;
1664         }
1665
1666         server_stats_init(&worker->stats, cfg);
1667         alloc_init(&worker->alloc, &worker->daemon->superalloc, 
1668                 worker->thread_num);
1669         alloc_set_id_cleanup(&worker->alloc, &worker_alloc_cleanup, worker);
1670         worker->env = *worker->daemon->env;
1671         comm_base_timept(worker->base, &worker->env.now, &worker->env.now_tv);
1672         if(worker->thread_num == 0)
1673                 log_set_time(worker->env.now);
1674         worker->env.worker = worker;
1675         worker->env.send_query = &worker_send_query;
1676         worker->env.alloc = &worker->alloc;
1677         worker->env.rnd = worker->rndstate;
1678         /* If case prefetch is triggered, the corresponding mesh will clear
1679          * the scratchpad for the module env in the middle of request handling.
1680          * It would be prone to a use-after-free kind of bug, so we avoid
1681          * sharing it with worker's own scratchpad at the cost of having
1682          * one more pad per worker. */
1683         worker->env.scratch = regional_create_custom(cfg->msg_buffer_size);
1684         if(!worker->env.scratch) {
1685                 log_err("malloc failure");
1686                 worker_delete(worker);
1687                 return 0;
1688         }
1689         worker->env.mesh = mesh_create(&worker->daemon->mods, &worker->env);
1690         worker->env.detach_subs = &mesh_detach_subs;
1691         worker->env.attach_sub = &mesh_attach_sub;
1692         worker->env.add_sub = &mesh_add_sub;
1693         worker->env.kill_sub = &mesh_state_delete;
1694         worker->env.detect_cycle = &mesh_detect_cycle;
1695         worker->env.scratch_buffer = sldns_buffer_new(cfg->msg_buffer_size);
1696         if(!(worker->env.fwds = forwards_create()) ||
1697                 !forwards_apply_cfg(worker->env.fwds, cfg)) {
1698                 log_err("Could not set forward zones");
1699                 worker_delete(worker);
1700                 return 0;
1701         }
1702         if(!(worker->env.hints = hints_create()) ||
1703                 !hints_apply_cfg(worker->env.hints, cfg)) {
1704                 log_err("Could not set root or stub hints");
1705                 worker_delete(worker);
1706                 return 0;
1707         }
1708         /* one probe timer per process -- if we have 5011 anchors */
1709         if(autr_get_num_anchors(worker->env.anchors) > 0
1710 #ifndef THREADS_DISABLED
1711                 && worker->thread_num == 0
1712 #endif
1713                 ) {
1714                 struct timeval tv;
1715                 tv.tv_sec = 0;
1716                 tv.tv_usec = 0;
1717                 worker->env.probe_timer = comm_timer_create(worker->base,
1718                         worker_probe_timer_cb, worker);
1719                 if(!worker->env.probe_timer) {
1720                         log_err("could not create 5011-probe timer");
1721                 } else {
1722                         /* let timer fire, then it can reset itself */
1723                         comm_timer_set(worker->env.probe_timer, &tv);
1724                 }
1725         }
1726         if(!worker->env.mesh || !worker->env.scratch_buffer) {
1727                 worker_delete(worker);
1728                 return 0;
1729         }
1730         worker_mem_report(worker, NULL);
1731         /* if statistics enabled start timer */
1732         if(worker->env.cfg->stat_interval > 0) {
1733                 verbose(VERB_ALGO, "set statistics interval %d secs", 
1734                         worker->env.cfg->stat_interval);
1735                 worker_restart_timer(worker);
1736         }
1737         return 1;
1738 }
1739
1740 void 
1741 worker_work(struct worker* worker)
1742 {
1743         comm_base_dispatch(worker->base);
1744 }
1745
1746 void 
1747 worker_delete(struct worker* worker)
1748 {
1749         if(!worker) 
1750                 return;
1751         if(worker->env.mesh && verbosity >= VERB_OPS) {
1752                 server_stats_log(&worker->stats, worker, worker->thread_num);
1753                 mesh_stats(worker->env.mesh, "mesh has");
1754                 worker_mem_report(worker, NULL);
1755         }
1756         outside_network_quit_prepare(worker->back);
1757         mesh_delete(worker->env.mesh);
1758         sldns_buffer_free(worker->env.scratch_buffer);
1759         forwards_delete(worker->env.fwds);
1760         hints_delete(worker->env.hints);
1761         listen_delete(worker->front);
1762         outside_network_delete(worker->back);
1763         comm_signal_delete(worker->comsig);
1764         tube_delete(worker->cmd);
1765         comm_timer_delete(worker->stat_timer);
1766         comm_timer_delete(worker->env.probe_timer);
1767         free(worker->ports);
1768         if(worker->thread_num == 0) {
1769                 log_set_time(NULL);
1770 #ifdef UB_ON_WINDOWS
1771                 wsvc_desetup_worker(worker);
1772 #endif /* UB_ON_WINDOWS */
1773         }
1774         comm_base_delete(worker->base);
1775         ub_randfree(worker->rndstate);
1776         alloc_clear(&worker->alloc);
1777         regional_destroy(worker->env.scratch);
1778         regional_destroy(worker->scratchpad);
1779         free(worker);
1780 }
1781
1782 struct outbound_entry*
1783 worker_send_query(struct query_info* qinfo, uint16_t flags, int dnssec,
1784         int want_dnssec, int nocaps, struct sockaddr_storage* addr,
1785         socklen_t addrlen, uint8_t* zone, size_t zonelen, int ssl_upstream,
1786         struct module_qstate* q)
1787 {
1788         struct worker* worker = q->env->worker;
1789         struct outbound_entry* e = (struct outbound_entry*)regional_alloc(
1790                 q->region, sizeof(*e));
1791         if(!e) 
1792                 return NULL;
1793         e->qstate = q;
1794         e->qsent = outnet_serviced_query(worker->back, qinfo, flags, dnssec,
1795                 want_dnssec, nocaps, q->env->cfg->tcp_upstream,
1796                 ssl_upstream, addr, addrlen, zone, zonelen, q,
1797                 worker_handle_service_reply, e, worker->back->udp_buff, q->env);
1798         if(!e->qsent) {
1799                 return NULL;
1800         }
1801         return e;
1802 }
1803
1804 void 
1805 worker_alloc_cleanup(void* arg)
1806 {
1807         struct worker* worker = (struct worker*)arg;
1808         slabhash_clear(&worker->env.rrset_cache->table);
1809         slabhash_clear(worker->env.msg_cache);
1810 }
1811
1812 void worker_stats_clear(struct worker* worker)
1813 {
1814         server_stats_init(&worker->stats, worker->env.cfg);
1815         mesh_stats_clear(worker->env.mesh);
1816         worker->back->unwanted_replies = 0;
1817         worker->back->num_tcp_outgoing = 0;
1818 }
1819
1820 void worker_start_accept(void* arg)
1821 {
1822         struct worker* worker = (struct worker*)arg;
1823         listen_start_accept(worker->front);
1824         if(worker->thread_num == 0)
1825                 daemon_remote_start_accept(worker->daemon->rc);
1826 }
1827
1828 void worker_stop_accept(void* arg)
1829 {
1830         struct worker* worker = (struct worker*)arg;
1831         listen_stop_accept(worker->front);
1832         if(worker->thread_num == 0)
1833                 daemon_remote_stop_accept(worker->daemon->rc);
1834 }
1835
1836 /* --- fake callbacks for fptr_wlist to work --- */
1837 struct outbound_entry* libworker_send_query(
1838         struct query_info* ATTR_UNUSED(qinfo),
1839         uint16_t ATTR_UNUSED(flags), int ATTR_UNUSED(dnssec),
1840         int ATTR_UNUSED(want_dnssec), int ATTR_UNUSED(nocaps),
1841         struct sockaddr_storage* ATTR_UNUSED(addr), socklen_t ATTR_UNUSED(addrlen),
1842         uint8_t* ATTR_UNUSED(zone), size_t ATTR_UNUSED(zonelen),
1843         int ATTR_UNUSED(ssl_upstream), struct module_qstate* ATTR_UNUSED(q))
1844 {
1845         log_assert(0);
1846         return 0;
1847 }
1848
1849 int libworker_handle_reply(struct comm_point* ATTR_UNUSED(c), 
1850         void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1851         struct comm_reply* ATTR_UNUSED(reply_info))
1852 {
1853         log_assert(0);
1854         return 0;
1855 }
1856
1857 int libworker_handle_service_reply(struct comm_point* ATTR_UNUSED(c), 
1858         void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1859         struct comm_reply* ATTR_UNUSED(reply_info))
1860 {
1861         log_assert(0);
1862         return 0;
1863 }
1864
1865 void libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
1866         uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
1867         int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
1868 {
1869         log_assert(0);
1870 }
1871
1872 void libworker_fg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1873         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1874         char* ATTR_UNUSED(why_bogus))
1875 {
1876         log_assert(0);
1877 }
1878
1879 void libworker_bg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1880         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1881         char* ATTR_UNUSED(why_bogus))
1882 {
1883         log_assert(0);
1884 }
1885
1886 void libworker_event_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1887         sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1888         char* ATTR_UNUSED(why_bogus))
1889 {
1890         log_assert(0);
1891 }
1892
1893 int context_query_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1894 {
1895         log_assert(0);
1896         return 0;
1897 }
1898
1899 int order_lock_cmp(const void* ATTR_UNUSED(e1), const void* ATTR_UNUSED(e2))
1900 {
1901         log_assert(0);
1902         return 0;
1903 }
1904
1905 int codeline_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1906 {
1907         log_assert(0);
1908         return 0;
1909 }
1910