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 LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * 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"
48 #include "libunbound/context.h"
49 #include "libunbound/libworker.h"
50 #include "util/locks.h"
51 #include "util/config_file.h"
52 #include "util/alloc.h"
53 #include "util/module.h"
54 #include "util/regional.h"
56 #include "util/random.h"
57 #include "util/net_help.h"
58 #include "util/tube.h"
59 #include "services/modstack.h"
60 #include "services/localzone.h"
61 #include "services/cache/infra.h"
62 #include "services/cache/rrset.h"
67 #if defined(UB_ON_WINDOWS) && defined (HAVE_WINDOWS_H)
70 #endif /* UB_ON_WINDOWS */
82 log_init(NULL, 0, NULL); /* logs to stderr */
83 log_ident_set("libunbound");
85 if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0) {
86 log_err("could not init winsock. WSAStartup: %s",
91 verbosity = 0; /* errors only */
93 ctx = (struct ub_ctx*)calloc(1, sizeof(*ctx));
98 alloc_init(&ctx->superalloc, NULL, 0);
99 seed = (unsigned int)time(NULL) ^ (unsigned int)getpid();
100 if(!(ctx->seed_rnd = ub_initstate(seed, NULL))) {
102 ub_randfree(ctx->seed_rnd);
108 if((ctx->qq_pipe = tube_create()) == NULL) {
110 ub_randfree(ctx->seed_rnd);
115 if((ctx->rr_pipe = tube_create()) == NULL) {
117 tube_delete(ctx->qq_pipe);
118 ub_randfree(ctx->seed_rnd);
123 lock_basic_init(&ctx->qqpipe_lock);
124 lock_basic_init(&ctx->rrpipe_lock);
125 lock_basic_init(&ctx->cfglock);
126 ctx->env = (struct module_env*)calloc(1, sizeof(*ctx->env));
128 tube_delete(ctx->qq_pipe);
129 tube_delete(ctx->rr_pipe);
130 ub_randfree(ctx->seed_rnd);
135 ctx->env->cfg = config_create_forlib();
137 tube_delete(ctx->qq_pipe);
138 tube_delete(ctx->rr_pipe);
140 ub_randfree(ctx->seed_rnd);
145 ctx->env->alloc = &ctx->superalloc;
146 ctx->env->worker = NULL;
147 ctx->env->need_to_validate = 0;
148 modstack_init(&ctx->mods);
149 rbtree_init(&ctx->queries, &context_query_cmp);
155 delq(rbnode_t* n, void* ATTR_UNUSED(arg))
157 struct ctx_query* q = (struct ctx_query*)n;
158 context_query_delete(q);
161 /** stop the bg thread */
162 static void ub_stop_bg(struct ub_ctx* ctx)
164 /* stop the bg thread */
165 lock_basic_lock(&ctx->cfglock);
166 if(ctx->created_bg) {
169 uint32_t cmd = UB_LIBCMD_QUIT;
170 lock_basic_unlock(&ctx->cfglock);
171 lock_basic_lock(&ctx->qqpipe_lock);
172 (void)tube_write_msg(ctx->qq_pipe, (uint8_t*)&cmd,
173 (uint32_t)sizeof(cmd), 0);
174 lock_basic_unlock(&ctx->qqpipe_lock);
175 lock_basic_lock(&ctx->rrpipe_lock);
176 while(tube_read_msg(ctx->rr_pipe, &msg, &len, 0)) {
177 /* discard all results except a quit confirm */
178 if(context_serial_getcmd(msg, len) == UB_LIBCMD_QUIT) {
184 lock_basic_unlock(&ctx->rrpipe_lock);
186 /* if bg worker is a thread, wait for it to exit, so that all
187 * resources are really gone. */
188 lock_basic_lock(&ctx->cfglock);
190 lock_basic_unlock(&ctx->cfglock);
191 ub_thread_join(ctx->bg_tid);
193 lock_basic_unlock(&ctx->cfglock);
197 lock_basic_unlock(&ctx->cfglock);
202 ub_ctx_delete(struct ub_ctx* ctx)
204 struct alloc_cache* a, *na;
208 /* see if bg thread is created and if threads have been killed */
209 /* no locks, because those may be held by terminated threads */
210 /* for processes the read pipe is closed and we see that on read */
212 if(ctx->created_bg && ctx->dothread) {
213 if(pthread_kill(ctx->bg_tid, 0) == ESRCH) {
214 /* thread has been killed */
218 #endif /* HAVE_PTHREAD */
222 modstack_desetup(&ctx->mods, ctx->env);
226 a->super = &ctx->superalloc;
231 local_zones_delete(ctx->local_zones);
232 lock_basic_destroy(&ctx->qqpipe_lock);
233 lock_basic_destroy(&ctx->rrpipe_lock);
234 lock_basic_destroy(&ctx->cfglock);
235 tube_delete(ctx->qq_pipe);
236 tube_delete(ctx->rr_pipe);
238 slabhash_delete(ctx->env->msg_cache);
239 rrset_cache_delete(ctx->env->rrset_cache);
240 infra_delete(ctx->env->infra_cache);
241 config_delete(ctx->env->cfg);
244 ub_randfree(ctx->seed_rnd);
245 alloc_clear(&ctx->superalloc);
246 traverse_postorder(&ctx->queries, delq, NULL);
254 ub_ctx_set_option(struct ub_ctx* ctx, const char* opt, const char* val)
256 lock_basic_lock(&ctx->cfglock);
258 lock_basic_unlock(&ctx->cfglock);
259 return UB_AFTERFINAL;
261 if(!config_set_option(ctx->env->cfg, opt, val)) {
262 lock_basic_unlock(&ctx->cfglock);
265 lock_basic_unlock(&ctx->cfglock);
270 ub_ctx_get_option(struct ub_ctx* ctx, const char* opt, char** str)
273 lock_basic_lock(&ctx->cfglock);
274 r = config_get_option_collate(ctx->env->cfg, opt, str);
275 lock_basic_unlock(&ctx->cfglock);
276 if(r == 0) r = UB_NOERROR;
277 else if(r == 1) r = UB_SYNTAX;
278 else if(r == 2) r = UB_NOMEM;
283 ub_ctx_config(struct ub_ctx* ctx, const char* fname)
285 lock_basic_lock(&ctx->cfglock);
287 lock_basic_unlock(&ctx->cfglock);
288 return UB_AFTERFINAL;
290 if(!config_read(ctx->env->cfg, fname, NULL)) {
291 lock_basic_unlock(&ctx->cfglock);
294 lock_basic_unlock(&ctx->cfglock);
299 ub_ctx_add_ta(struct ub_ctx* ctx, const char* ta)
301 char* dup = strdup(ta);
302 if(!dup) return UB_NOMEM;
303 lock_basic_lock(&ctx->cfglock);
305 lock_basic_unlock(&ctx->cfglock);
307 return UB_AFTERFINAL;
309 if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_list, dup)) {
310 lock_basic_unlock(&ctx->cfglock);
314 lock_basic_unlock(&ctx->cfglock);
319 ub_ctx_add_ta_file(struct ub_ctx* ctx, const char* fname)
321 char* dup = strdup(fname);
322 if(!dup) return UB_NOMEM;
323 lock_basic_lock(&ctx->cfglock);
325 lock_basic_unlock(&ctx->cfglock);
327 return UB_AFTERFINAL;
329 if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_file_list, dup)) {
330 lock_basic_unlock(&ctx->cfglock);
334 lock_basic_unlock(&ctx->cfglock);
339 ub_ctx_trustedkeys(struct ub_ctx* ctx, const char* fname)
341 char* dup = strdup(fname);
342 if(!dup) return UB_NOMEM;
343 lock_basic_lock(&ctx->cfglock);
345 lock_basic_unlock(&ctx->cfglock);
347 return UB_AFTERFINAL;
349 if(!cfg_strlist_insert(&ctx->env->cfg->trusted_keys_file_list, dup)) {
350 lock_basic_unlock(&ctx->cfglock);
354 lock_basic_unlock(&ctx->cfglock);
359 ub_ctx_debuglevel(struct ub_ctx* ctx, int d)
361 lock_basic_lock(&ctx->cfglock);
363 ctx->env->cfg->verbosity = d;
364 lock_basic_unlock(&ctx->cfglock);
368 int ub_ctx_debugout(struct ub_ctx* ctx, void* out)
370 lock_basic_lock(&ctx->cfglock);
371 log_file((FILE*)out);
372 ctx->logfile_override = 1;
374 lock_basic_unlock(&ctx->cfglock);
379 ub_ctx_async(struct ub_ctx* ctx, int dothread)
381 #ifdef THREADS_DISABLED
382 if(dothread) /* cannot do threading */
385 lock_basic_lock(&ctx->cfglock);
387 lock_basic_unlock(&ctx->cfglock);
388 return UB_AFTERFINAL;
390 ctx->dothread = dothread;
391 lock_basic_unlock(&ctx->cfglock);
396 ub_poll(struct ub_ctx* ctx)
398 /* no need to hold lock while testing for readability. */
399 return tube_poll(ctx->rr_pipe);
403 ub_fd(struct ub_ctx* ctx)
405 return tube_read_fd(ctx->rr_pipe);
408 /** process answer from bg worker */
410 process_answer_detail(struct ub_ctx* ctx, uint8_t* msg, uint32_t len,
411 ub_callback_t* cb, void** cbarg, int* err,
412 struct ub_result** res)
415 if(context_serial_getcmd(msg, len) != UB_LIBCMD_ANSWER) {
416 log_err("error: bad data from bg worker %d",
417 (int)context_serial_getcmd(msg, len));
421 lock_basic_lock(&ctx->cfglock);
422 q = context_deserialize_answer(ctx, msg, len, err);
424 lock_basic_unlock(&ctx->cfglock);
425 /* probably simply the lookup that failed, i.e.
426 * response returned before cancel was sent out, so noerror */
429 log_assert(q->async);
431 /* grab cb while locked */
441 ub_resolve_free(q->res);
443 /* parse the message, extract rcode, fill result */
444 ldns_buffer* buf = ldns_buffer_new(q->msg_len);
445 struct regional* region = regional_create();
447 (*res)->rcode = LDNS_RCODE_SERVFAIL;
449 ldns_buffer_clear(buf);
450 ldns_buffer_write(buf, q->msg, q->msg_len);
451 ldns_buffer_flip(buf);
452 libworker_enter_result(*res, buf, region,
455 (*res)->answer_packet = q->msg;
456 (*res)->answer_len = (int)q->msg_len;
458 ldns_buffer_free(buf);
459 regional_destroy(region);
462 /* delete the q from list */
463 (void)rbtree_delete(&ctx->queries, q->node.key);
465 context_query_delete(q);
466 lock_basic_unlock(&ctx->cfglock);
469 ub_resolve_free(*res);
473 /** process answer from bg worker */
475 process_answer(struct ub_ctx* ctx, uint8_t* msg, uint32_t len)
480 struct ub_result* res;
483 r = process_answer_detail(ctx, msg, len, &cb, &cbarg, &err, &res);
485 /* no locks held while calling callback, so that library is
488 (*cb)(cbarg, err, res);
494 ub_process(struct ub_ctx* ctx)
501 lock_basic_lock(&ctx->rrpipe_lock);
502 r = tube_read_msg(ctx->rr_pipe, &msg, &len, 1);
503 lock_basic_unlock(&ctx->rrpipe_lock);
508 if(!process_answer(ctx, msg, len)) {
518 ub_wait(struct ub_ctx* ctx)
523 struct ub_result* res;
527 /* this is basically the same loop as _process(), but with changes.
528 * holds the rrpipe lock and waits with tube_wait */
530 lock_basic_lock(&ctx->rrpipe_lock);
531 lock_basic_lock(&ctx->cfglock);
532 if(ctx->num_async == 0) {
533 lock_basic_unlock(&ctx->cfglock);
534 lock_basic_unlock(&ctx->rrpipe_lock);
537 lock_basic_unlock(&ctx->cfglock);
539 /* keep rrpipe locked, while
540 * o waiting for pipe readable
542 * o possibly decrementing num_async
543 * do callback without lock
545 r = tube_wait(ctx->rr_pipe);
547 r = tube_read_msg(ctx->rr_pipe, &msg, &len, 1);
549 lock_basic_unlock(&ctx->rrpipe_lock);
553 lock_basic_unlock(&ctx->rrpipe_lock);
556 r = process_answer_detail(ctx, msg, len,
557 &cb, &cbarg, &err, &res);
558 lock_basic_unlock(&ctx->rrpipe_lock);
563 (*cb)(cbarg, err, res);
565 lock_basic_unlock(&ctx->rrpipe_lock);
572 ub_resolve(struct ub_ctx* ctx, const char* name, int rrtype,
573 int rrclass, struct ub_result** result)
579 lock_basic_lock(&ctx->cfglock);
580 if(!ctx->finalized) {
581 r = context_finalize(ctx);
583 lock_basic_unlock(&ctx->cfglock);
587 /* create new ctx_query and attempt to add to the list */
588 lock_basic_unlock(&ctx->cfglock);
589 q = context_new(ctx, name, rrtype, rrclass, NULL, NULL);
592 /* become a resolver thread for a bit */
594 r = libworker_fg(ctx, q);
596 lock_basic_lock(&ctx->cfglock);
597 (void)rbtree_delete(&ctx->queries, q->node.key);
598 context_query_delete(q);
599 lock_basic_unlock(&ctx->cfglock);
602 q->res->answer_packet = q->msg;
603 q->res->answer_len = (int)q->msg_len;
608 lock_basic_lock(&ctx->cfglock);
609 (void)rbtree_delete(&ctx->queries, q->node.key);
610 context_query_delete(q);
611 lock_basic_unlock(&ctx->cfglock);
616 ub_resolve_async(struct ub_ctx* ctx, const char* name, int rrtype,
617 int rrclass, void* mydata, ub_callback_t callback, int* async_id)
625 lock_basic_lock(&ctx->cfglock);
626 if(!ctx->finalized) {
627 int r = context_finalize(ctx);
629 lock_basic_unlock(&ctx->cfglock);
633 if(!ctx->created_bg) {
636 lock_basic_unlock(&ctx->cfglock);
637 r = libworker_bg(ctx);
639 lock_basic_lock(&ctx->cfglock);
641 lock_basic_unlock(&ctx->cfglock);
645 lock_basic_unlock(&ctx->cfglock);
648 /* create new ctx_query and attempt to add to the list */
649 q = context_new(ctx, name, rrtype, rrclass, callback, mydata);
653 /* write over pipe to background worker */
654 lock_basic_lock(&ctx->cfglock);
655 msg = context_serialize_new_query(q, &len);
657 (void)rbtree_delete(&ctx->queries, q->node.key);
659 context_query_delete(q);
660 lock_basic_unlock(&ctx->cfglock);
664 *async_id = q->querynum;
665 lock_basic_unlock(&ctx->cfglock);
667 lock_basic_lock(&ctx->qqpipe_lock);
668 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
669 lock_basic_unlock(&ctx->qqpipe_lock);
673 lock_basic_unlock(&ctx->qqpipe_lock);
679 ub_cancel(struct ub_ctx* ctx, int async_id)
684 lock_basic_lock(&ctx->cfglock);
685 q = (struct ctx_query*)rbtree_search(&ctx->queries, &async_id);
686 if(!q || !q->async) {
687 /* it is not there, so nothing to do */
688 lock_basic_unlock(&ctx->cfglock);
691 log_assert(q->async);
695 if(!ctx->dothread) { /* if forked */
696 (void)rbtree_delete(&ctx->queries, q->node.key);
698 msg = context_serialize_cancel(q, &len);
699 context_query_delete(q);
700 lock_basic_unlock(&ctx->cfglock);
704 /* send cancel to background worker */
705 lock_basic_lock(&ctx->qqpipe_lock);
706 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
707 lock_basic_unlock(&ctx->qqpipe_lock);
711 lock_basic_unlock(&ctx->qqpipe_lock);
714 lock_basic_unlock(&ctx->cfglock);
720 ub_resolve_free(struct ub_result* result)
725 if(result->canonname != result->qname)
726 free(result->canonname);
728 for(p = result->data; *p; p++)
732 free(result->answer_packet);
733 free(result->why_bogus);
741 case UB_NOERROR: return "no error";
742 case UB_SOCKET: return "socket io error";
743 case UB_NOMEM: return "out of memory";
744 case UB_SYNTAX: return "syntax error";
745 case UB_SERVFAIL: return "server failure";
746 case UB_FORKFAIL: return "could not fork";
747 case UB_INITFAIL: return "initialization failure";
748 case UB_AFTERFINAL: return "setting change after finalize";
749 case UB_PIPE: return "error in pipe communication with async";
750 case UB_READFILE: return "error reading file";
751 case UB_NOID: return "error async_id does not exist";
752 default: return "unknown error";
757 ub_ctx_set_fwd(struct ub_ctx* ctx, const char* addr)
759 struct sockaddr_storage storage;
761 struct config_stub* s;
763 lock_basic_lock(&ctx->cfglock);
765 lock_basic_unlock(&ctx->cfglock);
767 return UB_AFTERFINAL;
770 /* disable fwd mode - the root stub should be first. */
771 if(ctx->env->cfg->forwards &&
772 strcmp(ctx->env->cfg->forwards->name, ".") == 0) {
773 s = ctx->env->cfg->forwards;
774 ctx->env->cfg->forwards = s->next;
778 lock_basic_unlock(&ctx->cfglock);
781 lock_basic_unlock(&ctx->cfglock);
783 /* check syntax for addr */
784 if(!extstrtoaddr(addr, &storage, &stlen)) {
789 /* it parses, add root stub in front of list */
790 lock_basic_lock(&ctx->cfglock);
791 if(!ctx->env->cfg->forwards ||
792 strcmp(ctx->env->cfg->forwards->name, ".") != 0) {
793 s = calloc(1, sizeof(*s));
795 lock_basic_unlock(&ctx->cfglock);
799 s->name = strdup(".");
802 lock_basic_unlock(&ctx->cfglock);
806 s->next = ctx->env->cfg->forwards;
807 ctx->env->cfg->forwards = s;
809 log_assert(ctx->env->cfg->forwards);
810 s = ctx->env->cfg->forwards;
814 lock_basic_unlock(&ctx->cfglock);
818 if(!cfg_strlist_insert(&s->addrs, dupl)) {
820 lock_basic_unlock(&ctx->cfglock);
824 lock_basic_unlock(&ctx->cfglock);
829 ub_ctx_resolvconf(struct ub_ctx* ctx, const char* fname)
838 #if !defined(UB_ON_WINDOWS) || !defined(HAVE_WINDOWS_H)
839 fname = "/etc/resolv.conf";
842 ULONG buflen = sizeof(*info);
845 info = (FIXED_INFO *) malloc(sizeof (FIXED_INFO));
849 if (GetNetworkParams(info, &buflen) == ERROR_BUFFER_OVERFLOW) {
851 info = (FIXED_INFO *) malloc(buflen);
856 if (GetNetworkParams(info, &buflen) == NO_ERROR) {
858 ptr = &(info->DnsServerList);
861 if((retval=ub_ctx_set_fwd(ctx,
862 ptr->IpAddress.String)!=0)) {
877 in = fopen(fname, "r");
879 /* error in errno! perror(fname) */
882 while(fgets(buf, (int)sizeof(buf), in)) {
883 buf[sizeof(buf)-1] = 0;
885 while(*parse == ' ' || *parse == '\t')
887 if(strncmp(parse, "nameserver", 10) == 0) {
889 parse += 10; /* skip 'nameserver' */
890 /* skip whitespace */
891 while(*parse == ' ' || *parse == '\t')
894 /* skip [0-9a-fA-F.:]*, i.e. IP4 and IP6 address */
895 while(isxdigit(*parse) || *parse=='.' || *parse==':')
897 /* terminate after the address, remove newline */
900 if((r = ub_ctx_set_fwd(ctx, addr)) != UB_NOERROR) {
908 /* from resolv.conf(5) if none given, use localhost */
909 return ub_ctx_set_fwd(ctx, "127.0.0.1");
915 ub_ctx_hosts(struct ub_ctx* ctx, const char* fname)
918 char buf[1024], ldata[1024];
919 char* parse, *addr, *name, *ins;
920 lock_basic_lock(&ctx->cfglock);
922 lock_basic_unlock(&ctx->cfglock);
924 return UB_AFTERFINAL;
926 lock_basic_unlock(&ctx->cfglock);
928 #if defined(UB_ON_WINDOWS) && defined(HAVE_WINDOWS_H)
930 * If this is Windows NT/XP/2K it's in
931 * %WINDIR%\system32\drivers\etc\hosts.
932 * If this is Windows 95/98/Me it's in %WINDIR%\hosts.
934 name = getenv("WINDIR");
937 snprintf(buf, sizeof(buf), "%s%s", name,
938 "\\system32\\drivers\\etc\\hosts");
939 if((retval=ub_ctx_hosts(ctx, buf)) !=0 ) {
940 snprintf(buf, sizeof(buf), "%s%s", name,
942 retval=ub_ctx_hosts(ctx, buf);
949 fname = "/etc/hosts";
952 in = fopen(fname, "r");
954 /* error in errno! perror(fname) */
957 while(fgets(buf, (int)sizeof(buf), in)) {
958 buf[sizeof(buf)-1] = 0;
960 while(*parse == ' ' || *parse == '\t')
963 continue; /* skip comment */
964 /* format: <addr> spaces <name> spaces <name> ... */
967 while(isxdigit(*parse) || *parse == '.' || *parse == ':')
969 if(*parse == '\n' || *parse == 0)
972 continue; /* ignore macOSX fe80::1%lo0 localhost */
973 if(*parse != ' ' && *parse != '\t') {
974 /* must have whitespace after address */
979 *parse++ = 0; /* end delimiter for addr ... */
980 /* go to names and add them */
982 while(*parse == ' ' || *parse == '\t' || *parse=='\n')
984 if(*parse == 0 || *parse == '#')
986 /* skip name, allows (too) many printable characters */
988 while('!' <= *parse && *parse <= '~')
991 *parse++ = 0; /* end delimiter for name */
992 snprintf(ldata, sizeof(ldata), "%s %s %s",
993 name, str_is_ip6(addr)?"AAAA":"A", addr);
1001 lock_basic_lock(&ctx->cfglock);
1002 if(!cfg_strlist_insert(&ctx->env->cfg->local_data,
1004 lock_basic_unlock(&ctx->cfglock);
1010 lock_basic_unlock(&ctx->cfglock);
1017 /** finalize the context, if not already finalized */
1018 static int ub_ctx_finalize(struct ub_ctx* ctx)
1021 lock_basic_lock(&ctx->cfglock);
1022 if (!ctx->finalized) {
1023 res = context_finalize(ctx);
1025 lock_basic_unlock(&ctx->cfglock);
1029 /* Print local zones and RR data */
1030 int ub_ctx_print_local_zones(struct ub_ctx* ctx)
1032 int res = ub_ctx_finalize(ctx);
1033 if (res) return res;
1035 local_zones_print(ctx->local_zones);
1040 /* Add a new zone */
1041 int ub_ctx_zone_add(struct ub_ctx* ctx, const char *zone_name,
1042 const char *zone_type)
1044 enum localzone_type t;
1045 struct local_zone* z;
1050 int res = ub_ctx_finalize(ctx);
1051 if (res) return res;
1053 if(!local_zone_str2type(zone_type, &t)) {
1057 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1061 lock_quick_lock(&ctx->local_zones->lock);
1062 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1063 LDNS_RR_CLASS_IN))) {
1064 /* already present in tree */
1065 lock_rw_wrlock(&z->lock);
1066 z->type = t; /* update type anyway */
1067 lock_rw_unlock(&z->lock);
1068 lock_quick_unlock(&ctx->local_zones->lock);
1072 if(!local_zones_add_zone(ctx->local_zones, nm, nmlen, nmlabs,
1073 LDNS_RR_CLASS_IN, t)) {
1074 lock_quick_unlock(&ctx->local_zones->lock);
1077 lock_quick_unlock(&ctx->local_zones->lock);
1082 int ub_ctx_zone_remove(struct ub_ctx* ctx, const char *zone_name)
1084 struct local_zone* z;
1089 int res = ub_ctx_finalize(ctx);
1090 if (res) return res;
1092 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1096 lock_quick_lock(&ctx->local_zones->lock);
1097 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1098 LDNS_RR_CLASS_IN))) {
1099 /* present in tree */
1100 local_zones_del_zone(ctx->local_zones, z);
1102 lock_quick_unlock(&ctx->local_zones->lock);
1107 /* Add new RR data */
1108 int ub_ctx_data_add(struct ub_ctx* ctx, const char *data)
1111 int res = ub_ctx_finalize(ctx);
1112 if (res) return res;
1114 lock_basic_lock(&ctx->cfglock);
1115 buf = ldns_buffer_new(ctx->env->cfg->msg_buffer_size);
1116 lock_basic_unlock(&ctx->cfglock);
1117 if(!buf) return UB_NOMEM;
1119 res = local_zones_add_RR(ctx->local_zones, data, buf);
1121 ldns_buffer_free(buf);
1122 return (!res) ? UB_NOMEM : UB_NOERROR;
1125 /* Remove RR data */
1126 int ub_ctx_data_remove(struct ub_ctx* ctx, const char *data)
1131 int res = ub_ctx_finalize(ctx);
1132 if (res) return res;
1134 if(!parse_dname(data, &nm, &nmlen, &nmlabs))
1137 local_zones_del_data(ctx->local_zones, nm, nmlen, nmlabs,
1144 const char* ub_version(void)
1146 return PACKAGE_VERSION;