summaryrefslogtreecommitdiffstats
path: root/src/afc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/afc.c')
-rw-r--r--src/afc.c71
1 files changed, 30 insertions, 41 deletions
diff --git a/src/afc.c b/src/afc.c
index b5228aa..83ee2ca 100644
--- a/src/afc.c
+++ b/src/afc.c
@@ -69,9 +69,7 @@ static void afc_unlock(afc_client_t client)
69 * Makes a connection to the AFC service on the device using the given 69 * Makes a connection to the AFC service on the device using the given
70 * connection. 70 * connection.
71 * 71 *
72 * @param connection An idevice_connection_t that must have been previously 72 * @param serviceclient A connected service client
73 * connected using idevice_connect(). Note that this connection will
74 * not be closed by calling afc_client_free().
75 * @param client Pointer that will be set to a newly allocated afc_client_t 73 * @param client Pointer that will be set to a newly allocated afc_client_t
76 * upon successful return. 74 * upon successful return.
77 * 75 *
@@ -79,14 +77,14 @@ static void afc_unlock(afc_client_t client)
79 * invalid, or AFC_E_NO_MEM if there is a memory allocation problem. 77 * invalid, or AFC_E_NO_MEM if there is a memory allocation problem.
80 */ 78 */
81 79
82afc_error_t afc_client_new_from_connection(idevice_connection_t connection, afc_client_t *client) 80afc_error_t afc_client_new_with_service_client(service_client_t serviceclient, afc_client_t *client)
83{ 81{
84 if (!connection) 82 if (!serviceclient)
85 return AFC_E_INVALID_ARG; 83 return AFC_E_INVALID_ARG;
86 84
87 afc_client_t client_loc = (afc_client_t) malloc(sizeof(struct afc_client_private)); 85 afc_client_t client_loc = (afc_client_t) malloc(sizeof(struct afc_client_private));
88 client_loc->connection = connection; 86 client_loc->parent = serviceclient;
89 client_loc->own_connection = 0; 87 client_loc->free_parent = 0;
90 88
91 /* allocate a packet */ 89 /* allocate a packet */
92 client_loc->afc_packet = (AFCPacket *) malloc(sizeof(AFCPacket)); 90 client_loc->afc_packet = (AFCPacket *) malloc(sizeof(AFCPacket));
@@ -113,10 +111,6 @@ afc_error_t afc_client_new_from_connection(idevice_connection_t connection, afc_
113 111
114/** 112/**
115 * Makes a connection to the AFC service on the device. 113 * Makes a connection to the AFC service on the device.
116 * This function calls afc_client_new_from_connection() after creating
117 * a connection to the specified device and port.
118 *
119 * @see afc_client_new_from_connection
120 * 114 *
121 * @param device The device to connect to. 115 * @param device The device to connect to.
122 * @param service The service descriptor returned by lockdownd_start_service. 116 * @param service The service descriptor returned by lockdownd_start_service.
@@ -132,21 +126,16 @@ afc_error_t afc_client_new(idevice_t device, lockdownd_service_descriptor_t serv
132 if (!device || service->port == 0) 126 if (!device || service->port == 0)
133 return AFC_E_INVALID_ARG; 127 return AFC_E_INVALID_ARG;
134 128
135 /* attempt connection */ 129 service_client_t parent = NULL;
136 idevice_connection_t connection = NULL; 130 if (service_client_new(device, service, &parent) != SERVICE_E_SUCCESS) {
137 if (idevice_connect(device, service->port, &connection) != IDEVICE_E_SUCCESS) {
138 return AFC_E_MUX_ERROR; 131 return AFC_E_MUX_ERROR;
139 } 132 }
140 133
141 /* enable SSL if requested */ 134 afc_error_t err = afc_client_new_with_service_client(parent, client);
142 if (service->ssl_enabled)
143 idevice_connection_enable_ssl(connection);
144
145 afc_error_t err = afc_client_new_from_connection(connection, client);
146 if (err != AFC_E_SUCCESS) { 135 if (err != AFC_E_SUCCESS) {
147 idevice_disconnect(connection); 136 service_client_free(parent);
148 } else { 137 } else {
149 (*client)->own_connection = 1; 138 (*client)->free_parent = 1;
150 } 139 }
151 return err; 140 return err;
152} 141}
@@ -162,9 +151,9 @@ afc_error_t afc_client_free(afc_client_t client)
162 if (!client || !client->afc_packet) 151 if (!client || !client->afc_packet)
163 return AFC_E_INVALID_ARG; 152 return AFC_E_INVALID_ARG;
164 153
165 if (client->own_connection && client->connection) { 154 if (client->free_parent && client->parent) {
166 idevice_disconnect(client->connection); 155 service_client_free(client->parent);
167 client->connection = NULL; 156 client->parent = NULL;
168 } 157 }
169 free(client->afc_packet); 158 free(client->afc_packet);
170#ifdef WIN32 159#ifdef WIN32
@@ -196,7 +185,7 @@ static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, ui
196 uint32_t offset = 0; 185 uint32_t offset = 0;
197 uint32_t sent = 0; 186 uint32_t sent = 0;
198 187
199 if (!client || !client->connection || !client->afc_packet) 188 if (!client || !client->parent || !client->afc_packet)
200 return AFC_E_INVALID_ARG; 189 return AFC_E_INVALID_ARG;
201 190
202 *bytes_sent = 0; 191 *bytes_sent = 0;
@@ -229,7 +218,7 @@ static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, ui
229 /* send AFC packet header */ 218 /* send AFC packet header */
230 AFCPacket_to_LE(client->afc_packet); 219 AFCPacket_to_LE(client->afc_packet);
231 sent = 0; 220 sent = 0;
232 idevice_connection_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); 221 service_send(client->parent, (void*)client->afc_packet, sizeof(AFCPacket), &sent);
233 AFCPacket_from_LE(client->afc_packet); 222 AFCPacket_from_LE(client->afc_packet);
234 if (sent == 0) { 223 if (sent == 0) {
235 /* FIXME: should this be handled as success?! */ 224 /* FIXME: should this be handled as success?! */
@@ -239,7 +228,7 @@ static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, ui
239 228
240 /* send AFC packet data */ 229 /* send AFC packet data */
241 sent = 0; 230 sent = 0;
242 idevice_connection_send(client->connection, data, offset, &sent); 231 service_send(client->parent, data, offset, &sent);
243 if (sent == 0) { 232 if (sent == 0) {
244 return AFC_E_SUCCESS; 233 return AFC_E_SUCCESS;
245 } 234 }
@@ -251,7 +240,7 @@ static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, ui
251 debug_buffer(data + offset, length - offset); 240 debug_buffer(data + offset, length - offset);
252 241
253 sent = 0; 242 sent = 0;
254 idevice_connection_send(client->connection, data + offset, length - offset, &sent); 243 service_send(client->parent, data + offset, length - offset, &sent);
255 244
256 *bytes_sent = sent; 245 *bytes_sent = sent;
257 return AFC_E_SUCCESS; 246 return AFC_E_SUCCESS;
@@ -264,7 +253,7 @@ static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, ui
264 /* send AFC packet header */ 253 /* send AFC packet header */
265 AFCPacket_to_LE(client->afc_packet); 254 AFCPacket_to_LE(client->afc_packet);
266 sent = 0; 255 sent = 0;
267 idevice_connection_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); 256 service_send(client->parent, (void*)client->afc_packet, sizeof(AFCPacket), &sent);
268 AFCPacket_from_LE(client->afc_packet); 257 AFCPacket_from_LE(client->afc_packet);
269 if (sent == 0) { 258 if (sent == 0) {
270 return AFC_E_SUCCESS; 259 return AFC_E_SUCCESS;
@@ -275,7 +264,7 @@ static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, ui
275 debug_info("packet data follows"); 264 debug_info("packet data follows");
276 265
277 debug_buffer(data, length); 266 debug_buffer(data, length);
278 idevice_connection_send(client->connection, data, length, &sent); 267 service_send(client->parent, data, length, &sent);
279 *bytes_sent += sent; 268 *bytes_sent += sent;
280 } 269 }
281 return AFC_E_SUCCESS; 270 return AFC_E_SUCCESS;
@@ -303,7 +292,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint3
303 *bytes_recv = 0; 292 *bytes_recv = 0;
304 293
305 /* first, read the AFC header */ 294 /* first, read the AFC header */
306 idevice_connection_receive(client->connection, (char*)&header, sizeof(AFCPacket), bytes_recv); 295 service_receive(client->parent, (char*)&header, sizeof(AFCPacket), bytes_recv);
307 AFCPacket_from_LE(&header); 296 AFCPacket_from_LE(&header);
308 if (*bytes_recv == 0) { 297 if (*bytes_recv == 0) {
309 debug_info("Just didn't get enough."); 298 debug_info("Just didn't get enough.");
@@ -357,7 +346,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint3
357 346
358 *dump_here = (char*)malloc(entire_len); 347 *dump_here = (char*)malloc(entire_len);
359 if (this_len > 0) { 348 if (this_len > 0) {
360 idevice_connection_receive(client->connection, *dump_here, this_len, bytes_recv); 349 service_receive(client->parent, *dump_here, this_len, bytes_recv);
361 if (*bytes_recv <= 0) { 350 if (*bytes_recv <= 0) {
362 free(*dump_here); 351 free(*dump_here);
363 *dump_here = NULL; 352 *dump_here = NULL;
@@ -375,7 +364,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint3
375 364
376 if (entire_len > this_len) { 365 if (entire_len > this_len) {
377 while (current_count < entire_len) { 366 while (current_count < entire_len) {
378 idevice_connection_receive(client->connection, (*dump_here)+current_count, entire_len - current_count, bytes_recv); 367 service_receive(client->parent, (*dump_here)+current_count, entire_len - current_count, bytes_recv);
379 if (*bytes_recv <= 0) { 368 if (*bytes_recv <= 0) {
380 debug_info("Error receiving data (recv returned %d)", *bytes_recv); 369 debug_info("Error receiving data (recv returned %d)", *bytes_recv);
381 break; 370 break;
@@ -625,7 +614,7 @@ afc_error_t afc_remove_path(afc_client_t client, const char *path)
625 uint32_t bytes = 0; 614 uint32_t bytes = 0;
626 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 615 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
627 616
628 if (!client || !path || !client->afc_packet || !client->connection) 617 if (!client || !path || !client->afc_packet || !client->parent)
629 return AFC_E_INVALID_ARG; 618 return AFC_E_INVALID_ARG;
630 619
631 afc_lock(client); 620 afc_lock(client);
@@ -668,7 +657,7 @@ afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *t
668 uint32_t bytes = 0; 657 uint32_t bytes = 0;
669 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 658 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
670 659
671 if (!client || !from || !to || !client->afc_packet || !client->connection) 660 if (!client || !from || !to || !client->afc_packet || !client->parent)
672 return AFC_E_INVALID_ARG; 661 return AFC_E_INVALID_ARG;
673 662
674 afc_lock(client); 663 afc_lock(client);
@@ -800,7 +789,7 @@ afc_file_open(afc_client_t client, const char *filename,
800 /* set handle to 0 so in case an error occurs, the handle is invalid */ 789 /* set handle to 0 so in case an error occurs, the handle is invalid */
801 *handle = 0; 790 *handle = 0;
802 791
803 if (!client || !client->connection || !client->afc_packet) 792 if (!client || !client->parent || !client->afc_packet)
804 return AFC_E_INVALID_ARG; 793 return AFC_E_INVALID_ARG;
805 794
806 afc_lock(client); 795 afc_lock(client);
@@ -856,7 +845,7 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length,
856 const uint32_t MAXIMUM_READ_SIZE = 1 << 16; 845 const uint32_t MAXIMUM_READ_SIZE = 1 << 16;
857 afc_error_t ret = AFC_E_SUCCESS; 846 afc_error_t ret = AFC_E_SUCCESS;
858 847
859 if (!client || !client->afc_packet || !client->connection || handle == 0) 848 if (!client || !client->afc_packet || !client->parent || handle == 0)
860 return AFC_E_INVALID_ARG; 849 return AFC_E_INVALID_ARG;
861 debug_info("called for length %i", length); 850 debug_info("called for length %i", length);
862 851
@@ -933,7 +922,7 @@ afc_file_write(afc_client_t client, uint64_t handle, const char *data, uint32_t
933 char *out_buffer = NULL; 922 char *out_buffer = NULL;
934 afc_error_t ret = AFC_E_SUCCESS; 923 afc_error_t ret = AFC_E_SUCCESS;
935 924
936 if (!client || !client->afc_packet || !client->connection || !bytes_written || (handle == 0)) 925 if (!client || !client->afc_packet || !client->parent || !bytes_written || (handle == 0))
937 return AFC_E_INVALID_ARG; 926 return AFC_E_INVALID_ARG;
938 927
939 afc_lock(client); 928 afc_lock(client);
@@ -1258,7 +1247,7 @@ afc_error_t afc_truncate(afc_client_t client, const char *path, uint64_t newsize
1258 uint64_t size_requested = htole64(newsize); 1247 uint64_t size_requested = htole64(newsize);
1259 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 1248 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
1260 1249
1261 if (!client || !path || !client->afc_packet || !client->connection) 1250 if (!client || !path || !client->afc_packet || !client->parent)
1262 return AFC_E_INVALID_ARG; 1251 return AFC_E_INVALID_ARG;
1263 1252
1264 afc_lock(client); 1253 afc_lock(client);
@@ -1302,7 +1291,7 @@ afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const c
1302 uint64_t type = htole64(linktype); 1291 uint64_t type = htole64(linktype);
1303 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 1292 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
1304 1293
1305 if (!client || !target || !linkname || !client->afc_packet || !client->connection) 1294 if (!client || !target || !linkname || !client->afc_packet || !client->parent)
1306 return AFC_E_INVALID_ARG; 1295 return AFC_E_INVALID_ARG;
1307 1296
1308 afc_lock(client); 1297 afc_lock(client);
@@ -1350,7 +1339,7 @@ afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mt
1350 uint64_t mtime_loc = htole64(mtime); 1339 uint64_t mtime_loc = htole64(mtime);
1351 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 1340 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
1352 1341
1353 if (!client || !path || !client->afc_packet || !client->connection) 1342 if (!client || !path || !client->afc_packet || !client->parent)
1354 return AFC_E_INVALID_ARG; 1343 return AFC_E_INVALID_ARG;
1355 1344
1356 afc_lock(client); 1345 afc_lock(client);