背景
我正在为 Haskell 开发一个跨平台的 Zeroconf/Bonjour/DNS-SD 库,并认为我最好的选择是针对dns_sd.h
API。在 Linux 下,此接口的实现由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
问题
- Avahi
dns_sd
兼容层仍然是跨平台绑定的合适目标吗?或者,关于使用本机 Avahi API 的警告消息是否足够严重,我应该考虑重新定位? - C 中跨平台 Zeroconf 的最新技术是什么?