fix #4 tested, removed logs, works

This commit is contained in:
AJ ONeal 2017-01-25 15:04:20 -07:00
parent 736ea21b54
commit 2c436fbefc
1 changed files with 26 additions and 39 deletions

View File

@ -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);
}