From 908fd5314c201b2a46037a31f9ac80d3106c24cc Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 19 Oct 2017 11:50:29 -0600 Subject: [PATCH] bugfix: pack multiple TXT strings correctly --- packer/type.txt.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/packer/type.txt.js b/packer/type.txt.js index e63c36e..63e64fc 100644 --- a/packer/type.txt.js +++ b/packer/type.txt.js @@ -2,32 +2,44 @@ 'use strict'; // Record type is just any text. +// RFC 1035 - 3.3. Standard RRs - +// RFC 1035 - 3.3.14. TXT RDATA format exports.DNS_PACKER_TYPE_TXT = function (ab, dv, total, record) { - if (!record.data){ - throw new Error("no data for TXT record"); + if ('string' === typeof record.data) { + console.warn("[WARN] 'data' should be an array of strings, not a single string"); + record.data = [ record.data ]; + } + if (!Array.isArray(record.data) || !record.data.length){ + throw new Error("bad data or no data for TXT record"); } var txtLen = 0; var rdLenIndex = total; - total += 3; + total += 2; // saving space for rdata length // RDATA record.data.forEach(function(str){ - str.split('').forEach(function(ch){ + if (str.length > 255) { + throw new Error("bad TXT string length " + str.length + " (max 255)"); + } - txtLen += 1; - // console.log(chcim); - dv.setUint8(total, ch.charCodeAt(0), false); + // size of this string + dv.setUint8(total, str.length, false); total += 1; + txtLen += 1; + + str.split('').forEach(function(ch) { + + dv.setUint8(total, ch.charCodeAt(0), false); + total += 1; + txtLen += 1; }); }); - dv.setUint16(rdLenIndex, txtLen+1, false); - dv.setUint8(rdLenIndex+2, txtLen, false); - // total +=1; + dv.setUint16(rdLenIndex, txtLen, false); return total; };