summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/idevice.c83
1 files changed, 76 insertions, 7 deletions
diff --git a/src/idevice.c b/src/idevice.c
index 0a62907..418ee86 100644
--- a/src/idevice.c
+++ b/src/idevice.c
@@ -421,7 +421,7 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connect(idevice_t device, uint16_t
421 return IDEVICE_E_INVALID_ARG; 421 return IDEVICE_E_INVALID_ARG;
422 } 422 }
423 423
424 if (device->conn_type == CONNECTION_USBMUXD || device->conn_type == CONNECTION_NETWORK) { 424 if (device->conn_type == CONNECTION_USBMUXD) {
425 int sfd = usbmuxd_connect(device->mux_id, port); 425 int sfd = usbmuxd_connect(device->mux_id, port);
426 if (sfd < 0) { 426 if (sfd < 0) {
427 debug_info("ERROR: Connecting to usbmuxd failed: %d (%s)", sfd, strerror(-sfd)); 427 debug_info("ERROR: Connecting to usbmuxd failed: %d (%s)", sfd, strerror(-sfd));
@@ -434,6 +434,45 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connect(idevice_t device, uint16_t
434 new_connection->device = device; 434 new_connection->device = device;
435 *connection = new_connection; 435 *connection = new_connection;
436 return IDEVICE_E_SUCCESS; 436 return IDEVICE_E_SUCCESS;
437 } else if (device->conn_type == CONNECTION_NETWORK) {
438 unsigned char saddr_[32];
439 memset(saddr_, '\0', sizeof(saddr_));
440 struct sockaddr* saddr = (struct sockaddr*)&saddr_[0];
441 if (((char*)device->conn_data)[1] == 0x02) { // AF_INET
442 saddr->sa_family = AF_INET;
443 memcpy(&saddr->sa_data[0], (char*)device->conn_data+2, 14);
444 }
445 else if (((char*)device->conn_data)[1] == 0x1E) { //AF_INET6 (bsd)
446#ifdef AF_INET6
447 saddr->sa_family = AF_INET6;
448 memcpy(&saddr->sa_data[0], (char*)device->conn_data+2, 26);
449#else
450 debug_info("ERROR: Got an IPv6 address but this system doesn't support IPv6");
451 return IDEVICE_E_UNKNOWN_ERROR;
452#endif
453 }
454 else {
455 debug_info("Unsupported address family 0x%02x", ((char*)device->conn_data)[1]);
456 return IDEVICE_E_UNKNOWN_ERROR;
457 }
458 char addrtxt[48];
459 addrtxt[0] = '\0';
460 if (!socket_addr_to_string(saddr, addrtxt, sizeof(addrtxt))) {
461 debug_info("Failed to convert network address: %d (%s)", errno, strerror(errno));
462 }
463 debug_info("Connecting to %s port %d...", addrtxt, port);
464 int sfd = socket_connect_addr(saddr, port);
465 if (sfd < 0) {
466 debug_info("ERROR: Connecting to network device failed: %d (%s)", errno, strerror(errno));
467 return IDEVICE_E_NO_DEVICE;
468 }
469 idevice_connection_t new_connection = (idevice_connection_t)malloc(sizeof(struct idevice_connection_private));
470 new_connection->type = CONNECTION_NETWORK;
471 new_connection->data = (void*)(long)sfd;
472 new_connection->ssl_data = NULL;
473 new_connection->device = device;
474 *connection = new_connection;
475 return IDEVICE_E_SUCCESS;
437 } else { 476 } else {
438 debug_info("Unknown connection type %d", device->conn_type); 477 debug_info("Unknown connection type %d", device->conn_type);
439 } 478 }
@@ -451,10 +490,14 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_disconnect(idevice_connection_t con
451 idevice_connection_disable_ssl(connection); 490 idevice_connection_disable_ssl(connection);
452 } 491 }
453 idevice_error_t result = IDEVICE_E_UNKNOWN_ERROR; 492 idevice_error_t result = IDEVICE_E_UNKNOWN_ERROR;
454 if (connection->type == CONNECTION_USBMUXD || connection->type == CONNECTION_NETWORK) { 493 if (connection->type == CONNECTION_USBMUXD) {
455 usbmuxd_disconnect((int)(long)connection->data); 494 usbmuxd_disconnect((int)(long)connection->data);
456 connection->data = NULL; 495 connection->data = NULL;
457 result = IDEVICE_E_SUCCESS; 496 result = IDEVICE_E_SUCCESS;
497 } else if (connection->type == CONNECTION_NETWORK) {
498 socket_close((int)(long)connection->data);
499 connection->data = NULL;
500 result = IDEVICE_E_SUCCESS;
458 } else { 501 } else {
459 debug_info("Unknown connection type %d", connection->type); 502 debug_info("Unknown connection type %d", connection->type);
460 } 503 }
@@ -474,13 +517,21 @@ static idevice_error_t internal_connection_send(idevice_connection_t connection,
474 return IDEVICE_E_INVALID_ARG; 517 return IDEVICE_E_INVALID_ARG;
475 } 518 }
476 519
477 if (connection->type == CONNECTION_USBMUXD || connection->type == CONNECTION_NETWORK) { 520 if (connection->type == CONNECTION_USBMUXD) {
478 int res = usbmuxd_send((int)(long)connection->data, data, len, sent_bytes); 521 int res = usbmuxd_send((int)(long)connection->data, data, len, sent_bytes);
479 if (res < 0) { 522 if (res < 0) {
480 debug_info("ERROR: usbmuxd_send returned %d (%s)", res, strerror(-res)); 523 debug_info("ERROR: usbmuxd_send returned %d (%s)", res, strerror(-res));
481 return IDEVICE_E_UNKNOWN_ERROR; 524 return IDEVICE_E_UNKNOWN_ERROR;
482 } 525 }
483 return IDEVICE_E_SUCCESS; 526 return IDEVICE_E_SUCCESS;
527 } else if (connection->type == CONNECTION_NETWORK) {
528 int s = socket_send((int)(long)connection->data, (void*)data, len);
529 if (s < 0) {
530 *sent_bytes = 0;
531 return IDEVICE_E_UNKNOWN_ERROR;
532 }
533 *sent_bytes = s;
534 return IDEVICE_E_SUCCESS;
484 } else { 535 } else {
485 debug_info("Unknown connection type %d", connection->type); 536 debug_info("Unknown connection type %d", connection->type);
486 } 537 }
@@ -545,7 +596,7 @@ static idevice_error_t internal_connection_receive_timeout(idevice_connection_t
545 return IDEVICE_E_INVALID_ARG; 596 return IDEVICE_E_INVALID_ARG;
546 } 597 }
547 598
548 if (connection->type == CONNECTION_USBMUXD || connection->type == CONNECTION_NETWORK) { 599 if (connection->type == CONNECTION_USBMUXD) {
549 int conn_error = usbmuxd_recv_timeout((int)(long)connection->data, data, len, recv_bytes, timeout); 600 int conn_error = usbmuxd_recv_timeout((int)(long)connection->data, data, len, recv_bytes, timeout);
550 idevice_error_t error = socket_recv_to_idevice_error(conn_error, len, *recv_bytes); 601 idevice_error_t error = socket_recv_to_idevice_error(conn_error, len, *recv_bytes);
551 602
@@ -554,6 +605,14 @@ static idevice_error_t internal_connection_receive_timeout(idevice_connection_t
554 } 605 }
555 606
556 return error; 607 return error;
608 } else if (connection->type == CONNECTION_NETWORK) {
609 int res = socket_receive_timeout((int)(long)connection->data, data, len, 0, timeout);
610 if (res < 0) {
611 debug_info("ERROR: socket_receive_timeout failed: %d (%s)", res, strerror(-res));
612 return (res == -EAGAIN ? IDEVICE_E_NOT_ENOUGH_DATA : IDEVICE_E_UNKNOWN_ERROR);
613 }
614 *recv_bytes = (uint32_t)res;
615 return IDEVICE_E_SUCCESS;
557 } else { 616 } else {
558 debug_info("Unknown connection type %d", connection->type); 617 debug_info("Unknown connection type %d", connection->type);
559 } 618 }
@@ -630,13 +689,20 @@ static idevice_error_t internal_connection_receive(idevice_connection_t connecti
630 return IDEVICE_E_INVALID_ARG; 689 return IDEVICE_E_INVALID_ARG;
631 } 690 }
632 691
633 if (connection->type == CONNECTION_USBMUXD || connection->type == CONNECTION_NETWORK) { 692 if (connection->type == CONNECTION_USBMUXD) {
634 int res = usbmuxd_recv((int)(long)connection->data, data, len, recv_bytes); 693 int res = usbmuxd_recv((int)(long)connection->data, data, len, recv_bytes);
635 if (res < 0) { 694 if (res < 0) {
636 debug_info("ERROR: usbmuxd_recv returned %d (%s)", res, strerror(-res)); 695 debug_info("ERROR: usbmuxd_recv returned %d (%s)", res, strerror(-res));
637 return IDEVICE_E_UNKNOWN_ERROR; 696 return IDEVICE_E_UNKNOWN_ERROR;
638 } 697 }
639 698 return IDEVICE_E_SUCCESS;
699 } else if (connection->type == CONNECTION_NETWORK) {
700 int res = socket_receive((int)(long)connection->data, data, len);
701 if (res < 0) {
702 debug_info("ERROR: socket_receive returned %d (%s)", res, strerror(-res));
703 return IDEVICE_E_UNKNOWN_ERROR;
704 }
705 *recv_bytes = (uint32_t)res;
640 return IDEVICE_E_SUCCESS; 706 return IDEVICE_E_SUCCESS;
641 } else { 707 } else {
642 debug_info("Unknown connection type %d", connection->type); 708 debug_info("Unknown connection type %d", connection->type);
@@ -674,7 +740,10 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_get_fd(idevice_connectio
674 } 740 }
675 741
676 idevice_error_t result = IDEVICE_E_UNKNOWN_ERROR; 742 idevice_error_t result = IDEVICE_E_UNKNOWN_ERROR;
677 if (connection->type == CONNECTION_USBMUXD || connection->type == CONNECTION_NETWORK) { 743 if (connection->type == CONNECTION_USBMUXD) {
744 *fd = (int)(long)connection->data;
745 result = IDEVICE_E_SUCCESS;
746 } else if (connection->type == CONNECTION_NETWORK) {
678 *fd = (int)(long)connection->data; 747 *fd = (int)(long)connection->data;
679 result = IDEVICE_E_SUCCESS; 748 result = IDEVICE_E_SUCCESS;
680 } else { 749 } else {