An \href{https://github.com/mafintosh/abstract-encoding}{\texttt{ abstract-\/encoding}} compliant module for encoding / decoding DNS packets. Lifted out of \href{https://github.com/mafintosh/multicast-dns}{\texttt{ multicast-\/dns}} as a separate module.


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

\end{DoxyCode}


\href{https://travis-ci.org/mafintosh/dns-packet}{\texttt{ }}\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8222}{}\doxysection{Usage}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8222}

\begin{DoxyCode}{0}
\DoxyCodeLine{var packet = require('dns-\/packet')}
\DoxyCodeLine{var dgram = require('dgram')}
\DoxyCodeLine{}
\DoxyCodeLine{var socket = dgram.createSocket('udp4')}
\DoxyCodeLine{}
\DoxyCodeLine{var buf = packet.encode(\{}
\DoxyCodeLine{  type: 'query',}
\DoxyCodeLine{  id: 1,}
\DoxyCodeLine{  flags: packet.RECURSION\_DESIRED,}
\DoxyCodeLine{  questions: [\{}
\DoxyCodeLine{    type: 'A',}
\DoxyCodeLine{    name: 'google.com'}
\DoxyCodeLine{  \}]}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{socket.on('message', function (message) \{}
\DoxyCodeLine{  console.log(packet.decode(message)) // prints out a response from google dns}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{socket.send(buf, 0, buf.length, 53, '8.8.8.8') }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8223}{}\doxysection{API}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8223}
\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8224}{}\doxysubsubsection{$<$tt$>$var buf = packets.\+encode(packet, \mbox{[}buf\mbox{]}, \mbox{[}offset\mbox{]})$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8224}
Encodes a DNS packet into a buffer.\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8225}{}\doxysubsubsection{$<$tt$>$var packet = packets.\+decode(buf, \mbox{[}offset\mbox{]})$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8225}
Decode a DNS packet from a buffer\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8226}{}\doxysubsubsection{$<$tt$>$var len = packets.\+encoding\+Length(packet)$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8226}
Returns how many bytes are needed to encode the DNS packet\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8227}{}\doxysection{Packets}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8227}
Packets look like this


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  type: 'query|response',}
\DoxyCodeLine{  id: optionalIdNumber,}
\DoxyCodeLine{  flags: optionalBitFlags,}
\DoxyCodeLine{  questions: [...],}
\DoxyCodeLine{  answers: [...],}
\DoxyCodeLine{  additionals: [...],}
\DoxyCodeLine{  authorities: [...]}
\DoxyCodeLine{\} }

\end{DoxyCode}


The bit flags available are


\begin{DoxyCode}{0}
\DoxyCodeLine{packet.RECURSION\_DESIRED}
\DoxyCodeLine{packet.RECURSION\_AVAILABLE}
\DoxyCodeLine{packet.TRUNCATED\_RESPONSE}
\DoxyCodeLine{packet.AUTHORITATIVE\_ANSWER}
\DoxyCodeLine{packet.AUTHENTIC\_DATA}
\DoxyCodeLine{packet.CHECKING\_DISABLED }

\end{DoxyCode}


To use more than one flag bitwise-\/or them together


\begin{DoxyCode}{0}
\DoxyCodeLine{var flags = packet.RECURSION\_DESIRED | packet.RECURSION\_AVAILABLE }

\end{DoxyCode}


And to check for a flag use bitwise-\/and


\begin{DoxyCode}{0}
\DoxyCodeLine{var isRecursive = message.flags \& packet.RECURSION\_DESIRED }

\end{DoxyCode}


A question looks like this


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  type: 'A', // or SRV, AAAA, etc}
\DoxyCodeLine{  name: 'google.com' // which record are you looking for}
\DoxyCodeLine{\} }

\end{DoxyCode}


And an answers, additional, or authority looks like this


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  type: 'A', // or SRV, AAAA, etc}
\DoxyCodeLine{  name: 'google.com', // which name is this record for}
\DoxyCodeLine{  ttl: optionalTimeToLiveInSeconds,}
\DoxyCodeLine{  (record specific data, see below)}
\DoxyCodeLine{\} }

\end{DoxyCode}


Currently the different available records are\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8228}{}\doxysubsubsection{$<$tt$>$\+A$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8228}

\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  data: 'IPv4 address' // fx 127.0.0.1}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8229}{}\doxysubsubsection{$<$tt$>$\+AAAA$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8229}

\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  data: 'IPv6 address' // fx fe80::1}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8230}{}\doxysubsubsection{$<$tt$>$\+TXT$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8230}

\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  data: Buffer('some text')}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8231}{}\doxysubsubsection{$<$tt$>$\+NS$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8231}

\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  data: nameServer}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8232}{}\doxysubsubsection{$<$tt$>$\+NULL$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8232}

\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  data: Buffer('any binary data')}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8233}{}\doxysubsubsection{$<$tt$>$\+SOA$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8233}

\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  data:}
\DoxyCodeLine{    \{}
\DoxyCodeLine{      mname: domainName,}
\DoxyCodeLine{      rname: mailbox,}
\DoxyCodeLine{      serial: zoneSerial,}
\DoxyCodeLine{      refresh: refreshInterval,}
\DoxyCodeLine{      retry: retryInterval,}
\DoxyCodeLine{      expire: expireInterval,}
\DoxyCodeLine{      minimum: minimumTTL}
\DoxyCodeLine{    \}}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8234}{}\doxysubsubsection{$<$tt$>$\+SRV$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8234}

\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  data: \{}
\DoxyCodeLine{    port: servicePort,}
\DoxyCodeLine{    target: serviceHostName,}
\DoxyCodeLine{    priority: optionalServicePriority,}
\DoxyCodeLine{    weight: optionalServiceWeight}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8235}{}\doxysubsubsection{$<$tt$>$\+HINFO$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8235}

\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  data: \{}
\DoxyCodeLine{    cpu: 'cpu info',}
\DoxyCodeLine{    os: 'os info'}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8236}{}\doxysubsubsection{$<$tt$>$\+PTR$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8236}

\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  data: 'points.to.another.record'}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8237}{}\doxysubsubsection{$<$tt$>$\+CNAME$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8237}

\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  data: 'cname.to.another.record'}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8238}{}\doxysubsubsection{$<$tt$>$\+DNAME$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8238}

\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  data: 'dname.to.another.record'}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8239}{}\doxysubsubsection{$<$tt$>$\+CAA$<$/tt$>$}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8239}

\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  flags: 128, // octet}
\DoxyCodeLine{  tag: 'issue|issuewild|iodef',}
\DoxyCodeLine{  value: 'ca.example.net'}
\DoxyCodeLine{\} }

\end{DoxyCode}


If you need another one, open an issue and we\textquotesingle{}ll try to add it.\hypertarget{md_sfcode_node_modules_dns_packet_README_autotoc_md8240}{}\doxysection{License}\label{md_sfcode_node_modules_dns_packet_README_autotoc_md8240}
MIT 