tags:

views:

1523

answers:

4

How do I get similar functionality to the host command using a c api (or any other language for that matter)? I need more information than just an IP address given by gethostbyname(); specifically, the SMTP-related data.

+1  A: 

I don't think there is a function in the C standard library for this, but many scripting languages do have this functionality 'built in'. For example, Perl has the Net::DNS package:

use Net::DNS;
my @mx = mx("example.com");
foreach $host (@mx) {
  print $host;
}

If you need to do this in C, a quick google shows up a few C libraries out there which you can use:

Dave Rigby
+1  A: 

I'd suggest FireDNS. It's a very fast C library for all kinds of dns queries.

ko-dos
+3  A: 

If a blocking (synchronous) query is ok, just use res_query(), and link your program with -lresolv.

 len = res_query(host, C_IN, T_MX, &answer, sizeof(answer));
mark4o
A: 

And I would add, unless you're writing a mail relay you almost certainly shouldn't be looking up MX records - you should be passing the mail on to a user-configured mail relay instead.

caf