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.
131 lines
3.4 KiB
Go
131 lines
3.4 KiB
Go
package hostapd
|
|
|
|
import (
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/nemunaire/repeater/internal/station/arp"
|
|
"github.com/nemunaire/repeater/internal/station/dhcp"
|
|
)
|
|
|
|
// DHCPCorrelator helps correlate hostapd stations with DHCP leases (and the
|
|
// ARP table as a fallback) so the UI gets an IP and hostname for every
|
|
// station, not just a MAC. Hostapd itself only knows MACs.
|
|
type DHCPCorrelator struct {
|
|
backend *Backend
|
|
dhcpLeasesPath string
|
|
arpTablePath string
|
|
stopChan chan struct{}
|
|
running bool
|
|
}
|
|
|
|
// NewDHCPCorrelator creates a new DHCP correlator
|
|
func NewDHCPCorrelator(backend *Backend, dhcpLeasesPath, arpTablePath string) *DHCPCorrelator {
|
|
if dhcpLeasesPath == "" {
|
|
dhcpLeasesPath = "/var/lib/misc/udhcpd.leases"
|
|
}
|
|
if arpTablePath == "" {
|
|
arpTablePath = "/proc/net/arp"
|
|
}
|
|
|
|
return &DHCPCorrelator{
|
|
backend: backend,
|
|
dhcpLeasesPath: dhcpLeasesPath,
|
|
arpTablePath: arpTablePath,
|
|
stopChan: make(chan struct{}),
|
|
}
|
|
}
|
|
|
|
// Start begins periodic correlation of DHCP leases with hostapd stations
|
|
func (dc *DHCPCorrelator) Start() {
|
|
if dc.running {
|
|
return
|
|
}
|
|
|
|
dc.running = true
|
|
go dc.correlationLoop()
|
|
log.Printf("DHCP/ARP correlation started (leases=%s, arp=%s)", dc.dhcpLeasesPath, dc.arpTablePath)
|
|
}
|
|
|
|
// Stop stops the correlation loop
|
|
func (dc *DHCPCorrelator) Stop() {
|
|
if !dc.running {
|
|
return
|
|
}
|
|
|
|
dc.running = false
|
|
close(dc.stopChan)
|
|
log.Printf("DHCP correlation stopped")
|
|
}
|
|
|
|
// correlationLoop periodically correlates DHCP leases with stations
|
|
func (dc *DHCPCorrelator) correlationLoop() {
|
|
// Do an initial correlation immediately
|
|
dc.correlate()
|
|
|
|
// Then correlate every 10 seconds
|
|
ticker := time.NewTicker(10 * time.Second)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
dc.correlate()
|
|
case <-dc.stopChan:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// correlate performs one correlation cycle. We pull from two sources:
|
|
//
|
|
// - The DHCP lease file (udhcpd binary or ISC text — auto-detected) is the
|
|
// authoritative source for hostnames and the IP that was actually
|
|
// assigned.
|
|
// - The ARP table is a universal IP fallback. If the lease file is missing
|
|
// or the station uses a static IP, ARP still gives us its current IPv4.
|
|
//
|
|
// The two are merged: ARP fills gaps, DHCP wins on conflict because it
|
|
// carries the hostname and is less prone to stale entries.
|
|
func (dc *DHCPCorrelator) correlate() {
|
|
macToIP := make(map[string]string)
|
|
macToHostname := make(map[string]string)
|
|
|
|
// ARP first, so DHCP overrides on conflict.
|
|
if entries, err := arp.ParseTable(dc.arpTablePath); err == nil {
|
|
for _, entry := range entries {
|
|
// Flags 2 (COMPLETE) and 6 (COMPLETE|PERM) — incomplete
|
|
// entries have a zero MAC and would pollute the mapping.
|
|
if entry.Flags != 2 && entry.Flags != 6 {
|
|
continue
|
|
}
|
|
mac := strings.ToLower(entry.HWAddress.String())
|
|
if mac == "" || mac == "00:00:00:00:00:00" {
|
|
continue
|
|
}
|
|
macToIP[mac] = entry.IP.String()
|
|
}
|
|
} else {
|
|
log.Printf("ARP table read failed (%s): %v", dc.arpTablePath, err)
|
|
}
|
|
|
|
if leases, err := dhcp.ParseLeases(dc.dhcpLeasesPath); err == nil {
|
|
for _, lease := range leases {
|
|
mac := strings.ToLower(lease.MAC)
|
|
if mac == "" {
|
|
continue
|
|
}
|
|
if lease.IP != "" {
|
|
macToIP[mac] = lease.IP
|
|
}
|
|
if lease.Hostname != "" {
|
|
macToHostname[mac] = lease.Hostname
|
|
}
|
|
}
|
|
} else {
|
|
log.Printf("DHCP lease parse failed (%s): %v", dc.dhcpLeasesPath, err)
|
|
}
|
|
|
|
dc.backend.UpdateLeaseInfo(macToIP, macToHostname)
|
|
}
|