Skip to content

Commit

Permalink
implement the NAPTR record (#89)
Browse files Browse the repository at this point in the history
* implement the NAPTR record

* implement NAPTR record for readme

* update

* Update index.js

* Update index.js

* Update index.js

* fix test failed

* Add actual example values to README

* Update README.md

* Update README.md

---------

Co-authored-by: root <[email protected]>
Co-authored-by: silverwind <[email protected]>
  • Loading branch information
3 people authored Mar 27, 2023
1 parent 31d3caf commit aca1ff7
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,22 @@ The options `PADDING`, `CLIENT_SUBNET`, `TCP_KEEPALIVE` and `KEY_TAG` support en
}
```

#### `NAPTR`

``` js
{
data:
{
order: 100,
preference: 10,
flags: 's',
services: 'SIP+D2U',
regexp: '!^.*$!sip:[email protected]!',
replacement: '_sip._udp.example.com'
}
}
```

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.

If you need another record type, open an issue and we'll try to add it.
Expand Down
57 changes: 57 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,62 @@ rsshfp.encodingLength = function (record) {
return 4 + Buffer.from(record.fingerprint, 'hex').byteLength
}

const rnaptr = exports.naptr = {}

rnaptr.encode = function (data, buf, offset) {
if (!buf) buf = Buffer.alloc(rnaptr.encodingLength(data))
if (!offset) offset = 0
const oldOffset = offset
offset += 2
buf.writeUInt16BE(data.order || 0, offset)
offset += 2
buf.writeUInt16BE(data.preference || 0, offset)
offset += 2
string.encode(data.flags, buf, offset)
offset += string.encode.bytes
string.encode(data.services, buf, offset)
offset += string.encode.bytes
string.encode(data.regexp, buf, offset)
offset += string.encode.bytes
name.encode(data.replacement, buf, offset)
offset += name.encode.bytes
rnaptr.encode.bytes = offset - oldOffset
buf.writeUInt16BE(rnaptr.encode.bytes - 2, oldOffset)
return buf
}

rnaptr.encode.bytes = 0

rnaptr.decode = function (buf, offset) {
if (!offset) offset = 0
const oldOffset = offset
const data = {}
offset += 2
data.order = buf.readUInt16BE(offset)
offset += 2
data.preference = buf.readUInt16BE(offset)
offset += 2
data.flags = string.decode(buf, offset)
offset += string.decode.bytes
data.services = string.decode(buf, offset)
offset += string.decode.bytes
data.regexp = string.decode(buf, offset)
offset += string.decode.bytes
data.replacement = name.decode(buf, offset)
offset += name.decode.bytes
rnaptr.decode.bytes = offset - oldOffset
return data
}

rnaptr.decode.bytes = 0

rnaptr.encodingLength = function (data) {
return string.encodingLength(data.flags) +
string.encodingLength(data.services) +
string.encodingLength(data.regexp) +
name.encodingLength(data.replacement) + 6
}

const renc = exports.record = function (type) {
switch (type.toUpperCase()) {
case 'A': return ra
Expand All @@ -1376,6 +1432,7 @@ const renc = exports.record = function (type) {
case 'NSEC3': return rnsec3
case 'SSHFP': return rsshfp
case 'DS': return rds
case 'NAPTR': return rnaptr
}
return runknown
}
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,18 @@ tape('ds', function (t) {
t.end()
})

tape('naptr', function (t) {
testEncoder(t, packet.naptr, {
order: 1,
preference: 1,
flags: 'S',
services: 'SIP+D2T',
regexp: '!^.*$!sip:[email protected]!',
replacement: '_sip._udp.xuexample.com'
})
t.end()
})

tape('unpack', function (t) {
const buf = Buffer.from([
0x00, 0x79,
Expand Down

0 comments on commit aca1ff7

Please sign in to comment.