From 42f505862b308796009ccbf5e2d0e9aa04d2b22d Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sun, 31 Aug 2025 08:29:33 +0100 Subject: [PATCH] VLSM Calculator --- src/lib/components/Icon.svelte | 14 + src/lib/components/VLSMCalculator.svelte | 825 ++++++++++++++++++ src/lib/constants/nav.ts | 25 +- src/lib/utils/vlsm-calculations.ts | 346 ++++++++ src/routes/subnet-calculator/+page.svelte | 21 +- src/routes/subnetting/+page.svelte | 167 ++++ .../subnetting/subnet-calculator/+page.svelte | 17 + .../subnetting/vlsm-calculator/+page.svelte | 16 + 8 files changed, 1412 insertions(+), 19 deletions(-) create mode 100644 src/lib/components/VLSMCalculator.svelte create mode 100644 src/lib/utils/vlsm-calculations.ts create mode 100644 src/routes/subnetting/+page.svelte create mode 100644 src/routes/subnetting/subnet-calculator/+page.svelte create mode 100644 src/routes/subnetting/vlsm-calculator/+page.svelte diff --git a/src/lib/components/Icon.svelte b/src/lib/components/Icon.svelte index bd0122f..3344ade 100644 --- a/src/lib/components/Icon.svelte +++ b/src/lib/components/Icon.svelte @@ -56,6 +56,8 @@ {:else if name === 'subnet-calculator'} + {:else if name === 'vlsm-calculator'} + {:else if name === 'cidr-convertor'} {:else if name === 'network-classes'} @@ -64,6 +66,18 @@ {:else if name === 'common-subnets'} + {:else if name === 'plus'} + + {:else if name === 'trash'} + + {:else if name === 'chevron-up'} + + {:else if name === 'chevron-down'} + + {:else if name === 'copy'} + + {:else if name === 'tick'} + {:else if name === 'grid'} diff --git a/src/lib/components/VLSMCalculator.svelte b/src/lib/components/VLSMCalculator.svelte new file mode 100644 index 0000000..a97b091 --- /dev/null +++ b/src/lib/components/VLSMCalculator.svelte @@ -0,0 +1,825 @@ + + +
+
+

VLSM Calculator

+

Design efficient subnets with Variable Length Subnet Masking for optimal address space utilization.

+
+ + +
+

Network Configuration

+
+
+ +
+
+ +
+ /{cidr} + + +
+
+
+
+ + +
+
+

Subnet Requirements

+ +
+ +
+ {#each subnets as subnet, index (subnet.id)} +
+
+ {index + 1} +
+ updateSubnet(subnet.id, 'name', e.target?.value)} + class="subnet-name-input" + /> +
+ + updateSubnet(subnet.id, 'hostsNeeded', parseInt(e.target?.value || '1'))} + class="hosts-number-input" + /> +
+
+ +
+ +
+ updateSubnet(subnet.id, 'description', e.target?.value)} + class="description-input" + /> +
+
+ {/each} +
+
+ + + {#if vlsmResult} +
+ {#if vlsmResult.success} + +
+

VLSM Calculation Results

+
+
+ Total Subnets + {vlsmResult.subnets.length} +
+
+ Hosts Requested + {vlsmResult.totalHostsRequested.toLocaleString()} +
+
+ Hosts Provided + {vlsmResult.totalHostsProvided.toLocaleString()} +
+
+ Wasted Hosts + {vlsmResult.totalWastedHosts.toLocaleString()} +
+
+ Efficiency + + {((1 - vlsmResult.totalWastedHosts / vlsmResult.totalHostsProvided) * 100).toFixed(1)}% + +
+
+ Remaining Addresses + {vlsmResult.remainingAddresses.toLocaleString()} +
+
+
+ + +
+

Calculated Subnets

+
+
+
Subnet
+
Network
+
Hosts
+
Mask
+
Efficiency
+
Actions
+
+ + {#each vlsmResult.subnets as subnet (subnet.id)} +
+
+
{subnet.name}
+ {#if subnet.description} +
{subnet.description}
+ {/if} +
+ +
+
+
{subnet.networkAddress}/{subnet.cidr}
+
+ {subnet.firstUsableHost} - {subnet.lastUsableHost} +
+
+
+ +
+
+
{subnet.hostsNeeded} needed
+
{subnet.hostsProvided} provided
+ {#if subnet.wastedHosts > 0} +
{subnet.wastedHosts} wasted
+ {/if} +
+
+ +
+
+
{subnet.subnetMask}
+
{subnet.wildcardMask}
+
+
+ +
+
+ {((1 - subnet.wastedHosts / subnet.hostsProvided) * 100).toFixed(1)}% +
+
+ +
+ + + + +
+
+ + {#if expandedSubnets.has(subnet.id)} +
+
+
+ Network Address + {subnet.networkAddress} +
+
+ Broadcast Address + {subnet.broadcastAddress} +
+
+ First Usable Host + {subnet.firstUsableHost} +
+
+ Last Usable Host + {subnet.lastUsableHost} +
+
+ Subnet Mask + {subnet.subnetMask} +
+
+ Wildcard Mask + {subnet.wildcardMask} +
+
+ Binary Mask + {subnet.binaryMask} +
+
+ Host Bits + {subnet.actualHostBits} bits +
+
+
+ {/if} + {/each} +
+
+ + + {#if vlsmResult.nextAvailableNetwork} +
+

Next Available Network

+

+ The next available network address for additional subnets: + {vlsmResult.nextAvailableNetwork} +

+
+ {/if} + + {:else} + +
+

Calculation Error

+

{vlsmResult.error}

+
+ {/if} +
+ {/if} +
+ + diff --git a/src/lib/constants/nav.ts b/src/lib/constants/nav.ts index d4ce975..ea32f56 100644 --- a/src/lib/constants/nav.ts +++ b/src/lib/constants/nav.ts @@ -11,17 +11,10 @@ export type NavGroup = { }; // Individual standalone pages -export const STANDALONE_PAGES: NavItem[] = [ - { - href: '/subnet-calculator', - label: 'Subnet Calculator', - description: 'Calculate subnet information, network addresses, broadcast addresses, and host ranges with visual network analysis', - icon: 'subnet-calculator' - } -]; +export const STANDALONE_PAGES: NavItem[] = []; export const TOP_NAV: NavItem[] = [ - { href: '/subnet-calculator', label: 'Subnet Calc' }, + { href: '/subnetting', label: 'Subnetting' }, { href: '/cidr-convertor', label: 'CIDR Convert' }, { href: '/ip-address-convertor', label: 'IP Convert' }, { href: '/ip-reference', label: 'Reference' } @@ -29,6 +22,20 @@ export const TOP_NAV: NavItem[] = [ // Sections that have sub-pages (drives the conditional SubHeader) export const SUB_NAV: Record = { + '/subnetting': [ + { + href: '/subnetting/subnet-calculator', + label: 'Subnet Calculator', + description: 'Calculate subnet information, network addresses, broadcast addresses, and host ranges with visual network analysis', + icon: 'subnet-calculator' + }, + { + href: '/subnetting/vlsm-calculator', + label: 'VLSM Calculator', + description: 'Variable Length Subnet Mask calculator to break networks into multiple smaller subnets with optimal allocation', + icon: 'vlsm-calculator' + } + ], '/cidr-convertor': [ { href: '/cidr-convertor/cidr-to-subnet-mask', diff --git a/src/lib/utils/vlsm-calculations.ts b/src/lib/utils/vlsm-calculations.ts new file mode 100644 index 0000000..9aa0012 --- /dev/null +++ b/src/lib/utils/vlsm-calculations.ts @@ -0,0 +1,346 @@ +/** + * VLSM (Variable Length Subnet Mask) Calculation Utilities + */ + +export interface SubnetRequirement { + id: string; + name: string; + hostsNeeded: number; + description?: string; +} + +export interface CalculatedSubnet { + id: string; + name: string; + description?: string; + hostsNeeded: number; + hostsProvided: number; + subnetMask: string; + cidr: number; + networkAddress: string; + broadcastAddress: string; + firstUsableHost: string; + lastUsableHost: string; + wildcardMask: string; + binaryMask: string; + actualHostBits: number; + wastedHosts: number; +} + +export interface VLSMResult { + success: boolean; + subnets: CalculatedSubnet[]; + error?: string; + originalNetwork: string; + originalCIDR: number; + totalHostsRequested: number; + totalHostsProvided: number; + totalWastedHosts: number; + remainingAddresses: number; + nextAvailableNetwork?: string; +} + +/** + * Convert IP address to 32-bit integer + */ +function ipToInt(ip: string): number { + const parts = ip.split('.').map(Number); + return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]; +} + +/** + * Convert 32-bit integer to IP address + */ +function intToIp(int: number): string { + return [ + (int >>> 24) & 0xFF, + (int >>> 16) & 0xFF, + (int >>> 8) & 0xFF, + int & 0xFF + ].join('.'); +} + +/** + * Calculate subnet mask from CIDR + */ +function cidrToSubnetMask(cidr: number): string { + const mask = 0xFFFFFFFF << (32 - cidr); + return intToIp(mask >>> 0); +} + +/** + * Calculate wildcard mask from CIDR + */ +function cidrToWildcardMask(cidr: number): string { + const wildcardInt = (1 << (32 - cidr)) - 1; + return intToIp(wildcardInt); +} + +/** + * Convert subnet mask to binary representation + */ +function subnetMaskToBinary(mask: string): string { + return mask.split('.').map(octet => { + return parseInt(octet).toString(2).padStart(8, '0'); + }).join('.'); +} + +/** + * Calculate the minimum number of host bits needed for a given number of hosts + */ +function calculateRequiredHostBits(hostsNeeded: number): number { + // Need 2 extra addresses (network and broadcast) + const totalAddresses = hostsNeeded + 2; + return Math.ceil(Math.log2(totalAddresses)); +} + +/** + * Calculate the number of hosts that can be accommodated with given host bits + */ +function calculateHostsFromBits(hostBits: number): number { + return Math.pow(2, hostBits) - 2; // Subtract network and broadcast addresses +} + +/** + * Validate IP address format + */ +function isValidIP(ip: string): boolean { + const parts = ip.split('.'); + if (parts.length !== 4) return false; + + return parts.every(part => { + const num = parseInt(part); + return !isNaN(num) && num >= 0 && num <= 255; + }); +} + +/** + * Validate CIDR notation + */ +function isValidCIDR(cidr: number): boolean { + return cidr >= 1 && cidr <= 30; // /31 and /32 don't allow host addresses +} + +/** + * Sort subnet requirements by hosts needed (descending order for optimal allocation) + */ +function sortSubnetRequirements(requirements: SubnetRequirement[]): SubnetRequirement[] { + return [...requirements].sort((a, b) => b.hostsNeeded - a.hostsNeeded); +} + +/** + * Check if there's enough address space in the network + */ +function validateAddressSpace(networkIP: string, cidr: number, requirements: SubnetRequirement[]): boolean { + const totalAvailableHosts = calculateHostsFromBits(32 - cidr); + const totalRequiredHosts = requirements.reduce((sum, req) => { + const hostBits = calculateRequiredHostBits(req.hostsNeeded); + return sum + Math.pow(2, hostBits); + }, 0); + + return totalRequiredHosts <= totalAvailableHosts; +} + +/** + * Main VLSM calculation function + */ +export function calculateVLSM( + networkIP: string, + cidr: number, + requirements: SubnetRequirement[] +): VLSMResult { + try { + // Input validation + if (!isValidIP(networkIP)) { + return { + success: false, + subnets: [], + error: 'Invalid IP address format', + originalNetwork: networkIP, + originalCIDR: cidr, + totalHostsRequested: 0, + totalHostsProvided: 0, + totalWastedHosts: 0, + remainingAddresses: 0 + }; + } + + if (!isValidCIDR(cidr)) { + return { + success: false, + subnets: [], + error: 'CIDR must be between /1 and /30', + originalNetwork: networkIP, + originalCIDR: cidr, + totalHostsRequested: 0, + totalHostsProvided: 0, + totalWastedHosts: 0, + remainingAddresses: 0 + }; + } + + if (requirements.length === 0) { + return { + success: false, + subnets: [], + error: 'No subnet requirements specified', + originalNetwork: networkIP, + originalCIDR: cidr, + totalHostsRequested: 0, + totalHostsProvided: 0, + totalWastedHosts: 0, + remainingAddresses: 0 + }; + } + + // Validate unique subnet names + const names = requirements.map(req => req.name); + const uniqueNames = new Set(names); + if (names.length !== uniqueNames.size) { + return { + success: false, + subnets: [], + error: 'Subnet names must be unique', + originalNetwork: networkIP, + originalCIDR: cidr, + totalHostsRequested: 0, + totalHostsProvided: 0, + totalWastedHosts: 0, + remainingAddresses: 0 + }; + } + + // Check if there's enough address space + if (!validateAddressSpace(networkIP, cidr, requirements)) { + return { + success: false, + subnets: [], + error: 'Not enough address space in the network for all requirements', + originalNetwork: networkIP, + originalCIDR: cidr, + totalHostsRequested: requirements.reduce((sum, req) => sum + req.hostsNeeded, 0), + totalHostsProvided: 0, + totalWastedHosts: 0, + remainingAddresses: calculateHostsFromBits(32 - cidr) + }; + } + + // Sort requirements by hosts needed (largest first) + const sortedRequirements = sortSubnetRequirements(requirements); + + // Calculate subnets + const calculatedSubnets: CalculatedSubnet[] = []; + let currentNetworkInt = ipToInt(networkIP); + const originalNetworkMask = 0xFFFFFFFF << (32 - cidr); + + // Ensure we start with the network address + currentNetworkInt = currentNetworkInt & (originalNetworkMask >>> 0); + + let totalHostsRequested = 0; + let totalHostsProvided = 0; + let totalWastedHosts = 0; + + for (const requirement of sortedRequirements) { + const hostBits = calculateRequiredHostBits(requirement.hostsNeeded); + const subnetCIDR = 32 - hostBits; + const hostsProvided = calculateHostsFromBits(hostBits); + const subnetSize = Math.pow(2, hostBits); + + // Calculate addresses + const networkAddress = intToIp(currentNetworkInt); + const broadcastInt = currentNetworkInt + subnetSize - 1; + const broadcastAddress = intToIp(broadcastInt); + const firstUsableHost = intToIp(currentNetworkInt + 1); + const lastUsableHost = intToIp(broadcastInt - 1); + + // Calculate masks + const subnetMask = cidrToSubnetMask(subnetCIDR); + const wildcardMask = cidrToWildcardMask(subnetCIDR); + const binaryMask = subnetMaskToBinary(subnetMask); + + const wastedHosts = hostsProvided - requirement.hostsNeeded; + + calculatedSubnets.push({ + id: requirement.id, + name: requirement.name, + description: requirement.description, + hostsNeeded: requirement.hostsNeeded, + hostsProvided, + subnetMask, + cidr: subnetCIDR, + networkAddress, + broadcastAddress, + firstUsableHost, + lastUsableHost, + wildcardMask, + binaryMask, + actualHostBits: hostBits, + wastedHosts + }); + + totalHostsRequested += requirement.hostsNeeded; + totalHostsProvided += hostsProvided; + totalWastedHosts += wastedHosts; + + // Move to next available network address + currentNetworkInt += subnetSize; + } + + // Calculate remaining address space + const totalNetworkSize = Math.pow(2, 32 - cidr); + const usedAddresses = currentNetworkInt - ipToInt(networkIP); + const remainingAddresses = totalNetworkSize - usedAddresses; + const nextAvailableNetwork = remainingAddresses > 0 ? intToIp(currentNetworkInt) : undefined; + + return { + success: true, + subnets: calculatedSubnets, + originalNetwork: networkIP, + originalCIDR: cidr, + totalHostsRequested, + totalHostsProvided, + totalWastedHosts, + remainingAddresses, + nextAvailableNetwork + }; + + } catch (error) { + return { + success: false, + subnets: [], + error: error instanceof Error ? error.message : 'Unknown error occurred', + originalNetwork: networkIP, + originalCIDR: cidr, + totalHostsRequested: 0, + totalHostsProvided: 0, + totalWastedHosts: 0, + remainingAddresses: 0 + }; + } +} + +/** + * Generate a unique ID for subnet requirements + */ +export function generateSubnetId(): string { + return 'subnet_' + Math.random().toString(36).substr(2, 9); +} + +/** + * Validate subnet requirement + */ +export function validateSubnetRequirement(requirement: SubnetRequirement): { valid: boolean; error?: string } { + if (!requirement.name.trim()) { + return { valid: false, error: 'Subnet name is required' }; + } + + if (requirement.hostsNeeded < 1) { + return { valid: false, error: 'Number of hosts must be at least 1' }; + } + + if (requirement.hostsNeeded > 16777214) { // Max hosts in a /8 network + return { valid: false, error: 'Number of hosts too large (max: 16,777,214)' }; + } + + return { valid: true }; +} \ No newline at end of file diff --git a/src/routes/subnet-calculator/+page.svelte b/src/routes/subnet-calculator/+page.svelte index a45e76c..e256e5a 100644 --- a/src/routes/subnet-calculator/+page.svelte +++ b/src/routes/subnet-calculator/+page.svelte @@ -1,17 +1,18 @@ - {pages.subnetCalculator.title} - - - - - + Redirecting to Subnet Calculator... + - +
+

Redirecting to the new subnet calculator location...

+

If you are not redirected automatically, click here.

+
diff --git a/src/routes/subnetting/+page.svelte b/src/routes/subnetting/+page.svelte new file mode 100644 index 0000000..9c5ab26 --- /dev/null +++ b/src/routes/subnetting/+page.svelte @@ -0,0 +1,167 @@ + + + + Subnetting Tools - Subnet & VLSM Calculators | {site.name} + + + + + + + +
+ + +
+

About Subnetting

+

+ Subnetting is the practice of dividing a network into smaller, more manageable sub-networks (subnets). + This technique helps improve network performance, security, and organization by creating logical boundaries within your IP address space. +

+ +

Key Benefits

+
    +
  • Improved Security: Isolate different network segments
  • +
  • Reduced Broadcast Traffic: Smaller broadcast domains
  • +
  • Better Organization: Logical grouping of devices
  • +
  • Efficient Address Usage: Optimal IP allocation with VLSM
  • +
+ +

Traditional vs VLSM Subnetting

+
+
+

Traditional Subnetting

+

All subnets use the same subnet mask, creating equal-sized networks. Simple but may waste IP addresses.

+
+
+

VLSM (Variable Length Subnet Mask)

+

Each subnet can have a different mask size, allowing optimal allocation based on actual host requirements.

+
+
+
+ +
+ {#if Array.isArray(subnettingTools)} + {#each subnettingTools as tool} + +
+ {#if tool.icon} + + {/if} +
+
+

{tool.label}

+

{tool.description}

+
+
+ {/each} + {/if} +
+
+ + \ No newline at end of file diff --git a/src/routes/subnetting/subnet-calculator/+page.svelte b/src/routes/subnetting/subnet-calculator/+page.svelte new file mode 100644 index 0000000..1fad34a --- /dev/null +++ b/src/routes/subnetting/subnet-calculator/+page.svelte @@ -0,0 +1,17 @@ + + + + {pages.subnetCalculator.title} + + + + + + + + diff --git a/src/routes/subnetting/vlsm-calculator/+page.svelte b/src/routes/subnetting/vlsm-calculator/+page.svelte new file mode 100644 index 0000000..6360660 --- /dev/null +++ b/src/routes/subnetting/vlsm-calculator/+page.svelte @@ -0,0 +1,16 @@ + + + + VLSM Calculator - Variable Length Subnet Mask Tool | {site.name} + + + + + + + + \ No newline at end of file