mafintosh / dns-packet

An abstract-encoding compliant module for encoding / decoding DNS packets
MIT License
207 stars 71 forks source link

Rework of PR #34 #37

Closed hildjj closed 6 years ago

hildjj commented 6 years ago

This rework includes working support for options inside the OPT.

silverwind commented 6 years ago

Few tweaks below. I did remove the aliases and renamed and added extendedRcode in the decoder.

diff --git a/index.js b/index.js
index aa42854..e0dac38 100644
--- a/index.js
+++ b/index.js
@@ -780,12 +780,10 @@ answer.encode = function (a, buf, offset) {
     if (a.name !== '.') {
       throw new Error('OPT name must be root.')
     }
-    buf.writeUInt16BE(a.updPayloadSize || a.class || 4096, offset + 2)
-    buf.writeUInt8(a.extRcode || 0, offset + 4)
+    buf.writeUInt16BE(a.updPayloadSize || 4096, offset + 2)
+    buf.writeUInt8(a.extendedRcode || 0, offset + 4)
     buf.writeUInt8(a.ednsVersion || 0, offset + 5)
-    // a.flags might be 0, in which case, don't use ttl
-    let flags = a.flags == null ? a.ttl : a.flags
-    buf.writeUInt16BE(flags || 0, offset + 6)
+    buf.writeUInt16BE(a.flags || 0, offset + 6)

     offset += 8
     ropt.encode(a.options || [], buf, offset)
@@ -819,6 +817,7 @@ answer.decode = function (buf, offset) {
   a.type = types.toString(buf.readUInt16BE(offset))
   if (a.type === 'OPT') {
     a.udpPayloadSize = buf.readUInt16BE(offset + 2)
+    a.extendedRcode = buf.readUInt8(offset + 4)
     a.ednsVersion = buf.readUInt8(offset + 5)
     a.flags = buf.readUInt16BE(offset + 6)
     a.flag_do = ((a.flags >> 15) & 0x1) === 1
diff --git a/test.js b/test.js
index 5c70454..31f2579 100644
--- a/test.js
+++ b/test.js
@@ -339,9 +339,11 @@ tape('opt', function (t) {
   let val2 = packet.decode(buf)
   const additional1 = val.additionals[0]
   let additional2 = val2.additionals[0]
+  t.ok(compare(t, additional1.name, additional2.name), 'name matches')
   t.ok(compare(t, additional1.udpPayloadSize, additional2.udpPayloadSize), 'udp payload size matches')
   t.ok(compare(t, 0, additional2.flags), 'flags match')
   additional1.flags = packet.DNSSEC_OK
+  additional1.extendedRcode = 0x80
   // padding, see RFC 7830
   additional1.options = [{
     code: 12,
@@ -352,6 +354,7 @@ tape('opt', function (t) {
   additional2 = val2.additionals[0]
   t.ok(compare(t, 1 << 15, additional2.flags), 'DO bit set in flags')
   t.ok(compare(t, true, additional2.flag_do), 'DO bit set')
+  t.ok(compare(t, additional1.extendedRcode, additional2.extendedRcode), 'extended rcode matches')
   t.ok(compare(t, additional1.options, additional2.options), 'options match')
   t.end()
 })