[TASK] split interfaces by listen, send multicast and send unicast (needed by babel)

This commit is contained in:
Martin Geno 2017-06-29 19:06:53 +02:00
parent fd7e712282
commit 55efa5f8bc
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
6 changed files with 54 additions and 35 deletions

View File

@ -19,8 +19,11 @@ func main() {
nodes := runtime.NewNodes(&runtime.Config{})
collector := respond.NewCollector(nil, nodes, iface, 0)
collector.SendPacket(net.ParseIP(dstAddress))
collector := respond.NewCollector(nil, nodes, iface, iface, iface, 0)
collector.SendPacket(net.UDPAddr{
IP: net.ParseIP(dstAddress),
Zone: iface,
})
time.Sleep(time.Second)

View File

@ -42,6 +42,12 @@ func main() {
if err != nil {
panic(err)
}
if config.Respondd.InterfaceSendUnicast == "" {
config.Respondd.InterfaceSendUnicast = config.Respondd.InterfaceListen
}
if config.Respondd.InterfaceSendMulticast == "" {
config.Respondd.InterfaceSendMulticast = config.Respondd.InterfaceListen
}
connections, err = all.Connect(config.Database.Connection)
if err != nil {
@ -74,7 +80,7 @@ func main() {
time.Sleep(delay)
}
collector = respond.NewCollector(connections, nodes, config.Respondd.Interface, config.Respondd.Port)
collector = respond.NewCollector(connections, nodes, config.Respondd.InterfaceListen, config.Respondd.InterfaceSendMulticast, config.Respondd.InterfaceSendUnicast, config.Respondd.Port)
collector.Start(config.Respondd.CollectInterval.Duration)
defer collector.Close()
}

View File

@ -5,8 +5,13 @@ enable = true
synchronize = "1m"
# how oftern request per multicast
collect_interval = "1m"
# on which interface
# on which interface to listen
interface = "eth0"
# send unicast request (default: see interface)
## interface_send_unicast = "eth0"
# send multicast request (default: see interface)
# interface_send_multicast = "eth1"
# define a port to listen
# (no or 0 would choose at port at his own)
#port = 10001

View File

@ -17,18 +17,19 @@ import (
// Collector for a specificle respond messages
type Collector struct {
connection *net.UDPConn // UDP socket
queue chan *Response // received responses
iface string
db database.Connection
nodes *runtime.Nodes
interval time.Duration // Interval for multicast packets
stop chan interface{}
connection *net.UDPConn // UDP socket
queue chan *Response // received responses
ifaceSendUnicast string
ifaceSendMulticast string
db database.Connection
nodes *runtime.Nodes
interval time.Duration // Interval for multicast packets
stop chan interface{}
}
// NewCollector creates a Collector struct
func NewCollector(db database.Connection, nodes *runtime.Nodes, iface string, port int) *Collector {
linkLocalAddr, err := getLinkLocalAddr(iface)
func NewCollector(db database.Connection, nodes *runtime.Nodes, ifaceListen string, ifaceSendUnicast string, ifaceSendMulticast string, port int) *Collector {
linkLocalAddr, err := getLinkLocalAddr(ifaceListen)
if err != nil {
log.Panic(err)
}
@ -37,7 +38,7 @@ func NewCollector(db database.Connection, nodes *runtime.Nodes, iface string, po
conn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: linkLocalAddr,
Port: port,
Zone: iface,
Zone: ifaceListen,
})
if err != nil {
log.Panic(err)
@ -45,12 +46,13 @@ func NewCollector(db database.Connection, nodes *runtime.Nodes, iface string, po
conn.SetReadBuffer(maxDataGramSize)
collector := &Collector{
connection: conn,
db: db,
nodes: nodes,
iface: iface,
queue: make(chan *Response, 400),
stop: make(chan interface{}),
connection: conn,
db: db,
nodes: nodes,
ifaceSendUnicast: ifaceSendUnicast,
ifaceSendMulticast: ifaceSendMulticast,
queue: make(chan *Response, 400),
stop: make(chan interface{}),
}
go collector.receiver()
@ -117,7 +119,10 @@ func (coll *Collector) sendOnce() {
func (coll *Collector) sendMulticast() {
log.Println("sending multicast")
coll.SendPacket(net.ParseIP(multiCastGroup))
coll.SendPacket(net.UDPAddr{
IP: net.ParseIP(multiCastGroup),
Zone: coll.ifaceSendMulticast,
})
}
// Send unicast packets to nodes that did not answer the multicast
@ -132,19 +137,17 @@ func (coll *Collector) sendUnicasts(seenBefore jsontime.Time) {
// Send unicast packets
log.Printf("sending unicast to %d nodes", len(nodes))
for _, node := range nodes {
coll.SendPacket(node.Address)
coll.SendPacket(net.UDPAddr{
IP: node.Address,
Zone: coll.ifaceSendUnicast,
})
time.Sleep(10 * time.Millisecond)
}
}
// SendPacket sends a UDP request to the given unicast or multicast address
func (coll *Collector) SendPacket(address net.IP) {
addr := net.UDPAddr{
IP: address,
Port: port,
Zone: coll.iface,
}
func (coll *Collector) SendPacket(addr net.UDPAddr) {
addr.Port = port
if _, err := coll.connection.WriteToUDP([]byte("GET nodeinfo statistics neighbours"), &addr); err != nil {
log.Println("WriteToUDP failed:", err)
}

View File

@ -9,11 +9,13 @@ import (
//Config the config File of this daemon
type Config struct {
Respondd struct {
Enable bool `toml:"enable"`
Synchronize Duration `toml:"synchronize"`
Interface string `toml:"interface"`
Port int `toml:"port"`
CollectInterval Duration `toml:"collect_interval"`
Enable bool `toml:"enable"`
Synchronize Duration `toml:"synchronize"`
InterfaceListen string `toml:"interface"`
InterfaceSendMulticast string `toml:"interface_send_multicast"`
InterfaceSendUnicast string `toml:"interface_send_unicast"`
Port int `toml:"port"`
CollectInterval Duration `toml:"collect_interval"`
}
Webserver struct {
Enable bool `toml:"enable"`

View File

@ -15,7 +15,7 @@ func TestReadConfig(t *testing.T) {
assert.NotNil(config)
assert.True(config.Respondd.Enable)
assert.Equal("eth0", config.Respondd.Interface)
assert.Equal("eth0", config.Respondd.InterfaceListen)
assert.Equal(time.Minute, config.Respondd.CollectInterval.Duration)
assert.Equal(2, config.Meshviewer.Version)