dns-suite.js/packer/type.txt.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-03-19 16:31:44 +00:00
// (function (exports) {
2017-03-10 02:34:27 +00:00
'use strict';
// Record type is just any text.
// RFC 1035 - 3.3. Standard RRs - <character-string>
// RFC 1035 - 3.3.14. TXT RDATA format
2017-03-10 02:34:27 +00:00
exports.DNS_PACKER_TYPE_TXT = function (ab, dv, total, 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");
}
2017-03-10 02:34:27 +00:00
var txtLen = 0;
var rdLenIndex = total;
total += 2; // saving space for rdata length
2017-03-10 02:34:27 +00:00
// RDATA
2017-03-23 23:33:55 +00:00
record.data.forEach(function(str){
2017-03-10 02:34:27 +00:00
if (str.length > 255) {
throw new Error("bad TXT string length " + str.length + " (max 255)");
}
2017-03-10 02:34:27 +00:00
// size of this string
dv.setUint8(total, str.length, false);
2017-03-23 23:33:55 +00:00
total += 1;
txtLen += 1;
str.split('').forEach(function(ch) {
dv.setUint8(total, ch.charCodeAt(0), false);
total += 1;
txtLen += 1;
2017-09-25 21:57:40 +00:00
});
});
2017-03-10 02:34:27 +00:00
dv.setUint16(rdLenIndex, txtLen, false);
2017-03-10 02:34:27 +00:00
return total;
2017-03-10 02:34:27 +00:00
};
2018-03-19 16:31:44 +00:00
// }('undefined' !== typeof window ? window : exports));