WIP unpack question and resource records

This commit is contained in:
AJ ONeal 2017-10-09 01:51:13 -06:00
parent 2f4807d1b4
commit 633e60d643
1 changed files with 107 additions and 13 deletions

View File

@ -5,7 +5,7 @@ var input = process.argv[2];
var fs = require('fs'); var fs = require('fs');
var nb = fs.readFileSync(input, null); var nb = fs.readFileSync(input, null);
var ab = nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength) var ab = nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength);
var dv = new DataView(ab); var dv = new DataView(ab);
// //
@ -19,36 +19,130 @@ console.log('');
console.log('id (int)', dv.getUint16(0)); console.log('id (int)', dv.getUint16(0));
var i = dv.getUint16(2, false); var flags = dv.getUint16(2, false);
console.log('header 0x', i.toString(16)); console.log('header 0x', flags.toString(16));
var flagsstr = '' var flagsstr = ''
+ 'qr ' + ((i & 0x8000) >> 15) + ', ' + 'qr ' + ((flags & 0x8000) >> 15) + ', '
+ 'opcode ' + ((i & 0x7800) >> 11) + ', ' + 'opcode ' + ((flags & 0x7800) >> 11) + ', '
+ 'aa ' + ((i & 0x400) >> 10) + ', ' + 'aa ' + ((flags & 0x400) >> 10) + ', '
+ 'tc ' + ((i & 0x200) >> 9) + ', ' + 'tc ' + ((flags & 0x200) >> 9) + ', '
+ 'rd ' + ((i & 0x100) >> 8) + ', ' + 'rd ' + ((flags & 0x100) >> 8) + ', '
+ 'ra ' + ((i & 0x80) >> 7) + ', ' + 'ra ' + ((flags & 0x80) >> 7) + ', '
+ 'res1 ' + ((i & 0x40) >> 6) + ', ' + 'res1 ' + ((flags & 0x40) >> 6) + ', '
+ 'res2 ' + ((i & 0x20) >> 5) + ', ' + 'res2 ' + ((flags & 0x20) >> 5) + ', '
+ 'res3 ' + ((i & 0x10) >> 4) + ', ' + 'res3 ' + ((flags & 0x10) >> 4) + ', '
+ 'rcode ' + ((i & 0xF)); + 'rcode ' + ((flags & 0xF));
console.log('(flags: ' + flagsstr); console.log('(flags: ' + flagsstr);
var qdcount = dv.getUint16(4, false); // query count var qdcount = dv.getUint16(4, false); // query count
var ancount = dv.getUint16(6, false); // answer count var ancount = dv.getUint16(6, false); // answer count
var nscount = dv.getUint16(8, false); // authority count var nscount = dv.getUint16(8, false); // authority count
var arcount = dv.getUint16(10, false); // additional count var arcount = dv.getUint16(10, false); // additional count
var count;
console.log('qdcount', qdcount); console.log('qdcount', qdcount);
console.log('ancount', ancount); console.log('ancount', ancount);
console.log('nscount', nscount); console.log('nscount', nscount);
console.log('arcount', arcount); console.log('arcount', arcount);
var total = 12;
function unpackQuestion(dv, total, len) {
var qnames = [];
var labelLen;
while (true) {
if (total >= len) {
break;
}
labelLen = dv.getUint8(total, false); // additional count
total += 1;
if (!labelLen) {
break;
}
if (192 === labelLen) {
console.log('ERROR: Compression pointer found');
console.log(dv.getUint8(total, false));
console.log(dv.getUint8(dv.getUint8(total + 0, false), false));
console.log(dv.getUint8(dv.getUint8(total + 1, false), false));
console.log(dv.getUint8(dv.getUint8(total + 2, false), false));
break;
}
var i;
var label = '';
console.log('label len', labelLen);
for (i = 0; i < labelLen; i += 1) {
label += String.fromCharCode(dv.getUint8(total + i, false));
}
total += label.length;
console.log('label:', label);
qnames.push(label);
}
console.log('QNAME:', qnames.join('.'));
console.log('QTYPE:', dv.getUint16(total), false);
total += 2;
console.log('QCLASS:', dv.getUint16(total), false);
total += 2;
return {
total: total
, qname: qnames.join('.')
};
}
var q = { total: total };
function mapChar(ch) { return String.fromCharCode(ch) + '(' + ch + ')'; }
console.log(''); console.log('');
console.log('//////////////////'); console.log('//////////////////');
console.log('// QUESTION //'); console.log('// QUESTION //');
console.log('//////////////////'); console.log('//////////////////');
console.log(''); console.log('');
for (count = 0; count < qdcount; count += 1) {
console.log('Question', count + 1, 'of', qdcount);
q = unpackQuestion(dv, q.total, ab.byteLength);
console.log('');
}
console.log('');
console.log('//////////////////');
console.log('// ANSWER //');
console.log('//////////////////');
console.log('');
for (count = 0; count < ancount; count += 1) {
console.log('Answer', count + 1, 'of', ancount);
q = unpackQuestion(dv, q.total, ab.byteLength);
console.log('');
}
console.log('');
console.log('//////////////////');
console.log('// AUTHORITY //');
console.log('//////////////////');
console.log('');
for (count = 0; count < nscount; count += 1) {
console.log('Authority', count + 1, 'of', nscount);
q = unpackQuestion(dv, q.total, ab.byteLength);
console.log('ttl:', dv.getUint32(q.total, false));
q.total += 4;
q.rdlength = dv.getUint16(q.total, false);
console.log('rdlen:', q.rdlength);
q.total += 2;
console.log('rrdata:');
console.log([].slice.call(new Uint8Array(ab), q.total, q.total + q.rdlength).map(mapChar));
q.total += q.rdlength;
}
console.log('');
console.log('//////////////////');
console.log('// ADDITIONAL //');
console.log('//////////////////');
console.log('');
for (count = 0; count < arcount; count += 1) {
console.log('Additional', count + 1, 'of', arcount);
q = unpackQuestion(dv, q.total, ab.byteLength);
console.log('');
}
}()); }());