From 1a6263d260a464cab4bd6d6ab501745a25c477ef Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sun, 19 Oct 2025 23:56:32 +0100 Subject: [PATCH] Freeform TLV Composer --- .../components/tools/DHCPv6DNSBuilder.svelte | 763 ++++++++++++++++++ src/lib/constants/nav.ts | 34 + src/lib/utils/dhcpv6-dns-rfc3646.ts | 314 +++++++ .../options/dns-search-rfc3646/+page.svelte | 5 + tests/unit/utils/dhcpv6-dns-rfc3646.test.ts | 228 ++++++ 5 files changed, 1344 insertions(+) create mode 100644 src/lib/components/tools/DHCPv6DNSBuilder.svelte create mode 100644 src/lib/utils/dhcpv6-dns-rfc3646.ts create mode 100644 src/routes/dhcp/v6/options/dns-search-rfc3646/+page.svelte create mode 100644 tests/unit/utils/dhcpv6-dns-rfc3646.test.ts diff --git a/src/lib/components/tools/DHCPv6DNSBuilder.svelte b/src/lib/components/tools/DHCPv6DNSBuilder.svelte new file mode 100644 index 0000000..aef2db2 --- /dev/null +++ b/src/lib/components/tools/DHCPv6DNSBuilder.svelte @@ -0,0 +1,763 @@ + + + + ex.label} + getDescription={(ex) => ex.description} + selectedIndex={selectedExampleIndex} + /> + +
+
+

Option 23: DNS Recursive Name Servers

+

IPv6 addresses of DNS servers for client name resolution

+
+
+ {#each config.dnsServers as _, i (`dns-${i}`)} +
+
+ + +
+ +
+ {/each} + + +
+
+ +
+
+

Option 24: Domain Search List

+

DNS search domains for hostname resolution

+
+
+ {#each config.searchDomains as _, i (`domain-${i}`)} +
+
+ + +
+ +
+ {/each} + + +
+
+ + {#if validationErrors.length > 0} +
+

Validation Errors

+ {#each validationErrors as error, i (i)} +
+ + {error} +
+ {/each} +
+ {/if} + + {#if result && validationErrors.length === 0} + {#if result.option23} +
+

Option 23: DNS Recursive Name Servers

+ +
+
Total Length: {result.option23.totalLength} bytes
+
Servers: {result.option23.servers.length}
+
+ +
+

DNS Servers

+ {#each result.option23.servers as server, i (i)} +
+ + Server {i + 1}: + {server} +
+ {/each} +
+ +
+
+

Hex-Encoded (Compact)

+ +
+
{result.option23.hexEncoded}
+
+ +
+
+

Wire Format (Spaced)

+ +
+
{result.option23.wireFormat}
+
+
+ {/if} + + {#if result.option24} +
+

Option 24: Domain Search List

+ +
+
Total Length: {result.option24.totalLength} bytes
+
Domains: {result.option24.domains.length}
+
+ +
+

Search Domains

+ {#each result.option24.domains as domain, i (i)} +
+ + Domain {i + 1}: + {domain} +
+ {/each} +
+ +
+
+

Hex-Encoded (Compact)

+ +
+
{result.option24.hexEncoded}
+
+ +
+
+

Wire Format (Spaced)

+ +
+
{result.option24.wireFormat}
+
+ + {#if result.option24.breakdown.length > 0} +
+

Domain Encoding Breakdown

+ {#each result.option24.breakdown as item, i (i)} +
+
{item.domain}
+
{item.wireFormat}
+
+ {/each} +
+ {/if} +
+ {/if} + + {#if result.examples.keaDhcp6} +
+

Configuration Example

+ +
+
+

Kea DHCPv6 Configuration

+ +
+
{result.examples.keaDhcp6}
+
+
+ {/if} + +
+

About RFC 3646

+

+ RFC 3646 defines DNS configuration options for DHCPv6, allowing IPv6 clients to automatically discover DNS + servers and search domains. +

+
    +
  • + Option 23: DNS Recursive Name Server - List of IPv6 DNS server addresses (16 bytes each) +
  • +
  • + Option 24: Domain Search List - DNS search domains encoded in DNS wire format (length-prefixed + labels) +
  • +
+

+ These options are essential for IPv6 network autoconfiguration, enabling clients to resolve hostnames without + manual DNS configuration. +

+
+ {/if} +
+ + diff --git a/src/lib/constants/nav.ts b/src/lib/constants/nav.ts index 24d7d07..ddfe8c5 100644 --- a/src/lib/constants/nav.ts +++ b/src/lib/constants/nav.ts @@ -1089,6 +1089,40 @@ export const SUB_NAV: Record = { 'local', ], }, + { + href: makePath('/dhcp/v6/options/dns-search-rfc3646'), + label: 'DHCPv6 Options 23/24 - DNS', + description: 'Configure DNS servers (Option 23) and search domains (Option 24) for DHCPv6 clients per RFC 3646', + icon: 'globe', + keywords: [ + 'dhcpv6', + 'dhcp', + 'v6', + 'ipv6', + 'option', + '23', + '24', + 'dns', + 'recursive', + 'name', + 'server', + 'domain', + 'search', + 'list', + 'rfc', + '3646', + 'wire', + 'format', + 'kea', + 'configuration', + 'stateless', + 'stateful', + 'slaac', + 'autoconfiguration', + 'resolver', + 'suffix', + ], + }, ], '/dns': [ { diff --git a/src/lib/utils/dhcpv6-dns-rfc3646.ts b/src/lib/utils/dhcpv6-dns-rfc3646.ts new file mode 100644 index 0000000..c264c1c --- /dev/null +++ b/src/lib/utils/dhcpv6-dns-rfc3646.ts @@ -0,0 +1,314 @@ +/** + * DHCPv6 DNS Options (RFC 3646) + * + * Option 23: DNS Recursive Name Server + * Option 24: Domain Search List + * + * Allows configuration of DNS servers and search domains for DHCPv6 clients. + */ + +export interface DNSv6Config { + dnsServers: string[]; + searchDomains: string[]; +} + +export interface DNSv6Result { + option23?: { + servers: string[]; + hexEncoded: string; + wireFormat: string; + totalLength: number; + }; + option24?: { + domains: string[]; + hexEncoded: string; + wireFormat: string; + totalLength: number; + breakdown: Array<{ + domain: string; + hex: string; + wireFormat: string; + }>; + }; + examples: { + keaDhcp6?: string; + }; +} + +/** + * Validates an IPv6 address + */ +function validateIPv6(ip: string): boolean { + // Comprehensive IPv6 validation + const ipv6Regex = /^([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}$/; + const ipv6CompressedRegex = /^::([0-9a-fA-F]{0,4}:){0,6}[0-9a-fA-F]{0,4}$/; + const ipv6FullCompressedRegex = /^::$/; + const ipv6MixedRegex = /^([0-9a-fA-F]{0,4}:){6}\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; + + return ( + ipv6Regex.test(ip) || ipv6CompressedRegex.test(ip) || ipv6FullCompressedRegex.test(ip) || ipv6MixedRegex.test(ip) + ); +} + +/** + * Validates FQDN (Fully Qualified Domain Name) + */ +function validateFQDN(fqdn: string): boolean { + // Basic FQDN validation + const fqdnRegex = /^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/; + return fqdnRegex.test(fqdn) && fqdn.length <= 253; +} + +/** + * Converts IPv6 address to hex bytes (16 bytes) + */ +function ipv6ToHex(ip: string): string { + let expanded = ip; + + // Handle :: compression + if (ip.includes('::')) { + const parts = ip.split('::'); + const leftParts = parts[0] ? parts[0].split(':') : []; + const rightParts = parts[1] ? parts[1].split(':') : []; + const missingParts = 8 - leftParts.length - rightParts.length; + const middleParts = Array(missingParts).fill('0000'); + expanded = [...leftParts, ...middleParts, ...rightParts].join(':'); + } + + // Pad each segment to 4 hex digits + const segments = expanded.split(':').map((seg) => seg.padStart(4, '0')); + + // Convert to hex bytes + return segments.join(''); +} + +/** + * Converts a string to hex bytes (UTF-8 encoding) + */ +function stringToHex(str: string): string { + const encoder = new TextEncoder(); + const bytes = encoder.encode(str); + return Array.from(bytes) + .map((b) => b.toString(16).padStart(2, '0')) + .join(''); +} + +/** + * Converts FQDN to DNS wire format (length-prefixed labels) + */ +function fqdnToHex(fqdn: string): string { + const labels = fqdn.split('.'); + let hex = ''; + + for (const label of labels) { + const labelLength = label.length; + hex += labelLength.toString(16).padStart(2, '0'); + hex += stringToHex(label); + } + + // Add null terminator + hex += '00'; + + return hex; +} + +/** + * Formats hex string as wire format (with spaces) + */ +function formatWireFormat(hex: string): string { + return hex.match(/.{1,2}/g)?.join(' ') || ''; +} + +/** + * Validates DNS configuration + */ +export function validateDNSv6Config(config: DNSv6Config): string[] { + const errors: string[] = []; + + // Check that at least one option is configured + const hasDNSServers = config.dnsServers && config.dnsServers.some((s) => s.trim()); + const hasSearchDomains = config.searchDomains && config.searchDomains.some((d) => d.trim()); + + if (!hasDNSServers && !hasSearchDomains) { + errors.push('At least one DNS server or search domain must be configured'); + } + + // Validate DNS servers (Option 23) + if (config.dnsServers) { + for (let i = 0; i < config.dnsServers.length; i++) { + const server = config.dnsServers[i]; + if (!server.trim()) continue; + + if (!validateIPv6(server)) { + errors.push(`DNS Server ${i + 1}: Invalid IPv6 address`); + } + } + } + + // Validate search domains (Option 24) + if (config.searchDomains) { + for (let i = 0; i < config.searchDomains.length; i++) { + const domain = config.searchDomains[i]; + if (!domain.trim()) continue; + + if (!validateFQDN(domain)) { + errors.push(`Search Domain ${i + 1}: Invalid domain name format`); + } + } + } + + return errors; +} + +/** + * Builds DHCPv6 DNS options + */ +export function buildDNSv6Options(config: DNSv6Config): DNSv6Result { + const errors = validateDNSv6Config(config); + if (errors.length > 0) { + throw new Error(errors.join('; ')); + } + + const result: DNSv6Result = { + examples: {}, + }; + + // Build Option 23: DNS Recursive Name Server + if (config.dnsServers && config.dnsServers.length > 0) { + const validServers = config.dnsServers.filter((s) => s.trim()); + + if (validServers.length > 0) { + let serversHex = ''; + + for (const server of validServers) { + serversHex += ipv6ToHex(server); + } + + result.option23 = { + servers: validServers, + hexEncoded: serversHex, + wireFormat: formatWireFormat(serversHex), + totalLength: serversHex.length / 2, + }; + } + } + + // Build Option 24: Domain Search List + if (config.searchDomains && config.searchDomains.length > 0) { + const validDomains = config.searchDomains.filter((d) => d.trim()); + + if (validDomains.length > 0) { + let domainsHex = ''; + const breakdown: Array<{ domain: string; hex: string; wireFormat: string }> = []; + + for (const domain of validDomains) { + const domainHex = fqdnToHex(domain); + domainsHex += domainHex; + + breakdown.push({ + domain, + hex: domainHex, + wireFormat: formatWireFormat(domainHex), + }); + } + + result.option24 = { + domains: validDomains, + hexEncoded: domainsHex, + wireFormat: formatWireFormat(domainsHex), + totalLength: domainsHex.length / 2, + breakdown, + }; + } + } + + // Generate configuration examples + result.examples = generateConfigExamples(result); + + return result; +} + +/** + * Generates Kea DHCPv6 configuration examples + */ +function generateConfigExamples(result: DNSv6Result): { + keaDhcp6?: string; +} { + const optionData: string[] = []; + + // Add Option 23 if present + if (result.option23) { + optionData.push(` { + "name": "dns-servers", + "code": 23, + "data": "${result.option23.servers.join(', ')}" + }`); + } + + // Add Option 24 if present + if (result.option24) { + optionData.push(` { + "name": "domain-search", + "code": 24, + "data": "${result.option24.domains.join(', ')}" + }`); + } + + if (optionData.length === 0) { + return {}; + } + + const keaDhcp6 = `{ + "Dhcp6": { + "subnet6": [ + { + "subnet": "2001:db8::/64", + "pools": [ + { + "pool": "2001:db8::100 - 2001:db8::200" + } + ], + "option-data": [ +${optionData.join(',\n')} + ] + } + ] + } +}`; + + return { + keaDhcp6, + }; +} + +/** + * Returns a default DNS configuration + */ +export function getDefaultDNSv6Config(): DNSv6Config { + return { + dnsServers: [], + searchDomains: [], + }; +} + +/** + * Example DNS configurations + */ +export const DNSv6_EXAMPLES: DNSv6Config[] = [ + { + dnsServers: ['2001:4860:4860::8888', '2001:4860:4860::8844'], + searchDomains: ['example.com', 'example.org'], + }, + { + dnsServers: ['2606:4700:4700::1111', '2606:4700:4700::1001'], + searchDomains: ['local.example.com'], + }, + { + dnsServers: ['2620:fe::fe', '2620:fe::9'], + searchDomains: ['corp.example.com', 'internal.example.com', 'vpn.example.com'], + }, + { + dnsServers: ['fd00::1'], + searchDomains: ['home.arpa'], + }, +]; diff --git a/src/routes/dhcp/v6/options/dns-search-rfc3646/+page.svelte b/src/routes/dhcp/v6/options/dns-search-rfc3646/+page.svelte new file mode 100644 index 0000000..2b7640e --- /dev/null +++ b/src/routes/dhcp/v6/options/dns-search-rfc3646/+page.svelte @@ -0,0 +1,5 @@ + + + diff --git a/tests/unit/utils/dhcpv6-dns-rfc3646.test.ts b/tests/unit/utils/dhcpv6-dns-rfc3646.test.ts new file mode 100644 index 0000000..001ee34 --- /dev/null +++ b/tests/unit/utils/dhcpv6-dns-rfc3646.test.ts @@ -0,0 +1,228 @@ +import { describe, it, expect } from 'vitest'; +import { + validateDNSv6Config, + buildDNSv6Options, + getDefaultDNSv6Config, + type DNSv6Config, +} from '$lib/utils/dhcpv6-dns-rfc3646'; + +describe('dhcpv6-dns-rfc3646', () => { + describe('validateDNSv6Config', () => { + it('should require at least one DNS server or search domain', () => { + const emptyConfig: DNSv6Config = { + dnsServers: [], + searchDomains: [], + }; + const errors = validateDNSv6Config(emptyConfig); + expect(errors.length).toBeGreaterThan(0); + expect(errors[0]).toContain('At least one DNS server or search domain'); + }); + + it('should validate IPv6 DNS servers', () => { + const invalidConfig: DNSv6Config = { + dnsServers: ['not-an-ipv6'], + searchDomains: [], + }; + const errors = validateDNSv6Config(invalidConfig); + expect(errors.length).toBeGreaterThan(0); + expect(errors[0]).toContain('Invalid IPv6 address'); + }); + + it('should accept valid IPv6 addresses', () => { + const validConfig: DNSv6Config = { + dnsServers: ['2001:4860:4860::8888', '2001:4860:4860::8844'], + searchDomains: [], + }; + const errors = validateDNSv6Config(validConfig); + expect(errors).toEqual([]); + }); + + it('should accept compressed IPv6 addresses', () => { + const validConfig: DNSv6Config = { + dnsServers: ['::1', 'fe80::1', '2001:db8::1'], + searchDomains: [], + }; + const errors = validateDNSv6Config(validConfig); + expect(errors).toEqual([]); + }); + + it('should validate search domain format', () => { + const invalidConfig: DNSv6Config = { + dnsServers: [], + searchDomains: ['-invalid-.com'], + }; + const errors = validateDNSv6Config(invalidConfig); + expect(errors.length).toBeGreaterThan(0); + expect(errors[0]).toContain('Invalid domain name format'); + }); + + it('should accept valid FQDNs', () => { + const validConfig: DNSv6Config = { + dnsServers: [], + searchDomains: ['example.com', 'subdomain.example.org'], + }; + const errors = validateDNSv6Config(validConfig); + expect(errors).toEqual([]); + }); + + it('should skip empty entries in validation', () => { + const config: DNSv6Config = { + dnsServers: ['2001:4860:4860::8888', ''], + searchDomains: ['example.com', ''], + }; + const errors = validateDNSv6Config(config); + expect(errors).toEqual([]); + }); + }); + + describe('buildDNSv6Options', () => { + it('should build Option 23 for DNS servers', () => { + const config: DNSv6Config = { + dnsServers: ['2001:4860:4860::8888'], + searchDomains: [], + }; + const result = buildDNSv6Options(config); + expect(result.option23).toBeDefined(); + expect(result.option23!.servers).toEqual(['2001:4860:4860::8888']); + expect(result.option23!.totalLength).toBe(16); // IPv6 is 16 bytes + }); + + it('should encode IPv6 address correctly', () => { + const config: DNSv6Config = { + dnsServers: ['2001:db8::1'], + searchDomains: [], + }; + const result = buildDNSv6Options(config); + expect(result.option23!.hexEncoded).toBe('20010db8000000000000000000000001'); + }); + + it('should handle multiple DNS servers', () => { + const config: DNSv6Config = { + dnsServers: ['2001:4860:4860::8888', '2001:4860:4860::8844'], + searchDomains: [], + }; + const result = buildDNSv6Options(config); + expect(result.option23!.servers.length).toBe(2); + expect(result.option23!.totalLength).toBe(32); // 2 * 16 bytes + }); + + it('should build Option 24 for search domains', () => { + const config: DNSv6Config = { + dnsServers: [], + searchDomains: ['example.com'], + }; + const result = buildDNSv6Options(config); + expect(result.option24).toBeDefined(); + expect(result.option24!.domains).toEqual(['example.com']); + }); + + it('should encode FQDN in DNS wire format', () => { + const config: DNSv6Config = { + dnsServers: [], + searchDomains: ['example.com'], + }; + const result = buildDNSv6Options(config); + // 07 (length) + example + 03 (length) + com + 00 (null terminator) + expect(result.option24!.hexEncoded).toBe('076578616d706c6503636f6d00'); + }); + + it('should handle multiple search domains', () => { + const config: DNSv6Config = { + dnsServers: [], + searchDomains: ['example.com', 'example.org'], + }; + const result = buildDNSv6Options(config); + expect(result.option24!.domains.length).toBe(2); + expect(result.option24!.breakdown.length).toBe(2); + }); + + it('should build both options when provided', () => { + const config: DNSv6Config = { + dnsServers: ['2001:4860:4860::8888'], + searchDomains: ['example.com'], + }; + const result = buildDNSv6Options(config); + expect(result.option23).toBeDefined(); + expect(result.option24).toBeDefined(); + }); + + it('should generate Kea configuration examples', () => { + const config: DNSv6Config = { + dnsServers: ['2001:4860:4860::8888'], + searchDomains: ['example.com'], + }; + const result = buildDNSv6Options(config); + expect(result.examples.keaDhcp6).toBeDefined(); + expect(result.examples.keaDhcp6).toContain('"code": 23'); + expect(result.examples.keaDhcp6).toContain('"code": 24'); + expect(result.examples.keaDhcp6).toContain('dns-servers'); + expect(result.examples.keaDhcp6).toContain('domain-search'); + }); + + it('should generate Kea config with only DNS servers', () => { + const config: DNSv6Config = { + dnsServers: ['2001:4860:4860::8888'], + searchDomains: [], + }; + const result = buildDNSv6Options(config); + expect(result.examples.keaDhcp6).toBeDefined(); + expect(result.examples.keaDhcp6).toContain('"code": 23'); + expect(result.examples.keaDhcp6).not.toContain('"code": 24'); + }); + + it('should generate Kea config with only search domains', () => { + const config: DNSv6Config = { + dnsServers: [], + searchDomains: ['example.com'], + }; + const result = buildDNSv6Options(config); + expect(result.examples.keaDhcp6).toBeDefined(); + expect(result.examples.keaDhcp6).not.toContain('"code": 23'); + expect(result.examples.keaDhcp6).toContain('"code": 24'); + }); + + it('should format wire format with spaces', () => { + const config: DNSv6Config = { + dnsServers: ['::1'], + searchDomains: [], + }; + const result = buildDNSv6Options(config); + expect(result.option23!.wireFormat).toContain(' '); + expect(result.option23!.wireFormat.split(' ').length).toBe(16); + }); + + it('should throw error for invalid configuration', () => { + const invalidConfig: DNSv6Config = { + dnsServers: ['invalid-ipv6'], + searchDomains: [], + }; + expect(() => buildDNSv6Options(invalidConfig)).toThrow(); + }); + + it('should skip empty DNS servers', () => { + const config: DNSv6Config = { + dnsServers: ['2001:4860:4860::8888', '', '2001:4860:4860::8844'], + searchDomains: [], + }; + const result = buildDNSv6Options(config); + expect(result.option23!.servers.length).toBe(2); + }); + + it('should skip empty search domains', () => { + const config: DNSv6Config = { + dnsServers: [], + searchDomains: ['example.com', '', 'example.org'], + }; + const result = buildDNSv6Options(config); + expect(result.option24!.domains.length).toBe(2); + }); + }); + + describe('getDefaultDNSv6Config', () => { + it('should return default configuration', () => { + const defaultConfig = getDefaultDNSv6Config(); + expect(defaultConfig.dnsServers).toEqual([]); + expect(defaultConfig.searchDomains).toEqual([]); + }); + }); +});