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