Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change class option to take strings and document it #4

Merged
merged 1 commit into from Jan 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
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
@@ -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
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
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