CIDR summerizer

This commit is contained in:
Alicia Sykes committed 2025-08-31 13:06:07 +01:00
1 parent 6629025e09
commit d425fe390d
4 files changed
+74 -13

No files matched your search

+13 -8
View File
@@ -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"
></textarea>
<button
type="button"
class="btn btn-secondary btn-sm clear-btn"
onclick={clearInput}
>
<Icon name="trash" size="sm" />
Clear
<Tooltip text="Clear input"><Icon name="trash" size="sm" /></Tooltip>
</button>
</div>
</div>
@@ -308,7 +316,7 @@ fe80::/10`
<span class="stat-label">Summarized IPv6 Blocks</span>
<span class="stat-value">{result.stats.summarizedIpv6Count}</span>
</div>
<div class="stat-card span-full">
<div class="stat-card">
<span class="stat-label">Total Addresses Covered</span>
<span class="stat-value large">{result.stats.totalAddressesCovered}</span>
</div>
@@ -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;
}
}
</style>
</style>
+1 -1
View File
@@ -21,7 +21,7 @@ export const TOP_NAV: NavItem[] = [
];
// Sections that have sub-pages (drives the conditional SubHeader)
export const SUB_NAV: Record<string, NavItem[] | NavGroup[]> = {
export const SUB_NAV: Record<string, (NavItem | NavGroup)[]> = {
'/subnetting': [
{
href: '/subnetting/ipv4-subnet-calculator',
+56 -3
View File
@@ -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) => {
+4 -1
View File
@@ -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 @@
}
}
}
</style>
</style>