Packer works!

This commit is contained in:
AJ ONeal 2017-02-16 16:29:23 -07:00
parent af870ebc8f
commit 737b31fcc2
3 changed files with 31 additions and 8 deletions

View File

@ -47,7 +47,17 @@ var dnspack = exports.DNS_PACKER = {
}
}
function packLabel(sequence, terminates) {
function packLabelSequence(sequence, terminates) {
console.log('sequence:', sequence);
if (labelsMap[sequence]) {
console.log('cached sequence:', sequence);
// minimal compression pointer 0xc0 (192)
dv.setUint8(total, 0xc0, false);
total += 1;
dv.setUint8(total, labelsMap[sequence].total, false);
total += 1;
return;
}
if (terminates) {
// we don't want non-terminating rdata cached, just terminating names
labelsMap[total] = { total: total, name: sequence };
@ -62,19 +72,22 @@ var dnspack = exports.DNS_PACKER = {
total += 1;
});
});
if (terminates) {
// trailing 0 (null) as label sequence terminator
dv.setUint8(total, 0, false);
total += 1;
}
}
function packQuestion(q) {
lint(q);
packLabel(q.name);
// trailing 0 (null) as label sequence terminator
dv.setUint8(total, 0, false);
total += 1;
packLabelSequence(q.name, true);
dv.setUint16(total, q.type, false);
dv.setUint16(total + 2, q.class, false);
total += 4; // two bytes type two bytes class
total += 2;
dv.setUint16(total, q.class, false);
total += 2;
}
function packAnswer(a) {

View File

@ -8,8 +8,12 @@ exports.DNS_PACKER_TYPE_A = function (ab, dv, total, record) {
throw new Error("no address on A record");
}
// RDLENGTH
dv.setUint16(total, 4, false);
total += 2;
// RDATA
// i.e. 127.0.0.1 => 0x7F, 0x00, 0x00, 0x01
//record.rdlength = 4;
record.address.split('.').forEach(function (octet) {
dv.setUint8(total, parseInt(octet, 10), false);
total += 1;

View File

@ -23,6 +23,12 @@ exports.DNS_PACKER_TYPE_AAAA = function (ab, dv, total, record) {
}
}
});
// RDLENGTH
dv.setUint16(total, 16, false);
total += 2;
// RDATA
parts.forEach(function (sedectet) {
dv.setUint16(total, parseInt(sedectet, 16), false);
total += 2;