【问题标题】:How to use c-ares with epoll?如何将 c-ares 与 epoll 一起使用?
【发布时间】:2022-01-04 13:36:52
【问题描述】:

我有一个使用 c-ares 库调用执行异步 DNS 解析的工作代码。该程序使用select 监控文件描述符,最大FD_SETSIZE 在我的系统上为1024。我想使用更多的文件描述符,所以想重写代码以使用epoll 而不是select

这是我当前程序的基于select 的函数:

static void
wait_ares(ares_channel channel)
{
    struct timeval *tvp, tv;
    fd_set read_fds, write_fds;
    int nfds;

    FD_ZERO(&read_fds);
    FD_ZERO(&write_fds);
    nfds = ares_fds(channel, &read_fds, &write_fds);

    if (nfds > 0) {
        tvp = ares_timeout(channel, NULL, &tv);
        select(nfds, &read_fds, &write_fds, NULL, tvp);
        ares_process(channel, &read_fds, &write_fds);
    }
}

在发布我的问题之前我已经做了一些谷歌搜索,我发现要使用 epoll 来实现它,我不能再使用 ares_fdsares_timeoutares_process,但必须使用 ares_getsock()ares_process_fd() 代替。但除此之外,我不知道如何做到这一点,也找不到任何使用epollc-ares 的示例代码。谁能修改下面提供的代码以使用epoll 而不是select?或者至少给我一些指导让我开始?

#include <ares.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>

#define MAXWAITING 1000 /* Max. number of parallel DNS queries */
#define MAXTRIES      3 /* Max. number of tries per domain */
#define TIMEOUT    3000 /* Max. number of ms for first try */

#define SERVERS    "1.0.0.1,8.8.8.8" /* DNS server to use (Cloudflare & Google) */

static int nwaiting;

static void
state_cb(void *data, int s, int read, int write)
{
    //printf("Change state fd %d read:%d write:%d\n", s, read, write);
}

static void
callback(void *arg, int status, int timeouts, struct hostent *host)
{
    nwaiting--;

    if(!host || status != ARES_SUCCESS){
        fprintf(stderr, "Failed to lookup %s\n", ares_strerror(status));
        return;
    }

    char ip[INET6_ADDRSTRLEN];

    if (host->h_addr_list[0] != NULL){
        inet_ntop(host->h_addrtype, host->h_addr_list[0], ip, sizeof(ip));
        printf("%s\n%s\n", host->h_name, ip);
    }
}

static void
wait_ares(ares_channel channel)
{
    struct timeval *tvp, tv;
    fd_set read_fds, write_fds;
    int nfds;

    FD_ZERO(&read_fds);
    FD_ZERO(&write_fds);
    nfds = ares_fds(channel, &read_fds, &write_fds);

    if (nfds > 0) {
        tvp = ares_timeout(channel, NULL, &tv);
        select(nfds, &read_fds, &write_fds, NULL, tvp);
        ares_process(channel, &read_fds, &write_fds);
    }
}

int
main(int argc, char *argv[])
{
    FILE * fp;
    char domain[128];
    size_t len = 0;
    ssize_t read;
    ares_channel channel;
    int status, done = 0;
    int optmask;
    
    status = ares_library_init(ARES_LIB_INIT_ALL);
    if (status != ARES_SUCCESS) {
        printf("ares_library_init: %s\n", ares_strerror(status));
        return 1;
    }

    struct ares_options options = {
        .timeout = TIMEOUT,     /* set first query timeout */
        .tries = MAXTRIES       /* set max. number of tries */
    };
    optmask = ARES_OPT_TIMEOUTMS | ARES_OPT_TRIES;

    status = ares_init_options(&channel, &options, optmask);
    if (status != ARES_SUCCESS) {
        printf("ares_init_options: %s\n", ares_strerror(status));
        return 1;
    }

    status = ares_set_servers_csv(channel, SERVERS);
    if (status != ARES_SUCCESS) {
        printf("ares_set_servers_csv: %s\n", ares_strerror(status));
        return 1;
    }
    
    
    fp = fopen(argv[1], "r");
    if (!fp)
        exit(EXIT_FAILURE);

    do {
        if (nwaiting >= MAXWAITING || done) {
            do {
                wait_ares(channel);
            } while (nwaiting > MAXWAITING);
        }

        if (!done) {
            if (fscanf(fp, "%127s", domain) == 1) {
                ares_gethostbyname(channel, domain, AF_INET, callback, NULL);
                nwaiting++;
            } else {
                fprintf(stderr, "done sending\n");
                done = 1;
            }
        }
    } while (nwaiting > 0);

    ares_destroy(channel);
    ares_library_cleanup();
    
    fclose(fp);

    return 0;
}

该程序需要一个每行都有一个域名的文件才能工作。

【问题讨论】:

    标签: c epoll c-ares


    【解决方案1】:

    这就是我最终想出的。

    #include <ares.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdarg.h>
    #include <string.h>
    #include <ctype.h>
    #include <unistd.h>
    #include <sys/epoll.h>
    #include <errno.h>
    
    #define MAXWAITING 1000 /* Max. number of parallel DNS queries */
    #define MAXTRIES      3 /* Max. number of tries per domain */
    #define TIMEOUT    3000 /* Max. number of ms for first try */
    #define DNS_MAX_EVENTS 10000
    #define DNS_MAX_SERVERS 2
    
    #define SERVERS    "1.0.0.1,8.8.8.8" /* DNS server to use (Cloudflare & Google) */
    
    static int nwaiting;
    
    ares_socket_t dns_client_fds[ARES_GETSOCK_MAXNUM] = {0};
    struct epoll_event ev, events[DNS_MAX_EVENTS];
    int i,bitmask,nfds, epollfd, timeout, fd_count, ret;
    
    static void
    state_cb(void *data, int s, int read, int write)
    {
        //printf("Change state fd %d read:%d write:%d\n", s, read, write);
    }
    
    static void
    callback(void *arg, int status, int timeouts, struct hostent *host)
    {
        nwaiting--;
    
        if(!host || status != ARES_SUCCESS){
            fprintf(stderr, "Failed to lookup %s\n", ares_strerror(status));
            return;
        }
    
        char ip[INET6_ADDRSTRLEN];
    
        if (host->h_addr_list[0] != NULL){
            inet_ntop(host->h_addrtype, host->h_addr_list[0], ip, sizeof(ip));
            printf("%s\n%s\n", host->h_name, ip);
        }
    }
    
    static void
    wait_ares(ares_channel channel)
    {    
        nfds=0;
        bitmask=0;
        for (i =0; i < DNS_MAX_SERVERS ; i++) {
            if (dns_client_fds[i] > 0) {
                if (epoll_ctl(epollfd, EPOLL_CTL_DEL, dns_client_fds[i], NULL) < 0) {
                    continue;
                }
            }
        }
        memset(dns_client_fds, 0, sizeof(dns_client_fds));
        bitmask = ares_getsock(channel, dns_client_fds, DNS_MAX_SERVERS);
        for (i =0; i < DNS_MAX_SERVERS ; i++) {
           if (dns_client_fds[i] > 0) {
                ev.events = 0;
                if (ARES_GETSOCK_READABLE(bitmask, i)) {
                    ev.events |= EPOLLIN;
                }
                if (ARES_GETSOCK_WRITABLE(bitmask, i)) {
                    ev.events |= EPOLLOUT;
                }
                ev.data.fd = dns_client_fds[i];
                if (epoll_ctl(epollfd, EPOLL_CTL_ADD, dns_client_fds[i], &ev) < 0) {
                    if(errno == EEXIST) {
                        nfds++;
                        continue;
                    }
                    continue;
                }
                nfds++;
            }
        }
        if(nfds==0)
        {
            return;
        }
        timeout = 1000;//millisecs
        fd_count = epoll_wait(epollfd, events, DNS_MAX_EVENTS, timeout);
        if (fd_count < 0) {
            return;
        }
        if (fd_count > 0) {
            for (i = 0; i < fd_count; ++i) {
                ares_process_fd(channel, ((events[i].events) & (EPOLLIN) ? events[i].data.fd:ARES_SOCKET_BAD), ((events[i].events) & (EPOLLOUT)? events[i].data.fd:ARES_SOCKET_BAD));
            }
        } else {
            ares_process_fd(channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
        }   
    }
    
    int
    main(int argc, char *argv[])
    {
        FILE * fp;
        char domain[128];
        size_t len = 0;
        ssize_t read;
        ares_channel channel;
        int status, done = 0;
        int optmask;
        
        status = ares_library_init(ARES_LIB_INIT_ALL);
        if (status != ARES_SUCCESS) {
            printf("ares_library_init: %s\n", ares_strerror(status));
            return 1;
        }
    
        struct ares_options options = {
            .timeout = TIMEOUT,     /* set first query timeout */
            .tries = MAXTRIES       /* set max. number of tries */
        };
        optmask = ARES_OPT_TIMEOUTMS | ARES_OPT_TRIES;
    
        status = ares_init_options(&channel, &options, optmask);
        if (status != ARES_SUCCESS) {
            printf("ares_init_options: %s\n", ares_strerror(status));
            return 1;
        }
    
        status = ares_set_servers_csv(channel, SERVERS);
        if (status != ARES_SUCCESS) {
            printf("ares_set_servers_csv: %s\n", ares_strerror(status));
            return 1;
        }
        
        
        fp = fopen(argv[1], "r");
        if (!fp)
            exit(EXIT_FAILURE);
        memset(dns_client_fds, 0, sizeof(dns_client_fds));
        memset((char *)&ev, 0, sizeof(struct epoll_event));
        memset((char *)&events[0], 0, sizeof(events));
    
        epollfd = epoll_create(DNS_MAX_SERVERS);
        if (epollfd < 0) {
            perror("epoll_create: ");
        }
        
    
        do {
            if (nwaiting >= MAXWAITING || done) {
                do {
                    wait_ares(channel);
                } while (nwaiting > MAXWAITING);
            }
    
            if (!done) {
                if (fscanf(fp, "%127s", domain) == 1) {
                    ares_gethostbyname(channel, domain, AF_INET, callback, NULL);
                    nwaiting++;
                } else {
                    fprintf(stderr, "done sending\n");
                    done = 1;
                }
            }
        } while (nwaiting > 0);
    
        ares_destroy(channel);
        ares_library_cleanup();
        
        fclose(fp);
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-18
      • 2017-06-14
      • 2010-12-09
      • 1970-01-01
      • 2021-12-26
      • 2011-06-01
      • 2011-05-08
      • 2014-06-29
      相关资源
      最近更新 更多