2 * unbound.c - unbound validating resolver public API implementation
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 functions to resolve DNS queries and
40 * validate the answers. Synchronously and asynchronously.
44 /* include the public api first, it should be able to stand alone */
45 #include "libunbound/unbound.h"
46 #include "libunbound/unbound-event.h"
49 #include "libunbound/context.h"
50 #include "libunbound/libworker.h"
51 #include "util/locks.h"
52 #include "util/config_file.h"
53 #include "util/alloc.h"
54 #include "util/module.h"
55 #include "util/regional.h"
57 #include "util/random.h"
58 #include "util/net_help.h"
59 #include "util/tube.h"
60 #include "util/ub_event.h"
61 #include "util/edns.h"
62 #include "services/modstack.h"
63 #include "services/localzone.h"
64 #include "services/cache/infra.h"
65 #include "services/cache/rrset.h"
66 #include "services/authzone.h"
67 #include "sldns/sbuffer.h"
71 #ifdef HAVE_SYS_WAIT_H
78 #if defined(UB_ON_WINDOWS) && defined (HAVE_WINDOWS_H)
81 #endif /* UB_ON_WINDOWS */
83 /** store that the logfile has a debug override */
84 int ctx_logfile_overridden = 0;
86 /** create context functionality, but no pipes */
87 static struct ub_ctx* ub_ctx_create_nopipe(void)
96 if(!ctx_logfile_overridden)
97 log_init(NULL, 0, NULL); /* logs to stderr */
98 log_ident_set("libunbound");
100 if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0) {
101 log_err("could not init winsock. WSAStartup: %s",
106 verbosity = NO_VERBOSE; /* errors only */
108 ctx = (struct ub_ctx*)calloc(1, sizeof(*ctx));
113 alloc_init(&ctx->superalloc, NULL, 0);
114 if(!(ctx->seed_rnd = ub_initstate(NULL))) {
115 ub_randfree(ctx->seed_rnd);
120 lock_basic_init(&ctx->qqpipe_lock);
121 lock_basic_init(&ctx->rrpipe_lock);
122 lock_basic_init(&ctx->cfglock);
123 ctx->env = (struct module_env*)calloc(1, sizeof(*ctx->env));
125 ub_randfree(ctx->seed_rnd);
130 ctx->env->cfg = config_create_forlib();
133 ub_randfree(ctx->seed_rnd);
138 /* init edns_known_options */
139 if(!edns_known_options_init(ctx->env)) {
140 config_delete(ctx->env->cfg);
142 ub_randfree(ctx->seed_rnd);
147 ctx->env->auth_zones = auth_zones_create();
148 if(!ctx->env->auth_zones) {
149 edns_known_options_delete(ctx->env);
150 config_delete(ctx->env->cfg);
152 ub_randfree(ctx->seed_rnd);
157 ctx->env->edns_tags = edns_tags_create();
158 if(!ctx->env->edns_tags) {
159 auth_zones_delete(ctx->env->auth_zones);
160 edns_known_options_delete(ctx->env);
161 config_delete(ctx->env->cfg);
163 ub_randfree(ctx->seed_rnd);
169 ctx->env->alloc = &ctx->superalloc;
170 ctx->env->worker = NULL;
171 ctx->env->need_to_validate = 0;
172 modstack_init(&ctx->mods);
173 rbtree_init(&ctx->queries, &context_query_cmp);
180 struct ub_ctx* ctx = ub_ctx_create_nopipe();
183 if((ctx->qq_pipe = tube_create()) == NULL) {
185 ub_randfree(ctx->seed_rnd);
186 config_delete(ctx->env->cfg);
187 modstack_desetup(&ctx->mods, ctx->env);
188 edns_known_options_delete(ctx->env);
189 edns_tags_delete(ctx->env->edns_tags);
195 if((ctx->rr_pipe = tube_create()) == NULL) {
197 tube_delete(ctx->qq_pipe);
198 ub_randfree(ctx->seed_rnd);
199 config_delete(ctx->env->cfg);
200 modstack_desetup(&ctx->mods, ctx->env);
201 edns_known_options_delete(ctx->env);
202 edns_tags_delete(ctx->env->edns_tags);
212 ub_ctx_create_ub_event(struct ub_event_base* ueb)
214 struct ub_ctx* ctx = ub_ctx_create_nopipe();
217 /* no pipes, but we have the locks to make sure everything works */
219 ctx->dothread = 1; /* the processing is in the same process,
220 makes ub_cancel and ub_ctx_delete do the right thing */
221 ctx->event_base = ueb;
226 ub_ctx_create_event(struct event_base* eb)
228 struct ub_ctx* ctx = ub_ctx_create_nopipe();
231 /* no pipes, but we have the locks to make sure everything works */
233 ctx->dothread = 1; /* the processing is in the same process,
234 makes ub_cancel and ub_ctx_delete do the right thing */
235 ctx->event_base = ub_libevent_event_base(eb);
236 if (!ctx->event_base) {
240 ctx->event_base_malloced = 1;
246 delq(rbnode_type* n, void* ATTR_UNUSED(arg))
248 struct ctx_query* q = (struct ctx_query*)n;
249 context_query_delete(q);
252 /** stop the bg thread */
253 static void ub_stop_bg(struct ub_ctx* ctx)
255 /* stop the bg thread */
256 lock_basic_lock(&ctx->cfglock);
257 if(ctx->created_bg) {
260 uint32_t cmd = UB_LIBCMD_QUIT;
261 lock_basic_unlock(&ctx->cfglock);
262 lock_basic_lock(&ctx->qqpipe_lock);
263 (void)tube_write_msg(ctx->qq_pipe, (uint8_t*)&cmd,
264 (uint32_t)sizeof(cmd), 0);
265 lock_basic_unlock(&ctx->qqpipe_lock);
266 lock_basic_lock(&ctx->rrpipe_lock);
267 while(tube_read_msg(ctx->rr_pipe, &msg, &len, 0)) {
268 /* discard all results except a quit confirm */
269 if(context_serial_getcmd(msg, len) == UB_LIBCMD_QUIT) {
275 lock_basic_unlock(&ctx->rrpipe_lock);
277 /* if bg worker is a thread, wait for it to exit, so that all
278 * resources are really gone. */
279 lock_basic_lock(&ctx->cfglock);
281 lock_basic_unlock(&ctx->cfglock);
282 ub_thread_join(ctx->bg_tid);
284 lock_basic_unlock(&ctx->cfglock);
285 #ifndef UB_ON_WINDOWS
286 if(waitpid(ctx->bg_pid, NULL, 0) == -1) {
288 log_err("waitpid: %s", strerror(errno));
294 lock_basic_unlock(&ctx->cfglock);
299 ub_ctx_delete(struct ub_ctx* ctx)
301 struct alloc_cache* a, *na;
305 /* see if bg thread is created and if threads have been killed */
306 /* no locks, because those may be held by terminated threads */
307 /* for processes the read pipe is closed and we see that on read */
309 if(ctx->created_bg && ctx->dothread) {
310 if(pthread_kill(ctx->bg_tid, 0) == ESRCH) {
311 /* thread has been killed */
315 #endif /* HAVE_PTHREAD */
318 libworker_delete_event(ctx->event_worker);
320 modstack_desetup(&ctx->mods, ctx->env);
324 a->super = &ctx->superalloc;
329 local_zones_delete(ctx->local_zones);
330 lock_basic_destroy(&ctx->qqpipe_lock);
331 lock_basic_destroy(&ctx->rrpipe_lock);
332 lock_basic_destroy(&ctx->cfglock);
333 tube_delete(ctx->qq_pipe);
334 tube_delete(ctx->rr_pipe);
336 slabhash_delete(ctx->env->msg_cache);
337 rrset_cache_delete(ctx->env->rrset_cache);
338 infra_delete(ctx->env->infra_cache);
339 config_delete(ctx->env->cfg);
340 edns_known_options_delete(ctx->env);
341 edns_tags_delete(ctx->env->edns_tags);
342 auth_zones_delete(ctx->env->auth_zones);
345 ub_randfree(ctx->seed_rnd);
346 alloc_clear(&ctx->superalloc);
347 traverse_postorder(&ctx->queries, delq, NULL);
348 if(ctx_logfile_overridden) {
350 ctx_logfile_overridden = 0;
352 if(ctx->event_base_malloced)
353 free(ctx->event_base);
361 ub_ctx_set_option(struct ub_ctx* ctx, const char* opt, const char* val)
363 lock_basic_lock(&ctx->cfglock);
365 lock_basic_unlock(&ctx->cfglock);
366 return UB_AFTERFINAL;
368 if(!config_set_option(ctx->env->cfg, opt, val)) {
369 lock_basic_unlock(&ctx->cfglock);
372 lock_basic_unlock(&ctx->cfglock);
377 ub_ctx_get_option(struct ub_ctx* ctx, const char* opt, char** str)
380 lock_basic_lock(&ctx->cfglock);
381 r = config_get_option_collate(ctx->env->cfg, opt, str);
382 lock_basic_unlock(&ctx->cfglock);
383 if(r == 0) r = UB_NOERROR;
384 else if(r == 1) r = UB_SYNTAX;
385 else if(r == 2) r = UB_NOMEM;
390 ub_ctx_config(struct ub_ctx* ctx, const char* fname)
392 lock_basic_lock(&ctx->cfglock);
394 lock_basic_unlock(&ctx->cfglock);
395 return UB_AFTERFINAL;
397 if(!config_read(ctx->env->cfg, fname, NULL)) {
398 lock_basic_unlock(&ctx->cfglock);
401 lock_basic_unlock(&ctx->cfglock);
406 ub_ctx_add_ta(struct ub_ctx* ctx, const char* ta)
408 char* dup = strdup(ta);
409 if(!dup) return UB_NOMEM;
410 lock_basic_lock(&ctx->cfglock);
412 lock_basic_unlock(&ctx->cfglock);
414 return UB_AFTERFINAL;
416 if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_list, dup)) {
417 lock_basic_unlock(&ctx->cfglock);
420 lock_basic_unlock(&ctx->cfglock);
425 ub_ctx_add_ta_file(struct ub_ctx* ctx, const char* fname)
427 char* dup = strdup(fname);
428 if(!dup) return UB_NOMEM;
429 lock_basic_lock(&ctx->cfglock);
431 lock_basic_unlock(&ctx->cfglock);
433 return UB_AFTERFINAL;
435 if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_file_list, dup)) {
436 lock_basic_unlock(&ctx->cfglock);
439 lock_basic_unlock(&ctx->cfglock);
443 int ub_ctx_add_ta_autr(struct ub_ctx* ctx, const char* fname)
445 char* dup = strdup(fname);
446 if(!dup) return UB_NOMEM;
447 lock_basic_lock(&ctx->cfglock);
449 lock_basic_unlock(&ctx->cfglock);
451 return UB_AFTERFINAL;
453 if(!cfg_strlist_insert(&ctx->env->cfg->auto_trust_anchor_file_list,
455 lock_basic_unlock(&ctx->cfglock);
458 lock_basic_unlock(&ctx->cfglock);
463 ub_ctx_trustedkeys(struct ub_ctx* ctx, const char* fname)
465 char* dup = strdup(fname);
466 if(!dup) return UB_NOMEM;
467 lock_basic_lock(&ctx->cfglock);
469 lock_basic_unlock(&ctx->cfglock);
471 return UB_AFTERFINAL;
473 if(!cfg_strlist_insert(&ctx->env->cfg->trusted_keys_file_list, dup)) {
474 lock_basic_unlock(&ctx->cfglock);
477 lock_basic_unlock(&ctx->cfglock);
482 ub_ctx_debuglevel(struct ub_ctx* ctx, int d)
484 lock_basic_lock(&ctx->cfglock);
486 ctx->env->cfg->verbosity = d;
487 lock_basic_unlock(&ctx->cfglock);
491 int ub_ctx_debugout(struct ub_ctx* ctx, void* out)
493 lock_basic_lock(&ctx->cfglock);
494 log_file((FILE*)out);
495 ctx_logfile_overridden = 1;
496 ctx->logfile_override = 1;
498 lock_basic_unlock(&ctx->cfglock);
503 ub_ctx_async(struct ub_ctx* ctx, int dothread)
505 #ifdef THREADS_DISABLED
506 if(dothread) /* cannot do threading */
509 lock_basic_lock(&ctx->cfglock);
511 lock_basic_unlock(&ctx->cfglock);
512 return UB_AFTERFINAL;
514 ctx->dothread = dothread;
515 lock_basic_unlock(&ctx->cfglock);
520 ub_poll(struct ub_ctx* ctx)
522 /* no need to hold lock while testing for readability. */
523 return tube_poll(ctx->rr_pipe);
527 ub_fd(struct ub_ctx* ctx)
529 return tube_read_fd(ctx->rr_pipe);
532 /** process answer from bg worker */
534 process_answer_detail(struct ub_ctx* ctx, uint8_t* msg, uint32_t len,
535 ub_callback_type* cb, void** cbarg, int* err,
536 struct ub_result** res)
539 if(context_serial_getcmd(msg, len) != UB_LIBCMD_ANSWER) {
540 log_err("error: bad data from bg worker %d",
541 (int)context_serial_getcmd(msg, len));
545 lock_basic_lock(&ctx->cfglock);
546 q = context_deserialize_answer(ctx, msg, len, err);
548 lock_basic_unlock(&ctx->cfglock);
549 /* probably simply the lookup that failed, i.e.
550 * response returned before cancel was sent out, so noerror */
553 log_assert(q->async);
555 /* grab cb while locked */
565 ub_resolve_free(q->res);
567 /* parse the message, extract rcode, fill result */
568 sldns_buffer* buf = sldns_buffer_new(q->msg_len);
569 struct regional* region = regional_create();
571 (*res)->rcode = LDNS_RCODE_SERVFAIL;
573 sldns_buffer_clear(buf);
574 sldns_buffer_write(buf, q->msg, q->msg_len);
575 sldns_buffer_flip(buf);
576 libworker_enter_result(*res, buf, region,
579 (*res)->answer_packet = q->msg;
580 (*res)->answer_len = (int)q->msg_len;
582 sldns_buffer_free(buf);
583 regional_destroy(region);
586 /* delete the q from list */
587 (void)rbtree_delete(&ctx->queries, q->node.key);
589 context_query_delete(q);
590 lock_basic_unlock(&ctx->cfglock);
593 ub_resolve_free(*res);
597 /** process answer from bg worker */
599 process_answer(struct ub_ctx* ctx, uint8_t* msg, uint32_t len)
604 struct ub_result* res;
607 r = process_answer_detail(ctx, msg, len, &cb, &cbarg, &err, &res);
609 /* no locks held while calling callback, so that library is
612 (*cb)(cbarg, err, res);
618 ub_process(struct ub_ctx* ctx)
625 lock_basic_lock(&ctx->rrpipe_lock);
626 r = tube_read_msg(ctx->rr_pipe, &msg, &len, 1);
627 lock_basic_unlock(&ctx->rrpipe_lock);
632 if(!process_answer(ctx, msg, len)) {
642 ub_wait(struct ub_ctx* ctx)
647 struct ub_result* res;
651 /* this is basically the same loop as _process(), but with changes.
652 * holds the rrpipe lock and waits with tube_wait */
654 lock_basic_lock(&ctx->rrpipe_lock);
655 lock_basic_lock(&ctx->cfglock);
656 if(ctx->num_async == 0) {
657 lock_basic_unlock(&ctx->cfglock);
658 lock_basic_unlock(&ctx->rrpipe_lock);
661 lock_basic_unlock(&ctx->cfglock);
663 /* keep rrpipe locked, while
664 * o waiting for pipe readable
666 * o possibly decrementing num_async
667 * do callback without lock
669 r = tube_wait(ctx->rr_pipe);
671 r = tube_read_msg(ctx->rr_pipe, &msg, &len, 1);
673 lock_basic_unlock(&ctx->rrpipe_lock);
677 lock_basic_unlock(&ctx->rrpipe_lock);
680 r = process_answer_detail(ctx, msg, len,
681 &cb, &cbarg, &err, &res);
682 lock_basic_unlock(&ctx->rrpipe_lock);
687 (*cb)(cbarg, err, res);
689 lock_basic_unlock(&ctx->rrpipe_lock);
696 ub_resolve(struct ub_ctx* ctx, const char* name, int rrtype,
697 int rrclass, struct ub_result** result)
703 lock_basic_lock(&ctx->cfglock);
704 if(!ctx->finalized) {
705 r = context_finalize(ctx);
707 lock_basic_unlock(&ctx->cfglock);
711 /* create new ctx_query and attempt to add to the list */
712 lock_basic_unlock(&ctx->cfglock);
713 q = context_new(ctx, name, rrtype, rrclass, NULL, NULL, NULL);
716 /* become a resolver thread for a bit */
718 r = libworker_fg(ctx, q);
720 lock_basic_lock(&ctx->cfglock);
721 (void)rbtree_delete(&ctx->queries, q->node.key);
722 context_query_delete(q);
723 lock_basic_unlock(&ctx->cfglock);
726 q->res->answer_packet = q->msg;
727 q->res->answer_len = (int)q->msg_len;
732 lock_basic_lock(&ctx->cfglock);
733 (void)rbtree_delete(&ctx->queries, q->node.key);
734 context_query_delete(q);
735 lock_basic_unlock(&ctx->cfglock);
740 ub_resolve_event(struct ub_ctx* ctx, const char* name, int rrtype,
741 int rrclass, void* mydata, ub_event_callback_type callback,
749 lock_basic_lock(&ctx->cfglock);
750 if(!ctx->finalized) {
751 r = context_finalize(ctx);
753 lock_basic_unlock(&ctx->cfglock);
757 lock_basic_unlock(&ctx->cfglock);
758 if(!ctx->event_worker) {
759 ctx->event_worker = libworker_create_event(ctx,
761 if(!ctx->event_worker) {
766 /* set time in case answer comes from cache */
767 ub_comm_base_now(ctx->event_worker->base);
769 /* create new ctx_query and attempt to add to the list */
770 q = context_new(ctx, name, rrtype, rrclass, NULL, callback, mydata);
775 if((r=libworker_attach_mesh(ctx, q, async_id)) != 0)
782 ub_resolve_async(struct ub_ctx* ctx, const char* name, int rrtype,
783 int rrclass, void* mydata, ub_callback_type callback, int* async_id)
791 lock_basic_lock(&ctx->cfglock);
792 if(!ctx->finalized) {
793 int r = context_finalize(ctx);
795 lock_basic_unlock(&ctx->cfglock);
799 if(!ctx->created_bg) {
802 lock_basic_unlock(&ctx->cfglock);
803 r = libworker_bg(ctx);
805 lock_basic_lock(&ctx->cfglock);
807 lock_basic_unlock(&ctx->cfglock);
811 lock_basic_unlock(&ctx->cfglock);
814 /* create new ctx_query and attempt to add to the list */
815 q = context_new(ctx, name, rrtype, rrclass, callback, NULL, mydata);
819 /* write over pipe to background worker */
820 lock_basic_lock(&ctx->cfglock);
821 msg = context_serialize_new_query(q, &len);
823 (void)rbtree_delete(&ctx->queries, q->node.key);
825 context_query_delete(q);
826 lock_basic_unlock(&ctx->cfglock);
830 *async_id = q->querynum;
831 lock_basic_unlock(&ctx->cfglock);
833 lock_basic_lock(&ctx->qqpipe_lock);
834 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
835 lock_basic_unlock(&ctx->qqpipe_lock);
839 lock_basic_unlock(&ctx->qqpipe_lock);
845 ub_cancel(struct ub_ctx* ctx, int async_id)
850 lock_basic_lock(&ctx->cfglock);
851 q = (struct ctx_query*)rbtree_search(&ctx->queries, &async_id);
852 if(!q || !q->async) {
853 /* it is not there, so nothing to do */
854 lock_basic_unlock(&ctx->cfglock);
857 log_assert(q->async);
861 if(!ctx->dothread) { /* if forked */
862 (void)rbtree_delete(&ctx->queries, q->node.key);
864 msg = context_serialize_cancel(q, &len);
865 context_query_delete(q);
866 lock_basic_unlock(&ctx->cfglock);
870 /* send cancel to background worker */
871 lock_basic_lock(&ctx->qqpipe_lock);
872 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
873 lock_basic_unlock(&ctx->qqpipe_lock);
877 lock_basic_unlock(&ctx->qqpipe_lock);
880 lock_basic_unlock(&ctx->cfglock);
886 ub_resolve_free(struct ub_result* result)
891 if(result->canonname != result->qname)
892 free(result->canonname);
894 for(p = result->data; *p; p++)
898 free(result->answer_packet);
899 free(result->why_bogus);
907 case UB_NOERROR: return "no error";
908 case UB_SOCKET: return "socket io error";
909 case UB_NOMEM: return "out of memory";
910 case UB_SYNTAX: return "syntax error";
911 case UB_SERVFAIL: return "server failure";
912 case UB_FORKFAIL: return "could not fork";
913 case UB_INITFAIL: return "initialization failure";
914 case UB_AFTERFINAL: return "setting change after finalize";
915 case UB_PIPE: return "error in pipe communication with async";
916 case UB_READFILE: return "error reading file";
917 case UB_NOID: return "error async_id does not exist";
918 default: return "unknown error";
923 ub_ctx_set_fwd(struct ub_ctx* ctx, const char* addr)
925 struct sockaddr_storage storage;
927 struct config_stub* s;
929 lock_basic_lock(&ctx->cfglock);
931 lock_basic_unlock(&ctx->cfglock);
933 return UB_AFTERFINAL;
936 /* disable fwd mode - the root stub should be first. */
937 if(ctx->env->cfg->forwards &&
938 strcmp(ctx->env->cfg->forwards->name, ".") == 0) {
939 s = ctx->env->cfg->forwards;
940 ctx->env->cfg->forwards = s->next;
944 lock_basic_unlock(&ctx->cfglock);
947 lock_basic_unlock(&ctx->cfglock);
949 /* check syntax for addr */
950 if(!extstrtoaddr(addr, &storage, &stlen)) {
955 /* it parses, add root stub in front of list */
956 lock_basic_lock(&ctx->cfglock);
957 if(!ctx->env->cfg->forwards ||
958 strcmp(ctx->env->cfg->forwards->name, ".") != 0) {
959 s = calloc(1, sizeof(*s));
961 lock_basic_unlock(&ctx->cfglock);
965 s->name = strdup(".");
968 lock_basic_unlock(&ctx->cfglock);
972 s->next = ctx->env->cfg->forwards;
973 ctx->env->cfg->forwards = s;
975 log_assert(ctx->env->cfg->forwards);
976 s = ctx->env->cfg->forwards;
980 lock_basic_unlock(&ctx->cfglock);
984 if(!cfg_strlist_insert(&s->addrs, dupl)) {
985 lock_basic_unlock(&ctx->cfglock);
989 lock_basic_unlock(&ctx->cfglock);
993 int ub_ctx_set_tls(struct ub_ctx* ctx, int tls)
995 lock_basic_lock(&ctx->cfglock);
997 lock_basic_unlock(&ctx->cfglock);
999 return UB_AFTERFINAL;
1001 ctx->env->cfg->ssl_upstream = tls;
1002 lock_basic_unlock(&ctx->cfglock);
1006 int ub_ctx_set_stub(struct ub_ctx* ctx, const char* zone, const char* addr,
1010 struct config_stub **prev, *elem;
1012 /* check syntax for zone name */
1017 if(!parse_dname(zone, &nm, &nmlen, &nmlabs)) {
1026 /* check syntax for addr (if not NULL) */
1028 struct sockaddr_storage storage;
1030 if(!extstrtoaddr(addr, &storage, &stlen)) {
1036 lock_basic_lock(&ctx->cfglock);
1037 if(ctx->finalized) {
1038 lock_basic_unlock(&ctx->cfglock);
1040 return UB_AFTERFINAL;
1043 /* arguments all right, now find or add the stub */
1044 prev = &ctx->env->cfg->stubs;
1045 elem = cfg_stub_find(&prev, zone);
1046 if(!elem && !addr) {
1047 /* not found and we want to delete, nothing to do */
1048 lock_basic_unlock(&ctx->cfglock);
1050 } else if(elem && !addr) {
1051 /* found, and we want to delete */
1053 config_delstub(elem);
1054 lock_basic_unlock(&ctx->cfglock);
1057 /* not found, create the stub entry */
1058 elem=(struct config_stub*)calloc(1, sizeof(struct config_stub));
1059 if(elem) elem->name = strdup(zone);
1060 if(!elem || !elem->name) {
1062 lock_basic_unlock(&ctx->cfglock);
1066 elem->next = ctx->env->cfg->stubs;
1067 ctx->env->cfg->stubs = elem;
1070 /* add the address to the list and set settings */
1071 elem->isprime = isprime;
1074 lock_basic_unlock(&ctx->cfglock);
1078 if(!cfg_strlist_insert(&elem->addrs, a)) {
1079 lock_basic_unlock(&ctx->cfglock);
1083 lock_basic_unlock(&ctx->cfglock);
1088 ub_ctx_resolvconf(struct ub_ctx* ctx, const char* fname)
1097 #if !defined(UB_ON_WINDOWS) || !defined(HAVE_WINDOWS_H)
1098 fname = "/etc/resolv.conf";
1101 ULONG buflen = sizeof(*info);
1102 IP_ADDR_STRING *ptr;
1104 info = (FIXED_INFO *) malloc(sizeof (FIXED_INFO));
1108 if (GetNetworkParams(info, &buflen) == ERROR_BUFFER_OVERFLOW) {
1110 info = (FIXED_INFO *) malloc(buflen);
1115 if (GetNetworkParams(info, &buflen) == NO_ERROR) {
1117 ptr = &(info->DnsServerList);
1120 if((retval=ub_ctx_set_fwd(ctx,
1121 ptr->IpAddress.String))!=0) {
1134 #endif /* WINDOWS */
1136 in = fopen(fname, "r");
1138 /* error in errno! perror(fname) */
1141 while(fgets(buf, (int)sizeof(buf), in)) {
1142 buf[sizeof(buf)-1] = 0;
1144 while(*parse == ' ' || *parse == '\t')
1146 if(strncmp(parse, "nameserver", 10) == 0) {
1148 parse += 10; /* skip 'nameserver' */
1149 /* skip whitespace */
1150 while(*parse == ' ' || *parse == '\t')
1153 /* skip [0-9a-fA-F.:]*, i.e. IP4 and IP6 address */
1154 while(isxdigit((unsigned char)*parse) || *parse=='.' || *parse==':')
1156 /* terminate after the address, remove newline */
1159 if((r = ub_ctx_set_fwd(ctx, addr)) != UB_NOERROR) {
1167 /* from resolv.conf(5) if none given, use localhost */
1168 return ub_ctx_set_fwd(ctx, "127.0.0.1");
1174 ub_ctx_hosts(struct ub_ctx* ctx, const char* fname)
1177 char buf[1024], ldata[2048];
1178 char* parse, *addr, *name, *ins;
1179 lock_basic_lock(&ctx->cfglock);
1180 if(ctx->finalized) {
1181 lock_basic_unlock(&ctx->cfglock);
1183 return UB_AFTERFINAL;
1185 lock_basic_unlock(&ctx->cfglock);
1187 #if defined(UB_ON_WINDOWS) && defined(HAVE_WINDOWS_H)
1189 * If this is Windows NT/XP/2K it's in
1190 * %WINDIR%\system32\drivers\etc\hosts.
1191 * If this is Windows 95/98/Me it's in %WINDIR%\hosts.
1193 name = getenv("WINDIR");
1196 snprintf(buf, sizeof(buf), "%s%s", name,
1197 "\\system32\\drivers\\etc\\hosts");
1198 if((retval=ub_ctx_hosts(ctx, buf)) !=0 ) {
1199 snprintf(buf, sizeof(buf), "%s%s", name,
1201 retval=ub_ctx_hosts(ctx, buf);
1207 fname = "/etc/hosts";
1210 in = fopen(fname, "r");
1212 /* error in errno! perror(fname) */
1215 while(fgets(buf, (int)sizeof(buf), in)) {
1216 buf[sizeof(buf)-1] = 0;
1218 while(*parse == ' ' || *parse == '\t')
1221 continue; /* skip comment */
1222 /* format: <addr> spaces <name> spaces <name> ... */
1225 while(isxdigit((unsigned char)*parse) || *parse == '.' || *parse == ':')
1229 if(*parse == '\n' || *parse == 0)
1232 continue; /* ignore macOSX fe80::1%lo0 localhost */
1233 if(*parse != ' ' && *parse != '\t') {
1234 /* must have whitespace after address */
1239 *parse++ = 0; /* end delimiter for addr ... */
1240 /* go to names and add them */
1242 while(*parse == ' ' || *parse == '\t' || *parse=='\n'
1245 if(*parse == 0 || *parse == '#')
1247 /* skip name, allows (too) many printable characters */
1249 while('!' <= *parse && *parse <= '~')
1252 *parse++ = 0; /* end delimiter for name */
1253 snprintf(ldata, sizeof(ldata), "%s %s %s",
1254 name, str_is_ip6(addr)?"AAAA":"A", addr);
1255 ins = strdup(ldata);
1262 lock_basic_lock(&ctx->cfglock);
1263 if(!cfg_strlist_insert(&ctx->env->cfg->local_data,
1265 lock_basic_unlock(&ctx->cfglock);
1270 lock_basic_unlock(&ctx->cfglock);
1277 /** finalize the context, if not already finalized */
1278 static int ub_ctx_finalize(struct ub_ctx* ctx)
1281 lock_basic_lock(&ctx->cfglock);
1282 if (!ctx->finalized) {
1283 res = context_finalize(ctx);
1285 lock_basic_unlock(&ctx->cfglock);
1289 /* Print local zones and RR data */
1290 int ub_ctx_print_local_zones(struct ub_ctx* ctx)
1292 int res = ub_ctx_finalize(ctx);
1293 if (res) return res;
1295 local_zones_print(ctx->local_zones);
1300 /* Add a new zone */
1301 int ub_ctx_zone_add(struct ub_ctx* ctx, const char *zone_name,
1302 const char *zone_type)
1304 enum localzone_type t;
1305 struct local_zone* z;
1310 int res = ub_ctx_finalize(ctx);
1311 if (res) return res;
1313 if(!local_zone_str2type(zone_type, &t)) {
1317 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1321 lock_rw_wrlock(&ctx->local_zones->lock);
1322 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1323 LDNS_RR_CLASS_IN))) {
1324 /* already present in tree */
1325 lock_rw_wrlock(&z->lock);
1326 z->type = t; /* update type anyway */
1327 lock_rw_unlock(&z->lock);
1328 lock_rw_unlock(&ctx->local_zones->lock);
1332 if(!local_zones_add_zone(ctx->local_zones, nm, nmlen, nmlabs,
1333 LDNS_RR_CLASS_IN, t)) {
1334 lock_rw_unlock(&ctx->local_zones->lock);
1337 lock_rw_unlock(&ctx->local_zones->lock);
1342 int ub_ctx_zone_remove(struct ub_ctx* ctx, const char *zone_name)
1344 struct local_zone* z;
1349 int res = ub_ctx_finalize(ctx);
1350 if (res) return res;
1352 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1356 lock_rw_wrlock(&ctx->local_zones->lock);
1357 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1358 LDNS_RR_CLASS_IN))) {
1359 /* present in tree */
1360 local_zones_del_zone(ctx->local_zones, z);
1362 lock_rw_unlock(&ctx->local_zones->lock);
1367 /* Add new RR data */
1368 int ub_ctx_data_add(struct ub_ctx* ctx, const char *data)
1370 int res = ub_ctx_finalize(ctx);
1371 if (res) return res;
1373 res = local_zones_add_RR(ctx->local_zones, data);
1374 return (!res) ? UB_NOMEM : UB_NOERROR;
1377 /* Remove RR data */
1378 int ub_ctx_data_remove(struct ub_ctx* ctx, const char *data)
1383 int res = ub_ctx_finalize(ctx);
1384 if (res) return res;
1386 if(!parse_dname(data, &nm, &nmlen, &nmlabs))
1389 local_zones_del_data(ctx->local_zones, nm, nmlen, nmlabs,
1396 const char* ub_version(void)
1398 return PACKAGE_VERSION;
1402 ub_ctx_set_event(struct ub_ctx* ctx, struct event_base* base) {
1403 struct ub_event_base* new_base;
1405 if (!ctx || !ctx->event_base || !base) {
1408 if (ub_libevent_get_event_base(ctx->event_base) == base) {
1413 lock_basic_lock(&ctx->cfglock);
1414 /* destroy the current worker - safe to pass in NULL */
1415 libworker_delete_event(ctx->event_worker);
1416 ctx->event_worker = NULL;
1417 new_base = ub_libevent_event_base(base);
1419 ctx->event_base = new_base;
1420 ctx->created_bg = 0;
1422 lock_basic_unlock(&ctx->cfglock);
1423 return new_base ? UB_NOERROR : UB_INITFAIL;