From 5e1fff748e5269c3f468481d15839521dfc9821e Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 25 Jan 2017 14:41:30 -0700 Subject: [PATCH] cp ptr updates --- bin/dns-parse.js | 1 + pure-parser.js | 43 ++++++++++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/bin/dns-parse.js b/bin/dns-parse.js index 0de8c38..cf138db 100644 --- a/bin/dns-parse.js +++ b/bin/dns-parse.js @@ -39,6 +39,7 @@ fs.readFileAsync(filename, null).then(function (nb) { try { record.data = pdns.unpackRdata(nb.buffer, packet, record); } catch (e) { + console.error(e.stack); record.error = e; } } diff --git a/pure-parser.js b/pure-parser.js index 827502b..59f6415 100644 --- a/pure-parser.js +++ b/pure-parser.js @@ -26,21 +26,33 @@ pdns.unpackHeader = function (i) { return header; }; -pdns.unpackQname = function (ui8) { - var total = 0; +pdns.unpackQname = function (ab, ptr, q) { + var ui8 = new Uint8Array(ab); + var total = ptr; var i; var len; - var q = { + var label = []; + q = q || { name: '' , type: 0 , class: 0 + , byteLength: 0 + , labels: [] + , cpcount: 0 }; - var str = []; 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')); throw new Error("discovered a compression pointer at byte " + total + " pointing to byte " + ui8[total + 1] + ", but compression pointer support is not yet implemented" @@ -56,19 +68,21 @@ pdns.unpackQname = function (ui8) { for (i = 0; i < len; i += 1) { total += 1; // TODO check url-allowable characters - str.push(String.fromCharCode(ui8[total])); + label.push(String.fromCharCode(ui8[total])); + } + if (label.length) { + q.labels.push(label.join('')); } total += 1; - if (ui8[total]) { - // pushd connecting '.', but not trailing - str.push('.'); - } //console.log('total', total, ui8[total], String.fromCharCode(ui8[total])); } while (len); //str.pop(); // remove trailing '.' - q.name = str.join(''); + q.name = q.labels.join('.'); + if (0 === q.cpcount) { + q.byteLength = total - ptr; + } return q; }; @@ -112,9 +126,8 @@ pdns.unpack = function (ab) { function unpackQuestion(ab, dv, total) { var ototal = total; var data = new Uint8Array(ab).slice(total); - var q = pdns.unpackQname(data); - total += q.name.length + 2; // account for leading and trailing string length byte - + var q = pdns.unpackQname(ab, total); + total += q.byteLength; if (ab.byteLength - total < 4) { // console.error(str.join('')); @@ -135,8 +148,8 @@ pdns.unpack = function (ab) { function unpackAnswer(ab, dv, total) { var ototal = total; var data = new Uint8Array(ab).slice(total); - var q = pdns.unpackQname(data); - total += q.name.length + 2; // account for leading and trailing string length byte + var q = pdns.unpackQname(ab, total); + total += q.byteLength; if (ab.byteLength - total < 10) {