2 * daemon/worker.c - worker that handles a pending list of requests.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file implements the worker that handles callbacks on events, for
44 #include "util/net_help.h"
45 #include "util/random.h"
46 #include "daemon/worker.h"
47 #include "daemon/daemon.h"
48 #include "daemon/remote.h"
49 #include "daemon/acl_list.h"
50 #include "util/netevent.h"
51 #include "util/config_file.h"
52 #include "util/module.h"
53 #include "util/regional.h"
54 #include "util/storage/slabhash.h"
55 #include "services/listen_dnsport.h"
56 #include "services/outside_network.h"
57 #include "services/outbound_list.h"
58 #include "services/cache/rrset.h"
59 #include "services/cache/infra.h"
60 #include "services/cache/dns.h"
61 #include "services/mesh.h"
62 #include "services/localzone.h"
63 #include "util/data/msgparse.h"
64 #include "util/data/msgencode.h"
65 #include "util/data/dname.h"
66 #include "util/fptr_wlist.h"
67 #include "util/tube.h"
68 #include "iterator/iter_fwd.h"
69 #include "iterator/iter_hints.h"
70 #include "validator/autotrust.h"
71 #include "validator/val_anchor.h"
72 #include "libunbound/context.h"
73 #include "libunbound/libworker.h"
74 #include "sldns/sbuffer.h"
76 #ifdef HAVE_SYS_TYPES_H
77 # include <sys/types.h>
84 #include "winrc/win_svc.h"
87 /** Size of an UDP datagram */
88 #define NORMAL_UDP_SIZE 512 /* bytes */
89 /** ratelimit for error responses */
90 #define ERROR_RATELIMIT 100 /* qps */
93 * seconds to add to prefetch leeway. This is a TTL that expires old rrsets
94 * earlier than they should in order to put the new update into the cache.
95 * This additional value is to make sure that if not all TTLs are equal in
96 * the message to be updated(and replaced), that rrsets with up to this much
97 * extra TTL are also replaced. This means that the resulting new message
98 * will have (most likely) this TTL at least, avoiding very small 'split
99 * second' TTLs due to operators choosing relative primes for TTLs (or so).
100 * Also has to be at least one to break ties (and overwrite cached entry).
102 #define PREFETCH_EXPIRY_ADD 60
104 /** Report on memory usage by this thread and global */
106 worker_mem_report(struct worker* ATTR_UNUSED(worker),
107 struct serviced_query* ATTR_UNUSED(cur_serv))
109 #ifdef UNBOUND_ALLOC_STATS
110 /* measure memory leakage */
111 extern size_t unbound_mem_alloc, unbound_mem_freed;
112 /* debug func in validator module */
113 size_t total, front, back, mesh, msg, rrset, infra, ac, superac;
114 size_t me, iter, val, anch;
116 if(verbosity < VERB_ALGO)
118 front = listen_get_mem(worker->front);
119 back = outnet_get_mem(worker->back);
120 msg = slabhash_get_mem(worker->env.msg_cache);
121 rrset = slabhash_get_mem(&worker->env.rrset_cache->table);
122 infra = infra_get_mem(worker->env.infra_cache);
123 mesh = mesh_get_mem(worker->env.mesh);
124 ac = alloc_get_mem(&worker->alloc);
125 superac = alloc_get_mem(&worker->daemon->superalloc);
126 anch = anchors_get_mem(worker->env.anchors);
129 for(i=0; i<worker->env.mesh->mods.num; i++) {
130 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
131 mods.mod[i]->get_mem));
132 if(strcmp(worker->env.mesh->mods.mod[i]->name, "validator")==0)
133 val += (*worker->env.mesh->mods.mod[i]->get_mem)
135 else iter += (*worker->env.mesh->mods.mod[i]->get_mem)
138 me = sizeof(*worker) + sizeof(*worker->base) + sizeof(*worker->comsig)
139 + comm_point_get_mem(worker->cmd_com)
140 + sizeof(worker->rndstate)
141 + regional_get_mem(worker->scratchpad)
142 + sizeof(*worker->env.scratch_buffer)
143 + sldns_buffer_capacity(worker->env.scratch_buffer)
144 + forwards_get_mem(worker->env.fwds)
145 + hints_get_mem(worker->env.hints);
146 if(worker->thread_num == 0)
147 me += acl_list_get_mem(worker->daemon->acl);
149 me += serviced_get_mem(cur_serv);
151 total = front+back+mesh+msg+rrset+infra+iter+val+ac+superac+me;
152 log_info("Memory conditions: %u front=%u back=%u mesh=%u msg=%u "
153 "rrset=%u infra=%u iter=%u val=%u anchors=%u "
154 "alloccache=%u globalalloccache=%u me=%u",
155 (unsigned)total, (unsigned)front, (unsigned)back,
156 (unsigned)mesh, (unsigned)msg, (unsigned)rrset,
157 (unsigned)infra, (unsigned)iter, (unsigned)val, (unsigned)anch,
158 (unsigned)ac, (unsigned)superac, (unsigned)me);
159 log_info("Total heap memory estimate: %u total-alloc: %u "
160 "total-free: %u", (unsigned)total,
161 (unsigned)unbound_mem_alloc, (unsigned)unbound_mem_freed);
162 #else /* no UNBOUND_ALLOC_STATS */
165 if(verbosity < VERB_QUERY)
167 for(i=0; i<worker->env.mesh->mods.num; i++) {
168 fptr_ok(fptr_whitelist_mod_get_mem(worker->env.mesh->
169 mods.mod[i]->get_mem));
170 if(strcmp(worker->env.mesh->mods.mod[i]->name, "validator")==0)
171 val += (*worker->env.mesh->mods.mod[i]->get_mem)
174 verbose(VERB_QUERY, "cache memory msg=%u rrset=%u infra=%u val=%u",
175 (unsigned)slabhash_get_mem(worker->env.msg_cache),
176 (unsigned)slabhash_get_mem(&worker->env.rrset_cache->table),
177 (unsigned)infra_get_mem(worker->env.infra_cache),
179 #endif /* UNBOUND_ALLOC_STATS */
183 worker_send_cmd(struct worker* worker, enum worker_commands cmd)
185 uint32_t c = (uint32_t)htonl(cmd);
186 if(!tube_write_msg(worker->cmd, (uint8_t*)&c, sizeof(c), 0)) {
187 log_err("worker send cmd %d failed", (int)cmd);
192 worker_handle_reply(struct comm_point* c, void* arg, int error,
193 struct comm_reply* reply_info)
195 struct module_qstate* q = (struct module_qstate*)arg;
196 struct worker* worker = q->env->worker;
197 struct outbound_entry e;
202 mesh_report_reply(worker->env.mesh, &e, reply_info, error);
203 worker_mem_report(worker, NULL);
207 if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
208 || LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) !=
210 || LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
211 /* error becomes timeout for the module as if this reply
213 mesh_report_reply(worker->env.mesh, &e, reply_info,
215 worker_mem_report(worker, NULL);
218 mesh_report_reply(worker->env.mesh, &e, reply_info, NETEVENT_NOERROR);
219 worker_mem_report(worker, NULL);
224 worker_handle_service_reply(struct comm_point* c, void* arg, int error,
225 struct comm_reply* reply_info)
227 struct outbound_entry* e = (struct outbound_entry*)arg;
228 struct worker* worker = e->qstate->env->worker;
229 struct serviced_query *sq = e->qsent;
231 verbose(VERB_ALGO, "worker svcd callback for qstate %p", e->qstate);
233 mesh_report_reply(worker->env.mesh, e, reply_info, error);
234 worker_mem_report(worker, sq);
238 if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
239 || LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) !=
241 || LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
242 /* error becomes timeout for the module as if this reply
244 verbose(VERB_ALGO, "worker: bad reply handled as timeout");
245 mesh_report_reply(worker->env.mesh, e, reply_info,
247 worker_mem_report(worker, sq);
250 mesh_report_reply(worker->env.mesh, e, reply_info, NETEVENT_NOERROR);
251 worker_mem_report(worker, sq);
255 /** ratelimit error replies
256 * @param worker: the worker struct with ratelimit counter
257 * @param err: error code that would be wanted.
258 * @return value of err if okay, or -1 if it should be discarded instead.
261 worker_err_ratelimit(struct worker* worker, int err)
263 if(worker->err_limit_time == *worker->env.now) {
264 /* see if limit is exceeded for this second */
265 if(worker->err_limit_count++ > ERROR_RATELIMIT)
268 /* new second, new limits */
269 worker->err_limit_time = *worker->env.now;
270 worker->err_limit_count = 1;
275 /** check request sanity.
276 * @param pkt: the wire packet to examine for sanity.
277 * @param worker: parameters for checking.
278 * @return error code, 0 OK, or -1 discard.
281 worker_check_request(sldns_buffer* pkt, struct worker* worker)
283 if(sldns_buffer_limit(pkt) < LDNS_HEADER_SIZE) {
284 verbose(VERB_QUERY, "request too short, discarded");
287 if(sldns_buffer_limit(pkt) > NORMAL_UDP_SIZE &&
288 worker->daemon->cfg->harden_large_queries) {
289 verbose(VERB_QUERY, "request too large, discarded");
292 if(LDNS_QR_WIRE(sldns_buffer_begin(pkt))) {
293 verbose(VERB_QUERY, "request has QR bit on, discarded");
296 if(LDNS_TC_WIRE(sldns_buffer_begin(pkt))) {
297 LDNS_TC_CLR(sldns_buffer_begin(pkt));
298 verbose(VERB_QUERY, "request bad, has TC bit on");
299 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
301 if(LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)) != LDNS_PACKET_QUERY) {
302 verbose(VERB_QUERY, "request unknown opcode %d",
303 LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)));
304 return worker_err_ratelimit(worker, LDNS_RCODE_NOTIMPL);
306 if(LDNS_QDCOUNT(sldns_buffer_begin(pkt)) != 1) {
307 verbose(VERB_QUERY, "request wrong nr qd=%d",
308 LDNS_QDCOUNT(sldns_buffer_begin(pkt)));
309 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
311 if(LDNS_ANCOUNT(sldns_buffer_begin(pkt)) != 0) {
312 verbose(VERB_QUERY, "request wrong nr an=%d",
313 LDNS_ANCOUNT(sldns_buffer_begin(pkt)));
314 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
316 if(LDNS_NSCOUNT(sldns_buffer_begin(pkt)) != 0) {
317 verbose(VERB_QUERY, "request wrong nr ns=%d",
318 LDNS_NSCOUNT(sldns_buffer_begin(pkt)));
319 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
321 if(LDNS_ARCOUNT(sldns_buffer_begin(pkt)) > 1) {
322 verbose(VERB_QUERY, "request wrong nr ar=%d",
323 LDNS_ARCOUNT(sldns_buffer_begin(pkt)));
324 return worker_err_ratelimit(worker, LDNS_RCODE_FORMERR);
330 worker_handle_control_cmd(struct tube* ATTR_UNUSED(tube), uint8_t* msg,
331 size_t len, int error, void* arg)
333 struct worker* worker = (struct worker*)arg;
334 enum worker_commands cmd;
335 if(error != NETEVENT_NOERROR) {
337 if(error == NETEVENT_CLOSED)
338 comm_base_exit(worker->base);
339 else log_info("control event: %d", error);
342 if(len != sizeof(uint32_t)) {
343 fatal_exit("bad control msg length %d", (int)len);
345 cmd = sldns_read_uint32(msg);
348 case worker_cmd_quit:
349 verbose(VERB_ALGO, "got control cmd quit");
350 comm_base_exit(worker->base);
352 case worker_cmd_stats:
353 verbose(VERB_ALGO, "got control cmd stats");
354 server_stats_reply(worker, 1);
356 case worker_cmd_stats_noreset:
357 verbose(VERB_ALGO, "got control cmd stats_noreset");
358 server_stats_reply(worker, 0);
360 case worker_cmd_remote:
361 verbose(VERB_ALGO, "got control cmd remote");
362 daemon_remote_exec(worker);
365 log_err("bad command %d", (int)cmd);
370 /** check if a delegation is secure */
371 static enum sec_status
372 check_delegation_secure(struct reply_info *rep)
374 /* return smallest security status */
376 enum sec_status sec = sec_status_secure;
378 size_t num = rep->an_numrrsets + rep->ns_numrrsets;
379 /* check if answer and authority are OK */
380 for(i=0; i<num; i++) {
381 s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
386 /* in additional, only unchecked triggers revalidation */
387 for(i=num; i<rep->rrset_count; i++) {
388 s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
390 if(s == sec_status_unchecked)
396 /** remove nonsecure from a delegation referral additional section */
398 deleg_remove_nonsecure_additional(struct reply_info* rep)
400 /* we can simply edit it, since we are working in the scratch region */
404 for(i = rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
405 s = ((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
407 if(s != sec_status_secure) {
408 memmove(rep->rrsets+i, rep->rrsets+i+1,
409 sizeof(struct ub_packed_rrset_key*)*
410 (rep->rrset_count - i - 1));
418 /** answer nonrecursive query from the cache */
420 answer_norec_from_cache(struct worker* worker, struct query_info* qinfo,
421 uint16_t id, uint16_t flags, struct comm_reply* repinfo,
422 struct edns_data* edns)
424 /* for a nonrecursive query return either:
425 * o an error (servfail; we try to avoid this)
426 * o a delegation (closest we have; this routine tries that)
427 * o the answer (checked by answer_from_cache)
429 * So, grab a delegation from the rrset cache.
430 * Then check if it needs validation, if so, this routine fails,
431 * so that iterator can prime and validator can verify rrsets.
433 uint16_t udpsize = edns->udp_size;
435 time_t timenow = *worker->env.now;
436 int must_validate = (!(flags&BIT_CD) || worker->env.cfg->ignore_cd)
437 && worker->env.need_to_validate;
438 struct dns_msg *msg = NULL;
441 dp = dns_cache_find_delegation(&worker->env, qinfo->qname,
442 qinfo->qname_len, qinfo->qtype, qinfo->qclass,
443 worker->scratchpad, &msg, timenow);
444 if(!dp) { /* no delegation, need to reprime */
447 /* In case we have a local alias, copy it into the delegation message.
448 * Shallow copy should be fine, as we'll be done with msg in this
450 msg->qinfo.local_alias = qinfo->local_alias;
452 switch(check_delegation_secure(msg->rep)) {
453 case sec_status_unchecked:
454 /* some rrsets have not been verified yet, go and
455 * let validator do that */
457 case sec_status_bogus:
458 /* some rrsets are bogus, reply servfail */
459 edns->edns_version = EDNS_ADVERTISED_VERSION;
460 edns->udp_size = EDNS_ADVERTISED_SIZE;
462 edns->bits &= EDNS_DO;
463 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL,
464 msg->rep, LDNS_RCODE_SERVFAIL, edns, worker->scratchpad))
466 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
467 &msg->qinfo, id, flags, edns);
468 if(worker->stats.extended) {
469 worker->stats.ans_bogus++;
470 worker->stats.ans_rcode[LDNS_RCODE_SERVFAIL]++;
473 case sec_status_secure:
474 /* all rrsets are secure */
475 /* remove non-secure rrsets from the add. section*/
476 if(worker->env.cfg->val_clean_additional)
477 deleg_remove_nonsecure_additional(msg->rep);
480 case sec_status_indeterminate:
481 case sec_status_insecure:
488 /* return this delegation from the cache */
489 edns->edns_version = EDNS_ADVERTISED_VERSION;
490 edns->udp_size = EDNS_ADVERTISED_SIZE;
492 edns->bits &= EDNS_DO;
493 if(!inplace_cb_reply_cache_call(&worker->env, qinfo, NULL, msg->rep,
494 (int)(flags&LDNS_RCODE_MASK), edns, worker->scratchpad))
496 msg->rep->flags |= BIT_QR|BIT_RA;
497 if(!reply_info_answer_encode(&msg->qinfo, msg->rep, id, flags,
498 repinfo->c->buffer, 0, 1, worker->scratchpad,
499 udpsize, edns, (int)(edns->bits & EDNS_DO), secure)) {
500 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL, NULL,
501 LDNS_RCODE_SERVFAIL, edns, worker->scratchpad))
502 edns->opt_list = NULL;
503 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
504 &msg->qinfo, id, flags, edns);
506 if(worker->stats.extended) {
507 if(secure) worker->stats.ans_secure++;
508 server_stats_insrcode(&worker->stats, repinfo->c->buffer);
513 /** answer query from the cache */
515 answer_from_cache(struct worker* worker, struct query_info* qinfo,
516 struct reply_info* rep, uint16_t id, uint16_t flags,
517 struct comm_reply* repinfo, struct edns_data* edns)
519 time_t timenow = *worker->env.now;
520 uint16_t udpsize = edns->udp_size;
522 int must_validate = (!(flags&BIT_CD) || worker->env.cfg->ignore_cd)
523 && worker->env.need_to_validate;
524 if(worker->env.cfg->serve_expired) {
525 /* always lock rrsets, rep->ttl is ignored */
526 if(!rrset_array_lock(rep->ref, rep->rrset_count, 0))
528 /* below, rrsets with ttl before timenow become TTL 0 in
530 /* This response was served with zero TTL */
531 if (timenow >= rep->ttl) {
532 worker->stats.zero_ttl_responses++;
535 /* see if it is possible */
536 if(rep->ttl < timenow) {
537 /* the rrsets may have been updated in the meantime.
538 * we will refetch the message format from the
539 * authoritative server
543 if(!rrset_array_lock(rep->ref, rep->rrset_count, timenow))
545 /* locked and ids and ttls are OK. */
547 /* check CNAME chain (if any) */
548 if(rep->an_numrrsets > 0 && (rep->rrsets[0]->rk.type ==
549 htons(LDNS_RR_TYPE_CNAME) || rep->rrsets[0]->rk.type ==
550 htons(LDNS_RR_TYPE_DNAME))) {
551 if(!reply_check_cname_chain(qinfo, rep)) {
552 /* cname chain invalid, redo iterator steps */
553 verbose(VERB_ALGO, "Cache reply: cname chain broken");
555 rrset_array_unlock_touch(worker->env.rrset_cache,
556 worker->scratchpad, rep->ref, rep->rrset_count);
560 /* check security status of the cached answer */
561 if( rep->security == sec_status_bogus && must_validate) {
563 edns->edns_version = EDNS_ADVERTISED_VERSION;
564 edns->udp_size = EDNS_ADVERTISED_SIZE;
566 edns->bits &= EDNS_DO;
567 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL, rep,
568 LDNS_RCODE_SERVFAIL, edns, worker->scratchpad))
570 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
571 qinfo, id, flags, edns);
572 rrset_array_unlock_touch(worker->env.rrset_cache,
573 worker->scratchpad, rep->ref, rep->rrset_count);
574 if(worker->stats.extended) {
575 worker->stats.ans_bogus ++;
576 worker->stats.ans_rcode[LDNS_RCODE_SERVFAIL] ++;
579 } else if( rep->security == sec_status_unchecked && must_validate) {
580 verbose(VERB_ALGO, "Cache reply: unchecked entry needs "
582 goto bail_out; /* need to validate cache entry first */
583 } else if(rep->security == sec_status_secure) {
584 if(reply_all_rrsets_secure(rep))
588 verbose(VERB_ALGO, "Cache reply: secure entry"
590 goto bail_out; /* rrset changed, re-verify */
596 edns->edns_version = EDNS_ADVERTISED_VERSION;
597 edns->udp_size = EDNS_ADVERTISED_SIZE;
599 edns->bits &= EDNS_DO;
600 if(!inplace_cb_reply_cache_call(&worker->env, qinfo, NULL, rep,
601 (int)(flags&LDNS_RCODE_MASK), edns, worker->scratchpad))
603 if(!reply_info_answer_encode(qinfo, rep, id, flags,
604 repinfo->c->buffer, timenow, 1, worker->scratchpad,
605 udpsize, edns, (int)(edns->bits & EDNS_DO), secure)) {
606 if(!inplace_cb_reply_servfail_call(&worker->env, qinfo, NULL, NULL,
607 LDNS_RCODE_SERVFAIL, edns, worker->scratchpad))
608 edns->opt_list = NULL;
609 error_encode(repinfo->c->buffer, LDNS_RCODE_SERVFAIL,
610 qinfo, id, flags, edns);
612 /* cannot send the reply right now, because blocking network syscall
613 * is bad while holding locks. */
614 rrset_array_unlock_touch(worker->env.rrset_cache, worker->scratchpad,
615 rep->ref, rep->rrset_count);
616 if(worker->stats.extended) {
617 if(secure) worker->stats.ans_secure++;
618 server_stats_insrcode(&worker->stats, repinfo->c->buffer);
620 /* go and return this buffer to the client */
624 /** Reply to client and perform prefetch to keep cache up to date */
626 reply_and_prefetch(struct worker* worker, struct query_info* qinfo,
627 uint16_t flags, struct comm_reply* repinfo, time_t leeway)
629 /* first send answer to client to keep its latency
630 * as small as a cachereply */
631 comm_point_send_reply(repinfo);
632 server_stats_prefetch(&worker->stats, worker);
634 /* create the prefetch in the mesh as a normal lookup without
635 * client addrs waiting, which has the cache blacklisted (to bypass
636 * the cache and go to the network for the data). */
637 /* this (potentially) runs the mesh for the new query */
638 mesh_new_prefetch(worker->env.mesh, qinfo, flags, leeway +
639 PREFETCH_EXPIRY_ADD);
643 * Fill CH class answer into buffer. Keeps query.
645 * @param str: string to put into text record (<255).
646 * @param edns: edns reply information.
647 * @param worker: worker with scratch region.
650 chaos_replystr(sldns_buffer* pkt, const char* str, struct edns_data* edns,
651 struct worker* worker)
653 size_t len = strlen(str);
654 unsigned int rd = LDNS_RD_WIRE(sldns_buffer_begin(pkt));
655 unsigned int cd = LDNS_CD_WIRE(sldns_buffer_begin(pkt));
656 if(len>255) len=255; /* cap size of TXT record */
657 sldns_buffer_clear(pkt);
658 sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip id */
659 sldns_buffer_write_u16(pkt, (uint16_t)(BIT_QR|BIT_RA));
660 if(rd) LDNS_RD_SET(sldns_buffer_begin(pkt));
661 if(cd) LDNS_CD_SET(sldns_buffer_begin(pkt));
662 sldns_buffer_write_u16(pkt, 1); /* qdcount */
663 sldns_buffer_write_u16(pkt, 1); /* ancount */
664 sldns_buffer_write_u16(pkt, 0); /* nscount */
665 sldns_buffer_write_u16(pkt, 0); /* arcount */
666 (void)query_dname_len(pkt); /* skip qname */
667 sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qtype */
668 sldns_buffer_skip(pkt, (ssize_t)sizeof(uint16_t)); /* skip qclass */
669 sldns_buffer_write_u16(pkt, 0xc00c); /* compr ptr to query */
670 sldns_buffer_write_u16(pkt, LDNS_RR_TYPE_TXT);
671 sldns_buffer_write_u16(pkt, LDNS_RR_CLASS_CH);
672 sldns_buffer_write_u32(pkt, 0); /* TTL */
673 sldns_buffer_write_u16(pkt, sizeof(uint8_t) + len);
674 sldns_buffer_write_u8(pkt, len);
675 sldns_buffer_write(pkt, str, len);
676 sldns_buffer_flip(pkt);
677 edns->edns_version = EDNS_ADVERTISED_VERSION;
678 edns->udp_size = EDNS_ADVERTISED_SIZE;
679 edns->bits &= EDNS_DO;
680 if(!inplace_cb_reply_local_call(&worker->env, NULL, NULL, NULL,
681 LDNS_RCODE_NOERROR, edns, worker->scratchpad))
682 edns->opt_list = NULL;
683 attach_edns_record(pkt, edns);
687 * Answer CH class queries.
689 * @param qinfo: query info. Pointer into packet buffer.
690 * @param edns: edns info from query.
691 * @param pkt: packet buffer.
692 * @return: true if a reply is to be sent.
695 answer_chaos(struct worker* w, struct query_info* qinfo,
696 struct edns_data* edns, sldns_buffer* pkt)
698 struct config_file* cfg = w->env.cfg;
699 if(qinfo->qtype != LDNS_RR_TYPE_ANY && qinfo->qtype != LDNS_RR_TYPE_TXT)
701 if(query_dname_compare(qinfo->qname,
702 (uint8_t*)"\002id\006server") == 0 ||
703 query_dname_compare(qinfo->qname,
704 (uint8_t*)"\010hostname\004bind") == 0)
706 if(cfg->hide_identity)
708 if(cfg->identity==NULL || cfg->identity[0]==0) {
709 char buf[MAXHOSTNAMELEN+1];
710 if (gethostname(buf, MAXHOSTNAMELEN) == 0) {
711 buf[MAXHOSTNAMELEN] = 0;
712 chaos_replystr(pkt, buf, edns, w);
714 log_err("gethostname: %s", strerror(errno));
715 chaos_replystr(pkt, "no hostname", edns, w);
718 else chaos_replystr(pkt, cfg->identity, edns, w);
721 if(query_dname_compare(qinfo->qname,
722 (uint8_t*)"\007version\006server") == 0 ||
723 query_dname_compare(qinfo->qname,
724 (uint8_t*)"\007version\004bind") == 0)
726 if(cfg->hide_version)
728 if(cfg->version==NULL || cfg->version[0]==0)
729 chaos_replystr(pkt, PACKAGE_STRING, edns, w);
730 else chaos_replystr(pkt, cfg->version, edns, w);
737 deny_refuse(struct comm_point* c, enum acl_access acl,
738 enum acl_access deny, enum acl_access refuse,
739 struct worker* worker, struct comm_reply* repinfo)
742 comm_point_drop_reply(repinfo);
743 if(worker->stats.extended)
744 worker->stats.unwanted_queries++;
746 } else if(acl == refuse) {
747 log_addr(VERB_ALGO, "refused query from",
748 &repinfo->addr, repinfo->addrlen);
749 log_buf(VERB_ALGO, "refuse", c->buffer);
750 if(worker->stats.extended)
751 worker->stats.unwanted_queries++;
752 if(worker_check_request(c->buffer, worker) == -1) {
753 comm_point_drop_reply(repinfo);
754 return 0; /* discard this */
756 sldns_buffer_set_limit(c->buffer, LDNS_HEADER_SIZE);
757 sldns_buffer_write_at(c->buffer, 4,
758 (uint8_t*)"\0\0\0\0\0\0\0\0", 8);
759 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
760 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
762 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
763 sldns_buffer_flip(c->buffer);
771 deny_refuse_all(struct comm_point* c, enum acl_access acl,
772 struct worker* worker, struct comm_reply* repinfo)
774 return deny_refuse(c, acl, acl_deny, acl_refuse, worker, repinfo);
778 deny_refuse_non_local(struct comm_point* c, enum acl_access acl,
779 struct worker* worker, struct comm_reply* repinfo)
781 return deny_refuse(c, acl, acl_deny_non_local, acl_refuse_non_local, worker, repinfo);
785 worker_handle_request(struct comm_point* c, void* arg, int error,
786 struct comm_reply* repinfo)
788 struct worker* worker = (struct worker*)arg;
791 struct lruhash_entry* e;
792 struct query_info qinfo;
793 struct edns_data edns;
795 struct acl_addr* acladdr;
798 if(error != NETEVENT_NOERROR) {
799 /* some bad tcp query DNS formats give these error calls */
800 verbose(VERB_ALGO, "handle request called with err=%d", error);
804 if(worker->dtenv.log_client_query_messages)
805 dt_msg_send_client_query(&worker->dtenv, &repinfo->addr, c->type,
808 acladdr = acl_addr_lookup(worker->daemon->acl, &repinfo->addr,
810 acl = acl_get_control(acladdr);
811 if((ret=deny_refuse_all(c, acl, worker, repinfo)) != -1)
817 if((ret=worker_check_request(c->buffer, worker)) != 0) {
818 verbose(VERB_ALGO, "worker check request: bad query.");
819 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
821 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
822 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
825 comm_point_drop_reply(repinfo);
829 worker->stats.num_queries++;
831 /* check if this query should be dropped based on source ip rate limiting */
832 if(!infra_ip_ratelimit_inc(worker->env.infra_cache, repinfo,
834 /* See if we are passed through with slip factor */
835 if(worker->env.cfg->ip_ratelimit_factor != 0 &&
836 ub_random_max(worker->env.rnd,
837 worker->env.cfg->ip_ratelimit_factor) == 1) {
840 addr_to_str(&repinfo->addr, repinfo->addrlen,
841 addrbuf, sizeof(addrbuf));
842 verbose(VERB_OPS, "ip_ratelimit allowed through for ip address %s ",
845 worker->stats.num_queries_ip_ratelimited++;
846 comm_point_drop_reply(repinfo);
851 /* see if query is in the cache */
852 if(!query_info_parse(&qinfo, c->buffer)) {
853 verbose(VERB_ALGO, "worker parse request: formerror.");
854 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
855 if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) {
856 comm_point_drop_reply(repinfo);
859 sldns_buffer_rewind(c->buffer);
860 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
861 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
863 server_stats_insrcode(&worker->stats, c->buffer);
866 if(worker->env.cfg->log_queries) {
868 addr_to_str(&repinfo->addr, repinfo->addrlen, ip, sizeof(ip));
869 log_nametypeclass(0, ip, qinfo.qname, qinfo.qtype, qinfo.qclass);
871 if(qinfo.qtype == LDNS_RR_TYPE_AXFR ||
872 qinfo.qtype == LDNS_RR_TYPE_IXFR) {
873 verbose(VERB_ALGO, "worker request: refused zone transfer.");
874 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
875 sldns_buffer_rewind(c->buffer);
876 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
877 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
879 if(worker->stats.extended) {
880 worker->stats.qtype[qinfo.qtype]++;
881 server_stats_insrcode(&worker->stats, c->buffer);
885 if(qinfo.qtype == LDNS_RR_TYPE_OPT ||
886 qinfo.qtype == LDNS_RR_TYPE_TSIG ||
887 qinfo.qtype == LDNS_RR_TYPE_TKEY ||
888 qinfo.qtype == LDNS_RR_TYPE_MAILA ||
889 qinfo.qtype == LDNS_RR_TYPE_MAILB ||
890 (qinfo.qtype >= 128 && qinfo.qtype <= 248)) {
891 verbose(VERB_ALGO, "worker request: formerror for meta-type.");
892 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
893 if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) {
894 comm_point_drop_reply(repinfo);
897 sldns_buffer_rewind(c->buffer);
898 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
899 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
901 if(worker->stats.extended) {
902 worker->stats.qtype[qinfo.qtype]++;
903 server_stats_insrcode(&worker->stats, c->buffer);
907 if((ret=parse_edns_from_pkt(c->buffer, &edns, worker->scratchpad)) != 0) {
908 struct edns_data reply_edns;
909 verbose(VERB_ALGO, "worker parse edns: formerror.");
910 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
911 memset(&reply_edns, 0, sizeof(reply_edns));
912 reply_edns.edns_present = 1;
913 reply_edns.udp_size = EDNS_ADVERTISED_SIZE;
914 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), ret);
915 error_encode(c->buffer, ret, &qinfo,
916 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
917 sldns_buffer_read_u16_at(c->buffer, 2), &reply_edns);
918 regional_free_all(worker->scratchpad);
919 server_stats_insrcode(&worker->stats, c->buffer);
922 if(edns.edns_present && edns.edns_version != 0) {
923 edns.ext_rcode = (uint8_t)(EDNS_RCODE_BADVERS>>4);
924 edns.edns_version = EDNS_ADVERTISED_VERSION;
925 edns.udp_size = EDNS_ADVERTISED_SIZE;
926 edns.bits &= EDNS_DO;
927 edns.opt_list = NULL;
928 verbose(VERB_ALGO, "query with bad edns version.");
929 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
930 error_encode(c->buffer, EDNS_RCODE_BADVERS&0xf, &qinfo,
931 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
932 sldns_buffer_read_u16_at(c->buffer, 2), NULL);
933 attach_edns_record(c->buffer, &edns);
934 regional_free_all(worker->scratchpad);
937 if(edns.edns_present && edns.udp_size < NORMAL_UDP_SIZE &&
938 worker->daemon->cfg->harden_short_bufsize) {
939 verbose(VERB_QUERY, "worker request: EDNS bufsize %d ignored",
941 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
942 edns.udp_size = NORMAL_UDP_SIZE;
944 if(edns.udp_size > worker->daemon->cfg->max_udp_size &&
945 c->type == comm_udp) {
947 "worker request: max UDP reply size modified"
948 " (%d to max-udp-size)", (int)edns.udp_size);
949 log_addr(VERB_CLIENT,"from",&repinfo->addr, repinfo->addrlen);
950 edns.udp_size = worker->daemon->cfg->max_udp_size;
952 if(edns.udp_size < LDNS_HEADER_SIZE) {
953 verbose(VERB_ALGO, "worker request: edns is too small.");
954 log_addr(VERB_CLIENT, "from", &repinfo->addr, repinfo->addrlen);
955 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
956 LDNS_TC_SET(sldns_buffer_begin(c->buffer));
957 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
958 LDNS_RCODE_SERVFAIL);
959 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE);
960 sldns_buffer_write_at(c->buffer, 4,
961 (uint8_t*)"\0\0\0\0\0\0\0\0", 8);
962 sldns_buffer_flip(c->buffer);
963 regional_free_all(worker->scratchpad);
966 if(worker->stats.extended)
967 server_stats_insquery(&worker->stats, c, qinfo.qtype,
968 qinfo.qclass, &edns, repinfo);
969 if(c->type != comm_udp)
970 edns.udp_size = 65535; /* max size for TCP replies */
971 if(qinfo.qclass == LDNS_RR_CLASS_CH && answer_chaos(worker, &qinfo,
973 server_stats_insrcode(&worker->stats, c->buffer);
974 regional_free_all(worker->scratchpad);
977 if(local_zones_answer(worker->daemon->local_zones, &worker->env, &qinfo,
978 &edns, c->buffer, worker->scratchpad, repinfo, acladdr->taglist,
979 acladdr->taglen, acladdr->tag_actions,
980 acladdr->tag_actions_size, acladdr->tag_datas,
981 acladdr->tag_datas_size, worker->daemon->cfg->tagname,
982 worker->daemon->cfg->num_tags, acladdr->view)) {
983 regional_free_all(worker->scratchpad);
984 if(sldns_buffer_limit(c->buffer) == 0) {
985 comm_point_drop_reply(repinfo);
988 server_stats_insrcode(&worker->stats, c->buffer);
992 /* We've looked in our local zones. If the answer isn't there, we
993 * might need to bail out based on ACLs now. */
994 if((ret=deny_refuse_non_local(c, acl, worker, repinfo)) != -1)
996 regional_free_all(worker->scratchpad);
1002 /* If this request does not have the recursion bit set, verify
1003 * ACLs allow the snooping. */
1004 if(!(LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) &&
1005 acl != acl_allow_snoop ) {
1006 sldns_buffer_set_limit(c->buffer, LDNS_HEADER_SIZE);
1007 sldns_buffer_write_at(c->buffer, 4,
1008 (uint8_t*)"\0\0\0\0\0\0\0\0", 8);
1009 LDNS_QR_SET(sldns_buffer_begin(c->buffer));
1010 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer),
1011 LDNS_RCODE_REFUSED);
1012 sldns_buffer_flip(c->buffer);
1013 regional_free_all(worker->scratchpad);
1014 server_stats_insrcode(&worker->stats, c->buffer);
1015 log_addr(VERB_ALGO, "refused nonrec (cache snoop) query from",
1016 &repinfo->addr, repinfo->addrlen);
1020 /* If we've found a local alias, replace the qname with the alias
1021 * target before resolving it. */
1022 if(qinfo.local_alias) {
1023 struct ub_packed_rrset_key* rrset = qinfo.local_alias->rrset;
1024 struct packed_rrset_data* d = rrset->entry.data;
1026 /* Sanity check: our current implementation only supports
1027 * a single CNAME RRset as a local alias. */
1028 if(qinfo.local_alias->next ||
1029 rrset->rk.type != htons(LDNS_RR_TYPE_CNAME) ||
1031 log_err("assumption failure: unexpected local alias");
1032 regional_free_all(worker->scratchpad);
1033 return 0; /* drop it */
1035 qinfo.qname = d->rr_data[0] + 2;
1036 qinfo.qname_len = d->rr_len[0] - 2;
1039 if(!edns_bypass_cache_stage(edns.opt_list, &worker->env)) {
1040 h = query_info_hash(&qinfo, sldns_buffer_read_u16_at(c->buffer, 2));
1041 if((e=slabhash_lookup(worker->env.msg_cache, h, &qinfo, 0))) {
1042 /* answer from cache - we have acquired a readlock on it */
1043 if(answer_from_cache(worker, &qinfo,
1044 (struct reply_info*)e->data,
1045 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1046 sldns_buffer_read_u16_at(c->buffer, 2), repinfo,
1048 /* prefetch it if the prefetch TTL expired */
1049 if((worker->env.cfg->prefetch || worker->env.cfg->serve_expired)
1050 && *worker->env.now >=
1051 ((struct reply_info*)e->data)->prefetch_ttl) {
1052 time_t leeway = ((struct reply_info*)e->
1053 data)->ttl - *worker->env.now;
1054 if(((struct reply_info*)e->data)->ttl
1057 lock_rw_unlock(&e->lock);
1058 reply_and_prefetch(worker, &qinfo,
1059 sldns_buffer_read_u16_at(c->buffer, 2),
1062 regional_free_all(worker->scratchpad);
1065 lock_rw_unlock(&e->lock);
1066 regional_free_all(worker->scratchpad);
1069 verbose(VERB_ALGO, "answer from the cache failed");
1070 lock_rw_unlock(&e->lock);
1072 if(!LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) {
1073 if(answer_norec_from_cache(worker, &qinfo,
1074 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer),
1075 sldns_buffer_read_u16_at(c->buffer, 2), repinfo,
1077 regional_free_all(worker->scratchpad);
1080 verbose(VERB_ALGO, "answer norec from cache -- "
1081 "need to validate or not primed");
1084 sldns_buffer_rewind(c->buffer);
1085 server_stats_querymiss(&worker->stats, worker);
1087 if(verbosity >= VERB_CLIENT) {
1088 if(c->type == comm_udp)
1089 log_addr(VERB_CLIENT, "udp request from",
1090 &repinfo->addr, repinfo->addrlen);
1091 else log_addr(VERB_CLIENT, "tcp request from",
1092 &repinfo->addr, repinfo->addrlen);
1095 /* grab a work request structure for this new request */
1096 mesh_new_client(worker->env.mesh, &qinfo,
1097 sldns_buffer_read_u16_at(c->buffer, 2),
1098 &edns, repinfo, *(uint16_t*)(void *)sldns_buffer_begin(c->buffer));
1099 regional_free_all(worker->scratchpad);
1100 worker_mem_report(worker, NULL);
1107 if(worker->dtenv.log_client_response_messages)
1108 dt_msg_send_client_response(&worker->dtenv, &repinfo->addr,
1109 c->type, c->buffer);
1111 if(worker->env.cfg->log_replies)
1113 struct timeval tv = {0, 0};
1114 log_reply_info(0, &qinfo, &repinfo->addr, repinfo->addrlen,
1121 worker_sighandler(int sig, void* arg)
1123 /* note that log, print, syscalls here give race conditions.
1124 * And cause hangups if the log-lock is held by the application. */
1125 struct worker* worker = (struct worker*)arg;
1129 comm_base_exit(worker->base);
1133 worker->need_to_exit = 1;
1134 comm_base_exit(worker->base);
1138 worker->need_to_exit = 1;
1139 comm_base_exit(worker->base);
1143 worker->need_to_exit = 1;
1144 comm_base_exit(worker->base);
1147 /* unknown signal, ignored */
1152 /** restart statistics timer for worker, if enabled */
1154 worker_restart_timer(struct worker* worker)
1156 if(worker->env.cfg->stat_interval > 0) {
1159 tv.tv_sec = worker->env.cfg->stat_interval;
1162 comm_timer_set(worker->stat_timer, &tv);
1166 void worker_stat_timer_cb(void* arg)
1168 struct worker* worker = (struct worker*)arg;
1169 server_stats_log(&worker->stats, worker, worker->thread_num);
1170 mesh_stats(worker->env.mesh, "mesh has");
1171 worker_mem_report(worker, NULL);
1172 if(!worker->daemon->cfg->stat_cumulative) {
1173 worker_stats_clear(worker);
1175 /* start next timer */
1176 worker_restart_timer(worker);
1179 void worker_probe_timer_cb(void* arg)
1181 struct worker* worker = (struct worker*)arg;
1184 tv.tv_sec = (time_t)autr_probe_timer(&worker->env);
1188 comm_timer_set(worker->env.probe_timer, &tv);
1192 worker_create(struct daemon* daemon, int id, int* ports, int n)
1195 struct worker* worker = (struct worker*)calloc(1,
1196 sizeof(struct worker));
1199 worker->numports = n;
1200 worker->ports = (int*)memdup(ports, sizeof(int)*n);
1201 if(!worker->ports) {
1205 worker->daemon = daemon;
1206 worker->thread_num = id;
1207 if(!(worker->cmd = tube_create())) {
1208 free(worker->ports);
1212 /* create random state here to avoid locking trouble in RAND_bytes */
1213 seed = (unsigned int)time(NULL) ^ (unsigned int)getpid() ^
1214 (((unsigned int)worker->thread_num)<<17);
1215 /* shift thread_num so it does not match out pid bits */
1216 if(!(worker->rndstate = ub_initstate(seed, daemon->rand))) {
1218 log_err("could not init random numbers.");
1219 tube_delete(worker->cmd);
1220 free(worker->ports);
1226 if(daemon->cfg->dnstap) {
1227 log_assert(daemon->dtenv != NULL);
1228 memcpy(&worker->dtenv, daemon->dtenv, sizeof(struct dt_env));
1229 if(!dt_init(&worker->dtenv))
1230 fatal_exit("dt_init failed");
1237 worker_init(struct worker* worker, struct config_file *cfg,
1238 struct listen_port* ports, int do_sigs)
1241 struct dt_env* dtenv = &worker->dtenv;
1245 worker->need_to_exit = 0;
1246 worker->base = comm_base_create(do_sigs);
1248 log_err("could not create event handling base");
1249 worker_delete(worker);
1252 comm_base_set_slow_accept_handlers(worker->base, &worker_stop_accept,
1253 &worker_start_accept, worker);
1256 ub_thread_sig_unblock(SIGHUP);
1258 ub_thread_sig_unblock(SIGINT);
1260 ub_thread_sig_unblock(SIGQUIT);
1262 ub_thread_sig_unblock(SIGTERM);
1263 #ifndef LIBEVENT_SIGNAL_PROBLEM
1264 worker->comsig = comm_signal_create(worker->base,
1265 worker_sighandler, worker);
1268 || !comm_signal_bind(worker->comsig, SIGHUP)
1271 || !comm_signal_bind(worker->comsig, SIGQUIT)
1273 || !comm_signal_bind(worker->comsig, SIGTERM)
1274 || !comm_signal_bind(worker->comsig, SIGINT)) {
1275 log_err("could not create signal handlers");
1276 worker_delete(worker);
1279 #endif /* LIBEVENT_SIGNAL_PROBLEM */
1280 if(!daemon_remote_open_accept(worker->daemon->rc,
1281 worker->daemon->rc_ports, worker)) {
1282 worker_delete(worker);
1285 #ifdef UB_ON_WINDOWS
1286 wsvc_setup_worker(worker);
1287 #endif /* UB_ON_WINDOWS */
1288 } else { /* !do_sigs */
1289 worker->comsig = NULL;
1291 worker->front = listen_create(worker->base, ports,
1292 cfg->msg_buffer_size, (int)cfg->incoming_num_tcp,
1293 worker->daemon->listen_sslctx, dtenv, worker_handle_request,
1295 if(!worker->front) {
1296 log_err("could not create listening sockets");
1297 worker_delete(worker);
1300 worker->back = outside_network_create(worker->base,
1301 cfg->msg_buffer_size, (size_t)cfg->outgoing_num_ports,
1302 cfg->out_ifs, cfg->num_out_ifs, cfg->do_ip4, cfg->do_ip6,
1303 cfg->do_tcp?cfg->outgoing_num_tcp:0,
1304 worker->daemon->env->infra_cache, worker->rndstate,
1305 cfg->use_caps_bits_for_id, worker->ports, worker->numports,
1306 cfg->unwanted_threshold, cfg->outgoing_tcp_mss,
1307 &worker_alloc_cleanup, worker,
1308 cfg->do_udp, worker->daemon->connect_sslctx, cfg->delay_close,
1311 log_err("could not create outgoing sockets");
1312 worker_delete(worker);
1315 /* start listening to commands */
1316 if(!tube_setup_bg_listen(worker->cmd, worker->base,
1317 &worker_handle_control_cmd, worker)) {
1318 log_err("could not create control compt.");
1319 worker_delete(worker);
1322 worker->stat_timer = comm_timer_create(worker->base,
1323 worker_stat_timer_cb, worker);
1324 if(!worker->stat_timer) {
1325 log_err("could not create statistics timer");
1328 /* we use the msg_buffer_size as a good estimate for what the
1329 * user wants for memory usage sizes */
1330 worker->scratchpad = regional_create_custom(cfg->msg_buffer_size);
1331 if(!worker->scratchpad) {
1332 log_err("malloc failure");
1333 worker_delete(worker);
1337 server_stats_init(&worker->stats, cfg);
1338 alloc_init(&worker->alloc, &worker->daemon->superalloc,
1339 worker->thread_num);
1340 alloc_set_id_cleanup(&worker->alloc, &worker_alloc_cleanup, worker);
1341 worker->env = *worker->daemon->env;
1342 comm_base_timept(worker->base, &worker->env.now, &worker->env.now_tv);
1343 if(worker->thread_num == 0)
1344 log_set_time(worker->env.now);
1345 worker->env.worker = worker;
1346 worker->env.send_query = &worker_send_query;
1347 worker->env.alloc = &worker->alloc;
1348 worker->env.rnd = worker->rndstate;
1349 worker->env.scratch = worker->scratchpad;
1350 worker->env.mesh = mesh_create(&worker->daemon->mods, &worker->env);
1351 worker->env.detach_subs = &mesh_detach_subs;
1352 worker->env.attach_sub = &mesh_attach_sub;
1353 worker->env.kill_sub = &mesh_state_delete;
1354 worker->env.detect_cycle = &mesh_detect_cycle;
1355 worker->env.scratch_buffer = sldns_buffer_new(cfg->msg_buffer_size);
1356 if(!(worker->env.fwds = forwards_create()) ||
1357 !forwards_apply_cfg(worker->env.fwds, cfg)) {
1358 log_err("Could not set forward zones");
1359 worker_delete(worker);
1362 if(!(worker->env.hints = hints_create()) ||
1363 !hints_apply_cfg(worker->env.hints, cfg)) {
1364 log_err("Could not set root or stub hints");
1365 worker_delete(worker);
1368 /* one probe timer per process -- if we have 5011 anchors */
1369 if(autr_get_num_anchors(worker->env.anchors) > 0
1370 #ifndef THREADS_DISABLED
1371 && worker->thread_num == 0
1377 worker->env.probe_timer = comm_timer_create(worker->base,
1378 worker_probe_timer_cb, worker);
1379 if(!worker->env.probe_timer) {
1380 log_err("could not create 5011-probe timer");
1382 /* let timer fire, then it can reset itself */
1383 comm_timer_set(worker->env.probe_timer, &tv);
1386 if(!worker->env.mesh || !worker->env.scratch_buffer) {
1387 worker_delete(worker);
1390 worker_mem_report(worker, NULL);
1391 /* if statistics enabled start timer */
1392 if(worker->env.cfg->stat_interval > 0) {
1393 verbose(VERB_ALGO, "set statistics interval %d secs",
1394 worker->env.cfg->stat_interval);
1395 worker_restart_timer(worker);
1401 worker_work(struct worker* worker)
1403 comm_base_dispatch(worker->base);
1407 worker_delete(struct worker* worker)
1411 if(worker->env.mesh && verbosity >= VERB_OPS) {
1412 server_stats_log(&worker->stats, worker, worker->thread_num);
1413 mesh_stats(worker->env.mesh, "mesh has");
1414 worker_mem_report(worker, NULL);
1416 outside_network_quit_prepare(worker->back);
1417 mesh_delete(worker->env.mesh);
1418 sldns_buffer_free(worker->env.scratch_buffer);
1419 forwards_delete(worker->env.fwds);
1420 hints_delete(worker->env.hints);
1421 listen_delete(worker->front);
1422 outside_network_delete(worker->back);
1423 comm_signal_delete(worker->comsig);
1424 tube_delete(worker->cmd);
1425 comm_timer_delete(worker->stat_timer);
1426 comm_timer_delete(worker->env.probe_timer);
1427 free(worker->ports);
1428 if(worker->thread_num == 0) {
1430 #ifdef UB_ON_WINDOWS
1431 wsvc_desetup_worker(worker);
1432 #endif /* UB_ON_WINDOWS */
1434 comm_base_delete(worker->base);
1435 ub_randfree(worker->rndstate);
1436 alloc_clear(&worker->alloc);
1437 regional_destroy(worker->scratchpad);
1441 struct outbound_entry*
1442 worker_send_query(struct query_info* qinfo, uint16_t flags, int dnssec,
1443 int want_dnssec, int nocaps, struct sockaddr_storage* addr,
1444 socklen_t addrlen, uint8_t* zone, size_t zonelen, int ssl_upstream,
1445 struct module_qstate* q)
1447 struct worker* worker = q->env->worker;
1448 struct outbound_entry* e = (struct outbound_entry*)regional_alloc(
1449 q->region, sizeof(*e));
1453 e->qsent = outnet_serviced_query(worker->back, qinfo, flags, dnssec,
1454 want_dnssec, nocaps, q->env->cfg->tcp_upstream,
1455 ssl_upstream, addr, addrlen, zone, zonelen, q,
1456 worker_handle_service_reply, e, worker->back->udp_buff, q->env);
1464 worker_alloc_cleanup(void* arg)
1466 struct worker* worker = (struct worker*)arg;
1467 slabhash_clear(&worker->env.rrset_cache->table);
1468 slabhash_clear(worker->env.msg_cache);
1471 void worker_stats_clear(struct worker* worker)
1473 server_stats_init(&worker->stats, worker->env.cfg);
1474 mesh_stats_clear(worker->env.mesh);
1475 worker->back->unwanted_replies = 0;
1476 worker->back->num_tcp_outgoing = 0;
1479 void worker_start_accept(void* arg)
1481 struct worker* worker = (struct worker*)arg;
1482 listen_start_accept(worker->front);
1483 if(worker->thread_num == 0)
1484 daemon_remote_start_accept(worker->daemon->rc);
1487 void worker_stop_accept(void* arg)
1489 struct worker* worker = (struct worker*)arg;
1490 listen_stop_accept(worker->front);
1491 if(worker->thread_num == 0)
1492 daemon_remote_stop_accept(worker->daemon->rc);
1495 /* --- fake callbacks for fptr_wlist to work --- */
1496 struct outbound_entry* libworker_send_query(
1497 struct query_info* ATTR_UNUSED(qinfo),
1498 uint16_t ATTR_UNUSED(flags), int ATTR_UNUSED(dnssec),
1499 int ATTR_UNUSED(want_dnssec), int ATTR_UNUSED(nocaps),
1500 struct sockaddr_storage* ATTR_UNUSED(addr), socklen_t ATTR_UNUSED(addrlen),
1501 uint8_t* ATTR_UNUSED(zone), size_t ATTR_UNUSED(zonelen),
1502 int ATTR_UNUSED(ssl_upstream), struct module_qstate* ATTR_UNUSED(q))
1508 int libworker_handle_reply(struct comm_point* ATTR_UNUSED(c),
1509 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1510 struct comm_reply* ATTR_UNUSED(reply_info))
1516 int libworker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
1517 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1518 struct comm_reply* ATTR_UNUSED(reply_info))
1524 void libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
1525 uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
1526 int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
1531 void libworker_fg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1532 sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1533 char* ATTR_UNUSED(why_bogus))
1538 void libworker_bg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1539 sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1540 char* ATTR_UNUSED(why_bogus))
1545 void libworker_event_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1546 sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1547 char* ATTR_UNUSED(why_bogus))
1552 int context_query_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1558 int order_lock_cmp(const void* ATTR_UNUSED(e1), const void* ATTR_UNUSED(e2))
1564 int codeline_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))