Using res_nquery etc

Paul Vixie vixie at
Sun Oct 17 14:45:11 UTC 2004

Pieter De Wit <pieter at> writes:

> I am still trying to get the MX records for a domain. This time I hope 
> to have more luck. Here is a sample program that I am trying
> ...
> Now, I would like to know why this program seg. faults on res_nsearch (I 
> have tried res_nquery - same result) Again google,resolv.h yielded no 
> result. I doubt if there is even a page on the net about the resolver - 
> except the IBM ones ???

you're sprintf()'ing binary data.  look at
for an example of how to call res_nquery() (which is very similar to 
res_nsearch() if that's your real goal.)  this is not an official release
of this code, but i use it in some spam robotry here and it works great:

here's an example of success:

        [sa:i386] dnsget mx && echo t
        NOERROR 2 3 0

here's an example of failure:

	[sa:i386] dnsget mx && echo t

deep in the heart of this program you'll see:

        n = res_nquery(res, domain, ns_c_in, rr->type, buf, sizeof buf);

and then there's a bunch of packet and rr parsing code to get the binary
response from DNS converted to presentation format for printing.  "dnsget"
does not handle every rr type, just the ones my perl/shell scripts need for
processing my inbound spam.  dnsget will ultimately be donated to isc and
published as part of bind, either as a standard utility or in "contrib".
(it exists because parsing the output from "dig" in a shell script is hard,
especially given the differences in output between various dig versions.)

for what you're doing, you should probably be using Perl and its Net::DNS
module.  but if you really want C, then "dnsget.c" is a way to learn what
it is res_nquery() (and perhaps also res_nsearch()) wants from you.
Paul Vixie

More information about the bind-users mailing list