Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: make isIPv4 and isIPv6 more efficient #5478

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1571,12 +1571,12 @@ exports.isIP = cares.isIP;


exports.isIPv4 = function(input) {
return exports.isIP(input) === 4;
return cares.isIPv4(input);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why you don't just assign it as exports.isIPv4 = cares.isIPv4; ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular reason, but I've just added check for symbol in js, so wrapper is necessary

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about now?

};


exports.isIPv6 = function(input) {
return exports.isIP(input) === 6;
return cares.isIPv6(input);
};


Expand Down
23 changes: 23 additions & 0 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,27 @@ static void IsIP(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(rc);
}

static void IsIPv4(const FunctionCallbackInfo<Value>& args) {
node::Utf8Value ip(args.GetIsolate(), args[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a check that args[0] is a string here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Utf8Value calls ToString under the hood, should be fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe include a test passing null, an object, and a number? Just a suggestion

char address_buffer[sizeof(struct in_addr)];

if (uv_inet_pton(AF_INET, *ip, &address_buffer) == 0) {
args.GetReturnValue().Set(true);
} else {
args.GetReturnValue().Set(false);
}
}

static void IsIPv6(const FunctionCallbackInfo<Value>& args) {
node::Utf8Value ip(args.GetIsolate(), args[0]);
char address_buffer[sizeof(struct in6_addr)];

if (uv_inet_pton(AF_INET6, *ip, &address_buffer) == 0) {
args.GetReturnValue().Set(true);
} else {
args.GetReturnValue().Set(false);
}
}

static void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand Down Expand Up @@ -1327,6 +1348,8 @@ static void Initialize(Local<Object> target,
env->SetMethod(target, "getaddrinfo", GetAddrInfo);
env->SetMethod(target, "getnameinfo", GetNameInfo);
env->SetMethod(target, "isIP", IsIP);
env->SetMethod(target, "isIPv4", IsIPv4);
env->SetMethod(target, "isIPv6", IsIPv6);

env->SetMethod(target, "strerror", StrError);
env->SetMethod(target, "getServers", GetServers);
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-net-isip.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,40 @@ assert.equal(net.isIP('0000:0000:0000:0000:0000:0000:12345:0000'), 0);
assert.equal(net.isIP('0'), 0);
assert.equal(net.isIP(), 0);
assert.equal(net.isIP(''), 0);
assert.equal(net.isIP(null), 0);
assert.equal(net.isIP(123), 0);
assert.equal(net.isIP(true), 0);
assert.equal(net.isIP({}), 0);
assert.equal(net.isIP({ toString: () => '::2001:252:1:255.255.255.255' }), 6);
assert.equal(net.isIP({ toString: () => '127.0.0.1' }), 4);
assert.equal(net.isIP({ toString: () => 'bla' }), 0);

assert.equal(net.isIPv4('127.0.0.1'), true);
assert.equal(net.isIPv4('example.com'), false);
assert.equal(net.isIPv4('2001:252:0:1::2008:6'), false);
assert.equal(net.isIPv4(), false);
assert.equal(net.isIPv4(''), false);
assert.equal(net.isIPv4(null), false);
assert.equal(net.isIPv4(123), false);
assert.equal(net.isIPv4(true), false);
assert.equal(net.isIPv4({}), false);
assert.equal(net.isIPv4({
toString: () => '::2001:252:1:255.255.255.255'
}), false);
assert.equal(net.isIPv4({ toString: () => '127.0.0.1' }), true);
assert.equal(net.isIPv4({ toString: () => 'bla' }), false);

assert.equal(net.isIPv6('127.0.0.1'), false);
assert.equal(net.isIPv6('example.com'), false);
assert.equal(net.isIPv6('2001:252:0:1::2008:6'), true);
assert.equal(net.isIPv6(), false);
assert.equal(net.isIPv6(''), false);
assert.equal(net.isIPv6(null), false);
assert.equal(net.isIPv6(123), false);
assert.equal(net.isIPv6(true), false);
assert.equal(net.isIPv6({}), false);
assert.equal(net.isIPv6({
toString: () => '::2001:252:1:255.255.255.255'
}), true);
assert.equal(net.isIPv6({ toString: () => '127.0.0.1' }), false);
assert.equal(net.isIPv6({ toString: () => 'bla' }), false);