begin packer and test

This commit is contained in:
AJ ONeal 2017-02-11 15:41:01 -07:00
parent 037247c91b
commit af89bfa000
3 changed files with 74 additions and 5 deletions

View File

@ -1,9 +1,36 @@
(function (exports) {
'use strict';
exports.DNS_PACKER = {
pack: function () {
throw new Error("DNS Packer Not Implemented");
var dnspack = exports.DNS_PACKER = {
pack: function (packet) {
var total = 0;
var id = packet.header.id; // 2 bytes
total += 2;
var header = dnspack.packHeader(packet.header); // 2 bytes
total += 2;
var ab = new ArrayBuffer(total);
var dv = new DataView(ab);
dv.setUint16(0, id, false);
dv.setUint16(2, header, false);
return ab;
}
, packHeader: function(h) {
var val = 0;
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;
return val;
}
};

View File

@ -46,7 +46,7 @@ pdns.unpackOpt = function (ab, packet, rec) {
// TODO index into the correct place in the ArrayBuffer
dv = new DataView(new ArrayBuffer(4));
// rec.ttl is actually 1 (extended_rcode), 1 (edns_version), 2:1 (DO), 2:7 (Z)
dv.setUint32(0, packet.ttl);
dv.setUint32(0, packet.ttl, false);
// is xrcode this edns_options?
packet.xrcode = '0x' + dv.getUint8(0, false).toString(16);
if ('0x0' === packet.xrcode) {

View File

@ -1,6 +1,48 @@
;(function () {
'use strict';
throw new Error("packer tests not written");
var fs = require('fs');
var path = require('path');
var dnsjs = require('../').DNSPacket;
var dirname = path.join(__dirname, 'fixtures');
var expected; // shim
var onefile = process.argv[2];
var json = JSON.parse(fs.readFileSync(onefile, 'utf8'));
var ab = dnsjs.write(json);
//console.log(ab);
var ui8 = new Uint8Array(ab);
//console.log(ui8);
var bytecount = 0;
var str = [].slice.call(ui8).map(function (i) {
var h = i.toString(16);
if (h.length < 2) {
h = '0' + h;
}
return h;
}).join('').match(/.{1,2}/g).join(' ').match(/.{1,47}/g).map(function (str) {
var lead = bytecount.toString(16);
bytecount += 10;
while (lead.length < 8) {
lead = '0' + lead;
}
return lead + ' ' + str;
}).join('\n');
var trail = ab.byteLength.toString(16);
while (trail.length < 8) {
trail = '0' + trail;
}
console.log('');
console.log('DEBUG with hexdump: ');
console.log('hexdump ' + onefile.replace(/\.[^\.]*$/, '.bin'));
console.log('');
console.log(str + '\n' + trail);
console.log('');
}());