gai_cancel()需要很长时间才能成功

时间:2019-06-12 01:59:42

标签: c++ linux dns getaddrinfo getaddrinfo-a

我正在尝试在c ++中异步查找域。原因是我希望能够有效地增加超时期限,以防系统无法查找域。我遇到了getaddrinfo_a()命令,因此决定尝试一下。但是,取消任何无法成功查找的dns(例如,当没有Internet连接时)在我的计算机上花费的时间绝不会少于20秒。这是一个简单的例子:

#include <iostream>
#include <netdb.h>
#include <string.h>
#include <unistd.h>

using namespace std;

int main() {


        int ret;
        gaicb* reqs;

        reqs = new gaicb;

        memset(reqs, 0, sizeof (gaicb));
        reqs->ar_name = "google.com";

        ret = getaddrinfo_a(GAI_NOWAIT, &reqs, 1, NULL);
        if (ret != 0) {
                cout << "something went wrong" << endl;
                return false;
        }

        while (1) {
                ret = gai_cancel(reqs);
                if (ret == EAI_CANCELED || ret == EAI_ALLDONE) {
                        break;
                }
                usleep(100 * 1000); //sleep for 100 milliseconds
        }

        cout << "finished cancellation" << endl;


        return 0;
}

像这样编译:

g++ -o main main.cpp -lanl

然后在没有Internet连接的情况下在基于linux的系统上运行命令,

time ./main

您会发现该程序总是需要20秒钟才能关闭。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

好的答案是,如果您需要少于20秒的异步dns查找,请不要使用getaddrinfo_a(3)。同样,根据Martin Sustrik对http://sourceware-org.1504.n7.nabble.com/getaddrinfo-a-memory-leaks-td233794.html的第二条评论以及我在这里的经验,getaddrinfo_a似乎是实验性的,无论如何都不应使用。我实际上最终使用了dns.c(https://github.com/wahern/dns)。这是寻找者的示例:

#include "dns.c"

uint8_t getaddrinfo_k(
uint32_t* ip_addr, const char* dns_address, uint32_t timeout) {
    int32_t                 rc;
    struct addrinfo         hints;
    struct dns_resolv_conf* dns_conf;
    struct dns_hosts*       dns_hosts;
    struct dns_hints*       dns_hints;
    struct dns_resolver*    resolver;
    struct dns_addrinfo*    ai;
    struct addrinfo*        it;

    dns_conf    = dns_resconf_local(&rc);
    assert(dns_conf);
    dns_hosts   = dns_hosts_local(&rc);
    assert(dns_hosts);
    dns_hints   = dns_hints_local(dns_conf, &rc);
    assert(dns_hints);
    resolver    = dns_res_open(
    dns_conf, dns_hosts, dns_hints, NULL, dns_opts(), &rc);
    assert(resolver);
    it          = NULL;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = PF_INET;

    ai = dns_ai_open(dns_address, "80", DNS_T_A, &hints, resolver, &rc);
    assert(ai);

    while (timeout-- > 0) {
        rc = dns_ai_nextent(&it, ai);
        switch(rc) {
                case 0:
                        *ip_addr = (
                        (struct sockaddr_in *) it->ai_addr)->sin_addr.s_addr;
                        free(it);
                        goto exit_loop;
                case EAGAIN:
                        rc = dns_ai_poll(ai, 1);
                        assert(rc == 0);
                        break;
                default:
                        goto exit_loop;
        }
        usleep(100 * 1000);
    }

    exit_loop:

    dns_ai_close        (ai);
    dns_res_close       (resolver);
    dns_hints_close     (dns_hints);
    dns_hosts_close     (dns_hosts);
    dns_resconf_close   (dns_conf);

    switch(rc) {
        case 0:
            return 1;
        case EAGAIN:
            printf("DNS_WRAPPER: timed out\n");
            break;
        case ENOENT:
            printf("DNS_WRAPPER: file doesn't exist\n");
            break;
        default:
            printf("DNS_WRAPPER: unknown error\n");
            break;
    }

    return 0;
}