Querying for an A record in C
I'm working on some code that will check DNS records using libresolv.
I first do the query with res_query and that succeeds, but if I'm checking an 'A' record, d开发者_开发百科n_expand will not give me the IP address back (and I don't think I should really expect it to). However, I have not been able to find a function that will decode the IP address from an rr How can I get the IP? I can post some code if needed.The way to do this is to take the rdata from the rr (using res_rr_rdata(rr) ), and run it through inet_ntop.
For an A record, this is what I ended with:
if (ns_parserr(&msg, ns_s_an, rrnum, &rr)) {
printf("ns_parserr: %s\n", strerror(errno));
}
if(ns_rr_type(rr) == ns_t_cname){
printf("Found cname\n");
}
inet_ntop(AF_INET, ns_rr_rdata(rr), decodedBuffer, NS_MAXDNAME);
decodedBuffer ends up with the ip. Obviously for AAAA, use AF_INET6
精彩评论