From 71d64a2def4d5157c70be194bddaa140876df8c5 Mon Sep 17 00:00:00 2001 From: fufesou Date: Wed, 3 May 2023 22:37:43 +0800 Subject: [PATCH 1/6] plugin_framework, add block input support Signed-off-by: fufesou --- src/plugin/callback_ext.rs | 49 ++++++++++++ src/plugin/callback_msg.rs | 153 +++++++++++++++++++++++++------------ src/plugin/errno.rs | 25 ++++-- src/plugin/mod.rs | 17 +++++ src/plugin/plugins.rs | 21 +++-- src/server/connection.rs | 73 ++++++++++++++++++ 6 files changed, 277 insertions(+), 61 deletions(-) create mode 100644 src/plugin/callback_ext.rs diff --git a/src/plugin/callback_ext.rs b/src/plugin/callback_ext.rs new file mode 100644 index 000000000..0e4425a9c --- /dev/null +++ b/src/plugin/callback_ext.rs @@ -0,0 +1,49 @@ +// External support for callback. +// 1. Support block input for some plugins. +// ----------------------------------------------------------------------------- + +use super::*; +use std::{ffi::c_void, ptr::null, str::FromStr}; + +const EXT_SUPPORT_BLOCK_INPUT: &str = "block-input"; + +pub(super) fn ext_support_callback( + id: &str, + peer: &str, + msg: &super::callback_msg::MsgToExtSupport, +) -> *const c_void { + match &msg.r#type as _ { + EXT_SUPPORT_BLOCK_INPUT => { + // let supported_plugins = []; + // let supported = supported_plugins.contains(&id); + let supported = true; + if supported { + match bool::from_str(&msg.data) { + Ok(block) => { + if crate::server::plugin_block_input(peer, block) == block { + null() + } else { + make_return_code_msg( + errno::ERR_CALLBACK_FAILED, + "Failed to block input", + ) + } + } + Err(err) => make_return_code_msg( + errno::ERR_CALLBACK_INVALID_ARGS, + &format!("Failed to parse data: {}", err), + ), + } + } else { + make_return_code_msg( + errno::ERR_CALLBACK_PLUGIN_ID, + &format!("This operation is not supported for plugin '{}', please contact the RustDesk team for support.", id), + ) + } + } + _ => make_return_code_msg( + errno::ERR_CALLBACK_TARGET_TYPE, + &format!("Unknown target type '{}'", &msg.r#type), + ), + } +} diff --git a/src/plugin/callback_msg.rs b/src/plugin/callback_msg.rs index d33d00962..7ed4cf589 100644 --- a/src/plugin/callback_msg.rs +++ b/src/plugin/callback_msg.rs @@ -6,12 +6,14 @@ use serde_json; use std::{ collections::HashMap, ffi::{c_char, c_void}, + ptr::null, sync::Arc, }; const MSG_TO_PEER_TARGET: &str = "peer"; const MSG_TO_UI_TARGET: &str = "ui"; const MSG_TO_CONFIG_TARGET: &str = "config"; +const MSG_TO_EXT_SUPPORT_TARGET: &str = "ext-support"; #[allow(dead_code)] const MSG_TO_UI_FLUTTER_CHANNEL_MAIN: u16 = 0x01 << 0; @@ -44,7 +46,6 @@ struct ConfigToUi { #[derive(Debug, Deserialize)] struct MsgToConfig { - id: String, r#type: String, key: String, value: String, @@ -52,6 +53,39 @@ struct MsgToConfig { ui: Option, // If not None, send msg to ui. } +#[derive(Debug, Deserialize)] +pub(super) struct MsgToExtSupport { + pub r#type: String, + pub data: String, +} + +macro_rules! cb_msg_field { + ($field: ident) => { + let $field = match cstr_to_string($field) { + Err(e) => { + log::error!("Failed to convert {} to string, {}", stringify!($field), e); + return make_return_code_msg( + errno::ERR_CALLBACK_INVALID_ARGS, + &format!("Failed to convert {} to string, {}", stringify!($field), e), + ); + } + Ok(v) => v, + }; + }; +} + +macro_rules! early_return_value { + ($e:expr, $code: ident, $($arg:tt)*) => { + match $e { + Err(e) => return make_return_code_msg( + errno::$code, + &format!("Failed to {} '{:?}'", format_args!($($arg)*), e), + ), + Ok(v) => v, + } + }; +} + /// Callback to send message to peer or ui. /// peer, target, id are utf8 strings(null terminated). /// @@ -60,6 +94,10 @@ struct MsgToConfig { /// id: The id of this plugin. /// content: The content. /// len: The length of the content. +/// +/// Return null ptr if success. +/// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string. +/// The plugin allocate memory with `libc::malloc` and return the pointer. #[no_mangle] pub(super) extern "C" fn cb_msg( peer: *const c_char, @@ -67,18 +105,7 @@ pub(super) extern "C" fn cb_msg( id: *const c_char, content: *const c_void, len: usize, -) { - macro_rules! cb_msg_field { - ($field: ident) => { - let $field = match cstr_to_string($field) { - Err(e) => { - log::error!("Failed to convert {} to string, {}", stringify!($field), e); - return; - } - Ok(v) => v, - }; - }; - } +) -> *const c_void { cb_msg_field!(peer); cb_msg_field!(target); cb_msg_field!(id); @@ -95,6 +122,12 @@ pub(super) extern "C" fn cb_msg( ..Default::default() }; session.send_plugin_request(request); + null() + } else { + make_return_code_msg( + errno::ERR_CALLBACK_PEER_NOT_FOUND, + &format!("Failed to find session for peer '{}'", peer), + ) } } MSG_TO_UI_TARGET => { @@ -103,47 +136,69 @@ pub(super) extern "C" fn cb_msg( let content = std::string::String::from_utf8(content_slice[2..].to_vec()) .unwrap_or("".to_string()); push_event_to_ui(channel, &peer, &content); + null() } MSG_TO_CONFIG_TARGET => { - if let Ok(s) = - std::str::from_utf8(unsafe { std::slice::from_raw_parts(content as _, len) }) - { - // No need to merge the msgs. Handling the msg one by one is ok. - if let Ok(msg) = serde_json::from_str::(s) { - match &msg.r#type as _ { - config::CONFIG_TYPE_SHARED => { - match config::SharedConfig::set(&msg.id, &msg.key, &msg.value) { - Ok(_) => { - if let Some(ui) = &msg.ui { - // No need to set the peer id for location config. - push_option_to_ui(ui.channel, "", &msg, ui); - } - } - Err(e) => { - log::error!("Failed to set local config, {}", e); - } - } - } - config::CONFIG_TYPE_PEER => { - match config::PeerConfig::set(&msg.id, &peer, &msg.key, &msg.value) { - Ok(_) => { - if let Some(ui) = &msg.ui { - push_option_to_ui(ui.channel, &peer, &msg, ui); - } - } - Err(e) => { - log::error!("Failed to set peer config, {}", e); - } - } - } - _ => {} + let s = early_return_value!( + std::str::from_utf8(unsafe { std::slice::from_raw_parts(content as _, len) }), + ERR_CALLBACK_INVALID_MSG, + "parse msg string" + ); + // No need to merge the msgs. Handling the msg one by one is ok. + let msg = early_return_value!( + serde_json::from_str::(s), + ERR_CALLBACK_INVALID_MSG, + "parse msg '{}'", + s + ); + match &msg.r#type as _ { + config::CONFIG_TYPE_SHARED => { + let _r = early_return_value!( + config::SharedConfig::set(&id, &msg.key, &msg.value), + ERR_CALLBACK_INVALID_MSG, + "set local config" + ); + if let Some(ui) = &msg.ui { + // No need to set the peer id for location config. + push_option_to_ui(ui.channel, &id, "", &msg, ui); } + null() } + config::CONFIG_TYPE_PEER => { + let _r = early_return_value!( + config::PeerConfig::set(&id, &peer, &msg.key, &msg.value), + ERR_CALLBACK_INVALID_MSG, + "set peer config" + ); + if let Some(ui) = &msg.ui { + push_option_to_ui(ui.channel, &id, &peer, &msg, ui); + } + null() + } + _ => make_return_code_msg( + errno::ERR_CALLBACK_TARGET_TYPE, + &format!("Unknown target type '{}'", &msg.r#type), + ), } } - _ => { - log::error!("Unknown target {}", target); + MSG_TO_EXT_SUPPORT_TARGET => { + let s = early_return_value!( + std::str::from_utf8(unsafe { std::slice::from_raw_parts(content as _, len) }), + ERR_CALLBACK_INVALID_MSG, + "parse msg string" + ); + let msg = early_return_value!( + serde_json::from_str::(s), + ERR_CALLBACK_INVALID_MSG, + "parse msg '{}'", + s + ); + super::callback_ext::ext_support_callback(&id, &peer, &msg) } + _ => make_return_code_msg( + errno::ERR_CALLBACK_TARGET, + &format!("Unknown target '{}'", target), + ), } } @@ -174,9 +229,9 @@ fn push_event_to_ui(channel: u16, peer: &str, content: &str) { } } -fn push_option_to_ui(channel: u16, peer: &str, msg: &MsgToConfig, ui: &ConfigToUi) { +fn push_option_to_ui(channel: u16, id: &str, peer: &str, msg: &MsgToConfig, ui: &ConfigToUi) { let v = [ - ("id", &msg.id as &str), + ("id", id), ("location", &ui.location), ("key", &msg.key), ("value", &msg.value), diff --git a/src/plugin/errno.rs b/src/plugin/errno.rs index f62a3daac..eda53f2f2 100644 --- a/src/plugin/errno.rs +++ b/src/plugin/errno.rs @@ -3,7 +3,7 @@ pub const ERR_SUCCESS: i32 = 0; // ====================================================== -// errors that will be handled by RustDesk +// Errors from the plugins, must be handled by RustDesk pub const ERR_RUSTDESK_HANDLE_BASE: i32 = 10000; @@ -21,10 +21,23 @@ pub const ERR_PEER_ID_MISMATCH: i32 = 10302; pub const ERR_NOT_HANDLED: i32 = 10401; // ====================================================== -// errors that should be handled by the plugin +// Errors from RustDesk callbacks. -pub const ERR_PLUGIN_HANDLE_BASE: i32 = 20000; +pub const ERR_CALLBACK_HANDLE_BASE: i32 = 20000; +pub const ERR_CALLBACK_PLUGIN_ID: i32 = 20001; +pub const ERR_CALLBACK_INVALID_ARGS: i32 = 20002; +pub const ERR_CALLBACK_INVALID_MSG: i32 = 20003; +pub const ERR_CALLBACK_TARGET: i32 = 20004; +pub const ERR_CALLBACK_TARGET_TYPE: i32 = 20005; +pub const ERR_CALLBACK_PEER_NOT_FOUND: i32 = 20006; -pub const EER_CALL_FAILED: i32 = 200021; -pub const ERR_PEER_ON_FAILED: i32 = 30012; -pub const ERR_PEER_OFF_FAILED: i32 = 30012; +pub const ERR_CALLBACK_FAILED: i32 = 21001; + +// ====================================================== +// Errors from the plugins, should be handled by the plugins. + +pub const ERR_PLUGIN_HANDLE_BASE: i32 = 30000; + +pub const EER_CALL_FAILED: i32 = 300021; +pub const ERR_PEER_ON_FAILED: i32 = 40012; +pub const ERR_PEER_OFF_FAILED: i32 = 40012; diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index ce4383ce5..e15a7f7ca 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -2,6 +2,7 @@ use hbb_common::{libc, ResultType}; use std::ffi::{c_char, c_void, CStr}; mod callback_msg; +mod callback_ext; mod config; pub mod desc; mod errno; @@ -50,6 +51,22 @@ fn str_to_cstr_ret(s: &str) -> *const c_char { } } +#[inline] +pub fn make_return_code_msg(code: i32, msg: &str) -> *const c_void { + let mut out = code.to_le_bytes().to_vec(); + out.extend(msg.as_bytes()); + out.push(0); + unsafe { + let r = libc::malloc(out.len()) as *mut c_char; + libc::memcpy( + r as *mut libc::c_void, + out.as_ptr() as *const libc::c_void, + out.len(), + ); + r as *const c_void + } +} + #[inline] fn get_code_msg_from_ret(ret: *const c_void) -> (i32, String) { assert!(ret.is_null() == false); diff --git a/src/plugin/plugins.rs b/src/plugin/plugins.rs index 4441d314d..5c9a04e0a 100644 --- a/src/plugin/plugins.rs +++ b/src/plugin/plugins.rs @@ -14,7 +14,7 @@ use std::{ collections::HashMap, ffi::{c_char, c_void}, path::PathBuf, - sync::{Arc, RwLock}, os::raw::c_uint, + sync::{Arc, RwLock}, }; const METHOD_HANDLE_UI: &[u8; 10] = b"handle_ui\0"; @@ -64,13 +64,17 @@ type PluginFuncDesc = extern "C" fn() -> *const c_char; /// id: The id of this plugin. /// content: The content. /// len: The length of the content. +/// +/// Return null ptr if success. +/// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string. +/// The plugin allocate memory with `libc::malloc` and return the pointer. type CallbackMsg = extern "C" fn( peer: *const c_char, target: *const c_char, id: *const c_char, content: *const c_void, len: usize, -); +) -> *const c_void; /// Callback to get the config. /// peer, key are utf8 strings(null terminated). /// @@ -93,12 +97,17 @@ type CallbackGetId = extern "C" fn() -> *const c_char; type CallbackLog = extern "C" fn(level: *const c_char, msg: *const c_char); /// Callback to the librustdesk core. -/// +/// /// method: the method name of this callback. /// json: the json data for the parameters. The argument *must* be non-null. /// raw: the binary data for this call, nullable. /// raw_len: the length of this binary data, only valid when we pass raw data to `raw`. -type CallbackNative = extern "C" fn(method: *const c_char, json: *const c_char, raw: *const c_void, raw_len: usize) -> super::native::NativeReturnValue; +type CallbackNative = extern "C" fn( + method: *const c_char, + json: *const c_char, + raw: *const c_void, + raw_len: usize, +) -> super::native::NativeReturnValue; /// The main function of the plugin on the client(self) side. /// /// method: The method. "handle_ui" or "handle_peer" @@ -148,7 +157,7 @@ struct Callbacks { get_conf: CallbackGetConf, get_id: CallbackGetId, log: CallbackLog, - native: CallbackNative + native: CallbackNative, } /// The plugin initialize data. @@ -343,7 +352,7 @@ fn load_plugin_path(path: &str) -> ResultType<()> { get_conf: config::cb_get_conf, get_id: config::cb_get_local_peer_id, log: super::plog::plugin_log, - native: super::native::cb_native_data + native: super::native::cb_native_data, }, }; plugin.init(&init_data, path)?; diff --git a/src/server/connection.rs b/src/server/connection.rs index b2651f0d3..5cc1a4678 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -65,6 +65,43 @@ lazy_static::lazy_static! { pub static CLICK_TIME: AtomicI64 = AtomicI64::new(0); pub static MOUSE_MOVE_TIME: AtomicI64 = AtomicI64::new(0); +#[cfg(all(feature = "flutter", feature = "plugin_framework"))] +#[cfg(not(any(target_os = "android", target_os = "ios")))] +lazy_static::lazy_static! { + static ref PLUGIN_BLOCK_INPUT_TXS: Arc>>> = Default::default(); + static ref PLUGIN_BLOCK_INPUT_TX_RX: (Arc>>, Arc>>) = { + let (tx, rx) = std_mpsc::channel(); + (Arc::new(Mutex::new(tx)), Arc::new(Mutex::new(rx))) + }; +} + +// Block input is required for some special cases, such as privacy mode. +#[cfg(all(feature = "flutter", feature = "plugin_framework"))] +#[cfg(not(any(target_os = "android", target_os = "ios")))] +pub fn plugin_block_input(peer: &str, block: bool) -> bool { + if let Some(tx) = PLUGIN_BLOCK_INPUT_TXS.lock().unwrap().get(peer) { + let _ = tx.send(if block { + MessageInput::BlockOnPlugin(peer.to_string()) + } else { + MessageInput::BlockOffPlugin(peer.to_string()) + }); + match PLUGIN_BLOCK_INPUT_TX_RX + .1 + .lock() + .unwrap() + .recv_timeout(std::time::Duration::from_millis(3_000)) + { + Ok(b) => b == block, + Err(..) => { + log::error!("plugin_block_input timeout"); + false + } + } + } else { + false + } +} + #[derive(Clone, Default)] pub struct ConnInner { id: i32, @@ -79,6 +116,12 @@ enum MessageInput { Key((KeyEvent, bool)), BlockOn, BlockOff, + #[cfg(all(feature = "flutter", feature = "plugin_framework"))] + #[cfg(not(any(target_os = "android", target_os = "ios")))] + BlockOnPlugin(String), + #[cfg(all(feature = "flutter", feature = "plugin_framework"))] + #[cfg(not(any(target_os = "android", target_os = "ios")))] + BlockOffPlugin(String), } #[derive(Clone, Debug)] @@ -646,6 +689,30 @@ impl Connection { ); } } + #[cfg(all(feature = "flutter", feature = "plugin_framework"))] + #[cfg(not(any(target_os = "android", target_os = "ios")))] + MessageInput::BlockOnPlugin(peer) => { + if crate::platform::block_input(true) { + block_input_mode = true; + } + let _r = PLUGIN_BLOCK_INPUT_TX_RX + .0 + .lock() + .unwrap() + .send(block_input_mode); + } + #[cfg(all(feature = "flutter", feature = "plugin_framework"))] + #[cfg(not(any(target_os = "android", target_os = "ios")))] + MessageInput::BlockOffPlugin(peer) => { + if crate::platform::block_input(false) { + block_input_mode = false; + } + let _r = PLUGIN_BLOCK_INPUT_TX_RX + .0 + .lock() + .unwrap() + .send(block_input_mode); + } }, Err(err) => { #[cfg(not(any(target_os = "android", target_os = "ios")))] @@ -944,6 +1011,12 @@ impl Connection { } } self.authorized = true; + #[cfg(all(feature = "flutter", feature = "plugin_framework"))] + #[cfg(not(any(target_os = "android", target_os = "ios")))] + PLUGIN_BLOCK_INPUT_TXS + .lock() + .unwrap() + .insert(self.lr.my_id.clone(), self.tx_input.clone()); pi.username = username; pi.sas_enabled = sas_enabled; From c05e87b3b63cc1caf018b88e56d5c704829f37b1 Mon Sep 17 00:00:00 2001 From: fufesou Date: Thu, 4 May 2023 16:27:44 +0800 Subject: [PATCH 2/6] plugin_framework, block_input, ready to debug Signed-off-by: fufesou --- src/plugin/callback_ext.rs | 26 +++++++++++--------------- src/plugin/callback_msg.rs | 2 +- src/plugin/errno.rs | 13 +++++++++---- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/plugin/callback_ext.rs b/src/plugin/callback_ext.rs index 0e4425a9c..ff78ca7ff 100644 --- a/src/plugin/callback_ext.rs +++ b/src/plugin/callback_ext.rs @@ -3,7 +3,7 @@ // ----------------------------------------------------------------------------- use super::*; -use std::{ffi::c_void, ptr::null, str::FromStr}; +use std::{ffi::c_void, ptr::null}; const EXT_SUPPORT_BLOCK_INPUT: &str = "block-input"; @@ -18,21 +18,17 @@ pub(super) fn ext_support_callback( // let supported = supported_plugins.contains(&id); let supported = true; if supported { - match bool::from_str(&msg.data) { - Ok(block) => { - if crate::server::plugin_block_input(peer, block) == block { - null() - } else { - make_return_code_msg( - errno::ERR_CALLBACK_FAILED, - "Failed to block input", - ) - } - } - Err(err) => make_return_code_msg( + if msg.data.len() != 1 { + return make_return_code_msg( errno::ERR_CALLBACK_INVALID_ARGS, - &format!("Failed to parse data: {}", err), - ), + "Invalid data length", + ); + } + let block = msg.data[0] != 0; + if crate::server::plugin_block_input(peer, block) == block { + null() + } else { + make_return_code_msg(errno::ERR_CALLBACK_FAILED, "Failed to block input") } } else { make_return_code_msg( diff --git a/src/plugin/callback_msg.rs b/src/plugin/callback_msg.rs index 7ed4cf589..39abca67a 100644 --- a/src/plugin/callback_msg.rs +++ b/src/plugin/callback_msg.rs @@ -56,7 +56,7 @@ struct MsgToConfig { #[derive(Debug, Deserialize)] pub(super) struct MsgToExtSupport { pub r#type: String, - pub data: String, + pub data: Vec, } macro_rules! cb_msg_field { diff --git a/src/plugin/errno.rs b/src/plugin/errno.rs index eda53f2f2..410df5132 100644 --- a/src/plugin/errno.rs +++ b/src/plugin/errno.rs @@ -10,13 +10,18 @@ pub const ERR_RUSTDESK_HANDLE_BASE: i32 = 10000; // not loaded pub const ERR_PLUGIN_LOAD: i32 = 10001; // not initialized -pub const ERR_PLUGIN_MSG_CB: i32 = 10101; +pub const ERR_PLUGIN_MSG_INIT: i32 = 10101; +pub const ERR_PLUGIN_MSG_INIT_INVALID: i32 = 10102; +pub const ERR_PLUGIN_MSG_GET_LOCAL_PEER_ID: i32 = 10103; // invalid -pub const ERR_CALL_INVALID_METHOD: i32 = 10201; -pub const ERR_CALL_NOT_SUPPORTED_METHOD: i32 = 10202; +pub const ERR_CALL_UNIMPLEMENTED: i32 = 10201; +pub const ERR_CALL_INVALID_METHOD: i32 = 10202; +pub const ERR_CALL_NOT_SUPPORTED_METHOD: i32 = 10203; +pub const ERR_CALL_INVALID_PEER: i32 = 10204; // failed on calling pub const ERR_CALL_INVALID_ARGS: i32 = 10301; pub const ERR_PEER_ID_MISMATCH: i32 = 10302; +pub const ERR_CALL_CONFIG_VALUE: i32 = 10303; // no handlers on calling pub const ERR_NOT_HANDLED: i32 = 10401; @@ -38,6 +43,6 @@ pub const ERR_CALLBACK_FAILED: i32 = 21001; pub const ERR_PLUGIN_HANDLE_BASE: i32 = 30000; -pub const EER_CALL_FAILED: i32 = 300021; +pub const EER_CALL_FAILED: i32 = 30021; pub const ERR_PEER_ON_FAILED: i32 = 40012; pub const ERR_PEER_OFF_FAILED: i32 = 40012; From 6dc02084c22a7848f5c9d282dbaa9393e2ef72b7 Mon Sep 17 00:00:00 2001 From: fufesou Date: Thu, 4 May 2023 17:13:15 +0800 Subject: [PATCH 3/6] plugin_framework, test block_input Signed-off-by: fufesou --- src/plugin/callback_ext.rs | 2 +- src/plugin/callback_msg.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugin/callback_ext.rs b/src/plugin/callback_ext.rs index ff78ca7ff..8312fffd4 100644 --- a/src/plugin/callback_ext.rs +++ b/src/plugin/callback_ext.rs @@ -28,7 +28,7 @@ pub(super) fn ext_support_callback( if crate::server::plugin_block_input(peer, block) == block { null() } else { - make_return_code_msg(errno::ERR_CALLBACK_FAILED, "Failed to block input") + make_return_code_msg(errno::ERR_CALLBACK_FAILED, "") } } else { make_return_code_msg( diff --git a/src/plugin/callback_msg.rs b/src/plugin/callback_msg.rs index 39abca67a..db0191376 100644 --- a/src/plugin/callback_msg.rs +++ b/src/plugin/callback_msg.rs @@ -79,7 +79,7 @@ macro_rules! early_return_value { match $e { Err(e) => return make_return_code_msg( errno::$code, - &format!("Failed to {} '{:?}'", format_args!($($arg)*), e), + &format!("Failed to {} '{}'", format_args!($($arg)*), e), ), Ok(v) => v, } From 9c6b303fa25c410c8e170397589b10b0d4d9f6ab Mon Sep 17 00:00:00 2001 From: fufesou Date: Thu, 4 May 2023 18:02:34 +0800 Subject: [PATCH 4/6] plugin_framework, change return type Signed-off-by: fufesou --- src/plugin/callback_ext.rs | 13 +++---- src/plugin/callback_msg.rs | 27 ++++++------- src/plugin/mod.rs | 79 +++++++++++++++++++++++--------------- src/plugin/plugins.rs | 58 ++++++++++++++-------------- 4 files changed, 94 insertions(+), 83 deletions(-) diff --git a/src/plugin/callback_ext.rs b/src/plugin/callback_ext.rs index 8312fffd4..715f47f7e 100644 --- a/src/plugin/callback_ext.rs +++ b/src/plugin/callback_ext.rs @@ -3,7 +3,6 @@ // ----------------------------------------------------------------------------- use super::*; -use std::{ffi::c_void, ptr::null}; const EXT_SUPPORT_BLOCK_INPUT: &str = "block-input"; @@ -11,7 +10,7 @@ pub(super) fn ext_support_callback( id: &str, peer: &str, msg: &super::callback_msg::MsgToExtSupport, -) -> *const c_void { +) -> PluginReturn { match &msg.r#type as _ { EXT_SUPPORT_BLOCK_INPUT => { // let supported_plugins = []; @@ -19,25 +18,25 @@ pub(super) fn ext_support_callback( let supported = true; if supported { if msg.data.len() != 1 { - return make_return_code_msg( + return PluginReturn::new( errno::ERR_CALLBACK_INVALID_ARGS, "Invalid data length", ); } let block = msg.data[0] != 0; if crate::server::plugin_block_input(peer, block) == block { - null() + PluginReturn::success() } else { - make_return_code_msg(errno::ERR_CALLBACK_FAILED, "") + PluginReturn::new(errno::ERR_CALLBACK_FAILED, "") } } else { - make_return_code_msg( + PluginReturn::new( errno::ERR_CALLBACK_PLUGIN_ID, &format!("This operation is not supported for plugin '{}', please contact the RustDesk team for support.", id), ) } } - _ => make_return_code_msg( + _ => PluginReturn::new( errno::ERR_CALLBACK_TARGET_TYPE, &format!("Unknown target type '{}'", &msg.r#type), ), diff --git a/src/plugin/callback_msg.rs b/src/plugin/callback_msg.rs index db0191376..6967e6853 100644 --- a/src/plugin/callback_msg.rs +++ b/src/plugin/callback_msg.rs @@ -6,7 +6,6 @@ use serde_json; use std::{ collections::HashMap, ffi::{c_char, c_void}, - ptr::null, sync::Arc, }; @@ -63,11 +62,9 @@ macro_rules! cb_msg_field { ($field: ident) => { let $field = match cstr_to_string($field) { Err(e) => { - log::error!("Failed to convert {} to string, {}", stringify!($field), e); - return make_return_code_msg( - errno::ERR_CALLBACK_INVALID_ARGS, - &format!("Failed to convert {} to string, {}", stringify!($field), e), - ); + let msg = format!("Failed to convert {} to string, {}", stringify!($field), e); + log::error!("{}", &msg); + return PluginReturn::new(errno::ERR_CALLBACK_INVALID_ARGS, &msg); } Ok(v) => v, }; @@ -77,7 +74,7 @@ macro_rules! cb_msg_field { macro_rules! early_return_value { ($e:expr, $code: ident, $($arg:tt)*) => { match $e { - Err(e) => return make_return_code_msg( + Err(e) => return PluginReturn::new( errno::$code, &format!("Failed to {} '{}'", format_args!($($arg)*), e), ), @@ -105,7 +102,7 @@ pub(super) extern "C" fn cb_msg( id: *const c_char, content: *const c_void, len: usize, -) -> *const c_void { +) -> PluginReturn { cb_msg_field!(peer); cb_msg_field!(target); cb_msg_field!(id); @@ -122,9 +119,9 @@ pub(super) extern "C" fn cb_msg( ..Default::default() }; session.send_plugin_request(request); - null() + PluginReturn::success() } else { - make_return_code_msg( + PluginReturn::new( errno::ERR_CALLBACK_PEER_NOT_FOUND, &format!("Failed to find session for peer '{}'", peer), ) @@ -136,7 +133,7 @@ pub(super) extern "C" fn cb_msg( let content = std::string::String::from_utf8(content_slice[2..].to_vec()) .unwrap_or("".to_string()); push_event_to_ui(channel, &peer, &content); - null() + PluginReturn::success() } MSG_TO_CONFIG_TARGET => { let s = early_return_value!( @@ -162,7 +159,7 @@ pub(super) extern "C" fn cb_msg( // No need to set the peer id for location config. push_option_to_ui(ui.channel, &id, "", &msg, ui); } - null() + PluginReturn::success() } config::CONFIG_TYPE_PEER => { let _r = early_return_value!( @@ -173,9 +170,9 @@ pub(super) extern "C" fn cb_msg( if let Some(ui) = &msg.ui { push_option_to_ui(ui.channel, &id, &peer, &msg, ui); } - null() + PluginReturn::success() } - _ => make_return_code_msg( + _ => PluginReturn::new( errno::ERR_CALLBACK_TARGET_TYPE, &format!("Unknown target type '{}'", &msg.r#type), ), @@ -195,7 +192,7 @@ pub(super) extern "C" fn cb_msg( ); super::callback_ext::ext_support_callback(&id, &peer, &msg) } - _ => make_return_code_msg( + _ => PluginReturn::new( errno::ERR_CALLBACK_TARGET, &format!("Unknown target '{}'", target), ), diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index e15a7f7ca..f5cbdc5dd 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -1,16 +1,19 @@ use hbb_common::{libc, ResultType}; -use std::ffi::{c_char, c_void, CStr}; +use std::{ + ffi::{c_char, c_void, CStr}, + ptr::null, +}; -mod callback_msg; mod callback_ext; +mod callback_msg; mod config; pub mod desc; mod errno; pub mod ipc; -mod plog; -mod plugins; pub mod native; pub mod native_handlers; +mod plog; +mod plugins; pub use plugins::{ handle_client_event, handle_listen_event, handle_server_event, handle_ui_event, load_plugin, @@ -29,6 +32,46 @@ pub const EVENT_ON_CONN_CLOSE_SERVER: &str = "on_conn_close_server"; pub use config::{ManagerConfig, PeerConfig, SharedConfig}; +/// The common return of plugin. +/// +/// [Note] +/// msg must be null if code is ERR_SUCCESS. +/// The msg must be freed by caller if it is not null. +#[repr(C)] +pub struct PluginReturn { + pub code: i32, + pub msg: *const c_char, +} + +impl PluginReturn { + pub fn success() -> Self { + PluginReturn { + code: errno::ERR_SUCCESS, + msg: null(), + } + } + + pub fn new(code: i32, msg: &str) -> Self { + let mut msg = msg.as_bytes().to_vec(); + msg.push(0); + let p = unsafe { + let p = libc::malloc(msg.len()) as *mut c_char; + libc::memcpy( + p as *mut libc::c_void, + msg.as_ptr() as *const libc::c_void, + msg.len(), + ); + p as *const c_char + }; + Self { code, msg: p } + } + + #[inline] + pub fn is_success(&self) -> bool { + self.code == errno::ERR_SUCCESS + } +} + #[inline] fn cstr_to_string(cstr: *const c_char) -> ResultType { Ok(String::from_utf8(unsafe { @@ -51,34 +94,6 @@ fn str_to_cstr_ret(s: &str) -> *const c_char { } } -#[inline] -pub fn make_return_code_msg(code: i32, msg: &str) -> *const c_void { - let mut out = code.to_le_bytes().to_vec(); - out.extend(msg.as_bytes()); - out.push(0); - unsafe { - let r = libc::malloc(out.len()) as *mut c_char; - libc::memcpy( - r as *mut libc::c_void, - out.as_ptr() as *const libc::c_void, - out.len(), - ); - r as *const c_void - } -} - -#[inline] -fn get_code_msg_from_ret(ret: *const c_void) -> (i32, String) { - assert!(ret.is_null() == false); - let code_bytes = unsafe { std::slice::from_raw_parts(ret as *const u8, 4) }; - let code = i32::from_le_bytes([code_bytes[0], code_bytes[1], code_bytes[2], code_bytes[3]]); - let msg = unsafe { CStr::from_ptr((ret as *const u8).add(4) as _) } - .to_str() - .unwrap_or("") - .to_string(); - (code, msg) -} - #[inline] fn free_c_ptr(ret: *mut c_void) { if !ret.is_null() { diff --git a/src/plugin/plugins.rs b/src/plugin/plugins.rs index 5c9a04e0a..87fd92510 100644 --- a/src/plugin/plugins.rs +++ b/src/plugin/plugins.rs @@ -38,7 +38,7 @@ struct PluginInfo { /// Return null ptr if success. /// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string. /// The plugin allocate memory with `libc::malloc` and return the pointer. -type PluginFuncInit = extern "C" fn(data: *const InitData) -> *const c_void; +type PluginFuncInit = extern "C" fn(data: *const InitData) -> PluginReturn; /// Reset the plugin. /// /// data: The initialize data. @@ -46,13 +46,13 @@ type PluginFuncInit = extern "C" fn(data: *const InitData) -> *const c_void; /// Return null ptr if success. /// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string. /// The plugin allocate memory with `libc::malloc` and return the pointer. -type PluginFuncReset = extern "C" fn(data: *const InitData) -> *const c_void; +type PluginFuncReset = extern "C" fn(data: *const InitData) -> PluginReturn; /// Clear the plugin. /// /// Return null ptr if success. /// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string. /// The plugin allocate memory with `libc::malloc` and return the pointer. -type PluginFuncClear = extern "C" fn() -> *const c_void; +type PluginFuncClear = extern "C" fn() -> PluginReturn; /// Get the description of the plugin. /// Return the description. The plugin allocate memory with `libc::malloc` and return the pointer. type PluginFuncDesc = extern "C" fn() -> *const c_char; @@ -74,7 +74,7 @@ type CallbackMsg = extern "C" fn( id: *const c_char, content: *const c_void, len: usize, -) -> *const c_void; +) -> PluginReturn; /// Callback to get the config. /// peer, key are utf8 strings(null terminated). /// @@ -123,7 +123,7 @@ type PluginFuncClientCall = extern "C" fn( peer: *const c_char, args: *const c_void, len: usize, -) -> *const c_void; +) -> PluginReturn; /// The main function of the plugin on the server(remote) side. /// /// method: The method. "handle_ui" or "handle_peer" @@ -144,7 +144,7 @@ type PluginFuncServerCall = extern "C" fn( len: usize, out: *mut *mut c_void, out_len: *mut usize, -) -> *const c_void; +) -> PluginReturn; /// The plugin callbacks. /// msg: The callback to send message to peer or ui. @@ -223,13 +223,13 @@ macro_rules! make_plugin { fn init(&self, data: &InitData, path: &str) -> ResultType<()> { let init_ret = (self.init)(data as _); - if !init_ret.is_null() { - let (code, msg) = get_code_msg_from_ret(init_ret); - free_c_ptr(init_ret as _); + if !init_ret.is_success() { + let msg = cstr_to_string(init_ret.msg).unwrap_or_default(); + free_c_ptr(init_ret.msg as _); bail!( "Failed to init plugin {}, code: {}, msg: {}", path, - code, + init_ret.code, msg ); } @@ -238,13 +238,13 @@ macro_rules! make_plugin { fn clear(&self, id: &str) { let clear_ret = (self.clear)(); - if !clear_ret.is_null() { - let (code, msg) = get_code_msg_from_ret(clear_ret); - free_c_ptr(clear_ret as _); + if !clear_ret.is_success() { + let msg = cstr_to_string(clear_ret.msg).unwrap_or_default(); + free_c_ptr(clear_ret.msg as _); log::error!( "Failed to clear plugin {}, code: {}, msg: {}", id, - code, + clear_ret.code, msg ); } @@ -413,16 +413,16 @@ fn handle_event(method: &[u8], id: &str, peer: &str, event: &[u8]) -> ResultType event.as_ptr() as _, event.len(), ); - if ret.is_null() { + if ret.is_success() { Ok(()) } else { - let (code, msg) = get_code_msg_from_ret(ret); - free_c_ptr(ret as _); + let msg = cstr_to_string(ret.msg).unwrap_or_default(); + free_c_ptr(ret.msg as _); bail!( "Failed to handle plugin event, id: {}, method: {}, code: {}, msg: {}", id, std::string::String::from_utf8(method.to_vec()).unwrap_or_default(), - code, + ret.code, msg ); } @@ -469,14 +469,14 @@ fn _handle_listen_event(event: String, peer: String) { evt_bytes.as_ptr() as _, evt_bytes.len(), ); - if !ret.is_null() { - let (code, msg) = get_code_msg_from_ret(ret); - free_c_ptr(ret as _); + if !ret.is_success() { + let msg = cstr_to_string(ret.msg).unwrap_or_default(); + free_c_ptr(ret.msg as _); log::error!( "Failed to handle plugin listen event, id: {}, event: {}, code: {}, msg: {}", id, event, - code, + ret.code, msg ); } @@ -510,18 +510,18 @@ pub fn handle_client_event(id: &str, peer: &str, event: &[u8]) -> Message { &mut out as _, &mut out_len as _, ); - if ret.is_null() { + if ret.is_success() { let msg = make_plugin_request(id, out, out_len); free_c_ptr(out as _); msg } else { - let (code, msg) = get_code_msg_from_ret(ret); - free_c_ptr(ret as _); - if code > ERR_RUSTDESK_HANDLE_BASE && code < ERR_PLUGIN_HANDLE_BASE { + let msg = cstr_to_string(ret.msg).unwrap_or_default(); + free_c_ptr(ret.msg as _); + if ret.code > ERR_RUSTDESK_HANDLE_BASE && ret.code < ERR_PLUGIN_HANDLE_BASE { log::debug!( "Plugin {} failed to handle client event, code: {}, msg: {}", id, - code, + ret.code, msg ); let name = match PLUGIN_INFO.read().unwrap().get(id) { @@ -529,7 +529,7 @@ pub fn handle_client_event(id: &str, peer: &str, event: &[u8]) -> Message { None => "???", } .to_owned(); - match code { + match ret.code { ERR_CALL_NOT_SUPPORTED_METHOD => { make_plugin_failure(id, &name, "Plugin method is not supported") } @@ -542,7 +542,7 @@ pub fn handle_client_event(id: &str, peer: &str, event: &[u8]) -> Message { log::error!( "Plugin {} failed to handle client event, code: {}, msg: {}", id, - code, + ret.code, msg ); let msg = make_plugin_request(id, out, out_len); From 1243d8f43433675408bdfbd2383b05327f4a357f Mon Sep 17 00:00:00 2001 From: fufesou Date: Thu, 4 May 2023 19:21:13 +0800 Subject: [PATCH 5/6] Revert "plugin_framework, change return type" This reverts commit 9c6b303fa25c410c8e170397589b10b0d4d9f6ab. --- src/plugin/callback_ext.rs | 13 ++++--- src/plugin/callback_msg.rs | 27 +++++++------ src/plugin/mod.rs | 79 +++++++++++++++----------------------- src/plugin/plugins.rs | 58 ++++++++++++++-------------- 4 files changed, 83 insertions(+), 94 deletions(-) diff --git a/src/plugin/callback_ext.rs b/src/plugin/callback_ext.rs index 715f47f7e..8312fffd4 100644 --- a/src/plugin/callback_ext.rs +++ b/src/plugin/callback_ext.rs @@ -3,6 +3,7 @@ // ----------------------------------------------------------------------------- use super::*; +use std::{ffi::c_void, ptr::null}; const EXT_SUPPORT_BLOCK_INPUT: &str = "block-input"; @@ -10,7 +11,7 @@ pub(super) fn ext_support_callback( id: &str, peer: &str, msg: &super::callback_msg::MsgToExtSupport, -) -> PluginReturn { +) -> *const c_void { match &msg.r#type as _ { EXT_SUPPORT_BLOCK_INPUT => { // let supported_plugins = []; @@ -18,25 +19,25 @@ pub(super) fn ext_support_callback( let supported = true; if supported { if msg.data.len() != 1 { - return PluginReturn::new( + return make_return_code_msg( errno::ERR_CALLBACK_INVALID_ARGS, "Invalid data length", ); } let block = msg.data[0] != 0; if crate::server::plugin_block_input(peer, block) == block { - PluginReturn::success() + null() } else { - PluginReturn::new(errno::ERR_CALLBACK_FAILED, "") + make_return_code_msg(errno::ERR_CALLBACK_FAILED, "") } } else { - PluginReturn::new( + make_return_code_msg( errno::ERR_CALLBACK_PLUGIN_ID, &format!("This operation is not supported for plugin '{}', please contact the RustDesk team for support.", id), ) } } - _ => PluginReturn::new( + _ => make_return_code_msg( errno::ERR_CALLBACK_TARGET_TYPE, &format!("Unknown target type '{}'", &msg.r#type), ), diff --git a/src/plugin/callback_msg.rs b/src/plugin/callback_msg.rs index 6967e6853..db0191376 100644 --- a/src/plugin/callback_msg.rs +++ b/src/plugin/callback_msg.rs @@ -6,6 +6,7 @@ use serde_json; use std::{ collections::HashMap, ffi::{c_char, c_void}, + ptr::null, sync::Arc, }; @@ -62,9 +63,11 @@ macro_rules! cb_msg_field { ($field: ident) => { let $field = match cstr_to_string($field) { Err(e) => { - let msg = format!("Failed to convert {} to string, {}", stringify!($field), e); - log::error!("{}", &msg); - return PluginReturn::new(errno::ERR_CALLBACK_INVALID_ARGS, &msg); + log::error!("Failed to convert {} to string, {}", stringify!($field), e); + return make_return_code_msg( + errno::ERR_CALLBACK_INVALID_ARGS, + &format!("Failed to convert {} to string, {}", stringify!($field), e), + ); } Ok(v) => v, }; @@ -74,7 +77,7 @@ macro_rules! cb_msg_field { macro_rules! early_return_value { ($e:expr, $code: ident, $($arg:tt)*) => { match $e { - Err(e) => return PluginReturn::new( + Err(e) => return make_return_code_msg( errno::$code, &format!("Failed to {} '{}'", format_args!($($arg)*), e), ), @@ -102,7 +105,7 @@ pub(super) extern "C" fn cb_msg( id: *const c_char, content: *const c_void, len: usize, -) -> PluginReturn { +) -> *const c_void { cb_msg_field!(peer); cb_msg_field!(target); cb_msg_field!(id); @@ -119,9 +122,9 @@ pub(super) extern "C" fn cb_msg( ..Default::default() }; session.send_plugin_request(request); - PluginReturn::success() + null() } else { - PluginReturn::new( + make_return_code_msg( errno::ERR_CALLBACK_PEER_NOT_FOUND, &format!("Failed to find session for peer '{}'", peer), ) @@ -133,7 +136,7 @@ pub(super) extern "C" fn cb_msg( let content = std::string::String::from_utf8(content_slice[2..].to_vec()) .unwrap_or("".to_string()); push_event_to_ui(channel, &peer, &content); - PluginReturn::success() + null() } MSG_TO_CONFIG_TARGET => { let s = early_return_value!( @@ -159,7 +162,7 @@ pub(super) extern "C" fn cb_msg( // No need to set the peer id for location config. push_option_to_ui(ui.channel, &id, "", &msg, ui); } - PluginReturn::success() + null() } config::CONFIG_TYPE_PEER => { let _r = early_return_value!( @@ -170,9 +173,9 @@ pub(super) extern "C" fn cb_msg( if let Some(ui) = &msg.ui { push_option_to_ui(ui.channel, &id, &peer, &msg, ui); } - PluginReturn::success() + null() } - _ => PluginReturn::new( + _ => make_return_code_msg( errno::ERR_CALLBACK_TARGET_TYPE, &format!("Unknown target type '{}'", &msg.r#type), ), @@ -192,7 +195,7 @@ pub(super) extern "C" fn cb_msg( ); super::callback_ext::ext_support_callback(&id, &peer, &msg) } - _ => PluginReturn::new( + _ => make_return_code_msg( errno::ERR_CALLBACK_TARGET, &format!("Unknown target '{}'", target), ), diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index f5cbdc5dd..e15a7f7ca 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -1,19 +1,16 @@ use hbb_common::{libc, ResultType}; -use std::{ - ffi::{c_char, c_void, CStr}, - ptr::null, -}; +use std::ffi::{c_char, c_void, CStr}; -mod callback_ext; mod callback_msg; +mod callback_ext; mod config; pub mod desc; mod errno; pub mod ipc; -pub mod native; -pub mod native_handlers; mod plog; mod plugins; +pub mod native; +pub mod native_handlers; pub use plugins::{ handle_client_event, handle_listen_event, handle_server_event, handle_ui_event, load_plugin, @@ -32,46 +29,6 @@ pub const EVENT_ON_CONN_CLOSE_SERVER: &str = "on_conn_close_server"; pub use config::{ManagerConfig, PeerConfig, SharedConfig}; -/// The common return of plugin. -/// -/// [Note] -/// msg must be null if code is ERR_SUCCESS. -/// The msg must be freed by caller if it is not null. -#[repr(C)] -pub struct PluginReturn { - pub code: i32, - pub msg: *const c_char, -} - -impl PluginReturn { - pub fn success() -> Self { - PluginReturn { - code: errno::ERR_SUCCESS, - msg: null(), - } - } - - pub fn new(code: i32, msg: &str) -> Self { - let mut msg = msg.as_bytes().to_vec(); - msg.push(0); - let p = unsafe { - let p = libc::malloc(msg.len()) as *mut c_char; - libc::memcpy( - p as *mut libc::c_void, - msg.as_ptr() as *const libc::c_void, - msg.len(), - ); - p as *const c_char - }; - Self { code, msg: p } - } - - #[inline] - pub fn is_success(&self) -> bool { - self.code == errno::ERR_SUCCESS - } -} - #[inline] fn cstr_to_string(cstr: *const c_char) -> ResultType { Ok(String::from_utf8(unsafe { @@ -94,6 +51,34 @@ fn str_to_cstr_ret(s: &str) -> *const c_char { } } +#[inline] +pub fn make_return_code_msg(code: i32, msg: &str) -> *const c_void { + let mut out = code.to_le_bytes().to_vec(); + out.extend(msg.as_bytes()); + out.push(0); + unsafe { + let r = libc::malloc(out.len()) as *mut c_char; + libc::memcpy( + r as *mut libc::c_void, + out.as_ptr() as *const libc::c_void, + out.len(), + ); + r as *const c_void + } +} + +#[inline] +fn get_code_msg_from_ret(ret: *const c_void) -> (i32, String) { + assert!(ret.is_null() == false); + let code_bytes = unsafe { std::slice::from_raw_parts(ret as *const u8, 4) }; + let code = i32::from_le_bytes([code_bytes[0], code_bytes[1], code_bytes[2], code_bytes[3]]); + let msg = unsafe { CStr::from_ptr((ret as *const u8).add(4) as _) } + .to_str() + .unwrap_or("") + .to_string(); + (code, msg) +} + #[inline] fn free_c_ptr(ret: *mut c_void) { if !ret.is_null() { diff --git a/src/plugin/plugins.rs b/src/plugin/plugins.rs index 87fd92510..5c9a04e0a 100644 --- a/src/plugin/plugins.rs +++ b/src/plugin/plugins.rs @@ -38,7 +38,7 @@ struct PluginInfo { /// Return null ptr if success. /// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string. /// The plugin allocate memory with `libc::malloc` and return the pointer. -type PluginFuncInit = extern "C" fn(data: *const InitData) -> PluginReturn; +type PluginFuncInit = extern "C" fn(data: *const InitData) -> *const c_void; /// Reset the plugin. /// /// data: The initialize data. @@ -46,13 +46,13 @@ type PluginFuncInit = extern "C" fn(data: *const InitData) -> PluginReturn; /// Return null ptr if success. /// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string. /// The plugin allocate memory with `libc::malloc` and return the pointer. -type PluginFuncReset = extern "C" fn(data: *const InitData) -> PluginReturn; +type PluginFuncReset = extern "C" fn(data: *const InitData) -> *const c_void; /// Clear the plugin. /// /// Return null ptr if success. /// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string. /// The plugin allocate memory with `libc::malloc` and return the pointer. -type PluginFuncClear = extern "C" fn() -> PluginReturn; +type PluginFuncClear = extern "C" fn() -> *const c_void; /// Get the description of the plugin. /// Return the description. The plugin allocate memory with `libc::malloc` and return the pointer. type PluginFuncDesc = extern "C" fn() -> *const c_char; @@ -74,7 +74,7 @@ type CallbackMsg = extern "C" fn( id: *const c_char, content: *const c_void, len: usize, -) -> PluginReturn; +) -> *const c_void; /// Callback to get the config. /// peer, key are utf8 strings(null terminated). /// @@ -123,7 +123,7 @@ type PluginFuncClientCall = extern "C" fn( peer: *const c_char, args: *const c_void, len: usize, -) -> PluginReturn; +) -> *const c_void; /// The main function of the plugin on the server(remote) side. /// /// method: The method. "handle_ui" or "handle_peer" @@ -144,7 +144,7 @@ type PluginFuncServerCall = extern "C" fn( len: usize, out: *mut *mut c_void, out_len: *mut usize, -) -> PluginReturn; +) -> *const c_void; /// The plugin callbacks. /// msg: The callback to send message to peer or ui. @@ -223,13 +223,13 @@ macro_rules! make_plugin { fn init(&self, data: &InitData, path: &str) -> ResultType<()> { let init_ret = (self.init)(data as _); - if !init_ret.is_success() { - let msg = cstr_to_string(init_ret.msg).unwrap_or_default(); - free_c_ptr(init_ret.msg as _); + if !init_ret.is_null() { + let (code, msg) = get_code_msg_from_ret(init_ret); + free_c_ptr(init_ret as _); bail!( "Failed to init plugin {}, code: {}, msg: {}", path, - init_ret.code, + code, msg ); } @@ -238,13 +238,13 @@ macro_rules! make_plugin { fn clear(&self, id: &str) { let clear_ret = (self.clear)(); - if !clear_ret.is_success() { - let msg = cstr_to_string(clear_ret.msg).unwrap_or_default(); - free_c_ptr(clear_ret.msg as _); + if !clear_ret.is_null() { + let (code, msg) = get_code_msg_from_ret(clear_ret); + free_c_ptr(clear_ret as _); log::error!( "Failed to clear plugin {}, code: {}, msg: {}", id, - clear_ret.code, + code, msg ); } @@ -413,16 +413,16 @@ fn handle_event(method: &[u8], id: &str, peer: &str, event: &[u8]) -> ResultType event.as_ptr() as _, event.len(), ); - if ret.is_success() { + if ret.is_null() { Ok(()) } else { - let msg = cstr_to_string(ret.msg).unwrap_or_default(); - free_c_ptr(ret.msg as _); + let (code, msg) = get_code_msg_from_ret(ret); + free_c_ptr(ret as _); bail!( "Failed to handle plugin event, id: {}, method: {}, code: {}, msg: {}", id, std::string::String::from_utf8(method.to_vec()).unwrap_or_default(), - ret.code, + code, msg ); } @@ -469,14 +469,14 @@ fn _handle_listen_event(event: String, peer: String) { evt_bytes.as_ptr() as _, evt_bytes.len(), ); - if !ret.is_success() { - let msg = cstr_to_string(ret.msg).unwrap_or_default(); - free_c_ptr(ret.msg as _); + if !ret.is_null() { + let (code, msg) = get_code_msg_from_ret(ret); + free_c_ptr(ret as _); log::error!( "Failed to handle plugin listen event, id: {}, event: {}, code: {}, msg: {}", id, event, - ret.code, + code, msg ); } @@ -510,18 +510,18 @@ pub fn handle_client_event(id: &str, peer: &str, event: &[u8]) -> Message { &mut out as _, &mut out_len as _, ); - if ret.is_success() { + if ret.is_null() { let msg = make_plugin_request(id, out, out_len); free_c_ptr(out as _); msg } else { - let msg = cstr_to_string(ret.msg).unwrap_or_default(); - free_c_ptr(ret.msg as _); - if ret.code > ERR_RUSTDESK_HANDLE_BASE && ret.code < ERR_PLUGIN_HANDLE_BASE { + let (code, msg) = get_code_msg_from_ret(ret); + free_c_ptr(ret as _); + if code > ERR_RUSTDESK_HANDLE_BASE && code < ERR_PLUGIN_HANDLE_BASE { log::debug!( "Plugin {} failed to handle client event, code: {}, msg: {}", id, - ret.code, + code, msg ); let name = match PLUGIN_INFO.read().unwrap().get(id) { @@ -529,7 +529,7 @@ pub fn handle_client_event(id: &str, peer: &str, event: &[u8]) -> Message { None => "???", } .to_owned(); - match ret.code { + match code { ERR_CALL_NOT_SUPPORTED_METHOD => { make_plugin_failure(id, &name, "Plugin method is not supported") } @@ -542,7 +542,7 @@ pub fn handle_client_event(id: &str, peer: &str, event: &[u8]) -> Message { log::error!( "Plugin {} failed to handle client event, code: {}, msg: {}", id, - ret.code, + code, msg ); let msg = make_plugin_request(id, out, out_len); From db683c6664540dbaca60e638dadbe785ddbd8eee Mon Sep 17 00:00:00 2001 From: fufesou Date: Thu, 4 May 2023 19:53:44 +0800 Subject: [PATCH 6/6] plugina_framework, comment out native call for tmp Signed-off-by: fufesou --- src/plugin/plugins.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugin/plugins.rs b/src/plugin/plugins.rs index 5c9a04e0a..f0ec10f14 100644 --- a/src/plugin/plugins.rs +++ b/src/plugin/plugins.rs @@ -157,7 +157,7 @@ struct Callbacks { get_conf: CallbackGetConf, get_id: CallbackGetId, log: CallbackLog, - native: CallbackNative, + // native: CallbackNative, } /// The plugin initialize data. @@ -352,7 +352,7 @@ fn load_plugin_path(path: &str) -> ResultType<()> { get_conf: config::cb_get_conf, get_id: config::cb_get_local_peer_id, log: super::plog::plugin_log, - native: super::native::cb_native_data, + // native: super::native::cb_native_data, }, }; plugin.init(&init_data, path)?;