Skip to content

Commit 0bb84b0

Browse files
aduh95danielleadams
authored andcommitted
dns: accept 'IPv4' and 'IPv6' for family
Refs: #43014 PR-URL: #43054 Fixes: #43014 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Beth Griggs <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent f91babe commit 0bb84b0

27 files changed

+88
-54
lines changed

doc/api/dgram.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ exist and calls such as `socket.address()` and `socket.setTTL()` will fail.
114114
<!-- YAML
115115
added: v0.1.99
116116
changes:
117+
- version: REPLACEME
118+
pr-url: https://github.com/nodejs/node/pull/43054
119+
description: The `family` property now returns a string instead of a number.
117120
- version: v18.0.0
118121
pr-url: https://github.com/nodejs/node/pull/41431
119122
description: The `family` property now returns a number instead of a string.
@@ -125,7 +128,7 @@ The event handler function is passed two arguments: `msg` and `rinfo`.
125128
* `msg` {Buffer} The message.
126129
* `rinfo` {Object} Remote address information.
127130
* `address` {string} The sender address.
128-
* `family` {number} The address family (`4` for IPv4 or `6` for IPv6).
131+
* `family` {string} The address family (`'IPv4'` or `'IPv6'`).
129132
* `port` {number} The sender port.
130133
* `size` {number} The message size.
131134

doc/api/dns.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ section if a custom port is used.
179179
<!-- YAML
180180
added: v0.1.90
181181
changes:
182+
- version: REPLACEME
183+
pr-url: https://github.com/nodejs/node/pull/43054
184+
description: For compatibility with `node:net`, when passing an option
185+
object the `family` option can be the string `'IPv4'` or the
186+
string `'IPv6'`.
182187
- version: v18.0.0
183188
pr-url: https://github.com/nodejs/node/pull/41678
184189
description: Passing an invalid callback to the `callback` argument
@@ -197,9 +202,10 @@ changes:
197202

198203
* `hostname` {string}
199204
* `options` {integer | Object}
200-
* `family` {integer} The record family. Must be `4`, `6`, or `0`. The value
201-
`0` indicates that IPv4 and IPv6 addresses are both returned. **Default:**
202-
`0`.
205+
* `family` {integer|string} The record family. Must be `4`, `6`, or `0`. For
206+
backward compatibility reasons,`'IPv4'` and `'IPv6'` are interpreted as `4`
207+
and `6` respectively. The value `0` indicates that IPv4 and IPv6 addresses
208+
are both returned. **Default:** `0`.
203209
* `hints` {number} One or more [supported `getaddrinfo` flags][]. Multiple
204210
flags may be passed by bitwise `OR`ing their values.
205211
* `all` {boolean} When `true`, the callback returns all resolved addresses in
@@ -219,8 +225,8 @@ changes:
219225

220226
Resolves a host name (e.g. `'nodejs.org'`) into the first found A (IPv4) or
221227
AAAA (IPv6) record. All `option` properties are optional. If `options` is an
222-
integer, then it must be `4` or `6` – if `options` is not provided, then IPv4
223-
and IPv6 addresses are both returned if found.
228+
integer, then it must be `4` or `6` – if `options` is `0` or not provided, then
229+
IPv4 and IPv6 addresses are both returned if found.
224230

225231
With the `all` option set to `true`, the arguments for `callback` change to
226232
`(err, addresses)`, with `addresses` being an array of objects with the

doc/api/net.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ Emitted when the server has been bound after calling [`server.listen()`][].
286286
<!-- YAML
287287
added: v0.1.90
288288
changes:
289+
- version: REPLACEME
290+
pr-url: https://github.com/nodejs/node/pull/43054
291+
description: The `family` property now returns a string instead of a number.
289292
- version: v18.0.0
290293
pr-url: https://github.com/nodejs/node/pull/41431
291294
description: The `family` property now returns a number instead of a string.
@@ -296,7 +299,7 @@ changes:
296299
Returns the bound `address`, the address `family` name, and `port` of the server
297300
as reported by the operating system if listening on an IP socket
298301
(useful to find which port was assigned when getting an OS-assigned address):
299-
`{ port: 12346, family: 4, address: '127.0.0.1' }`.
302+
`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`.
300303

301304
For a server listening on a pipe or Unix domain socket, the name is returned
302305
as a string.
@@ -743,6 +746,9 @@ See also: [`socket.setTimeout()`][].
743746
<!-- YAML
744747
added: v0.1.90
745748
changes:
749+
- version: REPLACEME
750+
pr-url: https://github.com/nodejs/node/pull/43054
751+
description: The `family` property now returns a string instead of a number.
746752
- version: v18.0.0
747753
pr-url: https://github.com/nodejs/node/pull/41431
748754
description: The `family` property now returns a number instead of a string.
@@ -752,7 +758,7 @@ changes:
752758

753759
Returns the bound `address`, the address `family` name and `port` of the
754760
socket as reported by the operating system:
755-
`{ port: 12346, family: 4, address: '127.0.0.1' }`
761+
`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`
756762

757763
### `socket.bufferSize`
758764

doc/api/os.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ always `[0, 0, 0]`.
225225
<!-- YAML
226226
added: v0.6.0
227227
changes:
228+
- version: REPLACEME
229+
pr-url: https://github.com/nodejs/node/pull/43054
230+
description: The `family` property now returns a string instead of a number.
228231
- version: v18.0.0
229232
pr-url: https://github.com/nodejs/node/pull/41431
230233
description: The `family` property now returns a number instead of a string.
@@ -242,12 +245,12 @@ The properties available on the assigned network address object include:
242245

243246
* `address` {string} The assigned IPv4 or IPv6 address
244247
* `netmask` {string} The IPv4 or IPv6 network mask
245-
* `family` {number} Either `4` (for IPv4) or `6` (for IPv6)
248+
* `family` {string} Either `IPv4` or `IPv6`
246249
* `mac` {string} The MAC address of the network interface
247250
* `internal` {boolean} `true` if the network interface is a loopback or
248251
similar interface that is not remotely accessible; otherwise `false`
249252
* `scopeid` {number} The numeric IPv6 scope ID (only specified when `family`
250-
is `6`)
253+
is `IPv6`)
251254
* `cidr` {string} The assigned IPv4 or IPv6 address with the routing prefix
252255
in CIDR notation. If the `netmask` is invalid, this property is set
253256
to `null`.
@@ -260,15 +263,15 @@ The properties available on the assigned network address object include:
260263
{
261264
address: '127.0.0.1',
262265
netmask: '255.0.0.0',
263-
family: 4,
266+
family: 'IPv4',
264267
mac: '00:00:00:00:00:00',
265268
internal: true,
266269
cidr: '127.0.0.1/8'
267270
},
268271
{
269272
address: '::1',
270273
netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
271-
family: 6,
274+
family: 'IPv6',
272275
mac: '00:00:00:00:00:00',
273276
scopeid: 0,
274277
internal: true,
@@ -279,15 +282,15 @@ The properties available on the assigned network address object include:
279282
{
280283
address: '192.168.1.108',
281284
netmask: '255.255.255.0',
282-
family: 4,
285+
family: 'IPv4',
283286
mac: '01:02:03:0a:0b:0c',
284287
internal: false,
285288
cidr: '192.168.1.108/24'
286289
},
287290
{
288291
address: 'fe80::a00:27ff:fe4e:66a1',
289292
netmask: 'ffff:ffff:ffff:ffff::',
290-
family: 6,
293+
family: 'IPv6',
291294
mac: '01:02:03:0a:0b:0c',
292295
scopeid: 1,
293296
internal: false,

doc/api/tls.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,9 @@ tlsSocket.once('session', (session) => {
956956
<!-- YAML
957957
added: v0.11.4
958958
changes:
959+
- version: REPLACEME
960+
pr-url: https://github.com/nodejs/node/pull/43054
961+
description: The `family` property now returns a string instead of a number.
959962
- version: v18.0.0
960963
pr-url: https://github.com/nodejs/node/pull/41431
961964
description: The `family` property now returns a number instead of a string.
@@ -965,7 +968,7 @@ changes:
965968

966969
Returns the bound `address`, the address `family` name, and `port` of the
967970
underlying socket as reported by the operating system:
968-
`{ port: 12346, family: 4, address: '127.0.0.1' }`.
971+
`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`.
969972

970973
### `tlsSocket.authorizationError`
971974

lib/dns.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,18 @@ function lookup(hostname, options, callback) {
145145
validateHints(hints);
146146
}
147147
if (options?.family != null) {
148-
validateOneOf(options.family, 'options.family', validFamilies, true);
149-
family = options.family;
148+
switch (options.family) {
149+
case 'IPv4':
150+
family = 4;
151+
break;
152+
case 'IPv6':
153+
family = 6;
154+
break;
155+
default:
156+
validateOneOf(options.family, 'options.family', validFamilies, true);
157+
family = options.family;
158+
break;
159+
}
150160
}
151161
if (options?.all != null) {
152162
validateBoolean(options.all, 'options.all');

lib/net.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -800,9 +800,7 @@ protoGetter('remoteAddress', function remoteAddress() {
800800
});
801801

802802
protoGetter('remoteFamily', function remoteFamily() {
803-
const { family } = this._getpeername();
804-
805-
return family ? `IPv${family}` : family;
803+
return this._getpeername().family;
806804
});
807805

808806
protoGetter('remotePort', function remotePort() {

lib/os.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ function getCIDR(address, netmask, family) {
216216
let groupLength = 8;
217217
let hasZeros = false;
218218

219-
if (family === 6) {
219+
if (family === 'IPv6') {
220220
split = ':';
221221
range = 16;
222222
groupLength = 16;
@@ -248,7 +248,7 @@ function getCIDR(address, netmask, family) {
248248
* @returns {Record<string, Array<{
249249
* address: string,
250250
* netmask: string,
251-
* family: 4 | 6,
251+
* family: 'IPv4' | 'IPv6',
252252
* mac: string,
253253
* internal: boolean,
254254
* scopeid: number,

src/env.h

+2
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ class NoArrayBufferZeroFillScope {
301301
V(input_string, "input") \
302302
V(internal_binding_string, "internalBinding") \
303303
V(internal_string, "internal") \
304+
V(ipv4_string, "IPv4") \
305+
V(ipv6_string, "IPv6") \
304306
V(isclosing_string, "isClosing") \
305307
V(issuer_string, "issuer") \
306308
V(issuercert_string, "issuerCertificate") \

src/node_os.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
174174
char ip[INET6_ADDRSTRLEN];
175175
char netmask[INET6_ADDRSTRLEN];
176176
std::array<char, 18> mac;
177-
Local<String> name;
178-
Local<Integer> family;
177+
Local<String> name, family;
179178

180179
int err = uv_interface_addresses(&interfaces, &count);
181180

@@ -215,14 +214,14 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
215214
if (interfaces[i].address.address4.sin_family == AF_INET) {
216215
uv_ip4_name(&interfaces[i].address.address4, ip, sizeof(ip));
217216
uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask));
218-
family = Integer::New(env->isolate(), 4);
217+
family = env->ipv4_string();
219218
} else if (interfaces[i].address.address4.sin_family == AF_INET6) {
220219
uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
221220
uv_ip6_name(&interfaces[i].netmask.netmask6, netmask, sizeof(netmask));
222-
family = Integer::New(env->isolate(), 6);
221+
family = env->ipv6_string();
223222
} else {
224223
strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN);
225-
family = Integer::New(env->isolate(), 0);
224+
family = env->unknown_string();
226225
}
227226

228227
result.emplace_back(name);

src/tcp_wrap.cc

+2-6
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,7 @@ MaybeLocal<Object> AddressToJS(Environment* env,
402402
info->Set(env->context(),
403403
env->address_string(),
404404
OneByteString(env->isolate(), ip)).Check();
405-
info->Set(env->context(),
406-
env->family_string(),
407-
Integer::New(env->isolate(), 6)).Check();
405+
info->Set(env->context(), env->family_string(), env->ipv6_string()).Check();
408406
info->Set(env->context(),
409407
env->port_string(),
410408
Integer::New(env->isolate(), port)).Check();
@@ -417,9 +415,7 @@ MaybeLocal<Object> AddressToJS(Environment* env,
417415
info->Set(env->context(),
418416
env->address_string(),
419417
OneByteString(env->isolate(), ip)).Check();
420-
info->Set(env->context(),
421-
env->family_string(),
422-
Integer::New(env->isolate(), 4)).Check();
418+
info->Set(env->context(), env->family_string(), env->ipv4_string()).Check();
423419
info->Set(env->context(),
424420
env->port_string(),
425421
Integer::New(env->isolate(), port)).Check();

test/common/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ const common = {
854854
const re = isWindows ? /Loopback Pseudo-Interface/ : /lo/;
855855
return Object.keys(iFaces).some((name) => {
856856
return re.test(name) &&
857-
iFaces[name].some(({ family }) => family === 6);
857+
iFaces[name].some(({ family }) => family === 'IPv6');
858858
});
859859
},
860860

test/common/udppair.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class FakeUDPWrap extends EventEmitter {
1616
this._handle.onwrite =
1717
(wrap, buffers, addr) => this._write(wrap, buffers, addr);
1818
this._handle.getsockname = (obj) => {
19-
Object.assign(obj, { address: '127.0.0.1', family: 4, port: 1337 });
19+
Object.assign(obj, { address: '127.0.0.1', family: 'IPv4', port: 1337 });
2020
return 0;
2121
};
2222

@@ -72,8 +72,8 @@ class FakeUDPWrap extends EventEmitter {
7272

7373
let familyInt;
7474
switch (family) {
75-
case 4: familyInt = 4; break;
76-
case 6: familyInt = 6; break;
75+
case 'IPv4': familyInt = 4; break;
76+
case 'IPv6': familyInt = 6; break;
7777
default: throw new Error('bad family');
7878
}
7979

test/es-module/test-http-imports.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ const internalInterfaces = Object.values(os.networkInterfaces()).flat().filter(
3939
);
4040
for (const iface of internalInterfaces) {
4141
testListeningOptions.push({
42-
hostname: iface?.family === 6 ? `[${iface.address}]` : iface?.address,
42+
hostname: iface?.family === 'IPv6' ? `[${iface?.address}]` : iface?.address,
4343
listenOptions: {
4444
host: iface?.address,
45-
ipv6Only: iface?.family === 6
45+
ipv6Only: iface?.family === 'IPv6'
4646
}
4747
});
4848
}

test/internet/test-dgram-broadcast-multi-process.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ get_bindAddress: for (const name in networkInterfaces) {
4747
const interfaces = networkInterfaces[name];
4848
for (let i = 0; i < interfaces.length; i++) {
4949
const localInterface = interfaces[i];
50-
if (!localInterface.internal && localInterface.family === 4) {
50+
if (!localInterface.internal && localInterface.family === 'IPv4') {
5151
bindAddress = localInterface.address;
5252
break get_bindAddress;
5353
}

test/internet/test-dgram-multicast-set-interface-lo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const TMPL = (tail) => `${NOW} - ${tail}`;
4949
const interfaceAddress = ((networkInterfaces) => {
5050
for (const name in networkInterfaces) {
5151
for (const localInterface of networkInterfaces[name]) {
52-
if (!localInterface.internal && `IPv${localInterface.family}` === FAM) {
52+
if (!localInterface.internal && localInterface.family === FAM) {
5353
let interfaceAddress = localInterface.address;
5454
// On Windows, IPv6 would need: `%${localInterface.scopeid}`
5555
if (FAM === 'IPv6')

test/internet/test-dgram-multicast-ssm-multi-process.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ get_sourceAddress: for (const name in networkInterfaces) {
2828
const interfaces = networkInterfaces[name];
2929
for (let i = 0; i < interfaces.length; i++) {
3030
const localInterface = interfaces[i];
31-
if (!localInterface.internal && localInterface.family === 4) {
31+
if (!localInterface.internal && localInterface.family === 'IPv4') {
3232
sourceAddress = localInterface.address;
3333
break get_sourceAddress;
3434
}

test/internet/test-dgram-multicast-ssmv6-multi-process.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ get_sourceAddress: for (const name in networkInterfaces) {
2828
const interfaces = networkInterfaces[name];
2929
for (let i = 0; i < interfaces.length; i++) {
3030
const localInterface = interfaces[i];
31-
if (!localInterface.internal && localInterface.family === 6) {
31+
if (!localInterface.internal && localInterface.family === 'IPv6') {
3232
sourceAddress = localInterface.address;
3333
break get_sourceAddress;
3434
}

test/internet/test-dns-lookup.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dns.lookup(addresses.NOT_FOUND, {
4747

4848
assert.throws(
4949
() => dnsPromises.lookup(addresses.NOT_FOUND, {
50-
family: 'IPv4',
50+
family: 'ipv4',
5151
all: 'all'
5252
}),
5353
{ code: 'ERR_INVALID_ARG_VALUE' }

test/parallel/parallel.status

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ test-crypto-dh-stateless: SKIP
3131
test-crypto-keygen: SKIP
3232

3333
[$system==solaris] # Also applies to SmartOS
34+
# https://github.com/nodejs/node/pull/43054
35+
test-net-socket-connect-without-cb: SKIP
36+
test-net-socket-ready-without-cb: SKIP
37+
test-tcp-wrap-listen: SKIP
3438

3539
[$system==freebsd]
3640
# https://github.com/nodejs/node/issues/31727
@@ -39,6 +43,10 @@ test-fs-stat-bigint: PASS,FLAKY
3943
test-worker-message-port-message-before-close: PASS,FLAKY
4044

4145
[$system==aix]
46+
# https://github.com/nodejs/node/pull/43054
47+
test-net-socket-connect-without-cb: SKIP
48+
test-net-socket-ready-without-cb: SKIP
49+
test-tcp-wrap-listen: SKIP
4250

4351
[$system==ibmi]
4452
# https://github.com/nodejs/node/pull/30819

0 commit comments

Comments
 (0)