Debian Codegraph @

tools/iproxy.c

raw
1 /*
2 * iproxy.c -- proxy that enables tcp service access to iOS devices
3 *
4 * Copyright (C) 2009-2020 Nikias Bassen <[email protected]>
5 * Copyright (C) 2014 Martin Szulecki <[email protected]>
6 * Copyright (C) 2009 Paul Sladen <[email protected]>
7 *
8 * Based upon iTunnel source code, Copyright (c) 2008 Jing Su.
9 * http://www.cs.toronto.edu/~jingsu/itunnel/
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #define TOOL_NAME "iproxy"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stddef.h>
36 #include <errno.h>
37
38 #ifdef _WIN32
39 #include <winsock2.h>
40 #include <windows.h>
41 typedef unsigned int socklen_t;
42 #else
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <sys/select.h>
46 #include <sys/socket.h>
47 #include <signal.h>
48 #endif
49
50 #include <getopt.h>
51 #include <libimobiledevice-glue/socket.h>
52 #include <libimobiledevice-glue/thread.h>
53 #include "usbmuxd.h"
54
55 #ifndef ETIMEDOUT
56 #define ETIMEDOUT 138
57 #endif
58
59 static int debug_level = 0;
60
61 struct client_data {
62 int fd;
63 int sfd;
64 char* udid;
65 enum usbmux_lookup_options lookup_opts;
66 uint16_t device_port;
67 };
68
69 #define CDATA_FREE(x) if (x) { \
70 if ((x)->fd > 0) socket_close((x)->fd); \
71 if ((x)->sfd > 0) socket_close((x)->sfd); \
72 free((x)->udid); \
73 free(x); \
74 }
75
76 static void *acceptor_thread(void *arg)
77 {
78 char buffer[32768];
79 struct client_data *cdata = (struct client_data*)arg;
80 usbmuxd_device_info_t *dev_list = NULL;
81 usbmuxd_device_info_t *dev = NULL;
82 usbmuxd_device_info_t muxdev;
83 int count;
84
85 if (!cdata) {
86 fprintf(stderr, "invalid client_data provided!\n");
87 return NULL;
88 }
89
90 if (cdata->udid) {
91 if (usbmuxd_get_device(cdata->udid, &muxdev, cdata->lookup_opts) > 0) {
92 dev = &muxdev;
93 }
94 } else {
95 if ((count = usbmuxd_get_device_list(&dev_list)) < 0) {
96 printf("Connecting to usbmuxd failed, terminating.\n");
97 free(dev_list);
98 CDATA_FREE(cdata);
99 return NULL;
100 }
101
102 if (dev_list == NULL || dev_list[0].handle == 0) {
103 printf("No connected device found, terminating.\n");
104 free(dev_list);
105 CDATA_FREE(cdata);
106 return NULL;
107 }
108
109 int i;
110 for (i = 0; i < count; i++) {
111 if (dev_list[i].conn_type == CONNECTION_TYPE_USB && (cdata->lookup_opts & DEVICE_LOOKUP_USBMUX)) {
112 dev = &(dev_list[i]);
113 break;
114 }
115 if (dev_list[i].conn_type == CONNECTION_TYPE_NETWORK && (cdata->lookup_opts & DEVICE_LOOKUP_NETWORK)) {
116 dev = &(dev_list[i]);
117 break;
118 }
119 }
120 }
121
122 if (dev == NULL || dev->handle == 0) {
123 printf("No connected/matching device found, disconnecting client.\n");
124 free(dev_list);
125 CDATA_FREE(cdata);
126 return NULL;
127 }
128
129 cdata->sfd = -1;
130 if (dev->conn_type == CONNECTION_TYPE_NETWORK) {
131 struct sockaddr_storage saddr_storage;
132 struct sockaddr* saddr = (struct sockaddr*)&saddr_storage;
133
134 if (dev->conn_data[1] == 0x02) { // AF_INET
135 saddr->sa_family = AF_INET;
136 memcpy(&saddr->sa_data[0], (uint8_t*)dev->conn_data+2, 14);
137 }
138 else if (dev->conn_data[1] == 0x1E) { //AF_INET6 (bsd)
139 #ifdef AF_INET6
140 saddr->sa_family = AF_INET6;
141 /* copy the address and the host dependent scope id */
142 memcpy(&saddr->sa_data[0], (uint8_t*)dev->conn_data+2, 26);
143 #else
144 fprintf(stderr, "ERROR: Got an IPv6 address but this system doesn't support IPv6\n");
145 CDATA_FREE(cdata);
146 return NULL;
147 #endif
148 }
149 else {
150 fprintf(stderr, "Unsupported address family 0x%02x\n", dev->conn_data[1]);
151 CDATA_FREE(cdata);
152 return NULL;
153 }
154 char addrtxt[48];
155 addrtxt[0] = '\0';
156 if (!socket_addr_to_string(saddr, addrtxt, sizeof(addrtxt))) {
157 fprintf(stderr, "Failed to convert network address: %d (%s)\n", errno, strerror(errno));
158 }
159 fprintf(stdout, "Requesting connection to NETWORK device %s (serial: %s), port %d\n", addrtxt, dev->udid, cdata->device_port);
160 cdata->sfd = socket_connect_addr(saddr, cdata->device_port);
161 if (cdata->sfd < 0) {
162 cdata->sfd = -errno;
163 }
164 } else if (dev->conn_type == CONNECTION_TYPE_USB) {
165 fprintf(stdout, "Requesting connection to USB device handle %d (serial: %s), port %d\n", dev->handle, dev->udid, cdata->device_port);
166
167 cdata->sfd = usbmuxd_connect(dev->handle, cdata->device_port);
168 }
169 free(dev_list);
170 if (cdata->sfd < 0) {
171 fprintf(stderr, "Error connecting to device: %s\n", strerror(-cdata->sfd));
172 } else {
173 fd_set fds;
174 FD_ZERO(&fds);
175 FD_SET(cdata->fd, &fds);
176 FD_SET(cdata->sfd, &fds);
177 int maxfd = cdata->fd > cdata->sfd ? cdata->fd : cdata->sfd;
178
179 while (1) {
180 fd_set read_fds = fds;
181 int ret_sel = select(maxfd+1, &read_fds, NULL, NULL, NULL);
182 if (ret_sel < 0) {
183 perror("select");
184 break;
185 }
186 if (FD_ISSET(cdata->fd, &read_fds)) {
187 int r = socket_receive_timeout(cdata->fd, buffer, sizeof(buffer), 0, 100);
188 if (r <= 0) {
189 break;
190 }
191 int sent = 0;
192 while (sent < r) {
193 int s = socket_send(cdata->sfd, buffer+sent, r-sent);
194 if (s <= 0) {
195 break;
196 }
197 sent += s;
198 }
199 }
200 if (FD_ISSET(cdata->sfd, &read_fds)) {
201 int r = socket_receive_timeout(cdata->sfd, buffer, sizeof(buffer), 0, 100);
202 if (r <= 0) {
203 break;
204 }
205 int sent = 0;
206 while (sent < r) {
207 int s = socket_send(cdata->fd, buffer+sent, r-sent);
208 if (s <= 0) {
209 break;
210 }
211 sent += s;
212 }
213 }
214 }
215 }
216
217 CDATA_FREE(cdata);
218
219 return NULL;
220 }
221
222 static void print_usage(int argc, char **argv, int is_error)
223 {
224 char *name = NULL;
225 name = strrchr(argv[0], '/');
226 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] LOCAL_PORT:DEVICE_PORT [LOCAL_PORT2:DEVICE_PORT2 ...]\n", (name ? name+1 : argv[0]));
227 fprintf(is_error ? stderr : stdout,
228 "\n" \
229 "Proxy that binds local TCP ports to be forwarded to the specified ports on a usbmux device.\n" \
230 "\n" \
231 "OPTIONS:\n" \
232 " -u, --udid UDID target specific device by UDID\n" \
233 " -n, --network connect to network device\n" \
234 " -l, --local connect to USB device (default)\n" \
235 " -s, --source ADDR source address for listening socket (default 127.0.0.1)\n" \
236 " -h, --help prints usage information\n" \
237 " -d, --debug increase debug level\n" \
238 " -v, --version prints version information\n" \
239 "\n" \
240 "Homepage: <" PACKAGE_URL ">\n"
241 "Bug Reports: <" PACKAGE_BUGREPORT ">\n"
242 );
243 }
244
245 int main(int argc, char **argv)
246 {
247 char* device_udid = NULL;
248 char* source_addr = NULL;
249 uint16_t listen_port[16];
250 uint16_t device_port[16];
251 #ifdef AF_INET6
252 #define MAX_LISTEN_NUM 32
253 #else
254 #define MAX_LISTEN_NUM 16
255 #endif
256 struct listen_sock {
257 int fd;
258 int index;
259 } listen_sock[MAX_LISTEN_NUM];
260 int num_listen = 0;
261 int num_pairs = 0;
262 int i = 0;
263 enum usbmux_lookup_options lookup_opts = 0;
264
265 const struct option longopts[] = {
266 { "debug", no_argument, NULL, 'd' },
267 { "help", no_argument, NULL, 'h' },
268 { "udid", required_argument, NULL, 'u' },
269 { "local", no_argument, NULL, 'l' },
270 { "network", no_argument, NULL, 'n' },
271 { "source", required_argument, NULL, 's' },
272 { "version", no_argument, NULL, 'v' },
273 { NULL, 0, NULL, 0}
274 };
275 int c = 0;
276 while ((c = getopt_long(argc, argv, "dhu:lns:v", longopts, NULL)) != -1) {
277 switch (c) {
278 case 'd':
279 libusbmuxd_set_debug_level(++debug_level);
280 break;
281 case 'u':
282 if (!*optarg) {
283 fprintf(stderr, "ERROR: UDID must not be empty!\n");
284 print_usage(argc, argv, 1);
285 return 2;
286 }
287 free(device_udid);
288 device_udid = strdup(optarg);
289 break;
290 case 'l':
291 lookup_opts |= DEVICE_LOOKUP_USBMUX;
292 break;
293 case 'n':
294 lookup_opts |= DEVICE_LOOKUP_NETWORK;
295 break;
296 case 's':
297 if (!*optarg) {
298 fprintf(stderr, "ERROR: source address must not be empty!\n");
299 print_usage(argc, argv, 1);
300 return 2;
301 }
302 free(source_addr);
303 source_addr = strdup(optarg);
304 break;
305 case 'h':
306 print_usage(argc, argv, 0);
307 return 0;
308 case 'v':
309 printf("%s %s\n", TOOL_NAME, libusbmuxd_version());
310 return 0;
311 default:
312 print_usage(argc, argv, 1);
313 return 2;
314 }
315 }
316
317 if (lookup_opts == 0) {
318 lookup_opts = DEVICE_LOOKUP_USBMUX;
319 }
320
321 argc -= optind;
322 argv += optind;
323
324 if (argc == 0) {
325 fprintf(stderr, "ERROR: Not enough parameters. Need at least one pair of ports.\n");
326 print_usage(argc + optind, argv - optind, 1);
327 free(device_udid);
328 free(source_addr);
329 return 2;
330 }
331
332 if (argc == 2 && (strchr(argv[0], ':') == NULL) && (strchr(argv[1], ':') == NULL)) {
333 /* support old-style port pair specification */
334 char* endp = NULL;
335 listen_port[0] = (uint16_t)strtol(argv[0], &endp, 10);
336 if (!listen_port[0] || *endp != '\0') {
337 fprintf(stderr, "Invalid listen port specified in argument '%s'!\n", argv[0]);
338 free(device_udid);
339 free(source_addr);
340 return EINVAL;
341 }
342 endp = NULL;
343 device_port[0] = (uint16_t)strtol(argv[1], &endp, 10);
344 if (!device_port[0] || *endp != '\0') {
345 fprintf(stderr, "Invalid device port specified in argument '%s'!\n", argv[1]);
346 free(device_udid);
347 free(source_addr);
348 return EINVAL;
349 }
350 num_pairs = 1;
351 } else {
352 /* new style, colon-separated local:device port pairs */
353 for (i = 0; i < argc; i++) {
354 char* endp = NULL;
355 listen_port[i] = (uint16_t)strtol(argv[i], &endp, 10);
356 if (!listen_port[i] || (*endp != ':')) {
357 fprintf(stderr, "Invalid listen port specified in argument '%s'!\n", argv[i]);
358 free(device_udid);
359 free(source_addr);
360 return EINVAL;
361 }
362 device_port[i] = (uint16_t)strtol(endp+1, &endp, 10);
363 if (!device_port[i] || (*endp != '\0')) {
364 fprintf(stderr, "Invalid device port specified in argument '%s'!\n", argv[i+1]);
365 free(device_udid);
366 free(source_addr);
367 return EINVAL;
368 }
369 }
370 num_pairs = argc;
371 }
372
373 if (num_pairs > 16) {
374 fprintf(stderr, "ERROR: Too many LOCAL:DEVICE port pairs. Maximum is 16.\n");
375 return 1;
376 }
377
378 #ifndef _WIN32
379 signal(SIGPIPE, SIG_IGN);
380 #endif
381
382 // first create the listening sockets
383 for (i = 0; i < num_pairs; i++) {
384 printf("Creating listening port %d for device port %d\n", listen_port[i], device_port[i]);
385 if (!source_addr) {
386 listen_sock[num_listen].fd = socket_create(NULL, listen_port[i]);
387 if (listen_sock[num_listen].fd < 0) {
388 int j;
389 fprintf(stderr, "Error creating socket for listen port %u: %s\n", listen_port[i], strerror(errno));
390 free(source_addr);
391 free(device_udid);
392 for (j = num_listen; j >= 0; j--) {
393 socket_close(listen_sock[j].fd);
394 }
395 return 1;
396 }
397 listen_sock[num_listen].index = i;
398 num_listen++;
399 } else {
400 listen_sock[num_listen].fd = socket_create(source_addr, listen_port[i]);
401 if (listen_sock[num_listen].fd < 0) {
402 int j;
403 fprintf(stderr, "Error creating socket for listen port %u: %s\n", listen_port[i], strerror(errno));
404 free(source_addr);
405 free(device_udid);
406 for (j = num_listen; j >= 0; j--) {
407 socket_close(listen_sock[j].fd);
408 }
409 return 1;
410 }
411 listen_sock[num_listen].index = i;
412 num_listen++;
413 }
414 }
415
416 // make them non-blocking and add them to fd set
417 fd_set fds;
418 FD_ZERO(&fds);
419 for (i = 0; i < num_listen; i++) {
420 #ifdef _WIN32
421 u_long l_yes = 1;
422 ioctlsocket(listen_sock[i].fd, FIONBIO, &l_yes);
423 #else
424 int flags = fcntl(listen_sock[i].fd, F_GETFL, 0);
425 fcntl(listen_sock[i].fd, F_SETFL, flags | O_NONBLOCK);
426 #endif
427 FD_SET(listen_sock[i].fd, &fds);
428 }
429
430 // main loop
431 while (1) {
432 printf("waiting for connection\n");
433 fd_set read_fds = fds;
434 int ret_sel = select(listen_sock[num_listen-1].fd+1, &read_fds, NULL, NULL, NULL);
435 if (ret_sel < 0) {
436 perror("select");
437 break;
438 }
439 for (i = 0; i < num_listen; i++) {
440 if (FD_ISSET(listen_sock[i].fd, &read_fds)) {
441 THREAD_T acceptor = THREAD_T_NULL;
442 struct client_data *cdata;
443 int c_sock = socket_accept(listen_sock[i].fd, listen_port[listen_sock[i].index]);
444 if (c_sock < 0) {
445 fprintf(stderr, "accept: %s\n", strerror(errno));
446 break;
447 }
448 printf("New connection for %d->%d, fd = %d\n", listen_port[listen_sock[i].index], device_port[listen_sock[i].index], c_sock);
449 cdata = (struct client_data*)malloc(sizeof(struct client_data));
450 if (!cdata) {
451 socket_close(c_sock);
452 fprintf(stderr, "ERROR: Out of memory\n");
453 free(device_udid);
454 return 1;
455 }
456 cdata->fd = c_sock;
457 cdata->sfd = -1;
458 cdata->udid = (device_udid) ? strdup(device_udid) : NULL;
459 cdata->lookup_opts = lookup_opts;
460 cdata->device_port = device_port[listen_sock[i].index];
461
462 if (thread_new(&acceptor, acceptor_thread, cdata) == 0) {
463 thread_detach(acceptor);
464 } else {
465 fprintf(stderr, "ERROR: Failed to created acceptor thread!\n");
466 }
467 }
468 }
469 }
470
471 for (i = 0; i < num_listen; i++) {
472 socket_close(listen_sock[i].fd);
473 }
474
475 free(device_udid);
476 free(source_addr);
477
478 return 0;
479 }