diff options
author | Vladimir Kurchatkin <vladimir.kurchatkin@gmail.com> | 2016-02-28 19:44:39 +0300 |
---|---|---|
committer | Myles Borins <mborins@us.ibm.com> | 2016-03-30 13:12:13 -0700 |
commit | 86484205867a8aac04b4fe44f8c7abc2bd5848aa (patch) | |
tree | 5f69f337c690dfc46e4e99bd9be6654d70e123b6 | |
parent | acbce4b72b43d5027e8c3e1e5a305908b284384c (diff) | |
download | nodejs-86484205867a8aac04b4fe44f8c7abc2bd5848aa.tar.gz nodejs-86484205867a8aac04b4fe44f8c7abc2bd5848aa.tar.bz2 nodejs-86484205867a8aac04b4fe44f8c7abc2bd5848aa.zip |
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: https://github.com/nodejs/node/pull/5478
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
-rw-r--r-- | lib/net.js | 4 | ||||
-rw-r--r-- | src/cares_wrap.cc | 23 | ||||
-rw-r--r-- | test/parallel/test-net-isip.js | 29 |
3 files changed, 54 insertions, 2 deletions
diff --git a/lib/net.js b/lib/net.js index 36ca93c49..020f9bd45 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1554,12 +1554,12 @@ exports.isIP = cares.isIP; exports.isIPv4 = function(input) { - return exports.isIP(input) === 4; + return cares.isIPv4(input); }; exports.isIPv6 = function(input) { - return exports.isIP(input) === 6; + return cares.isIPv6(input); }; diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 46636c528..66d5d08fe 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -1044,6 +1044,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]); + 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); @@ -1283,6 +1304,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); diff --git a/test/parallel/test-net-isip.js b/test/parallel/test-net-isip.js index 76432ed34..6b159b59d 100644 --- a/test/parallel/test-net-isip.js +++ b/test/parallel/test-net-isip.js @@ -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); |