From f72d1383592f1626eacd153ad22938222b72a97a Mon Sep 17 00:00:00 2001 From: Jackson Coxson Date: Mon, 24 Mar 2025 22:49:52 -0600 Subject: [PATCH] Create bindings for heartbeat --- ffi/examples/heartbeat.c | 71 ++++++++++++ ffi/src/heartbeat.rs | 229 +++++++++++++++++++++++++++++++++++++++ ffi/src/lib.rs | 2 + 3 files changed, 302 insertions(+) create mode 100644 ffi/examples/heartbeat.c create mode 100644 ffi/src/heartbeat.rs diff --git a/ffi/examples/heartbeat.c b/ffi/examples/heartbeat.c new file mode 100644 index 0000000..1975884 --- /dev/null +++ b/ffi/examples/heartbeat.c @@ -0,0 +1,71 @@ +// Jackson Coxson + +#include "idevice.h" +#include +#include +#include +#include +#include + +int main() { + // Initialize logger + idevice_init_logger(Debug, Disabled, NULL); + + // Create the socket address (replace with your device's IP) + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(LOCKDOWN_PORT); + inet_pton(AF_INET, "10.7.0.2", &addr.sin_addr); + + // Read pairing file (replace with your pairing file path) + IdevicePairingFile *pairing_file = NULL; + IdeviceErrorCode err = idevice_pairing_file_read( + "/Users/jacksoncoxson/Desktop/storage/00008140-001809302684801C.plist", + &pairing_file); + if (err != IdeviceSuccess) { + fprintf(stderr, "Failed to read pairing file: %d\n", err); + return 1; + } + + // Create TCP provider + TcpProviderHandle *provider = NULL; + err = idevice_tcp_provider_new((struct sockaddr *)&addr, pairing_file, + "ExampleProvider", &provider); + if (err != IdeviceSuccess) { + fprintf(stderr, "Failed to create TCP provider: %d\n", err); + idevice_pairing_file_free(pairing_file); + return 1; + } + + // Connect to installation proxy + HeartbeatClientHandle *client = NULL; + err = heartbeat_connect_tcp(provider, &client); + if (err != IdeviceSuccess) { + fprintf(stderr, "Failed to connect to installation proxy: %d\n", err); + tcp_provider_free(provider); + return 1; + } + tcp_provider_free(provider); + + u_int64_t current_interval = 15; + while (1) { + // Get the new interval + u_int64_t new_interval = 0; + err = heartbeat_get_marco(client, current_interval, &new_interval); + if (err != IdeviceSuccess) { + fprintf(stderr, "Failed to get marco: %d\n", err); + heartbeat_client_free(client); + return 1; + } + current_interval = new_interval + 5; + + // Reply + err = heartbeat_send_polo(client); + if (err != IdeviceSuccess) { + fprintf(stderr, "Failed to get marco: %d\n", err); + heartbeat_client_free(client); + return 1; + } + } +} diff --git a/ffi/src/heartbeat.rs b/ffi/src/heartbeat.rs new file mode 100644 index 0000000..6a4d3aa --- /dev/null +++ b/ffi/src/heartbeat.rs @@ -0,0 +1,229 @@ +// Jackson Coxson + +use std::ffi::c_void; + +use idevice::{ + IdeviceError, IdeviceService, heartbeat::HeartbeatClient, + installation_proxy::InstallationProxyClient, +}; + +use crate::{ + IdeviceErrorCode, IdeviceHandle, RUNTIME, + provider::{TcpProviderHandle, UsbmuxdProviderHandle}, + util, +}; + +pub struct HeartbeatClientHandle(pub HeartbeatClient); +#[allow(non_camel_case_types)] +pub struct plist_t; + +/// Automatically creates and connects to Installation Proxy, returning a client handle +/// +/// # Arguments +/// * [`provider`] - A TcpProvider +/// * [`client`] - On success, will be set to point to a newly allocated InstallationProxyClient handle +/// +/// # Returns +/// An error code indicating success or failure +/// +/// # Safety +/// `provider` must be a valid pointer to a handle allocated by this library +/// `client` must be a valid, non-null pointer to a location where the handle will be stored +#[unsafe(no_mangle)] +pub unsafe extern "C" fn heartbeat_connect_tcp( + provider: *mut TcpProviderHandle, + client: *mut *mut HeartbeatClientHandle, +) -> IdeviceErrorCode { + if provider.is_null() || client.is_null() { + log::error!("Null pointer provided"); + return IdeviceErrorCode::InvalidArg; + } + + let res: Result = RUNTIME.block_on(async move { + // Take ownership of the provider (without immediately dropping it) + let provider_box = unsafe { Box::from_raw(provider) }; + + // Get a reference to the inner value + let provider_ref = &provider_box.0; + + // Connect using the reference + let result = HeartbeatClient::connect(provider_ref).await; + + // Explicitly keep the provider_box alive until after connect completes + std::mem::forget(provider_box); + result + }); + + match res { + Ok(r) => { + let boxed = Box::new(HeartbeatClientHandle(r)); + unsafe { *client = Box::into_raw(boxed) }; + IdeviceErrorCode::IdeviceSuccess + } + Err(e) => { + // If connection failed, the provider_box was already forgotten, + // so we need to reconstruct it to avoid leak + let _ = unsafe { Box::from_raw(provider) }; + e.into() + } + } +} + +/// Automatically creates and connects to Installation Proxy, returning a client handle +/// +/// # Arguments +/// * [`provider`] - A UsbmuxdProvider +/// * [`client`] - On success, will be set to point to a newly allocated InstallationProxyClient handle +/// +/// # Returns +/// An error code indicating success or failure +/// +/// # Safety +/// `provider` must be a valid pointer to a handle allocated by this library +/// `client` must be a valid, non-null pointer to a location where the handle will be stored +#[unsafe(no_mangle)] +pub unsafe extern "C" fn heartbeat_connect_usbmuxd( + provider: *mut UsbmuxdProviderHandle, + client: *mut *mut HeartbeatClientHandle, +) -> IdeviceErrorCode { + if provider.is_null() { + log::error!("Provider is null"); + return IdeviceErrorCode::InvalidArg; + } + + let res: Result = RUNTIME.block_on(async move { + // Take ownership of the provider (without immediately dropping it) + let provider_box = unsafe { Box::from_raw(provider) }; + + // Get a reference to the inner value + let provider_ref = &provider_box.0; + + // Connect using the reference + let result = HeartbeatClient::connect(provider_ref).await; + + // Explicitly keep the provider_box alive until after connect completes + std::mem::forget(provider_box); + result + }); + + match res { + Ok(r) => { + let boxed = Box::new(HeartbeatClientHandle(r)); + unsafe { *client = Box::into_raw(boxed) }; + IdeviceErrorCode::IdeviceSuccess + } + Err(e) => e.into(), + } +} + +/// Automatically creates and connects to Installation Proxy, returning a client handle +/// +/// # Arguments +/// * [`socket`] - An IdeviceSocket handle +/// * [`client`] - On success, will be set to point to a newly allocated InstallationProxyClient handle +/// +/// # Returns +/// An error code indicating success or failure +/// +/// # Safety +/// `socket` must be a valid pointer to a handle allocated by this library +/// `client` must be a valid, non-null pointer to a location where the handle will be stored +#[unsafe(no_mangle)] +pub unsafe extern "C" fn heartbeat_new( + socket: *mut IdeviceHandle, + client: *mut *mut HeartbeatClientHandle, +) -> IdeviceErrorCode { + if socket.is_null() { + return IdeviceErrorCode::InvalidArg; + } + let socket = unsafe { Box::from_raw(socket) }.0; + let r = HeartbeatClient::new(socket); + let boxed = Box::new(HeartbeatClientHandle(r)); + unsafe { *client = Box::into_raw(boxed) }; + IdeviceErrorCode::IdeviceSuccess +} + +/// Sends a polo to the device +/// +/// # Arguments +/// * `client` - A valid HeartbeatClient handle +/// +/// # Returns +/// An error code indicating success or failure +/// +/// # Safety +/// `client` must be a valid pointer to a handle allocated by this library +#[unsafe(no_mangle)] +pub unsafe extern "C" fn heartbeat_send_polo( + client: *mut HeartbeatClientHandle, +) -> IdeviceErrorCode { + let res: Result<(), IdeviceError> = RUNTIME.block_on(async move { + // Take ownership of the client + let mut client_box = unsafe { Box::from_raw(client) }; + + // Get a reference to the inner value + let client_ref = &mut client_box.0; + let res = client_ref.send_polo().await; + + std::mem::forget(client_box); + res + }); + match res { + Ok(_) => IdeviceErrorCode::IdeviceSuccess, + Err(e) => e.into(), + } +} + +/// Sends a polo to the device +/// +/// # Arguments +/// * `client` - A valid HeartbeatClient handle +/// * `interval` - The time to wait for a marco +/// * `new_interval` - A pointer to set the requested marco +/// +/// # Returns +/// An error code indicating success or failure. +/// +/// # Safety +/// `client` must be a valid pointer to a handle allocated by this library +#[unsafe(no_mangle)] +pub unsafe extern "C" fn heartbeat_get_marco( + client: *mut HeartbeatClientHandle, + interval: u64, + new_interval: *mut u64, +) -> IdeviceErrorCode { + let res: Result = RUNTIME.block_on(async move { + // Take ownership of the client + let mut client_box = unsafe { Box::from_raw(client) }; + + // Get a reference to the inner value + let client_ref = &mut client_box.0; + let new = client_ref.get_marco(interval).await; + + std::mem::forget(client_box); + new + }); + match res { + Ok(n) => { + unsafe { *new_interval = n }; + IdeviceErrorCode::IdeviceSuccess + } + Err(e) => e.into(), + } +} + +/// Frees a handle +/// +/// # Arguments +/// * [`handle`] - The handle to free +/// +/// # Safety +/// `handle` must be a valid pointer to the handle that was allocated by this library, +/// or NULL (in which case this function does nothing) +#[unsafe(no_mangle)] +pub unsafe extern "C" fn heartbeat_client_free(handle: *mut HeartbeatClientHandle) { + if !handle.is_null() { + log::debug!("Freeing installation_proxy_client"); + let _ = unsafe { Box::from_raw(handle) }; + } +} diff --git a/ffi/src/lib.rs b/ffi/src/lib.rs index ddc34f0..73f2139 100644 --- a/ffi/src/lib.rs +++ b/ffi/src/lib.rs @@ -1,6 +1,7 @@ // Jackson Coxson mod errors; +pub mod heartbeat; pub mod installation_proxy; pub mod logging; mod pairing_file; @@ -19,6 +20,7 @@ use tokio::runtime::{self, Runtime}; static RUNTIME: Lazy = Lazy::new(|| { runtime::Builder::new_multi_thread() .enable_io() + .enable_time() .build() .unwrap() });