diff --git a/README.md b/README.md index 7a433bc..7263c4c 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,9 @@ npm test Usage ----- +* CLI +* API + **CLI** You can work directly from `node_modules/dns-suite`: @@ -85,19 +88,6 @@ You can work directly from `node_modules/dns-suite`: pushd node_modules/dns-suite/ ``` -Capture mDNS broadcast packets - -```bash -# example -# node bin/mdns-capture.js -node bin/mdns-capture.js mdns-test -``` - -```bash -# in another terminal -dig @224.0.0.251 -p 5353 -t PTR _services._dns-sd._udp.local -``` - Parsing a saved packet ```bash @@ -112,28 +102,49 @@ should be. **Library** -* `packet = dnsjs.unpack(arrayBuffer)` -* `packet = dnsjs.unpackRdatas(arrayBuffer, packet)` -* `packet.answers[0].data = dnsjs.unpackRdatas(arrayBuffer, packet, packet.answers[0])` +* `DNSPacket.parse(nodeOrArrayBuffer)` returns json (as shown above) +* `DNSPacket.pack(packet)` returns ArrayBuffer (browser and node) +* `DNSPacket.write(packet)` returns NodeBuffer (node only) node.js: ```js var nodeBuffer = fs.readFileSync('./samples/a-0.mdns.bin'); var arrayBuffer = nodeBuffer.buffer; -var dnsjs = require('dns-suite'); -var packet = dnsjs.unpack(arrayBuffer); +var DNSPacket = require('dns-suite').DNSPacket; +var packet = DNSPacket.parse(arrayBuffer); +var ab = DNSPacket.pack(packet); console.log(packet); +console.log(new Uint8Array(ab)); ``` Browser: ```js var arrayBuffer = new Uint8Array.from([ /* bytes */ ]).buffer; -var packet = pdns.unpack(arrayBuffer); +var packet = DNSPacket.parse(arrayBuffer); +var ab = DNSPacket.pack(packet); console.log(packet); +console.log(new Uint8Array(ab)); +``` + + +Capturing Packets +----- + +We have a command line tool for that! See [dig.js](https://git.daplie.com/Daplie/dig.js). + +``` +# Install +npm install -g 'git+https://git@git.daplie.com/Daplie/dig.js.git' + +# Use with DNS +dig.js A daplie.com --output . + +# Use with mDNS +dig.js --mdns PTR _services._dns-sd._udp.local --output . ``` Contributing and Development diff --git a/dns.js b/dns.js index eaa9b99..aab2e2b 100644 --- a/dns.js +++ b/dns.js @@ -8,7 +8,11 @@ var Packer = (exports.DNS_PACKER || require('./dns.packer.js').DNS_PACKER); exports.DNSPacket = { parse: function (nb) { // backwards compat with node buffer - var ab = nb.buffer || nb; + var ab = nb; + if (nb.buffer) { + // TODO pass byteOffsets to unpacker + ab = nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength); + } var packet = Parser.unpack(ab); function tryParseRdata(record) {