Skip to content

Commit

Permalink
Change class option to take strings and document it
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
silverwind committed Jan 12, 2018
1 parent a77e1bb commit d0c5c02
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var buf = packet.encode({
flags: packet.RECURSION_DESIRED,
questions: [{
type: 'A',
class: 'IN',
name: 'google.com'
}]
})
Expand Down Expand Up @@ -92,6 +93,7 @@ A question looks like this
``` js
{
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
}
```
Expand All @@ -101,6 +103,7 @@ And an answers, additional, or authority looks like this
``` js
{
type: 'A', // or SRV, AAAA, etc
class: 'IN', // one of IN, CS, CH, HS
name: 'google.com', // which name is this record for
ttl: optionalTimeToLiveInSeconds,
(record specific data, see below)
Expand Down
23 changes: 23 additions & 0 deletions classes.js
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const types = require('./types')
const rcodes = require('./rcodes')
const opcodes = require('./opcodes')
const classes = require('./classes')
const ip = require('ip')
const Buffer = require('safe-buffer').Buffer

Expand Down Expand Up @@ -586,7 +587,7 @@ answer.encode = function (a, buf, 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
buf.writeUInt16BE(klass, offset + 2)

Expand All @@ -611,7 +612,7 @@ answer.decode = function (buf, offset) {
a.name = name.decode(buf, offset)
offset += name.decode.bytes
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.flush = !!(a.class & FLUSH_MASK)
Expand Down Expand Up @@ -645,7 +646,7 @@ question.encode = function (q, buf, offset) {
buf.writeUInt16BE(types.toType(q.type), offset)
offset += 2

buf.writeUInt16BE(q.class === undefined ? 1 : q.class, offset)
buf.writeUInt16BE(classes.toClass(q.class === undefined ? 'IN' : q.class), offset)
offset += 2

question.encode.bytes = offset - oldOffset
Expand All @@ -666,7 +667,7 @@ question.decode = function (buf, offset) {
q.type = types.toString(buf.readUInt16BE(offset))
offset += 2

q.class = buf.readUInt16BE(offset)
q.class = classes.toString(buf.readUInt16BE(offset))
offset += 2

const qu = !!(q.class & QU_MASK)
Expand Down
6 changes: 5 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ tape('query', function (t) {
id: 42,
questions: [{
type: 'A',
class: 'IN',
name: 'hello.a.com'
}, {
type: 'SRV',
Expand All @@ -113,7 +114,7 @@ tape('query', function (t) {
id: 42,
questions: [{
type: 'A',
class: 100,
class: 'CH',
name: 'hello.a.com'
}, {
type: 'SRV',
Expand All @@ -130,17 +131,20 @@ tape('response', function (t) {
flags: packet.TRUNCATED_RESPONSE,
answers: [{
type: 'A',
class: 'IN',
name: 'hello.a.com',
data: '127.0.0.1'
}, {
type: 'SRV',
class: 'IN',
name: 'hello.srv.com',
data: {
port: 9090,
target: 'hello.target.com'
}
}, {
type: 'CNAME',
class: 'IN',
name: 'hello.cname.com',
data: 'hello.other.domain.com'
}]
Expand Down

0 comments on commit d0c5c02

Please sign in to comment.