From 113187e68a7619fa885c64bb924891538d9b8be3 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 17 Feb 2017 18:57:25 -0700 Subject: [PATCH] don't modify original object, chimney --- dns.packer.js | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/dns.packer.js b/dns.packer.js index 60824b7..594283c 100644 --- a/dns.packer.js +++ b/dns.packer.js @@ -23,10 +23,10 @@ var dnspack = exports.DNS_PACKER = { dv.setUint16(0, id, false); dv.setUint16(2, header, false); dv.setUint16(4, packet.question.length, false); - dv.setUint16(6, packet.answer.length, false); - dv.setUint16(8, packet.authority.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); + dv.setUint16(10, (packet.additional||[]).length + (packet.payload ? 1 : 0), false); function lint(r) { if (!r.name) { @@ -37,24 +37,20 @@ var dnspack = exports.DNS_PACKER = { if (!r.className) { throw new Error("no className"); } - r.class = classes[r.className]; } if (!r.type) { if (!r.typeName) { throw new Error("no typeName"); } - r.type = types[r.typeName]; - if (!r.type) { + if (!types[r.typeName]) { console.warn("ignoring invalid type '" + r.type + "' for '" + r.name + "', ignoring"); } } } 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; @@ -88,9 +84,9 @@ var dnspack = exports.DNS_PACKER = { packLabelSequence(q.name, true); - dv.setUint16(total, q.type, false); + dv.setUint16(total, q.type || types[q.typeName], false); total += 2; - dv.setUint16(total, q.class, false); + dv.setUint16(total, q.class || classes[q.className], false); total += 2; } @@ -118,13 +114,10 @@ var dnspack = exports.DNS_PACKER = { } } - //console.log(packet); - // fixed size, other than labels - packet.question.forEach(packQuestion); - packet.answer.forEach(packAnswer); - packet.authority.forEach(packAnswer); - packet.additional.forEach(packAnswer); + (packet.answer||[]).forEach(packAnswer); + (packet.authority||[]).forEach(packAnswer); + (packet.additional||[]).forEach(packAnswer); // TODO handle compression pointers // TODO handle edns