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 <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 6706be1 commit 3a26db9

21 files changed

+40
-33
lines changed

lib/net.js

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

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 === '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

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

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

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

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

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

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

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

Lines changed: 1 addition & 1 deletion
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')

0 commit comments

Comments
 (0)