简体   繁体   English

如何使用c-ares将IP解析为主机?

[英]How do I resolve an IP into host using c-ares?

This is what I've done so far. 这就是我到目前为止所做的。 It compiles, but it segfaults when I try to run it. 它编译,但当我尝试运行它时会出现段错误。

#include <iostream>
#include <netdb.h>
#include <arpa/inet.h>
#include <ares.h>

void dns_callback (void* arg, int status, int timeouts, struct hostent* host)
  {
    std::cout << host->h_name << "\n";
  }

int main(int argc, char **argv)
  {
    struct in_addr ip;
    char *arg;
    inet_aton(argv[1], &ip);
    ares_channel channel;
    ares_gethostbyaddr(channel, &ip, 4, AF_INET, dns_callback, arg);
    sleep(15);
    return 0;
  }

You atleast have to initialize the ares_channel before you use it 您至少必须在使用之前初始化 ares_channel

 if(ares_init(&channel) != ARES_SUCCESS) {
   //handle error
  }

You also need an event loop to process events on the ares file descriptors and call ares_process to handle those events (more commonly, you'd integrate this in the event loop of your application) There's nothing magic with ares, it doesn't use threads to do the async processing so simply calling sleep(15); 你还需要一个事件循环来处理ares文件描述符上的事件,并调用ares_process来处理这些事件(更常见的是,你将它集成到应用程序的事件循环中)没有什么神奇的东西,它不使用线程执行异步处理,只需调用sleep(15); doesn't let ares run in the "background" 不让战争在“背景”中运行

Your callback should also inspect the status variable, you can't access host->h_name if the lookup failed. 您的回调还应检查status变量,如果查找失败,则无法访问host->h_name

A full example becomes: 一个完整的例子变成:

#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)
{
    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);
        if (nfds == 0)
          break;
        tvp = ares_timeout(channel, NULL, &tv);
        count = select(nfds, &readers, &writers, NULL, tvp);
        ares_process(channel, &readers, &writers);
     }

}
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;
    if((res = ares_init(&channel)) != ARES_SUCCESS) {
        std::cout << "ares feiled: " << res << '\n';
        return 1;
    }
    ares_gethostbyaddr(channel, &ip, sizeof ip, AF_INET, dns_callback, NULL);
    main_loop(channel);
    return 0;
  }
$ g++ -Wall test_ares.cpp  -lcares
 $ ./a.out 8.8.8.8
google-public-dns-a.google.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在c-ares中获取DNS服务器 - how to get DNS server in c-ares c-ares指定DNS解析的网络接口 - c-ares specifying network interface for the DNS resolves 安装gRPC:ld:警告:找不到选项&#39;-L /的目录 <path> / GRPC /库/选择/ C-顷 - Installing gRPC : ld: warning: directory not found for option '-L/<path>/grpc/libs/opt/c-ares' bitbake grpc交叉编译/配置失败,并出现错误c-ares :: cares引用文件/usr/lib/libcares.so.2.2.0 - bitbake grpc cross compile/configure failing with error c-ares::cares references the file /usr/lib/libcares.so.2.2.0 C++ 从 URL 解析主机 IP 地址 - C++ Resolve a host IP address from a URL 如何打印主机的IP - How I Can Print The IP Of The Host 如何使用shellscalingapi.h解决我的C ++ Windows应用程序中的此链接器错误 - How do I resolve this linker error in my C++ Windows app using shellscalingapi.h 即使使用外部“ C”,如何解决名称重整问题? - How do I resolve issue occurred with name mangling even after using extern “C”? nslookup for C#和C ++使用特定服务器解析主机 - nslookup for C# and C++ to resolve a host using a specific Server 如何从 Windows 套接字 (C++) 获取连接主机的 IP 地址? - How can I get the connected host's IP address from a Windows Socket (C++)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM