diff options
Diffstat (limited to 'src')
| -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 |
3 files changed, 273 insertions, 1 deletions
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 | ||
