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 }
void ldns_rdf_deep_free(ldns_rdf *rd)
frees a rdf structure and frees the data.
Definition: rdata.c:230
DNS stub resolver structure.
Definition: resolver.h:59
ldns_pkt * ldns_resolver_query(const ldns_resolver *r, const ldns_rdf *name, ldns_rr_type t, ldns_rr_class c, uint16_t flags)
Send a query to a nameserver.
Definition: resolver.c:1057
List or Set of Resource Records.
Definition: rr.h:327
void ldns_resolver_deep_free(ldns_resolver *res)
Frees the allocated space for this resolver and all it's data.
Definition: resolver.c:945
#define LDNS_RD
Definition: packet.h:30
void ldns_rr_list_deep_free(ldns_rr_list *rr_list)
frees an rr_list structure and all rrs contained therein.
Definition: rr.c:984
ldns_rr_list * ldns_pkt_rr_list_by_type(const ldns_pkt *packet, ldns_rr_type type, ldns_pkt_section sec)
return all the rr with a specific type from a packet.
Definition: packet.c:284
Including this file will include all ldns files, and define some lookup tables.
ldns_rdf * ldns_dname_new_frm_str(const char *str)
creates a new dname rdf from a string.
Definition: dname.c:265
int main(int argc, char *argv[])
Definition: ldns-mx.c:20
void ldns_rr_list_print(FILE *output, const ldns_rr_list *lst)
print a rr_list to output
Definition: host2str.c:2549
the Internet
Definition: rr.h:50
DNS packet.
Definition: packet.h:233
enum ldns_enum_status ldns_status
Definition: error.h:131
ldns_status ldns_resolver_new_frm_file(ldns_resolver **res, const char *filename)
Configure a resolver by means of a resolv.conf file The file may be NULL in which case there will be ...
Definition: resolver.c:908
Resource record data field.
Definition: rdata.h:166
mail exchange
Definition: rr.h:111
void ldns_pkt_free(ldns_pkt *packet)
frees the packet structure and all data that it contains.
Definition: packet.c:784
void ldns_rr_list_sort(ldns_rr_list *unsorted)
sorts an rr_list (canonical wire format).
Definition: rr.c:1479