From aee82afabfcf763618f58c95f9613b40dcf334b0 Mon Sep 17 00:00:00 2001 From: Geoffrey Kruse Date: Sun, 7 Mar 2021 10:03:55 -0800 Subject: idevicebtlogger: Cleanup warnings, todos and magic numbers --- include/libimobiledevice/bt_packet_logger.h | 20 ++++++----------- src/bt_packet_logger.c | 33 ++++++++++++----------------- src/bt_packet_logger.h | 4 ++-- 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" { #include #define BT_PACKETLOGGER_SERVICE_NAME "com.apple.bluetooth.BTPacketLogger" +#define BT_MAX_PACKET_SIZE 65535 /** Error Codes */ typedef enum { @@ -43,6 +44,12 @@ typedef enum { BT_PACKET_LOGGER_E_UNKNOWN_ERROR = -256 } bt_packet_logger_error_t; +typedef struct { + uint32_t length; + uint32_t ts_secs; + uint32_t ts_usecs; +} bt_packet_logger_header_t; + typedef struct bt_packet_logger_client_private bt_packet_logger_client_private; typedef bt_packet_logger_client_private *bt_packet_logger_client_t; /**< The client handle. */ @@ -141,19 +148,6 @@ bt_packet_logger_error_t bt_packet_logger_stop_capture(bt_packet_logger_client_t */ 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); -/** - * Receives data from the service. - * - * @param client The bt_packet_logger client - * @param data Buffer that will be filled with the data received - * @param size Number of bytes to receive - * @param received Number of bytes received (can be NULL to ignore) - * @param timeout Maximum time in milliseconds to wait for data. - * - * @return BT_PACKET_LOGGER_E_SUCCESS on success, - * BT_PACKET_LOGGER_E_INVALID_ARG when client or plist is NULL - */ -bt_packet_logger_error_t bt_packet_logger_receive(bt_packet_logger_client_t client, char *data, uint32_t size, uint32_t *received); #ifdef __cplusplus } 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 @@ /* * bt_packet_logger.c - * com.apple.bt_packet_logger service implementation. + * com.apple.bluetooth.BTPacketLogger service implementation. * * Copyright (c) 2021 Geoffrey Kruse, All Rights Reserved. * @@ -28,13 +28,16 @@ #include "bt_packet_logger.h" #include "lockdown.h" #include "common/debug.h" - struct bt_packet_logger_worker_thread { bt_packet_logger_client_t client; bt_packet_logger_receive_cb_t cbfunc; void *user_data; + uint8_t rxbuff[BT_MAX_PACKET_SIZE]; }; +#define SZ_READ_TIMEOUT 100 +#define PAYLOAD_READ_TIMEOUT 500 + /** * Convert a service_error_t value to a bt_packet_logger_error_t value. * 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) 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) { - *client = NULL; - if (!device || !service || service->port == 0 || !client || *client) { debug_info("Incorrect parameter passed to bt_packet_logger_client_new."); 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 return err; } -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) -{ - return bt_packet_logger_receive_with_timeout(client, data, size, received, 1000); -} - 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) { bt_packet_logger_error_t res = BT_PACKET_LOGGER_E_UNKNOWN_ERROR; @@ -141,8 +137,9 @@ void *bt_packet_logger_worker(void *arg) bt_packet_logger_error_t ret = BT_PACKET_LOGGER_E_UNKNOWN_ERROR; struct bt_packet_logger_worker_thread *btwt = (struct bt_packet_logger_worker_thread*)arg; - if (!btwt) + if (!btwt) { return NULL; + } debug_info("Running"); @@ -150,7 +147,7 @@ void *bt_packet_logger_worker(void *arg) uint32_t bytes = 0; uint16_t len; - ret = bt_packet_logger_receive_with_timeout(btwt->client, &len, 2, &bytes, 100); + ret = bt_packet_logger_receive_with_timeout(btwt->client, (char*)&len, 2, &bytes, SZ_READ_TIMEOUT); if (ret == BT_PACKET_LOGGER_E_TIMEOUT || ret == BT_PACKET_LOGGER_E_NOT_ENOUGH_DATA || ((bytes == 0) && (ret == BT_PACKET_LOGGER_E_SUCCESS))) { continue; @@ -159,11 +156,10 @@ void *bt_packet_logger_worker(void *arg) break; } - // todo remove magic and move "c" off stack - if(bytes > 0 && len > 12) { - char c[65535]; + // sanity check received length + if(bytes > 0 && len > sizeof(bt_packet_logger_header_t)) { debug_info("Reading %u bytes\n", len); - ret = bt_packet_logger_receive_with_timeout(btwt->client, c, len, &bytes, 500); + ret = bt_packet_logger_receive_with_timeout(btwt->client, (char *)btwt->rxbuff, len, &bytes, PAYLOAD_READ_TIMEOUT); if(len != bytes) { debug_info("Failed Read Expected %u, Received %u\n", len, bytes); @@ -177,13 +173,12 @@ void *bt_packet_logger_worker(void *arg) break; } - btwt->cbfunc(c, len, btwt->user_data); + btwt->cbfunc(btwt->rxbuff, len, btwt->user_data); } } - if (btwt) { - free(btwt); - } + // null check performed above + free(btwt); debug_info("Exiting"); 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 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef _SYSLOG_RELAY_H -#define _SYSLOG_RELAY_H +#ifndef _BR_PACKET_LOGGER_H +#define _BR_PACKET_LOGGER_H #include "libimobiledevice/bt_packet_logger.h" #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 @@ #include #include -#include // todo windows??? - -#define BT_MAX_PACKET_SIZE 65535 +#include static int quit_flag = 0; static int exit_on_disconnect = 0; @@ -57,12 +55,6 @@ static int use_network = 0; static char* out_filename = NULL; static pcap_dumper_t * dump; -typedef struct { - uint32_t length; - uint32_t ts_secs; - uint32_t ts_usecs; -} PacketHeaderType; - typedef enum { HCI_COMMAND = 0x00, HCI_EVENT = 0x01, @@ -72,12 +64,12 @@ typedef enum { static void bt_packet_logger_callback(uint8_t * data, uint16_t len, void *user_data) { - PacketHeaderType * header = (PacketHeaderType *)data; - uint16_t offset = sizeof(PacketHeaderType); + bt_packet_logger_header_t * header = (bt_packet_logger_header_t *)data; + uint16_t offset = sizeof(bt_packet_logger_header_t); struct pcap_pkthdr pcap_header; pcap_header.caplen = ntohl(header->length); - pcap_header.len = len - sizeof(PacketHeaderType); + pcap_header.len = len - sizeof(bt_packet_logger_header_t); pcap_header.ts.tv_sec = ntohl(header->ts_secs); pcap_header.ts.tv_usec = ntohl(header->ts_usecs); -- cgit v1.1-32-gdbae