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: port isIPv6() to JS #22673

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 22 additions & 5 deletions lib/internal/net.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
'use strict';

const Buffer = require('buffer').Buffer;
const { internalBinding } = require('internal/bootstrap/loaders');
const { isIPv6 } = internalBinding('cares_wrap');
const { writeBuffer } = process.binding('fs');
const errors = require('internal/errors');

const octet = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
const re = new RegExp(`^${octet}[.]${octet}[.]${octet}[.]${octet}$`);
// IPv4 Segment
const v4Seg = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
const v4Str = `(${v4Seg}[.]){3}${v4Seg}`;
const IPv4Reg = new RegExp(`^${v4Str}$`);

// IPv6 Segment
const v6Seg = '(?:[0-9a-f]{1,4})';
const IPv6Reg = new RegExp('^(' +
`(?:${v6Seg}:){7}(?:${v6Seg}|:)|` +
`(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` +
`(?:${v6Seg}:){5}(?::${v4Str}|(:${v6Seg}){1,2}|:)|` +
`(?:${v6Seg}:){4}(?:(:${v6Seg}){0,1}:${v4Str}|(:${v6Seg}){1,3}|:)|` +
`(?:${v6Seg}:){3}(?:(:${v6Seg}){0,2}:${v4Str}|(:${v6Seg}){1,4}|:)|` +
`(?:${v6Seg}:){2}(?:(:${v6Seg}){0,3}:${v4Str}|(:${v6Seg}){1,5}|:)|` +
`(?:${v6Seg}:){1}(?:(:${v6Seg}){0,4}:${v4Str}|(:${v6Seg}){1,6}|:)|` +
`(?::((?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` +
')(%[0-9a-zA-Z]{1,})?$');

function isIPv4(s) {
return re.test(s);
return IPv4Reg.test(s);
}

function isIPv6(s) {
return IPv6Reg.test(s);
}

function isIP(s) {
Expand Down
6 changes: 0 additions & 6 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1954,11 +1954,6 @@ int ParseIP(const char* ip, ParseIPResult* result = nullptr) {
return 0;
}

void IsIPv6(const FunctionCallbackInfo<Value>& args) {
node::Utf8Value ip(args.GetIsolate(), args[0]);
args.GetReturnValue().Set(6 == ParseIP(*ip));
}

void CanonicalizeIP(const FunctionCallbackInfo<Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
node::Utf8Value ip(isolate, args[0]);
Expand Down Expand Up @@ -2207,7 +2202,6 @@ void Initialize(Local<Object> target,

env->SetMethod(target, "getaddrinfo", GetAddrInfo);
env->SetMethod(target, "getnameinfo", GetNameInfo);
env->SetMethodNoSideEffect(target, "isIPv6", IsIPv6);
env->SetMethodNoSideEffect(target, "canonicalizeIP", CanonicalizeIP);

env->SetMethod(target, "strerror", StrError);
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 @@ -91,3 +91,32 @@ assert.strictEqual(net.isIPv6({
}), true);
assert.strictEqual(net.isIPv6({ toString: () => '127.0.0.1' }), false);
assert.strictEqual(net.isIPv6({ toString: () => 'bla' }), false);
assert.strictEqual(net.isIPv6('1:2:3:4:5:6:7:8'), true);
assert.strictEqual(net.isIPv6('1::'), true);
assert.strictEqual(net.isIPv6('1:2:3:4:5:6:7::'), true);
assert.strictEqual(net.isIPv6('1::8'), true);
assert.strictEqual(net.isIPv6('1:2:3:4:5:6::8'), true);
assert.strictEqual(net.isIPv6('1::7:8'), true);
assert.strictEqual(net.isIPv6('1:2:3:4:5::7:8'), true);
assert.strictEqual(net.isIPv6('1:2:3:4:5::8'), true);
assert.strictEqual(net.isIPv6('1::6:7:8'), true);
assert.strictEqual(net.isIPv6('1:2:3:4::6:7:8'), true);
assert.strictEqual(net.isIPv6('1:2:3:4::8'), true);
assert.strictEqual(net.isIPv6('1::5:6:7:8'), true);
assert.strictEqual(net.isIPv6('1:2:3::5:6:7:8'), true);
assert.strictEqual(net.isIPv6('1:2:3::8'), true);
assert.strictEqual(net.isIPv6('1::4:5:6:7:8'), true);
assert.strictEqual(net.isIPv6('1:2::4:5:6:7:8'), true);
assert.strictEqual(net.isIPv6('1:2::8'), true);
assert.strictEqual(net.isIPv6('1::3:4:5:6:7:8'), true);
assert.strictEqual(net.isIPv6('1::8'), true);
assert.strictEqual(net.isIPv6('::2:3:4:5:6:7:8'), true);
assert.strictEqual(net.isIPv6('::8'), true);
assert.strictEqual(net.isIPv6('::'), true);
assert.strictEqual(net.isIPv6('fe80::7:8%1'), true);
assert.strictEqual(net.isIPv6('::255.255.255.255'), true);
assert.strictEqual(net.isIPv6('::ffff:255.255.255.255'), true);
assert.strictEqual(net.isIPv6('::ffff:0:255.255.255.255'), true);
assert.strictEqual(net.isIPv6('1:2:3:4::1.2.3.4'), true);
assert.strictEqual(net.isIPv6('1:2::1.2.3.4'), true);
assert.strictEqual(net.isIPv6('fe80::7:8%abcd'), true);