From 087d59bb636cccee86ddef9d3d6ab2d0439a8c77 Mon Sep 17 00:00:00 2001 From: silverwind Date: Sun, 11 Feb 2018 11:06:22 +0100 Subject: [PATCH] add MX record type Fixes: https://github.com/mafintosh/dns-packet/issues/10 --- README.md | 9 +++++++++ index.js | 41 +++++++++++++++++++++++++++++++++++++++++ test.js | 6 ++++++ 3 files changed, 56 insertions(+) diff --git a/README.md b/README.md index 18088f8..1a49636 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,15 @@ When encoding, scalar values are converted to an array and strings are converted } ``` +#### `MX` + +``` js +{ + preference: 10, + exchange: 'mail.example.net' +} +``` + If you need another one, open an issue and we'll try to add it. ## License diff --git a/index.js b/index.js index eff7871..e09fc51 100644 --- a/index.js +++ b/index.js @@ -564,6 +564,46 @@ rcaa.encodingLength = function (data) { return string.encodingLength(data.tag) + string.encodingLength(data.value) + 2 } +const rmx = exports.mx = {} + +rmx.encode = function (data, buf, offset) { + if (!buf) buf = Buffer.allocUnsafe(rmx.encodingLength(data)) + if (!offset) offset = 0 + + const oldOffset = offset + offset += 2 + buf.writeUInt16BE(data.preference || 0, offset) + offset += 2 + name.encode(data.exchange, buf, offset) + offset += name.encode.bytes + + buf.writeUInt16BE(offset - oldOffset - 2, oldOffset) + rmx.encode.bytes = offset - oldOffset + return buf +} + +rmx.encode.bytes = 0 + +rmx.decode = function (buf, offset) { + if (!offset) offset = 0 + + const oldOffset = offset + + const data = {} + offset += 2 + data.preference = buf.readUInt16BE(offset) + offset += 2 + data.exchange = name.decode(buf, offset) + offset += name.decode.bytes + + rmx.decode.bytes = offset - oldOffset + return data +} + +rmx.encodingLength = function (data) { + return 4 + name.encodingLength(data.exchange) +} + const ra = exports.a = {} ra.encode = function (host, buf, offset) { @@ -638,6 +678,7 @@ const renc = exports.record = function (type) { case 'CAA': return rcaa case 'NS': return rns case 'SOA': return rsoa + case 'MX': return rmx } return runknown } diff --git a/test.js b/test.js index d610ec1..4956d67 100644 --- a/test.js +++ b/test.js @@ -83,6 +83,12 @@ tape('caa', function (t) { t.end() }) +tape('mx', function (t) { + testEncoder(t, packet.mx, {preference: 10, exchange: 'mx.hello.world.com'}) + testEncoder(t, packet.mx, {exchange: 'mx.hello.world.com'}) + t.end() +}) + tape('ns', function (t) { testEncoder(t, packet.ns, 'ns.world.com') t.end()