如何在 Erlang 中构建 DNS 查询记录?
我正在构建一个本机 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我终于想通了。
I finally figured it out.
事实上,没有关于 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).
正如 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.