2026-01-23 23:31:57 -08:00
|
|
|
package tendrils
|
|
|
|
|
|
2026-01-25 09:53:31 -08:00
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
2026-01-23 23:31:57 -08:00
|
|
|
|
|
|
|
|
type Link struct {
|
2026-01-28 22:36:44 -08:00
|
|
|
ID string `json:"id"`
|
2026-01-24 11:03:34 -08:00
|
|
|
NodeA *Node `json:"node_a"`
|
|
|
|
|
InterfaceA string `json:"interface_a,omitempty"`
|
|
|
|
|
NodeB *Node `json:"node_b"`
|
|
|
|
|
InterfaceB string `json:"interface_b,omitempty"`
|
2026-01-23 23:31:57 -08:00
|
|
|
}
|
2026-01-25 09:53:31 -08:00
|
|
|
|
|
|
|
|
func (l *Link) MarshalJSON() ([]byte, error) {
|
|
|
|
|
type linkJSON struct {
|
2026-01-30 09:35:42 -08:00
|
|
|
ID string `json:"id"`
|
|
|
|
|
NodeAID string `json:"node_a_id"`
|
|
|
|
|
InterfaceA string `json:"interface_a,omitempty"`
|
|
|
|
|
NodeBID string `json:"node_b_id"`
|
|
|
|
|
InterfaceB string `json:"interface_b,omitempty"`
|
2026-01-25 09:53:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return json.Marshal(linkJSON{
|
2026-01-28 22:36:44 -08:00
|
|
|
ID: l.ID,
|
2026-01-30 09:35:42 -08:00
|
|
|
NodeAID: l.NodeA.ID,
|
2026-01-25 09:53:31 -08:00
|
|
|
InterfaceA: l.InterfaceA,
|
2026-01-30 09:35:42 -08:00
|
|
|
NodeBID: l.NodeB.ID,
|
2026-01-25 09:53:31 -08:00
|
|
|
InterfaceB: l.InterfaceB,
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-01-23 23:31:57 -08:00
|
|
|
|
|
|
|
|
func (l *Link) String() string {
|
|
|
|
|
nameA := l.NodeA.DisplayName()
|
|
|
|
|
if nameA == "" {
|
|
|
|
|
nameA = l.NodeA.FirstMAC()
|
|
|
|
|
}
|
|
|
|
|
nameB := l.NodeB.DisplayName()
|
|
|
|
|
if nameB == "" {
|
|
|
|
|
nameB = l.NodeB.FirstMAC()
|
|
|
|
|
}
|
|
|
|
|
sideA := nameA
|
|
|
|
|
if l.InterfaceA != "" {
|
|
|
|
|
sideA = nameA + ":" + l.InterfaceA
|
|
|
|
|
}
|
|
|
|
|
sideB := nameB
|
|
|
|
|
if l.InterfaceB != "" {
|
|
|
|
|
sideB = nameB + ":" + l.InterfaceB
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("%s <-> %s", sideA, sideB)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *Nodes) getDirectLinks() []*Link {
|
|
|
|
|
macToNode := map[string]*Node{}
|
|
|
|
|
for _, node := range n.nodes {
|
|
|
|
|
for _, iface := range node.Interfaces {
|
2026-01-24 11:03:34 -08:00
|
|
|
if iface.MAC != "" {
|
|
|
|
|
macToNode[string(iface.MAC)] = node
|
2026-01-23 23:31:57 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
seen := map[string]bool{}
|
|
|
|
|
var links []*Link
|
|
|
|
|
|
|
|
|
|
for _, target := range n.nodes {
|
|
|
|
|
seenMACs := map[string]bool{}
|
|
|
|
|
for _, iface := range target.Interfaces {
|
2026-01-24 11:03:34 -08:00
|
|
|
if iface.MAC == "" {
|
2026-01-23 23:31:57 -08:00
|
|
|
continue
|
|
|
|
|
}
|
2026-01-24 11:03:34 -08:00
|
|
|
mac := string(iface.MAC)
|
2026-01-23 23:31:57 -08:00
|
|
|
if seenMACs[mac] {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
seenMACs[mac] = true
|
|
|
|
|
|
2026-01-25 18:01:43 -08:00
|
|
|
lastHops := n.findAllLastHops(target, mac, macToNode)
|
|
|
|
|
for _, lh := range lastHops {
|
|
|
|
|
targetIface := n.findTargetInterface(target, lh.node, macToNode)
|
|
|
|
|
key := makeLinkKey(lh.node, lh.port, target, targetIface)
|
|
|
|
|
if !seen[key] {
|
|
|
|
|
seen[key] = true
|
2026-02-02 20:33:42 -08:00
|
|
|
if target.Type == "" {
|
|
|
|
|
if lh.node.Type == NodeTypeSwitch {
|
|
|
|
|
target.Type = NodeTypeWiredClient
|
|
|
|
|
} else if lh.node.Type == NodeTypeAP {
|
|
|
|
|
target.Type = NodeTypeWirelessClient
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-25 18:01:43 -08:00
|
|
|
links = append(links, &Link{
|
2026-01-28 22:36:44 -08:00
|
|
|
ID: newID("link"),
|
2026-01-25 18:01:43 -08:00
|
|
|
NodeA: lh.node,
|
|
|
|
|
InterfaceA: lh.port,
|
|
|
|
|
NodeB: target,
|
|
|
|
|
InterfaceB: targetIface,
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-01-23 23:31:57 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return links
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-25 18:01:43 -08:00
|
|
|
func (n *Nodes) findAllLastHops(target *Node, mac string, macToNode map[string]*Node) []struct {
|
|
|
|
|
node *Node
|
|
|
|
|
port string
|
|
|
|
|
} {
|
|
|
|
|
var results []struct {
|
|
|
|
|
node *Node
|
|
|
|
|
port string
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 23:31:57 -08:00
|
|
|
for _, node := range n.nodes {
|
|
|
|
|
port, sees := node.MACTable[mac]
|
|
|
|
|
if !sees || node == target {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !n.hasCloserNode(node, target, mac, port, macToNode) {
|
2026-01-25 18:01:43 -08:00
|
|
|
results = append(results, struct {
|
|
|
|
|
node *Node
|
|
|
|
|
port string
|
|
|
|
|
}{node, port})
|
2026-01-23 23:31:57 -08:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-25 18:01:43 -08:00
|
|
|
return results
|
2026-01-23 23:31:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *Nodes) hasCloserNode(node, target *Node, mac, port string, macToNode map[string]*Node) bool {
|
2026-01-24 11:48:24 -08:00
|
|
|
nodeMACs := map[string]bool{}
|
|
|
|
|
for _, iface := range node.Interfaces {
|
|
|
|
|
if iface.MAC != "" {
|
|
|
|
|
nodeMACs[string(iface.MAC)] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 23:31:57 -08:00
|
|
|
for otherMAC, otherPort := range node.MACTable {
|
|
|
|
|
if otherPort != port {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
otherNode := macToNode[otherMAC]
|
|
|
|
|
if otherNode == nil || otherNode == node || otherNode == target {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2026-01-24 11:48:24 -08:00
|
|
|
|
|
|
|
|
targetPort, alsoSees := otherNode.MACTable[mac]
|
|
|
|
|
if !alsoSees {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
seesNodeOnSamePort := false
|
|
|
|
|
for nodeMAC := range nodeMACs {
|
|
|
|
|
if nodePort, seesNode := otherNode.MACTable[nodeMAC]; seesNode && nodePort == targetPort {
|
|
|
|
|
seesNodeOnSamePort = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !seesNodeOnSamePort {
|
2026-01-23 23:31:57 -08:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *Nodes) findTargetInterface(target, lastHop *Node, macToNode map[string]*Node) string {
|
|
|
|
|
for lastHopMAC, targetPort := range target.MACTable {
|
|
|
|
|
if macToNode[lastHopMAC] == lastHop {
|
|
|
|
|
return targetPort
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func makeLinkKey(nodeA *Node, ifaceA string, nodeB *Node, ifaceB string) string {
|
|
|
|
|
ptrA := fmt.Sprintf("%p", nodeA)
|
|
|
|
|
ptrB := fmt.Sprintf("%p", nodeB)
|
|
|
|
|
if ptrA < ptrB {
|
|
|
|
|
return ptrA + ":" + ifaceA + "-" + ptrB + ":" + ifaceB
|
|
|
|
|
}
|
|
|
|
|
return ptrB + ":" + ifaceB + "-" + ptrA + ":" + ifaceA
|
|
|
|
|
}
|