station/hostapd: Resolve station IPs via udhcpd leases and ARP fallback

The hostapd backend never populated IPs: NewDHCPCorrelator was defined
but never instantiated, and even when it was, the parser only handled
ISC dhcpd's text format. On a BusyBox-based router using udhcpd, every
device showed up with an empty IP.

Two fixes:

- Add a udhcpd binary lease parser. The format is documented in
  busybox/networking/udhcp/dhcpd.{h,c}: an 8-byte big-endian unix-time
  header followed by 36-byte dyn_lease records (expires, IP, MAC,
  20-byte hostname, 2-byte pad). ParseLeases auto-detects the format
  by inspecting the header so the same code path handles both udhcpd
  and ISC text leases.

- Wire the DHCPCorrelator into Backend.Initialize and have it merge
  two sources: ARP first (universal IP fallback for any station that
  has been talked to) and DHCP leases on top (authoritative, carries
  the hostname). ARP fills the gap when leases are missing or the
  station uses a static IP; DHCP wins on conflict.

Default DHCPLeasesPath updated to /var/lib/udhcpd/udhcpd.leases — the
common BusyBox path. Configurable as before.
This commit is contained in:
nemunaire 2026-05-02 11:07:37 +08:00
commit 5a3942f351
8 changed files with 267 additions and 72 deletions

View file

@ -30,6 +30,8 @@ type Backend struct {
// IP / hostname correlation - populated by periodic DHCP lease correlation
ipByMAC map[string]string // MAC -> IP mapping
hostnameByMAC map[string]string // MAC -> hostname mapping
correlator *DHCPCorrelator
}
// NewBackend creates a new hostapd backend
@ -70,11 +72,20 @@ func (b *Backend) Initialize(config backend.BackendConfig) error {
log.Printf("Warning: Failed to load initial stations: %v", err)
}
// Hostapd only knows MACs. Pair every station with its IP/hostname by
// running a background correlator that polls the DHCP lease file and
// the ARP table. Without this, the device list shows MACs only.
b.correlator = NewDHCPCorrelator(b, config.DHCPLeasesPath, config.ARPTablePath)
b.correlator.Start()
return nil
}
// Close cleans up backend resources
func (b *Backend) Close() error {
if b.correlator != nil {
b.correlator.Stop()
}
b.StopEventMonitoring()
return nil
}