Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 34bb24a

Browse files
committedJun 5, 2022
net: make server.address() return a string for family
This reverts commits 68fb0bf, 4ad342a, and 3a26db9. Fixes: nodejs#43014
1 parent e169d54 commit 34bb24a

23 files changed

+56
-46
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/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/net.js

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

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

807805
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/parallel/test-dgram-address.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const dgram = require('dgram');
3535
assert.strictEqual(typeof address.port, 'number');
3636
assert.ok(isFinite(address.port));
3737
assert.ok(address.port > 0);
38-
assert.strictEqual(address.family, 4);
38+
assert.strictEqual(address.family, 'IPv4');
3939
socket.close();
4040
}));
4141

@@ -59,7 +59,7 @@ if (common.hasIPv6) {
5959
assert.strictEqual(typeof address.port, 'number');
6060
assert.ok(isFinite(address.port));
6161
assert.ok(address.port > 0);
62-
assert.strictEqual(address.family, 6);
62+
assert.strictEqual(address.family, 'IPv6');
6363
socket.close();
6464
}));
6565

‎test/parallel/test-dgram-udp6-link-local-address.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const { isWindows } = common;
1212
function linklocal() {
1313
for (const [ifname, entries] of Object.entries(os.networkInterfaces())) {
1414
for (const { address, family, scopeid } of entries) {
15-
if (family === 6 && address.startsWith('fe80:')) {
15+
if (family === 'IPv6' && address.startsWith('fe80:')) {
1616
return { address, ifname, scopeid };
1717
}
1818
}

‎test/parallel/test-net-listen-invalid-port.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const invalidPort = -1 >>> 0;
1212

1313
net.Server().listen(0, function() {
1414
const address = this.address();
15-
const key = `${address.family}:${address.address}:0`;
15+
const key = `${address.family.slice(-1)}:${address.address}:0`;
1616

1717
assert.strictEqual(this._connectionKey, key);
1818
this.close();

‎test/parallel/test-net-socket-connect-without-cb.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const server = net.createServer(common.mustCall(function(conn) {
1717
}));
1818

1919
const address = server.address();
20-
if (!common.hasIPv6 && address.family === 6) {
20+
if (!common.hasIPv6 && address.family === 'IPv6') {
2121
// Necessary to pass CI running inside containers.
2222
client.connect(address.port);
2323
} else {

‎test/parallel/test-os.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ switch (platform) {
138138
const expected = [{
139139
address: '127.0.0.1',
140140
netmask: '255.0.0.0',
141-
family: 4,
141+
family: 'IPv4',
142142
mac: '00:00:00:00:00:00',
143143
internal: true,
144144
cidr: '127.0.0.1/8'
@@ -154,7 +154,7 @@ switch (platform) {
154154
const expected = [{
155155
address: '127.0.0.1',
156156
netmask: '255.0.0.0',
157-
family: 4,
157+
family: 'IPv4',
158158
mac: '00:00:00:00:00:00',
159159
internal: true,
160160
cidr: '127.0.0.1/8'

‎test/sequential/test-net-server-address.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const net = require('net');
2626

2727
// Test on IPv4 Server
2828
{
29-
const family = 4;
29+
const family = 'IPv4';
3030
const server = net.createServer();
3131

3232
server.on('error', common.mustNotCall());
@@ -46,7 +46,7 @@ if (!common.hasIPv6) {
4646
return;
4747
}
4848

49-
const family6 = 6;
49+
const family6 = 'IPv6';
5050
const anycast6 = '::';
5151

5252
// Test on IPv6 Server

‎test/sequential/test-net-server-bind.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const net = require('net');
2424
const address = server.address();
2525
assert.strictEqual(address.port, common.PORT);
2626

27-
if (address.family === 6)
27+
if (address.family === 'IPv6')
2828
assert.strictEqual(server._connectionKey, `6::::${address.port}`);
2929
else
3030
assert.strictEqual(server._connectionKey, `4:0.0.0.0:${address.port}`);

0 commit comments

Comments
 (0)
Please sign in to comment.