yanic/respond/daemon/neighbours.go

72 lines
1.6 KiB
Go
Raw Normal View History

2019-04-10 13:18:05 +02:00
package respondd
import (
2019-05-18 23:33:13 +02:00
"errors"
"github.com/Vivena/babelweb2/parser"
2019-04-10 13:18:05 +02:00
"github.com/FreifunkBremen/yanic/data"
)
2019-04-10 21:57:31 +02:00
func (d *Daemon) updateNeighbours(iface string, resp *data.ResponseData) {
2019-04-10 13:18:05 +02:00
_, nodeID := d.getAnswer(iface)
2019-04-10 21:57:31 +02:00
resp.Neighbours.NodeID = nodeID
resp.Neighbours.Batadv = make(map[string]data.BatadvNeighbours)
2019-04-10 21:57:31 +02:00
for _, bface := range d.Batman {
2019-04-10 21:57:31 +02:00
b := NewBatman(bface)
if b == nil {
continue
}
2019-04-10 21:57:31 +02:00
for bfaceAddr, n := range b.Neighbours() {
resp.Neighbours.Batadv[bfaceAddr] = n
}
}
2019-05-18 23:33:13 +02:00
if d.babelData == nil {
return
}
resp.Neighbours.Babel = make(map[string]data.BabelNeighbours)
d.babelData.Iter(func(t parser.Transition) error {
if t.Table != "interface" {
2019-05-18 23:33:13 +02:00
return nil
}
if t.Data["up"].(bool) {
addr := t.Data["ipv6"].(string)
resp.Neighbours.Babel[string(t.Field)] = data.BabelNeighbours{
2019-05-18 23:33:13 +02:00
Protocol: "babel",
LinkLocalAddress: addr,
Neighbours: make(map[string]data.BabelLink),
}
}
return nil
})
d.babelData.Iter(func(t parser.Transition) error {
if t.Table != "neighbour" {
2019-05-18 23:33:13 +02:00
return nil
}
ifname, ok := t.Data["if"].(string)
2019-05-18 23:33:13 +02:00
if !ok {
return errors.New("neighbour without if")
}
addr := t.Data["address"].(string)
2019-05-18 23:33:13 +02:00
if !ok {
return errors.New("neighbour without address")
}
if bIfname, ok := resp.Neighbours.Babel[ifname]; ok {
link := data.BabelLink{
RXCost: int(t.Data["rxcost"].(uint64)),
TXCost: int(t.Data["txcost"].(uint64)),
Cost: int(t.Data["cost"].(uint64)),
2019-05-18 23:33:13 +02:00
}
bIfname.Neighbours[addr] = link
return nil
}
return errors.New("ifname not found during parsing")
})
2019-04-10 13:18:05 +02:00
}