Add adapter_close to stop TCP stack

This commit is contained in:
Jackson Coxson
2025-10-22 10:26:13 -06:00
parent dd2db92967
commit 5ed2144d9e
4 changed files with 80 additions and 30 deletions

View File

@@ -53,6 +53,14 @@ class Adapter {
return Ok(ReadWrite::adopt(s)); return Ok(ReadWrite::adopt(s));
} }
Result<void, FfiError> close() {
FfiError e(::adapter_close(handle_.get()));
if (e) {
return Err(e);
}
return Ok();
}
private: private:
explicit Adapter(AdapterHandle* h) noexcept : handle_(h) {} explicit Adapter(AdapterHandle* h) noexcept : handle_(h) {}
AdapterPtr handle_{}; AdapterPtr handle_{};

View File

@@ -6,10 +6,11 @@
namespace IdeviceFFI { namespace IdeviceFFI {
Result<void, FfiError> AdapterStream::close() { Result<void, FfiError> AdapterStream::close() {
if (!h_) if (!h_) {
return Ok(); return Ok();
}
FfiError e(::adapter_close(h_)); FfiError e(::adapter_stream_close(h_));
if (e) { if (e) {
return Err(e); return Err(e);
} }
@@ -18,9 +19,10 @@ Result<void, FfiError> AdapterStream::close() {
return Ok(); return Ok();
} }
Result<void, FfiError> AdapterStream::send(const uint8_t *data, size_t len) { Result<void, FfiError> AdapterStream::send(const uint8_t* data, size_t len) {
if (!h_) if (!h_) {
return Err(FfiError::NotConnected()); return Err(FfiError::NotConnected());
}
FfiError e(::adapter_send(h_, data, len)); FfiError e(::adapter_send(h_, data, len));
if (e) { if (e) {
return Err(e); return Err(e);
@@ -29,11 +31,13 @@ Result<void, FfiError> AdapterStream::send(const uint8_t *data, size_t len) {
} }
Result<std::vector<uint8_t>, FfiError> AdapterStream::recv(size_t max_hint) { Result<std::vector<uint8_t>, FfiError> AdapterStream::recv(size_t max_hint) {
if (!h_) if (!h_) {
return Err(FfiError::NotConnected()); return Err(FfiError::NotConnected());
}
if (max_hint == 0) if (max_hint == 0) {
max_hint = 2048; max_hint = 2048;
}
std::vector<uint8_t> out(max_hint); std::vector<uint8_t> out(max_hint);
size_t actual = 0; size_t actual = 0;

View File

@@ -103,7 +103,9 @@ pub unsafe extern "C" fn adapter_pcap(
/// # Safety /// # Safety
/// `handle` must be a valid pointer to a handle allocated by this library /// `handle` must be a valid pointer to a handle allocated by this library
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub unsafe extern "C" fn adapter_close(handle: *mut AdapterStreamHandle) -> *mut IdeviceFfiError { pub unsafe extern "C" fn adapter_stream_close(
handle: *mut AdapterStreamHandle,
) -> *mut IdeviceFfiError {
if handle.is_null() { if handle.is_null() {
return ffi_err!(IdeviceError::FfiInvalidArg); return ffi_err!(IdeviceError::FfiInvalidArg);
} }
@@ -114,6 +116,28 @@ pub unsafe extern "C" fn adapter_close(handle: *mut AdapterStreamHandle) -> *mut
null_mut() null_mut()
} }
/// Stops the entire adapter TCP stack
///
/// # Arguments
/// * [`handle`] - The adapter handle
///
/// # Returns
/// Null on success, an IdeviceFfiError otherwise
///
/// # Safety
/// `handle` must be a valid pointer to a handle allocated by this library
#[unsafe(no_mangle)]
pub unsafe extern "C" fn adapter_close(handle: *mut AdapterHandle) -> *mut IdeviceFfiError {
if handle.is_null() {
return ffi_err!(IdeviceError::FfiInvalidArg);
}
let adapter = unsafe { &mut (*handle).0 };
RUNTIME.block_on(async move { adapter.close().await.ok() });
null_mut()
}
/// Sends data through the adapter stream /// Sends data through the adapter stream
/// ///
/// # Arguments /// # Arguments

View File

@@ -38,6 +38,7 @@ enum HandleMessage {
path: PathBuf, path: PathBuf,
res: oneshot::Sender<Result<(), std::io::Error>>, res: oneshot::Sender<Result<(), std::io::Error>>,
}, },
Die,
} }
#[derive(Debug)] #[derive(Debug)]
@@ -90,6 +91,9 @@ impl AdapterHandle {
res res
} => { } => {
res.send(adapter.pcap(path).await).ok(); res.send(adapter.pcap(path).await).ok();
},
HandleMessage::Die => {
break;
} }
}, },
Err(_) => { Err(_) => {
@@ -218,6 +222,16 @@ impl AdapterHandle {
)), )),
} }
} }
pub async fn close(&mut self) -> Result<(), std::io::Error> {
if self.sender.send(HandleMessage::Die).is_err() {
return Err(std::io::Error::new(
std::io::ErrorKind::NetworkUnreachable,
"adapter closed",
));
}
Ok(())
}
} }
#[derive(Debug)] #[derive(Debug)]