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

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

Lines changed: 11 additions & 5 deletions
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

Lines changed: 8 additions & 2 deletions
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

Lines changed: 9 additions & 6 deletions
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

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

Lines changed: 12 additions & 2 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 4 additions & 5 deletions
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);

0 commit comments

Comments
 (0)