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

implement the NAPTR record #89

Merged
merged 10 commits into from Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -366,6 +366,22 @@ The options `PADDING`, `CLIENT_SUBNET`, `TCP_KEEPALIVE` and `KEY_TAG` support en
}
```

#### `NAPTR`
``` js
silverwind marked this conversation as resolved.
Show resolved Hide resolved
{
data:
{
order: 100,
preference: 10,
flags: 's',
services: 'SIP+D2U',
regexp: '!^.*$!sip:customer-service@example.com!',
replacement: '_sip._udp.example.com'
}
}
```

silverwind marked this conversation as resolved.
Show resolved Hide resolved

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
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
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:customer-service@xuexample.com!',
replacement: '_sip._udp.xuexample.com'
})
t.end()
})

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