diff options
| author | 2009-04-12 13:09:25 +0200 | |
|---|---|---|
| committer | 2009-04-12 13:09:25 +0200 | |
| commit | e1751b8c02822da3881b41a8e07a36665a163130 (patch) | |
| tree | 76679eec9f375789c5e302f68ef27d7dc1643825 /iphone.c | |
| parent | 0915fa88ac895e2ccbe6e34669c8b1bd73a5034e (diff) | |
| download | usbmuxd-e1751b8c02822da3881b41a8e07a36665a163130.tar.gz usbmuxd-e1751b8c02822da3881b41a8e07a36665a163130.tar.bz2 | |
better error handling, some cleanup, and changed confusing
struct/variable naming.
Diffstat (limited to 'iphone.c')
| -rw-r--r-- | iphone.c | 49 |
1 files changed, 29 insertions, 20 deletions
| @@ -114,7 +114,13 @@ static int clients = 0; | |||
| 114 | 114 | ||
| 115 | /** | 115 | /** |
| 116 | */ | 116 | */ |
| 117 | int toto_debug = 1; | 117 | int toto_debug = 0; |
| 118 | |||
| 119 | void iphone_set_debug(int e) | ||
| 120 | { | ||
| 121 | toto_debug = e; | ||
| 122 | } | ||
| 123 | |||
| 118 | void log_debug_msg(const char *format, ...) | 124 | void log_debug_msg(const char *format, ...) |
| 119 | { | 125 | { |
| 120 | #ifndef STRIP_DEBUG_CODE | 126 | #ifndef STRIP_DEBUG_CODE |
| @@ -518,13 +524,11 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen) | |||
| 518 | */ | 524 | */ |
| 519 | int recv_from_phone_timeout(iphone_device_t phone, char *data, int datalen, int timeoutmillis) | 525 | int recv_from_phone_timeout(iphone_device_t phone, char *data, int datalen, int timeoutmillis) |
| 520 | { | 526 | { |
| 521 | if (!phone) | ||
| 522 | return -1; | ||
| 523 | int bytes = 0; | 527 | int bytes = 0; |
| 524 | 528 | ||
| 525 | if (!phone) | 529 | if (!phone) |
| 526 | return -1; | 530 | return -EINVAL; |
| 527 | log_debug_msg("recv_from_phone(): attempting to receive %i bytes\n", datalen); | 531 | //log_debug_msg("recv_from_phone(): attempting to receive %i bytes\n", datalen); |
| 528 | 532 | ||
| 529 | bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, timeoutmillis); | 533 | bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, timeoutmillis); |
| 530 | if (bytes < 0) { | 534 | if (bytes < 0) { |
| @@ -534,6 +538,7 @@ int recv_from_phone_timeout(iphone_device_t phone, char *data, int datalen, int | |||
| 534 | if (bytes == -ETIMEDOUT) { | 538 | if (bytes == -ETIMEDOUT) { |
| 535 | // ignore this. it just means timeout reached before we | 539 | // ignore this. it just means timeout reached before we |
| 536 | // picked up any data. no problem. | 540 | // picked up any data. no problem. |
| 541 | return 0; | ||
| 537 | } | 542 | } |
| 538 | else { | 543 | else { |
| 539 | fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), | 544 | fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), |
| @@ -541,7 +546,7 @@ int recv_from_phone_timeout(iphone_device_t phone, char *data, int datalen, int | |||
| 541 | log_debug_msg("recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), | 546 | log_debug_msg("recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), |
| 542 | strerror(-bytes)); | 547 | strerror(-bytes)); |
| 543 | } | 548 | } |
| 544 | return -1; | 549 | return bytes; |
| 545 | } | 550 | } |
| 546 | 551 | ||
| 547 | #ifdef DEBUG | 552 | #ifdef DEBUG |
| @@ -736,7 +741,7 @@ iphone_error_t iphone_mux_new_client(iphone_device_t device, uint16_t src_port, | |||
| 736 | add_connection(new_connection); | 741 | add_connection(new_connection); |
| 737 | new_connection->error = IPHONE_E_SUCCESS; | 742 | new_connection->error = IPHONE_E_SUCCESS; |
| 738 | hton_header(new_connection->header); | 743 | hton_header(new_connection->header); |
| 739 | printf("%s: send_to_phone (%d --> %d)\n", __func__, ntohs(new_connection->header->sport), ntohs(new_connection->header->dport)); | 744 | log_debug_msg("%s: send_to_phone (%d --> %d)\n", __func__, ntohs(new_connection->header->sport), ntohs(new_connection->header->dport)); |
| 740 | if (send_to_phone(device, (char *) new_connection->header, sizeof(usbmux_tcp_header)) >= 0) { | 745 | if (send_to_phone(device, (char *) new_connection->header, sizeof(usbmux_tcp_header)) >= 0) { |
| 741 | *client = new_connection; | 746 | *client = new_connection; |
| 742 | return IPHONE_E_SUCCESS; | 747 | return IPHONE_E_SUCCESS; |
| @@ -842,7 +847,7 @@ iphone_error_t iphone_mux_send(iphone_umux_client_t client, const char *data, ui | |||
| 842 | memcpy(buffer, client->header, sizeof(usbmux_tcp_header)); | 847 | memcpy(buffer, client->header, sizeof(usbmux_tcp_header)); |
| 843 | memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen); | 848 | memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen); |
| 844 | 849 | ||
| 845 | printf("%s: send_to_phone(%d --> %d)\n", __func__, ntohs(client->header->sport), ntohs(client->header->dport)); | 850 | log_debug_msg("%s: send_to_phone(%d --> %d)\n", __func__, ntohs(client->header->sport), ntohs(client->header->dport)); |
| 846 | sendresult = send_to_phone(client->phone, buffer, blocksize); | 851 | sendresult = send_to_phone(client->phone, buffer, blocksize); |
| 847 | // Now that we've sent it off, we can clean up after our sloppy selves. | 852 | // Now that we've sent it off, we can clean up after our sloppy selves. |
| 848 | if (buffer) | 853 | if (buffer) |
| @@ -907,9 +912,9 @@ uint32 append_receive_buffer(iphone_umux_client_t client, char* packet) | |||
| 907 | // falls on our responsibility because we are the ones reading in | 912 | // falls on our responsibility because we are the ones reading in |
| 908 | // feedback. | 913 | // feedback. |
| 909 | if (client->header->scnt == 0 && client->header->ocnt == 0 ) { | 914 | if (client->header->scnt == 0 && client->header->ocnt == 0 ) { |
| 910 | fprintf(stdout, "client is still waiting for handshake.\n"); | 915 | log_debug_msg("client is still waiting for handshake.\n"); |
| 911 | if (header->tcp_flags == (TCP_SYN | TCP_ACK)) { | 916 | if (header->tcp_flags == (TCP_SYN | TCP_ACK)) { |
| 912 | fprintf(stdout, "yes, got syn+ack ; replying with ack.\n"); | 917 | log_debug_msg("yes, got syn+ack ; replying with ack.\n"); |
| 913 | client->header->tcp_flags = TCP_ACK; | 918 | client->header->tcp_flags = TCP_ACK; |
| 914 | client->header->length = sizeof(usbmux_tcp_header); | 919 | client->header->length = sizeof(usbmux_tcp_header); |
| 915 | client->header->length16 = sizeof(usbmux_tcp_header); | 920 | client->header->length16 = sizeof(usbmux_tcp_header); |
| @@ -918,9 +923,9 @@ uint32 append_receive_buffer(iphone_umux_client_t client, char* packet) | |||
| 918 | hton_header(client->header); | 923 | hton_header(client->header); |
| 919 | // push it to USB | 924 | // push it to USB |
| 920 | // TODO: need to check for error in the send here.... :( | 925 | // TODO: need to check for error in the send here.... :( |
| 921 | printf("%s: send_to_phone (%d --> %d)\n", __func__, ntohs(client->header->sport), ntohs(client->header->dport)); | 926 | log_debug_msg("%s: send_to_phone (%d --> %d)\n", __func__, ntohs(client->header->sport), ntohs(client->header->dport)); |
| 922 | if (send_to_phone(client->phone, (char *)client->header, sizeof(usbmux_tcp_header)) <= 0) { | 927 | if (send_to_phone(client->phone, (char *)client->header, sizeof(usbmux_tcp_header)) <= 0) { |
| 923 | fprintf(stdout, "%s: error when pushing to usb...\n", __func__); | 928 | log_debug_msg("%s: error when pushing to usb...\n", __func__); |
| 924 | } | 929 | } |
| 925 | // need to revert some of the fields back to host notation. | 930 | // need to revert some of the fields back to host notation. |
| 926 | ntoh_header(client->header); | 931 | ntoh_header(client->header); |
| @@ -929,7 +934,7 @@ uint32 append_receive_buffer(iphone_umux_client_t client, char* packet) | |||
| 929 | client->error = IPHONE_E_ECONNABORTED; | 934 | client->error = IPHONE_E_ECONNABORTED; |
| 930 | // woah... this connection failed us. | 935 | // woah... this connection failed us. |
| 931 | // TODO: somehow signal that this stream is a no-go. | 936 | // TODO: somehow signal that this stream is a no-go. |
| 932 | fprintf(stderr, "WOAH! client failed to get proper syn+ack.\n"); | 937 | log_debug_msg("WOAH! client failed to get proper syn+ack.\n"); |
| 933 | } | 938 | } |
| 934 | } | 939 | } |
| 935 | 940 | ||
| @@ -1002,7 +1007,7 @@ uint32 append_receive_buffer(iphone_umux_client_t client, char* packet) | |||
| 1002 | 1007 | ||
| 1003 | // ensure there is enough space, either by first malloc or realloc | 1008 | // ensure there is enough space, either by first malloc or realloc |
| 1004 | if (datalen > 0) { | 1009 | if (datalen > 0) { |
| 1005 | fprintf(stderr, "%s: putting %d bytes into client's recv_buffer\n", __func__, datalen); | 1010 | log_debug_msg("%s: putting %d bytes into client's recv_buffer\n", __func__, datalen); |
| 1006 | if (client->r_len == 0) dobroadcast = 1; | 1011 | if (client->r_len == 0) dobroadcast = 1; |
| 1007 | 1012 | ||
| 1008 | if (client->recv_buffer == NULL) { | 1013 | if (client->recv_buffer == NULL) { |
| @@ -1061,13 +1066,14 @@ iphone_umux_client_t find_client(usbmux_tcp_header* recv_header) | |||
| 1061 | 1066 | ||
| 1062 | /** pull in a big USB bulk packet and distribute it to queues appropriately. | 1067 | /** pull in a big USB bulk packet and distribute it to queues appropriately. |
| 1063 | */ | 1068 | */ |
| 1064 | void iphone_mux_pullbulk(iphone_device_t phone) | 1069 | int iphone_mux_pullbulk(iphone_device_t phone) |
| 1065 | { | 1070 | { |
| 1066 | if (!phone) { | 1071 | if (!phone) { |
| 1067 | fprintf(stderr, "iphone_mux_pullbulk: invalid argument\n"); | 1072 | fprintf(stderr, "iphone_mux_pullbulk: invalid argument\n"); |
| 1068 | return; | 1073 | return -EINVAL; |
| 1069 | } | 1074 | } |
| 1070 | 1075 | ||
| 1076 | int res = 0; | ||
| 1071 | static const int DEFAULT_CAPACITY = 128*1024; | 1077 | static const int DEFAULT_CAPACITY = 128*1024; |
| 1072 | if (phone->usbReceive.buffer == NULL) { | 1078 | if (phone->usbReceive.buffer == NULL) { |
| 1073 | phone->usbReceive.capacity = DEFAULT_CAPACITY; | 1079 | phone->usbReceive.capacity = DEFAULT_CAPACITY; |
| @@ -1080,6 +1086,7 @@ void iphone_mux_pullbulk(iphone_device_t phone) | |||
| 1080 | // pull in content, note that the amount we can pull is capacity minus leftover | 1086 | // pull in content, note that the amount we can pull is capacity minus leftover |
| 1081 | int readlen = recv_from_phone_timeout(phone, cursor, phone->usbReceive.capacity - phone->usbReceive.leftover, 5000); | 1087 | int readlen = recv_from_phone_timeout(phone, cursor, phone->usbReceive.capacity - phone->usbReceive.leftover, 5000); |
| 1082 | if (readlen < 0) { | 1088 | if (readlen < 0) { |
| 1089 | res = readlen; | ||
| 1083 | //fprintf(stderr, "recv_from_phone_timeout gave us an error.\n"); | 1090 | //fprintf(stderr, "recv_from_phone_timeout gave us an error.\n"); |
| 1084 | readlen = 0; | 1091 | readlen = 0; |
| 1085 | } | 1092 | } |
| @@ -1099,24 +1106,24 @@ void iphone_mux_pullbulk(iphone_device_t phone) | |||
| 1099 | if (phone->usbReceive.leftover < HEADERLEN) break; | 1106 | if (phone->usbReceive.leftover < HEADERLEN) break; |
| 1100 | usbmux_tcp_header *header = (usbmux_tcp_header *) cursor; | 1107 | usbmux_tcp_header *header = (usbmux_tcp_header *) cursor; |
| 1101 | 1108 | ||
| 1102 | printf("%s: recv_from_phone_timeout (%d --> %d)\n", __func__, ntohs(header->sport), ntohs(header->dport)); | 1109 | log_debug_msg("%s: recv_from_phone_timeout (%d --> %d)\n", __func__, ntohs(header->sport), ntohs(header->dport)); |
| 1103 | 1110 | ||
| 1104 | // now that we have a header, check if there is sufficient data | 1111 | // now that we have a header, check if there is sufficient data |
| 1105 | // to construct a full packet, including its data | 1112 | // to construct a full packet, including its data |
| 1106 | uint32 packetlen = ntohl(header->length); | 1113 | uint32 packetlen = ntohl(header->length); |
| 1107 | if (phone->usbReceive.leftover < packetlen) { | 1114 | if (phone->usbReceive.leftover < packetlen) { |
| 1108 | printf("%s: not enough data to construct a full packet\n", __func__); | 1115 | fprintf(stderr, "%s: not enough data to construct a full packet\n", __func__); |
| 1109 | break; | 1116 | break; |
| 1110 | } | 1117 | } |
| 1111 | 1118 | ||
| 1112 | // ok... find the client this packet will get stuffed to. | 1119 | // ok... find the client this packet will get stuffed to. |
| 1113 | iphone_umux_client_t client = find_client(header); | 1120 | iphone_umux_client_t client = find_client(header); |
| 1114 | if (client == NULL) { | 1121 | if (client == NULL) { |
| 1115 | fprintf(stderr, "WARNING: client for packet cannot be found. dropping packet.\n"); | 1122 | log_debug_msg("WARNING: client for packet cannot be found. dropping packet.\n"); |
| 1116 | } | 1123 | } |
| 1117 | else { | 1124 | else { |
| 1118 | // stuff the data | 1125 | // stuff the data |
| 1119 | fprintf(stderr, "%s: found client, calling append_receive_buffer\n", __func__); | 1126 | log_debug_msg("%s: found client, calling append_receive_buffer\n", __func__); |
| 1120 | append_receive_buffer(client, cursor); | 1127 | append_receive_buffer(client, cursor); |
| 1121 | } | 1128 | } |
| 1122 | 1129 | ||
| @@ -1141,6 +1148,8 @@ void iphone_mux_pullbulk(iphone_device_t phone) | |||
| 1141 | phone->usbReceive.buffer = newbuff; | 1148 | phone->usbReceive.buffer = newbuff; |
| 1142 | phone->usbReceive.capacity = DEFAULT_CAPACITY; | 1149 | phone->usbReceive.capacity = DEFAULT_CAPACITY; |
| 1143 | } | 1150 | } |
| 1151 | |||
| 1152 | return res; | ||
| 1144 | } | 1153 | } |
| 1145 | 1154 | ||
| 1146 | /** | 1155 | /** |
