Add broadcast pinging and fix node merging by shared IP

This commit is contained in:
Ian Gulliver
2026-01-24 20:01:43 -08:00
parent f72f716349
commit 4123b49a98
3 changed files with 89 additions and 2 deletions

69
broadcast.go Normal file
View File

@@ -0,0 +1,69 @@
package tendrils
import (
"context"
"log"
"net"
"time"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
)
func (t *Tendrils) pingBroadcast(ctx context.Context, iface net.Interface) {
_, broadcast := getInterfaceIPv4(iface)
if broadcast == nil {
return
}
t.sendBroadcastPing(broadcast, iface.Name)
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
t.sendBroadcastPing(broadcast, iface.Name)
}
}
}
func (t *Tendrils) sendBroadcastPing(broadcast net.IP, ifaceName string) {
conn, err := icmp.ListenPacket("ip4:icmp", "0.0.0.0")
if err != nil {
if t.DebugARP {
log.Printf("[broadcast] %s: failed to create icmp socket: %v", ifaceName, err)
}
return
}
defer conn.Close()
msg := icmp.Message{
Type: ipv4.ICMPTypeEcho,
Code: 0,
Body: &icmp.Echo{
ID: 1,
Seq: 1,
Data: []byte("tendrils"),
},
}
msgBytes, err := msg.Marshal(nil)
if err != nil {
return
}
_, err = conn.WriteTo(msgBytes, &net.IPAddr{IP: broadcast})
if err != nil {
if t.DebugARP {
log.Printf("[broadcast] %s: failed to send ping to %s: %v", ifaceName, broadcast, err)
}
return
}
if t.DebugARP {
log.Printf("[broadcast] %s: sent ping to %s", ifaceName, broadcast)
}
}

View File

@@ -189,9 +189,17 @@ func (n *Nodes) updateNodeIPs(node *Node, nodeID int, ips []net.IP) []string {
var added []string
for _, ip := range ips {
ipKey := ip.String()
if _, exists := n.ipIndex[ipKey]; exists {
if existingID, exists := n.ipIndex[ipKey]; exists {
if existingID == nodeID {
continue
}
if existingNode, nodeExists := n.nodes[existingID]; nodeExists {
n.mergeNodes(nodeID, existingID)
if n.t.LogEvents {
log.Printf("[merge] %s into %s (shared ip %s)", existingNode, node, ipKey)
}
}
}
n.ipIndex[ipKey] = nodeID
iface, exists := node.Interfaces[ipKey]
if !exists {
@@ -287,6 +295,14 @@ func (n *Nodes) updateNodeInterface(node *Node, nodeID int, mac net.HardwareAddr
for _, ip := range ips {
ipKey := ip.String()
if existingID, exists := n.ipIndex[ipKey]; exists && existingID != nodeID {
if existingNode, nodeExists := n.nodes[existingID]; nodeExists {
n.mergeNodes(nodeID, existingID)
if n.t.LogEvents {
log.Printf("[merge] %s into %s (shared ip %s)", existingNode, node, ipKey)
}
}
}
if !iface.IPs.Has(ipKey) {
added = append(added, "ip="+ipKey)
}

View File

@@ -223,6 +223,8 @@ func (t *Tendrils) updateInterfaces(interfaces []net.Interface) {
}
func (t *Tendrils) startInterface(ctx context.Context, iface net.Interface) {
go t.pingBroadcast(ctx, iface)
if !t.DisableLLDP {
go t.listenLLDP(ctx, iface)
}