diff options
-rw-r--r-- | include/libimobiledevice/bt_packet_logger.h | 162 | ||||
-rw-r--r-- | src/Makefile.am | 3 | ||||
-rw-r--r-- | src/bt_packet_logger.c | 235 | ||||
-rw-r--r-- | src/bt_packet_logger.h | 36 | ||||
-rw-r--r-- | tools/Makefile.am | 6 | ||||
-rw-r--r-- | tools/idevicebtlogger.c | 360 |
6 files changed, 801 insertions, 1 deletions
diff --git a/include/libimobiledevice/bt_packet_logger.h b/include/libimobiledevice/bt_packet_logger.h new file mode 100644 index 0000000..697e879 --- /dev/null +++ b/include/libimobiledevice/bt_packet_logger.h | |||
@@ -0,0 +1,162 @@ | |||
1 | /** | ||
2 | * @file libimobiledevice/bt_packet_logger.h | ||
3 | * @brief Capture the Bluetooth HCI trace from a device | ||
4 | * \internal | ||
5 | * | ||
6 | * Copyright (c) 2021 Geoffrey Kruse, All Rights Reserved. | ||
7 | * | ||
8 | * This library is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU Lesser General Public | ||
10 | * License as published by the Free Software Foundation; either | ||
11 | * version 2.1 of the License, or (at your option) any later version. | ||
12 | * | ||
13 | * This library is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Lesser General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Lesser General Public | ||
19 | * License along with this library; if not, write to the Free Software | ||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | |||
23 | #ifndef IBT_PACKET_LOGGER_H | ||
24 | #define IBT_PACKET_LOGGER_H | ||
25 | |||
26 | #ifdef __cplusplus | ||
27 | extern "C" { | ||
28 | #endif | ||
29 | |||
30 | #include <libimobiledevice/libimobiledevice.h> | ||
31 | #include <libimobiledevice/lockdown.h> | ||
32 | |||
33 | #define BT_PACKETLOGGER_SERVICE_NAME "com.apple.bluetooth.BTPacketLogger" | ||
34 | |||
35 | /** Error Codes */ | ||
36 | typedef enum { | ||
37 | BT_PACKET_LOGGER_E_SUCCESS = 0, | ||
38 | BT_PACKET_LOGGER_E_INVALID_ARG = -1, | ||
39 | BT_PACKET_LOGGER_E_MUX_ERROR = -2, | ||
40 | BT_PACKET_LOGGER_E_SSL_ERROR = -3, | ||
41 | BT_PACKET_LOGGER_E_NOT_ENOUGH_DATA = -4, | ||
42 | BT_PACKET_LOGGER_E_TIMEOUT = -5, | ||
43 | BT_PACKET_LOGGER_E_UNKNOWN_ERROR = -256 | ||
44 | } bt_packet_logger_error_t; | ||
45 | |||
46 | typedef struct bt_packet_logger_client_private bt_packet_logger_client_private; | ||
47 | typedef bt_packet_logger_client_private *bt_packet_logger_client_t; /**< The client handle. */ | ||
48 | |||
49 | /** Receives each character received from the device. */ | ||
50 | typedef void (*bt_packet_logger_receive_cb_t)(uint8_t * data, uint16_t len, void *user_data); | ||
51 | |||
52 | /* Interface */ | ||
53 | |||
54 | /** | ||
55 | * Connects to the bt_packet_logger service on the specified device. | ||
56 | * | ||
57 | * @param device The device to connect to. | ||
58 | * @param service The service descriptor returned by lockdownd_start_service. | ||
59 | * @param client Pointer that will point to a newly allocated | ||
60 | * bt_packet_logger_client_t upon successful return. Must be freed using | ||
61 | * bt_packet_logger_client_free() after use. | ||
62 | * | ||
63 | * @return BT_PACKET_LOGGER_E_SUCCESS on success, BT_PACKET_LOGGER_E_INVALID_ARG when | ||
64 | * client is NULL, or an BT_PACKET_LOGGER_E_* error code otherwise. | ||
65 | */ | ||
66 | bt_packet_logger_error_t bt_packet_logger_client_new(idevice_t device, lockdownd_service_descriptor_t service, bt_packet_logger_client_t * client); | ||
67 | |||
68 | /** | ||
69 | * Starts a new bt_packet_logger service on the specified device and connects to it. | ||
70 | * | ||
71 | * @param device The device to connect to. | ||
72 | * @param client Pointer that will point to a newly allocated | ||
73 | * bt_packet_logger_client_t upon successful return. Must be freed using | ||
74 | * bt_packet_logger_client_free() after use. | ||
75 | * @param label The label to use for communication. Usually the program name. | ||
76 | * Pass NULL to disable sending the label in requests to lockdownd. | ||
77 | * | ||
78 | * @return BT_PACKET_LOGGER_E_SUCCESS on success, or an BT_PACKET_LOGGER_E_* error | ||
79 | * code otherwise. | ||
80 | */ | ||
81 | bt_packet_logger_error_t bt_packet_logger_client_start_service(idevice_t device, bt_packet_logger_client_t * client, const char* label); | ||
82 | |||
83 | /** | ||
84 | * Disconnects a bt_packet_logger client from the device and frees up the | ||
85 | * bt_packet_logger client data. | ||
86 | * | ||
87 | * @param client The bt_packet_logger client to disconnect and free. | ||
88 | * | ||
89 | * @return BT_PACKET_LOGGER_E_SUCCESS on success, BT_PACKET_LOGGER_E_INVALID_ARG when | ||
90 | * client is NULL, or an BT_PACKET_LOGGER_E_* error code otherwise. | ||
91 | */ | ||
92 | bt_packet_logger_error_t bt_packet_logger_client_free(bt_packet_logger_client_t client); | ||
93 | |||
94 | |||
95 | /** | ||
96 | * Starts capturing the syslog of the device using a callback. | ||
97 | * | ||
98 | * Use bt_packet_logger_stop_capture() to stop receiving the syslog. | ||
99 | * | ||
100 | * @param client The bt_packet_logger client to use | ||
101 | * @param callback Callback to receive each character from the syslog. | ||
102 | * @param user_data Custom pointer passed to the callback function. | ||
103 | * | ||
104 | * @return BT_PACKET_LOGGER_E_SUCCESS on success, | ||
105 | * BT_PACKET_LOGGER_E_INVALID_ARG when one or more parameters are | ||
106 | * invalid or BT_PACKET_LOGGER_E_UNKNOWN_ERROR when an unspecified | ||
107 | * error occurs or a syslog capture has already been started. | ||
108 | */ | ||
109 | bt_packet_logger_error_t bt_packet_logger_start_capture(bt_packet_logger_client_t client, bt_packet_logger_receive_cb_t callback, void* user_data); | ||
110 | |||
111 | /** | ||
112 | * Stops capturing the syslog of the device. | ||
113 | * | ||
114 | * Use bt_packet_logger_start_capture() to start receiving the syslog. | ||
115 | * | ||
116 | * @param client The bt_packet_logger client to use | ||
117 | * | ||
118 | * @return BT_PACKET_LOGGER_E_SUCCESS on success, | ||
119 | * BT_PACKET_LOGGER_E_INVALID_ARG when one or more parameters are | ||
120 | * invalid or BT_PACKET_LOGGER_E_UNKNOWN_ERROR when an unspecified | ||
121 | * error occurs or a syslog capture has already been started. | ||
122 | */ | ||
123 | bt_packet_logger_error_t bt_packet_logger_stop_capture(bt_packet_logger_client_t client); | ||
124 | |||
125 | /* Receiving */ | ||
126 | |||
127 | /** | ||
128 | * Receives data using the given bt_packet_logger client with specified timeout. | ||
129 | * | ||
130 | * @param client The bt_packet_logger client to use for receiving | ||
131 | * @param data Buffer that will be filled with the data received | ||
132 | * @param size Number of bytes to receive | ||
133 | * @param received Number of bytes received (can be NULL to ignore) | ||
134 | * @param timeout Maximum time in milliseconds to wait for data. | ||
135 | * | ||
136 | * @return BT_PACKET_LOGGER_E_SUCCESS on success, | ||
137 | * BT_PACKET_LOGGER_E_INVALID_ARG when one or more parameters are | ||
138 | * invalid, BT_PACKET_LOGGER_E_MUX_ERROR when a communication error | ||
139 | * occurs, or BT_PACKET_LOGGER_E_UNKNOWN_ERROR when an unspecified | ||
140 | * error occurs. | ||
141 | */ | ||
142 | bt_packet_logger_error_t bt_packet_logger_receive_with_timeout(bt_packet_logger_client_t client, char *data, uint32_t size, uint32_t *received, unsigned int timeout); | ||
143 | |||
144 | /** | ||
145 | * Receives data from the service. | ||
146 | * | ||
147 | * @param client The bt_packet_logger client | ||
148 | * @param data Buffer that will be filled with the data received | ||
149 | * @param size Number of bytes to receive | ||
150 | * @param received Number of bytes received (can be NULL to ignore) | ||
151 | * @param timeout Maximum time in milliseconds to wait for data. | ||
152 | * | ||
153 | * @return BT_PACKET_LOGGER_E_SUCCESS on success, | ||
154 | * BT_PACKET_LOGGER_E_INVALID_ARG when client or plist is NULL | ||
155 | */ | ||
156 | bt_packet_logger_error_t bt_packet_logger_receive(bt_packet_logger_client_t client, char *data, uint32_t size, uint32_t *received); | ||
157 | |||
158 | #ifdef __cplusplus | ||
159 | } | ||
160 | #endif | ||
161 | |||
162 | #endif | ||
diff --git a/src/Makefile.am b/src/Makefile.am index 13221b9..1c80ed6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
@@ -57,7 +57,8 @@ libimobiledevice_1_0_la_SOURCES = \ | |||
57 | preboard.c preboard.h \ | 57 | preboard.c preboard.h \ |
58 | companion_proxy.c companion_proxy.h \ | 58 | companion_proxy.c companion_proxy.h \ |
59 | reverse_proxy.c reverse_proxy.h \ | 59 | reverse_proxy.c reverse_proxy.h \ |
60 | syslog_relay.c syslog_relay.h | 60 | syslog_relay.c syslog_relay.h \ |
61 | bt_packet_logger.c bt_packet_logger.h | ||
61 | 62 | ||
62 | if WIN32 | 63 | if WIN32 |
63 | libimobiledevice_1_0_la_LDFLAGS += -avoid-version -static-libgcc | 64 | libimobiledevice_1_0_la_LDFLAGS += -avoid-version -static-libgcc |
diff --git a/src/bt_packet_logger.c b/src/bt_packet_logger.c new file mode 100644 index 0000000..5391825 --- /dev/null +++ b/src/bt_packet_logger.c | |||
@@ -0,0 +1,235 @@ | |||
1 | /* | ||
2 | * bt_packet_logger.c | ||
3 | * com.apple.bt_packet_logger service implementation. | ||
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 | #include <string.h> | ||
26 | #include <stdlib.h> | ||
27 | |||
28 | #include "bt_packet_logger.h" | ||
29 | #include "lockdown.h" | ||
30 | #include "common/debug.h" | ||
31 | |||
32 | struct bt_packet_logger_worker_thread { | ||
33 | bt_packet_logger_client_t client; | ||
34 | bt_packet_logger_receive_cb_t cbfunc; | ||
35 | void *user_data; | ||
36 | }; | ||
37 | |||
38 | /** | ||
39 | * Convert a service_error_t value to a bt_packet_logger_error_t value. | ||
40 | * Used internally to get correct error codes. | ||
41 | * | ||
42 | * @param err An service_error_t error code | ||
43 | * | ||
44 | * @return A matching bt_packet_logger_error_t error code, | ||
45 | * BT_PACKET_LOGGER_E_UNKNOWN_ERROR otherwise. | ||
46 | */ | ||
47 | static bt_packet_logger_error_t bt_packet_logger_error(service_error_t err) | ||
48 | { | ||
49 | switch (err) { | ||
50 | case SERVICE_E_SUCCESS: | ||
51 | return BT_PACKET_LOGGER_E_SUCCESS; | ||
52 | case SERVICE_E_INVALID_ARG: | ||
53 | return BT_PACKET_LOGGER_E_INVALID_ARG; | ||
54 | case SERVICE_E_MUX_ERROR: | ||
55 | return BT_PACKET_LOGGER_E_MUX_ERROR; | ||
56 | case SERVICE_E_SSL_ERROR: | ||
57 | return BT_PACKET_LOGGER_E_SSL_ERROR; | ||
58 | case SERVICE_E_NOT_ENOUGH_DATA: | ||
59 | return BT_PACKET_LOGGER_E_NOT_ENOUGH_DATA; | ||
60 | case SERVICE_E_TIMEOUT: | ||
61 | return BT_PACKET_LOGGER_E_TIMEOUT; | ||
62 | default: | ||
63 | break; | ||
64 | } | ||
65 | return BT_PACKET_LOGGER_E_UNKNOWN_ERROR; | ||
66 | } | ||
67 | |||
68 | LIBIMOBILEDEVICE_API bt_packet_logger_error_t bt_packet_logger_client_new(idevice_t device, lockdownd_service_descriptor_t service, bt_packet_logger_client_t * client) | ||
69 | { | ||
70 | *client = NULL; | ||
71 | |||
72 | if (!device || !service || service->port == 0 || !client || *client) { | ||
73 | debug_info("Incorrect parameter passed to bt_packet_logger_client_new."); | ||
74 | return BT_PACKET_LOGGER_E_INVALID_ARG; | ||
75 | } | ||
76 | |||
77 | debug_info("Creating bt_packet_logger_client, port = %d.", service->port); | ||
78 | |||
79 | service_client_t parent = NULL; | ||
80 | bt_packet_logger_error_t ret = bt_packet_logger_error(service_client_new(device, service, &parent)); | ||
81 | if (ret != BT_PACKET_LOGGER_E_SUCCESS) { | ||
82 | debug_info("Creating base service client failed. Error: %i", ret); | ||
83 | return ret; | ||
84 | } | ||
85 | |||
86 | bt_packet_logger_client_t client_loc = (bt_packet_logger_client_t) malloc(sizeof(struct bt_packet_logger_client_private)); | ||
87 | client_loc->parent = parent; | ||
88 | client_loc->worker = THREAD_T_NULL; | ||
89 | |||
90 | *client = client_loc; | ||
91 | |||
92 | debug_info("bt_packet_logger_client successfully created."); | ||
93 | return 0; | ||
94 | } | ||
95 | |||
96 | LIBIMOBILEDEVICE_API bt_packet_logger_error_t bt_packet_logger_client_start_service(idevice_t device, bt_packet_logger_client_t * client, const char* label) | ||
97 | { | ||
98 | bt_packet_logger_error_t err = BT_PACKET_LOGGER_E_UNKNOWN_ERROR; | ||
99 | service_client_factory_start_service(device, BT_PACKETLOGGER_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(bt_packet_logger_client_new), &err); | ||
100 | return err; | ||
101 | } | ||
102 | |||
103 | LIBIMOBILEDEVICE_API bt_packet_logger_error_t bt_packet_logger_client_free(bt_packet_logger_client_t client) | ||
104 | { | ||
105 | if (!client) | ||
106 | return BT_PACKET_LOGGER_E_INVALID_ARG; | ||
107 | bt_packet_logger_stop_capture(client); | ||
108 | bt_packet_logger_error_t err = bt_packet_logger_error(service_client_free(client->parent)); | ||
109 | free(client); | ||
110 | |||
111 | return err; | ||
112 | } | ||
113 | |||
114 | LIBIMOBILEDEVICE_API bt_packet_logger_error_t bt_packet_logger_receive(bt_packet_logger_client_t client, char* data, uint32_t size, uint32_t *received) | ||
115 | { | ||
116 | return bt_packet_logger_receive_with_timeout(client, data, size, received, 1000); | ||
117 | } | ||
118 | |||
119 | LIBIMOBILEDEVICE_API bt_packet_logger_error_t bt_packet_logger_receive_with_timeout(bt_packet_logger_client_t client, char* data, uint32_t size, uint32_t *received, unsigned int timeout) | ||
120 | { | ||
121 | bt_packet_logger_error_t res = BT_PACKET_LOGGER_E_UNKNOWN_ERROR; | ||
122 | int bytes = 0; | ||
123 | |||
124 | if (!client || !data || (size == 0)) { | ||
125 | return BT_PACKET_LOGGER_E_INVALID_ARG; | ||
126 | } | ||
127 | |||
128 | res = bt_packet_logger_error(service_receive_with_timeout(client->parent, data, size, (uint32_t*)&bytes, timeout)); | ||
129 | if (res != BT_PACKET_LOGGER_E_SUCCESS && res != BT_PACKET_LOGGER_E_TIMEOUT && res != BT_PACKET_LOGGER_E_NOT_ENOUGH_DATA) { | ||
130 | debug_info("Could not read data, error %d", res); | ||
131 | } | ||
132 | if (received) { | ||
133 | *received = (uint32_t)bytes; | ||
134 | } | ||
135 | |||
136 | return res; | ||
137 | } | ||
138 | |||
139 | void *bt_packet_logger_worker(void *arg) | ||
140 | { | ||
141 | bt_packet_logger_error_t ret = BT_PACKET_LOGGER_E_UNKNOWN_ERROR; | ||
142 | struct bt_packet_logger_worker_thread *btwt = (struct bt_packet_logger_worker_thread*)arg; | ||
143 | |||
144 | if (!btwt) | ||
145 | return NULL; | ||
146 | |||
147 | debug_info("Running"); | ||
148 | |||
149 | while (btwt->client->parent) { | ||
150 | uint32_t bytes = 0; | ||
151 | uint16_t len; | ||
152 | |||
153 | ret = bt_packet_logger_receive_with_timeout(btwt->client, &len, 2, &bytes, 100); | ||
154 | |||
155 | if (ret == BT_PACKET_LOGGER_E_TIMEOUT || ret == BT_PACKET_LOGGER_E_NOT_ENOUGH_DATA || ((bytes == 0) && (ret == BT_PACKET_LOGGER_E_SUCCESS))) { | ||
156 | continue; | ||
157 | } else if (ret < 0) { | ||
158 | debug_info("Connection to bt packet logger interrupted"); | ||
159 | break; | ||
160 | } | ||
161 | |||
162 | // todo remove magic and move "c" off stack | ||
163 | if(bytes > 0 && len > 12) { | ||
164 | char c[65535]; | ||
165 | debug_info("Reading %u bytes\n", len); | ||
166 | ret = bt_packet_logger_receive_with_timeout(btwt->client, c, len, &bytes, 500); | ||
167 | |||
168 | if(len != bytes) { | ||
169 | debug_info("Failed Read Expected %u, Received %u\n", len, bytes); | ||
170 | continue; | ||
171 | } | ||
172 | |||
173 | if (ret == BT_PACKET_LOGGER_E_TIMEOUT || ret == BT_PACKET_LOGGER_E_NOT_ENOUGH_DATA || ((bytes == 0) && (ret == BT_PACKET_LOGGER_E_SUCCESS))) { | ||
174 | continue; | ||
175 | } else if (ret < 0) { | ||
176 | debug_info("Connection to bt packet logger interrupted"); | ||
177 | break; | ||
178 | } | ||
179 | |||
180 | btwt->cbfunc(c, len, btwt->user_data); | ||
181 | } | ||
182 | } | ||
183 | |||
184 | if (btwt) { | ||
185 | free(btwt); | ||
186 | } | ||
187 | |||
188 | debug_info("Exiting"); | ||
189 | |||
190 | return NULL; | ||
191 | } | ||
192 | |||
193 | LIBIMOBILEDEVICE_API bt_packet_logger_error_t bt_packet_logger_start_capture(bt_packet_logger_client_t client, bt_packet_logger_receive_cb_t callback, void* user_data) | ||
194 | { | ||
195 | if (!client || !callback) | ||
196 | return BT_PACKET_LOGGER_E_INVALID_ARG; | ||
197 | |||
198 | bt_packet_logger_error_t res = BT_PACKET_LOGGER_E_UNKNOWN_ERROR; | ||
199 | |||
200 | if (client->worker) { | ||
201 | debug_info("Another syslog capture thread appears to be running already."); | ||
202 | return res; | ||
203 | } | ||
204 | |||
205 | /* start worker thread */ | ||
206 | struct bt_packet_logger_worker_thread *btwt = (struct bt_packet_logger_worker_thread*)malloc(sizeof(struct bt_packet_logger_worker_thread)); | ||
207 | if (btwt) { | ||
208 | btwt->client = client; | ||
209 | btwt->cbfunc = callback; | ||
210 | btwt->user_data = user_data; | ||
211 | |||
212 | if (thread_new(&client->worker, bt_packet_logger_worker, btwt) == 0) { | ||
213 | res = BT_PACKET_LOGGER_E_SUCCESS; | ||
214 | } | ||
215 | } | ||
216 | |||
217 | return res; | ||
218 | } | ||
219 | |||
220 | |||
221 | LIBIMOBILEDEVICE_API bt_packet_logger_error_t bt_packet_logger_stop_capture(bt_packet_logger_client_t client) | ||
222 | { | ||
223 | if (client->worker) { | ||
224 | /* notify thread to finish */ | ||
225 | service_client_t parent = client->parent; | ||
226 | client->parent = NULL; | ||
227 | /* join thread to make it exit */ | ||
228 | thread_join(client->worker); | ||
229 | thread_free(client->worker); | ||
230 | client->worker = THREAD_T_NULL; | ||
231 | client->parent = parent; | ||
232 | } | ||
233 | |||
234 | return BT_PACKET_LOGGER_E_SUCCESS; | ||
235 | } | ||
diff --git a/src/bt_packet_logger.h b/src/bt_packet_logger.h new file mode 100644 index 0000000..1ad906d --- /dev/null +++ b/src/bt_packet_logger.h | |||
@@ -0,0 +1,36 @@ | |||
1 | /* | ||
2 | * bt_packet_logger.h | ||
3 | * com.apple.bt_packet_logger service header file. | ||
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 | #ifndef _SYSLOG_RELAY_H | ||
23 | #define _SYSLOG_RELAY_H | ||
24 | |||
25 | #include "libimobiledevice/bt_packet_logger.h" | ||
26 | #include "service.h" | ||
27 | #include "common/thread.h" | ||
28 | |||
29 | struct bt_packet_logger_client_private { | ||
30 | service_client_t parent; | ||
31 | THREAD_T worker; | ||
32 | }; | ||
33 | |||
34 | void *bt_packet_logger_worker(void *arg); | ||
35 | |||
36 | #endif | ||
diff --git a/tools/Makefile.am b/tools/Makefile.am index 4740330..d701bab 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am | |||
@@ -11,6 +11,7 @@ AM_LDFLAGS = \ | |||
11 | $(libplist_LIBS) | 11 | $(libplist_LIBS) |
12 | 12 | ||
13 | bin_PROGRAMS = \ | 13 | bin_PROGRAMS = \ |
14 | idevicebtlogger\ | ||
14 | idevice_id \ | 15 | idevice_id \ |
15 | ideviceinfo \ | 16 | ideviceinfo \ |
16 | idevicename \ | 17 | idevicename \ |
@@ -30,6 +31,11 @@ bin_PROGRAMS = \ | |||
30 | idevicecrashreport \ | 31 | idevicecrashreport \ |
31 | idevicesetlocation | 32 | idevicesetlocation |
32 | 33 | ||
34 | idevicebtlogger_SOURCES = idevicebtlogger.c | ||
35 | iidevicebtlogger_CFLAGS = $(AM_CFLAGS) | ||
36 | idevicebtlogger_LDFLAGS = $(top_builddir)/common/libinternalcommon.la $(AM_LDFLAGS) -lpcap | ||
37 | idevicebtlogger_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la | ||
38 | |||
33 | ideviceinfo_SOURCES = ideviceinfo.c | 39 | ideviceinfo_SOURCES = ideviceinfo.c |
34 | ideviceinfo_CFLAGS = $(AM_CFLAGS) $(limd_glue_CFLAGS) | 40 | ideviceinfo_CFLAGS = $(AM_CFLAGS) $(limd_glue_CFLAGS) |
35 | ideviceinfo_LDFLAGS = $(AM_LDFLAGS) $(limd_glue_LIBS) | 41 | ideviceinfo_LDFLAGS = $(AM_LDFLAGS) $(limd_glue_LIBS) |
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 | } | ||