dns-packet
Advanced tools
Comparing version 3.0.1 to 4.0.0
117
index.js
@@ -144,4 +144,4 @@ 'use strict' | ||
opcode: opcodes.toString((flags >> 11) & 0xf), | ||
flag_auth: ((flags >> 10) & 0x1) === 1, | ||
flag_trunc: ((flags >> 9) & 0x1) === 1, | ||
flag_aa: ((flags >> 10) & 0x1) === 1, | ||
flag_tc: ((flags >> 9) & 0x1) === 1, | ||
flag_rd: ((flags >> 8) & 0x1) === 1, | ||
@@ -288,9 +288,77 @@ flag_ra: ((flags >> 7) & 0x1) === 1, | ||
const rtxt = exports.txt = exports.null = {} | ||
const rnull = rtxt | ||
const rtxt = exports.txt = {} | ||
rtxt.encode = function (data, buf, offset) { | ||
if (!Array.isArray(data)) data = [data] | ||
for (let i = 0; i < data.length; i++) { | ||
if (typeof data[i] === 'string') { | ||
data[i] = Buffer.from(data[i]) | ||
} | ||
if (!Buffer.isBuffer(data[i])) { | ||
throw new Error('Must be a Buffer') | ||
} | ||
} | ||
if (!buf) buf = Buffer.allocUnsafe(rtxt.encodingLength(data)) | ||
if (!offset) offset = 0 | ||
const oldOffset = offset | ||
offset += 2 | ||
data.forEach(function (d) { | ||
buf[offset++] = d.length | ||
d.copy(buf, offset, 0, d.length) | ||
offset += d.length | ||
}) | ||
buf.writeUInt16BE(offset - oldOffset - 2, oldOffset) | ||
rtxt.encode.bytes = offset - oldOffset | ||
return buf | ||
} | ||
rtxt.encode.bytes = 0 | ||
rtxt.decode = function (buf, offset) { | ||
if (!offset) offset = 0 | ||
const oldOffset = offset | ||
let remaining = buf.readUInt16BE(offset) | ||
offset += 2 | ||
let data = [] | ||
while (remaining > 0) { | ||
const len = buf[offset++] | ||
--remaining | ||
if (remaining < len) { | ||
throw new Error('Buffer overflow') | ||
} | ||
data.push(buf.slice(offset, offset + len)) | ||
offset += len | ||
remaining -= len | ||
} | ||
rtxt.decode.bytes = offset - oldOffset | ||
return data | ||
} | ||
rtxt.decode.bytes = 0 | ||
rtxt.encodingLength = function (data) { | ||
if (!Array.isArray(data)) data = [data] | ||
let length = 2 | ||
data.forEach(function (buf) { | ||
if (typeof buf === 'string') { | ||
length += Buffer.byteLength(buf) + 1 | ||
} else { | ||
length += buf.length + 1 | ||
} | ||
}) | ||
return length | ||
} | ||
const rnull = exports.null = {} | ||
rnull.encode = function (data, buf, offset) { | ||
if (!buf) buf = Buffer.allocUnsafe(rnull.encodingLength(data)) | ||
if (!offset) offset = 0 | ||
if (typeof data === 'string') data = Buffer.from(data) | ||
@@ -307,9 +375,9 @@ if (!data) data = Buffer.allocUnsafe(0) | ||
buf.writeUInt16BE(offset - oldOffset - 2, oldOffset) | ||
rtxt.encode.bytes = offset - oldOffset | ||
rnull.encode.bytes = offset - oldOffset | ||
return buf | ||
} | ||
rtxt.encode.bytes = 0 | ||
rnull.encode.bytes = 0 | ||
rtxt.decode = function (buf, offset) { | ||
rnull.decode = function (buf, offset) { | ||
if (!offset) offset = 0 | ||
@@ -324,9 +392,9 @@ const oldOffset = offset | ||
rtxt.decode.bytes = offset - oldOffset | ||
rnull.decode.bytes = offset - oldOffset | ||
return data | ||
} | ||
rtxt.decode.bytes = 0 | ||
rnull.decode.bytes = 0 | ||
rtxt.encodingLength = function (data) { | ||
rnull.encodingLength = function (data) { | ||
if (!data) return 2 | ||
@@ -619,7 +687,7 @@ return (Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data)) + 2 | ||
a.type = types.toString(buf.readUInt16BE(offset)) | ||
a.class = classes.toString(buf.readUInt16BE(offset + 2)) | ||
const klass = buf.readUInt16BE(offset + 2) | ||
a.ttl = buf.readUInt32BE(offset + 4) | ||
a.flush = !!(a.class & FLUSH_MASK) | ||
if (a.flush) a.class &= NOT_FLUSH_MASK | ||
a.class = classes.toString(klass & NOT_FLUSH_MASK) | ||
a.flush = !!(klass & FLUSH_MASK) | ||
@@ -751,2 +819,25 @@ const enc = renc(a.type) | ||
exports.streamEncode = function (result) { | ||
const len = exports.encodingLength(result) | ||
const buf = Buffer.allocUnsafe(len + 2) | ||
exports.encode(result, buf, 2) | ||
buf.writeUInt16BE(len, 0) | ||
exports.streamEncode.bytes = len + 2 | ||
return buf | ||
} | ||
exports.streamEncode.bytes = 0 | ||
exports.streamDecode = function (buf) { | ||
const len = buf.readUInt16BE(0) | ||
if (buf.length < len + 2) { | ||
return null | ||
} | ||
const result = exports.decode(buf, 2) | ||
exports.streamDecode.bytes = exports.decode.bytes + 2 | ||
return result | ||
} | ||
exports.streamDecode.bytes = 0 | ||
function encodingLengthList (list, enc) { | ||
@@ -753,0 +844,0 @@ let len = 0 |
{ | ||
"name": "dns-packet", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "An abstract-encoding compliant module for encoding / decoding DNS packets", | ||
@@ -13,3 +13,3 @@ "author": "Mathias Buus", | ||
"scripts": { | ||
"test": "standard && eslint --color *.js && tape test.js" | ||
"test": "eslint --color *.js && tape test.js" | ||
}, | ||
@@ -21,4 +21,8 @@ "dependencies": { | ||
"devDependencies": { | ||
"eslint": "^4.15.0", | ||
"standard": "^10.0.3", | ||
"eslint": "^4.17.0", | ||
"eslint-config-standard": "^11.0.0-beta.0", | ||
"eslint-plugin-import": "^2.8.0", | ||
"eslint-plugin-node": "^5.2.1", | ||
"eslint-plugin-promise": "^3.6.0", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"tape": "^4.8.0" | ||
@@ -25,0 +29,0 @@ }, |
@@ -12,3 +12,3 @@ # dns-packet | ||
## Usage | ||
## UDP Usage | ||
@@ -39,2 +39,10 @@ ``` js | ||
Also see [the UDP example](examples/udp.js). | ||
## TCP Usage | ||
While DNS has traditionally been used over a datagram transport, it is increasingly being carried over TCP for larger responses commonly including DNSSEC responses and TCP/TLS for privacy reasons. | ||
See [the TCP example](examples/tcp.js). | ||
## API | ||
@@ -44,8 +52,16 @@ | ||
Encodes a DNS packet into a buffer. | ||
Encodes a DNS packet into a buffer containing a UDP payload. | ||
#### `var packet = packets.decode(buf, [offset])` | ||
Decode a DNS packet from a buffer | ||
Decode a DNS packet from a buffer containing a UDP payload. | ||
#### `var buf = packets.streamEncode(packet, [buf], [offset])` | ||
Encodes a DNS packet into a buffer containing a TCP payload. | ||
#### `var packet = packets.streamDecode(buf, [offset])` | ||
Decode a DNS packet from a buffer containing a TCP payload. | ||
#### `var len = packets.encodingLength(packet)` | ||
@@ -138,6 +154,8 @@ | ||
{ | ||
data: Buffer('some text') | ||
data: 'text' || Buffer || [ Buffer || 'text' ] | ||
} | ||
``` | ||
When encoding, scalar values are converted to an array and strings are converted to UTF-8 encoded Buffers. When decoding, the return value will always be an array of Buffer. | ||
#### `NS` | ||
@@ -144,0 +162,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32572
9
863
253
7