From 75a03ad902dc80725594c65c551f25ef1411307b Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 2 Feb 2017 19:43:56 -0700 Subject: [PATCH] chimney! --- dns.unpack-labels.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/dns.unpack-labels.js b/dns.unpack-labels.js index 306ffe9..477b95e 100644 --- a/dns.unpack-labels.js +++ b/dns.unpack-labels.js @@ -6,6 +6,10 @@ // ui8 is the ArrayBuffer of the entire packet // ptr is the current index of the packet // 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) { if (q.cpcount > 25) { 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 = []; do { - console.log("total: " + total); label.length = 0; len = ui8[total]; - console.log("len: " + len); if (len === undefined){ - console.log("error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + // RDATA is terminated by undefined, not len === 0 + break; } + if (0xc0 === len) { var cpptr = ui8[total + 1]; @@ -33,9 +37,10 @@ exports.DNS_UNPACK_LABELS = function (ui8, ptr, q) { q.cpcount += 1; q.byteLength += 2; // cp and len - // recursion + // recursion return exports.DNS_UNPACK_LABELS(ui8, ptr, q); } + //str.length = 0; // fast empty array if (ui8.byteLength - total < len) { throw new Error( @@ -43,22 +48,20 @@ exports.DNS_UNPACK_LABELS = function (ui8, ptr, q) { + " but packet itself has " + (ui8.byteLength - total) + " bytes remaining" ); } + for (i = 0; i < len; i += 1) { total += 1; // TODO check url-allowable characters 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) { - - console.log("pushed a label " + q.labels + " at index " + i); - q.labels.push(label.join('')); } + total += 1; - // console.log('total', total, ui8[total], String.fromCharCode(ui8[total])); - } while (len); + + } while (0 !== len && undefined !== len); //str.pop(); // remove trailing '.' @@ -66,7 +69,7 @@ exports.DNS_UNPACK_LABELS = function (ui8, ptr, q) { if (0 === q.cpcount) { q.byteLength = total - ptr; } - console.log("returning q! " + JSON.stringify(q)); + return q; };