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 "services/modstack.h"
61 #include "services/localzone.h"
62 #include "services/cache/infra.h"
63 #include "services/cache/rrset.h"
64 #include "ldns/sbuffer.h"
69 #if defined(UB_ON_WINDOWS) && defined (HAVE_WINDOWS_H)
72 #endif /* UB_ON_WINDOWS */
74 /** create context functionality, but no pipes */
75 static struct ub_ctx* ub_ctx_create_nopipe(void)
84 log_init(NULL, 0, NULL); /* logs to stderr */
85 log_ident_set("libunbound");
87 if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0) {
88 log_err("could not init winsock. WSAStartup: %s",
93 verbosity = 0; /* errors only */
95 ctx = (struct ub_ctx*)calloc(1, sizeof(*ctx));
100 alloc_init(&ctx->superalloc, NULL, 0);
101 seed = (unsigned int)time(NULL) ^ (unsigned int)getpid();
102 if(!(ctx->seed_rnd = ub_initstate(seed, NULL))) {
104 ub_randfree(ctx->seed_rnd);
110 lock_basic_init(&ctx->qqpipe_lock);
111 lock_basic_init(&ctx->rrpipe_lock);
112 lock_basic_init(&ctx->cfglock);
113 ctx->env = (struct module_env*)calloc(1, sizeof(*ctx->env));
115 ub_randfree(ctx->seed_rnd);
120 ctx->env->cfg = config_create_forlib();
123 ub_randfree(ctx->seed_rnd);
128 ctx->env->alloc = &ctx->superalloc;
129 ctx->env->worker = NULL;
130 ctx->env->need_to_validate = 0;
131 modstack_init(&ctx->mods);
132 rbtree_init(&ctx->queries, &context_query_cmp);
139 struct ub_ctx* ctx = ub_ctx_create_nopipe();
142 if((ctx->qq_pipe = tube_create()) == NULL) {
144 ub_randfree(ctx->seed_rnd);
145 config_delete(ctx->env->cfg);
146 modstack_desetup(&ctx->mods, ctx->env);
152 if((ctx->rr_pipe = tube_create()) == NULL) {
154 tube_delete(ctx->qq_pipe);
155 ub_randfree(ctx->seed_rnd);
156 config_delete(ctx->env->cfg);
157 modstack_desetup(&ctx->mods, ctx->env);
167 ub_ctx_create_event(struct event_base* eb)
169 struct ub_ctx* ctx = ub_ctx_create_nopipe();
172 /* no pipes, but we have the locks to make sure everything works */
174 ctx->dothread = 1; /* the processing is in the same process,
175 makes ub_cancel and ub_ctx_delete do the right thing */
176 ctx->event_base = eb;
182 delq(rbnode_t* n, void* ATTR_UNUSED(arg))
184 struct ctx_query* q = (struct ctx_query*)n;
185 context_query_delete(q);
188 /** stop the bg thread */
189 static void ub_stop_bg(struct ub_ctx* ctx)
191 /* stop the bg thread */
192 lock_basic_lock(&ctx->cfglock);
193 if(ctx->created_bg) {
196 uint32_t cmd = UB_LIBCMD_QUIT;
197 lock_basic_unlock(&ctx->cfglock);
198 lock_basic_lock(&ctx->qqpipe_lock);
199 (void)tube_write_msg(ctx->qq_pipe, (uint8_t*)&cmd,
200 (uint32_t)sizeof(cmd), 0);
201 lock_basic_unlock(&ctx->qqpipe_lock);
202 lock_basic_lock(&ctx->rrpipe_lock);
203 while(tube_read_msg(ctx->rr_pipe, &msg, &len, 0)) {
204 /* discard all results except a quit confirm */
205 if(context_serial_getcmd(msg, len) == UB_LIBCMD_QUIT) {
211 lock_basic_unlock(&ctx->rrpipe_lock);
213 /* if bg worker is a thread, wait for it to exit, so that all
214 * resources are really gone. */
215 lock_basic_lock(&ctx->cfglock);
217 lock_basic_unlock(&ctx->cfglock);
218 ub_thread_join(ctx->bg_tid);
220 lock_basic_unlock(&ctx->cfglock);
224 lock_basic_unlock(&ctx->cfglock);
229 ub_ctx_delete(struct ub_ctx* ctx)
231 struct alloc_cache* a, *na;
235 /* see if bg thread is created and if threads have been killed */
236 /* no locks, because those may be held by terminated threads */
237 /* for processes the read pipe is closed and we see that on read */
239 if(ctx->created_bg && ctx->dothread) {
240 if(pthread_kill(ctx->bg_tid, 0) == ESRCH) {
241 /* thread has been killed */
245 #endif /* HAVE_PTHREAD */
248 libworker_delete_event(ctx->event_worker);
250 modstack_desetup(&ctx->mods, ctx->env);
254 a->super = &ctx->superalloc;
259 local_zones_delete(ctx->local_zones);
260 lock_basic_destroy(&ctx->qqpipe_lock);
261 lock_basic_destroy(&ctx->rrpipe_lock);
262 lock_basic_destroy(&ctx->cfglock);
263 tube_delete(ctx->qq_pipe);
264 tube_delete(ctx->rr_pipe);
266 slabhash_delete(ctx->env->msg_cache);
267 rrset_cache_delete(ctx->env->rrset_cache);
268 infra_delete(ctx->env->infra_cache);
269 config_delete(ctx->env->cfg);
272 ub_randfree(ctx->seed_rnd);
273 alloc_clear(&ctx->superalloc);
274 traverse_postorder(&ctx->queries, delq, NULL);
282 ub_ctx_set_option(struct ub_ctx* ctx, const char* opt, const char* val)
284 lock_basic_lock(&ctx->cfglock);
286 lock_basic_unlock(&ctx->cfglock);
287 return UB_AFTERFINAL;
289 if(!config_set_option(ctx->env->cfg, opt, val)) {
290 lock_basic_unlock(&ctx->cfglock);
293 lock_basic_unlock(&ctx->cfglock);
298 ub_ctx_get_option(struct ub_ctx* ctx, const char* opt, char** str)
301 lock_basic_lock(&ctx->cfglock);
302 r = config_get_option_collate(ctx->env->cfg, opt, str);
303 lock_basic_unlock(&ctx->cfglock);
304 if(r == 0) r = UB_NOERROR;
305 else if(r == 1) r = UB_SYNTAX;
306 else if(r == 2) r = UB_NOMEM;
311 ub_ctx_config(struct ub_ctx* ctx, const char* fname)
313 lock_basic_lock(&ctx->cfglock);
315 lock_basic_unlock(&ctx->cfglock);
316 return UB_AFTERFINAL;
318 if(!config_read(ctx->env->cfg, fname, NULL)) {
319 lock_basic_unlock(&ctx->cfglock);
322 lock_basic_unlock(&ctx->cfglock);
327 ub_ctx_add_ta(struct ub_ctx* ctx, const char* ta)
329 char* dup = strdup(ta);
330 if(!dup) return UB_NOMEM;
331 lock_basic_lock(&ctx->cfglock);
333 lock_basic_unlock(&ctx->cfglock);
335 return UB_AFTERFINAL;
337 if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_list, dup)) {
338 lock_basic_unlock(&ctx->cfglock);
342 lock_basic_unlock(&ctx->cfglock);
347 ub_ctx_add_ta_file(struct ub_ctx* ctx, const char* fname)
349 char* dup = strdup(fname);
350 if(!dup) return UB_NOMEM;
351 lock_basic_lock(&ctx->cfglock);
353 lock_basic_unlock(&ctx->cfglock);
355 return UB_AFTERFINAL;
357 if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_file_list, dup)) {
358 lock_basic_unlock(&ctx->cfglock);
362 lock_basic_unlock(&ctx->cfglock);
367 ub_ctx_trustedkeys(struct ub_ctx* ctx, const char* fname)
369 char* dup = strdup(fname);
370 if(!dup) return UB_NOMEM;
371 lock_basic_lock(&ctx->cfglock);
373 lock_basic_unlock(&ctx->cfglock);
375 return UB_AFTERFINAL;
377 if(!cfg_strlist_insert(&ctx->env->cfg->trusted_keys_file_list, dup)) {
378 lock_basic_unlock(&ctx->cfglock);
382 lock_basic_unlock(&ctx->cfglock);
387 ub_ctx_debuglevel(struct ub_ctx* ctx, int d)
389 lock_basic_lock(&ctx->cfglock);
391 ctx->env->cfg->verbosity = d;
392 lock_basic_unlock(&ctx->cfglock);
396 int ub_ctx_debugout(struct ub_ctx* ctx, void* out)
398 lock_basic_lock(&ctx->cfglock);
399 log_file((FILE*)out);
400 ctx->logfile_override = 1;
402 lock_basic_unlock(&ctx->cfglock);
407 ub_ctx_async(struct ub_ctx* ctx, int dothread)
409 #ifdef THREADS_DISABLED
410 if(dothread) /* cannot do threading */
413 lock_basic_lock(&ctx->cfglock);
415 lock_basic_unlock(&ctx->cfglock);
416 return UB_AFTERFINAL;
418 ctx->dothread = dothread;
419 lock_basic_unlock(&ctx->cfglock);
424 ub_poll(struct ub_ctx* ctx)
426 /* no need to hold lock while testing for readability. */
427 return tube_poll(ctx->rr_pipe);
431 ub_fd(struct ub_ctx* ctx)
433 return tube_read_fd(ctx->rr_pipe);
436 /** process answer from bg worker */
438 process_answer_detail(struct ub_ctx* ctx, uint8_t* msg, uint32_t len,
439 ub_callback_t* cb, void** cbarg, int* err,
440 struct ub_result** res)
443 if(context_serial_getcmd(msg, len) != UB_LIBCMD_ANSWER) {
444 log_err("error: bad data from bg worker %d",
445 (int)context_serial_getcmd(msg, len));
449 lock_basic_lock(&ctx->cfglock);
450 q = context_deserialize_answer(ctx, msg, len, err);
452 lock_basic_unlock(&ctx->cfglock);
453 /* probably simply the lookup that failed, i.e.
454 * response returned before cancel was sent out, so noerror */
457 log_assert(q->async);
459 /* grab cb while locked */
469 ub_resolve_free(q->res);
471 /* parse the message, extract rcode, fill result */
472 sldns_buffer* buf = sldns_buffer_new(q->msg_len);
473 struct regional* region = regional_create();
475 (*res)->rcode = LDNS_RCODE_SERVFAIL;
477 sldns_buffer_clear(buf);
478 sldns_buffer_write(buf, q->msg, q->msg_len);
479 sldns_buffer_flip(buf);
480 libworker_enter_result(*res, buf, region,
483 (*res)->answer_packet = q->msg;
484 (*res)->answer_len = (int)q->msg_len;
486 sldns_buffer_free(buf);
487 regional_destroy(region);
490 /* delete the q from list */
491 (void)rbtree_delete(&ctx->queries, q->node.key);
493 context_query_delete(q);
494 lock_basic_unlock(&ctx->cfglock);
497 ub_resolve_free(*res);
501 /** process answer from bg worker */
503 process_answer(struct ub_ctx* ctx, uint8_t* msg, uint32_t len)
508 struct ub_result* res;
511 r = process_answer_detail(ctx, msg, len, &cb, &cbarg, &err, &res);
513 /* no locks held while calling callback, so that library is
516 (*cb)(cbarg, err, res);
522 ub_process(struct ub_ctx* ctx)
529 lock_basic_lock(&ctx->rrpipe_lock);
530 r = tube_read_msg(ctx->rr_pipe, &msg, &len, 1);
531 lock_basic_unlock(&ctx->rrpipe_lock);
536 if(!process_answer(ctx, msg, len)) {
546 ub_wait(struct ub_ctx* ctx)
551 struct ub_result* res;
555 /* this is basically the same loop as _process(), but with changes.
556 * holds the rrpipe lock and waits with tube_wait */
558 lock_basic_lock(&ctx->rrpipe_lock);
559 lock_basic_lock(&ctx->cfglock);
560 if(ctx->num_async == 0) {
561 lock_basic_unlock(&ctx->cfglock);
562 lock_basic_unlock(&ctx->rrpipe_lock);
565 lock_basic_unlock(&ctx->cfglock);
567 /* keep rrpipe locked, while
568 * o waiting for pipe readable
570 * o possibly decrementing num_async
571 * do callback without lock
573 r = tube_wait(ctx->rr_pipe);
575 r = tube_read_msg(ctx->rr_pipe, &msg, &len, 1);
577 lock_basic_unlock(&ctx->rrpipe_lock);
581 lock_basic_unlock(&ctx->rrpipe_lock);
584 r = process_answer_detail(ctx, msg, len,
585 &cb, &cbarg, &err, &res);
586 lock_basic_unlock(&ctx->rrpipe_lock);
591 (*cb)(cbarg, err, res);
593 lock_basic_unlock(&ctx->rrpipe_lock);
600 ub_resolve(struct ub_ctx* ctx, const char* name, int rrtype,
601 int rrclass, struct ub_result** result)
607 lock_basic_lock(&ctx->cfglock);
608 if(!ctx->finalized) {
609 r = context_finalize(ctx);
611 lock_basic_unlock(&ctx->cfglock);
615 /* create new ctx_query and attempt to add to the list */
616 lock_basic_unlock(&ctx->cfglock);
617 q = context_new(ctx, name, rrtype, rrclass, NULL, NULL);
620 /* become a resolver thread for a bit */
622 r = libworker_fg(ctx, q);
624 lock_basic_lock(&ctx->cfglock);
625 (void)rbtree_delete(&ctx->queries, q->node.key);
626 context_query_delete(q);
627 lock_basic_unlock(&ctx->cfglock);
630 q->res->answer_packet = q->msg;
631 q->res->answer_len = (int)q->msg_len;
636 lock_basic_lock(&ctx->cfglock);
637 (void)rbtree_delete(&ctx->queries, q->node.key);
638 context_query_delete(q);
639 lock_basic_unlock(&ctx->cfglock);
644 ub_resolve_event(struct ub_ctx* ctx, const char* name, int rrtype,
645 int rrclass, void* mydata, ub_event_callback_t callback, int* async_id)
652 lock_basic_lock(&ctx->cfglock);
653 if(!ctx->finalized) {
654 int r = context_finalize(ctx);
656 lock_basic_unlock(&ctx->cfglock);
660 lock_basic_unlock(&ctx->cfglock);
661 if(!ctx->event_worker) {
662 ctx->event_worker = libworker_create_event(ctx,
664 if(!ctx->event_worker) {
669 /* create new ctx_query and attempt to add to the list */
670 q = context_new(ctx, name, rrtype, rrclass, (ub_callback_t)callback,
676 if((r=libworker_attach_mesh(ctx, q, async_id)) != 0)
683 ub_resolve_async(struct ub_ctx* ctx, const char* name, int rrtype,
684 int rrclass, void* mydata, ub_callback_t callback, int* async_id)
692 lock_basic_lock(&ctx->cfglock);
693 if(!ctx->finalized) {
694 int r = context_finalize(ctx);
696 lock_basic_unlock(&ctx->cfglock);
700 if(!ctx->created_bg) {
703 lock_basic_unlock(&ctx->cfglock);
704 r = libworker_bg(ctx);
706 lock_basic_lock(&ctx->cfglock);
708 lock_basic_unlock(&ctx->cfglock);
712 lock_basic_unlock(&ctx->cfglock);
715 /* create new ctx_query and attempt to add to the list */
716 q = context_new(ctx, name, rrtype, rrclass, callback, mydata);
720 /* write over pipe to background worker */
721 lock_basic_lock(&ctx->cfglock);
722 msg = context_serialize_new_query(q, &len);
724 (void)rbtree_delete(&ctx->queries, q->node.key);
726 context_query_delete(q);
727 lock_basic_unlock(&ctx->cfglock);
731 *async_id = q->querynum;
732 lock_basic_unlock(&ctx->cfglock);
734 lock_basic_lock(&ctx->qqpipe_lock);
735 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
736 lock_basic_unlock(&ctx->qqpipe_lock);
740 lock_basic_unlock(&ctx->qqpipe_lock);
746 ub_cancel(struct ub_ctx* ctx, int async_id)
751 lock_basic_lock(&ctx->cfglock);
752 q = (struct ctx_query*)rbtree_search(&ctx->queries, &async_id);
753 if(!q || !q->async) {
754 /* it is not there, so nothing to do */
755 lock_basic_unlock(&ctx->cfglock);
758 log_assert(q->async);
762 if(!ctx->dothread) { /* if forked */
763 (void)rbtree_delete(&ctx->queries, q->node.key);
765 msg = context_serialize_cancel(q, &len);
766 context_query_delete(q);
767 lock_basic_unlock(&ctx->cfglock);
771 /* send cancel to background worker */
772 lock_basic_lock(&ctx->qqpipe_lock);
773 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
774 lock_basic_unlock(&ctx->qqpipe_lock);
778 lock_basic_unlock(&ctx->qqpipe_lock);
781 lock_basic_unlock(&ctx->cfglock);
787 ub_resolve_free(struct ub_result* result)
792 if(result->canonname != result->qname)
793 free(result->canonname);
795 for(p = result->data; *p; p++)
799 free(result->answer_packet);
800 free(result->why_bogus);
808 case UB_NOERROR: return "no error";
809 case UB_SOCKET: return "socket io error";
810 case UB_NOMEM: return "out of memory";
811 case UB_SYNTAX: return "syntax error";
812 case UB_SERVFAIL: return "server failure";
813 case UB_FORKFAIL: return "could not fork";
814 case UB_INITFAIL: return "initialization failure";
815 case UB_AFTERFINAL: return "setting change after finalize";
816 case UB_PIPE: return "error in pipe communication with async";
817 case UB_READFILE: return "error reading file";
818 case UB_NOID: return "error async_id does not exist";
819 default: return "unknown error";
824 ub_ctx_set_fwd(struct ub_ctx* ctx, const char* addr)
826 struct sockaddr_storage storage;
828 struct config_stub* s;
830 lock_basic_lock(&ctx->cfglock);
832 lock_basic_unlock(&ctx->cfglock);
834 return UB_AFTERFINAL;
837 /* disable fwd mode - the root stub should be first. */
838 if(ctx->env->cfg->forwards &&
839 strcmp(ctx->env->cfg->forwards->name, ".") == 0) {
840 s = ctx->env->cfg->forwards;
841 ctx->env->cfg->forwards = s->next;
845 lock_basic_unlock(&ctx->cfglock);
848 lock_basic_unlock(&ctx->cfglock);
850 /* check syntax for addr */
851 if(!extstrtoaddr(addr, &storage, &stlen)) {
856 /* it parses, add root stub in front of list */
857 lock_basic_lock(&ctx->cfglock);
858 if(!ctx->env->cfg->forwards ||
859 strcmp(ctx->env->cfg->forwards->name, ".") != 0) {
860 s = calloc(1, sizeof(*s));
862 lock_basic_unlock(&ctx->cfglock);
866 s->name = strdup(".");
869 lock_basic_unlock(&ctx->cfglock);
873 s->next = ctx->env->cfg->forwards;
874 ctx->env->cfg->forwards = s;
876 log_assert(ctx->env->cfg->forwards);
877 s = ctx->env->cfg->forwards;
881 lock_basic_unlock(&ctx->cfglock);
885 if(!cfg_strlist_insert(&s->addrs, dupl)) {
887 lock_basic_unlock(&ctx->cfglock);
891 lock_basic_unlock(&ctx->cfglock);
896 ub_ctx_resolvconf(struct ub_ctx* ctx, const char* fname)
905 #if !defined(UB_ON_WINDOWS) || !defined(HAVE_WINDOWS_H)
906 fname = "/etc/resolv.conf";
909 ULONG buflen = sizeof(*info);
912 info = (FIXED_INFO *) malloc(sizeof (FIXED_INFO));
916 if (GetNetworkParams(info, &buflen) == ERROR_BUFFER_OVERFLOW) {
918 info = (FIXED_INFO *) malloc(buflen);
923 if (GetNetworkParams(info, &buflen) == NO_ERROR) {
925 ptr = &(info->DnsServerList);
928 if((retval=ub_ctx_set_fwd(ctx,
929 ptr->IpAddress.String)!=0)) {
944 in = fopen(fname, "r");
946 /* error in errno! perror(fname) */
949 while(fgets(buf, (int)sizeof(buf), in)) {
950 buf[sizeof(buf)-1] = 0;
952 while(*parse == ' ' || *parse == '\t')
954 if(strncmp(parse, "nameserver", 10) == 0) {
956 parse += 10; /* skip 'nameserver' */
957 /* skip whitespace */
958 while(*parse == ' ' || *parse == '\t')
961 /* skip [0-9a-fA-F.:]*, i.e. IP4 and IP6 address */
962 while(isxdigit(*parse) || *parse=='.' || *parse==':')
964 /* terminate after the address, remove newline */
967 if((r = ub_ctx_set_fwd(ctx, addr)) != UB_NOERROR) {
975 /* from resolv.conf(5) if none given, use localhost */
976 return ub_ctx_set_fwd(ctx, "127.0.0.1");
982 ub_ctx_hosts(struct ub_ctx* ctx, const char* fname)
985 char buf[1024], ldata[1024];
986 char* parse, *addr, *name, *ins;
987 lock_basic_lock(&ctx->cfglock);
989 lock_basic_unlock(&ctx->cfglock);
991 return UB_AFTERFINAL;
993 lock_basic_unlock(&ctx->cfglock);
995 #if defined(UB_ON_WINDOWS) && defined(HAVE_WINDOWS_H)
997 * If this is Windows NT/XP/2K it's in
998 * %WINDIR%\system32\drivers\etc\hosts.
999 * If this is Windows 95/98/Me it's in %WINDIR%\hosts.
1001 name = getenv("WINDIR");
1004 snprintf(buf, sizeof(buf), "%s%s", name,
1005 "\\system32\\drivers\\etc\\hosts");
1006 if((retval=ub_ctx_hosts(ctx, buf)) !=0 ) {
1007 snprintf(buf, sizeof(buf), "%s%s", name,
1009 retval=ub_ctx_hosts(ctx, buf);
1016 fname = "/etc/hosts";
1019 in = fopen(fname, "r");
1021 /* error in errno! perror(fname) */
1024 while(fgets(buf, (int)sizeof(buf), in)) {
1025 buf[sizeof(buf)-1] = 0;
1027 while(*parse == ' ' || *parse == '\t')
1030 continue; /* skip comment */
1031 /* format: <addr> spaces <name> spaces <name> ... */
1034 while(isxdigit(*parse) || *parse == '.' || *parse == ':')
1036 if(*parse == '\n' || *parse == 0)
1039 continue; /* ignore macOSX fe80::1%lo0 localhost */
1040 if(*parse != ' ' && *parse != '\t') {
1041 /* must have whitespace after address */
1046 *parse++ = 0; /* end delimiter for addr ... */
1047 /* go to names and add them */
1049 while(*parse == ' ' || *parse == '\t' || *parse=='\n')
1051 if(*parse == 0 || *parse == '#')
1053 /* skip name, allows (too) many printable characters */
1055 while('!' <= *parse && *parse <= '~')
1058 *parse++ = 0; /* end delimiter for name */
1059 snprintf(ldata, sizeof(ldata), "%s %s %s",
1060 name, str_is_ip6(addr)?"AAAA":"A", addr);
1061 ins = strdup(ldata);
1068 lock_basic_lock(&ctx->cfglock);
1069 if(!cfg_strlist_insert(&ctx->env->cfg->local_data,
1071 lock_basic_unlock(&ctx->cfglock);
1077 lock_basic_unlock(&ctx->cfglock);
1084 /** finalize the context, if not already finalized */
1085 static int ub_ctx_finalize(struct ub_ctx* ctx)
1088 lock_basic_lock(&ctx->cfglock);
1089 if (!ctx->finalized) {
1090 res = context_finalize(ctx);
1092 lock_basic_unlock(&ctx->cfglock);
1096 /* Print local zones and RR data */
1097 int ub_ctx_print_local_zones(struct ub_ctx* ctx)
1099 int res = ub_ctx_finalize(ctx);
1100 if (res) return res;
1102 local_zones_print(ctx->local_zones);
1107 /* Add a new zone */
1108 int ub_ctx_zone_add(struct ub_ctx* ctx, const char *zone_name,
1109 const char *zone_type)
1111 enum localzone_type t;
1112 struct local_zone* z;
1117 int res = ub_ctx_finalize(ctx);
1118 if (res) return res;
1120 if(!local_zone_str2type(zone_type, &t)) {
1124 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1128 lock_rw_wrlock(&ctx->local_zones->lock);
1129 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1130 LDNS_RR_CLASS_IN))) {
1131 /* already present in tree */
1132 lock_rw_wrlock(&z->lock);
1133 z->type = t; /* update type anyway */
1134 lock_rw_unlock(&z->lock);
1135 lock_rw_unlock(&ctx->local_zones->lock);
1139 if(!local_zones_add_zone(ctx->local_zones, nm, nmlen, nmlabs,
1140 LDNS_RR_CLASS_IN, t)) {
1141 lock_rw_unlock(&ctx->local_zones->lock);
1144 lock_rw_unlock(&ctx->local_zones->lock);
1149 int ub_ctx_zone_remove(struct ub_ctx* ctx, const char *zone_name)
1151 struct local_zone* z;
1156 int res = ub_ctx_finalize(ctx);
1157 if (res) return res;
1159 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1163 lock_rw_wrlock(&ctx->local_zones->lock);
1164 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1165 LDNS_RR_CLASS_IN))) {
1166 /* present in tree */
1167 local_zones_del_zone(ctx->local_zones, z);
1169 lock_rw_unlock(&ctx->local_zones->lock);
1174 /* Add new RR data */
1175 int ub_ctx_data_add(struct ub_ctx* ctx, const char *data)
1177 int res = ub_ctx_finalize(ctx);
1178 if (res) return res;
1180 res = local_zones_add_RR(ctx->local_zones, data);
1181 return (!res) ? UB_NOMEM : UB_NOERROR;
1184 /* Remove RR data */
1185 int ub_ctx_data_remove(struct ub_ctx* ctx, const char *data)
1190 int res = ub_ctx_finalize(ctx);
1191 if (res) return res;
1193 if(!parse_dname(data, &nm, &nmlen, &nmlabs))
1196 local_zones_del_data(ctx->local_zones, nm, nmlen, nmlabs,
1203 const char* ub_version(void)
1205 return PACKAGE_VERSION;
1209 ub_ctx_set_event(struct ub_ctx* ctx, struct event_base* base) {
1210 if (!ctx || !ctx->event_base || !base) {
1213 if (ctx->event_base == base) {
1218 lock_basic_lock(&ctx->cfglock);
1219 /* destroy the current worker - safe to pass in NULL */
1220 libworker_delete_event(ctx->event_worker);
1221 ctx->event_worker = NULL;
1222 ctx->event_base = base;
1223 ctx->created_bg = 0;
1225 lock_basic_unlock(&ctx->cfglock);