Mx lookup via res_query in C

Kevin Darcy kcd at daimlerchrysler.com
Thu Aug 17 22:56:43 UTC 2000


Jim Reid wrote:

> >>>>> "Mikael" == Mikael Chambon <quanta at akasha.cronos.org> writes:
>
>     Mikael> HEllo, I am trying to do a MX lookup in C (under linux
>     Mikael> RH6.2), in order to do it I use the res_query function
>     Mikael> that should do the job.
>
>     Mikael> int res_query(const char *dname, int class, int type,
>     Mikael>               unsigned char *answer, int anslen);
>
>     Mikael> The problem is that I can't find any information about the
>     Mikael> "type", It should be there that I write MX. But what is
>     Mikael> the proper Integer that I should write to do a MX lookup
>     Mikael> ??  If someone has some informations ...
>
>     Mikael> I 've already check in /usr/include/resolv.h and
>     Mikael> res_query.c but I didn't find anything..
>
> The type parameter should be the QTYPE code for the resource record.
> In this case that would be 15, the value defined in RFC1035 for an MX
> record. You'll usually find a list of mnemonics and code values for
> resource records in <arpa/nameser.h>. I'm sure there will be examples
> of how to do MX record lookups in any of the open source mail
> implementations like sendmail or Postfix.

A relevant line in sendmail (domain.c, line 169, in the 8.10.1 version)
is:

n = (*resfunc)(host, C_IN, T_MX, (u_char *) &answer, sizeof(answer));

"resfunc" is either res_query() or res_search(), depending on the
configured settings related to wildcard MX handling. On older systems,
T_MX is defined in /usr/include/arpa/nameser.h; on newer systems, it is
defined in /usr/include/arpa/nameser_compat.h as a #define for "ns_t_mx"
which is then defined in /usr/include/arpa/nameser.h. Unless you want
backwards compatibility with older systems, the modern usage would be to
just use "ns_t_mx" for the type and "ns_c_in" for the class and not
#include /usr/include/arpa/nameser_compat.h at all.

Be aware that _sending_ an MX query is the easy part: decoding the answer
to that query can be much, much harder. Look at the subsequent code in
domain.c to see what I mean...


- Kevin





More information about the bind-users mailing list