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; return header;
}; };
pdns.unpackQname = function (ab, ptr, q) { pdns.unpackQname = function (ui8, ptr, q) {
var ui8 = new Uint8Array(ab); return pdns._unpackQname(ui8, ptr, q || {
var total = ptr;
var i;
var len;
var label = [];
q = q || {
name: '' name: ''
, type: 0 , type: 0
, class: 0 , class: 0
, byteLength: 0 , byteLength: 0
, labels: [] , labels: []
, cpcount: 0 , cpcount: 0
}; });
};
pdns._unpackQname = function (ui8, ptr, q) {
if (q.cpcount > 25) { if (q.cpcount > 25) {
throw new Error("compression pointer loop detected (over 25 pointers seems like a loop)"); throw new Error("compression pointer loop detected (over 25 pointers seems like a loop)");
} }
var total = ptr;
var i;
var len;
var label = [];
do { do {
label.length = 0; label.length = 0;
len = ui8[total]; len = ui8[total];
if (0xc0 === len) { if (0xc0 === len) {
console.log('byteLength', ui8.byteLength);
var cpptr = ui8[total + 1]; 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! // we're not coming back!
ptr = cpptr; ptr = cpptr;
q.cpcount += 1; q.cpcount += 1;
q.byteLength += 2; // cp and len q.byteLength += 2; // cp and len
return pdns.unpackQname(ab, ptr, q); return pdns.unpackQname(ui8, 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"
);
} }
//str.length = 0; // fast empty array //str.length = 0; // fast empty array
if (ui8.byteLength - total < len) { if (ui8.byteLength - total < len) {
@ -131,14 +122,14 @@ pdns.unpack = function (ab) {
var total = 12; var total = 12;
var i; var i;
var rec; var rec;
var ui8 = new Uint8Array(ab);
// TODO move to pdns.unpackQuestion to make testable // TODO move to pdns.unpackQuestion to make testable
function unpackQuestion(ab, dv, total) { function unpackQuestion(ab, dv, ui8, total) {
var ototal = total; var ototal = total;
var data = new Uint8Array(ab).slice(total); var q = pdns.unpackQname(ui8, total);
var q = pdns.unpackQname(ab, total); //console.log('unpackQuestion QNAME:');
console.log('unpackQuestion:'); //console.log(q);
console.log(q);
total += q.byteLength; total += q.byteLength;
if (ab.byteLength - total < 4) { if (ab.byteLength - total < 4) {
@ -157,12 +148,11 @@ pdns.unpack = function (ab) {
return q; return q;
} }
function unpackAnswer(ab, dv, total) { function unpackAnswer(ab, dv, ui8, total) {
var ototal = total; var ototal = total;
var data = new Uint8Array(ab).slice(total); var q = pdns.unpackQname(ui8, total);
var q = pdns.unpackQname(ab, total); //console.log('unpackAnswer QNAME:');
console.log('unpackAnswer:'); //console.log(q);
console.log(q);
total += q.byteLength; total += q.byteLength;
@ -184,15 +174,12 @@ pdns.unpack = function (ab) {
total += 2; total += 2;
// TODO actually parse RDATA // 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); q.rdata = new Uint8Array(ab).slice(total, total + q.rdlength);
console.log('q.rdata', q.rdata.byteLength, 'bytes:'); console.log('q.rdata', q.rdata.byteLength, 'bytes:');
q.rdata = Array.prototype.slice.apply(q.rdata);
console.log(q.rdata); console.log(q.rdata);
console.log('total', total); //q.rdata = Array.prototype.slice.apply(q.rdata);
total += q.rdlength; total += q.rdlength;
console.log('total', total);
q.byteLength = total - ototal; q.byteLength = total - ototal;
return q; return q;
@ -203,7 +190,7 @@ pdns.unpack = function (ab) {
console.log('qdcount', qdcount); console.log('qdcount', qdcount);
header.questions = []; header.questions = [];
for (i = 0; i < qdcount; i += 1) { for (i = 0; i < qdcount; i += 1) {
rec = unpackQuestion(ab, dv, total); rec = unpackQuestion(ab, dv, ui8, total);
total += rec.byteLength; total += rec.byteLength;
header.questions.push(rec); header.questions.push(rec);
} }
@ -211,7 +198,7 @@ pdns.unpack = function (ab) {
console.log('ancount', ancount); console.log('ancount', ancount);
header.answers = []; header.answers = [];
for (i = 0; i < ancount; i += 1) { for (i = 0; i < ancount; i += 1) {
rec = unpackAnswer(ab, dv, total); rec = unpackAnswer(ab, dv, ui8, total);
total += rec.byteLength; total += rec.byteLength;
header.answers.push(rec); header.answers.push(rec);
} }
@ -219,7 +206,7 @@ pdns.unpack = function (ab) {
console.log('nscount', nscount); console.log('nscount', nscount);
header.authority = []; header.authority = [];
for (i = 0; i < nscount; i += 1) { for (i = 0; i < nscount; i += 1) {
rec = unpackAnswer(ab, dv, total); rec = unpackAnswer(ab, dv, ui8, total);
total += rec.byteLength; total += rec.byteLength;
header.authority.push(rec); header.authority.push(rec);
} }
@ -227,7 +214,7 @@ pdns.unpack = function (ab) {
console.log('arcount', arcount); console.log('arcount', arcount);
header.additional = []; header.additional = [];
for (i = 0; i < arcount; i += 1) { for (i = 0; i < arcount; i += 1) {
rec = unpackAnswer(ab, dv, total); rec = unpackAnswer(ab, dv, ui8, total);
total += rec.byteLength; total += rec.byteLength;
header.additional.push(rec); header.additional.push(rec);
} }