add MX record type

Fixes: https://github.com/mafintosh/dns-packet/issues/10
This commit is contained in:
silverwind 2018-02-11 11:06:22 +01:00
parent 8000f406c8
commit 087d59bb63
No known key found for this signature in database
GPG Key ID: 2E62B41C93869443
3 changed files with 56 additions and 0 deletions

View File

@ -245,6 +245,15 @@ When encoding, scalar values are converted to an array and strings are converted
}
```
#### `MX`
``` js
{
preference: 10,
exchange: 'mail.example.net'
}
```
If you need another one, open an issue and we'll try to add it.
## License

View File

@ -564,6 +564,46 @@ rcaa.encodingLength = function (data) {
return string.encodingLength(data.tag) + string.encodingLength(data.value) + 2
}
const rmx = exports.mx = {}
rmx.encode = function (data, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(rmx.encodingLength(data))
if (!offset) offset = 0
const oldOffset = offset
offset += 2
buf.writeUInt16BE(data.preference || 0, offset)
offset += 2
name.encode(data.exchange, buf, offset)
offset += name.encode.bytes
buf.writeUInt16BE(offset - oldOffset - 2, oldOffset)
rmx.encode.bytes = offset - oldOffset
return buf
}
rmx.encode.bytes = 0
rmx.decode = function (buf, offset) {
if (!offset) offset = 0
const oldOffset = offset
const data = {}
offset += 2
data.preference = buf.readUInt16BE(offset)
offset += 2
data.exchange = name.decode(buf, offset)
offset += name.decode.bytes
rmx.decode.bytes = offset - oldOffset
return data
}
rmx.encodingLength = function (data) {
return 4 + name.encodingLength(data.exchange)
}
const ra = exports.a = {}
ra.encode = function (host, buf, offset) {
@ -638,6 +678,7 @@ const renc = exports.record = function (type) {
case 'CAA': return rcaa
case 'NS': return rns
case 'SOA': return rsoa
case 'MX': return rmx
}
return runknown
}

View File

@ -83,6 +83,12 @@ tape('caa', function (t) {
t.end()
})
tape('mx', function (t) {
testEncoder(t, packet.mx, {preference: 10, exchange: 'mx.hello.world.com'})
testEncoder(t, packet.mx, {exchange: 'mx.hello.world.com'})
t.end()
})
tape('ns', function (t) {
testEncoder(t, packet.ns, 'ns.world.com')
t.end()