[erlang-questions] Encoding a DNS Query correctly

145 views
Skip to first unread message

Jarrod Roberson

unread,
Nov 16, 2009, 2:56:11 PM11/16/09
to Erlang
I am writing a Bonjour/Zeroconf implemenation in native Erlang. This is
basically multicast DNS.
I have the listener and parser for the resulting DNS records working.
What I can't get right is the encoding for sending.

Here is what I am trying:

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

Here is what I am getting in the console when I call :send()

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

If I can get over this hump I will have a basic implementation done.
Anyone have any ideas?

Magnus Henoch

unread,
Nov 16, 2009, 4:11:32 PM11/16/09
to Erlang
Jarrod Roberson <jar...@vertigrated.com> writes:

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

You're passing a dns_query record to inet_dns:encode/1, but it expects a
dns_rec record. (If I knew the DNS protocol by heart, I'd tell you how
to build one, but for now I'll have to refer to inet_dns.hrl.)

--
Magnus Henoch, mag...@erlang-consulting.com
Erlang Training and Consulting
http://www.erlang-consulting.com/

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Andrew Thompson

unread,
Nov 16, 2009, 4:15:04 PM11/16/09
to Erlang

Maybe try adding

-include_lib("kernel/src/inet_dns.hrl").

or something? It looks like you're missing the definition of the dns_rec
record.

Andrew

Jarrod Roberson

unread,
Nov 16, 2009, 5:02:35 PM11/16/09
to Erlang
thanks for the responses I now have it working!

send(Domain) ->


{ok,S} = gen_udp:open(5353,[{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).

Reply all
Reply to author
Forward
0 new messages