gpt4 book ai didi

c - Avahi dns_sd 兼容层无法运行浏览回调

转载 作者:太空狗 更新时间:2023-10-29 15:04:52 24 4
gpt4 key购买 nike

背景

我正在为 Haskell 开发一个跨平台的 Zeroconf/Bonjour/DNS-SD 库,我认为我最好的选择是针对 dns_sd.h应用程序接口(interface)。在Linux下,这个接口(interface)的实现由Avahi提供。 ,它声称支持 Bonjour API 的一个子集。

问题

作为对我的库的健全性检查,我用 C 编写了一个小测试程序,它只使用 API 的基本框架。它浏览类型为 _http._tcp 的网络上的任何服务,一看到就打印一条消息,然后结束:

#include <dns_sd.h>
#include <stdio.h>
#include <stdlib.h>

void cb(DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char *serviceName,
const char *regtype,
const char *replyDomain,
void *context) {
printf("called!\n");
}

int main() {
DNSServiceRef sd = malloc(sizeof(DNSServiceRef));
const char *regtype = "_http._tcp";
DNSServiceErrorType err1 = DNSServiceBrowse(&sd, 0, 0, regtype, NULL, &cb, NULL);
printf("err1=%d\n", err1);
DNSServiceErrorType err2 = DNSServiceProcessResult(sd);
printf("err2=%d\n", err2);
return 0;
}

在我的 Mac 上,这个测试程序在 C 和等效的 Haskell 中都运行良好(它找到了我的打印机;令人兴奋!):

$ gcc test.c -o test
$ ./test
err1=0
called!
err2=0

但是在我的 Linux 机器上,程序在退出前斥责我,但没有调用回调:

$ gcc test.c -o test -ldns_sd
$ ./test
*** WARNING *** The program 'test' uses the Apple Bonjour compatibility layer of Avahi.
*** WARNING *** Please fix your application to use the native API of Avahi!
*** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=test>
err1=0
err2=0

问题

  1. Ava​​hi dns_sd 兼容层是否仍然是跨平台绑定(bind)的合适目标?或者关于使用 native Avahi API 的警告消息是否足够严重,我应该考虑重定向?
  2. C 语言跨平台 Zeroconf 的最新技术水平如何?

最佳答案

由于我不知道的原因,它只适用于非阻塞调用。下面是改进后的代码。 Avahi 的socket 设置为非阻塞模式,然后使用select (3) 等待可用数据。每次套接字上有数据时都必须调用 DNSServiceProcessResult(sd),因此您的示例在其他平台上运行可能纯属幸运。

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dns_sd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

static int set_nonblocking(int fd)
{
int flags;
/* If they have O_NONBLOCK, use the Posix way to do it */
#if defined(O_NONBLOCK)
/* Fixme: O_NONBLOCK is defined but broken on SunOS 4.1.x and AIX 3.2.5. */
if (-1 == (flags = fcntl(fd, F_GETFL, 0)))
flags = 0;
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
#else
/* Otherwise, use the old way of doing it */
flags = 1;
return ioctl(fd, FIOBIO, &flags);
#endif
}

void cb(DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char *serviceName,
const char *regtype,
const char *replyDomain,
void *context) {
printf("called %s %s!\n", serviceName, regtype);
}

int main() {
DNSServiceRef sd = malloc(sizeof(DNSServiceRef));
const char *regtype = "_http._tcp";
DNSServiceErrorType err1 = DNSServiceBrowse(&sd, 0, 0, regtype, NULL, &cb, NULL);
printf("err1=%d\n", err1);
int socket = DNSServiceRefSockFD(sd);
set_nonblocking(socket);

fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(socket, &read_fds);

while(1) {
if(select(socket+1, &read_fds, NULL, NULL, NULL) < 0) {
perror("select");
}
DNSServiceErrorType err2 = DNSServiceProcessResult(sd);
printf("err2=%d\n", err2);
if(err2 != 0)
return 2;
}
return 0;
}

关于c - Avahi dns_sd 兼容层无法运行浏览回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7391079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com