forked from imkira/mobiledevice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobiledevice.c
345 lines (295 loc) · 8.16 KB
/
mobiledevice.c
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
#include "mobiledevice.h";
#include <string.h>
#include <stdlib.h>
enum MobileDeviceCommandType
{
GetUDID,
InstallApp,
UninstallApp,
ListInstalledApps,
Tunnel
};
struct
{
struct am_device_notification *notification;
enum MobileDeviceCommandType type;
const char *app_path;
const char *bundle_id;
uint16_t src_port;
uint16_t dst_port;
} command;
void create_tunnel(struct am_device *device, uint16_t src_port, uint16_t dst_port);
#define ASSERT_OR_EXIT(_cnd_, ...) \
do \
{ \
if (!(_cnd_)) \
{ \
fprintf(stderr, __VA_ARGS__); \
unregister_device_notification(1); \
} \
} while (0)
char *create_cstr_from_cfstring(CFStringRef cfstring)
{
CFIndex str_len = CFStringGetLength(cfstring);
if (str_len < 0)
{
return NULL;
}
CFIndex buf_len = CFStringGetMaximumSizeForEncoding(str_len, kCFStringEncodingUTF8) + 1;
if (buf_len < 1)
{
return NULL;
}
char *cstr = (char *)malloc(buf_len);
if (cstr == NULL)
{
return NULL;
}
if (CFStringGetCString(cfstring, cstr, buf_len, kCFStringEncodingUTF8))
{
return cstr;
}
return NULL;
}
void unregister_device_notification(int status)
{
AMDeviceNotificationUnsubscribe(command.notification);
exit(status);
}
void connect_to_device(struct am_device *device)
{
AMDeviceConnect(device);
ASSERT_OR_EXIT(AMDeviceIsPaired(device), "!AMDeviceIsPaired\n");
ASSERT_OR_EXIT(!AMDeviceValidatePairing(device), "!AMDeviceValidatePairing\n");
ASSERT_OR_EXIT(!AMDeviceStartSession(device), "!AMDeviceStartSession\n");
}
CFURLRef get_absolute_file_url(const char *file_path)
{
CFStringRef path = CFStringCreateWithCString(NULL, file_path, kCFStringEncodingUTF8);
CFURLRef relative_url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, false);
CFURLRef url = CFURLCopyAbsoluteURL(relative_url);
CFRelease(path);
CFRelease(relative_url);
return url;
}
CFStringRef get_bundle_id(const char *app_path)
{
CFURLRef app_url = get_absolute_file_url(app_path);
if (app_url == NULL)
{
return NULL;
}
CFURLRef url = CFURLCreateCopyAppendingPathComponent(NULL, app_url,
CFSTR("Info.plist"), false);
CFRelease(app_url);
if (url == NULL)
{
return NULL;
}
CFReadStreamRef stream = CFReadStreamCreateWithFile(NULL, url);
CFRelease(url);
if (stream == NULL)
{
return NULL;
}
CFPropertyListRef plist = NULL;
if (CFReadStreamOpen(stream) == TRUE)
{
plist = CFPropertyListCreateWithStream(NULL, stream, 0,
kCFPropertyListImmutable, NULL, NULL);
}
CFReadStreamClose(stream);
CFRelease(stream);
if (plist == NULL)
{
return NULL;
}
const void *value = CFDictionaryGetValue(plist, CFSTR("CFBundleIdentifier"));
CFStringRef bundle_id = NULL;
if (value != NULL)
{
bundle_id = CFRetain(value);
}
CFRelease(plist);
return bundle_id;
}
void get_udid(struct am_device *device)
{
char *udid = create_cstr_from_cfstring(AMDeviceCopyDeviceIdentifier(device));
if (udid == NULL)
{
unregister_device_notification(1);
}
// print UDID to stdout
printf("%s\n", udid);
free(udid);
unregister_device_notification(0);
}
void install_app(struct am_device *device)
{
connect_to_device(device);
CFURLRef local_app_url = get_absolute_file_url(command.app_path);
CFStringRef keys[] = { CFSTR("PackageType") }, values[] = { CFSTR("Developer") };
CFDictionaryRef options = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, 1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
// copy .app to device
ASSERT_OR_EXIT(!AMDeviceSecureTransferPath(0, device, local_app_url, options,
NULL, 0), "!AMDeviceSecureTransferPath\n");
// install package on device
ASSERT_OR_EXIT(!AMDeviceSecureInstallApplication(0, device, local_app_url,
options, NULL, 0), "!AMDeviceSecureInstallApplication\n");
CFRelease(options);
CFRelease(local_app_url);
printf("OK\n");
unregister_device_notification(0);
}
void uninstall_app(struct am_device *device)
{
connect_to_device(device);
CFStringRef bundle_id = CFStringCreateWithCString(NULL, command.bundle_id, kCFStringEncodingUTF8);
// install package on device
ASSERT_OR_EXIT(!AMDeviceSecureUninstallApplication(0, device, bundle_id,
0, NULL, 0), "!AMDeviceSecureUninstallApplication\n");
CFRelease(bundle_id);
printf("OK\n");
unregister_device_notification(0);
}
static void print_bundle_id(const void *key, const void *value, void *context)
{
if ((key == NULL) || (value == NULL))
{
return;
}
char *bundle_id = create_cstr_from_cfstring((CFStringRef)key);
if (bundle_id != NULL)
{
printf("%s\n", bundle_id);
free(bundle_id);
}
}
void list_installed_apps(struct am_device *device)
{
connect_to_device(device);
CFDictionaryRef apps;
ASSERT_OR_EXIT(!AMDeviceLookupApplications(device, 0, &apps),
"!AMDeviceLookupApplications\n");
CFDictionaryApplyFunction(apps, print_bundle_id, NULL);
CFRelease(apps);
unregister_device_notification(0);
}
void on_device_connected(struct am_device *device)
{
if (command.type == GetUDID)
{
get_udid(device);
}
else if (command.type == InstallApp)
{
install_app(device);
}
else if (command.type == UninstallApp)
{
uninstall_app(device);
}
else if (command.type == ListInstalledApps)
{
list_installed_apps(device);
}
else if (command.type == Tunnel)
{
connect_to_device(device);
create_tunnel(device, command.src_port, command.dst_port);
}
}
void on_device_notification(struct am_device_notification_callback_info *info, int cookie)
{
switch (info->msg)
{
case ADNCI_MSG_CONNECTED:
on_device_connected(info->dev);
break;
default:
break;
}
}
void register_device_notification()
{
AMDeviceNotificationSubscribe(&on_device_notification, 0, 0, 0, &command.notification);
CFRunLoopRun();
}
void print_syntax()
{
printf("Usage: mobiledevice <command>\n\n");
printf("<Commands>\n");
printf(" get_udid : Display UDID of connected device\n");
printf(" get_bundle_id <path_to_app> : Display bundle identifier of app (.app folder)\n");
printf(" install_app <path_to_app> : Install app (.app folder) to device\n");
printf(" uninstall <bundle_id> : Uninstall app by bundle id\n");
printf(" list_installed_apps : Lists all installed apps on device\n");
printf(" tunnel <from_port> <to_port> : Forward TCP connections to connected device\n");
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
print_syntax();
exit(2);
}
// get_udid
if ((argc == 2) && (strcmp(argv[1], "get_udid") == 0))
{
command.type = GetUDID;
}
// get_bundle_id
else if ((argc == 3) && (strcmp(argv[1], "get_bundle_id") == 0))
{
CFStringRef bundle_id = get_bundle_id(argv[2]);
if (bundle_id == NULL)
{
exit(1);
}
char *_bundle_id = create_cstr_from_cfstring(bundle_id);
CFRelease(bundle_id);
if (_bundle_id == NULL)
{
exit(1);
}
printf("%s\n", _bundle_id);
free(_bundle_id);
exit(0);
}
// install_app
else if ((argc == 3) && (strcmp(argv[1], "install_app") == 0))
{
command.type = InstallApp;
command.app_path = argv[2];
}
// uninstall_app
else if ((argc == 3) && (strcmp(argv[1], "uninstall_app") == 0))
{
command.type = UninstallApp;
command.bundle_id = argv[2];
}
// list_installed_apps
else if (strcmp(argv[1], "list_installed_apps") == 0)
{
command.type = ListInstalledApps;
}
// tunnel
else if ((argc == 4) && (strcmp(argv[1], "tunnel") == 0))
{
command.type = Tunnel;
command.src_port = (uint16_t)atoi(argv[2]);
command.dst_port = (uint16_t)atoi(argv[3]);
}
else
{
fprintf(stderr, "Unknown command\n");
print_syntax();
exit(1);
}
// wait for device
register_device_notification();
return 1;
}