"use client" import React, { useState } from 'react'; import { Netmask } from 'netmask'; export default function Cidr() { const [ip, setIp] = useState([10, 88, 135, 144]); const [cidr, setCidr] = useState(28); const [cols] = useState(["bg-purple-400", "bg-red-400", "bg-green-400", "bg-yellow-400"]); const bits = ip.map(octet => Array.from({ length: 8 }, (_, i) => (octet >> (7 - i)) & 1)); const parseOctet = (val: string, max: number) => { const num = parseInt(val); if (isNaN(num) || num < 0) return 0; if (num > max) return max; return num; } const setIpOctet = (i: number, val: number) => { const newIp = [...ip]; newIp[i] = val; setIp(newIp); } const pretty = ip.join('.') + '/' + cidr; const netmask = new Netmask(pretty); const details = { "Netmask": netmask.mask, "CIDR Base IP": netmask.base, "Broadcast IP": netmask.broadcast, "Count": netmask.size.toLocaleString(), "First Usable IP": netmask.first, "Last Usable IP": netmask.last } return (

IP / CIDR Calculator

CIDR is a notation for describing blocks of IP addresses and is used heavily in various networking configurations.

IP addresses contain 4 octets, each consisting of 8 bits giving values between 0 and 255. The decimal value that comes after the slash is the number of bits consisting of the routing prefix.

This in turn can be translated into a netmask, and also designates how many available addresses are in the block.

{ip.map((octet, i) => (
setIpOctet(i, parseOctet(e.target.value, 255))} className={`w-20 h-20 text-3xl text-center rounded-md ${cols[i]}`} maxLength={3} /> {i == 3 ? "/" : "."}
))} setCidr(parseOctet(e.target.value, 32))} className="w-20 h-20 text-3xl text-center rounded-md bg-slate-300" maxLength={2} />
{bits.map((octet, i) => {octet.map((bit, j) => {bit})} )}
{Object.entries(details).map(([label, val]) =>
{val}
{label}
) }
); }