1 /* $NetBSD: test_dns_lookup.c,v 1.1.1.3 2014/07/06 19:27:50 tron 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 26 /* System library. */ 27 28 #include <sys_defs.h> 29 #include <netinet/in.h> 30 #include <arpa/inet.h> 31 #include <stdlib.h> 32 33 /* Utility library. */ 34 35 #include <vstring.h> 36 #include <msg.h> 37 #include <msg_vstream.h> 38 #include <mymalloc.h> 39 #include <argv.h> 40 41 /* Application-specific. */ 42 43 #include "dns.h" 44 45 static void print_rr(DNS_RR *rr) 46 { 47 MAI_HOSTADDR_STR host; 48 size_t i; 49 50 while (rr) { 51 printf("%s: ad: %u, ttl: %9u ", rr->rname, rr->dnssec_valid, rr->ttl); 52 switch (rr->type) { 53 case T_A: 54 #ifdef T_AAAA 55 case T_AAAA: 56 #endif 57 if (dns_rr_to_pa(rr, &host) == 0) 58 msg_fatal("conversion error for resource record type %s: %m", 59 dns_strtype(rr->type)); 60 printf("%s: %s\n", dns_strtype(rr->type), host.buf); 61 break; 62 case T_CNAME: 63 case T_DNAME: 64 case T_MB: 65 case T_MG: 66 case T_MR: 67 case T_NS: 68 case T_PTR: 69 case T_TXT: 70 printf("%s: %s\n", dns_strtype(rr->type), rr->data); 71 break; 72 case T_MX: 73 printf("pref: %d %s: %s\n", 74 rr->pref, dns_strtype(rr->type), rr->data); 75 break; 76 case T_TLSA: 77 if (rr->data_len >= 3) { 78 uint8_t *ip = (uint8_t *) rr->data; 79 uint8_t usage = *ip++; 80 uint8_t selector = *ip++; 81 uint8_t mtype = *ip++; 82 83 printf("%s: %d %d %d ", dns_strtype(rr->type), 84 usage, selector, mtype); 85 for (i = 3; i < rr->data_len; ++i) 86 printf("%02x", *ip++); 87 putchar('\n'); 88 } else { 89 printf("%s: truncated record\n", dns_strtype(rr->type)); 90 } 91 break; 92 default: 93 msg_fatal("print_rr: don't know how to print type %s", 94 dns_strtype(rr->type)); 95 } 96 rr = rr->next; 97 } 98 } 99 100 int main(int argc, char **argv) 101 { 102 ARGV *types_argv; 103 unsigned *types; 104 char *name; 105 VSTRING *fqdn = vstring_alloc(100); 106 VSTRING *why = vstring_alloc(100); 107 int rcode; 108 DNS_RR *rr; 109 int i; 110 111 msg_vstream_init(argv[0], VSTREAM_ERR); 112 if (argc != 3) 113 msg_fatal("usage: %s types name", argv[0]); 114 types_argv = argv_split(argv[1], ", \t\r\n"); 115 types = (unsigned *) mymalloc(sizeof(*types) * (types_argv->argc + 1)); 116 for (i = 0; i < types_argv->argc; i++) 117 if ((types[i] = dns_type(types_argv->argv[i])) == 0) 118 msg_fatal("invalid query type: %s", types_argv->argv[i]); 119 types[i] = 0; 120 argv_free(types_argv); 121 name = argv[2]; 122 msg_verbose = 1; 123 switch (dns_lookup_rv(name, RES_DEBUG | RES_USE_DNSSEC, &rr, fqdn, why, 124 &rcode, DNS_REQ_FLAG_NONE, types)) { 125 default: 126 msg_fatal("%s (rcode=%d)", vstring_str(why), rcode); 127 case DNS_OK: 128 printf("%s: fqdn: %s\n", name, vstring_str(fqdn)); 129 print_rr(rr); 130 dns_rr_free(rr); 131 } 132 myfree((char *) types); 133 exit(0); 134 } 135