diff --git a/src/lib/components/furniture/TopNav.svelte b/src/lib/components/furniture/TopNav.svelte index eb604b1..accfb17 100644 --- a/src/lib/components/furniture/TopNav.svelte +++ b/src/lib/components/furniture/TopNav.svelte @@ -40,7 +40,7 @@ secondaryDropdownPositions = { ...secondaryDropdownPositions, - [title]: spaceOnRight < 330 ? 'left' : 'right' + [title]: spaceOnRight < 330 ? 'left' : 'right', }; }); }); diff --git a/src/lib/components/tools/DHCPOption43Generator.svelte b/src/lib/components/tools/DHCPOption43Generator.svelte new file mode 100644 index 0000000..e47e226 --- /dev/null +++ b/src/lib/components/tools/DHCPOption43Generator.svelte @@ -0,0 +1,663 @@ + + + + VENDOR_INFO[ex.vendor].name} + getDescription={(ex) => ex.description} + getTooltip={(ex) => `Generate Option 43 for ${VENDOR_INFO[ex.vendor].name}`} +/> + + +
+
+

Generator Configuration

+
+
+
+
+ + + {vendorInfo.description} +
+ +
+ + +
+ +
+
+ + {#if errors.length > 0} +
+ {#each errors as error, i (i)} +
+ + {error} +
+ {/each} +
+ {/if} +
+
+
+ +{#if result} +
+

Generated Option 43 Values

+ + {#if result.iosCommand && result.workings} +
+
+

+ + {result.commandLabel || 'DHCP Server Command'} +

+ +
+
{result.iosCommand}
+ +
+
How this value is calculated:
+
    + {#each result.workings as working, i (i)} +
  • {working}
  • + {/each} +
+
+
+ {/if} + +
+ +

{result.explanation}

+
+ +
+
+
+

Hexadecimal String

+ +
+ {result.hex} +

Raw hexadecimal - used in most DHCP server configurations

+
+ +
+
+

Colon-Separated Hex

+ +
+ {result.colonHex} +

Used by Infoblox and some network appliances

+
+ +
+
+

Windows DHCP Binary

+ +
+ {result.windowsBinary} +

Enter in Windows DHCP Server's Binary field for Option 43

+
+ +
+
+

ISC DHCP Configuration

+ +
+
{result.iscDhcp}
+

Add to dhcpd.conf for ISC DHCP server

+
+ +
+
+

Mikrotik Configuration

+ +
+ {result.mikrotik} +

RouterOS DHCP option configuration command

+
+
+
+ +
+

Important Notes

+ +
+{/if} + + diff --git a/src/lib/constants/icon-map.ts b/src/lib/constants/icon-map.ts index 3b54651..53a61b8 100644 --- a/src/lib/constants/icon-map.ts +++ b/src/lib/constants/icon-map.ts @@ -306,6 +306,8 @@ export const iconMap: Record = { docker: ``, headset: ``, template: ``, + dhcp: ``, + router: ``, // 'mail-check': ``, // 'eye': ``, // 'star': ``, diff --git a/src/lib/constants/nav.ts b/src/lib/constants/nav.ts index 7a362ed..2f9a6e3 100644 --- a/src/lib/constants/nav.ts +++ b/src/lib/constants/nav.ts @@ -155,6 +155,30 @@ export const TOP_NAV: NavItem[] = [ 'domain', ], }, + { + href: makePath('/dhcp'), + label: 'DHCP', + description: 'DHCP option generators for wireless controllers and network configuration', + keywords: [ + 'dhcp', + 'option', + '43', + 'vendor', + 'specific', + 'wireless', + 'controller', + 'cisco', + 'aruba', + 'ruckus', + 'unifi', + 'ap', + 'discovery', + 'capwap', + 'lwapp', + 'configuration', + 'network', + ], + }, { href: makePath('/diagnostics'), label: 'Lookups', @@ -776,6 +800,38 @@ export const SUB_NAV: Record = { ], }, ], + '/dhcp': [ + { + href: makePath('/dhcp/option-43'), + label: 'Option 43 Generator', + description: 'Generate DHCP Option 43 for wireless controller discovery (Cisco, Aruba, Ruckus, UniFi)', + icon: 'router', + featured: true, + keywords: [ + 'dhcp', + 'option', + '43', + 'wireless', + 'controller', + 'cisco', + 'catalyst', + 'meraki', + 'aruba', + 'ruckus', + 'smartzone', + 'zonedirector', + 'unifi', + 'extreme', + 'vendor-specific', + 'ap', + 'discovery', + 'capwap', + 'lwapp', + 'access point', + 'wlc', + ], + }, + ], '/dns': [ { href: makePath('/dns/generators'), diff --git a/src/lib/utils/dhcp-option43.ts b/src/lib/utils/dhcp-option43.ts new file mode 100644 index 0000000..d53a25b --- /dev/null +++ b/src/lib/utils/dhcp-option43.ts @@ -0,0 +1,378 @@ +/** + * DHCP Option 43 - Vendor-Specific Information + * + * Utilities for generating DHCP Option 43 values for wireless controller discovery. + * Different vendors encode controller IP addresses in different formats. + */ + +export type VendorType = + | 'cisco-catalyst' + | 'cisco-meraki' + | 'ruckus-smartzone' + | 'ruckus-zonedirector' + | 'aruba' + | 'unifi' + | 'extreme' + | 'generic'; + +export interface Option43Result { + hex: string; + colonHex: string; + windowsBinary: string; + iscDhcp: string; + infoblox: string; + mikrotik: string; + explanation: string; + subOption?: number; + iosCommand?: string; + commandLabel?: string; + workings?: string[]; +} + +/** + * Convert IPv4 address to hexadecimal format + * Example: "192.168.10.5" -> "c0a80a05" + */ +export function ipToHex(ip: string): string { + return ip + .split('.') + .map((octet) => { + const num = parseInt(octet, 10); + return num.toString(16).padStart(2, '0'); + }) + .join(''); +} + +/** + * Convert IPv4 address to ASCII hexadecimal format (for Ruckus) + * Example: "192.168.10.5" -> "3139322e3136382e31302e35" + * Each character (including dots) is converted to its ASCII hex value + */ +export function ipToAsciiHex(ip: string): string { + return ip + .split('') + .map((char) => char.charCodeAt(0).toString(16).padStart(2, '0')) + .join(''); +} + +/** + * Convert hex string to colon-separated format + * Example: "f108c0a80a05" -> "f1:08:c0:a8:0a:05" + */ +export function hexToColonSeparated(hex: string): string { + return hex.match(/.{1,2}/g)?.join(':') || ''; +} + +/** + * Convert hex string to space-separated pairs for Windows DHCP + * Example: "f108c0a80a05" -> "f1 08 c0 a8 0a 05" + */ +export function hexToWindowsBinary(hex: string): string { + return hex.match(/.{1,2}/g)?.join(' ') || ''; +} + +/** + * Generate Cisco Option 43 (for Catalyst and legacy WLC) + * Format: Type (0xF1) + Length (num of IPs * 4) + IP addresses in hex + */ +export function generateCiscoOption43(ips: string[]): Option43Result { + const type = 'f1'; // Sub-option 241 + const length = (ips.length * 4).toString(16).padStart(2, '0'); + const ipHexValues = ips.map((ip) => ipToHex(ip)).join(''); + const hex = type + length + ipHexValues; + + const explanation = `Cisco uses sub-option 0xF1 (241). Length is ${ips.length} × 4 = ${ips.length * 4} bytes. IP addresses are encoded as hexadecimal octets.`; + + const workings = [ + `Option 43 static prefix for Cisco Wireless LAN Controllers: f1 (hex)`, + `IP count: ${ips.length}`, + `IP count × 4 bytes (hex): ${length}`, + ...ips.map((ip) => `IP ${ip} => ${ipToHex(ip)} (hex)`), + ]; + + return { + hex, + colonHex: hexToColonSeparated(hex), + windowsBinary: hexToWindowsBinary(hex), + iscDhcp: `option space Cisco_LWAPP_AP;\noption Cisco_LWAPP_AP.server-address code 241 = array of ip-address;\noption local-encapsulation code 43 = encapsulate Cisco_LWAPP_AP;\noption Cisco_LWAPP_AP.server-address ${ips.join(', ')};`, + infoblox: hexToColonSeparated(hex), + mikrotik: `option code=43 name=cisco-capwap value='${hex}'`, + explanation, + subOption: 241, + iosCommand: `ip dhcp pool \n option 43 hex ${hex}`, + commandLabel: 'Cisco IOS/IOS-XE DHCP Server', + workings, + }; +} + +/** + * Generate Cisco Meraki Option 43 + * Meraki uses a simple ASCII string format + */ +export function generateMerakiOption43(ips: string[]): Option43Result { + // Meraki uses comma-separated IP addresses in ASCII + const asciiString = ips.join(','); + const hex = asciiString + .split('') + .map((char) => char.charCodeAt(0).toString(16).padStart(2, '0')) + .join(''); + + const explanation = `Cisco Meraki uses ASCII-encoded comma-separated IP addresses. Each character is converted to hexadecimal.`; + + const workings = [ + `Cisco Meraki uses ASCII-encoded IP addresses`, + `IP count: ${ips.length}`, + `Concatenated IP string: ${asciiString} (length: ${asciiString.length} chars)`, + `Which translates to: ${hex} (hex)`, + ]; + + return { + hex, + colonHex: hexToColonSeparated(hex), + windowsBinary: hexToWindowsBinary(hex), + iscDhcp: `option vendor-encapsulated-options "${asciiString}";`, + infoblox: hexToColonSeparated(hex), + mikrotik: `option code=43 name=meraki value='"${asciiString}"'`, + explanation, + iosCommand: `ip dhcp pool \n option 43 hex ${hex}`, + commandLabel: 'Cisco IOS/IOS-XE DHCP Server', + workings, + }; +} + +/** + * Generate Ruckus Option 43 + * Format: Vendor code (03 or 06) + Length in hex + IP in ASCII hex + */ +export function generateRuckusOption43(ips: string[], type: 'smartzone' | 'zonedirector'): Option43Result { + const vendorCode = type === 'smartzone' ? '06' : '03'; + const ip = ips[0]; // Ruckus typically uses single controller + const asciiHex = ipToAsciiHex(ip); + const length = (asciiHex.length / 2).toString(16).padStart(2, '0'); + const hex = vendorCode + length + asciiHex; + + const explanation = `Ruckus uses vendor code ${vendorCode} (${type === 'smartzone' ? 'SmartZone' : 'ZoneDirector'}). Length is ${asciiHex.length / 2} bytes. IP address is encoded as ASCII characters converted to hexadecimal.`; + + const vendorName = type === 'smartzone' ? 'Ruckus Wireless (SmartZone)' : 'Ruckus Wireless (ZoneDirector)'; + const workings = [ + `Option 43 static prefix for ${vendorName}: ${vendorCode} (hex)`, + `IP count: 1`, + `Concatenated IP string: ${ip} (length: ${ip.length}/0x${ip.length.toString(16).padStart(2, '0')} chars)`, + `Which translates to: ${asciiHex} (hex)`, + ]; + + // Ruckus controllers can act as DHCP servers + const ruckusCommand = + type === 'smartzone' + ? `config\n dhcp-profile \n option43 sub-option ${parseInt(vendorCode, 16)} type string ${ip}\n end` + : `config\n dhcp-profile \n option43 sub-option ${parseInt(vendorCode, 16)} type string ${ip}\n end`; + + return { + hex, + colonHex: hexToColonSeparated(hex), + windowsBinary: hexToWindowsBinary(hex), + iscDhcp: `option vendor-class-identifier "Ruckus CPE";\noption vendor-encapsulated-options ${hex};`, + infoblox: hexToColonSeparated(hex), + mikrotik: `option code=43 name=ruckus value='${hex}'`, + explanation, + subOption: parseInt(vendorCode, 16), + iosCommand: ruckusCommand, + commandLabel: type === 'smartzone' ? 'Ruckus SmartZone CLI' : 'Ruckus ZoneDirector CLI', + workings, + }; +} + +/** + * Generate Aruba Option 43 + * Aruba uses simple ASCII IP address format + */ +export function generateArubaOption43(ips: string[]): Option43Result { + const ip = ips[0]; // Aruba typically uses single controller + const hex = ipToAsciiHex(ip); + + const explanation = `Aruba uses ASCII-encoded IP address. The IP ${ip} is converted directly to hexadecimal representation of ASCII characters.`; + + const workings = [ + `Aruba uses ASCII-encoded IP address`, + `IP count: 1`, + `IP string: ${ip} (length: ${ip.length} chars)`, + `Which translates to: ${hex} (hex)`, + ]; + + return { + hex, + colonHex: hexToColonSeparated(hex), + windowsBinary: hexToWindowsBinary(hex), + iscDhcp: `option vendor-class-identifier "ArubaAP";\noption vendor-encapsulated-options "${ip}";`, + infoblox: hexToColonSeparated(hex), + mikrotik: `option code=43 name=aruba value='"${ip}"'`, + explanation, + iosCommand: `ip dhcp \n option 43 hex ${hex}`, + commandLabel: 'Aruba OS Switch/Controller', + workings, + }; +} + +/** + * Generate Ubiquiti UniFi Option 43 + * Format: 01:04 + IP address in hex (single controller only) + */ +export function generateUniFiOption43(ip: string): Option43Result { + const prefix = '0104'; // Type 01, Length 04 + const ipHex = ipToHex(ip); + const hex = prefix + ipHex; + + const explanation = `UniFi uses sub-option 01 (type) with length 04 (4 bytes). IP address is encoded as hexadecimal octets. Note: Hex must use UPPERCASE in some configurations.`; + + const workings = [ + `UniFi uses sub-option 01 with length 04`, + `IP count: 1`, + `Sub-option prefix: 01 (type) + 04 (length in bytes)`, + `IP ${ip} => ${ipHex} (hex)`, + ]; + + return { + hex, + colonHex: hexToColonSeparated(hex), + windowsBinary: hexToWindowsBinary(hex), + iscDhcp: `option space ubnt;\noption ubnt.unifi-address code 1 = ip-address;\noption local-encapsulation code 43 = encapsulate ubnt;\noption ubnt.unifi-address ${ip};`, + infoblox: hexToColonSeparated(hex), + mikrotik: `option code=43 name=unifi value='${hex}'`, + explanation, + subOption: 1, + iosCommand: `set service dhcp-server shared-network-name subnet static-mapping static-mapping-parameters "option ubnt-unifi-address ${ip};"`, + commandLabel: 'UniFi EdgeRouter/USG', + workings, + }; +} + +/** + * Generate Extreme Networks (Aerohive) Option 43 + * Uses vendor class identifier and option 43 + */ +export function generateExtremeOption43(ips: string[]): Option43Result { + const ip = ips[0]; + const hex = ipToAsciiHex(ip); + + const explanation = `Extreme Networks (formerly Aerohive) uses vendor class identifier with the AP model. Option 43 contains ASCII-encoded controller IP.`; + + const workings = [ + `Extreme Networks uses ASCII-encoded IP address`, + `IP count: 1`, + `IP string: ${ip} (length: ${ip.length} chars)`, + `Which translates to: ${hex} (hex)`, + ]; + + return { + hex, + colonHex: hexToColonSeparated(hex), + windowsBinary: hexToWindowsBinary(hex), + iscDhcp: `option vendor-class-identifier "HiPath AP";\noption vendor-encapsulated-options "${ip}";`, + infoblox: hexToColonSeparated(hex), + mikrotik: `option code=43 name=extreme value='"${ip}"'`, + explanation, + iosCommand: `configure dhcp-server dhcp-option 43 hex ${hex}`, + commandLabel: 'Extreme Networks Switch', + workings, + }; +} + +/** + * Generate Option 43 for specified vendor + */ +export function generateOption43(vendor: VendorType, ips: string[]): Option43Result { + if (ips.length === 0) { + throw new Error('At least one IP address is required'); + } + + switch (vendor) { + case 'cisco-catalyst': + return generateCiscoOption43(ips); + case 'cisco-meraki': + return generateMerakiOption43(ips); + case 'ruckus-smartzone': + return generateRuckusOption43(ips, 'smartzone'); + case 'ruckus-zonedirector': + return generateRuckusOption43(ips, 'zonedirector'); + case 'aruba': + return generateArubaOption43(ips); + case 'unifi': + return generateUniFiOption43(ips[0]); + case 'extreme': + return generateExtremeOption43(ips); + case 'generic': + // Generic TLV format with custom sub-option + return generateCiscoOption43(ips); + default: + throw new Error(`Unsupported vendor: ${vendor}`); + } +} + +/** + * Validate IPv4 address format + */ +export function isValidIPv4(ip: string): boolean { + const octets = ip.split('.'); + if (octets.length !== 4) return false; + + return octets.every((octet) => { + const num = parseInt(octet, 10); + return !isNaN(num) && num >= 0 && num <= 255 && octet === num.toString(); + }); +} + +/** + * Parse and validate IP address list (comma or newline separated) + */ +export function parseIPList(input: string): string[] { + return input + .split(/[,\n]+/) + .map((ip) => ip.trim()) + .filter((ip) => ip.length > 0); +} + +/** + * Vendor display names and descriptions + */ +export const VENDOR_INFO: Record = { + 'cisco-catalyst': { + name: 'Cisco Catalyst / WLC', + description: 'Cisco Wireless LAN Controllers and Catalyst with embedded wireless', + maxIPs: 10, + }, + 'cisco-meraki': { + name: 'Cisco Meraki', + description: 'Cisco Meraki cloud-managed wireless access points', + maxIPs: 5, + }, + 'ruckus-smartzone': { + name: 'Ruckus SmartZone', + description: 'Ruckus SmartZone wireless controllers (vendor code 06)', + maxIPs: 1, + }, + 'ruckus-zonedirector': { + name: 'Ruckus ZoneDirector', + description: 'Ruckus ZoneDirector wireless controllers (vendor code 03)', + maxIPs: 1, + }, + aruba: { + name: 'Aruba / HP Aruba', + description: 'Aruba Networks wireless controllers', + maxIPs: 1, + }, + unifi: { + name: 'Ubiquiti UniFi', + description: 'Ubiquiti UniFi Network Controller for access points', + maxIPs: 1, + }, + extreme: { + name: 'Extreme Networks', + description: 'Extreme Networks (formerly Aerohive) wireless controllers', + maxIPs: 1, + }, + generic: { + name: 'Generic / Custom', + description: 'Generic TLV format with custom sub-option code', + maxIPs: 10, + }, +}; diff --git a/src/routes/dhcp/option-43/+page.svelte b/src/routes/dhcp/option-43/+page.svelte new file mode 100644 index 0000000..cffce1c --- /dev/null +++ b/src/routes/dhcp/option-43/+page.svelte @@ -0,0 +1,11 @@ + + + + + diff --git a/tests/unit/utils/dhcp-option43.test.ts b/tests/unit/utils/dhcp-option43.test.ts new file mode 100644 index 0000000..773cacd --- /dev/null +++ b/tests/unit/utils/dhcp-option43.test.ts @@ -0,0 +1,365 @@ +import { describe, it, expect } from 'vitest'; +import { + ipToHex, + ipToAsciiHex, + hexToColonSeparated, + hexToWindowsBinary, + generateCiscoOption43, + generateMerakiOption43, + generateRuckusOption43, + generateArubaOption43, + generateUniFiOption43, + generateExtremeOption43, + generateOption43, + isValidIPv4, + parseIPList, + type VendorType, +} from '$lib/utils/dhcp-option43'; + +describe('dhcp-option43 utilities', () => { + describe('ipToHex', () => { + it('should convert IPv4 address to hex', () => { + expect(ipToHex('192.168.1.10')).toBe('c0a8010a'); + expect(ipToHex('192.168.1.11')).toBe('c0a8010b'); + expect(ipToHex('10.0.0.1')).toBe('0a000001'); + expect(ipToHex('255.255.255.255')).toBe('ffffffff'); + expect(ipToHex('0.0.0.0')).toBe('00000000'); + }); + + it('should handle single digit octets correctly', () => { + expect(ipToHex('1.2.3.4')).toBe('01020304'); + expect(ipToHex('10.20.30.40')).toBe('0a141e28'); + }); + }); + + describe('ipToAsciiHex', () => { + it('should convert IP string to ASCII hex', () => { + expect(ipToAsciiHex('192.168.10.5')).toBe('3139322e3136382e31302e35'); + expect(ipToAsciiHex('172.16.1.50')).toBe('3137322e31362e312e3530'); + }); + + it('should handle different IP lengths', () => { + expect(ipToAsciiHex('10.0.0.1')).toBe('31302e302e302e31'); + expect(ipToAsciiHex('255.255.255.255')).toBe('3235352e3235352e3235352e323535'); + }); + }); + + describe('hexToColonSeparated', () => { + it('should convert hex to colon-separated format', () => { + expect(hexToColonSeparated('f108c0a8010ac0a8010b')).toBe('f1:08:c0:a8:01:0a:c0:a8:01:0b'); + expect(hexToColonSeparated('0104c0a80114')).toBe('01:04:c0:a8:01:14'); + }); + + it('should handle odd-length hex strings', () => { + expect(hexToColonSeparated('abc')).toBe('ab:c'); + }); + + it('should handle empty string', () => { + expect(hexToColonSeparated('')).toBe(''); + }); + }); + + describe('hexToWindowsBinary', () => { + it('should convert hex to space-separated format', () => { + expect(hexToWindowsBinary('f108c0a8010ac0a8010b')).toBe('f1 08 c0 a8 01 0a c0 a8 01 0b'); + expect(hexToWindowsBinary('0104c0a80114')).toBe('01 04 c0 a8 01 14'); + }); + + it('should handle empty string', () => { + expect(hexToWindowsBinary('')).toBe(''); + }); + }); + + describe('generateCiscoOption43', () => { + it('should generate correct hex for single IP', () => { + const result = generateCiscoOption43(['192.168.1.10']); + expect(result.hex).toBe('f104c0a8010a'); + expect(result.subOption).toBe(241); + }); + + it('should generate correct hex for dual IPs', () => { + const result = generateCiscoOption43(['192.168.1.10', '192.168.1.11']); + expect(result.hex).toBe('f108c0a8010ac0a8010b'); + expect(result.colonHex).toBe('f1:08:c0:a8:01:0a:c0:a8:01:0b'); + expect(result.windowsBinary).toBe('f1 08 c0 a8 01 0a c0 a8 01 0b'); + }); + + it('should generate correct hex for multiple IPs', () => { + const result = generateCiscoOption43(['10.0.0.1', '10.0.0.2', '10.0.0.3']); + expect(result.hex).toBe('f10c0a0000010a0000020a000003'); + }); + + it('should have correct IOS command', () => { + const result = generateCiscoOption43(['192.168.1.10']); + expect(result.iosCommand).toContain('ip dhcp pool'); + expect(result.iosCommand).toContain('option 43 hex'); + expect(result.iosCommand).toContain('f104c0a8010a'); + expect(result.commandLabel).toBe('Cisco IOS/IOS-XE DHCP Server'); + }); + + it('should have workings array', () => { + const result = generateCiscoOption43(['192.168.1.10', '192.168.1.11']); + expect(result.workings).toBeDefined(); + expect(result.workings!.length).toBeGreaterThan(0); + expect(result.workings![0]).toContain('f1'); + expect(result.workings![2]).toContain('08'); + }); + + it('should generate all output formats', () => { + const result = generateCiscoOption43(['192.168.1.10']); + expect(result.hex).toBeDefined(); + expect(result.colonHex).toBeDefined(); + expect(result.windowsBinary).toBeDefined(); + expect(result.iscDhcp).toBeDefined(); + expect(result.infoblox).toBeDefined(); + expect(result.mikrotik).toBeDefined(); + }); + }); + + describe('generateMerakiOption43', () => { + it('should generate correct ASCII hex for single IP', () => { + const result = generateMerakiOption43(['192.168.10.5']); + expect(result.hex).toBe('3139322e3136382e31302e35'); + }); + + it('should generate correct ASCII hex for multiple IPs', () => { + const result = generateMerakiOption43(['192.168.1.10', '192.168.1.11']); + // "192.168.1.10,192.168.1.11" + expect(result.hex).toBe('3139322e3136382e312e31302c3139322e3136382e312e3131'); + }); + + it('should have correct IOS command with hex format', () => { + const result = generateMerakiOption43(['192.168.10.5']); + expect(result.iosCommand).toContain('option 43 hex'); + expect(result.iosCommand).not.toContain('ascii'); + }); + + it('should have workings showing ASCII conversion', () => { + const result = generateMerakiOption43(['192.168.10.5']); + expect(result.workings).toBeDefined(); + expect(result.workings!.some((w) => w.includes('ASCII'))).toBe(true); + }); + }); + + describe('generateRuckusOption43', () => { + it('should generate correct hex for SmartZone', () => { + const result = generateRuckusOption43(['172.16.1.50'], 'smartzone'); + expect(result.hex).toBe('060b3137322e31362e312e3530'); + expect(result.subOption).toBe(6); + }); + + it('should generate correct hex for ZoneDirector', () => { + const result = generateRuckusOption43(['10.50.100.200'], 'zonedirector'); + expect(result.hex).toBe('030d31302e35302e3130302e323030'); + expect(result.subOption).toBe(3); + }); + + it('should use different vendor codes', () => { + const smartzone = generateRuckusOption43(['192.168.1.1'], 'smartzone'); + const zonedirector = generateRuckusOption43(['192.168.1.1'], 'zonedirector'); + expect(smartzone.hex.substring(0, 2)).toBe('06'); + expect(zonedirector.hex.substring(0, 2)).toBe('03'); + }); + + it('should have correct command labels', () => { + const smartzone = generateRuckusOption43(['192.168.1.1'], 'smartzone'); + const zonedirector = generateRuckusOption43(['192.168.1.1'], 'zonedirector'); + expect(smartzone.commandLabel).toBe('Ruckus SmartZone CLI'); + expect(zonedirector.commandLabel).toBe('Ruckus ZoneDirector CLI'); + }); + + it('should have Ruckus-specific commands', () => { + const result = generateRuckusOption43(['172.16.1.50'], 'smartzone'); + expect(result.iosCommand).toContain('dhcp-profile'); + expect(result.iosCommand).toContain('option43 sub-option'); + }); + + it('should calculate correct length', () => { + const result = generateRuckusOption43(['172.16.1.50'], 'smartzone'); + // "172.16.1.50" is 11 characters = 0x0b + expect(result.hex.substring(2, 4)).toBe('0b'); + }); + }); + + describe('generateArubaOption43', () => { + it('should generate correct ASCII hex', () => { + const result = generateArubaOption43(['172.16.1.50']); + expect(result.hex).toBe('3137322e31362e312e3530'); + }); + + it('should use only first IP if multiple provided', () => { + const result = generateArubaOption43(['172.16.1.50', '172.16.1.51']); + expect(result.hex).toBe('3137322e31362e312e3530'); + }); + + it('should have correct command format', () => { + const result = generateArubaOption43(['172.16.1.50']); + expect(result.iosCommand).toContain('ip dhcp'); + expect(result.iosCommand).toContain('option 43 hex'); + expect(result.commandLabel).toBe('Aruba OS Switch/Controller'); + }); + }); + + describe('generateUniFiOption43', () => { + it('should generate correct hex with prefix', () => { + const result = generateUniFiOption43('192.168.1.20'); + expect(result.hex).toBe('0104c0a80114'); + expect(result.subOption).toBe(1); + }); + + it('should start with 0104 prefix', () => { + const result = generateUniFiOption43('10.0.0.1'); + expect(result.hex.substring(0, 4)).toBe('0104'); + }); + + it('should have EdgeRouter command format', () => { + const result = generateUniFiOption43('192.168.1.20'); + expect(result.iosCommand).toContain('set service dhcp-server'); + expect(result.commandLabel).toBe('UniFi EdgeRouter/USG'); + }); + + it('should have correct ISC DHCP format', () => { + const result = generateUniFiOption43('192.168.1.20'); + expect(result.iscDhcp).toContain('option space ubnt'); + expect(result.iscDhcp).toContain('encapsulate ubnt'); + }); + }); + + describe('generateExtremeOption43', () => { + it('should generate correct ASCII hex', () => { + const result = generateExtremeOption43(['172.16.1.50']); + expect(result.hex).toBe('3137322e31362e312e3530'); + }); + + it('should have Extreme-specific command', () => { + const result = generateExtremeOption43(['172.16.1.50']); + expect(result.iosCommand).toContain('configure dhcp-server'); + expect(result.commandLabel).toBe('Extreme Networks Switch'); + }); + }); + + describe('generateOption43', () => { + it('should route to correct vendor function', () => { + const cisco = generateOption43('cisco-catalyst', ['192.168.1.10']); + const meraki = generateOption43('cisco-meraki', ['192.168.10.5']); + const ruckusSZ = generateOption43('ruckus-smartzone', ['10.0.0.1']); + const ruckusZD = generateOption43('ruckus-zonedirector', ['10.0.0.1']); + const aruba = generateOption43('aruba', ['172.16.1.50']); + const unifi = generateOption43('unifi', ['192.168.1.20']); + const extreme = generateOption43('extreme', ['10.0.0.1']); + + expect(cisco.hex).toBe('f104c0a8010a'); + expect(meraki.hex).toBe('3139322e3136382e31302e35'); + expect(ruckusSZ.hex.substring(0, 2)).toBe('06'); + expect(ruckusZD.hex.substring(0, 2)).toBe('03'); + expect(aruba.hex).toBe('3137322e31362e312e3530'); + expect(unifi.hex).toBe('0104c0a80114'); + expect(extreme.hex).toBeDefined(); + }); + + it('should handle generic vendor type', () => { + const result = generateOption43('generic', ['192.168.1.10']); + expect(result.hex).toBe('f104c0a8010a'); // Uses Cisco format + }); + + it('should throw error for empty IP array', () => { + expect(() => generateOption43('cisco-catalyst', [])).toThrow('At least one IP address is required'); + }); + + it('should throw error for unsupported vendor', () => { + expect(() => generateOption43('invalid-vendor' as VendorType, ['192.168.1.1'])).toThrow( + 'Unsupported vendor' + ); + }); + }); + + describe('isValidIPv4', () => { + it('should validate correct IPv4 addresses', () => { + expect(isValidIPv4('192.168.1.1')).toBe(true); + expect(isValidIPv4('10.0.0.1')).toBe(true); + expect(isValidIPv4('255.255.255.255')).toBe(true); + expect(isValidIPv4('0.0.0.0')).toBe(true); + expect(isValidIPv4('172.16.1.50')).toBe(true); + }); + + it('should reject invalid IPv4 addresses', () => { + expect(isValidIPv4('256.1.1.1')).toBe(false); + expect(isValidIPv4('192.168.1')).toBe(false); + expect(isValidIPv4('192.168.1.1.1')).toBe(false); + expect(isValidIPv4('192.168.-1.1')).toBe(false); + expect(isValidIPv4('192.168.1.abc')).toBe(false); + expect(isValidIPv4('not.an.ip.address')).toBe(false); + }); + + it('should reject leading zeros', () => { + expect(isValidIPv4('192.168.01.1')).toBe(false); + expect(isValidIPv4('192.168.001.1')).toBe(false); + }); + + it('should reject empty or invalid formats', () => { + expect(isValidIPv4('')).toBe(false); + expect(isValidIPv4('...')).toBe(false); + expect(isValidIPv4('192.168.1.')).toBe(false); + expect(isValidIPv4('.192.168.1.1')).toBe(false); + }); + }); + + describe('parseIPList', () => { + it('should parse comma-separated IPs', () => { + expect(parseIPList('192.168.1.1,192.168.1.2')).toEqual(['192.168.1.1', '192.168.1.2']); + }); + + it('should parse newline-separated IPs', () => { + expect(parseIPList('192.168.1.1\n192.168.1.2')).toEqual(['192.168.1.1', '192.168.1.2']); + }); + + it('should parse mixed separators', () => { + expect(parseIPList('192.168.1.1,192.168.1.2\n192.168.1.3')).toEqual([ + '192.168.1.1', + '192.168.1.2', + '192.168.1.3', + ]); + }); + + it('should trim whitespace', () => { + expect(parseIPList(' 192.168.1.1 , 192.168.1.2 ')).toEqual(['192.168.1.1', '192.168.1.2']); + }); + + it('should filter empty strings', () => { + expect(parseIPList('192.168.1.1,,192.168.1.2')).toEqual(['192.168.1.1', '192.168.1.2']); + expect(parseIPList('192.168.1.1\n\n192.168.1.2')).toEqual(['192.168.1.1', '192.168.1.2']); + }); + + it('should handle single IP', () => { + expect(parseIPList('192.168.1.1')).toEqual(['192.168.1.1']); + }); + + it('should return empty array for empty input', () => { + expect(parseIPList('')).toEqual([]); + expect(parseIPList(' ')).toEqual([]); + }); + }); + + describe('Integration tests', () => { + it('should produce consistent results across formats', () => { + const result = generateCiscoOption43(['192.168.1.10', '192.168.1.11']); + const hexPairs = result.hex.match(/.{1,2}/g) || []; + + expect(result.colonHex).toBe(hexPairs.join(':')); + expect(result.windowsBinary).toBe(hexPairs.join(' ')); + expect(result.infoblox).toBe(hexPairs.join(':')); + }); + + it('should handle real-world example from user', () => { + // User's example: 192.168.1.10, 192.168.1.11 should produce f108c0a8010ac0a8010b + const result = generateCiscoOption43(['192.168.1.10', '192.168.1.11']); + expect(result.hex).toBe('f108c0a8010ac0a8010b'); + }); + + it('should handle Ruckus example from user', () => { + // User's example: 172.16.1.50 for Ruckus should produce 030b3137322e31362e312e3530 + const result = generateRuckusOption43(['172.16.1.50'], 'zonedirector'); + expect(result.hex).toBe('030b3137322e31362e312e3530'); + }); + }); +});