diff options
Diffstat (limited to 'src/usbmux.c')
| -rw-r--r-- | src/usbmux.c | 69 |
1 files changed, 10 insertions, 59 deletions
diff --git a/src/usbmux.c b/src/usbmux.c index 2157e05..1d6497e 100644 --- a/src/usbmux.c +++ b/src/usbmux.c | |||
| @@ -822,7 +822,6 @@ int usbmux_send(usbmux_client_t client, const char *data, uint32_t datalen, | |||
| 822 | pthread_mutex_lock(&client->mutex); | 822 | pthread_mutex_lock(&client->mutex); |
| 823 | 823 | ||
| 824 | int sendresult = 0; | 824 | int sendresult = 0; |
| 825 | int fullsendresult = 0; | ||
| 826 | uint32_t blocksize = 0; | 825 | uint32_t blocksize = 0; |
| 827 | if (client->wr_window <= 0) { | 826 | if (client->wr_window <= 0) { |
| 828 | struct timespec ts; | 827 | struct timespec ts; |
| @@ -841,67 +840,20 @@ int usbmux_send(usbmux_client_t client, const char *data, uint32_t datalen, | |||
| 841 | // client->scnt and client->ocnt should already be in host notation... | 840 | // client->scnt and client->ocnt should already be in host notation... |
| 842 | // we don't need to change them juuuust yet. | 841 | // we don't need to change them juuuust yet. |
| 843 | char *buffer = (char *) malloc(blocksize + 2); // allow 2 bytes of safety padding | 842 | char *buffer = (char *) malloc(blocksize + 2); // allow 2 bytes of safety padding |
| 844 | const char *dataptr = data; | ||
| 845 | uint32_t curlen = datalen; | ||
| 846 | uint32_t packetsize = blocksize; | ||
| 847 | |||
| 848 | // BEGIN HACK | ||
| 849 | if ((blocksize % 128) == 0) { | ||
| 850 | int cutoff = 28; | ||
| 851 | // HACK: we need to split up the packet because of an unresolved | ||
| 852 | // usb communication issue aka 'N*128 problem' or 'N*512 problem' | ||
| 853 | log_debug_msg("%s: HACK: splitting packet, two send_to_device calls will follow\n"); | ||
| 854 | packetsize = blocksize - cutoff; | ||
| 855 | curlen = datalen - cutoff; | ||
| 856 | client->header->length = packetsize; | ||
| 857 | client->header->length16 = packetsize; | ||
| 858 | hton_header(client->header); | ||
| 859 | memcpy(buffer, client->header, sizeof(usbmux_tcp_header)); | ||
| 860 | memcpy(buffer + sizeof(usbmux_tcp_header), dataptr, curlen); | ||
| 861 | |||
| 862 | log_debug_msg("%s: send_to_device(%d --> %d) window = %d\n", __func__, | ||
| 863 | ntohs(client->header->sport), | ||
| 864 | ntohs(client->header->dport), | ||
| 865 | ntohs(client->header->window)); | ||
| 866 | sendresult = send_to_device(client->device, buffer, packetsize); | ||
| 867 | fullsendresult = sendresult; | ||
| 868 | |||
| 869 | // revert header fields that have been swapped before trying to send | ||
| 870 | ntoh_header(client->header); | ||
| 871 | |||
| 872 | // update counts ONLY if the send succeeded. | ||
| 873 | if ((uint32_t) sendresult == packetsize) { | ||
| 874 | // Re-calculate scnt | ||
| 875 | client->header->scnt += curlen; | ||
| 876 | client->wr_window -= packetsize; | ||
| 877 | } else { | ||
| 878 | goto err_cond; | ||
| 879 | } | ||
| 880 | |||
| 881 | dataptr += curlen; | ||
| 882 | curlen = cutoff; | ||
| 883 | packetsize = sizeof(usbmux_tcp_header) + curlen; | ||
| 884 | // fix fullsendresult to not include the header length to make | ||
| 885 | // setting *sent_bytes work properly | ||
| 886 | fullsendresult -= sizeof(usbmux_tcp_header); | ||
| 887 | } | ||
| 888 | // END HACK | ||
| 889 | |||
| 890 | // Set the length | 843 | // Set the length |
| 891 | client->header->length = packetsize; | 844 | client->header->length = blocksize; |
| 892 | client->header->length16 = packetsize; | 845 | client->header->length16 = blocksize; |
| 893 | 846 | ||
| 894 | // Put header into big-endian notation | 847 | // Put header into big-endian notation |
| 895 | hton_header(client->header); | 848 | hton_header(client->header); |
| 896 | // Concatenation of stuff in the buffer. | 849 | // Concatenation of stuff in the buffer. |
| 897 | memcpy(buffer, client->header, sizeof(usbmux_tcp_header)); | 850 | memcpy(buffer, client->header, sizeof(usbmux_tcp_header)); |
| 898 | memcpy(buffer + sizeof(usbmux_tcp_header), dataptr, curlen); | 851 | memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen); |
| 899 | 852 | ||
| 900 | log_debug_msg("%s: send_to_device(%d --> %d)\n", __func__, | 853 | log_debug_msg("%s: send_to_device(%d --> %d)\n", __func__, |
| 901 | ntohs(client->header->sport), | 854 | ntohs(client->header->sport), |
| 902 | ntohs(client->header->dport)); | 855 | ntohs(client->header->dport)); |
| 903 | sendresult = send_to_device(client->device, buffer, packetsize); | 856 | sendresult = send_to_device(client->device, buffer, blocksize); |
| 904 | fullsendresult += sendresult; | ||
| 905 | // Now that we've sent it off, we can clean up after our sloppy selves. | 857 | // Now that we've sent it off, we can clean up after our sloppy selves. |
| 906 | if (buffer) | 858 | if (buffer) |
| 907 | free(buffer); | 859 | free(buffer); |
| @@ -910,13 +862,12 @@ int usbmux_send(usbmux_client_t client, const char *data, uint32_t datalen, | |||
| 910 | ntoh_header(client->header); | 862 | ntoh_header(client->header); |
| 911 | 863 | ||
| 912 | // update counts ONLY if the send succeeded. | 864 | // update counts ONLY if the send succeeded. |
| 913 | if ((uint32_t) sendresult == packetsize) { | 865 | if ((uint32_t) sendresult == blocksize) { |
| 914 | // Re-calculate scnt | 866 | // Re-calculate scnt |
| 915 | client->header->scnt += curlen; | 867 | client->header->scnt += datalen; |
| 916 | client->wr_window -= packetsize; | 868 | client->wr_window -= blocksize; |
| 917 | } | 869 | } |
| 918 | 870 | ||
| 919 | err_cond: | ||
| 920 | pthread_mutex_unlock(&client->mutex); | 871 | pthread_mutex_unlock(&client->mutex); |
| 921 | 872 | ||
| 922 | if (sendresult == -ETIMEDOUT || sendresult == 0) { | 873 | if (sendresult == -ETIMEDOUT || sendresult == 0) { |
| @@ -925,14 +876,14 @@ err_cond: | |||
| 925 | return -ETIMEDOUT; | 876 | return -ETIMEDOUT; |
| 926 | } else if (sendresult < 0) { | 877 | } else if (sendresult < 0) { |
| 927 | return sendresult; | 878 | return sendresult; |
| 928 | } else if ((uint32_t) fullsendresult == blocksize) { | 879 | } else if ((uint32_t) sendresult == blocksize) { |
| 929 | // actual number of data bytes sent. | 880 | // actual number of data bytes sent. |
| 930 | *sent_bytes = fullsendresult - sizeof(usbmux_tcp_header); | 881 | *sent_bytes = sendresult - sizeof(usbmux_tcp_header); |
| 931 | return 0; | 882 | return 0; |
| 932 | } else { | 883 | } else { |
| 933 | fprintf(stderr, | 884 | fprintf(stderr, |
| 934 | "usbsend managed to dump a packet that is not full size. %d of %d\n", | 885 | "usbsend managed to dump a packet that is not full size. %d of %d\n", |
| 935 | sendresult, packetsize); | 886 | sendresult, blocksize); |
| 936 | return -EBADMSG; | 887 | return -EBADMSG; |
| 937 | } | 888 | } |
| 938 | } | 889 | } |
