From 2c436fbefc6a7a4038ef759e84d6828fe8abb7ac Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 25 Jan 2017 15:04:20 -0700 Subject: [PATCH] fix #4 tested, removed logs, works --- pure-parser.js | 65 ++++++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/pure-parser.js b/pure-parser.js index 32cc34c..a4eaadb 100644 --- a/pure-parser.js +++ b/pure-parser.js @@ -26,47 +26,38 @@ pdns.unpackHeader = function (i) { return header; }; -pdns.unpackQname = function (ab, ptr, q) { - var ui8 = new Uint8Array(ab); - var total = ptr; - var i; - - var len; - var label = []; - q = q || { +pdns.unpackQname = function (ui8, ptr, q) { + return pdns._unpackQname(ui8, ptr, q || { name: '' , type: 0 , class: 0 , byteLength: 0 , labels: [] , cpcount: 0 - }; - + }); +}; +pdns._unpackQname = function (ui8, ptr, q) { if (q.cpcount > 25) { throw new Error("compression pointer loop detected (over 25 pointers seems like a loop)"); } + var total = ptr; + var i; + + var len; + var label = []; + do { label.length = 0; len = ui8[total]; if (0xc0 === len) { - console.log('byteLength', ui8.byteLength); var cpptr = ui8[total + 1]; - var cplen = ui8[ui8[total + 1]]; - console.log('cp at', total); - console.log('ptr to', cpptr); - console.log('label len', cplen); - console.log('label', Buffer.from(ui8.slice(cpptr + 1, cpptr + 1 + cplen)).toString('ascii')); // we're not coming back! ptr = cpptr; q.cpcount += 1; q.byteLength += 2; // cp and len - return pdns.unpackQname(ab, ptr, q); - throw new Error("discovered a compression pointer at byte " + total - + " pointing to byte " + ui8[total + 1] - + ", but compression pointer support is not yet implemented" - ); + return pdns.unpackQname(ui8, ptr, q); } //str.length = 0; // fast empty array if (ui8.byteLength - total < len) { @@ -131,14 +122,14 @@ pdns.unpack = function (ab) { var total = 12; var i; var rec; + var ui8 = new Uint8Array(ab); // TODO move to pdns.unpackQuestion to make testable - function unpackQuestion(ab, dv, total) { + function unpackQuestion(ab, dv, ui8, total) { var ototal = total; - var data = new Uint8Array(ab).slice(total); - var q = pdns.unpackQname(ab, total); - console.log('unpackQuestion:'); - console.log(q); + var q = pdns.unpackQname(ui8, total); + //console.log('unpackQuestion QNAME:'); + //console.log(q); total += q.byteLength; if (ab.byteLength - total < 4) { @@ -157,12 +148,11 @@ pdns.unpack = function (ab) { return q; } - function unpackAnswer(ab, dv, total) { + function unpackAnswer(ab, dv, ui8, total) { var ototal = total; - var data = new Uint8Array(ab).slice(total); - var q = pdns.unpackQname(ab, total); - console.log('unpackAnswer:'); - console.log(q); + var q = pdns.unpackQname(ui8, total); + //console.log('unpackAnswer QNAME:'); + //console.log(q); total += q.byteLength; @@ -184,15 +174,12 @@ pdns.unpack = function (ab) { total += 2; // TODO actually parse RDATA - console.log(ab.byteLength, ab.byteLength - total, ab.byteLength + -total + q.rdlength); q.rdata = new Uint8Array(ab).slice(total, total + q.rdlength); console.log('q.rdata', q.rdata.byteLength, 'bytes:'); - q.rdata = Array.prototype.slice.apply(q.rdata); console.log(q.rdata); - console.log('total', total); + //q.rdata = Array.prototype.slice.apply(q.rdata); total += q.rdlength; - console.log('total', total); q.byteLength = total - ototal; return q; @@ -203,7 +190,7 @@ pdns.unpack = function (ab) { console.log('qdcount', qdcount); header.questions = []; for (i = 0; i < qdcount; i += 1) { - rec = unpackQuestion(ab, dv, total); + rec = unpackQuestion(ab, dv, ui8, total); total += rec.byteLength; header.questions.push(rec); } @@ -211,7 +198,7 @@ pdns.unpack = function (ab) { console.log('ancount', ancount); header.answers = []; for (i = 0; i < ancount; i += 1) { - rec = unpackAnswer(ab, dv, total); + rec = unpackAnswer(ab, dv, ui8, total); total += rec.byteLength; header.answers.push(rec); } @@ -219,7 +206,7 @@ pdns.unpack = function (ab) { console.log('nscount', nscount); header.authority = []; for (i = 0; i < nscount; i += 1) { - rec = unpackAnswer(ab, dv, total); + rec = unpackAnswer(ab, dv, ui8, total); total += rec.byteLength; header.authority.push(rec); } @@ -227,7 +214,7 @@ pdns.unpack = function (ab) { console.log('arcount', arcount); header.additional = []; for (i = 0; i < arcount; i += 1) { - rec = unpackAnswer(ab, dv, total); + rec = unpackAnswer(ab, dv, ui8, total); total += rec.byteLength; header.additional.push(rec); }