From 27ddd5f1bfd812d3a1e89a06056805d21de38e1e Mon Sep 17 00:00:00 2001 From: Drew Warren Date: Fri, 17 Feb 2017 15:37:07 -0700 Subject: [PATCH] Add MX Test --- dns.packer.type.cname.js | 2 +- dns.packer.type.mx.js | 41 +++++++++++++++++++++++++++++++++++++++ test/packer.type.cname.js | 2 +- test/packer.type.mx.js | 21 ++++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 dns.packer.type.mx.js create mode 100644 test/packer.type.mx.js diff --git a/dns.packer.type.cname.js b/dns.packer.type.cname.js index 70cd22c..3ca92fe 100644 --- a/dns.packer.type.cname.js +++ b/dns.packer.type.cname.js @@ -13,7 +13,7 @@ exports.DNS_PACKER_TYPE_CNAME = function (ab, dv, total, record) { total += 2; // RDATA - // i.e. 127.0.0.1 => 0x7F, 0x00, 0x00, 0x01 + // a sequence of labels record.data.split('.').forEach(function (label) { cnameLen += 1 + label.length; diff --git a/dns.packer.type.mx.js b/dns.packer.type.mx.js new file mode 100644 index 0000000..910e47c --- /dev/null +++ b/dns.packer.type.mx.js @@ -0,0 +1,41 @@ +(function (exports) { +'use strict'; + +// An 'MX' record is a 32-bit value representing the IP address + +exports.DNS_PACKER_TYPE_MX = function (ab, dv, total, record) { + if (!record.exchange) { + throw new Error("no exchange for MX record"); + } + if (!record.priority) { + // TODO: Check that number is in range 1-64k + throw new Error("no priority for MX record"); + } + + var mxLen = 2; // 16-bit priority plus label sequence + var rdLenIndex = total; + total += 2; // The space for RDLENGTH is reserved now and set below + dv.setUint16(total, parseInt(record.priority, 10), false); + total += 2; + + // RDATA + // 16-bit priority and a sequence of labels as the exchange + record.exchange.split('.').forEach(function (label) { + mxLen += 1 + label.length; + + dv.setUint8(total, label.length, false); + total += 1; + + label.split('').forEach(function (ch) { + dv.setUint8(total, ch.charCodeAt(0), false); + total += 1; + }); + }); + + // RDLENGTH + dv.setUint16(rdLenIndex, mxLen, false); + + return total; +}; + +}('undefined' !== typeof window ? window : exports)); diff --git a/test/packer.type.cname.js b/test/packer.type.cname.js index d18d8ee..c69355f 100644 --- a/test/packer.type.cname.js +++ b/test/packer.type.cname.js @@ -1,7 +1,7 @@ 'use strict'; var packer = require('../dns.packer.type.cname.js').DNS_PACKER_TYPE_CNAME; -var ab = new ArrayBuffer(255); // max len per label is 63, max label sequence is 254 + null terminator +var ab = new ArrayBuffer(254); // max len per label is 63, max label sequence is 254 var dv = new DataView(ab); var total = 0; diff --git a/test/packer.type.mx.js b/test/packer.type.mx.js new file mode 100644 index 0000000..34ee983 --- /dev/null +++ b/test/packer.type.mx.js @@ -0,0 +1,21 @@ +'use strict'; + +var packer = require('../dns.packer.type.mx.js').DNS_PACKER_TYPE_MX; +var ab = new ArrayBuffer(256); // priority + max len per label is 63, max label sequence is 254 +var dv = new DataView(ab); +var total = 0; + +[ [ '10:www.example.com' + , [ 0x00, 0x12, 0x00, 0x0A, 0x03, 119, 119, 119, 0x07, 101, 120, 97, 109, 112, 108, 101, 0x03, 99, 111, 109 ].join(' ') ] +].forEach(function (mx) { + total = packer(ab, dv, total, { priority: mx[0].split(':')[0], exchange: mx[0].split(':')[1] }); + ab = ab.slice(0, total); + // TODO: Check that total increments appropriately + if (mx[1] !== new Uint8Array(ab).join(' ')) { + console.error("expected: ", mx[1]); + console.error("actual: ", new Uint8Array(ab).join(' ')); + process.exit(1); + } +}); + +console.log('PASS');