From c1bed5fe94db9abeb3dd7418fb7e2feb4d088f97 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 3 Apr 2019 17:07:15 -0700 Subject: [PATCH 1/2] dns: refactor internal/dns/promises.js Use `isIP()` instead of `isIPv4()` since it does the additional functionality that we were adding after our calls to `isIP()`. This not-so-incidentally also increases code coverage from tests. At least one of the replaced ternaries was difficult to cover reliably because operating system/configuration variances were too unpredictable. --- lib/internal/dns/promises.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/dns/promises.js b/lib/internal/dns/promises.js index 25696bf2228b64..c64adb65e541ce 100644 --- a/lib/internal/dns/promises.js +++ b/lib/internal/dns/promises.js @@ -7,7 +7,7 @@ const { } = require('internal/dns/utils'); const { codes, dnsException } = require('internal/errors'); const { toASCII } = require('internal/idna'); -const { isIP, isIPv4, isLegalPort } = require('internal/net'); +const { isIP, isLegalPort } = require('internal/net'); const { getaddrinfo, getnameinfo, @@ -31,7 +31,7 @@ function onlookup(err, addresses) { return; } - const family = this.family ? this.family : isIPv4(addresses[0]) ? 4 : 6; + const family = this.family ? this.family : isIP(addresses[0]); this.resolve({ address: addresses[0], family }); } @@ -48,7 +48,7 @@ function onlookupall(err, addresses) { addresses[i] = { address, - family: family ? family : isIPv4(addresses[i]) ? 4 : 6 + family: family ? family : isIP(addresses[i]) }; } From d3c742101608e726699e5392966e54a4783e5767 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Tue, 16 Apr 2019 11:07:17 -0700 Subject: [PATCH 2/2] dns: do not indicate invalid IPs are IPv6 In lib/dns.js, use `isIP()` instead of `isIPv4()` for determining the `family` property in `lookup()`. If an invalid IP address is returned, the `family` currently provided is `6`. With this change, it will be `0`. Update documentation to reflect this. --- doc/api/dns.md | 4 +++- lib/dns.js | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/api/dns.md b/doc/api/dns.md index 1dc4f50ef668d2..8e4dd9bc81e8c8 100644 --- a/doc/api/dns.md +++ b/doc/api/dns.md @@ -154,7 +154,9 @@ changes: * `callback` {Function} - `err` {Error} - `address` {string} A string representation of an IPv4 or IPv6 address. - - `family` {integer} `4` or `6`, denoting the family of `address`. + - `family` {integer} `4` or `6`, denoting the family of `address`, or `0` if + the address is not an IPv4 or IPv6 address. `0` is a likely indicator of a + bug in the name resolution service used by the operating system. Resolves a hostname (e.g. `'nodejs.org'`) into the first found A (IPv4) or AAAA (IPv6) record. All `option` properties are optional. If `options` is an diff --git a/lib/dns.js b/lib/dns.js index 3d057d6b50081d..fba27ea77e2c37 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -23,7 +23,7 @@ const cares = internalBinding('cares_wrap'); const { toASCII } = require('internal/idna'); -const { isIP, isIPv4, isLegalPort } = require('internal/net'); +const { isIP, isLegalPort } = require('internal/net'); const { customPromisifyArgs } = require('internal/util'); const errors = require('internal/errors'); const { @@ -60,7 +60,7 @@ function onlookup(err, addresses) { if (this.family) { this.callback(null, addresses[0], this.family); } else { - this.callback(null, addresses[0], isIPv4(addresses[0]) ? 4 : 6); + this.callback(null, addresses[0], isIP(addresses[0])); } } @@ -75,7 +75,7 @@ function onlookupall(err, addresses) { const addr = addresses[i]; addresses[i] = { address: addr, - family: family || (isIPv4(addr) ? 4 : 6) + family: family || isIP(addr) }; }