2 * libunbound/worker.c - worker thread or process that resolves
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 contains the worker process or thread that performs
40 * the DNS resolving and validation. The worker is called by a procedure
41 * and if in the background continues until exit, if in the foreground
42 * returns from the procedure when done.
46 #include <openssl/ssl.h>
48 #include "libunbound/libworker.h"
49 #include "libunbound/context.h"
50 #include "libunbound/unbound.h"
51 #include "libunbound/worker.h"
52 #include "libunbound/unbound-event.h"
53 #include "services/outside_network.h"
54 #include "services/mesh.h"
55 #include "services/localzone.h"
56 #include "services/cache/rrset.h"
57 #include "services/outbound_list.h"
58 #include "services/authzone.h"
59 #include "util/fptr_wlist.h"
60 #include "util/module.h"
61 #include "util/regional.h"
62 #include "util/random.h"
63 #include "util/config_file.h"
64 #include "util/netevent.h"
65 #include "util/storage/lookup3.h"
66 #include "util/storage/slabhash.h"
67 #include "util/net_help.h"
68 #include "util/data/dname.h"
69 #include "util/data/msgreply.h"
70 #include "util/data/msgencode.h"
71 #include "util/tube.h"
72 #include "iterator/iter_fwd.h"
73 #include "iterator/iter_hints.h"
74 #include "sldns/sbuffer.h"
75 #include "sldns/str2wire.h"
77 /** handle new query command for bg worker */
78 static void handle_newq(struct libworker* w, uint8_t* buf, uint32_t len);
80 /** delete libworker env */
82 libworker_delete_env(struct libworker* w)
85 outside_network_quit_prepare(w->back);
86 mesh_delete(w->env->mesh);
87 context_release_alloc(w->ctx, w->env->alloc,
88 !w->is_bg || w->is_bg_thread);
89 sldns_buffer_free(w->env->scratch_buffer);
90 regional_destroy(w->env->scratch);
91 forwards_delete(w->env->fwds);
92 hints_delete(w->env->hints);
93 ub_randfree(w->env->rnd);
97 SSL_CTX_free(w->sslctx);
99 outside_network_delete(w->back);
102 /** delete libworker struct */
104 libworker_delete(struct libworker* w)
107 libworker_delete_env(w);
108 comm_base_delete(w->base);
113 libworker_delete_event(struct libworker* w)
116 libworker_delete_env(w);
117 comm_base_delete_no_base(w->base);
121 /** setup fresh libworker struct */
122 static struct libworker*
123 libworker_setup(struct ub_ctx* ctx, int is_bg, struct ub_event_base* eb)
126 struct libworker* w = (struct libworker*)calloc(1, sizeof(*w));
127 struct config_file* cfg = ctx->env->cfg;
133 w->env = (struct module_env*)malloc(sizeof(*w->env));
139 w->env->alloc = context_obtain_alloc(ctx, !w->is_bg || w->is_bg_thread);
144 w->thread_num = w->env->alloc->thread_num;
145 alloc_set_id_cleanup(w->env->alloc, &libworker_alloc_cleanup, w);
146 if(!w->is_bg || w->is_bg_thread) {
147 lock_basic_lock(&ctx->cfglock);
149 w->env->scratch = regional_create_custom(cfg->msg_buffer_size);
150 w->env->scratch_buffer = sldns_buffer_new(cfg->msg_buffer_size);
151 w->env->fwds = forwards_create();
152 if(w->env->fwds && !forwards_apply_cfg(w->env->fwds, cfg)) {
153 forwards_delete(w->env->fwds);
156 w->env->hints = hints_create();
157 if(w->env->hints && !hints_apply_cfg(w->env->hints, cfg)) {
158 hints_delete(w->env->hints);
159 w->env->hints = NULL;
161 if(cfg->ssl_upstream) {
162 w->sslctx = connect_sslctx_create(NULL, NULL,
163 cfg->tls_cert_bundle);
165 /* to make the setup fail after unlock */
166 hints_delete(w->env->hints);
167 w->env->hints = NULL;
170 if(!w->is_bg || w->is_bg_thread) {
171 lock_basic_unlock(&ctx->cfglock);
173 if(!w->env->scratch || !w->env->scratch_buffer || !w->env->fwds ||
178 w->env->worker = (struct worker*)w;
179 w->env->probe_timer = NULL;
180 seed = (unsigned int)time(NULL) ^ (unsigned int)getpid() ^
181 (((unsigned int)w->thread_num)<<17);
182 seed ^= (unsigned int)w->env->alloc->next_id;
183 if(!w->is_bg || w->is_bg_thread) {
184 lock_basic_lock(&ctx->cfglock);
186 if(!(w->env->rnd = ub_initstate(seed, ctx->seed_rnd))) {
187 if(!w->is_bg || w->is_bg_thread) {
188 lock_basic_unlock(&ctx->cfglock);
194 if(!w->is_bg || w->is_bg_thread) {
195 lock_basic_unlock(&ctx->cfglock);
198 /* primitive lockout for threading: if it overwrites another
199 * thread it is like wiping the cache (which is likely empty
201 /* note we are holding the ctx lock in normal threaded
202 * cases so that is solved properly, it is only for many ctx
203 * in different threads that this may clash */
204 static int done_raninit = 0;
207 hash_set_raninit((uint32_t)ub_random(w->env->rnd));
213 w->base = comm_base_create_event(eb);
214 else w->base = comm_base_create(0);
219 w->env->worker_base = w->base;
220 if(!w->is_bg || w->is_bg_thread) {
221 lock_basic_lock(&ctx->cfglock);
223 numports = cfg_condense_ports(cfg, &ports);
225 int locked = !w->is_bg || w->is_bg_thread;
228 lock_basic_unlock(&ctx->cfglock);
232 w->back = outside_network_create(w->base, cfg->msg_buffer_size,
233 (size_t)cfg->outgoing_num_ports, cfg->out_ifs,
234 cfg->num_out_ifs, cfg->do_ip4, cfg->do_ip6,
235 cfg->do_tcp?cfg->outgoing_num_tcp:0,
236 w->env->infra_cache, w->env->rnd, cfg->use_caps_bits_for_id,
237 ports, numports, cfg->unwanted_threshold,
238 cfg->outgoing_tcp_mss, &libworker_alloc_cleanup, w,
239 cfg->do_udp || cfg->udp_upstream_without_downstream, w->sslctx,
240 cfg->delay_close, NULL);
241 w->env->outnet = w->back;
242 if(!w->is_bg || w->is_bg_thread) {
243 lock_basic_unlock(&ctx->cfglock);
250 w->env->mesh = mesh_create(&ctx->mods, w->env);
255 w->env->send_query = &libworker_send_query;
256 w->env->detach_subs = &mesh_detach_subs;
257 w->env->attach_sub = &mesh_attach_sub;
258 w->env->add_sub = &mesh_add_sub;
259 w->env->kill_sub = &mesh_state_delete;
260 w->env->detect_cycle = &mesh_detect_cycle;
261 comm_base_timept(w->base, &w->env->now, &w->env->now_tv);
265 struct libworker* libworker_create_event(struct ub_ctx* ctx,
266 struct ub_event_base* eb)
268 return libworker_setup(ctx, 0, eb);
271 /** handle cancel command for bg worker */
273 handle_cancel(struct libworker* w, uint8_t* buf, uint32_t len)
276 if(w->is_bg_thread) {
277 lock_basic_lock(&w->ctx->cfglock);
278 q = context_deserialize_cancel(w->ctx, buf, len);
279 lock_basic_unlock(&w->ctx->cfglock);
281 q = context_deserialize_cancel(w->ctx, buf, len);
284 /* probably simply lookup failed, i.e. the message had been
285 * processed and answered before the cancel arrived */
292 /** do control command coming into bg server */
294 libworker_do_cmd(struct libworker* w, uint8_t* msg, uint32_t len)
296 switch(context_serial_getcmd(msg, len)) {
298 case UB_LIBCMD_ANSWER:
299 log_err("unknown command for bg worker %d",
300 (int)context_serial_getcmd(msg, len));
301 /* and fall through to quit */
305 comm_base_exit(w->base);
307 case UB_LIBCMD_NEWQUERY:
308 handle_newq(w, msg, len);
310 case UB_LIBCMD_CANCEL:
311 handle_cancel(w, msg, len);
316 /** handle control command coming into server */
318 libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
319 uint8_t* msg, size_t len, int err, void* arg)
321 struct libworker* w = (struct libworker*)arg;
325 /* it is of no use to go on, exit */
326 comm_base_exit(w->base);
329 libworker_do_cmd(w, msg, len); /* also frees the buf */
332 /** the background thread func */
334 libworker_dobg(void* arg)
338 struct libworker* w = (struct libworker*)arg;
341 log_err("libunbound bg worker init failed, nomem");
345 log_thread_set(&w->thread_num);
346 #ifdef THREADS_DISABLED
349 /* close non-used parts of the pipes */
350 tube_close_write(ctx->qq_pipe);
351 tube_close_read(ctx->rr_pipe);
353 if(!tube_setup_bg_listen(ctx->qq_pipe, w->base,
354 libworker_handle_control_cmd, w)) {
355 log_err("libunbound bg worker init failed, no bglisten");
358 if(!tube_setup_bg_write(ctx->rr_pipe, w->base)) {
359 log_err("libunbound bg worker init failed, no bgwrite");
364 comm_base_dispatch(w->base);
368 tube_remove_bg_listen(w->ctx->qq_pipe);
369 tube_remove_bg_write(w->ctx->rr_pipe);
371 (void)tube_write_msg(ctx->rr_pipe, (uint8_t*)&m,
372 (uint32_t)sizeof(m), 0);
373 #ifdef THREADS_DISABLED
374 /* close pipes from forked process before exit */
375 tube_close_read(ctx->qq_pipe);
376 tube_close_write(ctx->rr_pipe);
381 int libworker_bg(struct ub_ctx* ctx)
384 /* fork or threadcreate */
385 lock_basic_lock(&ctx->cfglock);
387 lock_basic_unlock(&ctx->cfglock);
388 w = libworker_setup(ctx, 1, NULL);
389 if(!w) return UB_NOMEM;
391 #ifdef ENABLE_LOCK_CHECKS
392 w->thread_num = 1; /* for nicer DEBUG checklocks */
394 ub_thread_create(&ctx->bg_tid, libworker_dobg, w);
396 lock_basic_unlock(&ctx->cfglock);
398 /* no fork on windows */
400 #else /* HAVE_FORK */
401 switch((ctx->bg_pid=fork())) {
403 w = libworker_setup(ctx, 1, NULL);
404 if(!w) fatal_exit("out of memory");
405 /* close non-used parts of the pipes */
406 tube_close_write(ctx->qq_pipe);
407 tube_close_read(ctx->rr_pipe);
408 (void)libworker_dobg(w);
414 /* close non-used parts, so that the worker
415 * bgprocess gets 'pipe closed' when the
416 * main process exits */
417 tube_close_read(ctx->qq_pipe);
418 tube_close_write(ctx->rr_pipe);
421 #endif /* HAVE_FORK */
426 /** insert canonname */
428 fill_canon(struct ub_result* res, uint8_t* s)
432 res->canonname = strdup(buf);
433 return res->canonname != 0;
436 /** fill data into result */
438 fill_res(struct ub_result* res, struct ub_packed_rrset_key* answer,
439 uint8_t* finalcname, struct query_info* rq, struct reply_info* rep)
442 struct packed_rrset_data* data;
446 if(!fill_canon(res, finalcname))
447 return 0; /* out of memory */
449 if(rep->rrset_count != 0)
450 res->ttl = (int)rep->ttl;
451 res->data = (char**)calloc(1, sizeof(char*));
452 res->len = (int*)calloc(1, sizeof(int));
453 return (res->data && res->len);
455 data = (struct packed_rrset_data*)answer->entry.data;
456 if(query_dname_compare(rq->qname, answer->rk.dname) != 0) {
457 if(!fill_canon(res, answer->rk.dname))
458 return 0; /* out of memory */
459 } else res->canonname = NULL;
460 res->data = (char**)calloc(data->count+1, sizeof(char*));
461 res->len = (int*)calloc(data->count+1, sizeof(int));
462 if(!res->data || !res->len)
463 return 0; /* out of memory */
464 for(i=0; i<data->count; i++) {
465 /* remove rdlength from rdata */
466 res->len[i] = (int)(data->rr_len[i] - 2);
467 res->data[i] = memdup(data->rr_data[i]+2, (size_t)res->len[i]);
469 return 0; /* out of memory */
471 /* ttl for positive answers, from CNAME and answer RRs */
472 if(data->count != 0) {
474 res->ttl = (int)data->ttl;
475 for(j=0; j<rep->an_numrrsets; j++) {
476 struct packed_rrset_data* d =
477 (struct packed_rrset_data*)rep->rrsets[j]->
479 if((int)d->ttl < res->ttl)
480 res->ttl = (int)d->ttl;
483 /* ttl for negative answers */
484 if(data->count == 0 && rep->rrset_count != 0)
485 res->ttl = (int)rep->ttl;
486 res->data[data->count] = NULL;
487 res->len[data->count] = 0;
491 /** fill result from parsed message, on error fills servfail */
493 libworker_enter_result(struct ub_result* res, sldns_buffer* buf,
494 struct regional* temp, enum sec_status msg_security)
496 struct query_info rq;
497 struct reply_info* rep;
498 res->rcode = LDNS_RCODE_SERVFAIL;
499 rep = parse_reply_in_temp_region(buf, temp, &rq);
501 log_err("cannot parse buf");
502 return; /* error parsing buf, or out of memory */
504 if(!fill_res(res, reply_find_answer_rrset(&rq, rep),
505 reply_find_final_cname_target(&rq, rep), &rq, rep))
506 return; /* out of memory */
507 /* rcode, havedata, nxdomain, secure, bogus */
508 res->rcode = (int)FLAGS_GET_RCODE(rep->flags);
509 if(res->data && res->data[0])
511 if(res->rcode == LDNS_RCODE_NXDOMAIN)
513 if(msg_security == sec_status_secure)
515 if(msg_security == sec_status_bogus)
519 /** fillup fg results */
521 libworker_fillup_fg(struct ctx_query* q, int rcode, sldns_buffer* buf,
522 enum sec_status s, char* why_bogus)
525 q->res->why_bogus = strdup(why_bogus);
527 q->res->rcode = rcode;
532 q->res->rcode = LDNS_RCODE_SERVFAIL;
534 q->msg = memdup(sldns_buffer_begin(buf), sldns_buffer_limit(buf));
535 q->msg_len = sldns_buffer_limit(buf);
537 return; /* the error is in the rcode */
540 /* canonname and results */
542 libworker_enter_result(q->res, buf, q->w->env->scratch, s);
546 libworker_fg_done_cb(void* arg, int rcode, sldns_buffer* buf, enum sec_status s,
549 struct ctx_query* q = (struct ctx_query*)arg;
550 /* fg query is done; exit comm base */
551 comm_base_exit(q->w->base);
553 libworker_fillup_fg(q, rcode, buf, s, why_bogus);
556 /** setup qinfo and edns */
558 setup_qinfo_edns(struct libworker* w, struct ctx_query* q,
559 struct query_info* qinfo, struct edns_data* edns)
561 qinfo->qtype = (uint16_t)q->res->qtype;
562 qinfo->qclass = (uint16_t)q->res->qclass;
563 qinfo->local_alias = NULL;
564 qinfo->qname = sldns_str2wire_dname(q->res->qname, &qinfo->qname_len);
568 qinfo->local_alias = NULL;
569 edns->edns_present = 1;
571 edns->edns_version = 0;
572 edns->bits = EDNS_DO;
573 edns->opt_list = NULL;
574 if(sldns_buffer_capacity(w->back->udp_buff) < 65535)
575 edns->udp_size = (uint16_t)sldns_buffer_capacity(
577 else edns->udp_size = 65535;
581 int libworker_fg(struct ub_ctx* ctx, struct ctx_query* q)
583 struct libworker* w = libworker_setup(ctx, 0, NULL);
584 uint16_t qflags, qid;
585 struct query_info qinfo;
586 struct edns_data edns;
589 if(!setup_qinfo_edns(w, q, &qinfo, &edns)) {
596 /* see if there is a fixed answer */
597 sldns_buffer_write_u16_at(w->back->udp_buff, 0, qid);
598 sldns_buffer_write_u16_at(w->back->udp_buff, 2, qflags);
599 if(local_zones_answer(ctx->local_zones, w->env, &qinfo, &edns,
600 w->back->udp_buff, w->env->scratch, NULL, NULL, 0, NULL, 0,
601 NULL, 0, NULL, 0, NULL)) {
602 regional_free_all(w->env->scratch);
603 libworker_fillup_fg(q, LDNS_RCODE_NOERROR,
604 w->back->udp_buff, sec_status_insecure, NULL);
609 if(ctx->env->auth_zones && auth_zones_answer(ctx->env->auth_zones,
610 w->env, &qinfo, &edns, w->back->udp_buff, w->env->scratch)) {
611 regional_free_all(w->env->scratch);
612 libworker_fillup_fg(q, LDNS_RCODE_NOERROR,
613 w->back->udp_buff, sec_status_insecure, NULL);
618 /* process new query */
619 if(!mesh_new_callback(w->env->mesh, &qinfo, qflags, &edns,
620 w->back->udp_buff, qid, libworker_fg_done_cb, q)) {
627 comm_base_dispatch(w->base);
634 libworker_event_done_cb(void* arg, int rcode, sldns_buffer* buf,
635 enum sec_status s, char* why_bogus)
637 struct ctx_query* q = (struct ctx_query*)arg;
638 ub_event_callback_type cb = (ub_event_callback_type)q->cb;
639 void* cb_arg = q->cb_arg;
640 int cancelled = q->cancelled;
643 struct ub_ctx* ctx = q->w->ctx;
644 lock_basic_lock(&ctx->cfglock);
645 (void)rbtree_delete(&ctx->queries, q->node.key);
647 context_query_delete(q);
648 lock_basic_unlock(&ctx->cfglock);
653 if(s == sec_status_bogus)
655 else if(s == sec_status_secure)
657 (*cb)(cb_arg, rcode, (void*)sldns_buffer_begin(buf),
658 (int)sldns_buffer_limit(buf), sec, why_bogus);
662 int libworker_attach_mesh(struct ub_ctx* ctx, struct ctx_query* q,
665 struct libworker* w = ctx->event_worker;
666 uint16_t qflags, qid;
667 struct query_info qinfo;
668 struct edns_data edns;
671 if(!setup_qinfo_edns(w, q, &qinfo, &edns))
676 /* see if there is a fixed answer */
677 sldns_buffer_write_u16_at(w->back->udp_buff, 0, qid);
678 sldns_buffer_write_u16_at(w->back->udp_buff, 2, qflags);
679 if(local_zones_answer(ctx->local_zones, w->env, &qinfo, &edns,
680 w->back->udp_buff, w->env->scratch, NULL, NULL, 0, NULL, 0,
681 NULL, 0, NULL, 0, NULL)) {
682 regional_free_all(w->env->scratch);
684 libworker_event_done_cb(q, LDNS_RCODE_NOERROR,
685 w->back->udp_buff, sec_status_insecure, NULL);
688 if(ctx->env->auth_zones && auth_zones_answer(ctx->env->auth_zones,
689 w->env, &qinfo, &edns, w->back->udp_buff, w->env->scratch)) {
690 regional_free_all(w->env->scratch);
692 libworker_event_done_cb(q, LDNS_RCODE_NOERROR,
693 w->back->udp_buff, sec_status_insecure, NULL);
696 /* process new query */
698 *async_id = q->querynum;
699 if(!mesh_new_callback(w->env->mesh, &qinfo, qflags, &edns,
700 w->back->udp_buff, qid, libworker_event_done_cb, q)) {
708 /** add result to the bg worker result queue */
710 add_bg_result(struct libworker* w, struct ctx_query* q, sldns_buffer* pkt,
711 int err, char* reason)
716 /* serialize and delete unneeded q */
717 if(w->is_bg_thread) {
718 lock_basic_lock(&w->ctx->cfglock);
720 q->res->why_bogus = strdup(reason);
722 q->msg_len = sldns_buffer_remaining(pkt);
723 q->msg = memdup(sldns_buffer_begin(pkt), q->msg_len);
725 msg = context_serialize_answer(q, UB_NOMEM,
727 else msg = context_serialize_answer(q, err,
729 } else msg = context_serialize_answer(q, err, NULL, &len);
730 lock_basic_unlock(&w->ctx->cfglock);
733 q->res->why_bogus = strdup(reason);
734 msg = context_serialize_answer(q, err, pkt, &len);
735 (void)rbtree_delete(&w->ctx->queries, q->node.key);
737 context_query_delete(q);
741 log_err("out of memory for async answer");
744 if(!tube_queue_item(w->ctx->rr_pipe, msg, len)) {
745 log_err("out of memory for async answer");
751 libworker_bg_done_cb(void* arg, int rcode, sldns_buffer* buf, enum sec_status s,
754 struct ctx_query* q = (struct ctx_query*)arg;
756 if(q->cancelled || q->w->back->want_to_quit) {
757 if(q->w->is_bg_thread) {
759 struct ub_ctx* ctx = q->w->ctx;
760 lock_basic_lock(&ctx->cfglock);
761 (void)rbtree_delete(&ctx->queries, q->node.key);
763 context_query_delete(q);
764 lock_basic_unlock(&ctx->cfglock);
766 /* cancelled, do not give answer */
771 buf = q->w->env->scratch_buffer;
773 error_encode(buf, rcode, NULL, 0, BIT_RD, NULL);
775 add_bg_result(q->w, q, buf, UB_NOERROR, why_bogus);
779 /** handle new query command for bg worker */
781 handle_newq(struct libworker* w, uint8_t* buf, uint32_t len)
783 uint16_t qflags, qid;
784 struct query_info qinfo;
785 struct edns_data edns;
787 if(w->is_bg_thread) {
788 lock_basic_lock(&w->ctx->cfglock);
789 q = context_lookup_new_query(w->ctx, buf, len);
790 lock_basic_unlock(&w->ctx->cfglock);
792 q = context_deserialize_new_query(w->ctx, buf, len);
796 log_err("failed to deserialize newq");
799 if(!setup_qinfo_edns(w, q, &qinfo, &edns)) {
800 add_bg_result(w, q, NULL, UB_SYNTAX, NULL);
805 /* see if there is a fixed answer */
806 sldns_buffer_write_u16_at(w->back->udp_buff, 0, qid);
807 sldns_buffer_write_u16_at(w->back->udp_buff, 2, qflags);
808 if(local_zones_answer(w->ctx->local_zones, w->env, &qinfo, &edns,
809 w->back->udp_buff, w->env->scratch, NULL, NULL, 0, NULL, 0,
810 NULL, 0, NULL, 0, NULL)) {
811 regional_free_all(w->env->scratch);
812 q->msg_security = sec_status_insecure;
813 add_bg_result(w, q, w->back->udp_buff, UB_NOERROR, NULL);
817 if(w->ctx->env->auth_zones && auth_zones_answer(w->ctx->env->auth_zones,
818 w->env, &qinfo, &edns, w->back->udp_buff, w->env->scratch)) {
819 regional_free_all(w->env->scratch);
820 q->msg_security = sec_status_insecure;
821 add_bg_result(w, q, w->back->udp_buff, UB_NOERROR, NULL);
826 /* process new query */
827 if(!mesh_new_callback(w->env->mesh, &qinfo, qflags, &edns,
828 w->back->udp_buff, qid, libworker_bg_done_cb, q)) {
829 add_bg_result(w, q, NULL, UB_NOMEM, NULL);
834 void libworker_alloc_cleanup(void* arg)
836 struct libworker* w = (struct libworker*)arg;
837 slabhash_clear(&w->env->rrset_cache->table);
838 slabhash_clear(w->env->msg_cache);
841 struct outbound_entry* libworker_send_query(struct query_info* qinfo,
842 uint16_t flags, int dnssec, int want_dnssec, int nocaps,
843 struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone,
844 size_t zonelen, int ssl_upstream, struct module_qstate* q)
846 struct libworker* w = (struct libworker*)q->env->worker;
847 struct outbound_entry* e = (struct outbound_entry*)regional_alloc(
848 q->region, sizeof(*e));
852 e->qsent = outnet_serviced_query(w->back, qinfo, flags, dnssec,
853 want_dnssec, nocaps, q->env->cfg->tcp_upstream, ssl_upstream,
854 addr, addrlen, zone, zonelen, q, libworker_handle_service_reply,
855 e, w->back->udp_buff, q->env);
863 libworker_handle_reply(struct comm_point* c, void* arg, int error,
864 struct comm_reply* reply_info)
866 struct module_qstate* q = (struct module_qstate*)arg;
867 struct libworker* lw = (struct libworker*)q->env->worker;
868 struct outbound_entry e;
873 mesh_report_reply(lw->env->mesh, &e, reply_info, error);
877 if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
878 || LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) !=
880 || LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
881 /* error becomes timeout for the module as if this reply
883 mesh_report_reply(lw->env->mesh, &e, reply_info,
887 mesh_report_reply(lw->env->mesh, &e, reply_info, NETEVENT_NOERROR);
892 libworker_handle_service_reply(struct comm_point* c, void* arg, int error,
893 struct comm_reply* reply_info)
895 struct outbound_entry* e = (struct outbound_entry*)arg;
896 struct libworker* lw = (struct libworker*)e->qstate->env->worker;
899 mesh_report_reply(lw->env->mesh, e, reply_info, error);
903 if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer))
904 || LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) !=
906 || LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) {
907 /* error becomes timeout for the module as if this reply
909 mesh_report_reply(lw->env->mesh, e, reply_info,
913 mesh_report_reply(lw->env->mesh, e, reply_info, NETEVENT_NOERROR);
917 /* --- fake callbacks for fptr_wlist to work --- */
918 void worker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
919 uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
920 int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
925 int worker_handle_request(struct comm_point* ATTR_UNUSED(c),
926 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
927 struct comm_reply* ATTR_UNUSED(repinfo))
933 int worker_handle_reply(struct comm_point* ATTR_UNUSED(c),
934 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
935 struct comm_reply* ATTR_UNUSED(reply_info))
941 int worker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
942 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
943 struct comm_reply* ATTR_UNUSED(reply_info))
949 int remote_accept_callback(struct comm_point* ATTR_UNUSED(c),
950 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
951 struct comm_reply* ATTR_UNUSED(repinfo))
957 int remote_control_callback(struct comm_point* ATTR_UNUSED(c),
958 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
959 struct comm_reply* ATTR_UNUSED(repinfo))
965 void worker_sighandler(int ATTR_UNUSED(sig), void* ATTR_UNUSED(arg))
970 struct outbound_entry* worker_send_query(struct query_info* ATTR_UNUSED(qinfo),
971 uint16_t ATTR_UNUSED(flags), int ATTR_UNUSED(dnssec),
972 int ATTR_UNUSED(want_dnssec), int ATTR_UNUSED(nocaps),
973 struct sockaddr_storage* ATTR_UNUSED(addr), socklen_t ATTR_UNUSED(addrlen),
974 uint8_t* ATTR_UNUSED(zone), size_t ATTR_UNUSED(zonelen),
975 int ATTR_UNUSED(ssl_upstream), struct module_qstate* ATTR_UNUSED(q))
982 worker_alloc_cleanup(void* ATTR_UNUSED(arg))
987 void worker_stat_timer_cb(void* ATTR_UNUSED(arg))
992 void worker_probe_timer_cb(void* ATTR_UNUSED(arg))
997 void worker_start_accept(void* ATTR_UNUSED(arg))
1002 void worker_stop_accept(void* ATTR_UNUSED(arg))
1007 int order_lock_cmp(const void* ATTR_UNUSED(e1), const void* ATTR_UNUSED(e2))
1014 codeline_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1020 int replay_var_compare(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1026 void remote_get_opt_ssl(char* ATTR_UNUSED(str), void* ATTR_UNUSED(arg))
1031 #ifdef UB_ON_WINDOWS
1033 worker_win_stop_cb(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev), void*
1039 wsvc_cron_cb(void* ATTR_UNUSED(arg))
1043 #endif /* UB_ON_WINDOWS */