diff options
Diffstat (limited to 'tools/idevicebtlogger.c')
-rw-r--r-- | tools/idevicebtlogger.c | 360 |
1 files changed, 360 insertions, 0 deletions
diff --git a/tools/idevicebtlogger.c b/tools/idevicebtlogger.c new file mode 100644 index 0000000..fc42290 --- /dev/null +++ b/tools/idevicebtlogger.c | |||
@@ -0,0 +1,360 @@ | |||
1 | /* | ||
2 | * idevicebt_packet_logger.c | ||
3 | * Capture bt HCI packet log to pcap | ||
4 | * | ||
5 | * Copyright (c) 2021 Geoffrey Kruse, All Rights Reserved. | ||
6 | * | ||
7 | * This library is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * This library 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 GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this library; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | #ifdef HAVE_CONFIG_H | ||
23 | #include "config.h" | ||
24 | #endif | ||
25 | |||
26 | #define TOOL_NAME "idevicebtlogger" | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <string.h> | ||
30 | #include <errno.h> | ||
31 | #include <signal.h> | ||
32 | #include <stdlib.h> | ||
33 | #include <unistd.h> | ||
34 | #include <getopt.h> | ||
35 | |||
36 | #ifdef WIN32 | ||
37 | #include <windows.h> | ||
38 | #define sleep(x) Sleep(x*1000) | ||
39 | #else | ||
40 | #include <arpa/inet.h> | ||
41 | #endif | ||
42 | |||
43 | |||
44 | #include <libimobiledevice/libimobiledevice.h> | ||
45 | #include <libimobiledevice/bt_packet_logger.h> | ||
46 | #include <pcap.h>// todo windows??? | ||
47 | |||
48 | #define BT_MAX_PACKET_SIZE 65535 | ||
49 | |||
50 | static int quit_flag = 0; | ||
51 | static int exit_on_disconnect = 0; | ||
52 | |||
53 | static char* udid = NULL; | ||
54 | static idevice_t device = NULL; | ||
55 | static bt_packet_logger_client_t bt_packet_logger = NULL; | ||
56 | static int use_network = 0; | ||
57 | static char* out_filename = NULL; | ||
58 | static pcap_dumper_t * dump; | ||
59 | |||
60 | typedef struct { | ||
61 | uint32_t length; | ||
62 | uint32_t ts_secs; | ||
63 | uint32_t ts_usecs; | ||
64 | } PacketHeaderType; | ||
65 | |||
66 | typedef enum { | ||
67 | HCI_COMMAND = 0x00, | ||
68 | HCI_EVENT = 0x01, | ||
69 | SENT_ACL_DATA = 0x02, | ||
70 | RECV_ACL_DATA = 0x03 | ||
71 | } PacketLoggerPacketType; | ||
72 | |||
73 | static void bt_packet_logger_callback(uint8_t * data, uint16_t len, void *user_data) | ||
74 | { | ||
75 | PacketHeaderType * header = (PacketHeaderType *)data; | ||
76 | uint16_t offset = sizeof(PacketHeaderType); | ||
77 | |||
78 | struct pcap_pkthdr pcap_header; | ||
79 | pcap_header.caplen = ntohl(header->length); | ||
80 | pcap_header.len = len - sizeof(PacketHeaderType); | ||
81 | pcap_header.ts.tv_sec = ntohl(header->ts_secs); | ||
82 | pcap_header.ts.tv_usec = ntohl(header->ts_usecs); | ||
83 | |||
84 | // Sanity check incoming data and drop packet if its unreasonable. | ||
85 | if(pcap_header.len > BT_MAX_PACKET_SIZE || pcap_header.caplen > BT_MAX_PACKET_SIZE) { | ||
86 | fprintf(stderr, "WARNING: Packet length exceeded max size, corruption likely.\n "); | ||
87 | return; | ||
88 | } | ||
89 | |||
90 | uint8_t packet_type = data[offset]; | ||
91 | uint8_t hci_h4_type = 0xff; | ||
92 | |||
93 | switch(packet_type) { | ||
94 | case HCI_EVENT: | ||
95 | hci_h4_type = 0x04; | ||
96 | break; | ||
97 | |||
98 | case HCI_COMMAND: | ||
99 | hci_h4_type = 0x01; | ||
100 | break; | ||
101 | |||
102 | case SENT_ACL_DATA: | ||
103 | hci_h4_type = 0x02; | ||
104 | break; | ||
105 | |||
106 | case RECV_ACL_DATA: | ||
107 | hci_h4_type = 0x02; | ||
108 | break; | ||
109 | |||
110 | default: | ||
111 | // unknown packet logger type, just pass it on | ||
112 | hci_h4_type = packet_type; | ||
113 | break; | ||
114 | } | ||
115 | if(hci_h4_type != 0xff) { | ||
116 | data[offset] = hci_h4_type; | ||
117 | pcap_dump((unsigned char*)dump, &pcap_header, &data[offset]); | ||
118 | pcap_dump_flush(dump); | ||
119 | } | ||
120 | |||
121 | // for(; offset < len; offset++) { | ||
122 | // if( (offset - sizeof(PacketHeaderType)) % 16 == 0) { | ||
123 | // printf("\n"); | ||
124 | // } | ||
125 | // printf("0x%02x, ", 0xff&data[offset]); | ||
126 | // } | ||
127 | // printf("\n------------------------------------------------------------------------------------------------\n"); | ||
128 | } | ||
129 | |||
130 | static void stop_logging(void) | ||
131 | { | ||
132 | fflush(NULL); | ||
133 | |||
134 | if (bt_packet_logger) { | ||
135 | bt_packet_logger_client_free(bt_packet_logger); | ||
136 | bt_packet_logger = NULL; | ||
137 | } | ||
138 | |||
139 | if (device) { | ||
140 | idevice_free(device); | ||
141 | device = NULL; | ||
142 | } | ||
143 | } | ||
144 | |||
145 | static int start_logging(void) | ||
146 | { | ||
147 | idevice_error_t ret = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX); | ||
148 | if (ret != IDEVICE_E_SUCCESS) { | ||
149 | fprintf(stderr, "Device with udid %s not found!?\n", udid); | ||
150 | return -1; | ||
151 | } | ||
152 | |||
153 | lockdownd_client_t lockdown = NULL; | ||
154 | lockdownd_error_t lerr = lockdownd_client_new_with_handshake(device, &lockdown, TOOL_NAME); | ||
155 | if (lerr != LOCKDOWN_E_SUCCESS) { | ||
156 | fprintf(stderr, "ERROR: Could not connect to lockdownd: %d\n", lerr); | ||
157 | idevice_free(device); | ||
158 | device = NULL; | ||
159 | return -1; | ||
160 | } | ||
161 | |||
162 | /* start bt_packet_logger service */ | ||
163 | lockdownd_service_descriptor_t svc = NULL; | ||
164 | lerr = lockdownd_start_service(lockdown, BT_PACKETLOGGER_SERVICE_NAME, &svc); | ||
165 | if (lerr == LOCKDOWN_E_PASSWORD_PROTECTED) { | ||
166 | fprintf(stderr, "*** Device is passcode protected, enter passcode on the device to continue ***\n"); | ||
167 | while (!quit_flag) { | ||
168 | lerr = lockdownd_start_service(lockdown, BT_PACKETLOGGER_SERVICE_NAME, &svc); | ||
169 | if (lerr != LOCKDOWN_E_PASSWORD_PROTECTED) { | ||
170 | break; | ||
171 | } | ||
172 | sleep(1); | ||
173 | } | ||
174 | } | ||
175 | if (lerr != LOCKDOWN_E_SUCCESS) { | ||
176 | fprintf(stderr, "ERROR: Could not connect to lockdownd: %d\n", lerr); | ||
177 | fprintf(stderr, "Please ensure the target device has a valid Bluetooth logging profile installed\n"); | ||
178 | idevice_free(device); | ||
179 | device = NULL; | ||
180 | return -1; | ||
181 | } | ||
182 | lockdownd_client_free(lockdown); | ||
183 | |||
184 | /* connect to bt_packet_logger service */ | ||
185 | bt_packet_logger_error_t serr = BT_PACKET_LOGGER_E_UNKNOWN_ERROR; | ||
186 | serr = bt_packet_logger_client_new(device, svc, &bt_packet_logger); | ||
187 | lockdownd_service_descriptor_free(svc); | ||
188 | if (serr != BT_PACKET_LOGGER_E_SUCCESS) { | ||
189 | fprintf(stderr, "ERROR: Could not start service %s.\n", BT_PACKETLOGGER_SERVICE_NAME); | ||
190 | fprintf(stderr, "Please ensure the target device has a valid Bluetooth logging profile installed\n"); | ||
191 | idevice_free(device); | ||
192 | device = NULL; | ||
193 | return -1; | ||
194 | } | ||
195 | |||
196 | /* start capturing bt_packet_logger */ | ||
197 | serr = bt_packet_logger_start_capture(bt_packet_logger, bt_packet_logger_callback, NULL); | ||
198 | if (serr != BT_PACKET_LOGGER_E_SUCCESS) { | ||
199 | fprintf(stderr, "ERROR: Unable to start capturing bt_packet_logger.\n"); | ||
200 | bt_packet_logger_client_free(bt_packet_logger); | ||
201 | bt_packet_logger = NULL; | ||
202 | idevice_free(device); | ||
203 | device = NULL; | ||
204 | return -1; | ||
205 | } | ||
206 | |||
207 | fprintf(stdout, "[connected:%s]\n", udid); | ||
208 | fflush(stdout); | ||
209 | |||
210 | return 0; | ||
211 | } | ||
212 | |||
213 | static void device_event_cb(const idevice_event_t* event, void* userdata) | ||
214 | { | ||
215 | if (use_network && event->conn_type != CONNECTION_NETWORK) { | ||
216 | return; | ||
217 | } else if (!use_network && event->conn_type != CONNECTION_USBMUXD) { | ||
218 | return; | ||
219 | } | ||
220 | if (event->event == IDEVICE_DEVICE_ADD) { | ||
221 | if (!bt_packet_logger) { | ||
222 | if (!udid) { | ||
223 | udid = strdup(event->udid); | ||
224 | } | ||
225 | if (strcmp(udid, event->udid) == 0) { | ||
226 | if (start_logging() != 0) { | ||
227 | fprintf(stderr, "Could not start logger for udid %s\n", udid); | ||
228 | } | ||
229 | } | ||
230 | } | ||
231 | } else if (event->event == IDEVICE_DEVICE_REMOVE) { | ||
232 | if (bt_packet_logger && (strcmp(udid, event->udid) == 0)) { | ||
233 | stop_logging(); | ||
234 | fprintf(stdout, "[disconnected:%s]\n", udid); | ||
235 | if (exit_on_disconnect) { | ||
236 | quit_flag++; | ||
237 | } | ||
238 | } | ||
239 | } | ||
240 | } | ||
241 | |||
242 | /** | ||
243 | * signal handler function for cleaning up properly | ||
244 | */ | ||
245 | static void clean_exit(int sig) | ||
246 | { | ||
247 | fprintf(stderr, "\nExiting...\n"); | ||
248 | quit_flag++; | ||
249 | } | ||
250 | |||
251 | static void print_usage(int argc, char **argv, int is_error) | ||
252 | { | ||
253 | char *name = NULL; | ||
254 | name = strrchr(argv[0], '/'); | ||
255 | fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] <FILE>\n", (name ? name + 1: argv[0])); | ||
256 | fprintf(is_error ? stderr : stdout, | ||
257 | "\n" \ | ||
258 | "Capture HCI packets from a connected device.\n" \ | ||
259 | "\n" \ | ||
260 | "OPTIONS:\n" \ | ||
261 | " -u, --udid UDID target specific device by UDID\n" \ | ||
262 | " -n, --network connect to network device\n" \ | ||
263 | " -x, --exit exit when device disconnects\n" \ | ||
264 | " -h, --help prints usage information\n" \ | ||
265 | " -d, --debug enable communication debugging\n" \ | ||
266 | " -v, --version prints version information\n" \ | ||
267 | "\n" \ | ||
268 | "Homepage: <" PACKAGE_URL ">\n" | ||
269 | "Bug Reports: <" PACKAGE_BUGREPORT ">\n" | ||
270 | ); | ||
271 | } | ||
272 | |||
273 | int main(int argc, char *argv[]) | ||
274 | { | ||
275 | int c = 0; | ||
276 | const struct option longopts[] = { | ||
277 | { "debug", no_argument, NULL, 'd' }, | ||
278 | { "help", no_argument, NULL, 'h' }, | ||
279 | { "udid", required_argument, NULL, 'u' }, | ||
280 | { "network", no_argument, NULL, 'n' }, | ||
281 | { "exit", no_argument, NULL, 'x' }, | ||
282 | { "version", no_argument, NULL, 'v' }, | ||
283 | { NULL, 0, NULL, 0} | ||
284 | }; | ||
285 | |||
286 | signal(SIGINT, clean_exit); | ||
287 | signal(SIGTERM, clean_exit); | ||
288 | #ifndef WIN32 | ||
289 | signal(SIGQUIT, clean_exit); | ||
290 | signal(SIGPIPE, SIG_IGN); | ||
291 | #endif | ||
292 | |||
293 | while ((c = getopt_long(argc, argv, "dhu:nxv", longopts, NULL)) != -1) { | ||
294 | switch (c) { | ||
295 | case 'd': | ||
296 | idevice_set_debug_level(1); | ||
297 | break; | ||
298 | case 'u': | ||
299 | if (!*optarg) { | ||
300 | fprintf(stderr, "ERROR: UDID must not be empty!\n"); | ||
301 | print_usage(argc, argv, 1); | ||
302 | return 2; | ||
303 | } | ||
304 | free(udid); | ||
305 | udid = strdup(optarg); | ||
306 | break; | ||
307 | case 'n': | ||
308 | use_network = 1; | ||
309 | break; | ||
310 | case 'x': | ||
311 | exit_on_disconnect = 1; | ||
312 | break; | ||
313 | case 'h': | ||
314 | print_usage(argc, argv, 0); | ||
315 | return 0; | ||
316 | case 'v': | ||
317 | printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); | ||
318 | return 0; | ||
319 | default: | ||
320 | print_usage(argc, argv, 1); | ||
321 | return 2; | ||
322 | } | ||
323 | } | ||
324 | |||
325 | if (optind < argc) { | ||
326 | out_filename = argv[optind]; | ||
327 | printf("Output File: %s\n", out_filename); | ||
328 | } | ||
329 | else { | ||
330 | print_usage(argc, argv, 1); | ||
331 | return 2; | ||
332 | } | ||
333 | |||
334 | int num = 0; | ||
335 | idevice_info_t *devices = NULL; | ||
336 | idevice_get_device_list_extended(&devices, &num); | ||
337 | idevice_device_list_extended_free(devices); | ||
338 | if (num == 0) { | ||
339 | if (!udid) { | ||
340 | fprintf(stderr, "No device found. Plug in a device or pass UDID with -u to wait for device to be available.\n"); | ||
341 | return -1; | ||
342 | } else { | ||
343 | fprintf(stderr, "Waiting for device with UDID %s to become available...\n", udid); | ||
344 | } | ||
345 | } | ||
346 | |||
347 | dump = pcap_dump_open(pcap_open_dead(187, BT_MAX_PACKET_SIZE), out_filename); | ||
348 | idevice_event_subscribe(device_event_cb, NULL); | ||
349 | |||
350 | while (!quit_flag) { | ||
351 | sleep(1); | ||
352 | } | ||
353 | |||
354 | idevice_event_unsubscribe(); | ||
355 | stop_logging(); | ||
356 | |||
357 | free(udid); | ||
358 | |||
359 | return 0; | ||
360 | } | ||