Update FFI to use error struct for message

This commit is contained in:
Jackson Coxson
2025-06-02 19:42:17 -06:00
parent 5d7aa50a7d
commit 35ba07889b
36 changed files with 1358 additions and 1135 deletions

View File

@@ -1,11 +1,12 @@
// Jackson Coxson
use std::ffi::c_void;
use std::{ffi::c_void, ptr::null_mut};
use idevice::{IdeviceError, IdeviceService, lockdown::LockdownClient, provider::IdeviceProvider};
use crate::{
IdeviceErrorCode, IdeviceHandle, IdevicePairingFile, RUNTIME, provider::IdeviceProviderHandle,
IdeviceFfiError, IdeviceHandle, IdevicePairingFile, RUNTIME, ffi_err,
provider::IdeviceProviderHandle,
};
pub struct LockdowndClientHandle(pub LockdownClient);
@@ -17,7 +18,7 @@ pub struct LockdowndClientHandle(pub LockdownClient);
/// * [`client`] - On success, will be set to point to a newly allocated LockdowndClient handle
///
/// # Returns
/// An error code indicating success or failure
/// An IdeviceFfiError on error, null on success
///
/// # Safety
/// `provider` must be a valid pointer to a handle allocated by this library
@@ -26,10 +27,10 @@ pub struct LockdowndClientHandle(pub LockdownClient);
pub unsafe extern "C" fn lockdownd_connect(
provider: *mut IdeviceProviderHandle,
client: *mut *mut LockdowndClientHandle,
) -> IdeviceErrorCode {
) -> *mut IdeviceFfiError {
if provider.is_null() || client.is_null() {
log::error!("Null pointer provided");
return IdeviceErrorCode::InvalidArg;
return ffi_err!(IdeviceError::FfiInvalidArg);
}
let res: Result<LockdownClient, IdeviceError> = RUNTIME.block_on(async move {
@@ -41,11 +42,11 @@ pub unsafe extern "C" fn lockdownd_connect(
Ok(r) => {
let boxed = Box::new(LockdowndClientHandle(r));
unsafe { *client = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
null_mut()
}
Err(e) => {
let _ = unsafe { Box::from_raw(provider) };
e.into()
ffi_err!(e)
}
}
}
@@ -57,7 +58,7 @@ pub unsafe extern "C" fn lockdownd_connect(
/// * [`client`] - On success, will be set to point to a newly allocated LockdowndClient handle
///
/// # Returns
/// An error code indicating success or failure
/// An IdeviceFfiError on error, null on success
///
/// # Safety
/// `socket` must be a valid pointer to a handle allocated by this library. The socket is consumed,
@@ -67,15 +68,15 @@ pub unsafe extern "C" fn lockdownd_connect(
pub unsafe extern "C" fn lockdownd_new(
socket: *mut IdeviceHandle,
client: *mut *mut LockdowndClientHandle,
) -> IdeviceErrorCode {
) -> *mut IdeviceFfiError {
if socket.is_null() || client.is_null() {
return IdeviceErrorCode::InvalidArg;
return ffi_err!(IdeviceError::FfiInvalidArg);
}
let socket = unsafe { Box::from_raw(socket) }.0;
let r = LockdownClient::new(socket);
let boxed = Box::new(LockdowndClientHandle(r));
unsafe { *client = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
null_mut()
}
/// Starts a session with lockdownd
@@ -85,7 +86,7 @@ pub unsafe extern "C" fn lockdownd_new(
/// * `pairing_file` - An IdevicePairingFile alocated by this library
///
/// # Returns
/// An error code indicating success or failure
/// An IdeviceFfiError on error, null on success
///
/// # Safety
/// `client` must be a valid pointer to a handle allocated by this library
@@ -94,7 +95,7 @@ pub unsafe extern "C" fn lockdownd_new(
pub unsafe extern "C" fn lockdownd_start_session(
client: *mut LockdowndClientHandle,
pairing_file: *mut IdevicePairingFile,
) -> IdeviceErrorCode {
) -> *mut IdeviceFfiError {
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move {
let client_ref = unsafe { &mut (*client).0 };
let pairing_file_ref = unsafe { &(*pairing_file).0 };
@@ -103,8 +104,8 @@ pub unsafe extern "C" fn lockdownd_start_session(
});
match res {
Ok(_) => IdeviceErrorCode::IdeviceSuccess,
Err(e) => e.into(),
Ok(_) => null_mut(),
Err(e) => ffi_err!(e),
}
}
@@ -117,7 +118,7 @@ pub unsafe extern "C" fn lockdownd_start_session(
/// * `ssl` - Pointer to store whether SSL should be enabled
///
/// # Returns
/// An error code indicating success or failure
/// An IdeviceFfiError on error, null on success
///
/// # Safety
/// `client` must be a valid pointer to a handle allocated by this library
@@ -129,9 +130,9 @@ pub unsafe extern "C" fn lockdownd_start_service(
identifier: *const libc::c_char,
port: *mut u16,
ssl: *mut bool,
) -> IdeviceErrorCode {
) -> *mut IdeviceFfiError {
if identifier.is_null() || port.is_null() || ssl.is_null() {
return IdeviceErrorCode::InvalidArg;
return ffi_err!(IdeviceError::FfiInvalidArg);
}
let identifier = unsafe { std::ffi::CStr::from_ptr(identifier) }
@@ -149,9 +150,9 @@ pub unsafe extern "C" fn lockdownd_start_service(
*port = p;
*ssl = s;
}
IdeviceErrorCode::IdeviceSuccess
null_mut()
}
Err(e) => e.into(),
Err(e) => ffi_err!(e),
}
}
@@ -164,7 +165,7 @@ pub unsafe extern "C" fn lockdownd_start_service(
/// * `out_plist` - Pointer to store the returned plist value
///
/// # Returns
/// An error code indicating success or failure
/// An IdeviceFfiError on error, null on success
///
/// # Safety
/// `client` must be a valid pointer to a handle allocated by this library
@@ -176,9 +177,9 @@ pub unsafe extern "C" fn lockdownd_get_value(
key: *const libc::c_char,
domain: *const libc::c_char,
out_plist: *mut *mut c_void,
) -> IdeviceErrorCode {
) -> *mut IdeviceFfiError {
if key.is_null() || out_plist.is_null() {
return IdeviceErrorCode::InvalidArg;
return ffi_err!(IdeviceError::FfiInvalidArg);
}
let value = unsafe { std::ffi::CStr::from_ptr(key) }
@@ -205,9 +206,9 @@ pub unsafe extern "C" fn lockdownd_get_value(
unsafe {
*out_plist = crate::util::plist_to_libplist(&value);
}
IdeviceErrorCode::IdeviceSuccess
null_mut()
}
Err(e) => e.into(),
Err(e) => ffi_err!(e),
}
}
@@ -218,7 +219,7 @@ pub unsafe extern "C" fn lockdownd_get_value(
/// * `out_plist` - Pointer to store the returned plist dictionary
///
/// # Returns
/// An error code indicating success or failure
/// An IdeviceFfiError on error, null on success
///
/// # Safety
/// `client` must be a valid pointer to a handle allocated by this library
@@ -227,9 +228,9 @@ pub unsafe extern "C" fn lockdownd_get_value(
pub unsafe extern "C" fn lockdownd_get_all_values(
client: *mut LockdowndClientHandle,
out_plist: *mut *mut c_void,
) -> IdeviceErrorCode {
) -> *mut IdeviceFfiError {
if out_plist.is_null() {
return IdeviceErrorCode::InvalidArg;
return ffi_err!(IdeviceError::FfiInvalidArg);
}
let res: Result<plist::Dictionary, IdeviceError> = RUNTIME.block_on(async move {
@@ -242,9 +243,9 @@ pub unsafe extern "C" fn lockdownd_get_all_values(
unsafe {
*out_plist = crate::util::plist_to_libplist(&plist::Value::Dictionary(dict));
}
IdeviceErrorCode::IdeviceSuccess
null_mut()
}
Err(e) => e.into(),
Err(e) => ffi_err!(e),
}
}