xref: /netbsd-src/external/ibm-public/postfix/dist/src/dns/test_dns_lookup.c (revision 7a6a7ae08ac6c612f0fbb0d4425825c6be2a9050)
1 /*	$NetBSD: test_dns_lookup.c,v 1.1.1.2 2011/03/02 19:32:10 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 
49     while (rr) {
50 	printf("%s: ttl: %9d ", rr->rname, rr->ttl);
51 	switch (rr->type) {
52 	case T_A:
53 #ifdef T_AAAA
54 	case T_AAAA:
55 #endif
56 	    if (dns_rr_to_pa(rr, &host) == 0)
57 		msg_fatal("conversion error for resource record type %s: %m",
58 			  dns_strtype(rr->type));
59 	    printf("%s: %s\n", dns_strtype(rr->type), host.buf);
60 	    break;
61 	case T_CNAME:
62 	case T_MB:
63 	case T_MG:
64 	case T_MR:
65 	case T_NS:
66 	case T_PTR:
67 	case T_TXT:
68 	    printf("%s: %s\n", dns_strtype(rr->type), rr->data);
69 	    break;
70 	case T_MX:
71 	    printf("pref: %d %s: %s\n",
72 		   rr->pref, dns_strtype(rr->type), rr->data);
73 	    break;
74 	default:
75 	    msg_fatal("print_rr: don't know how to print type %s",
76 		      dns_strtype(rr->type));
77 	}
78 	rr = rr->next;
79     }
80 }
81 
82 int     main(int argc, char **argv)
83 {
84     ARGV   *types_argv;
85     unsigned *types;
86     char   *name;
87     VSTRING *fqdn = vstring_alloc(100);
88     VSTRING *why = vstring_alloc(100);
89     DNS_RR *rr;
90     int     i;
91 
92     msg_vstream_init(argv[0], VSTREAM_ERR);
93     if (argc != 3)
94 	msg_fatal("usage: %s types name", argv[0]);
95     types_argv = argv_split(argv[1], ", \t\r\n");
96     types = (unsigned *) mymalloc(sizeof(*types) * (types_argv->argc + 1));
97     for (i = 0; i < types_argv->argc; i++)
98 	if ((types[i] = dns_type(types_argv->argv[i])) == 0)
99 	    msg_fatal("invalid query type: %s", types_argv->argv[i]);
100     types[i] = 0;
101     argv_free(types_argv);
102     name = argv[2];
103     msg_verbose = 1;
104     switch (dns_lookup_v(name, RES_DEBUG, &rr, fqdn, why,
105 			 DNS_REQ_FLAG_NONE, types)) {
106     default:
107 	msg_fatal("%s", vstring_str(why));
108     case DNS_OK:
109 	printf("%s: fqdn: %s\n", name, vstring_str(fqdn));
110 	print_rr(rr);
111 	dns_rr_free(rr);
112     }
113     myfree((char *) types);
114     exit(0);
115 }
116