summaryrefslogtreecommitdiffstats
path: root/iproxy.c
blob: d88b7ff9720fd68a21aa5997c6e19a61c5035a79 (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
/*
 * iproxy -- proxy that enables tcp service access to iPhone/iPod
 *  via USB cable
 * TODO: improve code...
 * 
 * Copyright (c) 2009 Nikias Bassen. All Rights Reserved.
 * Based upon iTunnel source code, Copyright (c) 2008 Jing Su.
 *  http://www.cs.toronto.edu/~jingsu/itunnel/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stddef.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "usbmuxd.h"
#include "sock_stuff.h"

#define SOCKET_FILE "/var/run/usbmuxd"

static uint16_t listen_port = 0;
static uint16_t device_port = 0;

pthread_mutex_t smutex = PTHREAD_MUTEX_INITIALIZER;

struct client_data {
    int fd;
    int sfd;
    volatile int stop_ctos;
    volatile int stop_stoc;
};

int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result)
{
    struct usbmux_result res;
    int recv_len;
    int i;
    uint32_t rrr[5];

    if (!result) {
	return -EINVAL;
    }

    if ((recv_len = recv_buf(sfd, &res, sizeof(res))) <= 0) {
	perror("recv");
	return -errno;
    } else {
	memcpy(&rrr, &res, recv_len);
	for (i = 0; i < recv_len/4; i++) {
	    fprintf(stderr, "%08x ", rrr[i]);
	}
	fprintf(stderr, "\n");
	if ((recv_len == sizeof(res))
	    && (res.header.length == recv_len)
	    && (res.header.reserved == 0)
	    && (res.header.type == usbmux_result)
	   ) {
	    *result = res.result;
	    if (res.header.tag == tag) {
		return 1;
	    } else {
		return 0;
	    }
        }
    }

    return -1;
}

void *run_stoc_loop(void *arg)
{
    struct client_data *cdata = (struct client_data*)arg;
    int recv_len;
    int sent;
    char buffer[131072];

    printf("%s: fd = %d\n", __func__, cdata->fd);

    while (!cdata->stop_stoc && cdata->fd>0 && cdata->sfd>0) {
	recv_len = recv_buf_timeout(cdata->sfd, buffer, sizeof(buffer), 0, 5000);
	if (recv_len <= 0) {
	    if (recv_len == 0) {
		// try again
		continue;
	    } else {
		fprintf(stderr, "recv failed: %s\n", strerror(errno));
		break;
	    }
	} else {
	    printf("received %d bytes from server\n", recv_len);
	    // send to socket
	    sent = send_buf(cdata->fd, buffer, recv_len);
	    if (sent < recv_len) {
		if (sent <= 0) {
		    fprintf(stderr, "send failed: %s\n", strerror(errno));
		    break;
		} else {
		    fprintf(stderr, "only sent %d from %d bytes\n", sent, recv_len);
		}
	    } else {
		// sending succeeded, receive from device
		printf("pushed %d bytes to client\n", sent);
	    }
	}
    }
    close(cdata->fd);
    cdata->fd = -1;
    cdata->stop_ctos = 1;

    return NULL;
}

void *run_ctos_loop(void *arg)
{
    struct client_data *cdata = (struct client_data*)arg;
    int recv_len;
    int sent;
    char buffer[131072];
    pthread_t stoc = 0;

    printf("%s: fd = %d\n", __func__, cdata->fd);

    cdata->stop_stoc = 0;
    pthread_create(&stoc, NULL, run_stoc_loop, cdata);

    while (!cdata->stop_ctos && cdata->fd>0 && cdata->sfd>0) {
	recv_len = recv_buf_timeout(cdata->fd, buffer, sizeof(buffer), 0, 5000);
	if (recv_len <= 0) {
	    if (recv_len == 0) {
		// try again
		continue;
	    } else {
		fprintf(stderr, "recv failed: %s\n", strerror(errno));
		break;
	    }
	} else {
	    printf("pulled %d bytes from client\n", recv_len);
	    // send to local socket
	    sent = send_buf(cdata->sfd, buffer, recv_len);
	    if (sent < recv_len) {
		if (sent <= 0) {
		    fprintf(stderr, "send failed: %s\n", strerror(errno));
		    break;
		} else {
		    fprintf(stderr, "only sent %d from %d bytes\n", sent, recv_len);
		}
	    } else {
		// sending succeeded, receive from device
		printf("sent %d bytes to server\n", sent);
	    }
	}
    }
    close(cdata->fd);
    cdata->fd = -1;
    cdata->stop_stoc = 1;

    pthread_join(stoc, NULL);

    return NULL;
}

void *acceptor_thread(void *arg)
{
    struct client_data *cdata;
    int recv_len = 0;
    int hello_done;
    int connected;
    uint32_t pktlen;
    unsigned char *buf;
    struct usbmux_header hello;
    struct usbmux_dev_info device_info;
    pthread_t ctos;

    if (!arg) {
	fprintf(stderr, "invalid client_data provided!\n");
	return NULL;
    }

    cdata = (struct client_data*)arg;

    cdata->sfd = connect_unix_socket(SOCKET_FILE);
    if (cdata->sfd < 0) {
	printf("error opening socket, terminating.\n");
	return NULL;
    }

    // send hello
    hello.length = sizeof(struct usbmux_header);
    hello.reserved = 0;
    hello.type = usbmux_hello;
    hello.tag = 2;

    hello_done = 0;
    connected = 0;

    fprintf(stdout, "sending Hello packet\n");
    if (send(cdata->sfd, &hello, hello.length, 0) == hello.length) {
	uint32_t res = -1;
	// get response
	if (usbmuxd_get_result(cdata->sfd, hello.tag, &res) && (res==0)) {
	    fprintf(stdout, "Got Hello Response!\n");
	    hello_done = 1;
	} else {
	    fprintf(stderr, "Did not get Hello response (with result=0)...\n");
	    close(cdata->sfd);
	    cdata->sfd = -1;
	    return NULL;
	}

	device_info.device_id = 0;

	if (hello_done) {
	    // get all devices
	    while (1) {
		if (recv_buf_timeout(cdata->sfd, &pktlen, 4, MSG_PEEK, 1000) == 4) {
		    buf = (unsigned char*)malloc(pktlen);
		    if (!buf) {
			exit(-ENOMEM);
		    }
		    recv_len = recv_buf(cdata->sfd, buf, pktlen);
		    if (recv_len < pktlen) {
			fprintf(stdout, "received less data than specified in header!\n");
		    }
		    fprintf(stdout, "Received device data\n");
		    //log_debug_buffer(stdout, (char*)buf, pktlen);
		    memcpy(&device_info, buf + sizeof(struct usbmux_header), sizeof(device_info));
		    free(buf);
		} else {
		    // we _should_ have all of them now.
		    // or perhaps an error occured.
		    break;
		}
	    }
	}

	if (device_info.device_id > 0) {
	    struct usbmux_connect_request c_req;

	    fprintf(stdout, "Requesting connecion to device %d port %d\n", device_info.device_id, device_port);

	    // try to connect to last device found
	    c_req.header.length = sizeof(c_req);
	    c_req.header.reserved = 0;
	    c_req.header.type = usbmux_connect;
	    c_req.header.tag = 3;
	    c_req.device_id = device_info.device_id;
	    c_req.port = htons(device_port);
	    c_req.reserved = 0;

	    if (send_buf(cdata->sfd, &c_req, sizeof(c_req)) < 0) {
		perror("send");
	    } else {
		// read ACK
		res = -1;
		fprintf(stdout, "Reading connect result...\n");
		if (usbmuxd_get_result(cdata->sfd, c_req.header.tag, &res)) {
		    if (res == 0) {
			fprintf(stdout, "Connect success!\n");
			connected = 1;
		    } else {
			fprintf(stderr, "Connect failed, Error code=%d\n", res);
		    }
		}
	    }
	}

	if (connected) {
		cdata->stop_ctos = 0;
		pthread_create(&ctos, NULL, run_ctos_loop, cdata);
		pthread_join(ctos, NULL);
	} else {
	    fprintf(stderr, "Error connecting to device!\n");
	}
    }
    close(cdata->sfd);

    return NULL;
}

int main(int argc, char **argv)
{
    int mysock = -1;

    if (argc != 3) {
	printf("usage: %s LOCAL_PORT DEVICE_PORT\n", argv[0]);
	return 0;
    }

    listen_port = atoi(argv[1]);
    device_port = atoi(argv[2]);

    if (!listen_port) {
	fprintf(stderr, "Invalid listen_port specified!\n");
	return -EINVAL;
    }

    if (!device_port) {
	fprintf(stderr, "Invalid device_port specified!\n");
	return -EINVAL;
    }

    // first create the listening socket endpoint waiting for connections.
    mysock = create_socket(listen_port);
    if (mysock < 0) {
	fprintf(stderr, "Error creating socket: %s\n", strerror(errno));
	return -errno;
    } else {
	pthread_t acceptor;
	struct sockaddr_in c_addr;
	socklen_t len = sizeof(struct sockaddr_in);
	struct client_data cdata;
	int c_sock;
	while (1) {
	    printf("waiting for connection\n");
	    c_sock = accept(mysock, (struct sockaddr*)&c_addr, &len);
	    if (c_sock) {
		printf("accepted connection, fd = %d\n", c_sock);
		cdata.fd = c_sock;
		pthread_create(&acceptor, NULL, acceptor_thread, &cdata);
		pthread_join(acceptor, NULL);
	    } else {
		break;
	    }
	}
	close(c_sock);
	close(mysock);
    }

/*
    sfd = connect_unix_socket(SOCKET_FILE);
    if (sfd < 0) {
	printf("error opening socket, terminating.\n");
	return -1;
    }

    // send hello
    hello.length = sizeof(struct usbmux_header);
    hello.reserved = 0;
    hello.type = usbmux_hello;
    hello.tag = 2;

    hello_done = 0;
    connected = 0;

    fprintf(stdout, "sending Hello packet\n");
    if (send(sfd, &hello, hello.length, 0) == hello.length) {
	uint32_t res = -1;
	// get response
	if (usbmuxd_get_result(sfd, hello.tag, &res) && (res==0)) {
	    fprintf(stdout, "Got Hello Response!\n");
	    hello_done = 1;
	} else {
	    fprintf(stderr, "Did not get Hello response (with result=0)...\n");
	    close(sfd);
	    return -1;
	}

	device_info.device_id = 0;

	if (hello_done) {
	    // get all devices
	    while (1) {
		if (recv_buf_timeout(sfd, &pktlen, 4, MSG_PEEK, 1000) == 4) {
		    buf = (unsigned char*)malloc(pktlen);
		    if (!buf) {
			exit(-ENOMEM);
		    }
		    recv_len = recv_buf(sfd, buf, pktlen);
		    if (recv_len < pktlen) {
			fprintf(stdout, "received less data than specified in header!\n");
		    }
		    fprintf(stdout, "Received device data\n");
		    //log_debug_buffer(stdout, (char*)buf, pktlen);
		    memcpy(&device_info, buf + sizeof(struct usbmux_header), sizeof(device_info));
		    free(buf);
		} else {
		    // we _should_ have all of them now.
		    // or perhaps an error occured.
		    break;
		}
	    }
	}

	if (device_info.device_id > 0) {
	    struct usbmux_connect_request c_req;

	    fprintf(stdout, "Requesting connecion to device %d port %d\n", device_info.device_id, device_port);

	    // try to connect to last device found
	    c_req.header.length = sizeof(c_req);
	    c_req.header.reserved = 0;
	    c_req.header.type = usbmux_connect;
	    c_req.header.tag = 3;
	    c_req.device_id = device_info.device_id;
	    c_req.port = htons(device_port);
	    c_req.reserved = 0;

	    if (send_buf(sfd, &c_req, sizeof(c_req)) < 0) {
		perror("send");
	    } else {
		// read ACK
		res = -1;
		fprintf(stdout, "Reading connect result...\n");
		if (usbmuxd_get_result(sfd, c_req.header.tag, &res)) {
		    if (res == 0) {
			fprintf(stdout, "Connect success!\n");
			connected = 1;
		    } else {
			fprintf(stderr, "Connect failed, Error code=%d\n", res);
		    }
		}
	    }
	}

	if (connected) {
	    //
	} else {
	    fprintf(stderr, "No attached device found?!\n");
	}
    }
    close(sfd);
*/

    return 0;
}