Skip to content

Commit 3a26db9

Browse files
committed
net: make server.address() return an integer for family
`dns.lookup` options only accepts integer for `family` options, having a string doesn't really make sense here. PR-URL: #41431 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 6706be1 commit 3a26db9

21 files changed

+40
-33
lines changed

lib/net.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ protoGetter('remoteAddress', function remoteAddress() {
760760
});
761761

762762
protoGetter('remoteFamily', function remoteFamily() {
763-
return this._getpeername().family;
763+
return `IPv${this._getpeername().family}`;
764764
});
765765

766766
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 === 'IPv6') {
219+
if (family === 6) {
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: 'IPv4' | 'IPv6'
251+
* family: 4 | 6
252252
* mac: string
253253
* internal: boolean
254254
* scopeid: number

src/env.h

-2
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,6 @@ constexpr size_t kFsStatsBufferLength =
289289
V(input_string, "input") \
290290
V(internal_binding_string, "internalBinding") \
291291
V(internal_string, "internal") \
292-
V(ipv4_string, "IPv4") \
293-
V(ipv6_string, "IPv6") \
294292
V(isclosing_string, "isClosing") \
295293
V(issuer_string, "issuer") \
296294
V(issuercert_string, "issuerCertificate") \

src/node_os.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ 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, family;
177+
Local<String> name;
178+
Local<Integer> family;
178179

179180
int err = uv_interface_addresses(&interfaces, &count);
180181

@@ -214,14 +215,14 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
214215
if (interfaces[i].address.address4.sin_family == AF_INET) {
215216
uv_ip4_name(&interfaces[i].address.address4, ip, sizeof(ip));
216217
uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask));
217-
family = env->ipv4_string();
218+
family = Integer::New(env->isolate(), 4);
218219
} else if (interfaces[i].address.address4.sin_family == AF_INET6) {
219220
uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
220221
uv_ip6_name(&interfaces[i].netmask.netmask6, netmask, sizeof(netmask));
221-
family = env->ipv6_string();
222+
family = Integer::New(env->isolate(), 6);
222223
} else {
223224
strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN);
224-
family = env->unknown_string();
225+
family = Integer::New(env->isolate(), 0);
225226
}
226227

227228
result.emplace_back(name);

src/tcp_wrap.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ MaybeLocal<Object> AddressToJS(Environment* env,
380380
OneByteString(env->isolate(), ip)).Check();
381381
info->Set(env->context(),
382382
env->family_string(),
383-
env->ipv6_string()).Check();
383+
Integer::New(env->isolate(), 6)).Check();
384384
info->Set(env->context(),
385385
env->port_string(),
386386
Integer::New(env->isolate(), port)).Check();
@@ -395,7 +395,7 @@ MaybeLocal<Object> AddressToJS(Environment* env,
395395
OneByteString(env->isolate(), ip)).Check();
396396
info->Set(env->context(),
397397
env->family_string(),
398-
env->ipv4_string()).Check();
398+
Integer::New(env->isolate(), 4)).Check();
399399
info->Set(env->context(),
400400
env->port_string(),
401401
Integer::New(env->isolate(), port)).Check();

test/common/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ const common = {
845845
const re = isWindows ? /Loopback Pseudo-Interface/ : /lo/;
846846
return Object.keys(iFaces).some((name) => {
847847
return re.test(name) &&
848-
iFaces[name].some(({ family }) => family === 'IPv6');
848+
iFaces[name].some(({ family }) => family === 6);
849849
});
850850
},
851851

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: 'IPv4', port: 1337 });
19+
Object.assign(obj, { address: '127.0.0.1', family: 4, port: 1337 });
2020
return 0;
2121
};
2222

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

7373
let familyInt;
7474
switch (family) {
75-
case 'IPv4': familyInt = 4; break;
76-
case 'IPv6': familyInt = 6; break;
75+
case 4: familyInt = 4; break;
76+
case 6: 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 === 'IPv6' ? `[${iface?.address}]` : iface?.address,
42+
hostname: iface?.family === 6 ? `[${iface.address}]` : iface?.address,
4343
listenOptions: {
4444
host: iface?.address,
45-
ipv6Only: iface?.family === 'IPv6'
45+
ipv6Only: iface?.family === 6
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 === 'IPv4') {
50+
if (!localInterface.internal && localInterface.family === 4) {
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 && localInterface.family === FAM) {
52+
if (!localInterface.internal && `IPv${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 === 'IPv4') {
31+
if (!localInterface.internal && localInterface.family === 4) {
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 === 'IPv6') {
31+
if (!localInterface.internal && localInterface.family === 6) {
3232
sourceAddress = localInterface.address;
3333
break get_sourceAddress;
3434
}

test/internet/test-dns-ipv4.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Flags: --dns-result-order=ipv4first
2+
13
'use strict';
24
const common = require('../common');
35
const { addresses } = require('../common/internet');

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, 'IPv4');
38+
assert.strictEqual(address.family, 4);
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, 'IPv6');
62+
assert.strictEqual(address.family, 6);
6363
socket.close();
6464
}));
6565

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.slice(-1)}:${address.address}:0`;
15+
const key = `${address.family}:${address.address}:0`;
1616

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

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

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

19-
client.connect(server.address());
19+
const address = server.address();
20+
if (!common.hasIPv6 && address.family === 6) {
21+
// Necessary to pass CI running inside containers.
22+
client.connect(address.port);
23+
} else {
24+
client.connect(address);
25+
}
2026
}));

test/parallel/test-net-socket-ready-without-cb.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const net = require('net');
99
const server = net.createServer(common.mustCall(function(conn) {
1010
conn.end();
1111
server.close();
12-
})).listen(0, common.mustCall(function() {
12+
})).listen(0, 'localhost', common.mustCall(function() {
1313
const client = new net.Socket();
1414

1515
client.on('ready', common.mustCall(function() {

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: 'IPv4',
141+
family: 4,
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: 'IPv4',
157+
family: 4,
158158
mac: '00:00:00:00:00:00',
159159
internal: true,
160160
cidr: '127.0.0.1/8'

test/parallel/test-tcp-wrap-listen.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const r = (common.hasIPv6 ?
1818
server.bind6('::', 0) :
1919
server.bind('0.0.0.0', 0));
2020
assert.strictEqual(r, 0);
21-
let port = {};
21+
22+
const port = {};
2223
server.getsockname(port);
23-
port = port.port;
2424

2525
server.listen(128);
2626

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 = 'IPv4';
29+
const family = 4;
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 = 'IPv6';
49+
const family6 = 6;
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 === 'IPv6')
27+
if (address.family === 6)
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)