summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main.c114
1 files changed, 69 insertions, 45 deletions
diff --git a/main.c b/main.c
index 2f08fec..ec9b007 100644
--- a/main.c
+++ b/main.c
@@ -122,7 +122,7 @@ static void print_buffer(const char *data, const int length)
122 * 122 *
123 * @return 123 * @return
124 */ 124 */
125static int usbmuxd_get_request(int fd, void *data, size_t len) 125static int usbmuxd_get_request(int fd, void **data, size_t len)
126{ 126{
127 uint32_t pktlen; 127 uint32_t pktlen;
128 int recv_len; 128 int recv_len;
@@ -131,17 +131,27 @@ static int usbmuxd_get_request(int fd, void *data, size_t len)
131 return -errno; 131 return -errno;
132 } 132 }
133 133
134 if (len < pktlen) { 134 if (len == 0) {
135 // allocate buffer space
136 *data = malloc(pktlen);
137 } else if (len < pktlen) {
135 // target buffer is to small to hold this packet! fix it! 138 // target buffer is to small to hold this packet! fix it!
136 fprintf(stderr, "%s: WARNING -- packet (%d) is larger than target buffer (%d)! Truncating.\n", __func__, pktlen, len); 139 fprintf(stderr, "%s: WARNING -- packet (%d) is larger than target buffer (%d)! Truncating.\n", __func__, pktlen, len);
137 pktlen = len; 140 pktlen = len;
138 } 141 }
139 142
140 recv_len = recv_buf(fd, data, pktlen); 143 recv_len = recv_buf(fd, *data, pktlen);
141 if ((recv_len > 0) && (recv_len < pktlen)) { 144 if ((recv_len > 0) && (recv_len < pktlen)) {
142 fprintf(stderr, "%s: Uh-oh, we got less than the packet's size, %d instead of %d...\n", __func__, recv_len, pktlen); 145 fprintf(stderr, "%s: Uh-oh, we got less than the packet's size, %d instead of %d...\n", __func__, recv_len, pktlen);
143 } 146 }
144 147
148#ifdef DEBUG
149 if (*data && (recv_len > 0)) {
150 fprintf(stderr, "%s: received:\n", __func__);
151 print_buffer(*data,recv_len);
152 }
153#endif
154
145 return recv_len; 155 return recv_len;
146} 156}
147 157
@@ -199,7 +209,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
199 209
200 cdata->reader_dead = 0; 210 cdata->reader_dead = 0;
201 211
202 fprintf(stdout, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); 212 fprintf(stderr, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count);
203 213
204 while (!quit_flag && !cdata->reader_quit) { 214 while (!quit_flag && !cdata->reader_quit) {
205 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT); 215 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT);
@@ -228,7 +238,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
228 fsync(cdata->socket); 238 fsync(cdata->socket);
229 } 239 }
230 240
231 fprintf(stdout, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); 241 fprintf(stderr, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count);
232 242
233 cdata->reader_dead = 1; 243 cdata->reader_dead = 1;
234 244
@@ -325,13 +335,13 @@ static void *usbmuxd_client_handler_thread(void *arg)
325 335
326 cdata = (struct client_data*)arg; 336 cdata = (struct client_data*)arg;
327 337
328 fprintf(stdout, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id,cdata->duinfo->use_count); 338 fprintf(stderr, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id,cdata->duinfo->use_count);
329 339
330 if (usbmuxd_handleConnectResult(cdata)) { 340 if (usbmuxd_handleConnectResult(cdata)) {
331 fprintf(stderr, "handleConnectResult: Error\n"); 341 fprintf(stderr, "handleConnectResult: Error\n");
332 goto leave; 342 goto leave;
333 } 343 }
334 fprintf(stdout, "handleConnectResult: Success\n"); 344 fprintf(stderr, "handleConnectResult: Success\n");
335 345
336 // starting mux reader thread 346 // starting mux reader thread
337 cdata->reader_quit = 0; 347 cdata->reader_quit = 0;
@@ -388,7 +398,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
388 398
389leave: 399leave:
390 // cleanup 400 // cleanup
391 fprintf(stdout, "%s[%d:%d]: terminating\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); 401 fprintf(stderr, "%s[%d:%d]: terminating\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count);
392 if (cdata->reader != 0) { 402 if (cdata->reader != 0) {
393 cdata->reader_quit = 1; 403 cdata->reader_quit = 1;
394 pthread_join(cdata->reader, NULL); 404 pthread_join(cdata->reader, NULL);
@@ -396,7 +406,7 @@ leave:
396 406
397 cdata->handler_dead = 1; 407 cdata->handler_dead = 1;
398 408
399 fprintf(stdout, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); 409 fprintf(stderr, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count);
400 return NULL; 410 return NULL;
401} 411}
402 412
@@ -446,9 +456,9 @@ static void *usbmuxd_bulk_reader_thread(void *arg)
446static void *usbmuxd_client_init_thread(void *arg) 456static void *usbmuxd_client_init_thread(void *arg)
447{ 457{
448 struct client_data *cdata; 458 struct client_data *cdata;
449 struct usbmuxd_hello hello; 459 struct usbmuxd_hello *hello = NULL;
450 struct usbmuxd_device_info_request dev_info_req; 460 struct usbmuxd_device_info_request dev_info_req;
451 struct usbmuxd_connect_request c_req; 461 struct usbmuxd_connect_request *c_req = NULL;
452 462
453 struct usb_bus *bus; 463 struct usb_bus *bus;
454 struct usb_device *dev; 464 struct usb_device *dev;
@@ -471,22 +481,27 @@ static void *usbmuxd_client_init_thread(void *arg)
471 cdata = (struct client_data*)arg; 481 cdata = (struct client_data*)arg;
472 cdata->dead = 0; 482 cdata->dead = 0;
473 483
474 fprintf(stdout, "%s: started (fd=%d)\n", __func__, cdata->socket); 484 fprintf(stderr, "%s: started (fd=%d)\n", __func__, cdata->socket);
475 485
476 if ((recv_len = usbmuxd_get_request(cdata->socket, &hello, sizeof(hello))) <= 0) { 486 if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&hello, 0)) <= 0) {
477 fprintf(stderr, "%s: No Hello packet received, error %s\n", __func__, strerror(errno)); 487 fprintf(stderr, "%s: No Hello packet received, error %s\n", __func__, strerror(errno));
478 goto leave; 488 goto leave;
479 } 489 }
480 490
481 if ((recv_len == sizeof(hello)) && (hello.header.length == sizeof(hello)) 491 if ((recv_len == sizeof(struct usbmuxd_hello)) && (hello->header.length == sizeof(struct usbmuxd_hello))
482 && (hello.header.reserved == 0) && (hello.header.type == USBMUXD_HELLO)) { 492 && (hello->header.reserved == 0) && (hello->header.type == USBMUXD_HELLO)) {
483 // send success response 493 // send success response
484 usbmuxd_send_result(cdata->socket, hello.header.tag, 0); 494 fprintf(stderr, "%s: Got Hello packet!\n", __func__);
495 usbmuxd_send_result(cdata->socket, hello->header.tag, 0);
496 } else if ((recv_len == sizeof(struct usbmuxd_connect_request)) && (hello->header.type == USBMUXD_CONNECT)) {
497 c_req = (struct usbmuxd_connect_request*)hello;
498 hello = NULL;
499 goto connect;
485 } else { 500 } else {
486 // send error response and exit 501 // send error response and exit
487 fprintf(stderr, "%s: Invalid Hello packet received.\n", __func__); 502 fprintf(stderr, "%s: Invalid Hello packet received.\n", __func__);
488 // TODO is this required?! 503 // TODO is this required?!
489 usbmuxd_send_result(cdata->socket, hello.header.tag, EINVAL); 504 usbmuxd_send_result(cdata->socket, hello->header.tag, EINVAL);
490 goto leave; 505 goto leave;
491 } 506 }
492 507
@@ -501,7 +516,7 @@ static void *usbmuxd_client_init_thread(void *arg)
501 && dev->descriptor.idProduct >= 0x1290 516 && dev->descriptor.idProduct >= 0x1290
502 && dev->descriptor.idProduct <= 0x1293) 517 && dev->descriptor.idProduct <= 0x1293)
503 { 518 {
504 fprintf(stdout, "%s: Found device on bus %d, id %d\n", __func__, bus->location, dev->devnum); 519 fprintf(stderr, "%s: Found device on bus %d, id %d\n", __func__, bus->location, dev->devnum);
505 found++; 520 found++;
506 521
507 // construct packet 522 // construct packet
@@ -540,18 +555,20 @@ static void *usbmuxd_client_init_thread(void *arg)
540 goto leave; 555 goto leave;
541 } 556 }
542 557
543 memset(&c_req, 0, sizeof(c_req)); 558 //memset(&c_req, 0, sizeof(c_req));
544 if ((recv_len = usbmuxd_get_request(cdata->socket, &c_req, sizeof(c_req))) <= 0) { 559 if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&c_req, 0)) <= 0) {
545 fprintf(stderr, "%s: Did not receive any connect request.\n", __func__); 560 fprintf(stderr, "%s: Did not receive any connect request.\n", __func__);
546 goto leave; 561 goto leave;
547 } 562 }
548 563
549 if (c_req.header.type != USBMUXD_CONNECT) { 564connect:
550 fprintf(stderr, "%s: Unexpected packet of type %d received.\n", __func__, c_req.header.type); 565
566 if (c_req->header.type != USBMUXD_CONNECT) {
567 fprintf(stderr, "%s: Unexpected packet of type %d received.\n", __func__, c_req->header.type);
551 goto leave; 568 goto leave;
552 } 569 }
553 570
554 fprintf(stdout, "%s: Setting up connection to usb device #%d on port %d\n", __func__, c_req.device_id, ntohs(c_req.tcp_dport)); 571 fprintf(stderr, "%s: Setting up connection to usb device #%d on port %d\n", __func__, c_req->device_id, ntohs(c_req->tcp_dport));
555 572
556 // find the device, and open usb connection 573 // find the device, and open usb connection
557 phone = NULL; 574 phone = NULL;
@@ -561,7 +578,7 @@ static void *usbmuxd_client_init_thread(void *arg)
561 pthread_mutex_lock(&usbmux_mutex); 578 pthread_mutex_lock(&usbmux_mutex);
562 for (i = 0; i < device_use_count; i++) { 579 for (i = 0; i < device_use_count; i++) {
563 if (device_use_list[i]) { 580 if (device_use_list[i]) {
564 if (device_use_list[i]->device_id == c_req.device_id) { 581 if (device_use_list[i]->device_id == c_req->device_id) {
565 device_use_list[i]->use_count++; 582 device_use_list[i]->use_count++;
566 cur_dev = device_use_list[i]; 583 cur_dev = device_use_list[i];
567 phone = cur_dev->phone; 584 phone = cur_dev->phone;
@@ -573,21 +590,21 @@ static void *usbmuxd_client_init_thread(void *arg)
573 } 590 }
574 if (!phone) { 591 if (!phone) {
575 // if not found, make a new connection 592 // if not found, make a new connection
576 if (iphone_get_specific_device(0, c_req.device_id, &phone) != IPHONE_E_SUCCESS) { 593 if (iphone_get_specific_device(0, c_req->device_id, &phone) != IPHONE_E_SUCCESS) {
577 fprintf(stderr, "%s: device_id %d could not be opened\n", __func__, c_req.device_id); 594 fprintf(stderr, "%s: device_id %d could not be opened\n", __func__, c_req->device_id);
578 usbmuxd_send_result(cdata->socket, c_req.header.tag, ENODEV); 595 usbmuxd_send_result(cdata->socket, c_req->header.tag, ENODEV);
579 goto leave; 596 goto leave;
580 } 597 }
581 // add to device list 598 // add to device list
582 cur_dev = (struct device_use_info*)malloc(sizeof(struct device_use_info)); 599 cur_dev = (struct device_use_info*)malloc(sizeof(struct device_use_info));
583 memset(cur_dev, 0, sizeof(struct device_use_info)); 600 memset(cur_dev, 0, sizeof(struct device_use_info));
584 cur_dev->use_count = 1; 601 cur_dev->use_count = 1;
585 cur_dev->device_id = c_req.device_id; 602 cur_dev->device_id = c_req->device_id;
586 cur_dev->phone = phone; 603 cur_dev->phone = phone;
587 pthread_mutex_init(&cur_dev->mutex, NULL); 604 pthread_mutex_init(&cur_dev->mutex, NULL);
588 pthread_mutex_init(&cur_dev->writer_mutex, NULL); 605 pthread_mutex_init(&cur_dev->writer_mutex, NULL);
589 606
590 fprintf(stdout, "%s: device_use_count = %d\n", __func__, device_use_count); 607 fprintf(stderr, "%s: device_use_count = %d\n", __func__, device_use_count);
591 pthread_create(&cur_dev->bulk_reader, NULL, usbmuxd_bulk_reader_thread, cur_dev); 608 pthread_create(&cur_dev->bulk_reader, NULL, usbmuxd_bulk_reader_thread, cur_dev);
592 609
593 pthread_mutex_lock(&usbmux_mutex); 610 pthread_mutex_lock(&usbmux_mutex);
@@ -598,23 +615,23 @@ static void *usbmuxd_client_init_thread(void *arg)
598 } 615 }
599 pthread_mutex_unlock(&usbmux_mutex); 616 pthread_mutex_unlock(&usbmux_mutex);
600 } else { 617 } else {
601 fprintf(stdout, "%s: reusing usb connection device_id %d\n", __func__, c_req.device_id); 618 fprintf(stderr, "%s: reusing usb connection device_id %d\n", __func__, c_req->device_id);
602 } 619 }
603 620
604 // setup connection to iPhone/iPod 621 // setup connection to iPhone/iPod
605// pthread_mutex_lock(&usbmux_mutex); 622// pthread_mutex_lock(&usbmux_mutex);
606 res = iphone_mux_new_client(cur_dev->phone, 0, ntohs(c_req.tcp_dport), &(cdata->muxclient)); 623 res = iphone_mux_new_client(cur_dev->phone, 0, ntohs(c_req->tcp_dport), &(cdata->muxclient));
607// pthread_mutex_unlock(&usbmux_mutex); 624// pthread_mutex_unlock(&usbmux_mutex);
608 625
609 if (res != 0) { 626 if (res != 0) {
610 usbmuxd_send_result(cdata->socket, c_req.header.tag, res); 627 usbmuxd_send_result(cdata->socket, c_req->header.tag, res);
611 fprintf(stderr, "%s: mux_new_client returned %d, aborting.\n", __func__, res); 628 fprintf(stderr, "%s: mux_new_client returned %d, aborting.\n", __func__, res);
612 goto leave; 629 goto leave;
613 } 630 }
614 631
615 // start connection handler thread 632 // start connection handler thread
616 cdata->handler_dead = 0; 633 cdata->handler_dead = 0;
617 cdata->tag = c_req.header.tag; 634 cdata->tag = c_req->header.tag;
618 cdata->duinfo = cur_dev; 635 cdata->duinfo = cur_dev;
619 if (pthread_create(&cdata->handler, NULL, usbmuxd_client_handler_thread, cdata) != 0) { 636 if (pthread_create(&cdata->handler, NULL, usbmuxd_client_handler_thread, cdata) != 0) {
620 fprintf(stderr, "%s: could not create usbmuxd_client_handler_thread!\n", __func__); 637 fprintf(stderr, "%s: could not create usbmuxd_client_handler_thread!\n", __func__);
@@ -643,14 +660,14 @@ static void *usbmuxd_client_init_thread(void *arg)
643 //usbmuxd_send_result(cdata->socket, c_req.header.tag, err); 660 //usbmuxd_send_result(cdata->socket, c_req.header.tag, err);
644 }*/ 661 }*/
645 662
646 //fprintf(stdout, "%s: terminating\n", __func__); 663 //fprintf(stderr, "%s: terminating\n", __func__);
647 664
648 // wait for handler thread to finish its work 665 // wait for handler thread to finish its work
649 if (cdata->handler != 0) { 666 if (cdata->handler != 0) {
650 pthread_join(cdata->handler, NULL); 667 pthread_join(cdata->handler, NULL);
651 } 668 }
652 669
653 fprintf(stdout, "%s: closing connection\n", __func__); 670 fprintf(stderr, "%s: closing connection\n", __func__);
654 671
655 // time to clean up 672 // time to clean up
656 if (cdata && cdata->muxclient) { // should be non-NULL 673 if (cdata && cdata->muxclient) { // should be non-NULL
@@ -658,7 +675,14 @@ static void *usbmuxd_client_init_thread(void *arg)
658 } 675 }
659 676
660leave: 677leave:
661 fprintf(stdout, "%s: terminating\n", __func__); 678 fprintf(stderr, "%s: terminating\n", __func__);
679
680 if (hello) {
681 free(hello);
682 }
683 if (c_req) {
684 free(c_req);
685 }
662 686
663 // this has to be freed only if it's not in use anymore as it closes 687 // this has to be freed only if it's not in use anymore as it closes
664 // the USB connection 688 // the USB connection
@@ -702,7 +726,7 @@ leave:
702 cdata->dead = 1; 726 cdata->dead = 1;
703 close(cdata->socket); 727 close(cdata->socket);
704 728
705 fprintf(stdout, "%s: terminated\n", __func__); 729 fprintf(stderr, "%s: terminated\n", __func__);
706 730
707 return NULL; 731 return NULL;
708} 732}
@@ -722,7 +746,7 @@ static int daemonize()
722static void clean_exit(int sig) 746static void clean_exit(int sig)
723{ 747{
724 if (sig == SIGINT) { 748 if (sig == SIGINT) {
725 fprintf(stdout, "CTRL+C pressed\n"); 749 fprintf(stderr, "CTRL+C pressed\n");
726 } 750 }
727 quit_flag = 1; 751 quit_flag = 1;
728} 752}
@@ -742,7 +766,7 @@ int main(int argc, char **argv)
742 int result = 0; 766 int result = 0;
743 int cnt = 0; 767 int cnt = 0;
744 768
745 fprintf(stdout, "usbmuxd: starting\n"); 769 fprintf(stderr, "usbmuxd: starting\n");
746 770
747 // TODO: Parameter checking. 771 // TODO: Parameter checking.
748 772
@@ -775,7 +799,7 @@ int main(int argc, char **argv)
775 } 799 }
776 memset(children, 0, sizeof(struct client_data*) * children_capacity); 800 memset(children, 0, sizeof(struct client_data*) * children_capacity);
777 801
778 fprintf(stdout, "usbmuxd: waiting for connection\n"); 802 fprintf(stderr, "usbmuxd: waiting for connection\n");
779 while (!quit_flag) { 803 while (!quit_flag) {
780 // Check the file descriptor before accepting a connection. 804 // Check the file descriptor before accepting a connection.
781 // If no connection attempt is made, just repeat... 805 // If no connection attempt is made, just repeat...
@@ -787,7 +811,7 @@ int main(int argc, char **argv)
787 if (children[i]) { 811 if (children[i]) {
788 if (children[i]->dead != 0) { 812 if (children[i]->dead != 0) {
789 pthread_join(children[i]->thread, NULL); 813 pthread_join(children[i]->thread, NULL);
790 fprintf(stdout, "usbmuxd: reclaimed client thread (fd=%d)\n", children[i]->socket); 814 fprintf(stderr, "usbmuxd: reclaimed client thread (fd=%d)\n", children[i]->socket);
791 free(children[i]); 815 free(children[i]);
792 children[i] = NULL; 816 children[i] = NULL;
793 cnt++; 817 cnt++;
@@ -830,7 +854,7 @@ int main(int argc, char **argv)
830 } 854 }
831 } 855 }
832 856
833 fprintf(stdout, "usbmuxd: new client connected (fd=%d)\n", cdata->socket); 857 fprintf(stderr, "usbmuxd: new client connected (fd=%d)\n", cdata->socket);
834 858
835 // create client thread: 859 // create client thread:
836 if (pthread_create(&cdata->thread, NULL, usbmuxd_client_init_thread, cdata) == 0) { 860 if (pthread_create(&cdata->thread, NULL, usbmuxd_client_init_thread, cdata) == 0) {
@@ -854,10 +878,10 @@ int main(int argc, char **argv)
854 } 878 }
855 } 879 }
856 880
857 fprintf(stdout, "usbmuxd: terminating\n"); 881 fprintf(stderr, "usbmuxd: terminating\n");
858 882
859 // preparing for shutdown: wait for child threads to terminate (if any) 883 // preparing for shutdown: wait for child threads to terminate (if any)
860 fprintf(stdout, "usbmuxd: waiting for child threads to terminate...\n"); 884 fprintf(stderr, "usbmuxd: waiting for child threads to terminate...\n");
861 for (i = 0; i < children_capacity; i++) { 885 for (i = 0; i < children_capacity; i++) {
862 if (children[i] != NULL) { 886 if (children[i] != NULL) {
863 pthread_join(children[i]->thread, NULL); 887 pthread_join(children[i]->thread, NULL);
@@ -876,7 +900,7 @@ int main(int argc, char **argv)
876 900
877 unlink(USBMUXD_SOCKET_FILE); 901 unlink(USBMUXD_SOCKET_FILE);
878 902
879 fprintf(stdout, "usbmuxd: terminated\n"); 903 fprintf(stderr, "usbmuxd: terminated\n");
880 904
881 return 0; 905 return 0;
882} 906}