diff --git a/pure-parser-cli.js b/pure-parser-cli.js index 880488e..9e793bd 100644 --- a/pure-parser-cli.js +++ b/pure-parser-cli.js @@ -30,5 +30,8 @@ fs.readFileAsync(filename, null).then(function (nb) { // nb is a Uint8Array (ArrayBufferView) for nb.buffer // nb.buffer is the actual ArrayBuffer - pdns.unpack(nb.buffer); + var packet = pdns.unpack(nb.buffer); + + console.log('[packet]', nb.byteLength, 'bytes:'); + console.log(packet); }); diff --git a/pure-parser.js b/pure-parser.js index 8964079..b4fe39d 100644 --- a/pure-parser.js +++ b/pure-parser.js @@ -13,14 +13,14 @@ pdns.unpackHeader = function (i) { , qr: (i & 0x8000) >> 15 // Query Response (0 is question, 1 is response) , opcode: (i & 0x7800) >> 11 // 0 is question , aa: (i & 0x400) >> 10 // Authoritative Answer (response-only) - , tc: (i & 0x200) >> 9 // TrunCated - expect another packet with same (?) id - , rd: (i & 0x100) >> 8 // Recursion Desired + , tc: (i & 0x200) >> 9 // TrunCated - expect another packet with same (?) id + , rd: (i & 0x100) >> 8 // Recursion Desired - , ra: (i & 0x80) >> 7 - , res1: (i & 0x40) >> 6 // z - , res2: (i & 0x20) >> 5 // ad - , res3: (i & 0x10) >> 4 // cd - , rcode: (i & 0xF) // Error Code (response-only) + , ra: (i & 0x80) >> 7 + , res1: (i & 0x40) >> 6 // z + , res2: (i & 0x20) >> 5 // ad + , res3: (i & 0x10) >> 4 // cd + , rcode: (i & 0xF) // Error Code (response-only) }; return header; @@ -29,16 +29,16 @@ pdns.unpackHeader = function (i) { pdns.packHeader = function(h) { var val = 0; - val += (h.qr << 15) & 0x8000; + val += (h.qr << 15) & 0x8000; val += (h.opcode << 11) & 0x7800; - val += (h.aa << 10) & 0x400; - val += (h.tc << 9) & 0x200; - val += (h.rd << 8) & 0x100; - val += (h.ra << 7) & 0x80; - val += (h.res1 << 6) & 0x40; - val += (h.res2 << 5) & 0x20; - val += (h.res3 << 4) & 0x10; - val += h.rcode & 0xF; + val += (h.aa << 10) & 0x400; + val += (h.tc << 9) & 0x200; + val += (h.rd << 8) & 0x100; + val += (h.ra << 7) & 0x80; + val += (h.res1 << 6) & 0x40; + val += (h.res2 << 5) & 0x20; + val += (h.res3 << 4) & 0x10; + val += h.rcode & 0xF; return val; }; @@ -116,17 +116,17 @@ pdns.unpack = function (ab) { var nscount = dv.getUint16(8); // authority count var arcount = dv.getUint16(10); // additional count var total = 12; - var data; var i; - var q; + var rec; //console.log('datalen', data.length); console.log(dv, qdcount); // TODO move to pdns.unpackQuestion to make testable - function unpackQuestion() { - data = new Uint8Array(ab).slice(total); - q = pdns.unpackQname(data); + function unpackQuestion(ab, dv, total) { + var ototal = total; + var data = new Uint8Array(ab).slice(total); + var q = pdns.unpackQname(data); total += q.name.length + 2; // account for leading and trailing string length byte @@ -145,13 +145,14 @@ pdns.unpack = function (ab) { console.log('class', q.class, total); total += 2; console.log('total', total); - - header.questions.push(q); + q.qtotal = total - ototal; + return q; } - function unpackAnswer(answers) { - data = new Uint8Array(ab).slice(total); - q = pdns.unpackQname(data); + function unpackAnswer(ab, dv, total) { + var ototal = total; + var data = new Uint8Array(ab).slice(total); + var q = pdns.unpackQname(data); total += q.name.length + 2; // account for leading and trailing string length byte @@ -190,7 +191,8 @@ pdns.unpack = function (ab) { total += q.rdlength; console.log('total', total); - answers.push(q); + q.qtotal = total - ototal; + return q; } header.id = id; @@ -198,13 +200,17 @@ pdns.unpack = function (ab) { console.log('qdcount', qdcount); header.questions = []; for (i = 0; i < qdcount; i += 1) { - unpackQuestion(); + rec = unpackQuestion(ab, dv, total); + total += rec.qtotal; + header.questions.push(rec); } console.log('ancount', ancount); header.answers = []; for (i = 0; i < ancount; i += 1) { - unpackAnswer(header.answers); + rec = unpackAnswer(ab, dv, total); + total += rec.qtotal; + header.answers.push(rec); } console.log('nscount', nscount); @@ -219,5 +225,6 @@ pdns.unpack = function (ab) { unpackAnswer(header.additional); } - console.log('[packet]', header); + header.total = total; + return header; };