From 6981666c6d8f639a1ff9c09a3bc08769e19928af Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Tue, 20 Dec 2022 15:35:44 +0100 Subject: Failover: Initial implementation --- exports/exports.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'exports') 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 #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)) -- cgit v1.2.3