// TODO: isIP should be moved to the DNS code. Putting it here now because
// this is what the legacy system did.
+// NOTE: This does not accept IPv6 with an IPv4 dotted address at the end,
+// and it does not detect more than one double : in a string.
exports.isIP = function(input) {
if (!input) {
return 4;
}
}
return 4;
- } else if (/^(::)?([a-fA-F0-9]+(::?)?)*$/.test(input)) {
+ } else if (/^::|^::1|^([a-fA-F0-9]{1,4}::?){1,7}([a-fA-F0-9]{1,4})$/.test(input)) {
return 6;
} else {
return 0;
assert.equal(net.isIP('2001:252:0:1::2008:6'), 6);
assert.equal(net.isIP('2001:dead:beef:1::2008:6'), 6);
assert.equal(net.isIP('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'), 6);
+assert.equal(net.isIP('::1'), 6);
+assert.equal(net.isIP('::'), 6);
+assert.equal(net.isIP('0000:0000:0000:0000:0000:0000:00001:0000'), 0);
+assert.equal(net.isIP('0'), 0);
assert.equal(net.isIPv4('127.0.0.1'), true);
assert.equal(net.isIPv4('example.com'), false);