2018-05-23 10:13:53 +00:00
|
|
|
package yggdrasil
|
|
|
|
|
2018-06-12 22:50:08 +00:00
|
|
|
import (
|
2018-12-05 22:39:04 +00:00
|
|
|
"context"
|
2018-06-12 22:50:08 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-01-14 19:27:13 +00:00
|
|
|
"regexp"
|
2018-06-12 22:50:08 +00:00
|
|
|
"time"
|
2018-05-23 10:13:53 +00:00
|
|
|
|
2018-06-12 22:50:08 +00:00
|
|
|
"golang.org/x/net/ipv6"
|
|
|
|
)
|
2018-05-23 10:13:53 +00:00
|
|
|
|
|
|
|
type multicast struct {
|
2018-12-29 18:51:51 +00:00
|
|
|
core *Core
|
2018-12-30 12:04:42 +00:00
|
|
|
reconfigure chan chan error
|
2018-12-29 18:51:51 +00:00
|
|
|
sock *ipv6.PacketConn
|
|
|
|
groupAddr string
|
2019-03-06 11:06:13 +00:00
|
|
|
listeners map[string]*tcpListener
|
2019-03-10 19:08:56 +00:00
|
|
|
listenPort uint16
|
2018-05-23 10:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *multicast) init(core *Core) {
|
|
|
|
m.core = core
|
2018-12-30 12:04:42 +00:00
|
|
|
m.reconfigure = make(chan chan error, 1)
|
2019-03-06 11:06:13 +00:00
|
|
|
m.listeners = make(map[string]*tcpListener)
|
2019-03-10 19:08:56 +00:00
|
|
|
m.core.configMutex.RLock()
|
|
|
|
m.listenPort = m.core.config.LinkLocalTCPPort
|
|
|
|
m.core.configMutex.RUnlock()
|
2018-12-29 18:51:51 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2019-01-15 08:51:19 +00:00
|
|
|
e := <-m.reconfigure
|
|
|
|
e <- nil
|
2018-12-29 18:51:51 +00:00
|
|
|
}
|
|
|
|
}()
|
2018-05-23 10:13:53 +00:00
|
|
|
m.groupAddr = "[ff02::114]:9001"
|
2018-05-26 20:50:47 +00:00
|
|
|
// Check if we've been given any expressions
|
2019-01-14 19:27:13 +00:00
|
|
|
if count := len(m.interfaces()); count != 0 {
|
2019-01-27 13:31:43 +00:00
|
|
|
m.core.log.Infoln("Found", count, "multicast interface(s)")
|
2018-05-26 20:50:47 +00:00
|
|
|
}
|
2018-05-23 10:13:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
func (m *multicast) start() error {
|
2019-01-14 19:27:13 +00:00
|
|
|
if len(m.interfaces()) == 0 {
|
2019-01-27 13:31:43 +00:00
|
|
|
m.core.log.Infoln("Multicast discovery is disabled")
|
2018-05-23 10:13:53 +00:00
|
|
|
} else {
|
2019-01-27 13:31:43 +00:00
|
|
|
m.core.log.Infoln("Multicast discovery is enabled")
|
2018-05-23 10:28:20 +00:00
|
|
|
addr, err := net.ResolveUDPAddr("udp", m.groupAddr)
|
|
|
|
if err != nil {
|
2018-05-27 21:13:37 +00:00
|
|
|
return err
|
2018-05-23 10:28:20 +00:00
|
|
|
}
|
|
|
|
listenString := fmt.Sprintf("[::]:%v", addr.Port)
|
2018-12-05 22:39:04 +00:00
|
|
|
lc := net.ListenConfig{
|
2019-03-01 19:26:50 +00:00
|
|
|
Control: m.multicastReuse,
|
2018-12-05 22:39:04 +00:00
|
|
|
}
|
|
|
|
conn, err := lc.ListenPacket(context.Background(), "udp6", listenString)
|
2018-05-23 10:28:20 +00:00
|
|
|
if err != nil {
|
2018-05-27 21:13:37 +00:00
|
|
|
return err
|
2018-05-23 10:28:20 +00:00
|
|
|
}
|
|
|
|
m.sock = ipv6.NewPacketConn(conn)
|
|
|
|
if err = m.sock.SetControlMessage(ipv6.FlagDst, true); err != nil {
|
|
|
|
// Windows can't set this flag, so we need to handle it in other ways
|
|
|
|
}
|
2018-05-23 10:13:53 +00:00
|
|
|
|
2019-03-10 18:05:27 +00:00
|
|
|
go m.multicastStarted()
|
2018-05-23 10:28:20 +00:00
|
|
|
go m.listen()
|
|
|
|
go m.announce()
|
|
|
|
}
|
2018-05-27 21:13:37 +00:00
|
|
|
return nil
|
2018-05-23 10:13:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 12:07:33 +00:00
|
|
|
func (m *multicast) interfaces() map[string]net.Interface {
|
2019-01-14 19:27:13 +00:00
|
|
|
// Get interface expressions from config
|
|
|
|
m.core.configMutex.RLock()
|
|
|
|
exprs := m.core.config.MulticastInterfaces
|
|
|
|
m.core.configMutex.RUnlock()
|
2018-06-08 03:07:19 +00:00
|
|
|
// Ask the system for network interfaces
|
2019-03-06 12:07:33 +00:00
|
|
|
interfaces := make(map[string]net.Interface)
|
2018-06-08 03:07:19 +00:00
|
|
|
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
|
|
|
|
}
|
2019-01-14 19:27:13 +00:00
|
|
|
for _, expr := range exprs {
|
2019-03-06 11:06:13 +00:00
|
|
|
// Compile each regular expression
|
2019-01-14 19:27:13 +00:00
|
|
|
e, err := regexp.Compile(expr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-03-06 11:06:13 +00:00
|
|
|
// Does the interface match the regular expression? Store it if so
|
2019-01-14 19:27:13 +00:00
|
|
|
if e.MatchString(iface.Name) {
|
2019-03-06 12:07:33 +00:00
|
|
|
interfaces[iface.Name] = iface
|
2018-06-08 03:07:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return interfaces
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:13:53 +00:00
|
|
|
func (m *multicast) announce() {
|
|
|
|
groupAddr, err := net.ResolveUDPAddr("udp6", m.groupAddr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
destAddr, err := net.ResolveUDPAddr("udp6", m.groupAddr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for {
|
2019-03-06 12:07:33 +00:00
|
|
|
interfaces := m.interfaces()
|
2019-03-06 12:09:57 +00:00
|
|
|
// There might be interfaces that we configured listeners for but are no
|
|
|
|
// longer up - if that's the case then we should stop the listeners
|
|
|
|
for name, listener := range m.listeners {
|
2019-03-13 17:43:33 +00:00
|
|
|
// Prepare our stop function!
|
|
|
|
stop := func() {
|
2019-03-06 12:09:57 +00:00
|
|
|
listener.stop <- true
|
|
|
|
delete(m.listeners, name)
|
|
|
|
m.core.log.Debugln("No longer multicasting on", name)
|
|
|
|
}
|
2019-03-13 17:43:33 +00:00
|
|
|
// If the interface is no longer visible on the system then stop the
|
|
|
|
// listener, as another one will be started further down
|
|
|
|
if _, ok := interfaces[name]; !ok {
|
|
|
|
stop()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// It's possible that the link-local listener address has changed so if
|
|
|
|
// that is the case then we should clean up the interface listener
|
|
|
|
found := false
|
|
|
|
listenaddr, err := net.ResolveTCPAddr("tcp6", listener.listener.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
stop()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Find the interface that matches the listener
|
|
|
|
if intf, err := net.InterfaceByName(name); err == nil {
|
|
|
|
if addrs, err := intf.Addrs(); err == nil {
|
|
|
|
// Loop through the addresses attached to that listener and see if any
|
|
|
|
// of them match the current address of the listener
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if ip, _, err := net.ParseCIDR(addr.String()); err == nil {
|
|
|
|
// Does the interface address match our listener address?
|
|
|
|
if ip.Equal(listenaddr.IP) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the address has not been found on the adapter then we should stop
|
|
|
|
// and clean up the TCP listener. A new one will be created below if a
|
|
|
|
// suitable link-local address is found
|
|
|
|
if !found {
|
|
|
|
stop()
|
|
|
|
}
|
2019-03-06 12:09:57 +00:00
|
|
|
}
|
2019-03-06 12:07:33 +00:00
|
|
|
// Now that we have a list of valid interfaces from the operating system,
|
|
|
|
// we can start checking if we can send multicasts on them
|
|
|
|
for _, iface := range interfaces {
|
2019-03-06 11:06:13 +00:00
|
|
|
// Find interface addresses
|
2018-05-23 10:13:53 +00:00
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
addrIP, _, _ := net.ParseCIDR(addr.String())
|
2019-03-06 11:06:13 +00:00
|
|
|
// Ignore IPv4 addresses
|
2018-05-23 10:13:53 +00:00
|
|
|
if addrIP.To4() != nil {
|
|
|
|
continue
|
2019-03-06 11:06:13 +00:00
|
|
|
}
|
|
|
|
// Ignore non-link-local addresses
|
2018-05-23 10:13:53 +00:00
|
|
|
if !addrIP.IsLinkLocalUnicast() {
|
|
|
|
continue
|
|
|
|
}
|
2019-03-06 11:06:13 +00:00
|
|
|
// Join the multicast group
|
|
|
|
m.sock.JoinGroup(&iface, groupAddr)
|
|
|
|
// Try and see if we already have a TCP listener for this interface
|
|
|
|
var listener *tcpListener
|
2019-03-06 12:07:33 +00:00
|
|
|
if l, ok := m.listeners[iface.Name]; !ok || l.listener == nil {
|
2019-03-06 11:06:13 +00:00
|
|
|
// No listener was found - let's create one
|
2019-03-10 19:08:56 +00:00
|
|
|
listenaddr := fmt.Sprintf("[%s%%%s]:%d", addrIP, iface.Name, m.listenPort)
|
2019-03-06 16:40:48 +00:00
|
|
|
if li, err := m.core.link.tcp.listen(listenaddr); err == nil {
|
2019-03-06 12:07:33 +00:00
|
|
|
m.core.log.Debugln("Started multicasting on", iface.Name)
|
2019-03-06 11:06:13 +00:00
|
|
|
// Store the listener so that we can stop it later if needed
|
2019-03-06 16:40:48 +00:00
|
|
|
m.listeners[iface.Name] = li
|
|
|
|
listener = li
|
2019-03-10 19:08:56 +00:00
|
|
|
} else {
|
|
|
|
m.core.log.Warnln("Not multicasting on", iface.Name, "due to error:", err)
|
2019-03-06 11:06:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// An existing listener was found
|
|
|
|
listener = m.listeners[iface.Name]
|
|
|
|
}
|
2019-03-06 12:07:33 +00:00
|
|
|
// Make sure nothing above failed for some reason
|
|
|
|
if listener == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-03-06 11:06:13 +00:00
|
|
|
// Get the listener details and construct the multicast beacon
|
2019-03-06 12:07:33 +00:00
|
|
|
lladdr := listener.listener.Addr().String()
|
2019-03-06 11:06:13 +00:00
|
|
|
if a, err := net.ResolveTCPAddr("tcp6", lladdr); err == nil {
|
2019-03-09 09:24:52 +00:00
|
|
|
a.Zone = ""
|
2019-03-06 11:06:13 +00:00
|
|
|
destAddr.Zone = iface.Name
|
|
|
|
msg := []byte(a.String())
|
|
|
|
m.sock.WriteTo(msg, nil, destAddr)
|
|
|
|
}
|
2018-05-23 10:13:53 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-03-06 12:15:40 +00:00
|
|
|
time.Sleep(time.Second * 15)
|
2018-05-23 10:13:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *multicast) listen() {
|
|
|
|
groupAddr, err := net.ResolveUDPAddr("udp6", m.groupAddr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
bs := make([]byte, 2048)
|
|
|
|
for {
|
|
|
|
nBytes, rcm, fromAddr, err := m.sock.ReadFrom(bs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if rcm != nil {
|
|
|
|
// Windows can't set the flag needed to return a non-nil value here
|
|
|
|
// So only make these checks if we get something useful back
|
|
|
|
// TODO? Skip them always, I'm not sure if they're really needed...
|
|
|
|
if !rcm.Dst.IsLinkLocalMulticast() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !rcm.Dst.Equal(groupAddr.IP) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
anAddr := string(bs[:nBytes])
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp6", anAddr)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
2018-06-12 22:50:08 +00:00
|
|
|
}
|
2018-05-23 10:13:53 +00:00
|
|
|
from := fromAddr.(*net.UDPAddr)
|
|
|
|
if addr.IP.String() != from.IP.String() {
|
|
|
|
continue
|
|
|
|
}
|
2019-03-09 09:24:52 +00:00
|
|
|
addr.Zone = ""
|
|
|
|
if err := m.core.link.call("tcp://"+addr.String(), from.Zone); err != nil {
|
|
|
|
m.core.log.Debugln("Call from multicast failed:", err)
|
|
|
|
}
|
2018-05-23 10:13:53 +00:00
|
|
|
}
|
|
|
|
}
|