2018-05-23 11:13:53 +01:00
|
|
|
package yggdrasil
|
|
|
|
|
2018-06-12 17:50:08 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
2018-05-23 11:13:53 +01:00
|
|
|
|
2018-06-12 17:50:08 -05:00
|
|
|
"golang.org/x/net/ipv6"
|
|
|
|
)
|
2018-05-23 11:13:53 +01:00
|
|
|
|
|
|
|
type multicast struct {
|
2018-06-07 22:07:19 -05:00
|
|
|
core *Core
|
|
|
|
sock *ipv6.PacketConn
|
|
|
|
groupAddr string
|
2018-05-23 11:13:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *multicast) init(core *Core) {
|
|
|
|
m.core = core
|
|
|
|
m.groupAddr = "[ff02::114]:9001"
|
2018-05-26 21:50:47 +01:00
|
|
|
// Check if we've been given any expressions
|
|
|
|
if len(m.core.ifceExpr) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2018-05-23 11:28:20 +01:00
|
|
|
// Ask the system for network interfaces
|
2018-06-07 22:07:19 -05:00
|
|
|
m.core.log.Println("Found", len(m.interfaces()), "multicast interface(s)")
|
2018-05-23 11:13:53 +01:00
|
|
|
}
|
|
|
|
|
2018-05-27 22:13:37 +01:00
|
|
|
func (m *multicast) start() error {
|
2018-05-23 11:13:53 +01:00
|
|
|
if len(m.core.ifceExpr) == 0 {
|
2018-05-23 11:28:20 +01:00
|
|
|
m.core.log.Println("Multicast discovery is disabled")
|
2018-05-23 11:13:53 +01:00
|
|
|
} else {
|
2018-05-23 11:28:20 +01:00
|
|
|
m.core.log.Println("Multicast discovery is enabled")
|
|
|
|
addr, err := net.ResolveUDPAddr("udp", m.groupAddr)
|
|
|
|
if err != nil {
|
2018-05-27 22:13:37 +01:00
|
|
|
return err
|
2018-05-23 11:28:20 +01:00
|
|
|
}
|
|
|
|
listenString := fmt.Sprintf("[::]:%v", addr.Port)
|
|
|
|
conn, err := net.ListenPacket("udp6", listenString)
|
|
|
|
if err != nil {
|
2018-05-27 22:13:37 +01:00
|
|
|
return err
|
2018-05-23 11:28:20 +01: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 11:13:53 +01:00
|
|
|
|
2018-05-23 11:28:20 +01:00
|
|
|
go m.listen()
|
|
|
|
go m.announce()
|
|
|
|
}
|
2018-05-27 22:13:37 +01:00
|
|
|
return nil
|
2018-05-23 11:13:53 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 22:07:19 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-05-23 11:13:53 +01:00
|
|
|
func (m *multicast) announce() {
|
|
|
|
groupAddr, err := net.ResolveUDPAddr("udp6", m.groupAddr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var anAddr net.TCPAddr
|
2018-05-27 22:13:37 +01:00
|
|
|
myAddr := m.core.tcp.getAddr()
|
2018-05-23 11:13:53 +01:00
|
|
|
anAddr.Port = myAddr.Port
|
|
|
|
destAddr, err := net.ResolveUDPAddr("udp6", m.groupAddr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for {
|
2018-06-07 22:07:19 -05:00
|
|
|
for _, iface := range m.interfaces() {
|
2018-05-23 11:13:53 +01:00
|
|
|
m.sock.JoinGroup(&iface, groupAddr)
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
addrIP, _, _ := net.ParseCIDR(addr.String())
|
|
|
|
if addrIP.To4() != nil {
|
|
|
|
continue
|
|
|
|
} // IPv6 only
|
|
|
|
if !addrIP.IsLinkLocalUnicast() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
anAddr.IP = addrIP
|
|
|
|
anAddr.Zone = iface.Name
|
|
|
|
destAddr.Zone = iface.Name
|
|
|
|
msg := []byte(anAddr.String())
|
|
|
|
m.sock.WriteTo(msg, nil, destAddr)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 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
|
|
|
|
}
|
|
|
|
addr.Zone = from.Zone
|
|
|
|
saddr := addr.String()
|
2018-05-27 22:13:37 +01:00
|
|
|
m.core.tcp.connect(saddr)
|
2018-05-23 11:13:53 +01:00
|
|
|
}
|
|
|
|
}
|