5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 02:31:37 +00:00
yggdrasil-go/src/multicast/multicast_darwin.go

71 lines
1.4 KiB
Go
Raw Normal View History

2019-01-13 18:08:41 +00:00
// +build darwin
package multicast
2019-01-13 18:08:41 +00:00
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Foundation
#import <Foundation/Foundation.h>
NSNetServiceBrowser *serviceBrowser;
void StartAWDLBrowsing() {
if (serviceBrowser == nil) {
serviceBrowser = [[NSNetServiceBrowser alloc] init];
serviceBrowser.includesPeerToPeer = YES;
}
[serviceBrowser searchForServicesOfType:@"_yggdrasil._tcp" inDomain:@""];
}
void StopAWDLBrowsing() {
if (serviceBrowser == nil) {
return;
}
[serviceBrowser stop];
}
*/
import "C"
import (
"syscall"
"time"
"golang.org/x/sys/unix"
)
var awdlGoroutineStarted bool
func (m *Multicast) _multicastStarted() {
2020-05-24 20:46:18 +00:00
if !m.isOpen {
return
}
C.StopAWDLBrowsing()
2020-05-09 10:56:36 +00:00
for intf := range m._interfaces {
if intf == "awdl0" {
C.StartAWDLBrowsing()
break
}
}
2020-05-24 20:46:18 +00:00
time.AfterFunc(time.Minute, func() {
2020-05-09 10:56:36 +00:00
m.Act(nil, m._multicastStarted)
2019-09-18 15:51:46 +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
}
}