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
// jkcoxson
use std::convert::TryInto;
use std::marker::PhantomData;
use std::os::raw::c_char;
use crate::bindings as unsafe_bindings;
use crate::error::IdeviceError;
use crate::idevice::Device;
pub struct DeviceConnection<'a> {
pub(crate) pointer: *mut unsafe_bindings::idevice_connection_private,
phantom: PhantomData<&'a Device>,
}
pub struct SslData {}
pub enum DeviceConnectionType {
Usbmuxd,
Network,
}
impl DeviceConnection<'_> {
/// Create a connection to an iOS device
/// This is NOT a lockdown connection, for things like debugging use a specific service
/// # Arguments
/// * `device` - The device to create a connection to
/// * `port` - The port to connect to
/// # Returns
/// A handle for the connection
///
/// ***Verified:*** False
pub fn connect(device: Device, port: u16) -> Result<Self, IdeviceError> {
let mut to_fill = unsafe { std::mem::zeroed() };
let result =
unsafe { unsafe_bindings::idevice_connect(device.pointer, port, &mut to_fill) }.into();
if result != IdeviceError::Success {
return Err(result);
}
Ok(DeviceConnection {
pointer: to_fill,
phantom: std::marker::PhantomData,
})
}
/// Sends data to the device
/// # Arguments
/// * `data` - The data to send
/// # Returns
/// The number of bytes sent
///
/// ***Verified:*** False
pub fn send(&self, data: Vec<u8>) -> Result<u32, IdeviceError> {
let mut to_fill = unsafe { std::mem::zeroed() };
let result = unsafe {
unsafe_bindings::idevice_connection_send(
self.pointer,
data.as_ptr() as *const c_char,
data.len().try_into().unwrap(),
&mut to_fill,
)
}
.into();
if result != IdeviceError::Success {
return Err(result);
}
Ok(to_fill)
}
/// Receives data from the device
/// # Arguments
/// * `len` - The number of bytes to receive
/// * `timeout` - The timeout in milliseconds
/// # Returns
/// The received data
///
/// ***Verified:*** False
pub fn recieve(&self, len: u32, timeout: u32) -> Result<c_char, IdeviceError> {
let mut buffer = unsafe { std::mem::zeroed() };
let mut recieved = unsafe { std::mem::zeroed() };
let result = match timeout > 0 {
true => unsafe {
unsafe_bindings::idevice_connection_receive_timeout(
self.pointer,
&mut buffer,
len,
&mut recieved,
timeout,
)
},
false => unsafe {
unsafe_bindings::idevice_connection_receive(
self.pointer,
&mut buffer,
len,
&mut recieved,
)
},
}
.into();
if result != IdeviceError::Success {
return Err(result);
}
Ok(buffer) // idk if this is correct
}
/// Toggles SSL on the connection
/// # Arguments
/// * `enable` - Whether to enable SSL
/// # Returns
/// *none*
///
/// ***Verified:*** False
pub fn enable_ssl(&self, enable: bool) -> Result<(), IdeviceError> {
let result = match enable {
true => unsafe { unsafe_bindings::idevice_connection_enable_ssl(self.pointer) },
false => unsafe { unsafe_bindings::idevice_connection_disable_ssl(self.pointer) },
}
.into();
if result != IdeviceError::Success {
return Err(result);
}
Ok(())
}
/// Bypasses the SSL on the iOS device
/// # Arguments
/// * `bypass` - Whether to close the connection or not
/// # Returns
/// *none*
///
/// ***Verified:*** False
pub fn disable_bypass_ssl(&self, bypass: bool) -> Result<(), IdeviceError> {
let result = unsafe {
unsafe_bindings::idevice_connection_disable_bypass_ssl(self.pointer, bypass as u8)
}
.into();
if result != IdeviceError::Success {
return Err(result);
}
Ok(())
}
/// Gets the file descriptor of the connection
/// # Arguments
/// *none*
/// # Returns
/// The file descriptor
///
/// ***Verified:*** False
pub fn get_fd(&self) -> i32 {
let mut to_fill = unsafe { std::mem::zeroed() };
unsafe { unsafe_bindings::idevice_connection_get_fd(self.pointer, &mut to_fill) };
to_fill
}
}
impl Drop for DeviceConnection<'_> {
fn drop(&mut self) {
unsafe { unsafe_bindings::idevice_disconnect(self.pointer) };
}
}
impl From<u32> for DeviceConnectionType {
fn from(value: u32) -> Self {
match value {
0 => DeviceConnectionType::Usbmuxd,
1 => DeviceConnectionType::Network,
_ => panic!("Unknown connection type"),
}
}
}