1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! USB Multiplexing Daemon (usbmuxd) Client
//!
//! Provides functionality for interacting with the usbmuxd service which manages
//! connections to iOS devices over USB and network and pairing files
use std::{
net::{AddrParseError, IpAddr, SocketAddr},
pin::Pin,
str::FromStr,
};
#[cfg(not(unix))]
use std::net::{Ipv4Addr, SocketAddrV4};
use futures::Stream;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tracing::{debug, warn};
use crate::{
Idevice, IdeviceError, ReadWrite, pairing_file::PairingFile, provider::UsbmuxdProvider,
usbmuxd::des::DeviceListResponse,
};
use errors::UsbmuxdError;
mod des;
pub mod errors;
mod raw_packet;
/// Represents the connection type of a device
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Connection {
/// Connected via USB
Usb,
/// Connected via network with specific IP address
Network(IpAddr),
/// Unknown connection type with description
Unknown(String),
}
/// Represents a device connected through usbmuxd
#[derive(Debug, Clone)]
pub struct UsbmuxdDevice {
/// How the device is connected
pub connection_type: Connection,
/// Unique Device Identifier
pub udid: String,
/// usbmuxd-assigned device ID
pub device_id: u32,
}
/// Listen events from the socket
#[derive(Debug, Clone)]
pub enum UsbmuxdListenEvent {
Connected(UsbmuxdDevice),
/// The mux ID
Disconnected(u32),
}
/// Active connection to the usbmuxd service
#[derive(Debug)]
pub struct UsbmuxdConnection {
socket: Box<dyn ReadWrite>,
tag: u32,
}
/// Address of the usbmuxd service
#[allow(missing_copy_implementations)]
#[derive(Clone, Debug)]
pub enum UsbmuxdAddr {
/// Unix domain socket path (Unix systems only)
#[cfg(unix)]
UnixSocket(String),
/// TCP socket address
TcpSocket(SocketAddr),
}
impl UsbmuxdAddr {
/// Default TCP port for usbmuxd
pub const DEFAULT_PORT: u16 = 27015;
/// Default Unix socket path for usbmuxd
pub const SOCKET_FILE: &'static str = "/var/run/usbmuxd";
/// Connects to the usbmuxd service
///
/// # Returns
/// A boxed transport stream
///
/// # Errors
/// Returns `IdeviceError` if connection fails
pub async fn to_socket(&self) -> Result<Box<dyn ReadWrite>, IdeviceError> {
Ok(match self {
#[cfg(unix)]
Self::UnixSocket(addr) => Box::new(tokio::net::UnixStream::connect(addr).await?),
Self::TcpSocket(addr) => Box::new(tokio::net::TcpStream::connect(addr).await?),
})
}
/// Creates a new usbmuxd connection
///
/// # Arguments
/// * `tag` - Connection tag/identifier
///
/// # Returns
/// A connected `UsbmuxdConnection`
pub async fn connect(&self, tag: u32) -> Result<UsbmuxdConnection, IdeviceError> {
let socket = self.to_socket().await?;
Ok(UsbmuxdConnection::new(socket, tag))
}
/// Creates a UsbmuxdAddr from environment variable
///
/// Checks `USBMUXD_SOCKET_ADDRESS` environment variable, falls back to default
///
/// # Returns
/// Configured UsbmuxdAddr or parse error
pub fn from_env_var() -> Result<Self, AddrParseError> {
Ok(match std::env::var("USBMUXD_SOCKET_ADDRESS") {
Ok(var) => {
#[cfg(unix)]
if var.contains(':') {
Self::TcpSocket(SocketAddr::from_str(&var)?)
} else {
Self::UnixSocket(var)
}
#[cfg(not(unix))]
Self::TcpSocket(SocketAddr::from_str(&var)?)
}
Err(_) => Self::default(),
})
}
}
impl Default for UsbmuxdAddr {
/// Creates default usbmuxd address based on platform:
/// - Unix: Uses default socket path
/// - Non-Unix: Uses localhost TCP port
fn default() -> Self {
#[cfg(not(unix))]
{
Self::TcpSocket(SocketAddr::V4(SocketAddrV4::new(
Ipv4Addr::new(127, 0, 0, 1),
Self::DEFAULT_PORT,
)))
}
#[cfg(unix)]
Self::UnixSocket(Self::SOCKET_FILE.to_string())
}
}
impl UsbmuxdConnection {
/// Binary PLIST protocol version
pub const BINARY_PLIST_VERSION: u32 = 0;
/// XML PLIST protocol version
pub const XML_PLIST_VERSION: u32 = 1;
/// Result message type
pub const RESULT_MESSAGE_TYPE: u32 = 1;
/// PLIST message type
pub const PLIST_MESSAGE_TYPE: u32 = 8;
/// Creates a default usbmuxd connection
///
/// Uses default address based on platform
///
/// # Returns
/// Connected `UsbmuxdConnection` or error
pub async fn default() -> Result<Self, IdeviceError> {
let socket = UsbmuxdAddr::default().to_socket().await?;
Ok(Self {
socket: Box::new(socket),
tag: 0,
})
}
/// Creates a new usbmuxd connection
///
/// # Arguments
/// * `socket` - The transport stream
/// * `tag` - Connection tag/identifier
pub fn new(socket: Box<dyn ReadWrite>, tag: u32) -> Self {
Self { socket, tag }
}
/// Lists all connected devices
///
/// # Returns
/// Vector of connected devices
///
/// # Errors
/// Returns `IdeviceError` if:
/// - Communication fails
/// - Response is malformed
/// - Device info is incomplete
pub async fn get_devices(&mut self) -> Result<Vec<UsbmuxdDevice>, IdeviceError> {
let mut req = plist::Dictionary::new();
req.insert("MessageType".into(), "ListDevices".into());
req.insert("ClientVersionString".into(), "idevice-rs".into());
req.insert("kLibUSBMuxVersion".into(), 3.into());
self.write_plist(req).await?;
let res = self.read_plist().await?;
let res = plist::to_value(&res)?;
let res = plist::from_value::<des::ListDevicesResponse>(&res)?;
let devs = res
.device_list
.into_iter()
.flat_map(|x| x.into_usbmuxd_dev())
.collect::<Vec<UsbmuxdDevice>>();
Ok(devs)
}
/// Gets a specific device by UDID
///
/// # Arguments
/// * `udid` - The device UDID to find
///
/// # Returns
/// The matching device or error if not found
pub async fn get_device(&mut self, udid: &str) -> Result<UsbmuxdDevice, IdeviceError> {
let devices = self.get_devices().await?;
match devices.into_iter().find(|x| x.udid == udid) {
Some(d) => Ok(d),
None => Err(IdeviceError::DeviceNotFound),
}
}
/// Gets the pairing record for a device
///
/// # Arguments
/// * `udid` - The device UDID
///
/// # Returns
/// The pairing file or error
pub async fn get_pair_record(&mut self, udid: &str) -> Result<PairingFile, IdeviceError> {
debug!("Getting pair record for {udid}");
let mut req = plist::Dictionary::new();
req.insert("MessageType".into(), "ReadPairRecord".into());
req.insert("PairRecordID".into(), udid.into());
self.write_plist(req).await?;
let res = self.read_plist().await?;
match res.get("PairRecordData") {
Some(plist::Value::Data(d)) => PairingFile::from_bytes(d),
_ => Err(IdeviceError::UnexpectedResponse(
"missing PairRecordData in pair record response".into(),
)),
}
}
/// Gets the BUID
///
/// # Returns
/// The BUID string or error
pub async fn get_buid(&mut self) -> Result<String, IdeviceError> {
let mut req = plist::Dictionary::new();
req.insert("MessageType".into(), "ReadBUID".into());
self.write_plist(req).await?;
let mut res = self.read_plist().await?;
match res.remove("BUID") {
Some(plist::Value::String(s)) => Ok(s),
_ => Err(IdeviceError::UnexpectedResponse(
"missing BUID string in ReadBUID response".into(),
)),
}
}
/// Connects to a service on the device
///
/// # Arguments
/// * `device_id` - usbmuxd device ID
/// * `port` - TCP port to connect to (host byte order)
/// * `label` - Connection label
///
/// # Returns
/// An `Idevice` connection or error
pub async fn connect_to_device(
mut self,
device_id: u32,
port: u16,
label: impl Into<String>,
) -> Result<Idevice, IdeviceError> {
debug!("Connecting to device {device_id} on port {port}");
let port = port.to_be();
let mut req = plist::Dictionary::new();
req.insert("MessageType".into(), "Connect".into());
req.insert("DeviceID".into(), device_id.into());
req.insert("PortNumber".into(), port.into());
self.write_plist(req).await?;
match self.read_plist().await?.get("Number") {
Some(plist::Value::Integer(i)) => match i.as_unsigned() {
Some(0) => Ok(Idevice::new(self.socket, label)),
Some(1) => Err(UsbmuxdError::BadCommand.into()),
Some(2) => Err(UsbmuxdError::BadDevice.into()),
Some(3) => Err(UsbmuxdError::ConnectionRefused.into()),
Some(6) => Err(UsbmuxdError::BadVersion.into()),
_ => Err(IdeviceError::UnexpectedResponse(
"unknown usbmuxd connect error code".into(),
)),
},
_ => Err(IdeviceError::UnexpectedResponse(
"missing Number field in usbmuxd connect response".into(),
)),
}
}
/// Tells usbmuxd to save the pairing record in its storage
///
/// # Arguments
/// * `device_id` - usbmuxd device ID
/// * `udid` - the device UDID/serial
/// * `pair_record` - a serialized plist of the pair record
pub async fn save_pair_record(
&mut self,
udid: &str,
pair_record: Vec<u8>,
) -> Result<(), IdeviceError> {
let req = crate::plist!(dict {
"MessageType": "SavePairRecord",
"PairRecordData": pair_record,
"PairRecordID": udid,
});
self.write_plist(req).await?;
let res = self.read_plist().await?;
match res.get("Number").and_then(|x| x.as_unsigned_integer()) {
Some(0) => Ok(()),
_ => Err(IdeviceError::UnexpectedResponse(
"SavePairRecord did not return success".into(),
)),
}
}
pub async fn listen<'a>(
&'a mut self,
) -> Result<
Pin<Box<dyn Stream<Item = Result<UsbmuxdListenEvent, IdeviceError>> + 'a>>,
IdeviceError,
> {
let req = crate::plist!(dict {
"MessageType": "Listen",
});
self.write_plist(req).await?;
// First, read the handshake response to confirm the "Listen" request was successful
let res = self.read_plist().await?;
match res.get("Number").and_then(|x| x.as_unsigned_integer()) {
Some(0) => {
// Success, now create the stream
let stream = futures::stream::try_unfold(self, |conn| async move {
// This loop is to skip non-Attach/Detach messages
loop {
// Read the next packet. This will propagate IO errors.
let msg = conn.read_plist().await?;
if let Some(plist::Value::String(s)) = msg.get("MessageType") {
match s.as_str() {
"Attached" => {
if let Ok(props) = plist::from_value::<DeviceListResponse>(
&plist::Value::Dictionary(msg),
) {
let dev: UsbmuxdDevice = match props.into_usbmuxd_dev() {
Ok(d) => d,
Err(e) => {
warn!(
"Failed to convert props into usbmuxd device: {e:?}"
);
continue;
}
};
let res = UsbmuxdListenEvent::Connected(dev);
// Yield the device and the next state
return Ok(Some((res, conn)));
} else {
warn!(
"Received malformed message during listen (no device props and ID)"
);
}
}
"Detached" => {
// Log it and continue the loop to wait for the next message
if let Some(id) =
msg.get("DeviceID").and_then(|v| v.as_unsigned_integer())
{
let res = UsbmuxdListenEvent::Disconnected(id as u32);
return Ok(Some((res, conn)));
} else {
debug!("Device detached (unknown ID)");
}
// Continue loop
}
_ => {
// Unexpected message type, log and continue
warn!("Received unexpected message type during listen: {}", s);
// Continue loop
}
}
} else {
// Malformed message, log and continue
warn!("Received malformed message during listen (no MessageType)");
// Continue loop
}
}
});
// Box and Pin the stream
Ok(Box::pin(stream))
}
_ => {
// "Listen" request failed
Err(IdeviceError::UnexpectedResponse(
"usbmuxd Listen request did not return success".into(),
))
}
}
}
/// Writes a PLIST message to usbmuxd
async fn write_plist(&mut self, req: plist::Dictionary) -> Result<(), IdeviceError> {
let raw = raw_packet::RawPacket::new(
req,
Self::XML_PLIST_VERSION,
Self::PLIST_MESSAGE_TYPE,
self.tag,
);
let raw: Vec<u8> = raw.into();
self.socket.write_all(&raw).await?;
Ok(())
}
/// Reads a PLIST message from usbmuxd
async fn read_plist(&mut self) -> Result<plist::Dictionary, IdeviceError> {
let mut header_buffer = [0; 16];
self.socket.read_exact(&mut header_buffer).await?;
// We are safe to unwrap as it only panics if the buffer isn't 4
let packet_size = u32::from_le_bytes(header_buffer[..4].try_into().unwrap()) - 16;
debug!("Reading {packet_size} bytes from muxer");
let mut body_buffer = vec![0; packet_size as usize];
self.socket.read_exact(&mut body_buffer).await?;
let res = plist::from_bytes(&body_buffer)?;
debug!("Read from muxer: {}", crate::pretty_print_dictionary(&res));
Ok(res)
}
}
impl UsbmuxdDevice {
/// Creates a provider for this device
///
/// # Arguments
/// * `addr` - usbmuxd address
/// * `tag` - Connection tag
/// * `label` - Connection label
///
/// # Returns
/// Configured `UsbmuxdProvider`
pub fn to_provider(&self, addr: UsbmuxdAddr, label: impl Into<String>) -> UsbmuxdProvider {
let label = label.into();
UsbmuxdProvider {
addr,
tag: self.device_id,
udid: self.udid.clone(),
device_id: self.device_id,
label,
}
}
}