Skip to content

Commit

Permalink
dns: use faster IP address type check on results
Browse files Browse the repository at this point in the history
PR-URL: #13261
Reviewed-By: Roman Reiss <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
mscdex authored and jasnell committed Jun 5, 2017
1 parent 007a033 commit 92de432
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,31 @@ function errnoException(err, syscall, hostname) {
return ex;
}

const digits = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0-15
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 32-47
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48-63
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 64-79
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-95
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 96-111
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 112-127
];
function isIPv4(str) {
if (!digits[str.charCodeAt(0)]) return false;
if (str.length === 1) return false;
if (str.charCodeAt(1) === 46/*'.'*/)
return true;
else if (!digits[str.charCodeAt(1)])
return false;
if (str.length === 2) return false;
if (str.charCodeAt(2) === 46/*'.'*/)
return true;
else if (!digits[str.charCodeAt(2)])
return false;
return (str.length > 3 && str.charCodeAt(3) === 46/*'.'*/);
}


function onlookup(err, addresses) {
if (err) {
Expand All @@ -68,25 +93,26 @@ function onlookup(err, addresses) {
if (this.family) {
this.callback(null, addresses[0], this.family);
} else {
this.callback(null, addresses[0], addresses[0].indexOf(':') >= 0 ? 6 : 4);
this.callback(null, addresses[0], isIPv4(addresses[0]) ? 4 : 6);
}
}


function onlookupall(err, addresses) {
var results = [];
if (err) {
return this.callback(errnoException(err, 'getaddrinfo', this.hostname));
}

var family = this.family;
for (var i = 0; i < addresses.length; i++) {
results.push({
address: addresses[i],
family: this.family || (addresses[i].indexOf(':') >= 0 ? 6 : 4)
});
const addr = addresses[i];
addresses[i] = {
address: addr,
family: family || (isIPv4(addr) ? 4 : 6)
};
}

this.callback(null, results);
this.callback(null, addresses);
}


Expand Down

0 comments on commit 92de432

Please sign in to comment.