I am working on creating a WireGuard Windows client using Go. When I create the WireGuard adapter without terminating the program till the WireGuard interface is up, everything works fine. However, if I terminate the program while the adapter is still being created, I encounter the error 'Cannot create a file when that file already exists.' But, upon rerunning the code, the WireGuard adapter is successfully created and configured without any issues. How can I address this error and ensure smooth creation of the WireGuard adapter even when the code is terminated prematurely?

I am using the code from this link https://git.zx2c4.com/wireguard-windows/tree/driver?h=v0.5.3

Below is the code for creating adapter

func CreateAdapter(name, tunnelType string, requestedGUID *windows.GUID) (wireguard *Adapter, err error) {
    var name16 *uint16
    name16, err = windows.UTF16PtrFromString(name)
    if err != nil {
        return
    }
    var tunnelType16 *uint16
    tunnelType16, err = windows.UTF16PtrFromString(tunnelType)
    if err != nil {
        return
    }

        //
        // If I terminate the code before this line, then I am getting error mentioned before
        //

    r0, _, e1 := syscall.SyscallN(procWireGuardCreateAdapter.Addr(), uintptr(unsafe.Pointer(name16)), uintptr(unsafe.Pointer(tunnelType16)), uintptr(unsafe.Pointer(requestedGUID)))

    if r0 == 0 {
        err = e1
        return
    }
    wireguard = &Adapter{handle: r0}
    runtime.SetFinalizer(wireguard, closeAdapter)
    return
}

If I terminate the code before below line, then I am getting error mentioned before

r0, _, e1 := syscall.SyscallN(procWireGuardCreateAdapter.Addr(), uintptr(unsafe.Pointer(name16)), uintptr(unsafe.Pointer(tunnelType16)), uintptr(unsafe.Pointer(requestedGUID)))

0

There are 0 best solutions below