forked from LittleChest/dns-packet
support bit flags
This commit is contained in:
parent
d939033c83
commit
1d241f5f97
25
README.md
25
README.md
@ -20,6 +20,7 @@ var socket = dgram.createSocket('udp4')
|
|||||||
var buf = packet.encode({
|
var buf = packet.encode({
|
||||||
type: 'query',
|
type: 'query',
|
||||||
id: 1,
|
id: 1,
|
||||||
|
flags: packet.RECURSION_DESIRED,
|
||||||
questions: [{
|
questions: [{
|
||||||
type: 'A',
|
type: 'A',
|
||||||
name: 'google.com'
|
name: 'google.com'
|
||||||
@ -55,6 +56,7 @@ Packets look like this
|
|||||||
{
|
{
|
||||||
type: 'query|response',
|
type: 'query|response',
|
||||||
id: optionalIdNumber,
|
id: optionalIdNumber,
|
||||||
|
flags: optionalBitFlags,
|
||||||
questions: [...],
|
questions: [...],
|
||||||
answers: [...],
|
answers: [...],
|
||||||
additionals: [...],
|
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
|
A question looks like this
|
||||||
|
|
||||||
``` js
|
``` js
|
||||||
|
|||||||
@ -6,6 +6,7 @@ var socket = dgram.createSocket('udp4')
|
|||||||
var buf = packet.encode({
|
var buf = packet.encode({
|
||||||
type: 'query',
|
type: 'query',
|
||||||
id: 1,
|
id: 1,
|
||||||
|
flags: packet.RECURSION_DESIRED,
|
||||||
questions: [{
|
questions: [{
|
||||||
type: 'A',
|
type: 'A',
|
||||||
name: 'google.com'
|
name: 'google.com'
|
||||||
|
|||||||
16
index.js
16
index.js
@ -101,8 +101,11 @@ header.encode = function (h, buf, offset) {
|
|||||||
if (!buf) buf = header.encodingLength(h)
|
if (!buf) buf = header.encodingLength(h)
|
||||||
if (!offset) offset = 0
|
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.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.questions.length, offset + 4)
|
||||||
buf.writeUInt16BE(h.answers.length, offset + 6)
|
buf.writeUInt16BE(h.answers.length, offset + 6)
|
||||||
buf.writeUInt16BE(h.authorities.length, offset + 8)
|
buf.writeUInt16BE(h.authorities.length, offset + 8)
|
||||||
@ -116,10 +119,12 @@ header.encode.bytes = 12
|
|||||||
header.decode = function (buf, offset) {
|
header.decode = function (buf, offset) {
|
||||||
if (!offset) offset = 0
|
if (!offset) offset = 0
|
||||||
if (buf.length < 12) throw new Error('Header must be 12 bytes')
|
if (buf.length < 12) throw new Error('Header must be 12 bytes')
|
||||||
|
var flags = buf.readUInt16BE(offset + 2)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: buf.readUInt16BE(offset),
|
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)),
|
questions: new Array(buf.readUInt16BE(offset + 4)),
|
||||||
answers: new Array(buf.readUInt16BE(offset + 6)),
|
answers: new Array(buf.readUInt16BE(offset + 6)),
|
||||||
authorities: new Array(buf.readUInt16BE(offset + 8)),
|
authorities: new Array(buf.readUInt16BE(offset + 8)),
|
||||||
@ -503,6 +508,13 @@ question.encodingLength = function (q) {
|
|||||||
return name.encodingLength(q.name) + 4
|
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) {
|
exports.encode = function (result, buf, offset) {
|
||||||
if (!buf) buf = Buffer(exports.encodingLength(result))
|
if (!buf) buf = Buffer(exports.encodingLength(result))
|
||||||
if (!offset) offset = 0
|
if (!offset) offset = 0
|
||||||
|
|||||||
2
test.js
2
test.js
@ -97,6 +97,7 @@ tape('query', function (t) {
|
|||||||
tape('response', function (t) {
|
tape('response', function (t) {
|
||||||
testEncoder(t, packet, {
|
testEncoder(t, packet, {
|
||||||
type: 'response',
|
type: 'response',
|
||||||
|
flags: packet.TRUNCATED_RESPONSE,
|
||||||
answers: [{
|
answers: [{
|
||||||
type: 'A',
|
type: 'A',
|
||||||
name: 'hello.a.com',
|
name: 'hello.a.com',
|
||||||
@ -118,6 +119,7 @@ tape('response', function (t) {
|
|||||||
testEncoder(t, packet, {
|
testEncoder(t, packet, {
|
||||||
type: 'response',
|
type: 'response',
|
||||||
id: 100,
|
id: 100,
|
||||||
|
flags: 0,
|
||||||
additionals: [{
|
additionals: [{
|
||||||
type: 'AAAA',
|
type: 'AAAA',
|
||||||
name: 'hello.a.com',
|
name: 'hello.a.com',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user