summaryrefslogtreecommitdiffstats
path: root/tools/idevicesetlocation.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/idevicesetlocation.c')
-rw-r--r--tools/idevicesetlocation.c182
1 files changed, 182 insertions, 0 deletions
diff --git a/tools/idevicesetlocation.c b/tools/idevicesetlocation.c
new file mode 100644
index 0000000..d2b8864
--- /dev/null
+++ b/tools/idevicesetlocation.c
@@ -0,0 +1,182 @@
1/*
2 * idevicesetlocation.c
3 * Simulate location on iOS device with mounted developer disk image
4 *
5 * Copyright (c) 2016-2020 Nikias Bassen, All Rights Reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24#include <stdio.h>
25#include <stdlib.h>
26#include <stdint.h>
27#include <string.h>
28#include <errno.h>
29#include <getopt.h>
30
31#include <libimobiledevice/libimobiledevice.h>
32#include <libimobiledevice/lockdown.h>
33#include <libimobiledevice/service.h>
34
35#include <endianness.h>
36
37#define DT_SIMULATELOCATION_SERVICE "com.apple.dt.simulatelocation"
38
39enum {
40 SET_LOCATION = 0,
41 RESET_LOCATION = 1
42};
43
44static void print_usage(int argc, char **argv, int is_error)
45{
46 char *bname = strrchr(argv[0], '/');
47 bname = (bname) ? bname + 1 : argv[0];
48
49 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] -- <LAT> <LONG>\n", bname);
50 fprintf(is_error ? stderr : stdout, " %s [OPTIONS] reset\n", bname);
51 fprintf(is_error ? stderr : stdout, "\n" \
52 "OPTIONS:\n" \
53 " -u, --udid UDID target specific device by UDID\n" \
54 " -n, --network connect to network device even if available via USB\n" \
55 " -h, --help prints usage information\n" \
56 " -d, --debug enable communication debugging\n" \
57 "\n"
58 "Homepage: <" PACKAGE_URL ">\n"
59 );
60}
61
62int main(int argc, char **argv)
63{
64 const struct option longopts[] = {
65 { "help", no_argument, NULL, 'h' },
66 { "udid", required_argument, NULL, 'u' },
67 { "debug", no_argument, NULL, 'd' },
68 { "network", no_argument, NULL, 'n' },
69 { NULL, 0, NULL, 0}
70 };
71 uint32_t mode = 0;
72 char *udid = NULL;
73 enum idevice_options lookup_opts = IDEVICE_LOOKUP_USBMUX | IDEVICE_LOOKUP_NETWORK;
74
75 int c = 0;
76 while ((c = getopt_long(argc, argv, "dhu:n", longopts, NULL)) != -1) {
77 switch (c) {
78 case 'd':
79 idevice_set_debug_level(1);
80 break;
81 case 'u':
82 if (!*optarg) {
83 fprintf(stderr, "ERROR: UDID must not be empty!\n");
84 print_usage(argc, argv, 1);
85 return 2;
86 }
87 free(udid);
88 udid = strdup(optarg);
89 break;
90 case 'n':
91 lookup_opts |= IDEVICE_LOOKUP_PREFER_NETWORK;
92 break;
93 case 'h':
94 print_usage(argc, argv, 0);
95 return 0;
96 default:
97 print_usage(argc, argv, 1);
98 return 2;
99 }
100 }
101
102 argc -= optind;
103 argv += optind;
104
105 if ((argc > 2) || (argc < 1)) {
106 print_usage(argc+optind, argv-optind, 1);
107 return -1;
108 }
109
110 if (argc == 2) {
111 mode = SET_LOCATION;
112 } else if (argc == 1) {
113 if (strcmp(argv[0], "reset") == 0) {
114 mode = RESET_LOCATION;
115 } else {
116 print_usage(argc+optind, argv-optind, 1);
117 return -1;
118 }
119 }
120
121 idevice_t device = NULL;
122
123 if (idevice_new_with_options(&device, udid, lookup_opts) != IDEVICE_E_SUCCESS) {
124 if (udid) {
125 printf("ERROR: Device %s not found!\n", udid);
126 } else {
127 printf("ERROR: No device found!\n");
128 }
129 return -1;
130 }
131
132 lockdownd_client_t lockdown;
133 lockdownd_client_new_with_handshake(device, &lockdown, NULL);
134
135 lockdownd_service_descriptor_t svc = NULL;
136 if (lockdownd_start_service(lockdown, DT_SIMULATELOCATION_SERVICE, &svc) != LOCKDOWN_E_SUCCESS) {
137 lockdownd_client_free(lockdown);
138 idevice_free(device);
139 printf("ERROR: Could not start the simulatelocation service. Make sure a developer disk image is mounted!\n");
140 return -1;
141 }
142 lockdownd_client_free(lockdown);
143
144 service_client_t service = NULL;
145
146 service_error_t serr = service_client_new(device, svc, &service);
147
148 lockdownd_service_descriptor_free(svc);
149
150 if (serr != SERVICE_E_SUCCESS) {
151 lockdownd_client_free(lockdown);
152 idevice_free(device);
153 printf("ERROR: Could not connect to simulatelocation service (%d)\n", serr);
154 return -1;
155 }
156
157 uint32_t l;
158 uint32_t s = 0;
159
160 l = htobe32(mode);
161 service_send(service, (const char*)&l, 4, &s);
162 if (mode == SET_LOCATION) {
163 int len = 4 + strlen(argv[0]) + 4 + strlen(argv[1]);
164 char *buf = malloc(len);
165 uint32_t latlen;
166 latlen = strlen(argv[0]);
167 l = htobe32(latlen);
168 memcpy(buf, &l, 4);
169 memcpy(buf+4, argv[0], latlen);
170 uint32_t longlen = strlen(argv[1]);
171 l = htobe32(longlen);
172 memcpy(buf+4+latlen, &l, 4);
173 memcpy(buf+4+latlen+4, argv[1], longlen);
174
175 s = 0;
176 service_send(service, buf, len, &s);
177 }
178
179 idevice_free(device);
180
181 return 0;
182}