2019-01-13 18:08:41 +00:00
|
|
|
// +build darwin
|
|
|
|
|
2019-03-28 16:13:14 +00:00
|
|
|
package multicast
|
2019-01-13 18:08:41 +00:00
|
|
|
|
2019-03-01 19:26:50 +00:00
|
|
|
/*
|
|
|
|
#cgo CFLAGS: -x objective-c
|
|
|
|
#cgo LDFLAGS: -framework Foundation
|
|
|
|
#import <Foundation/Foundation.h>
|
2019-03-10 18:05:27 +00:00
|
|
|
NSNetServiceBrowser *serviceBrowser;
|
|
|
|
void StartAWDLBrowsing() {
|
|
|
|
if (serviceBrowser == nil) {
|
|
|
|
serviceBrowser = [[NSNetServiceBrowser alloc] init];
|
|
|
|
serviceBrowser.includesPeerToPeer = YES;
|
|
|
|
}
|
2019-03-01 19:26:50 +00:00
|
|
|
[serviceBrowser searchForServicesOfType:@"_yggdrasil._tcp" inDomain:@""];
|
|
|
|
}
|
2019-03-10 18:05:27 +00:00
|
|
|
void StopAWDLBrowsing() {
|
|
|
|
if (serviceBrowser == nil) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[serviceBrowser stop];
|
|
|
|
}
|
2019-03-01 19:26:50 +00:00
|
|
|
*/
|
|
|
|
import "C"
|
2019-03-10 18:05:27 +00:00
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
var awdlGoroutineStarted bool
|
|
|
|
|
2019-03-28 16:13:14 +00:00
|
|
|
func (m *Multicast) multicastStarted() {
|
2019-03-10 18:05:27 +00:00
|
|
|
if awdlGoroutineStarted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
awdlGoroutineStarted = true
|
|
|
|
for {
|
|
|
|
C.StopAWDLBrowsing()
|
2019-06-20 14:11:55 +00:00
|
|
|
for _, intf := range m.GetInterfaces() {
|
2019-03-10 18:05:27 +00:00
|
|
|
if intf.Name == "awdl0" {
|
2019-06-20 14:11:55 +00:00
|
|
|
m.log.Infoln("Multicast discovery is using AWDL discovery")
|
2019-03-10 18:05:27 +00:00
|
|
|
C.StartAWDLBrowsing()
|
|
|
|
break
|
|
|
|
}
|
2019-03-01 19:26:50 +00:00
|
|
|
}
|
2019-03-10 18:05:27 +00:00
|
|
|
time.Sleep(time.Minute)
|
2019-03-01 19:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 16:13:14 +00:00
|
|
|
func (m *Multicast) multicastReuse(network string, address string, c syscall.RawConn) error {
|
2019-01-13 18:08:41 +00:00
|
|
|
var control error
|
|
|
|
var reuseport error
|
|
|
|
var recvanyif error
|
|
|
|
|
|
|
|
control = c.Control(func(fd uintptr) {
|
|
|
|
reuseport = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
|
|
|
|
|
|
|
|
// sys/socket.h: #define SO_RECV_ANYIF 0x1104
|
|
|
|
recvanyif = unix.SetsockoptInt(int(fd), syscall.SOL_SOCKET, 0x1104, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case reuseport != nil:
|
|
|
|
return reuseport
|
|
|
|
case recvanyif != nil:
|
|
|
|
return recvanyif
|
|
|
|
default:
|
|
|
|
return control
|
|
|
|
}
|
|
|
|
}
|