rusty_libimobiledevice 0.1.3

An ergonomic library to communicate with iOS devices
Documentation
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
// jkcoxson

use std::{convert::TryInto, os::raw::c_char};

use libc::c_int;
use log::info;

use crate::{bindings as unsafe_bindings, error::DebugServerError, idevice::Device};

pub struct DebugServer<'a> {
    pub(crate) pointer: unsafe_bindings::debugserver_client_t,
    pub(crate) phantom: std::marker::PhantomData<&'a Device>,
}

unsafe impl Send for DebugServer<'_> {}
unsafe impl Sync for DebugServer<'_> {}

/// A command that can be sent to the debug server service
pub struct DebugServerCommand {
    command: unsafe_bindings::debugserver_command_t,
}

unsafe impl Send for DebugServerCommand {}
unsafe impl Sync for DebugServerCommand {}

impl DebugServer<'_> {
    /// Starts a new debug server on the device
    /// # Arguments
    /// * `device` - The device to start the debug server on
    /// * `label` - The label to use for the debug server
    /// # Returns
    /// A debug server struct
    ///
    /// ***Verified:*** False
    pub fn new(device: &Device, label: &str) -> Result<Self, DebugServerError> {
        let mut client: unsafe_bindings::debugserver_client_t = unsafe { std::mem::zeroed() };
        let client_ptr: *mut unsafe_bindings::debugserver_client_t = &mut client;

        let label_c_str = std::ffi::CString::new(label).unwrap();
        info!("Creating debug server for {}", device.get_udid());
        let result = unsafe {
            unsafe_bindings::debugserver_client_start_service(
                device.pointer,
                client_ptr,
                label_c_str.as_ptr(),
            )
        }
        .into();
        if result != DebugServerError::Success {
            return Err(result);
        }

        Ok(DebugServer {
            pointer: unsafe { *client_ptr },
            phantom: std::marker::PhantomData,
        })
    }

    /// Sends a command to the debug server
    /// # Arguments
    /// * `data` - The command to send
    /// # Returns
    /// *none*
    ///
    /// ***Verified:*** False
    pub fn send(&self, data: impl Into<String>) -> Result<(), DebugServerError> {
        let data_c_str = std::ffi::CString::new(data.into()).unwrap();
        let mut sent = 0;
        let result = unsafe {
            unsafe_bindings::debugserver_client_send(
                self.pointer,
                data_c_str.as_ptr(),
                data_c_str.as_bytes().len().try_into().unwrap(),
                &mut sent,
            )
        }
        .into();
        if result != DebugServerError::Success {
            return Err(result);
        }

        Ok(())
    }

    /// Receives a command from the debug server with a timeout
    /// # Arguments
    /// * `size` - The number of bytes to receive
    /// * `timeout` - The timeout in milliseconds. If zero, this will be blocking
    /// # Returns
    /// The bytes received
    ///
    /// ***Verified:*** False
    pub fn recieve(&self, size: u32, timeout: u32) -> Result<Vec<u8>, DebugServerError> {
        let mut data = vec![0u8; size as usize];
        let mut received = 0;

        if timeout == 0 {
            let result = unsafe {
                unsafe_bindings::debugserver_client_receive(
                    self.pointer,
                    data.as_mut_ptr() as *mut c_char,
                    size,
                    &mut received,
                )
            }
            .into();
            if result != DebugServerError::Success {
                return Err(result);
            }
        } else {
            let result = unsafe {
                unsafe_bindings::debugserver_client_receive_with_timeout(
                    self.pointer,
                    data.as_mut_ptr() as *mut c_char,
                    size,
                    &mut received,
                    timeout,
                )
            }
            .into();
            if result != DebugServerError::Success {
                return Err(result);
            }
        }

        Ok(data[..received as usize].to_vec())
    }

    /// Receives a response from the debug server
    /// Blocks until a response is received
    /// # Arguments
    /// *none*
    /// # Returns
    /// The response
    ///
    /// ***Verified:*** False
    pub fn receive_response(&self) -> Result<String, DebugServerError> {
        let mut data = unsafe { std::mem::zeroed() };
        let mut size = 0;
        let result = unsafe {
            unsafe_bindings::debugserver_client_receive_response(self.pointer, &mut data, &mut size)
        }
        .into();
        if result != DebugServerError::Success {
            return Err(result);
        }
        let data = data as *mut u8;

        Ok(
            String::from_utf8(unsafe { std::slice::from_raw_parts(data, size as usize).to_vec() })
                .unwrap(),
        )
    }

    /// Sets the ack mode of the debug server
    /// # Arguments
    /// * `enabled` - Whether to enable ack mode
    /// # Returns
    /// *none*
    ///
    /// ***Verified:*** False
    pub fn set_ack_mode(&self, enabled: bool) -> Result<(), DebugServerError> {
        let result = unsafe {
            unsafe_bindings::debugserver_client_set_ack_mode(self.pointer, enabled as c_int)
        }
        .into();
        if result != DebugServerError::Success {
            return Err(result);
        }

        Ok(())
    }

    /// Sets the environment with a hex value
    /// # Arguments
    /// * `env` - The environment variable as 'KEY=VALUE'
    /// # Returns
    /// The response to the request
    ///
    /// ***Verified:*** False
    pub fn set_environment_hex_encoded(
        &self,
        env: impl Into<String>,
    ) -> Result<String, DebugServerError> {
        let env_c_str = std::ffi::CString::new(env.into()).unwrap();
        let mut response = unsafe { std::mem::zeroed() };
        let result = unsafe {
            unsafe_bindings::debugserver_client_set_environment_hex_encoded(
                self.pointer,
                env_c_str.as_ptr(),
                &mut response,
            )
        }
        .into();
        if result != DebugServerError::Success {
            return Err(result);
        }

        Ok(unsafe {
            std::ffi::CStr::from_ptr(response)
                .to_string_lossy()
                .into_owned()
        })
    }

    /// Sends a command to the debug server
    /// # Arguments
    /// * `command` - The command to send as a debug server command
    /// # Returns
    /// The response from the command, usually 'OK'
    ///
    /// ***Verified:*** False
    pub fn send_command(&self, command: DebugServerCommand) -> Result<String, DebugServerError> {
        let mut response: std::os::raw::c_char = unsafe { std::mem::zeroed() };
        let mut response_ptr: *mut std::os::raw::c_char = &mut response;
        let response_ptr_ptr: *mut *mut std::os::raw::c_char = &mut response_ptr;

        let response_size = std::ptr::null_mut();
        info!("Sending command to debug server");
        let result = unsafe {
            unsafe_bindings::debugserver_client_send_command(
                self.pointer,
                command.command,
                response_ptr_ptr,
                response_size,
            )
        }
        .into();
        if result != DebugServerError::Success {
            return Err(result);
        }

        if response == 0 {
            return Ok("".to_string());
        }
        // Convert response to String
        let response_str = unsafe {
            std::ffi::CStr::from_ptr(response_ptr)
                .to_string_lossy()
                .to_string()
        };

        Ok(response_str)
    }

    /// Sets the argument value for a command
    /// # Arguments
    /// * `args` - A list of arguments
    /// # Returns
    /// The response from the command, usually 'OK'
    ///
    /// ***Verified:*** False
    pub fn set_argv(&self, args: Vec<String>) -> Result<String, DebugServerError> {
        let mut argv: Vec<*mut std::os::raw::c_char> = Vec::new();
        let mut c_strings = vec![];
        for arg in args {
            let arg_c_str = std::ffi::CString::new(arg).unwrap().into_raw();
            argv.push(arg_c_str);
            c_strings.push(arg_c_str);
        }
        argv.push(std::ptr::null_mut());

        let mut response: std::os::raw::c_char = unsafe { std::mem::zeroed() };
        let mut response_ptr: *mut std::os::raw::c_char = &mut response;
        let response_ptr_ptr: *mut *mut std::os::raw::c_char = &mut response_ptr;

        info!("Setting argv for debug server");
        let result = unsafe {
            unsafe_bindings::debugserver_client_set_argv(
                self.pointer,
                argv.len() as i32,
                argv.as_mut_ptr(),
                response_ptr_ptr,
            )
        }
        .into();
        if result != DebugServerError::Success {
            return Err(result);
        }

        // Convert response to String
        let response_str = unsafe {
            std::ffi::CStr::from_ptr(response_ptr)
                .to_string_lossy()
                .to_string()
        };

        Ok(response_str)
    }

    /// Encodes a string into hex notation
    /// # Arguments
    /// * `buffer` - The string to encode
    /// # Returns
    /// The encoded bytes
    ///
    /// ***Verified:*** False
    pub fn encode_string(buffer: impl Into<String>) -> Vec<c_char> {
        let mut encoded_buffer = unsafe { std::mem::zeroed() };
        let mut encoded_buffer_size = 0;
        let buffer_c_str = std::ffi::CString::new(buffer.into()).unwrap();
        let buffer_ptr = buffer_c_str.as_ptr() as *mut c_char;
        unsafe {
            unsafe_bindings::debugserver_encode_string(
                buffer_ptr,
                &mut encoded_buffer,
                &mut encoded_buffer_size,
            );
        }
        unsafe {
            std::vec::Vec::from_raw_parts(
                encoded_buffer,
                encoded_buffer_size as usize,
                encoded_buffer_size as usize,
            )
        }
    }

    /// Decodes a string encoded in hex
    /// # Arguments
    /// * `buffer` - The string to decode
    pub fn decode_string(buffer: impl Into<String>) -> String {
        let buffer = buffer.into();
        let mut decoded_buffer = unsafe { std::mem::zeroed() };
        let buffer_len = buffer.len() as unsafe_bindings::size_t;
        let buffer_c_str = std::ffi::CString::new(buffer).unwrap();
        let buffer_ptr = buffer_c_str.as_ptr() as *mut c_char;
        unsafe {
            unsafe_bindings::debugserver_decode_string(buffer_ptr, buffer_len, &mut decoded_buffer);
        }
        let decoded_buffer_str = unsafe {
            std::ffi::CStr::from_ptr(decoded_buffer)
                .to_string_lossy()
                .to_string()
        };
        decoded_buffer_str
    }
}

impl DebugServerCommand {
    /// Assembles a new debug server command
    /// # Arguments
    /// * `command` - The command to run
    /// * `arguments` - A list of arguments for the command
    /// # Returns
    /// The struct containing the command
    ///
    /// ***Verified:*** False
    pub fn new(
        command: impl Into<String>,
        arguments: Vec<String>,
    ) -> Result<DebugServerCommand, String> {
        let mut command_ptr: unsafe_bindings::debugserver_command_t = unsafe { std::mem::zeroed() };
        let command_ptr_ptr: *mut unsafe_bindings::debugserver_command_t = &mut command_ptr;

        let command_c_str = std::ffi::CString::new(command.into()).unwrap();

        // Create C array
        let mut arguments_c_array: Vec<c_char> = Vec::new();
        for i in arguments.iter() {
            let c_str = std::ffi::CString::new(i.clone()).unwrap();
            arguments_c_array.push(c_str.as_bytes_with_nul()[0].try_into().unwrap());
        }
        // Create pointer to to_fill[0]
        let mut c_array_ptr: *mut std::os::raw::c_char = arguments_c_array.as_mut_ptr();
        let mut c_array_ptr_ptr: *mut *mut std::os::raw::c_char = &mut c_array_ptr;

        if arguments.is_empty() {
            c_array_ptr_ptr = std::ptr::null_mut();
        }

        info!("Creating debug server command");
        let result = unsafe {
            unsafe_bindings::debugserver_command_new(
                command_c_str.as_ptr(),
                arguments.len() as i32,
                c_array_ptr_ptr,
                command_ptr_ptr,
            )
        };
        if result < 0 {
            return Err(String::from("Failed to create command"));
        }

        Ok(DebugServerCommand {
            command: command_ptr,
        })
    }
}

impl From<String> for DebugServerCommand {
    fn from(s: String) -> Self {
        // Split string into command and arguments
        let mut split = s.split_whitespace();
        let command = split.next().unwrap().to_string();
        let arguments: Vec<String> = split.map(|s| s.to_string()).collect();
        DebugServerCommand::new(command, arguments).unwrap()
    }
}
impl From<&str> for DebugServerCommand {
    fn from(s: &str) -> DebugServerCommand {
        s.to_string().into()
    }
}