forked from imkira/mobiledevice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunnel.m
355 lines (299 loc) · 8.29 KB
/
tunnel.m
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
#include "cli.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <signal.h>
#define TUNNEL_MAX_CONNS (FD_SETSIZE / 2 - 1)
#define FLPRINT(...) \
do \
{ \
printf( __VA_ARGS__); \
fflush(stdout); \
} while (0)
#define TUNNEL_ASSERT_OR_EXIT(_cnd_, _t_, ...) \
do \
{ \
if (!(_cnd_)) \
{ \
fprintf(stderr, __VA_ARGS__); \
terminate_tunnel(_t_); \
device_unregister(1); \
} \
} while (0)
struct mobiletunnel
{
struct am_device *device;
uint16_t src_port;
uint16_t dst_port;
int sock;
struct
{
int src_sock;
int dst_sock;
} conns[TUNNEL_MAX_CONNS];
int conn_count;
fd_set monitored_socks;
} *current_tunnel = NULL;
void init_tunnel(struct mobiletunnel *t)
{
t->sock = -1;
t->conn_count = 0;
FD_ZERO(&t->monitored_socks);
}
int tunnel_max_socket(struct mobiletunnel *t)
{
int i, max_sock = t->sock;
for (i = 0; i < t->conn_count; ++i)
{
if (t->conns[i].src_sock > max_sock)
{
max_sock = t->conns[i].src_sock;
}
if (t->conns[i].dst_sock > max_sock)
{
max_sock = t->conns[i].dst_sock;
}
}
return max_sock;
}
void remove_tunnel_conn(struct mobiletunnel *t, int conn)
{
// close connection sockets
close(t->conns[conn].src_sock);
close(t->conns[conn].dst_sock);
// unmonitor them
FD_CLR(t->conns[conn].src_sock, &t->monitored_socks);
FD_CLR(t->conns[conn].dst_sock, &t->monitored_socks);
// remove them from the array
if (t->conn_count > 1)
{
memcpy(&t->conns[conn], &t->conns[t->conn_count - 1], sizeof(t->conns[0]));
}
t->conn_count--;
}
void terminate_tunnel(struct mobiletunnel *t)
{
int i = 0;
// close all connections
while (t->conn_count > 0)
{
FLPRINT("Closing connection between client and device...\n");
remove_tunnel_conn(t, i);
}
// close server socket
if (t->sock != -1)
{
FLPRINT("Closing tunnel...\n");
close(t->sock);
}
init_tunnel(t);
}
void add_tunnel_conn(struct mobiletunnel *t, int src_sock, int dst_sock)
{
// add connection sockets to array
t->conns[t->conn_count].src_sock = src_sock;
t->conns[t->conn_count].dst_sock = dst_sock;
t->conn_count++;
// monitor new connection sockets
FD_SET(src_sock, &t->monitored_socks);
FD_SET(dst_sock, &t->monitored_socks);
}
int accept_tunnel_conn(struct mobiletunnel *t)
{
struct sockaddr_in addr;
socklen_t addr_size = sizeof(addr);
int src_sock = accept(t->sock, (struct sockaddr *)&addr, &addr_size);
// could not accept client?
if (src_sock < 0)
{
return 0;
}
FLPRINT("Got new client!\n");
if (t->conn_count >= TUNNEL_MAX_CONNS)
{
fprintf(stderr, "Cannot accept client: too many connections!\n");
// too many connections, reject this client
close(src_sock);
return 0;
}
unsigned int device_id = AMDeviceGetConnectionID(t->device);
int dst_sock;
// connect to device
if (USBMuxConnectByPort(device_id, htons(t->dst_port), &dst_sock) != 0)
{
fprintf(stderr, "Device refused connection to port %d!\n", t->dst_port);
// connection failed, reject this client
close(src_sock);
return 0;
}
FLPRINT("Forwarding connection to device...\n");
// register connection between source and remove sockets
add_tunnel_conn(t, src_sock, dst_sock);
return 1;
}
int process_tunnel_sock(struct mobiletunnel *t, int conn, int direction)
{
char buffer[512];
int src_sock, dst_sock;
if (direction == 0)
{
src_sock = t->conns[conn].src_sock;
dst_sock = t->conns[conn].dst_sock;
}
else
{
src_sock = t->conns[conn].dst_sock;
dst_sock = t->conns[conn].src_sock;
}
// read data from client or device
ssize_t recv_count = recv(src_sock, buffer, sizeof(buffer), 0);
// error reading or connection terminated?
if (recv_count <= 0)
{
fprintf(stderr, "Connection terminated!\n");
remove_tunnel_conn(t, conn);
return 0;
}
// forward data to the other party
send(dst_sock, buffer, recv_count, 0);
return 1;
}
void tunnel_loop(struct mobiletunnel *t)
{
fd_set signaled_socks;
int ready_count, processed_count, i;
FD_SET(t->sock, &t->monitored_socks);
while (1)
{
signaled_socks = t->monitored_socks;
processed_count = 0;
// wait for a new connection or new data on sockets to be tunnelled
ready_count = select(tunnel_max_socket(t) + 1, &signaled_socks,
NULL, NULL, NULL);
TUNNEL_ASSERT_OR_EXIT(ready_count > 0, t, "!select\n");
// new connection to tunnel server?
if (FD_ISSET(t->sock, &signaled_socks))
{
accept_tunnel_conn(t);
++processed_count;
}
// process sockets (in reverse order because they can be removed)
for (i = t->conn_count - 1; (processed_count < ready_count) && (i >= 0); --i)
{
// client wants to write data to device?
if (FD_ISSET(t->conns[i].src_sock, &signaled_socks))
{
++processed_count;
if (!process_tunnel_sock(t, i, 0))
{
// don't care about processed_count possibly becoming out of sync
continue;
}
}
// device wants to write data to client?
if (FD_ISSET(t->conns[i].dst_sock, &signaled_socks))
{
++processed_count;
process_tunnel_sock(t, i, 1);
}
}
}
}
void terminate_tunnel_at_exit(int sig)
{
if (current_tunnel != NULL)
{
FLPRINT("Terminating...\n");
terminate_tunnel(current_tunnel);
current_tunnel = NULL;
device_unregister(0);
}
exit(0);
}
void create_tunnel(struct am_device *device, uint16_t src_port, uint16_t dst_port)
{
struct mobiletunnel t;
int reuse = 1;
struct sockaddr_in addr;
socklen_t addr_size;
// initialize tunnel server socket
init_tunnel(&t);
t.device = device;
t.src_port = src_port;
t.dst_port = dst_port;
t.sock = socket(AF_INET, SOCK_STREAM, 0);
TUNNEL_ASSERT_OR_EXIT(t.sock != -1, &t, "!socket\n");
setsockopt(t.sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse));
// specify tunnel server address (localhost) and port
addr_size = sizeof(addr);
memset(&addr, 0, addr_size);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = htons(src_port);
// bind server socket to the address we specified
if ((bind(t.sock, (struct sockaddr *)&addr, addr_size) != 0) ||
(listen(t.sock, 10) != 0))
{
TUNNEL_ASSERT_OR_EXIT(0, &t, "Failed to bind to port %d!\n", src_port);
}
// if user passes port 0 as local port, we want to print out the real port.
if ((src_port == 0) &&
(getsockname(t.sock, (struct sockaddr *)&addr, &addr_size) == 0))
{
src_port = ntohs(addr.sin_port);
}
FLPRINT("Tunneling from local port %u to device port %u...\n", src_port, dst_port);
// register termination function
current_tunnel = &t;
signal(SIGHUP, terminate_tunnel_at_exit);
signal(SIGINT, terminate_tunnel_at_exit);
signal(SIGQUIT, terminate_tunnel_at_exit);
signal(SIGTERM, terminate_tunnel_at_exit);
// accept connections and tunnel them
tunnel_loop(&t);
}
static char *expected_udid = NULL;
static uint16_t tunnel_src_port = 0;
static uint16_t tunnel_dst_port = 0;
static void on_device_connected(struct am_device *device)
{
if (!device_matches(device, expected_udid))
{
return;
}
device_delayed_unregister_aborted = true;
device_connect(device);
create_tunnel(device, tunnel_src_port, tunnel_dst_port);
}
int tunnel(int argc, char *argv[])
{
int flag;
char *endptr;
int64_t timeout = -1;
while ((flag = getopt(argc, argv, "u:t:")) != -1)
{
switch (flag)
{
case 'u':
expected_udid = optarg;
break;
case 't':
timeout = strtoll(optarg, &endptr, 10);
break;
default:
help(argc, argv);
return 1;
}
}
argc -= optind;
argv += optind;
if (argc != 2)
{
return invalid_usage(argc, argv);
}
tunnel_src_port = (uint16_t)atoi(argv[0]);
tunnel_dst_port = (uint16_t)atoi(argv[1]);
device_delayed_unregister_status = 1;
device_register(on_device_connected, timeout);
return 1;
}