2019-03-28 16:13:14 +00:00
|
|
|
package multicast
|
2018-05-23 11:13:53 +01:00
|
|
|
|
2018-06-12 17:50:08 -05:00
|
|
|
import (
|
2018-12-05 22:39:04 +00:00
|
|
|
"context"
|
2018-06-12 17:50:08 -05:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-01-14 19:27:13 +00:00
|
|
|
"regexp"
|
2018-06-12 17:50:08 -05:00
|
|
|
"time"
|
2018-05-23 11:13:53 +01:00
|
|
|
|
2019-09-18 16:51:46 +01:00
|
|
|
"github.com/Arceliar/phony"
|
2019-03-28 16:13:14 +00:00
|
|
|
"github.com/gologme/log"
|
|
|
|
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
|
2018-06-12 17:50:08 -05:00
|
|
|
"golang.org/x/net/ipv6"
|
|
|
|
)
|
2018-05-23 11:13:53 +01:00
|
|
|
|
2020-05-02 22:26:41 +01:00
|
|
|
const (
|
|
|
|
// GroupAddr contains the multicast group and port used for multicast packets.
|
|
|
|
GroupAddr = "[ff02::114]:9001"
|
|
|
|
)
|
|
|
|
|
2019-03-29 18:18:31 +00:00
|
|
|
// Multicast represents the multicast advertisement and discovery mechanism used
|
|
|
|
// by Yggdrasil to find peers on the same subnet. When a beacon is received on a
|
|
|
|
// configured multicast interface, Yggdrasil will attempt to peer with that node
|
|
|
|
// automatically.
|
2019-03-28 16:13:14 +00:00
|
|
|
type Multicast struct {
|
2019-09-18 16:51:46 +01:00
|
|
|
phony.Inbox
|
2020-05-02 22:37:12 +01:00
|
|
|
core *yggdrasil.Core
|
|
|
|
config *config.NodeState
|
|
|
|
log *log.Logger
|
|
|
|
sock *ipv6.PacketConn
|
|
|
|
groupAddr *net.UDPAddr
|
|
|
|
listeners map[string]*multicastInterface
|
|
|
|
listenPort uint16
|
|
|
|
isOpen bool
|
|
|
|
monitor *time.Timer
|
|
|
|
platformhandler *time.Timer
|
2018-05-23 11:13:53 +01:00
|
|
|
}
|
|
|
|
|
2020-05-02 22:26:41 +01:00
|
|
|
type multicastInterface struct {
|
|
|
|
phony.Inbox
|
|
|
|
sock *ipv6.PacketConn
|
|
|
|
destAddr net.UDPAddr
|
2019-09-20 17:42:42 -05:00
|
|
|
listener *yggdrasil.TcpListener
|
2020-05-02 22:26:41 +01:00
|
|
|
zone string
|
|
|
|
timer *time.Timer
|
2019-09-20 17:42:42 -05:00
|
|
|
interval time.Duration
|
2020-05-02 22:26:41 +01:00
|
|
|
stop chan interface{}
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:18:31 +00:00
|
|
|
// Init prepares the multicast interface for use.
|
2020-05-02 22:26:41 +01:00
|
|
|
func (m *Multicast) Init(core *yggdrasil.Core, state *config.NodeState, log *log.Logger, options interface{}) (err error) {
|
2018-05-23 11:13:53 +01:00
|
|
|
m.core = core
|
2019-03-28 19:09:19 +00:00
|
|
|
m.config = state
|
2019-03-28 16:13:14 +00:00
|
|
|
m.log = log
|
2020-05-02 22:26:41 +01:00
|
|
|
m.listeners = make(map[string]*multicastInterface)
|
2019-07-27 15:00:09 +01:00
|
|
|
current := m.config.GetCurrent()
|
2019-03-28 00:30:25 +00:00
|
|
|
m.listenPort = current.LinkLocalTCPPort
|
2020-05-02 22:26:41 +01:00
|
|
|
m.groupAddr, err = net.ResolveUDPAddr("udp6", GroupAddr)
|
|
|
|
return
|
2018-05-23 11:13:53 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 18:18:31 +00:00
|
|
|
// Start starts the multicast interface. This launches goroutines which will
|
|
|
|
// listen for multicast beacons from other hosts and will advertise multicast
|
|
|
|
// beacons out to the network.
|
2019-03-28 16:13:14 +00:00
|
|
|
func (m *Multicast) Start() error {
|
2019-10-24 09:25:31 +01:00
|
|
|
var err error
|
|
|
|
phony.Block(m, func() {
|
|
|
|
err = m._start()
|
|
|
|
})
|
2019-10-24 09:54:57 +01:00
|
|
|
m.log.Debugln("Started multicast module")
|
2019-10-24 09:25:31 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Multicast) _start() error {
|
2019-10-24 23:31:47 +01:00
|
|
|
if m.isOpen {
|
2019-10-24 09:25:31 +01:00
|
|
|
return fmt.Errorf("multicast module is already started")
|
|
|
|
}
|
|
|
|
if len(m.config.GetCurrent().MulticastInterfaces) == 0 {
|
2019-10-24 09:54:57 +01:00
|
|
|
return nil
|
2019-10-24 09:25:31 +01:00
|
|
|
}
|
|
|
|
m.log.Infoln("Starting multicast module")
|
2020-05-02 22:26:41 +01:00
|
|
|
addr, err := net.ResolveUDPAddr("udp", GroupAddr)
|
2019-06-20 15:11:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-05-23 11:28:20 +01:00
|
|
|
}
|
2019-06-20 15:11:55 +01:00
|
|
|
listenString := fmt.Sprintf("[::]:%v", addr.Port)
|
|
|
|
lc := net.ListenConfig{
|
|
|
|
Control: m.multicastReuse,
|
|
|
|
}
|
|
|
|
conn, err := lc.ListenPacket(context.Background(), "udp6", listenString)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-24 23:31:47 +01:00
|
|
|
m.isOpen = true
|
2019-06-20 15:11:55 +01:00
|
|
|
go m.listen()
|
2019-09-18 16:51:46 +01:00
|
|
|
m.Act(m, m.multicastStarted)
|
2020-05-02 22:26:41 +01:00
|
|
|
m.Act(m, m.monitorInterfaceChanges)
|
2019-06-20 15:11:55 +01:00
|
|
|
|
2018-05-27 22:13:37 +01:00
|
|
|
return nil
|
2018-05-23 11:13:53 +01:00
|
|
|
}
|
|
|
|
|
2019-10-24 09:25:31 +01:00
|
|
|
// IsStarted returns true if the module has been started.
|
|
|
|
func (m *Multicast) IsStarted() bool {
|
2019-10-24 23:31:47 +01:00
|
|
|
var isOpen bool
|
|
|
|
phony.Block(m, func() {
|
|
|
|
isOpen = m.isOpen
|
|
|
|
})
|
|
|
|
return isOpen
|
2019-10-24 09:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the multicast module.
|
2019-10-24 09:54:57 +01:00
|
|
|
func (m *Multicast) Stop() error {
|
|
|
|
var err error
|
|
|
|
phony.Block(m, func() {
|
|
|
|
err = m._stop()
|
|
|
|
})
|
|
|
|
m.log.Debugln("Stopped multicast module")
|
2019-11-30 16:05:44 +02:00
|
|
|
return err
|
2019-10-24 09:25:31 +01:00
|
|
|
}
|
|
|
|
|
2019-10-24 09:54:57 +01:00
|
|
|
func (m *Multicast) _stop() error {
|
2019-10-24 09:25:31 +01:00
|
|
|
m.log.Infoln("Stopping multicast module")
|
2019-10-24 23:31:47 +01:00
|
|
|
m.isOpen = false
|
2020-05-02 22:37:12 +01:00
|
|
|
for name := range m.listeners {
|
|
|
|
close(m.listeners[name].stop)
|
|
|
|
delete(m.listeners, name)
|
|
|
|
}
|
2019-09-18 15:34:26 +01:00
|
|
|
if m.platformhandler != nil {
|
|
|
|
m.platformhandler.Stop()
|
|
|
|
}
|
2019-10-25 18:40:09 -05:00
|
|
|
if m.sock != nil {
|
|
|
|
m.sock.Close()
|
|
|
|
}
|
2019-10-24 09:54:57 +01:00
|
|
|
return nil
|
2019-03-28 19:09:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 22:29:52 +01:00
|
|
|
// UpdateConfig updates the multicast module with the provided config.NodeConfig
|
|
|
|
// and then signals the various module goroutines to reconfigure themselves if
|
|
|
|
// needed.
|
|
|
|
func (m *Multicast) UpdateConfig(config *config.NodeConfig) {
|
2019-10-24 09:25:31 +01:00
|
|
|
m.Act(m, func() { m._updateConfig(config) })
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Multicast) _updateConfig(config *config.NodeConfig) {
|
2019-10-24 09:54:57 +01:00
|
|
|
m.log.Infoln("Reloading multicast configuration...")
|
2019-10-24 23:31:47 +01:00
|
|
|
if m.isOpen {
|
2019-10-24 09:25:31 +01:00
|
|
|
if len(config.MulticastInterfaces) == 0 || config.LinkLocalTCPPort != m.listenPort {
|
2019-10-24 09:54:57 +01:00
|
|
|
if err := m._stop(); err != nil {
|
|
|
|
m.log.Errorln("Error stopping multicast module:", err)
|
|
|
|
}
|
2019-10-24 09:25:31 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 22:29:52 +01:00
|
|
|
m.config.Replace(*config)
|
2019-10-24 09:25:31 +01:00
|
|
|
m.listenPort = config.LinkLocalTCPPort
|
2019-10-24 23:31:47 +01:00
|
|
|
if !m.isOpen && len(config.MulticastInterfaces) > 0 {
|
2019-10-24 09:54:57 +01:00
|
|
|
if err := m._start(); err != nil {
|
|
|
|
m.log.Errorln("Error starting multicast module:", err)
|
|
|
|
}
|
2019-10-24 09:25:31 +01:00
|
|
|
}
|
2019-10-24 09:54:57 +01:00
|
|
|
m.log.Debugln("Reloaded multicast configuration successfully")
|
2019-05-17 22:29:52 +01:00
|
|
|
}
|
|
|
|
|
2020-05-02 22:26:41 +01:00
|
|
|
func (m *Multicast) monitorInterfaceChanges() {
|
|
|
|
interfaces := m.Interfaces()
|
|
|
|
|
|
|
|
// Look for interfaces we don't know about yet.
|
|
|
|
for name, intf := range interfaces {
|
|
|
|
if _, ok := m.listeners[name]; !ok {
|
|
|
|
// Look up interface addresses.
|
|
|
|
addrs, err := intf.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Find the first link-local address.
|
|
|
|
for _, addr := range addrs {
|
|
|
|
addrIP, _, _ := net.ParseCIDR(addr.String())
|
|
|
|
// Join the multicast group.
|
|
|
|
m.sock.JoinGroup(&intf, m.groupAddr)
|
|
|
|
// Construct a listener on this address.
|
|
|
|
listenaddr := fmt.Sprintf("[%s%%%s]:%d", addrIP, intf.Name, m.listenPort)
|
|
|
|
listener, err := m.core.ListenTCP(listenaddr)
|
|
|
|
if err != nil {
|
|
|
|
m.log.Warnln("Not multicasting on", name, "due to error:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// This is a new interface. Start an announcer for it.
|
|
|
|
multicastInterface := &multicastInterface{
|
|
|
|
sock: m.sock,
|
|
|
|
destAddr: *m.groupAddr,
|
|
|
|
listener: listener,
|
|
|
|
stop: make(chan interface{}),
|
|
|
|
zone: name,
|
|
|
|
}
|
|
|
|
multicastInterface.Act(multicastInterface, multicastInterface.announce)
|
|
|
|
m.listeners[name] = multicastInterface
|
2020-05-02 22:33:25 +01:00
|
|
|
m.log.Debugln("Started multicasting on", name)
|
2020-05-02 22:26:41 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Look for interfaces we knew about but are no longer there.
|
|
|
|
for name, intf := range m.listeners {
|
|
|
|
if _, ok := interfaces[name]; !ok {
|
|
|
|
// This is a disappeared interface. Stop the announcer.
|
|
|
|
close(intf.stop)
|
|
|
|
delete(m.listeners, name)
|
2020-05-02 22:33:25 +01:00
|
|
|
m.log.Debugln("Stopped multicasting on", name)
|
2020-05-02 22:26:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Queue the next check.
|
2020-05-02 22:37:12 +01:00
|
|
|
m.monitor = time.AfterFunc(time.Second, func() {
|
2020-05-02 22:26:41 +01:00
|
|
|
m.Act(m, m.monitorInterfaceChanges)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *multicastInterface) announce() {
|
|
|
|
// Check if the multicast interface has been stopped. This will happen
|
|
|
|
// if it disappears from the system or goes down.
|
|
|
|
select {
|
|
|
|
case <-m.stop:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
// Send the beacon.
|
|
|
|
lladdr := m.listener.Listener.Addr().String()
|
|
|
|
if a, err := net.ResolveTCPAddr("tcp6", lladdr); err == nil {
|
|
|
|
a.Zone = ""
|
|
|
|
msg := []byte(a.String())
|
|
|
|
m.sock.WriteTo(msg, nil, &m.destAddr)
|
|
|
|
}
|
|
|
|
// Queue the next beacon.
|
|
|
|
if m.interval.Seconds() < 15 {
|
|
|
|
m.interval += time.Second
|
|
|
|
}
|
|
|
|
m.timer = time.AfterFunc(m.interval, func() {
|
|
|
|
m.Act(m, m.announce)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-20 15:11:55 +01:00
|
|
|
// GetInterfaces returns the currently known/enabled multicast interfaces. It is
|
|
|
|
// expected that UpdateInterfaces has been called at least once before calling
|
|
|
|
// this method.
|
2019-06-29 00:12:56 +01:00
|
|
|
func (m *Multicast) Interfaces() map[string]net.Interface {
|
|
|
|
interfaces := make(map[string]net.Interface)
|
2019-01-14 19:27:13 +00:00
|
|
|
// Get interface expressions from config
|
2019-07-27 15:00:09 +01:00
|
|
|
current := m.config.GetCurrent()
|
2019-03-28 00:30:25 +00:00
|
|
|
exprs := current.MulticastInterfaces
|
2018-06-07 22:07:19 -05:00
|
|
|
// Ask the system for network interfaces
|
|
|
|
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
|
|
|
|
}
|
2020-05-02 22:26:41 +01:00
|
|
|
addrs, _ := iface.Addrs()
|
|
|
|
hasLLAddr := false
|
|
|
|
for _, addr := range addrs {
|
|
|
|
addrIP, _, _ := net.ParseCIDR(addr.String())
|
|
|
|
if addrIP.To4() == nil && addrIP.IsLinkLocalUnicast() {
|
|
|
|
hasLLAddr = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasLLAddr {
|
|
|
|
// Ignore interfaces without link-local addresses
|
|
|
|
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-06-29 00:12:56 +01:00
|
|
|
interfaces[iface.Name] = iface
|
2018-06-07 22:07:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-29 00:12:56 +01:00
|
|
|
return interfaces
|
2018-06-07 22:07:19 -05:00
|
|
|
}
|
|
|
|
|
2019-03-28 16:13:14 +00:00
|
|
|
func (m *Multicast) listen() {
|
2020-05-02 22:26:41 +01:00
|
|
|
groupAddr, err := net.ResolveUDPAddr("udp6", GroupAddr)
|
2018-05-23 11:13:53 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
bs := make([]byte, 2048)
|
|
|
|
for {
|
|
|
|
nBytes, rcm, fromAddr, err := m.sock.ReadFrom(bs)
|
|
|
|
if err != nil {
|
2019-10-24 09:54:57 +01:00
|
|
|
if !m.IsStarted() {
|
2019-07-06 11:52:30 +01:00
|
|
|
return
|
|
|
|
}
|
2018-05-23 11:13:53 +01:00
|
|
|
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 17:50:08 -05:00
|
|
|
}
|
2018-05-23 11:13:53 +01:00
|
|
|
from := fromAddr.(*net.UDPAddr)
|
|
|
|
if addr.IP.String() != from.IP.String() {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-29 00:12:56 +01:00
|
|
|
if _, ok := m.Interfaces()[from.Zone]; ok {
|
2019-06-20 15:11:55 +01:00
|
|
|
addr.Zone = ""
|
|
|
|
if err := m.core.CallPeer("tcp://"+addr.String(), from.Zone); err != nil {
|
|
|
|
m.log.Debugln("Call from multicast failed:", err)
|
|
|
|
}
|
2019-03-09 09:24:52 +00:00
|
|
|
}
|
2018-05-23 11:13:53 +01:00
|
|
|
}
|
|
|
|
}
|