]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
test app, and hopefully useful tool.
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Mon, 3 Dec 2007 11:02:31 +0000 (11:02 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Mon, 3 Dec 2007 11:02:31 +0000 (11:02 +0000)
git-svn-id: file:///svn/unbound/trunk@795 be551aaa-1e26-0410-a405-d3ace91eadb9

Makefile.in
doc/Changelog
libunbound/unbound.c
libunbound/unbound.h
smallapp/unbound-host.c [new file with mode: 0644]

index 6a3775382d627a7ba3a46697e4bfb668c79d4a50..ddd8a2e4465518cafd0d92091e438e1ac6240df5 100644 (file)
@@ -70,6 +70,8 @@ DAEMON_SRC=$(patsubst $(srcdir)/%,%, $(wildcard $(srcdir)/daemon/*.c)) \
 DAEMON_OBJ=$(addprefix $(BUILD),$(DAEMON_SRC:.c=.o)) $(COMPAT_OBJ)
 CHECKCONF_SRC=smallapp/unbound-checkconf.c smallapp/worker_cb.c $(COMMON_SRC)
 CHECKCONF_OBJ=$(addprefix $(BUILD),$(CHECKCONF_SRC:.c=.o)) $(COMPAT_OBJ)
+HOST_SRC=smallapp/unbound-host.c
+HOST_OBJ=$(addprefix $(BUILD),$(HOST_SRC:.c=.o)) $(COMPAT_OBJ)
 TESTBOUND_SRC=testcode/testbound.c testcode/ldns-testpkts.c \
        daemon/worker.c daemon/acl_list.c daemon/daemon.c daemon/stats.c \
        testcode/replay.c testcode/fake_event.c $(filter-out util/netevent.c \
@@ -85,7 +87,7 @@ SIGNIT_OBJ=$(addprefix $(BUILD),$(SIGNIT_SRC:.c=.o)) $(COMPAT_OBJ)
 MEMSTATS_SRC=testcode/memstats.c smallapp/worker_cb.c $(COMMON_SRC)
 MEMSTATS_OBJ=$(addprefix $(BUILD),$(MEMSTATS_SRC:.c=.o)) $(COMPAT_OBJ)
 LIBUNBOUND_SRC=$(patsubst $(srcdir)/%,%, \
-       $(wildcard $(srcdir)/libunbound/*.c) $(COMMON_SRC))
+       $(wildcard $(srcdir)/libunbound/*.c) smallapp/worker_cb.c $(COMMON_SRC))
 LIBUNBOUND_OBJ=$(addprefix $(BUILD),$(LIBUNBOUND_SRC:.c=.o)) $(COMPAT_OBJ)
 ALL_SRC=$(COMMON_SRC) $(UNITTEST_SRC) $(DAEMON_SRC) \
        $(TESTBOUND_SRC) $(LOCKVERIFY_SRC) $(PKTVIEW_SRC) $(SIGNIT_SRC) \
@@ -106,7 +108,7 @@ $(BUILD)%.o:    $(srcdir)/%.c
        @if test ! -d $(dir $@); then $(INSTALL) -d $(patsubst %/,%,$(dir $@)); fi
        $Q$(COMPILE) -c $< -o $@
 
-all:   $(COMMON_OBJ) unbound unbound-checkconf lib
+all:   $(COMMON_OBJ) unbound unbound-checkconf lib unbound-host
 
 tests: all unittest testbound lock-verify pktview signit memstats
 
@@ -136,6 +138,10 @@ unbound-checkconf: $(CHECKCONF_OBJ) $(ldnslib)
        $(INFO) Link $@
        $Q$(LINK) -o $@ $(sort $(CHECKCONF_OBJ)) $(LIBS)
 
+unbound-host:  $(HOST_OBJ) lib
+       $(INFO) Link $@
+       $Q$(LINK) -o $@ $(sort $(HOST_OBJ)) -L. -L.libs -lunbound $(LIBS)
+
 unittest:      $(UNITTEST_OBJ) $(ldnslib)
        $(INFO) Link $@
        $Q$(LINK) -o $@ $(sort $(UNITTEST_OBJ)) $(LIBS)
@@ -218,18 +224,22 @@ install:
        $(INSTALL) -m 755 -d $(mandir)/man8
        $(INSTALL) -m 755 -d $(mandir)/man5
        $(INSTALL) -m 755 -d $(libdir)
+       $(INSTALL) -m 755 -d $(includedir)
        $(LIBTOOL) --mode=install cp unbound $(bindir)/unbound
        $(LIBTOOL) --mode=install cp unbound-checkconf $(bindir)/unbound-checkconf
+       $(LIBTOOL) --mode=install cp unbound-host $(bindir)/unbound-host
        $(INSTALL) -c -m 644 $(srcdir)/doc/unbound.8 $(mandir)/man8
        $(INSTALL) -c -m 644 $(srcdir)/doc/unbound-checkconf.8 $(mandir)/man8
        $(INSTALL) -c -m 644 $(srcdir)/doc/unbound.conf.5 $(mandir)/man5
        if test ! -e $(configfile); then $(INSTALL) -d `dirname $(configfile)`; $(INSTALL) -c -m 644 $(srcdir)/doc/example.conf $(configfile); fi
+       $(LIBTOOL) --mode=install cp $(srcdir)/libunbound/unbound.h $(includedir)/unbound.h
        $(LIBTOOL) --mode=install cp libunbound.la $(libdir)
        $(LIBTOOL) --mode=finish $(libdir)
 
 uninstall:
-       rm -f -- $(bindir)/unbound $(bindir)/unbound-checkconf
+       rm -f -- $(bindir)/unbound $(bindir)/unbound-checkconf $(bindir)/unbound-host
        rm -f -- $(mandir)/man8/unbound.8 $(mandir)/man8/unbound-checkconf.8 $(mandir)/man5/unbound.conf.5
+       rm -f -- $(includedir)/unbound.h
        $(LIBTOOL) --mode=uninstall rm -f $(libdir)/libunbound.la
        @echo
        @echo "You still need to remove `dirname $(configfile)` , $(configfile) by hand"
index 4e2dd4e9dab2024a9b3e6f2f10f949928b5fdd38..b5c7dc2584a9c49f428000b9dd470ed81bca05fa 100644 (file)
@@ -4,6 +4,7 @@
        - install dirs created with -m 755 because they need to be accessible.
        - library extensive featurelist added to TODO.
        - please doxygen, lint.
+       - library test application, with basic functionality.
 
 30 November 2007: Wouter
        - makefile that creates libunbound.la, basic file or libunbound.a
index 4039f36053fa61333c30c3f99b5ecb9be07478e1..cdae6ec45bae7cbddac5737108c7c5b62a0ff03b 100644 (file)
@@ -109,6 +109,7 @@ ub_val_ctx_create()
                errno = ENOMEM;
                return NULL;
        }
+       checklock_start();
        if(socketpair(AF_UNIX, SOCK_STREAM, 0, ctx->qqpipe) == -1) {
                free(ctx);
                return NULL;
@@ -144,6 +145,8 @@ ub_val_ctx_delete(struct ub_val_ctx* ctx)
        close(ctx->rrpipe[1]);
        config_delete(ctx->cfg);
        free(ctx);
+       checklock_stop(); /* assumes during checklock tests libunbound users
+               only create one context */
 }
 
 int 
@@ -236,7 +239,7 @@ ub_val_ctx_fd(struct ub_val_ctx* ctx)
 int 
 ub_val_ctx_process(struct ub_val_ctx* ctx)
 {
-       return UB_NOERROR;
+       return UB_NOMEM;
 }
 
 int 
@@ -245,20 +248,20 @@ ub_val_resolve(struct ub_val_ctx* ctx, char* name, int rrtype,
 {
        /* become a resolver thread for a bit */
 
-       return UB_NOERROR;
+       return UB_NOMEM;
 }
 
 int 
 ub_val_resolve_async(struct ub_val_ctx* ctx, char* name, int rrtype, 
        int rrclass, void* mydata, ub_val_callback_t callback, int* async_id)
 {
-       return UB_NOERROR;
+       return UB_NOMEM;
 }
 
 int 
 ub_val_cancel(struct ub_val_ctx* ctx, int async_id)
 {
-       return UB_NOERROR;
+       return UB_NOMEM;
 }
 
 void 
index 21be5a574de708776b72085d73d1d90744b5bc33..4a6d5b6c686b25c46b594cdc059928d3ce6cc0f5 100644 (file)
@@ -133,6 +133,12 @@ struct ub_val_result {
         */
        char* canonname;
 
+       /**
+        * DNS RCODE for the result. May contain additional error code if
+        * there was no data due to an error. 0 (NOERROR) if okay.
+        */
+       int rcode;
+
        /** 
         * If there was no data, and the domain did not exist, this is true.
         * If it is false, and there was no data, then the domain name 
diff --git a/smallapp/unbound-host.c b/smallapp/unbound-host.c
new file mode 100644 (file)
index 0000000..9780314
--- /dev/null
@@ -0,0 +1,320 @@
+/*
+ * checkconf/unbound-host.c - replacement for host that supports validation.
+ *
+ * Copyright (c) 2007, NLnet Labs. All rights reserved.
+ *
+ * This software is open source.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 
+ * Neither the name of the NLNET LABS nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * \file
+ *
+ * This file performs functionality like 'host', and also supports validation.
+ * It uses the libunbound library.
+ */
+
+#include "config.h"
+#include "libunbound/unbound.h"
+#include <ldns/ldns.h>
+
+/** Give unbound-host usage, and exit (1). */
+static void
+usage()
+{
+       printf("Usage:  unbound-host [-c class] [-t type] hostname\n");
+       printf("  Queries the DNS for information.\n");
+       printf("  The hostname is looked up for IP4, IP6 and mail.\n");
+       printf("  If an ip-address is given a reverse lookup is done.\n");
+       printf("-t type         what type to look for.\n");
+       printf("-c class        what class to look for, if not class IN.\n");
+       printf("-h              show this usage help.\n");
+       printf("Version %s\n", PACKAGE_VERSION);
+       printf("BSD licensed, see LICENSE in source package for details.\n");
+       printf("Report bugs to %s\n", PACKAGE_BUGREPORT);
+       exit(1);
+}
+
+/** determine if str is ip4 */
+static int
+isip4(const char* nm, char** res)
+{
+       struct in_addr addr;
+       /* ddd.ddd.ddd.ddd.in-addr.arpa. is less than 32 */
+       char buf[32];
+       if(inet_pton(AF_INET, nm, &addr) <= 0) {
+               return 0;
+       }
+       snprintf(buf, sizeof(buf), "%u.%u.%u.%u.in-addr.arpa.",
+               ((uint8_t*)&addr)[3], ((uint8_t*)&addr)[2],
+               ((uint8_t*)&addr)[1], ((uint8_t*)&addr)[0]);
+       *res = strdup(buf);
+       return 1;
+}
+
+/** determine if str is ip6 */
+static int
+isip6(const char* nm, char** res)
+{
+       struct in6_addr addr;
+       /* [nibble.]{32}.ip6.arpa. is less than 128 */
+       char* hex = "0123456789abcdef";
+       char buf[128];
+       char *p;
+       int i;
+       if(inet_pton(AF_INET6, nm, &addr) <= 0) {
+               return 0;
+       }
+       p = buf;
+       for(i=15; i>=0; i--) {
+               uint8_t b = ((uint8_t*)&addr)[i];
+               *p++ = hex[ (b&0x0f) ];
+               *p++ = '.';
+               *p++ = hex[ (b&0xf0) >> 4 ];
+               *p++ = '.';
+       }
+       snprintf(buf+16*4, sizeof(buf)-16*4, "ip6.arpa.");
+       *res = strdup(buf);
+       return 1;
+}
+
+/** massage input name */
+static char*
+massage_qname(const char* nm, int* reverse)
+{
+       /* recognise IP4 and IP6, create reverse addresses if needed */
+       char* res;
+       if(isip4(nm, &res)) {
+               *reverse = 1;
+       } else if(isip6(nm, &res)) {
+               *reverse = 1;
+       } else {
+               res = strdup(nm);
+       }
+       if(!res) {
+               fprintf(stderr, "error: out of memory\n");
+               exit(1);
+       }
+       return res;
+}
+
+/** massage input type */
+static int
+massage_type(const char* t, int reverse, int* multi)
+{
+       if(t) {
+               int r = ldns_get_rr_type_by_name(t);
+               if(r == 0 && strcasecmp(t, "TYPE0") != 0 && 
+                       strcmp(t, "") != 0) {
+                       fprintf(stderr, "error unknown type %s\n", t);
+                       exit(1);
+               }
+               return r;
+       }
+       if(!t && reverse)
+               return LDNS_RR_TYPE_PTR;
+       *multi = 1;
+       return LDNS_RR_TYPE_A;
+}
+
+/** massage input class */
+static int
+massage_class(const char* c)
+{
+       if(c) {
+               int r = ldns_get_rr_class_by_name(c);
+               if(r == 0 && strcasecmp(c, "CLASS0") != 0 && 
+                       strcmp(c, "") != 0) {
+                       fprintf(stderr, "error unknown class %s\n", c);
+                       exit(1);
+               }
+               return r;
+       }
+       return LDNS_RR_CLASS_IN;
+}
+
+/** nice security status string */
+static const char* 
+statstr(int sec, struct ub_val_result* result)
+{
+       if(sec) return "[secure]";
+       if(result->bogus) return "[not-secure (BOGUS)]";
+       return "[not-secure (insecure)]";
+}
+
+/** nice string for type */
+static void
+pretty_type(char* s, size_t len, int t)
+{
+       const ldns_rr_descriptor *d = ldns_rr_descript(t);
+       if(d) {
+               snprintf(s, len, "%s", d->_name);
+       } else {
+               snprintf(s, len, "TYPE%d", t);
+       }
+}
+
+/** nice string for class */
+static void
+pretty_class(char* s, size_t len, int c)
+{
+       ldns_lookup_table *cl = ldns_lookup_by_id(ldns_rr_classes, c);
+       if(cl) {
+               snprintf(s, len, "%s", cl->name);
+       } else {
+               snprintf(s, len, "CLASS%d", c);
+       }
+}
+
+/** nice string for rcode */
+static void
+pretty_rcode(char* s, size_t len, int r)
+{
+       ldns_lookup_table *rcode = ldns_lookup_by_id(ldns_rcodes, r);
+       if(rcode) {
+               snprintf(s, len, "%s", rcode->name);
+       } else {
+               snprintf(s, len, "RCODE%d", r);
+       }
+}
+
+/** pretty line of output for results */
+static void
+pretty_output(char* q, int t, int c, int sec, int haved, 
+       struct ub_val_result* result)
+{
+       const char *secstatus = statstr(sec, result);
+       char tstr[16];
+       char cstr[16];
+       char rcodestr[16];
+       pretty_type(tstr, 16, t);
+       pretty_class(cstr, 16, c);
+       pretty_rcode(rcodestr, 16, result->rcode);
+
+       if(!haved && result->rcode) {
+               printf("Host %s not found: %d(%s). %s\n",
+                       q, result->rcode, rcodestr, secstatus);
+               return;
+       }
+       if(!haved) {
+               printf("%s %s %s: no data. %s\n",
+                       q, cstr, tstr, secstatus);
+               return;
+       }
+       printf("%s %s %s: have data. %s\n",
+               q, cstr, tstr, secstatus);
+       /* TODO print the data nicely */
+}
+
+/** perform a lookup and printout return if domain existed */
+static int
+dnslook(struct ub_val_ctx* ctx, char* q, int t, int c)
+{
+       int ret, sec, haved;
+       struct ub_val_result* result;
+
+       ret = ub_val_resolve(ctx, q, t, c, &sec, &haved, &result);
+       if(ret != 0) {
+               fprintf(stderr, "error: %s\n", ub_val_strerror(ret));
+               exit(1);
+       }
+       pretty_output(q, t, c, sec, haved, result);
+       ret = result->nxdomain;
+       ub_val_result_free(result);
+       return ret;
+}
+
+/** perform host lookup */
+static void
+lookup(const char* nm, const char* qt, const char* qc)
+{
+       /* massage input into a query name, type and class */
+       int multi = 0;   /* no type, so do A, AAAA, MX */
+       int reverse = 0; /* we are doing a reverse lookup */
+       char* realq = massage_qname(nm, &reverse);
+       int t = massage_type(qt, reverse, &multi);
+       int c = massage_class(qc);
+
+       /* perform the query */
+       struct ub_val_ctx* ctx = NULL;
+       
+       printf("lookup %s %d %d reverse=%d multi=%d\n", 
+               realq, t, c, reverse, multi);
+       ctx = ub_val_ctx_create();
+       if(!ctx) {
+               fprintf(stderr, "error: out of memory\n");
+               exit(1);
+       }
+       if(multi) {
+               if(!dnslook(ctx, realq, LDNS_RR_TYPE_A, c)) {
+                       /* domain exists, lookup more */
+                       (void)dnslook(ctx, realq, LDNS_RR_TYPE_AAAA, c);
+                       (void)dnslook(ctx, realq, LDNS_RR_TYPE_MX, c);
+               }
+       } else {
+               (void)dnslook(ctx, realq, t, c);
+       }
+       ub_val_ctx_delete(ctx);
+       free(realq);
+}
+
+/** getopt global, in case header files fail to declare it. */
+extern int optind;
+/** getopt global, in case header files fail to declare it. */
+extern char* optarg;
+
+/** Main routine for checkconf */
+int main(int argc, char* argv[])
+{
+       int c;
+       char* qclass = NULL;
+       char* qtype = NULL;
+       /* parse the options */
+       while( (c=getopt(argc, argv, "c:ht:")) != -1) {
+               switch(c) {
+               case 'c':
+                       qclass = optarg;
+                       break;
+               case 't':
+                       qtype = optarg;
+                       break;
+               case '?':
+               case 'h':
+               default:
+                       usage();
+               }
+       }
+       argc -= optind;
+       argv += optind;
+       if(argc != 1)
+               usage();
+
+       lookup(argv[0], qtype, qclass);
+       return 0;
+}