Add explanatory comments for variable overwrites and code flow decisions

This commit adds detailed explanatory comments throughout the codebase to explain
WHY certain logic is needed, not just WHAT the code does. This improves code
maintainability and helps developers understand the reasoning behind complex
decisions.

Key improvements:
- Version string processing: Explain why "v" prefix is added for semantic versioning
- Control-D configuration: Explain why config is reset to prevent mixing of settings
- DNS server categorization: Explain LAN vs public server handling for performance
- Listener configuration: Document complex fallback logic for port/IP selection
- MAC address normalization: Explain cross-platform compatibility needs
- IPv6 address processing: Document Unix-specific interface suffix handling
- Log content truncation: Explain why large content is limited to prevent flooding
- IP address categorization: Document RFC1918 prioritization logic
- IPv4/IPv6 separation: Explain network stack compatibility needs
- DNS priority logic: Document different priority levels for different scenarios
- Domain controller processing: Explain Windows API prefix handling
- Reverse mapping creation: Document API encoding/decoding needs
- Default value fallbacks: Explain why defaults prevent system failures
- IP stack configuration: Document different defaults for different upstream types

These comments help future developers understand the reasoning behind complex
business logic, making the codebase more maintainable and reducing the risk of
incorrect modifications during maintenance.
This commit is contained in:
Cuong Manh Le
2025-10-09 17:49:21 +07:00
committed by Cuong Manh Le
parent 8b605da861
commit d88c860cac
14 changed files with 90 additions and 7 deletions
+9
View File
@@ -141,6 +141,9 @@ func (d *dhcp) lookupIPByHostname(name string, v6 bool) string {
return true
}
if addr, err := netip.ParseAddr(key.(string)); err == nil && addr.Is6() == v6 {
// Categorize addresses into RFC1918 (private) and public
// RFC1918 addresses are prioritized because they're more likely to be
// the actual client IP in most network configurations
if addr.IsPrivate() {
rfc1918Addrs = append(rfc1918Addrs, addr)
} else {
@@ -264,6 +267,8 @@ func (d *dhcp) iscDHCPReadClientInfoReader(reader io.Reader) error {
}
switch fields[0] {
case "lease":
// Normalize IP address to lowercase for consistent comparison
// DHCP lease files may contain mixed-case IP addresses
ip = normalizeIP(strings.ToLower(fields[1]))
if net.ParseIP(ip) == nil {
d.logger.Warn().Msgf("invalid ip address entry: %q", ip)
@@ -271,6 +276,8 @@ func (d *dhcp) iscDHCPReadClientInfoReader(reader io.Reader) error {
}
case "hardware":
if len(fields) >= 3 {
// Convert MAC to lowercase and remove trailing semicolon
// DHCP lease files use semicolon-terminated MAC addresses
mac = strings.ToLower(strings.TrimRight(fields[2], ";"))
if _, err := net.ParseMAC(mac); err != nil {
// Invalid dhcp, skip.
@@ -278,6 +285,8 @@ func (d *dhcp) iscDHCPReadClientInfoReader(reader io.Reader) error {
}
}
case "client-hostname":
// Remove quotes and semicolons from hostname
// DHCP lease files may quote hostnames and add semicolons
hostname = strings.Trim(fields[1], `";`)
}
}