Use opaque handle for readwrite objects

This commit is contained in:
Jackson Coxson
2025-05-26 16:34:15 -06:00
parent bc6a1c0503
commit b8e2b115a5
6 changed files with 134 additions and 24 deletions

View File

@@ -1,11 +1,11 @@
// Jackson Coxson // Jackson Coxson
use std::ffi::{CString, c_char}; use std::ffi::{CStr, c_char};
use idevice::tcp::stream::AdapterStream; use idevice::tcp::stream::AdapterStream;
use crate::core_device_proxy::AdapterHandle; use crate::core_device_proxy::AdapterHandle;
use crate::{IdeviceErrorCode, RUNTIME}; use crate::{IdeviceErrorCode, RUNTIME, ReadWriteOpaque};
pub struct AdapterStreamHandle<'a>(pub AdapterStream<'a>); pub struct AdapterStreamHandle<'a>(pub AdapterStream<'a>);
@@ -27,7 +27,7 @@ pub struct AdapterStreamHandle<'a>(pub AdapterStream<'a>);
pub unsafe extern "C" fn adapter_connect( pub unsafe extern "C" fn adapter_connect(
adapter_handle: *mut AdapterHandle, adapter_handle: *mut AdapterHandle,
port: u16, port: u16,
stream_handle: *mut *mut AdapterStreamHandle, stream_handle: *mut *mut ReadWriteOpaque,
) -> IdeviceErrorCode { ) -> IdeviceErrorCode {
if adapter_handle.is_null() || stream_handle.is_null() { if adapter_handle.is_null() || stream_handle.is_null() {
return IdeviceErrorCode::InvalidArg; return IdeviceErrorCode::InvalidArg;
@@ -37,7 +37,13 @@ pub unsafe extern "C" fn adapter_connect(
let res = RUNTIME.block_on(async move { AdapterStream::connect(adapter, port).await }); let res = RUNTIME.block_on(async move { AdapterStream::connect(adapter, port).await });
match res { match res {
Ok(_) => IdeviceErrorCode::IdeviceSuccess, Ok(r) => {
let boxed = Box::new(ReadWriteOpaque {
inner: Some(Box::new(r)),
});
unsafe { *stream_handle = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
}
Err(e) => { Err(e) => {
log::error!("Adapter connect failed: {}", e); log::error!("Adapter connect failed: {}", e);
IdeviceErrorCode::AdapterIOFailed IdeviceErrorCode::AdapterIOFailed
@@ -67,7 +73,7 @@ pub unsafe extern "C" fn adapter_pcap(
} }
let adapter = unsafe { &mut (*handle).0 }; let adapter = unsafe { &mut (*handle).0 };
let c_str = unsafe { CString::from_raw(path as *mut c_char) }; let c_str = unsafe { CStr::from_ptr(path) };
let path_str = match c_str.to_str() { let path_str = match c_str.to_str() {
Ok(s) => s, Ok(s) => s,
Err(_) => return IdeviceErrorCode::InvalidArg, Err(_) => return IdeviceErrorCode::InvalidArg,

View File

@@ -4,9 +4,12 @@ use std::ffi::{CStr, CString, c_char};
use std::os::raw::c_int; use std::os::raw::c_int;
use std::ptr; use std::ptr;
use idevice::ReadWrite;
use idevice::debug_proxy::{DebugProxyClient, DebugserverCommand}; use idevice::debug_proxy::{DebugProxyClient, DebugserverCommand};
use idevice::tcp::stream::AdapterStream;
use idevice::{IdeviceError, ReadWrite, RsdService};
use crate::core_device_proxy::AdapterHandle;
use crate::rsd::RsdHandshakeHandle;
use crate::{IdeviceErrorCode, RUNTIME}; use crate::{IdeviceErrorCode, RUNTIME};
/// Opaque handle to a DebugProxyClient /// Opaque handle to a DebugProxyClient
@@ -112,6 +115,47 @@ pub unsafe extern "C" fn debugserver_command_free(command: *mut DebugserverComma
} }
} }
/// Creates a new DebugProxyClient
///
/// # Arguments
/// * [`provider`] - An adapter created by this library
/// * [`handshake`] - An RSD handshake from the same provider
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `provider` must be a valid pointer to a handle allocated by this library
/// `handshake` must be a valid pointer to a location where the handle will be stored
#[unsafe(no_mangle)]
pub unsafe extern "C" fn debug_proxy_connect_rsd(
provider: *mut AdapterHandle,
handshake: *mut RsdHandshakeHandle,
handle: *mut *mut DebugProxyHandle,
) -> IdeviceErrorCode {
if provider.is_null() || handshake.is_null() || handshake.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let res: Result<DebugProxyClient<AdapterStream>, IdeviceError> = RUNTIME.block_on(async move {
let provider_ref = unsafe { &mut (*provider).0 };
let handshake_ref = unsafe { &mut (*handshake).0 };
// Connect using the reference
DebugProxyClient::connect_rsd(provider_ref, handshake_ref).await
});
match res {
Ok(d) => {
let boxed = Box::new(DebugProxyHandle(DebugProxyClient::new(Box::new(
d.into_inner(),
))));
unsafe { *handle = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
}
Err(e) => e.into(),
}
}
/// Creates a new DebugProxyClient /// Creates a new DebugProxyClient
/// ///
/// # Arguments /// # Arguments

View File

@@ -42,7 +42,7 @@ pub mod util;
pub use errors::*; pub use errors::*;
pub use pairing_file::*; pub use pairing_file::*;
use idevice::{Idevice, IdeviceSocket}; use idevice::{Idevice, IdeviceSocket, ReadWrite};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::ffi::{CStr, CString, c_char}; use std::ffi::{CStr, CString, c_char};
use tokio::runtime::{self, Runtime}; use tokio::runtime::{self, Runtime};
@@ -57,6 +57,11 @@ static RUNTIME: Lazy<Runtime> = Lazy::new(|| {
pub const LOCKDOWN_PORT: u16 = 62078; pub const LOCKDOWN_PORT: u16 = 62078;
#[repr(C)]
pub struct ReadWriteOpaque {
pub inner: Option<Box<dyn ReadWrite>>,
}
/// Opaque C-compatible handle to an Idevice connection /// Opaque C-compatible handle to an Idevice connection
pub struct IdeviceHandle(pub Idevice); pub struct IdeviceHandle(pub Idevice);
pub struct IdeviceSocketHandle(IdeviceSocket); pub struct IdeviceSocketHandle(IdeviceSocket);

View File

@@ -21,7 +21,7 @@ pub struct IdeviceProviderHandle(pub Box<dyn IdeviceProvider>);
/// ///
/// # Safety /// # Safety
/// `ip` must be a valid sockaddr /// `ip` must be a valid sockaddr
/// `pairing_file` must never be used again /// `pairing_file` is consumed must never be used again
/// `label` must be a valid Cstr /// `label` must be a valid Cstr
/// `provider` must be a valid, non-null pointer to a location where the handle will be stored /// `provider` must be a valid, non-null pointer to a location where the handle will be stored
#[cfg(feature = "tcp")] #[cfg(feature = "tcp")]

View File

@@ -1,8 +1,11 @@
// Jackson Coxson // Jackson Coxson
use crate::{IdeviceErrorCode, RUNTIME}; use crate::core_device_proxy::AdapterHandle;
use crate::rsd::RsdHandshakeHandle;
use crate::{IdeviceErrorCode, RUNTIME, ReadWriteOpaque};
use idevice::dvt::remote_server::RemoteServerClient; use idevice::dvt::remote_server::RemoteServerClient;
use idevice::{IdeviceError, ReadWrite}; use idevice::tcp::stream::AdapterStream;
use idevice::{IdeviceError, ReadWrite, RsdService};
/// Opaque handle to a RemoteServerClient /// Opaque handle to a RemoteServerClient
pub struct RemoteServerHandle(pub RemoteServerClient<Box<dyn ReadWrite>>); pub struct RemoteServerHandle(pub RemoteServerClient<Box<dyn ReadWrite>>);
@@ -17,25 +20,29 @@ pub struct RemoteServerHandle(pub RemoteServerClient<Box<dyn ReadWrite>>);
/// An error code indicating success or failure /// An error code indicating success or failure
/// ///
/// # Safety /// # Safety
/// `socket` must be a valid pointer to a handle allocated by this library /// `socket` must be a valid pointer to a handle allocated by this library. It is consumed and may
/// not be used again.
/// `handle` must be a valid pointer to a location where the handle will be stored /// `handle` must be a valid pointer to a location where the handle will be stored
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub unsafe extern "C" fn remote_server_new( pub unsafe extern "C" fn remote_server_new(
socket: *mut Box<dyn ReadWrite>, socket: *mut ReadWriteOpaque,
handle: *mut *mut RemoteServerHandle, handle: *mut *mut RemoteServerHandle,
) -> IdeviceErrorCode { ) -> IdeviceErrorCode {
if socket.is_null() { if socket.is_null() {
return IdeviceErrorCode::InvalidArg; return IdeviceErrorCode::InvalidArg;
} }
let connection = unsafe { Box::from_raw(socket) }; let wrapper = unsafe { &mut *socket };
let res: Result<RemoteServerClient<Box<dyn ReadWrite>>, IdeviceError> = let res: Result<RemoteServerClient<Box<dyn ReadWrite>>, IdeviceError> =
RUNTIME.block_on(async move { match wrapper.inner.take() {
let mut client = RemoteServerClient::new(*connection); Some(stream) => RUNTIME.block_on(async move {
client.read_message(0).await?; // Until Message has bindings, we'll do the first read let mut client = RemoteServerClient::new(stream);
Ok(client) client.read_message(0).await?;
}); Ok(client)
}),
None => return IdeviceErrorCode::InvalidArg,
};
match res { match res {
Ok(client) => { Ok(client) => {
@@ -47,6 +54,48 @@ pub unsafe extern "C" fn remote_server_new(
} }
} }
/// Creates a new RemoteServerClient from a handshake and adapter
///
/// # Arguments
/// * [`provider`] - An adapter created by this library
/// * [`handshake`] - An RSD handshake from the same provider
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `provider` must be a valid pointer to a handle allocated by this library
/// `handshake` must be a valid pointer to a location where the handle will be stored
#[unsafe(no_mangle)]
pub unsafe extern "C" fn remote_server_connect_rsd(
provider: *mut AdapterHandle,
handshake: *mut RsdHandshakeHandle,
handle: *mut *mut RemoteServerHandle,
) -> IdeviceErrorCode {
if provider.is_null() || handshake.is_null() || handshake.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let res: Result<RemoteServerClient<AdapterStream>, IdeviceError> =
RUNTIME.block_on(async move {
let provider_ref = unsafe { &mut (*provider).0 };
let handshake_ref = unsafe { &mut (*handshake).0 };
// Connect using the reference
RemoteServerClient::connect_rsd(provider_ref, handshake_ref).await
});
match res {
Ok(d) => {
let boxed = Box::new(RemoteServerHandle(RemoteServerClient::new(Box::new(
d.into_inner(),
))));
unsafe { *handle = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
}
Err(e) => e.into(),
}
}
/// Frees a RemoteServerClient handle /// Frees a RemoteServerClient handle
/// ///
/// # Arguments /// # Arguments

View File

@@ -5,9 +5,9 @@
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::ptr; use std::ptr;
use idevice::{ReadWrite, rsd::RsdHandshake}; use idevice::rsd::RsdHandshake;
use crate::{IdeviceErrorCode, RUNTIME}; use crate::{IdeviceErrorCode, RUNTIME, ReadWriteOpaque};
/// Opaque handle to an RsdHandshake /// Opaque handle to an RsdHandshake
pub struct RsdHandshakeHandle(pub RsdHandshake); pub struct RsdHandshakeHandle(pub RsdHandshake);
@@ -50,20 +50,26 @@ pub struct CRsdServiceArray {
/// An error code indicating success or failure /// An error code indicating success or failure
/// ///
/// # Safety /// # Safety
/// `socket` must be a valid pointer to a ReadWrite handle allocated by this library /// `socket` must be a valid pointer to a ReadWrite handle allocated by this library. It is
/// consumed and may not be used again.
/// `handle` must be a valid pointer to a location where the handle will be stored /// `handle` must be a valid pointer to a location where the handle will be stored
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub unsafe extern "C" fn rsd_handshake_new( pub unsafe extern "C" fn rsd_handshake_new(
socket: *mut Box<dyn ReadWrite>, socket: *mut ReadWriteOpaque,
handle: *mut *mut RsdHandshakeHandle, handle: *mut *mut RsdHandshakeHandle,
) -> IdeviceErrorCode { ) -> IdeviceErrorCode {
if socket.is_null() || handle.is_null() { if socket.is_null() || handle.is_null() {
return IdeviceErrorCode::InvalidArg; return IdeviceErrorCode::InvalidArg;
} }
let connection = unsafe { Box::from_raw(socket) }; let wrapper = unsafe { &mut *socket };
let res = RUNTIME.block_on(async move { RsdHandshake::new(*connection).await }); let res = match wrapper.inner.take() {
Some(mut w) => RUNTIME.block_on(async move { RsdHandshake::new(w.as_mut()).await }),
None => {
return IdeviceErrorCode::InvalidArg;
}
};
match res { match res {
Ok(handshake) => { Ok(handshake) => {