From 83a419bf1559fb4742e3155dab4c1fab3a0dc128 Mon Sep 17 00:00:00 2001 From: Wojciech Kosior Date: Mon, 11 May 2020 14:43:54 +0200 Subject: make helper program able to ask DNS at address provided on the command line --- Makefile | 4 +- src/ask_localhost.c | 138 --------------------------------------------------- src/ask_resolver.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 140 deletions(-) delete mode 100644 src/ask_localhost.c create mode 100644 src/ask_resolver.c diff --git a/Makefile b/Makefile index ccd3186..89c14f8 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ CFLAGS = -std=c99 -Wall -Werror -I include build/%.o : src/%.c | build gcc $(CFLAGS) $^ -c -o $@ -ask_localhost : build/ask_localhost.o +ask_resolver : build/ask_resolver.o $(CC) $^ -lunbound -o $@ build : @@ -16,6 +16,6 @@ build : all : 0tDNS receive_respond clean : - -rm -r build 0tDNS ask_localhost + -rm -r build 0tDNS ask_resolver .PHONY : clean diff --git a/src/ask_localhost.c b/src/ask_localhost.c deleted file mode 100644 index 49845c3..0000000 --- a/src/ask_localhost.c +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Code in examine_result() is taken from official libunbound examples: - * https://nlnetlabs.nl/documentation/unbound/libunbound-tutorial-3/ - * The rest of this file is - * Copyright (C) 2020 by Wojtek Kosior - - * Permission to use, copy, modify, and/or distribute this software - * for any purpose with or without fee is hereby granted. - - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH - * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, - * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR - * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * This is a simple helper program for testing our resolver. - */ - -#include -#include -#include - -#include - -/* examine the result structure in detail */ -void examine_result(const char *query, struct ub_result *result) -{ - int i; - int num; - - printf("The query is for: %s\n", query); - printf("The result has:\n"); - printf("qname: %s\n", result->qname); - printf("qtype: %d\n", result->qtype); - printf("qclass: %d\n", result->qclass); - if(result->canonname) - printf("canonical name: %s\n", - result->canonname); - else printf("canonical name: \n"); - - if(result->havedata) - printf("has data\n"); - else printf("has no data\n"); - - if(result->nxdomain) - printf("nxdomain (name does not exist)\n"); - else printf("not an nxdomain (name exists)\n"); - - if(result->secure) - printf("validated to be secure\n"); - else printf("not validated as secure\n"); - - if(result->bogus) - printf("a security failure! (bogus)\n"); - else printf("not a security failure (not bogus)\n"); - - printf("DNS rcode: %d\n", result->rcode); - - if(!result->havedata) - return; - - num = 0; - for(i=0; result->data[i]; i++) { - printf("result data element %d has length %d\n", - i, result->len[i]); - printf("result data element %d is: %s\n", - i, inet_ntoa(*(struct in_addr*)result->data[i])); - num++; - } - printf("result has %d data element(s)\n", num); -} - -struct ub_ctx *create_ub_context(int debuglevel) { - int rc; - struct ub_ctx* ctx; - char *error_message_format; - - ctx = ub_ctx_create(); - if (!ctx) { - fprintf(stderr, "Couldn't create libunbound context.\n"); - return NULL; - } - - error_message_format = "Couldn't set forwarder: %s\n"; - rc = ub_ctx_set_fwd(ctx, "127.0.0.1"); - if (rc) - goto out; - - error_message_format = "Couldn't set debuglevel: %s\n"; - rc = ub_ctx_debuglevel(ctx, debuglevel); - -out: - if (rc) { - fprintf(stderr, error_message_format, ub_strerror(rc)); - ub_ctx_delete(ctx); - return NULL; - } - - return ctx; -} - -void ztdns_try_resolve(struct ub_ctx *ctx, const char *name) { - struct ub_result* result; - int rc; - rc = ub_resolve(ctx, name, - 1 /* TYPE A (IPv4 address) */, - 1 /* CLASS IN (internet) */, &result); - if(rc) - printf("resolve error: %s\n", ub_strerror(rc)); - else { - examine_result(name, result); - ub_resolve_free(result); - } -} - -int main(int argc, char** argv) -{ - struct ub_ctx *ctx; - - if (argc < 2) { - printf("Usage: %s DOMAINNAME\n", argv[0]); - return EXIT_FAILURE; - } - - ctx = create_ub_context(3); - if (!ctx) - return EXIT_FAILURE; - - ztdns_try_resolve(ctx, argv[1]); - - ub_ctx_delete(ctx); - - return EXIT_SUCCESS; -} diff --git a/src/ask_resolver.c b/src/ask_resolver.c new file mode 100644 index 0000000..dc7c660 --- /dev/null +++ b/src/ask_resolver.c @@ -0,0 +1,140 @@ +/* + * Code in examine_result() is taken from official libunbound examples: + * https://nlnetlabs.nl/documentation/unbound/libunbound-tutorial-3/ + * The rest of this file is + * Copyright (C) 2020 by Wojtek Kosior + + * Permission to use, copy, modify, and/or distribute this software + * for any purpose with or without fee is hereby granted. + + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, + * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM + * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * This is a simple helper program for testing our resolver. + */ + +#include +#include +#include + +#include + +/* examine the result structure in detail */ +void examine_result(const char *query, struct ub_result *result) +{ + int i; + int num; + + printf("The query is for: %s\n", query); + printf("The result has:\n"); + printf("qname: %s\n", result->qname); + printf("qtype: %d\n", result->qtype); + printf("qclass: %d\n", result->qclass); + if(result->canonname) + printf("canonical name: %s\n", + result->canonname); + else printf("canonical name: \n"); + + if(result->havedata) + printf("has data\n"); + else printf("has no data\n"); + + if(result->nxdomain) + printf("nxdomain (name does not exist)\n"); + else printf("not an nxdomain (name exists)\n"); + + if(result->secure) + printf("validated to be secure\n"); + else printf("not validated as secure\n"); + + if(result->bogus) + printf("a security failure! (bogus)\n"); + else printf("not a security failure (not bogus)\n"); + + printf("DNS rcode: %d\n", result->rcode); + + if(!result->havedata) + return; + + num = 0; + for(i=0; result->data[i]; i++) { + printf("result data element %d has length %d\n", + i, result->len[i]); + printf("result data element %d is: %s\n", + i, inet_ntoa(*(struct in_addr*)result->data[i])); + num++; + } + printf("result has %d data element(s)\n", num); +} + +struct ub_ctx *create_ub_context(char *forwarder_addr, int debuglevel) +{ + int rc; + struct ub_ctx* ctx; + char *error_message_format; + + ctx = ub_ctx_create(); + if (!ctx) { + fprintf(stderr, "Couldn't create libunbound context.\n"); + return NULL; + } + + error_message_format = "Couldn't set forwarder: %s\n"; + rc = ub_ctx_set_fwd(ctx, forwarder_addr); + if (rc) + goto out; + + error_message_format = "Couldn't set debuglevel: %s\n"; + rc = ub_ctx_debuglevel(ctx, debuglevel); + +out: + if (rc) { + fprintf(stderr, error_message_format, ub_strerror(rc)); + ub_ctx_delete(ctx); + return NULL; + } + + return ctx; +} + +void ztdns_try_resolve(struct ub_ctx *ctx, const char *name) +{ + struct ub_result* result; + int rc; + rc = ub_resolve(ctx, name, + 1 /* TYPE A (IPv4 address) */, + 1 /* CLASS IN (internet) */, &result); + if(rc) + printf("resolve error: %s\n", ub_strerror(rc)); + else { + examine_result(name, result); + ub_resolve_free(result); + } +} + +int main(int argc, char** argv) +{ + struct ub_ctx *ctx; + + if (argc != 3) { + printf("Usage: %s DNS_SERVER DOMAINNAME\n", argv[0]); + return EXIT_FAILURE; + } + + ctx = create_ub_context(argv[1], 3); + if (!ctx) + return EXIT_FAILURE; + + ztdns_try_resolve(ctx, argv[2]); + + ub_ctx_delete(ctx); + + return EXIT_SUCCESS; +} -- cgit v1.2.3