From 6df987789562bc0944e08c631a578c1d42087a68 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sat, 13 May 2023 21:24:06 +0800 Subject: [PATCH] refactor: improve the code --- .vscode/settings.json | 3 ++- common/src/vpn/ffi.rs | 22 ++++++++++++++++++---- common/src/vpn/mod.rs | 12 ++---------- common/src/vpn/vpn.c | 16 +++++----------- common/src/vpn/vpn.h | 7 ++++--- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index b16b12d..8d8fab5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -20,6 +20,7 @@ "stdio.h": "c", "openconnect.h": "c", "compare": "c", - "stdlib.h": "c" + "stdlib.h": "c", + "vpn.h": "c" } } diff --git a/common/src/vpn/ffi.rs b/common/src/vpn/ffi.rs index 8f24b49..9b3d843 100644 --- a/common/src/vpn/ffi.rs +++ b/common/src/vpn/ffi.rs @@ -1,4 +1,5 @@ use std::ffi::c_void; +use tokio::sync::mpsc; #[repr(C)] #[derive(Debug, Copy, Clone)] @@ -12,11 +13,24 @@ pub(crate) struct Options { #[link(name = "vpn")] extern "C" { #[link_name = "start"] - pub(crate) fn connect( - options: *const Options, - on_connected: extern "C" fn(i32, *mut c_void), - ) -> ::std::os::raw::c_int; + pub(crate) fn connect(options: *const Options) -> ::std::os::raw::c_int; #[link_name = "stop"] pub(crate) fn disconnect(); } + +#[no_mangle] +extern "C" fn on_vpn_connected(value: i32, sender: *mut c_void) { + let sender = unsafe { &*(sender as *const mpsc::Sender) }; + sender + .blocking_send(value) + .expect("Failed to send VPN connection code"); +} + +// Logger used in the C code. +#[no_mangle] +extern "C" fn vpn_log(level: i32, message: *const ::std::os::raw::c_char) { + println!("{}: {:?}", level, unsafe { + std::ffi::CStr::from_ptr(message) + }); +} diff --git a/common/src/vpn/mod.rs b/common/src/vpn/mod.rs index 27ea692..a4c7fa5 100644 --- a/common/src/vpn/mod.rs +++ b/common/src/vpn/mod.rs @@ -1,5 +1,3 @@ -mod ffi; - use serde::{Deserialize, Serialize}; use std::ffi::{c_void, CString}; use std::sync::Arc; @@ -7,13 +5,7 @@ use std::thread; use tokio::sync::watch; use tokio::sync::{mpsc, Mutex}; -#[no_mangle] -extern "C" fn on_connected(value: i32, sender: *mut c_void) { - let sender = unsafe { &*(sender as *const mpsc::Sender) }; - sender - .blocking_send(value) - .expect("Failed to send VPN connection code"); -} +mod ffi; #[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] @@ -119,7 +111,7 @@ impl Vpn { // Start the VPN connection, this will block until the connection is closed status_holder.blocking_lock().set(VpnStatus::Connecting); - let ret = unsafe { ffi::connect(&oc_options, on_connected) }; + let ret = unsafe { ffi::connect(&oc_options) }; println!("VPN connection closed with code: {}", ret); status_holder.blocking_lock().set(VpnStatus::Disconnected); diff --git a/common/src/vpn/vpn.c b/common/src/vpn/vpn.c index fe8eb0d..4efc0f6 100644 --- a/common/src/vpn/vpn.c +++ b/common/src/vpn/vpn.c @@ -9,7 +9,6 @@ #include "vpn.h" void *g_user_data; -on_connected_cb g_on_connected_cb; static int g_cmd_pipe_fd; const char *g_vpnc_script; @@ -43,19 +42,18 @@ static void print_progress(__attribute__((unused)) void *_vpninfo, int level, co static void setup_tun_handler(void *_vpninfo) { openconnect_setup_tun_device(_vpninfo, g_vpnc_script, NULL); - - if (g_on_connected_cb) - { - g_on_connected_cb(g_cmd_pipe_fd, g_user_data); - } + on_vpn_connected(g_cmd_pipe_fd, g_user_data); } /* Initialize VPN connection */ -int start(const Options *options, on_connected_cb cb) +int start(const Options *options) { struct openconnect_info *vpninfo; struct utsname utsbuf; + g_user_data = options->user_data; + g_vpnc_script = options->script; + vpninfo = openconnect_vpninfo_new("PAN GlobalProtect", validate_peer_cert, NULL, NULL, print_progress, NULL); if (!vpninfo) @@ -95,10 +93,6 @@ int start(const Options *options, on_connected_cb cb) } // Essential step - // openconnect_setup_tun_device(vpninfo, options->script, NULL); - g_user_data = options->user_data; - g_on_connected_cb = cb; - g_vpnc_script = options->script; openconnect_set_setup_tun_handler(vpninfo, setup_tun_handler); while (1) diff --git a/common/src/vpn/vpn.h b/common/src/vpn/vpn.h index 0e28b76..2c0e9a9 100644 --- a/common/src/vpn/vpn.h +++ b/common/src/vpn/vpn.h @@ -1,5 +1,3 @@ -typedef void (*on_connected_cb)(int32_t, void *); - typedef struct Options { const char *server; const char *cookie; @@ -7,5 +5,8 @@ typedef struct Options { void *user_data; } Options; -int start(const Options *options, on_connected_cb cb); +int start(const Options *options); void stop(); + +extern void on_vpn_connected(int cmd_pipe_fd, void *user_data); +extern void vpn_log(int level, const char *msg); \ No newline at end of file