From 7bf6bfc1981d35fa9905ee0812a570edefce5ab4 Mon Sep 17 00:00:00 2001
From: Ramkumar Ethiraj <64294490+rmkr-dev@users.noreply.github.com>
Date: Mon, 7 Sep 2026 12:47:49 +0000
Subject: [PATCH] Add IPv4 CIDR calculator to the toolkit
---
README.md | 1 +
assets/site.js | 1 +
cidr/app.js | 130 ++++++++++++++++++++++++++++++++++++++
cidr/index.html | 32 ++++++++++
scripts/cidr-lib.mjs | 100 +++++++++++++++++++++++++++++
scripts/cidr-lib.test.mjs | 61 ++++++++++++++++++
scripts/site-check.mjs | 7 ++
7 files changed, 332 insertions(+)
create mode 100644 cidr/app.js
create mode 100644 cidr/index.html
create mode 100644 scripts/cidr-lib.mjs
create mode 100644 scripts/cidr-lib.test.mjs
diff --git a/README.md b/README.md
index 2e3a63a..72f1478 100644
--- a/README.md
+++ b/README.md
@@ -33,6 +33,7 @@ Skillbook and Promptbook load JSON over `fetch`, so use a local server (opening
- **Hash Lab** — SHA-256, SHA-1, MD5
- **UUID Generator**
- **PEM / SSH Viewer**
+- **CIDR Calculator** — IPv4 network, broadcast, mask, host range
### Time
diff --git a/assets/site.js b/assets/site.js
index f4a2b7b..4d88519 100644
--- a/assets/site.js
+++ b/assets/site.js
@@ -15,6 +15,7 @@
{ id: "hash", path: "hash/", name: "Hash Lab", blurb: "SHA-256, SHA-1, and MD5 hashes in the browser.", cat: "Security" },
{ id: "uuid", path: "uuid/", name: "UUID Generator", blurb: "Create v4 UUIDs in bulk and copy them.", cat: "Security" },
{ id: "cert", path: "cert/", name: "PEM / SSH Viewer", blurb: "Inspect PEM blocks and SSH key fingerprints.", cat: "Security" },
+ { id: "cidr", path: "cidr/", name: "CIDR Calculator", blurb: "Network, broadcast, mask, and host range from an IPv4 CIDR.", cat: "Security" },
{ id: "time", path: "time/", name: "Time Overlay", blurb: "Compare working hours across time zones.", cat: "Time" },
{ id: "timestamp", path: "timestamp/", name: "Unix Timestamp", blurb: "Convert Unix time to human dates and back.", cat: "Time" },
{ id: "cron", path: "cron/", name: "Cron Explainer", blurb: "Read cron expressions and preview next runs.", cat: "Time" },
diff --git a/cidr/app.js b/cidr/app.js
new file mode 100644
index 0000000..c3777d4
--- /dev/null
+++ b/cidr/app.js
@@ -0,0 +1,130 @@
+(function () {
+ function ipv4ToInt(ip) {
+ var parts = String(ip).trim().split(".");
+ if (parts.length !== 4) throw new Error("IPv4 address must have 4 octets");
+ var n = 0;
+ for (var i = 0; i < parts.length; i++) {
+ var p = parts[i];
+ if (!/^\d+$/.test(p)) throw new Error("Invalid IPv4 octet");
+ var v = Number(p);
+ if (v < 0 || v > 255) throw new Error("IPv4 octet out of range");
+ n = (n << 8) + v;
+ }
+ return n >>> 0;
+ }
+ function intToIpv4(n) {
+ var x = n >>> 0;
+ return [(x >>> 24) & 255, (x >>> 16) & 255, (x >>> 8) & 255, x & 255].join(".");
+ }
+ function prefixToMask(prefix) {
+ var p = Number(prefix);
+ if (!Number.isInteger(p) || p < 0 || p > 32) throw new Error("Prefix must be an integer from 0 to 32");
+ if (p === 0) return 0;
+ return (0xffffffff << (32 - p)) >>> 0;
+ }
+ function parseCidr(input) {
+ var raw = String(input || "").trim();
+ if (!raw) throw new Error("Enter an IPv4 address or CIDR");
+ if (raw.indexOf(":") >= 0) throw new Error("IPv6 is not supported yet");
+ var ipPart = raw;
+ var prefix = 32;
+ if (raw.indexOf("/") >= 0) {
+ var bits = raw.split("/");
+ if (bits.length !== 2) throw new Error("Use address/prefix, e.g. 10.0.0.0/24");
+ ipPart = bits[0].trim();
+ prefix = Number(bits[1].trim());
+ if (!Number.isInteger(prefix) || prefix < 0 || prefix > 32) throw new Error("Prefix must be an integer from 0 to 32");
+ }
+ var ip = ipv4ToInt(ipPart);
+ var mask = prefixToMask(prefix);
+ var network = (ip & mask) >>> 0;
+ var broadcast = (network | (~mask >>> 0)) >>> 0;
+ var hostBits = 32 - prefix;
+ var size = hostBits === 32 ? Math.pow(2, 32) : Math.pow(2, hostBits);
+ var firstHost, lastHost, hostCount;
+ if (prefix >= 31) {
+ firstHost = network;
+ lastHost = broadcast;
+ hostCount = prefix === 31 ? 2 : 1;
+ } else {
+ firstHost = network + 1;
+ lastHost = broadcast - 1;
+ hostCount = size - 2;
+ }
+ return {
+ input: raw,
+ ip: intToIpv4(ip),
+ prefix: prefix,
+ netmask: intToIpv4(mask),
+ wildcard: intToIpv4((~mask) >>> 0),
+ network: intToIpv4(network),
+ broadcast: intToIpv4(broadcast),
+ firstHost: intToIpv4(firstHost),
+ lastHost: intToIpv4(lastHost),
+ hostCount: hostCount,
+ addressCount: size,
+ isNetworkAligned: ip === network
+ };
+ }
+ function formatCidrSummary(info) {
+ var lines = [
+ "CIDR: " + info.network + "/" + info.prefix,
+ "Address: " + info.ip,
+ "Netmask: " + info.netmask,
+ "Wildcard: " + info.wildcard,
+ "Network: " + info.network,
+ "Broadcast: " + info.broadcast,
+ "Host range: " + info.firstHost + " - " + info.lastHost,
+ "Usable hosts: " + info.hostCount,
+ "Addresses in block: " + info.addressCount
+ ];
+ if (!info.isNetworkAligned) lines.push("Note: input IP is a host inside " + info.network + "/" + info.prefix);
+ return lines.join("\n");
+ }
+ function card(title, value) {
+ return '
';
+ }
+ function ready() {
+ var input = document.getElementById("cidrInput");
+ var output = document.getElementById("output");
+ var status = document.getElementById("status");
+ var cards = document.getElementById("cards");
+ function render(info) {
+ output.value = formatCidrSummary(info);
+ cards.innerHTML = [
+ card("Network", info.network + "/" + info.prefix),
+ card("Netmask", info.netmask),
+ card("Wildcard", info.wildcard),
+ card("Broadcast", info.broadcast),
+ card("Host range", info.firstHost + " - " + info.lastHost),
+ card("Usable hosts", String(info.hostCount))
+ ].join("");
+ RMKR.setStatus(status, info.isNetworkAligned ? "Aligned network address." : "Input is a host inside this block.", "ok");
+ }
+ function calculate() {
+ try { render(parseCidr(input.value)); }
+ catch (e) {
+ cards.innerHTML = "";
+ output.value = "";
+ RMKR.setStatus(status, e.message || String(e), "err");
+ }
+ }
+ document.getElementById("run").onclick = calculate;
+ document.getElementById("clear").onclick = function () {
+ input.value = "";
+ output.value = "";
+ cards.innerHTML = "";
+ RMKR.setStatus(status, "", "");
+ input.focus();
+ };
+ document.getElementById("copyOut").onclick = function () {
+ var v = output.value;
+ if (!v) return RMKR.setStatus(status, "Calculate first.", "err");
+ RMKR.copy(v).then(function () { RMKR.setStatus(status, "Copied.", "ok"); });
+ };
+ input.addEventListener("keydown", function (e) { if (e.key === "Enter") calculate(); });
+ calculate();
+ }
+ if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", ready);
+ else ready();
+})();
diff --git a/cidr/index.html b/cidr/index.html
new file mode 100644
index 0000000..1c7e503
--- /dev/null
+++ b/cidr/index.html
@@ -0,0 +1,32 @@
+
+
+
+
+
+ CIDR Calculator | RMKR Toolkit
+
+
+
+
+
+
+
+
+
+ CIDR Calculator
+ Paste an IPv4 CIDR (or bare address). Network math stays in this tab.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/scripts/cidr-lib.mjs b/scripts/cidr-lib.mjs
new file mode 100644
index 0000000..8688ba4
--- /dev/null
+++ b/scripts/cidr-lib.mjs
@@ -0,0 +1,100 @@
+/**
+ * IPv4 CIDR helpers for the toolkit CIDR calculator.
+ * Pure functions — safe to use from Node tests and the browser.
+ */
+
+export function ipv4ToInt(ip) {
+ const parts = String(ip).trim().split(".");
+ if (parts.length !== 4) throw new Error("IPv4 address must have 4 octets");
+ let n = 0;
+ for (const p of parts) {
+ if (!/^\d+$/.test(p)) throw new Error("Invalid IPv4 octet");
+ const v = Number(p);
+ if (v < 0 || v > 255) throw new Error("IPv4 octet out of range");
+ n = (n << 8) + v;
+ }
+ return n >>> 0;
+}
+
+export function intToIpv4(n) {
+ const x = n >>> 0;
+ return [
+ (x >>> 24) & 255,
+ (x >>> 16) & 255,
+ (x >>> 8) & 255,
+ x & 255,
+ ].join(".");
+}
+
+export function prefixToMask(prefix) {
+ const p = Number(prefix);
+ if (!Number.isInteger(p) || p < 0 || p > 32) {
+ throw new Error("Prefix must be an integer from 0 to 32");
+ }
+ if (p === 0) return 0;
+ return (0xffffffff << (32 - p)) >>> 0;
+}
+
+export function parseCidr(input) {
+ const raw = String(input || "").trim();
+ if (!raw) throw new Error("Enter an IPv4 address or CIDR");
+ if (raw.includes(":")) throw new Error("IPv6 is not supported yet");
+
+ let ipPart = raw;
+ let prefix = 32;
+ if (raw.includes("/")) {
+ const bits = raw.split("/");
+ if (bits.length !== 2) throw new Error("Use address/prefix, e.g. 10.0.0.0/24");
+ ipPart = bits[0].trim();
+ prefix = Number(bits[1].trim());
+ if (!Number.isInteger(prefix) || prefix < 0 || prefix > 32) {
+ throw new Error("Prefix must be an integer from 0 to 32");
+ }
+ }
+
+ const ip = ipv4ToInt(ipPart);
+ const mask = prefixToMask(prefix);
+ const network = (ip & mask) >>> 0;
+ const broadcast = (network | (~mask >>> 0)) >>> 0;
+ const hostBits = 32 - prefix;
+ const size = hostBits === 32 ? 2 ** 32 : 2 ** hostBits;
+ const usable =
+ prefix >= 31
+ ? prefix === 31
+ ? [network, broadcast]
+ : [network]
+ : [network + 1, broadcast - 1];
+
+ return {
+ input: raw,
+ ip: intToIpv4(ip),
+ prefix,
+ netmask: intToIpv4(mask),
+ wildcard: intToIpv4((~mask) >>> 0),
+ network: intToIpv4(network),
+ broadcast: intToIpv4(broadcast),
+ firstHost: intToIpv4(usable[0]),
+ lastHost: intToIpv4(usable[usable.length - 1]),
+ hostCount: prefix >= 31 ? usable.length : size - 2,
+ addressCount: size,
+ isNetworkAligned: ip === network,
+ };
+}
+
+export function formatCidrSummary(info) {
+ const lines = [
+ `CIDR: ${info.network}/${info.prefix}`,
+ `Address: ${info.ip}`,
+ `Netmask: ${info.netmask}`,
+ `Wildcard: ${info.wildcard}`,
+ `Network: ${info.network}`,
+ `Broadcast: ${info.broadcast}`,
+ `Host range: ${info.firstHost} – ${info.lastHost}`,
+ `Usable hosts: ${info.hostCount}`,
+ `Addresses in block: ${info.addressCount}`,
+ ];
+ if (!info.isNetworkAligned) {
+ lines.push(`Note: input IP is a host inside ${info.network}/${info.prefix}`);
+ }
+ return lines.join("\n");
+}
diff --git a/scripts/cidr-lib.test.mjs b/scripts/cidr-lib.test.mjs
new file mode 100644
index 0000000..ef06407
--- /dev/null
+++ b/scripts/cidr-lib.test.mjs
@@ -0,0 +1,61 @@
+import assert from "node:assert/strict";
+import {
+ ipv4ToInt,
+ intToIpv4,
+ parseCidr,
+ formatCidrSummary,
+ prefixToMask,
+} from "./cidr-lib.mjs";
+
+export function runCidrTests() {
+ assert.equal(intToIpv4(ipv4ToInt("10.0.0.1")), "10.0.0.1");
+ assert.equal(intToIpv4(prefixToMask(24)), "255.255.255.0");
+ assert.equal(intToIpv4(prefixToMask(0)), "0.0.0.0");
+ assert.equal(intToIpv4(prefixToMask(32)), "255.255.255.255");
+
+ const c24 = parseCidr("10.0.0.0/24");
+ assert.equal(c24.network, "10.0.0.0");
+ assert.equal(c24.broadcast, "10.0.0.255");
+ assert.equal(c24.netmask, "255.255.255.0");
+ assert.equal(c24.wildcard, "0.0.0.255");
+ assert.equal(c24.firstHost, "10.0.0.1");
+ assert.equal(c24.lastHost, "10.0.0.254");
+ assert.equal(c24.hostCount, 254);
+ assert.equal(c24.addressCount, 256);
+ assert.equal(c24.isNetworkAligned, true);
+
+ const host = parseCidr("192.168.1.10/28");
+ assert.equal(host.network, "192.168.1.0");
+ assert.equal(host.broadcast, "192.168.1.15");
+ assert.equal(host.firstHost, "192.168.1.1");
+ assert.equal(host.lastHost, "192.168.1.14");
+ assert.equal(host.hostCount, 14);
+ assert.equal(host.isNetworkAligned, false);
+
+ const single = parseCidr("8.8.8.8");
+ assert.equal(single.prefix, 32);
+ assert.equal(single.network, "8.8.8.8");
+ assert.equal(single.broadcast, "8.8.8.8");
+ assert.equal(single.hostCount, 1);
+
+ const p31 = parseCidr("10.0.0.0/31");
+ assert.equal(p31.hostCount, 2);
+ assert.equal(p31.firstHost, "10.0.0.0");
+ assert.equal(p31.lastHost, "10.0.0.1");
+
+ const summary = formatCidrSummary(c24);
+ assert.match(summary, /10\.0\.0\.0\/24/);
+ assert.match(summary, /Usable hosts: 254/);
+
+ assert.throws(() => parseCidr(""), /Enter an IPv4/);
+ assert.throws(() => parseCidr("2001:db8::/32"), /IPv6/);
+ assert.throws(() => parseCidr("10.0.0.0/33"), /Prefix/);
+ assert.throws(() => parseCidr("10.0.0.256/24"), /octet/);
+ assert.throws(() => ipv4ToInt("1.2.3"), /4 octets/);
+}
+
+const isDirect = process.argv[1] && process.argv[1].endsWith("cidr-lib.test.mjs");
+if (isDirect) {
+ runCidrTests();
+ console.log("cidr-lib.test: OK");
+}
diff --git a/scripts/site-check.mjs b/scripts/site-check.mjs
index 7236c68..1899d24 100644
--- a/scripts/site-check.mjs
+++ b/scripts/site-check.mjs
@@ -167,6 +167,13 @@ checkSiteJsSyntax();
checkToolPages();
checkHtmlPages();
+const { runCidrTests } = await import("./cidr-lib.test.mjs");
+try {
+ runCidrTests();
+} catch (e) {
+ fail(`cidr-lib tests: ${e.message}`);
+}
+
if (warnings.length) {
console.log("Warnings:");
for (const w of warnings) console.log(" - " + w);