summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--iphone.c162
-rw-r--r--usbmuxd.c61
2 files changed, 175 insertions, 48 deletions
diff --git a/iphone.c b/iphone.c
index 9035be9..bf0d5de 100644
--- a/iphone.c
+++ b/iphone.c
@@ -132,6 +132,61 @@ void log_debug_msg(const char *format, ...)
132#endif 132#endif
133} 133}
134 134
135#ifdef DEBUG
136/**
137 * for debugging purposes.
138 */
139static void print_buffer(const char *data, const int length)
140{
141 int i;
142 int j;
143 unsigned char c;
144
145 for(i=0; i<length; i+=16) {
146 printf("%04x: ", i);
147 for (j=0;j<16;j++) {
148 if (i+j >= length) {
149 printf(" ");
150 continue;
151 }
152 printf("%02hhx ", *(data+i+j));
153 }
154 printf(" | ");
155 for(j=0;j<16;j++) {
156 if (i+j >= length)
157 break;
158 c = *(data+i+j);
159 if ((c < 32) || (c > 127)) {
160 printf(".");
161 continue;
162 }
163 printf("%c", c);
164 }
165 printf("\n");
166 }
167 printf("\n");
168}
169#endif
170
171void hton_header(usbmux_tcp_header *hdr)
172{
173 if (hdr) {
174 hdr->length = htonl(hdr->length);
175 hdr->scnt = htonl(hdr->scnt);
176 hdr->ocnt = htonl(hdr->ocnt);
177 hdr->length16 = htons(hdr->length16);
178 }
179}
180
181void ntoh_header(usbmux_tcp_header *hdr)
182{
183 if (hdr) {
184 hdr->length = ntohl(hdr->length);
185 hdr->scnt = ntohl(hdr->scnt);
186 hdr->ocnt = ntohl(hdr->ocnt);
187 hdr->length16 = ntohs(hdr->length16);
188 }
189}
135 190
136/** Creates a USBMux header containing version information 191/** Creates a USBMux header containing version information
137 * 192 *
@@ -392,6 +447,12 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen)
392 int timeout = 1000; 447 int timeout = 1000;
393 int retrycount = 0; 448 int retrycount = 0;
394 int bytes = 0; 449 int bytes = 0;
450
451#ifdef DEBUG
452 printf("===============================\n%s: trying to send\n", __func__);
453 print_buffer(data, datalen);
454 printf("===============================\n");
455#endif
395 do { 456 do {
396 if (retrycount > 3) { 457 if (retrycount > 3) {
397 fprintf(stderr, "EPIC FAIL! aborting on retry count overload.\n"); 458 fprintf(stderr, "EPIC FAIL! aborting on retry count overload.\n");
@@ -426,6 +487,14 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen)
426 } 487 }
427 while(0); // fall out 488 while(0); // fall out
428 489
490#ifdef DEBUG
491 if (bytes > 0) {
492 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
493 printf("%s: sent to phone\n", __func__);
494 print_buffer(data, bytes);
495 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
496 }
497#endif
429 return bytes; 498 return bytes;
430} 499}
431 500
@@ -459,6 +528,15 @@ int recv_from_phone_timeout(iphone_device_t phone, char *data, int datalen, int
459 return -1; 528 return -1;
460 } 529 }
461 530
531#ifdef DEBUG
532 if (bytes > 0) {
533 printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
534 printf("%s: received from phone:\n", __func__);
535 print_buffer(data, bytes);
536 printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
537 }
538#endif
539
462 return bytes; 540 return bytes;
463} 541}
464 542
@@ -621,8 +699,8 @@ iphone_error_t iphone_mux_new_client(iphone_device_t device, uint16_t src_port,
621 // send TCP syn 699 // send TCP syn
622 if (new_connection && new_connection->header) { 700 if (new_connection && new_connection->header) {
623 new_connection->header->tcp_flags = TCP_SYN; 701 new_connection->header->tcp_flags = TCP_SYN;
624 new_connection->header->length = htonl(new_connection->header->length); 702 new_connection->header->length = new_connection->header->length;
625 new_connection->header->length16 = htons(new_connection->header->length16); 703 new_connection->header->length16 = new_connection->header->length16;
626 new_connection->header->scnt = 0; 704 new_connection->header->scnt = 0;
627 new_connection->header->ocnt = 0; 705 new_connection->header->ocnt = 0;
628 new_connection->phone = device; 706 new_connection->phone = device;
@@ -635,6 +713,8 @@ iphone_error_t iphone_mux_new_client(iphone_device_t device, uint16_t src_port,
635 new_connection->wr_window = 0; 713 new_connection->wr_window = 0;
636 add_connection(new_connection); 714 add_connection(new_connection);
637 new_connection->error = IPHONE_E_SUCCESS; 715 new_connection->error = IPHONE_E_SUCCESS;
716 hton_header(new_connection->header);
717 printf("%s: send_to_phone (%d --> %d)\n", __func__, ntohs(new_connection->header->sport), ntohs(new_connection->header->dport));
638 if (send_to_phone(device, (char *) new_connection->header, sizeof(usbmux_tcp_header)) >= 0) { 718 if (send_to_phone(device, (char *) new_connection->header, sizeof(usbmux_tcp_header)) >= 0) {
639 *client = new_connection; 719 *client = new_connection;
640 return IPHONE_E_SUCCESS; 720 return IPHONE_E_SUCCESS;
@@ -661,11 +741,10 @@ iphone_error_t iphone_mux_free_client(iphone_umux_client_t client)
661 741
662 pthread_mutex_lock(&client->mutex); 742 pthread_mutex_lock(&client->mutex);
663 client->header->tcp_flags = TCP_FIN; 743 client->header->tcp_flags = TCP_FIN;
664 client->header->length = htonl(0x1C); 744 client->header->length = 0x1C;
665 client->header->scnt = htonl(client->header->scnt);
666 client->header->ocnt = htonl(client->header->ocnt);
667 client->header->window = 0; 745 client->header->window = 0;
668 client->header->length16 = htons(0x1C); 746 client->header->length16 = 0x1C;
747 hton_header(client->header);
669 int bytes = 0; 748 int bytes = 0;
670 749
671 bytes = usb_bulk_write(client->phone->device, BULKOUT, (char *) client->header, sizeof(usbmux_tcp_header), 800); 750 bytes = usb_bulk_write(client->phone->device, BULKOUT, (char *) client->header, sizeof(usbmux_tcp_header), 800);
@@ -731,31 +810,29 @@ iphone_error_t iphone_mux_send(iphone_umux_client_t client, const char *data, ui
731 // client->scnt and client->ocnt should already be in host notation... 810 // client->scnt and client->ocnt should already be in host notation...
732 // we don't need to change them juuuust yet. 811 // we don't need to change them juuuust yet.
733 char *buffer = (char *) malloc(blocksize + 2); // allow 2 bytes of safety padding 812 char *buffer = (char *) malloc(blocksize + 2); // allow 2 bytes of safety padding
734 // Set the length and pre-emptively htonl/htons it 813 // Set the length
735 client->header->length = htonl(blocksize); 814 client->header->length = blocksize;
736 client->header->length16 = htons(blocksize); 815 client->header->length16 = blocksize;
737 816
738 // Put scnt and ocnt into big-endian notation 817 // Put header into big-endian notation
739 client->header->scnt = htonl(client->header->scnt); 818 hton_header(client->header);
740 client->header->ocnt = htonl(client->header->ocnt);
741 // Concatenation of stuff in the buffer. 819 // Concatenation of stuff in the buffer.
742 memcpy(buffer, client->header, sizeof(usbmux_tcp_header)); 820 memcpy(buffer, client->header, sizeof(usbmux_tcp_header));
743 memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen); 821 memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen);
744 822
823 printf("%s: send_to_phone(%d --> %d)\n", __func__, ntohs(client->header->sport), ntohs(client->header->dport));
745 sendresult = send_to_phone(client->phone, buffer, blocksize); 824 sendresult = send_to_phone(client->phone, buffer, blocksize);
746 // Now that we've sent it off, we can clean up after our sloppy selves. 825 // Now that we've sent it off, we can clean up after our sloppy selves.
747 if (buffer) 826 if (buffer)
748 free(buffer); 827 free(buffer);
749 828
829 // revert header fields that have been swapped before trying to send
830 ntoh_header(client->header);
831
750 // update counts ONLY if the send succeeded. 832 // update counts ONLY if the send succeeded.
751 if (sendresult == blocksize) { 833 if (sendresult == blocksize) {
752 // Re-calculate scnt and ocnt 834 // Re-calculate scnt
753 client->header->scnt = ntohl(client->header->scnt) + datalen; 835 client->header->scnt += datalen;
754 client->header->ocnt = ntohl(client->header->ocnt);
755 // Revert lengths
756 client->header->length = ntohl(client->header->length);
757 client->header->length16 = ntohs(client->header->length16);
758
759 client->wr_window -= blocksize; 836 client->wr_window -= blocksize;
760 } 837 }
761 838
@@ -812,20 +889,19 @@ uint32 append_receive_buffer(iphone_umux_client_t client, char* packet)
812 if (header->tcp_flags == (TCP_SYN | TCP_ACK)) { 889 if (header->tcp_flags == (TCP_SYN | TCP_ACK)) {
813 fprintf(stdout, "yes, got syn+ack ; replying with ack.\n"); 890 fprintf(stdout, "yes, got syn+ack ; replying with ack.\n");
814 client->header->tcp_flags = TCP_ACK; 891 client->header->tcp_flags = TCP_ACK;
815 client->header->length = htonl(sizeof(usbmux_tcp_header)); 892 client->header->length = sizeof(usbmux_tcp_header);
816 client->header->length16 = htons(sizeof(usbmux_tcp_header)); 893 client->header->length16 = sizeof(usbmux_tcp_header);
817 client->header->scnt = htonl(client->header->scnt + 1); 894 client->header->scnt += 1;
818 client->header->ocnt = header->ocnt; 895 client->header->ocnt = header->ocnt;
896 hton_header(client->header);
819 // push it to USB 897 // push it to USB
820 // TODO: need to check for error in the send here.... :( 898 // TODO: need to check for error in the send here.... :(
899 printf("%s: send_to_phone (%d --> %d)\n", __func__, ntohs(client->header->sport), ntohs(client->header->dport));
821 if (send_to_phone(client->phone, (char *)client->header, sizeof(usbmux_tcp_header)) <= 0) { 900 if (send_to_phone(client->phone, (char *)client->header, sizeof(usbmux_tcp_header)) <= 0) {
822 fprintf(stdout, "%s: error when pushing to usb...\n", __func__); 901 fprintf(stdout, "%s: error when pushing to usb...\n", __func__);
823 } 902 }
824 // need to revert some of the fields back to host notation. 903 // need to revert some of the fields back to host notation.
825 client->header->scnt = ntohl(client->header->scnt); 904 ntoh_header(client->header);
826 client->header->ocnt = ntohl(client->header->ocnt);
827 client->header->length = ntohl(client->header->length);
828 client->header->length16 = ntohs(client->header->length16);
829 } 905 }
830 else { 906 else {
831 client->error = IPHONE_E_ECONNABORTED; 907 client->error = IPHONE_E_ECONNABORTED;
@@ -843,7 +919,33 @@ uint32 append_receive_buffer(iphone_umux_client_t client, char* packet)
843 // larger number. 919 // larger number.
844 if (header->tcp_flags & TCP_RST) { 920 if (header->tcp_flags & TCP_RST) {
845 client->error = IPHONE_E_ECONNRESET; 921 client->error = IPHONE_E_ECONNRESET;
846 fprintf(stderr, "peer sent connection reset. setting error: %d\n", client->error); 922
923 if (datalen > 0) {
924 char e_msg[128];
925 e_msg[0] = 0;
926 if (datalen > 1) {
927 memcpy(e_msg, data+1, datalen-1);
928 e_msg[datalen-1] = 0;
929 }
930 // fetch the message
931 switch(data[0]) {
932 case 0:
933 // this is not an error, it's just a status message.
934 fprintf(stdout, "received status message: %s\n", e_msg);
935 datalen = 0;
936 break;
937 case 1:
938 fprintf(stderr, "received error message: %s\n", e_msg);
939 datalen = 0;
940 break;
941 default:
942 fprintf(stderr, "received unknown message (type 0x%02x): %s\n", data[0], e_msg);
943 //datalen = 0; // <-- we let this commented out for testing
944 break;
945 }
946 } else {
947 fprintf(stderr, "peer sent connection reset. setting error: %d\n", client->error);
948 }
847 } 949 }
848 950
849 // the packet's ocnt tells us how much of our data the device has received. 951 // the packet's ocnt tells us how much of our data the device has received.
@@ -954,7 +1056,7 @@ void iphone_mux_pullbulk(iphone_device_t phone)
954 readlen = 0; 1056 readlen = 0;
955 } 1057 }
956 if (readlen > 0) { 1058 if (readlen > 0) {
957 //fprintf(stdout, "recv_from_phone_timeout pulled an extra %d bytes\n", readlen); 1059 //fprintf(stdout, "recv_from_phone_timeout pulled an extra %d bytes\n", readlen);
958 } 1060 }
959 1061
960 // the amount of content we have to work with is the remainder plus 1062 // the amount of content we have to work with is the remainder plus
@@ -968,7 +1070,9 @@ void iphone_mux_pullbulk(iphone_device_t phone)
968 // check if there's even sufficient data to decode a header 1070 // check if there's even sufficient data to decode a header
969 if (usbReceive.leftover < HEADERLEN) break; 1071 if (usbReceive.leftover < HEADERLEN) break;
970 usbmux_tcp_header *header = (usbmux_tcp_header *) cursor; 1072 usbmux_tcp_header *header = (usbmux_tcp_header *) cursor;
971 1073
1074 printf("%s: recv_from_phone_timeout (%d --> %d)\n", __func__, ntohs(header->sport), ntohs(header->dport));
1075
972 // now that we have a header, check if there is sufficient data 1076 // now that we have a header, check if there is sufficient data
973 // to construct a full packet, including its data 1077 // to construct a full packet, including its data
974 uint32 packetlen = ntohl(header->length); 1078 uint32 packetlen = ntohl(header->length);
diff --git a/usbmuxd.c b/usbmuxd.c
index 8f2a6e9..0f4339c 100644
--- a/usbmuxd.c
+++ b/usbmuxd.c
@@ -47,6 +47,12 @@
47static int quit_flag = 0; 47static int quit_flag = 0;
48static int fsock = -1; 48static int fsock = -1;
49 49
50struct device_use_info {
51 uint32_t device_id;
52 iphone_device_t phone;
53 int use_count;
54};
55
50struct client_data { 56struct client_data {
51 volatile int dead; 57 volatile int dead;
52 int socket; 58 int socket;
@@ -58,12 +64,7 @@ struct client_data {
58 int reader_dead; 64 int reader_dead;
59 int handler_dead; 65 int handler_dead;
60 iphone_umux_client_t muxclient; 66 iphone_umux_client_t muxclient;
61}; 67 struct device_use_info *duinfo;
62
63struct device_use_info {
64 uint32_t device_id;
65 iphone_device_t phone;
66 int use_count;
67}; 68};
68 69
69static struct device_use_info **device_use_list = NULL; 70static struct device_use_info **device_use_list = NULL;
@@ -71,6 +72,18 @@ static int device_use_count = 0;
71static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER; 72static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER;
72 73
73/** 74/**
75 * mutex for mutual exclusion of calling the iphone_mux_send function
76 * TODO: I don't know if we really need this?
77 */
78static pthread_mutex_t writer_mutex = PTHREAD_MUTEX_INITIALIZER;
79
80/**
81 * mutex to keep the reader threads from reading partial packages
82 */
83static pthread_mutex_t reader_mutex = PTHREAD_MUTEX_INITIALIZER;
84
85#ifdef DEBUG
86/**
74 * for debugging purposes. 87 * for debugging purposes.
75 */ 88 */
76static void print_buffer(const char *data, const int length) 89static void print_buffer(const char *data, const int length)
@@ -103,6 +116,7 @@ static void print_buffer(const char *data, const int length)
103 } 116 }
104 printf("\n"); 117 printf("\n");
105} 118}
119#endif
106 120
107/** 121/**
108 * Read incoming usbmuxd packet. If the packet is larger than 122 * Read incoming usbmuxd packet. If the packet is larger than
@@ -192,7 +206,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
192 206
193 cdata->reader_dead = 0; 207 cdata->reader_dead = 0;
194 208
195 fprintf(stdout, "%s: started\n", __func__); 209 fprintf(stdout, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count);
196 210
197 while (!quit_flag && !cdata->reader_quit) { 211 while (!quit_flag && !cdata->reader_quit) {
198 result = check_fd(cdata->socket, fdwrite, DEFAULT_TIMEOUT); 212 result = check_fd(cdata->socket, fdwrite, DEFAULT_TIMEOUT);
@@ -204,9 +218,11 @@ static void *usbmuxd_client_reader_thread(void *arg)
204 } 218 }
205 219
206 rlen = 0; 220 rlen = 0;
221 //pthread_mutex_lock(&usbmux_mutex);
207 err = iphone_mux_recv_timeout(cdata->muxclient, rbuffer, rbuffersize, &rlen, DEFAULT_TIMEOUT); 222 err = iphone_mux_recv_timeout(cdata->muxclient, rbuffer, rbuffersize, &rlen, DEFAULT_TIMEOUT);
223 //pthread_mutex_unlock(&usbmux_mutex);
208 if (err != 0) { 224 if (err != 0) {
209 fprintf(stderr, "%s: encountered USB read error: %d\n", __func__, err); 225 fprintf(stderr, "%s[%d:%d]: encountered USB read error: %d\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, err);
210 break; 226 break;
211 } 227 }
212 228
@@ -221,7 +237,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
221 fsync(cdata->socket); 237 fsync(cdata->socket);
222 } 238 }
223 239
224 fprintf(stdout, "%s: terminated\n", __func__); 240 fprintf(stdout, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count);
225 241
226 cdata->reader_dead = 1; 242 cdata->reader_dead = 1;
227 243
@@ -268,7 +284,6 @@ static int usbmuxd_handleConnectResult(struct client_data *cdata)
268 return err; 284 return err;
269 } else { 285 } else {
270 if (rlen > 0) { 286 if (rlen > 0) {
271 //print_buffer(buffer, rlen);
272 if ((buffer[0] == 1) && (rlen > 20) && !memcmp(buffer+1, "handleConnectResult:", 20)) { 287 if ((buffer[0] == 1) && (rlen > 20) && !memcmp(buffer+1, "handleConnectResult:", 20)) {
273 // hm... we got an error message! 288 // hm... we got an error message!
274 buffer[rlen] = 0; 289 buffer[rlen] = 0;
@@ -319,7 +334,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
319 334
320 cdata = (struct client_data*)arg; 335 cdata = (struct client_data*)arg;
321 336
322 fprintf(stdout, "%s: started\n", __func__); 337 fprintf(stdout, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id,cdata->duinfo->use_count);
323 338
324 if (usbmuxd_handleConnectResult(cdata)) { 339 if (usbmuxd_handleConnectResult(cdata)) {
325 goto leave; 340 goto leave;
@@ -349,18 +364,20 @@ static void *usbmuxd_client_handler_thread(void *arg)
349 break; 364 break;
350 } 365 }
351 if (len < 0) { 366 if (len < 0) {
352 fprintf(stderr, "%s: Error: recv: %s\n", __func__, strerror(errno)); 367 fprintf(stderr, "%s[%d:%d]: Error: recv: %s\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, strerror(errno));
353 break; 368 break;
354 } 369 }
355 370
356 cursor = buffer; 371 cursor = buffer;
372
373 pthread_mutex_lock(&writer_mutex);
357 do { 374 do {
358 wlen = 0; 375 wlen = 0;
359 err = iphone_mux_send(cdata->muxclient, cursor, len, &wlen); 376 err = iphone_mux_send(cdata->muxclient, cursor, len, &wlen);
360 if (err == IPHONE_E_TIMEOUT) { 377 if (err == IPHONE_E_TIMEOUT) {
361 // some kind of timeout... just be patient and retry. 378 // some kind of timeout... just be patient and retry.
362 } else if (err != IPHONE_E_SUCCESS) { 379 } else if (err != IPHONE_E_SUCCESS) {
363 fprintf(stderr, "%s: USB write error: %d\n", __func__, err); 380 fprintf(stderr, "%s[%d:%d]: USB write error: %d\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, err);
364 len = -1; 381 len = -1;
365 break; 382 break;
366 } 383 }
@@ -370,6 +387,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
370 // advance cursor appropiately. 387 // advance cursor appropiately.
371 cursor += wlen; 388 cursor += wlen;
372 } while ((len > 0) && !quit_flag); 389 } while ((len > 0) && !quit_flag);
390 pthread_mutex_unlock(&writer_mutex);
373 if (len < 0) { 391 if (len < 0) {
374 break; 392 break;
375 } 393 }
@@ -377,7 +395,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
377 395
378leave: 396leave:
379 // cleanup 397 // cleanup
380 fprintf(stdout, "%s: terminating\n", __func__); 398 fprintf(stdout, "%s[%d:%d]: terminating\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count);
381 if (cdata->reader != 0) { 399 if (cdata->reader != 0) {
382 cdata->reader_quit = 1; 400 cdata->reader_quit = 1;
383 pthread_join(cdata->reader, NULL); 401 pthread_join(cdata->reader, NULL);
@@ -385,7 +403,7 @@ leave:
385 403
386 cdata->handler_dead = 1; 404 cdata->handler_dead = 1;
387 405
388 fprintf(stdout, "%s: terminated\n", __func__); 406 fprintf(stdout, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count);
389 return NULL; 407 return NULL;
390} 408}
391 409
@@ -472,7 +490,9 @@ static void *usbmuxd_client_init_thread(void *arg)
472 //pthread_mutex_unlock(&usbmux_mutex); 490 //pthread_mutex_unlock(&usbmux_mutex);
473 } 491 }
474 492
493#ifdef DEBUG
475 print_buffer((char*)&dev_info_req, sizeof(dev_info_req)); 494 print_buffer((char*)&dev_info_req, sizeof(dev_info_req));
495#endif
476 496
477 // send it 497 // send it
478 if (send_buf(cdata->socket, &dev_info_req, sizeof(dev_info_req)) <= 0) { 498 if (send_buf(cdata->socket, &dev_info_req, sizeof(dev_info_req)) <= 0) {
@@ -534,6 +554,8 @@ static void *usbmuxd_client_init_thread(void *arg)
534 cur_dev->device_id = c_req.device_id; 554 cur_dev->device_id = c_req.device_id;
535 cur_dev->phone = phone; 555 cur_dev->phone = phone;
536 556
557 fprintf(stdout, "%s: device_use_count = %d\n", __func__, device_use_count);
558
537 pthread_mutex_lock(&usbmux_mutex); 559 pthread_mutex_lock(&usbmux_mutex);
538 device_use_list = (struct device_use_info**)realloc(device_use_list, sizeof(struct device_use_info*) * (device_use_count+1)); 560 device_use_list = (struct device_use_info**)realloc(device_use_list, sizeof(struct device_use_info*) * (device_use_count+1));
539 if (device_use_list) { 561 if (device_use_list) {
@@ -559,6 +581,7 @@ static void *usbmuxd_client_init_thread(void *arg)
559 // start connection handler thread 581 // start connection handler thread
560 cdata->handler_dead = 0; 582 cdata->handler_dead = 0;
561 cdata->tag = c_req.header.tag; 583 cdata->tag = c_req.header.tag;
584 cdata->duinfo = cur_dev;
562 if (pthread_create(&cdata->handler, NULL, usbmuxd_client_handler_thread, cdata) != 0) { 585 if (pthread_create(&cdata->handler, NULL, usbmuxd_client_handler_thread, cdata) != 0) {
563 fprintf(stderr, "%s: could not create usbmuxd_client_handler_thread!\n", __func__); 586 fprintf(stderr, "%s: could not create usbmuxd_client_handler_thread!\n", __func__);
564 cdata->handler = 0; 587 cdata->handler = 0;
@@ -569,13 +592,12 @@ static void *usbmuxd_client_init_thread(void *arg)
569 592
570 // start reading data from the connected device 593 // start reading data from the connected device
571 while (!quit_flag && !cdata->handler_dead) { 594 while (!quit_flag && !cdata->handler_dead) {
595 pthread_mutex_lock(&reader_mutex);
572 iphone_mux_pullbulk(cur_dev->phone); 596 iphone_mux_pullbulk(cur_dev->phone);
573 err = iphone_mux_get_error(cdata->muxclient); 597 err = iphone_mux_get_error(cdata->muxclient);
598 pthread_mutex_unlock(&reader_mutex);
574 if (err != IPHONE_E_SUCCESS) { 599 if (err != IPHONE_E_SUCCESS) {
575 break; 600 break;
576 /*} else if (!sent_result) {
577 usbmuxd_send_result(cdata->socket, c_req.header.tag, 0);
578 sent_result = 1;*/
579 } 601 }
580 } 602 }
581 603
@@ -630,6 +652,7 @@ leave:
630 } 652 }
631 653
632 cdata->dead = 1; 654 cdata->dead = 1;
655 close(cdata->socket);
633 656
634 fprintf(stdout, "%s: terminated\n", __func__); 657 fprintf(stdout, "%s: terminated\n", __func__);
635 658
@@ -684,7 +707,7 @@ static void *usbmuxd_accept_thread(void *arg)
684 while (!quit_flag) { 707 while (!quit_flag) {
685 // Check the file descriptor before accepting a connection. 708 // Check the file descriptor before accepting a connection.
686 // If no connection attempt is made, just repeat... 709 // If no connection attempt is made, just repeat...
687 result = check_fd(fsock, fdread, DEFAULT_TIMEOUT); 710 result = check_fd(fsock, fdread, 1000);
688 if (result <= 0) { 711 if (result <= 0) {
689 if (result == 0) { 712 if (result == 0) {
690 // cleanup 713 // cleanup