如何使用getaddrinfo_a与glibc进行异步解析

时间:2020-03-05 18:51:58  来源:igfitidea点击:

一个经常被忽略的函数,不需要外部库,但是基本上没有任何文档。

解决方案

回答

更新(2010-10-11):Linux手册页现在包含getaddrinfo_a的文档,我们可以在这里找到它:http://www.kernel.org/doc/man-pages/online/pages/man3/getaddrinfo_a .3.html

作为免责声明,我应该补充一点,我是C语言的新手,但不是一个新手,因此可能存在错误或者不良的编码习惯,请纠正我的意思(我的语法也很糟糕)。

直到我接触了Adam Langley的这篇文章时,我个人才知道它,我将给出一些代码片段来说明它的用法,并阐明一些初次使用时可能不清楚的东西。使用此方法的好处是,我们可以取回易于在socket(),listen()和其他函数中使用的数据,并且如果操作正确,也不必担心ipv4 / v6.
因此,从上面的链接开始,从基础开始(我们将需要针对libanl(-lanl)进行链接):
这是函数原型:

int getaddrinfo_a(int mode, struct gaicb *list[], int ent, 
                  struct sigevent *);
  • 该模式是GAI_WAIT(可能不是我们想要的)和GAI_NOWAIT用于异步查找
  • gaicb参数接受要查找的主机数组,而ent参数指定该数组具有多少个元素
  • sigevent将负责告诉函数如何通知我们,稍后将对此进行详细说明

gaicb结构如下所示:

struct gaicb {
    const char *ar_name;
    const char *ar_service;
    const struct addrinfo *ar_request;
    struct addrinfo *ar_result;
};

如果我们熟悉getaddrinfo,则这些字段与它们相对应,如下所示:

int getaddrinfo(const char *node, const char *service,
                const struct addrinfo *hints,
                struct addrinfo **res);

节点是ar_name字段,服务是端口,hints参数对应于ar_request成员,结果存储在其余部分中。
现在,我们指定希望如何通过sigevent结构得到通知:

struct sigevent {
    sigval_t sigev_value;
    int sigev_signo;
    int sigev_notify;
    void (*sigev_notify_function) (sigval_t);
    pthread_addr_t *sigev_notify_attributes;
};
  • 我们可以通过将_sigev_notify_设置为SIGEV_NONE来忽略通知
  • 我们可以通过将sigev_notify设置为SIGEV_SIGNAL并将sigev_signo设置为所需信号来触发信号。请注意,在使用实时信号(SIGRTMIN-SIGRTMAX,请始终通过宏和添加SIGRTMIN + 2等使用它)时,我们可以在sigev_value.sival_ptr或者sigev_value.sival_int成员尊重中传递指针或者值
  • 我们可以通过将sigev_notify设置为SIGEV_NONE在新线程中请求回调

因此,基本上,如果要查找主机名,则将ar_name设置为主机,并将其他所有设置为NULL,如果要连接到主机,则设置ar_name和ar_service,如果要创建服务器,请指定ar_service和ar_result字段。我们当然可以根据自己的喜好自定义ar_request成员,请查看man getaddrinfo以获取更多信息。

如果事件循环带有select / poll / epoll / kqueue,则可能需要使用signalfd来方便使用。 Signalfd创建一个文件描述符,我们可以在其上使用常规的事件轮询机制,如下所示:

#define _GNU_SOURCE //yes this will not be so standardish
#include <netdb.h>
#include <signal.h>
#include <sys/signalfd.h>

void signalfd_setup(void) {
    int sfd;
    sigset_t mask;

    sigemptyset(&mask);
    sigaddset(&mask, SIGRTMIN);
    sigprocmask(SIG_BLOCK, &mask, NULL); //we block the signal
    sfd = signalfd(-1, &mask, 0);
    //add it to the event queue
}
void signalfd_read(int fd) {
    ssize_t s;
    struct signalfd_siginfo fdsi;
    struct gaicb *host;

    while((s = read(fd, &fdsi, sizeof(struct signalfd_siginfo))) > 0){
    if (s != sizeof(struct signalfd_siginfo)) return; //thats bad
    host = fdsi.ssi_ptr; //the pointer passed to the sigevent structure
            //the result is in the host->ar_result member
            create_server(host);
    }
}
void create_server(struct gaicb *host) {
    struct addrinfo *rp, *result;
    int fd;

    result = host->ar_result;
    for(rp = result; rp != NULL; rp = rp->ai_next) {
        fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        bind(fd, rp->ai_addr, rp->ai_addrlen);
        listen(fd, SOMAXCONN);
        //error checks are missing!

        freeaddrinfo(host->ar_request);
        freeaddrinfo(result);
        //you should free everything you put into the gaicb
    }
}
int main(int argc, char *argv[]) {
    struct gaicb *host;
    struct addrinfo *hints;
    struct sigevent sig;

    host = calloc(1, sizeof(struct gaicb));
    hints = calloc(1, sizeof(struct addrinfo));

    hints->ai_family = AF_UNSPEC; //we dont care if its v4 or v6
    hints->ai_socktype = SOCK_STREAM;
    hints->ai_flags = AI_PASSIVE;
    //every other field is NULL-d by calloc

    host->ar_service = "8888"; //the port we will listen on
    host->ar_request = hints;

    sig.sigev_notify = SIGEV_SIGNAL;
    sig.sigev_value.sival_ptr = host;
    sig.sigev_signo = SIGRTMIN;

    getaddrinfo_a(GAI_NOWAIT, &host, 1, &sig);

    signalfd_setup();

    //start your event loop
    return 0;
}

当然,我们也可以使用简单的信号处理程序来完成这项工作,有关更多信息,请参见man sigaction。