为什么此 Avahi 客户端代码无法将 CNAME 别名添加到我的 Linux 计算机?
我正在尝试编写一个小程序,将 mDNS CNAME 别名添加到我的 Linux 设备,以便可以通过多个“something.local”访问它。域名。
该程序的预期功能与 avahi-aliases Python 脚本相同,但为了为了避免 Python 依赖,我尝试用 C++ 来实现它。
我的代码(目前)基于 Avahi 源代码发行版中包含的 client-publish-service.c 示例。当我未更改地运行该示例时,它按预期工作:特别是,我看到“MegaPrinter”显示在我的 Mac 上的 Bonjour 浏览器等中。
我的下一步是修改示例代码以添加 CNAME 记录,而不是添加服务。所以我 #ifdef 删除了 avahi_entry_group_add_service() 调用并将其放入:
const int TTL = 60;
char rdata[] = "\0msli-10135114\0local"; // "msli10135114.local." is the device's normal FQDN, which I want to make aliases to
rdata[0] = 13;
rdata[14] = 5;
printf("rdata=[%s] _moduleName=[%s]\n", rdata, _moduleName);
printf("add_record: %s\n", avahi_strerror(avahi_entry_group_add_record (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, "TestX", 0x01, 0x10, 120, "\5booya", 6)));
if ((ret = avahi_entry_group_add_record(
group, //AvahiEntryGroup *group,
AVAHI_IF_UNSPEC, //AvahiIfIndex interface,
AVAHI_PROTO_UNSPEC, //AvahiProtocol protocol,
(AvahiPublishFlags)0, //AvahiPublishFlags flags,
_moduleName, //const char *name,
AVAHI_DNS_CLASS_IN, //uint16_t clazz,
AVAHI_DNS_TYPE_CNAME, //uint16_t type,
TTL, //uint32_t ttl,
rdata, //const void *rdata,
sizeof(rdata) //size_t size
)) < 0)
{
if (ret == AVAHI_ERR_COLLISION) goto collision;
fprintf(stderr, "Failed to add module record: %s\n", avahi_strerror(ret));
goto fail;
}
.... 但它不起作用;特别是,运行例程只给我这个输出:
msli-10135114local] _moduleName=[Wild-Tracks-1]
add_record: Not supported
Failed to add module record: Not supported
奇怪的是,不仅我自己对 avahi_entry_group_add_record() 的调用失败并显示代码 AVAHI_ERR_NOT_SUPPORTED,而且我的一次性测试调用(在 printf("add_record") 内)也失败失败并显示相同的错误代码。但是该调用是从 Avahi 附带的 avahi-client/client-test.c 文件中逐字复制的,因此看起来它应该是一个有效的调用。
谁能建议为什么这些调用可能会失败,或者我在这里做错了什么?
如果有帮助,测试的完整源代码位于此处。
I'm trying to write a little program that will add mDNS CNAME aliases to my Linux device, so that it can be accessed via more than one "something.local." domain name.
This program's intended function is the same as the avahi-aliases Python script, but in order to avoid a Python dependency, I'm trying to implement it in C++ instead.
I've based my code (for now) on the client-publish-service.c example that is included in the Avahi source distribution. When I run that example unchanged, it works as expected: in particular, I see "MegaPrinter" show up in Bonjour Browser on my Mac, etc.
My next step is to modify the example code to add a CNAME record instead of adding services. So I #ifdef out the avahi_entry_group_add_service() calls and put this in instead:
const int TTL = 60;
char rdata[] = "\0msli-10135114\0local"; // "msli10135114.local." is the device's normal FQDN, which I want to make aliases to
rdata[0] = 13;
rdata[14] = 5;
printf("rdata=[%s] _moduleName=[%s]\n", rdata, _moduleName);
printf("add_record: %s\n", avahi_strerror(avahi_entry_group_add_record (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, "TestX", 0x01, 0x10, 120, "\5booya", 6)));
if ((ret = avahi_entry_group_add_record(
group, //AvahiEntryGroup *group,
AVAHI_IF_UNSPEC, //AvahiIfIndex interface,
AVAHI_PROTO_UNSPEC, //AvahiProtocol protocol,
(AvahiPublishFlags)0, //AvahiPublishFlags flags,
_moduleName, //const char *name,
AVAHI_DNS_CLASS_IN, //uint16_t clazz,
AVAHI_DNS_TYPE_CNAME, //uint16_t type,
TTL, //uint32_t ttl,
rdata, //const void *rdata,
sizeof(rdata) //size_t size
)) < 0)
{
if (ret == AVAHI_ERR_COLLISION) goto collision;
fprintf(stderr, "Failed to add module record: %s\n", avahi_strerror(ret));
goto fail;
}
.... but it doesn't work; in particular, running the routine only gives me this output:
msli-10135114local] _moduleName=[Wild-Tracks-1]
add_record: Not supported
Failed to add module record: Not supported
What's strange is, not only does my own call to avahi_entry_group_add_record() fail with code AVAHI_ERR_NOT_SUPPORTED, but also my throw-away test call (inside the printf("add_record")) also fails with the same error code. But that call is copied verbatim out of the avahi-client/client-test.c file that comes with Avahi, so it seems like it should be a valid call.
Can anyone suggest why these calls might be failing, or what I am doing wrong here?
In case it helps, the complete source code for the test is here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于弄清楚了这一点...问题是 avahi_entry_group_add_record 的 (AvahiPublishFlags) 参数需要包含 AVAHI_PUBLISH_USE_MULTICAST 位,而不仅仅是零。奇怪的是,我用作示例的 Python 脚本不包含该位。
无论如何,源代码的工作版本位于此处,以防万一有人有兴趣。
I finally figured this one out... the problem was that the (AvahiPublishFlags) argument to avahi_entry_group_add_record needed to include the AVAHI_PUBLISH_USE_MULTICAST bit, not just be zero. Oddly enough, the Python script I used as an example didn't include that bit.
In any case, a working version of the source code is here, in case anyone is interested.