5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-10 07:30:27 +00:00

enable multicast on interfaces that come up after startup

This commit is contained in:
Arceliar 2018-06-07 22:07:19 -05:00
parent 2f8aaa5c20
commit e29f700dd6
2 changed files with 36 additions and 31 deletions

View File

@ -175,7 +175,7 @@ func (a *admin) init(c *Core, listenaddr string) {
}) })
a.addHandler("getMulticastInterfaces", []string{}, func(in admin_info) (admin_info, error) { a.addHandler("getMulticastInterfaces", []string{}, func(in admin_info) (admin_info, error) {
var intfs []string var intfs []string
for _, v := range a.core.multicast.interfaces { for _, v := range a.core.multicast.interfaces() {
intfs = append(intfs, v.Name) intfs = append(intfs, v.Name)
} }
return admin_info{"multicast_interfaces": intfs}, nil return admin_info{"multicast_interfaces": intfs}, nil

View File

@ -10,7 +10,6 @@ type multicast struct {
core *Core core *Core
sock *ipv6.PacketConn sock *ipv6.PacketConn
groupAddr string groupAddr string
interfaces []net.Interface
} }
func (m *multicast) init(core *Core) { func (m *multicast) init(core *Core) {
@ -21,31 +20,7 @@ func (m *multicast) init(core *Core) {
return return
} }
// Ask the system for network interfaces // Ask the system for network interfaces
allifaces, err := net.Interfaces() m.core.log.Println("Found", len(m.interfaces()), "multicast interface(s)")
if err != nil {
panic(err)
}
// Work out which interfaces to announce on
for _, iface := range allifaces {
if iface.Flags&net.FlagUp == 0 {
// Ignore interfaces that are down
continue
}
if iface.Flags&net.FlagMulticast == 0 {
// Ignore non-multicast interfaces
continue
}
if iface.Flags&net.FlagPointToPoint != 0 {
// Ignore point-to-point interfaces
continue
}
for _, expr := range m.core.ifceExpr {
if expr.MatchString(iface.Name) {
m.interfaces = append(m.interfaces, iface)
}
}
}
m.core.log.Println("Found", len(m.interfaces), "multicast interface(s)")
} }
func (m *multicast) start() error { func (m *multicast) start() error {
@ -75,6 +50,36 @@ func (m *multicast) start() error {
return nil return nil
} }
func (m *multicast) interfaces() []net.Interface {
// Ask the system for network interfaces
var interfaces []net.Interface
allifaces, err := net.Interfaces()
if err != nil {
panic(err)
}
// Work out which interfaces to announce on
for _, iface := range allifaces {
if iface.Flags&net.FlagUp == 0 {
// Ignore interfaces that are down
continue
}
if iface.Flags&net.FlagMulticast == 0 {
// Ignore non-multicast interfaces
continue
}
if iface.Flags&net.FlagPointToPoint != 0 {
// Ignore point-to-point interfaces
continue
}
for _, expr := range m.core.ifceExpr {
if expr.MatchString(iface.Name) {
interfaces = append(interfaces, iface)
}
}
}
return interfaces
}
func (m *multicast) announce() { func (m *multicast) announce() {
groupAddr, err := net.ResolveUDPAddr("udp6", m.groupAddr) groupAddr, err := net.ResolveUDPAddr("udp6", m.groupAddr)
if err != nil { if err != nil {
@ -88,7 +93,7 @@ func (m *multicast) announce() {
panic(err) panic(err)
} }
for { for {
for _, iface := range m.interfaces { for _, iface := range m.interfaces() {
m.sock.JoinGroup(&iface, groupAddr) m.sock.JoinGroup(&iface, groupAddr)
//err := n.sock.JoinGroup(&iface, groupAddr) //err := n.sock.JoinGroup(&iface, groupAddr)
//if err != nil { panic(err) } //if err != nil { panic(err) }