2017-01-28 22:15:27 +00:00
|
|
|
;(function (exports) {
|
2017-01-21 10:15:57 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-01-30 23:30:08 +00:00
|
|
|
var pdns = module.exports.DNS_PARSER = {};
|
2017-01-21 10:15:57 +00:00
|
|
|
|
2017-01-27 00:03:16 +00:00
|
|
|
var classes = exports.DNS_CLASSES || require('./dns.classes.js').DNS_CLASSES;
|
|
|
|
var types = exports.DNS_TYPES || require('./dns.types.js').DNS_TYPES;
|
|
|
|
|
2017-01-21 10:15:57 +00:00
|
|
|
// Order http://www.zytrax.com/books/dns/ch15/
|
|
|
|
|
|
|
|
pdns.unpackHeader = function (i) {
|
|
|
|
// i is one element from a Uint16Array (as a 16-bit unsigned integer)
|
|
|
|
|
|
|
|
var header = {
|
2017-01-21 10:38:25 +00:00
|
|
|
id: 0 // added here to preserve console.log order
|
|
|
|
|
|
|
|
, qr: (i & 0x8000) >> 15 // Query Response (0 is question, 1 is response)
|
2017-01-21 10:15:57 +00:00
|
|
|
, opcode: (i & 0x7800) >> 11 // 0 is question
|
|
|
|
, aa: (i & 0x400) >> 10 // Authoritative Answer (response-only)
|
2017-01-21 17:45:45 +00:00
|
|
|
, 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)
|
2017-01-21 10:15:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return header;
|
|
|
|
};
|
|
|
|
|
2017-01-28 22:15:27 +00:00
|
|
|
pdns._unpackLabels = exports.DNS_UNPACK_LABELS || require('./dns.unpack-labels.js').DNS_UNPACK_LABELS;
|
2017-01-21 10:15:57 +00:00
|
|
|
|
|
|
|
pdns.unpack = function (ab) {
|
|
|
|
if (ab.buffer) {
|
|
|
|
ab = ab.buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SANITY Check
|
|
|
|
if (ab.byteLength < 12) {
|
|
|
|
throw new Error(
|
|
|
|
"A DNS header has a minimum length of 12 bytes but this packet has only " + ab.byteLength + "bytes."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DO: new Uint8Array(arrayBuffer);
|
|
|
|
// DO NOT: Uint8Array.from(arrayBuffer); // WILL NOT WORK
|
|
|
|
|
2017-01-30 18:43:41 +00:00
|
|
|
// DO: new DataView(arrayBuffer).getUint16(7, false);
|
2017-01-21 10:15:57 +00:00
|
|
|
// DO NOT: arrayBuffer.slice();
|
|
|
|
//
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice
|
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
|
|
|
|
var dv = new DataView(ab);
|
2017-01-30 18:43:41 +00:00
|
|
|
var id = dv.getUint16(0, false);
|
|
|
|
var header = pdns.unpackHeader(dv.getUint16(2, false));
|
|
|
|
var qdcount = dv.getUint16(4, false); // query count
|
|
|
|
var ancount = dv.getUint16(6, false); // answer count
|
|
|
|
var nscount = dv.getUint16(8, false); // authority count
|
|
|
|
var arcount = dv.getUint16(10, false); // additional count
|
2017-01-21 10:15:57 +00:00
|
|
|
var total = 12;
|
|
|
|
var i;
|
2017-01-21 17:45:45 +00:00
|
|
|
var rec;
|
2017-01-25 22:04:20 +00:00
|
|
|
var ui8 = new Uint8Array(ab);
|
2017-01-21 10:15:57 +00:00
|
|
|
|
|
|
|
// TODO move to pdns.unpackQuestion to make testable
|
2017-01-25 22:04:20 +00:00
|
|
|
function unpackQuestion(ab, dv, ui8, total) {
|
2017-01-21 17:45:45 +00:00
|
|
|
var ototal = total;
|
2017-01-28 22:15:27 +00:00
|
|
|
var q = pdns._unpackLabels(ui8, total, {
|
|
|
|
name: ''
|
|
|
|
, type: 0
|
|
|
|
, typeName: ''
|
|
|
|
, class: 0
|
|
|
|
, className: ''
|
|
|
|
, byteLength: 0
|
|
|
|
, labels: []
|
|
|
|
, cpcount: 0
|
|
|
|
});
|
2017-01-25 22:04:20 +00:00
|
|
|
//console.log('unpackQuestion QNAME:');
|
|
|
|
//console.log(q);
|
2017-01-25 21:41:30 +00:00
|
|
|
total += q.byteLength;
|
2017-01-21 10:15:57 +00:00
|
|
|
|
2017-01-28 22:15:27 +00:00
|
|
|
|
2017-01-27 00:03:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-21 10:15:57 +00:00
|
|
|
if (ab.byteLength - total < 4) {
|
|
|
|
// console.error(str.join(''));
|
|
|
|
throw new Error(
|
|
|
|
"Expected a 2-byte QTYPE and 2-byte QCLASS following " + total + "-byte QNAME"
|
|
|
|
+ " but packet itself has " + (ab.byteLength - total) + " bytes remaining"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-30 18:43:41 +00:00
|
|
|
q.type = dv.getUint16(total, false);
|
2017-01-21 10:15:57 +00:00
|
|
|
total += 2;
|
2017-01-30 18:43:41 +00:00
|
|
|
q.class = dv.getUint16(total, false);
|
2017-01-21 10:15:57 +00:00
|
|
|
total += 2;
|
2017-01-21 22:11:39 +00:00
|
|
|
q.byteLength = total - ototal;
|
2017-01-27 00:03:16 +00:00
|
|
|
|
|
|
|
q.className = classes[q.class];
|
|
|
|
q.typeName = types[q.type];
|
2017-01-27 02:18:46 +00:00
|
|
|
|
|
|
|
console.log('*********************');
|
|
|
|
console.log(q.typeName);
|
2017-01-28 22:15:27 +00:00
|
|
|
|
2017-01-21 17:45:45 +00:00
|
|
|
return q;
|
2017-01-21 10:15:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 22:04:20 +00:00
|
|
|
function unpackAnswer(ab, dv, ui8, total) {
|
2017-01-21 17:45:45 +00:00
|
|
|
var ototal = total;
|
2017-01-28 22:15:27 +00:00
|
|
|
var q = pdns._unpackLabels(ui8, total, {
|
|
|
|
name: ''
|
|
|
|
, type: 0
|
|
|
|
, typeName: ''
|
|
|
|
, class: 0
|
|
|
|
, className: ''
|
|
|
|
, byteLength: 0
|
|
|
|
, labels: []
|
|
|
|
, cpcount: 0
|
|
|
|
|
|
|
|
, rdstart: 0
|
2017-01-30 18:28:05 +00:00
|
|
|
//, rdata: 0
|
2017-01-28 22:15:27 +00:00
|
|
|
, rdlength: 0
|
|
|
|
});
|
2017-01-25 22:04:20 +00:00
|
|
|
//console.log('unpackAnswer QNAME:');
|
|
|
|
//console.log(q);
|
2017-01-25 21:41:30 +00:00
|
|
|
total += q.byteLength;
|
2017-01-27 00:03:16 +00:00
|
|
|
q.className = classes[q.class];
|
|
|
|
q.typeName = types[q.type];
|
2017-01-21 10:15:57 +00:00
|
|
|
|
|
|
|
if (ab.byteLength - total < 10) {
|
|
|
|
// console.error(str.join(''));
|
|
|
|
throw new Error(
|
|
|
|
"Expected a 2-byte QTYPE, 2-byte QCLASS, 4-byte TTL, and 2-byte RDLENGTH following " + total + "-byte QNAME"
|
|
|
|
+ " but packet itself has " + (ab.byteLength - total) + " bytes remaining"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-30 18:43:41 +00:00
|
|
|
q.type = dv.getUint16(total, false);
|
2017-01-21 10:15:57 +00:00
|
|
|
total += 2;
|
2017-01-30 18:43:41 +00:00
|
|
|
q.class = dv.getUint16(total, false);
|
2017-01-21 10:15:57 +00:00
|
|
|
total += 2;
|
2017-01-30 18:43:41 +00:00
|
|
|
q.ttl = dv.getUint32(total, false);
|
2017-01-21 10:15:57 +00:00
|
|
|
total += 4;
|
2017-01-30 18:43:41 +00:00
|
|
|
q.rdlength = dv.getUint16(total, false);
|
2017-01-21 10:15:57 +00:00
|
|
|
total += 2;
|
2017-01-21 18:02:31 +00:00
|
|
|
|
2017-01-27 00:03:16 +00:00
|
|
|
q.className = classes[q.class];
|
|
|
|
q.typeName = types[q.type];
|
2017-01-28 22:15:27 +00:00
|
|
|
|
2017-01-27 00:03:16 +00:00
|
|
|
|
2017-01-21 18:02:31 +00:00
|
|
|
// TODO actually parse RDATA
|
2017-01-28 22:15:27 +00:00
|
|
|
q.rdstart = total;
|
2017-01-30 18:43:41 +00:00
|
|
|
console.log('q.rdata', q.rdlength, 'bytes:');
|
2017-01-30 18:28:05 +00:00
|
|
|
console.log(new Uint8Array(ab).slice(total, total + q.rdlength));
|
2017-01-25 22:04:20 +00:00
|
|
|
//q.rdata = Array.prototype.slice.apply(q.rdata);
|
2017-01-21 10:15:57 +00:00
|
|
|
|
2017-01-30 18:28:05 +00:00
|
|
|
//q.rdend = q.rdstart + q.rdlength;
|
2017-01-21 10:15:57 +00:00
|
|
|
|
2017-01-30 18:43:41 +00:00
|
|
|
total += q.rdlength;
|
2017-01-21 22:11:39 +00:00
|
|
|
q.byteLength = total - ototal;
|
2017-01-21 17:45:45 +00:00
|
|
|
return q;
|
2017-01-21 10:15:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 10:38:25 +00:00
|
|
|
header.id = id;
|
|
|
|
|
2017-01-21 10:15:57 +00:00
|
|
|
console.log('qdcount', qdcount);
|
2017-02-02 01:28:43 +00:00
|
|
|
header.question = [];
|
2017-01-21 10:15:57 +00:00
|
|
|
for (i = 0; i < qdcount; i += 1) {
|
2017-01-25 22:04:20 +00:00
|
|
|
rec = unpackQuestion(ab, dv, ui8, total);
|
2017-01-21 22:11:39 +00:00
|
|
|
total += rec.byteLength;
|
2017-02-02 01:28:43 +00:00
|
|
|
header.question.push(rec);
|
2017-01-21 10:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log('ancount', ancount);
|
2017-02-02 01:28:43 +00:00
|
|
|
header.answer = [];
|
2017-01-21 10:15:57 +00:00
|
|
|
for (i = 0; i < ancount; i += 1) {
|
2017-01-25 22:04:20 +00:00
|
|
|
rec = unpackAnswer(ab, dv, ui8, total);
|
2017-01-21 22:11:39 +00:00
|
|
|
total += rec.byteLength;
|
2017-02-02 01:28:43 +00:00
|
|
|
header.answer.push(rec);
|
2017-01-21 10:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log('nscount', nscount);
|
|
|
|
header.authority = [];
|
|
|
|
for (i = 0; i < nscount; i += 1) {
|
2017-01-25 22:04:20 +00:00
|
|
|
rec = unpackAnswer(ab, dv, ui8, total);
|
2017-01-21 22:11:39 +00:00
|
|
|
total += rec.byteLength;
|
2017-01-21 18:13:42 +00:00
|
|
|
header.authority.push(rec);
|
2017-01-21 10:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log('arcount', arcount);
|
|
|
|
header.additional = [];
|
|
|
|
for (i = 0; i < arcount; i += 1) {
|
2017-01-25 22:04:20 +00:00
|
|
|
rec = unpackAnswer(ab, dv, ui8, total);
|
2017-01-21 22:11:39 +00:00
|
|
|
total += rec.byteLength;
|
2017-01-21 18:13:42 +00:00
|
|
|
header.additional.push(rec);
|
2017-01-21 10:15:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 18:02:31 +00:00
|
|
|
if (ab.byteLength !== total) {
|
|
|
|
throw new Error(
|
|
|
|
"Parsed " + total + " bytes, but packet is " + ab.byteLength + " bytes."
|
|
|
|
);
|
|
|
|
}
|
2017-01-21 22:11:39 +00:00
|
|
|
header.byteLength = total;
|
2017-01-21 17:45:45 +00:00
|
|
|
return header;
|
2017-01-21 10:15:57 +00:00
|
|
|
};
|
2017-01-30 23:34:11 +00:00
|
|
|
pdns.unpackRdata = exports.DNS_RDATA_PARSE || require('./dns.rdata.parse.js').DNS_RDATA_PARSE;
|
2017-01-30 23:30:08 +00:00
|
|
|
|
2017-01-28 22:15:27 +00:00
|
|
|
}('undefined' !== typeof window ? window : exports));
|