2016-02-26 08:28:31 +00:00
|
|
|
package respond
|
2015-12-29 03:08:03 +00:00
|
|
|
|
|
|
|
import (
|
2016-03-19 00:50:23 +00:00
|
|
|
"bytes"
|
|
|
|
"compress/flate"
|
2016-03-11 23:59:36 +00:00
|
|
|
"encoding/json"
|
2017-04-10 16:54:12 +00:00
|
|
|
"fmt"
|
2015-12-29 03:08:03 +00:00
|
|
|
"net"
|
|
|
|
"time"
|
2017-02-01 17:37:39 +00:00
|
|
|
|
2019-01-17 12:26:16 +00:00
|
|
|
"github.com/bdlm/log"
|
|
|
|
|
2017-03-03 15:19:35 +00:00
|
|
|
"github.com/FreifunkBremen/yanic/data"
|
|
|
|
"github.com/FreifunkBremen/yanic/database"
|
2018-01-07 20:00:56 +00:00
|
|
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
2017-04-10 16:54:12 +00:00
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
2015-12-29 03:08:03 +00:00
|
|
|
)
|
|
|
|
|
2017-01-29 21:14:40 +00:00
|
|
|
// Collector for a specificle respond messages
|
2015-12-29 03:08:03 +00:00
|
|
|
type Collector struct {
|
2017-12-05 22:17:49 +00:00
|
|
|
connections []multicastConn // UDP sockets
|
2017-10-18 16:22:14 +00:00
|
|
|
|
2018-01-17 19:20:35 +00:00
|
|
|
queue chan *Response // received responses
|
|
|
|
db database.Connection
|
|
|
|
nodes *runtime.Nodes
|
|
|
|
sitesDomains map[string][]string
|
|
|
|
interval time.Duration // Interval for multicast packets
|
|
|
|
stop chan interface{}
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 22:17:49 +00:00
|
|
|
type multicastConn struct {
|
|
|
|
Conn *net.UDPConn
|
2018-03-11 19:21:35 +00:00
|
|
|
SendRequest bool
|
2017-12-05 22:17:49 +00:00
|
|
|
MulticastAddress net.IP
|
|
|
|
}
|
|
|
|
|
2017-01-20 21:27:44 +00:00
|
|
|
// NewCollector creates a Collector struct
|
2017-12-05 22:17:49 +00:00
|
|
|
func NewCollector(db database.Connection, nodes *runtime.Nodes, sitesDomains map[string][]string, ifaces []InterfaceConfig) *Collector {
|
2017-10-18 16:22:14 +00:00
|
|
|
|
|
|
|
coll := &Collector{
|
2018-01-17 19:20:35 +00:00
|
|
|
db: db,
|
|
|
|
nodes: nodes,
|
|
|
|
sitesDomains: sitesDomains,
|
|
|
|
queue: make(chan *Response, 400),
|
|
|
|
stop: make(chan interface{}),
|
2017-10-18 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
coll.listenUDP(iface)
|
|
|
|
}
|
|
|
|
|
|
|
|
go coll.parser()
|
|
|
|
|
|
|
|
if coll.db != nil {
|
|
|
|
go coll.globalStatsWorker()
|
|
|
|
}
|
|
|
|
|
|
|
|
return coll
|
|
|
|
}
|
|
|
|
|
2017-12-05 22:17:49 +00:00
|
|
|
func (coll *Collector) listenUDP(iface InterfaceConfig) {
|
|
|
|
|
|
|
|
var addr net.IP
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if iface.IPAddress != "" {
|
|
|
|
addr = net.ParseIP(iface.IPAddress)
|
|
|
|
} else {
|
2018-05-08 15:15:49 +00:00
|
|
|
addr, err = getUnicastAddr(iface.InterfaceName, iface.MulticastAddress == "")
|
2017-12-05 22:17:49 +00:00
|
|
|
if err != nil {
|
2019-01-17 12:26:16 +00:00
|
|
|
log.WithField("iface", iface.InterfaceName).Panic(err)
|
2017-12-05 22:17:49 +00:00
|
|
|
}
|
2017-10-18 16:22:14 +00:00
|
|
|
}
|
2017-12-05 22:17:49 +00:00
|
|
|
|
|
|
|
multicastAddress := multicastAddressDefault
|
|
|
|
if iface.MulticastAddress != "" {
|
|
|
|
multicastAddress = iface.MulticastAddress
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open socket
|
2017-02-01 17:37:39 +00:00
|
|
|
conn, err := net.ListenUDP("udp", &net.UDPAddr{
|
2017-12-05 22:17:49 +00:00
|
|
|
IP: addr,
|
|
|
|
Port: iface.Port,
|
|
|
|
Zone: iface.InterfaceName,
|
2017-02-01 17:37:39 +00:00
|
|
|
})
|
2015-12-29 03:08:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2016-02-25 20:06:37 +00:00
|
|
|
conn.SetReadBuffer(maxDataGramSize)
|
2015-12-29 03:08:03 +00:00
|
|
|
|
2017-12-05 22:17:49 +00:00
|
|
|
coll.connections = append(coll.connections, multicastConn{
|
|
|
|
Conn: conn,
|
2018-03-11 19:21:35 +00:00
|
|
|
SendRequest: !iface.SendNoRequest,
|
2017-12-05 22:17:49 +00:00
|
|
|
MulticastAddress: net.ParseIP(multicastAddress),
|
|
|
|
})
|
2016-03-12 00:04:22 +00:00
|
|
|
|
2017-10-18 16:22:14 +00:00
|
|
|
// Start receiver
|
|
|
|
go coll.receiver(conn)
|
2016-12-22 02:06:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 15:15:49 +00:00
|
|
|
// Returns a unicast address of given interface (linklocal or global unicast address)
|
|
|
|
func getUnicastAddr(ifname string, linklocal bool) (net.IP, error) {
|
2017-02-01 17:37:39 +00:00
|
|
|
iface, err := net.InterfaceByName(ifname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addresses, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-05 22:17:49 +00:00
|
|
|
var ip net.IP
|
2017-02-01 17:37:39 +00:00
|
|
|
|
|
|
|
for _, addr := range addresses {
|
2017-12-05 22:17:49 +00:00
|
|
|
ipnet, ok := addr.(*net.IPNet)
|
|
|
|
if !ok {
|
|
|
|
continue
|
2017-02-01 17:37:39 +00:00
|
|
|
}
|
2018-05-08 15:15:49 +00:00
|
|
|
if (!linklocal && ipnet.IP.IsGlobalUnicast()) || (linklocal && ipnet.IP.IsLinkLocalUnicast()) {
|
2017-12-05 22:17:49 +00:00
|
|
|
ip = ipnet.IP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ip != nil {
|
|
|
|
return ip, nil
|
2017-02-01 17:37:39 +00:00
|
|
|
}
|
2019-01-17 12:26:16 +00:00
|
|
|
return nil, fmt.Errorf("unable to find a unicast address")
|
2017-02-01 17:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 02:06:46 +00:00
|
|
|
// Start Collector
|
|
|
|
func (coll *Collector) Start(interval time.Duration) {
|
2017-01-29 21:26:16 +00:00
|
|
|
if coll.interval != 0 {
|
2019-01-17 12:26:16 +00:00
|
|
|
log.Panic("already started")
|
2016-12-22 02:06:46 +00:00
|
|
|
}
|
2017-01-29 21:26:16 +00:00
|
|
|
if interval <= 0 {
|
2019-01-17 12:26:16 +00:00
|
|
|
log.Panic("invalid collector interval")
|
2017-01-29 21:26:16 +00:00
|
|
|
}
|
|
|
|
coll.interval = interval
|
2016-12-22 02:06:46 +00:00
|
|
|
|
2016-03-12 17:26:51 +00:00
|
|
|
go func() {
|
2016-12-22 02:06:46 +00:00
|
|
|
coll.sendOnce() // immediately
|
|
|
|
coll.sender() // periodically
|
2016-03-12 17:26:51 +00:00
|
|
|
}()
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 22:56:23 +00:00
|
|
|
// Close Collector
|
2015-12-29 03:08:03 +00:00
|
|
|
func (coll *Collector) Close() {
|
2017-01-29 21:26:16 +00:00
|
|
|
close(coll.stop)
|
2017-10-18 16:22:14 +00:00
|
|
|
for _, conn := range coll.connections {
|
2017-12-05 22:17:49 +00:00
|
|
|
conn.Conn.Close()
|
2017-10-18 16:22:14 +00:00
|
|
|
}
|
2015-12-29 03:08:03 +00:00
|
|
|
close(coll.queue)
|
|
|
|
}
|
|
|
|
|
2015-12-29 13:05:47 +00:00
|
|
|
func (coll *Collector) sendOnce() {
|
2017-01-29 21:14:40 +00:00
|
|
|
now := jsontime.Now()
|
|
|
|
coll.sendMulticast()
|
|
|
|
|
|
|
|
// Wait for the multicast responses to be processed and send unicasts
|
|
|
|
time.Sleep(coll.interval / 2)
|
|
|
|
coll.sendUnicasts(now)
|
2015-12-29 13:05:47 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 21:14:40 +00:00
|
|
|
func (coll *Collector) sendMulticast() {
|
2019-01-17 12:26:16 +00:00
|
|
|
log.Info("sending multicasts")
|
2017-10-18 16:22:14 +00:00
|
|
|
for _, conn := range coll.connections {
|
2018-03-11 19:21:35 +00:00
|
|
|
if conn.SendRequest {
|
|
|
|
coll.sendPacket(conn.Conn, conn.MulticastAddress)
|
|
|
|
}
|
2017-10-18 16:22:14 +00:00
|
|
|
}
|
2017-01-29 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send unicast packets to nodes that did not answer the multicast
|
|
|
|
func (coll *Collector) sendUnicasts(seenBefore jsontime.Time) {
|
|
|
|
seenAfter := seenBefore.Add(-time.Minute * 10)
|
|
|
|
|
|
|
|
// Select online nodes that has not been seen recently
|
2017-04-10 16:54:12 +00:00
|
|
|
nodes := coll.nodes.Select(func(n *runtime.Node) bool {
|
2017-01-29 21:14:40 +00:00
|
|
|
return n.Lastseen.After(seenAfter) && n.Lastseen.Before(seenBefore) && n.Address != nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Send unicast packets
|
2017-12-05 22:17:49 +00:00
|
|
|
count := 0
|
2017-01-29 21:14:40 +00:00
|
|
|
for _, node := range nodes {
|
2017-12-05 22:17:49 +00:00
|
|
|
send := 0
|
|
|
|
for _, conn := range coll.connections {
|
2018-07-27 18:42:03 +00:00
|
|
|
if node.Address.Zone != "" && conn.Conn.LocalAddr().(*net.UDPAddr).Zone != node.Address.Zone && conn.SendRequest {
|
2017-12-05 22:17:49 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
coll.sendPacket(conn.Conn, node.Address.IP)
|
|
|
|
send++
|
|
|
|
}
|
|
|
|
if send == 0 {
|
2019-01-17 12:26:16 +00:00
|
|
|
log.WithField("iface", node.Address.Zone).Error("unable to find connection")
|
2017-12-05 22:17:49 +00:00
|
|
|
} else {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
count += send
|
2017-10-18 16:22:14 +00:00
|
|
|
}
|
2017-01-29 21:14:40 +00:00
|
|
|
}
|
2019-01-17 12:26:16 +00:00
|
|
|
log.WithFields(map[string]interface{}{
|
|
|
|
"pkg_count": count,
|
|
|
|
"nodes_count": len(nodes),
|
|
|
|
}).Info("sending unicast pkg")
|
2017-01-29 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 16:22:14 +00:00
|
|
|
// SendPacket sends a UDP request to the given unicast or multicast address on the first UDP socket
|
|
|
|
func (coll *Collector) SendPacket(destination net.IP) {
|
2017-12-05 22:17:49 +00:00
|
|
|
coll.sendPacket(coll.connections[0].Conn, destination)
|
2017-10-18 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendPacket sends a UDP request to the given unicast or multicast address on the given UDP socket
|
|
|
|
func (coll *Collector) sendPacket(conn *net.UDPConn, destination net.IP) {
|
2017-01-29 21:14:40 +00:00
|
|
|
addr := net.UDPAddr{
|
2017-10-18 16:22:14 +00:00
|
|
|
IP: destination,
|
2017-01-29 21:14:40 +00:00
|
|
|
Port: port,
|
2017-10-18 16:22:14 +00:00
|
|
|
Zone: conn.LocalAddr().(*net.UDPAddr).Zone,
|
2016-02-19 10:13:30 +00:00
|
|
|
}
|
2015-12-29 13:05:47 +00:00
|
|
|
|
2017-10-18 16:22:14 +00:00
|
|
|
if _, err := conn.WriteToUDP([]byte("GET nodeinfo statistics neighbours"), &addr); err != nil {
|
2019-01-17 12:26:16 +00:00
|
|
|
log.WithField("address", addr.String()).Errorf("WriteToUDP failed: %s", err)
|
2016-03-07 00:37:07 +00:00
|
|
|
}
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 15:54:48 +00:00
|
|
|
// send packets continuously
|
2016-03-11 22:56:23 +00:00
|
|
|
func (coll *Collector) sender() {
|
2017-01-29 21:26:16 +00:00
|
|
|
ticker := time.NewTicker(coll.interval)
|
2016-03-11 22:56:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-coll.stop:
|
2017-01-29 21:26:16 +00:00
|
|
|
ticker.Stop()
|
2016-03-11 22:56:23 +00:00
|
|
|
return
|
2017-01-29 21:26:16 +00:00
|
|
|
case <-ticker.C:
|
2016-10-03 23:05:18 +00:00
|
|
|
// send the multicast packet to request per-node statistics
|
2016-03-11 22:56:23 +00:00
|
|
|
coll.sendOnce()
|
|
|
|
}
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (coll *Collector) parser() {
|
2016-01-04 01:07:09 +00:00
|
|
|
for obj := range coll.queue {
|
2016-10-03 17:55:37 +00:00
|
|
|
if data, err := obj.parse(); err != nil {
|
2019-01-17 12:26:16 +00:00
|
|
|
log.WithField("address", obj.Address.String()).Errorf("unable to decode response %s", err)
|
2016-10-03 17:55:37 +00:00
|
|
|
} else {
|
|
|
|
coll.saveResponse(obj.Address, data)
|
2016-03-11 23:59:36 +00:00
|
|
|
}
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 17:55:37 +00:00
|
|
|
func (res *Response) parse() (*data.ResponseData, error) {
|
2016-03-19 22:27:07 +00:00
|
|
|
// Deflate
|
2016-10-03 17:55:37 +00:00
|
|
|
deflater := flate.NewReader(bytes.NewReader(res.Raw))
|
2016-03-19 22:27:07 +00:00
|
|
|
defer deflater.Close()
|
2016-03-19 14:07:44 +00:00
|
|
|
|
2016-03-19 22:27:07 +00:00
|
|
|
// Unmarshal
|
2016-10-03 17:55:37 +00:00
|
|
|
rdata := &data.ResponseData{}
|
|
|
|
err := json.NewDecoder(deflater).Decode(rdata)
|
|
|
|
|
|
|
|
return rdata, err
|
|
|
|
}
|
|
|
|
|
2017-10-18 16:22:14 +00:00
|
|
|
func (coll *Collector) saveResponse(addr *net.UDPAddr, res *data.ResponseData) {
|
2016-10-03 17:55:37 +00:00
|
|
|
// Search for NodeID
|
2017-01-20 21:27:44 +00:00
|
|
|
var nodeID string
|
2019-01-24 01:56:13 +00:00
|
|
|
if val := res.Nodeinfo; val != nil {
|
2017-01-20 21:27:44 +00:00
|
|
|
nodeID = val.NodeID
|
2016-10-03 17:55:37 +00:00
|
|
|
} else if val := res.Neighbours; val != nil {
|
2017-01-20 21:27:44 +00:00
|
|
|
nodeID = val.NodeID
|
2016-10-03 17:55:37 +00:00
|
|
|
} else if val := res.Statistics; val != nil {
|
2017-01-20 21:27:44 +00:00
|
|
|
nodeID = val.NodeID
|
2016-10-03 17:55:37 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 21:26:16 +00:00
|
|
|
// Check length of nodeID
|
2017-01-20 21:27:44 +00:00
|
|
|
if len(nodeID) != 12 {
|
2019-01-17 12:26:16 +00:00
|
|
|
log.WithFields(map[string]interface{}{
|
|
|
|
"node_id": nodeID,
|
|
|
|
"address": addr.String(),
|
|
|
|
}).Warn("invalid NodeID")
|
2016-10-03 17:55:37 +00:00
|
|
|
return
|
2016-03-19 14:07:44 +00:00
|
|
|
}
|
2017-01-29 21:26:16 +00:00
|
|
|
|
2017-09-27 11:55:02 +00:00
|
|
|
// Set fields to nil if nodeID is inconsistent
|
|
|
|
if res.Statistics != nil && res.Statistics.NodeID != nodeID {
|
|
|
|
res.Statistics = nil
|
|
|
|
}
|
|
|
|
if res.Neighbours != nil && res.Neighbours.NodeID != nodeID {
|
|
|
|
res.Neighbours = nil
|
|
|
|
}
|
2019-01-24 01:56:13 +00:00
|
|
|
if res.Nodeinfo != nil && res.Nodeinfo.NodeID != nodeID {
|
|
|
|
res.Nodeinfo = nil
|
2017-09-27 11:55:02 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 21:14:40 +00:00
|
|
|
// Process the data and update IP address
|
2017-01-20 21:27:44 +00:00
|
|
|
node := coll.nodes.Update(nodeID, res)
|
2017-10-18 16:22:14 +00:00
|
|
|
node.Address = addr
|
2016-03-19 14:07:44 +00:00
|
|
|
|
2017-04-17 23:48:38 +00:00
|
|
|
// Store statistics in database
|
2017-09-27 11:55:02 +00:00
|
|
|
if db := coll.db; db != nil {
|
|
|
|
db.InsertNode(node)
|
|
|
|
|
|
|
|
// Store link data
|
|
|
|
if neighbours := node.Neighbours; neighbours != nil {
|
2017-05-20 12:46:29 +00:00
|
|
|
coll.nodes.RLock()
|
2017-09-27 11:55:02 +00:00
|
|
|
for _, link := range coll.nodes.NodeLinks(node) {
|
|
|
|
db.InsertLink(&link, node.Lastseen.GetTime())
|
|
|
|
}
|
2017-05-20 12:46:29 +00:00
|
|
|
coll.nodes.RUnlock()
|
2017-09-27 11:55:02 +00:00
|
|
|
}
|
2016-10-03 17:55:37 +00:00
|
|
|
}
|
2016-03-19 00:50:23 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 16:22:14 +00:00
|
|
|
func (coll *Collector) receiver(conn *net.UDPConn) {
|
2016-02-25 20:06:37 +00:00
|
|
|
buf := make([]byte, maxDataGramSize)
|
2015-12-29 03:08:03 +00:00
|
|
|
for {
|
2017-10-18 16:22:14 +00:00
|
|
|
n, src, err := conn.ReadFromUDP(buf)
|
2015-12-29 03:08:03 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-01-17 12:26:16 +00:00
|
|
|
if conn != nil {
|
|
|
|
log.WithFields(map[string]interface{}{
|
|
|
|
"local": conn.LocalAddr(),
|
|
|
|
"remote": conn.RemoteAddr(),
|
|
|
|
}).Errorf("ReadFromUDP failed: %s", err)
|
|
|
|
} else {
|
|
|
|
log.Errorf("ReadFromUDP failed: %s", err)
|
|
|
|
}
|
2015-12-29 13:05:47 +00:00
|
|
|
return
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
2016-01-04 01:07:09 +00:00
|
|
|
|
|
|
|
raw := make([]byte, n)
|
|
|
|
copy(raw, buf)
|
|
|
|
|
|
|
|
coll.queue <- &Response{
|
2017-10-18 16:22:14 +00:00
|
|
|
Address: src,
|
2016-01-04 01:07:09 +00:00
|
|
|
Raw: raw,
|
|
|
|
}
|
2015-12-29 03:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-15 10:10:09 +00:00
|
|
|
|
|
|
|
func (coll *Collector) globalStatsWorker() {
|
|
|
|
ticker := time.NewTicker(time.Minute)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-coll.stop:
|
2017-01-29 21:26:16 +00:00
|
|
|
ticker.Stop()
|
2016-12-15 10:10:09 +00:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
coll.saveGlobalStats()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// saves global statistics
|
|
|
|
func (coll *Collector) saveGlobalStats() {
|
2018-01-17 19:20:35 +00:00
|
|
|
stats := runtime.NewGlobalStats(coll.nodes, coll.sitesDomains)
|
2016-12-15 10:10:09 +00:00
|
|
|
|
2018-01-17 19:20:35 +00:00
|
|
|
for site, domains := range stats {
|
|
|
|
for domain, stat := range domains {
|
|
|
|
coll.db.InsertGlobals(stat, time.Now(), site, domain)
|
|
|
|
}
|
2017-11-21 14:12:06 +00:00
|
|
|
}
|
2016-12-15 10:10:09 +00:00
|
|
|
}
|