diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-12-20 15:35:44 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-12-21 18:28:00 +0100 |
| commit | 6981666c6d8f639a1ff9c09a3bc08769e19928af (patch) | |
| tree | bdb94d76a7fb6a08ef200e9bbbbd5fff1d6b134c /exports | |
| parent | 697dfed1f9f5d2916889a81a7a64bd1158caf2d2 (diff) | |
Failover: Initial implementation
Diffstat (limited to 'exports')
| -rw-r--r-- | exports/exports.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/exports/exports.go b/exports/exports.go index 87ce331..e374661 100644 --- a/exports/exports.go +++ b/exports/exports.go @@ -4,8 +4,13 @@ package main #include <stdlib.h> #include "error.h" +typedef long long int (*ReadRxBytes)(); typedef int (*PythonCB)(const char* name, int oldstate, int newstate, void* data); +static long long int get_read_rx_bytes(ReadRxBytes read) +{ + return read(); +} static int call_callback(PythonCB callback, const char *name, int oldstate, int newstate, void* data) { return callback(name, oldstate, newstate, data); @@ -441,6 +446,44 @@ func SetSupportWireguard(name *C.char, support C.int) *C.error { return nil } +//export StartFailover +func StartFailover(name *C.char, gateway *C.char, mtu C.int, readRxBytes C.ReadRxBytes) (C.int, *C.error) { + nameStr := C.GoString(name) + state, stateErr := GetVPNState(nameStr) + if stateErr != nil { + return C.int(0), getError(stateErr) + } + dropped, droppedErr := state.StartFailover(C.GoString(gateway), int(mtu), func() (int64, error) { + rxBytes := int64(C.get_read_rx_bytes(readRxBytes)) + if rxBytes == -1 { + return 0, errors.New("client gave an invalid rx bytes value") + } + return rxBytes, nil + }) + if droppedErr != nil { + return C.int(0), getError(droppedErr) + } + droppedC := C.int(0) + if dropped { + droppedC = C.int(1) + } + return droppedC, nil +} + +//export CancelFailover +func CancelFailover(name *C.char) *C.error { + nameStr := C.GoString(name) + state, stateErr := GetVPNState(nameStr) + if stateErr != nil { + return getError(stateErr) + } + cancelErr := state.CancelFailover() + if cancelErr != nil { + return getError(cancelErr) + } + return nil +} + //export FreeString func FreeString(addr *C.char) { C.free(unsafe.Pointer(addr)) |
