summaryrefslogtreecommitdiffstats
path: root/include/libimobiledevice/debugserver.h
blob: 809b97f7f0e8372939ea15734cd23b666bc29337 (plain)
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
/**
 * @file libimobiledevice/debugserver.h
 * @brief Communicate with debugserver on the device.
 * \internal
 *
 * Copyright (c) 2014 Martin Szulecki All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef IDEBUGSERVER_H
#define IDEBUGSERVER_H

#ifdef __cplusplus
extern "C" {
#endif

#include <libimobiledevice/libimobiledevice.h>
#include <libimobiledevice/lockdown.h>

/** Service identifier passed to lockdownd_start_service() to start the debugserver service */
#define DEBUGSERVER_SERVICE_NAME "com.apple.debugserver"
/** Service identifier passed to lockdownd_start_service() to start the secure debugserver service */
#define DEBUGSERVER_SECURE_SERVICE_NAME DEBUGSERVER_SERVICE_NAME ".DVTSecureSocketProxy"

/** Error Codes */
typedef enum {
	DEBUGSERVER_E_SUCCESS        =  0,
	DEBUGSERVER_E_INVALID_ARG    = -1,
	DEBUGSERVER_E_MUX_ERROR      = -2,
	DEBUGSERVER_E_SSL_ERROR      = -3,
	DEBUGSERVER_E_RESPONSE_ERROR = -4,
	DEBUGSERVER_E_TIMEOUT        = -5,
	DEBUGSERVER_E_UNKNOWN_ERROR  = -256
} debugserver_error_t;

typedef struct debugserver_client_private debugserver_client_private; /**< \private */
typedef debugserver_client_private *debugserver_client_t; /**< The client handle. */

typedef struct debugserver_command_private debugserver_command_private; /**< \private */
typedef debugserver_command_private *debugserver_command_t; /**< The command handle. */

/* Interface */

/**
 * Connects to the debugserver service on the specified device.
 *
 * @param device The device to connect to.
 * @param service The service descriptor returned by lockdownd_start_service.
 * @param client Pointer that will point to a newly allocated
 *     debugserver_client_t upon successful return. Must be freed using
 *     debugserver_client_free() after use.
 *
 * @return DEBUGSERVER_E_SUCCESS on success, DEBUGSERVER_E_INVALID_ARG when
 *     client is NULL, or an DEBUGSERVER_E_* error code otherwise.
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_new(idevice_t device, lockdownd_service_descriptor_t service, debugserver_client_t * client);

/**
 * Starts a new debugserver service on the specified device and connects to it.
 *
 * @param device The device to connect to.
 * @param client Pointer that will point to a newly allocated
 *     debugserver_client_t upon successful return. Must be freed using
 *     debugserver_client_free() after use.
 * @param label The label to use for communication. Usually the program name.
 *  Pass NULL to disable sending the label in requests to lockdownd.
 *
 * @return DEBUGSERVER_E_SUCCESS on success, or an DEBUGSERVER_E_* error
 *     code otherwise.
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_start_service(idevice_t device, debugserver_client_t * client, const char* label);

/**
 * Disconnects a debugserver client from the device and frees up the
 * debugserver client data.
 *
 * @param client The debugserver client to disconnect and free.
 *
 * @return DEBUGSERVER_E_SUCCESS on success, DEBUGSERVER_E_INVALID_ARG when
 *     client is NULL, or an DEBUGSERVER_E_* error code otherwise.
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_free(debugserver_client_t client);

/**
 * Sends raw data using the given debugserver service client.
 *
 * @param client The debugserver client to use for sending
 * @param data Data to send
 * @param size Size of the data to send
 * @param sent Number of bytes sent (can be NULL to ignore)
 *
 * @return DEBUGSERVER_E_SUCCESS on success,
 *      DEBUGSERVER_E_INVALID_ARG when one or more parameters are
 *      invalid, or DEBUGSERVER_E_UNKNOWN_ERROR when an unspecified
 *      error occurs.
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_send(debugserver_client_t client, const char* data, uint32_t size, uint32_t *sent);

/**
 * Receives raw data using the given debugserver client with specified timeout.
 *
 * @param client The debugserver client to use for receiving
 * @param data Buffer that will be filled with the data received
 * @param size Number of bytes to receive
 * @param received Number of bytes received (can be NULL to ignore)
 * @param timeout Maximum time in milliseconds to wait for data.
 *
 * @return DEBUGSERVER_E_SUCCESS on success,
 *      DEBUGSERVER_E_INVALID_ARG when one or more parameters are
 *      invalid, DEBUGSERVER_E_MUX_ERROR when a communication error
 *      occurs, DEBUGSERVER_E_TIMEOUT when the timeout is reached,
 *      or DEBUGSERVER_E_UNKNOWN_ERROR when an unspecified
 *      error occurs.
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_receive_with_timeout(debugserver_client_t client, char *data, uint32_t size, uint32_t *received, unsigned int timeout);

/**
 * Receives raw data from the debugserver service.
 *
 * @param client The debugserver client
 * @param data Buffer that will be filled with the data received
 * @param size Number of bytes to receive
 * @param received Number of bytes received (can be NULL to ignore)
 * @note The default read timeout is 10 seconds.
 *
 * @return DEBUGSERVER_E_SUCCESS on success,
 *  DEBUGSERVER_E_INVALID_ARG when client or plist is NULL
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_receive(debugserver_client_t client, char *data, uint32_t size, uint32_t *received);

/**
 * Sends a command to the debugserver service.
 *
 * @param client The debugserver client
 * @param command Command to process and send
 * @param response Response received for the command (can be NULL to ignore)
 * @param response_size Pointer to receive response size. Set to NULL to ignore.
 *
 * @return DEBUGSERVER_E_SUCCESS on success,
 *  DEBUGSERVER_E_INVALID_ARG when client or command is NULL
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_send_command(debugserver_client_t client, debugserver_command_t command, char** response, size_t* response_size);

/**
 * Receives and parses response of debugserver service.
 *
 * @param client The debugserver client
 * @param response Response received for last command (can be NULL to ignore)
 * @param response_size Pointer to receive response size. Set to NULL to ignore.
 *
 * @return DEBUGSERVER_E_SUCCESS on success,
 *  DEBUGSERVER_E_INVALID_ARG when client is NULL
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_receive_response(debugserver_client_t client, char** response, size_t* response_size);

/**
 * Controls status of ACK mode when sending commands or receiving responses.
 *
 * @see debugserver_client_send_command, debugserver_client_receive_response
 *
 * @param client The debugserver client
 * @param enabled A boolean flag indicating whether the internal ACK mode
 *   handling should be enabled or disabled.
 *
 * @return DEBUGSERVER_E_SUCCESS on success, or an DEBUGSERVER_E_* error
 *     code otherwise.
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_set_ack_mode(debugserver_client_t client, int enabled);

/**
 * Sets behavior when awaiting a response from the server.
 *
 * @see debugserver_client_send_command, debugserver_client_receive_response,
 *   debugserver_client_receive
 *
 * @param client The debugserver client
 * @param cancel_receive A function pointer that will be called approximately
 *     every receive_loop_timeout milliseconds; the function should return a
 *     boolean flag specifying whether to stop waiting for a response. If NULL,
 *     behaves as if it always returns true.
 * @param receive_loop_timeout Time in milliseconds between calls to
 *     cancel_receive.
 *
 * @return DEBUGSERVER_E_SUCCESS on success, or an DEBUGSERVER_E_* error
 *     code otherwise.
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_set_receive_params(debugserver_client_t client, int (*cancel_receive)(), int receive_loop_timeout);

/**
 * Sets the argv which launches an app.
 *
 * @param client The debugserver client
 * @param argc Number of arguments
 * @param argv Array starting with the executable to be run followed by it's arguments
 * @param response Response received for the command (can be NULL to ignore)
 *
 * @return DEBUGSERVER_E_SUCCESS on success,
 *  DEBUGSERVER_E_INVALID_ARG when client is NULL
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_set_argv(debugserver_client_t client, int argc, char* argv[], char** response);

/**
 * Adds or sets an environment variable.
 *
 * @param client The debugserver client
 * @param env The environment variable in "KEY=VALUE" notation
 * @param response Response received for the command (can be NULL to ignore)
 *
 * @return DEBUGSERVER_E_SUCCESS on success,
 *  DEBUGSERVER_E_INVALID_ARG when client is NULL
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_set_environment_hex_encoded(debugserver_client_t client, const char* env, char** response);

/**
 * Creates and initializes a new command object.
 *
 * @param name The name of the command which is sent in plain text
 * @param argv Array of tokens for the command ment to be encoded
 * @param argc Number of items in the token array
 * @param command New command object
 *
 * @return DEBUGSERVER_E_SUCCESS on success,
 *  DEBUGSERVER_E_INVALID_ARG when name or command is NULL
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_command_new(const char* name, int argc, char* argv[], debugserver_command_t* command);

/**
 * Frees memory of command object.
 *
 * @param command The command object
 *
 * @return DEBUGSERVER_E_SUCCESS on success,
 *  DEBUGSERVER_E_INVALID_ARG when command is NULL
 */
LIBIMOBILEDEVICE_API debugserver_error_t debugserver_command_free(debugserver_command_t command);

/**
 * Encodes a string into hex notation.
 *
 * @param buffer String to encode into hex notiation
 * @param encoded_buffer The buffer receives a hex encoded string
 * @param encoded_length Length of the hex encoded string
 */
LIBIMOBILEDEVICE_API void debugserver_encode_string(const char* buffer, char** encoded_buffer, uint32_t* encoded_length);

/**
 * Decodes a hex encoded string.
 *
 * @param encoded_buffer The buffer with a hex encoded string
 * @param encoded_length Length of the encoded buffer
 * @param buffer Decoded string to be freed by the caller
 */
LIBIMOBILEDEVICE_API void debugserver_decode_string(const char *encoded_buffer, size_t encoded_length, char** buffer);

#ifdef __cplusplus
}
#endif

#endif