Skip to content

Commit

Permalink
support bit flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Feb 23, 2016
1 parent d939033 commit 1d241f5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -55,13 +56,37 @@ Packets look like this
{
type: 'query|response',
id: optionalIdNumber,
flags: optionalBitFlags,
questions: [...],
answers: [...],
additionals: [...],
authorities: [...]
}
```

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
Expand Down
1 change: 1 addition & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)),
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -118,6 +119,7 @@ tape('response', function (t) {
testEncoder(t, packet, {
type: 'response',
id: 100,
flags: 0,
additionals: [{
type: 'AAAA',
name: 'hello.a.com',
Expand Down

0 comments on commit 1d241f5

Please sign in to comment.