diff options
Diffstat (limited to 'iphone.c')
| -rw-r--r-- | iphone.c | 72 |
1 files changed, 42 insertions, 30 deletions
| @@ -53,11 +53,17 @@ static const uint8 TCP_URG = 1 << 5; | |||
| 53 | static const uint32 WINDOW_MAX = 5 * 1024; | 53 | static const uint32 WINDOW_MAX = 5 * 1024; |
| 54 | static const uint32 WINDOW_INCREMENT = 512; | 54 | static const uint32 WINDOW_INCREMENT = 512; |
| 55 | 55 | ||
| 56 | typedef struct { | ||
| 57 | char* buffer; | ||
| 58 | int leftover; | ||
| 59 | int capacity; | ||
| 60 | } receivebuf_t; | ||
| 56 | 61 | ||
| 57 | struct iphone_device_int { | 62 | struct iphone_device_int { |
| 58 | char *buffer; | 63 | char *buffer; |
| 59 | struct usb_dev_handle *device; | 64 | struct usb_dev_handle *device; |
| 60 | struct usb_device *__device; | 65 | struct usb_device *__device; |
| 66 | receivebuf_t usbReceive; | ||
| 61 | }; | 67 | }; |
| 62 | 68 | ||
| 63 | typedef struct { | 69 | typedef struct { |
| @@ -100,17 +106,10 @@ struct iphone_umux_client_int { | |||
| 100 | }; | 106 | }; |
| 101 | 107 | ||
| 102 | 108 | ||
| 103 | typedef struct { | ||
| 104 | char* buffer; | ||
| 105 | int leftover; | ||
| 106 | int capacity; | ||
| 107 | } receivebuf_t; | ||
| 108 | |||
| 109 | |||
| 110 | static pthread_mutex_t iphonemutex = PTHREAD_MUTEX_INITIALIZER; | 109 | static pthread_mutex_t iphonemutex = PTHREAD_MUTEX_INITIALIZER; |
| 111 | static iphone_umux_client_t *connlist = NULL; | 110 | static iphone_umux_client_t *connlist = NULL; |
| 112 | static int clients = 0; | 111 | static int clients = 0; |
| 113 | static receivebuf_t usbReceive = {NULL, 0, 0}; | 112 | //static receivebuf_t usbReceive = {NULL, 0, 0}; |
| 114 | 113 | ||
| 115 | 114 | ||
| 116 | /** | 115 | /** |
| @@ -275,7 +274,7 @@ static iphone_error_t iphone_config_usb_device(iphone_device_t phone) | |||
| 275 | */ | 274 | */ |
| 276 | iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t * device) | 275 | iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t * device) |
| 277 | { | 276 | { |
| 278 | struct usb_bus *bus, *busses; | 277 | struct usb_bus *bus; |
| 279 | struct usb_device *dev; | 278 | struct usb_device *dev; |
| 280 | usbmux_version_header *version; | 279 | usbmux_version_header *version; |
| 281 | int bytes = 0; | 280 | int bytes = 0; |
| @@ -295,10 +294,9 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t | |||
| 295 | usb_init(); | 294 | usb_init(); |
| 296 | usb_find_busses(); | 295 | usb_find_busses(); |
| 297 | usb_find_devices(); | 296 | usb_find_devices(); |
| 298 | busses = usb_get_busses(); | ||
| 299 | 297 | ||
| 300 | // Set the device configuration | 298 | // Set the device configuration |
| 301 | for (bus = busses; bus; bus = bus->next) | 299 | for (bus = usb_get_busses(); bus; bus = bus->next) |
| 302 | if (bus->location == bus_n) | 300 | if (bus->location == bus_n) |
| 303 | for (dev = bus->devices; dev != NULL; dev = dev->next) | 301 | for (dev = bus->devices; dev != NULL; dev = dev->next) |
| 304 | if (dev->devnum == dev_n) { | 302 | if (dev->devnum == dev_n) { |
| @@ -417,6 +415,9 @@ iphone_error_t iphone_free_device(iphone_device_t device) | |||
| 417 | if (device->buffer) { | 415 | if (device->buffer) { |
| 418 | free(device->buffer); | 416 | free(device->buffer); |
| 419 | } | 417 | } |
| 418 | if (device->usbReceive.buffer) { | ||
| 419 | free(device->usbReceive.buffer); | ||
| 420 | } | ||
| 420 | if (device->device) { | 421 | if (device->device) { |
| 421 | usb_release_interface(device->device, 1); | 422 | usb_release_interface(device->device, 1); |
| 422 | usb_close(device->device); | 423 | usb_close(device->device); |
| @@ -586,10 +587,13 @@ usbmux_tcp_header *new_mux_packet(uint16 s_port, uint16 d_port) | |||
| 586 | */ | 587 | */ |
| 587 | static void delete_connection(iphone_umux_client_t connection) | 588 | static void delete_connection(iphone_umux_client_t connection) |
| 588 | { | 589 | { |
| 590 | iphone_umux_client_t *newlist = NULL; | ||
| 591 | |||
| 589 | pthread_mutex_lock(&iphonemutex); | 592 | pthread_mutex_lock(&iphonemutex); |
| 590 | 593 | ||
| 591 | // update the global list of connections | 594 | // update the global list of connections |
| 592 | iphone_umux_client_t *newlist = (iphone_umux_client_t *) malloc(sizeof(iphone_umux_client_t) * (clients - 1)); | 595 | if (clients > 1) { |
| 596 | newlist = (iphone_umux_client_t *) malloc(sizeof(iphone_umux_client_t) * (clients - 1)); | ||
| 593 | int i = 0, j = 0; | 597 | int i = 0, j = 0; |
| 594 | for (i = 0; i < clients; i++) { | 598 | for (i = 0; i < clients; i++) { |
| 595 | if (connlist[i] == connection) | 599 | if (connlist[i] == connection) |
| @@ -599,9 +603,12 @@ static void delete_connection(iphone_umux_client_t connection) | |||
| 599 | j++; | 603 | j++; |
| 600 | } | 604 | } |
| 601 | } | 605 | } |
| 606 | } | ||
| 607 | if (connlist) { | ||
| 602 | free(connlist); | 608 | free(connlist); |
| 603 | connlist = newlist; | 609 | } |
| 604 | clients--; | 610 | connlist = newlist; |
| 611 | clients--; | ||
| 605 | 612 | ||
| 606 | // free up this connection | 613 | // free up this connection |
| 607 | pthread_mutex_lock(&connection->mutex); | 614 | pthread_mutex_lock(&connection->mutex); |
| @@ -1043,17 +1050,22 @@ iphone_umux_client_t find_client(usbmux_tcp_header* recv_header) | |||
| 1043 | */ | 1050 | */ |
| 1044 | void iphone_mux_pullbulk(iphone_device_t phone) | 1051 | void iphone_mux_pullbulk(iphone_device_t phone) |
| 1045 | { | 1052 | { |
| 1053 | if (!phone) { | ||
| 1054 | fprintf(stderr, "iphone_mux_pullbulk: invalid argument\n"); | ||
| 1055 | return; | ||
| 1056 | } | ||
| 1057 | |||
| 1046 | static const int DEFAULT_CAPACITY = 128*1024; | 1058 | static const int DEFAULT_CAPACITY = 128*1024; |
| 1047 | if (usbReceive.buffer == NULL) { | 1059 | if (phone->usbReceive.buffer == NULL) { |
| 1048 | usbReceive.capacity = DEFAULT_CAPACITY; | 1060 | phone->usbReceive.capacity = DEFAULT_CAPACITY; |
| 1049 | usbReceive.buffer = malloc(usbReceive.capacity); | 1061 | phone->usbReceive.buffer = malloc(phone->usbReceive.capacity); |
| 1050 | usbReceive.leftover = 0; | 1062 | phone->usbReceive.leftover = 0; |
| 1051 | } | 1063 | } |
| 1052 | 1064 | ||
| 1053 | // start the cursor off just ahead of the leftover. | 1065 | // start the cursor off just ahead of the leftover. |
| 1054 | char* cursor = &usbReceive.buffer[usbReceive.leftover]; | 1066 | char* cursor = &phone->usbReceive.buffer[phone->usbReceive.leftover]; |
| 1055 | // pull in content, note that the amount we can pull is capacity minus leftover | 1067 | // pull in content, note that the amount we can pull is capacity minus leftover |
| 1056 | int readlen = recv_from_phone_timeout(phone, cursor, usbReceive.capacity - usbReceive.leftover, 5000); | 1068 | int readlen = recv_from_phone_timeout(phone, cursor, phone->usbReceive.capacity - phone->usbReceive.leftover, 5000); |
| 1057 | if (readlen < 0) { | 1069 | if (readlen < 0) { |
| 1058 | //fprintf(stderr, "recv_from_phone_timeout gave us an error.\n"); | 1070 | //fprintf(stderr, "recv_from_phone_timeout gave us an error.\n"); |
| 1059 | readlen = 0; | 1071 | readlen = 0; |
| @@ -1064,14 +1076,14 @@ void iphone_mux_pullbulk(iphone_device_t phone) | |||
| 1064 | 1076 | ||
| 1065 | // the amount of content we have to work with is the remainder plus | 1077 | // the amount of content we have to work with is the remainder plus |
| 1066 | // what we managed to read | 1078 | // what we managed to read |
| 1067 | usbReceive.leftover += readlen; | 1079 | phone->usbReceive.leftover += readlen; |
| 1068 | 1080 | ||
| 1069 | // reset the cursor to the front of that buffer and work through | 1081 | // reset the cursor to the front of that buffer and work through |
| 1070 | // trying to decode packets out of them. | 1082 | // trying to decode packets out of them. |
| 1071 | cursor = usbReceive.buffer; | 1083 | cursor = phone->usbReceive.buffer; |
| 1072 | while (1) { | 1084 | while (1) { |
| 1073 | // check if there's even sufficient data to decode a header | 1085 | // check if there's even sufficient data to decode a header |
| 1074 | if (usbReceive.leftover < HEADERLEN) break; | 1086 | if (phone->usbReceive.leftover < HEADERLEN) break; |
| 1075 | usbmux_tcp_header *header = (usbmux_tcp_header *) cursor; | 1087 | usbmux_tcp_header *header = (usbmux_tcp_header *) cursor; |
| 1076 | 1088 | ||
| 1077 | printf("%s: recv_from_phone_timeout (%d --> %d)\n", __func__, ntohs(header->sport), ntohs(header->dport)); | 1089 | printf("%s: recv_from_phone_timeout (%d --> %d)\n", __func__, ntohs(header->sport), ntohs(header->dport)); |
| @@ -1079,7 +1091,7 @@ void iphone_mux_pullbulk(iphone_device_t phone) | |||
| 1079 | // now that we have a header, check if there is sufficient data | 1091 | // now that we have a header, check if there is sufficient data |
| 1080 | // to construct a full packet, including its data | 1092 | // to construct a full packet, including its data |
| 1081 | uint32 packetlen = ntohl(header->length); | 1093 | uint32 packetlen = ntohl(header->length); |
| 1082 | if (usbReceive.leftover < packetlen) { | 1094 | if (phone->usbReceive.leftover < packetlen) { |
| 1083 | printf("%s: not enough data to construct a full packet\n", __func__); | 1095 | printf("%s: not enough data to construct a full packet\n", __func__); |
| 1084 | break; | 1096 | break; |
| 1085 | } | 1097 | } |
| @@ -1097,7 +1109,7 @@ void iphone_mux_pullbulk(iphone_device_t phone) | |||
| 1097 | 1109 | ||
| 1098 | // move the cursor and account for the consumption | 1110 | // move the cursor and account for the consumption |
| 1099 | cursor += packetlen; | 1111 | cursor += packetlen; |
| 1100 | usbReceive.leftover -= packetlen; | 1112 | phone->usbReceive.leftover -= packetlen; |
| 1101 | } | 1113 | } |
| 1102 | 1114 | ||
| 1103 | // now, we need to manage any leftovers. | 1115 | // now, we need to manage any leftovers. |
| @@ -1108,13 +1120,13 @@ void iphone_mux_pullbulk(iphone_device_t phone) | |||
| 1108 | // | 1120 | // |
| 1109 | // if there are no leftovers, we just leave the datastructure as is, | 1121 | // if there are no leftovers, we just leave the datastructure as is, |
| 1110 | // and re-use the block next time. | 1122 | // and re-use the block next time. |
| 1111 | if (usbReceive.leftover > 0 && cursor != usbReceive.buffer) { | 1123 | if (phone->usbReceive.leftover > 0 && cursor != phone->usbReceive.buffer) { |
| 1112 | fprintf(stderr, "%s: we got a leftover, so handle it\n", __func__); | 1124 | fprintf(stderr, "%s: we got a leftover, so handle it\n", __func__); |
| 1113 | char* newbuff = malloc(DEFAULT_CAPACITY); | 1125 | char* newbuff = malloc(DEFAULT_CAPACITY); |
| 1114 | memcpy(newbuff, cursor, usbReceive.leftover); | 1126 | memcpy(newbuff, cursor, phone->usbReceive.leftover); |
| 1115 | free(usbReceive.buffer); | 1127 | free(phone->usbReceive.buffer); |
| 1116 | usbReceive.buffer = newbuff; | 1128 | phone->usbReceive.buffer = newbuff; |
| 1117 | usbReceive.capacity = DEFAULT_CAPACITY; | 1129 | phone->usbReceive.capacity = DEFAULT_CAPACITY; |
| 1118 | } | 1130 | } |
| 1119 | } | 1131 | } |
| 1120 | 1132 | ||
