| 1 |
/* |
| 2 |
* file_relay.c |
| 3 |
* com.apple.mobile.file_relay service implementation. |
| 4 |
* |
| 5 |
* Copyright (c) 2010 Nikias Bassen, All Rights Reserved. |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
#include <config.h> |
| 24 |
#endif |
| 25 |
#include <string.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include "file_relay.h" |
| 28 |
#include "property_list_service.h" |
| 29 |
#include "common/debug.h" |
| 30 |
|
| 31 |
file_relay_error_t file_relay_client_new(idevice_t device, lockdownd_service_descriptor_t service, file_relay_client_t *client) |
| 32 |
{ |
| 33 |
if (!device || !service || service->port == 0 || !client || *client) { |
| 34 |
return FILE_RELAY_E_INVALID_ARG; |
| 35 |
} |
| 36 |
|
| 37 |
property_list_service_client_t plistclient = NULL; |
| 38 |
if (property_list_service_client_new(device, service, &plistclient) != PROPERTY_LIST_SERVICE_E_SUCCESS) { |
| 39 |
return FILE_RELAY_E_MUX_ERROR; |
| 40 |
} |
| 41 |
|
| 42 |
/* create client object */ |
| 43 |
file_relay_client_t client_loc = (file_relay_client_t) malloc(sizeof(struct file_relay_client_private)); |
| 44 |
client_loc->parent = plistclient; |
| 45 |
|
| 46 |
/* all done, return success */ |
| 47 |
*client = client_loc; |
| 48 |
return FILE_RELAY_E_SUCCESS; |
| 49 |
} |
| 50 |
|
| 51 |
file_relay_error_t file_relay_client_start_service(idevice_t device, file_relay_client_t * client, const char* label) |
| 52 |
{ |
| 53 |
file_relay_error_t err = FILE_RELAY_E_UNKNOWN_ERROR; |
| 54 |
service_client_factory_start_service(device, FILE_RELAY_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(file_relay_client_new), &err); |
| 55 |
return err; |
| 56 |
} |
| 57 |
|
| 58 |
file_relay_error_t file_relay_client_free(file_relay_client_t client) |
| 59 |
{ |
| 60 |
if (!client) |
| 61 |
return FILE_RELAY_E_INVALID_ARG; |
| 62 |
|
| 63 |
if (property_list_service_client_free(client->parent) != PROPERTY_LIST_SERVICE_E_SUCCESS) { |
| 64 |
return FILE_RELAY_E_UNKNOWN_ERROR; |
| 65 |
} |
| 66 |
free(client); |
| 67 |
return FILE_RELAY_E_SUCCESS; |
| 68 |
} |
| 69 |
|
| 70 |
file_relay_error_t file_relay_request_sources_timeout(file_relay_client_t client, const char **sources, idevice_connection_t *connection, unsigned int timeout) |
| 71 |
{ |
| 72 |
if (!client || !client->parent || !sources || !sources[0]) { |
| 73 |
return FILE_RELAY_E_INVALID_ARG; |
| 74 |
} |
| 75 |
*connection = NULL; |
| 76 |
file_relay_error_t err = FILE_RELAY_E_UNKNOWN_ERROR; |
| 77 |
/* set up request plist */ |
| 78 |
plist_t array = plist_new_array(); |
| 79 |
int i = 0; |
| 80 |
while (sources[i]) { |
| 81 |
plist_array_append_item(array, plist_new_string(sources[i])); |
| 82 |
i++; |
| 83 |
} |
| 84 |
plist_t dict = plist_new_dict(); |
| 85 |
plist_dict_set_item(dict, "Sources", array); |
| 86 |
|
| 87 |
if (property_list_service_send_xml_plist(client->parent, dict) != PROPERTY_LIST_SERVICE_E_SUCCESS) { |
| 88 |
debug_info("ERROR: Could not send request to device!"); |
| 89 |
err = FILE_RELAY_E_MUX_ERROR; |
| 90 |
goto leave; |
| 91 |
} |
| 92 |
plist_free(dict); |
| 93 |
|
| 94 |
dict = NULL; |
| 95 |
if (property_list_service_receive_plist_with_timeout(client->parent, &dict, timeout) != PROPERTY_LIST_SERVICE_E_SUCCESS) { |
| 96 |
debug_info("ERROR: Could not receive answer from device!"); |
| 97 |
err = FILE_RELAY_E_MUX_ERROR; |
| 98 |
goto leave; |
| 99 |
} |
| 100 |
|
| 101 |
if (!dict) { |
| 102 |
debug_info("ERROR: Did not receive any plist!"); |
| 103 |
err = FILE_RELAY_E_PLIST_ERROR; |
| 104 |
goto leave; |
| 105 |
} |
| 106 |
|
| 107 |
plist_t error = plist_dict_get_item(dict, "Error"); |
| 108 |
if (error) { |
| 109 |
char *errmsg = NULL; |
| 110 |
plist_get_string_val(error, &errmsg); |
| 111 |
if (errmsg) { |
| 112 |
if (!strcmp(errmsg, "InvalidSource")) { |
| 113 |
debug_info("ERROR: One or more given sources are invalid!"); |
| 114 |
err = FILE_RELAY_E_INVALID_SOURCE; |
| 115 |
} else if (!strcmp(errmsg, "StagingEmpty")) { |
| 116 |
debug_info("ERROR: StagingEmpty - No data available!"); |
| 117 |
err = FILE_RELAY_E_STAGING_EMPTY; |
| 118 |
} else if (!strcmp(errmsg, "PermissionDenied")) { |
| 119 |
debug_info("ERROR: Permission denied."); |
| 120 |
err = FILE_RELAY_E_PERMISSION_DENIED; |
| 121 |
} else { |
| 122 |
debug_info("ERROR: Unknown error '%s'", errmsg); |
| 123 |
} |
| 124 |
free(errmsg); |
| 125 |
} else { |
| 126 |
debug_info("ERROR: Could not get error message!"); |
| 127 |
} |
| 128 |
goto leave; |
| 129 |
} |
| 130 |
|
| 131 |
plist_t status = plist_dict_get_item(dict, "Status"); |
| 132 |
if (!status) { |
| 133 |
debug_info("ERROR: Unexpected plist received!"); |
| 134 |
debug_plist(dict); |
| 135 |
err = FILE_RELAY_E_PLIST_ERROR; |
| 136 |
goto leave; |
| 137 |
} |
| 138 |
|
| 139 |
char *ack = NULL; |
| 140 |
plist_get_string_val(status, &ack); |
| 141 |
if (!ack) { |
| 142 |
debug_info("ERROR: Could not get 'Acknowledged' string!"); |
| 143 |
goto leave; |
| 144 |
} |
| 145 |
|
| 146 |
if (strcmp(ack, "Acknowledged") != 0) { |
| 147 |
debug_info("ERROR: Did not receive 'Acknowledged' but '%s'", ack); |
| 148 |
goto leave; |
| 149 |
} |
| 150 |
free(ack); |
| 151 |
err = FILE_RELAY_E_SUCCESS; |
| 152 |
|
| 153 |
*connection = client->parent->parent->connection; |
| 154 |
|
| 155 |
leave: |
| 156 |
if (dict) { |
| 157 |
plist_free(dict); |
| 158 |
} |
| 159 |
return err; |
| 160 |
} |
| 161 |
|
| 162 |
file_relay_error_t file_relay_request_sources(file_relay_client_t client, const char **sources, idevice_connection_t *connection) |
| 163 |
{ |
| 164 |
return file_relay_request_sources_timeout(client, sources, connection, 60000); |
| 165 |
} |