c-ares是一个C语言的异步DNS解析库,可以很方便的和使用者的事件循环统一起来,实现DNS的非
阻塞异步解析,libcurl, libevent, gevent, nodejs都在使用。
下面摘自Stack Overflow的一个例子,
#include <time.h>
#include <iostream>
#include <netdb.h>
#include <arpa/inet.h>
#include <ares.h>
void dns_callback (void* arg, int status, int timeouts, struct hostent* host) //ares 处理完成,返回DNS解析的信息
{
if(status == ARES_SUCCESS)
std::cout << host->h_name << "\n";
else
std::cout << "lookup failed: " << status << '\n';
}
void main_loop(ares_channel &channel)
{
int nfds, count;
fd_set readers, writers;
timeval tv, *tvp;
while (1) {
FD_ZERO(&readers);
FD_ZERO(&writers);
nfds = ares_fds(channel, &readers, &writers); //获取ares channel使用的FD
if (nfds == 0)
break;
tvp = ares_timeout(channel, NULL, &tv);
count = select(nfds, &readers, &writers, NULL, tvp); //将ares的SOCKET FD 加入事件循环
ares_process(channel, &readers, &writers); // 有事件发生 交由ares 处理
}
}
int main(int argc, char **argv)
{
struct in_addr ip;
int res;
if(argc < 2 ) {
std::cout << "usage: " << argv[0] << " ip.address\n";
return 1;
}
inet_aton(argv[1], &ip);
ares_channel channel; // 创建一个ares_channel
if((res = ares_init(&channel)) != ARES_SUCCESS) { // ares 对channel 进行初始化
std::cout << "ares feiled: " << res << '\n';
return 1;
}
ares_gethostbyaddr(channel, &ip, sizeof ip, AF_INET, dns_callback, NULL); //传递给c-ares channal 和 回调
main_loop(channel); //主程序事件循环
return 0;
}
如果有需要可以采用c-ares实现一个自己的DNS异步解析的Server,做成独立的dns解析服务
转载请注明出处~~谢谢