"use strict"; class IP { /** * * @param {Number} fo First octet * @param {Number} se Second octet * @param {Number} to Third octet * @param {Number} lo Last octet */ constructor(fo, se, to, lo) { this.raw = BigInt(fo) << 24n | BigInt(se) << 16n | BigInt(to) << 8n | BigInt(lo); } /** * * @param {BigInt} num Amount to add */ add(num=1n) { if (typeof(num) !== typeof(0n)) { num = BigInt(num); } this.raw += num; } /** * * @param {BigInt} num Amount to sub */ sub(num=1n) { if (typeof(num) !== typeof(0n)) { num = BigInt(num); } this.raw -= num; } /** * * @param {Number} num Which octet to pull (0-based) */ octet(num) { if (typeof(num) !== typeof(0n)) { num = BigInt(num); } if (num > 3n) throw new Error("Four octets in IPv4 IP-address"); return this.raw >> (8n * (3n - num)) & 0xFFn; } toString() { return `${this.raw >> 24n & 0xFFn}.${this.raw >> 16n & 0xFFn}.${this.raw >> 8n & 0xFFn}.${this.raw & 0xFFn}`; } clone() { return new IP(this.octet(0n), this.octet(1n), this.octet(2n), this.octet(3n)); } } class Subnet { /** * * @param {IP} ip * @param {Number} subnetMask */ constructor(ip, subnetMask) { this.subnetCapacity = Math.pow(2, 32 - subnetMask); this.gateway = ip; const bmask = "".padStart(32 - subnetMask, 0).padStart(32, 1); const fo = parseInt(bmask.substr(0, 8), 2); const se = parseInt(bmask.substr(8, 8), 2); const to = parseInt(bmask.substr(16, 8), 2); const lo = parseInt(bmask.substr(24, 8), 2); this.mask = new IP(fo, se, to, lo); this.firstIP = this.gateway.clone(); this.firstIP.add(1); this.lastIP = this.firstIP.clone(); this.lastIP.add(this.subnetCapacity); this.lastIP.sub(3); this.broadcastIP = this.lastIP.clone(); this.broadcastIP.add(1); } }