Skip to content

Commit

Permalink
dns: do not indicate invalid IPs are IPv6
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Trott committed Apr 16, 2019
1 parent c1bed5f commit d3c7421
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]));
}
}

Expand All @@ -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)
};
}

Expand Down

0 comments on commit d3c7421

Please sign in to comment.