Tidy up somewhat

This commit is contained in:
Neil Alexander 2020-05-31 22:35:57 +01:00
parent 872a6d18e8
commit 5101205866
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
2 changed files with 35 additions and 24 deletions

View file

@ -28,6 +28,7 @@ type Multicast struct {
listeners map[string]*listenerInfo listeners map[string]*listenerInfo
listenPort uint16 listenPort uint16
isOpen bool isOpen bool
stop chan struct{}
_interfaces map[string]interfaceInfo _interfaces map[string]interfaceInfo
} }
@ -74,6 +75,7 @@ func (m *Multicast) _start() error {
if len(m.config.GetCurrent().MulticastInterfaces) == 0 { if len(m.config.GetCurrent().MulticastInterfaces) == 0 {
return nil return nil
} }
m.stop = make(chan struct{})
m.log.Infoln("Starting multicast module") m.log.Infoln("Starting multicast module")
addr, err := net.ResolveUDPAddr("udp", m.groupAddr) addr, err := net.ResolveUDPAddr("udp", m.groupAddr)
if err != nil { if err != nil {
@ -121,6 +123,7 @@ func (m *Multicast) Stop() error {
func (m *Multicast) _stop() error { func (m *Multicast) _stop() error {
m.log.Infoln("Stopping multicast module") m.log.Infoln("Stopping multicast module")
close(m.stop)
m.isOpen = false m.isOpen = false
if m.sock != nil { if m.sock != nil {
m.sock.Close() m.sock.Close()
@ -158,8 +161,10 @@ func (m *Multicast) Interfaces() map[string]net.Interface {
interfaces := make(map[string]net.Interface) interfaces := make(map[string]net.Interface)
phony.Block(m, func() { phony.Block(m, func() {
for _, info := range m._interfaces { for _, info := range m._interfaces {
if len(info.addrs) > 0 {
interfaces[info.iface.Name] = info.iface interfaces[info.iface.Name] = info.iface
} }
}
}) })
return interfaces return interfaces
} }
@ -188,7 +193,7 @@ func (m *Multicast) _announce() {
} }
// If the interface is no longer visible on the system then stop the // If the interface is no longer visible on the system then stop the
// listener, as another one will be started further down // listener, as another one will be started further down
if _, ok := m._interfaces[name]; !ok { if intf, ok := m._interfaces[name]; !ok || len(intf.addrs) == 0 {
stop() stop()
continue continue
} }
@ -201,7 +206,7 @@ func (m *Multicast) _announce() {
continue continue
} }
// Find the interface that matches the listener // Find the interface that matches the listener
if info, ok := m._interfaces[name]; ok { if info, ok := m._interfaces[name]; ok && len(info.addrs) > 0 {
for _, addr := range info.addrs { for _, addr := range info.addrs {
if ip, _, err := net.ParseCIDR(addr.String()); err == nil { if ip, _, err := net.ParseCIDR(addr.String()); err == nil {
// Does the interface address match our listener address? // Does the interface address match our listener address?

View file

@ -3,7 +3,6 @@
package multicast package multicast
import ( import (
"fmt"
"net" "net"
"regexp" "regexp"
"syscall" "syscall"
@ -21,7 +20,7 @@ func (m *Multicast) _multicastStarted() {
addrClose := make(chan struct{}) addrClose := make(chan struct{})
errorCallback := func(err error) { errorCallback := func(err error) {
fmt.Println("Netlink error:", err) m.log.Warnln("Netlink error:", err)
} }
linkSubscribeOptions := netlink.LinkSubscribeOptions{ linkSubscribeOptions := netlink.LinkSubscribeOptions{
@ -45,10 +44,10 @@ func (m *Multicast) _multicastStarted() {
} }
}() }()
fmt.Println("Listening for netlink changes") m.log.Debugln("Listening for netlink interface changes")
go func() { go func() {
defer fmt.Println("No longer listening for netlink changes") defer m.log.Debugln("No longer listening for netlink interface changes")
indexToIntf := map[int]string{} indexToIntf := map[int]string{}
@ -60,10 +59,11 @@ func (m *Multicast) _multicastStarted() {
case change := <-linkChanges: case change := <-linkChanges:
attrs := change.Attrs() attrs := change.Attrs()
add := true add := true
add = add && attrs.Flags&net.FlagUp == 1 add = add && attrs.Flags&net.FlagUp != 0
//add = add && attrs.Flags&net.FlagMulticast == 1 add = add && attrs.Flags&net.FlagMulticast != 0
//add = add && attrs.Flags&net.FlagPointToPoint == 0 add = add && attrs.Flags&net.FlagPointToPoint == 0
if add {
match := false match := false
for _, expr := range exprs { for _, expr := range exprs {
e, err := regexp.Compile(expr) e, err := regexp.Compile(expr)
@ -76,6 +76,7 @@ func (m *Multicast) _multicastStarted() {
} }
} }
add = add && match add = add && match
}
if add { if add {
indexToIntf[attrs.Index] = attrs.Name indexToIntf[attrs.Index] = attrs.Name
@ -84,7 +85,7 @@ func (m *Multicast) _multicastStarted() {
if err != nil { if err != nil {
return return
} }
fmt.Println("Link added:", attrs.Name) m.log.Debugln("Multicast on interface", attrs.Name, "enabled")
if info, ok := m._interfaces[attrs.Name]; ok { if info, ok := m._interfaces[attrs.Name]; ok {
info.iface = *iface info.iface = *iface
m._interfaces[attrs.Name] = info m._interfaces[attrs.Name] = info
@ -97,7 +98,7 @@ func (m *Multicast) _multicastStarted() {
} else { } else {
delete(indexToIntf, attrs.Index) delete(indexToIntf, attrs.Index)
m.Act(nil, func() { m.Act(nil, func() {
fmt.Println("Link removed:", attrs.Name) m.log.Debugln("Multicast on interface", attrs.Name, "disabled")
delete(m._interfaces, attrs.Name) delete(m._interfaces, attrs.Name)
}) })
} }
@ -113,7 +114,7 @@ func (m *Multicast) _multicastStarted() {
if add { if add {
m.Act(nil, func() { m.Act(nil, func() {
fmt.Println("Addr added:", change) m.log.Debugln("Multicast address", change.LinkAddress.IP, "on", name, "enabled")
if info, ok := m._interfaces[name]; ok { if info, ok := m._interfaces[name]; ok {
info.addrs = append(info.addrs, &net.IPAddr{ info.addrs = append(info.addrs, &net.IPAddr{
IP: change.LinkAddress.IP, IP: change.LinkAddress.IP,
@ -124,7 +125,7 @@ func (m *Multicast) _multicastStarted() {
}) })
} else { } else {
m.Act(nil, func() { m.Act(nil, func() {
fmt.Println("Addr removed:", change) m.log.Debugln("Multicast address", change.LinkAddress.IP, "on", name, "disabled")
if info, ok := m._interfaces[name]; ok { if info, ok := m._interfaces[name]; ok {
info.addrs = nil info.addrs = nil
m._interfaces[name] = info m._interfaces[name] = info
@ -137,6 +138,11 @@ func (m *Multicast) _multicastStarted() {
case <-addrClose: case <-addrClose:
return return
case <-m.stop:
close(linkClose)
close(addrClose)
return
} }
} }
}() }()