diff options
-rw-r--r-- | include/libimobiledevice/bt_packet_logger.h | 20 | ||||
-rw-r--r-- | src/bt_packet_logger.c | 33 | ||||
-rw-r--r-- | src/bt_packet_logger.h | 4 | ||||
-rw-r--r-- | tools/idevicebtlogger.c | 16 |
4 files changed, 27 insertions, 46 deletions
diff --git a/include/libimobiledevice/bt_packet_logger.h b/include/libimobiledevice/bt_packet_logger.h index 697e879..8916219 100644 --- a/include/libimobiledevice/bt_packet_logger.h +++ b/include/libimobiledevice/bt_packet_logger.h | |||
@@ -31,6 +31,7 @@ extern "C" { | |||
31 | #include <libimobiledevice/lockdown.h> | 31 | #include <libimobiledevice/lockdown.h> |
32 | 32 | ||
33 | #define BT_PACKETLOGGER_SERVICE_NAME "com.apple.bluetooth.BTPacketLogger" | 33 | #define BT_PACKETLOGGER_SERVICE_NAME "com.apple.bluetooth.BTPacketLogger" |
34 | #define BT_MAX_PACKET_SIZE 65535 | ||
34 | 35 | ||
35 | /** Error Codes */ | 36 | /** Error Codes */ |
36 | typedef enum { | 37 | typedef enum { |
@@ -43,6 +44,12 @@ typedef enum { | |||
43 | BT_PACKET_LOGGER_E_UNKNOWN_ERROR = -256 | 44 | BT_PACKET_LOGGER_E_UNKNOWN_ERROR = -256 |
44 | } bt_packet_logger_error_t; | 45 | } bt_packet_logger_error_t; |
45 | 46 | ||
47 | typedef struct { | ||
48 | uint32_t length; | ||
49 | uint32_t ts_secs; | ||
50 | uint32_t ts_usecs; | ||
51 | } bt_packet_logger_header_t; | ||
52 | |||
46 | typedef struct bt_packet_logger_client_private bt_packet_logger_client_private; | 53 | 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. */ | 54 | typedef bt_packet_logger_client_private *bt_packet_logger_client_t; /**< The client handle. */ |
48 | 55 | ||
@@ -141,19 +148,6 @@ bt_packet_logger_error_t bt_packet_logger_stop_capture(bt_packet_logger_client_t | |||
141 | */ | 148 | */ |
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); | 149 | 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 | 150 | ||
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 | 151 | ||
158 | #ifdef __cplusplus | 152 | #ifdef __cplusplus |
159 | } | 153 | } |
diff --git a/src/bt_packet_logger.c b/src/bt_packet_logger.c index 5391825..196039e 100644 --- a/src/bt_packet_logger.c +++ b/src/bt_packet_logger.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | * bt_packet_logger.c | 2 | * bt_packet_logger.c |
3 | * com.apple.bt_packet_logger service implementation. | 3 | * com.apple.bluetooth.BTPacketLogger service implementation. |
4 | * | 4 | * |
5 | * Copyright (c) 2021 Geoffrey Kruse, All Rights Reserved. | 5 | * Copyright (c) 2021 Geoffrey Kruse, All Rights Reserved. |
6 | * | 6 | * |
@@ -28,13 +28,16 @@ | |||
28 | #include "bt_packet_logger.h" | 28 | #include "bt_packet_logger.h" |
29 | #include "lockdown.h" | 29 | #include "lockdown.h" |
30 | #include "common/debug.h" | 30 | #include "common/debug.h" |
31 | |||
32 | struct bt_packet_logger_worker_thread { | 31 | struct bt_packet_logger_worker_thread { |
33 | bt_packet_logger_client_t client; | 32 | bt_packet_logger_client_t client; |
34 | bt_packet_logger_receive_cb_t cbfunc; | 33 | bt_packet_logger_receive_cb_t cbfunc; |
35 | void *user_data; | 34 | void *user_data; |
35 | uint8_t rxbuff[BT_MAX_PACKET_SIZE]; | ||
36 | }; | 36 | }; |
37 | 37 | ||
38 | #define SZ_READ_TIMEOUT 100 | ||
39 | #define PAYLOAD_READ_TIMEOUT 500 | ||
40 | |||
38 | /** | 41 | /** |
39 | * Convert a service_error_t value to a bt_packet_logger_error_t value. | 42 | * Convert a service_error_t value to a bt_packet_logger_error_t value. |
40 | * Used internally to get correct error codes. | 43 | * Used internally to get correct error codes. |
@@ -67,8 +70,6 @@ static bt_packet_logger_error_t bt_packet_logger_error(service_error_t err) | |||
67 | 70 | ||
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) | 71 | 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 | { | 72 | { |
70 | *client = NULL; | ||
71 | |||
72 | if (!device || !service || service->port == 0 || !client || *client) { | 73 | if (!device || !service || service->port == 0 || !client || *client) { |
73 | debug_info("Incorrect parameter passed to bt_packet_logger_client_new."); | 74 | debug_info("Incorrect parameter passed to bt_packet_logger_client_new."); |
74 | return BT_PACKET_LOGGER_E_INVALID_ARG; | 75 | return BT_PACKET_LOGGER_E_INVALID_ARG; |
@@ -111,11 +112,6 @@ LIBIMOBILEDEVICE_API bt_packet_logger_error_t bt_packet_logger_client_free(bt_pa | |||
111 | return err; | 112 | return err; |
112 | } | 113 | } |
113 | 114 | ||
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) | 115 | 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 | { | 116 | { |
121 | bt_packet_logger_error_t res = BT_PACKET_LOGGER_E_UNKNOWN_ERROR; | 117 | bt_packet_logger_error_t res = BT_PACKET_LOGGER_E_UNKNOWN_ERROR; |
@@ -141,8 +137,9 @@ void *bt_packet_logger_worker(void *arg) | |||
141 | bt_packet_logger_error_t ret = BT_PACKET_LOGGER_E_UNKNOWN_ERROR; | 137 | 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; | 138 | struct bt_packet_logger_worker_thread *btwt = (struct bt_packet_logger_worker_thread*)arg; |
143 | 139 | ||
144 | if (!btwt) | 140 | if (!btwt) { |
145 | return NULL; | 141 | return NULL; |
142 | } | ||
146 | 143 | ||
147 | debug_info("Running"); | 144 | debug_info("Running"); |
148 | 145 | ||
@@ -150,7 +147,7 @@ void *bt_packet_logger_worker(void *arg) | |||
150 | uint32_t bytes = 0; | 147 | uint32_t bytes = 0; |
151 | uint16_t len; | 148 | uint16_t len; |
152 | 149 | ||
153 | ret = bt_packet_logger_receive_with_timeout(btwt->client, &len, 2, &bytes, 100); | 150 | ret = bt_packet_logger_receive_with_timeout(btwt->client, (char*)&len, 2, &bytes, SZ_READ_TIMEOUT); |
154 | 151 | ||
155 | if (ret == BT_PACKET_LOGGER_E_TIMEOUT || ret == BT_PACKET_LOGGER_E_NOT_ENOUGH_DATA || ((bytes == 0) && (ret == BT_PACKET_LOGGER_E_SUCCESS))) { | 152 | 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; | 153 | continue; |
@@ -159,11 +156,10 @@ void *bt_packet_logger_worker(void *arg) | |||
159 | break; | 156 | break; |
160 | } | 157 | } |
161 | 158 | ||
162 | // todo remove magic and move "c" off stack | 159 | // sanity check received length |
163 | if(bytes > 0 && len > 12) { | 160 | if(bytes > 0 && len > sizeof(bt_packet_logger_header_t)) { |
164 | char c[65535]; | ||
165 | debug_info("Reading %u bytes\n", len); | 161 | debug_info("Reading %u bytes\n", len); |
166 | ret = bt_packet_logger_receive_with_timeout(btwt->client, c, len, &bytes, 500); | 162 | ret = bt_packet_logger_receive_with_timeout(btwt->client, (char *)btwt->rxbuff, len, &bytes, PAYLOAD_READ_TIMEOUT); |
167 | 163 | ||
168 | if(len != bytes) { | 164 | if(len != bytes) { |
169 | debug_info("Failed Read Expected %u, Received %u\n", len, bytes); | 165 | debug_info("Failed Read Expected %u, Received %u\n", len, bytes); |
@@ -177,13 +173,12 @@ void *bt_packet_logger_worker(void *arg) | |||
177 | break; | 173 | break; |
178 | } | 174 | } |
179 | 175 | ||
180 | btwt->cbfunc(c, len, btwt->user_data); | 176 | btwt->cbfunc(btwt->rxbuff, len, btwt->user_data); |
181 | } | 177 | } |
182 | } | 178 | } |
183 | 179 | ||
184 | if (btwt) { | 180 | // null check performed above |
185 | free(btwt); | 181 | free(btwt); |
186 | } | ||
187 | 182 | ||
188 | debug_info("Exiting"); | 183 | debug_info("Exiting"); |
189 | 184 | ||
diff --git a/src/bt_packet_logger.h b/src/bt_packet_logger.h index 1ad906d..f9e0c3e 100644 --- a/src/bt_packet_logger.h +++ b/src/bt_packet_logger.h | |||
@@ -19,8 +19,8 @@ | |||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | */ | 20 | */ |
21 | 21 | ||
22 | #ifndef _SYSLOG_RELAY_H | 22 | #ifndef _BR_PACKET_LOGGER_H |
23 | #define _SYSLOG_RELAY_H | 23 | #define _BR_PACKET_LOGGER_H |
24 | 24 | ||
25 | #include "libimobiledevice/bt_packet_logger.h" | 25 | #include "libimobiledevice/bt_packet_logger.h" |
26 | #include "service.h" | 26 | #include "service.h" |
diff --git a/tools/idevicebtlogger.c b/tools/idevicebtlogger.c index fc42290..5446ecf 100644 --- a/tools/idevicebtlogger.c +++ b/tools/idevicebtlogger.c | |||
@@ -43,9 +43,7 @@ | |||
43 | 43 | ||
44 | #include <libimobiledevice/libimobiledevice.h> | 44 | #include <libimobiledevice/libimobiledevice.h> |
45 | #include <libimobiledevice/bt_packet_logger.h> | 45 | #include <libimobiledevice/bt_packet_logger.h> |
46 | #include <pcap.h>// todo windows??? | 46 | #include <pcap.h> |
47 | |||
48 | #define BT_MAX_PACKET_SIZE 65535 | ||
49 | 47 | ||
50 | static int quit_flag = 0; | 48 | static int quit_flag = 0; |
51 | static int exit_on_disconnect = 0; | 49 | static int exit_on_disconnect = 0; |
@@ -57,12 +55,6 @@ static int use_network = 0; | |||
57 | static char* out_filename = NULL; | 55 | static char* out_filename = NULL; |
58 | static pcap_dumper_t * dump; | 56 | static pcap_dumper_t * dump; |
59 | 57 | ||
60 | typedef struct { | ||
61 | uint32_t length; | ||
62 | uint32_t ts_secs; | ||
63 | uint32_t ts_usecs; | ||
64 | } PacketHeaderType; | ||
65 | |||
66 | typedef enum { | 58 | typedef enum { |
67 | HCI_COMMAND = 0x00, | 59 | HCI_COMMAND = 0x00, |
68 | HCI_EVENT = 0x01, | 60 | HCI_EVENT = 0x01, |
@@ -72,12 +64,12 @@ typedef enum { | |||
72 | 64 | ||
73 | static void bt_packet_logger_callback(uint8_t * data, uint16_t len, void *user_data) | 65 | static void bt_packet_logger_callback(uint8_t * data, uint16_t len, void *user_data) |
74 | { | 66 | { |
75 | PacketHeaderType * header = (PacketHeaderType *)data; | 67 | bt_packet_logger_header_t * header = (bt_packet_logger_header_t *)data; |
76 | uint16_t offset = sizeof(PacketHeaderType); | 68 | uint16_t offset = sizeof(bt_packet_logger_header_t); |
77 | 69 | ||
78 | struct pcap_pkthdr pcap_header; | 70 | struct pcap_pkthdr pcap_header; |
79 | pcap_header.caplen = ntohl(header->length); | 71 | pcap_header.caplen = ntohl(header->length); |
80 | pcap_header.len = len - sizeof(PacketHeaderType); | 72 | pcap_header.len = len - sizeof(bt_packet_logger_header_t); |
81 | pcap_header.ts.tv_sec = ntohl(header->ts_secs); | 73 | pcap_header.ts.tv_sec = ntohl(header->ts_secs); |
82 | pcap_header.ts.tv_usec = ntohl(header->ts_usecs); | 74 | pcap_header.ts.tv_usec = ntohl(header->ts_usecs); |
83 | 75 | ||