Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How might I perform DNS lookups using C/C++ on Linux?

Tags:

c

linux

smtp

dns

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.

like image 576
hacim Avatar asked Jul 14 '09 22:07

hacim


People also ask

How does Linux do DNS lookup?

The dig command in Linux is used to gather DNS information. It stands for Domain Information Groper, and it collects data about Domain Name Servers. The dig command is helpful for troubleshooting DNS problems, but is also used to display DNS information.

How do I run a DNS lookup?

Go to Start and type cmd in the search field to open the command prompt. Alternatively, go to Start > Run > type cmd or command. Type nslookup and hit Enter. The displayed information will be your local DNS server and its IP address.

Which program can be used to perform a DNS lookup?

nslookup (from name server lookup) is a network administration command-line tool for querying the Domain Name System (DNS) to obtain the mapping between domain name and IP address, or other DNS records.


3 Answers

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));
like image 77
mark4o Avatar answered Nov 01 '22 15:11

mark4o


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

like image 42
ko-dos Avatar answered Nov 01 '22 15:11

ko-dos


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.

like image 34
unDEFER Avatar answered Nov 01 '22 13:11

unDEFER