Skip to content

Commit 09cdc37

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. PR-URL: #27081 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent bc2d258 commit 09cdc37

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

‎doc/api/dns.md‎

Lines changed: 3 additions & 1 deletion
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‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const{Object } = primordials;
2525

2626
constcares=internalBinding('cares_wrap');
2727
const{ toASCII }=require('internal/idna');
28-
const{ isIP,isIPv4,isLegalPort }=require('internal/net');
28+
const{ isIP, isLegalPort }=require('internal/net');
2929
const{ customPromisifyArgs }=require('internal/util');
3030
consterrors=require('internal/errors');
3131
const{
@@ -62,7 +62,7 @@ function onlookup(err, addresses){
6262
if(this.family){
6363
this.callback(null,addresses[0],this.family);
6464
}else{
65-
this.callback(null,addresses[0],isIPv4(addresses[0]) ? 4 : 6);
65+
this.callback(null,addresses[0],isIP(addresses[0]));
6666
}
6767
}
6868

@@ -77,7 +77,7 @@ function onlookupall(err, addresses){
7777
constaddr=addresses[i];
7878
addresses[i]={
7979
address: addr,
80-
family: family||(isIPv4(addr) ? 4 : 6)
80+
family: family||isIP(addr)
8181
};
8282
}
8383

0 commit comments

Comments
(0)