mirror of
https://github.com/lissy93/networking-toolbox.git
synced 2026-11-28 00:10:03 +03:00
DHCP Option 82 Builder
This commit is contained in:
1 parent
b0f8d84730
commit
46a7666f6c
5 files changed
+1703
No files matched your search
@@ -0,0 +1,864 @@
|
||||
<script lang="ts">
|
||||
import { untrack } from 'svelte';
|
||||
import Icon from '$lib/components/global/Icon.svelte';
|
||||
import ToolContentContainer from '$lib/components/global/ToolContentContainer.svelte';
|
||||
import ExamplesCard from '$lib/components/common/ExamplesCard.svelte';
|
||||
import { useClipboard } from '$lib/composables';
|
||||
import {
|
||||
buildOption82,
|
||||
parseOption82,
|
||||
getDefaultOption82Config,
|
||||
type Option82Config,
|
||||
type Option82Result,
|
||||
type ParsedOption82,
|
||||
type SuboptionType,
|
||||
type EncodingFormat,
|
||||
} from '$lib/utils/dhcp-option82.js';
|
||||
|
||||
const modeOptions = [
|
||||
{ value: 'build' as const, label: 'Build', icon: 'wrench' },
|
||||
{ value: 'parse' as const, label: 'Parse', icon: 'search' },
|
||||
];
|
||||
|
||||
let mode = $state<'build' | 'parse'>('build');
|
||||
let config = $state<Option82Config>(getDefaultOption82Config());
|
||||
let result = $state<Option82Result | null>(null);
|
||||
let parseInput = $state<string>('');
|
||||
let parseResult = $state<ParsedOption82 | null>(null);
|
||||
let validationErrors = $state<string[]>([]);
|
||||
let selectedExampleIndex = $state<number | null>(null);
|
||||
|
||||
const clipboard = useClipboard();
|
||||
|
||||
const formatOptions: Array<{ value: EncodingFormat; label: string }> = [
|
||||
{ value: 'ascii', label: 'ASCII Text' },
|
||||
{ value: 'hex', label: 'Hexadecimal' },
|
||||
{ value: 'vlan-id', label: 'VLAN ID' },
|
||||
{ value: 'hostname-port', label: 'Hostname:Port' },
|
||||
];
|
||||
|
||||
interface BuildExample {
|
||||
label: string;
|
||||
type: SuboptionType;
|
||||
format: EncodingFormat;
|
||||
value: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
interface ParseExample {
|
||||
label: string;
|
||||
hexInput: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
const buildExamples: BuildExample[] = [
|
||||
{
|
||||
label: 'VLAN 100',
|
||||
type: 'circuit-id',
|
||||
format: 'vlan-id',
|
||||
value: '100',
|
||||
description: 'Circuit-ID as VLAN ID 100',
|
||||
},
|
||||
{
|
||||
label: 'Switch Port',
|
||||
type: 'circuit-id',
|
||||
format: 'hostname-port',
|
||||
value: 'sw1:Gi0/1',
|
||||
description: 'Circuit-ID as hostname:port',
|
||||
},
|
||||
{
|
||||
label: 'Custom Circuit',
|
||||
type: 'circuit-id',
|
||||
format: 'ascii',
|
||||
value: 'building-a-floor-3',
|
||||
description: 'Circuit-ID as custom ASCII text',
|
||||
},
|
||||
{
|
||||
label: 'Switch Hostname',
|
||||
type: 'remote-id',
|
||||
format: 'ascii',
|
||||
value: 'relay-sw1.example.com',
|
||||
description: 'Remote-ID as hostname',
|
||||
},
|
||||
{
|
||||
label: 'MAC Address',
|
||||
type: 'remote-id',
|
||||
format: 'hex',
|
||||
value: '001122334455',
|
||||
description: 'Remote-ID as MAC address',
|
||||
},
|
||||
{
|
||||
label: 'Agent ID',
|
||||
type: 'remote-id',
|
||||
format: 'ascii',
|
||||
value: 'DHCP-RELAY-01',
|
||||
description: 'Remote-ID as relay agent identifier',
|
||||
},
|
||||
];
|
||||
|
||||
const parseExamples: ParseExample[] = [
|
||||
{
|
||||
label: 'VLAN + Hostname',
|
||||
hexInput: '01020064020c7377312e6578616d706c65',
|
||||
description: 'Circuit-ID (VLAN 100) + Remote-ID (sw1.example)',
|
||||
},
|
||||
{
|
||||
label: 'Switch Port',
|
||||
hexInput: '01094769302f31020c7377312e6578616d706c65',
|
||||
description: 'Circuit-ID (Gi0/1) + Remote-ID (sw1.example)',
|
||||
},
|
||||
{
|
||||
label: 'MAC Address',
|
||||
hexInput: '0206001122334455',
|
||||
description: 'Remote-ID as MAC address (00:11:22:33:44:55)',
|
||||
},
|
||||
];
|
||||
|
||||
// Reactive generation - use untrack to prevent infinite loop
|
||||
$effect(() => {
|
||||
if (mode === 'build') {
|
||||
// Read config to track it
|
||||
const currentConfig = config;
|
||||
|
||||
// Untrack writes to prevent infinite loop
|
||||
untrack(() => {
|
||||
validateAndGenerate(currentConfig);
|
||||
checkIfExampleStillMatches();
|
||||
});
|
||||
} else {
|
||||
// Track parseInput changes in parse mode
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||
parseInput;
|
||||
|
||||
untrack(() => {
|
||||
checkIfExampleStillMatches();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Clear selected example when switching modes
|
||||
$effect(() => {
|
||||
// Track mode to trigger effect
|
||||
void mode;
|
||||
untrack(() => {
|
||||
selectedExampleIndex = null;
|
||||
});
|
||||
});
|
||||
|
||||
function validateAndGenerate(cfg: Option82Config = config) {
|
||||
const errors: string[] = [];
|
||||
|
||||
// Validate suboptions
|
||||
for (let i = 0; i < cfg.suboptions.length; i++) {
|
||||
const sub = cfg.suboptions[i];
|
||||
|
||||
if (!sub.value.trim()) {
|
||||
errors.push(`Suboption ${i + 1}: Value is required`);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sub.format === 'vlan-id') {
|
||||
const vlan = parseInt(sub.value, 10);
|
||||
if (isNaN(vlan) || vlan < 0 || vlan > 4095) {
|
||||
errors.push(`Suboption ${i + 1}: VLAN ID must be between 0 and 4095`);
|
||||
}
|
||||
}
|
||||
|
||||
if (sub.format === 'hex') {
|
||||
if (!/^[0-9a-fA-F:]+$/.test(sub.value.replace(/\s/g, ''))) {
|
||||
errors.push(`Suboption ${i + 1}: Invalid hex format`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
validationErrors = errors;
|
||||
|
||||
if (errors.length === 0) {
|
||||
result = buildOption82(cfg);
|
||||
} else {
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
|
||||
function parse() {
|
||||
if (!parseInput.trim()) {
|
||||
parseResult = null;
|
||||
validationErrors = [];
|
||||
return;
|
||||
}
|
||||
|
||||
if (!/^[0-9a-fA-F\s:]+$/.test(parseInput)) {
|
||||
validationErrors = ['Invalid hex input: only hexadecimal characters allowed'];
|
||||
parseResult = null;
|
||||
return;
|
||||
}
|
||||
|
||||
validationErrors = [];
|
||||
parseResult = parseOption82(parseInput);
|
||||
}
|
||||
|
||||
function addSuboption() {
|
||||
config.suboptions = [
|
||||
...config.suboptions,
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'ascii',
|
||||
value: '',
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function removeSuboption(index: number) {
|
||||
if (config.suboptions.length > 1) {
|
||||
config.suboptions = config.suboptions.filter((_, i) => i !== index);
|
||||
}
|
||||
}
|
||||
|
||||
function loadBuildExample(example: BuildExample, index: number) {
|
||||
config.suboptions = [
|
||||
{
|
||||
type: example.type,
|
||||
format: example.format,
|
||||
value: example.value,
|
||||
},
|
||||
];
|
||||
selectedExampleIndex = index;
|
||||
}
|
||||
|
||||
function loadParseExample(example: ParseExample, index: number) {
|
||||
parseInput = example.hexInput;
|
||||
selectedExampleIndex = index;
|
||||
parse();
|
||||
}
|
||||
|
||||
function checkIfExampleStillMatches() {
|
||||
if (selectedExampleIndex === null) return;
|
||||
|
||||
if (mode === 'build') {
|
||||
const example = buildExamples[selectedExampleIndex];
|
||||
if (!example) {
|
||||
selectedExampleIndex = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if current config matches the selected example
|
||||
const matches =
|
||||
config.suboptions.length === 1 &&
|
||||
config.suboptions[0].type === example.type &&
|
||||
config.suboptions[0].format === example.format &&
|
||||
config.suboptions[0].value === example.value;
|
||||
|
||||
if (!matches) {
|
||||
selectedExampleIndex = null;
|
||||
}
|
||||
} else {
|
||||
const example = parseExamples[selectedExampleIndex];
|
||||
if (!example) {
|
||||
selectedExampleIndex = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if parse input matches the selected example
|
||||
if (parseInput !== example.hexInput) {
|
||||
selectedExampleIndex = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<ToolContentContainer
|
||||
title="DHCP Option 82 Builder"
|
||||
description="Construct and parse DHCP Relay Agent Information (Option 82) with Circuit-ID, Remote-ID, and VLAN formats. Includes examples for relay ACLs and policies."
|
||||
navOptions={modeOptions}
|
||||
bind:selectedNav={mode}
|
||||
>
|
||||
{#if mode === 'build'}
|
||||
<ExamplesCard
|
||||
examples={buildExamples}
|
||||
onSelect={loadBuildExample}
|
||||
getLabel={(ex) => ex.label}
|
||||
getDescription={(ex) => ex.description}
|
||||
selectedIndex={selectedExampleIndex}
|
||||
/>
|
||||
{:else}
|
||||
<ExamplesCard
|
||||
examples={parseExamples}
|
||||
onSelect={loadParseExample}
|
||||
getLabel={(ex) => ex.label}
|
||||
getDescription={(ex) => ex.description}
|
||||
selectedIndex={selectedExampleIndex}
|
||||
/>
|
||||
{/if}
|
||||
|
||||
{#if mode === 'build'}
|
||||
<div class="card input-card">
|
||||
<div class="card-header">
|
||||
<h3>Configuration</h3>
|
||||
</div>
|
||||
<div class="card-content">
|
||||
{#each config.suboptions as suboption, i (`sub-${i}-${suboption.type}`)}
|
||||
<div class="suboption-group">
|
||||
<div class="suboption-header">
|
||||
<h4>Suboption {i + 1}</h4>
|
||||
{#if config.suboptions.length > 1}
|
||||
<button type="button" class="btn-icon" onclick={() => removeSuboption(i)}>
|
||||
<Icon name="x" size="sm" />
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="input-row">
|
||||
<div class="input-group">
|
||||
<label for="type-{i}">
|
||||
<Icon name="tag" size="sm" />
|
||||
Suboption Type
|
||||
</label>
|
||||
<select id="type-{i}" bind:value={suboption.type}>
|
||||
<option value="circuit-id">Circuit-ID (Suboption 1)</option>
|
||||
<option value="remote-id">Remote-ID (Suboption 2)</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="input-group">
|
||||
<label for="format-{i}">
|
||||
<Icon name="code" size="sm" />
|
||||
Encoding Format
|
||||
</label>
|
||||
<select id="format-{i}" bind:value={suboption.format}>
|
||||
{#each formatOptions as option (option.value)}
|
||||
<option value={option.value}>{option.label}</option>
|
||||
{/each}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="input-group">
|
||||
<label for="value-{i}">
|
||||
<Icon name="edit" size="sm" />
|
||||
Value
|
||||
</label>
|
||||
<input
|
||||
id="value-{i}"
|
||||
type="text"
|
||||
bind:value={suboption.value}
|
||||
placeholder={suboption.format === 'vlan-id'
|
||||
? '100'
|
||||
: suboption.format === 'hex'
|
||||
? '001122334455'
|
||||
: 'Enter value'}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
|
||||
<button type="button" class="btn-add" onclick={addSuboption}>
|
||||
<Icon name="plus" size="sm" />
|
||||
Add Suboption
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if validationErrors.length > 0}
|
||||
<div class="card errors-card">
|
||||
<h3>Validation Errors</h3>
|
||||
{#each validationErrors as error, i (i)}
|
||||
<div class="error-message">
|
||||
<Icon name="alert-triangle" size="sm" />
|
||||
{error}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if result && validationErrors.length === 0}
|
||||
<div class="card results">
|
||||
<h3>Generated Option 82</h3>
|
||||
|
||||
<div class="output-group">
|
||||
<div class="output-header">
|
||||
<h4>Hex-Encoded Value</h4>
|
||||
<button
|
||||
type="button"
|
||||
class="copy-btn"
|
||||
class:copied={clipboard.isCopied('hex')}
|
||||
onclick={() => clipboard.copy(result!.hexEncoded, 'hex')}
|
||||
>
|
||||
<Icon name={clipboard.isCopied('hex') ? 'check' : 'copy'} size="xs" />
|
||||
{clipboard.isCopied('hex') ? 'Copied' : 'Copy'}
|
||||
</button>
|
||||
</div>
|
||||
<pre class="output-value code-block">{result.hexEncoded}</pre>
|
||||
</div>
|
||||
|
||||
<div class="breakdown-section">
|
||||
<h4>Breakdown</h4>
|
||||
{#each result.breakdown as breakdown, i (i)}
|
||||
<div class="breakdown-item">
|
||||
<div class="breakdown-header">
|
||||
<strong>{breakdown.type} (Code {breakdown.typeCode})</strong>
|
||||
<span class="breakdown-length">Length: {breakdown.length} bytes</span>
|
||||
</div>
|
||||
<p class="breakdown-desc">{breakdown.description}</p>
|
||||
<div class="breakdown-values">
|
||||
<div><strong>Value:</strong> {breakdown.value}</div>
|
||||
<div><strong>Hex:</strong> {breakdown.hexValue}</div>
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
{#if result.examples.iscDhcpd}
|
||||
<div class="output-group">
|
||||
<div class="output-header">
|
||||
<h4>ISC dhcpd Configuration Example</h4>
|
||||
<button
|
||||
type="button"
|
||||
class="copy-btn"
|
||||
class:copied={clipboard.isCopied('isc')}
|
||||
onclick={() => clipboard.copy(result!.examples.iscDhcpd!, 'isc')}
|
||||
>
|
||||
<Icon name={clipboard.isCopied('isc') ? 'check' : 'copy'} size="xs" />
|
||||
{clipboard.isCopied('isc') ? 'Copied' : 'Copy'}
|
||||
</button>
|
||||
</div>
|
||||
<pre class="output-value code-block">{result.examples.iscDhcpd}</pre>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if result.examples.keaDhcp4}
|
||||
<div class="output-group">
|
||||
<div class="output-header">
|
||||
<h4>Kea DHCPv4 Configuration Example</h4>
|
||||
<button
|
||||
type="button"
|
||||
class="copy-btn"
|
||||
class:copied={clipboard.isCopied('kea')}
|
||||
onclick={() => clipboard.copy(result!.examples.keaDhcp4!, 'kea')}
|
||||
>
|
||||
<Icon name={clipboard.isCopied('kea') ? 'check' : 'copy'} size="xs" />
|
||||
{clipboard.isCopied('kea') ? 'Copied' : 'Copy'}
|
||||
</button>
|
||||
</div>
|
||||
<pre class="output-value code-block">{result.examples.keaDhcp4}</pre>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if result.examples.ciscoRelay}
|
||||
<div class="output-group">
|
||||
<div class="output-header">
|
||||
<h4>Cisco Relay Agent Example</h4>
|
||||
<button
|
||||
type="button"
|
||||
class="copy-btn"
|
||||
class:copied={clipboard.isCopied('cisco')}
|
||||
onclick={() => clipboard.copy(result!.examples.ciscoRelay!, 'cisco')}
|
||||
>
|
||||
<Icon name={clipboard.isCopied('cisco') ? 'check' : 'copy'} size="xs" />
|
||||
{clipboard.isCopied('cisco') ? 'Copied' : 'Copy'}
|
||||
</button>
|
||||
</div>
|
||||
<pre class="output-value code-block">{result.examples.ciscoRelay}</pre>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
{:else}
|
||||
<div class="card input-card">
|
||||
<div class="card-header">
|
||||
<h3>Parse Option 82 Hex</h3>
|
||||
</div>
|
||||
<div class="card-content">
|
||||
<div class="input-group">
|
||||
<label for="parse-input">
|
||||
<Icon name="code" size="sm" />
|
||||
Hex-Encoded Option 82
|
||||
</label>
|
||||
<textarea
|
||||
id="parse-input"
|
||||
bind:value={parseInput}
|
||||
placeholder="Enter hex string (e.g., 01064769302f31020b7377312e6578616d706c65)"
|
||||
rows="4"
|
||||
></textarea>
|
||||
</div>
|
||||
<button type="button" class="btn-primary" onclick={parse}>
|
||||
<Icon name="search" size="sm" />
|
||||
Parse
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if validationErrors.length > 0}
|
||||
<div class="card errors-card">
|
||||
<h3>Validation Errors</h3>
|
||||
{#each validationErrors as error, i (i)}
|
||||
<div class="error-message">
|
||||
<Icon name="alert-triangle" size="sm" />
|
||||
{error}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if parseResult && validationErrors.length === 0}
|
||||
<div class="card results">
|
||||
<h3>Parsed Option 82</h3>
|
||||
|
||||
<div class="parse-summary">
|
||||
<div><strong>Total Length:</strong> {parseResult.totalLength} bytes</div>
|
||||
<div><strong>Suboptions Found:</strong> {parseResult.suboptions.length}</div>
|
||||
</div>
|
||||
|
||||
<div class="breakdown-section">
|
||||
<h4>Suboptions</h4>
|
||||
{#each parseResult.suboptions as suboption, i (i)}
|
||||
<div class="breakdown-item">
|
||||
<div class="breakdown-header">
|
||||
<strong>{suboption.type} (Code {suboption.typeCode})</strong>
|
||||
<span class="breakdown-length">Length: {suboption.length} bytes</span>
|
||||
</div>
|
||||
<p class="breakdown-desc">{suboption.description}</p>
|
||||
<div class="breakdown-values">
|
||||
<div><strong>Decoded Value:</strong> {suboption.value}</div>
|
||||
<div><strong>Hex Value:</strong> {suboption.hexValue}</div>
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
</ToolContentContainer>
|
||||
|
||||
<style lang="scss">
|
||||
.card {
|
||||
background: var(--bg-secondary);
|
||||
border-radius: var(--radius-lg);
|
||||
padding: var(--spacing-lg);
|
||||
border: 1px solid var(--border-primary);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
|
||||
&.input-card {
|
||||
background: var(--bg-tertiary);
|
||||
.card-header {
|
||||
margin-bottom: var(--spacing-sm);
|
||||
}
|
||||
}
|
||||
|
||||
h3 {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
margin: 0 0 var(--spacing-sm);
|
||||
font-size: 1.25rem;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
}
|
||||
|
||||
.card-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-md);
|
||||
}
|
||||
|
||||
.suboption-group {
|
||||
padding: var(--spacing-md);
|
||||
background: var(--bg-secondary);
|
||||
border-radius: var(--radius-md);
|
||||
border: 1px solid var(--border-secondary);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-md);
|
||||
}
|
||||
|
||||
.suboption-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding-bottom: var(--spacing-sm);
|
||||
border-bottom: 1px solid var(--border-secondary);
|
||||
|
||||
h4 {
|
||||
margin: 0;
|
||||
font-size: 0.9375rem;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
}
|
||||
|
||||
.input-group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-xs);
|
||||
|
||||
label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-xs);
|
||||
font-weight: 500;
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
input,
|
||||
select,
|
||||
textarea {
|
||||
padding: var(--spacing-sm);
|
||||
background: var(--bg-primary);
|
||||
border: 1px solid var(--border-primary);
|
||||
border-radius: var(--radius-md);
|
||||
color: var(--text-primary);
|
||||
font-family: inherit;
|
||||
font-size: 0.9375rem;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:focus {
|
||||
outline: none;
|
||||
border-color: var(--color-primary);
|
||||
box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-primary), transparent 90%);
|
||||
}
|
||||
}
|
||||
|
||||
textarea {
|
||||
resize: vertical;
|
||||
font-family: var(--font-mono);
|
||||
}
|
||||
}
|
||||
|
||||
.input-row {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: var(--spacing-md);
|
||||
|
||||
@media (max-width: 768px) {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
.btn-icon {
|
||||
padding: var(--spacing-xs);
|
||||
background: transparent;
|
||||
border: 1px solid var(--border-secondary);
|
||||
border-radius: var(--radius-sm);
|
||||
color: var(--color-error);
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background: color-mix(in srgb, var(--color-error), transparent 90%);
|
||||
border-color: var(--color-error);
|
||||
}
|
||||
}
|
||||
|
||||
.btn-add,
|
||||
.btn-primary {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: var(--spacing-xs);
|
||||
padding: var(--spacing-sm);
|
||||
background: transparent;
|
||||
border: 1px dashed var(--border-secondary);
|
||||
border-radius: var(--radius-md);
|
||||
color: var(--text-secondary);
|
||||
font-size: 0.875rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background: var(--surface-hover);
|
||||
border-color: var(--color-primary);
|
||||
color: var(--color-primary);
|
||||
}
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
border-style: solid;
|
||||
background: var(--color-primary);
|
||||
color: var(--bg-primary);
|
||||
border-color: var(--color-primary);
|
||||
|
||||
&:hover {
|
||||
background: color-mix(in srgb, var(--color-primary), black 10%);
|
||||
}
|
||||
}
|
||||
|
||||
.errors-card {
|
||||
background: color-mix(in srgb, var(--color-error), transparent 95%);
|
||||
border-color: var(--color-error);
|
||||
}
|
||||
|
||||
.error-message {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
padding: var(--spacing-sm);
|
||||
color: var(--color-error);
|
||||
font-size: 0.9375rem;
|
||||
}
|
||||
|
||||
.results {
|
||||
background: var(--bg-tertiary);
|
||||
animation: slideIn 0.3s ease-out;
|
||||
}
|
||||
|
||||
@keyframes slideIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(-10px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
.output-group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-xs);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
|
||||
&:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.output-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
|
||||
h4 {
|
||||
margin: 0;
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
}
|
||||
|
||||
.copy-btn {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-xs);
|
||||
padding: var(--spacing-xs) var(--spacing-sm);
|
||||
background: transparent;
|
||||
border: 1px solid var(--border-secondary);
|
||||
border-radius: var(--radius-sm);
|
||||
color: var(--text-secondary);
|
||||
font-size: 0.875rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background: var(--surface-hover);
|
||||
border-color: var(--color-primary);
|
||||
color: var(--color-primary);
|
||||
}
|
||||
|
||||
&.copied {
|
||||
background: color-mix(in srgb, var(--color-success), transparent 90%);
|
||||
border-color: var(--color-success);
|
||||
color: var(--color-success);
|
||||
}
|
||||
}
|
||||
|
||||
.output-value {
|
||||
padding: var(--spacing-md);
|
||||
background: var(--bg-primary);
|
||||
border: 1px solid var(--border-primary);
|
||||
border-radius: var(--radius-md);
|
||||
font-family: var(--font-mono);
|
||||
font-size: 0.875rem;
|
||||
color: var(--color-primary);
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
.code-block {
|
||||
white-space: pre;
|
||||
word-break: normal;
|
||||
}
|
||||
|
||||
.breakdown-section {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-md);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
|
||||
h4 {
|
||||
margin: 0 0 var(--spacing-xs);
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
}
|
||||
|
||||
.breakdown-item {
|
||||
padding: var(--spacing-md);
|
||||
background: var(--bg-primary);
|
||||
border: 1px solid var(--border-primary);
|
||||
border-radius: var(--radius-md);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.breakdown-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
flex-wrap: wrap;
|
||||
gap: var(--spacing-xs);
|
||||
|
||||
strong {
|
||||
color: var(--text-primary);
|
||||
}
|
||||
}
|
||||
|
||||
.breakdown-length {
|
||||
font-size: 0.8125rem;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.breakdown-desc {
|
||||
margin: 0;
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-secondary);
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.breakdown-values {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-xs);
|
||||
font-size: 0.875rem;
|
||||
font-family: var(--font-mono);
|
||||
|
||||
div {
|
||||
strong {
|
||||
color: var(--text-secondary);
|
||||
font-weight: 500;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.parse-summary {
|
||||
display: flex;
|
||||
gap: var(--spacing-lg);
|
||||
padding: var(--spacing-md);
|
||||
background: color-mix(in srgb, var(--color-info), transparent 95%);
|
||||
border: 1px solid var(--color-info);
|
||||
border-radius: var(--radius-md);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
font-size: 0.9375rem;
|
||||
|
||||
div {
|
||||
strong {
|
||||
color: var(--text-primary);
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
flex-direction: column;
|
||||
gap: var(--spacing-xs);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -888,6 +888,35 @@ export const SUB_NAV: Record<string, (NavItem | NavGroup)[]> = {
|
||||
'dhcp6',
|
||||
],
|
||||
},
|
||||
{
|
||||
href: makePath('/dhcp/options/option82-builder'),
|
||||
label: 'Option 82 Builder',
|
||||
description:
|
||||
'Construct and parse DHCP Relay Agent Information (Option 82) with Circuit-ID, Remote-ID, and VLAN formats',
|
||||
icon: 'tools',
|
||||
keywords: [
|
||||
'dhcp',
|
||||
'option',
|
||||
'82',
|
||||
'relay',
|
||||
'agent',
|
||||
'information',
|
||||
'circuit-id',
|
||||
'remote-id',
|
||||
'vlan',
|
||||
'builder',
|
||||
'parser',
|
||||
'suboption',
|
||||
'tlv',
|
||||
'acl',
|
||||
'policy',
|
||||
'cisco',
|
||||
'kea',
|
||||
'isc',
|
||||
'network',
|
||||
'infrastructure',
|
||||
],
|
||||
},
|
||||
],
|
||||
'/dns': [
|
||||
{
|
||||
|
||||
@@ -0,0 +1,353 @@
|
||||
/**
|
||||
* DHCP Option 82 - Relay Agent Information (RFC 3046)
|
||||
* Utilities for constructing and parsing Option 82 suboptions
|
||||
*/
|
||||
|
||||
export type SuboptionType = 'circuit-id' | 'remote-id';
|
||||
export type EncodingFormat = 'hex' | 'ascii' | 'vlan-id' | 'hostname-port';
|
||||
|
||||
export interface Suboption {
|
||||
type: SuboptionType;
|
||||
format: EncodingFormat;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface Option82Config {
|
||||
suboptions: Suboption[];
|
||||
}
|
||||
|
||||
export interface Option82Result {
|
||||
hexEncoded: string;
|
||||
asciiDecoded: string;
|
||||
breakdown: SuboptionBreakdown[];
|
||||
examples: {
|
||||
iscDhcpd?: string;
|
||||
keaDhcp4?: string;
|
||||
ciscoRelay?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface SuboptionBreakdown {
|
||||
type: SuboptionType;
|
||||
typeCode: number;
|
||||
length: number;
|
||||
value: string;
|
||||
hexValue: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export interface ParsedOption82 {
|
||||
suboptions: SuboptionBreakdown[];
|
||||
totalLength: number;
|
||||
rawHex: string;
|
||||
}
|
||||
|
||||
const SUBOPTION_CODES: Record<SuboptionType, number> = {
|
||||
'circuit-id': 1,
|
||||
'remote-id': 2,
|
||||
};
|
||||
|
||||
const SUBOPTION_NAMES: Record<number, SuboptionType> = {
|
||||
1: 'circuit-id',
|
||||
2: 'remote-id',
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert string to hex based on format
|
||||
*/
|
||||
function encodeValue(value: string, format: EncodingFormat): string {
|
||||
switch (format) {
|
||||
case 'hex':
|
||||
// Already hex, validate and clean
|
||||
return value.replace(/[^0-9a-fA-F]/g, '');
|
||||
|
||||
case 'ascii':
|
||||
// Convert ASCII to hex
|
||||
return Array.from(value)
|
||||
.map((c) => c.charCodeAt(0).toString(16).padStart(2, '0'))
|
||||
.join('');
|
||||
|
||||
case 'vlan-id': {
|
||||
// VLAN ID format: 2 bytes for VLAN (0-4095)
|
||||
const vlanId = parseInt(value, 10);
|
||||
if (isNaN(vlanId) || vlanId < 0 || vlanId > 4095) {
|
||||
return '0000';
|
||||
}
|
||||
return vlanId.toString(16).padStart(4, '0');
|
||||
}
|
||||
|
||||
case 'hostname-port': {
|
||||
// Format: hostname:port as ASCII
|
||||
return Array.from(value)
|
||||
.map((c) => c.charCodeAt(0).toString(16).padStart(2, '0'))
|
||||
.join('');
|
||||
}
|
||||
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode hex value to readable string based on format
|
||||
*/
|
||||
function decodeValue(hex: string, format: EncodingFormat): string {
|
||||
switch (format) {
|
||||
case 'hex':
|
||||
return hex;
|
||||
|
||||
case 'ascii':
|
||||
case 'hostname-port': {
|
||||
let result = '';
|
||||
for (let i = 0; i < hex.length; i += 2) {
|
||||
const byte = hex.substring(i, i + 2);
|
||||
result += String.fromCharCode(parseInt(byte, 16));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
case 'vlan-id': {
|
||||
const vlanId = parseInt(hex, 16);
|
||||
return vlanId.toString();
|
||||
}
|
||||
|
||||
default:
|
||||
return hex;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build Option 82 hex string from suboptions
|
||||
*/
|
||||
export function buildOption82(config: Option82Config): Option82Result {
|
||||
const breakdown: SuboptionBreakdown[] = [];
|
||||
const hexParts: string[] = [];
|
||||
|
||||
for (const suboption of config.suboptions) {
|
||||
const typeCode = SUBOPTION_CODES[suboption.type];
|
||||
const hexValue = encodeValue(suboption.value, suboption.format);
|
||||
const length = hexValue.length / 2; // Length in bytes
|
||||
|
||||
// TLV format: Type (1 byte) + Length (1 byte) + Value (n bytes)
|
||||
const typeHex = typeCode.toString(16).padStart(2, '0');
|
||||
const lengthHex = length.toString(16).padStart(2, '0');
|
||||
|
||||
hexParts.push(typeHex + lengthHex + hexValue);
|
||||
|
||||
breakdown.push({
|
||||
type: suboption.type,
|
||||
typeCode,
|
||||
length,
|
||||
value: suboption.value,
|
||||
hexValue,
|
||||
description: getSuboptionDescription(suboption.type, suboption.format),
|
||||
});
|
||||
}
|
||||
|
||||
const hexEncoded = hexParts.join('');
|
||||
const asciiDecoded = breakdown.map((b) => `${b.type}: ${b.value}`).join(', ');
|
||||
|
||||
const examples = generateExamples(config, hexEncoded);
|
||||
|
||||
return {
|
||||
hexEncoded,
|
||||
asciiDecoded,
|
||||
breakdown,
|
||||
examples,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse Option 82 hex string
|
||||
*/
|
||||
export function parseOption82(hexString: string): ParsedOption82 {
|
||||
const cleanHex = hexString.replace(/[^0-9a-fA-F]/g, '');
|
||||
const suboptions: SuboptionBreakdown[] = [];
|
||||
let offset = 0;
|
||||
|
||||
while (offset < cleanHex.length) {
|
||||
// Read Type (1 byte)
|
||||
const typeHex = cleanHex.substring(offset, offset + 2);
|
||||
const typeCode = parseInt(typeHex, 16);
|
||||
offset += 2;
|
||||
|
||||
if (offset >= cleanHex.length) break;
|
||||
|
||||
// Read Length (1 byte)
|
||||
const lengthHex = cleanHex.substring(offset, offset + 2);
|
||||
const length = parseInt(lengthHex, 16);
|
||||
offset += 2;
|
||||
|
||||
if (offset + length * 2 > cleanHex.length) break;
|
||||
|
||||
// Read Value (length bytes)
|
||||
const hexValue = cleanHex.substring(offset, offset + length * 2);
|
||||
offset += length * 2;
|
||||
|
||||
const type = SUBOPTION_NAMES[typeCode];
|
||||
if (!type) continue;
|
||||
|
||||
// Try to decode as ASCII
|
||||
const asciiValue = decodeValue(hexValue, 'ascii');
|
||||
|
||||
suboptions.push({
|
||||
type,
|
||||
typeCode,
|
||||
length,
|
||||
value: asciiValue,
|
||||
hexValue,
|
||||
description: getSuboptionDescription(type, 'ascii'),
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
suboptions,
|
||||
totalLength: offset / 2,
|
||||
rawHex: cleanHex,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get description for suboption type
|
||||
*/
|
||||
function getSuboptionDescription(type: SuboptionType, _format: EncodingFormat): string {
|
||||
const descriptions: Record<SuboptionType, string> = {
|
||||
'circuit-id': 'Identifies the circuit (interface/VLAN) that received the DHCP packet',
|
||||
'remote-id': 'Identifies the relay agent itself (hostname, MAC, or ID)',
|
||||
};
|
||||
|
||||
return descriptions[type];
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate configuration examples for different DHCP servers
|
||||
*/
|
||||
function generateExamples(config: Option82Config, _hexEncoded: string): Option82Result['examples'] {
|
||||
const examples: Option82Result['examples'] = {};
|
||||
|
||||
// ISC dhcpd example
|
||||
const iscLines: string[] = [];
|
||||
iscLines.push('# Match clients based on Option 82 relay agent information');
|
||||
iscLines.push('class "option82-matched" {');
|
||||
|
||||
for (const suboption of config.suboptions) {
|
||||
if (suboption.type === 'circuit-id') {
|
||||
iscLines.push(` match if binary-to-ascii(16, 8, ":", option agent.circuit-id) = "${suboption.value}";`);
|
||||
} else if (suboption.type === 'remote-id') {
|
||||
iscLines.push(` match if binary-to-ascii(16, 8, ":", option agent.remote-id) = "${suboption.value}";`);
|
||||
}
|
||||
}
|
||||
|
||||
iscLines.push('}');
|
||||
iscLines.push('');
|
||||
iscLines.push('# Pool restricted to matched clients');
|
||||
iscLines.push('subnet 192.168.1.0 netmask 255.255.255.0 {');
|
||||
iscLines.push(' pool {');
|
||||
iscLines.push(' range 192.168.1.100 192.168.1.200;');
|
||||
iscLines.push(' allow members of "option82-matched";');
|
||||
iscLines.push(' }');
|
||||
iscLines.push('}');
|
||||
|
||||
examples.iscDhcpd = iscLines.join('\n');
|
||||
|
||||
// Kea DHCPv4 example
|
||||
const keaLines: string[] = [];
|
||||
keaLines.push('// Client class based on Option 82');
|
||||
keaLines.push('{');
|
||||
keaLines.push(' "client-classes": [');
|
||||
keaLines.push(' {');
|
||||
keaLines.push(' "name": "option82-matched",');
|
||||
|
||||
for (const suboption of config.suboptions) {
|
||||
const subCode = SUBOPTION_CODES[suboption.type];
|
||||
keaLines.push(` "test": "relay4[${subCode}].hex == 0x${encodeValue(suboption.value, suboption.format)}",`);
|
||||
}
|
||||
|
||||
keaLines.push(' "only-if-required": true');
|
||||
keaLines.push(' }');
|
||||
keaLines.push(' ],');
|
||||
keaLines.push(' "subnet4": [');
|
||||
keaLines.push(' {');
|
||||
keaLines.push(' "subnet": "192.168.1.0/24",');
|
||||
keaLines.push(' "pools": [');
|
||||
keaLines.push(' {');
|
||||
keaLines.push(' "pool": "192.168.1.100 - 192.168.1.200",');
|
||||
keaLines.push(' "client-class": "option82-matched"');
|
||||
keaLines.push(' }');
|
||||
keaLines.push(' ]');
|
||||
keaLines.push(' }');
|
||||
keaLines.push(' ]');
|
||||
keaLines.push('}');
|
||||
|
||||
examples.keaDhcp4 = keaLines.join('\n');
|
||||
|
||||
// Cisco relay agent example
|
||||
const ciscoLines: string[] = [];
|
||||
ciscoLines.push('! Enable DHCP relay agent information option');
|
||||
ciscoLines.push('interface GigabitEthernet0/1');
|
||||
ciscoLines.push(' ip address 192.168.1.1 255.255.255.0');
|
||||
ciscoLines.push(' ip helper-address 192.168.100.10');
|
||||
ciscoLines.push(' !');
|
||||
|
||||
for (const suboption of config.suboptions) {
|
||||
if (suboption.type === 'circuit-id' && suboption.format === 'vlan-id') {
|
||||
ciscoLines.push(` ! Circuit-ID will be automatically set based on VLAN ${suboption.value}`);
|
||||
} else if (suboption.type === 'remote-id') {
|
||||
ciscoLines.push(` ip dhcp relay information option-insert`);
|
||||
}
|
||||
}
|
||||
|
||||
ciscoLines.push('!');
|
||||
ciscoLines.push('! Trust Option 82 from downstream relays');
|
||||
ciscoLines.push('ip dhcp relay information trust-all');
|
||||
ciscoLines.push('!');
|
||||
ciscoLines.push('! Policy to customize Option 82');
|
||||
ciscoLines.push('ip dhcp relay information policy-action replace');
|
||||
|
||||
examples.ciscoRelay = ciscoLines.join('\n');
|
||||
|
||||
return examples;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default config
|
||||
*/
|
||||
export function getDefaultOption82Config(): Option82Config {
|
||||
return {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'vlan-id',
|
||||
value: '100',
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Common VLAN formats
|
||||
*/
|
||||
export const VLAN_EXAMPLES = [
|
||||
{ label: 'VLAN 10 (Guests)', value: '10' },
|
||||
{ label: 'VLAN 20 (Corporate)', value: '20' },
|
||||
{ label: 'VLAN 100 (Servers)', value: '100' },
|
||||
{ label: 'VLAN 200 (VoIP)', value: '200' },
|
||||
];
|
||||
|
||||
/**
|
||||
* Common Circuit-ID examples
|
||||
*/
|
||||
export const CIRCUIT_ID_EXAMPLES = [
|
||||
{ label: 'Hostname:Port', value: 'sw1:Gi0/1', format: 'hostname-port' as EncodingFormat },
|
||||
{ label: 'VLAN ID', value: '100', format: 'vlan-id' as EncodingFormat },
|
||||
{ label: 'Custom ASCII', value: 'building-a-floor-3', format: 'ascii' as EncodingFormat },
|
||||
];
|
||||
|
||||
/**
|
||||
* Common Remote-ID examples
|
||||
*/
|
||||
export const REMOTE_ID_EXAMPLES = [
|
||||
{ label: 'Hostname', value: 'relay-sw1.example.com', format: 'ascii' as EncodingFormat },
|
||||
{ label: 'MAC Address', value: '001122334455', format: 'hex' as EncodingFormat },
|
||||
{ label: 'Agent ID', value: 'agent-001', format: 'ascii' as EncodingFormat },
|
||||
];
|
||||
@@ -0,0 +1,5 @@
|
||||
<script lang="ts">
|
||||
import DHCPOption82Builder from '$lib/components/tools/DHCPOption82Builder.svelte';
|
||||
</script>
|
||||
|
||||
<DHCPOption82Builder />
|
||||
@@ -0,0 +1,452 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
buildOption82,
|
||||
parseOption82,
|
||||
getDefaultOption82Config,
|
||||
type Option82Config,
|
||||
type EncodingFormat,
|
||||
} from '$lib/utils/dhcp-option82';
|
||||
|
||||
describe('DHCP Option 82', () => {
|
||||
describe('getDefaultOption82Config', () => {
|
||||
it('should return default configuration', () => {
|
||||
const config = getDefaultOption82Config();
|
||||
expect(config).toBeDefined();
|
||||
expect(config.suboptions).toHaveLength(1);
|
||||
expect(config.suboptions[0].type).toBe('circuit-id');
|
||||
expect(config.suboptions[0].format).toBe('vlan-id');
|
||||
expect(config.suboptions[0].value).toBe('100');
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildOption82', () => {
|
||||
it('should build option 82 with ASCII circuit-id', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'ascii',
|
||||
value: 'Gi0/1',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.hexEncoded).toBeTruthy();
|
||||
expect(result.breakdown).toHaveLength(1);
|
||||
expect(result.breakdown[0].type).toBe('circuit-id');
|
||||
expect(result.breakdown[0].typeCode).toBe(1);
|
||||
expect(result.breakdown[0].value).toBe('Gi0/1');
|
||||
});
|
||||
|
||||
it('should build option 82 with VLAN ID', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'vlan-id',
|
||||
value: '100',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.hexEncoded).toBeTruthy();
|
||||
expect(result.breakdown[0].hexValue).toBe('0064');
|
||||
expect(result.breakdown[0].length).toBe(2);
|
||||
});
|
||||
|
||||
it('should build option 82 with multiple suboptions', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'ascii',
|
||||
value: 'Gi0/1',
|
||||
},
|
||||
{
|
||||
type: 'remote-id',
|
||||
format: 'ascii',
|
||||
value: 'sw1.example',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.breakdown).toHaveLength(2);
|
||||
expect(result.breakdown[0].typeCode).toBe(1);
|
||||
expect(result.breakdown[1].typeCode).toBe(2);
|
||||
});
|
||||
|
||||
it('should handle hex format', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'remote-id',
|
||||
format: 'hex',
|
||||
value: '001122334455',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.breakdown[0].hexValue).toBe('001122334455');
|
||||
});
|
||||
|
||||
it('should generate ISC dhcpd examples', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'ascii',
|
||||
value: 'Gi0/1',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.examples.iscDhcpd).toBeDefined();
|
||||
expect(result.examples.iscDhcpd).toContain('match if');
|
||||
expect(result.examples.iscDhcpd).toContain('agent.circuit-id');
|
||||
});
|
||||
|
||||
it('should generate Kea DHCPv4 examples', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'ascii',
|
||||
value: 'Gi0/1',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.examples.keaDhcp4).toBeDefined();
|
||||
expect(result.examples.keaDhcp4).toContain('relay4');
|
||||
expect(result.examples.keaDhcp4).toContain('client-classes');
|
||||
});
|
||||
|
||||
it('should generate Cisco relay examples', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'vlan-id',
|
||||
value: '100',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.examples.ciscoRelay).toBeDefined();
|
||||
expect(result.examples.ciscoRelay).toContain('ip helper-address');
|
||||
});
|
||||
|
||||
it('should handle hostname:port format', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'hostname-port',
|
||||
value: 'sw1:Gi0/1',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.breakdown[0].value).toBe('sw1:Gi0/1');
|
||||
expect(result.breakdown[0].hexValue.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('should handle invalid VLAN ID gracefully', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'vlan-id',
|
||||
value: '9999', // Out of range
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.breakdown[0].hexValue).toBe('0000');
|
||||
});
|
||||
|
||||
it('should clean hex input', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'remote-id',
|
||||
format: 'hex',
|
||||
value: '00:11:22:33:44:55', // With colons
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.breakdown[0].hexValue).toBe('001122334455');
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseOption82', () => {
|
||||
it('should parse circuit-id suboption', () => {
|
||||
// TLV: Type=01, Length=05, Value="Gi0/1" (4769302f31 in hex)
|
||||
const hex = '01054769302f31';
|
||||
const result = parseOption82(hex);
|
||||
|
||||
expect(result.suboptions).toHaveLength(1);
|
||||
expect(result.suboptions[0].type).toBe('circuit-id');
|
||||
expect(result.suboptions[0].typeCode).toBe(1);
|
||||
expect(result.suboptions[0].length).toBe(5);
|
||||
expect(result.suboptions[0].value).toBe('Gi0/1');
|
||||
});
|
||||
|
||||
it('should parse remote-id suboption', () => {
|
||||
// TLV: Type=02, Length=0b, Value="sw1.example" (7377312e6578616d706c65 in hex, 11 bytes)
|
||||
const hex = '020b7377312e6578616d706c65';
|
||||
const result = parseOption82(hex);
|
||||
|
||||
expect(result.suboptions).toHaveLength(1);
|
||||
expect(result.suboptions[0].type).toBe('remote-id');
|
||||
expect(result.suboptions[0].typeCode).toBe(2);
|
||||
expect(result.suboptions[0].value).toBe('sw1.example');
|
||||
});
|
||||
|
||||
it('should parse multiple suboptions', () => {
|
||||
// Circuit-ID + Remote-ID
|
||||
const hex = '01054769302f31' + '020b7377312e6578616d706c65';
|
||||
const result = parseOption82(hex);
|
||||
|
||||
expect(result.suboptions).toHaveLength(2);
|
||||
expect(result.suboptions[0].type).toBe('circuit-id');
|
||||
expect(result.suboptions[1].type).toBe('remote-id');
|
||||
});
|
||||
|
||||
it('should handle hex with spaces', () => {
|
||||
const hex = '01 05 47 69 30 2f 31';
|
||||
const result = parseOption82(hex);
|
||||
|
||||
expect(result.suboptions).toHaveLength(1);
|
||||
expect(result.suboptions[0].value).toBe('Gi0/1');
|
||||
});
|
||||
|
||||
it('should calculate total length correctly', () => {
|
||||
const hex = '01054769302f31020b7377312e6578616d706c65';
|
||||
const result = parseOption82(hex);
|
||||
|
||||
expect(result.totalLength).toBe(20); // 1+1+5 + 1+1+11
|
||||
});
|
||||
|
||||
it('should handle truncated data', () => {
|
||||
// Type and length present, but value missing
|
||||
const hex = '0105';
|
||||
const result = parseOption82(hex);
|
||||
|
||||
expect(result.suboptions).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should skip unknown suboption types', () => {
|
||||
// Type=99 (unknown), Length=04, Value=00000000, then Type=01 (known)
|
||||
const hex = '990400000000' + '01054769302f31';
|
||||
const result = parseOption82(hex);
|
||||
|
||||
// Only circuit-id should be parsed (type 01)
|
||||
expect(result.suboptions.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('should handle empty input', () => {
|
||||
const hex = '';
|
||||
const result = parseOption82(hex);
|
||||
|
||||
expect(result.suboptions).toHaveLength(0);
|
||||
expect(result.totalLength).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle VLAN ID suboption', () => {
|
||||
// Type=01, Length=02, Value=0064 (100 decimal)
|
||||
const hex = '0102006 4';
|
||||
const result = parseOption82(hex);
|
||||
|
||||
expect(result.suboptions).toHaveLength(1);
|
||||
expect(result.suboptions[0].hexValue).toBe('0064');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Round-trip encoding/decoding', () => {
|
||||
it('should preserve ASCII data through round-trip', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'ascii',
|
||||
value: 'Gi0/1',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const built = buildOption82(config);
|
||||
const parsed = parseOption82(built.hexEncoded);
|
||||
|
||||
expect(parsed.suboptions[0].value).toBe('Gi0/1');
|
||||
});
|
||||
|
||||
it('should preserve multiple suboptions through round-trip', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'ascii',
|
||||
value: 'Gi0/1',
|
||||
},
|
||||
{
|
||||
type: 'remote-id',
|
||||
format: 'ascii',
|
||||
value: 'switch-1',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const built = buildOption82(config);
|
||||
const parsed = parseOption82(built.hexEncoded);
|
||||
|
||||
expect(parsed.suboptions).toHaveLength(2);
|
||||
expect(parsed.suboptions[0].value).toBe('Gi0/1');
|
||||
expect(parsed.suboptions[1].value).toBe('switch-1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Edge cases', () => {
|
||||
it('should handle empty suboption value', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'ascii',
|
||||
value: '',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.breakdown[0].length).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle special characters in ASCII', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'ascii',
|
||||
value: 'Port-1/2.3',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.breakdown[0].value).toBe('Port-1/2.3');
|
||||
});
|
||||
|
||||
it('should handle maximum VLAN ID', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'vlan-id',
|
||||
value: '4095',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.breakdown[0].hexValue).toBe('0fff');
|
||||
});
|
||||
|
||||
it('should handle minimum VLAN ID', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'vlan-id',
|
||||
value: '0',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.breakdown[0].hexValue).toBe('0000');
|
||||
});
|
||||
|
||||
it('should handle long ASCII values', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'remote-id',
|
||||
format: 'ascii',
|
||||
value: 'very-long-switch-hostname.example.com',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.breakdown[0].value).toBe('very-long-switch-hostname.example.com');
|
||||
expect(result.breakdown[0].length).toBeGreaterThan(10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Description and metadata', () => {
|
||||
it('should include descriptions for circuit-id', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'ascii',
|
||||
value: 'Gi0/1',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.breakdown[0].description).toBeDefined();
|
||||
expect(result.breakdown[0].description).toContain('circuit');
|
||||
});
|
||||
|
||||
it('should include descriptions for remote-id', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'remote-id',
|
||||
format: 'ascii',
|
||||
value: 'switch-1',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.breakdown[0].description).toBeDefined();
|
||||
expect(result.breakdown[0].description).toContain('relay');
|
||||
});
|
||||
|
||||
it('should generate ascii decoded summary', () => {
|
||||
const config: Option82Config = {
|
||||
suboptions: [
|
||||
{
|
||||
type: 'circuit-id',
|
||||
format: 'ascii',
|
||||
value: 'Gi0/1',
|
||||
},
|
||||
{
|
||||
type: 'remote-id',
|
||||
format: 'ascii',
|
||||
value: 'sw1',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = buildOption82(config);
|
||||
expect(result.asciiDecoded).toContain('circuit-id: Gi0/1');
|
||||
expect(result.asciiDecoded).toContain('remote-id: sw1');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user