简体   繁体   中英

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

How do I get similar functionality to the host command using ac 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.

如果阻塞(同步)查询没问题,则只需使用res_query() ,然后将程序与-lresolv链接即可。

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

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

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.

我喜欢adns,因为它允许异步请求

而且我要补充一点,除非您正在编写邮件中继,否则几乎可以肯定不应该查找MX记录-您应该将邮件传递给用户配置的邮件中继。

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

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM