ldns-mx.c

Go to the documentation of this file.
00001 /*
00002  * mx is a small programs that prints out the mx records
00003  * for a particulary domain
00004  * (c) NLnet Labs, 2005
00005  * See the file LICENSE for the license
00006  */
00007 
00008 #include "config.h"
00009 
00010 #include <ldns/ldns.h>
00011 
00012 int
00013 usage(FILE *fp, char *prog) {
00014         fprintf(fp, "%s domain\n", prog);
00015         fprintf(fp, "  print out the mx for domain\n");
00016         return 0;
00017 }
00018 
00019 int
00020 main(int argc, char *argv[])
00021 {
00022         ldns_resolver *res;
00023         ldns_rdf *domain;
00024         ldns_pkt *p;
00025         ldns_rr_list *mx;
00026         ldns_status s;
00027         
00028         p = NULL;
00029         mx = NULL;
00030         domain = NULL;
00031         res = NULL;
00032         
00033         if (argc != 2) {
00034                 usage(stdout, argv[0]);
00035                 exit(EXIT_FAILURE);
00036         } else {
00037                 /* create a rdf from the command line arg */
00038                 domain = ldns_dname_new_frm_str(argv[1]);
00039                 if (!domain) {
00040                         usage(stdout, argv[0]);
00041                         exit(EXIT_FAILURE);
00042                 }
00043         }
00044 
00045         /* create a new resolver from /etc/resolv.conf */
00046         s = ldns_resolver_new_frm_file(&res, NULL);
00047 
00048         if (s != LDNS_STATUS_OK) {
00049                 exit(EXIT_FAILURE);
00050         }
00051 
00052         /* use the resolver to send it a query for the mx 
00053          * records of the domain given on the command line
00054          */
00055         p = ldns_resolver_query(res,
00056                                 domain,
00057                                 LDNS_RR_TYPE_MX,
00058                                 LDNS_RR_CLASS_IN,
00059                                 LDNS_RD);
00060 
00061         ldns_rdf_deep_free(domain);
00062         
00063         if (!p)  {
00064                 exit(EXIT_FAILURE);
00065         } else {
00066                 /* retrieve the MX records from the answer section of that
00067                  * packet
00068                  */
00069                 mx = ldns_pkt_rr_list_by_type(p,
00070                                               LDNS_RR_TYPE_MX,
00071                                               LDNS_SECTION_ANSWER);
00072                 if (!mx) {
00073                         fprintf(stderr, 
00074                                         " *** invalid answer name %s after MX query for %s\n",
00075                                         argv[1], argv[1]);
00076                         ldns_pkt_free(p);
00077                         ldns_resolver_deep_free(res);
00078                         exit(EXIT_FAILURE);
00079                 } else {
00080                         ldns_rr_list_sort(mx); 
00081                         ldns_rr_list_print(stdout, mx);
00082                         ldns_rr_list_deep_free(mx);
00083                 }
00084         }
00085         ldns_pkt_free(p);
00086         ldns_resolver_deep_free(res);
00087         return 0;
00088 }

Generated on Wed Feb 20 20:28:36 2008 for ldns by  doxygen 1.5.5