[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{}) nodes := runtime.NewNodes(&runtime.Config{})
collector := respond.NewCollector(nil, nodes, iface, 0) collector := respond.NewCollector(nil, nodes, iface, iface, iface, 0)
collector.SendPacket(net.ParseIP(dstAddress)) collector.SendPacket(net.UDPAddr{
IP: net.ParseIP(dstAddress),
Zone: iface,
})
time.Sleep(time.Second) time.Sleep(time.Second)

View File

@ -42,6 +42,12 @@ func main() {
if err != nil { if err != nil {
panic(err) 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) connections, err = all.Connect(config.Database.Connection)
if err != nil { if err != nil {
@ -74,7 +80,7 @@ func main() {
time.Sleep(delay) 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) collector.Start(config.Respondd.CollectInterval.Duration)
defer collector.Close() defer collector.Close()
} }

View File

@ -5,8 +5,13 @@ enable = true
synchronize = "1m" synchronize = "1m"
# how oftern request per multicast # how oftern request per multicast
collect_interval = "1m" collect_interval = "1m"
# on which interface # on which interface to listen
interface = "eth0" 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 # define a port to listen
# (no or 0 would choose at port at his own) # (no or 0 would choose at port at his own)
#port = 10001 #port = 10001

View File

@ -19,7 +19,8 @@ import (
type Collector struct { type Collector struct {
connection *net.UDPConn // UDP socket connection *net.UDPConn // UDP socket
queue chan *Response // received responses queue chan *Response // received responses
iface string ifaceSendUnicast string
ifaceSendMulticast string
db database.Connection db database.Connection
nodes *runtime.Nodes nodes *runtime.Nodes
interval time.Duration // Interval for multicast packets interval time.Duration // Interval for multicast packets
@ -27,8 +28,8 @@ type Collector struct {
} }
// NewCollector creates a Collector struct // NewCollector creates a Collector struct
func NewCollector(db database.Connection, nodes *runtime.Nodes, iface string, port int) *Collector { func NewCollector(db database.Connection, nodes *runtime.Nodes, ifaceListen string, ifaceSendUnicast string, ifaceSendMulticast string, port int) *Collector {
linkLocalAddr, err := getLinkLocalAddr(iface) linkLocalAddr, err := getLinkLocalAddr(ifaceListen)
if err != nil { if err != nil {
log.Panic(err) 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{ conn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: linkLocalAddr, IP: linkLocalAddr,
Port: port, Port: port,
Zone: iface, Zone: ifaceListen,
}) })
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
@ -48,7 +49,8 @@ func NewCollector(db database.Connection, nodes *runtime.Nodes, iface string, po
connection: conn, connection: conn,
db: db, db: db,
nodes: nodes, nodes: nodes,
iface: iface, ifaceSendUnicast: ifaceSendUnicast,
ifaceSendMulticast: ifaceSendMulticast,
queue: make(chan *Response, 400), queue: make(chan *Response, 400),
stop: make(chan interface{}), stop: make(chan interface{}),
} }
@ -117,7 +119,10 @@ func (coll *Collector) sendOnce() {
func (coll *Collector) sendMulticast() { func (coll *Collector) sendMulticast() {
log.Println("sending multicast") 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 // 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 // Send unicast packets
log.Printf("sending unicast to %d nodes", len(nodes)) log.Printf("sending unicast to %d nodes", len(nodes))
for _, node := range nodes { for _, node := range nodes {
coll.SendPacket(node.Address) coll.SendPacket(net.UDPAddr{
IP: node.Address,
Zone: coll.ifaceSendUnicast,
})
time.Sleep(10 * time.Millisecond) time.Sleep(10 * time.Millisecond)
} }
} }
// SendPacket sends a UDP request to the given unicast or multicast address // SendPacket sends a UDP request to the given unicast or multicast address
func (coll *Collector) SendPacket(address net.IP) { func (coll *Collector) SendPacket(addr net.UDPAddr) {
addr := net.UDPAddr{ addr.Port = port
IP: address,
Port: port,
Zone: coll.iface,
}
if _, err := coll.connection.WriteToUDP([]byte("GET nodeinfo statistics neighbours"), &addr); err != nil { if _, err := coll.connection.WriteToUDP([]byte("GET nodeinfo statistics neighbours"), &addr); err != nil {
log.Println("WriteToUDP failed:", err) log.Println("WriteToUDP failed:", err)
} }

View File

@ -11,7 +11,9 @@ type Config struct {
Respondd struct { Respondd struct {
Enable bool `toml:"enable"` Enable bool `toml:"enable"`
Synchronize Duration `toml:"synchronize"` Synchronize Duration `toml:"synchronize"`
Interface string `toml:"interface"` InterfaceListen string `toml:"interface"`
InterfaceSendMulticast string `toml:"interface_send_multicast"`
InterfaceSendUnicast string `toml:"interface_send_unicast"`
Port int `toml:"port"` Port int `toml:"port"`
CollectInterval Duration `toml:"collect_interval"` CollectInterval Duration `toml:"collect_interval"`
} }

View File

@ -15,7 +15,7 @@ func TestReadConfig(t *testing.T) {
assert.NotNil(config) assert.NotNil(config)
assert.True(config.Respondd.Enable) 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(time.Minute, config.Respondd.CollectInterval.Duration)
assert.Equal(2, config.Meshviewer.Version) assert.Equal(2, config.Meshviewer.Version)