Change class option to take strings and document it (#4)

This changes to class option to take strings instead of integers, so 1
becomes 'IN', 3 becomes 'CH' and so on. Also documented the class
option.
This commit is contained in:
silverwind 2018-01-12 21:20:38 +01:00 committed by GitHub
parent a77e1bbcc7
commit 3b76b23221
Signed by: Github
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 5 deletions

View File

@ -23,6 +23,7 @@ var buf = packet.encode({
flags: packet.RECURSION_DESIRED, flags: packet.RECURSION_DESIRED,
questions: [{ questions: [{
type: 'A', type: 'A',
class: 'IN',
name: 'google.com' name: 'google.com'
}] }]
}) })
@ -92,6 +93,7 @@ A question looks like this
``` js ``` js
{ {
type: 'A', // or SRV, AAAA, etc type: 'A', // or SRV, AAAA, etc
class: 'IN', // one of IN, CS, CH, HS, ANY. Default: IN
name: 'google.com' // which record are you looking for name: 'google.com' // which record are you looking for
} }
``` ```
@ -101,6 +103,7 @@ And an answers, additional, or authority looks like this
``` js ``` js
{ {
type: 'A', // or SRV, AAAA, etc type: 'A', // or SRV, AAAA, etc
class: 'IN', // one of IN, CS, CH, HS
name: 'google.com', // which name is this record for name: 'google.com', // which name is this record for
ttl: optionalTimeToLiveInSeconds, ttl: optionalTimeToLiveInSeconds,
(record specific data, see below) (record specific data, see below)

23
classes.js Normal file
View File

@ -0,0 +1,23 @@
'use strict'
exports.toString = function (klass) {
switch (klass) {
case 1: return 'IN'
case 2: return 'CS'
case 3: return 'CH'
case 4: return 'HS'
case 255: return 'ANY'
}
return 'UNKNOWN_' + klass
}
exports.toClass = function (name) {
switch (name.toUpperCase()) {
case 'IN': return 1
case 'CS': return 2
case 'CH': return 3
case 'HS': return 4
case 'ANY': return 255
}
return 0
}

View File

@ -3,6 +3,7 @@
const types = require('./types') const types = require('./types')
const rcodes = require('./rcodes') const rcodes = require('./rcodes')
const opcodes = require('./opcodes') const opcodes = require('./opcodes')
const classes = require('./classes')
const ip = require('ip') const ip = require('ip')
const Buffer = require('safe-buffer').Buffer const Buffer = require('safe-buffer').Buffer
@ -586,7 +587,7 @@ answer.encode = function (a, buf, offset) {
buf.writeUInt16BE(types.toType(a.type), offset) buf.writeUInt16BE(types.toType(a.type), offset)
let klass = a.class === undefined ? 1 : a.class let klass = classes.toClass(a.class === undefined ? 'IN' : a.class)
if (a.flush) klass |= FLUSH_MASK // the 1st bit of the class is the flush bit if (a.flush) klass |= FLUSH_MASK // the 1st bit of the class is the flush bit
buf.writeUInt16BE(klass, offset + 2) buf.writeUInt16BE(klass, offset + 2)
@ -611,7 +612,7 @@ answer.decode = function (buf, offset) {
a.name = name.decode(buf, offset) a.name = name.decode(buf, offset)
offset += name.decode.bytes offset += name.decode.bytes
a.type = types.toString(buf.readUInt16BE(offset)) a.type = types.toString(buf.readUInt16BE(offset))
a.class = buf.readUInt16BE(offset + 2) a.class = classes.toString(buf.readUInt16BE(offset + 2))
a.ttl = buf.readUInt32BE(offset + 4) a.ttl = buf.readUInt32BE(offset + 4)
a.flush = !!(a.class & FLUSH_MASK) a.flush = !!(a.class & FLUSH_MASK)
@ -645,7 +646,7 @@ question.encode = function (q, buf, offset) {
buf.writeUInt16BE(types.toType(q.type), offset) buf.writeUInt16BE(types.toType(q.type), offset)
offset += 2 offset += 2
buf.writeUInt16BE(q.class === undefined ? 1 : q.class, offset) buf.writeUInt16BE(classes.toClass(q.class === undefined ? 'IN' : q.class), offset)
offset += 2 offset += 2
question.encode.bytes = offset - oldOffset question.encode.bytes = offset - oldOffset
@ -666,7 +667,7 @@ question.decode = function (buf, offset) {
q.type = types.toString(buf.readUInt16BE(offset)) q.type = types.toString(buf.readUInt16BE(offset))
offset += 2 offset += 2
q.class = buf.readUInt16BE(offset) q.class = classes.toString(buf.readUInt16BE(offset))
offset += 2 offset += 2
const qu = !!(q.class & QU_MASK) const qu = !!(q.class & QU_MASK)

View File

@ -101,6 +101,7 @@ tape('query', function (t) {
id: 42, id: 42,
questions: [{ questions: [{
type: 'A', type: 'A',
class: 'IN',
name: 'hello.a.com' name: 'hello.a.com'
}, { }, {
type: 'SRV', type: 'SRV',
@ -113,7 +114,7 @@ tape('query', function (t) {
id: 42, id: 42,
questions: [{ questions: [{
type: 'A', type: 'A',
class: 100, class: 'CH',
name: 'hello.a.com' name: 'hello.a.com'
}, { }, {
type: 'SRV', type: 'SRV',
@ -130,10 +131,12 @@ tape('response', function (t) {
flags: packet.TRUNCATED_RESPONSE, flags: packet.TRUNCATED_RESPONSE,
answers: [{ answers: [{
type: 'A', type: 'A',
class: 'IN',
name: 'hello.a.com', name: 'hello.a.com',
data: '127.0.0.1' data: '127.0.0.1'
}, { }, {
type: 'SRV', type: 'SRV',
class: 'IN',
name: 'hello.srv.com', name: 'hello.srv.com',
data: { data: {
port: 9090, port: 9090,
@ -141,6 +144,7 @@ tape('response', function (t) {
} }
}, { }, {
type: 'CNAME', type: 'CNAME',
class: 'IN',
name: 'hello.cname.com', name: 'hello.cname.com',
data: 'hello.other.domain.com' data: 'hello.other.domain.com'
}] }]