How do I build a DNS Query record in Erlang?
Asked Answered
M

3

6

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> 
Mcgannon answered 16/11, 2009 at 5:47 Comment(0)
M
5

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).
Mcgannon answered 16/11, 2009 at 21:39 Comment(0)
L
1

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

Lablab answered 16/11, 2009 at 20:58 Comment(1)
You should project this in context of 20+ years ago where bits were much more expensive than today.Pharsalus
M
1

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.

Manus answered 16/11, 2009 at 22:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.