mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
net_uv: add isIP
Issue #1270: Doesn't completely pass test/simple/test-net-isip.js yet.
This commit is contained in:
1 parent
f6fa20fd55
commit
38f3bf6610
1 file changed
+32
@@ -540,3 +540,35 @@ function onconnection(clientHandle) {
|
||||
Server.prototype.close = function() {
|
||||
this._handle.close();
|
||||
};
|
||||
|
||||
|
||||
// TODO: isIP should be moved to the DNS code. Putting it here now because
|
||||
// this is what the legacy system did.
|
||||
exports.isIP = function(input) {
|
||||
if (!input) {
|
||||
return 4;
|
||||
} else if (/^(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)$/.test(input)) {
|
||||
var parts = input.split('.');
|
||||
for (var i = 0; i < parts.length; i++) {
|
||||
var part = parseInt(parts[i]);
|
||||
if (part < 0 || 255 < part) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 4;
|
||||
} else if (/^(::)?([a-fA-F0-9]+(::?)?)*$/.test(input)) {
|
||||
return 6;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
exports.isIPv4 = function(input) {
|
||||
return TCP.isIP(input) === 4;
|
||||
};
|
||||
|
||||
|
||||
exports.isIPv6 = function(input) {
|
||||
return TCP.isIP(input) === 6;
|
||||
};
|
||||
Reference in new issue
Block a user