Skip to content

Commit 3b20941

Browse files
vkurchatkinjasnell
authored andcommitted
net: make isIPv4 and isIPv6 more efficient
`isIPv4` and `isIPv6` are implemented on top of `isIP`, which in turn checks the sting for being both IPv4 and IPv6, which can be inefficient in some scenarios. This commit makes them use `uv_inet_pton` directly instead. PR-URL: #5478 Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Roman Reiss <[email protected]>
1 parent 6404481 commit 3b20941

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

lib/net.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,12 +1573,12 @@ exports.isIP = cares.isIP;
15731573

15741574

15751575
exports.isIPv4 = function(input) {
1576-
return exports.isIP(input) === 4;
1576+
return cares.isIPv4(input);
15771577
};
15781578

15791579

15801580
exports.isIPv6 = function(input) {
1581-
return exports.isIP(input) === 6;
1581+
return cares.isIPv6(input);
15821582
};
15831583

15841584

src/cares_wrap.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,27 @@ static void IsIP(const FunctionCallbackInfo<Value>& args) {
10871087
args.GetReturnValue().Set(rc);
10881088
}
10891089

1090+
static void IsIPv4(const FunctionCallbackInfo<Value>& args) {
1091+
node::Utf8Value ip(args.GetIsolate(), args[0]);
1092+
char address_buffer[sizeof(struct in_addr)];
1093+
1094+
if (uv_inet_pton(AF_INET, *ip, &address_buffer) == 0) {
1095+
args.GetReturnValue().Set(true);
1096+
} else {
1097+
args.GetReturnValue().Set(false);
1098+
}
1099+
}
1100+
1101+
static void IsIPv6(const FunctionCallbackInfo<Value>& args) {
1102+
node::Utf8Value ip(args.GetIsolate(), args[0]);
1103+
char address_buffer[sizeof(struct in6_addr)];
1104+
1105+
if (uv_inet_pton(AF_INET6, *ip, &address_buffer) == 0) {
1106+
args.GetReturnValue().Set(true);
1107+
} else {
1108+
args.GetReturnValue().Set(false);
1109+
}
1110+
}
10901111

10911112
static void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
10921113
Environment* env = Environment::GetCurrent(args);
@@ -1327,6 +1348,8 @@ static void Initialize(Local<Object> target,
13271348
env->SetMethod(target, "getaddrinfo", GetAddrInfo);
13281349
env->SetMethod(target, "getnameinfo", GetNameInfo);
13291350
env->SetMethod(target, "isIP", IsIP);
1351+
env->SetMethod(target, "isIPv4", IsIPv4);
1352+
env->SetMethod(target, "isIPv6", IsIPv6);
13301353

13311354
env->SetMethod(target, "strerror", StrError);
13321355
env->SetMethod(target, "getServers", GetServers);

test/parallel/test-net-isip.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,40 @@ assert.equal(net.isIP('0000:0000:0000:0000:0000:0000:12345:0000'), 0);
3131
assert.equal(net.isIP('0'), 0);
3232
assert.equal(net.isIP(), 0);
3333
assert.equal(net.isIP(''), 0);
34+
assert.equal(net.isIP(null), 0);
35+
assert.equal(net.isIP(123), 0);
36+
assert.equal(net.isIP(true), 0);
37+
assert.equal(net.isIP({}), 0);
38+
assert.equal(net.isIP({ toString: () => '::2001:252:1:255.255.255.255' }), 6);
39+
assert.equal(net.isIP({ toString: () => '127.0.0.1' }), 4);
40+
assert.equal(net.isIP({ toString: () => 'bla' }), 0);
3441

3542
assert.equal(net.isIPv4('127.0.0.1'), true);
3643
assert.equal(net.isIPv4('example.com'), false);
3744
assert.equal(net.isIPv4('2001:252:0:1::2008:6'), false);
45+
assert.equal(net.isIPv4(), false);
46+
assert.equal(net.isIPv4(''), false);
47+
assert.equal(net.isIPv4(null), false);
48+
assert.equal(net.isIPv4(123), false);
49+
assert.equal(net.isIPv4(true), false);
50+
assert.equal(net.isIPv4({}), false);
51+
assert.equal(net.isIPv4({
52+
toString: () => '::2001:252:1:255.255.255.255'
53+
}), false);
54+
assert.equal(net.isIPv4({ toString: () => '127.0.0.1' }), true);
55+
assert.equal(net.isIPv4({ toString: () => 'bla' }), false);
3856

3957
assert.equal(net.isIPv6('127.0.0.1'), false);
4058
assert.equal(net.isIPv6('example.com'), false);
4159
assert.equal(net.isIPv6('2001:252:0:1::2008:6'), true);
60+
assert.equal(net.isIPv6(), false);
61+
assert.equal(net.isIPv6(''), false);
62+
assert.equal(net.isIPv6(null), false);
63+
assert.equal(net.isIPv6(123), false);
64+
assert.equal(net.isIPv6(true), false);
65+
assert.equal(net.isIPv6({}), false);
66+
assert.equal(net.isIPv6({
67+
toString: () => '::2001:252:1:255.255.255.255'
68+
}), true);
69+
assert.equal(net.isIPv6({ toString: () => '127.0.0.1' }), false);
70+
assert.equal(net.isIPv6({ toString: () => 'bla' }), false);

0 commit comments

Comments
 (0)