From 087dc9555d0c94ee4b9398ead2f8d0aabaeda3f1 Mon Sep 17 00:00:00 2001 From: Daplie Date: Thu, 2 Feb 2017 18:39:32 -0700 Subject: [PATCH] started fixing format to match native dns. Spotted bug in unpacking labels. Not fixed yet --- demo.css | 11 +++++++++++ demo.html | 28 +++++++++++++++++++++++++++- dns.js | 10 +++++----- dns.parser.js | 8 ++++---- dns.rdata.parse.js | 3 +++ dns.type.a.js | 3 ++- dns.type.aaaa.js | 3 ++- dns.type.cname.js | 3 ++- dns.type.mx.js | 8 ++++---- dns.type.ns.js | 4 ++-- dns.type.ptr.js | 4 ++-- dns.type.srv.js | 30 ++++++------------------------ dns.type.txt.js | 27 +++++++++++++++++++++++---- dns.unpack-labels.js | 16 ++++++++++++++-- 14 files changed, 107 insertions(+), 51 deletions(-) create mode 100644 demo.css diff --git a/demo.css b/demo.css new file mode 100644 index 0000000..e6e9b49 --- /dev/null +++ b/demo.css @@ -0,0 +1,11 @@ +/* style page for DNS demo*/ + + +input { + + + width: 50%; + height: 150px; + padding: 12px 20px; + +} \ No newline at end of file diff --git a/demo.html b/demo.html index e094fc2..264d7d9 100644 --- a/demo.html +++ b/demo.html @@ -1,5 +1,9 @@ + + DNS Parser Demo + + +
+ + base64:
+ +
+ + urlsafe base64:
+ +
+ + hex:
+ +
+ + + @@ -21,10 +44,13 @@ + - + + + diff --git a/dns.js b/dns.js index 6cd213e..c257669 100644 --- a/dns.js +++ b/dns.js @@ -6,7 +6,8 @@ var Parser = (exports.DNS_PARSER || require('./dns.parser.js').DNS_PARSER); var Packer = (exports.DNS_PACKER || require('./dns.packer.js').DNS_PACKER); - +var classes = exports.DNS_CLASSES || require('./dns.classes.js').DNS_CLASSES; +var types = exports.DNS_TYPES || require('./dns.types.js').DNS_TYPES; exports.DNSPacket = { parse: function (nb) { // backwards compat with node buffer @@ -14,11 +15,10 @@ exports.DNSPacket = { var packet = Parser.unpack(ab); function tryParseRdata(record) { - record.data = null; - record.error = null; - + try { - record.data = Parser.unpackRdata(ab, packet, record); + + record = Parser.unpackRdata(ab, packet, record); } catch (e) { console.error('[Error] unpackRdata: ' + e.message); record.error = e; diff --git a/dns.parser.js b/dns.parser.js index 6e1fde4..f60eacd 100644 --- a/dns.parser.js +++ b/dns.parser.js @@ -106,8 +106,8 @@ pdns.unpack = function (ab) { q.className = classes[q.class]; q.typeName = types[q.type]; - console.log('*********************'); - console.log(q.typeName); + // console.log('*********************'); + // console.log(q.typeName); return q; } @@ -157,8 +157,8 @@ pdns.unpack = function (ab) { // TODO actually parse RDATA q.rdstart = total; - console.log('q.rdata', q.rdlength, 'bytes:'); - console.log(new Uint8Array(ab).slice(total, total + q.rdlength)); + // console.log('q.rdata', q.rdlength, 'bytes:'); + // console.log(new Uint8Array(ab).slice(total, total + q.rdlength)); //q.rdata = Array.prototype.slice.apply(q.rdata); //q.rdend = q.rdstart + q.rdlength; diff --git a/dns.rdata.parse.js b/dns.rdata.parse.js index 2645056..5defe27 100644 --- a/dns.rdata.parse.js +++ b/dns.rdata.parse.js @@ -37,6 +37,9 @@ exports.DNS_RDATA_PARSE = function (ab, packet, record) { ); } + // NOTE: record will be modified + // Things that get added include: + // address, data, priority exchange, weight, return parser(ab, packet, record); }; diff --git a/dns.type.a.js b/dns.type.a.js index f1dd471..5d94592 100644 --- a/dns.type.a.js +++ b/dns.type.a.js @@ -6,8 +6,9 @@ exports.DNS_TYPE_A = function (ab, packet, record) { var ui8 = new Uint8Array(ab.slice(record.rdstart, record.rdstart + record.rdlength)); // i.e. 127.0.0.1 + record.address = ui8[0] + '.' + ui8[1] + '.' + ui8[2] + '.' + ui8[3]; - return ui8[0] + '.' + ui8[1] + '.' + ui8[2] + '.' + ui8[3]; + return record; }; }('undefined' !== typeof window ? window : exports)); diff --git a/dns.type.aaaa.js b/dns.type.aaaa.js index 6c8cd19..bce01db 100644 --- a/dns.type.aaaa.js +++ b/dns.type.aaaa.js @@ -27,7 +27,8 @@ exports.DNS_TYPE_AAAA = function (ab, packet, record) { s += dv.getUint16(i, false).toString(16); } - return s; + record.address = s; + return record; }; }('undefined' !== typeof window ? window : exports)); diff --git a/dns.type.cname.js b/dns.type.cname.js index fff9249..a167da7 100644 --- a/dns.type.cname.js +++ b/dns.type.cname.js @@ -7,7 +7,8 @@ var unpackLabels = exports.DNS_UNPACK_LABELS || require('./dns.unpack-labels.js').DNS_UNPACK_LABELS; exports.DNS_TYPE_CNAME = function (ab, packet, record) { - return unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; + record.data = unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; + return record; }; }('undefined' !== typeof window ? window : exports)); diff --git a/dns.type.mx.js b/dns.type.mx.js index ebb5505..e01fdc4 100644 --- a/dns.type.mx.js +++ b/dns.type.mx.js @@ -15,10 +15,10 @@ exports.DNS_TYPE_MX = function (ab, packet, record) { var rdataAb = ab.slice(record.rdstart, record.rdstart + record.rdlength); var dv = new DataView(rdataAb); - return { - priority: dv.getUint16(0, false) - , exchange: unpackLabels(new Uint8Array(ab), record.rdstart+2, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name - }; + record.priority = dv.getUint16(0, false); + record.exchange = unpackLabels(new Uint8Array(ab), record.rdstart+2, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; + + return record; }; diff --git a/dns.type.ns.js b/dns.type.ns.js index b432395..0b558d5 100644 --- a/dns.type.ns.js +++ b/dns.type.ns.js @@ -8,8 +8,8 @@ var unpackLabels = exports.DNS_UNPACK_LABELS || require('./dns.unpack-labels.js' exports.DNS_TYPE_NS = function (ab , packet, record) { - - return unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; + record.data = unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; + return record; }; diff --git a/dns.type.ptr.js b/dns.type.ptr.js index 38bf195..58c4b3f 100644 --- a/dns.type.ptr.js +++ b/dns.type.ptr.js @@ -11,7 +11,7 @@ var unpackLabels = exports.DNS_UNPACK_LABELS || require('./dns.unpack-labels.js').DNS_UNPACK_LABELS; exports.DNS_TYPE_PTR = function (ab, pack, record) { - - return unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; + record.data = unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; + return record; }; }('undefined' !== typeof window ? window : exports)); diff --git a/dns.type.srv.js b/dns.type.srv.js index 53ecd76..333c446 100644 --- a/dns.type.srv.js +++ b/dns.type.srv.js @@ -11,31 +11,13 @@ exports.DNS_TYPE_SRV = function (ab, packet, record) { var rdataAb = ab.slice(record.rdstart,record.rdstart + record.rdlength) var dv = new DataView(rdataAb); - // var string = ''; - // var data = ''; - // for (var i = 0; i < dv.byteLength; i+=1){ - // data = dv.getUint8(i, false); - // string = String.fromCharCode(data); - // console.log("data at index " + i + " is " + string); - // } - - console.log("***************************"); - console.log("printing dataView!!!"); - for (var i = 0; i < dv.byteLength; i+=1) { - - console.log(dv.getUint8(i, false)); - } - - var s = { - priority: dv.getUint16(0, false) - , weight: dv.getUint16(2, false) - , port: dv.getUint16(4, false) - , target: unpackLabels(new Uint8Array(ab), record.rdstart+6, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name - } - - - return s; + record.priority = dv.getUint16(0, false); + record.weight = dv.getUint16(2, false); + record.port = dv.getUint16(4, false); + record.target = unpackLabels(new Uint8Array(ab), record.rdstart+6, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; + + return record; }; }('undefined' !== typeof window ? window : exports)); diff --git a/dns.type.txt.js b/dns.type.txt.js index 8ae3f7a..9310146 100644 --- a/dns.type.txt.js +++ b/dns.type.txt.js @@ -5,11 +5,30 @@ // with a host or other name, such as a human readable information about a server // network, data center, and other accounting information. - - var unpackLabels = exports.DNS_UNPACK_LABELS || require('./dns.unpack-labels.js').DNS_UNPACK_LABELS; -exports.DNS_TYPE_TXT = function (ab, pack, record) { - return unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; +exports.DNS_TYPE_TXT = function (ab, packet, record) { + + + var rdataAb = ab.slice(record.rdstart, record.rdstart + record.rdlength); + console.log(rdataAb); + var dv = new DataView(rdataAb); + // var d = ''; + // var string = ''; + // for (var i = 0; i < dv.byteLength; i+= 1){ + + // d = dv.getUint8(i, false); + // string = String.fromCharCode(d); + // console.log("data at: " + i + " is: " + string); + + + + // + record.data = unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; + + var l = unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' }); + + console.log("labels: " + JSON.stringify(l)); + return record; }; }('undefined' !== typeof window ? window : exports)); diff --git a/dns.unpack-labels.js b/dns.unpack-labels.js index bd43172..306ffe9 100644 --- a/dns.unpack-labels.js +++ b/dns.unpack-labels.js @@ -18,8 +18,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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + } if (0xc0 === len) { var cpptr = ui8[total + 1]; @@ -27,6 +32,8 @@ exports.DNS_UNPACK_LABELS = function (ui8, ptr, q) { ptr = cpptr; q.cpcount += 1; q.byteLength += 2; // cp and len + + // recursion return exports.DNS_UNPACK_LABELS(ui8, ptr, q); } //str.length = 0; // fast empty array @@ -40,12 +47,17 @@ exports.DNS_UNPACK_LABELS = function (ui8, ptr, q) { 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])); + // console.log('total', total, ui8[total], String.fromCharCode(ui8[total])); } while (len); //str.pop(); // remove trailing '.' @@ -54,7 +66,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; };