blob: e26ea7b7daacf8acca561226195e57e7ca407e5a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
/* SPDX-License-Identifier: Apache-2.0
*
* Copyright (C) 2017-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
* Copyright (C) 2021 Mullvad VPN AB. All Rights Reserved.
*/
package main
// #include <stdlib.h>
import "C"
import (
"bufio"
"bytes"
"runtime"
"strings"
"unsafe"
"github.com/mullvad/mullvadvpn-app/wireguard/libwg/tunnelcontainer"
)
const (
ERROR_GENERAL_FAILURE = -1
ERROR_INTERMITTENT_FAILURE = -2
)
var tunnels tunnelcontainer.Container
func init() {
tunnels = tunnelcontainer.New()
}
//export wgTurnOff
func wgTurnOff(tunnelHandle int32) {
{
tunnel, err := tunnels.Remove(tunnelHandle)
if err != nil {
return
}
tunnel.Device.Close()
}
// Calling twice convinces the GC to release NOW.
runtime.GC()
runtime.GC()
}
//export wgGetConfig
func wgGetConfig(tunnelHandle int32) *C.char {
tunnel, err := tunnels.Get(tunnelHandle)
if err != nil {
return nil
}
settings := new(bytes.Buffer)
writer := bufio.NewWriter(settings)
if err := tunnel.Device.IpcGetOperation(writer); err != nil {
tunnel.Logger.Errorf("Failed to get config for tunnel: %s\n", err)
return nil
}
writer.Flush()
return C.CString(settings.String())
}
//export wgSetConfig
func wgSetConfig(tunnelHandle int32, cSettings *C.char) int32 {
tunnel, err := tunnels.Get(tunnelHandle)
if err != nil {
return ERROR_GENERAL_FAILURE
}
if cSettings == nil {
tunnel.Logger.Errorf("cSettings is null\n")
return ERROR_GENERAL_FAILURE
}
settings := C.GoString(cSettings)
setError := tunnel.Device.IpcSetOperation(bufio.NewReader(strings.NewReader(settings)))
if setError != nil {
tunnel.Logger.Errorf("Failed to set device configuration\n")
tunnel.Logger.Errorf("%s\n", setError)
return ERROR_GENERAL_FAILURE
}
return 0
}
//export wgFreePtr
func wgFreePtr(ptr unsafe.Pointer) {
C.free(ptr)
}
func main() {}
|