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 ctx->env->alloc = &ctx->superalloc;
136 ctx->env->worker = NULL;
137 ctx->env->need_to_validate = 0;
138 modstack_init(&ctx->mods);
139 rbtree_init(&ctx->queries, &context_query_cmp);
146 struct ub_ctx* ctx = ub_ctx_create_nopipe();
149 if((ctx->qq_pipe = tube_create()) == NULL) {
151 ub_randfree(ctx->seed_rnd);
152 config_delete(ctx->env->cfg);
153 modstack_desetup(&ctx->mods, ctx->env);
159 if((ctx->rr_pipe = tube_create()) == NULL) {
161 tube_delete(ctx->qq_pipe);
162 ub_randfree(ctx->seed_rnd);
163 config_delete(ctx->env->cfg);
164 modstack_desetup(&ctx->mods, ctx->env);
174 ub_ctx_create_ub_event(struct ub_event_base* ueb)
176 struct ub_ctx* ctx = ub_ctx_create_nopipe();
179 /* no pipes, but we have the locks to make sure everything works */
181 ctx->dothread = 1; /* the processing is in the same process,
182 makes ub_cancel and ub_ctx_delete do the right thing */
183 ctx->event_base = ueb;
188 ub_ctx_create_event(struct event_base* eb)
190 struct ub_ctx* ctx = ub_ctx_create_nopipe();
193 /* no pipes, but we have the locks to make sure everything works */
195 ctx->dothread = 1; /* the processing is in the same process,
196 makes ub_cancel and ub_ctx_delete do the right thing */
197 ctx->event_base = ub_libevent_event_base(eb);
198 if (!ctx->event_base) {
207 delq(rbnode_t* n, void* ATTR_UNUSED(arg))
209 struct ctx_query* q = (struct ctx_query*)n;
210 context_query_delete(q);
213 /** stop the bg thread */
214 static void ub_stop_bg(struct ub_ctx* ctx)
216 /* stop the bg thread */
217 lock_basic_lock(&ctx->cfglock);
218 if(ctx->created_bg) {
221 uint32_t cmd = UB_LIBCMD_QUIT;
222 lock_basic_unlock(&ctx->cfglock);
223 lock_basic_lock(&ctx->qqpipe_lock);
224 (void)tube_write_msg(ctx->qq_pipe, (uint8_t*)&cmd,
225 (uint32_t)sizeof(cmd), 0);
226 lock_basic_unlock(&ctx->qqpipe_lock);
227 lock_basic_lock(&ctx->rrpipe_lock);
228 while(tube_read_msg(ctx->rr_pipe, &msg, &len, 0)) {
229 /* discard all results except a quit confirm */
230 if(context_serial_getcmd(msg, len) == UB_LIBCMD_QUIT) {
236 lock_basic_unlock(&ctx->rrpipe_lock);
238 /* if bg worker is a thread, wait for it to exit, so that all
239 * resources are really gone. */
240 lock_basic_lock(&ctx->cfglock);
242 lock_basic_unlock(&ctx->cfglock);
243 ub_thread_join(ctx->bg_tid);
245 lock_basic_unlock(&ctx->cfglock);
246 #ifndef UB_ON_WINDOWS
247 if(waitpid(ctx->bg_pid, NULL, 0) == -1) {
249 log_err("waitpid: %s", strerror(errno));
255 lock_basic_unlock(&ctx->cfglock);
260 ub_ctx_delete(struct ub_ctx* ctx)
262 struct alloc_cache* a, *na;
266 /* see if bg thread is created and if threads have been killed */
267 /* no locks, because those may be held by terminated threads */
268 /* for processes the read pipe is closed and we see that on read */
270 if(ctx->created_bg && ctx->dothread) {
271 if(pthread_kill(ctx->bg_tid, 0) == ESRCH) {
272 /* thread has been killed */
276 #endif /* HAVE_PTHREAD */
279 libworker_delete_event(ctx->event_worker);
281 modstack_desetup(&ctx->mods, ctx->env);
285 a->super = &ctx->superalloc;
290 local_zones_delete(ctx->local_zones);
291 lock_basic_destroy(&ctx->qqpipe_lock);
292 lock_basic_destroy(&ctx->rrpipe_lock);
293 lock_basic_destroy(&ctx->cfglock);
294 tube_delete(ctx->qq_pipe);
295 tube_delete(ctx->rr_pipe);
297 slabhash_delete(ctx->env->msg_cache);
298 rrset_cache_delete(ctx->env->rrset_cache);
299 infra_delete(ctx->env->infra_cache);
300 config_delete(ctx->env->cfg);
303 ub_randfree(ctx->seed_rnd);
304 alloc_clear(&ctx->superalloc);
305 traverse_postorder(&ctx->queries, delq, NULL);
313 ub_ctx_set_option(struct ub_ctx* ctx, const char* opt, const char* val)
315 lock_basic_lock(&ctx->cfglock);
317 lock_basic_unlock(&ctx->cfglock);
318 return UB_AFTERFINAL;
320 if(!config_set_option(ctx->env->cfg, opt, val)) {
321 lock_basic_unlock(&ctx->cfglock);
324 lock_basic_unlock(&ctx->cfglock);
329 ub_ctx_get_option(struct ub_ctx* ctx, const char* opt, char** str)
332 lock_basic_lock(&ctx->cfglock);
333 r = config_get_option_collate(ctx->env->cfg, opt, str);
334 lock_basic_unlock(&ctx->cfglock);
335 if(r == 0) r = UB_NOERROR;
336 else if(r == 1) r = UB_SYNTAX;
337 else if(r == 2) r = UB_NOMEM;
342 ub_ctx_config(struct ub_ctx* ctx, const char* fname)
344 lock_basic_lock(&ctx->cfglock);
346 lock_basic_unlock(&ctx->cfglock);
347 return UB_AFTERFINAL;
349 if(!config_read(ctx->env->cfg, fname, NULL)) {
350 lock_basic_unlock(&ctx->cfglock);
353 lock_basic_unlock(&ctx->cfglock);
358 ub_ctx_add_ta(struct ub_ctx* ctx, const char* ta)
360 char* dup = strdup(ta);
361 if(!dup) return UB_NOMEM;
362 lock_basic_lock(&ctx->cfglock);
364 lock_basic_unlock(&ctx->cfglock);
366 return UB_AFTERFINAL;
368 if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_list, dup)) {
369 lock_basic_unlock(&ctx->cfglock);
373 lock_basic_unlock(&ctx->cfglock);
378 ub_ctx_add_ta_file(struct ub_ctx* ctx, const char* fname)
380 char* dup = strdup(fname);
381 if(!dup) return UB_NOMEM;
382 lock_basic_lock(&ctx->cfglock);
384 lock_basic_unlock(&ctx->cfglock);
386 return UB_AFTERFINAL;
388 if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_file_list, dup)) {
389 lock_basic_unlock(&ctx->cfglock);
393 lock_basic_unlock(&ctx->cfglock);
397 int ub_ctx_add_ta_autr(struct ub_ctx* ctx, const char* fname)
399 char* dup = strdup(fname);
400 if(!dup) return UB_NOMEM;
401 lock_basic_lock(&ctx->cfglock);
403 lock_basic_unlock(&ctx->cfglock);
405 return UB_AFTERFINAL;
407 if(!cfg_strlist_insert(&ctx->env->cfg->auto_trust_anchor_file_list,
409 lock_basic_unlock(&ctx->cfglock);
413 lock_basic_unlock(&ctx->cfglock);
418 ub_ctx_trustedkeys(struct ub_ctx* ctx, const char* fname)
420 char* dup = strdup(fname);
421 if(!dup) return UB_NOMEM;
422 lock_basic_lock(&ctx->cfglock);
424 lock_basic_unlock(&ctx->cfglock);
426 return UB_AFTERFINAL;
428 if(!cfg_strlist_insert(&ctx->env->cfg->trusted_keys_file_list, dup)) {
429 lock_basic_unlock(&ctx->cfglock);
433 lock_basic_unlock(&ctx->cfglock);
438 ub_ctx_debuglevel(struct ub_ctx* ctx, int d)
440 lock_basic_lock(&ctx->cfglock);
442 ctx->env->cfg->verbosity = d;
443 lock_basic_unlock(&ctx->cfglock);
447 int ub_ctx_debugout(struct ub_ctx* ctx, void* out)
449 lock_basic_lock(&ctx->cfglock);
450 log_file((FILE*)out);
451 ctx->logfile_override = 1;
453 lock_basic_unlock(&ctx->cfglock);
458 ub_ctx_async(struct ub_ctx* ctx, int dothread)
460 #ifdef THREADS_DISABLED
461 if(dothread) /* cannot do threading */
464 lock_basic_lock(&ctx->cfglock);
466 lock_basic_unlock(&ctx->cfglock);
467 return UB_AFTERFINAL;
469 ctx->dothread = dothread;
470 lock_basic_unlock(&ctx->cfglock);
475 ub_poll(struct ub_ctx* ctx)
477 /* no need to hold lock while testing for readability. */
478 return tube_poll(ctx->rr_pipe);
482 ub_fd(struct ub_ctx* ctx)
484 return tube_read_fd(ctx->rr_pipe);
487 /** process answer from bg worker */
489 process_answer_detail(struct ub_ctx* ctx, uint8_t* msg, uint32_t len,
490 ub_callback_t* cb, void** cbarg, int* err,
491 struct ub_result** res)
494 if(context_serial_getcmd(msg, len) != UB_LIBCMD_ANSWER) {
495 log_err("error: bad data from bg worker %d",
496 (int)context_serial_getcmd(msg, len));
500 lock_basic_lock(&ctx->cfglock);
501 q = context_deserialize_answer(ctx, msg, len, err);
503 lock_basic_unlock(&ctx->cfglock);
504 /* probably simply the lookup that failed, i.e.
505 * response returned before cancel was sent out, so noerror */
508 log_assert(q->async);
510 /* grab cb while locked */
520 ub_resolve_free(q->res);
522 /* parse the message, extract rcode, fill result */
523 sldns_buffer* buf = sldns_buffer_new(q->msg_len);
524 struct regional* region = regional_create();
526 (*res)->rcode = LDNS_RCODE_SERVFAIL;
528 sldns_buffer_clear(buf);
529 sldns_buffer_write(buf, q->msg, q->msg_len);
530 sldns_buffer_flip(buf);
531 libworker_enter_result(*res, buf, region,
534 (*res)->answer_packet = q->msg;
535 (*res)->answer_len = (int)q->msg_len;
537 sldns_buffer_free(buf);
538 regional_destroy(region);
541 /* delete the q from list */
542 (void)rbtree_delete(&ctx->queries, q->node.key);
544 context_query_delete(q);
545 lock_basic_unlock(&ctx->cfglock);
548 ub_resolve_free(*res);
552 /** process answer from bg worker */
554 process_answer(struct ub_ctx* ctx, uint8_t* msg, uint32_t len)
559 struct ub_result* res;
562 r = process_answer_detail(ctx, msg, len, &cb, &cbarg, &err, &res);
564 /* no locks held while calling callback, so that library is
567 (*cb)(cbarg, err, res);
573 ub_process(struct ub_ctx* ctx)
580 lock_basic_lock(&ctx->rrpipe_lock);
581 r = tube_read_msg(ctx->rr_pipe, &msg, &len, 1);
582 lock_basic_unlock(&ctx->rrpipe_lock);
587 if(!process_answer(ctx, msg, len)) {
597 ub_wait(struct ub_ctx* ctx)
602 struct ub_result* res;
606 /* this is basically the same loop as _process(), but with changes.
607 * holds the rrpipe lock and waits with tube_wait */
609 lock_basic_lock(&ctx->rrpipe_lock);
610 lock_basic_lock(&ctx->cfglock);
611 if(ctx->num_async == 0) {
612 lock_basic_unlock(&ctx->cfglock);
613 lock_basic_unlock(&ctx->rrpipe_lock);
616 lock_basic_unlock(&ctx->cfglock);
618 /* keep rrpipe locked, while
619 * o waiting for pipe readable
621 * o possibly decrementing num_async
622 * do callback without lock
624 r = tube_wait(ctx->rr_pipe);
626 r = tube_read_msg(ctx->rr_pipe, &msg, &len, 1);
628 lock_basic_unlock(&ctx->rrpipe_lock);
632 lock_basic_unlock(&ctx->rrpipe_lock);
635 r = process_answer_detail(ctx, msg, len,
636 &cb, &cbarg, &err, &res);
637 lock_basic_unlock(&ctx->rrpipe_lock);
642 (*cb)(cbarg, err, res);
644 lock_basic_unlock(&ctx->rrpipe_lock);
651 ub_resolve(struct ub_ctx* ctx, const char* name, int rrtype,
652 int rrclass, struct ub_result** result)
658 lock_basic_lock(&ctx->cfglock);
659 if(!ctx->finalized) {
660 r = context_finalize(ctx);
662 lock_basic_unlock(&ctx->cfglock);
666 /* create new ctx_query and attempt to add to the list */
667 lock_basic_unlock(&ctx->cfglock);
668 q = context_new(ctx, name, rrtype, rrclass, NULL, NULL);
671 /* become a resolver thread for a bit */
673 r = libworker_fg(ctx, q);
675 lock_basic_lock(&ctx->cfglock);
676 (void)rbtree_delete(&ctx->queries, q->node.key);
677 context_query_delete(q);
678 lock_basic_unlock(&ctx->cfglock);
681 q->res->answer_packet = q->msg;
682 q->res->answer_len = (int)q->msg_len;
687 lock_basic_lock(&ctx->cfglock);
688 (void)rbtree_delete(&ctx->queries, q->node.key);
689 context_query_delete(q);
690 lock_basic_unlock(&ctx->cfglock);
695 ub_resolve_event(struct ub_ctx* ctx, const char* name, int rrtype,
696 int rrclass, void* mydata, ub_event_callback_t callback, int* async_id)
703 lock_basic_lock(&ctx->cfglock);
704 if(!ctx->finalized) {
705 int r = context_finalize(ctx);
707 lock_basic_unlock(&ctx->cfglock);
711 lock_basic_unlock(&ctx->cfglock);
712 if(!ctx->event_worker) {
713 ctx->event_worker = libworker_create_event(ctx,
715 if(!ctx->event_worker) {
720 /* set time in case answer comes from cache */
721 ub_comm_base_now(ctx->event_worker->base);
723 /* create new ctx_query and attempt to add to the list */
724 q = context_new(ctx, name, rrtype, rrclass, (ub_callback_t)callback,
730 if((r=libworker_attach_mesh(ctx, q, async_id)) != 0)
737 ub_resolve_async(struct ub_ctx* ctx, const char* name, int rrtype,
738 int rrclass, void* mydata, ub_callback_t callback, int* async_id)
746 lock_basic_lock(&ctx->cfglock);
747 if(!ctx->finalized) {
748 int r = context_finalize(ctx);
750 lock_basic_unlock(&ctx->cfglock);
754 if(!ctx->created_bg) {
757 lock_basic_unlock(&ctx->cfglock);
758 r = libworker_bg(ctx);
760 lock_basic_lock(&ctx->cfglock);
762 lock_basic_unlock(&ctx->cfglock);
766 lock_basic_unlock(&ctx->cfglock);
769 /* create new ctx_query and attempt to add to the list */
770 q = context_new(ctx, name, rrtype, rrclass, callback, mydata);
774 /* write over pipe to background worker */
775 lock_basic_lock(&ctx->cfglock);
776 msg = context_serialize_new_query(q, &len);
778 (void)rbtree_delete(&ctx->queries, q->node.key);
780 context_query_delete(q);
781 lock_basic_unlock(&ctx->cfglock);
785 *async_id = q->querynum;
786 lock_basic_unlock(&ctx->cfglock);
788 lock_basic_lock(&ctx->qqpipe_lock);
789 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
790 lock_basic_unlock(&ctx->qqpipe_lock);
794 lock_basic_unlock(&ctx->qqpipe_lock);
800 ub_cancel(struct ub_ctx* ctx, int async_id)
805 lock_basic_lock(&ctx->cfglock);
806 q = (struct ctx_query*)rbtree_search(&ctx->queries, &async_id);
807 if(!q || !q->async) {
808 /* it is not there, so nothing to do */
809 lock_basic_unlock(&ctx->cfglock);
812 log_assert(q->async);
816 if(!ctx->dothread) { /* if forked */
817 (void)rbtree_delete(&ctx->queries, q->node.key);
819 msg = context_serialize_cancel(q, &len);
820 context_query_delete(q);
821 lock_basic_unlock(&ctx->cfglock);
825 /* send cancel to background worker */
826 lock_basic_lock(&ctx->qqpipe_lock);
827 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
828 lock_basic_unlock(&ctx->qqpipe_lock);
832 lock_basic_unlock(&ctx->qqpipe_lock);
835 lock_basic_unlock(&ctx->cfglock);
841 ub_resolve_free(struct ub_result* result)
846 if(result->canonname != result->qname)
847 free(result->canonname);
849 for(p = result->data; *p; p++)
853 free(result->answer_packet);
854 free(result->why_bogus);
862 case UB_NOERROR: return "no error";
863 case UB_SOCKET: return "socket io error";
864 case UB_NOMEM: return "out of memory";
865 case UB_SYNTAX: return "syntax error";
866 case UB_SERVFAIL: return "server failure";
867 case UB_FORKFAIL: return "could not fork";
868 case UB_INITFAIL: return "initialization failure";
869 case UB_AFTERFINAL: return "setting change after finalize";
870 case UB_PIPE: return "error in pipe communication with async";
871 case UB_READFILE: return "error reading file";
872 case UB_NOID: return "error async_id does not exist";
873 default: return "unknown error";
878 ub_ctx_set_fwd(struct ub_ctx* ctx, const char* addr)
880 struct sockaddr_storage storage;
882 struct config_stub* s;
884 lock_basic_lock(&ctx->cfglock);
886 lock_basic_unlock(&ctx->cfglock);
888 return UB_AFTERFINAL;
891 /* disable fwd mode - the root stub should be first. */
892 if(ctx->env->cfg->forwards &&
893 strcmp(ctx->env->cfg->forwards->name, ".") == 0) {
894 s = ctx->env->cfg->forwards;
895 ctx->env->cfg->forwards = s->next;
899 lock_basic_unlock(&ctx->cfglock);
902 lock_basic_unlock(&ctx->cfglock);
904 /* check syntax for addr */
905 if(!extstrtoaddr(addr, &storage, &stlen)) {
910 /* it parses, add root stub in front of list */
911 lock_basic_lock(&ctx->cfglock);
912 if(!ctx->env->cfg->forwards ||
913 strcmp(ctx->env->cfg->forwards->name, ".") != 0) {
914 s = calloc(1, sizeof(*s));
916 lock_basic_unlock(&ctx->cfglock);
920 s->name = strdup(".");
923 lock_basic_unlock(&ctx->cfglock);
927 s->next = ctx->env->cfg->forwards;
928 ctx->env->cfg->forwards = s;
930 log_assert(ctx->env->cfg->forwards);
931 s = ctx->env->cfg->forwards;
935 lock_basic_unlock(&ctx->cfglock);
939 if(!cfg_strlist_insert(&s->addrs, dupl)) {
941 lock_basic_unlock(&ctx->cfglock);
945 lock_basic_unlock(&ctx->cfglock);
949 int ub_ctx_set_stub(struct ub_ctx* ctx, const char* zone, const char* addr,
953 struct config_stub **prev, *elem;
955 /* check syntax for zone name */
960 if(!parse_dname(zone, &nm, &nmlen, &nmlabs)) {
969 /* check syntax for addr (if not NULL) */
971 struct sockaddr_storage storage;
973 if(!extstrtoaddr(addr, &storage, &stlen)) {
979 lock_basic_lock(&ctx->cfglock);
981 lock_basic_unlock(&ctx->cfglock);
983 return UB_AFTERFINAL;
986 /* arguments all right, now find or add the stub */
987 prev = &ctx->env->cfg->stubs;
988 elem = cfg_stub_find(&prev, zone);
990 /* not found and we want to delete, nothing to do */
991 lock_basic_unlock(&ctx->cfglock);
993 } else if(elem && !addr) {
994 /* found, and we want to delete */
996 config_delstub(elem);
997 lock_basic_unlock(&ctx->cfglock);
1000 /* not found, create the stub entry */
1001 elem=(struct config_stub*)calloc(1, sizeof(struct config_stub));
1002 if(elem) elem->name = strdup(zone);
1003 if(!elem || !elem->name) {
1005 lock_basic_unlock(&ctx->cfglock);
1009 elem->next = ctx->env->cfg->stubs;
1010 ctx->env->cfg->stubs = elem;
1013 /* add the address to the list and set settings */
1014 elem->isprime = isprime;
1017 lock_basic_unlock(&ctx->cfglock);
1021 if(!cfg_strlist_insert(&elem->addrs, a)) {
1022 lock_basic_unlock(&ctx->cfglock);
1027 lock_basic_unlock(&ctx->cfglock);
1032 ub_ctx_resolvconf(struct ub_ctx* ctx, const char* fname)
1041 #if !defined(UB_ON_WINDOWS) || !defined(HAVE_WINDOWS_H)
1042 fname = "/etc/resolv.conf";
1045 ULONG buflen = sizeof(*info);
1046 IP_ADDR_STRING *ptr;
1048 info = (FIXED_INFO *) malloc(sizeof (FIXED_INFO));
1052 if (GetNetworkParams(info, &buflen) == ERROR_BUFFER_OVERFLOW) {
1054 info = (FIXED_INFO *) malloc(buflen);
1059 if (GetNetworkParams(info, &buflen) == NO_ERROR) {
1061 ptr = &(info->DnsServerList);
1064 if((retval=ub_ctx_set_fwd(ctx,
1065 ptr->IpAddress.String))!=0) {
1078 #endif /* WINDOWS */
1080 in = fopen(fname, "r");
1082 /* error in errno! perror(fname) */
1085 while(fgets(buf, (int)sizeof(buf), in)) {
1086 buf[sizeof(buf)-1] = 0;
1088 while(*parse == ' ' || *parse == '\t')
1090 if(strncmp(parse, "nameserver", 10) == 0) {
1092 parse += 10; /* skip 'nameserver' */
1093 /* skip whitespace */
1094 while(*parse == ' ' || *parse == '\t')
1097 /* skip [0-9a-fA-F.:]*, i.e. IP4 and IP6 address */
1098 while(isxdigit((unsigned char)*parse) || *parse=='.' || *parse==':')
1100 /* terminate after the address, remove newline */
1103 if((r = ub_ctx_set_fwd(ctx, addr)) != UB_NOERROR) {
1111 /* from resolv.conf(5) if none given, use localhost */
1112 return ub_ctx_set_fwd(ctx, "127.0.0.1");
1118 ub_ctx_hosts(struct ub_ctx* ctx, const char* fname)
1121 char buf[1024], ldata[1024];
1122 char* parse, *addr, *name, *ins;
1123 lock_basic_lock(&ctx->cfglock);
1124 if(ctx->finalized) {
1125 lock_basic_unlock(&ctx->cfglock);
1127 return UB_AFTERFINAL;
1129 lock_basic_unlock(&ctx->cfglock);
1131 #if defined(UB_ON_WINDOWS) && defined(HAVE_WINDOWS_H)
1133 * If this is Windows NT/XP/2K it's in
1134 * %WINDIR%\system32\drivers\etc\hosts.
1135 * If this is Windows 95/98/Me it's in %WINDIR%\hosts.
1137 name = getenv("WINDIR");
1140 snprintf(buf, sizeof(buf), "%s%s", name,
1141 "\\system32\\drivers\\etc\\hosts");
1142 if((retval=ub_ctx_hosts(ctx, buf)) !=0 ) {
1143 snprintf(buf, sizeof(buf), "%s%s", name,
1145 retval=ub_ctx_hosts(ctx, buf);
1151 fname = "/etc/hosts";
1154 in = fopen(fname, "r");
1156 /* error in errno! perror(fname) */
1159 while(fgets(buf, (int)sizeof(buf), in)) {
1160 buf[sizeof(buf)-1] = 0;
1162 while(*parse == ' ' || *parse == '\t')
1165 continue; /* skip comment */
1166 /* format: <addr> spaces <name> spaces <name> ... */
1169 while(isxdigit((unsigned char)*parse) || *parse == '.' || *parse == ':')
1173 if(*parse == '\n' || *parse == 0)
1176 continue; /* ignore macOSX fe80::1%lo0 localhost */
1177 if(*parse != ' ' && *parse != '\t') {
1178 /* must have whitespace after address */
1183 *parse++ = 0; /* end delimiter for addr ... */
1184 /* go to names and add them */
1186 while(*parse == ' ' || *parse == '\t' || *parse=='\n'
1189 if(*parse == 0 || *parse == '#')
1191 /* skip name, allows (too) many printable characters */
1193 while('!' <= *parse && *parse <= '~')
1196 *parse++ = 0; /* end delimiter for name */
1197 snprintf(ldata, sizeof(ldata), "%s %s %s",
1198 name, str_is_ip6(addr)?"AAAA":"A", addr);
1199 ins = strdup(ldata);
1206 lock_basic_lock(&ctx->cfglock);
1207 if(!cfg_strlist_insert(&ctx->env->cfg->local_data,
1209 lock_basic_unlock(&ctx->cfglock);
1215 lock_basic_unlock(&ctx->cfglock);
1222 /** finalize the context, if not already finalized */
1223 static int ub_ctx_finalize(struct ub_ctx* ctx)
1226 lock_basic_lock(&ctx->cfglock);
1227 if (!ctx->finalized) {
1228 res = context_finalize(ctx);
1230 lock_basic_unlock(&ctx->cfglock);
1234 /* Print local zones and RR data */
1235 int ub_ctx_print_local_zones(struct ub_ctx* ctx)
1237 int res = ub_ctx_finalize(ctx);
1238 if (res) return res;
1240 local_zones_print(ctx->local_zones);
1245 /* Add a new zone */
1246 int ub_ctx_zone_add(struct ub_ctx* ctx, const char *zone_name,
1247 const char *zone_type)
1249 enum localzone_type t;
1250 struct local_zone* z;
1255 int res = ub_ctx_finalize(ctx);
1256 if (res) return res;
1258 if(!local_zone_str2type(zone_type, &t)) {
1262 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1266 lock_rw_wrlock(&ctx->local_zones->lock);
1267 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1268 LDNS_RR_CLASS_IN))) {
1269 /* already present in tree */
1270 lock_rw_wrlock(&z->lock);
1271 z->type = t; /* update type anyway */
1272 lock_rw_unlock(&z->lock);
1273 lock_rw_unlock(&ctx->local_zones->lock);
1277 if(!local_zones_add_zone(ctx->local_zones, nm, nmlen, nmlabs,
1278 LDNS_RR_CLASS_IN, t)) {
1279 lock_rw_unlock(&ctx->local_zones->lock);
1282 lock_rw_unlock(&ctx->local_zones->lock);
1287 int ub_ctx_zone_remove(struct ub_ctx* ctx, const char *zone_name)
1289 struct local_zone* z;
1294 int res = ub_ctx_finalize(ctx);
1295 if (res) return res;
1297 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1301 lock_rw_wrlock(&ctx->local_zones->lock);
1302 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1303 LDNS_RR_CLASS_IN))) {
1304 /* present in tree */
1305 local_zones_del_zone(ctx->local_zones, z);
1307 lock_rw_unlock(&ctx->local_zones->lock);
1312 /* Add new RR data */
1313 int ub_ctx_data_add(struct ub_ctx* ctx, const char *data)
1315 int res = ub_ctx_finalize(ctx);
1316 if (res) return res;
1318 res = local_zones_add_RR(ctx->local_zones, data);
1319 return (!res) ? UB_NOMEM : UB_NOERROR;
1322 /* Remove RR data */
1323 int ub_ctx_data_remove(struct ub_ctx* ctx, const char *data)
1328 int res = ub_ctx_finalize(ctx);
1329 if (res) return res;
1331 if(!parse_dname(data, &nm, &nmlen, &nmlabs))
1334 local_zones_del_data(ctx->local_zones, nm, nmlen, nmlabs,
1341 const char* ub_version(void)
1343 return PACKAGE_VERSION;
1347 ub_ctx_set_event(struct ub_ctx* ctx, struct event_base* base) {
1348 struct ub_event_base* new_base;
1350 if (!ctx || !ctx->event_base || !base) {
1353 if (ub_libevent_get_event_base(ctx->event_base) == base) {
1358 lock_basic_lock(&ctx->cfglock);
1359 /* destroy the current worker - safe to pass in NULL */
1360 libworker_delete_event(ctx->event_worker);
1361 ctx->event_worker = NULL;
1362 new_base = ub_libevent_event_base(base);
1364 ctx->event_base = new_base;
1365 ctx->created_bg = 0;
1367 lock_basic_unlock(&ctx->cfglock);
1368 return new_base ? UB_NOERROR : UB_INITFAIL;