summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore6
-rw-r--r--Makefile35
-rw-r--r--Readme.txt34
-rw-r--r--iproxy.c54
-rw-r--r--main.c (renamed from usbmuxd.c)48
-rw-r--r--sock_stuff.c8
-rw-r--r--sock_stuff.h6
-rw-r--r--testclient.c32
-rw-r--r--usbmuxd.h46
9 files changed, 157 insertions, 112 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..26cda67
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
1*~
2*.o
3.*.swp
4iproxy
5testclient
6usbmuxd
diff --git a/Makefile b/Makefile
index 04a36b3..2036ddc 100644
--- a/Makefile
+++ b/Makefile
@@ -1,26 +1,31 @@
1TARGET=usbmuxd 1TARGETS=usbmuxd iproxy testclient
2CFLAGS=-Wall 2CFLAGS=-Wall -g
3LDFLAGS=-lpthread -lusb -lrt 3LIBS=-lpthread -lusb -lrt
4LDFLAGS=
4 5
5objects = sock_stuff.o usbmuxd.o iphone.o 6all: $(TARGETS)
6 7
7all: $(TARGET) 8main.o: main.c usbmuxd.h sock_stuff.h iphone.h
9iphone.o: iproxy.c iphone.h usbmuxd.h sock_stuff.h
10sock_stuff.o: sock_stuff.c sock_stuff.h
11testclient.o: testclient.c sock_stuff.h
8 12
9%.o: %.c %.h 13%.o: %.c
10 $(CC) -o $@ $(CFLAGS) -c $< 14 $(CC) -o $@ $(CFLAGS) -c $<
11 15
12$(TARGET): $(objects) 16usbmuxd: main.o sock_stuff.o iphone.o
13 $(CC) -o $@ $(LDFLAGS) $^ 17 $(CC) -o $@ $(LDFLAGS) $^ $(LIBS)
18
19testclient: testclient.o sock_stuff.o
20 $(CC) -o $@ $(LDFLAGS) $^ $(LIBS)
21
22iproxy: iproxy.o sock_stuff.o
23 $(CC) -o $@ $(LDFLAGS) $^ $(LIBS)
14 24
15clean: 25clean:
16 rm -f *.o $(TARGET) 26 rm -f *.o $(TARGETS)
17 27
18realclean: clean 28realclean: clean
19 rm -f *~ 29 rm -f *~
20 30
21testclient: testclient.c sock_stuff.o 31.PHONY: all clean realclean
22 $(CC) $(LDFLAGS) -o testclient $(CFLAGS) $< sock_stuff.o
23
24iproxy: iproxy.c sock_stuff.o
25 $(CC) -lpthread -o iproxy $(CFLAGS) $< sock_stuff.o
26
diff --git a/Readme.txt b/Readme.txt
new file mode 100644
index 0000000..deece52
--- /dev/null
+++ b/Readme.txt
@@ -0,0 +1,34 @@
1Build
2=====
3
4 make
5
6Use
7===
8
9 sudo ./usbmuxd &
10 ./iproxy 2222 22 &
11 ssh -p 2222 root@localhost
12
13Muwahahaha. Hopefully you get the normal SSH login prompt.
14
15 Unfortunately, as of 2009-02-24 this only survives for a single
16 connection. You will have to restart the 'iproxy' part.
17
18SSH
19===
20
21If your iphone is rooted, but isn't running SSH, you will need to
22mount it with 'ifuse --afc2' (to access the root directory of the
23device).
24
25And then edit:
26
27 /Library/LaunchDaemons/com.openssh.sshd.plist
28
29to _remove_ the lines:
30
31 <key>Diabled</key>
32 <true/>
33
34Reboot the device and then sshd should be running.
diff --git a/iproxy.c b/iproxy.c
index a58365a..775b819 100644
--- a/iproxy.c
+++ b/iproxy.c
@@ -34,8 +34,6 @@
34#include "usbmuxd.h" 34#include "usbmuxd.h"
35#include "sock_stuff.h" 35#include "sock_stuff.h"
36 36
37#define SOCKET_FILE "/var/run/usbmuxd"
38
39static uint16_t listen_port = 0; 37static uint16_t listen_port = 0;
40static uint16_t device_port = 0; 38static uint16_t device_port = 0;
41 39
@@ -50,7 +48,7 @@ struct client_data {
50 48
51int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result) 49int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result)
52{ 50{
53 struct usbmux_result res; 51 struct usbmuxd_result res;
54 int recv_len; 52 int recv_len;
55 int i; 53 int i;
56 uint32_t rrr[5]; 54 uint32_t rrr[5];
@@ -71,7 +69,7 @@ int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result)
71 if ((recv_len == sizeof(res)) 69 if ((recv_len == sizeof(res))
72 && (res.header.length == recv_len) 70 && (res.header.length == recv_len)
73 && (res.header.reserved == 0) 71 && (res.header.reserved == 0)
74 && (res.header.type == usbmux_result) 72 && (res.header.type == USBMUXD_RESULT)
75 ) { 73 ) {
76 *result = res.result; 74 *result = res.result;
77 if (res.header.tag == tag) { 75 if (res.header.tag == tag) {
@@ -185,8 +183,8 @@ void *acceptor_thread(void *arg)
185 int connected; 183 int connected;
186 uint32_t pktlen; 184 uint32_t pktlen;
187 unsigned char *buf; 185 unsigned char *buf;
188 struct usbmux_header hello; 186 struct usbmuxd_hello hello;
189 struct usbmux_dev_info device_info; 187 struct usbmuxd_device_info device_info;
190 pthread_t ctos; 188 pthread_t ctos;
191 189
192 if (!arg) { 190 if (!arg) {
@@ -196,26 +194,26 @@ void *acceptor_thread(void *arg)
196 194
197 cdata = (struct client_data*)arg; 195 cdata = (struct client_data*)arg;
198 196
199 cdata->sfd = connect_unix_socket(SOCKET_FILE); 197 cdata->sfd = connect_unix_socket(USBMUXD_SOCKET_FILE);
200 if (cdata->sfd < 0) { 198 if (cdata->sfd < 0) {
201 printf("error opening socket, terminating.\n"); 199 printf("error opening socket, terminating.\n");
202 return NULL; 200 return NULL;
203 } 201 }
204 202
205 // send hello 203 // send hello
206 hello.length = sizeof(struct usbmux_header); 204 hello.header.length = sizeof(struct usbmuxd_hello);
207 hello.reserved = 0; 205 hello.header.reserved = 0;
208 hello.type = usbmux_hello; 206 hello.header.type = USBMUXD_HELLO;
209 hello.tag = 2; 207 hello.header.tag = 2;
210 208
211 hello_done = 0; 209 hello_done = 0;
212 connected = 0; 210 connected = 0;
213 211
214 fprintf(stdout, "sending Hello packet\n"); 212 fprintf(stdout, "sending Hello packet\n");
215 if (send(cdata->sfd, &hello, hello.length, 0) == hello.length) { 213 if (send(cdata->sfd, &hello, hello.header.length, 0) == hello.header.length) {
216 uint32_t res = -1; 214 uint32_t res = -1;
217 // get response 215 // get response
218 if (usbmuxd_get_result(cdata->sfd, hello.tag, &res) && (res==0)) { 216 if (usbmuxd_get_result(cdata->sfd, hello.header.tag, &res) && (res==0)) {
219 fprintf(stdout, "Got Hello Response!\n"); 217 fprintf(stdout, "Got Hello Response!\n");
220 hello_done = 1; 218 hello_done = 1;
221 } else { 219 } else {
@@ -241,7 +239,7 @@ void *acceptor_thread(void *arg)
241 } 239 }
242 fprintf(stdout, "Received device data\n"); 240 fprintf(stdout, "Received device data\n");
243 //log_debug_buffer(stdout, (char*)buf, pktlen); 241 //log_debug_buffer(stdout, (char*)buf, pktlen);
244 memcpy(&device_info, buf + sizeof(struct usbmux_header), sizeof(device_info)); 242 memcpy(&device_info, buf + sizeof(struct usbmuxd_header), sizeof(device_info));
245 free(buf); 243 free(buf);
246 } else { 244 } else {
247 // we _should_ have all of them now. 245 // we _should_ have all of them now.
@@ -252,17 +250,17 @@ void *acceptor_thread(void *arg)
252 } 250 }
253 251
254 if (device_info.device_id > 0) { 252 if (device_info.device_id > 0) {
255 struct usbmux_connect_request c_req; 253 struct usbmuxd_connect_request c_req;
256 254
257 fprintf(stdout, "Requesting connecion to device %d port %d\n", device_info.device_id, device_port); 255 fprintf(stdout, "Requesting connecion to device %d port %d\n", device_info.device_id, device_port);
258 256
259 // try to connect to last device found 257 // try to connect to last device found
260 c_req.header.length = sizeof(c_req); 258 c_req.header.length = sizeof(c_req);
261 c_req.header.reserved = 0; 259 c_req.header.reserved = 0;
262 c_req.header.type = usbmux_connect; 260 c_req.header.type = USBMUXD_CONNECT;
263 c_req.header.tag = 3; 261 c_req.header.tag = 3;
264 c_req.device_id = device_info.device_id; 262 c_req.device_id = device_info.device_id;
265 c_req.port = htons(device_port); 263 c_req.tcp_dport = htons(device_port);
266 c_req.reserved = 0; 264 c_req.reserved = 0;
267 265
268 if (send_buf(cdata->sfd, &c_req, sizeof(c_req)) < 0) { 266 if (send_buf(cdata->sfd, &c_req, sizeof(c_req)) < 0) {
@@ -346,26 +344,26 @@ int main(int argc, char **argv)
346 } 344 }
347 345
348/* 346/*
349 sfd = connect_unix_socket(SOCKET_FILE); 347 sfd = connect_unix_socket(USBMUXD_SOCKET_FILE);
350 if (sfd < 0) { 348 if (sfd < 0) {
351 printf("error opening socket, terminating.\n"); 349 printf("error opening socket, terminating.\n");
352 return -1; 350 return -1;
353 } 351 }
354 352
355 // send hello 353 // send hello
356 hello.length = sizeof(struct usbmux_header); 354 hello.header.length = sizeof(hello);
357 hello.reserved = 0; 355 hello.header.reserved = 0;
358 hello.type = usbmux_hello; 356 hello.header.type = USBMUXD_HELLO;
359 hello.tag = 2; 357 hello.header.tag = 2;
360 358
361 hello_done = 0; 359 hello_done = 0;
362 connected = 0; 360 connected = 0;
363 361
364 fprintf(stdout, "sending Hello packet\n"); 362 fprintf(stdout, "sending Hello packet\n");
365 if (send(sfd, &hello, hello.length, 0) == hello.length) { 363 if (send(sfd, &hello, hello.header.length, 0) == hello.header.length) {
366 uint32_t res = -1; 364 uint32_t res = -1;
367 // get response 365 // get response
368 if (usbmuxd_get_result(sfd, hello.tag, &res) && (res==0)) { 366 if (usbmuxd_get_result(sfd, hello.header.tag, &res) && (res==0)) {
369 fprintf(stdout, "Got Hello Response!\n"); 367 fprintf(stdout, "Got Hello Response!\n");
370 hello_done = 1; 368 hello_done = 1;
371 } else { 369 } else {
@@ -390,7 +388,7 @@ int main(int argc, char **argv)
390 } 388 }
391 fprintf(stdout, "Received device data\n"); 389 fprintf(stdout, "Received device data\n");
392 //log_debug_buffer(stdout, (char*)buf, pktlen); 390 //log_debug_buffer(stdout, (char*)buf, pktlen);
393 memcpy(&device_info, buf + sizeof(struct usbmux_header), sizeof(device_info)); 391 memcpy(&device_info, buf + sizeof(struct usbmuxd_header), sizeof(device_info));
394 free(buf); 392 free(buf);
395 } else { 393 } else {
396 // we _should_ have all of them now. 394 // we _should_ have all of them now.
@@ -401,17 +399,17 @@ int main(int argc, char **argv)
401 } 399 }
402 400
403 if (device_info.device_id > 0) { 401 if (device_info.device_id > 0) {
404 struct usbmux_connect_request c_req; 402 struct usbmuxd_connect_request c_req;
405 403
406 fprintf(stdout, "Requesting connecion to device %d port %d\n", device_info.device_id, device_port); 404 fprintf(stdout, "Requesting connecion to device %d port %d\n", device_info.device_id, device_port);
407 405
408 // try to connect to last device found 406 // try to connect to last device found
409 c_req.header.length = sizeof(c_req); 407 c_req.header.length = sizeof(c_req);
410 c_req.header.reserved = 0; 408 c_req.header.reserved = 0;
411 c_req.header.type = usbmux_connect; 409 c_req.header.type = USBMUXD_CONNECT;
412 c_req.header.tag = 3; 410 c_req.header.tag = 3;
413 c_req.device_id = device_info.device_id; 411 c_req.device_id = device_info.device_id;
414 c_req.port = htons(device_port); 412 c_req.tcp_dport = htons(device_port);
415 c_req.reserved = 0; 413 c_req.reserved = 0;
416 414
417 if (send_buf(sfd, &c_req, sizeof(c_req)) < 0) { 415 if (send_buf(sfd, &c_req, sizeof(c_req)) < 0) {
diff --git a/usbmuxd.c b/main.c
index 0f4339c..bf062b7 100644
--- a/usbmuxd.c
+++ b/main.c
@@ -39,8 +39,6 @@
39 39
40#include "iphone.h" 40#include "iphone.h"
41 41
42#define SOCKET_FILE "/var/run/usbmuxd"
43
44#define DEFAULT_TIMEOUT 4000 42#define DEFAULT_TIMEOUT 4000
45#define DEFAULT_CHILDREN_CAPACITY 10 43#define DEFAULT_CHILDREN_CAPACITY 10
46 44
@@ -163,11 +161,11 @@ static int usbmuxd_get_request(int fd, void *data, size_t len)
163 */ 161 */
164static int usbmuxd_send_result(int fd, uint32_t tag, uint32_t result_code) 162static int usbmuxd_send_result(int fd, uint32_t tag, uint32_t result_code)
165{ 163{
166 struct usbmux_result res; 164 struct usbmuxd_result res;
167 165
168 res.header.length = sizeof(res); 166 res.header.length = sizeof(res);
169 res.header.reserved = 0; 167 res.header.reserved = 0;
170 res.header.type = usbmux_result; 168 res.header.type = USBMUXD_RESULT;
171 res.header.tag = tag; 169 res.header.tag = tag;
172 res.result = result_code; 170 res.result = result_code;
173 171
@@ -209,7 +207,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
209 fprintf(stdout, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); 207 fprintf(stdout, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count);
210 208
211 while (!quit_flag && !cdata->reader_quit) { 209 while (!quit_flag && !cdata->reader_quit) {
212 result = check_fd(cdata->socket, fdwrite, DEFAULT_TIMEOUT); 210 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT);
213 if (result <= 0) { 211 if (result <= 0) {
214 if (result < 0) { 212 if (result < 0) {
215 fprintf(stderr, "%s: select error: %s\n", __func__, strerror(errno)); 213 fprintf(stderr, "%s: select error: %s\n", __func__, strerror(errno));
@@ -269,7 +267,7 @@ static int usbmuxd_handleConnectResult(struct client_data *cdata)
269 return -EINVAL; 267 return -EINVAL;
270 } 268 }
271 269
272 result = check_fd(cdata->socket, fdwrite, DEFAULT_TIMEOUT); 270 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT);
273 if (result <= 0) { 271 if (result <= 0) {
274 if (result < 0) { 272 if (result < 0) {
275 fprintf(stderr, "%s: select error: %s\n", __func__, strerror(errno)); 273 fprintf(stderr, "%s: select error: %s\n", __func__, strerror(errno));
@@ -349,7 +347,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
349 } 347 }
350 348
351 while (!quit_flag && !cdata->reader_dead) { 349 while (!quit_flag && !cdata->reader_dead) {
352 result = check_fd(cdata->socket, fdread, DEFAULT_TIMEOUT); 350 result = check_fd(cdata->socket, FD_READ, DEFAULT_TIMEOUT);
353 if (result <= 0) { 351 if (result <= 0) {
354 if (result < 0) { 352 if (result < 0) {
355 fprintf(stderr, "%s: Error: checkfd: %s\n", __func__, strerror(errno)); 353 fprintf(stderr, "%s: Error: checkfd: %s\n", __func__, strerror(errno));
@@ -415,9 +413,9 @@ leave:
415static void *usbmuxd_client_init_thread(void *arg) 413static void *usbmuxd_client_init_thread(void *arg)
416{ 414{
417 struct client_data *cdata; 415 struct client_data *cdata;
418 struct usbmux_header hello; 416 struct usbmuxd_hello hello;
419 struct usbmux_dev_info_request dev_info_req; 417 struct usbmuxd_device_info_request dev_info_req;
420 struct usbmux_connect_request c_req; 418 struct usbmuxd_connect_request c_req;
421 419
422 struct usb_bus *bus; 420 struct usb_bus *bus;
423 struct usb_device *dev; 421 struct usb_device *dev;
@@ -447,15 +445,15 @@ static void *usbmuxd_client_init_thread(void *arg)
447 goto leave; 445 goto leave;
448 } 446 }
449 447
450 if ((recv_len == 16) && (hello.length == 16) 448 if ((recv_len == sizeof(hello)) && (hello.header.length == sizeof(hello))
451 && (hello.reserved == 0) && (hello.type == usbmux_hello)) { 449 && (hello.header.reserved == 0) && (hello.header.type == USBMUXD_HELLO)) {
452 // send success response 450 // send success response
453 usbmuxd_send_result(cdata->socket, hello.tag, 0); 451 usbmuxd_send_result(cdata->socket, hello.header.tag, 0);
454 } else { 452 } else {
455 // send error response and exit 453 // send error response and exit
456 fprintf(stderr, "%s: Invalid Hello packet received.\n", __func__); 454 fprintf(stderr, "%s: Invalid Hello packet received.\n", __func__);
457 // TODO is this required?! 455 // TODO is this required?!
458 usbmuxd_send_result(cdata->socket, hello.tag, EINVAL); 456 usbmuxd_send_result(cdata->socket, hello.header.tag, EINVAL);
459 goto leave; 457 goto leave;
460 } 458 }
461 459
@@ -476,15 +474,15 @@ static void *usbmuxd_client_init_thread(void *arg)
476 // construct packet 474 // construct packet
477 memset(&dev_info_req, 0, sizeof(dev_info_req)); 475 memset(&dev_info_req, 0, sizeof(dev_info_req));
478 dev_info_req.header.length = sizeof(dev_info_req); 476 dev_info_req.header.length = sizeof(dev_info_req);
479 dev_info_req.header.type = usbmux_device_info; 477 dev_info_req.header.type = USBMUXD_DEVICE_INFO;
480 dev_info_req.dev_info.device_id = dev->devnum; 478 dev_info_req.device_info.device_id = dev->devnum;
481 dev_info_req.dev_info.product_id = dev->descriptor.idProduct; 479 dev_info_req.device_info.product_id = dev->descriptor.idProduct;
482 if (dev->descriptor.iSerialNumber) { 480 if (dev->descriptor.iSerialNumber) {
483 usb_dev_handle *udev; 481 usb_dev_handle *udev;
484 //pthread_mutex_lock(&usbmux_mutex); 482 //pthread_mutex_lock(&usbmux_mutex);
485 udev = usb_open(dev); 483 udev = usb_open(dev);
486 if (udev) { 484 if (udev) {
487 usb_get_string_simple(udev, dev->descriptor.iSerialNumber, dev_info_req.dev_info.serial_number, sizeof(dev_info_req.dev_info.serial_number)+1); 485 usb_get_string_simple(udev, dev->descriptor.iSerialNumber, dev_info_req.device_info.serial_number, sizeof(dev_info_req.device_info.serial_number)+1);
488 usb_close(udev); 486 usb_close(udev);
489 } 487 }
490 //pthread_mutex_unlock(&usbmux_mutex); 488 //pthread_mutex_unlock(&usbmux_mutex);
@@ -515,12 +513,12 @@ static void *usbmuxd_client_init_thread(void *arg)
515 goto leave; 513 goto leave;
516 } 514 }
517 515
518 if (c_req.header.type != usbmux_connect) { 516 if (c_req.header.type != USBMUXD_CONNECT) {
519 fprintf(stderr, "%s: Unexpected packet of type %d received.\n", __func__, c_req.header.type); 517 fprintf(stderr, "%s: Unexpected packet of type %d received.\n", __func__, c_req.header.type);
520 goto leave; 518 goto leave;
521 } 519 }
522 520
523 fprintf(stdout, "%s: Setting up connection to usb device #%d on port %d\n", __func__, c_req.device_id, ntohs(c_req.port)); 521 fprintf(stdout, "%s: Setting up connection to usb device #%d on port %d\n", __func__, c_req.device_id, ntohs(c_req.tcp_dport));
524 522
525 // find the device, and open usb connection 523 // find the device, and open usb connection
526 phone = NULL; 524 phone = NULL;
@@ -569,7 +567,7 @@ static void *usbmuxd_client_init_thread(void *arg)
569 567
570 // setup connection to iPhone/iPod 568 // setup connection to iPhone/iPod
571// pthread_mutex_lock(&usbmux_mutex); 569// pthread_mutex_lock(&usbmux_mutex);
572 res = iphone_mux_new_client(cur_dev->phone, 0, ntohs(c_req.port), &(cdata->muxclient)); 570 res = iphone_mux_new_client(cur_dev->phone, 0, ntohs(c_req.tcp_dport), &(cdata->muxclient));
573// pthread_mutex_unlock(&usbmux_mutex); 571// pthread_mutex_unlock(&usbmux_mutex);
574 572
575 if (res != 0) { 573 if (res != 0) {
@@ -707,7 +705,7 @@ static void *usbmuxd_accept_thread(void *arg)
707 while (!quit_flag) { 705 while (!quit_flag) {
708 // Check the file descriptor before accepting a connection. 706 // Check the file descriptor before accepting a connection.
709 // If no connection attempt is made, just repeat... 707 // If no connection attempt is made, just repeat...
710 result = check_fd(fsock, fdread, 1000); 708 result = check_fd(fsock, FD_READ, 1000);
711 if (result <= 0) { 709 if (result <= 0) {
712 if (result == 0) { 710 if (result == 0) {
713 // cleanup 711 // cleanup
@@ -814,13 +812,13 @@ int main(int argc, char **argv)
814 812
815 // TODO: Parameter checking. 813 // TODO: Parameter checking.
816 814
817 fsock = create_unix_socket(SOCKET_FILE); 815 fsock = create_unix_socket(USBMUXD_SOCKET_FILE);
818 if (fsock < 0) { 816 if (fsock < 0) {
819 fprintf(stderr, "Could not create socket, exiting\n"); 817 fprintf(stderr, "Could not create socket, exiting\n");
820 return -1; 818 return -1;
821 } 819 }
822 820
823 chmod(SOCKET_FILE, 0666); 821 chmod(USBMUXD_SOCKET_FILE, 0666);
824 822
825 if (!foreground) { 823 if (!foreground) {
826 if (daemonize() < 0) { 824 if (daemonize() < 0) {
@@ -848,7 +846,7 @@ int main(int argc, char **argv)
848 close(fsock); 846 close(fsock);
849 } 847 }
850 848
851 unlink(SOCKET_FILE); 849 unlink(USBMUXD_SOCKET_FILE);
852 850
853 return 0; 851 return 0;
854} 852}
diff --git a/sock_stuff.c b/sock_stuff.c
index 1a23bc1..3d11a27 100644
--- a/sock_stuff.c
+++ b/sock_stuff.c
@@ -208,13 +208,13 @@ int check_fd(int fd, fd_mode fdm, unsigned int timeout)
208 do { 208 do {
209 eagain = 0; 209 eagain = 0;
210 switch(fdm) { 210 switch(fdm) {
211 case fdread: 211 case FD_READ:
212 sret = select(fd+1,&fds,NULL,NULL,&to); 212 sret = select(fd+1,&fds,NULL,NULL,&to);
213 break; 213 break;
214 case fdwrite: 214 case FD_WRITE:
215 sret = select(fd+1,NULL,&fds,NULL,&to); 215 sret = select(fd+1,NULL,&fds,NULL,&to);
216 break; 216 break;
217 case fdexcept: 217 case FD_EXCEPT:
218 sret = select(fd+1,NULL,NULL,&fds,&to); 218 sret = select(fd+1,NULL,NULL,&fds,&to);
219 break; 219 break;
220 } 220 }
@@ -255,7 +255,7 @@ int recv_buf_timeout(int fd, void *data, size_t length, int flags, unsigned int
255 int result; 255 int result;
256 256
257 // check if data is available 257 // check if data is available
258 res = check_fd(fd, fdread, timeout); 258 res = check_fd(fd, FD_READ, timeout);
259 if (res <= 0) { 259 if (res <= 0) {
260 return res; 260 return res;
261 } 261 }
diff --git a/sock_stuff.h b/sock_stuff.h
index 01082d1..9965f4e 100644
--- a/sock_stuff.h
+++ b/sock_stuff.h
@@ -5,9 +5,9 @@
5 5
6enum fd_mode 6enum fd_mode
7{ 7{
8 fdread, 8 FD_READ,
9 fdwrite, 9 FD_WRITE,
10 fdexcept 10 FD_EXCEPT
11}; 11};
12typedef enum fd_mode fd_mode; 12typedef enum fd_mode fd_mode;
13 13
diff --git a/testclient.c b/testclient.c
index fafbf23..679b6d0 100644
--- a/testclient.c
+++ b/testclient.c
@@ -10,11 +10,9 @@
10#include "usbmuxd.h" 10#include "usbmuxd.h"
11#include "sock_stuff.h" 11#include "sock_stuff.h"
12 12
13#define SOCKET_FILE "/var/run/usbmuxd"
14
15int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result) 13int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result)
16{ 14{
17 struct usbmux_result res; 15 struct usbmuxd_result res;
18 int recv_len; 16 int recv_len;
19 17
20 if (!result) { 18 if (!result) {
@@ -28,7 +26,7 @@ int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t *result)
28 if ((recv_len == sizeof(res)) 26 if ((recv_len == sizeof(res))
29 && (res.header.length == recv_len) 27 && (res.header.length == recv_len)
30 && (res.header.reserved == 0) 28 && (res.header.reserved == 0)
31 && (res.header.type == usbmux_result) 29 && (res.header.type == USBMUXD_RESULT)
32 ) { 30 ) {
33 *result = res.result; 31 *result = res.result;
34 if (res.header.tag == tag) { 32 if (res.header.tag == tag) {
@@ -50,29 +48,29 @@ int main(int argc, char **argv)
50 int connected; 48 int connected;
51 uint32_t pktlen; 49 uint32_t pktlen;
52 unsigned char *buf; 50 unsigned char *buf;
53 struct usbmux_header hello; 51 struct usbmuxd_hello hello;
54 struct usbmux_dev_info device_info; 52 struct usbmuxd_device_info device_info;
55 53
56 sfd = connect_unix_socket(SOCKET_FILE); 54 sfd = connect_unix_socket(USBMUXD_SOCKET_FILE);
57 if (sfd < 0) { 55 if (sfd < 0) {
58 printf("error opening socket, terminating.\n"); 56 printf("error opening socket, terminating.\n");
59 return -1; 57 return -1;
60 } 58 }
61 59
62 // send hello 60 // send hello
63 hello.length = sizeof(struct usbmux_header); 61 hello.header.length = sizeof(struct usbmuxd_hello);
64 hello.reserved = 0; 62 hello.header.reserved = 0;
65 hello.type = usbmux_hello; 63 hello.header.type = USBMUXD_HELLO;
66 hello.tag = 2; 64 hello.header.tag = 2;
67 65
68 hello_done = 0; 66 hello_done = 0;
69 connected = 0; 67 connected = 0;
70 68
71 fprintf(stdout, "sending Hello packet\n"); 69 fprintf(stdout, "sending Hello packet\n");
72 if (send(sfd, &hello, hello.length, 0) == hello.length) { 70 if (send(sfd, &hello, hello.header.length, 0) == hello.header.length) {
73 uint32_t res = -1; 71 uint32_t res = -1;
74 // get response 72 // get response
75 if (usbmuxd_get_result(sfd, hello.tag, &res) && (res==0)) { 73 if (usbmuxd_get_result(sfd, hello.header.tag, &res) && (res==0)) {
76 fprintf(stdout, "Got Hello Response!\n"); 74 fprintf(stdout, "Got Hello Response!\n");
77 hello_done = 1; 75 hello_done = 1;
78 } else { 76 } else {
@@ -97,7 +95,7 @@ int main(int argc, char **argv)
97 } 95 }
98 fprintf(stdout, "got device data:\n"); 96 fprintf(stdout, "got device data:\n");
99 //log_debug_buffer(stdout, (char*)buf, pktlen); 97 //log_debug_buffer(stdout, (char*)buf, pktlen);
100 memcpy(&device_info, buf + sizeof(struct usbmux_header), sizeof(device_info)); 98 memcpy(&device_info, buf + sizeof(struct usbmuxd_header), sizeof(device_info));
101 free(buf); 99 free(buf);
102 } else { 100 } else {
103 // we _should_ have all of them now. 101 // we _should_ have all of them now.
@@ -108,15 +106,15 @@ int main(int argc, char **argv)
108 } 106 }
109 107
110 if (device_info.device_id > 0) { 108 if (device_info.device_id > 0) {
111 struct usbmux_connect_request c_req; 109 struct usbmuxd_connect_request c_req;
112 110
113 // try to connect to last device found 111 // try to connect to last device found
114 c_req.header.length = sizeof(c_req); 112 c_req.header.length = sizeof(c_req);
115 c_req.header.reserved = 0; 113 c_req.header.reserved = 0;
116 c_req.header.type = usbmux_connect; 114 c_req.header.type = USBMUXD_CONNECT;
117 c_req.header.tag = 3; 115 c_req.header.tag = 3;
118 c_req.device_id = device_info.device_id; 116 c_req.device_id = device_info.device_id;
119 c_req.port = htons(22); 117 c_req.tcp_dport = htons(22);
120 c_req.reserved = 0; 118 c_req.reserved = 0;
121 119
122 if (send_buf(sfd, &c_req, sizeof(c_req)) < 0) { 120 if (send_buf(sfd, &c_req, sizeof(c_req)) < 0) {
diff --git a/usbmuxd.h b/usbmuxd.h
index fcbee52..d749baf 100644
--- a/usbmuxd.h
+++ b/usbmuxd.h
@@ -3,42 +3,48 @@
3 3
4#include <stdint.h> 4#include <stdint.h>
5 5
6struct usbmux_header { 6#define USBMUXD_SOCKET_FILE "/var/run/usbmuxd"
7
8struct usbmuxd_header {
7 uint32_t length; // length of message, including header 9 uint32_t length; // length of message, including header
8 uint32_t reserved; // always zero 10 uint32_t reserved; // always zero
9 uint32_t type; // message type 11 uint32_t type; // message type
10 uint32_t tag; // responses to this query will echo back this tag 12 uint32_t tag; // responses to this query will echo back this tag
11}; 13} __attribute__((__packed__));
12 14
13struct usbmux_result { 15struct usbmuxd_result {
14 struct usbmux_header header; 16 struct usbmuxd_header header;
15 uint32_t result; 17 uint32_t result;
16}; 18} __attribute__((__packed__));
17 19
18struct usbmux_connect_request { 20struct usbmuxd_connect_request {
19 struct usbmux_header header; 21 struct usbmuxd_header header;
20 uint32_t device_id; 22 uint32_t device_id;
21 uint16_t port; // TCP port number 23 uint16_t tcp_dport; // TCP port number
22 uint16_t reserved; // set to zero 24 uint16_t reserved; // set to zero
23}; 25} __attribute__((__packed__));
24 26
25struct usbmux_dev_info { 27struct usbmuxd_device_info {
26 uint32_t device_id; 28 uint32_t device_id;
27 uint16_t product_id; 29 uint16_t product_id;
28 char serial_number[40]; 30 char serial_number[40];
29}; 31} __attribute__((__packed__));
30 32
31struct usbmux_dev_info_request { 33struct usbmuxd_device_info_request {
32 struct usbmux_header header; 34 struct usbmuxd_header header;
33 struct usbmux_dev_info dev_info; 35 struct usbmuxd_device_info device_info;
34 unsigned char padding[222]; 36 char padding[222];
35}; 37} __attribute__((__packed__));
38
39struct usbmuxd_hello {
40 struct usbmuxd_header header;
41} __attribute__((__packed__));
36 42
37enum { 43enum {
38 usbmux_result = 1, 44 USBMUXD_RESULT = 1,
39 usbmux_connect = 2, 45 USBMUXD_CONNECT = 2,
40 usbmux_hello = 3, 46 USBMUXD_HELLO = 3,
41 usbmux_device_info = 4, 47 USBMUXD_DEVICE_INFO = 4,
42}; 48};
43 49
44#endif 50#endif