wireguard-apple/wireguard-go-bridge/src/api-ios.go

191 lines
4.1 KiB
Go
Raw Normal View History

2018-06-09 01:14:34 +00:00
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (C) 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
*/
package main
// #include <stdlib.h>
2018-06-09 01:14:34 +00:00
// #include <sys/types.h>
// static void callLogger(void *func, int level, const char *msg)
2018-06-09 01:14:34 +00:00
// {
// ((void(*)(int, const char *))func)(level, msg);
2018-06-09 01:14:34 +00:00
// }
import "C"
import (
"bufio"
"errors"
2018-06-09 01:14:34 +00:00
"git.zx2c4.com/wireguard-go/tun"
"golang.org/x/sys/unix"
"io/ioutil"
"log"
"math"
"os"
"os/signal"
"runtime"
"strings"
"unsafe"
)
var loggerFunc unsafe.Pointer
var versionString *C.char
2018-06-09 01:14:34 +00:00
type CLogger struct {
level C.int
2018-06-09 01:14:34 +00:00
}
func (l *CLogger) Write(p []byte) (int, error) {
if uintptr(loggerFunc) == 0 {
return 0, errors.New("No logger initialized")
}
message := C.CString(string(p))
C.callLogger(loggerFunc, l.level, message)
C.free(unsafe.Pointer(message))
2018-06-09 01:14:34 +00:00
return len(p), nil
}
var tunnelHandles map[int32]*Device
2018-06-09 01:14:34 +00:00
func init() {
versionString = C.CString(WireGuardGoVersion)
2018-06-09 01:14:34 +00:00
roamingDisabled = true
tunnelHandles = make(map[int32]*Device)
2018-06-09 01:14:34 +00:00
signals := make(chan os.Signal)
signal.Notify(signals, unix.SIGUSR2)
go func() {
buf := make([]byte, os.Getpagesize())
for {
select {
case <-signals:
n := runtime.Stack(buf, true)
buf[n] = 0
if uintptr(loggerFunc) != 0 {
C.callLogger(loggerFunc, 0, (*_Ctype_char)(unsafe.Pointer(&buf[0])))
2018-06-09 01:14:34 +00:00
}
}
}
}()
}
//export wgSetLogger
func wgSetLogger(loggerFn uintptr) {
loggerFunc = unsafe.Pointer(loggerFn)
}
//export wgTurnOn
func wgTurnOn(settings string, tunFd int32) int32 {
2018-06-09 01:14:34 +00:00
logger := &Logger{
Debug: log.New(&CLogger{level: 0}, "", 0),
Info: log.New(&CLogger{level: 1}, "", 0),
Error: log.New(&CLogger{level: 2}, "", 0),
2018-06-09 01:14:34 +00:00
}
tun, _, err := tun.CreateTUNFromFD(int(tunFd))
if err != nil {
logger.Error.Println(err)
return -1
}
2018-06-09 01:14:34 +00:00
logger.Info.Println("Attaching to interface")
device := NewDevice(tun, logger)
bufferedSettings := bufio.NewReadWriter(bufio.NewReader(strings.NewReader(settings)), bufio.NewWriter(ioutil.Discard))
setError := ipcSetOperation(device, bufferedSettings)
if setError != nil {
logger.Error.Println(setError)
return -1
}
device.Up()
logger.Info.Println("Device started")
var i int32
for i = 0; i < math.MaxInt32; i++ {
if _, exists := tunnelHandles[i]; !exists {
break
}
}
if i == math.MaxInt32 {
return -1
}
tunnelHandles[i] = device
2018-06-09 01:14:34 +00:00
return i
}
//export wgTurnOff
func wgTurnOff(tunnelHandle int32) {
device, ok := tunnelHandles[tunnelHandle]
2018-06-09 01:14:34 +00:00
if !ok {
return
}
delete(tunnelHandles, tunnelHandle)
device.Close()
}
//export wgSetConfig
2018-12-11 23:45:50 +00:00
func wgSetConfig(tunnelHandle int32, settings string) int64 {
device, ok := tunnelHandles[tunnelHandle]
if !ok {
2018-12-11 23:45:50 +00:00
return 0
}
bufferedSettings := bufio.NewReadWriter(bufio.NewReader(strings.NewReader(settings)), bufio.NewWriter(ioutil.Discard))
err := ipcSetOperation(device, bufferedSettings)
if err != nil {
device.log.Error.Println(err)
2018-12-11 23:45:50 +00:00
return err.Code
}
return 0
}
//export wgBindInterfaceScope
func wgBindInterfaceScope(tunnelHandle int32, ifscope int32) {
var operr error
2018-12-11 23:45:50 +00:00
device, ok := tunnelHandles[tunnelHandle]
if !ok {
return
}
device.log.Info.Printf("Binding sockets to interface %d\n", ifscope)
bind := device.net.bind.(*NativeBind)
for bind.ipv4 != nil {
fd, err := bind.ipv4.SyscallConn()
if err != nil {
device.log.Error.Printf("Unable to bind v4 socket to interface:", err)
break
}
err = fd.Control(func(fd uintptr) {
operr = unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, int(ifscope))
})
if err == nil {
err = operr
}
if err != nil {
device.log.Error.Printf("Unable to bind v4 socket to interface:", err)
}
break
}
for bind.ipv6 != nil {
fd, err := bind.ipv6.SyscallConn()
if err != nil {
device.log.Error.Printf("Unable to bind v6 socket to interface:", err)
break
}
err = fd.Control(func(fd uintptr) {
operr = unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, int(ifscope))
})
if err == nil {
err = operr
}
if err != nil {
device.log.Error.Printf("Unable to bind v6 socket to interface:", err)
}
break
}
2018-06-09 01:14:34 +00:00
}
//export wgVersion
func wgVersion() *C.char {
return versionString
2018-06-09 01:14:34 +00:00
}
func main() {}