diff options
Diffstat (limited to 'src/afc.c')
| -rw-r--r-- | src/afc.c | 205 |
1 files changed, 185 insertions, 20 deletions
@@ -26,9 +26,12 @@ #endif #include <stdio.h> #include <stdlib.h> -#include <unistd.h> #include <string.h> +#ifndef _MSC_VER +#include <unistd.h> +#endif + #include "idevice.h" #include "afc.h" #include "common/debug.h" @@ -56,6 +59,27 @@ static void afc_unlock(afc_client_t client) mutex_unlock(&client->mutex); } +static afc_error_t service_to_afc_error(service_error_t err) +{ + switch (err) { + case SERVICE_E_SUCCESS: + return AFC_E_SUCCESS; + case SERVICE_E_INVALID_ARG: + return AFC_E_INVALID_ARG; + case SERVICE_E_MUX_ERROR: + return AFC_E_MUX_ERROR; + case SERVICE_E_SSL_ERROR: + return AFC_E_SSL_ERROR; + case SERVICE_E_NOT_ENOUGH_DATA: + return AFC_E_NOT_ENOUGH_DATA; + case SERVICE_E_TIMEOUT: + return AFC_E_OP_TIMEOUT; + default: + break; + } + return AFC_E_UNKNOWN_ERROR; +} + /** * Makes a connection to the AFC service on the device using the given * connection. @@ -115,7 +139,7 @@ afc_error_t afc_client_new(idevice_t device, lockdownd_service_descriptor_t serv afc_error_t afc_client_start_service(idevice_t device, afc_client_t * client, const char* label) { - afc_error_t err = AFC_E_UNKNOWN_ERROR; + int32_t err = AFC_E_UNKNOWN_ERROR; service_client_factory_start_service(device, AFC_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(afc_client_new), &err); return err; } @@ -171,11 +195,16 @@ static afc_error_t afc_dispatch_packet(afc_client_t client, uint64_t operation, AFCPacket_to_LE(client->afc_packet); debug_buffer((char*)client->afc_packet, sizeof(AFCPacket) + data_length); sent = 0; - service_send(client->parent, (void*)client->afc_packet, sizeof(AFCPacket) + data_length, &sent); + afc_error_t err = service_to_afc_error(service_send(client->parent, (void*)client->afc_packet, sizeof(AFCPacket) + data_length, &sent)); AFCPacket_from_LE(client->afc_packet); + if (err != AFC_E_SUCCESS) { + debug_info("Failed to send packet (sent %i/%i): %s (%d)", sent, sizeof(AFCPacket) + data_length, afc_strerror(err), err); + return err; + } *bytes_sent += sent; if (sent < sizeof(AFCPacket) + data_length) { - return AFC_E_SUCCESS; + debug_info("Failed to send entire packet (sent %i/%i)", sent, sizeof(AFCPacket) + data_length); + return AFC_E_NOT_ENOUGH_DATA; } sent = 0; @@ -187,11 +216,16 @@ static afc_error_t afc_dispatch_packet(afc_client_t client, uint64_t operation, debug_info("packet payload follows"); debug_buffer(payload, payload_length); } - service_send(client->parent, payload, payload_length, &sent); + err = service_to_afc_error(service_send(client->parent, payload, payload_length, &sent)); + if (err != AFC_E_SUCCESS) { + debug_info("Failed to send payload (sent: %i/%i): %s (%d)", sent, payload_length, afc_strerror(err), err); + return err; + } } *bytes_sent += sent; if (sent < payload_length) { - return AFC_E_SUCCESS; + debug_info("Failed to send entire payload (sent: %i/%i): %s (%d)", sent, payload_length, afc_strerror(err), err); + return AFC_E_NOT_ENOUGH_DATA; } return AFC_E_SUCCESS; @@ -224,21 +258,28 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t } /* first, read the AFC header */ - service_receive(client->parent, (char*)&header, sizeof(AFCPacket), &recv_len); - AFCPacket_from_LE(&header); + afc_error_t err = service_to_afc_error(service_receive(client->parent, (char*)&header, sizeof(AFCPacket), &recv_len)); + if (err != AFC_E_SUCCESS) { + debug_info("Failed to receive AFC header: %s (%d)", afc_strerror(err), err); + return err; + } if (recv_len == 0) { debug_info("Just didn't get enough."); - return AFC_E_MUX_ERROR; + return AFC_E_NOT_ENOUGH_DATA; } if (recv_len < sizeof(AFCPacket)) { debug_info("Did not even get the AFCPacket header"); - return AFC_E_MUX_ERROR; + return AFC_E_NOT_ENOUGH_DATA; } + /* make sure endianness is correct */ + AFCPacket_from_LE(&header); + /* check if it's a valid AFC header */ if (strncmp(header.magic, AFC_MAGIC, AFC_MAGIC_LEN) != 0) { debug_info("Invalid AFC packet received (magic != " AFC_MAGIC ")!"); + return AFC_E_UNKNOWN_PACKET_TYPE; } /* check if it has the correct packet number */ @@ -270,16 +311,20 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t buf = (char*)malloc(entire_len); if (this_len > 0) { recv_len = 0; - service_receive(client->parent, buf, this_len, &recv_len); + err = service_to_afc_error(service_receive(client->parent, buf, this_len, &recv_len)); + if (err != AFC_E_SUCCESS) { + free(buf); + debug_info("Failed to receive data: %s (%d)", afc_strerror(err), err); + } if (recv_len <= 0) { free(buf); debug_info("Did not get packet contents!"); - return AFC_E_NOT_ENOUGH_DATA; + return (err == AFC_E_SUCCESS) ? AFC_E_NOT_ENOUGH_DATA : err; } if (recv_len < this_len) { free(buf); debug_info("Could not receive this_len=%d bytes", this_len); - return AFC_E_NOT_ENOUGH_DATA; + return (err == AFC_E_SUCCESS) ? AFC_E_END_OF_DATA : err; } } @@ -288,7 +333,11 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t if (entire_len > this_len) { while (current_count < entire_len) { recv_len = 0; - service_receive(client->parent, buf+current_count, entire_len - current_count, &recv_len); + err = service_to_afc_error(service_receive(client->parent, buf+current_count, entire_len - current_count, &recv_len)); + if (err != AFC_E_SUCCESS) { + debug_info("Error receiving data: %s (%d)", afc_strerror(err), err); + break; + } if (recv_len <= 0) { debug_info("Error receiving data (recv returned %d)", recv_len); break; @@ -296,7 +345,9 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t current_count += recv_len; } if (current_count < entire_len) { - debug_info("WARNING: could not receive full packet (read %s, size %d)", current_count, entire_len); + free(buf); + debug_info("ERROR: Could not receive entire packet (read %i/%i)", current_count, entire_len); + return (err == AFC_E_SUCCESS) ? AFC_E_END_OF_DATA : err; } } @@ -338,7 +389,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t free(buf); debug_info("WARNING: Unknown operation code received 0x%llx param1=%lld", header.operation, param1); -#ifndef WIN32 +#ifndef _WIN32 fprintf(stderr, "%s: WARNING: Unknown operation code received 0x%llx param1=%lld", __func__, (long long)header.operation, (long long)param1); #endif @@ -399,6 +450,50 @@ static char **make_strings_list(char *tokens, uint32_t length) return list; } +static plist_t *make_dictionary(char *tokens, size_t length) +{ + size_t j = 0; + plist_t dict = NULL; + + if (!tokens || !length) + return NULL; + + dict = plist_new_dict(); + + while (j < length) { + size_t key_len = strnlen(tokens + j, length - j); + if (j + key_len >= length) { + plist_free(dict); + return NULL; + } + char* key = tokens + j; + j += key_len + 1; + + if (j >= length) { + plist_free(dict); + return NULL; + } + + size_t val_len = strnlen(tokens + j, length - j); + if (j + val_len >= length) { + plist_free(dict); + return NULL; + } + char* val = tokens + j; + j += val_len + 1; + + char* endp = NULL; + unsigned long long u64val = strtoull(val, &endp, 10); + if (endp && *endp == '\0') { + plist_dict_set_item(dict, key, plist_new_uint(u64val)); + } else { + plist_dict_set_item(dict, key, plist_new_string(val)); + } + } + + return dict; +} + static int _afc_check_packet_buffer(afc_client_t client, uint32_t data_len) { if (data_len > client->packet_extra) { @@ -495,6 +590,40 @@ afc_error_t afc_get_device_info(afc_client_t client, char ***device_information) return ret; } +afc_error_t afc_get_device_info_plist(afc_client_t client, plist_t *device_information) +{ + uint32_t bytes = 0; + char *data = NULL; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; + + if (!client || !device_information) + return AFC_E_INVALID_ARG; + + afc_lock(client); + + /* Send the command */ + ret = afc_dispatch_packet(client, AFC_OP_GET_DEVINFO, 0, NULL, 0, &bytes); + if (ret != AFC_E_SUCCESS) { + afc_unlock(client); + return AFC_E_NOT_ENOUGH_DATA; + } + /* Receive the data */ + ret = afc_receive_data(client, &data, &bytes); + if (ret != AFC_E_SUCCESS) { + if (data) + free(data); + afc_unlock(client); + return ret; + } + /* Parse the data */ + *device_information = make_dictionary(data, bytes); + free(data); + + afc_unlock(client); + + return ret; +} + afc_error_t afc_get_device_info_key(afc_client_t client, const char *key, char **value) { afc_error_t ret = AFC_E_INTERNAL_ERROR; @@ -644,8 +773,6 @@ afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***fil return AFC_E_NO_MEM; } - debug_info("We got %p and %p", client->afc_packet, AFC_PACKET_DATA_PTR); - /* Send command */ memcpy(AFC_PACKET_DATA_PTR, path, data_len); ret = afc_dispatch_packet(client, AFC_OP_GET_FILE_INFO, data_len, NULL, 0, &bytes); @@ -666,6 +793,44 @@ afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***fil return ret; } +afc_error_t afc_get_file_info_plist(afc_client_t client, const char *path, plist_t *file_information) +{ + char *received = NULL; + uint32_t bytes = 0; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; + + if (!client || !path || !file_information) + return AFC_E_INVALID_ARG; + + afc_lock(client); + + uint32_t data_len = (uint32_t)strlen(path)+1; + if (_afc_check_packet_buffer(client, data_len) < 0) { + afc_unlock(client); + debug_info("Failed to realloc packet buffer"); + return AFC_E_NO_MEM; + } + + /* Send command */ + memcpy(AFC_PACKET_DATA_PTR, path, data_len); + ret = afc_dispatch_packet(client, AFC_OP_GET_FILE_INFO, data_len, NULL, 0, &bytes); + if (ret != AFC_E_SUCCESS) { + afc_unlock(client); + return AFC_E_NOT_ENOUGH_DATA; + } + + /* Receive data */ + ret = afc_receive_data(client, &received, &bytes); + if (received) { + *file_information = make_dictionary(received, bytes); + free(received); + } + + afc_unlock(client); + + return ret; +} + afc_error_t afc_file_open(afc_client_t client, const char *filename, afc_file_mode_t file_mode, uint64_t *handle) { if (!client || !client->parent || !client->afc_packet) @@ -1182,14 +1347,14 @@ const char* afc_strerror(afc_error_t err) return "Internal error"; case AFC_E_MUX_ERROR: return "MUX error"; + case AFC_E_SSL_ERROR: + return "SSL error"; case AFC_E_NO_MEM: return "Out of memory"; case AFC_E_NOT_ENOUGH_DATA: return "Not enough data"; case AFC_E_DIR_NOT_EMPTY: return "Directory not empty"; - case AFC_E_FORCE_SIGNED_TYPE: - return "Force signed type"; default: break; } |
