From 8148e25d185f6a64019b24482043ded3d4eca782 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 11 Feb 2017 09:32:50 -0700 Subject: [PATCH] more helpful error --- dns.parser.js | 34 ++++++++++++++++++++-------------- test/parser.js | 9 ++++++++- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/dns.parser.js b/dns.parser.js index c9223f1..769b412 100644 --- a/dns.parser.js +++ b/dns.parser.js @@ -58,11 +58,11 @@ pdns.unpack = function (ab) { // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView var dv = new DataView(ab); var id = dv.getUint16(0, false); - packet.header = pdns.unpackHeader(dv.getUint16(2, false)); - var qdcount = dv.getUint16(4, false); // query count - var ancount = dv.getUint16(6, false); // answer count - var nscount = dv.getUint16(8, false); // authority count - var arcount = dv.getUint16(10, false); // additional count + packet.header = pdns.unpackHeader(dv.getUint16(2, false)); + packet.qdcount = dv.getUint16(4, false); // query count + packet.ancount = dv.getUint16(6, false); // answer count + packet.nscount = dv.getUint16(8, false); // authority count + packet.arcount = dv.getUint16(10, false); // additional count var total = 12; var i; var rec; @@ -114,6 +114,7 @@ pdns.unpack = function (ab) { function unpackAnswer(ab, dv, ui8, total) { var ototal = total; + var err; var q = pdns._unpackLabels(ui8, total, { name: '' , type: 0 @@ -136,10 +137,15 @@ pdns.unpack = function (ab) { if (ab.byteLength - total < 10) { // console.error(str.join('')); - throw new Error( + //console.error(JSON.stringify(packet, null, 1)); + //console.error(JSON.stringify(q, null, 1)); + err = new Error( "Expected a 2-byte QTYPE, 2-byte QCLASS, 4-byte TTL, and 2-byte RDLENGTH following " + total + "-byte QNAME" + " but packet itself has " + (ab.byteLength - total) + " bytes remaining" ); + err.packet = packet; + err.record = q; + throw err; } q.type = dv.getUint16(total, false); @@ -171,33 +177,33 @@ pdns.unpack = function (ab) { packet.header.id = id; - console.log('qdcount', qdcount); + console.log('qdcount', packet.qdcount); packet.question = []; - for (i = 0; i < qdcount; i += 1) { + for (i = 0; i < packet.qdcount; i += 1) { rec = unpackQuestion(ab, dv, ui8, total); total += rec.byteLength; packet.question.push(rec); } - console.log('ancount', ancount); + console.log('ancount', packet.ancount); packet.answer = []; - for (i = 0; i < ancount; i += 1) { + for (i = 0; i < packet.ancount; i += 1) { rec = unpackAnswer(ab, dv, ui8, total); total += rec.byteLength; packet.answer.push(rec); } - console.log('nscount', nscount); + console.log('nscount', packet.nscount); packet.authority = []; - for (i = 0; i < nscount; i += 1) { + for (i = 0; i < packet.nscount; i += 1) { rec = unpackAnswer(ab, dv, ui8, total); total += rec.byteLength; packet.authority.push(rec); } - console.log('arcount', arcount); + console.log('arcount', packet.arcount); packet.additional = []; - for (i = 0; i < arcount; i += 1) { + for (i = 0; i < packet.arcount; i += 1) { rec = unpackAnswer(ab, dv, ui8, total); total += rec.byteLength; packet.additional.push(rec); diff --git a/test/parser.js b/test/parser.js index 9cc09a9..ea3bdcb 100644 --- a/test/parser.js +++ b/test/parser.js @@ -8,6 +8,7 @@ var path = require('path'); var dnsjs = require('../').DNSPacket; var expected; // shim + var onefile = process.argv[2]; var dirname = path.join(__dirname, 'fixtures'); @@ -42,6 +43,9 @@ } fs.readdirSync(dirname).forEach(function (name) { + if (onefile && !onefile.split(',').some(function (f) { return name.match(onefile); })) { + return; + } if (!/\.bin$/.test(name)) { return; } @@ -58,8 +62,11 @@ try { result = dnsjs.parse(ab); } catch(e) { + console.error('[Error] parse error'); console.log(ab); - console.error('[Error] ' + e.message); + console.error(e.packet); + console.error(e.record); + console.error(e.stack); console.error(''); //console.error(e.stack); return;