1 /* $NetBSD: test_dns_lookup.c,v 1.2 2017/02/14 01:16:44 christos Exp $ */ 2 3 /*++ 4 /* NAME 5 /* test_dns_lookup 1 6 /* SUMMARY 7 /* DNS lookup test program 8 /* SYNOPSIS 9 /* test_dns_lookup query-type domain-name 10 /* DESCRIPTION 11 /* test_dns_lookup performs a DNS query of the specified resource 12 /* type for the specified resource name. 13 /* DIAGNOSTICS 14 /* Problems are reported to the standard error stream. 15 /* LICENSE 16 /* .ad 17 /* .fi 18 /* The Secure Mailer license must be distributed with this software. 19 /* AUTHOR(S) 20 /* Wietse Venema 21 /* IBM T.J. Watson Research 22 /* P.O. Box 704 23 /* Yorktown Heights, NY 10598, USA 24 /* 25 /* Wietse Venema 26 /* Google, Inc. 27 /* 111 8th Avenue 28 /* New York, NY 10011, USA 29 /*--*/ 30 31 /* System library. */ 32 33 #include <sys_defs.h> 34 #include <netinet/in.h> 35 #include <arpa/inet.h> 36 #include <stdlib.h> 37 38 /* Utility library. */ 39 40 #include <vstring.h> 41 #include <msg.h> 42 #include <msg_vstream.h> 43 #include <mymalloc.h> 44 #include <argv.h> 45 46 /* Global library. */ 47 48 #include <mail_params.h> 49 50 /* Application-specific. */ 51 52 #include "dns.h" 53 54 static void print_rr(VSTRING *buf, DNS_RR *rr) 55 { 56 while (rr) { 57 vstream_printf("ad: %u, rr: %s\n", 58 rr->dnssec_valid, dns_strrecord(buf, rr)); 59 rr = rr->next; 60 } 61 } 62 63 static NORETURN usage(char **argv) 64 { 65 msg_fatal("usage: %s [-npv] [-f filter] types name", argv[0]); 66 } 67 68 int main(int argc, char **argv) 69 { 70 ARGV *types_argv; 71 unsigned *types; 72 char *name; 73 VSTRING *fqdn = vstring_alloc(100); 74 VSTRING *why = vstring_alloc(100); 75 VSTRING *buf; 76 int rcode; 77 DNS_RR *rr; 78 int i; 79 int ch; 80 int lflags = DNS_REQ_FLAG_NONE; 81 82 msg_vstream_init(argv[0], VSTREAM_ERR); 83 while ((ch = GETOPT(argc, argv, "f:npv")) > 0) { 84 switch (ch) { 85 case 'v': 86 msg_verbose++; 87 break; 88 case 'f': 89 dns_rr_filter_compile("DNS reply filter", optarg); 90 break; 91 case 'n': 92 lflags |= DNS_REQ_FLAG_NCACHE_TTL; 93 break; 94 case 'p': 95 var_dns_ncache_ttl_fix = 1; 96 break; 97 default: 98 usage(argv); 99 } 100 } 101 if (argc != optind + 2) 102 usage(argv); 103 types_argv = argv_split(argv[optind], CHARS_COMMA_SP); 104 types = (unsigned *) mymalloc(sizeof(*types) * (types_argv->argc + 1)); 105 for (i = 0; i < types_argv->argc; i++) 106 if ((types[i] = dns_type(types_argv->argv[i])) == 0) 107 msg_fatal("invalid query type: %s", types_argv->argv[i]); 108 types[i] = 0; 109 argv_free(types_argv); 110 name = argv[optind + 1]; 111 msg_verbose = 1; 112 switch (dns_lookup_rv(name, RES_USE_DNSSEC, &rr, fqdn, why, 113 &rcode, lflags, types)) { 114 default: 115 msg_warn("%s (rcode=%d)", vstring_str(why), rcode); 116 case DNS_OK: 117 if (rr) { 118 vstream_printf("%s: fqdn: %s\n", name, vstring_str(fqdn)); 119 buf = vstring_alloc(100); 120 print_rr(buf, rr); 121 dns_rr_free(rr); 122 vstring_free(buf); 123 vstream_fflush(VSTREAM_OUT); 124 } 125 } 126 myfree((void *) types); 127 exit(0); 128 } 129