Skip to content

Instantly share code, notes, and snippets.

@jarrodhroberson
Created February 14, 2014 03:24
Show Gist options
  • Save jarrodhroberson/8995269 to your computer and use it in GitHub Desktop.
Save jarrodhroberson/8995269 to your computer and use it in GitHub Desktop.
Bonjour / Zeroconf In Erlang ( a start at least )
-module(zeroconf).
-include("zeroconf.hrl").
-export([open/0,start/0]).
-export([stop/1,receiver/0]).
-export([send/1]).
-define(ADDR, {224,0,0,251}).
-define(PORT, 5353).
open() ->
{ok,S} = gen_udp:open(?PORT,[{reuseaddr,true}, {ip,?ADDR}, {multicast_ttl,4}, {multicast_loop,false}, binary]),
inet:setopts(S,[{add_membership,{?ADDR,{0,0,0,0}}}]),
S.
close(S) -> gen_udp:close(S).
start() ->
S=open(),
Pid=spawn(?MODULE,receiver,[]),
gen_udp:controlling_process(S,Pid),
{S,Pid}.
stop({S,Pid}) ->
close(S),
Pid ! stop.
receiver() ->
receive
{udp, _Socket, IP, InPortNo, Packet} ->
io:format("~n~nFrom: ~p~nPort: ~p~nData: ~p~n",[IP,InPortNo,inet_dns:decode(Packet)]),
receiver();
stop -> true;
AnythingElse -> io:format("RECEIVED: ~p~n",[AnythingElse]),
receiver()
end.
-record(dns_header,
{
id = 0, %% ushort query identification number
%% byte F0
qr = 0, %% :1 response flag
opcode = 0, %% :4 purpose of message
aa = 0, %% :1 authoritive answer
tc = 0, %% :1 truncated message
rd = 0, %% :1 recursion desired
%% byte F1
ra = 0, %% :1 recursion available
pr = 0, %% :1 primary server required (non standard)
%% :2 unused bits
rcode = 0 %% :4 response code
}).
-record(dns_rec,
{
header, %% dns_header record
qdlist = [], %% list of question entries
anlist = [], %% list of answer entries
nslist = [], %% list of authority entries
arlist = [] %% list of resource entries
}).
-record(dns_query,
{
domain, %% query domain
type, %% query type
class %% query class
}).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment