diff options
| author | 2008-10-25 16:11:27 +0200 | |
|---|---|---|
| committer | 2008-10-25 16:52:20 +0200 | |
| commit | 0b2cfd2c7c6211ff5902e48720c34067f238ce90 (patch) | |
| tree | 9c30578b4288795e7589c959caa08f3547ca0b88 /src/AFC.c | |
| parent | bbd813da84c1ff8484bbd3eb7f575775271424da (diff) | |
| download | libimobiledevice-0b2cfd2c7c6211ff5902e48720c34067f238ce90.tar.gz libimobiledevice-0b2cfd2c7c6211ff5902e48720c34067f238ce90.tar.bz2 | |
Handle debugging through utilitary functions
Diffstat (limited to 'src/AFC.c')
| -rw-r--r-- | src/AFC.c | 116 |
1 files changed, 39 insertions, 77 deletions
| @@ -27,16 +27,13 @@ | |||
| 27 | // This is the maximum size an AFC data packet can be | 27 | // This is the maximum size an AFC data packet can be |
| 28 | const int MAXIMUM_PACKET_SIZE = (2 << 15) - 32; | 28 | const int MAXIMUM_PACKET_SIZE = (2 << 15) - 32; |
| 29 | 29 | ||
| 30 | extern int debug; | ||
| 31 | |||
| 32 | /** Locks an AFC client, done for thread safety stuff | 30 | /** Locks an AFC client, done for thread safety stuff |
| 33 | * | 31 | * |
| 34 | * @param client The AFC client connection to lock | 32 | * @param client The AFC client connection to lock |
| 35 | */ | 33 | */ |
| 36 | static void afc_lock(iphone_afc_client_t client) | 34 | static void afc_lock(iphone_afc_client_t client) |
| 37 | { | 35 | { |
| 38 | if (debug) | 36 | log_debug_msg("Locked\n"); |
| 39 | fprintf(stderr, "Locked\n"); | ||
| 40 | while (client->lock) { | 37 | while (client->lock) { |
| 41 | usleep(500); // they say it's obsolete, but whatever | 38 | usleep(500); // they say it's obsolete, but whatever |
| 42 | } | 39 | } |
| @@ -49,8 +46,7 @@ static void afc_lock(iphone_afc_client_t client) | |||
| 49 | */ | 46 | */ |
| 50 | static void afc_unlock(iphone_afc_client_t client) | 47 | static void afc_unlock(iphone_afc_client_t client) |
| 51 | { // just to be pretty | 48 | { // just to be pretty |
| 52 | if (debug) | 49 | log_debug_msg("Unlocked\n"); |
| 53 | fprintf(stderr, "Unlocked\n"); | ||
| 54 | client->lock = 0; | 50 | client->lock = 0; |
| 55 | } | 51 | } |
| 56 | 52 | ||
| @@ -156,16 +152,12 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int | |||
| 156 | memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); | 152 | memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); |
| 157 | offset = client->afc_packet->this_length - sizeof(AFCPacket); | 153 | offset = client->afc_packet->this_length - sizeof(AFCPacket); |
| 158 | 154 | ||
| 159 | if (debug) | 155 | log_debug_msg("dispatch_AFC_packet: Offset: %i\n", offset); |
| 160 | fprintf(stderr, "dispatch_AFC_packet: Offset: %i\n", offset); | ||
| 161 | if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { | 156 | if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { |
| 162 | if (debug) { | 157 | log_debug_msg("dispatch_AFC_packet: Length did not resemble what it was supposed"); |
| 163 | fprintf(stderr, "dispatch_AFC_packet: Length did not resemble what it was supposed"); | 158 | log_debug_msg("to based on the packet.\n"); |
| 164 | fprintf(stderr, "to based on the packet.\n"); | 159 | log_debug_msg("length minus offset: %i\n", length - offset); |
| 165 | fprintf(stderr, "length minus offset: %i\n", length - offset); | 160 | log_debug_msg("rest of packet: %i\n", client->afc_packet->entire_length - client->afc_packet->this_length); |
| 166 | fprintf(stderr, "rest of packet: %i\n", | ||
| 167 | client->afc_packet->entire_length - client->afc_packet->this_length); | ||
| 168 | } | ||
| 169 | free(buffer); | 161 | free(buffer); |
| 170 | return -1; | 162 | return -1; |
| 171 | } | 163 | } |
| @@ -176,32 +168,25 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int | |||
| 176 | return bytes; | 168 | return bytes; |
| 177 | } | 169 | } |
| 178 | 170 | ||
| 179 | if (debug) { | 171 | log_debug_msg("dispatch_AFC_packet: sent the first now go with the second\n"); |
| 180 | fprintf(stderr, "dispatch_AFC_packet: sent the first now go with the second\n"); | 172 | log_debug_msg("Length: %i\n", length - offset); |
| 181 | fprintf(stderr, "Length: %i\n", length - offset); | 173 | log_debug_msg("Buffer: \n"); |
| 182 | fprintf(stderr, "Buffer: \n"); | 174 | log_debug_msg(data + offset); |
| 183 | fwrite(data + offset, 1, length - offset, stdout); | ||
| 184 | } | ||
| 185 | 175 | ||
| 186 | iphone_mux_send(client->connection, data + offset, length - offset, &bytes); | 176 | iphone_mux_send(client->connection, data + offset, length - offset, &bytes); |
| 187 | return bytes; | 177 | return bytes; |
| 188 | } else { | 178 | } else { |
| 189 | if (debug) | 179 | log_debug_msg("dispatch_AFC_packet doin things the old way\n"); |
| 190 | fprintf(stderr, "dispatch_AFC_packet doin things the old way\n"); | ||
| 191 | char *buffer = (char *) malloc(sizeof(char) * client->afc_packet->this_length); | 180 | char *buffer = (char *) malloc(sizeof(char) * client->afc_packet->this_length); |
| 192 | if (debug) | 181 | log_debug_msg("dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length); |
| 193 | fprintf(stderr, "dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length); | ||
| 194 | memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); | 182 | memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); |
| 195 | if (debug) | 183 | log_debug_msg("dispatch_AFC_packet packet data follows\n"); |
| 196 | fprintf(stderr, "dispatch_AFC_packet packet data follows\n"); | ||
| 197 | if (length > 0) { | 184 | if (length > 0) { |
| 198 | memcpy(buffer + sizeof(AFCPacket), data, length); | 185 | memcpy(buffer + sizeof(AFCPacket), data, length); |
| 199 | buffer[sizeof(AFCPacket) + length] = '\0'; | 186 | buffer[sizeof(AFCPacket) + length] = '\0'; |
| 200 | } | 187 | } |
| 201 | if (debug) | 188 | log_debug_buffer(buffer, client->afc_packet->this_length); |
| 202 | fwrite(buffer, 1, client->afc_packet->this_length, stdout); | 189 | log_debug_msg("\n"); |
| 203 | if (debug) | ||
| 204 | fprintf(stderr, "\n"); | ||
| 205 | iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes); | 190 | iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes); |
| 206 | 191 | ||
| 207 | if (buffer) { | 192 | if (buffer) { |
| @@ -257,30 +242,23 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) | |||
| 257 | free(buffer); | 242 | free(buffer); |
| 258 | 243 | ||
| 259 | if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) { | 244 | if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) { |
| 260 | if (debug) | 245 | log_debug_msg("Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n", |
| 261 | fprintf(stderr, | 246 | r_packet->operation, client->afc_packet->operation, param1); |
| 262 | "Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n", | ||
| 263 | r_packet->operation, client->afc_packet->operation, param1); | ||
| 264 | recv_len = r_packet->entire_length - r_packet->this_length; | 247 | recv_len = r_packet->entire_length - r_packet->this_length; |
| 265 | free(r_packet); | 248 | free(r_packet); |
| 266 | if (debug) | 249 | log_debug_msg("recv_len=%d\n", recv_len); |
| 267 | fprintf(stderr, "recv_len=%d\n", recv_len); | ||
| 268 | if (param1 == 0) { | 250 | if (param1 == 0) { |
| 269 | if (debug) | 251 | log_debug_msg("... false alarm, but still\n"); |
| 270 | fprintf(stderr, "... false alarm, but still\n"); | ||
| 271 | *dump_here = NULL; | 252 | *dump_here = NULL; |
| 272 | return 0; | 253 | return 0; |
| 273 | } else { | 254 | } else { |
| 274 | if (debug) | 255 | log_debug_msg("Errno %i\n", param1); |
| 275 | fprintf(stderr, "Errno %i\n", param1); | ||
| 276 | } | 256 | } |
| 277 | *dump_here = NULL; | 257 | *dump_here = NULL; |
| 278 | return -1; | 258 | return -1; |
| 279 | } else { | 259 | } else { |
| 280 | if (debug) | 260 | log_debug_msg("Operation code %x\nFull length %i and this length %i\n", |
| 281 | fprintf(stderr, | 261 | r_packet->operation, r_packet->entire_length, r_packet->this_length); |
| 282 | "Operation code %x\nFull length %i and this length %i\n", | ||
| 283 | r_packet->operation, r_packet->entire_length, r_packet->this_length); | ||
| 284 | } | 262 | } |
| 285 | 263 | ||
| 286 | recv_len = r_packet->entire_length - r_packet->this_length; | 264 | recv_len = r_packet->entire_length - r_packet->this_length; |
| @@ -294,25 +272,19 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) | |||
| 294 | final_buffer = (char *) malloc(sizeof(char) * recv_len); | 272 | final_buffer = (char *) malloc(sizeof(char) * recv_len); |
| 295 | while (current_count < recv_len) { | 273 | while (current_count < recv_len) { |
| 296 | iphone_mux_recv(client->connection, buffer, recv_len - current_count, &bytes); | 274 | iphone_mux_recv(client->connection, buffer, recv_len - current_count, &bytes); |
| 297 | if (debug) | 275 | log_debug_msg("receive_AFC_data: still collecting packets\n"); |
| 298 | fprintf(stderr, "receive_AFC_data: still collecting packets\n"); | ||
| 299 | if (bytes < 0) { | 276 | if (bytes < 0) { |
| 300 | if (debug) | 277 | log_debug_msg("receive_AFC_data: mux_recv failed: %d\n", bytes); |
| 301 | fprintf(stderr, "receive_AFC_data: mux_recv failed: %d\n", bytes); | ||
| 302 | break; | 278 | break; |
| 303 | } | 279 | } |
| 304 | if (bytes > recv_len - current_count) { | 280 | if (bytes > recv_len - current_count) { |
| 305 | if (debug) | 281 | log_debug_msg("receive_AFC_data: mux_recv delivered too much data\n"); |
| 306 | fprintf(stderr, "receive_AFC_data: mux_recv delivered too much data\n"); | ||
| 307 | break; | 282 | break; |
| 308 | } | 283 | } |
| 309 | if (bytes > 7 && strstr(buffer, "CFA6LPAA")) { | 284 | if (bytes > 7 && strstr(buffer, "CFA6LPAA")) { |
| 310 | if (debug) | 285 | log_debug_msg("receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n", |
| 311 | fprintf(stderr, | 286 | strstr(buffer, "CFA6LPAA") - buffer); |
| 312 | "receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n", | 287 | log_debug_msg("receive_AFC_data: the total packet length is %i\n", bytes); |
| 313 | strstr(buffer, "CFA6LPAA") - buffer); | ||
| 314 | if (debug) | ||
| 315 | fprintf(stderr, "receive_AFC_data: the total packet length is %i\n", bytes); | ||
| 316 | } | 288 | } |
| 317 | 289 | ||
| 318 | memcpy(final_buffer + current_count, buffer, bytes); | 290 | memcpy(final_buffer + current_count, buffer, bytes); |
| @@ -701,8 +673,7 @@ iphone_afc_open_file(iphone_afc_client_t client, const char *filename, | |||
| 701 | free(data); | 673 | free(data); |
| 702 | 674 | ||
| 703 | if (bytes <= 0) { | 675 | if (bytes <= 0) { |
| 704 | if (debug) | 676 | log_debug_msg("afc_open_file: Didn't receive a response to the command\n"); |
| 705 | fprintf(stderr, "afc_open_file: Didn't receive a response to the command\n"); | ||
| 706 | afc_unlock(client); | 677 | afc_unlock(client); |
| 707 | return IPHONE_E_NOT_ENOUGH_DATA; | 678 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 708 | } | 679 | } |
| @@ -718,8 +689,7 @@ iphone_afc_open_file(iphone_afc_client_t client, const char *filename, | |||
| 718 | *file = file_loc; | 689 | *file = file_loc; |
| 719 | return IPHONE_E_SUCCESS; | 690 | return IPHONE_E_SUCCESS; |
| 720 | } else { | 691 | } else { |
| 721 | if (debug) | 692 | log_debug_msg("afc_open_file: Didn't get any further data\n"); |
| 722 | fprintf(stderr, "afc_open_file: Didn't get any further data\n"); | ||
| 723 | afc_unlock(client); | 693 | afc_unlock(client); |
| 724 | return IPHONE_E_NOT_ENOUGH_DATA; | 694 | return IPHONE_E_NOT_ENOUGH_DATA; |
| 725 | } | 695 | } |
| @@ -747,16 +717,14 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d | |||
| 747 | 717 | ||
| 748 | if (!client || !client->afc_packet || !client->connection || !file) | 718 | if (!client || !client->afc_packet || !client->connection || !file) |
| 749 | return IPHONE_E_INVALID_ARG; | 719 | return IPHONE_E_INVALID_ARG; |
| 750 | if (debug) | 720 | log_debug_msg("afc_read_file called for length %i\n", length); |
| 751 | fprintf(stderr, "afc_read_file called for length %i\n", length); | ||
| 752 | 721 | ||
| 753 | afc_lock(client); | 722 | afc_lock(client); |
| 754 | 723 | ||
| 755 | // Looping here to get around the maximum amount of data that | 724 | // Looping here to get around the maximum amount of data that |
| 756 | // recieve_AFC_data can handle | 725 | // recieve_AFC_data can handle |
| 757 | while (current_count < length) { | 726 | while (current_count < length) { |
| 758 | if (debug) | 727 | log_debug_msg("afc_read_file: current count is %i but length is %i\n", current_count, length); |
| 759 | fprintf(stderr, "afc_read_file: current count is %i but length is %i\n", current_count, length); | ||
| 760 | 728 | ||
| 761 | // Send the read command | 729 | // Send the read command |
| 762 | AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); | 730 | AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); |
| @@ -774,8 +742,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d | |||
| 774 | } | 742 | } |
| 775 | // Receive the data | 743 | // Receive the data |
| 776 | bytes_loc = receive_AFC_data(client, &input); | 744 | bytes_loc = receive_AFC_data(client, &input); |
| 777 | if (debug) | 745 | log_debug_msg("afc_read_file: bytes returned: %i\n", bytes_loc); |
| 778 | fprintf(stderr, "afc_read_file: bytes returned: %i\n", bytes_loc); | ||
| 779 | if (bytes_loc < 0) { | 746 | if (bytes_loc < 0) { |
| 780 | if (input) | 747 | if (input) |
| 781 | free(input); | 748 | free(input); |
| @@ -790,8 +757,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d | |||
| 790 | // success | 757 | // success |
| 791 | } else { | 758 | } else { |
| 792 | if (input) { | 759 | if (input) { |
| 793 | if (debug) | 760 | log_debug_msg("afc_read_file: %d\n", bytes_loc); |
| 794 | fprintf(stderr, "afc_read_file: %d\n", bytes_loc); | ||
| 795 | memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc); | 761 | memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc); |
| 796 | free(input); | 762 | free(input); |
| 797 | input = NULL; | 763 | input = NULL; |
| @@ -799,8 +765,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d | |||
| 799 | } | 765 | } |
| 800 | } | 766 | } |
| 801 | } | 767 | } |
| 802 | if (debug) | 768 | log_debug_msg("afc_read_file: returning current_count as %i\n", current_count); |
| 803 | fprintf(stderr, "afc_read_file: returning current_count as %i\n", current_count); | ||
| 804 | 769 | ||
| 805 | afc_unlock(client); | 770 | afc_unlock(client); |
| 806 | *bytes = current_count; | 771 | *bytes = current_count; |
| @@ -831,8 +796,7 @@ iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file, | |||
| 831 | 796 | ||
| 832 | afc_lock(client); | 797 | afc_lock(client); |
| 833 | 798 | ||
| 834 | if (debug) | 799 | log_debug_msg("afc_write_file: Write length: %i\n", length); |
| 835 | fprintf(stderr, "afc_write_file: Write length: %i\n", length); | ||
| 836 | 800 | ||
| 837 | // Divide the file into segments. | 801 | // Divide the file into segments. |
| 838 | for (i = 0; i < segments; i++) { | 802 | for (i = 0; i < segments; i++) { |
| @@ -893,8 +857,7 @@ iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file, | |||
| 893 | bytes_loc = receive_AFC_data(client, &acknowledgement); | 857 | bytes_loc = receive_AFC_data(client, &acknowledgement); |
| 894 | afc_unlock(client); | 858 | afc_unlock(client); |
| 895 | if (bytes_loc < 0) { | 859 | if (bytes_loc < 0) { |
| 896 | if (debug) | 860 | log_debug_msg("afc_write_file: uh oh?\n"); |
| 897 | fprintf(stderr, "afc_write_file: uh oh?\n"); | ||
| 898 | } | 861 | } |
| 899 | *bytes = current_count; | 862 | *bytes = current_count; |
| 900 | return IPHONE_E_SUCCESS; | 863 | return IPHONE_E_SUCCESS; |
| @@ -916,8 +879,7 @@ iphone_error_t iphone_afc_close_file(iphone_afc_client_t client, iphone_afc_file | |||
| 916 | 879 | ||
| 917 | afc_lock(client); | 880 | afc_lock(client); |
| 918 | 881 | ||
| 919 | if (debug) | 882 | log_debug_msg("afc_close_file: File handle %i\n", file->filehandle); |
| 920 | fprintf(stderr, "afc_close_file: File handle %i\n", file->filehandle); | ||
| 921 | 883 | ||
| 922 | // Send command | 884 | // Send command |
| 923 | memcpy(buffer, &file->filehandle, sizeof(uint32)); | 885 | memcpy(buffer, &file->filehandle, sizeof(uint32)); |
