ldns-mx.c
Go to the documentation of this file.
1 /*
2  * mx is a small program that prints out the mx records
3  * for a particular domain
4  * (c) NLnet Labs, 2005 - 2008
5  * See the file LICENSE for the license
6  */
7 
8 #include "config.h"
9 
10 #include <ldns/ldns.h>
11 
12 static int
13 usage(FILE *fp, char *prog) {
14  fprintf(fp, "%s domain\n", prog);
15  fprintf(fp, " print out the mx for domain\n");
16  return 0;
17 }
18 
19 int
20 main(int argc, char *argv[])
21 {
22  ldns_resolver *res;
23  ldns_rdf *domain;
24  ldns_pkt *p;
25  ldns_rr_list *mx;
26  ldns_status s;
27 
28  p = NULL;
29  mx = NULL;
30  domain = NULL;
31  res = NULL;
32 
33  if (argc != 2) {
34  usage(stdout, argv[0]);
35  exit(EXIT_FAILURE);
36  } else {
37  /* create a rdf from the command line arg */
38  domain = ldns_dname_new_frm_str(argv[1]);
39  if (!domain) {
40  usage(stdout, argv[0]);
41  exit(EXIT_FAILURE);
42  }
43  }
44 
45  /* create a new resolver from /etc/resolv.conf */
46  s = ldns_resolver_new_frm_file(&res, NULL);
47 
48  if (s != LDNS_STATUS_OK) {
49  exit(EXIT_FAILURE);
50  }
51 
52  /* use the resolver to send a query for the mx
53  * records of the domain given on the command line
54  */
55  p = ldns_resolver_query(res,
56  domain,
59  LDNS_RD);
60 
61  ldns_rdf_deep_free(domain);
62 
63  if (!p) {
64  exit(EXIT_FAILURE);
65  } else {
66  /* retrieve the MX records from the answer section of that
67  * packet
68  */
72  if (!mx) {
73  fprintf(stderr,
74  " *** invalid answer name %s after MX query for %s\n",
75  argv[1], argv[1]);
76  ldns_pkt_free(p);
78  exit(EXIT_FAILURE);
79  } else {
80  ldns_rr_list_sort(mx);
81  ldns_rr_list_print(stdout, mx);
83  }
84  }
85  ldns_pkt_free(p);
87  return 0;
88 }