From 1d241f5f9728334ff51d3d5aeadb75cb297542da Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Mon, 22 Feb 2016 17:37:12 -0800 Subject: [PATCH] support bit flags --- README.md | 25 +++++++++++++++++++++++++ example.js | 1 + index.js | 16 ++++++++++++++-- test.js | 2 ++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eba92f7..5105895 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ var socket = dgram.createSocket('udp4') var buf = packet.encode({ type: 'query', id: 1, + flags: packet.RECURSION_DESIRED, questions: [{ type: 'A', name: 'google.com' @@ -55,6 +56,7 @@ Packets look like this { type: 'query|response', id: optionalIdNumber, + flags: optionalBitFlags, questions: [...], answers: [...], additionals: [...], @@ -62,6 +64,29 @@ Packets look like this } ``` +The bit flags available are + +``` js +packet.RECURSION_DESIRED +packet.RECURSION_AVAILABLE +packet.TRUNCATED_RESPONSE +packet.AUTHORITATIVE_ANSWER +packet.AUTHENTIC_DATA +packet.CHECKING_DISABLED +``` + +To use more than one flag bitwise-or them together + +``` js +var flags = packet.RECURSION_DESIRED | packet.RECURSION_AVAILABLE +``` + +And to check for a flag use bitwise-and + +``` js +var isRecursive = message.flags & packet.RECURSION_DESIRED +``` + A question looks like this ``` js diff --git a/example.js b/example.js index 5ddb4ee..7d07820 100644 --- a/example.js +++ b/example.js @@ -6,6 +6,7 @@ var socket = dgram.createSocket('udp4') var buf = packet.encode({ type: 'query', id: 1, + flags: packet.RECURSION_DESIRED, questions: [{ type: 'A', name: 'google.com' diff --git a/index.js b/index.js index c7f0f03..eefec31 100644 --- a/index.js +++ b/index.js @@ -101,8 +101,11 @@ header.encode = function (h, buf, offset) { if (!buf) buf = header.encodingLength(h) if (!offset) offset = 0 + var flags = (h.flags || 0) & 32767 + var type = h.type === 'response' ? RESPONSE_FLAG : QUERY_FLAG + buf.writeUInt16BE(h.id || 0, offset) - buf.writeUInt16BE(h.type === 'response' ? RESPONSE_FLAG : QUERY_FLAG, offset + 2) + buf.writeUInt16BE(flags | type, offset + 2) buf.writeUInt16BE(h.questions.length, offset + 4) buf.writeUInt16BE(h.answers.length, offset + 6) buf.writeUInt16BE(h.authorities.length, offset + 8) @@ -116,10 +119,12 @@ header.encode.bytes = 12 header.decode = function (buf, offset) { if (!offset) offset = 0 if (buf.length < 12) throw new Error('Header must be 12 bytes') + var flags = buf.readUInt16BE(offset + 2) return { id: buf.readUInt16BE(offset), - type: buf.readUInt16BE(offset + 2) & RESPONSE_FLAG ? 'response' : 'query', + type: flags & RESPONSE_FLAG ? 'response' : 'query', + flags: flags & 32767, questions: new Array(buf.readUInt16BE(offset + 4)), answers: new Array(buf.readUInt16BE(offset + 6)), authorities: new Array(buf.readUInt16BE(offset + 8)), @@ -503,6 +508,13 @@ question.encodingLength = function (q) { return name.encodingLength(q.name) + 4 } +exports.AUTHORITATIVE_ANSWER = 1 << 10 +exports.TRUNCATED_RESPONSE = 1 << 9 +exports.RECURSION_DESIRED = 1 << 8 +exports.RECURSION_AVAILABLE = 1 << 7 +exports.AUTHENTIC_DATA = 1 << 5 +exports.CHECKING_DISABLED = 1 << 4 + exports.encode = function (result, buf, offset) { if (!buf) buf = Buffer(exports.encodingLength(result)) if (!offset) offset = 0 diff --git a/test.js b/test.js index 4176482..f089186 100644 --- a/test.js +++ b/test.js @@ -97,6 +97,7 @@ tape('query', function (t) { tape('response', function (t) { testEncoder(t, packet, { type: 'response', + flags: packet.TRUNCATED_RESPONSE, answers: [{ type: 'A', name: 'hello.a.com', @@ -118,6 +119,7 @@ tape('response', function (t) { testEncoder(t, packet, { type: 'response', id: 100, + flags: 0, additionals: [{ type: 'AAAA', name: 'hello.a.com',