2026-01-25 18:56:12 -08:00
|
|
|
package tendrils
|
|
|
|
|
|
|
|
|
|
import (
|
2026-01-25 19:40:39 -08:00
|
|
|
"log"
|
2026-01-25 18:56:12 -08:00
|
|
|
"net"
|
2026-01-25 19:40:39 -08:00
|
|
|
"sync"
|
2026-01-25 18:56:12 -08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/net/icmp"
|
|
|
|
|
"golang.org/x/net/ipv4"
|
|
|
|
|
)
|
|
|
|
|
|
2026-01-25 19:40:39 -08:00
|
|
|
type pendingPing struct {
|
|
|
|
|
ip string
|
|
|
|
|
response chan bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PingManager struct {
|
2026-01-25 20:05:29 -08:00
|
|
|
mu sync.Mutex
|
|
|
|
|
conn *icmp.PacketConn
|
|
|
|
|
pending map[uint16]*pendingPing
|
|
|
|
|
nextID uint16
|
|
|
|
|
minID uint16
|
|
|
|
|
failures map[string]int
|
2026-01-25 19:40:39 -08:00
|
|
|
}
|
|
|
|
|
|
2026-01-25 21:03:15 -08:00
|
|
|
const pingFailureThreshold = 5
|
2026-01-25 20:05:29 -08:00
|
|
|
|
2026-01-25 19:40:39 -08:00
|
|
|
func NewPingManager() *PingManager {
|
|
|
|
|
pm := &PingManager{
|
2026-01-25 20:05:29 -08:00
|
|
|
pending: map[uint16]*pendingPing{},
|
|
|
|
|
nextID: 1000,
|
|
|
|
|
minID: 1000,
|
|
|
|
|
failures: map[string]int{},
|
2026-01-25 18:56:12 -08:00
|
|
|
}
|
|
|
|
|
|
2026-01-25 19:40:39 -08:00
|
|
|
conn, err := icmp.ListenPacket("ip4:icmp", "0.0.0.0")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return pm
|
2026-01-25 18:56:12 -08:00
|
|
|
}
|
2026-01-25 19:40:39 -08:00
|
|
|
pm.conn = conn
|
2026-01-25 18:56:12 -08:00
|
|
|
|
2026-01-25 19:40:39 -08:00
|
|
|
go pm.readLoop()
|
|
|
|
|
|
|
|
|
|
return pm
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (pm *PingManager) readLoop() {
|
|
|
|
|
buf := make([]byte, 1500)
|
|
|
|
|
for {
|
|
|
|
|
n, peer, err := pm.conn.ReadFrom(buf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg, err := icmp.ParseMessage(1, buf[:n])
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if msg.Type != ipv4.ICMPTypeEchoReply {
|
|
|
|
|
continue
|
2026-01-25 18:56:12 -08:00
|
|
|
}
|
2026-01-25 19:40:39 -08:00
|
|
|
|
|
|
|
|
echo, ok := msg.Body.(*icmp.Echo)
|
|
|
|
|
if !ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ipAddr, ok := peer.(*net.IPAddr)
|
|
|
|
|
if !ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pm.mu.Lock()
|
|
|
|
|
id := uint16(echo.ID)
|
|
|
|
|
if p, exists := pm.pending[id]; exists {
|
|
|
|
|
if p.ip == ipAddr.IP.String() {
|
|
|
|
|
select {
|
|
|
|
|
case p.response <- true:
|
|
|
|
|
default:
|
|
|
|
|
log.Printf("[ping] late response from %s (channel full)", ipAddr.IP)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if id >= pm.minID {
|
|
|
|
|
log.Printf("[ping] late response from %s (id %d expired)", ipAddr.IP, echo.ID)
|
|
|
|
|
}
|
|
|
|
|
pm.mu.Unlock()
|
2026-01-25 18:56:12 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-25 19:40:39 -08:00
|
|
|
func (pm *PingManager) Ping(ipStr string, timeout time.Duration) bool {
|
|
|
|
|
if pm.conn == nil {
|
2026-01-25 18:56:12 -08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-25 19:40:39 -08:00
|
|
|
pm.mu.Lock()
|
|
|
|
|
pm.nextID++
|
|
|
|
|
id := pm.nextID
|
|
|
|
|
p := &pendingPing{
|
|
|
|
|
ip: ipStr,
|
|
|
|
|
response: make(chan bool, 1),
|
|
|
|
|
}
|
|
|
|
|
pm.pending[id] = p
|
|
|
|
|
pm.mu.Unlock()
|
2026-01-25 18:56:12 -08:00
|
|
|
|
2026-01-25 19:40:39 -08:00
|
|
|
defer func() {
|
|
|
|
|
pm.mu.Lock()
|
|
|
|
|
delete(pm.pending, id)
|
|
|
|
|
pm.mu.Unlock()
|
|
|
|
|
}()
|
2026-01-25 18:56:12 -08:00
|
|
|
|
|
|
|
|
msg := icmp.Message{
|
|
|
|
|
Type: ipv4.ICMPTypeEcho,
|
|
|
|
|
Code: 0,
|
|
|
|
|
Body: &icmp.Echo{
|
2026-01-25 19:40:39 -08:00
|
|
|
ID: int(id),
|
2026-01-25 18:56:12 -08:00
|
|
|
Seq: 1,
|
|
|
|
|
Data: []byte("tendrils"),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
msgBytes, err := msg.Marshal(nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-25 19:40:39 -08:00
|
|
|
ip := net.ParseIP(ipStr)
|
|
|
|
|
_, err = pm.conn.WriteTo(msgBytes, &net.IPAddr{IP: ip})
|
2026-01-25 18:56:12 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-25 19:40:39 -08:00
|
|
|
select {
|
|
|
|
|
case <-p.response:
|
|
|
|
|
return true
|
|
|
|
|
case <-time.After(timeout):
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-25 18:56:12 -08:00
|
|
|
|
2026-01-25 19:40:39 -08:00
|
|
|
func (t *Tendrils) pingNode(node *Node) {
|
|
|
|
|
t.nodes.mu.RLock()
|
|
|
|
|
var ips []string
|
|
|
|
|
nodeName := node.DisplayName()
|
2026-01-25 21:03:15 -08:00
|
|
|
nodeID := node.TypeID
|
2026-01-25 19:40:39 -08:00
|
|
|
for _, iface := range node.Interfaces {
|
|
|
|
|
for ipStr := range iface.IPs {
|
|
|
|
|
ip := net.ParseIP(ipStr)
|
|
|
|
|
if ip != nil && ip.To4() != nil {
|
|
|
|
|
ips = append(ips, ipStr)
|
|
|
|
|
}
|
2026-01-25 18:56:12 -08:00
|
|
|
}
|
2026-01-25 19:40:39 -08:00
|
|
|
}
|
|
|
|
|
t.nodes.mu.RUnlock()
|
2026-01-25 18:56:12 -08:00
|
|
|
|
2026-01-25 19:40:39 -08:00
|
|
|
if len(ips) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-25 21:03:15 -08:00
|
|
|
anyReachable := false
|
2026-01-25 19:40:39 -08:00
|
|
|
for _, ipStr := range ips {
|
2026-01-25 21:03:15 -08:00
|
|
|
if t.ping.Ping(ipStr, 2*time.Second) {
|
|
|
|
|
anyReachable = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.ping.mu.Lock()
|
|
|
|
|
if anyReachable {
|
|
|
|
|
t.ping.failures[nodeID] = 0
|
|
|
|
|
t.ping.mu.Unlock()
|
|
|
|
|
if t.errors.ClearUnreachable(node) {
|
|
|
|
|
log.Printf("[ping] %s is now reachable", nodeName)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
t.ping.failures[nodeID]++
|
|
|
|
|
failures := t.ping.failures[nodeID]
|
|
|
|
|
t.ping.mu.Unlock()
|
|
|
|
|
if failures >= pingFailureThreshold {
|
|
|
|
|
if t.errors.SetUnreachable(node) {
|
|
|
|
|
log.Printf("[ping] %s is now unreachable", nodeName)
|
2026-01-25 18:56:12 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|