2016-03-07 01:29:31 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Graph struct {
|
|
|
|
Version int `json:"version"`
|
|
|
|
Batadv struct {
|
2016-06-16 16:03:45 +00:00
|
|
|
Directed bool `json:"directed"`
|
|
|
|
Graph []string `json:"graph"`
|
|
|
|
Nodes []*GraphNode `json:"nodes"`
|
|
|
|
Links []*GraphLink `json:"links"`
|
2016-03-07 01:29:31 +00:00
|
|
|
} `json:"batadv"`
|
|
|
|
}
|
|
|
|
|
2016-05-16 09:41:45 +00:00
|
|
|
type GraphNode struct {
|
2016-06-16 16:03:45 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
NodeID string `json:"node_id"`
|
2016-05-16 09:41:45 +00:00
|
|
|
}
|
2016-03-07 01:29:31 +00:00
|
|
|
type GraphLink struct {
|
2016-06-16 19:17:06 +00:00
|
|
|
Source int `json:"source"`
|
|
|
|
Target int `json:"target"`
|
|
|
|
VPN bool `json:"vpn"`
|
|
|
|
TQ float32 `json:"tq"`
|
|
|
|
Bidirect bool `json:"bidirect"`
|
2016-03-07 01:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GraphBuilder struct {
|
2016-03-07 11:05:53 +00:00
|
|
|
macToID map[string]string // mapping from MAC address to node id
|
|
|
|
links map[string]*GraphLink // mapping from $idA-$idB to existing link
|
|
|
|
vpn map[string]interface{} // IDs/addresses of VPN servers
|
2016-03-07 01:29:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-20 17:30:44 +00:00
|
|
|
func (nodes *Nodes) BuildGraph() *Graph {
|
2016-03-07 01:29:31 +00:00
|
|
|
builder := &GraphBuilder{
|
|
|
|
macToID: make(map[string]string),
|
|
|
|
links: make(map[string]*GraphLink),
|
2016-03-07 11:05:53 +00:00
|
|
|
vpn: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
|
2016-03-07 01:29:31 +00:00
|
|
|
builder.readNodes(nodes.List)
|
|
|
|
|
2016-05-16 09:41:45 +00:00
|
|
|
graph := &Graph{Version: 1}
|
|
|
|
graph.Batadv.Directed = false
|
|
|
|
graph.Batadv.Nodes, graph.Batadv.Links = builder.Extract()
|
2016-03-07 01:29:31 +00:00
|
|
|
return graph
|
|
|
|
}
|
|
|
|
|
|
|
|
func (builder *GraphBuilder) readNodes(nodes map[string]*Node) {
|
|
|
|
// Fill mac->id map
|
2016-06-16 16:03:45 +00:00
|
|
|
for sourceID, node := range nodes {
|
2016-03-17 10:51:45 +00:00
|
|
|
if nodeinfo := node.Nodeinfo; nodeinfo != nil {
|
2016-05-11 19:30:54 +00:00
|
|
|
// is VPN address?
|
|
|
|
if nodeinfo.VPN {
|
2016-06-16 16:03:45 +00:00
|
|
|
builder.vpn[sourceID] = nil
|
2016-05-11 19:30:54 +00:00
|
|
|
}
|
2016-07-13 15:36:01 +00:00
|
|
|
|
|
|
|
// Batman neighbours
|
2016-06-16 16:03:45 +00:00
|
|
|
for _, batinterface := range nodeinfo.Network.Mesh {
|
2016-04-29 06:25:45 +00:00
|
|
|
interfaces := batinterface.Interfaces
|
|
|
|
addresses := append(append(interfaces.Other, interfaces.Tunnel...), interfaces.Wireless...)
|
2016-03-17 10:51:45 +00:00
|
|
|
|
2016-04-29 06:25:45 +00:00
|
|
|
for _, sourceAddress := range addresses {
|
2016-06-16 16:03:45 +00:00
|
|
|
builder.macToID[sourceAddress] = sourceID
|
2016-03-07 11:05:53 +00:00
|
|
|
}
|
2016-03-07 01:29:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-13 15:36:01 +00:00
|
|
|
|
|
|
|
// Iterate over local MAC addresses from LLDP
|
|
|
|
if neighbours := node.Neighbours; neighbours != nil {
|
|
|
|
for sourceAddr, _ := range neighbours.LLDP {
|
|
|
|
builder.macToID[sourceAddr] = sourceID
|
|
|
|
}
|
|
|
|
}
|
2016-03-07 01:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add links
|
2016-06-16 16:03:45 +00:00
|
|
|
for sourceID, node := range nodes {
|
2016-10-08 09:41:08 +00:00
|
|
|
if node.Flags.Online {
|
2016-06-22 07:16:06 +00:00
|
|
|
if neighbours := node.Neighbours; neighbours != nil {
|
2016-07-13 15:36:01 +00:00
|
|
|
// Batman neighbours
|
2016-06-22 07:16:06 +00:00
|
|
|
for _, batadvNeighbours := range neighbours.Batadv {
|
|
|
|
for targetAddress, link := range batadvNeighbours.Neighbours {
|
|
|
|
if targetID, found := builder.macToID[targetAddress]; found {
|
|
|
|
builder.addLink(targetID, sourceID, link.Tq)
|
|
|
|
}
|
2016-03-07 01:29:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-13 15:36:01 +00:00
|
|
|
// LLDP
|
|
|
|
for _, neighbours := range neighbours.LLDP {
|
|
|
|
for targetAddress, _ := range neighbours {
|
|
|
|
if targetID, found := builder.macToID[targetAddress]; found {
|
|
|
|
builder.addLink(targetID, sourceID, 255)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-07 01:29:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 16:03:45 +00:00
|
|
|
func (builder *GraphBuilder) Extract() ([]*GraphNode, []*GraphLink) {
|
2016-03-07 01:29:31 +00:00
|
|
|
links := make([]*GraphLink, len(builder.links))
|
2016-05-16 09:41:45 +00:00
|
|
|
nodes := make([]*GraphNode, len(builder.macToID))
|
2016-06-16 19:17:06 +00:00
|
|
|
idToIndex := make(map[string]int)
|
2016-03-07 01:29:31 +00:00
|
|
|
|
2016-06-16 19:17:06 +00:00
|
|
|
// collect nodes and create mapping to index
|
|
|
|
i := 0
|
2016-05-16 09:41:45 +00:00
|
|
|
for mac, nodeID := range builder.macToID {
|
2016-06-16 19:17:06 +00:00
|
|
|
nodes[i] = &GraphNode{
|
2016-06-16 16:03:45 +00:00
|
|
|
ID: mac,
|
2016-05-16 09:41:45 +00:00
|
|
|
NodeID: nodeID,
|
|
|
|
}
|
2016-06-16 19:17:06 +00:00
|
|
|
idToIndex[nodeID] = i
|
|
|
|
i++
|
2016-05-16 09:41:45 +00:00
|
|
|
}
|
2016-06-16 19:17:06 +00:00
|
|
|
|
|
|
|
// collect links
|
|
|
|
i = 0
|
2016-05-16 09:41:45 +00:00
|
|
|
for key, link := range builder.links {
|
2016-06-16 19:17:06 +00:00
|
|
|
pos := strings.IndexByte(key, '-')
|
|
|
|
|
|
|
|
link.Source = idToIndex[key[:pos]]
|
|
|
|
link.Target = idToIndex[key[pos+1:]]
|
|
|
|
links[i] = link
|
|
|
|
i++
|
2016-03-07 01:29:31 +00:00
|
|
|
}
|
2016-06-16 19:17:06 +00:00
|
|
|
|
|
|
|
return nodes, links
|
2016-03-07 01:29:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 11:05:53 +00:00
|
|
|
func (builder *GraphBuilder) isVPN(ids ...string) bool {
|
|
|
|
for _, id := range ids {
|
|
|
|
if _, found := builder.vpn[id]; found {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-06-16 16:03:45 +00:00
|
|
|
func (builder *GraphBuilder) addLink(targetID string, sourceID string, linkTq int) {
|
2016-03-20 11:09:32 +00:00
|
|
|
// Sort IDs to generate the key
|
2016-03-07 01:29:31 +00:00
|
|
|
var key string
|
2016-06-16 16:03:45 +00:00
|
|
|
if strings.Compare(sourceID, targetID) > 0 {
|
|
|
|
key = fmt.Sprintf("%s-%s", sourceID, targetID)
|
2016-03-07 01:29:31 +00:00
|
|
|
} else {
|
2016-06-16 16:03:45 +00:00
|
|
|
key = fmt.Sprintf("%s-%s", targetID, sourceID)
|
2016-03-07 01:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var tq float32
|
|
|
|
if linkTq > 0 {
|
|
|
|
tq = float32(1.0 / (float32(linkTq) / 255.0))
|
|
|
|
}
|
|
|
|
|
|
|
|
if link, ok := builder.links[key]; !ok {
|
|
|
|
builder.links[key] = &GraphLink{
|
2016-06-16 16:03:45 +00:00
|
|
|
VPN: builder.isVPN(sourceID, targetID),
|
|
|
|
TQ: tq,
|
2016-03-07 01:29:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Use lowest of both link qualities
|
|
|
|
if tq < link.TQ {
|
|
|
|
link.TQ = tq
|
|
|
|
}
|
|
|
|
link.Bidirect = true
|
|
|
|
}
|
|
|
|
}
|