Log decoded XPC messages

This commit is contained in:
Jackson Coxson
2025-05-22 21:54:45 -06:00
parent 9a1987d923
commit 9a02e2bb6d
2 changed files with 28 additions and 14 deletions

View File

@@ -5,7 +5,7 @@ use std::{
}; };
use indexmap::IndexMap; use indexmap::IndexMap;
use log::warn; use log::{debug, warn};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::IdeviceError; use crate::IdeviceError;
@@ -217,6 +217,7 @@ impl XPCObject {
} }
pub fn decode(buf: &[u8]) -> Result<Self, IdeviceError> { pub fn decode(buf: &[u8]) -> Result<Self, IdeviceError> {
debug!("Decoding {buf:02X?}");
if buf.len() < 8 { if buf.len() < 8 {
return Err(IdeviceError::NotEnoughBytes(buf.len(), 8)); return Err(IdeviceError::NotEnoughBytes(buf.len(), 8));
} }
@@ -407,6 +408,7 @@ impl XPCMessage {
} }
pub fn decode(data: &[u8]) -> Result<XPCMessage, IdeviceError> { pub fn decode(data: &[u8]) -> Result<XPCMessage, IdeviceError> {
debug!("Decoding {data:02X?}");
if data.len() < 24 { if data.len() < 24 {
Err(IdeviceError::NotEnoughBytes(data.len(), 24))? Err(IdeviceError::NotEnoughBytes(data.len(), 24))?
} }
@@ -421,6 +423,7 @@ impl XPCMessage {
let body_len = u64::from_le_bytes([ let body_len = u64::from_le_bytes([
data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15], data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15],
]); ]);
debug!("Body_len: {body_len}");
let message_id = u64::from_le_bytes([ let message_id = u64::from_le_bytes([
data[16], data[17], data[18], data[19], data[20], data[21], data[22], data[23], data[16], data[17], data[18], data[19], data[20], data[21], data[22], data[23],
]); ]);
@@ -429,22 +432,21 @@ impl XPCMessage {
"Body length is {body_len}, but received bytes is {}", "Body length is {body_len}, but received bytes is {}",
data.len() data.len()
); );
println!("{}", String::from_utf8_lossy(data));
Err(IdeviceError::PacketSizeMismatch)? Err(IdeviceError::PacketSizeMismatch)?
} }
if body_len == 0 { let res = XPCMessage {
return Ok(XPCMessage {
flags, flags,
message: None, message: if body_len > 0 {
Some(XPCObject::decode(&data[24..24 + body_len as usize])?)
} else {
None
},
message_id: Some(message_id), message_id: Some(message_id),
}); };
}
Ok(XPCMessage { debug!("Decoded {res:#?}");
flags, Ok(res)
message: Some(XPCObject::decode(&data[24..24 + body_len as usize])?),
message_id: Some(message_id),
})
} }
pub fn encode(self, message_id: u64) -> Result<Vec<u8>, IdeviceError> { pub fn encode(self, message_id: u64) -> Result<Vec<u8>, IdeviceError> {

View File

@@ -49,8 +49,9 @@ impl<R: ReadWrite> RemoteXpcClient<R> {
None, None,
)) ))
.await?; .await?;
self.h2_client.read(ROOT_CHANNEL).await?;
self.h2_client.read(ROOT_CHANNEL).await?; self.recv_root().await?;
self.recv_root().await?;
debug!("Sending weird flags"); debug!("Sending weird flags");
self.send_root(XPCMessage::new(Some(XPCFlag::Custom(0x201)), None, None)) self.send_root(XPCMessage::new(Some(XPCFlag::Custom(0x201)), None, None))
@@ -102,6 +103,17 @@ impl<R: ReadWrite> RemoteXpcClient<R> {
} }
} }
pub async fn recv_root(&mut self) -> Result<Option<plist::Value>, IdeviceError> {
let msg = self.h2_client.read(ROOT_CHANNEL).await?;
let msg = XPCMessage::decode(&msg)?;
if let Some(msg) = msg.message {
Ok(Some(msg.to_plist()))
} else {
Ok(None)
}
}
async fn send_root(&mut self, msg: XPCMessage) -> Result<(), IdeviceError> { async fn send_root(&mut self, msg: XPCMessage) -> Result<(), IdeviceError> {
self.h2_client self.h2_client
.send(msg.encode(self.root_id)?, ROOT_CHANNEL) .send(msg.encode(self.root_id)?, ROOT_CHANNEL)