bugfix: pack multiple TXT strings correctly

This commit is contained in:
AJ ONeal 2017-10-19 11:50:29 -06:00
parent d743b8f3d0
commit 908fd5314c
1 changed files with 22 additions and 10 deletions

View File

@ -2,32 +2,44 @@
'use strict'; 'use strict';
// Record type is just any text. // Record type is just any text.
// RFC 1035 - 3.3. Standard RRs - <character-string>
// RFC 1035 - 3.3.14. TXT RDATA format
exports.DNS_PACKER_TYPE_TXT = function (ab, dv, total, record) { exports.DNS_PACKER_TYPE_TXT = function (ab, dv, total, record) {
if (!record.data){ if ('string' === typeof record.data) {
throw new Error("no data for TXT record"); 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 txtLen = 0;
var rdLenIndex = total; var rdLenIndex = total;
total += 3; total += 2; // saving space for rdata length
// RDATA // RDATA
record.data.forEach(function(str){ 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; // size of this string
// console.log(chcim); dv.setUint8(total, str.length, false);
dv.setUint8(total, ch.charCodeAt(0), false);
total += 1; 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.setUint16(rdLenIndex, txtLen, false);
dv.setUint8(rdLenIndex+2, txtLen, false);
// total +=1;
return total; return total;
}; };