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

标签 c dns glibc getaddrinfo-a

一个经常被忽视的功能,不需要外部库,但基本上没有任何文档。

最佳答案

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

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

在我遇到 this post 之前我个人并不知道这件事由 Adam Langley 撰写,我将给出一些代码片段来说明它的用法,并澄清一些在第一次使用时可能不太清楚的事情。使用它的好处是您可以轻松取回可用于 socket()listen() 和其他函数的数据,如果操作正确,您就不必担心关于 ipv4/v6。
因此,从上面的链接中获取的基础知识开始(您需要链接到 libanl (-lanl)):
这是函数原型(prototype):

int getaddrinfo_a(int mode, struct gaicb *list[], int ent, 
                  struct sigevent *);
  1. mode 是 GAI_WAIT(这可能不是您想要的)和 GAI_NOWAIT 用于异步查找
  2. gaicb 参数接受要查找的主机数组,ent 参数指定数组有多少元素
  3. 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字段,service是端口,hints参数对应ar_request成员,结果存放在rest中。
现在您指定您希望如何通过 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;
};
  1. 您可以通过将 _sigev_notify_ 设置为 SIGEV_NONE 来忽略通知
  2. 您可以通过将 sigev_notify 设置为 SIGEV_SIGNAL 并将 sigev_signo 设置为所需信号来触发信号。请注意,在使用实时信号时(SIGRTMIN-SIGRTMAX,始终通过宏和加法 SIGRTMIN+2 等来使用它)您可以在 sigev_value.sival_ptr 或 sigev_value.sival_int 成员 respecivley 中传递指针或值
  3. 您可以通过将 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了解更多信息。

关于c - 如何使用 getaddrinfo_a 与 glibc 进行异步解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58069/

相关文章:

c++ - 同一系统上的两个版本的 glibc

linux - aarch64 的 glibc 版本

c - 如何从c中的null分隔的char数组中读取字符串?

使用数组将 C 代码转换为 MIPS 汇编语言

c - sqrt 仅在参数为非负时定义

python - DNS 查找失败 : address 'your.proxy.com' not found: [Errno -5] No address associated with hostname

ssl - 有什么方法可以在免费的 heroku dyno 上添加免费的 SSL 证书?

c - 简单的C scanf 不起作用?

php - 如何在 PHP 中启用用户自定义域

c - gets() 函数和输入中的 '\0' 零字节