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.
71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package arp
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// ARPEntry represents an entry in the ARP table
|
|
type ARPEntry struct {
|
|
IP net.IP
|
|
HWType int
|
|
Flags int
|
|
HWAddress net.HardwareAddr
|
|
Mask string
|
|
Device string
|
|
}
|
|
|
|
// ParseTable is the exported entry point for the /proc/net/arp parser.
|
|
// Other packages (e.g. the hostapd correlator) use it as a universal IP
|
|
// source for stations whose DHCP lease isn't available.
|
|
func ParseTable(path string) ([]ARPEntry, error) {
|
|
return parseARPTable(path)
|
|
}
|
|
|
|
// parseARPTable reads and parses ARP table from /proc/net/arp format
|
|
func parseARPTable(path string) ([]ARPEntry, error) {
|
|
var entries []ARPEntry
|
|
|
|
content, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return entries, err
|
|
}
|
|
|
|
for line := range strings.SplitSeq(string(content), "\n") {
|
|
fields := strings.Fields(line)
|
|
if len(fields) > 5 {
|
|
var entry ARPEntry
|
|
|
|
// Parse HWType (hex format)
|
|
if _, err := fmt.Sscanf(fields[1], "0x%x", &entry.HWType); err != nil {
|
|
continue
|
|
}
|
|
|
|
// Parse Flags (hex format)
|
|
if _, err := fmt.Sscanf(fields[2], "0x%x", &entry.Flags); err != nil {
|
|
continue
|
|
}
|
|
|
|
// Parse IP address
|
|
entry.IP = net.ParseIP(fields[0])
|
|
if entry.IP == nil {
|
|
continue
|
|
}
|
|
|
|
// Parse MAC address
|
|
entry.HWAddress, err = net.ParseMAC(fields[3])
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
entry.Mask = fields[4]
|
|
entry.Device = fields[5]
|
|
|
|
entries = append(entries, entry)
|
|
}
|
|
}
|
|
|
|
return entries, nil
|
|
}
|