How can I do a DNS lookup using C / C ++ on Linux?

How to get similar functionality with the host command using c api (or any other language, for that matter)? I need more information than just the IP address specified by gethostbyname() ; in particular, data related to SMTP.

+6
c linux smtp dns
source share
6 answers

If the lock request (synchronous) is ok, use res_query() and link your program with -lresolv.

  len = res_query(host, C_IN, T_MX, &answer, sizeof(answer)); 
+15
source share

I suggest FireDNS . This is a very fast C library for all kinds of DNS queries.

+1
source share

I like adns as it allows asynchronous requests

+1
source share

I know the question is old, but I've been looking for the dns library for a long time, and all the answers here just drown me out. I think libraries like adns / udns are not written for people. And FireDNS does not work with link loading for a long time.

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

+1
source share

I don’t think there is a function for this in the standard C library, but many scripting languages ​​have a built-in function. 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, there will be several C libraries in fast Google that you can use:

0
source share

And I would add that if you do not write a mail relay, you almost certainly should not look for MX records - you should send mail to a user-configured mail relay.

0
source share

All Articles