ldns-mx.c
Go to the documentation of this file.
/*
* mx is a small program that prints out the mx records
* for a particular domain
* (c) NLnet Labs, 2005 - 2008
* See the file LICENSE for the license
*/
#include "config.h"
#include <ldns/ldns.h>
static int
usage(FILE *fp, char *prog) {
fprintf(fp, "%s domain\n", prog);
fprintf(fp, " print out the mx for domain\n");
return 0;
}
int
main(int argc, char *argv[])
{
ldns_resolver *res;
ldns_rdf *domain;
ldns_pkt *p;
ldns_rr_list *mx;
ldns_status s;
p = NULL;
mx = NULL;
domain = NULL;
res = NULL;
if (argc != 2) {
usage(stdout, argv[0]);
exit(EXIT_FAILURE);
} else {
/* create a rdf from the command line arg */
domain = ldns_dname_new_frm_str(argv[1]);
if (!domain) {
usage(stdout, argv[0]);
exit(EXIT_FAILURE);
}
if (! ldns_dname_str_absolute(argv[1]) &&
ldns_dname_absolute(domain)) {
/* ldns_dname_new_frm_str makes absolute dnames always!
* So deabsolutify domain.
* TODO: Create ldns_dname_new_frm_str_relative? Yuck!
*/
ldns_rdf_set_size(domain, ldns_rdf_size(domain) - 1);
}
}
/* create a new resolver from /etc/resolv.conf */
s = ldns_resolver_new_frm_file(&res, NULL);
if (s != LDNS_STATUS_OK) {
exit(EXIT_FAILURE);
}
/* use the resolver to send a query for the mx
* records of the domain given on the command line
*/
p = ldns_resolver_search(res,
domain,
LDNS_RR_TYPE_MX,
LDNS_RR_CLASS_IN,
LDNS_RD);
ldns_rdf_deep_free(domain);
if (!p) {
exit(EXIT_FAILURE);
} else {
/* retrieve the MX records from the answer section of that
* packet
*/
mx = ldns_pkt_rr_list_by_type(p,
LDNS_RR_TYPE_MX,
LDNS_SECTION_ANSWER);
if (!mx) {
fprintf(stderr,
" *** invalid answer name %s after MX query for %s\n",
argv[1], argv[1]);
ldns_pkt_free(p);
ldns_resolver_deep_free(res);
exit(EXIT_FAILURE);
} else {
ldns_rr_list_sort(mx);
ldns_rr_list_print(stdout, mx);
ldns_rr_list_deep_free(mx);
}
}
ldns_pkt_free(p);
ldns_resolver_deep_free(res);
return 0;
}
ldns_dname_str_absolute
signed char ldns_dname_str_absolute(const char *dname_str)
Checks whether the given dname string is absolute (i.e.
Definition dname.c:518
ldns_dname_absolute
signed char ldns_dname_absolute(const ldns_rdf *dname)
Checks whether the given dname is absolute (i.e.
Definition dname.c:548
ldns_dname_new_frm_str
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
@ LDNS_STATUS_OK
Definition error.h:26
ldns_status
enum ldns_enum_status ldns_status
Definition error.h:152
ldns_rr_list_print
void ldns_rr_list_print(FILE *output, const ldns_rr_list *list)
print a rr_list to output
Definition host2str.c:3514
prog
char * prog
Definition ldns-signzone.c:36
ldns.h
Including this file will include all ldns files, and define some lookup tables.
main
int main(void)
Definition linktest.c:6
ldns_pkt_free
void ldns_pkt_free(ldns_pkt *packet)
frees the packet structure and all data that it contains.
Definition packet.c:917
ldns_pkt_rr_list_by_type
ldns_rr_list * ldns_pkt_rr_list_by_type(const ldns_pkt *p, ldns_rr_type t, ldns_pkt_section s)
return all the rr with a specific type from a packet.
Definition packet.c:323
ldns_pkt
struct ldns_struct_pkt ldns_pkt
Definition packet.h:272
LDNS_SECTION_ANSWER
@ LDNS_SECTION_ANSWER
Definition packet.h:279
LDNS_RD
#define LDNS_RD
Definition packet.h:30
ldns_rdf_deep_free
void ldns_rdf_deep_free(ldns_rdf *rd)
frees a rdf structure and frees the data.
Definition rdata.c:230
ldns_rdf_size
size_t ldns_rdf_size(const ldns_rdf *rd)
returns the size of the rdf.
Definition rdata.c:24
ldns_rdf
struct ldns_struct_rdf ldns_rdf
Definition rdata.h:211
ldns_rdf_set_size
void ldns_rdf_set_size(ldns_rdf *rd, size_t size)
sets the size of the rdf.
Definition rdata.c:46
ldns_resolver
struct ldns_struct_resolver ldns_resolver
Definition resolver.h:147
ldns_resolver_search
ldns_pkt * ldns_resolver_search(const ldns_resolver *r, const ldns_rdf *rdf, 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:1101
ldns_resolver_deep_free
void ldns_resolver_deep_free(ldns_resolver *res)
Frees the allocated space for this resolver and all it's data.
Definition resolver.c:1004
ldns_resolver_new_frm_file
ldns_status ldns_resolver_new_frm_file(ldns_resolver **r, 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:967
ldns_rr_list_deep_free
void ldns_rr_list_deep_free(ldns_rr_list *rr_list)
frees an rr_list structure and all rrs contained therein.
Definition rr.c:1018
ldns_rr_list_sort
void ldns_rr_list_sort(ldns_rr_list *unsorted)
sorts an rr_list (canonical wire format).
Definition rr.c:1554
LDNS_RR_TYPE_MX
@ LDNS_RR_TYPE_MX
mail exchange
Definition rr.h:108
ldns_rr_list
struct ldns_struct_rr_list ldns_rr_list
Definition rr.h:360
LDNS_RR_CLASS_IN
@ LDNS_RR_CLASS_IN
the Internet
Definition rr.h:47