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