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_type* 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_type* 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_type callback,
717 lock_basic_lock(&ctx->cfglock);
718 if(!ctx->finalized) {
719 int r = context_finalize(ctx);
721 lock_basic_unlock(&ctx->cfglock);
725 lock_basic_unlock(&ctx->cfglock);
726 if(!ctx->event_worker) {
727 ctx->event_worker = libworker_create_event(ctx,
729 if(!ctx->event_worker) {
734 /* set time in case answer comes from cache */
735 ub_comm_base_now(ctx->event_worker->base);
737 /* create new ctx_query and attempt to add to the list */
738 q = context_new(ctx, name, rrtype, rrclass, (ub_callback_type)callback,
744 if((r=libworker_attach_mesh(ctx, q, async_id)) != 0)
751 ub_resolve_async(struct ub_ctx* ctx, const char* name, int rrtype,
752 int rrclass, void* mydata, ub_callback_type callback, int* async_id)
760 lock_basic_lock(&ctx->cfglock);
761 if(!ctx->finalized) {
762 int r = context_finalize(ctx);
764 lock_basic_unlock(&ctx->cfglock);
768 if(!ctx->created_bg) {
771 lock_basic_unlock(&ctx->cfglock);
772 r = libworker_bg(ctx);
774 lock_basic_lock(&ctx->cfglock);
776 lock_basic_unlock(&ctx->cfglock);
780 lock_basic_unlock(&ctx->cfglock);
783 /* create new ctx_query and attempt to add to the list */
784 q = context_new(ctx, name, rrtype, rrclass, callback, mydata);
788 /* write over pipe to background worker */
789 lock_basic_lock(&ctx->cfglock);
790 msg = context_serialize_new_query(q, &len);
792 (void)rbtree_delete(&ctx->queries, q->node.key);
794 context_query_delete(q);
795 lock_basic_unlock(&ctx->cfglock);
799 *async_id = q->querynum;
800 lock_basic_unlock(&ctx->cfglock);
802 lock_basic_lock(&ctx->qqpipe_lock);
803 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
804 lock_basic_unlock(&ctx->qqpipe_lock);
808 lock_basic_unlock(&ctx->qqpipe_lock);
814 ub_cancel(struct ub_ctx* ctx, int async_id)
819 lock_basic_lock(&ctx->cfglock);
820 q = (struct ctx_query*)rbtree_search(&ctx->queries, &async_id);
821 if(!q || !q->async) {
822 /* it is not there, so nothing to do */
823 lock_basic_unlock(&ctx->cfglock);
826 log_assert(q->async);
830 if(!ctx->dothread) { /* if forked */
831 (void)rbtree_delete(&ctx->queries, q->node.key);
833 msg = context_serialize_cancel(q, &len);
834 context_query_delete(q);
835 lock_basic_unlock(&ctx->cfglock);
839 /* send cancel to background worker */
840 lock_basic_lock(&ctx->qqpipe_lock);
841 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
842 lock_basic_unlock(&ctx->qqpipe_lock);
846 lock_basic_unlock(&ctx->qqpipe_lock);
849 lock_basic_unlock(&ctx->cfglock);
855 ub_resolve_free(struct ub_result* result)
860 if(result->canonname != result->qname)
861 free(result->canonname);
863 for(p = result->data; *p; p++)
867 free(result->answer_packet);
868 free(result->why_bogus);
876 case UB_NOERROR: return "no error";
877 case UB_SOCKET: return "socket io error";
878 case UB_NOMEM: return "out of memory";
879 case UB_SYNTAX: return "syntax error";
880 case UB_SERVFAIL: return "server failure";
881 case UB_FORKFAIL: return "could not fork";
882 case UB_INITFAIL: return "initialization failure";
883 case UB_AFTERFINAL: return "setting change after finalize";
884 case UB_PIPE: return "error in pipe communication with async";
885 case UB_READFILE: return "error reading file";
886 case UB_NOID: return "error async_id does not exist";
887 default: return "unknown error";
892 ub_ctx_set_fwd(struct ub_ctx* ctx, const char* addr)
894 struct sockaddr_storage storage;
896 struct config_stub* s;
898 lock_basic_lock(&ctx->cfglock);
900 lock_basic_unlock(&ctx->cfglock);
902 return UB_AFTERFINAL;
905 /* disable fwd mode - the root stub should be first. */
906 if(ctx->env->cfg->forwards &&
907 strcmp(ctx->env->cfg->forwards->name, ".") == 0) {
908 s = ctx->env->cfg->forwards;
909 ctx->env->cfg->forwards = s->next;
913 lock_basic_unlock(&ctx->cfglock);
916 lock_basic_unlock(&ctx->cfglock);
918 /* check syntax for addr */
919 if(!extstrtoaddr(addr, &storage, &stlen)) {
924 /* it parses, add root stub in front of list */
925 lock_basic_lock(&ctx->cfglock);
926 if(!ctx->env->cfg->forwards ||
927 strcmp(ctx->env->cfg->forwards->name, ".") != 0) {
928 s = calloc(1, sizeof(*s));
930 lock_basic_unlock(&ctx->cfglock);
934 s->name = strdup(".");
937 lock_basic_unlock(&ctx->cfglock);
941 s->next = ctx->env->cfg->forwards;
942 ctx->env->cfg->forwards = s;
944 log_assert(ctx->env->cfg->forwards);
945 s = ctx->env->cfg->forwards;
949 lock_basic_unlock(&ctx->cfglock);
953 if(!cfg_strlist_insert(&s->addrs, dupl)) {
955 lock_basic_unlock(&ctx->cfglock);
959 lock_basic_unlock(&ctx->cfglock);
963 int ub_ctx_set_stub(struct ub_ctx* ctx, const char* zone, const char* addr,
967 struct config_stub **prev, *elem;
969 /* check syntax for zone name */
974 if(!parse_dname(zone, &nm, &nmlen, &nmlabs)) {
983 /* check syntax for addr (if not NULL) */
985 struct sockaddr_storage storage;
987 if(!extstrtoaddr(addr, &storage, &stlen)) {
993 lock_basic_lock(&ctx->cfglock);
995 lock_basic_unlock(&ctx->cfglock);
997 return UB_AFTERFINAL;
1000 /* arguments all right, now find or add the stub */
1001 prev = &ctx->env->cfg->stubs;
1002 elem = cfg_stub_find(&prev, zone);
1003 if(!elem && !addr) {
1004 /* not found and we want to delete, nothing to do */
1005 lock_basic_unlock(&ctx->cfglock);
1007 } else if(elem && !addr) {
1008 /* found, and we want to delete */
1010 config_delstub(elem);
1011 lock_basic_unlock(&ctx->cfglock);
1014 /* not found, create the stub entry */
1015 elem=(struct config_stub*)calloc(1, sizeof(struct config_stub));
1016 if(elem) elem->name = strdup(zone);
1017 if(!elem || !elem->name) {
1019 lock_basic_unlock(&ctx->cfglock);
1023 elem->next = ctx->env->cfg->stubs;
1024 ctx->env->cfg->stubs = elem;
1027 /* add the address to the list and set settings */
1028 elem->isprime = isprime;
1031 lock_basic_unlock(&ctx->cfglock);
1035 if(!cfg_strlist_insert(&elem->addrs, a)) {
1036 lock_basic_unlock(&ctx->cfglock);
1041 lock_basic_unlock(&ctx->cfglock);
1046 ub_ctx_resolvconf(struct ub_ctx* ctx, const char* fname)
1055 #if !defined(UB_ON_WINDOWS) || !defined(HAVE_WINDOWS_H)
1056 fname = "/etc/resolv.conf";
1059 ULONG buflen = sizeof(*info);
1060 IP_ADDR_STRING *ptr;
1062 info = (FIXED_INFO *) malloc(sizeof (FIXED_INFO));
1066 if (GetNetworkParams(info, &buflen) == ERROR_BUFFER_OVERFLOW) {
1068 info = (FIXED_INFO *) malloc(buflen);
1073 if (GetNetworkParams(info, &buflen) == NO_ERROR) {
1075 ptr = &(info->DnsServerList);
1078 if((retval=ub_ctx_set_fwd(ctx,
1079 ptr->IpAddress.String))!=0) {
1092 #endif /* WINDOWS */
1094 in = fopen(fname, "r");
1096 /* error in errno! perror(fname) */
1099 while(fgets(buf, (int)sizeof(buf), in)) {
1100 buf[sizeof(buf)-1] = 0;
1102 while(*parse == ' ' || *parse == '\t')
1104 if(strncmp(parse, "nameserver", 10) == 0) {
1106 parse += 10; /* skip 'nameserver' */
1107 /* skip whitespace */
1108 while(*parse == ' ' || *parse == '\t')
1111 /* skip [0-9a-fA-F.:]*, i.e. IP4 and IP6 address */
1112 while(isxdigit((unsigned char)*parse) || *parse=='.' || *parse==':')
1114 /* terminate after the address, remove newline */
1117 if((r = ub_ctx_set_fwd(ctx, addr)) != UB_NOERROR) {
1125 /* from resolv.conf(5) if none given, use localhost */
1126 return ub_ctx_set_fwd(ctx, "127.0.0.1");
1132 ub_ctx_hosts(struct ub_ctx* ctx, const char* fname)
1135 char buf[1024], ldata[1024];
1136 char* parse, *addr, *name, *ins;
1137 lock_basic_lock(&ctx->cfglock);
1138 if(ctx->finalized) {
1139 lock_basic_unlock(&ctx->cfglock);
1141 return UB_AFTERFINAL;
1143 lock_basic_unlock(&ctx->cfglock);
1145 #if defined(UB_ON_WINDOWS) && defined(HAVE_WINDOWS_H)
1147 * If this is Windows NT/XP/2K it's in
1148 * %WINDIR%\system32\drivers\etc\hosts.
1149 * If this is Windows 95/98/Me it's in %WINDIR%\hosts.
1151 name = getenv("WINDIR");
1154 snprintf(buf, sizeof(buf), "%s%s", name,
1155 "\\system32\\drivers\\etc\\hosts");
1156 if((retval=ub_ctx_hosts(ctx, buf)) !=0 ) {
1157 snprintf(buf, sizeof(buf), "%s%s", name,
1159 retval=ub_ctx_hosts(ctx, buf);
1165 fname = "/etc/hosts";
1168 in = fopen(fname, "r");
1170 /* error in errno! perror(fname) */
1173 while(fgets(buf, (int)sizeof(buf), in)) {
1174 buf[sizeof(buf)-1] = 0;
1176 while(*parse == ' ' || *parse == '\t')
1179 continue; /* skip comment */
1180 /* format: <addr> spaces <name> spaces <name> ... */
1183 while(isxdigit((unsigned char)*parse) || *parse == '.' || *parse == ':')
1187 if(*parse == '\n' || *parse == 0)
1190 continue; /* ignore macOSX fe80::1%lo0 localhost */
1191 if(*parse != ' ' && *parse != '\t') {
1192 /* must have whitespace after address */
1197 *parse++ = 0; /* end delimiter for addr ... */
1198 /* go to names and add them */
1200 while(*parse == ' ' || *parse == '\t' || *parse=='\n'
1203 if(*parse == 0 || *parse == '#')
1205 /* skip name, allows (too) many printable characters */
1207 while('!' <= *parse && *parse <= '~')
1210 *parse++ = 0; /* end delimiter for name */
1211 snprintf(ldata, sizeof(ldata), "%s %s %s",
1212 name, str_is_ip6(addr)?"AAAA":"A", addr);
1213 ins = strdup(ldata);
1220 lock_basic_lock(&ctx->cfglock);
1221 if(!cfg_strlist_insert(&ctx->env->cfg->local_data,
1223 lock_basic_unlock(&ctx->cfglock);
1229 lock_basic_unlock(&ctx->cfglock);
1236 /** finalize the context, if not already finalized */
1237 static int ub_ctx_finalize(struct ub_ctx* ctx)
1240 lock_basic_lock(&ctx->cfglock);
1241 if (!ctx->finalized) {
1242 res = context_finalize(ctx);
1244 lock_basic_unlock(&ctx->cfglock);
1248 /* Print local zones and RR data */
1249 int ub_ctx_print_local_zones(struct ub_ctx* ctx)
1251 int res = ub_ctx_finalize(ctx);
1252 if (res) return res;
1254 local_zones_print(ctx->local_zones);
1259 /* Add a new zone */
1260 int ub_ctx_zone_add(struct ub_ctx* ctx, const char *zone_name,
1261 const char *zone_type)
1263 enum localzone_type t;
1264 struct local_zone* z;
1269 int res = ub_ctx_finalize(ctx);
1270 if (res) return res;
1272 if(!local_zone_str2type(zone_type, &t)) {
1276 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1280 lock_rw_wrlock(&ctx->local_zones->lock);
1281 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1282 LDNS_RR_CLASS_IN))) {
1283 /* already present in tree */
1284 lock_rw_wrlock(&z->lock);
1285 z->type = t; /* update type anyway */
1286 lock_rw_unlock(&z->lock);
1287 lock_rw_unlock(&ctx->local_zones->lock);
1291 if(!local_zones_add_zone(ctx->local_zones, nm, nmlen, nmlabs,
1292 LDNS_RR_CLASS_IN, t)) {
1293 lock_rw_unlock(&ctx->local_zones->lock);
1296 lock_rw_unlock(&ctx->local_zones->lock);
1301 int ub_ctx_zone_remove(struct ub_ctx* ctx, const char *zone_name)
1303 struct local_zone* z;
1308 int res = ub_ctx_finalize(ctx);
1309 if (res) return res;
1311 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1315 lock_rw_wrlock(&ctx->local_zones->lock);
1316 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1317 LDNS_RR_CLASS_IN))) {
1318 /* present in tree */
1319 local_zones_del_zone(ctx->local_zones, z);
1321 lock_rw_unlock(&ctx->local_zones->lock);
1326 /* Add new RR data */
1327 int ub_ctx_data_add(struct ub_ctx* ctx, const char *data)
1329 int res = ub_ctx_finalize(ctx);
1330 if (res) return res;
1332 res = local_zones_add_RR(ctx->local_zones, data);
1333 return (!res) ? UB_NOMEM : UB_NOERROR;
1336 /* Remove RR data */
1337 int ub_ctx_data_remove(struct ub_ctx* ctx, const char *data)
1342 int res = ub_ctx_finalize(ctx);
1343 if (res) return res;
1345 if(!parse_dname(data, &nm, &nmlen, &nmlabs))
1348 local_zones_del_data(ctx->local_zones, nm, nmlen, nmlabs,
1355 const char* ub_version(void)
1357 return PACKAGE_VERSION;
1361 ub_ctx_set_event(struct ub_ctx* ctx, struct event_base* base) {
1362 struct ub_event_base* new_base;
1364 if (!ctx || !ctx->event_base || !base) {
1367 if (ub_libevent_get_event_base(ctx->event_base) == base) {
1372 lock_basic_lock(&ctx->cfglock);
1373 /* destroy the current worker - safe to pass in NULL */
1374 libworker_delete_event(ctx->event_worker);
1375 ctx->event_worker = NULL;
1376 new_base = ub_libevent_event_base(base);
1378 ctx->event_base = new_base;
1379 ctx->created_bg = 0;
1381 lock_basic_unlock(&ctx->cfglock);
1382 return new_base ? UB_NOERROR : UB_INITFAIL;