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"
64 #if defined(UB_ON_WINDOWS) && defined (HAVE_WINDOWS_H)
67 #endif /* UB_ON_WINDOWS */
79 log_init(NULL, 0, NULL); /* logs to stderr */
80 log_ident_set("libunbound");
82 if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0) {
83 log_err("could not init winsock. WSAStartup: %s",
88 verbosity = 0; /* errors only */
90 ctx = (struct ub_ctx*)calloc(1, sizeof(*ctx));
95 alloc_init(&ctx->superalloc, NULL, 0);
96 seed = (unsigned int)time(NULL) ^ (unsigned int)getpid();
97 if(!(ctx->seed_rnd = ub_initstate(seed, NULL))) {
99 ub_randfree(ctx->seed_rnd);
105 if((ctx->qq_pipe = tube_create()) == NULL) {
107 ub_randfree(ctx->seed_rnd);
112 if((ctx->rr_pipe = tube_create()) == NULL) {
114 tube_delete(ctx->qq_pipe);
115 ub_randfree(ctx->seed_rnd);
120 lock_basic_init(&ctx->qqpipe_lock);
121 lock_basic_init(&ctx->rrpipe_lock);
122 lock_basic_init(&ctx->cfglock);
123 ctx->env = (struct module_env*)calloc(1, sizeof(*ctx->env));
125 tube_delete(ctx->qq_pipe);
126 tube_delete(ctx->rr_pipe);
127 ub_randfree(ctx->seed_rnd);
132 ctx->env->cfg = config_create_forlib();
134 tube_delete(ctx->qq_pipe);
135 tube_delete(ctx->rr_pipe);
137 ub_randfree(ctx->seed_rnd);
142 ctx->env->alloc = &ctx->superalloc;
143 ctx->env->worker = NULL;
144 ctx->env->need_to_validate = 0;
145 modstack_init(&ctx->mods);
146 rbtree_init(&ctx->queries, &context_query_cmp);
152 delq(rbnode_t* n, void* ATTR_UNUSED(arg))
154 struct ctx_query* q = (struct ctx_query*)n;
155 context_query_delete(q);
159 ub_ctx_delete(struct ub_ctx* ctx)
161 struct alloc_cache* a, *na;
163 /* stop the bg thread */
164 lock_basic_lock(&ctx->cfglock);
165 if(ctx->created_bg) {
168 uint32_t cmd = UB_LIBCMD_QUIT;
169 lock_basic_unlock(&ctx->cfglock);
170 lock_basic_lock(&ctx->qqpipe_lock);
171 (void)tube_write_msg(ctx->qq_pipe, (uint8_t*)&cmd,
172 (uint32_t)sizeof(cmd), 0);
173 lock_basic_unlock(&ctx->qqpipe_lock);
174 lock_basic_lock(&ctx->rrpipe_lock);
175 while(tube_read_msg(ctx->rr_pipe, &msg, &len, 0)) {
176 /* discard all results except a quit confirm */
177 if(context_serial_getcmd(msg, len) == UB_LIBCMD_QUIT) {
183 lock_basic_unlock(&ctx->rrpipe_lock);
185 /* if bg worker is a thread, wait for it to exit, so that all
186 * resources are really gone. */
187 lock_basic_lock(&ctx->cfglock);
189 lock_basic_unlock(&ctx->cfglock);
190 ub_thread_join(ctx->bg_tid);
192 lock_basic_unlock(&ctx->cfglock);
196 lock_basic_unlock(&ctx->cfglock);
200 modstack_desetup(&ctx->mods, ctx->env);
204 a->super = &ctx->superalloc;
209 local_zones_delete(ctx->local_zones);
210 lock_basic_destroy(&ctx->qqpipe_lock);
211 lock_basic_destroy(&ctx->rrpipe_lock);
212 lock_basic_destroy(&ctx->cfglock);
213 tube_delete(ctx->qq_pipe);
214 tube_delete(ctx->rr_pipe);
216 slabhash_delete(ctx->env->msg_cache);
217 rrset_cache_delete(ctx->env->rrset_cache);
218 infra_delete(ctx->env->infra_cache);
219 config_delete(ctx->env->cfg);
222 ub_randfree(ctx->seed_rnd);
223 alloc_clear(&ctx->superalloc);
224 traverse_postorder(&ctx->queries, delq, NULL);
232 ub_ctx_set_option(struct ub_ctx* ctx, const char* opt, const char* val)
234 lock_basic_lock(&ctx->cfglock);
236 lock_basic_unlock(&ctx->cfglock);
237 return UB_AFTERFINAL;
239 if(!config_set_option(ctx->env->cfg, opt, val)) {
240 lock_basic_unlock(&ctx->cfglock);
243 lock_basic_unlock(&ctx->cfglock);
248 ub_ctx_get_option(struct ub_ctx* ctx, const char* opt, char** str)
251 lock_basic_lock(&ctx->cfglock);
252 r = config_get_option_collate(ctx->env->cfg, opt, str);
253 lock_basic_unlock(&ctx->cfglock);
254 if(r == 0) r = UB_NOERROR;
255 else if(r == 1) r = UB_SYNTAX;
256 else if(r == 2) r = UB_NOMEM;
261 ub_ctx_config(struct ub_ctx* ctx, const char* fname)
263 lock_basic_lock(&ctx->cfglock);
265 lock_basic_unlock(&ctx->cfglock);
266 return UB_AFTERFINAL;
268 if(!config_read(ctx->env->cfg, fname, NULL)) {
269 lock_basic_unlock(&ctx->cfglock);
272 lock_basic_unlock(&ctx->cfglock);
277 ub_ctx_add_ta(struct ub_ctx* ctx, const char* ta)
279 char* dup = strdup(ta);
280 if(!dup) return UB_NOMEM;
281 lock_basic_lock(&ctx->cfglock);
283 lock_basic_unlock(&ctx->cfglock);
285 return UB_AFTERFINAL;
287 if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_list, dup)) {
288 lock_basic_unlock(&ctx->cfglock);
292 lock_basic_unlock(&ctx->cfglock);
297 ub_ctx_add_ta_file(struct ub_ctx* ctx, const char* fname)
299 char* dup = strdup(fname);
300 if(!dup) return UB_NOMEM;
301 lock_basic_lock(&ctx->cfglock);
303 lock_basic_unlock(&ctx->cfglock);
305 return UB_AFTERFINAL;
307 if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_file_list, dup)) {
308 lock_basic_unlock(&ctx->cfglock);
312 lock_basic_unlock(&ctx->cfglock);
317 ub_ctx_trustedkeys(struct ub_ctx* ctx, const char* fname)
319 char* dup = strdup(fname);
320 if(!dup) return UB_NOMEM;
321 lock_basic_lock(&ctx->cfglock);
323 lock_basic_unlock(&ctx->cfglock);
325 return UB_AFTERFINAL;
327 if(!cfg_strlist_insert(&ctx->env->cfg->trusted_keys_file_list, dup)) {
328 lock_basic_unlock(&ctx->cfglock);
332 lock_basic_unlock(&ctx->cfglock);
337 ub_ctx_debuglevel(struct ub_ctx* ctx, int d)
339 lock_basic_lock(&ctx->cfglock);
341 ctx->env->cfg->verbosity = d;
342 lock_basic_unlock(&ctx->cfglock);
346 int ub_ctx_debugout(struct ub_ctx* ctx, void* out)
348 lock_basic_lock(&ctx->cfglock);
349 log_file((FILE*)out);
350 ctx->logfile_override = 1;
352 lock_basic_unlock(&ctx->cfglock);
357 ub_ctx_async(struct ub_ctx* ctx, int dothread)
359 #ifdef THREADS_DISABLED
360 if(dothread) /* cannot do threading */
363 lock_basic_lock(&ctx->cfglock);
365 lock_basic_unlock(&ctx->cfglock);
366 return UB_AFTERFINAL;
368 ctx->dothread = dothread;
369 lock_basic_unlock(&ctx->cfglock);
374 ub_poll(struct ub_ctx* ctx)
376 /* no need to hold lock while testing for readability. */
377 return tube_poll(ctx->rr_pipe);
381 ub_fd(struct ub_ctx* ctx)
383 return tube_read_fd(ctx->rr_pipe);
386 /** process answer from bg worker */
388 process_answer_detail(struct ub_ctx* ctx, uint8_t* msg, uint32_t len,
389 ub_callback_t* cb, void** cbarg, int* err,
390 struct ub_result** res)
393 if(context_serial_getcmd(msg, len) != UB_LIBCMD_ANSWER) {
394 log_err("error: bad data from bg worker %d",
395 (int)context_serial_getcmd(msg, len));
399 lock_basic_lock(&ctx->cfglock);
400 q = context_deserialize_answer(ctx, msg, len, err);
402 lock_basic_unlock(&ctx->cfglock);
403 /* probably simply the lookup that failed, i.e.
404 * response returned before cancel was sent out, so noerror */
407 log_assert(q->async);
409 /* grab cb while locked */
419 ub_resolve_free(q->res);
421 /* parse the message, extract rcode, fill result */
422 ldns_buffer* buf = ldns_buffer_new(q->msg_len);
423 struct regional* region = regional_create();
425 (*res)->rcode = LDNS_RCODE_SERVFAIL;
427 ldns_buffer_clear(buf);
428 ldns_buffer_write(buf, q->msg, q->msg_len);
429 ldns_buffer_flip(buf);
430 libworker_enter_result(*res, buf, region,
433 (*res)->answer_packet = q->msg;
434 (*res)->answer_len = (int)q->msg_len;
436 ldns_buffer_free(buf);
437 regional_destroy(region);
440 /* delete the q from list */
441 (void)rbtree_delete(&ctx->queries, q->node.key);
443 context_query_delete(q);
444 lock_basic_unlock(&ctx->cfglock);
447 ub_resolve_free(*res);
451 /** process answer from bg worker */
453 process_answer(struct ub_ctx* ctx, uint8_t* msg, uint32_t len)
458 struct ub_result* res;
461 r = process_answer_detail(ctx, msg, len, &cb, &cbarg, &err, &res);
463 /* no locks held while calling callback, so that library is
466 (*cb)(cbarg, err, res);
472 ub_process(struct ub_ctx* ctx)
479 lock_basic_lock(&ctx->rrpipe_lock);
480 r = tube_read_msg(ctx->rr_pipe, &msg, &len, 1);
481 lock_basic_unlock(&ctx->rrpipe_lock);
486 if(!process_answer(ctx, msg, len)) {
496 ub_wait(struct ub_ctx* ctx)
501 struct ub_result* res;
505 /* this is basically the same loop as _process(), but with changes.
506 * holds the rrpipe lock and waits with tube_wait */
508 lock_basic_lock(&ctx->rrpipe_lock);
509 lock_basic_lock(&ctx->cfglock);
510 if(ctx->num_async == 0) {
511 lock_basic_unlock(&ctx->cfglock);
512 lock_basic_unlock(&ctx->rrpipe_lock);
515 lock_basic_unlock(&ctx->cfglock);
517 /* keep rrpipe locked, while
518 * o waiting for pipe readable
520 * o possibly decrementing num_async
521 * do callback without lock
523 r = tube_wait(ctx->rr_pipe);
525 r = tube_read_msg(ctx->rr_pipe, &msg, &len, 1);
527 lock_basic_unlock(&ctx->rrpipe_lock);
531 lock_basic_unlock(&ctx->rrpipe_lock);
534 r = process_answer_detail(ctx, msg, len,
535 &cb, &cbarg, &err, &res);
536 lock_basic_unlock(&ctx->rrpipe_lock);
541 (*cb)(cbarg, err, res);
543 lock_basic_unlock(&ctx->rrpipe_lock);
550 ub_resolve(struct ub_ctx* ctx, const char* name, int rrtype,
551 int rrclass, struct ub_result** result)
557 lock_basic_lock(&ctx->cfglock);
558 if(!ctx->finalized) {
559 r = context_finalize(ctx);
561 lock_basic_unlock(&ctx->cfglock);
565 /* create new ctx_query and attempt to add to the list */
566 lock_basic_unlock(&ctx->cfglock);
567 q = context_new(ctx, name, rrtype, rrclass, NULL, NULL);
570 /* become a resolver thread for a bit */
572 r = libworker_fg(ctx, q);
574 lock_basic_lock(&ctx->cfglock);
575 (void)rbtree_delete(&ctx->queries, q->node.key);
576 context_query_delete(q);
577 lock_basic_unlock(&ctx->cfglock);
580 q->res->answer_packet = q->msg;
581 q->res->answer_len = (int)q->msg_len;
586 lock_basic_lock(&ctx->cfglock);
587 (void)rbtree_delete(&ctx->queries, q->node.key);
588 context_query_delete(q);
589 lock_basic_unlock(&ctx->cfglock);
594 ub_resolve_async(struct ub_ctx* ctx, const char* name, int rrtype,
595 int rrclass, void* mydata, ub_callback_t callback, int* async_id)
603 lock_basic_lock(&ctx->cfglock);
604 if(!ctx->finalized) {
605 int r = context_finalize(ctx);
607 lock_basic_unlock(&ctx->cfglock);
611 if(!ctx->created_bg) {
614 lock_basic_unlock(&ctx->cfglock);
615 r = libworker_bg(ctx);
617 lock_basic_lock(&ctx->cfglock);
619 lock_basic_unlock(&ctx->cfglock);
623 lock_basic_unlock(&ctx->cfglock);
626 /* create new ctx_query and attempt to add to the list */
627 q = context_new(ctx, name, rrtype, rrclass, callback, mydata);
631 /* write over pipe to background worker */
632 lock_basic_lock(&ctx->cfglock);
633 msg = context_serialize_new_query(q, &len);
635 (void)rbtree_delete(&ctx->queries, q->node.key);
637 context_query_delete(q);
638 lock_basic_unlock(&ctx->cfglock);
642 *async_id = q->querynum;
643 lock_basic_unlock(&ctx->cfglock);
645 lock_basic_lock(&ctx->qqpipe_lock);
646 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
647 lock_basic_unlock(&ctx->qqpipe_lock);
651 lock_basic_unlock(&ctx->qqpipe_lock);
657 ub_cancel(struct ub_ctx* ctx, int async_id)
662 lock_basic_lock(&ctx->cfglock);
663 q = (struct ctx_query*)rbtree_search(&ctx->queries, &async_id);
664 if(!q || !q->async) {
665 /* it is not there, so nothing to do */
666 lock_basic_unlock(&ctx->cfglock);
669 log_assert(q->async);
673 if(!ctx->dothread) { /* if forked */
674 (void)rbtree_delete(&ctx->queries, q->node.key);
676 msg = context_serialize_cancel(q, &len);
677 context_query_delete(q);
678 lock_basic_unlock(&ctx->cfglock);
682 /* send cancel to background worker */
683 lock_basic_lock(&ctx->qqpipe_lock);
684 if(!tube_write_msg(ctx->qq_pipe, msg, len, 0)) {
685 lock_basic_unlock(&ctx->qqpipe_lock);
689 lock_basic_unlock(&ctx->qqpipe_lock);
692 lock_basic_unlock(&ctx->cfglock);
698 ub_resolve_free(struct ub_result* result)
703 if(result->canonname != result->qname)
704 free(result->canonname);
706 for(p = result->data; *p; p++)
710 free(result->answer_packet);
711 free(result->why_bogus);
719 case UB_NOERROR: return "no error";
720 case UB_SOCKET: return "socket io error";
721 case UB_NOMEM: return "out of memory";
722 case UB_SYNTAX: return "syntax error";
723 case UB_SERVFAIL: return "server failure";
724 case UB_FORKFAIL: return "could not fork";
725 case UB_INITFAIL: return "initialization failure";
726 case UB_AFTERFINAL: return "setting change after finalize";
727 case UB_PIPE: return "error in pipe communication with async";
728 case UB_READFILE: return "error reading file";
729 case UB_NOID: return "error async_id does not exist";
730 default: return "unknown error";
735 ub_ctx_set_fwd(struct ub_ctx* ctx, const char* addr)
737 struct sockaddr_storage storage;
739 struct config_stub* s;
741 lock_basic_lock(&ctx->cfglock);
743 lock_basic_unlock(&ctx->cfglock);
745 return UB_AFTERFINAL;
748 /* disable fwd mode - the root stub should be first. */
749 if(ctx->env->cfg->forwards &&
750 strcmp(ctx->env->cfg->forwards->name, ".") == 0) {
751 s = ctx->env->cfg->forwards;
752 ctx->env->cfg->forwards = s->next;
756 lock_basic_unlock(&ctx->cfglock);
759 lock_basic_unlock(&ctx->cfglock);
761 /* check syntax for addr */
762 if(!extstrtoaddr(addr, &storage, &stlen)) {
767 /* it parses, add root stub in front of list */
768 lock_basic_lock(&ctx->cfglock);
769 if(!ctx->env->cfg->forwards ||
770 strcmp(ctx->env->cfg->forwards->name, ".") != 0) {
771 s = calloc(1, sizeof(*s));
773 lock_basic_unlock(&ctx->cfglock);
777 s->name = strdup(".");
780 lock_basic_unlock(&ctx->cfglock);
784 s->next = ctx->env->cfg->forwards;
785 ctx->env->cfg->forwards = s;
787 log_assert(ctx->env->cfg->forwards);
788 s = ctx->env->cfg->forwards;
792 lock_basic_unlock(&ctx->cfglock);
796 if(!cfg_strlist_insert(&s->addrs, dupl)) {
798 lock_basic_unlock(&ctx->cfglock);
802 lock_basic_unlock(&ctx->cfglock);
807 ub_ctx_resolvconf(struct ub_ctx* ctx, const char* fname)
816 #if !defined(UB_ON_WINDOWS) || !defined(HAVE_WINDOWS_H)
817 fname = "/etc/resolv.conf";
820 ULONG buflen = sizeof(*info);
823 info = (FIXED_INFO *) malloc(sizeof (FIXED_INFO));
827 if (GetNetworkParams(info, &buflen) == ERROR_BUFFER_OVERFLOW) {
829 info = (FIXED_INFO *) malloc(buflen);
834 if (GetNetworkParams(info, &buflen) == NO_ERROR) {
836 ptr = &(info->DnsServerList);
839 if((retval=ub_ctx_set_fwd(ctx,
840 ptr->IpAddress.String)!=0)) {
855 in = fopen(fname, "r");
857 /* error in errno! perror(fname) */
860 while(fgets(buf, (int)sizeof(buf), in)) {
861 buf[sizeof(buf)-1] = 0;
863 while(*parse == ' ' || *parse == '\t')
865 if(strncmp(parse, "nameserver", 10) == 0) {
867 parse += 10; /* skip 'nameserver' */
868 /* skip whitespace */
869 while(*parse == ' ' || *parse == '\t')
872 /* skip [0-9a-fA-F.:]*, i.e. IP4 and IP6 address */
873 while(isxdigit(*parse) || *parse=='.' || *parse==':')
875 /* terminate after the address, remove newline */
878 if((r = ub_ctx_set_fwd(ctx, addr)) != UB_NOERROR) {
886 /* from resolv.conf(5) if none given, use localhost */
887 return ub_ctx_set_fwd(ctx, "127.0.0.1");
893 ub_ctx_hosts(struct ub_ctx* ctx, const char* fname)
896 char buf[1024], ldata[1024];
897 char* parse, *addr, *name, *ins;
898 lock_basic_lock(&ctx->cfglock);
900 lock_basic_unlock(&ctx->cfglock);
902 return UB_AFTERFINAL;
904 lock_basic_unlock(&ctx->cfglock);
906 #if defined(UB_ON_WINDOWS) && defined(HAVE_WINDOWS_H)
908 * If this is Windows NT/XP/2K it's in
909 * %WINDIR%\system32\drivers\etc\hosts.
910 * If this is Windows 95/98/Me it's in %WINDIR%\hosts.
912 name = getenv("WINDIR");
915 snprintf(buf, sizeof(buf), "%s%s", name,
916 "\\system32\\drivers\\etc\\hosts");
917 if((retval=ub_ctx_hosts(ctx, buf)) !=0 ) {
918 snprintf(buf, sizeof(buf), "%s%s", name,
920 retval=ub_ctx_hosts(ctx, buf);
927 fname = "/etc/hosts";
930 in = fopen(fname, "r");
932 /* error in errno! perror(fname) */
935 while(fgets(buf, (int)sizeof(buf), in)) {
936 buf[sizeof(buf)-1] = 0;
938 while(*parse == ' ' || *parse == '\t')
941 continue; /* skip comment */
942 /* format: <addr> spaces <name> spaces <name> ... */
945 while(isxdigit(*parse) || *parse == '.' || *parse == ':')
947 if(*parse == '\n' || *parse == 0)
950 continue; /* ignore macOSX fe80::1%lo0 localhost */
951 if(*parse != ' ' && *parse != '\t') {
952 /* must have whitespace after address */
957 *parse++ = 0; /* end delimiter for addr ... */
958 /* go to names and add them */
960 while(*parse == ' ' || *parse == '\t' || *parse=='\n')
962 if(*parse == 0 || *parse == '#')
964 /* skip name, allows (too) many printable characters */
966 while('!' <= *parse && *parse <= '~')
969 *parse++ = 0; /* end delimiter for name */
970 snprintf(ldata, sizeof(ldata), "%s %s %s",
971 name, str_is_ip6(addr)?"AAAA":"A", addr);
979 lock_basic_lock(&ctx->cfglock);
980 if(!cfg_strlist_insert(&ctx->env->cfg->local_data,
982 lock_basic_unlock(&ctx->cfglock);
988 lock_basic_unlock(&ctx->cfglock);
995 /** finalize the context, if not already finalized */
996 static int ub_ctx_finalize(struct ub_ctx* ctx)
999 lock_basic_lock(&ctx->cfglock);
1000 if (!ctx->finalized) {
1001 res = context_finalize(ctx);
1003 lock_basic_unlock(&ctx->cfglock);
1007 /* Print local zones and RR data */
1008 int ub_ctx_print_local_zones(struct ub_ctx* ctx)
1010 int res = ub_ctx_finalize(ctx);
1011 if (res) return res;
1013 local_zones_print(ctx->local_zones);
1018 /* Add a new zone */
1019 int ub_ctx_zone_add(struct ub_ctx* ctx, char *zone_name, char *zone_type)
1021 enum localzone_type t;
1022 struct local_zone* z;
1027 int res = ub_ctx_finalize(ctx);
1028 if (res) return res;
1030 if(!local_zone_str2type(zone_type, &t)) {
1034 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1038 lock_quick_lock(&ctx->local_zones->lock);
1039 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1040 LDNS_RR_CLASS_IN))) {
1041 /* already present in tree */
1042 lock_rw_wrlock(&z->lock);
1043 z->type = t; /* update type anyway */
1044 lock_rw_unlock(&z->lock);
1045 lock_quick_unlock(&ctx->local_zones->lock);
1049 if(!local_zones_add_zone(ctx->local_zones, nm, nmlen, nmlabs,
1050 LDNS_RR_CLASS_IN, t)) {
1051 lock_quick_unlock(&ctx->local_zones->lock);
1054 lock_quick_unlock(&ctx->local_zones->lock);
1059 int ub_ctx_zone_remove(struct ub_ctx* ctx, char *zone_name)
1061 struct local_zone* z;
1066 int res = ub_ctx_finalize(ctx);
1067 if (res) return res;
1069 if(!parse_dname(zone_name, &nm, &nmlen, &nmlabs)) {
1073 lock_quick_lock(&ctx->local_zones->lock);
1074 if((z=local_zones_find(ctx->local_zones, nm, nmlen, nmlabs,
1075 LDNS_RR_CLASS_IN))) {
1076 /* present in tree */
1077 local_zones_del_zone(ctx->local_zones, z);
1079 lock_quick_unlock(&ctx->local_zones->lock);
1084 /* Add new RR data */
1085 int ub_ctx_data_add(struct ub_ctx* ctx, char *data)
1088 int res = ub_ctx_finalize(ctx);
1089 if (res) return res;
1091 lock_basic_lock(&ctx->cfglock);
1092 buf = ldns_buffer_new(ctx->env->cfg->msg_buffer_size);
1093 lock_basic_unlock(&ctx->cfglock);
1094 if(!buf) return UB_NOMEM;
1096 res = local_zones_add_RR(ctx->local_zones, data, buf);
1098 ldns_buffer_free(buf);
1099 return (!res) ? UB_NOMEM : UB_NOERROR;
1102 /* Remove RR data */
1103 int ub_ctx_data_remove(struct ub_ctx* ctx, char *data)
1108 int res = ub_ctx_finalize(ctx);
1109 if (res) return res;
1111 if(!parse_dname(data, &nm, &nmlen, &nmlabs))
1114 local_zones_del_data(ctx->local_zones, nm, nmlen, nmlabs,
1121 const char* ub_version(void)
1123 return PACKAGE_VERSION;