mirror of
https://github.com/lissy93/networking-toolbox.git
synced 2026-11-22 14:01:44 +03:00
CIDR diff tool
This commit is contained in:
1 parent
fb179e591e
commit
56ce83d011
7 files changed
+1793
-4
No files matched your search
@@ -0,0 +1,846 @@
|
||||
<script lang="ts">
|
||||
import { computeCIDRDifference, type DiffResult, type AlignmentMode } from '$lib/utils/cidr-diff.js';
|
||||
import { tooltip } from '$lib/actions/tooltip.js';
|
||||
import Tooltip from './Tooltip.svelte';
|
||||
import Icon from './Icon.svelte';
|
||||
|
||||
let setA = $state(`192.168.1.0/24
|
||||
192.168.2.0/24`);
|
||||
let setB = $state(`192.168.1.100-192.168.1.200
|
||||
192.168.2.50/26`);
|
||||
let alignment = $state<AlignmentMode>('minimal');
|
||||
let constrainedPrefix = $state(24);
|
||||
let result = $state<DiffResult | null>(null);
|
||||
let copiedStates = $state<Record<string, boolean>>({});
|
||||
|
||||
const alignmentModes = [
|
||||
{
|
||||
value: 'minimal' as const,
|
||||
label: 'Minimal',
|
||||
description: 'Generate the most efficient CIDR blocks'
|
||||
},
|
||||
{
|
||||
value: 'constrained' as const,
|
||||
label: 'Constrained',
|
||||
description: 'Align to specific prefix boundaries'
|
||||
}
|
||||
];
|
||||
|
||||
const examples = [
|
||||
{
|
||||
label: 'Basic IPv4 Subtraction',
|
||||
setA: '192.168.1.0/24',
|
||||
setB: '192.168.1.128/25'
|
||||
},
|
||||
{
|
||||
label: 'Multiple Ranges',
|
||||
setA: `10.0.0.0/16
|
||||
172.16.0.0/16`,
|
||||
setB: `10.0.1.0/24
|
||||
172.16.50.0/24`
|
||||
},
|
||||
{
|
||||
label: 'IPv6 Example',
|
||||
setA: '2001:db8::/48',
|
||||
setB: '2001:db8:1::/64'
|
||||
},
|
||||
{
|
||||
label: 'Mixed Operations',
|
||||
setA: `192.168.0.0/16
|
||||
2001:db8::/32`,
|
||||
setB: `192.168.100.0/24
|
||||
2001:db8:abcd::/48`
|
||||
}
|
||||
];
|
||||
|
||||
/* Set example */
|
||||
function setExample(example: typeof examples[0]) {
|
||||
setA = example.setA;
|
||||
setB = example.setB;
|
||||
performDiff();
|
||||
}
|
||||
|
||||
/* Copy to clipboard */
|
||||
async function copyToClipboard(text: string, id: string) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
copiedStates[id] = true;
|
||||
setTimeout(() => copiedStates[id] = false, 3000);
|
||||
} catch (err) {
|
||||
console.error('Failed to copy:', err);
|
||||
}
|
||||
}
|
||||
|
||||
/* Copy all results */
|
||||
function copyAllResults(format: 'text' | 'json') {
|
||||
if (!result) return;
|
||||
|
||||
let content = '';
|
||||
if (format === 'json') {
|
||||
content = JSON.stringify({
|
||||
ipv4: result.ipv4,
|
||||
ipv6: result.ipv6,
|
||||
stats: result.stats
|
||||
}, null, 2);
|
||||
} else {
|
||||
const sections = [];
|
||||
if (result.ipv4.length > 0) {
|
||||
sections.push('IPv4:', ...result.ipv4);
|
||||
}
|
||||
if (result.ipv6.length > 0) {
|
||||
sections.push('IPv6:', ...result.ipv6);
|
||||
}
|
||||
content = sections.join('\n');
|
||||
}
|
||||
|
||||
copyToClipboard(content, `all-${format}`);
|
||||
}
|
||||
|
||||
/* Clear inputs */
|
||||
function clearInputs() {
|
||||
setA = '';
|
||||
setB = '';
|
||||
result = null;
|
||||
}
|
||||
|
||||
/* Perform difference computation */
|
||||
function performDiff() {
|
||||
if (!setA.trim()) {
|
||||
result = null;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
result = computeCIDRDifference(
|
||||
setA,
|
||||
setB,
|
||||
alignment,
|
||||
alignment === 'constrained' ? constrainedPrefix : undefined
|
||||
);
|
||||
} catch (error) {
|
||||
result = {
|
||||
ipv4: [],
|
||||
ipv6: [],
|
||||
stats: { inputA: { count: 0, addresses: '0' }, inputB: { count: 0, addresses: '0' }, output: { count: 0, addresses: '0' }, removed: { count: 0, addresses: '0' }, efficiency: 0 },
|
||||
visualization: { setA: [], setB: [], result: [], version: 4, totalRange: { start: 0n, end: 0n } },
|
||||
errors: [error instanceof Error ? error.message : 'Unknown error']
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/* Calculate visualization bar width percentage */
|
||||
function getBarWidth(range: { start: bigint; end: bigint }): number {
|
||||
if (!result?.visualization.totalRange) return 0;
|
||||
const totalSize = result.visualization.totalRange.end - result.visualization.totalRange.start + 1n;
|
||||
const rangeSize = range.end - range.start + 1n;
|
||||
return Number((rangeSize * 10000n) / totalSize) / 100;
|
||||
}
|
||||
|
||||
/* Calculate visualization bar offset percentage */
|
||||
function getBarOffset(range: { start: bigint; end: bigint }): number {
|
||||
if (!result?.visualization.totalRange) return 0;
|
||||
const totalSize = result.visualization.totalRange.end - result.visualization.totalRange.start + 1n;
|
||||
const offset = range.start - result.visualization.totalRange.start;
|
||||
return Number((offset * 10000n) / totalSize) / 100;
|
||||
}
|
||||
|
||||
/* Generate tooltip text for visualization segments */
|
||||
function getSegmentTooltip(range: { start: bigint; end: bigint; cidr?: string }, type: 'A' | 'B' | 'result'): string {
|
||||
const version = result?.visualization.version || 4;
|
||||
const startIP = version === 4 ?
|
||||
[Math.floor(Number(range.start) / 16777216) % 256, Math.floor(Number(range.start) / 65536) % 256, Math.floor(Number(range.start) / 256) % 256, Number(range.start) % 256].join('.') :
|
||||
'IPv6'; // Simplified for tooltip
|
||||
const endIP = version === 4 ?
|
||||
[Math.floor(Number(range.end) / 16777216) % 256, Math.floor(Number(range.end) / 65536) % 256, Math.floor(Number(range.end) / 256) % 256, Number(range.end) % 256].join('.') :
|
||||
'IPv6';
|
||||
const size = range.end - range.start + 1n;
|
||||
|
||||
return `Set ${type}\nRange: ${startIP} - ${endIP}\nSize: ${size.toLocaleString()}${range.cidr ? `\nCIDR: ${range.cidr}` : ''}`;
|
||||
}
|
||||
|
||||
// Reactive computation
|
||||
$effect(() => {
|
||||
if (setA.trim()) {
|
||||
performDiff();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<div class="card">
|
||||
<header class="card-header">
|
||||
<h2>CIDR Difference Calculator</h2>
|
||||
<p>Compute A - B where A and B are sets of IP addresses, CIDR blocks, or ranges. Shows minimal non-overlapping results.</p>
|
||||
</header>
|
||||
|
||||
<!-- Alignment Mode -->
|
||||
<div class="mode-section">
|
||||
<h3>Alignment Mode</h3>
|
||||
<div class="tabs">
|
||||
{#each alignmentModes as mode}
|
||||
<button
|
||||
type="button"
|
||||
class="tab"
|
||||
class:active={alignment === mode.value}
|
||||
onclick={() => alignment = mode.value}
|
||||
>
|
||||
{mode.label}
|
||||
<Tooltip text={mode.description} position="top">
|
||||
<Icon name="help" size="sm" />
|
||||
</Tooltip>
|
||||
</button>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
{#if alignment === 'constrained'}
|
||||
<div class="constraint-input">
|
||||
<label for="constrained-prefix">
|
||||
Constrained prefix length
|
||||
<Tooltip text="Force alignment to this prefix boundary">
|
||||
<Icon name="help" size="sm" />
|
||||
</Tooltip>
|
||||
</label>
|
||||
<input
|
||||
id="constrained-prefix"
|
||||
type="number"
|
||||
bind:value={constrainedPrefix}
|
||||
min="8"
|
||||
max="30"
|
||||
class="input-field constraint-field"
|
||||
/>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<!-- Input Section -->
|
||||
<div class="input-section">
|
||||
<div class="input-grid">
|
||||
<!-- Set A -->
|
||||
<div class="input-group">
|
||||
<h3>
|
||||
Set A (Base)
|
||||
<Tooltip text="The base set of IP addresses, CIDR blocks, or ranges">
|
||||
<Icon name="help" size="sm" />
|
||||
</Tooltip>
|
||||
</h3>
|
||||
<div class="input-wrapper">
|
||||
<textarea
|
||||
bind:value={setA}
|
||||
placeholder="192.168.1.0/24 10.0.0.0-10.0.0.100"
|
||||
class="input-textarea set-a"
|
||||
rows="6"
|
||||
></textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Set B -->
|
||||
<div class="input-group">
|
||||
<h3>
|
||||
Set B (Subtract)
|
||||
<Tooltip text="The set to subtract from Set A (can be empty)">
|
||||
<Icon name="help" size="sm" />
|
||||
</Tooltip>
|
||||
</h3>
|
||||
<div class="input-wrapper">
|
||||
<textarea
|
||||
bind:value={setB}
|
||||
placeholder="192.168.1.128/25 10.0.0.50-10.0.0.75"
|
||||
class="input-textarea set-b"
|
||||
rows="6"
|
||||
></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="input-actions">
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-secondary btn-sm"
|
||||
onclick={clearInputs}
|
||||
>
|
||||
<Icon name="trash" size="sm" />
|
||||
Clear All
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Examples -->
|
||||
<div class="examples-section">
|
||||
<h4>Quick Examples</h4>
|
||||
<div class="examples-grid">
|
||||
{#each examples as example}
|
||||
<button
|
||||
type="button"
|
||||
class="example-btn"
|
||||
onclick={() => setExample(example)}
|
||||
>
|
||||
{example.label}
|
||||
</button>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Results Section -->
|
||||
{#if result}
|
||||
<div class="results-section">
|
||||
{#if result.errors.length > 0}
|
||||
<div class="info-panel error">
|
||||
<h3>Parse Errors</h3>
|
||||
<ul class="error-list">
|
||||
{#each result.errors as error}
|
||||
<li>{error}</li>
|
||||
{/each}
|
||||
</ul>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if result.ipv4.length > 0 || result.ipv6.length > 0}
|
||||
<!-- Statistics -->
|
||||
<div class="stats-section">
|
||||
<div class="summary-header">
|
||||
<h3>Difference Results (A - B)</h3>
|
||||
<div class="export-buttons">
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-primary btn-sm"
|
||||
class:copied={copiedStates['all-text']}
|
||||
onclick={() => copyAllResults('text')}
|
||||
>
|
||||
<Icon name={copiedStates['all-text'] ? 'check' : 'copy'} size="sm" />
|
||||
Text
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-secondary btn-sm"
|
||||
class:copied={copiedStates['all-json']}
|
||||
onclick={() => copyAllResults('json')}
|
||||
>
|
||||
<Icon name={copiedStates['all-json'] ? 'check' : 'download'} size="sm" />
|
||||
JSON
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="stats-grid">
|
||||
<div class="stat-card input-a">
|
||||
<span class="stat-label">Set A (Input)</span>
|
||||
<span class="stat-value">{result.stats.inputA.count} items</span>
|
||||
<span class="stat-detail">{result.stats.inputA.addresses} addresses</span>
|
||||
</div>
|
||||
<div class="stat-card input-b">
|
||||
<span class="stat-label">Set B (Subtract)</span>
|
||||
<span class="stat-value">{result.stats.inputB.count} items</span>
|
||||
<span class="stat-detail">{result.stats.inputB.addresses} addresses</span>
|
||||
</div>
|
||||
<div class="stat-card result">
|
||||
<span class="stat-label">Result (A - B)</span>
|
||||
<span class="stat-value">{result.stats.output.count} CIDRs</span>
|
||||
<span class="stat-detail">{result.stats.output.addresses} addresses</span>
|
||||
</div>
|
||||
<div class="stat-card efficiency">
|
||||
<span class="stat-label">Efficiency</span>
|
||||
<span class="stat-value">{result.stats.efficiency}%</span>
|
||||
<span class="stat-detail">{result.stats.removed.addresses} removed</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Visualization -->
|
||||
{#if result.visualization.setA.length > 0}
|
||||
<div class="visualization-section">
|
||||
<h4>Set Operation Visualization</h4>
|
||||
<div class="viz-legend">
|
||||
<div class="legend-item">
|
||||
<div class="legend-color set-a-color"></div>
|
||||
<span>Set A (Base)</span>
|
||||
</div>
|
||||
<div class="legend-item">
|
||||
<div class="legend-color set-b-color"></div>
|
||||
<span>Set B (Subtract)</span>
|
||||
</div>
|
||||
<div class="legend-item">
|
||||
<div class="legend-color result-color"></div>
|
||||
<span>Result (A - B)</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="visualization-bar">
|
||||
<!-- Set A (background) -->
|
||||
{#each result.visualization.setA as range}
|
||||
<div
|
||||
class="viz-segment set-a-segment"
|
||||
style="width: {getBarWidth(range)}%; left: {getBarOffset(range)}%"
|
||||
use:tooltip={{ text: getSegmentTooltip(range, 'A'), position: 'top' }}
|
||||
></div>
|
||||
{/each}
|
||||
|
||||
<!-- Set B (overlay) -->
|
||||
{#each result.visualization.setB as range}
|
||||
<div
|
||||
class="viz-segment set-b-segment"
|
||||
style="width: {getBarWidth(range)}%; left: {getBarOffset(range)}%"
|
||||
use:tooltip={{ text: getSegmentTooltip(range, 'B'), position: 'top' }}
|
||||
></div>
|
||||
{/each}
|
||||
|
||||
<!-- Result (final) -->
|
||||
{#each result.visualization.result as range}
|
||||
<div
|
||||
class="viz-segment result-segment"
|
||||
style="width: {getBarWidth(range)}%; left: {getBarOffset(range)}%"
|
||||
use:tooltip={{ text: getSegmentTooltip(range, 'result'), position: 'bottom' }}
|
||||
></div>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<!-- Results Grid -->
|
||||
<div class="results-grid">
|
||||
<!-- IPv4 Results -->
|
||||
{#if result.ipv4.length > 0}
|
||||
<div class="result-panel ipv4">
|
||||
<div class="panel-header">
|
||||
<h4>IPv4 Results ({result.ipv4.length})</h4>
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-icon"
|
||||
class:copied={copiedStates['ipv4']}
|
||||
onclick={() => copyToClipboard((result?.ipv4 || []).join('\n'), 'ipv4')}
|
||||
>
|
||||
<Icon name={copiedStates['ipv4'] ? 'check' : 'copy'} size="sm" />
|
||||
</button>
|
||||
</div>
|
||||
<div class="cidr-list">
|
||||
{#each result.ipv4 as cidr}
|
||||
<div class="cidr-item">
|
||||
<code class="cidr-block">{cidr}</code>
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-icon btn-xs"
|
||||
class:copied={copiedStates[cidr]}
|
||||
onclick={() => copyToClipboard(cidr, cidr)}
|
||||
>
|
||||
<Icon name={copiedStates[cidr] ? 'check' : 'copy'} size="xs" />
|
||||
</button>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<!-- IPv6 Results -->
|
||||
{#if result.ipv6.length > 0}
|
||||
<div class="result-panel ipv6">
|
||||
<div class="panel-header">
|
||||
<h4>IPv6 Results ({result.ipv6.length})</h4>
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-icon"
|
||||
class:copied={copiedStates['ipv6']}
|
||||
onclick={() => copyToClipboard((result?.ipv6 || []).join('\n'), 'ipv6')}
|
||||
>
|
||||
<Icon name={copiedStates['ipv6'] ? 'check' : 'copy'} size="sm" />
|
||||
</button>
|
||||
</div>
|
||||
<div class="cidr-list">
|
||||
{#each result.ipv6 as cidr}
|
||||
<div class="cidr-item">
|
||||
<code class="cidr-block">{cidr}</code>
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-icon btn-xs"
|
||||
class:copied={copiedStates[cidr]}
|
||||
onclick={() => copyToClipboard(cidr, cidr)}
|
||||
>
|
||||
<Icon name={copiedStates[cidr] ? 'check' : 'copy'} size="xs" />
|
||||
</button>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{:else}
|
||||
<div class="info-panel info">
|
||||
<h3>No Results</h3>
|
||||
<p>The difference A - B resulted in an empty set. Set B completely contains or covers Set A.</p>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style lang="scss">
|
||||
/* Reusable tokens */
|
||||
%section-title {
|
||||
color: var(--color-primary);
|
||||
margin-bottom: var(--spacing-md);
|
||||
}
|
||||
|
||||
%bg-surface {
|
||||
background-color: var(--bg-secondary);
|
||||
}
|
||||
|
||||
/* Mode section */
|
||||
.mode-section {
|
||||
margin-bottom: var(--spacing-lg);
|
||||
|
||||
h3 { @extend %section-title; }
|
||||
|
||||
.tabs {
|
||||
margin-bottom: var(--spacing-md);
|
||||
|
||||
.tab {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-xs);
|
||||
|
||||
:global(.tooltip-trigger) {
|
||||
opacity: 0.7;
|
||||
transition: opacity var(--transition-fast);
|
||||
|
||||
&:hover { opacity: 1; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.constraint-input {
|
||||
.constraint-field {
|
||||
width: 120px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Input section */
|
||||
.input-section {
|
||||
margin-bottom: var(--spacing-lg);
|
||||
|
||||
.input-grid {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: var(--spacing-lg);
|
||||
margin-bottom: var(--spacing-md);
|
||||
}
|
||||
|
||||
.input-group {
|
||||
h3 {
|
||||
@extend %section-title;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-xs);
|
||||
|
||||
:global(.tooltip-trigger) {
|
||||
opacity: 0.7;
|
||||
&:hover { opacity: 1; color: var(--color-info); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.input-textarea {
|
||||
width: 100%;
|
||||
height: 140px;
|
||||
font-family: var(--font-mono);
|
||||
font-size: var(--font-size-sm);
|
||||
resize: vertical;
|
||||
|
||||
&.set-a { border-left: 4px solid var(--color-info); }
|
||||
&.set-b { border-left: 4px solid var(--color-warning); }
|
||||
}
|
||||
|
||||
.input-actions {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
margin-bottom: var(--spacing-lg);
|
||||
}
|
||||
}
|
||||
|
||||
/* Examples */
|
||||
.examples-section {
|
||||
margin-top: var(--spacing-lg);
|
||||
|
||||
h4 { @extend %section-title; }
|
||||
|
||||
.examples-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||||
gap: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.example-btn {
|
||||
padding: var(--spacing-sm);
|
||||
background-color: var(--bg-tertiary);
|
||||
border: 1px solid var(--border-secondary);
|
||||
border-radius: var(--radius-sm);
|
||||
font-size: var(--font-size-sm);
|
||||
transition: all var(--transition-fast);
|
||||
|
||||
&:hover {
|
||||
background-color: var(--surface-hover);
|
||||
border-color: var(--color-primary);
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Results */
|
||||
.results-section {
|
||||
border-top: 2px solid var(--border-secondary);
|
||||
padding-top: var(--spacing-lg);
|
||||
}
|
||||
|
||||
.summary-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: var(--spacing-lg);
|
||||
|
||||
h3 {
|
||||
color: var(--color-primary);
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.export-buttons {
|
||||
display: flex;
|
||||
gap: var(--spacing-sm);
|
||||
}
|
||||
}
|
||||
|
||||
/* Stats */
|
||||
.stats-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||||
gap: var(--spacing-md);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
}
|
||||
|
||||
.stat-card {
|
||||
@extend %bg-surface;
|
||||
padding: var(--spacing-md);
|
||||
border-radius: var(--radius-md);
|
||||
border: 1px solid var(--border-primary);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
text-align: center;
|
||||
|
||||
&.input-a { border-left: 4px solid var(--color-info); }
|
||||
&.input-b { border-left: 4px solid var(--color-warning); }
|
||||
&.result { border-left: 4px solid var(--color-success); }
|
||||
&.efficiency { border-left: 4px solid var(--color-primary); }
|
||||
}
|
||||
|
||||
.stat-label {
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-secondary);
|
||||
margin-bottom: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
font-family: var(--font-mono);
|
||||
font-size: var(--font-size-lg);
|
||||
font-weight: 600;
|
||||
color: var(--color-primary);
|
||||
margin-bottom: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.stat-detail {
|
||||
font-size: var(--font-size-xs);
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
/* Visualization */
|
||||
.visualization-section {
|
||||
margin-bottom: var(--spacing-lg);
|
||||
|
||||
h4 { @extend %section-title; }
|
||||
|
||||
.viz-legend {
|
||||
display: flex;
|
||||
gap: var(--spacing-lg);
|
||||
margin-bottom: var(--spacing-md);
|
||||
justify-content: center;
|
||||
|
||||
.legend-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
font-size: var(--font-size-sm);
|
||||
|
||||
.legend-color {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
border-radius: var(--radius-xs);
|
||||
|
||||
&.set-a-color { background-color: var(--color-info); }
|
||||
&.set-b-color { background-color: var(--color-warning); }
|
||||
&.result-color { background-color: var(--color-success); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.visualization-bar {
|
||||
position: relative;
|
||||
height: 60px;
|
||||
background-color: var(--bg-tertiary);
|
||||
border-radius: var(--radius-sm);
|
||||
border: 2px solid var(--border-primary);
|
||||
overflow: hidden;
|
||||
margin-bottom: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.viz-segment {
|
||||
position: absolute;
|
||||
height: 100%;
|
||||
transition: all var(--transition-fast);
|
||||
cursor: pointer;
|
||||
|
||||
&.set-a-segment {
|
||||
background-color: var(--color-info);
|
||||
opacity: 0.6;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
&.set-b-segment {
|
||||
background-color: var(--color-warning);
|
||||
opacity: 0.8;
|
||||
z-index: 2;
|
||||
top: 25%;
|
||||
height: 50%;
|
||||
}
|
||||
|
||||
&.result-segment {
|
||||
background-color: var(--color-success);
|
||||
opacity: 0.9;
|
||||
z-index: 3;
|
||||
top: 10%;
|
||||
height: 80%;
|
||||
border: 1px solid var(--bg-primary);
|
||||
}
|
||||
|
||||
&:hover {
|
||||
filter: brightness(1.1);
|
||||
z-index: 10;
|
||||
}
|
||||
}
|
||||
|
||||
/* Result panels */
|
||||
.results-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
||||
gap: var(--spacing-lg);
|
||||
}
|
||||
|
||||
.result-panel {
|
||||
@extend %bg-surface;
|
||||
border-radius: var(--radius-lg);
|
||||
overflow: hidden;
|
||||
|
||||
&.ipv4 { border: 2px solid var(--color-info); }
|
||||
&.ipv6 { border: 2px solid var(--color-success); }
|
||||
|
||||
.panel-header {
|
||||
padding: var(--spacing-md);
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
|
||||
h4 {
|
||||
margin: 0;
|
||||
color: var(--bg-primary);
|
||||
font-size: var(--font-size-md);
|
||||
}
|
||||
}
|
||||
|
||||
&.ipv4 .panel-header {
|
||||
background: linear-gradient(135deg, var(--color-info), var(--color-info-light));
|
||||
}
|
||||
|
||||
&.ipv6 .panel-header {
|
||||
background: linear-gradient(135deg, var(--color-success), var(--color-success-light));
|
||||
}
|
||||
}
|
||||
|
||||
.cidr-list {
|
||||
padding: var(--spacing-md);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.cidr-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: var(--spacing-sm);
|
||||
background-color: var(--bg-tertiary);
|
||||
border-radius: var(--radius-sm);
|
||||
border: 1px solid var(--border-primary);
|
||||
}
|
||||
|
||||
.cidr-block {
|
||||
font-family: var(--font-mono);
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
/* Shared styles */
|
||||
.error-list {
|
||||
margin: var(--spacing-sm) 0;
|
||||
padding-left: var(--spacing-md);
|
||||
|
||||
li {
|
||||
color: var(--color-error);
|
||||
margin-bottom: var(--spacing-xs);
|
||||
}
|
||||
}
|
||||
|
||||
.btn {
|
||||
&.copied {
|
||||
color: var(--color-success);
|
||||
background-color: rgba(35, 134, 54, 0.1);
|
||||
border-color: var(--color-success);
|
||||
}
|
||||
|
||||
:global(.icon) {
|
||||
width: 1rem;
|
||||
height: 1rem;
|
||||
}
|
||||
|
||||
&.btn-xs :global(.icon) {
|
||||
width: 0.75rem;
|
||||
height: 0.75rem;
|
||||
}
|
||||
}
|
||||
|
||||
/* Responsive */
|
||||
@media (max-width: 768px) {
|
||||
.input-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.results-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.stats-grid {
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
}
|
||||
|
||||
.examples-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.summary-header {
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-sm);
|
||||
align-items: stretch;
|
||||
}
|
||||
|
||||
.viz-legend {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: var(--spacing-sm);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -5,12 +5,13 @@
|
||||
*/
|
||||
interface Props {
|
||||
name: string;
|
||||
size?: 'sm' | 'md' | 'lg';
|
||||
size?: 'xs' | 'sm' | 'md' | 'lg';
|
||||
}
|
||||
|
||||
let { name, size = 'md' }: Props = $props();
|
||||
|
||||
const sizeClasses = {
|
||||
xs: 'w-2 h-2',
|
||||
sm: 'w-4 h-4',
|
||||
md: 'w-6 h-6',
|
||||
lg: 'w-8 h-8'
|
||||
|
||||
@@ -359,7 +359,7 @@
|
||||
|
||||
&.network-bit {
|
||||
background-color: var(--color-info);
|
||||
color: var(--text-primary);
|
||||
color: var(--bg-secondary);
|
||||
}
|
||||
|
||||
&.host-bit {
|
||||
|
||||
@@ -77,6 +77,12 @@ export const SUB_NAV: Record<string, (NavItem | NavGroup)[]> = {
|
||||
label: 'Subnet Splitter',
|
||||
description: 'Split a network into N equal child subnets or to a target prefix length',
|
||||
icon: 'subnet-splitter'
|
||||
},
|
||||
{
|
||||
href: '/cidr/diff',
|
||||
label: 'Set Difference',
|
||||
description: 'Compute A - B set operations on CIDR blocks, ranges, and IP addresses',
|
||||
icon: 'set-difference'
|
||||
}
|
||||
],
|
||||
'/ip-address-convertor': [
|
||||
|
||||
@@ -0,0 +1,431 @@
|
||||
/* CIDR Difference Utilities - Set Operations */
|
||||
|
||||
export interface IPRange {
|
||||
start: bigint;
|
||||
end: bigint;
|
||||
version: 4 | 6;
|
||||
}
|
||||
|
||||
export interface ParsedInput {
|
||||
ip: string;
|
||||
type: 'single' | 'cidr' | 'range';
|
||||
version: 4 | 6;
|
||||
range: IPRange;
|
||||
}
|
||||
|
||||
export interface DiffResult {
|
||||
ipv4: string[];
|
||||
ipv6: string[];
|
||||
stats: {
|
||||
inputA: { count: number; addresses: string };
|
||||
inputB: { count: number; addresses: string };
|
||||
output: { count: number; addresses: string };
|
||||
removed: { count: number; addresses: string };
|
||||
efficiency: number; // percentage kept
|
||||
};
|
||||
visualization: {
|
||||
setA: Array<{ start: bigint; end: bigint; cidr?: string }>;
|
||||
setB: Array<{ start: bigint; end: bigint; cidr?: string }>;
|
||||
result: Array<{ start: bigint; end: bigint; cidr: string }>;
|
||||
version: 4 | 6;
|
||||
totalRange: { start: bigint; end: bigint };
|
||||
};
|
||||
errors: string[];
|
||||
}
|
||||
|
||||
export type AlignmentMode = 'minimal' | 'constrained';
|
||||
|
||||
/* IPv4/IPv6 conversion utilities */
|
||||
function ipv4ToBigInt(ip: string): bigint {
|
||||
const parts = ip.split('.').map(Number);
|
||||
if (parts.length !== 4 || parts.some(p => isNaN(p) || p < 0 || p > 255)) {
|
||||
throw new Error('Invalid IPv4 address');
|
||||
}
|
||||
return BigInt(parts[0] * 16777216 + parts[1] * 65536 + parts[2] * 256 + parts[3]);
|
||||
}
|
||||
|
||||
function bigIntToIPv4(num: bigint): string {
|
||||
const n = Number(num);
|
||||
return [
|
||||
Math.floor(n / 16777216) % 256,
|
||||
Math.floor(n / 65536) % 256,
|
||||
Math.floor(n / 256) % 256,
|
||||
n % 256
|
||||
].join('.');
|
||||
}
|
||||
|
||||
function ipv6ToBigInt(ip: string): bigint {
|
||||
let expanded = expandIPv6Simple(ip);
|
||||
const groups = expanded.split(':');
|
||||
|
||||
let result = 0n;
|
||||
for (let i = 0; i < 8; i++) {
|
||||
const group = parseInt(groups[i] || '0', 16);
|
||||
result = (result << 16n) + BigInt(group);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function expandIPv6Simple(ip: string): string {
|
||||
if (!ip.includes('::')) {
|
||||
return ip.split(':').map(g => g.padStart(4, '0')).join(':');
|
||||
}
|
||||
|
||||
const parts = ip.split('::');
|
||||
const left = parts[0] ? parts[0].split(':') : [];
|
||||
const right = parts[1] ? parts[1].split(':') : [];
|
||||
const missing = 8 - left.length - right.length;
|
||||
const middle = Array(missing).fill('0000');
|
||||
|
||||
return [...left, ...middle, ...right].map(g => g.padStart(4, '0')).join(':');
|
||||
}
|
||||
|
||||
function bigIntToIPv6(num: bigint): string {
|
||||
const groups = [];
|
||||
let remaining = num;
|
||||
|
||||
for (let i = 0; i < 8; i++) {
|
||||
groups.unshift((remaining & 0xffffn).toString(16));
|
||||
remaining >>= 16n;
|
||||
}
|
||||
|
||||
return groups.join(':');
|
||||
}
|
||||
|
||||
function detectIPVersion(ip: string): 4 | 6 {
|
||||
if (ip.includes('.') && /^(\d{1,3}\.){3}\d{1,3}$/.test(ip)) {
|
||||
return 4;
|
||||
}
|
||||
if (ip.includes(':')) {
|
||||
return 6;
|
||||
}
|
||||
throw new Error(`Cannot determine IP version: ${ip}`);
|
||||
}
|
||||
|
||||
/* Parse input (IP, CIDR, or range) */
|
||||
export function parseInput(input: string): ParsedInput {
|
||||
const trimmed = input.trim();
|
||||
|
||||
// Range format: IP1-IP2
|
||||
if (trimmed.includes('-')) {
|
||||
const [start, end] = trimmed.split('-').map(s => s.trim());
|
||||
const startVersion = detectIPVersion(start);
|
||||
const endVersion = detectIPVersion(end);
|
||||
|
||||
if (startVersion !== endVersion) {
|
||||
throw new Error('Range must use same IP version');
|
||||
}
|
||||
|
||||
const startBig = startVersion === 4 ? ipv4ToBigInt(start) : ipv6ToBigInt(start);
|
||||
const endBig = startVersion === 4 ? ipv4ToBigInt(end) : ipv6ToBigInt(end);
|
||||
|
||||
if (startBig > endBig) {
|
||||
throw new Error('Invalid range: start must be <= end');
|
||||
}
|
||||
|
||||
return {
|
||||
ip: trimmed,
|
||||
type: 'range',
|
||||
version: startVersion,
|
||||
range: { start: startBig, end: endBig, version: startVersion }
|
||||
};
|
||||
}
|
||||
|
||||
// CIDR format: IP/prefix
|
||||
if (trimmed.includes('/')) {
|
||||
const [ip, prefixStr] = trimmed.split('/');
|
||||
const version = detectIPVersion(ip);
|
||||
const prefix = parseInt(prefixStr);
|
||||
const maxPrefix = version === 4 ? 32 : 128;
|
||||
|
||||
if (prefix < 0 || prefix > maxPrefix) {
|
||||
throw new Error(`Invalid prefix: ${prefix}`);
|
||||
}
|
||||
|
||||
const ipBig = version === 4 ? ipv4ToBigInt(ip) : ipv6ToBigInt(ip);
|
||||
const hostBits = BigInt(maxPrefix - prefix);
|
||||
const networkBig = ipBig >> hostBits << hostBits;
|
||||
const broadcastBig = networkBig + (1n << hostBits) - 1n;
|
||||
|
||||
return {
|
||||
ip: trimmed,
|
||||
type: 'cidr',
|
||||
version,
|
||||
range: { start: networkBig, end: broadcastBig, version }
|
||||
};
|
||||
}
|
||||
|
||||
// Single IP
|
||||
const version = detectIPVersion(trimmed);
|
||||
const ipBig = version === 4 ? ipv4ToBigInt(trimmed) : ipv6ToBigInt(trimmed);
|
||||
|
||||
return {
|
||||
ip: trimmed,
|
||||
type: 'single',
|
||||
version,
|
||||
range: { start: ipBig, end: ipBig, version }
|
||||
};
|
||||
}
|
||||
|
||||
/* Merge overlapping ranges */
|
||||
function mergeRanges(ranges: IPRange[]): IPRange[] {
|
||||
if (ranges.length === 0) return [];
|
||||
|
||||
const sorted = [...ranges].sort((a, b) => a.start < b.start ? -1 : 1);
|
||||
const merged: IPRange[] = [sorted[0]];
|
||||
|
||||
for (let i = 1; i < sorted.length; i++) {
|
||||
const current = sorted[i];
|
||||
const last = merged[merged.length - 1];
|
||||
|
||||
if (current.start <= last.end + 1n) {
|
||||
last.end = current.end > last.end ? current.end : last.end;
|
||||
} else {
|
||||
merged.push(current);
|
||||
}
|
||||
}
|
||||
|
||||
return merged;
|
||||
}
|
||||
|
||||
/* Subtract ranges B from ranges A */
|
||||
function subtractRanges(setA: IPRange[], setB: IPRange[]): IPRange[] {
|
||||
if (setA.length === 0) return [];
|
||||
if (setB.length === 0) return setA;
|
||||
|
||||
let result: IPRange[] = [...setA];
|
||||
|
||||
for (const b of setB) {
|
||||
const newResult: IPRange[] = [];
|
||||
|
||||
for (const a of result) {
|
||||
// No overlap
|
||||
if (a.end < b.start || a.start > b.end) {
|
||||
newResult.push(a);
|
||||
continue;
|
||||
}
|
||||
|
||||
// B completely contains A
|
||||
if (b.start <= a.start && b.end >= a.end) {
|
||||
// A is completely removed
|
||||
continue;
|
||||
}
|
||||
|
||||
// A completely contains B
|
||||
if (a.start < b.start && a.end > b.end) {
|
||||
// Split A into two ranges
|
||||
newResult.push({ start: a.start, end: b.start - 1n, version: a.version });
|
||||
newResult.push({ start: b.end + 1n, end: a.end, version: a.version });
|
||||
continue;
|
||||
}
|
||||
|
||||
// Partial overlap - left side of A remains
|
||||
if (a.start < b.start && a.end >= b.start) {
|
||||
newResult.push({ start: a.start, end: b.start - 1n, version: a.version });
|
||||
}
|
||||
|
||||
// Partial overlap - right side of A remains
|
||||
if (a.start <= b.end && a.end > b.end) {
|
||||
newResult.push({ start: b.end + 1n, end: a.end, version: a.version });
|
||||
}
|
||||
}
|
||||
|
||||
result = newResult;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Convert range to CIDR blocks */
|
||||
function rangeToCIDRs(range: IPRange, alignment: AlignmentMode = 'minimal', constrainedPrefix?: number): string[] {
|
||||
const cidrs: string[] = [];
|
||||
let start = range.start;
|
||||
const end = range.end;
|
||||
const maxPrefix = range.version === 4 ? 32 : 128;
|
||||
|
||||
// Safety counter
|
||||
let iterations = 0;
|
||||
const maxIterations = 1000;
|
||||
|
||||
while (start <= end && iterations < maxIterations) {
|
||||
iterations++;
|
||||
|
||||
let prefixLength = maxPrefix;
|
||||
let blockSize = 1n;
|
||||
|
||||
if (alignment === 'constrained' && constrainedPrefix !== undefined) {
|
||||
// Force alignment to specific prefix
|
||||
prefixLength = constrainedPrefix;
|
||||
blockSize = 1n << BigInt(maxPrefix - prefixLength);
|
||||
const alignedStart = start & ~(blockSize - 1n);
|
||||
|
||||
if (alignedStart === start && start + blockSize - 1n <= end) {
|
||||
// Perfect alignment
|
||||
} else {
|
||||
// Fall back to /32 or /128
|
||||
prefixLength = maxPrefix;
|
||||
blockSize = 1n;
|
||||
}
|
||||
} else {
|
||||
// Find largest aligned block that fits
|
||||
for (let p = 0; p < maxPrefix; p++) {
|
||||
const testBlockSize = 1n << BigInt(p);
|
||||
const alignedStart = start & ~(testBlockSize - 1n);
|
||||
|
||||
if (alignedStart === start && start + testBlockSize - 1n <= end) {
|
||||
blockSize = testBlockSize;
|
||||
prefixLength = maxPrefix - p;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ip = range.version === 4 ? bigIntToIPv4(start) : bigIntToIPv6(start);
|
||||
cidrs.push(`${ip}/${prefixLength}`);
|
||||
|
||||
start += blockSize;
|
||||
}
|
||||
|
||||
return cidrs;
|
||||
}
|
||||
|
||||
/* Calculate total addresses in ranges */
|
||||
function calculateTotalAddresses(ranges: IPRange[]): bigint {
|
||||
return ranges.reduce((total, range) => {
|
||||
return total + (range.end - range.start + 1n);
|
||||
}, 0n);
|
||||
}
|
||||
|
||||
/* Create visualization data */
|
||||
function createVisualization(
|
||||
setA: IPRange[],
|
||||
setB: IPRange[],
|
||||
result: IPRange[],
|
||||
version: 4 | 6
|
||||
): DiffResult['visualization'] {
|
||||
// Find overall range for visualization
|
||||
const allRanges = [...setA, ...setB, ...result];
|
||||
if (allRanges.length === 0) {
|
||||
return {
|
||||
setA: [],
|
||||
setB: [],
|
||||
result: [],
|
||||
version,
|
||||
totalRange: { start: 0n, end: 0n }
|
||||
};
|
||||
}
|
||||
|
||||
const totalStart = allRanges.reduce((min, r) => r.start < min ? r.start : min, allRanges[0].start);
|
||||
const totalEnd = allRanges.reduce((max, r) => r.end > max ? r.end : max, allRanges[0].end);
|
||||
|
||||
return {
|
||||
setA: setA.map(r => ({ start: r.start, end: r.end })),
|
||||
setB: setB.map(r => ({ start: r.start, end: r.end })),
|
||||
result: result.map(r => ({
|
||||
start: r.start,
|
||||
end: r.end,
|
||||
cidr: rangeToCIDRs(r)[0] || `${version === 4 ? bigIntToIPv4(r.start) : bigIntToIPv6(r.start)}/${version === 4 ? 32 : 128}`
|
||||
})),
|
||||
version,
|
||||
totalRange: { start: totalStart, end: totalEnd }
|
||||
};
|
||||
}
|
||||
|
||||
/* Main CIDR difference function */
|
||||
export function computeCIDRDifference(
|
||||
inputA: string,
|
||||
inputB: string,
|
||||
alignment: AlignmentMode = 'minimal',
|
||||
constrainedPrefix?: number
|
||||
): DiffResult {
|
||||
const linesA = inputA.trim().split('\n').filter(line => line.trim());
|
||||
const linesB = inputB.trim().split('\n').filter(line => line.trim());
|
||||
|
||||
const parsedA: ParsedInput[] = [];
|
||||
const parsedB: ParsedInput[] = [];
|
||||
const errors: string[] = [];
|
||||
|
||||
// Parse inputs
|
||||
linesA.forEach((line, index) => {
|
||||
try {
|
||||
parsedA.push(parseInput(line));
|
||||
} catch (error) {
|
||||
errors.push(`Set A line ${index + 1}: ${error instanceof Error ? error.message : 'Parse error'}`);
|
||||
}
|
||||
});
|
||||
|
||||
linesB.forEach((line, index) => {
|
||||
try {
|
||||
parsedB.push(parseInput(line));
|
||||
} catch (error) {
|
||||
errors.push(`Set B line ${index + 1}: ${error instanceof Error ? error.message : 'Parse error'}`);
|
||||
}
|
||||
});
|
||||
|
||||
// Separate by version
|
||||
const ipv4RangesA = parsedA.filter(p => p.version === 4).map(p => p.range);
|
||||
const ipv6RangesA = parsedA.filter(p => p.version === 6).map(p => p.range);
|
||||
const ipv4RangesB = parsedB.filter(p => p.version === 4).map(p => p.range);
|
||||
const ipv6RangesB = parsedB.filter(p => p.version === 6).map(p => p.range);
|
||||
|
||||
// Merge overlaps
|
||||
const mergedA4 = mergeRanges(ipv4RangesA);
|
||||
const mergedA6 = mergeRanges(ipv6RangesA);
|
||||
const mergedB4 = mergeRanges(ipv4RangesB);
|
||||
const mergedB6 = mergeRanges(ipv6RangesB);
|
||||
|
||||
// Compute differences
|
||||
const result4 = subtractRanges(mergedA4, mergedB4);
|
||||
const result6 = subtractRanges(mergedA6, mergedB6);
|
||||
|
||||
// Convert to CIDRs
|
||||
const ipv4CIDRs = result4.flatMap(r => rangeToCIDRs(r, alignment, constrainedPrefix)).sort();
|
||||
const ipv6CIDRs = result6.flatMap(r => rangeToCIDRs(r, alignment, constrainedPrefix)).sort();
|
||||
|
||||
// Calculate statistics
|
||||
const totalA4 = calculateTotalAddresses(mergedA4);
|
||||
const totalA6 = calculateTotalAddresses(mergedA6);
|
||||
const totalB4 = calculateTotalAddresses(mergedB4);
|
||||
const totalB6 = calculateTotalAddresses(mergedB6);
|
||||
const totalResult4 = calculateTotalAddresses(result4);
|
||||
const totalResult6 = calculateTotalAddresses(result6);
|
||||
|
||||
const totalInputA = totalA4 + totalA6;
|
||||
const totalInputB = totalB4 + totalB6;
|
||||
const totalOutput = totalResult4 + totalResult6;
|
||||
const totalRemoved = totalInputA - totalOutput;
|
||||
const efficiency = totalInputA > 0 ? Number((totalOutput * 100n) / totalInputA) : 0;
|
||||
|
||||
// Create visualization (use IPv4 if available, otherwise IPv6)
|
||||
const vizVersion = mergedA4.length > 0 || mergedB4.length > 0 ? 4 : 6;
|
||||
const vizA = vizVersion === 4 ? mergedA4 : mergedA6;
|
||||
const vizB = vizVersion === 4 ? mergedB4 : mergedB6;
|
||||
const vizResult = vizVersion === 4 ? result4 : result6;
|
||||
|
||||
return {
|
||||
ipv4: ipv4CIDRs,
|
||||
ipv6: ipv6CIDRs,
|
||||
stats: {
|
||||
inputA: {
|
||||
count: ipv4RangesA.length + ipv6RangesA.length,
|
||||
addresses: totalInputA.toLocaleString()
|
||||
},
|
||||
inputB: {
|
||||
count: ipv4RangesB.length + ipv6RangesB.length,
|
||||
addresses: totalInputB.toLocaleString()
|
||||
},
|
||||
output: {
|
||||
count: ipv4CIDRs.length + ipv6CIDRs.length,
|
||||
addresses: totalOutput.toLocaleString()
|
||||
},
|
||||
removed: {
|
||||
count: 0, // Not tracking individual removed ranges
|
||||
addresses: totalRemoved.toLocaleString()
|
||||
},
|
||||
efficiency: Math.round(efficiency)
|
||||
},
|
||||
visualization: createVisualization(vizA, vizB, vizResult, vizVersion),
|
||||
errors
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,500 @@
|
||||
<script lang="ts">
|
||||
import CIDRDiff from '$lib/components/CIDRDiff.svelte';
|
||||
import { site } from '$lib/constants/site';
|
||||
import Icon from '$lib/components/Icon.svelte';
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>CIDR Set Difference Calculator - Compute A - B Operations | {site.title}</title>
|
||||
<meta name="description" content="Calculate CIDR set differences with A - B operations. Supports IPv4/IPv6 networks, ranges, and single IPs with visualization and alignment options." />
|
||||
<meta name="keywords" content="{site.keywords}, CIDR difference, set operations, network subtraction, IP set operations, CIDR calculator" />
|
||||
<meta property="og:title" content="CIDR Set Difference Calculator - Compute A - B Operations" />
|
||||
<meta property="og:description" content="Perform set difference operations on CIDR blocks with minimal alignment and visualization." />
|
||||
<meta property="og:url" content="{site.url}/cidr/diff" />
|
||||
</svelte:head>
|
||||
|
||||
<div class="page-container">
|
||||
<CIDRDiff />
|
||||
|
||||
<section class="explainer-section">
|
||||
<h3>About CIDR Set Operations</h3>
|
||||
<p>
|
||||
Set operations allow you to combine, subtract, and analyze IP address ranges mathematically.
|
||||
The difference operation (A - B) returns all addresses in set A that are NOT in set B.
|
||||
</p>
|
||||
|
||||
<div class="set-operations">
|
||||
<h4>Set Operations Quick Reference</h4>
|
||||
<div class="operations-grid">
|
||||
<div class="operation-card">
|
||||
<div class="operation-symbol">A ∪ B</div>
|
||||
<div class="operation-name">Union</div>
|
||||
<div class="operation-desc">All addresses in A OR B</div>
|
||||
</div>
|
||||
<div class="operation-card">
|
||||
<div class="operation-symbol">A ∩ B</div>
|
||||
<div class="operation-name">Intersection</div>
|
||||
<div class="operation-desc">Addresses in BOTH A AND B</div>
|
||||
</div>
|
||||
<div class="operation-card active">
|
||||
<div class="operation-symbol">A - B</div>
|
||||
<div class="operation-name">Difference</div>
|
||||
<div class="operation-desc">Addresses in A but NOT in B</div>
|
||||
</div>
|
||||
<div class="operation-card">
|
||||
<div class="operation-symbol">A ⊆ B</div>
|
||||
<div class="operation-name">Containment</div>
|
||||
<div class="operation-desc">Check if A is subset of B</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="worked-examples">
|
||||
<h4>Worked Examples</h4>
|
||||
|
||||
<div class="example-card ipv4">
|
||||
<h5><Icon name="network" size="sm" /> IPv4 Network Segmentation</h5>
|
||||
<div class="example-content">
|
||||
<div class="example-input">
|
||||
<strong>Set A:</strong> <code>192.168.1.0/24</code> (256 addresses)<br>
|
||||
<strong>Set B:</strong> <code>192.168.1.128/25</code> (128 addresses)
|
||||
</div>
|
||||
<div class="example-arrow">↓</div>
|
||||
<div class="example-result">
|
||||
<strong>A - B:</strong> <code>192.168.1.0/25</code> (128 addresses)<br>
|
||||
<span class="example-explanation">The first half remains after subtracting the second half</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="example-card ipv6">
|
||||
<h5><Icon name="globe" size="sm" /> IPv6 Address Management</h5>
|
||||
<div class="example-content">
|
||||
<div class="example-input">
|
||||
<strong>Set A:</strong> <code>2001:db8::/48</code> (entire /48 block)<br>
|
||||
<strong>Set B:</strong> <code>2001:db8:1::/64</code> (single /64 subnet)
|
||||
</div>
|
||||
<div class="example-arrow">↓</div>
|
||||
<div class="example-result">
|
||||
<strong>A - B:</strong> Multiple CIDR blocks excluding the /64<br>
|
||||
<span class="example-explanation">All /48 addresses except the allocated /64 subnet</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="example-card mixed">
|
||||
<h5><Icon name="layers" size="sm" /> Complex Range Operations</h5>
|
||||
<div class="example-content">
|
||||
<div class="example-input">
|
||||
<strong>Set A:</strong> <code>10.0.0.0/16</code> (65,536 addresses)<br>
|
||||
<strong>Set B:</strong> <code>10.0.10.0/24</code>, <code>10.0.20.50-10.0.20.100</code>
|
||||
</div>
|
||||
<div class="example-arrow">↓</div>
|
||||
<div class="example-result">
|
||||
<strong>A - B:</strong> Multiple optimized CIDR blocks<br>
|
||||
<span class="example-explanation">Original /16 with specific ranges carved out</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="edge-cases">
|
||||
<h4>Edge Cases & Considerations</h4>
|
||||
<div class="cases-grid">
|
||||
<div class="case-item">
|
||||
<h5>Non-aligned Ranges</h5>
|
||||
<p>
|
||||
When subtracting ranges that don't align to CIDR boundaries, the result may
|
||||
require multiple smaller CIDR blocks to represent exactly.
|
||||
</p>
|
||||
</div>
|
||||
<div class="case-item">
|
||||
<h5>Duplicate Inputs</h5>
|
||||
<p>
|
||||
Overlapping ranges in the same set are automatically merged before processing.
|
||||
<code>10.0.0.0/24</code> + <code>10.0.0.128/25</code> = <code>10.0.0.0/24</code>
|
||||
</p>
|
||||
</div>
|
||||
<div class="case-item">
|
||||
<h5>Mixed IP Families</h5>
|
||||
<p>
|
||||
IPv4 and IPv6 addresses are processed separately. You cannot subtract
|
||||
IPv6 ranges from IPv4 ranges (and vice versa).
|
||||
</p>
|
||||
</div>
|
||||
<div class="case-item">
|
||||
<h5>Empty Results</h5>
|
||||
<p>
|
||||
If set B completely contains set A, the result is empty. For example:
|
||||
<code>192.168.1.0/25 - 192.168.1.0/24 = ∅</code>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="alignment-modes">
|
||||
<h4>Alignment Modes</h4>
|
||||
<div class="alignment-comparison">
|
||||
<div class="alignment-mode">
|
||||
<h5><Icon name="minimize" size="sm" /> Minimal Alignment</h5>
|
||||
<p>
|
||||
Generates the most efficient CIDR representation with the fewest blocks.
|
||||
Uses variable prefix lengths to exactly cover the result ranges.
|
||||
</p>
|
||||
<div class="alignment-example">
|
||||
<strong>Example:</strong> Result might use /25, /26, /30 prefixes for optimal coverage
|
||||
</div>
|
||||
</div>
|
||||
<div class="alignment-mode">
|
||||
<h5><Icon name="target" size="sm" /> Constrained Alignment</h5>
|
||||
<p>
|
||||
Forces results to align to specific prefix boundaries. Useful for
|
||||
organizational policies or routing requirements.
|
||||
</p>
|
||||
<div class="alignment-example">
|
||||
<strong>Example:</strong> Force all results to /24 boundaries, may require more blocks
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="use-cases">
|
||||
<h4>Common Use Cases</h4>
|
||||
<div class="use-case-list">
|
||||
<div class="use-case">
|
||||
<Icon name="shield" size="sm" />
|
||||
<div>
|
||||
<strong>Firewall Rules:</strong> Define allowed ranges by subtracting blocked addresses
|
||||
</div>
|
||||
</div>
|
||||
<div class="use-case">
|
||||
<Icon name="server" size="sm" />
|
||||
<div>
|
||||
<strong>IP Allocation:</strong> Track available addresses after assignments
|
||||
</div>
|
||||
</div>
|
||||
<div class="use-case">
|
||||
<Icon name="cloud" size="sm" />
|
||||
<div>
|
||||
<strong>Cloud Networks:</strong> Reserve address space while excluding system ranges
|
||||
</div>
|
||||
</div>
|
||||
<div class="use-case">
|
||||
<Icon name="analytics" size="sm" />
|
||||
<div>
|
||||
<strong>Network Analysis:</strong> Calculate unused address space in allocations
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="visualization-guide">
|
||||
<h4>Understanding the Visualization</h4>
|
||||
<div class="viz-guide-content">
|
||||
<p>
|
||||
The overlay visualization shows how set operations work spatially across the address space:
|
||||
</p>
|
||||
<ul class="viz-guide-list">
|
||||
<li><strong>Blue bars:</strong> Set A (base ranges) - your starting address space</li>
|
||||
<li><strong>Orange overlay:</strong> Set B (subtract ranges) - what gets removed</li>
|
||||
<li><strong>Green segments:</strong> Final result - remaining address ranges</li>
|
||||
<li><strong>Hover details:</strong> Shows exact IP ranges, sizes, and CIDR notations</li>
|
||||
</ul>
|
||||
<p class="viz-tip">
|
||||
<Icon name="lightbulb" size="sm" />
|
||||
<strong>Tip:</strong> The visualization helps you understand why certain results
|
||||
require multiple CIDR blocks - they represent the "holes" left after subtraction.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<style lang="scss">
|
||||
.explainer-section {
|
||||
margin-top: var(--spacing-xl);
|
||||
padding: var(--spacing-xl);
|
||||
background-color: var(--bg-secondary);
|
||||
border-radius: var(--radius-lg);
|
||||
|
||||
h3 {
|
||||
color: var(--color-primary);
|
||||
margin-bottom: var(--spacing-md);
|
||||
font-size: var(--font-size-lg);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
h4 {
|
||||
color: var(--text-primary);
|
||||
margin: var(--spacing-xl) 0 var(--spacing-md) 0;
|
||||
font-size: var(--font-size-md);
|
||||
font-weight: 600;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
}
|
||||
|
||||
h5 {
|
||||
color: var(--color-primary);
|
||||
margin-bottom: var(--spacing-sm);
|
||||
font-size: var(--font-size-sm);
|
||||
font-weight: 600;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
}
|
||||
}
|
||||
|
||||
/* Set Operations */
|
||||
.operations-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||||
gap: var(--spacing-md);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
}
|
||||
|
||||
.operation-card {
|
||||
padding: var(--spacing-md);
|
||||
background-color: var(--bg-primary);
|
||||
border: 1px solid var(--border-secondary);
|
||||
border-radius: var(--radius-md);
|
||||
text-align: center;
|
||||
|
||||
&.active {
|
||||
border-color: var(--color-primary);
|
||||
background-color: var(--surface-hover);
|
||||
}
|
||||
|
||||
.operation-symbol {
|
||||
font-size: 2rem;
|
||||
font-weight: bold;
|
||||
color: var(--color-primary);
|
||||
margin-bottom: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.operation-name {
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
margin-bottom: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.operation-desc {
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
}
|
||||
|
||||
/* Worked Examples */
|
||||
.example-card {
|
||||
border: 1px solid var(--border-primary);
|
||||
border-radius: var(--radius-md);
|
||||
padding: var(--spacing-md);
|
||||
margin-bottom: var(--spacing-md);
|
||||
background: var(--bg-primary);
|
||||
|
||||
&.ipv4 { border-left: 4px solid var(--color-info); }
|
||||
&.ipv6 { border-left: 4px solid var(--color-success); }
|
||||
&.mixed { border-left: 4px solid var(--color-warning); }
|
||||
|
||||
.example-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-sm);
|
||||
|
||||
.example-input, .example-result {
|
||||
padding: var(--spacing-sm);
|
||||
background-color: var(--bg-tertiary);
|
||||
border-radius: var(--radius-sm);
|
||||
|
||||
code {
|
||||
background-color: var(--bg-secondary);
|
||||
padding: 2px var(--spacing-xs);
|
||||
border-radius: var(--radius-xs);
|
||||
font-family: var(--font-mono);
|
||||
font-size: var(--font-size-sm);
|
||||
}
|
||||
}
|
||||
|
||||
.example-arrow {
|
||||
text-align: center;
|
||||
font-size: var(--font-size-lg);
|
||||
color: var(--color-primary);
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.example-explanation {
|
||||
display: block;
|
||||
margin-top: var(--spacing-xs);
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-secondary);
|
||||
font-style: italic;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Edge Cases */
|
||||
.cases-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
|
||||
gap: var(--spacing-md);
|
||||
}
|
||||
|
||||
.case-item {
|
||||
padding: var(--spacing-md);
|
||||
background-color: var(--bg-primary);
|
||||
border: 1px solid var(--border-secondary);
|
||||
border-radius: var(--radius-md);
|
||||
|
||||
h5 {
|
||||
margin-bottom: var(--spacing-sm);
|
||||
color: var(--color-primary);
|
||||
}
|
||||
|
||||
p {
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-secondary);
|
||||
margin: 0;
|
||||
|
||||
code {
|
||||
background-color: var(--bg-secondary);
|
||||
padding: 1px var(--spacing-xs);
|
||||
border-radius: var(--radius-xs);
|
||||
font-family: var(--font-mono);
|
||||
font-size: var(--font-size-xs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Alignment Modes */
|
||||
.alignment-comparison {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
||||
gap: var(--spacing-lg);
|
||||
}
|
||||
|
||||
.alignment-mode {
|
||||
padding: var(--spacing-md);
|
||||
background-color: var(--bg-primary);
|
||||
border: 1px solid var(--border-primary);
|
||||
border-left: 4px solid var(--color-info);
|
||||
border-radius: var(--radius-md);
|
||||
|
||||
h5 {
|
||||
margin-bottom: var(--spacing-sm);
|
||||
}
|
||||
|
||||
p {
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-secondary);
|
||||
margin-bottom: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.alignment-example {
|
||||
padding: var(--spacing-sm);
|
||||
background-color: var(--bg-tertiary);
|
||||
border-radius: var(--radius-sm);
|
||||
font-size: var(--font-size-sm);
|
||||
|
||||
strong {
|
||||
color: var(--color-primary);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Use Cases */
|
||||
.use-case-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-md);
|
||||
}
|
||||
|
||||
.use-case {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-md);
|
||||
padding: var(--spacing-md);
|
||||
background-color: var(--bg-primary);
|
||||
border: 1px solid var(--border-secondary);
|
||||
border-radius: var(--radius-md);
|
||||
|
||||
:global(.icon) {
|
||||
color: var(--color-primary);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
div strong {
|
||||
color: var(--color-primary);
|
||||
}
|
||||
}
|
||||
|
||||
/* Visualization Guide */
|
||||
.viz-guide-content {
|
||||
p {
|
||||
margin-bottom: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.viz-guide-list {
|
||||
margin: var(--spacing-md) 0;
|
||||
padding-left: var(--spacing-lg);
|
||||
|
||||
li {
|
||||
margin-bottom: var(--spacing-sm);
|
||||
font-size: var(--font-size-sm);
|
||||
|
||||
strong {
|
||||
color: var(--color-primary);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.viz-tip {
|
||||
padding: var(--spacing-md);
|
||||
background-color: var(--bg-primary);
|
||||
border: 1px solid var(--border-secondary);
|
||||
border-left: 4px solid var(--color-warning);
|
||||
border-radius: var(--radius-md);
|
||||
margin-top: var(--spacing-md);
|
||||
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: var(--spacing-sm);
|
||||
|
||||
:global(.icon) {
|
||||
color: var(--color-warning);
|
||||
flex-shrink: 0;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
strong {
|
||||
color: var(--color-primary);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Responsive */
|
||||
@media (max-width: 768px) {
|
||||
.operations-grid {
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
}
|
||||
|
||||
.cases-grid,
|
||||
.alignment-comparison {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.example-content {
|
||||
.example-arrow {
|
||||
transform: rotate(90deg);
|
||||
margin: var(--spacing-sm) 0;
|
||||
}
|
||||
}
|
||||
|
||||
.viz-tip {
|
||||
flex-direction: column;
|
||||
|
||||
:global(.icon) {
|
||||
align-self: flex-start;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -9,10 +9,11 @@
|
||||
/* Text colors */
|
||||
--text-primary: #c9d1d9;
|
||||
--text-secondary: #7d8590;
|
||||
--text-tertiary: #6272a4;
|
||||
|
||||
/* Primary brand color */
|
||||
--color-primary: #dbf465;
|
||||
--color-primary-hover: #c5e146;
|
||||
--color-primary: #e3ed70; // was #f1fa8c;
|
||||
--color-primary-hover: #FFFFA5;
|
||||
--color-primary-dark: #a8c237;
|
||||
|
||||
/* Action colors */
|
||||
@@ -24,6 +25,10 @@
|
||||
--color-error-light: #FF6E6E;
|
||||
--color-info: #8be9fd;
|
||||
--color-info-light: #A4FFFF;
|
||||
|
||||
--color-purple: #BD93F9;
|
||||
--color-pink: #ff79c6;
|
||||
|
||||
|
||||
/* Border and surface colors */
|
||||
--border-primary: #30363d;
|
||||
|
||||
Reference in new issue
Block a user