summaryrefslogtreecommitdiffstats
path: root/src/reverse_proxy.c
blob: 3ab1031e2528ff10b88c283eb8050db153637ea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/*
 * reverse_proxy.c
 * com.apple.PurpleReverseProxy service implementation.
 *
 * Copyright (c) 2021 Nikias Bassen, All Rights Reserved.
 * Copyright (c) 2014 BALATON Zoltan. All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <plist/plist.h>
#include <libimobiledevice-glue/thread.h>
#include <libimobiledevice-glue/socket.h>

#include "reverse_proxy.h"
#include "lockdown.h"
#include "common/debug.h"
#include "endianness.h"
#include "asprintf.h"

#ifndef ECONNRESET
#define ECONNRESET 108
#endif
#ifndef ETIMEDOUT
#define ETIMEDOUT 138
#endif

#define CTRL_PORT 1082
#define CTRLCMD  "BeginCtrl"
#define HELLOCTRLCMD "HelloCtrl"
#define HELLOCMD "HelloConn"

#define RP_SYNC_MSG  0x1
#define RP_PROXY_MSG 0x105
#define RP_PLIST_MSG 0xbbaa

/**
 * Convert a service_error_t value to a reverse_proxy_error_t value.
 * Used internally to get correct error codes.
 *
 * @param err A service_error_t error code
 *
 * @return A matching reverse_proxy_error_t error code,
 *     REVERSE_PROXY_E_UNKNOWN_ERROR otherwise.
 */
static reverse_proxy_error_t reverse_proxy_error(service_error_t err)
{
	switch (err) {
		case SERVICE_E_SUCCESS:
			return REVERSE_PROXY_E_SUCCESS;
		case SERVICE_E_INVALID_ARG:
			return REVERSE_PROXY_E_INVALID_ARG;
		case SERVICE_E_MUX_ERROR:
			return REVERSE_PROXY_E_MUX_ERROR;
		case SERVICE_E_SSL_ERROR:
			return REVERSE_PROXY_E_SSL_ERROR;
		case SERVICE_E_NOT_ENOUGH_DATA:
			return REVERSE_PROXY_E_NOT_ENOUGH_DATA;
		case SERVICE_E_TIMEOUT:
			return REVERSE_PROXY_E_TIMEOUT;
		default:
			break;
	}
	return REVERSE_PROXY_E_UNKNOWN_ERROR;
}

static void _reverse_proxy_log(reverse_proxy_client_t client, const char* format, ...)
{
	if (!client || !client->log_cb) {
		return;
	}
	va_list args;
	va_start(args, format);
	char* buffer = NULL;
	(void)vasprintf(&buffer, format, args);
	va_end(args);
	client->log_cb(client, buffer, client->log_cb_user_data);
	free(buffer);
}

static void _reverse_proxy_data(reverse_proxy_client_t client, int direction, char* buffer, uint32_t length)
{
	if (!client || !client->data_cb) {
		return;
	}
	client->data_cb(client, direction, buffer, length, client->data_cb_user_data);
}

static void _reverse_proxy_status(reverse_proxy_client_t client, int status, const char* format, ...)
{
	if (!client || !client->status_cb) {
		return;
	}
	va_list args;
	va_start(args, format);
	char* buffer = NULL;
	(void)vasprintf(&buffer, format, args);
	va_end(args);
	client->status_cb(client, status, buffer, client->status_cb_user_data);
	free(buffer);
}

static int _reverse_proxy_handle_proxy_cmd(reverse_proxy_client_t client)
{
	reverse_proxy_error_t err = REVERSE_PROXY_E_SUCCESS;
	char *buf = NULL;
	size_t bufsize = 1048576;
	uint32_t sent = 0, bytes = 0;
	uint32_t sent_total = 0;
	uint32_t recv_total = 0;
	char *host = NULL;
	uint16_t port = 0;

	buf = malloc(bufsize);
	if (!buf) {
		_reverse_proxy_log(client, "ERROR: Failed to allocate buffer");
		return -1;
	}

	err = reverse_proxy_receive(client, buf, bufsize, &bytes);
	if (err != REVERSE_PROXY_E_SUCCESS) {
		free(buf);
		_reverse_proxy_log(client, "ERROR: Unable to read data for proxy command");
		return -1;
	}
	_reverse_proxy_log(client, "Handling proxy command");

	/* Just return success here unconditionally because we don't know
	 * anything else and we will eventually abort on failure anyway */
	uint16_t ack = 5;
	err = reverse_proxy_send(client, (char *)&ack, sizeof(ack), &sent);
	if (err != REVERSE_PROXY_E_SUCCESS || sent != sizeof(ack)) {
		free(buf);
		_reverse_proxy_log(client, "ERROR: Unable to send ack. Sent %u of %u bytes.", sent, (uint32_t)sizeof(ack));
		return -1;
	}

	if (bytes < 3) {
		free(buf);
		_reverse_proxy_log(client, "Proxy command data too short, retrying");
		return 0;
	}

	/* ack command data too */
	err = reverse_proxy_send(client, buf, bytes, &sent);
	if (err != REVERSE_PROXY_E_SUCCESS || sent != bytes) {
		free(buf);
		_reverse_proxy_log(client, "ERROR: Unable to send data. Sent %u of %u bytes.", sent, bytes);
		return -1;
	}

	/* Now try to handle actual messages */
	/* Connect: 0 3 hostlen <host> <port> */
	if (buf[0] == 0 && buf[1] == 3) {
		uint16_t *p = (uint16_t *)&buf[bytes - 2];
		port = be16toh(*p);
		buf[bytes - 2] = '\0';
		host = strdup(&buf[3]);
		_reverse_proxy_log(client, "Connect request to %s:%u", host, port);
	}

	if (!host || !buf[2]) {
		/* missing or zero length host name */
		free(buf);
		return 0;
	}

	/* else wait for messages and forward them */
	int sockfd = socket_connect(host, port);
	if (sockfd < 0) {
		free(buf);
		_reverse_proxy_log(client, "ERROR: Connection to %s:%u failed: %s", host, port, strerror(errno));
		free(host);
		return -1;
	}

	_reverse_proxy_status(client, RP_STATUS_CONNECTED, "Connected to %s:%u", host, port);

	int res = 0, bytes_ret;
	while (1) {
		bytes = 0;
		err = reverse_proxy_receive_with_timeout(client, buf, bufsize, &bytes, 100);
		if (err == REVERSE_PROXY_E_TIMEOUT || (err == REVERSE_PROXY_E_SUCCESS && !bytes)) {
			/* just a timeout condition */
		}
		else if (err != REVERSE_PROXY_E_SUCCESS) {
			_reverse_proxy_log(client, "Connection closed");
			res = -1;
			break;
		}
		if (bytes) {
			_reverse_proxy_log(client, "Proxying %u bytes of data", bytes);
			_reverse_proxy_data(client, RP_DATA_DIRECTION_OUT, buf, bytes);
			sent = 0;
			while (sent < bytes) {
				int s = socket_send(sockfd, buf + sent, bytes - sent);
				if (s < 0) {
					break;
				}
				sent += s;
			}
			sent_total += sent;
			if (sent != bytes) {
				_reverse_proxy_log(client, "ERROR: Sending proxy payload failed: %s. Sent %u of %u bytes.", strerror(errno), sent, bytes);
				socket_close(sockfd);
				res = -1;
				break;
			}
		}
		bytes_ret = socket_receive_timeout(sockfd, buf, bufsize, 0, 100);
		if (bytes_ret == -ETIMEDOUT) {
			bytes_ret = 0;
		} else if (bytes_ret == -ECONNRESET) {
			res = 1;
			break;
		} else if (bytes_ret < 0) {
			_reverse_proxy_log(client, "ERROR: Failed to receive from host: %s", strerror(-bytes_ret));
			break;
		}

		bytes = bytes_ret;
		if (bytes) {
			_reverse_proxy_log(client, "Received %u bytes reply data, sending to device\n", bytes);
			_reverse_proxy_data(client, RP_DATA_DIRECTION_IN, buf, bytes);
			recv_total += bytes;
			sent = 0;
			while (sent < bytes) {
				uint32_t s;
				err = reverse_proxy_send(client, buf + sent, bytes - sent, &s);
				if (err != REVERSE_PROXY_E_SUCCESS) {
					break;
				}
				sent += s;
			}
			if (err != REVERSE_PROXY_E_SUCCESS || bytes != sent) {
				_reverse_proxy_log(client, "ERROR: Unable to send data (%d). Sent %u of %u bytes.", err, sent, bytes);
				res = -1;
				break;
			}
		}
	}
	socket_close(sockfd);
	free(host);
	free(buf);

	_reverse_proxy_status(client, RP_STATUS_DISCONNECTED, "Disconnected (out: %u / in: %u)", sent_total, recv_total);

	return res;
}

static int _reverse_proxy_handle_plist_cmd(reverse_proxy_client_t client)
{
	plist_t dict;
	reverse_proxy_error_t err;

	err = reverse_proxy_receive_plist(client, &dict);
	if (err != REVERSE_PROXY_E_SUCCESS) {
		_reverse_proxy_log(client, "ERROR: Unable to receive plist command, error", err);
		return -1;
	}
	plist_t node = plist_dict_get_item(dict, "Command");
	if (!node || (plist_get_node_type(node) != PLIST_STRING)) {
		_reverse_proxy_log(client, "ERROR: No 'Command' in reply", err);
		plist_free(dict);
		return -1;
	}
	char *command = NULL;
	plist_get_string_val(node, &command);
	plist_free(dict);

	if (!command) {
		_reverse_proxy_log(client, "ERROR: Empty 'Command' string");
		return -1;
	}

	if (!strcmp(command, "Ping")) {
		_reverse_proxy_log(client, "Received Ping command, replying with Pong");
		dict = plist_new_dict();
		plist_dict_set_item(dict, "Pong", plist_new_bool(1));
		err = reverse_proxy_send_plist(client, dict);
		plist_free(dict);
		if (err) {
			_reverse_proxy_log(client, "ERROR: Unable to send Ping command reply");
			free(command);
			return -1;
		}
	} else {
		_reverse_proxy_log(client, "WARNING: Received unhandled plist command '%s'", command);
		free(command);
		return -1;
	}

	free(command);
	/* reverse proxy connection will be terminated remotely. Next receive will get nothing, error and terminate this worker thread. */
	return 0;
}

static reverse_proxy_error_t reverse_proxy_client_new(idevice_t device, lockdownd_service_descriptor_t service, reverse_proxy_client_t * client)
{
	*client = NULL;

	if (!device || !service || service->port == 0 || !client || *client) {
		return REVERSE_PROXY_E_INVALID_ARG;
	}

	debug_info("Creating reverse_proxy_client, port = %d.", service->port);

	service_client_t sclient = NULL;
	reverse_proxy_error_t ret = reverse_proxy_error(service_client_new(device, service, &sclient));
	if (ret != REVERSE_PROXY_E_SUCCESS) {
		debug_info("Creating service client failed. Error: %i", ret);
		return ret;
	}

	reverse_proxy_client_t client_loc = (reverse_proxy_client_t) calloc(1, sizeof(struct reverse_proxy_client_private));
	client_loc->parent = sclient;
	client_loc->th_ctrl = THREAD_T_NULL;
	*client = client_loc;

	return 0;
}

static void* _reverse_proxy_connection_thread(void *cdata)
{
	reverse_proxy_client_t client = (reverse_proxy_client_t)cdata;
	uint32_t bytes = 0;
	reverse_proxy_client_t conn_client = NULL;
	reverse_proxy_error_t err = REVERSE_PROXY_E_UNKNOWN_ERROR;

	if (client->conn_port == 0) {
		service_client_factory_start_service(client->parent->connection->device, "com.apple.PurpleReverseProxy.Conn", (void**)&conn_client, client->label, SERVICE_CONSTRUCTOR(reverse_proxy_client_new), &err);
		if (!conn_client) {
			_reverse_proxy_log(client, "ERROR: Failed to start proxy connection service, error %d", err);
		}
	} else {
		struct lockdownd_service_descriptor svc;
		svc.port = client->conn_port;
		svc.ssl_enabled = 0;
		svc.identifier = NULL;
		err = reverse_proxy_client_new(client->parent->connection->device, &svc, &conn_client);
		if (!conn_client) {
			_reverse_proxy_log(client, "ERROR: Failed to connect to proxy connection port %u, error %d", client->conn_port, err);
		}
	}
	if (!conn_client) {
		goto leave;
	}
	conn_client->type = RP_TYPE_CONN;
	conn_client->protoversion = client->protoversion;
	conn_client->log_cb = client->log_cb;
	conn_client->log_cb_user_data = client->log_cb_user_data;
	conn_client->status_cb = client->status_cb;
	conn_client->status_cb_user_data = client->status_cb_user_data;

	err = reverse_proxy_send(conn_client, HELLOCMD, sizeof(HELLOCMD), &bytes);
	if (err != REVERSE_PROXY_E_SUCCESS || bytes != sizeof(HELLOCMD)) {
		_reverse_proxy_log(conn_client, "ERROR: Unable to send " HELLOCMD " (sent %u/%u bytes)", bytes, sizeof(HELLOCMD));
		goto leave;
	}

	if (conn_client->protoversion == 2) {
		plist_t reply = NULL;
		err = reverse_proxy_receive_plist(conn_client, &reply);
		if (err != REVERSE_PROXY_E_SUCCESS) {
			_reverse_proxy_log(conn_client, "ERROR: Did not receive " HELLOCMD " reply, error %d", err);
			goto leave;
		}
		char* identifier = NULL;
		char* cmd = NULL;
		plist_t node = NULL;
		node = plist_dict_get_item(reply, "Command");
		if (node) {
			plist_get_string_val(node, &cmd);
		}
		node = plist_dict_get_item(reply, "Identifier");
		if (node) {
			plist_get_string_val(node, &identifier);
		}
		plist_free(reply);

		if (!cmd || (strcmp(cmd, HELLOCMD) != 0)) {
			free(cmd);
			free(identifier);
			_reverse_proxy_log(conn_client, "ERROR: Unexpected reply to " HELLOCMD " received");
			goto leave;
		}
		free(cmd);

		if (identifier) {
			_reverse_proxy_log(conn_client, "Got device identifier %s", identifier);
			free(identifier);
		}
	} else {
		char buf[16];
		memset(buf, '\0', sizeof(buf));
		bytes = 0;
		err = reverse_proxy_receive(conn_client, buf, sizeof(HELLOCMD), &bytes);
		if (err != REVERSE_PROXY_E_SUCCESS) {
			_reverse_proxy_log(conn_client, "ERROR: Did not receive " HELLOCMD " reply, error %d", err);
			goto leave;
		}
		if (memcmp(buf, HELLOCMD, sizeof(HELLOCMD)) != 0) {
			_reverse_proxy_log(conn_client, "ERROR: Did not receive " HELLOCMD " as reply, but %.*s", (int)bytes, buf);
			goto leave;
		}
	}

	_reverse_proxy_status(conn_client, RP_STATUS_READY, "Ready");

	int running = 1;
	while (client->th_ctrl != THREAD_T_NULL && conn_client && running) {
		uint16_t cmd = 0;
		bytes = 0;
		err = reverse_proxy_receive_with_timeout(conn_client, (char*)&cmd, sizeof(cmd), &bytes, 1000);
		if (err == REVERSE_PROXY_E_TIMEOUT || (err == REVERSE_PROXY_E_SUCCESS && bytes != sizeof(cmd))) {
			continue;
		} else if (err != REVERSE_PROXY_E_SUCCESS) {
			_reverse_proxy_log(conn_client, "Connection closed");
			break;
		}
		cmd = le16toh(cmd);
		switch (cmd) {
		case 0xBBAA:
			/* plist command */
			if (_reverse_proxy_handle_plist_cmd(conn_client) < 0) {
				running = 0;
			}
			break;
		case 0x105:
			/* proxy command */
			if (_reverse_proxy_handle_proxy_cmd(conn_client) < 0) {
				running = 0;
			}
			break;
		default:
			/* unknown */
			debug_info("ERROR: Unknown request 0x%x", cmd);
			_reverse_proxy_log(conn_client, "ERROR: Unknown request 0x%x", cmd);
			running = 0;
			break;
		}
	}

leave:
	_reverse_proxy_status(conn_client, RP_STATUS_TERMINATE, "Terminated");
	if (conn_client) {
		reverse_proxy_client_free(conn_client);
	}

	return NULL;
}

static void* _reverse_proxy_control_thread(void *cdata)
{
	reverse_proxy_client_t client = (reverse_proxy_client_t)cdata;
	THREAD_T th_conn = THREAD_T_NULL;
	int running = 1;
	_reverse_proxy_status(client, RP_STATUS_READY, "Ready");
	while (client && client->parent && running) {
		uint32_t cmd = 0;
		uint32_t bytes = 0;
		reverse_proxy_error_t err = reverse_proxy_receive_with_timeout(client, (char*)&cmd, sizeof(cmd), &bytes, 1000);
		if (err == REVERSE_PROXY_E_TIMEOUT || (err == REVERSE_PROXY_E_SUCCESS && bytes != sizeof(cmd))) {
			continue;
		} else if (err != REVERSE_PROXY_E_SUCCESS) {
			_reverse_proxy_log(client, "Connection closed");
			break;
		}
		cmd = le32toh(cmd);
		switch (cmd) {
		case 1:
			/* connection request */
			debug_info("ReverseProxy<%p> got connect request", client);
			_reverse_proxy_status(client, RP_STATUS_CONNECT_REQ, "Connect Request");
			if (thread_new(&th_conn, _reverse_proxy_connection_thread, client) != 0) {
				debug_info("ERROR: Failed to start connection thread");
				th_conn = THREAD_T_NULL;
				running = 0;
			}
			break;
		case 2:
			/* shutdown request */
			debug_info("ReverseProxy<%p> got shutdown request", client);
			_reverse_proxy_status(client, RP_STATUS_SHUTDOWN_REQ, "Shutdown Request");
			running = 0;
			break;
		default:
			/* unknown */
			debug_info("ERROR: Unknown request 0x%x", cmd);
			_reverse_proxy_log(client, "ERROR: Unknown request 0x%x", cmd);
			running = 0;
			break;
		}
	}
	_reverse_proxy_log(client, "Terminating");

	client->th_ctrl = THREAD_T_NULL;
	if (th_conn) {
		debug_info("joining connection thread");
		thread_join(th_conn);
		thread_free(th_conn);
	}

	_reverse_proxy_status(client, RP_STATUS_TERMINATE, "Terminated");

	return NULL;
}

LIBIMOBILEDEVICE_API reverse_proxy_error_t reverse_proxy_client_start_proxy(reverse_proxy_client_t client, int control_protocol_version)
{
	char buf[16] = {0, };
	uint32_t bytes = 0;
	reverse_proxy_error_t err = REVERSE_PROXY_E_UNKNOWN_ERROR;

	if (!client) {
		return REVERSE_PROXY_E_INVALID_ARG;
	}
	if (control_protocol_version < 1 || control_protocol_version > 2) {
		debug_info("invalid protocol version %d, must be 1 or 2", control_protocol_version);
		return REVERSE_PROXY_E_INVALID_ARG;
	}

	if (control_protocol_version == 2) {
		err = reverse_proxy_send(client, CTRLCMD, sizeof(CTRLCMD), &bytes);
		if (err != REVERSE_PROXY_E_SUCCESS) {
			_reverse_proxy_log(client, "ERROR: Failed to send " CTRLCMD " to device, error %d", err);
			return err;
		}
		plist_t dict = plist_new_dict();
		plist_dict_set_item(dict, "Command", plist_new_string(CTRLCMD));
		plist_dict_set_item(dict, "CtrlProtoVersion", plist_new_uint(client->protoversion));
		err = reverse_proxy_send_plist(client, dict);
		plist_free(dict);
		if (err != REVERSE_PROXY_E_SUCCESS) {
			_reverse_proxy_log(client, "ERROR: Could not send " CTRLCMD " plist command, error %d", err);
			return err;
		}
		dict = NULL;
		err = reverse_proxy_receive_plist(client, &dict);
		if (err != REVERSE_PROXY_E_SUCCESS) {
			_reverse_proxy_log(client, "ERROR: Could not receive " CTRLCMD " plist reply, error %d", err);
			return err;
		}
		plist_t node = plist_dict_get_item(dict, "ConnPort");
		if (node && plist_get_node_type(node) == PLIST_UINT) {
			uint64_t u64val = 0;
			plist_get_uint_val(node, &u64val);
			client->conn_port = (uint16_t)u64val;
		} else {
			_reverse_proxy_log(client, "ERROR: Could not get ConnPort value");
			return REVERSE_PROXY_E_UNKNOWN_ERROR;
		}
		client->protoversion = 2;
	} else {
		err = reverse_proxy_send(client, HELLOCTRLCMD, sizeof(HELLOCTRLCMD), &bytes);
		if (err != REVERSE_PROXY_E_SUCCESS) {
			_reverse_proxy_log(client, "ERROR: Failed to send " HELLOCTRLCMD " to device, error %d", err);
			return err;
		}

		bytes = 0;
		err = reverse_proxy_receive(client, buf, sizeof(HELLOCTRLCMD)-1, &bytes);
		if (err != REVERSE_PROXY_E_SUCCESS) {
			_reverse_proxy_log(client, "ERROR: Could not receive " HELLOCTRLCMD " reply, error %d", err);
			return err;
		}

		uint16_t cport = 0;
		bytes = 0;
		err = reverse_proxy_receive(client, (char*)&cport, 2, &bytes);
		if (err != REVERSE_PROXY_E_SUCCESS) {
			_reverse_proxy_log(client, "ERROR: Failed to receive connection port, error %d", err);
			return err;
		}
		client->conn_port = le16toh(cport);
		client->protoversion = 1;
	}

	if (thread_new(&(client->th_ctrl), _reverse_proxy_control_thread, client) != 0) {
		_reverse_proxy_log(client, "ERROR: Failed to start control thread");
		client->th_ctrl = THREAD_T_NULL; /* undefined after failure */
		err = REVERSE_PROXY_E_UNKNOWN_ERROR;
	}

	return err;
}

LIBIMOBILEDEVICE_API reverse_proxy_error_t reverse_proxy_client_create_with_service(idevice_t device, reverse_proxy_client_t* client, const char* label)
{
	reverse_proxy_error_t err = REVERSE_PROXY_E_UNKNOWN_ERROR;
	service_client_factory_start_service(device, "com.apple.PurpleReverseProxy.Ctrl", (void**)client, label, SERVICE_CONSTRUCTOR(reverse_proxy_client_new), &err);
	if (!*client) {
		return err;
	}
	(*client)->label = strdup(label);
	(*client)->type = RP_TYPE_CTRL;

	return REVERSE_PROXY_E_SUCCESS;
}

LIBIMOBILEDEVICE_API reverse_proxy_error_t reverse_proxy_client_create_with_port(idevice_t device, reverse_proxy_client_t* client, uint16_t device_port)
{
	reverse_proxy_client_t client_loc = NULL;
	reverse_proxy_error_t err;

	struct lockdownd_service_descriptor svc;
	svc.port = device_port;
	svc.ssl_enabled = 0;
	svc.identifier = NULL;

	err = reverse_proxy_client_new(device, &svc, &client_loc);
	if (err != REVERSE_PROXY_E_SUCCESS) {
		return err;
	}

	client_loc->type = RP_TYPE_CTRL;
	*client = client_loc;

	return REVERSE_PROXY_E_SUCCESS;
}

LIBIMOBILEDEVICE_API reverse_proxy_error_t reverse_proxy_client_free(reverse_proxy_client_t client)
{
	if (!client)
		return REVERSE_PROXY_E_INVALID_ARG;
	service_client_t parent = client->parent;
	client->parent = NULL;
	if (client->th_ctrl) {
		debug_info("joining control thread");
		thread_join(client->th_ctrl);
		thread_free(client->th_ctrl);
		client->th_ctrl = THREAD_T_NULL;
	}
	reverse_proxy_error_t err = reverse_proxy_error(service_client_free(parent));
	free(client->label);
	free(client);

	return err;
}

LIBIMOBILEDEVICE_API reverse_proxy_client_type_t reverse_proxy_get_type(reverse_proxy_client_t client)
{
	if (!client)
		return 0;
	return client->type;
}

LIBIMOBILEDEVICE_API void reverse_proxy_client_set_status_callback(reverse_proxy_client_t client, reverse_proxy_status_cb_t status_callback, void* user_data)
{
	if (!client) {
		return;
	}
	client->status_cb = status_callback;
	client->status_cb_user_data = user_data;
}

LIBIMOBILEDEVICE_API void reverse_proxy_client_set_log_callback(reverse_proxy_client_t client, reverse_proxy_log_cb_t log_callback, void* user_data)
{
	if (!client) {
		return;
	}
	client->log_cb = log_callback;
	client->log_cb_user_data = user_data;
}

LIBIMOBILEDEVICE_API void reverse_proxy_client_set_data_callback(reverse_proxy_client_t client, reverse_proxy_data_cb_t data_callback, void* user_data)
{
	if (!client) {
		return;
	}
	client->data_cb = data_callback;
	client->data_cb_user_data = user_data;
}

reverse_proxy_error_t reverse_proxy_send(reverse_proxy_client_t client, const char* data, uint32_t len, uint32_t* sent)
{
	reverse_proxy_error_t err = reverse_proxy_error(service_send(client->parent, data, len, sent));
	return err;
}

reverse_proxy_error_t reverse_proxy_receive_with_timeout(reverse_proxy_client_t client, char* buffer, uint32_t len, uint32_t* received, unsigned int timeout)
{
	if (!client)
		return REVERSE_PROXY_E_INVALID_ARG;
	return reverse_proxy_error(service_receive_with_timeout(client->parent, buffer, len, received, timeout));
}

reverse_proxy_error_t reverse_proxy_receive(reverse_proxy_client_t client, char* buffer, uint32_t len, uint32_t* received)
{
	return reverse_proxy_receive_with_timeout(client, buffer, len, received, 20000);
}

reverse_proxy_error_t reverse_proxy_send_plist(reverse_proxy_client_t client, plist_t plist)
{
	reverse_proxy_error_t err;
	uint32_t len = 0;
	char* buf = NULL;
	uint32_t bytes = 0;

	plist_to_bin(plist, &buf, &len);

	if (!buf) {
		return REVERSE_PROXY_E_INVALID_ARG;
	}

	debug_info("Sending %u bytes", len);

	uint32_t slen = htole32(len);
	err = reverse_proxy_send(client, (char*)&slen, sizeof(slen), &bytes);
	if (err != REVERSE_PROXY_E_SUCCESS) {
		free(buf);
		debug_info("ERROR: Unable to send data length, error %d. Sent %u/%u bytes.", err, bytes, (uint32_t)sizeof(slen));
		return err;
	}
	uint32_t done = 0;
	do {
		bytes = 0;
		err = reverse_proxy_send(client, buf+done, len-done, &bytes);
		if (err != REVERSE_PROXY_E_SUCCESS) {
			break;
		}
		done += bytes;
	} while (done < len);
	free(buf);
	if (err != REVERSE_PROXY_E_SUCCESS || done != len) {
		debug_info("ERROR: Unable to send data, error %d. Sent %u/%u bytes.", err, done, len);
		return err;
	}

	debug_info("Sent %u bytes", len);

	return REVERSE_PROXY_E_SUCCESS;
}

reverse_proxy_error_t reverse_proxy_receive_plist(reverse_proxy_client_t client, plist_t* plist)
{
	return reverse_proxy_receive_plist_with_timeout(client, plist, 20000);
}

reverse_proxy_error_t reverse_proxy_receive_plist_with_timeout(reverse_proxy_client_t client, plist_t * plist, uint32_t timeout_ms)
{
	uint32_t len;
	uint32_t bytes;
	reverse_proxy_error_t err;

	err = reverse_proxy_receive_with_timeout(client, (char*)&len, sizeof(len), &bytes, timeout_ms);
	if (err != REVERSE_PROXY_E_SUCCESS) {
		if (err != REVERSE_PROXY_E_TIMEOUT) {
			debug_info("ERROR: Unable to receive packet length, error %d\n", err);
		}
		return err;
	}

	len = le32toh(len);
	char* buf = calloc(1, len);
	if (!buf) {
		debug_info("ERROR: Out of memory");
		return REVERSE_PROXY_E_UNKNOWN_ERROR;
	}

	uint32_t done = 0;
	do {
		bytes = 0;
		err = reverse_proxy_receive_with_timeout(client, buf+done, len-done, &bytes, timeout_ms);
		if (err != REVERSE_PROXY_E_SUCCESS) {
			break;
		}
		done += bytes;
	} while (done < len);

	if (err != REVERSE_PROXY_E_SUCCESS || done != len) {
		free(buf);
		debug_info("ERROR: Unable to receive data, error %d. Received %u/%u bytes.", err, done, len);
		return err;
	}

	debug_info("Received %u bytes", len);

	plist_from_bin(buf, len, plist);
	free(buf);

	if (!(*plist)) {
		debug_info("ERROR: Failed to convert buffer to plist");
		return REVERSE_PROXY_E_PLIST_ERROR;
	}

	return REVERSE_PROXY_E_SUCCESS;
}