Use 'ip' library to process network data
Closes-Bug: #1514493 Change-Id: I8afff721aff7fb9aebbe1fe61653befade43a99a
This commit is contained in:
parent
234371f92b
commit
7cf906b160
|
@ -8308,6 +8308,10 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"ip": {
|
||||
"version": "1.0.2",
|
||||
"from": "ip@1.0.2"
|
||||
},
|
||||
"javascript-natural-sort": {
|
||||
"version": "0.7.1",
|
||||
"from": "javascript-natural-sort@"
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
"gulp-util": "3.0.4",
|
||||
"i18next-client": "1.11.1",
|
||||
"imports-loader": "0.6.4",
|
||||
"ip": "1.0.2",
|
||||
"javascript-natural-sort": "0.7.1",
|
||||
"jquery": "1.11.3",
|
||||
"js-cookie": "1.5.1",
|
||||
|
|
|
@ -140,8 +140,7 @@ define([
|
|||
var getGateway = utils.getDefaultGatewayForCidr;
|
||||
|
||||
assert.equal(getGateway('172.16.0.0/24'), '172.16.0.1', 'Getting default gateway for CIDR');
|
||||
//FIXME: the following test should be restored within #1514493 fix
|
||||
//assert.equal(getGateway('192.168.0.0/10'), '192.128.0.1', 'Getting default gateway for CIDR');
|
||||
assert.equal(getGateway('192.168.0.0/10'), '192.128.0.1', 'Getting default gateway for CIDR');
|
||||
assert.equal(getGateway('172.16.0.0/31'), '', 'No gateway returned for inappropriate CIDR (network is too small)');
|
||||
assert.equal(getGateway('172.16.0.0/'), '', 'No gateway returned for invalid CIDR');
|
||||
});
|
||||
|
@ -150,8 +149,7 @@ define([
|
|||
var getRange = utils.getDefaultIPRangeForCidr;
|
||||
|
||||
assert.deepEqual(getRange('172.16.0.0/24'), [['172.16.0.1', '172.16.0.254']], 'Getting default IP range for CIDR');
|
||||
//FIXME: the following test should be restored within #1514493 fix
|
||||
//assert.deepEqual(getRange('192.168.0.0/10', true), [['192.128.0.2', '192.191.255.254']], 'Gateway address excluded from default IP range');
|
||||
assert.deepEqual(getRange('192.168.0.0/10', true), [['192.128.0.2', '192.191.255.254']], 'Gateway address excluded from default IP range');
|
||||
assert.deepEqual(getRange('172.16.0.0/31'), [['', '']], 'No IP range returned for inappropriate CIDR (network is too small)');
|
||||
assert.deepEqual(getRange('172.16.0.0/', true), [['', '']], 'No IP range returned for invalid CIDR');
|
||||
});
|
||||
|
@ -161,7 +159,8 @@ define([
|
|||
|
||||
assert.ok(validate('172.16.0.0/20', '172.16.0.2'), 'Check IP, that corresponds to CIDR');
|
||||
assert.ok(validate('172.16.0.5/24', '172.16.0.2'), 'Check IP, that corresponds to CIDR');
|
||||
assert.ok(validate('172.16.0.0/20', '172.16.15.255'), 'Check boundary value');
|
||||
assert.notOk(validate('172.16.0.0/20', '172.16.15.255'), 'Check broadcast address');
|
||||
assert.notOk(validate('172.16.0.0/20', '172.16.0.0'), 'Check network address');
|
||||
assert.notOk(validate('192.168.0.0/10', '192.231.255.254'), 'Check IP, that does not correspond to CIDR');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -24,8 +24,9 @@ define([
|
|||
'javascript-natural-sort',
|
||||
'expression',
|
||||
'expression/objects',
|
||||
'react'
|
||||
], function(require, $, _, i18n, Backbone, classNames, naturalSort, Expression, expressionObjects, React) {
|
||||
'react',
|
||||
'ip'
|
||||
], function(require, $, _, i18n, Backbone, classNames, naturalSort, Expression, expressionObjects, React, IP) {
|
||||
'use strict';
|
||||
|
||||
var utils = {
|
||||
|
@ -213,7 +214,7 @@ define([
|
|||
return !_.isString(ip) || !ip.match(utils.regexes.ip);
|
||||
},
|
||||
validateIPrange: function(startIP, endIP) {
|
||||
return utils.ipIntRepresentation(startIP) - utils.ipIntRepresentation(endIP) <= 0;
|
||||
return IP.toLong(startIP) - IP.toLong(endIP) <= 0;
|
||||
},
|
||||
validateIpRanges: function(ranges, cidr, disallowSingleAddress) {
|
||||
var ipRangesErrors = [];
|
||||
|
@ -240,44 +241,28 @@ define([
|
|||
}
|
||||
return ipRangesErrors;
|
||||
},
|
||||
ipIntRepresentation: function(ip) {
|
||||
return _.reduce(ip.split('.'), function(sum, octet, index) {return sum + octet * Math.pow(256, 3 - index);}, 0);
|
||||
},
|
||||
validateIpCorrespondsToCIDR: function(cidr, ip) {
|
||||
var result = true;
|
||||
if (cidr) {
|
||||
/* jshint bitwise: false */
|
||||
var networkAddressToInt = utils.ipIntRepresentation(cidr.split('/')[0]);
|
||||
var netmask = ~((Math.pow(2, 32) - 1) >>> cidr.split('/')[1]);
|
||||
var ipToInt = utils.ipIntRepresentation(ip);
|
||||
result = (networkAddressToInt & netmask).toString(16) == (ipToInt & netmask).toString(16);
|
||||
/* jshint bitwise: true */
|
||||
}
|
||||
return result;
|
||||
if (!cidr) return true;
|
||||
var networkData = IP.cidrSubnet(cidr),
|
||||
ipInt = IP.toLong(ip);
|
||||
return ipInt >= IP.toLong(networkData.firstAddress) && ipInt <= IP.toLong(networkData.lastAddress);
|
||||
},
|
||||
validateVlanRange: function(vlanStart, vlanEnd, vlan) {
|
||||
return vlan >= vlanStart && vlan <= vlanEnd;
|
||||
},
|
||||
intToIP: function(ipInt) {
|
||||
/* jshint bitwise: false */
|
||||
var ip = [ipInt >>> 24, ipInt >>> 16 & 0xFF, ipInt >>> 8 & 0xFF, ipInt & 0xFF].join('.');
|
||||
/* jshint bitwise: true */
|
||||
return ip;
|
||||
},
|
||||
getDefaultGatewayForCidr: function(cidr) {
|
||||
if (!_.isEmpty(utils.validateCidr(cidr))) return '';
|
||||
var gatewayInt = utils.ipIntRepresentation(cidr.split('/')[0]) + 1; // the first address isn't used
|
||||
return utils.intToIP(gatewayInt);
|
||||
return IP.cidrSubnet(cidr).firstAddress;
|
||||
},
|
||||
getDefaultIPRangeForCidr: function(cidr, excludeGateway) {
|
||||
if (!_.isEmpty(utils.validateCidr(cidr))) return [['', '']];
|
||||
cidr = cidr.split('/');
|
||||
var netAddressInt = utils.ipIntRepresentation(cidr[0]);
|
||||
var startIPInt = netAddressInt + 1;
|
||||
if (excludeGateway) startIPInt++;
|
||||
var endIPInt = _.min([Math.pow(2, 32 - cidr[1]) + netAddressInt - 1, utils.ipIntRepresentation('255.255.255.255')]);
|
||||
endIPInt--; // broadcast address isn't used
|
||||
return [[utils.intToIP(startIPInt), utils.intToIP(endIPInt)]];
|
||||
var networkData = IP.cidrSubnet(cidr);
|
||||
if (excludeGateway) {
|
||||
var startIPInt = IP.toLong(networkData.firstAddress);
|
||||
startIPInt++;
|
||||
return [[IP.fromLong(startIPInt), networkData.lastAddress]];
|
||||
}
|
||||
return [[networkData.firstAddress, networkData.lastAddress]];
|
||||
},
|
||||
sortEntryProperties: function(entry, sortOrder) {
|
||||
sortOrder = sortOrder || ['name'];
|
||||
|
|
Loading…
Reference in New Issue