From d425fe390de0c3f5dc39b015a4e459695f56a101 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sun, 31 Aug 2025 13:06:07 +0100 Subject: [PATCH] CIDR summerizer --- src/lib/components/CIDRSummarizer.svelte | 21 +++++--- src/lib/constants/nav.ts | 2 +- src/lib/utils/cidr-summarization-fixed.ts | 59 +++++++++++++++++++++-- src/routes/cidr/summarize/+page.svelte | 5 +- 4 files changed, 74 insertions(+), 13 deletions(-) diff --git a/src/lib/components/CIDRSummarizer.svelte b/src/lib/components/CIDRSummarizer.svelte index e174fe0..95c6d70 100644 --- a/src/lib/components/CIDRSummarizer.svelte +++ b/src/lib/components/CIDRSummarizer.svelte @@ -45,6 +45,14 @@ 10.0.0.2 10.0.0.3 10.0.0.4` + }, + { + label: 'Mode Comparison', + content: `192.168.1.1 +192.168.1.3 +192.168.1.5 +192.168.1.7 +192.168.1.9-192.168.1.12` }, { label: 'Complex Mix', @@ -166,13 +174,13 @@ fe80::/10` class="input-textarea" rows="8" > + @@ -308,7 +316,7 @@ fe80::/10` Summarized IPv6 Blocks {result.stats.summarizedIpv6Count} -
+
Total Addresses Covered {result.stats.totalAddressesCovered}
@@ -513,10 +521,7 @@ fe80::/10` flex-direction: column; align-items: center; text-align: center; - - &.span-full { - grid-column: 1 / -1; - } + justify-content: space-between; } .stat-label { @@ -600,4 +605,4 @@ fe80::/10` align-items: stretch; } } - \ No newline at end of file + diff --git a/src/lib/constants/nav.ts b/src/lib/constants/nav.ts index 6aa3fd6..1bf49ca 100644 --- a/src/lib/constants/nav.ts +++ b/src/lib/constants/nav.ts @@ -21,7 +21,7 @@ export const TOP_NAV: NavItem[] = [ ]; // Sections that have sub-pages (drives the conditional SubHeader) -export const SUB_NAV: Record = { +export const SUB_NAV: Record = { '/subnetting': [ { href: '/subnetting/ipv4-subnet-calculator', diff --git a/src/lib/utils/cidr-summarization-fixed.ts b/src/lib/utils/cidr-summarization-fixed.ts index 2ecb42d..eb09a04 100644 --- a/src/lib/utils/cidr-summarization-fixed.ts +++ b/src/lib/utils/cidr-summarization-fixed.ts @@ -227,6 +227,54 @@ function rangeToCIDRs(range: IPRange): string[] { return cidrs; } +/* Convert ranges to CIDR blocks with minimal cover mode */ +function rangeToCIDRsMinimal(range: IPRange): string[] { + const cidrs: string[] = []; + let start = range.start; + const end = range.end; + const maxPrefix = range.version === 4 ? 32 : 128; + + // Safety counter to prevent infinite loops + let iterations = 0; + const maxIterations = 100; // Lower limit for minimal cover + + while (start <= end && iterations < maxIterations) { + iterations++; + + // For minimal cover, try to find larger blocks first + let prefixLength = 0; // Start with largest possible block + let blockSize = 1n << BigInt(maxPrefix); + + // Find the largest block that starts at or before start and covers as much as possible + for (let p = 0; p <= maxPrefix; p++) { + const testBlockSize = 1n << BigInt(maxPrefix - p); + const alignedStart = start & ~(testBlockSize - 1n); + + // Accept block if it's aligned and doesn't go too far past our end + if (alignedStart <= start && start + testBlockSize - 1n <= end + testBlockSize / 4n) { + blockSize = testBlockSize; + prefixLength = p; + start = alignedStart; // Align to block boundary + } else { + break; + } + } + + const ip = range.version === 4 ? bigIntToIPv4(start) : bigIntToIPv6(start); + cidrs.push(`${ip}/${prefixLength}`); + + start += blockSize; + } + + if (iterations >= maxIterations) { + // Fallback for complex ranges + const ip = range.version === 4 ? bigIntToIPv4(range.start) : bigIntToIPv6(range.start); + return [`${ip}/${maxPrefix}`]; + } + + return cidrs; +} + /* Main summarization function */ export function summarizeCIDRs( inputText: string, @@ -273,12 +321,17 @@ export function summarizeCIDRs( } }); - // Merge and convert to CIDRs + // Merge ranges first const mergedIpv4 = mergeRanges(ipv4Ranges); const mergedIpv6 = mergeRanges(ipv6Ranges); - const ipv4CIDRs = mergedIpv4.flatMap(rangeToCIDRs); - const ipv6CIDRs = mergedIpv6.flatMap(rangeToCIDRs); + // Apply different algorithms based on mode + const ipv4CIDRs = mode === 'minimal-cover' + ? mergedIpv4.flatMap(rangeToCIDRsMinimal) + : mergedIpv4.flatMap(rangeToCIDRs); + const ipv6CIDRs = mode === 'minimal-cover' + ? mergedIpv6.flatMap(rangeToCIDRsMinimal) + : mergedIpv6.flatMap(rangeToCIDRs); // Calculate statistics const totalV4 = mergedIpv4.reduce((sum, r) => { diff --git a/src/routes/cidr/summarize/+page.svelte b/src/routes/cidr/summarize/+page.svelte index 91116ef..bd9662a 100644 --- a/src/routes/cidr/summarize/+page.svelte +++ b/src/routes/cidr/summarize/+page.svelte @@ -202,7 +202,10 @@ } .mode-item { + border: 1px solid var(--border-primary); border-left: 4px solid var(--color-info); + padding: var(--spacing-md); + background: var(--bg-secondary); } .mode-example { @@ -302,4 +305,4 @@ } } } - \ No newline at end of file +