10

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.

Shog9
  • 156,901
  • 35
  • 231
  • 235
hacim
  • 281
  • 1
  • 3
  • 9

7 Answers7

21

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
  • 58,919
  • 18
  • 87
  • 102
1

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

ko-dos
  • 1,420
  • 12
  • 11
  • 2
    The project looks abandonned (last release in 2004) and unfortunately the download link doesn't work (http://www.firestuff.org/)... – Destroyica Mar 29 '12 at 08:09
1

I know that the question is old, but I have long searched a dns library, and all answers here just stubs me. I think libraries like adns/udns have written not for human beings. And FireDNS for a long time have not working download links.

I have found poslib as the best dns library with very easy interface.

unDEFER
  • 129
  • 1
  • 4
1

I like adns because it allows for asynchronous requests

yfrancis
  • 2,616
  • 1
  • 17
  • 26
Finaldie
  • 61
  • 3
0

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:

DaveR
  • 9,540
  • 3
  • 39
  • 58
0

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
  • 233,326
  • 40
  • 323
  • 462
0

You can also try c-ares library https://c-ares.haxx.se/, which allows to send asynchronous DNS queries. It also comes with adig - its own version of dig utility for querying DNS. You can check it to see how to parse DNS reply: adig.c source

Daniel Frużyński
  • 2,091
  • 19
  • 28