| 1 |
/* |
| 2 |
* idevicedebugserverproxy.c |
| 3 |
* Proxy a debugserver connection from device for remote debugging |
| 4 |
* |
| 5 |
* Copyright (c) 2021 Nikias Bassen, All Rights Reserved. |
| 6 |
* Copyright (c) 2012 Martin Szulecki 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 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#define TOOL_NAME "idevicedebugserverproxy" |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
#include <getopt.h> |
| 33 |
#include <errno.h> |
| 34 |
#include <signal.h> |
| 35 |
#ifdef _WIN32 |
| 36 |
#include <winsock2.h> |
| 37 |
#include <windows.h> |
| 38 |
#else |
| 39 |
#include <sys/select.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#include <libimobiledevice/libimobiledevice.h> |
| 43 |
#include <libimobiledevice/debugserver.h> |
| 44 |
|
| 45 |
#include <libimobiledevice-glue/socket.h> |
| 46 |
#include <libimobiledevice-glue/thread.h> |
| 47 |
|
| 48 |
#ifndef ETIMEDOUT |
| 49 |
#define ETIMEDOUT 138 |
| 50 |
#endif |
| 51 |
|
| 52 |
#define info(...) fprintf(stdout, __VA_ARGS__); fflush(stdout) |
| 53 |
#define debug(...) if(debug_mode) fprintf(stdout, __VA_ARGS__) |
| 54 |
|
| 55 |
static int support_lldb = 0; |
| 56 |
static int debug_mode = 0; |
| 57 |
static int quit_flag = 0; |
| 58 |
static uint16_t local_port = 0; |
| 59 |
|
| 60 |
typedef struct { |
| 61 |
int client_fd; |
| 62 |
idevice_t device; |
| 63 |
debugserver_client_t debugserver_client; |
| 64 |
} socket_info_t; |
| 65 |
|
| 66 |
struct thread_info { |
| 67 |
THREAD_T th; |
| 68 |
int client_fd; |
| 69 |
struct thread_info *next; |
| 70 |
}; |
| 71 |
|
| 72 |
typedef struct thread_info thread_info_t; |
| 73 |
|
| 74 |
|
| 75 |
static void clean_exit(int sig) |
| 76 |
{ |
| 77 |
fprintf(stderr, "Exiting...\n"); |
| 78 |
quit_flag++; |
| 79 |
} |
| 80 |
|
| 81 |
static void print_usage(int argc, char **argv, int is_error) |
| 82 |
{ |
| 83 |
char *name = strrchr(argv[0], '/'); |
| 84 |
fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] [PORT]\n", (name ? name + 1: argv[0])); |
| 85 |
fprintf(is_error ? stderr : stdout, |
| 86 |
"\n" |
| 87 |
"Proxy debugserver connection from device to a local socket at PORT.\n" |
| 88 |
"If PORT is omitted, the next available port will be used and printed\n" |
| 89 |
"to stdout.\n" |
| 90 |
"\n" |
| 91 |
"OPTIONS:\n" |
| 92 |
" -u, --udid UDID target specific device by UDID\n" |
| 93 |
" -n, --network connect to network device\n" |
| 94 |
" -d, --debug enable communication debugging\n" |
| 95 |
" -l, --lldb enable lldb support\n" |
| 96 |
" -h, --help prints usage information\n" |
| 97 |
" -v, --version prints version information\n" |
| 98 |
"\n" |
| 99 |
"Homepage: <" PACKAGE_URL ">\n" |
| 100 |
"Bug Reports: <" PACKAGE_BUGREPORT ">\n" |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
static int intercept_packet(char *packet, ssize_t *packet_len) { |
| 105 |
static const char kReqLaunchServer[] = "$qLaunchGDBServer;#4b"; |
| 106 |
|
| 107 |
char buffer[64] = {0}; |
| 108 |
if (*packet_len == (ssize_t)(sizeof(kReqLaunchServer) - 1) |
| 109 |
&& memcmp(packet, kReqLaunchServer, sizeof(kReqLaunchServer) - 1) == 0) { |
| 110 |
sprintf(buffer, "port:%d;", local_port); |
| 111 |
} else { |
| 112 |
return 0; |
| 113 |
} |
| 114 |
int sum = 0; |
| 115 |
for (size_t i = 0; i < strlen(buffer); i++) { |
| 116 |
sum += buffer[i]; |
| 117 |
} |
| 118 |
sum = sum & 255; |
| 119 |
sprintf(packet, "$%s#%02x", buffer, sum); |
| 120 |
*packet_len = strlen(packet); |
| 121 |
return 1; |
| 122 |
} |
| 123 |
|
| 124 |
static void* connection_handler(void* data) |
| 125 |
{ |
| 126 |
debugserver_error_t derr = DEBUGSERVER_E_SUCCESS; |
| 127 |
socket_info_t* socket_info = (socket_info_t*)data; |
| 128 |
const int bufsize = 65536; |
| 129 |
char* buf; |
| 130 |
|
| 131 |
int client_fd = socket_info->client_fd; |
| 132 |
|
| 133 |
debug("%s: client_fd = %d\n", __func__, client_fd); |
| 134 |
|
| 135 |
derr = debugserver_client_start_service(socket_info->device, &socket_info->debugserver_client, TOOL_NAME); |
| 136 |
if (derr != DEBUGSERVER_E_SUCCESS) { |
| 137 |
fprintf(stderr, "Could not start debugserver on device!\nPlease make sure to mount a developer disk image first.\n"); |
| 138 |
return NULL; |
| 139 |
} |
| 140 |
|
| 141 |
buf = malloc(bufsize); |
| 142 |
if (!buf) { |
| 143 |
fprintf(stderr, "Failed to allocate buffer\n"); |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
|
| 147 |
fd_set fds; |
| 148 |
FD_ZERO(&fds); |
| 149 |
FD_SET(client_fd, &fds); |
| 150 |
|
| 151 |
int dtimeout = 1; |
| 152 |
|
| 153 |
while (!quit_flag) { |
| 154 |
ssize_t n = socket_receive_timeout(client_fd, buf, bufsize, 0, 1); |
| 155 |
if (n != -ETIMEDOUT) { |
| 156 |
if (n < 0) { |
| 157 |
fprintf(stderr, "Failed to read from client fd: %s\n", strerror(-n)); |
| 158 |
break; |
| 159 |
} else if (n == 0) { |
| 160 |
fprintf(stderr, "connection closed\n"); |
| 161 |
break; |
| 162 |
} |
| 163 |
if (support_lldb && intercept_packet(buf, &n)) { |
| 164 |
socket_send(client_fd, buf, n); |
| 165 |
continue; |
| 166 |
} |
| 167 |
uint32_t sent = 0; |
| 168 |
debugserver_client_send(socket_info->debugserver_client, buf, n, &sent); |
| 169 |
} |
| 170 |
do { |
| 171 |
uint32_t r = 0; |
| 172 |
derr = debugserver_client_receive_with_timeout(socket_info->debugserver_client, buf, bufsize, &r, dtimeout); |
| 173 |
if (r > 0) { |
| 174 |
socket_send(client_fd, buf, r); |
| 175 |
dtimeout = 1; |
| 176 |
} else if (derr == DEBUGSERVER_E_TIMEOUT) { |
| 177 |
dtimeout = 5; |
| 178 |
break; |
| 179 |
} else { |
| 180 |
fprintf(stderr, "debugserver connection closed\n"); |
| 181 |
break; |
| 182 |
} |
| 183 |
} while (derr == DEBUGSERVER_E_SUCCESS); |
| 184 |
if (derr != DEBUGSERVER_E_TIMEOUT && derr != DEBUGSERVER_E_SUCCESS) { |
| 185 |
break; |
| 186 |
} |
| 187 |
} |
| 188 |
free(buf); |
| 189 |
|
| 190 |
debug("%s: shutting down...\n", __func__); |
| 191 |
|
| 192 |
debugserver_client_free(socket_info->debugserver_client); |
| 193 |
socket_info->debugserver_client = NULL; |
| 194 |
|
| 195 |
/* shutdown client socket */ |
| 196 |
socket_shutdown(socket_info->client_fd, SHUT_RDWR); |
| 197 |
socket_close(socket_info->client_fd); |
| 198 |
|
| 199 |
return NULL; |
| 200 |
} |
| 201 |
|
| 202 |
int main(int argc, char *argv[]) |
| 203 |
{ |
| 204 |
idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; |
| 205 |
idevice_t device = NULL; |
| 206 |
thread_info_t *thread_list = NULL; |
| 207 |
const char* udid = NULL; |
| 208 |
int use_network = 0; |
| 209 |
int server_fd; |
| 210 |
int result = EXIT_SUCCESS; |
| 211 |
int c = 0; |
| 212 |
const struct option longopts[] = { |
| 213 |
{ "debug", no_argument, NULL, 'd' }, |
| 214 |
{ "help", no_argument, NULL, 'h' }, |
| 215 |
{ "udid", required_argument, NULL, 'u' }, |
| 216 |
{ "network", no_argument, NULL, 'n' }, |
| 217 |
{ "lldb", no_argument, NULL, 'l' }, |
| 218 |
{ "version", no_argument, NULL, 'v' }, |
| 219 |
{ NULL, 0, NULL, 0} |
| 220 |
}; |
| 221 |
|
| 222 |
#ifndef _WIN32 |
| 223 |
struct sigaction sa; |
| 224 |
struct sigaction si; |
| 225 |
memset(&sa, '\0', sizeof(struct sigaction)); |
| 226 |
memset(&si, '\0', sizeof(struct sigaction)); |
| 227 |
|
| 228 |
sa.sa_handler = clean_exit; |
| 229 |
sigemptyset(&sa.sa_mask); |
| 230 |
|
| 231 |
si.sa_handler = SIG_IGN; |
| 232 |
sigemptyset(&si.sa_mask); |
| 233 |
|
| 234 |
sigaction(SIGINT, &sa, NULL); |
| 235 |
sigaction(SIGTERM, &sa, NULL); |
| 236 |
sigaction(SIGQUIT, &sa, NULL); |
| 237 |
sigaction(SIGPIPE, &si, NULL); |
| 238 |
#else |
| 239 |
/* bind signals */ |
| 240 |
signal(SIGINT, clean_exit); |
| 241 |
signal(SIGTERM, clean_exit); |
| 242 |
#endif |
| 243 |
|
| 244 |
/* parse cmdline arguments */ |
| 245 |
while ((c = getopt_long(argc, argv, "dhu:nv", longopts, NULL)) != -1) { |
| 246 |
switch (c) { |
| 247 |
case 'd': |
| 248 |
debug_mode = 1; |
| 249 |
idevice_set_debug_level(1); |
| 250 |
socket_set_verbose(3); |
| 251 |
break; |
| 252 |
case 'u': |
| 253 |
if (!*optarg) { |
| 254 |
fprintf(stderr, "ERROR: UDID argument must not be empty!\n"); |
| 255 |
print_usage(argc, argv, 1); |
| 256 |
return 2; |
| 257 |
} |
| 258 |
udid = optarg; |
| 259 |
break; |
| 260 |
case 'n': |
| 261 |
use_network = 1; |
| 262 |
break; |
| 263 |
case 'l': |
| 264 |
support_lldb = 1; |
| 265 |
break; |
| 266 |
case 'h': |
| 267 |
print_usage(argc, argv, 0); |
| 268 |
return 0; |
| 269 |
case 'v': |
| 270 |
printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); |
| 271 |
return 0; |
| 272 |
default: |
| 273 |
print_usage(argc, argv, 1); |
| 274 |
return 2; |
| 275 |
} |
| 276 |
} |
| 277 |
argc -= optind; |
| 278 |
argv += optind; |
| 279 |
|
| 280 |
if (argv[0] && (atoi(argv[0]) > 0)) { |
| 281 |
local_port = atoi(argv[0]); |
| 282 |
} |
| 283 |
|
| 284 |
/* start services and connect to device */ |
| 285 |
ret = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX); |
| 286 |
if (ret != IDEVICE_E_SUCCESS) { |
| 287 |
if (udid) { |
| 288 |
fprintf(stderr, "No device found with udid %s.\n", udid); |
| 289 |
} else { |
| 290 |
fprintf(stderr, "No device found.\n"); |
| 291 |
} |
| 292 |
result = EXIT_FAILURE; |
| 293 |
goto leave_cleanup; |
| 294 |
} |
| 295 |
|
| 296 |
/* create local socket */ |
| 297 |
server_fd = socket_create("127.0.0.1", local_port); |
| 298 |
if (server_fd < 0) { |
| 299 |
fprintf(stderr, "Could not create socket\n"); |
| 300 |
result = EXIT_FAILURE; |
| 301 |
goto leave_cleanup; |
| 302 |
} |
| 303 |
|
| 304 |
if (local_port == 0) { |
| 305 |
/* The user asked for any available port. Report the actual port. */ |
| 306 |
uint16_t port; |
| 307 |
if (0 > socket_get_socket_port(server_fd, &port)) { |
| 308 |
fprintf(stderr, "Could not determine socket port\n"); |
| 309 |
result = EXIT_FAILURE; |
| 310 |
goto leave_cleanup; |
| 311 |
} |
| 312 |
printf("Listening on port %d\n", port); |
| 313 |
} |
| 314 |
|
| 315 |
while (!quit_flag) { |
| 316 |
debug("%s: Waiting for connection on local port %d\n", __func__, local_port); |
| 317 |
|
| 318 |
/* wait for client */ |
| 319 |
int client_fd = socket_accept(server_fd, local_port); |
| 320 |
if (client_fd < 0) { |
| 321 |
continue; |
| 322 |
} |
| 323 |
|
| 324 |
debug("%s: Handling new client connection...\n", __func__); |
| 325 |
|
| 326 |
thread_info_t *el = (thread_info_t*)malloc(sizeof(thread_info_t)); |
| 327 |
if (!el) { |
| 328 |
fprintf(stderr, "Out of memory\n"); |
| 329 |
exit(EXIT_FAILURE); |
| 330 |
} |
| 331 |
el->client_fd = client_fd; |
| 332 |
el->next = NULL; |
| 333 |
|
| 334 |
if (thread_list) { |
| 335 |
thread_list->next = el; |
| 336 |
} else { |
| 337 |
thread_list = el; |
| 338 |
} |
| 339 |
|
| 340 |
socket_info_t *sinfo = (socket_info_t*)malloc(sizeof(socket_info_t)); |
| 341 |
if (!sinfo) { |
| 342 |
fprintf(stderr, "Out of memory\n"); |
| 343 |
exit(EXIT_FAILURE); |
| 344 |
} |
| 345 |
sinfo->client_fd = client_fd; |
| 346 |
sinfo->device = device; |
| 347 |
|
| 348 |
if (thread_new(&(el->th), connection_handler, (void*)sinfo) != 0) { |
| 349 |
fprintf(stderr, "Could not start connection handler.\n"); |
| 350 |
socket_shutdown(server_fd, SHUT_RDWR); |
| 351 |
socket_close(server_fd); |
| 352 |
break; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
debug("%s: Shutting down debugserver proxy...\n", __func__); |
| 357 |
|
| 358 |
/* join and clean up threads */ |
| 359 |
while (thread_list) { |
| 360 |
thread_info_t *el = thread_list; |
| 361 |
socket_shutdown(el->client_fd, SHUT_RDWR); |
| 362 |
socket_close(el->client_fd); |
| 363 |
thread_join(el->th); |
| 364 |
thread_free(el->th); |
| 365 |
thread_list = el->next; |
| 366 |
free(el); |
| 367 |
} |
| 368 |
|
| 369 |
leave_cleanup: |
| 370 |
if (device) { |
| 371 |
idevice_free(device); |
| 372 |
} |
| 373 |
|
| 374 |
return result; |
| 375 |
} |