How might I perform DNS lookups using C/C++ on Lin

2020-02-12 03:11发布

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.

标签: c linux smtp dns
7条回答
混吃等死
2楼-- · 2020-02-12 03:22

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.

查看更多
Root(大扎)
3楼-- · 2020-02-12 03:23

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:

查看更多
霸刀☆藐视天下
4楼-- · 2020-02-12 03:30

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));
查看更多
\"骚年 ilove
5楼-- · 2020-02-12 03:33

I like adns because it allows for asynchronous requests

查看更多
来,给爷笑一个
6楼-- · 2020-02-12 03:37

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

查看更多
The star\"
7楼-- · 2020-02-12 03:42

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

查看更多
登录 后发表回答