2025-11-29 21:08:32 -08:00
|
|
|
package tendrils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"log"
|
|
|
|
|
"net"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (t *Tendrils) pollARP(ctx context.Context) {
|
|
|
|
|
ticker := time.NewTicker(30 * time.Second)
|
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
case <-ticker.C:
|
|
|
|
|
t.readARPTable()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type arpEntry struct {
|
|
|
|
|
ip net.IP
|
|
|
|
|
mac net.HardwareAddr
|
|
|
|
|
iface string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *Tendrils) readARPTable() {
|
|
|
|
|
entries := t.parseARPTable()
|
2026-01-18 14:54:06 -08:00
|
|
|
localNode := t.getLocalNode()
|
|
|
|
|
|
2025-11-29 21:08:32 -08:00
|
|
|
for _, entry := range entries {
|
2026-01-17 21:02:30 -08:00
|
|
|
if t.Interface != "" && entry.iface != t.Interface {
|
2026-01-17 20:54:58 -08:00
|
|
|
continue
|
|
|
|
|
}
|
2025-11-29 21:08:32 -08:00
|
|
|
if isBroadcastOrZero(entry.mac) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 21:12:21 -08:00
|
|
|
if t.DebugARP {
|
|
|
|
|
log.Printf("[arp] %s: ip=%s mac=%s", entry.iface, entry.ip, entry.mac)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 14:44:15 -08:00
|
|
|
t.nodes.Update(nil, entry.mac, []net.IP{entry.ip}, "", "", "arp")
|
2026-01-18 14:54:06 -08:00
|
|
|
if localNode != nil {
|
|
|
|
|
t.nodes.UpdateMACTable(localNode, entry.mac, entry.iface)
|
|
|
|
|
}
|
2025-11-29 21:08:32 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func normalizeMACAddress(mac string) string {
|
|
|
|
|
parts := strings.Split(mac, ":")
|
|
|
|
|
for i, part := range parts {
|
|
|
|
|
if len(part) == 1 {
|
|
|
|
|
parts[i] = "0" + part
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return strings.Join(parts, ":")
|
|
|
|
|
}
|
2026-01-23 00:24:36 -08:00
|
|
|
|
|
|
|
|
func (t *Tendrils) requestARP(ip net.IP) {
|
|
|
|
|
if t.DisableARP {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
conn, err := net.DialTimeout("udp4", ip.String()+":1", 100*time.Millisecond)
|
|
|
|
|
if err == nil {
|
|
|
|
|
conn.Close()
|
|
|
|
|
}
|
|
|
|
|
}
|