//build1: g++ dns_parse_cares.cc /usr/local/lib/libcares.a -lrt -o test
//build2: g++ dns_parse_cares.cc -lcares -lrt -o test
#include <time.h>
#include <iostream>
#include <netdb.h>
#include <arpa/inet.h>
#include <ares.h>
#include <sys/select.h>
#include <string.h>
#define IP_LEN 32
typedef struct {
char host[64];
char ip[5][IP_LEN];
int count;
}IpList;
void dns_callback (void* arg, int status, int timeouts, struct hostent* hptr) //ares 处理完成,返回DNS解析的信息
{
IpList *ips = (IpList*)arg;
if( ips == NULL ) return;
if(status == ARES_SUCCESS){
strncpy(ips->host, hptr->h_name, sizeof(ips->host));
char **pptr=hptr->h_addr_list;
for(int i=0; *pptr!=NULL && i<5; pptr++,++i){
inet_ntop(hptr->h_addrtype, *pptr, ips->ip[ips->count++], IP_LEN);
}
}else{
std::cout << "lookup failed: " << status << std::endl;
c-ares DNS域名异步解析成IP
最新推荐文章于 2025-06-26 09:21:35 发布