Name
sn_cmdb_int_util.CmdbIntegrationNetworkUtil
Description
Utility class to cleanse network and IP related fields
Script
var CmdbIntegrationNetworkUtil = Class.create();
/*
cleanseIpAddress : extracts and cleanses an ip address. works on both ipv4 and ipv6.
deriveIpVersion : given a cleansed ip address returns 4 or 6 depending on ip verison
cleanseMacAddress : extracts and cleanses a mac address. output is lower case and in the form xx:xx:xx:xx:xx:xx.
*/
CmdbIntegrationNetworkUtil.prototype = {
initialize: function() {
this.ipv4Reg = /\d{1,3}[,\-.|_ ]\d{1,3}[,\-. ]\d{1,3}[,\-. ]\d{1,3}/g;
this.ipv4ReplaceReg = /[,\-.|_ ]/g;
this.ipv4CleanReg = /^\d{1,3}[.]\d{1,3}[.]\d{1,3}[.]\d{1,3}$/g;
this.localhost = '127.0.0.1';
this.empty = '0.0.0.0';
this.INVALID_MAC_ADDRESSES = ["00:00:00:00:00:00","aa:aa:aa:aa:aa:aa"];
/*
what each group is trying to catch
(
([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}| // 1:2:3:4:5:6:7:8
:((:[0-9a-fA-F]{1,4}){1,7}|:)| // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8
[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})| // 1::3:4:5:6:7:8 1::3:4:5:6:7:8 1::8
([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}| // 1::4:5:6:7:8 1:2::4:5:6:7:8 1:2::8
([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}| // 1::5:6:7:8 1:2:3::5:6:7:8 1:2:3::8
([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}| // 1::6:7:8 1:2:3:4::6:7:8 1:2:3:4::8
([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}| // 1::7:8 1:2:3:4:5::7:8 1:2:3:4:5::8
([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}| // 1::8 1:2:3:4:5:6::8 1:2:3:4:5:6::8
([0-9a-fA-F]{1,4}:){1,7}: // 1:: 1:2:3:4:5:6:7::
)
not included are patterns to catch mixed ipv4/ipv6 structures
fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}| // fe80::7:8%eth0 fe80::7:8%1 (link-local IPv6 addresses with zone index)
::(ffff(:0{1,4}){0,1}:){0,1}
((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}
(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])| // ::255.255.255.255 ::ffff:255.255.255.255 ::ffff:0:255.255.255.255 (IPv4-mapped IPv6 addresses and IPv4-translated addresses)
([0-9a-fA-F]{1,4}:){1,4}:
((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}
(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]) // 2001:db8:3:4::192.0.2.33 64:ff9b::192.0.2.33 (IPv4-Embedded IPv6 Address)
*/
this.ipv6Reg = /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|:((:[0-9a-fA-F]{1,4}){1,7}|:)|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:)/g;
this.macReg = /([a-f0-9]{2}[-: ]?){5}[a-f0-9]{2}/g;
this.macCleansedReg = /^([a-f0-9]{2}[:]){5}[a-f0-9]{2}$/g;
this.macReplaceReg = /[-: ]/g;
this.macChunkingReg = /.{2}/g;
},
cleanseIpAddress: function(ipAddressValueIn) {
if (!ipAddressValueIn)
return null;
// check for IP as an int
if (!isNaN(ipAddressValueIn) && ipAddressValueIn > 0 && ipAddressValueIn < 4294967296)
ipAddressValueIn = this._convertIpFromIntToIpv4(ipAddressValueIn);
// IPv4 test
var matchingValue = ipAddressValueIn.match(this.ipv4Reg);
if (matchingValue && matchingValue.length > 0) {
// return the first valid ipv4 we find
for (var i = 0; i < matchingValue.length; i++) {
// converting from misc punctuation that might exist -> periods
var cleansedIpV4 = matchingValue[i].replace(this.ipv4ReplaceReg, '.');
// we're not going to return any value for localhost or empty
if (this.localhost == cleansedIpV4 || this.empty == cleansedIpV4)
continue;
else
return cleansedIpV4;
}
return null;
}
// IPv6 test
matchingValue = ipAddressValueIn.match(this.ipv6Reg);
if (matchingValue && matchingValue[0]) {
return matchingValue[0].toLowerCase();
}
return null;
},
// given a cleansed ip address return the lookup value for cmdb_ci_ip_address.ip_version in sys_choice
deriveIpVersion: function(ipAddress) {
if (!ipAddress)
return null;
if (this.ipv4CleanReg.test(ipAddress))
return 4;
if (this.ipv6Reg.test(ipAddress))
return 6;
return null;
},
cleanseMacAddress: function(macAddress) {
if (!macAddress)
return null;
// common with electronic scanners to simply pass the regex
if (this.macCleansedReg.test(macAddress)) {
return this._nullInvalidMac(macAddress);
}
var macOut = macAddress.toLowerCase();
var macMatch = macOut.match(this.macReg);
if (!macMatch || macMatch.length == 0)
return null;
macOut = macMatch[0].replace(this.macReplaceReg, ":");
if (this.macCleansedReg.test(macOut)) {
return this._nullInvalidMac(macOut);
}
// there is a case of 12ab12ab12ab (no colons) we need to add the colons in
macChunks = macOut.match(this.macChunkingReg);
// something unexpected
if (!macChunks || macChunks.length != 6)
return null;
return this._nullInvalidMac(macChunks.join(":"));
},
_convertIpFromIntToIpv4: function(ipAddress) {
var ipOut = ipAddress % 256;
for (var i = 3; i > 0; i--) {
ipAddress = Math.floor(ipAddress / 256);
ipOut = ipAddress % 256 + '.' + ipOut;
}
return ipOut;
},
_nullInvalidMac: function(macAddress) {
if (this.INVALID_MAC_ADDRESSES.indexOf(macAddress) > -1) {
return null;
}
return macAddress;
},
type: 'CmdbIntegrationNetworkUtil'
};
Sys ID
df98c1b4732100102b6265a751ab9ef3