This commit is contained in:
AJ ONeal 2017-02-02 19:43:56 -07:00
parent c65aa77333
commit 75a03ad902
1 changed files with 15 additions and 12 deletions

View File

@ -6,6 +6,10 @@
// ui8 is the ArrayBuffer of the entire packet // ui8 is the ArrayBuffer of the entire packet
// ptr is the current index of the packet // ptr is the current index of the packet
// q = { byteLength: 0, cpcount: 0, labels: [], name: '' } // q = { byteLength: 0, cpcount: 0, labels: [], name: '' }
//
// NOTE:
// "NAME"s are terminated with 0x00
// "RDATA" is terminated by its length
exports.DNS_UNPACK_LABELS = function (ui8, ptr, q) { exports.DNS_UNPACK_LABELS = 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)");
@ -18,13 +22,13 @@ exports.DNS_UNPACK_LABELS = function (ui8, ptr, q) {
var label = []; var label = [];
do { do {
console.log("total: " + total);
label.length = 0; label.length = 0;
len = ui8[total]; len = ui8[total];
console.log("len: " + len);
if (len === undefined){ if (len === undefined){
console.log("error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); // RDATA is terminated by undefined, not len === 0
break;
} }
if (0xc0 === len) { if (0xc0 === len) {
var cpptr = ui8[total + 1]; var cpptr = ui8[total + 1];
@ -36,6 +40,7 @@ exports.DNS_UNPACK_LABELS = function (ui8, ptr, q) {
// recursion // recursion
return exports.DNS_UNPACK_LABELS(ui8, ptr, q); return exports.DNS_UNPACK_LABELS(ui8, ptr, q);
} }
//str.length = 0; // fast empty array //str.length = 0; // fast empty array
if (ui8.byteLength - total < len) { if (ui8.byteLength - total < len) {
throw new Error( throw new Error(
@ -43,22 +48,20 @@ exports.DNS_UNPACK_LABELS = function (ui8, ptr, q) {
+ " but packet itself has " + (ui8.byteLength - total) + " bytes remaining" + " but packet itself has " + (ui8.byteLength - total) + " bytes remaining"
); );
} }
for (i = 0; i < len; i += 1) { for (i = 0; i < len; i += 1) {
total += 1; total += 1;
// TODO check url-allowable characters // TODO check url-allowable characters
label.push(String.fromCharCode(ui8[total])); label.push(String.fromCharCode(ui8[total]));
console.log("pushed (ui8) " + ui8[total] + " to labels at i = " + i);
console.log("in char: " + String.fromCharCode(ui8[total]));
} }
if (label.length) { if (label.length) {
console.log("pushed a label " + q.labels + " at index " + i);
q.labels.push(label.join('')); q.labels.push(label.join(''));
} }
total += 1; total += 1;
// console.log('total', total, ui8[total], String.fromCharCode(ui8[total]));
} while (len); } while (0 !== len && undefined !== len);
//str.pop(); // remove trailing '.' //str.pop(); // remove trailing '.'
@ -66,7 +69,7 @@ exports.DNS_UNPACK_LABELS = function (ui8, ptr, q) {
if (0 === q.cpcount) { if (0 === q.cpcount) {
q.byteLength = total - ptr; q.byteLength = total - ptr;
} }
console.log("returning q! " + JSON.stringify(q));
return q; return q;
}; };