60 lines
2.2 KiB
JavaScript
60 lines
2.2 KiB
JavaScript
"use strict";
|
|
|
|
let ip_input = null;
|
|
let mask_input = null;
|
|
let subnets_input = null;
|
|
let info_output = null;
|
|
|
|
const dataChanged = function() {
|
|
try {
|
|
const ipData = ip_input.value;
|
|
const [fo, se, to, lo] = ipData.split(".").map(e => parseInt(e));
|
|
console.log([fo, se, to, lo]);
|
|
if (fo === undefined || se === undefined || to === undefined || lo === undefined ||
|
|
fo < 0 || se < 0 || to < 0 || lo < 0 ||
|
|
fo > 255 || se > 255 || to > 255 || lo > 255) {
|
|
throw new Error("Wrong IP format");
|
|
}
|
|
|
|
const mask = ~~mask_input.value;
|
|
if (mask < 0 || mask > 31) {
|
|
throw new Error("Wrong mask (in range [0..32))");
|
|
}
|
|
|
|
const networkCapacity = Math.pow(2, 32 - mask);
|
|
const subnets = subnets_input.value.split(" ").map(e => ~e);
|
|
let cumNetSize = 0;
|
|
subnets.forEach(e => {
|
|
cumNetSize += e;
|
|
});
|
|
if (cumNetSize > networkCapacity) {
|
|
throw new Error(`Cannot fit ${cumNetSize} in this subnet`);
|
|
}
|
|
|
|
const ip = new IP(fo, se, to, lo);
|
|
const net = new Subnet(ip, mask);
|
|
|
|
// info_output.textContent = `Subnet info for ${subnet.gateway.toString()}/${mask}\n`;
|
|
// info_output.textContent += `Subnet mask: ${subnet.mask.toString()}\n`;
|
|
// info_output.textContent += `Subnet capacity: ${subnet.subnetCapacity.toString()} addresses\n\n`;
|
|
// info_output.textContent += `First IP: ${subnet.firstIP.toString()}\n`;
|
|
// info_output.textContent += `Last IP: ${subnet.lastIP.toString()}\n`;
|
|
// info_output.textContent += `Broadcast IP: ${subnet.broadcastIP.toString()}\n`;
|
|
} catch (e) {
|
|
info_output.textContent = `Error:\n${e.message}`;
|
|
}
|
|
}
|
|
|
|
|
|
const init = function() {
|
|
ip_input = document.getElementById("ip");
|
|
mask_input = document.getElementById("mask");
|
|
subnets_input = document.getElementById("subnets");
|
|
info_output = document.getElementById("subnects-info");
|
|
|
|
ip_input.addEventListener("input", dataChanged);
|
|
mask_input.addEventListener("input", dataChanged);
|
|
subnets_input.addEventListener("input", dataChanged);
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", init);
|