Skip to content

Commit d3c7421

Browse files
committed
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.
1 parent c1bed5f commit d3c7421

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

doc/api/dns.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ changes:
154154
* `callback` {Function}
155155
- `err` {Error}
156156
- `address` {string} A string representation of an IPv4 or IPv6 address.
157-
- `family` {integer} `4` or `6`, denoting the family of `address`.
157+
- `family` {integer} `4` or `6`, denoting the family of `address`, or `0` if
158+
the address is not an IPv4 or IPv6 address. `0` is a likely indicator of a
159+
bug in the name resolution service used by the operating system.
158160

159161
Resolves a hostname (e.g. `'nodejs.org'`) into the first found A (IPv4) or
160162
AAAA (IPv6) record. All `option` properties are optional. If `options` is an

lib/dns.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
const cares = internalBinding('cares_wrap');
2525
const { toASCII } = require('internal/idna');
26-
const { isIP, isIPv4, isLegalPort } = require('internal/net');
26+
const { isIP, isLegalPort } = require('internal/net');
2727
const { customPromisifyArgs } = require('internal/util');
2828
const errors = require('internal/errors');
2929
const {
@@ -60,7 +60,7 @@ function onlookup(err, addresses) {
6060
if (this.family) {
6161
this.callback(null, addresses[0], this.family);
6262
} else {
63-
this.callback(null, addresses[0], isIPv4(addresses[0]) ? 4 : 6);
63+
this.callback(null, addresses[0], isIP(addresses[0]));
6464
}
6565
}
6666

@@ -75,7 +75,7 @@ function onlookupall(err, addresses) {
7575
const addr = addresses[i];
7676
addresses[i] = {
7777
address: addr,
78-
family: family || (isIPv4(addr) ? 4 : 6)
78+
family: family || isIP(addr)
7979
};
8080
}
8181

0 commit comments

Comments
 (0)