summaryrefslogtreecommitdiffstats
path: root/src/MobileSync.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/MobileSync.c')
-rw-r--r--src/MobileSync.c153
1 files changed, 37 insertions, 116 deletions
diff --git a/src/MobileSync.c b/src/MobileSync.c
index 76aefa0..827ed35 100644
--- a/src/MobileSync.c
+++ b/src/MobileSync.c
@@ -25,153 +25,76 @@
25#include <arpa/inet.h> 25#include <arpa/inet.h>
26 26
27#include "MobileSync.h" 27#include "MobileSync.h"
28#include "iphone.h" 28#include "device_link_service.h"
29#include "utils.h" 29#include "debug.h"
30 30
31#define MSYNC_VERSION_INT1 100 31#define MSYNC_VERSION_INT1 100
32#define MSYNC_VERSION_INT2 100 32#define MSYNC_VERSION_INT2 100
33 33
34/** 34/**
35 * Convert an iphone_error_t value to an mobilesync_error_t value. 35 * Convert an device_link_service_error_t value to an mobilesync_error_t value.
36 * Used internally to get correct error codes when using plist helper 36 * Used internally to get correct error codes when using device_link_service stuff.
37 * functions.
38 * 37 *
39 * @param err An iphone_error_t error code 38 * @param err An device_link_service_error_t error code
40 * 39 *
41 * @return A matching mobilesync_error_t error code, 40 * @return A matching mobilesync_error_t error code,
42 * MOBILESYNC_E_UNKNOWN_ERROR otherwise. 41 * MOBILESYNC_E_UNKNOWN_ERROR otherwise.
43 */ 42 */
44static mobilesync_error_t iphone_to_mobilesync_error(iphone_error_t err) 43static mobilesync_error_t mobilesync_error(device_link_service_error_t err)
45{ 44{
46 switch (err) { 45 switch (err) {
47 case IPHONE_E_SUCCESS: 46 case DEVICE_LINK_SERVICE_E_SUCCESS:
48 return MOBILESYNC_E_SUCCESS; 47 return MOBILESYNC_E_SUCCESS;
49 case IPHONE_E_INVALID_ARG: 48 case DEVICE_LINK_SERVICE_E_INVALID_ARG:
50 return MOBILESYNC_E_INVALID_ARG; 49 return MOBILESYNC_E_INVALID_ARG;
51 case IPHONE_E_PLIST_ERROR: 50 case DEVICE_LINK_SERVICE_E_PLIST_ERROR:
52 return MOBILESYNC_E_PLIST_ERROR; 51 return MOBILESYNC_E_PLIST_ERROR;
52 case DEVICE_LINK_SERVICE_E_MUX_ERROR:
53 return MOBILESYNC_E_MUX_ERROR;
54 case DEVICE_LINK_SERVICE_E_BAD_VERSION:
55 return MOBILESYNC_E_BAD_VERSION;
53 default: 56 default:
54 break; 57 break;
55 } 58 }
56 return MOBILESYNC_E_UNKNOWN_ERROR; 59 return MOBILESYNC_E_UNKNOWN_ERROR;
57} 60}
58 61
59mobilesync_error_t mobilesync_client_new(iphone_device_t device, int dst_port, 62mobilesync_error_t mobilesync_client_new(iphone_device_t device, uint16_t port,
60 mobilesync_client_t * client) 63 mobilesync_client_t * client)
61{ 64{
62 if (!device || dst_port == 0 || !client || *client) 65 if (!device || port == 0 || !client || *client)
63 return MOBILESYNC_E_INVALID_ARG; 66 return MOBILESYNC_E_INVALID_ARG;
64 67
65 mobilesync_error_t ret = MOBILESYNC_E_UNKNOWN_ERROR; 68 device_link_service_client_t dlclient = NULL;
66 69 mobilesync_error_t ret = mobilesync_error(device_link_service_client_new(device, port, &dlclient));
67 /* Attempt connection */ 70 if (ret != MOBILESYNC_E_SUCCESS) {
68 iphone_connection_t connection = NULL;
69 if (iphone_device_connect(device, dst_port, &connection) != IPHONE_E_SUCCESS) {
70 return ret; 71 return ret;
71 } 72 }
72 73
73 mobilesync_client_t client_loc = (mobilesync_client_t) malloc(sizeof(struct mobilesync_client_int)); 74 mobilesync_client_t client_loc = (mobilesync_client_t) malloc(sizeof(struct mobilesync_client_int));
74 client_loc->connection = connection; 75 client_loc->parent = dlclient;
75 76
76 /* perform handshake */ 77 /* perform handshake */
77 plist_t array = NULL; 78 ret = mobilesync_error(device_link_service_version_exchange(dlclient, MSYNC_VERSION_INT1, MSYNC_VERSION_INT2));
78 79 if (ret != MOBILESYNC_E_SUCCESS) {
79 /* first receive version */ 80 debug_info("version exchange failed, error %d", ret);
80 ret = mobilesync_recv(client_loc, &array); 81 mobilesync_client_free(client_loc);
81 82 return ret;
82 plist_t msg_node = plist_array_get_item(array, 0);
83
84 char* msg = NULL;
85 plist_type type = plist_get_node_type(msg_node);
86 if (PLIST_STRING == type) {
87 plist_get_string_val(msg_node, &msg);
88 }
89 if (PLIST_STRING != type || strcmp(msg, "DLMessageVersionExchange") || plist_array_get_size(array) < 3) {
90 log_debug_msg("%s: ERROR: MobileSync client expected a version exchange !\n", __func__);
91 }
92 free(msg);
93 msg = NULL;
94
95 plist_t ver_1 = plist_array_get_item(array, 1);
96 plist_t ver_2 = plist_array_get_item(array, 2);
97
98 plist_type ver_1_type = plist_get_node_type(ver_1);
99 plist_type ver_2_type = plist_get_node_type(ver_2);
100
101 if (PLIST_UINT == ver_1_type && PLIST_UINT == ver_2_type) {
102
103 uint64_t ver_1_val = 0;
104 uint64_t ver_2_val = 0;
105
106 plist_get_uint_val(ver_1, &ver_1_val);
107 plist_get_uint_val(ver_2, &ver_2_val);
108
109 plist_free(array);
110 array = NULL;
111
112 if (ver_1_type == PLIST_UINT && ver_2_type == PLIST_UINT && ver_1_val == MSYNC_VERSION_INT1
113 && ver_2_val == MSYNC_VERSION_INT2) {
114
115 array = plist_new_array();
116 plist_array_append_item(array, plist_new_string("DLMessageVersionExchange"));
117 plist_array_append_item(array, plist_new_string("DLVersionsOk"));
118
119 ret = mobilesync_send(client_loc, array);
120
121 plist_free(array);
122 array = NULL;
123
124 ret = mobilesync_recv(client_loc, &array);
125 plist_t rep_node = plist_array_get_item(array, 0);
126
127 type = plist_get_node_type(rep_node);
128 if (PLIST_STRING == type) {
129 plist_get_string_val(rep_node, &msg);
130 }
131 if (PLIST_STRING != type || strcmp(msg, "DLMessageDeviceReady")) {
132 log_debug_msg("%s: ERROR: MobileSync client failed to start session !\n", __func__);
133 ret = MOBILESYNC_E_BAD_VERSION;
134 }
135 else
136 {
137 ret = MOBILESYNC_E_SUCCESS;
138 *client = client_loc;
139 }
140 free(msg);
141 msg = NULL;
142
143 plist_free(array);
144 array = NULL;
145 }
146 } 83 }
147 84
148 if (MOBILESYNC_E_SUCCESS != ret) 85 *client = client_loc;
149 mobilesync_client_free(client_loc);
150 86
151 return ret; 87 return ret;
152} 88}
153 89
154static void mobilesync_disconnect(mobilesync_client_t client)
155{
156 if (!client)
157 return;
158
159 plist_t array = plist_new_array();
160 plist_array_append_item(array, plist_new_string("DLMessageDisconnect"));
161 plist_array_append_item(array, plist_new_string("All done, thanks for the memories"));
162
163 mobilesync_send(client, array);
164 plist_free(array);
165 array = NULL;
166}
167
168mobilesync_error_t mobilesync_client_free(mobilesync_client_t client) 90mobilesync_error_t mobilesync_client_free(mobilesync_client_t client)
169{ 91{
170 if (!client) 92 if (!client)
171 return IPHONE_E_INVALID_ARG; 93 return MOBILESYNC_E_INVALID_ARG;
172 94 device_link_service_disconnect(client->parent);
173 mobilesync_disconnect(client); 95 mobilesync_error_t err = mobilesync_error(device_link_service_client_free(client->parent));
174 return (iphone_device_disconnect(client->connection) == 0 ? MOBILESYNC_E_SUCCESS: MOBILESYNC_E_MUX_ERROR); 96 free(client);
97 return err;
175} 98}
176 99
177/** Polls the iPhone for MobileSync data. 100/** Polls the iPhone for MobileSync data.
@@ -183,19 +106,17 @@ mobilesync_error_t mobilesync_client_free(mobilesync_client_t client)
183 */ 106 */
184mobilesync_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist) 107mobilesync_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist)
185{ 108{
186 if (!client || !plist || (plist && *plist)) 109 if (!client)
187 return MOBILESYNC_E_INVALID_ARG; 110 return MOBILESYNC_E_INVALID_ARG;
188 111 mobilesync_error_t ret = mobilesync_error(device_link_service_receive(client->parent, plist));
189 mobilesync_error_t ret = iphone_to_mobilesync_error(iphone_device_receive_plist(client->connection, plist)); 112#ifndef STRIP_DEBUG_CODE
190 if (ret != MOBILESYNC_E_SUCCESS) { 113 if (ret != MOBILESYNC_E_SUCCESS) {
191 return MOBILESYNC_E_MUX_ERROR; 114 return ret;
192 } 115 }
193
194#ifndef STRIP_DEBUG_CODE
195 char *XMLContent = NULL; 116 char *XMLContent = NULL;
196 uint32_t length = 0; 117 uint32_t length = 0;
197 plist_to_xml(*plist, &XMLContent, &length); 118 plist_to_xml(*plist, &XMLContent, &length);
198 log_dbg_msg(DBGMASK_MOBILESYNC, "%s: plist size: %i\nbuffer :\n%s\n", __func__, length, XMLContent); 119 debug_info("plist size: %i\nbuffer :\n%s", length, XMLContent);
199 free(XMLContent); 120 free(XMLContent);
200#endif 121#endif
201 return ret; 122 return ret;
@@ -220,8 +141,8 @@ mobilesync_error_t mobilesync_send(mobilesync_client_t client, plist_t plist)
220 char *XMLContent = NULL; 141 char *XMLContent = NULL;
221 uint32_t length = 0; 142 uint32_t length = 0;
222 plist_to_xml(plist, &XMLContent, &length); 143 plist_to_xml(plist, &XMLContent, &length);
223 log_dbg_msg(DBGMASK_MOBILESYNC, "%s: plist size: %i\nbuffer :\n%s\n", __func__, length, XMLContent); 144 debug_info("plist size: %i\nbuffer :\n%s", length, XMLContent);
224 free(XMLContent); 145 free(XMLContent);
225#endif 146#endif
226 return (iphone_device_send_binary_plist(client->connection, plist) == IPHONE_E_SUCCESS ? MOBILESYNC_E_SUCCESS : MOBILESYNC_E_MUX_ERROR); 147 return mobilesync_error(device_link_service_send(client->parent, plist));
227} 148}