From b5c0ab3161789533f9db3c8f38cabb1759ee8683 Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 22 Jan 2019 22:37:33 +0100 Subject: [PATCH] fix RP handling --- README.md | 4 ++-- index.js | 26 +++++++++++++------------- test.js | 7 ++++--- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 00f02a1..87f62b2 100644 --- a/README.md +++ b/README.md @@ -293,8 +293,8 @@ And an answer, additional, or authority looks like this ``` js { - mbox: mbox-dname, - txt: txt-dname + mbox: 'admin.example.com', + txt: 'txt.example.com' } ``` diff --git a/index.js b/index.js index fb6e171..b2ed655 100644 --- a/index.js +++ b/index.js @@ -881,15 +881,17 @@ rrrsig.encodingLength = function (sig) { const rrp = exports.rp = {} rrp.encode = function (data, buf, offset) { - data.txt = data.txt || '.' if (!buf) buf = Buffer.allocUnsafe(rrp.encodingLength(data)) if (!offset) offset = 0 + const oldOffset = offset - string.encode(data.mbox, buf, offset) - offset += string.encode.bytes - string.encode(data.txt, buf, offset) - - rrp.encode.bytes = rrp.encodingLength(data) + offset += 2 // Leave space for length + name.encode(data.mbox || '.', buf, offset) + offset += name.encode.bytes + name.encode(data.txt || '.', buf, offset) + offset += name.encode.bytes + rrp.encode.bytes = offset - oldOffset + buf.writeUInt16BE(rrp.encode.bytes - 2, oldOffset) return buf } @@ -897,15 +899,13 @@ rrp.encode.bytes = 0 rrp.decode = function (buf, offset) { if (!offset) offset = 0 - const oldOffset = offset const data = {} - data.mbox = string.decode(buf, offset) - offset += string.decode.bytes - data.txt = string.decode(buf, offset) - offset += string.decode.bytes - + offset += 2 + data.mbox = name.decode(buf, offset) || '.' + offset += name.decode.bytes + data.txt = name.decode(buf, offset) || '.' rrp.decode.bytes = offset - oldOffset return data } @@ -913,7 +913,7 @@ rrp.decode = function (buf, offset) { rrp.decode.bytes = 0 rrp.encodingLength = function (data) { - return string.encodingLength(data.mbox || data) + string.encodingLength(data.txt || '.') + 2 + return 2 + name.encodingLength(data.mbox) + name.encodingLength(data.txt || '.') } const typebitmap = {} diff --git a/test.js b/test.js index 936eae8..c49bf8f 100644 --- a/test.js +++ b/test.js @@ -401,11 +401,12 @@ tape('rrsig', function (t) { tape('rrp', function (t) { testEncoder(t, packet.rp, { - mbox: 'rp.world.com', - txt: 'hello' + mbox: 'foo.bar.com', + txt: 'baz.bar.com' }) testEncoder(t, packet.rp, { - mbox: 'rp.world.com' + mbox: 'rp.world.com', + txt: '.' }) t.end() })