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
);
366 int ub_ctx_add_ta_autr(struct ub_ctx
* ctx
, const char* fname
)
368 char* dup
= strdup(fname
);
369 if(!dup
) return UB_NOMEM
;
370 lock_basic_lock(&ctx
->cfglock
);
372 lock_basic_unlock(&ctx
->cfglock
);
374 return UB_AFTERFINAL
;
376 if(!cfg_strlist_insert(&ctx
->env
->cfg
->auto_trust_anchor_file_list
,
378 lock_basic_unlock(&ctx
->cfglock
);
382 lock_basic_unlock(&ctx
->cfglock
);
387 ub_ctx_trustedkeys(struct ub_ctx
* ctx
, const char* fname
)
389 char* dup
= strdup(fname
);
390 if(!dup
) return UB_NOMEM
;
391 lock_basic_lock(&ctx
->cfglock
);
393 lock_basic_unlock(&ctx
->cfglock
);
395 return UB_AFTERFINAL
;
397 if(!cfg_strlist_insert(&ctx
->env
->cfg
->trusted_keys_file_list
, dup
)) {
398 lock_basic_unlock(&ctx
->cfglock
);
402 lock_basic_unlock(&ctx
->cfglock
);
407 ub_ctx_debuglevel(struct ub_ctx
* ctx
, int d
)
409 lock_basic_lock(&ctx
->cfglock
);
411 ctx
->env
->cfg
->verbosity
= d
;
412 lock_basic_unlock(&ctx
->cfglock
);
416 int ub_ctx_debugout(struct ub_ctx
* ctx
, void* out
)
418 lock_basic_lock(&ctx
->cfglock
);
419 log_file((FILE*)out
);
420 ctx
->logfile_override
= 1;
422 lock_basic_unlock(&ctx
->cfglock
);
427 ub_ctx_async(struct ub_ctx
* ctx
, int dothread
)
429 #ifdef THREADS_DISABLED
430 if(dothread
) /* cannot do threading */
433 lock_basic_lock(&ctx
->cfglock
);
435 lock_basic_unlock(&ctx
->cfglock
);
436 return UB_AFTERFINAL
;
438 ctx
->dothread
= dothread
;
439 lock_basic_unlock(&ctx
->cfglock
);
444 ub_poll(struct ub_ctx
* ctx
)
446 /* no need to hold lock while testing for readability. */
447 return tube_poll(ctx
->rr_pipe
);
451 ub_fd(struct ub_ctx
* ctx
)
453 return tube_read_fd(ctx
->rr_pipe
);
456 /** process answer from bg worker */
458 process_answer_detail(struct ub_ctx
* ctx
, uint8_t* msg
, uint32_t len
,
459 ub_callback_t
* cb
, void** cbarg
, int* err
,
460 struct ub_result
** res
)
463 if(context_serial_getcmd(msg
, len
) != UB_LIBCMD_ANSWER
) {
464 log_err("error: bad data from bg worker %d",
465 (int)context_serial_getcmd(msg
, len
));
469 lock_basic_lock(&ctx
->cfglock
);
470 q
= context_deserialize_answer(ctx
, msg
, len
, err
);
472 lock_basic_unlock(&ctx
->cfglock
);
473 /* probably simply the lookup that failed, i.e.
474 * response returned before cancel was sent out, so noerror */
477 log_assert(q
->async
);
479 /* grab cb while locked */
489 ub_resolve_free(q
->res
);
491 /* parse the message, extract rcode, fill result */
492 sldns_buffer
* buf
= sldns_buffer_new(q
->msg_len
);
493 struct regional
* region
= regional_create();
495 (*res
)->rcode
= LDNS_RCODE_SERVFAIL
;
497 sldns_buffer_clear(buf
);
498 sldns_buffer_write(buf
, q
->msg
, q
->msg_len
);
499 sldns_buffer_flip(buf
);
500 libworker_enter_result(*res
, buf
, region
,
503 (*res
)->answer_packet
= q
->msg
;
504 (*res
)->answer_len
= (int)q
->msg_len
;
506 sldns_buffer_free(buf
);
507 regional_destroy(region
);
510 /* delete the q from list */
511 (void)rbtree_delete(&ctx
->queries
, q
->node
.key
);
513 context_query_delete(q
);
514 lock_basic_unlock(&ctx
->cfglock
);
517 ub_resolve_free(*res
);
521 /** process answer from bg worker */
523 process_answer(struct ub_ctx
* ctx
, uint8_t* msg
, uint32_t len
)
528 struct ub_result
* res
;
531 r
= process_answer_detail(ctx
, msg
, len
, &cb
, &cbarg
, &err
, &res
);
533 /* no locks held while calling callback, so that library is
536 (*cb
)(cbarg
, err
, res
);
542 ub_process(struct ub_ctx
* ctx
)
549 lock_basic_lock(&ctx
->rrpipe_lock
);
550 r
= tube_read_msg(ctx
->rr_pipe
, &msg
, &len
, 1);
551 lock_basic_unlock(&ctx
->rrpipe_lock
);
556 if(!process_answer(ctx
, msg
, len
)) {
566 ub_wait(struct ub_ctx
* ctx
)
571 struct ub_result
* res
;
575 /* this is basically the same loop as _process(), but with changes.
576 * holds the rrpipe lock and waits with tube_wait */
578 lock_basic_lock(&ctx
->rrpipe_lock
);
579 lock_basic_lock(&ctx
->cfglock
);
580 if(ctx
->num_async
== 0) {
581 lock_basic_unlock(&ctx
->cfglock
);
582 lock_basic_unlock(&ctx
->rrpipe_lock
);
585 lock_basic_unlock(&ctx
->cfglock
);
587 /* keep rrpipe locked, while
588 * o waiting for pipe readable
590 * o possibly decrementing num_async
591 * do callback without lock
593 r
= tube_wait(ctx
->rr_pipe
);
595 r
= tube_read_msg(ctx
->rr_pipe
, &msg
, &len
, 1);
597 lock_basic_unlock(&ctx
->rrpipe_lock
);
601 lock_basic_unlock(&ctx
->rrpipe_lock
);
604 r
= process_answer_detail(ctx
, msg
, len
,
605 &cb
, &cbarg
, &err
, &res
);
606 lock_basic_unlock(&ctx
->rrpipe_lock
);
611 (*cb
)(cbarg
, err
, res
);
613 lock_basic_unlock(&ctx
->rrpipe_lock
);
620 ub_resolve(struct ub_ctx
* ctx
, const char* name
, int rrtype
,
621 int rrclass
, struct ub_result
** result
)
627 lock_basic_lock(&ctx
->cfglock
);
628 if(!ctx
->finalized
) {
629 r
= context_finalize(ctx
);
631 lock_basic_unlock(&ctx
->cfglock
);
635 /* create new ctx_query and attempt to add to the list */
636 lock_basic_unlock(&ctx
->cfglock
);
637 q
= context_new(ctx
, name
, rrtype
, rrclass
, NULL
, NULL
);
640 /* become a resolver thread for a bit */
642 r
= libworker_fg(ctx
, q
);
644 lock_basic_lock(&ctx
->cfglock
);
645 (void)rbtree_delete(&ctx
->queries
, q
->node
.key
);
646 context_query_delete(q
);
647 lock_basic_unlock(&ctx
->cfglock
);
650 q
->res
->answer_packet
= q
->msg
;
651 q
->res
->answer_len
= (int)q
->msg_len
;
656 lock_basic_lock(&ctx
->cfglock
);
657 (void)rbtree_delete(&ctx
->queries
, q
->node
.key
);
658 context_query_delete(q
);
659 lock_basic_unlock(&ctx
->cfglock
);
664 ub_resolve_event(struct ub_ctx
* ctx
, const char* name
, int rrtype
,
665 int rrclass
, void* mydata
, ub_event_callback_t callback
, int* async_id
)
672 lock_basic_lock(&ctx
->cfglock
);
673 if(!ctx
->finalized
) {
674 int r
= context_finalize(ctx
);
676 lock_basic_unlock(&ctx
->cfglock
);
680 lock_basic_unlock(&ctx
->cfglock
);
681 if(!ctx
->event_worker
) {
682 ctx
->event_worker
= libworker_create_event(ctx
,
684 if(!ctx
->event_worker
) {
689 /* create new ctx_query and attempt to add to the list */
690 q
= context_new(ctx
, name
, rrtype
, rrclass
, (ub_callback_t
)callback
,
696 if((r
=libworker_attach_mesh(ctx
, q
, async_id
)) != 0)
703 ub_resolve_async(struct ub_ctx
* ctx
, const char* name
, int rrtype
,
704 int rrclass
, void* mydata
, ub_callback_t callback
, int* async_id
)
712 lock_basic_lock(&ctx
->cfglock
);
713 if(!ctx
->finalized
) {
714 int r
= context_finalize(ctx
);
716 lock_basic_unlock(&ctx
->cfglock
);
720 if(!ctx
->created_bg
) {
723 lock_basic_unlock(&ctx
->cfglock
);
724 r
= libworker_bg(ctx
);
726 lock_basic_lock(&ctx
->cfglock
);
728 lock_basic_unlock(&ctx
->cfglock
);
732 lock_basic_unlock(&ctx
->cfglock
);
735 /* create new ctx_query and attempt to add to the list */
736 q
= context_new(ctx
, name
, rrtype
, rrclass
, callback
, mydata
);
740 /* write over pipe to background worker */
741 lock_basic_lock(&ctx
->cfglock
);
742 msg
= context_serialize_new_query(q
, &len
);
744 (void)rbtree_delete(&ctx
->queries
, q
->node
.key
);
746 context_query_delete(q
);
747 lock_basic_unlock(&ctx
->cfglock
);
751 *async_id
= q
->querynum
;
752 lock_basic_unlock(&ctx
->cfglock
);
754 lock_basic_lock(&ctx
->qqpipe_lock
);
755 if(!tube_write_msg(ctx
->qq_pipe
, msg
, len
, 0)) {
756 lock_basic_unlock(&ctx
->qqpipe_lock
);
760 lock_basic_unlock(&ctx
->qqpipe_lock
);
766 ub_cancel(struct ub_ctx
* ctx
, int async_id
)
771 lock_basic_lock(&ctx
->cfglock
);
772 q
= (struct ctx_query
*)rbtree_search(&ctx
->queries
, &async_id
);
773 if(!q
|| !q
->async
) {
774 /* it is not there, so nothing to do */
775 lock_basic_unlock(&ctx
->cfglock
);
778 log_assert(q
->async
);
782 if(!ctx
->dothread
) { /* if forked */
783 (void)rbtree_delete(&ctx
->queries
, q
->node
.key
);
785 msg
= context_serialize_cancel(q
, &len
);
786 context_query_delete(q
);
787 lock_basic_unlock(&ctx
->cfglock
);
791 /* send cancel to background worker */
792 lock_basic_lock(&ctx
->qqpipe_lock
);
793 if(!tube_write_msg(ctx
->qq_pipe
, msg
, len
, 0)) {
794 lock_basic_unlock(&ctx
->qqpipe_lock
);
798 lock_basic_unlock(&ctx
->qqpipe_lock
);
801 lock_basic_unlock(&ctx
->cfglock
);
807 ub_resolve_free(struct ub_result
* result
)
812 if(result
->canonname
!= result
->qname
)
813 free(result
->canonname
);
815 for(p
= result
->data
; *p
; p
++)
819 free(result
->answer_packet
);
820 free(result
->why_bogus
);
828 case UB_NOERROR
: return "no error";
829 case UB_SOCKET
: return "socket io error";
830 case UB_NOMEM
: return "out of memory";
831 case UB_SYNTAX
: return "syntax error";
832 case UB_SERVFAIL
: return "server failure";
833 case UB_FORKFAIL
: return "could not fork";
834 case UB_INITFAIL
: return "initialization failure";
835 case UB_AFTERFINAL
: return "setting change after finalize";
836 case UB_PIPE
: return "error in pipe communication with async";
837 case UB_READFILE
: return "error reading file";
838 case UB_NOID
: return "error async_id does not exist";
839 default: return "unknown error";
844 ub_ctx_set_fwd(struct ub_ctx
* ctx
, const char* addr
)
846 struct sockaddr_storage storage
;
848 struct config_stub
* s
;
850 lock_basic_lock(&ctx
->cfglock
);
852 lock_basic_unlock(&ctx
->cfglock
);
854 return UB_AFTERFINAL
;
857 /* disable fwd mode - the root stub should be first. */
858 if(ctx
->env
->cfg
->forwards
&&
859 strcmp(ctx
->env
->cfg
->forwards
->name
, ".") == 0) {
860 s
= ctx
->env
->cfg
->forwards
;
861 ctx
->env
->cfg
->forwards
= s
->next
;
865 lock_basic_unlock(&ctx
->cfglock
);
868 lock_basic_unlock(&ctx
->cfglock
);
870 /* check syntax for addr */
871 if(!extstrtoaddr(addr
, &storage
, &stlen
)) {
876 /* it parses, add root stub in front of list */
877 lock_basic_lock(&ctx
->cfglock
);
878 if(!ctx
->env
->cfg
->forwards
||
879 strcmp(ctx
->env
->cfg
->forwards
->name
, ".") != 0) {
880 s
= calloc(1, sizeof(*s
));
882 lock_basic_unlock(&ctx
->cfglock
);
886 s
->name
= strdup(".");
889 lock_basic_unlock(&ctx
->cfglock
);
893 s
->next
= ctx
->env
->cfg
->forwards
;
894 ctx
->env
->cfg
->forwards
= s
;
896 log_assert(ctx
->env
->cfg
->forwards
);
897 s
= ctx
->env
->cfg
->forwards
;
901 lock_basic_unlock(&ctx
->cfglock
);
905 if(!cfg_strlist_insert(&s
->addrs
, dupl
)) {
907 lock_basic_unlock(&ctx
->cfglock
);
911 lock_basic_unlock(&ctx
->cfglock
);
916 ub_ctx_resolvconf(struct ub_ctx
* ctx
, const char* fname
)
925 #if !defined(UB_ON_WINDOWS) || !defined(HAVE_WINDOWS_H)
926 fname
= "/etc/resolv.conf";
929 ULONG buflen
= sizeof(*info
);
932 info
= (FIXED_INFO
*) malloc(sizeof (FIXED_INFO
));
936 if (GetNetworkParams(info
, &buflen
) == ERROR_BUFFER_OVERFLOW
) {
938 info
= (FIXED_INFO
*) malloc(buflen
);
943 if (GetNetworkParams(info
, &buflen
) == NO_ERROR
) {
945 ptr
= &(info
->DnsServerList
);
948 if((retval
=ub_ctx_set_fwd(ctx
,
949 ptr
->IpAddress
.String
)!=0)) {
964 in
= fopen(fname
, "r");
966 /* error in errno! perror(fname) */
969 while(fgets(buf
, (int)sizeof(buf
), in
)) {
970 buf
[sizeof(buf
)-1] = 0;
972 while(*parse
== ' ' || *parse
== '\t')
974 if(strncmp(parse
, "nameserver", 10) == 0) {
976 parse
+= 10; /* skip 'nameserver' */
977 /* skip whitespace */
978 while(*parse
== ' ' || *parse
== '\t')
981 /* skip [0-9a-fA-F.:]*, i.e. IP4 and IP6 address */
982 while(isxdigit((unsigned char)*parse
) || *parse
=='.' || *parse
==':')
984 /* terminate after the address, remove newline */
987 if((r
= ub_ctx_set_fwd(ctx
, addr
)) != UB_NOERROR
) {
995 /* from resolv.conf(5) if none given, use localhost */
996 return ub_ctx_set_fwd(ctx
, "127.0.0.1");
1002 ub_ctx_hosts(struct ub_ctx
* ctx
, const char* fname
)
1005 char buf
[1024], ldata
[1024];
1006 char* parse
, *addr
, *name
, *ins
;
1007 lock_basic_lock(&ctx
->cfglock
);
1008 if(ctx
->finalized
) {
1009 lock_basic_unlock(&ctx
->cfglock
);
1011 return UB_AFTERFINAL
;
1013 lock_basic_unlock(&ctx
->cfglock
);
1015 #if defined(UB_ON_WINDOWS) && defined(HAVE_WINDOWS_H)
1017 * If this is Windows NT/XP/2K it's in
1018 * %WINDIR%\system32\drivers\etc\hosts.
1019 * If this is Windows 95/98/Me it's in %WINDIR%\hosts.
1021 name
= getenv("WINDIR");
1024 snprintf(buf
, sizeof(buf
), "%s%s", name
,
1025 "\\system32\\drivers\\etc\\hosts");
1026 if((retval
=ub_ctx_hosts(ctx
, buf
)) !=0 ) {
1027 snprintf(buf
, sizeof(buf
), "%s%s", name
,
1029 retval
=ub_ctx_hosts(ctx
, buf
);
1036 fname
= "/etc/hosts";
1039 in
= fopen(fname
, "r");
1041 /* error in errno! perror(fname) */
1044 while(fgets(buf
, (int)sizeof(buf
), in
)) {
1045 buf
[sizeof(buf
)-1] = 0;
1047 while(*parse
== ' ' || *parse
== '\t')
1050 continue; /* skip comment */
1051 /* format: <addr> spaces <name> spaces <name> ... */
1054 while(isxdigit((unsigned char)*parse
) || *parse
== '.' || *parse
== ':')
1056 if(*parse
== '\n' || *parse
== 0)
1059 continue; /* ignore macOSX fe80::1%lo0 localhost */
1060 if(*parse
!= ' ' && *parse
!= '\t') {
1061 /* must have whitespace after address */
1066 *parse
++ = 0; /* end delimiter for addr ... */
1067 /* go to names and add them */
1069 while(*parse
== ' ' || *parse
== '\t' || *parse
=='\n')
1071 if(*parse
== 0 || *parse
== '#')
1073 /* skip name, allows (too) many printable characters */
1075 while('!' <= *parse
&& *parse
<= '~')
1078 *parse
++ = 0; /* end delimiter for name */
1079 snprintf(ldata
, sizeof(ldata
), "%s %s %s",
1080 name
, str_is_ip6(addr
)?"AAAA":"A", addr
);
1081 ins
= strdup(ldata
);
1088 lock_basic_lock(&ctx
->cfglock
);
1089 if(!cfg_strlist_insert(&ctx
->env
->cfg
->local_data
,
1091 lock_basic_unlock(&ctx
->cfglock
);
1097 lock_basic_unlock(&ctx
->cfglock
);
1104 /** finalize the context, if not already finalized */
1105 static int ub_ctx_finalize(struct ub_ctx
* ctx
)
1108 lock_basic_lock(&ctx
->cfglock
);
1109 if (!ctx
->finalized
) {
1110 res
= context_finalize(ctx
);
1112 lock_basic_unlock(&ctx
->cfglock
);
1116 /* Print local zones and RR data */
1117 int ub_ctx_print_local_zones(struct ub_ctx
* ctx
)
1119 int res
= ub_ctx_finalize(ctx
);
1120 if (res
) return res
;
1122 local_zones_print(ctx
->local_zones
);
1127 /* Add a new zone */
1128 int ub_ctx_zone_add(struct ub_ctx
* ctx
, const char *zone_name
,
1129 const char *zone_type
)
1131 enum localzone_type t
;
1132 struct local_zone
* z
;
1137 int res
= ub_ctx_finalize(ctx
);
1138 if (res
) return res
;
1140 if(!local_zone_str2type(zone_type
, &t
)) {
1144 if(!parse_dname(zone_name
, &nm
, &nmlen
, &nmlabs
)) {
1148 lock_rw_wrlock(&ctx
->local_zones
->lock
);
1149 if((z
=local_zones_find(ctx
->local_zones
, nm
, nmlen
, nmlabs
,
1150 LDNS_RR_CLASS_IN
))) {
1151 /* already present in tree */
1152 lock_rw_wrlock(&z
->lock
);
1153 z
->type
= t
; /* update type anyway */
1154 lock_rw_unlock(&z
->lock
);
1155 lock_rw_unlock(&ctx
->local_zones
->lock
);
1159 if(!local_zones_add_zone(ctx
->local_zones
, nm
, nmlen
, nmlabs
,
1160 LDNS_RR_CLASS_IN
, t
)) {
1161 lock_rw_unlock(&ctx
->local_zones
->lock
);
1164 lock_rw_unlock(&ctx
->local_zones
->lock
);
1169 int ub_ctx_zone_remove(struct ub_ctx
* ctx
, const char *zone_name
)
1171 struct local_zone
* z
;
1176 int res
= ub_ctx_finalize(ctx
);
1177 if (res
) return res
;
1179 if(!parse_dname(zone_name
, &nm
, &nmlen
, &nmlabs
)) {
1183 lock_rw_wrlock(&ctx
->local_zones
->lock
);
1184 if((z
=local_zones_find(ctx
->local_zones
, nm
, nmlen
, nmlabs
,
1185 LDNS_RR_CLASS_IN
))) {
1186 /* present in tree */
1187 local_zones_del_zone(ctx
->local_zones
, z
);
1189 lock_rw_unlock(&ctx
->local_zones
->lock
);
1194 /* Add new RR data */
1195 int ub_ctx_data_add(struct ub_ctx
* ctx
, const char *data
)
1197 int res
= ub_ctx_finalize(ctx
);
1198 if (res
) return res
;
1200 res
= local_zones_add_RR(ctx
->local_zones
, data
);
1201 return (!res
) ? UB_NOMEM
: UB_NOERROR
;
1204 /* Remove RR data */
1205 int ub_ctx_data_remove(struct ub_ctx
* ctx
, const char *data
)
1210 int res
= ub_ctx_finalize(ctx
);
1211 if (res
) return res
;
1213 if(!parse_dname(data
, &nm
, &nmlen
, &nmlabs
))
1216 local_zones_del_data(ctx
->local_zones
, nm
, nmlen
, nmlabs
,
1223 const char* ub_version(void)
1225 return PACKAGE_VERSION
;
1229 ub_ctx_set_event(struct ub_ctx
* ctx
, struct event_base
* base
) {
1230 if (!ctx
|| !ctx
->event_base
|| !base
) {
1233 if (ctx
->event_base
== base
) {
1238 lock_basic_lock(&ctx
->cfglock
);
1239 /* destroy the current worker - safe to pass in NULL */
1240 libworker_delete_event(ctx
->event_worker
);
1241 ctx
->event_worker
= NULL
;
1242 ctx
->event_base
= base
;
1243 ctx
->created_bg
= 0;
1245 lock_basic_unlock(&ctx
->cfglock
);