Low level multicast-\/dns implementation in pure javascript


\begin{DoxyCode}{0}
\DoxyCodeLine{npm install multicast-\/dns }

\end{DoxyCode}


\href{http://travis-ci.org/mafintosh/multicast-dns}{\texttt{ }}\hypertarget{md_sfcode_node_modules_multicast_dns_README_autotoc_md17864}{}\doxysection{Usage}\label{md_sfcode_node_modules_multicast_dns_README_autotoc_md17864}

\begin{DoxyCode}{0}
\DoxyCodeLine{var mdns = require('multicast-\/dns')()}
\DoxyCodeLine{}
\DoxyCodeLine{mdns.on('response', function(response) \{}
\DoxyCodeLine{  console.log('got a response packet:', response)}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{mdns.on('query', function(query) \{}
\DoxyCodeLine{  console.log('got a query packet:', query)}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{// lets query for an A record for 'brunhilde.local'}
\DoxyCodeLine{mdns.query(\{}
\DoxyCodeLine{  questions:[\{}
\DoxyCodeLine{    name: 'brunhilde.local',}
\DoxyCodeLine{    type: 'A'}
\DoxyCodeLine{  \}]}
\DoxyCodeLine{\}) }

\end{DoxyCode}


Running the above (change {\ttfamily brunhilde.\+local} to {\ttfamily your-\/own-\/hostname.\+local}) will print an echo of the query packet first


\begin{DoxyCode}{0}
\DoxyCodeLine{got a query packet: \{ type: 'query',}
\DoxyCodeLine{  questions: [ \{ name: 'brunhilde.local', type: 'A', class: 1 \} ],}
\DoxyCodeLine{  answers: [],}
\DoxyCodeLine{  authorities: [],}
\DoxyCodeLine{  additionals: [] \} }

\end{DoxyCode}


And then a response packet


\begin{DoxyCode}{0}
\DoxyCodeLine{got a response packet: \{ type: 'response',}
\DoxyCodeLine{  questions: [],}
\DoxyCodeLine{  answers:}
\DoxyCodeLine{   [ \{ name: 'brunhilde.local',}
\DoxyCodeLine{       type: 'A',}
\DoxyCodeLine{       class: 1,}
\DoxyCodeLine{       ttl: 120,}
\DoxyCodeLine{       flush: true,}
\DoxyCodeLine{       data: '192.168.1.5' \} ],}
\DoxyCodeLine{  authorities: [],}
\DoxyCodeLine{  additionals:}
\DoxyCodeLine{   [ \{ name: 'brunhilde.local',}
\DoxyCodeLine{       type: 'A',}
\DoxyCodeLine{       class: 1,}
\DoxyCodeLine{       ttl: 120,}
\DoxyCodeLine{       flush: true,}
\DoxyCodeLine{       data: '192.168.1.5' \},}
\DoxyCodeLine{     \{ name: 'brunhilde.local',}
\DoxyCodeLine{       type: 'AAAA',}
\DoxyCodeLine{       class: 1,}
\DoxyCodeLine{       ttl: 120,}
\DoxyCodeLine{       flush: true,}
\DoxyCodeLine{       data: 'fe80::5ef9:38ff:fe8c:ceaa' \} ] \} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_multicast_dns_README_autotoc_md17865}{}\doxysection{CLI}\label{md_sfcode_node_modules_multicast_dns_README_autotoc_md17865}

\begin{DoxyCode}{0}
\DoxyCodeLine{npm install -\/g multicast-\/dns }

\end{DoxyCode}



\begin{DoxyCode}{0}
\DoxyCodeLine{multicast-\/dns brunhilde.local}
\DoxyCodeLine{> 192.168.1.1 }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_multicast_dns_README_autotoc_md17866}{}\doxysection{API}\label{md_sfcode_node_modules_multicast_dns_README_autotoc_md17866}
A packet has the following format


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  questions: [\{}
\DoxyCodeLine{    name:'brunhilde.local',}
\DoxyCodeLine{    type:'A'}
\DoxyCodeLine{  \}],}
\DoxyCodeLine{  answers: [\{}
\DoxyCodeLine{    name:'brunhilde.local',}
\DoxyCodeLine{    type:'A',}
\DoxyCodeLine{    ttl:seconds,}
\DoxyCodeLine{    data:(record type specific data)}
\DoxyCodeLine{  \}],}
\DoxyCodeLine{  additionals: [}
\DoxyCodeLine{    (same format as answers)}
\DoxyCodeLine{  ],}
\DoxyCodeLine{  authorities: [}
\DoxyCodeLine{    (same format as answers)}
\DoxyCodeLine{  ]}
\DoxyCodeLine{\} }

\end{DoxyCode}


Currently data from {\ttfamily SRV}, {\ttfamily A}, {\ttfamily PTR}, {\ttfamily TXT}, {\ttfamily AAAA} and {\ttfamily HINFO} records is passed\hypertarget{md_sfcode_node_modules_multicast_dns_README_autotoc_md17867}{}\doxyparagraph{$<$tt$>$mdns = multicastdns(\mbox{[}options\mbox{]})$<$/tt$>$}\label{md_sfcode_node_modules_multicast_dns_README_autotoc_md17867}
Creates a new {\ttfamily mdns} instance. Options can contain the following


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  multicast: true // use udp multicasting}
\DoxyCodeLine{  interface: '192.168.0.2' // explicitly specify a network interface. defaults to all}
\DoxyCodeLine{  port: 5353, // set the udp port}
\DoxyCodeLine{  ip: '224.0.0.251', // set the udp ip}
\DoxyCodeLine{  ttl: 255, // set the multicast ttl}
\DoxyCodeLine{  loopback: true, // receive your own packets}
\DoxyCodeLine{  reuseAddr: true // set the reuseAddr option when creating the socket (requires node >=0.11.13)}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_multicast_dns_README_autotoc_md17868}{}\doxyparagraph{‘mdns.\+on(\textquotesingle{}query’, (packet, rinfo))\`{}}\label{md_sfcode_node_modules_multicast_dns_README_autotoc_md17868}
Emitted when a query packet is received.


\begin{DoxyCode}{0}
\DoxyCodeLine{mdns.on('query', function(query) \{}
\DoxyCodeLine{  if (query.questions[0] \&\& query.questions[0].name === 'brunhilde.local') \{}
\DoxyCodeLine{    mdns.respond(someResponse) // see below}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\}) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_multicast_dns_README_autotoc_md17869}{}\doxyparagraph{‘mdns.\+on(\textquotesingle{}response’, (packet, rinfo))\`{}}\label{md_sfcode_node_modules_multicast_dns_README_autotoc_md17869}
Emitted when a response packet is received.

The response might not be a response to a query you send as this is the result of someone multicasting a response.\hypertarget{md_sfcode_node_modules_multicast_dns_README_autotoc_md17870}{}\doxyparagraph{$<$tt$>$mdns.\+query(packet, \mbox{[}cb\mbox{]})$<$/tt$>$}\label{md_sfcode_node_modules_multicast_dns_README_autotoc_md17870}
Send a dns query. The callback will be called when the packet was sent.

The following shorthands are equivalent


\begin{DoxyCode}{0}
\DoxyCodeLine{mdns.query('brunhilde.local', 'A')}
\DoxyCodeLine{mdns.query([\{name:'brunhilde.local', type:'A'\}])}
\DoxyCodeLine{mdns.query(\{}
\DoxyCodeLine{  questions: [\{name:'brunhilde.local', type:'A'\}]}
\DoxyCodeLine{\}) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_multicast_dns_README_autotoc_md17871}{}\doxyparagraph{$<$tt$>$mdns.\+respond(packet, \mbox{[}cb\mbox{]})$<$/tt$>$}\label{md_sfcode_node_modules_multicast_dns_README_autotoc_md17871}
Send a dns response. The callback will be called when the packet was sent.


\begin{DoxyCode}{0}
\DoxyCodeLine{// reply with a SRV and a A record as an answer}
\DoxyCodeLine{mdns.respond(\{}
\DoxyCodeLine{  answers: [\{}
\DoxyCodeLine{    name: 'my-\/service',}
\DoxyCodeLine{    type: 'SRV',}
\DoxyCodeLine{    data: \{}
\DoxyCodeLine{      port:9999,}
\DoxyCodeLine{      weigth: 0,}
\DoxyCodeLine{      priority: 10,}
\DoxyCodeLine{      target: 'my-\/service.example.com'}
\DoxyCodeLine{    \}}
\DoxyCodeLine{  \}, \{}
\DoxyCodeLine{    name: 'brunhilde.local',}
\DoxyCodeLine{    type: 'A',}
\DoxyCodeLine{    ttl: 300,}
\DoxyCodeLine{    data: '192.168.1.5'}
\DoxyCodeLine{  \}]}
\DoxyCodeLine{\}) }

\end{DoxyCode}


The following shorthands are equivalent


\begin{DoxyCode}{0}
\DoxyCodeLine{mdns.respond([\{name:'brunhilde.local', type:'A', data:'192.158.1.5'\}])}
\DoxyCodeLine{mdns.respond(\{}
\DoxyCodeLine{  answers: [\{name:'brunhilde.local', type:'A', data:'192.158.1.5'\}]}
\DoxyCodeLine{\}) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_multicast_dns_README_autotoc_md17872}{}\doxyparagraph{$<$tt$>$mdns.\+destroy()$<$/tt$>$}\label{md_sfcode_node_modules_multicast_dns_README_autotoc_md17872}
Destroy the mdns instance. Closes the udp socket.\hypertarget{md_sfcode_node_modules_multicast_dns_README_autotoc_md17873}{}\doxysection{Development}\label{md_sfcode_node_modules_multicast_dns_README_autotoc_md17873}
To start hacking on this module you can use this example to get started


\begin{DoxyCode}{0}
\DoxyCodeLine{git clone git://github.com/mafintosh/multicast-\/dns.git}
\DoxyCodeLine{npm install}
\DoxyCodeLine{node example.js}
\DoxyCodeLine{node cli.js \$(hostname).local }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_multicast_dns_README_autotoc_md17874}{}\doxysubsection{License}\label{md_sfcode_node_modules_multicast_dns_README_autotoc_md17874}
MIT 