diff options
| author | 2009-05-18 22:29:39 +0200 | |
|---|---|---|
| committer | 2009-05-18 18:47:20 -0700 | |
| commit | 8eaac0513bfb238edec22d46320669f5c9c76542 (patch) | |
| tree | 2db842339bf525d67017525bcbd3f4d35181e5ca /src/AFC.c | |
| parent | dca1758c4f9602fc240c6a7c9ae45839e154d15f (diff) | |
| download | libimobiledevice-8eaac0513bfb238edec22d46320669f5c9c76542.tar.gz libimobiledevice-8eaac0513bfb238edec22d46320669f5c9c76542.tar.bz2 | |
Make use of usbmuxd and remove libusb dependencies
Signed-off-by: Matt Colyer <matt@colyer.name>
Diffstat (limited to 'src/AFC.c')
| -rw-r--r-- | src/AFC.c | 75 |
1 files changed, 39 insertions, 36 deletions
| @@ -20,7 +20,9 @@ | |||
| 20 | */ | 20 | */ |
| 21 | 21 | ||
| 22 | #include <stdio.h> | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> | ||
| 23 | #include <errno.h> | 24 | #include <errno.h> |
| 25 | #include <unistd.h> | ||
| 24 | #include "AFC.h" | 26 | #include "AFC.h" |
| 25 | #include "utils.h" | 27 | #include "utils.h" |
| 26 | 28 | ||
| @@ -61,29 +63,28 @@ static void afc_unlock(iphone_afc_client_t client) | |||
| 61 | * | 63 | * |
| 62 | * @return A handle to the newly-connected client or NULL upon error. | 64 | * @return A handle to the newly-connected client or NULL upon error. |
| 63 | */ | 65 | */ |
| 64 | iphone_error_t iphone_afc_new_client(iphone_device_t device, int src_port, int dst_port, iphone_afc_client_t * client) | 66 | iphone_error_t iphone_afc_new_client(iphone_device_t device, int dst_port, iphone_afc_client_t * client) |
| 65 | { | 67 | { |
| 66 | int ret = IPHONE_E_SUCCESS; | ||
| 67 | |||
| 68 | //makes sure thread environment is available | 68 | //makes sure thread environment is available |
| 69 | if (!g_thread_supported()) | 69 | if (!g_thread_supported()) |
| 70 | g_thread_init(NULL); | 70 | g_thread_init(NULL); |
| 71 | iphone_afc_client_t client_loc = (iphone_afc_client_t) malloc(sizeof(struct iphone_afc_client_int)); | ||
| 72 | 71 | ||
| 73 | if (!device) | 72 | if (!device) |
| 74 | return IPHONE_E_INVALID_ARG; | 73 | return IPHONE_E_INVALID_ARG; |
| 75 | 74 | ||
| 76 | // Attempt connection | 75 | // Attempt connection |
| 77 | client_loc->connection = NULL; | 76 | int sfd = usbmuxd_connect(device->handle, dst_port); |
| 78 | ret = iphone_mux_new_client(device, src_port, dst_port, &client_loc->connection); | 77 | if (sfd < 0) { |
| 79 | if (IPHONE_E_SUCCESS != ret || !client_loc->connection) { | 78 | return IPHONE_E_UNKNOWN_ERROR; // ret; |
| 80 | free(client_loc); | ||
| 81 | return ret; | ||
| 82 | } | 79 | } |
| 80 | |||
| 81 | iphone_afc_client_t client_loc = (iphone_afc_client_t) malloc(sizeof(struct iphone_afc_client_int)); | ||
| 82 | client_loc->sfd = sfd; | ||
| 83 | |||
| 83 | // Allocate a packet | 84 | // Allocate a packet |
| 84 | client_loc->afc_packet = (AFCPacket *) malloc(sizeof(AFCPacket)); | 85 | client_loc->afc_packet = (AFCPacket *) malloc(sizeof(AFCPacket)); |
| 85 | if (!client_loc->afc_packet) { | 86 | if (!client_loc->afc_packet) { |
| 86 | iphone_mux_free_client(client_loc->connection); | 87 | usbmuxd_disconnect(client_loc->sfd); |
| 87 | free(client_loc); | 88 | free(client_loc); |
| 88 | return IPHONE_E_UNKNOWN_ERROR; | 89 | return IPHONE_E_UNKNOWN_ERROR; |
| 89 | } | 90 | } |
| @@ -106,10 +107,10 @@ iphone_error_t iphone_afc_new_client(iphone_device_t device, int src_port, int d | |||
| 106 | */ | 107 | */ |
| 107 | iphone_error_t iphone_afc_free_client(iphone_afc_client_t client) | 108 | iphone_error_t iphone_afc_free_client(iphone_afc_client_t client) |
| 108 | { | 109 | { |
| 109 | if (!client || !client->connection || !client->afc_packet) | 110 | if (!client || client->sfd < 0 || !client->afc_packet) |
| 110 | return IPHONE_E_INVALID_ARG; | 111 | return IPHONE_E_INVALID_ARG; |
| 111 | 112 | ||
| 112 | iphone_mux_free_client(client->connection); | 113 | usbmuxd_disconnect(client->sfd); |
| 113 | free(client->afc_packet); | 114 | free(client->afc_packet); |
| 114 | if (client->mutex) { | 115 | if (client->mutex) { |
| 115 | g_mutex_free(client->mutex); | 116 | g_mutex_free(client->mutex); |
| @@ -217,7 +218,7 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int | |||
| 217 | int bytes = 0, offset = 0; | 218 | int bytes = 0, offset = 0; |
| 218 | char *buffer; | 219 | char *buffer; |
| 219 | 220 | ||
| 220 | if (!client || !client->connection || !client->afc_packet) | 221 | if (!client || client->sfd < 0 || !client->afc_packet) |
| 221 | return 0; | 222 | return 0; |
| 222 | if (!data || !length) | 223 | if (!data || !length) |
| 223 | length = 0; | 224 | length = 0; |
| @@ -248,7 +249,7 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int | |||
| 248 | return -1; | 249 | return -1; |
| 249 | } | 250 | } |
| 250 | memcpy(buffer + sizeof(AFCPacket), data, offset); | 251 | memcpy(buffer + sizeof(AFCPacket), data, offset); |
| 251 | iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, (uint32_t*)&bytes); | 252 | usbmuxd_send(client->sfd, buffer, client->afc_packet->this_length, (uint32_t*)&bytes); |
| 252 | free(buffer); | 253 | free(buffer); |
| 253 | if (bytes <= 0) { | 254 | if (bytes <= 0) { |
| 254 | return bytes; | 255 | return bytes; |
| @@ -259,7 +260,7 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int | |||
| 259 | log_debug_msg("Buffer: \n"); | 260 | log_debug_msg("Buffer: \n"); |
| 260 | log_debug_buffer(data + offset, length - offset); | 261 | log_debug_buffer(data + offset, length - offset); |
| 261 | 262 | ||
| 262 | iphone_mux_send(client->connection, data + offset, length - offset, (uint32_t*)&bytes); | 263 | usbmuxd_send(client->sfd, data + offset, length - offset, (uint32_t*)&bytes); |
| 263 | return bytes; | 264 | return bytes; |
| 264 | } else { | 265 | } else { |
| 265 | log_debug_msg("dispatch_AFC_packet doin things the old way\n"); | 266 | log_debug_msg("dispatch_AFC_packet doin things the old way\n"); |
| @@ -273,7 +274,7 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int | |||
| 273 | } | 274 | } |
| 274 | log_debug_buffer(buffer, client->afc_packet->this_length); | 275 | log_debug_buffer(buffer, client->afc_packet->this_length); |
| 275 | log_debug_msg("\n"); | 276 | log_debug_msg("\n"); |
| 276 | iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, (uint32_t*)&bytes); | 277 | usbmuxd_send(client->sfd, buffer, client->afc_packet->this_length, (uint32_t*)&bytes); |
| 277 | 278 | ||
| 278 | if (buffer) { | 279 | if (buffer) { |
| 279 | free(buffer); | 280 | free(buffer); |
| @@ -307,7 +308,7 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) | |||
| 307 | client->afcerror = 0; | 308 | client->afcerror = 0; |
| 308 | 309 | ||
| 309 | // first, read the AFC header | 310 | // first, read the AFC header |
| 310 | iphone_mux_recv(client->connection, (char*)&header, sizeof(AFCPacket), (uint32_t*)&bytes); | 311 | usbmuxd_recv(client->sfd, (char*)&header, sizeof(AFCPacket), (uint32_t*)&bytes); |
| 311 | if (bytes <= 0) { | 312 | if (bytes <= 0) { |
| 312 | log_debug_msg("%s: Just didn't get enough.\n", __func__); | 313 | log_debug_msg("%s: Just didn't get enough.\n", __func__); |
| 313 | *dump_here = NULL; | 314 | *dump_here = NULL; |
| @@ -359,24 +360,26 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) | |||
| 359 | } | 360 | } |
| 360 | 361 | ||
| 361 | *dump_here = (char*)malloc(entire_len); | 362 | *dump_here = (char*)malloc(entire_len); |
| 362 | iphone_mux_recv(client->connection, *dump_here, this_len, (uint32_t*)&bytes); | 363 | if (this_len > 0) { |
| 363 | if (bytes <= 0) { | 364 | usbmuxd_recv(client->sfd, *dump_here, this_len, (uint32_t*)&bytes); |
| 364 | free(*dump_here); | 365 | if (bytes <= 0) { |
| 365 | *dump_here = NULL; | 366 | free(*dump_here); |
| 366 | log_debug_msg("%s: Did not get packet contents!\n", __func__); | 367 | *dump_here = NULL; |
| 367 | return -1; | 368 | log_debug_msg("%s: Did not get packet contents!\n", __func__); |
| 368 | } else if ((uint32_t)bytes < this_len) { | 369 | return -1; |
| 369 | free(*dump_here); | 370 | } else if ((uint32_t)bytes < this_len) { |
| 370 | *dump_here = NULL; | 371 | free(*dump_here); |
| 371 | log_debug_msg("%s: Could not receive this_len=%d bytes\n", __func__, this_len); | 372 | *dump_here = NULL; |
| 372 | return -1; | 373 | log_debug_msg("%s: Could not receive this_len=%d bytes\n", __func__, this_len); |
| 374 | return -1; | ||
| 375 | } | ||
| 373 | } | 376 | } |
| 374 | 377 | ||
| 375 | current_count = this_len; | 378 | current_count = this_len; |
| 376 | 379 | ||
| 377 | if (entire_len > this_len) { | 380 | if (entire_len > this_len) { |
| 378 | while (current_count < entire_len) { | 381 | while (current_count < entire_len) { |
| 379 | iphone_mux_recv(client->connection, (*dump_here)+current_count, entire_len - current_count, (uint32_t*)&bytes); | 382 | usbmuxd_recv(client->sfd, (*dump_here)+current_count, entire_len - current_count, (uint32_t*)&bytes); |
| 380 | if (bytes <= 0) { | 383 | if (bytes <= 0) { |
| 381 | log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, bytes); | 384 | log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, bytes); |
| 382 | break; | 385 | break; |
| @@ -559,7 +562,7 @@ iphone_error_t iphone_afc_delete_file(iphone_afc_client_t client, const char *pa | |||
| 559 | char *response = NULL; | 562 | char *response = NULL; |
| 560 | int bytes; | 563 | int bytes; |
| 561 | 564 | ||
| 562 | if (!client || !path || !client->afc_packet || !client->connection) | 565 | if (!client || !path || !client->afc_packet || client->sfd < 0) |
| 563 | return IPHONE_E_INVALID_ARG; | 566 | return IPHONE_E_INVALID_ARG; |
| 564 | 567 | ||
| 565 | afc_lock(client); | 568 | afc_lock(client); |
| @@ -600,7 +603,7 @@ iphone_error_t iphone_afc_rename_file(iphone_afc_client_t client, const char *fr | |||
| 600 | char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32_t))); | 603 | char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32_t))); |
| 601 | int bytes = 0; | 604 | int bytes = 0; |
| 602 | 605 | ||
| 603 | if (!client || !from || !to || !client->afc_packet || !client->connection) | 606 | if (!client || !from || !to || !client->afc_packet || client->sfd < 0) |
| 604 | return IPHONE_E_INVALID_ARG; | 607 | return IPHONE_E_INVALID_ARG; |
| 605 | 608 | ||
| 606 | afc_lock(client); | 609 | afc_lock(client); |
| @@ -748,7 +751,7 @@ iphone_error_t iphone_afc_get_file_attr(iphone_afc_client_t client, const char * | |||
| 748 | { | 751 | { |
| 749 | 752 | ||
| 750 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 753 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; |
| 751 | if (!client || !client->connection || !client->afc_packet || !stbuf) | 754 | if (!client || client->sfd < 0 || !client->afc_packet || !stbuf) |
| 752 | return IPHONE_E_INVALID_ARG; | 755 | return IPHONE_E_INVALID_ARG; |
| 753 | 756 | ||
| 754 | memset(stbuf, 0, sizeof(struct stat)); | 757 | memset(stbuf, 0, sizeof(struct stat)); |
| @@ -793,7 +796,7 @@ iphone_afc_open_file(iphone_afc_client_t client, const char *filename, | |||
| 793 | int bytes = 0, length = 0; | 796 | int bytes = 0, length = 0; |
| 794 | char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); | 797 | char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); |
| 795 | 798 | ||
| 796 | if (!client || !client->connection || !client->afc_packet) | 799 | if (!client || client->sfd < 0|| !client->afc_packet) |
| 797 | return IPHONE_E_INVALID_ARG; | 800 | return IPHONE_E_INVALID_ARG; |
| 798 | 801 | ||
| 799 | afc_lock(client); | 802 | afc_lock(client); |
| @@ -851,7 +854,7 @@ iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *d | |||
| 851 | int current_count = 0, bytes_loc = 0; | 854 | int current_count = 0, bytes_loc = 0; |
| 852 | const int MAXIMUM_READ_SIZE = 1 << 16; | 855 | const int MAXIMUM_READ_SIZE = 1 << 16; |
| 853 | 856 | ||
| 854 | if (!client || !client->afc_packet || !client->connection || !file) | 857 | if (!client || !client->afc_packet || client->sfd < 0 || !file) |
| 855 | return IPHONE_E_INVALID_ARG; | 858 | return IPHONE_E_INVALID_ARG; |
| 856 | log_debug_msg("afc_read_file called for length %i\n", length); | 859 | log_debug_msg("afc_read_file called for length %i\n", length); |
| 857 | 860 | ||
| @@ -926,7 +929,7 @@ iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file, | |||
| 926 | int bytes_loc = 0; | 929 | int bytes_loc = 0; |
| 927 | char *out_buffer = NULL; | 930 | char *out_buffer = NULL; |
| 928 | 931 | ||
| 929 | if (!client || !client->afc_packet || !client->connection || !file || !bytes) | 932 | if (!client || !client->afc_packet || client->sfd < 0 || !file || !bytes) |
| 930 | return IPHONE_E_INVALID_ARG; | 933 | return IPHONE_E_INVALID_ARG; |
| 931 | 934 | ||
| 932 | afc_lock(client); | 935 | afc_lock(client); |
| @@ -1219,7 +1222,7 @@ iphone_error_t iphone_afc_truncate(iphone_afc_client_t client, const char *path, | |||
| 1219 | int bytes = 0; | 1222 | int bytes = 0; |
| 1220 | uint64_t size_requested = newsize; | 1223 | uint64_t size_requested = newsize; |
| 1221 | 1224 | ||
| 1222 | if (!client || !path || !client->afc_packet || !client->connection) | 1225 | if (!client || !path || !client->afc_packet || client->sfd < 0) |
| 1223 | return IPHONE_E_INVALID_ARG; | 1226 | return IPHONE_E_INVALID_ARG; |
| 1224 | 1227 | ||
| 1225 | afc_lock(client); | 1228 | afc_lock(client); |
