Debian Codegraph @

tools/inetcat.c

raw
1 /*
2 * inetcat.c -- simple netcat-like tool that enables service access to iOS devices
3 *
4 * Copyright (C) 2017 Adrien Guinet <[email protected]>
5 *
6 * Based on iproxy which is based upon iTunnel source code, Copyright (c)
7 * 2008 Jing Su. http://www.cs.toronto.edu/~jingsu/itunnel/
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #define TOOL_NAME "inetcat"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include <stddef.h>
35 #include <errno.h>
36
37 #ifdef _WIN32
38 #include <winsock2.h>
39 #include <windows.h>
40 #ifndef _MSC_VER
41 #include <unistd.h>
42 #endif
43 #else
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <sys/select.h>
47 #include <sys/socket.h>
48 #include <sys/un.h>
49 #include <sys/ioctl.h>
50 #include <signal.h>
51 #endif
52
53 #ifdef _MSC_VER
54 #include <basetsd.h>
55 typedef SSIZE_T ssize_t;
56 #define STDIN_FILENO _fileno(stdin)
57 #define STDOUT_FILENO _fileno(stdout)
58 #endif
59
60 #include <getopt.h>
61 #include <libimobiledevice-glue/socket.h>
62 #include "usbmuxd.h"
63
64 static int debug_level = 0;
65
66 static size_t read_data_socket(int fd, uint8_t* buf, size_t bufsize)
67 {
68 #ifdef _WIN32
69 u_long bytesavailable = 0;
70 if (fd == STDIN_FILENO) {
71 bytesavailable = bufsize;
72 } else if (ioctlsocket(fd, FIONREAD, &bytesavailable) != 0) {
73 perror("ioctlsocket FIONREAD failed");
74 exit(1);
75 }
76 #else
77 size_t bytesavailable = 0;
78 if (ioctl(fd, FIONREAD, &bytesavailable) != 0) {
79 perror("ioctl FIONREAD failed");
80 exit(1);
81 }
82 #endif
83 size_t bufread = (bytesavailable >= bufsize) ? bufsize:bytesavailable;
84 ssize_t ret = read(fd, buf, bufread);
85 if (ret < 0) {
86 perror("read failed");
87 exit(1);
88 }
89 return (size_t)ret;
90 }
91
92 static void print_usage(int argc, char **argv, int is_error)
93 {
94 char *name = NULL;
95 name = strrchr(argv[0], '/');
96 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] DEVICE_PORT\n", (name ? name + 1: argv[0]));
97 fprintf(is_error ? stderr : stdout,
98 "\n" \
99 "Opens a read/write interface via STDIN/STDOUT to a TCP port on a usbmux device.\n" \
100 "\n" \
101 "OPTIONS:\n" \
102 " -u, --udid UDID target specific device by UDID\n" \
103 " -n, --network connect to network device\n" \
104 " -l, --local connect to USB device (default)\n" \
105 " -h, --help prints usage information\n" \
106 " -d, --debug increase debug level\n" \
107 " -v, --version prints version information\n" \
108 "\n" \
109 "Homepage: <" PACKAGE_URL ">\n"
110 "Bug Reports: <" PACKAGE_BUGREPORT ">\n"
111 );
112 }
113
114 int main(int argc, char **argv)
115 {
116 const struct option longopts[] = {
117 { "debug", no_argument, NULL, 'd' },
118 { "help", no_argument, NULL, 'h' },
119 { "udid", required_argument, NULL, 'u' },
120 { "local", no_argument, NULL, 'l' },
121 { "network", no_argument, NULL, 'n' },
122 { "version", no_argument, NULL, 'v' },
123 { NULL, 0, NULL, 0}
124 };
125
126 char* device_udid = NULL;
127 static enum usbmux_lookup_options lookup_opts = 0;
128
129 int c = 0;
130 while ((c = getopt_long(argc, argv, "dhu:lnv", longopts, NULL)) != -1) {
131 switch (c) {
132 case 'd':
133 libusbmuxd_set_debug_level(++debug_level);
134 break;
135 case 'u':
136 if (!*optarg) {
137 fprintf(stderr, "ERROR: UDID must not be empty!\n");
138 print_usage(argc, argv, 1);
139 return 2;
140 }
141 free(device_udid);
142 device_udid = strdup(optarg);
143 break;
144 case 'l':
145 lookup_opts |= DEVICE_LOOKUP_USBMUX;
146 break;
147 case 'n':
148 lookup_opts |= DEVICE_LOOKUP_NETWORK;
149 break;
150 case 'h':
151 print_usage(argc, argv, 0);
152 return 0;
153 case 'v':
154 printf("%s %s\n", TOOL_NAME, libusbmuxd_version());
155 return 0;
156 default:
157 print_usage(argc, argv, 1);
158 return 2;
159 }
160 }
161
162 if (lookup_opts == 0) {
163 lookup_opts = DEVICE_LOOKUP_USBMUX;
164 }
165
166 argc -= optind;
167 argv += optind;
168
169 if (argc < 1) {
170 print_usage(argc + optind, argv - optind, 1);
171 return 2;
172 }
173
174 int device_port = atoi(argv[0]);
175 if (!device_port) {
176 fprintf(stderr, "Invalid device_port specified!\n");
177 return -EINVAL;
178 }
179
180 #ifndef _WIN32
181 signal(SIGPIPE, SIG_IGN);
182 #endif
183
184 usbmuxd_device_info_t *dev_list = NULL;
185 usbmuxd_device_info_t *dev = NULL;
186 usbmuxd_device_info_t muxdev;
187
188 if (device_udid) {
189 if (usbmuxd_get_device(device_udid, &muxdev, lookup_opts) > 0) {
190 dev = &muxdev;
191 }
192 } else {
193 int count;
194 if ((count = usbmuxd_get_device_list(&dev_list)) < 0) {
195 printf("Connecting to usbmuxd failed, terminating.\n");
196 free(dev_list);
197 return 1;
198 }
199
200 if (dev_list == NULL || dev_list[0].handle == 0) {
201 fprintf(stderr, "No connected device found, terminating.\n");
202 free(dev_list);
203 return 1;
204 }
205
206 int i;
207 for (i = 0; i < count; i++) {
208 if (dev_list[i].conn_type == CONNECTION_TYPE_USB && (lookup_opts & DEVICE_LOOKUP_USBMUX)) {
209 dev = &(dev_list[i]);
210 break;
211 }
212 if (dev_list[i].conn_type == CONNECTION_TYPE_NETWORK && (lookup_opts & DEVICE_LOOKUP_NETWORK)) {
213 dev = &(dev_list[i]);
214 break;
215 }
216 }
217 }
218
219 if (dev == NULL || dev->handle == 0) {
220 fprintf(stderr, "No connected/matching device found, disconnecting client.\n");
221 free(dev_list);
222 return 1;
223 }
224
225 int devfd = -1;
226 if (dev->conn_type == CONNECTION_TYPE_NETWORK) {
227 unsigned char saddr_[32];
228 memset(saddr_, '\0', sizeof(saddr_));
229 struct sockaddr* saddr = (struct sockaddr*)&saddr_[0];
230 if (dev->conn_data[1] == 0x02) { // AF_INET
231 saddr->sa_family = AF_INET;
232 memcpy(&saddr->sa_data[0], (uint8_t*)dev->conn_data+2, 14);
233 }
234 else if (dev->conn_data[1] == 0x1E) { //AF_INET6 (bsd)
235 #ifdef AF_INET6
236 saddr->sa_family = AF_INET6;
237 memcpy(&saddr->sa_data[0], (uint8_t*)dev->conn_data+2, 26);
238 #else
239 fprintf(stderr, "ERROR: Got an IPv6 address but this system doesn't support IPv6\n");
240 free(dev_list);
241 return 1;
242 #endif
243 }
244 else {
245 fprintf(stderr, "Unsupported address family 0x%02x\n", dev->conn_data[1]);
246 free(dev_list);
247 return 1;
248 }
249 char addrtxt[48];
250 addrtxt[0] = '\0';
251 if (!socket_addr_to_string(saddr, addrtxt, sizeof(addrtxt))) {
252 fprintf(stderr, "Failed to convert network address: %d (%s)\n", errno, strerror(errno));
253 }
254 devfd = socket_connect_addr(saddr, device_port);
255 if (devfd < 0) {
256 devfd = -errno;
257 }
258 } else if (dev->conn_type == CONNECTION_TYPE_USB) {
259 devfd = usbmuxd_connect(dev->handle, device_port);
260 }
261 free(dev_list);
262 if (devfd < 0) {
263 fprintf(stderr, "Error connecting to device: %s\n", strerror(-devfd));
264 return 1;
265 }
266
267 fd_set fds;
268 FD_ZERO(&fds);
269 FD_SET(STDIN_FILENO, &fds);
270 FD_SET(devfd, &fds);
271
272 int ret = 0;
273 uint8_t buf[4096];
274 while (1) {
275 fd_set read_fds = fds;
276 int ret_sel = select(devfd+1, &read_fds, NULL, NULL, NULL);
277 if (ret_sel < 0) {
278 perror("select");
279 ret = 1;
280 break;
281 }
282
283 if (FD_ISSET(STDIN_FILENO, &read_fds)) {
284 size_t n = read_data_socket(STDIN_FILENO, buf, sizeof(buf));
285 if (n == 0) {
286 break;
287 }
288 write(devfd, buf, n);
289 }
290
291 if (FD_ISSET(devfd, &read_fds)) {
292 size_t n = read_data_socket(devfd, buf, sizeof(buf));
293 if (n == 0) {
294 break;
295 }
296 write(STDOUT_FILENO, buf, n);
297 }
298 }
299
300 socket_close(devfd);
301 return ret;
302 }