如何在 Erlang 中构建 DNS 查询记录?

发布于 2024-08-11 09:50:33 字数 525 浏览 8 评论 0原文

我正在构建一个本机 Bonjour / Zeroconf 库,并且需要构建 DNS 查询记录以广播到其他计算机。我尝试过查看 Erlang 源代码,但由于我对 Erlang 比较陌生,它在所有 inet_XXX.erl 和 .hrl 文件的内部变得有点密集。我有一个用于接收和解析 DNS 记录有效负载的侦听器,但我只是不知道如何创建查询记录。我真正需要知道的是我需要传递给 inet_dns:encode() 以获得可以发送的二进制文件。这就是我正在尝试做的事情。

{ok,P} = inet_dns:encode(#dns_query{domain="_daap._tcp.local",type=ptr,class=in})

这是我收到的错误

10> test:send().
** exception error: {badrecord,dns_rec}
     in function  inet_dns:encode/1
     in call from test:send/0
11> 

I am building a native Bonjour / Zeroconf library and need to build DNS query records to broadcast off to the other machines. I have tried looking thru the Erlang source code but as I am relatively new to Erlang it gets kind of dense down the bowels of all the inet_XXX.erl and .hrl files. I have a listener that works for receiving and parsing the DNS record payloads, I just can't figure out how to create the query records. What I really need to know is what I need to pass into inet_dns:encode() to get a binary I can send out. Here is what I am trying to do.

{ok,P} = inet_dns:encode(#dns_query{domain="_daap._tcp.local",type=ptr,class=in})

here is the error I am getting

10> test:send().
** exception error: {badrecord,dns_rec}
     in function  inet_dns:encode/1
     in call from test:send/0
11> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

≈。彩虹 2024-08-18 09:50:33

我终于想通了。

send(Domain) ->
    {ok,S} = gen_udp:open(5555,[{reuseaddr,true}, {ip,{224,0,0,251}}, {multicast_ttl,4}, {multicast_loop,false}, {broadcast,true}, binary]),
    P = #dns_rec{header=#dns_header{},qdlist=[#dns_query{domain=Domain,type=ptr,class=in}]},
    gen_udp:send(S,{224,0,0,251},5353,inet_dns:encode(P)),
    gen_udp:close(S).

I finally figured it out.

send(Domain) ->
    {ok,S} = gen_udp:open(5555,[{reuseaddr,true}, {ip,{224,0,0,251}}, {multicast_ttl,4}, {multicast_loop,false}, {broadcast,true}, binary]),
    P = #dns_rec{header=#dns_header{},qdlist=[#dns_query{domain=Domain,type=ptr,class=in}]},
    gen_udp:send(S,{224,0,0,251},5353,inet_dns:encode(P)),
    gen_udp:close(S).
多情癖 2024-08-18 09:50:33

事实上,没有关于 inet_dns 模块的文档,这会让您在代码中使用它时非常谨慎。我希望您充分意识到,如果他们(OTP 团队)想要更改模块的实现和使用方式,则不会考虑您的项目。

阅读代码了解实现思路,或者直接使用基于 DNS 协议 RFC 的 Erlang 位语法创建 DNS 协议消息。创建 DNS 包比解析它容易得多(我自己也曾沿着这条路走下去,最小化数据包大小的“聪明技巧”似乎不值得)。

The fact that there is no documentation for the inet_dns module should make you very wary of using it from your code. I hope you are fully aware that no consideration will be taken to your project if they (the OTP team) feel like changing how the module is implemented and used.

Read the code for implementation ideas, or just get down to creating the DNS protocol message using the Erlang bit syntax based on the RFCs on the DNS protocol. Creating a DNS package is much easier than parsing it (I've been down that road myself, and the "clever tricks" to minimize packet size hardly seem worth it).

紅太極 2024-08-18 09:50:33

正如 Magnus 在 Erlang 问题邮件列表中所解释的:

http://groups.google.com/group/erlang-programming/browse_thread/thread/ce547dab981219df/47c3ca96b15092e0?show_docid=47c3ca96b15092e0

您在encode/1函数中传递了dns_query而不是dns_rec记录。

As explained by Magnus in the Erlang Questions Mailing list:

http://groups.google.com/group/erlang-programming/browse_thread/thread/ce547dab981219df/47c3ca96b15092e0?show_docid=47c3ca96b15092e0

you were passing a dns_query instead of a dns_rec record in the encode/1 function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文