From a7a0b78b41219b40d28d71232f79f8170f528f30 Mon Sep 17 00:00:00 2001 From: vulcainman Date: Mon, 9 Oct 2023 17:07:30 +0200 Subject: [PATCH] Implement QU bit support in question encoder/decoder This commit fix current QU bit implementation that may be used in mDNS protocol. Indeed, until now, if a packet with QU bit set is received, it is decoded as: { ... questions: [ { name: '...', type: 'PTR', class: 'UNKNOWN_32769' }, { name: '...', type: 'PTR', class: 'UNKNOWN_32769' } ], ... } Instead of : { ... questions: [ { name: '...', type: 'PTR', class: 'IN' }, { name: '...', type: 'PTR', class: 'IN' } ], ... } This commit adds a proper QU bit support via the qu_bit field. It enables: - The encoder to parse properly both the class and the QU bit - THe decoder to encode a DNS packet with the QU bit set --- index.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index d55b688..d460e4d 100644 --- a/index.js +++ b/index.js @@ -1609,7 +1609,7 @@ question.encode = function (q, buf, offset) { buf.writeUInt16BE(types.toType(q.type), offset) offset += 2 - buf.writeUInt16BE(classes.toClass(q.class === undefined ? 'IN' : q.class), offset) + buf.writeUInt16BE(((q.qu_bit === undefined || !q.qu_bit) ? 0 : QU_MASK) | classes.toClass(q.class === undefined ? 'IN' : q.class), offset) offset += 2 question.encode.bytes = offset - oldOffset @@ -1630,12 +1630,10 @@ question.decode = function (buf, offset) { q.type = types.toString(buf.readUInt16BE(offset)) offset += 2 - q.class = classes.toString(buf.readUInt16BE(offset)) + q.qu_bit = (buf.readUInt16BE(offset) & QU_MASK) != 0; + q.class = q.qu_bit ? classes.toString(buf.readUInt16BE(offset) - QU_MASK) : classes.toString(buf.readUInt16BE(offset)); offset += 2 - const qu = !!(q.class & QU_MASK) - if (qu) q.class &= NOT_QU_MASK - question.decode.bytes = offset - oldOffset return q }