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 Jun 22, 2017
1 parent 412574e commit 9f9af0a
Show file tree
Hide file tree
Showing 4 changed files with 34 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
21 changes: 21 additions & 0 deletions classes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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
@@ -1,4 +1,5 @@
var types = require('./types')
var classes = require('./classes')
var ip = require('ip')
var Buffer = require('safe-buffer').Buffer

Expand Down Expand Up @@ -413,7 +414,7 @@ answer.encode = function (a, buf, offset) {

buf.writeUInt16BE(types.toType(a.type), offset)

var klass = a.class === undefined ? 1 : a.class
var 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 @@ -438,7 +439,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 @@ -472,7 +473,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 @@ -493,7 +494,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

var 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 @@ -72,6 +72,7 @@ tape('query', function (t) {
id: 42,
questions: [{
type: 'A',
class: 'IN',
name: 'hello.a.com'
}, {
type: 'SRV',
Expand All @@ -84,7 +85,7 @@ tape('query', function (t) {
id: 42,
questions: [{
type: 'A',
class: 100,
class: 'CH',
name: 'hello.a.com'
}, {
type: 'SRV',
Expand All @@ -101,17 +102,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 9f9af0a

Please sign in to comment.