2017-01-30 23:30:08 +00:00
|
|
|
(function (exports) {
|
|
|
|
'use strict';
|
|
|
|
|
2017-02-11 22:41:01 +00:00
|
|
|
var dnspack = exports.DNS_PACKER = {
|
|
|
|
pack: function (packet) {
|
|
|
|
var total = 0;
|
|
|
|
var id = packet.header.id; // 2 bytes
|
|
|
|
var header = dnspack.packHeader(packet.header); // 2 bytes
|
2017-02-11 23:01:02 +00:00
|
|
|
total = 12; // 2+2+2+2 bytes ({qd,an,ns,ar}count)
|
2017-02-11 22:41:01 +00:00
|
|
|
|
|
|
|
var ab = new ArrayBuffer(total);
|
|
|
|
var dv = new DataView(ab);
|
|
|
|
dv.setUint16(0, id, false);
|
|
|
|
dv.setUint16(2, header, false);
|
2017-02-11 23:01:02 +00:00
|
|
|
dv.setUint16(4, packet.question.length, false);
|
|
|
|
dv.setUint16(6, packet.answer.length, false);
|
|
|
|
dv.setUint16(8, packet.authority.length, false);
|
|
|
|
// EDNS is added as an additional with TYPE 41 (OPT, 0x29)
|
|
|
|
dv.setUint16(10, packet.additional.length + (packet.payload ? 1 : 0), false);
|
2017-02-11 22:41:01 +00:00
|
|
|
|
|
|
|
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;
|
2017-01-30 23:30:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}('undefined' !== typeof window ? window : exports));
|