kaanalnet
Version:
Virtual Network Emulator Lab for SDN and traditional networks
132 lines (111 loc) • 3.9 kB
JavaScript
// Generated by CoffeeScript 1.9.3
(function() {
var IPManager, async, extend, ip, iplist, lanipsubnets, log, request, util, wansubnetting;
util = require('util');
request = require('request-json');
extend = require('util')._extend;
ip = require('ip');
async = require('async');
log = require('./utils/logger').getLogger();
log.info("IPManager - Logger test message");
wansubnetting = function(net, curprefix, newprefix) {
var answer, iter, iterations, netmask, newmask, xx;
netmask = ip.fromPrefixLen(curprefix);
newmask = ip.fromPrefixLen(newprefix);
xx = new Buffer(4);
iter = newprefix - curprefix;
iterations = Math.pow(2, iter);
answer = [];
(function() {
var i, j, k, ref, ref1, result, results, str;
results = [];
for (i = j = 0, ref = iterations - 1; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) {
result = ip.subnet(net, newmask);
result.status = "free";
result.iparray = [];
xx = ip.toBuffer(result.firstAddress);
for (i = k = 0, ref1 = result.numHosts - 1; 0 <= ref1 ? k <= ref1 : k >= ref1; i = 0 <= ref1 ? ++k : --k) {
result.iparray[i] = ip.toString(xx);
xx[3]++;
}
answer.push(result);
xx = ip.toBuffer(result.broadcastAddress);
xx[3]++;
if (xx[3] === 0x00) {
xx[2]++;
}
str = ip.toString(xx);
results.push(net = str);
}
return results;
})();
return answer;
};
iplist = function(address) {
var i, iparray, j, ref, result, xx;
iparray = [];
result = ip.subnet(address, '255.255.255.0');
xx = ip.toBuffer(result.firstAddress);
for (i = j = 0, ref = result.numHosts - 1; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) {
iparray[i] = ip.toString(xx);
xx[3]++;
}
return iparray;
};
lanipsubnets = function(address) {
var answer, iterations, net, netmask, xx;
netmask = '255.255.255.0';
net = address;
iterations = 255;
xx = new Buffer(4);
answer = [];
(function() {
var i, j, k, ref, ref1, result, results;
results = [];
for (i = j = 0, ref = iterations - 1; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) {
result = ip.subnet(net, netmask);
result.status = "free";
result.iparray = [];
xx = ip.toBuffer(result.firstAddress);
for (i = k = 0, ref1 = result.numHosts - 1; 0 <= ref1 ? k <= ref1 : k >= ref1; i = 0 <= ref1 ? ++k : --k) {
result.iparray[i] = ip.toString(xx);
xx[3]++;
}
answer.push(result);
xx = ip.toBuffer(net);
xx[2]++;
results.push(net = ip.toString(xx));
}
return results;
})();
return answer;
};
IPManager = (function() {
function IPManager(wan, lan, mgmt) {
log.info("IPManager starts with wan pool " + wan + " lan pool " + lan + " mgmt pool " + mgmt);
this.wansubnets = wansubnetting(wan, 24, 29);
this.lansubnets = lanipsubnets(lan);
this.wanindex = 0;
this.lanindex = 0;
this.mgmtindex = 1;
this.mgmtips = iplist(mgmt);
}
IPManager.prototype.listwansubnets = function() {
return log.info(" IPManager - listwansubnets " + JSON.stringify(this.wansubnets));
};
IPManager.prototype.listlanubnets = function() {
return log.info("IPManager - listlansubnets " + JSON.stringify(this.lansubnets));
};
IPManager.prototype.getFreeWanSubnet = function() {
return this.wansubnets[this.wanindex++];
};
IPManager.prototype.getFreeLanSubnet = function() {
return this.lansubnets[this.lanindex++];
};
IPManager.prototype.getFreeMgmtIP = function() {
return this.mgmtips[this.mgmtindex++];
};
return IPManager;
})();
module.exports = IPManager;
}).call(this);