dns-suite.js/pure-parser.js

253 lines
6.8 KiB
JavaScript

'use strict';
var pdns = module.exports;
var classes = exports.DNS_CLASSES || require('./dns.classes.js').DNS_CLASSES;
var types = exports.DNS_TYPES || require('./dns.types.js').DNS_TYPES;
// 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 = {
id: 0 // added here to preserve console.log order
, qr: (i & 0x8000) >> 15 // Query Response (0 is question, 1 is response)
, opcode: (i & 0x7800) >> 11 // 0 is question
, aa: (i & 0x400) >> 10 // Authoritative Answer (response-only)
, 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)
};
return header;
};
pdns.unpackQname = function (ui8, ptr, q) {
return pdns._unpackQname(ui8, ptr, q || {
name: ''
, type: 0
, typeName: ''
, class: 0
, className: ''
, byteLength: 0
, labels: []
, cpcount: 0
});
};
pdns._unpackQname = function (ui8, ptr, q) {
if (q.cpcount > 25) {
throw new Error("compression pointer loop detected (over 25 pointers seems like a loop)");
}
var total = ptr;
var i;
var len;
var label = [];
do {
label.length = 0;
len = ui8[total];
if (0xc0 === len) {
var cpptr = ui8[total + 1];
// we're not coming back!
ptr = cpptr;
q.cpcount += 1;
q.byteLength += 2; // cp and len
return pdns.unpackQname(ui8, ptr, q);
}
//str.length = 0; // fast empty array
if (ui8.byteLength - total < len) {
throw new Error(
"Expected a string of length " + len
+ " but packet itself has " + (ui8.byteLength - total) + " bytes remaining"
);
}
for (i = 0; i < len; i += 1) {
total += 1;
// TODO check url-allowable characters
label.push(String.fromCharCode(ui8[total]));
}
if (label.length) {
q.labels.push(label.join(''));
}
total += 1;
//console.log('total', total, ui8[total], String.fromCharCode(ui8[total]));
} while (len);
//str.pop(); // remove trailing '.'
q.name = q.labels.join('.');
if (0 === q.cpcount) {
q.byteLength = total - ptr;
}
return q;
};
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
// DO: new DataView(arrayBuffer).getUint16(7);
// 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);
var id = dv.getUint16(0);
var header = pdns.unpackHeader(dv.getUint16(2));
var qdcount = dv.getUint16(4); // query count
var ancount = dv.getUint16(6); // answer count
var nscount = dv.getUint16(8); // authority count
var arcount = dv.getUint16(10); // additional count
var total = 12;
var i;
var rec;
var ui8 = new Uint8Array(ab);
// TODO move to pdns.unpackQuestion to make testable
function unpackQuestion(ab, dv, ui8, total) {
var ototal = total;
var q = pdns.unpackQname(ui8, total);
//console.log('unpackQuestion QNAME:');
//console.log(q);
total += q.byteLength;
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"
);
}
q.type = dv.getUint16(total);
total += 2;
q.class = dv.getUint16(total);
total += 2;
q.byteLength = total - ototal;
q.className = classes[q.class];
q.typeName = types[q.type];
console.log('*********************');
console.log(q.typeName);
return q;
}
function unpackAnswer(ab, dv, ui8, total) {
var ototal = total;
var q = pdns.unpackQname(ui8, total);
//console.log('unpackAnswer QNAME:');
//console.log(q);
total += q.byteLength;
q.className = classes[q.class];
q.typeName = types[q.type];
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"
);
}
q.type = dv.getUint16(total);
total += 2;
q.class = dv.getUint16(total);
total += 2;
q.ttl = dv.getUint32(total);
total += 4;
q.rdlength = dv.getUint16(total);
total += 2;
q.className = classes[q.class];
q.typeName = types[q.type];
// TODO actually parse RDATA
q.rdata = new Uint8Array(ab).slice(total, total + q.rdlength);
console.log('q.rdata', q.rdata.byteLength, 'bytes:');
console.log(q.rdata);
//q.rdata = Array.prototype.slice.apply(q.rdata);
total += q.rdlength;
q.byteLength = total - ototal;
return q;
}
header.id = id;
console.log('qdcount', qdcount);
header.questions = [];
for (i = 0; i < qdcount; i += 1) {
rec = unpackQuestion(ab, dv, ui8, total);
total += rec.byteLength;
header.questions.push(rec);
}
console.log('ancount', ancount);
header.answers = [];
for (i = 0; i < ancount; i += 1) {
rec = unpackAnswer(ab, dv, ui8, total);
total += rec.byteLength;
header.answers.push(rec);
}
console.log('nscount', nscount);
header.authority = [];
for (i = 0; i < nscount; i += 1) {
rec = unpackAnswer(ab, dv, ui8, total);
total += rec.byteLength;
header.authority.push(rec);
}
console.log('arcount', arcount);
header.additional = [];
for (i = 0; i < arcount; i += 1) {
rec = unpackAnswer(ab, dv, ui8, total);
total += rec.byteLength;
header.additional.push(rec);
}
if (ab.byteLength !== total) {
throw new Error(
"Parsed " + total + " bytes, but packet is " + ab.byteLength + " bytes."
);
}
header.byteLength = total;
return header;
};
pdns.unpackRdata = require('./dns.rdata.parse.js').DNS_RDATA_PARSE;