English 中文(简体)
How do I build a DNS Query record in Erlang?
原标题:

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> 
最佳回答

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).
问题回答

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).

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.





相关问题
How do I build a DNS Query record in Erlang?

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 ...

powerdns-on-rails ArgumentError

My Environments: CentOS 5 ruby 1.8.6 (2008-08-11 patchlevel 287) [x86_64-linux] Ruby Enterprise Edition 20090610 passneger or webrick I use this gem list. *** LOCAL GEMS *** actionmailer (2.3.2, 2....

简讯

我是否可以使用某些软件来建立简便的航天国家服务器,最好是在 Java? 所有我都希望我的航天国家服务机在任何要求中都用同样的IP地址来回答。

Domains & Foreward Slash

This is rather difficult to explain so please bear with me. We will be hosting 4 websites on our server and the plan is to have each site sit under its own domain: site-a.com site-b.com sub1.site-b....

Checking if a domain name is registered

How would I go about checking if a domain name is registered? I was thinking about checking if it has a corresponding IP but it doesn t seem to work as well as I had hoped. Is there a solution in ...

How do I setup a reverse DNS entry in Plesk 9.2.2 frontent?

I need to get a reverse DNS entry into my zones file for one of my domains hosted under Plesk 9.2.2. Does anybody have ANY idea how this done? If all else fails I will have to update the zones file ...

热门标签