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  if (! ldns_dname_str_absolute(argv[1]) &&
44  ldns_dname_absolute(domain)) {
45 
46  /* ldns_dname_new_frm_str makes absolute dnames always!
47  * So deabsolutify domain.
48  * TODO: Create ldns_dname_new_frm_str_relative? Yuck!
49  */
50  ldns_rdf_set_size(domain, ldns_rdf_size(domain) - 1);
51  }
52  }
53 
54  /* create a new resolver from /etc/resolv.conf */
55  s = ldns_resolver_new_frm_file(&res, NULL);
56 
57  if (s != LDNS_STATUS_OK) {
58  exit(EXIT_FAILURE);
59  }
60 
61  /* use the resolver to send a query for the mx
62  * records of the domain given on the command line
63  */
64  p = ldns_resolver_search(res,
65  domain,
68  LDNS_RD);
69 
70  ldns_rdf_deep_free(domain);
71 
72  if (!p) {
73  exit(EXIT_FAILURE);
74  } else {
75  /* retrieve the MX records from the answer section of that
76  * packet
77  */
81  if (!mx) {
82  fprintf(stderr,
83  " *** invalid answer name %s after MX query for %s\n",
84  argv[1], argv[1]);
85  ldns_pkt_free(p);
87  exit(EXIT_FAILURE);
88  } else {
89  ldns_rr_list_sort(mx);
90  ldns_rr_list_print(stdout, mx);
92  }
93  }
94  ldns_pkt_free(p);
96  return 0;
97 }
bool ldns_dname_str_absolute(const char *dname_str)
Checks whether the given dname string is absolute (i.e.
Definition: dname.c:518
bool ldns_dname_absolute(const ldns_rdf *rdf)
Checks whether the given dname is absolute (i.e.
Definition: dname.c:548
ldns_rdf * ldns_dname_new_frm_str(const char *str)
creates a new dname rdf from a string.
Definition: dname.c:268
@ LDNS_STATUS_OK
Definition: error.h:26
enum ldns_enum_status ldns_status
Definition: error.h:146
void ldns_rr_list_print(FILE *output, const ldns_rr_list *lst)
print a rr_list to output
Definition: host2str.c:3446
int main(int argc, char *argv[])
Definition: ldns-mx.c:20
char * prog
Definition: ldns-signzone.c:32
Including this file will include all ldns files, and define some lookup tables.
void ldns_pkt_free(ldns_pkt *packet)
frees the packet structure and all data that it contains.
Definition: packet.c:895
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:304
@ LDNS_SECTION_ANSWER
Definition: packet.h:279
#define LDNS_RD
Definition: packet.h:30
void ldns_rdf_deep_free(ldns_rdf *rd)
frees a rdf structure and frees the data.
Definition: rdata.c:230
size_t ldns_rdf_size(const ldns_rdf *rd)
returns the size of the rdf.
Definition: rdata.c:24
void ldns_rdf_set_size(ldns_rdf *rd, size_t size)
sets the size of the rdf.
Definition: rdata.c:46
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:1002
ldns_pkt * ldns_resolver_search(const ldns_resolver *r, const ldns_rdf *name, ldns_rr_type t, ldns_rr_class c, uint16_t flags)
Send the query for using the resolver and take the search list into account The search algorithm is a...
Definition: resolver.c:1130
void ldns_resolver_deep_free(ldns_resolver *res)
Frees the allocated space for this resolver and all it's data.
Definition: resolver.c:1039
void ldns_rr_list_deep_free(ldns_rr_list *rr_list)
frees an rr_list structure and all rrs contained therein.
Definition: rr.c:1020
void ldns_rr_list_sort(ldns_rr_list *unsorted)
sorts an rr_list (canonical wire format).
Definition: rr.c:1516
@ LDNS_RR_TYPE_MX
mail exchange
Definition: rr.h:108
@ LDNS_RR_CLASS_IN
the Internet
Definition: rr.h:47
DNS packet.
Definition: packet.h:235
Resource record data field.
Definition: rdata.h:197
DNS stub resolver structure.
Definition: resolver.h:60
List or Set of Resource Records.
Definition: rr.h:338