cidr_calculate
Calculate network details from CIDR notation. Get network address, broadcast, host range, and host count for IPv4 networks.
Instructions
Calculate network details from a CIDR notation (e.g., '192.168.1.0/24'). Returns network address, broadcast, host range, and host count.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cidr | Yes | IPv4 CIDR notation (e.g., '192.168.1.0/24', '10.0.0.0/8') |
Implementation Reference
- src/tools/network.ts:6-110 (handler)The handler and registration for 'cidr_calculate' tool in src/tools/network.ts.
server.tool( "cidr_calculate", "Calculate network details from a CIDR notation (e.g., '192.168.1.0/24'). Returns network address, broadcast, host range, and host count.", { cidr: z .string() .describe("IPv4 CIDR notation (e.g., '192.168.1.0/24', '10.0.0.0/8')"), }, async ({ cidr }) => { try { const parts = cidr.split("/"); if (parts.length !== 2) { return { content: [ { type: "text" as const, text: "Error: Invalid CIDR notation. Expected format: 'x.x.x.x/y'", }, ], isError: true, }; } const ip = parts[0]; const prefix = parseInt(parts[1], 10); if (prefix < 0 || prefix > 32 || isNaN(prefix)) { return { content: [ { type: "text" as const, text: "Error: Prefix length must be between 0 and 32", }, ], isError: true, }; } const ipParts = ip.split(".").map(Number); if ( ipParts.length !== 4 || ipParts.some((p) => isNaN(p) || p < 0 || p > 255) ) { return { content: [ { type: "text" as const, text: "Error: Invalid IPv4 address", }, ], isError: true, }; } const ipNum = (ipParts[0] << 24) | (ipParts[1] << 16) | (ipParts[2] << 8) | ipParts[3]; const mask = prefix === 0 ? 0 : (~0 << (32 - prefix)) >>> 0; const network = (ipNum & mask) >>> 0; const broadcast = (network | ~mask) >>> 0; const numToIp = (num: number): string => [(num >>> 24) & 255, (num >>> 16) & 255, (num >>> 8) & 255, num & 255].join("."); const numHosts = prefix >= 31 ? (prefix === 32 ? 1 : 2) : Math.pow(2, 32 - prefix) - 2; const firstHost = prefix >= 31 ? network : (network + 1) >>> 0; const lastHost = prefix >= 31 ? broadcast : (broadcast - 1) >>> 0; const result = { cidr, network_address: numToIp(network), broadcast_address: numToIp(broadcast), subnet_mask: numToIp(mask), wildcard_mask: numToIp((~mask) >>> 0), first_host: numToIp(firstHost), last_host: numToIp(lastHost), total_hosts: numHosts, prefix_length: prefix, ip_class: ipParts[0] < 128 ? "A" : ipParts[0] < 192 ? "B" : ipParts[0] < 224 ? "C" : ipParts[0] < 240 ? "D" : "E", is_private: (ipParts[0] === 10) || (ipParts[0] === 172 && ipParts[1] >= 16 && ipParts[1] <= 31) || (ipParts[0] === 192 && ipParts[1] === 168), }; return { content: [ { type: "text" as const, text: JSON.stringify(result, null, 2) }, ], }; } catch (e) { return { content: [ { type: "text" as const, text: `Error: ${e instanceof Error ? e.message : String(e)}`, }, ], isError: true, }; } } );