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. Synchonously 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 "services/modstack.h"
62 #include "services/localzone.h"
63 #include "services/cache/infra.h"
64 #include "services/cache/rrset.h"
65 #include "sldns/sbuffer.h"
69 #ifdef HAVE_SYS_WAIT_H
76 #if defined(UB_ON_WINDOWS) && defined (HAVE_WINDOWS_H)
79 #endif /* UB_ON_WINDOWS */
81 /** create context functionality, but no pipes */
82 static struct ub_ctx* ub_ctx_create_nopipe(void)
91 log_init(NULL, 0, NULL); /* logs to stderr */
92 log_ident_set("libunbound");
94 if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0) {
95 log_err("could not init winsock. WSAStartup: %s",
100 verbosity = 0; /* errors only */
102 ctx = (struct ub_ctx*)calloc(1, sizeof(*ctx));
107 alloc_init(&ctx->superalloc, NULL, 0);
108 seed = (unsigned int)time(NULL) ^ (unsigned int)getpid();
109 if(!(ctx->seed_rnd = ub_initstate(seed, NULL))) {
111 ub_randfree(ctx->seed_rnd);
117 lock_basic_init(&ctx->qqpipe_lock);
118 lock_basic_init(&ctx->rrpipe_lock);
119 lock_basic_init(&ctx->cfglock);
120 ctx->env = (struct module_env*)calloc(1, sizeof(*ctx->env));
122 ub_randfree(ctx->seed_rnd);
127 ctx->env->cfg = config_create_forlib();
130 ub_randfree(ctx->seed_rnd);
135 /* init edns_known_options */
136 if(!edns_known_options_init(ctx->env)) {
137 config_delete(ctx->env->cfg);
139 ub_randfree(ctx->seed_rnd);
144 ctx->env->alloc = &ctx->superalloc;
145 ctx->env->worker = NULL;
146 ctx->env->need_to_validate = 0;
147 modstack_init(&ctx->mods);
148 rbtree_init(&ctx->queries, &context_query_cmp);
155 struct ub_ctx* ctx = ub_ctx_create_nopipe();
158 if((ctx->qq_pipe = tube_create()) == NULL) {
160 ub_randfree(ctx->seed_rnd);
161 config_delete(ctx->env->cfg);
162 modstack_desetup(&ctx->mods, ctx->env);
163 edns_known_options_delete(ctx->env);
169 if((ctx->rr_pipe = tube_create()) == NULL) {
171 tube_delete(ctx->qq_pipe);
172 ub_randfree(ctx->seed_rnd);
173 config_delete(ctx->env->cfg);
174 modstack_desetup(&ctx->mods, ctx->env);
175 edns_known_options_delete(ctx->env);
185 ub_ctx_create_ub_event(struct ub_event_base* ueb)
187 struct ub_ctx* ctx = ub_ctx_create_nopipe();
190 /* no pipes, but we have the locks to make sure everything works */
192 ctx->dothread = 1; /* the processing is in the same process,
193 makes ub_cancel and ub_ctx_delete do the right thing */
194 ctx->event_base = ueb;
199 ub_ctx_create_event(struct event_base* eb)
201 struct ub_ctx* ctx = ub_ctx_create_nopipe();
204 /* no pipes, but we have the locks to make sure everything works */
206 ctx->dothread = 1; /* the processing is in the same process,
207 makes ub_cancel and ub_ctx_delete do the right thing */
208 ctx->event_base = ub_libevent_event_base(eb);
209 if (!ctx->event_base) {
218 delq(rbnode_t* n, void* ATTR_UNUSED(arg))
220 struct ctx_query* q = (struct ctx_query*)n;
221 context_query_delete(q);
224 /** stop the bg thread */
225 static void ub_stop_bg(struct ub_ctx* ctx)
227 /* stop the bg thread */
228 lock_basic_lock(&ctx->cfglock);
229 if(ctx->created_bg) {
232 uint32_t cmd = UB_LIBCMD_QUIT;
233 lock_basic_unlock(&ctx->cfglock);
234 lock_basic_lock(&ctx->qqpipe_lock);
235 (void)tube_write_msg(ctx->qq_pipe, (uint8_t*)&cmd,
236 (uint32_t)sizeof(cmd), 0);
237 lock_basic_unlock(&ctx->qqpipe_lock);
238 lock_basic_lock(&ctx->rrpipe_lock);
239 while(tube_read_msg(ctx->rr_pipe, &msg, &len, 0)) {
240 /* discard all results except a quit confirm */
241 if(context_serial_getcmd(msg, len) == UB_LIBCMD_QUIT) {
247 lock_basic_unlock(&ctx->rrpipe_lock);
249 /* if bg worker is a thread, wait for it to exit, so that all
250 * resources are really gone. */
251 lock_basic_lock(&ctx->cfglock);
253 lock_basic_unlock(&ctx->cfglock);
254 ub_thread_join(ctx->bg_tid);
256 lock_basic_unlock(&ctx->cfglock);
257 #ifndef UB_ON_WINDOWS
258 if(waitpid(ctx->bg_pid, NULL, 0) == -1) {
260 log_err("waitpid: %s", strerror(errno));
266 lock_basic_unlock(&ctx->cfglock);
271 ub_ctx_delete(struct ub_ctx* ctx)
273 struct alloc_cache* a, *na;
277 /* see if bg thread is created and if threads have been killed */
278 /* no locks, because those may be held by terminated threads */
279 /* for processes the read pipe is closed and we see that on read */
281 if(ctx->created_bg && ctx->dothread) {
282 if(pthread_kill(ctx->bg_tid, 0) == ESRCH) {
283 /* thread has been killed */
287 #endif /* HAVE_PTHREAD */
290 libworker_delete_event(ctx->event_worker);
292 modstack_desetup(&ctx->mods, ctx->env);
296 a->super = &ctx->superalloc;
301 local_zones_delete(ctx->local_zones);
302 lock_basic_destroy(&ctx->qqpipe_lock);
303 lock_basic_destroy(&ctx->rrpipe_lock);
304 lock_basic_destroy(&ctx->cfglock);
305 tube_delete(ctx->qq_pipe);
306 tube_delete(ctx->rr_pipe);
308 slabhash_delete(ctx->env->msg_cache);
309 rrset_cache_delete(ctx->env->rrset_cache);
310 infra_delete(ctx->env->infra_cache);
311 config_delete(ctx->env->cfg);
312 edns_known_options_delete(ctx->env);
313 inplace_cb_lists_delete(ctx->env);
316 ub_randfree(ctx->seed_rnd);
317 alloc_clear(&ctx->superalloc);
318 traverse_postorder(&ctx->queries, delq, NULL);
326 ub_ctx_set_option(struct ub_ctx* ctx, const char* opt, const char* val)
328 lock_basic_lock(&ctx->cfglock);
330 lock_basic_unlock(&ctx->cfglock);
331 return UB_AFTERFINAL;
333 if(!config_set_option(ctx->env->cfg, opt, val)) {
334 lock_basic_unlock(&ctx->cfglock);
337 lock_basic_unlock(&ctx->cfglock);
342 ub_ctx_get_option(struct ub_ctx* ctx, const char* opt, char** str)
345 lock_basic_lock(&ctx->cfglock);
346 r = config_get_option_collate(ctx->env->cfg, opt, str);
347 lock_basic_unlock(&ctx->cfglock);
348 if(r == 0) r = UB_NOERROR;
349 else if(r == 1) r = UB_SYNTAX;
350 else if(r == 2) r = UB_NOMEM;
355 ub_ctx_config(struct ub_ctx* ctx, const char* fname)
357 lock_basic_lock(&ctx->cfglock);
359 lock_basic_unlock(&ctx->cfglock);
360 return UB_AFTERFINAL;
362 if(!config_read(ctx->env->cfg, fname, NULL)) {
363 lock_basic_unlock(&ctx->cfglock);
366 lock_basic_unlock(&ctx->cfglock);
371 ub_ctx_add_ta(struct ub_ctx* ctx, const char* ta)
373 char* dup = strdup(ta);
374 if(!dup) return UB_NOMEM;
375 lock_basic_lock(&ctx->cfglock);
377 lock_basic_unlock(&ctx->cfglock);
379 return UB_AFTERFINAL;
381 if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_list, dup)) {
382 lock_basic_unlock(&ctx->cfglock);
386 lock_basic_unlock(&ctx->cfglock);
391 ub_ctx_add_ta_file(struct ub_ctx* ctx, const char* fname)
393 char* dup = strdup(fname);
394 if(!dup) return UB_NOMEM;
395 lock_basic_lock(&ctx->cfglock);
397 lock_basic_unlock(&ctx->cfglock);
399 return UB_AFTERFINAL;
401 if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_file_list, dup)) {
402 lock_basic_unlock(&ctx->cfglock);
406 lock_basic_unlock(&ctx->cfglock);
410 int ub_ctx_add_ta_autr(struct ub_ctx* ctx, const char* fname)
412 char* dup = strdup(fname);
413 if(!dup) return UB_NOMEM;
414 lock_basic_lock(&ctx->cfglock);
416 lock_basic_unlock(&ctx->cfglock);
418 return UB_AFTERFINAL;
420 if(!cfg_strlist_insert(&ctx->env->cfg->auto_trust_anchor_file_list,
422 lock_basic_unlock(&ctx->cfglock);
426 lock_basic_unlock(&ctx->cfglock);
431 ub_ctx_trustedkeys(struct ub_ctx* ctx, const char* fname)
433 char* dup = strdup(fname);
434 if(!dup) return UB_NOMEM;
435 lock_basic_lock(&ctx->cfglock);
437 lock_basic_unlock(&ctx->cfglock);
439 return UB_AFTERFINAL;
441 if(!cfg_strlist_insert(&ctx->env->cfg->trusted_keys_file_list, dup)) {
442 lock_basic_unlock(&ctx->cfglock);
446 lock_basic_unlock(&ctx->cfglock);
451 ub_ctx_debuglevel(struct ub_ctx* ctx, int d)
453 lock_basic_lock(&ctx->cfglock);
455 ctx->env->cfg->verbosity = d;
456 lock_basic_unlock(&ctx->cfglock);
460 int ub_ctx_debugout(struct ub_ctx* ctx, void* out)
462 lock_basic_lock(&ctx->cfglock);
463 log_file((FILE*)out);
464 ctx->logfile_override = 1;
466 lock_basic_unlock(&ctx->cfglock);
471 ub_ctx_async(struct ub_ctx* ctx, int dothread)
473 #ifdef THREADS_DISABLED
474 if(dothread) /* cannot do threading */
477 lock_basic_lock(&ctx->cfglock);
479 lock_basic_unlock(&ctx->cfglock);
480 return UB_AFTERFINAL;
482 ctx->dothread = dothread;
483 lock_basic_unlock(&ctx->cfglock);
488 ub_poll(struct ub_ctx* ctx)
490 /* no need to hold lock while testing for readability. */
491 return tube_poll(ctx->rr_pipe);
495 ub_fd(struct ub_ctx* ctx)
497 return tube_read_fd(ctx->rr_pipe);
500 /** process answer from bg worker */
502 process_answer_detail(struct ub_ctx* ctx, uint8_t* msg, uint32_t len,
503 ub_callback_t* cb, void** cbarg, int* err,
504 struct ub_result** res)
507 if(context_serial_getcmd(msg, len) != UB_LIBCMD_ANSWER) {
508 log_err("error: bad data from bg worker %d",
509 (int)context_serial_getcmd(msg, len));
513 lock_basic_lock(&ctx->cfglock);
514 q = context_deserialize_answer(ctx, msg, len, err);
516 lock_basic_unlock(&ctx->cfglock);
517 /* probably simply the lookup that failed, i.e.
518 * response returned before cancel was sent out, so noerror */
521 log_assert(q->async);
523 /* grab cb while locked */
533 ub_resolve_free(q->res);
535 /* parse the message, extract rcode, fill result */
536 sldns_buffer* buf = sldns_buffer_new(q->msg_len);
537 struct regional* region = regional_create();
539 (*res)->rcode = LDNS_RCODE_SERVFAIL;
541 sldns_buffer_clear(buf);
542 sldns_buffer_write(buf, q->msg, q->msg_len);
543 sldns_buffer_flip(buf);
544 libworker_enter_result(*res, buf, region,
547 (*res)->answer_packet = q->msg;
548 (*res)->answer_len = (int)q->msg_len;
550 sldns_buffer_free(buf);
551 regional_destroy(region);
554 /* delete the q from list */
555 (void)rbtree_delete(&ctx->queries, q->node.key);
557 context_query_delete(q);
558 lock_basic_unlock(&ctx->cfglock);
561 ub_resolve_free(*res);
565 /** process answer from bg worker */
567 process_answer(struct ub_ctx* ctx, uint8_t* msg, uint32_t len)
572 struct ub_result* res;
575 r = process_answer_detail(ctx, msg, len, &cb, &cbarg, &err, &res);
577 /* no locks held while calling callback, so that library is
580 (*cb)(cbarg, err, res);
586 ub_process(struct ub_ctx* ctx)
593 lock_basic_lock(&ctx->rrpipe_lock);
594 r = tube_read_msg(ctx->rr_pipe, &msg, &len, 1);
595 lock_basic_unlock(&ctx->rrpipe_lock);
600 if(!process_answer(ctx, msg, len)) {
610 ub_wait(struct ub_ctx* ctx)
615 struct ub_result* res;
619 /* this is basically the same loop as _process(), but with changes.
620 * holds the rrpipe lock and waits with tube_wait */
622 lock_basic_lock(&ctx->rrpipe_lock);
623 lock_basic_lock(&ctx->cfglock);
624 if(ctx->num_async == 0) {
625 lock_basic_unlock(&ctx->cfglock);
626 lock_basic_unlock(&ctx->rrpipe_lock);
629 lock_basic_unlock(&ctx->cfglock);
631 /* keep rrpipe locked, while
632 * o waiting for pipe readable
634 * o possibly decrementing num_async
635 * do callback without lock
637 r = tube_wait(ctx->rr_pipe);
639 r = tube_read_msg(ctx->rr_pipe, &msg, &len, 1);
641 lock_basic_unlock(&ctx->rrpipe_lock);
645 lock_basic_unlock(&ctx->rrpipe_lock);
648 r = process_answer_detail(ctx, msg, len,
649 &cb, &cbarg, &err, &res);
650 lock_basic_unlock(&ctx->rrpipe_lock);
655 (*cb)(cbarg, err, res);
657 lock_basic_unlock(&ctx->rrpipe_lock);
664 ub_resolve(struct ub_ctx* ctx, const char* name, int rrtype,
665 int rrclass, struct ub_result** result)
671 lock_basic_lock(&ctx->cfglock);
672 if(!ctx->finalized) {
673 r = context_finalize(ctx);
675 lock_basic_unlock(&ctx->cfglock);
679 /* create new ctx_query and attempt to add to the list */
680 lock_basic_unlock(&ctx->cfglock);
681 q = context_new(ctx, name, rrtype, rrclass, NULL, NULL);
684 /* become a resolver thread for a bit */
686 r = libworker_fg(ctx, q);
688 lock_basic_lock(&ctx->cfglock);
689 (void)rbtree_delete(&ctx->queries, q->node.key);
690 context_query_delete(q);
691 lock_basic_unlock(&ctx->cfglock);
694 q->res->answer_packet = q->msg;
695 q->res->answer_len = (int)q->msg_len;
700 lock_basic_lock(&ctx->cfglock);
701 (void)rbtree_delete(&ctx->queries, q->node.key);
702 context_query_delete(q);
703 lock_basic_unlock(&ctx->cfglock);
708 ub_resolve_event(struct ub_ctx* ctx, const char* name, int rrtype,
709 int rrclass, void* mydata, ub_event_callback_t callback, int* async_id)
716 lock_basic_lock(&ctx->cfglock);
717 if(!ctx->finalized) {
718 int r = context_finalize(ctx);
720 lock_basic_unlock(&ctx->cfglock);
724 lock_basic_unlock(&ctx->cfglock);
725 if(!ctx->event_worker) {
726 ctx->event_worker = libworker_create_event(ctx,
728 if(!ctx->event_worker) {
733 /* set time in case answer comes from cache */
734 ub_comm_base_now(ctx->event_worker->base);
736 /* create new ctx_query and attempt to add to the list */
737 q = context_new(ctx, name, rrtype, rrclass, (ub_callback_t)callback,
743 if((r=libworker_attach_mesh(ctx, q, async_id)) != 0)
750 ub_resolve_async(struct ub_ctx* ctx, const char* name, int rrtype,
751 int rrclass, void* mydata, ub_callback_t callback, int* async_id)
759 lock_basic_lock(&ctx->cfglock);
760 if(!ctx->finalized) {
761 int r = context_finalize(ctx);
763 lock_basic_unlock(&ctx->cfglock);
767 if(!ctx->created_bg) {
770 lock_basic_unlock(&ctx->cfglock);
771 r = libworker_bg(ctx);
773 lock_basic_lock(&ctx->cfglock);
775 lock_basic_unlock(&ctx->cfglock);
779 lock_basic_unlock(&ctx->cfglock);
782 /* create new ctx_query and attempt to add to the list */
783 q = context_new(ctx, name, rrtype, rrclass, callback, mydata);
787 /* write over pipe to background worker */
788 lock_basic_lock(&ctx->cfglock);
789 msg = context_serialize_new_query(q, &len);
791 (void)rbtree_delete(&ctx->queries, q->node.key);
793 context_query_delete(q);
794 lock_basic_unlock(&ctx->cfglock);
798 *async_id = q->querynum;
799 lock_basic_unlock(&ctx->cfglock);
801 lock_basic_lock(&ctx->qqpipe_lock);
802 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
803 lock_basic_unlock(&ctx->qqpipe_lock);
807 lock_basic_unlock(&ctx->qqpipe_lock);
813 ub_cancel(struct ub_ctx* ctx, int async_id)
818 lock_basic_lock(&ctx->cfglock);
819 q = (struct ctx_query*)rbtree_search(&ctx->queries, &async_id);
820 if(!q || !q->async) {
821 /* it is not there, so nothing to do */
822 lock_basic_unlock(&ctx->cfglock);
825 log_assert(q->async);
829 if(!ctx->dothread) { /* if forked */
830 (void)rbtree_delete(&ctx->queries, q->node.key);
832 msg = context_serialize_cancel(q, &len);
833 context_query_delete(q);
834 lock_basic_unlock(&ctx->cfglock);
838 /* send cancel to background worker */
839 lock_basic_lock(&ctx->qqpipe_lock);
840 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
841 lock_basic_unlock(&ctx->qqpipe_lock);
845 lock_basic_unlock(&ctx->qqpipe_lock);
848 lock_basic_unlock(&ctx->cfglock);
854 ub_resolve_free(struct ub_result* result)
859 if(result->canonname != result->qname)
860 free(result->canonname);
862 for(p = result->data; *p; p++)
866 free(result->answer_packet);
867 free(result->why_bogus);
875 case UB_NOERROR: return "no error";
876 case UB_SOCKET: return "socket io error";
877 case UB_NOMEM: return "out of memory";
878 case UB_SYNTAX: return "syntax error";
879 case UB_SERVFAIL: return "server failure";
880 case UB_FORKFAIL: return "could not fork";
881 case UB_INITFAIL: return "initialization failure";
882 case UB_AFTERFINAL: return "setting change after finalize";
883 case UB_PIPE: return "error in pipe communication with async";
884 case UB_READFILE: return "error reading file";
885 case UB_NOID: return "error async_id does not exist";
886 default: return "unknown error";
891 ub_ctx_set_fwd(struct ub_ctx* ctx, const char* addr)
893 struct sockaddr_storage storage;
895 struct config_stub* s;
897 lock_basic_lock(&ctx->cfglock);
899 lock_basic_unlock(&ctx->cfglock);
901 return UB_AFTERFINAL;
904 /* disable fwd mode - the root stub should be first. */
905 if(ctx->env->cfg->forwards &&
906 strcmp(ctx->env->cfg->forwards->name, ".") == 0) {
907 s = ctx->env->cfg->forwards;
908 ctx->env->cfg->forwards = s->next;
912 lock_basic_unlock(&ctx->cfglock);
915 lock_basic_unlock(&ctx->cfglock);
917 /* check syntax for addr */
918 if(!extstrtoaddr(addr, &storage, &stlen)) {
923 /* it parses, add root stub in front of list */
924 lock_basic_lock(&ctx->cfglock);
925 if(!ctx->env->cfg->forwards ||
926 strcmp(ctx->env->cfg->forwards->name, ".") != 0) {
927 s = calloc(1, sizeof(*s));
929 lock_basic_unlock(&ctx->cfglock);
933 s->name = strdup(".");
936 lock_basic_unlock(&ctx->cfglock);
940 s->next = ctx->env->cfg->forwards;
941 ctx->env->cfg->forwards = s;
943 log_assert(ctx->env->cfg->forwards);
944 s = ctx->env->cfg->forwards;
948 lock_basic_unlock(&ctx->cfglock);
952 if(!cfg_strlist_insert(&s->addrs, dupl)) {
954 lock_basic_unlock(&ctx->cfglock);
958 lock_basic_unlock(&ctx->cfglock);
962 int ub_ctx_set_stub(struct ub_ctx* ctx, const char* zone, const char* addr,
966 struct config_stub **prev, *elem;
968 /* check syntax for zone name */
973 if(!parse_dname(zone, &nm, &nmlen, &nmlabs)) {
982 /* check syntax for addr (if not NULL) */
984 struct sockaddr_storage storage;
986 if(!extstrtoaddr(addr, &storage, &stlen)) {
992 lock_basic_lock(&ctx->cfglock);
994 lock_basic_unlock(&ctx->cfglock);
996 return UB_AFTERFINAL;
999 /* arguments all right, now find or add the stub */
1000 prev = &ctx->env->cfg->stubs;
1001 elem = cfg_stub_find(&prev, zone);
1002 if(!elem && !addr) {
1003 /* not found and we want to delete, nothing to do */
1004 lock_basic_unlock(&ctx->cfglock);
1006 } else if(elem && !addr) {
1007 /* found, and we want to delete */
1009 config_delstub(elem);
1010 lock_basic_unlock(&ctx->cfglock);
1013 /* not found, create the stub entry */
1014 elem=(struct config_stub*)calloc(1, sizeof(struct config_stub));
1015 if(elem) elem->name = strdup(zone);
1016 if(!elem || !elem->name) {
1018 lock_basic_unlock(&ctx->cfglock);
1022 elem->next = ctx->env->cfg->stubs;
1023 ctx->env->cfg->stubs = elem;
1026 /* add the address to the list and set settings */
1027 elem->isprime = isprime;
1030 lock_basic_unlock(&ctx->cfglock);
1034 if(!cfg_strlist_insert(&elem->addrs, a)) {
1035 lock_basic_unlock(&ctx->cfglock);
1040 lock_basic_unlock(&ctx->cfglock);
1045 ub_ctx_resolvconf(struct ub_ctx* ctx, const char* fname)
1054 #if !defined(UB_ON_WINDOWS) || !defined(HAVE_WINDOWS_H)
1055 fname = "/etc/resolv.conf";
1058 ULONG buflen = sizeof(*info);
1059 IP_ADDR_STRING *ptr;
1061 info = (FIXED_INFO *) malloc(sizeof (FIXED_INFO));
1065 if (GetNetworkParams(info, &buflen) == ERROR_BUFFER_OVERFLOW) {
1067 info = (FIXED_INFO *) malloc(buflen);
1072 if (GetNetworkParams(info, &buflen) == NO_ERROR) {
1074 ptr = &(info->DnsServerList);
1077 if((retval=ub_ctx_set_fwd(ctx,
1078 ptr->IpAddress.String))!=0) {
1091 #endif /* WINDOWS */
1093 in = fopen(fname, "r");
1095 /* error in errno! perror(fname) */
1098 while(fgets(buf, (int)sizeof(buf), in)) {
1099 buf[sizeof(buf)-1] = 0;
1101 while(*parse == ' ' || *parse == '\t')
1103 if(strncmp(parse, "nameserver", 10) == 0) {
1105 parse += 10; /* skip 'nameserver' */
1106 /* skip whitespace */
1107 while(*parse == ' ' || *parse == '\t')
1110 /* skip [0-9a-fA-F.:]*, i.e. IP4 and IP6 address */
1111 while(isxdigit((unsigned char)*parse) || *parse=='.' || *parse==':')
1113 /* terminate after the address, remove newline */
1116 if((r = ub_ctx_set_fwd(ctx, addr)) != UB_NOERROR) {
1124 /* from resolv.conf(5) if none given, use localhost */
1125 return ub_ctx_set_fwd(ctx, "127.0.0.1");
1131 ub_ctx_hosts(struct ub_ctx* ctx, const char* fname)
1134 char buf[1024], ldata[1024];
1135 char* parse, *addr, *name, *ins;
1136 lock_basic_lock(&ctx->cfglock);
1137 if(ctx->finalized) {
1138 lock_basic_unlock(&ctx->cfglock);
1140 return UB_AFTERFINAL;
1142 lock_basic_unlock(&ctx->cfglock);
1144 #if defined(UB_ON_WINDOWS) && defined(HAVE_WINDOWS_H)
1146 * If this is Windows NT/XP/2K it's in
1147 * %WINDIR%\system32\drivers\etc\hosts.
1148 * If this is Windows 95/98/Me it's in %WINDIR%\hosts.
1150 name = getenv("WINDIR");
1153 snprintf(buf, sizeof(buf), "%s%s", name,
1154 "\\system32\\drivers\\etc\\hosts");
1155 if((retval=ub_ctx_hosts(ctx, buf)) !=0 ) {
1156 snprintf(buf, sizeof(buf), "%s%s", name,
1158 retval=ub_ctx_hosts(ctx, buf);
1164 fname = "/etc/hosts";
1167 in = fopen(fname, "r");
1169 /* error in errno! perror(fname) */
1172 while(fgets(buf, (int)sizeof(buf), in)) {
1173 buf[sizeof(buf)-1] = 0;
1175 while(*parse == ' ' || *parse == '\t')
1178 continue; /* skip comment */
1179 /* format: <addr> spaces <name> spaces <name> ... */
1182 while(isxdigit((unsigned char)*parse) || *parse == '.' || *parse == ':')
1186 if(*parse == '\n' || *parse == 0)
1189 continue; /* ignore macOSX fe80::1%lo0 localhost */
1190 if(*parse != ' ' && *parse != '\t') {
1191 /* must have whitespace after address */
1196 *parse++ = 0; /* end delimiter for addr ... */
1197 /* go to names and add them */
1199 while(*parse == ' ' || *parse == '\t' || *parse=='\n'
1202 if(*parse == 0 || *parse == '#')
1204 /* skip name, allows (too) many printable characters */
1206 while('!' <= *parse && *parse <= '~')
1209 *parse++ = 0; /* end delimiter for name */
1210 snprintf(ldata, sizeof(ldata), "%s %s %s",
1211 name, str_is_ip6(addr)?"AAAA":"A", addr);
1212 ins = strdup(ldata);
1219 lock_basic_lock(&ctx->cfglock);
1220 if(!cfg_strlist_insert(&ctx->env->cfg->local_data,
1222 lock_basic_unlock(&ctx->cfglock);
1228 lock_basic_unlock(&ctx->cfglock);
1235 /** finalize the context, if not already finalized */
1236 static int ub_ctx_finalize(struct ub_ctx* ctx)
1239 lock_basic_lock(&ctx->cfglock);
1240 if (!ctx->finalized) {
1241 res = context_finalize(ctx);
1243 lock_basic_unlock(&ctx->cfglock);
1247 /* Print local zones and RR data */
1248 int ub_ctx_print_local_zones(struct ub_ctx* ctx)
1250 int res = ub_ctx_finalize(ctx);
1251 if (res) return res;
1253 local_zones_print(ctx->local_zones);
1258 /* Add a new zone */
1259 int ub_ctx_zone_add(struct ub_ctx* ctx, const char *zone_name,
1260 const char *zone_type)
1262 enum localzone_type t;
1263 struct local_zone* z;
1268 int res = ub_ctx_finalize(ctx);
1269 if (res) return res;
1271 if(!local_zone_str2type(zone_type, &t)) {
1275 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1279 lock_rw_wrlock(&ctx->local_zones->lock);
1280 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1281 LDNS_RR_CLASS_IN))) {
1282 /* already present in tree */
1283 lock_rw_wrlock(&z->lock);
1284 z->type = t; /* update type anyway */
1285 lock_rw_unlock(&z->lock);
1286 lock_rw_unlock(&ctx->local_zones->lock);
1290 if(!local_zones_add_zone(ctx->local_zones, nm, nmlen, nmlabs,
1291 LDNS_RR_CLASS_IN, t)) {
1292 lock_rw_unlock(&ctx->local_zones->lock);
1295 lock_rw_unlock(&ctx->local_zones->lock);
1300 int ub_ctx_zone_remove(struct ub_ctx* ctx, const char *zone_name)
1302 struct local_zone* z;
1307 int res = ub_ctx_finalize(ctx);
1308 if (res) return res;
1310 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1314 lock_rw_wrlock(&ctx->local_zones->lock);
1315 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1316 LDNS_RR_CLASS_IN))) {
1317 /* present in tree */
1318 local_zones_del_zone(ctx->local_zones, z);
1320 lock_rw_unlock(&ctx->local_zones->lock);
1325 /* Add new RR data */
1326 int ub_ctx_data_add(struct ub_ctx* ctx, const char *data)
1328 int res = ub_ctx_finalize(ctx);
1329 if (res) return res;
1331 res = local_zones_add_RR(ctx->local_zones, data);
1332 return (!res) ? UB_NOMEM : UB_NOERROR;
1335 /* Remove RR data */
1336 int ub_ctx_data_remove(struct ub_ctx* ctx, const char *data)
1341 int res = ub_ctx_finalize(ctx);
1342 if (res) return res;
1344 if(!parse_dname(data, &nm, &nmlen, &nmlabs))
1347 local_zones_del_data(ctx->local_zones, nm, nmlen, nmlabs,
1354 const char* ub_version(void)
1356 return PACKAGE_VERSION;
1360 ub_ctx_set_event(struct ub_ctx* ctx, struct event_base* base) {
1361 struct ub_event_base* new_base;
1363 if (!ctx || !ctx->event_base || !base) {
1366 if (ub_libevent_get_event_base(ctx->event_base) == base) {
1371 lock_basic_lock(&ctx->cfglock);
1372 /* destroy the current worker - safe to pass in NULL */
1373 libworker_delete_event(ctx->event_worker);
1374 ctx->event_worker = NULL;
1375 new_base = ub_libevent_event_base(base);
1377 ctx->event_base = new_base;
1378 ctx->created_bg = 0;
1380 lock_basic_unlock(&ctx->cfglock);
1381 return new_base ? UB_NOERROR : UB_INITFAIL;