Skip to content

Commit

Permalink
dns: use char code conversion instead of Buffer for nsec epsilons
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Sep 18, 2021
1 parent 4a8667e commit 4b96d4b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
16 changes: 16 additions & 0 deletions bench/nsec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const bench = require('./bench');
const nsec = require('../lib/dns/nsec');
const {TYPE_MAP_EMPTY} = require('../lib/dns/common');

const tld = 'dollarydoo';
const ops = 100000;

const end = bench('nsec');
for (let i = 0; i < ops; i++) {
const prev = nsec.prevName(tld);
const next = nsec.nextName(tld);
nsec.create(prev, next, TYPE_MAP_EMPTY);
}
end(ops);
18 changes: 9 additions & 9 deletions lib/dns/nsec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ function nextName(tld) {
// increment last character by one
if (tld.length === 63) {
// Assuming no escaped octets are present
const next = Buffer.from(tld, 'ascii');
next[next.length-1]++;
return next.toString() + '.';
let last = tld.charCodeAt(62);
last = String.fromCharCode(last + 1);
return tld.slice(0, -1) + last + '.';
}

return tld + '\\000.';
Expand All @@ -42,9 +42,9 @@ function prevName(tld) {

// Decrement the last character by 1
// assuming no escaped octets are present
let prev = Buffer.from(tld, 'ascii');
prev[prev.length-1]--;
prev = prev.toString();
let last = tld.charCodeAt(tld.length - 1);
last = String.fromCharCode(last - 1);
tld = tld.slice(0, -1) + last;

// See RFC4034 6.1 Canonical DNS Name Order
// https://tools.ietf.org/html/rfc4034#section-6.1
Expand All @@ -54,11 +54,11 @@ function prevName(tld) {
// smaller name is `helln` append `\255`
// to ensure that helln\255 > hellna
// while keeping helln\255 < hello
if (prev.length < 63) {
prev += '\\255';
if (tld.length < 63) {
tld += '\\255';
}

return util.fqdn(prev);
return util.fqdn(tld);
}

exports.create = create;
Expand Down

0 comments on commit 4b96d4b

Please sign in to comment.