diff options
| author | 2013-02-23 00:49:26 +0100 | |
|---|---|---|
| committer | 2013-02-23 00:49:26 +0100 | |
| commit | 3658fc69f9e36e03638123dbf38cff4da74748e0 (patch) | |
| tree | 22a43c26f31a00cf11cafd24a5a21cbc3227aa95 /src/webinspector.c | |
| parent | 8afcea3008c993d9ed77228c3a45681086460f67 (diff) | |
| download | libimobiledevice-3658fc69f9e36e03638123dbf38cff4da74748e0.tar.gz libimobiledevice-3658fc69f9e36e03638123dbf38cff4da74748e0.tar.bz2 | |
webinspector: Add new service protocol implementation
Diffstat (limited to 'src/webinspector.c')
| -rw-r--r-- | src/webinspector.c | 261 |
1 files changed, 261 insertions, 0 deletions
diff --git a/src/webinspector.c b/src/webinspector.c new file mode 100644 index 0000000..ac87ce0 --- /dev/null +++ b/src/webinspector.c | |||
| @@ -0,0 +1,261 @@ | |||
| 1 | /* | ||
| 2 | * webinspector.c | ||
| 3 | * com.apple.webinspector service implementation. | ||
| 4 | * | ||
| 5 | * Copyright (c) 2013 Yury Melnichek 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 <plist/plist.h> | ||
| 28 | |||
| 29 | #include "webinspector.h" | ||
| 30 | #include "lockdown.h" | ||
| 31 | #include "debug.h" | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Convert a property_list_service_error_t value to a webinspector_error_t value. | ||
| 35 | * Used internally to get correct error codes. | ||
| 36 | * | ||
| 37 | * @param err An property_list_service_error_t error code | ||
| 38 | * | ||
| 39 | * @return A matching webinspector_error_t error code, | ||
| 40 | * WEBINSPECTOR_E_UNKNOWN_ERROR otherwise. | ||
| 41 | */ | ||
| 42 | static webinspector_error_t webinspector_error(property_list_service_error_t err) | ||
| 43 | { | ||
| 44 | switch (err) { | ||
| 45 | case PROPERTY_LIST_SERVICE_E_SUCCESS: | ||
| 46 | return WEBINSPECTOR_E_SUCCESS; | ||
| 47 | case PROPERTY_LIST_SERVICE_E_INVALID_ARG: | ||
| 48 | return WEBINSPECTOR_E_INVALID_ARG; | ||
| 49 | case PROPERTY_LIST_SERVICE_E_PLIST_ERROR: | ||
| 50 | return WEBINSPECTOR_E_PLIST_ERROR; | ||
| 51 | case PROPERTY_LIST_SERVICE_E_MUX_ERROR: | ||
| 52 | return WEBINSPECTOR_E_MUX_ERROR; | ||
| 53 | case PROPERTY_LIST_SERVICE_E_SSL_ERROR: | ||
| 54 | return WEBINSPECTOR_E_SSL_ERROR; | ||
| 55 | default: | ||
| 56 | break; | ||
| 57 | } | ||
| 58 | return WEBINSPECTOR_E_UNKNOWN_ERROR; | ||
| 59 | } | ||
| 60 | |||
| 61 | /** | ||
| 62 | * Connects to the webinspector service on the specified device. | ||
| 63 | * | ||
| 64 | * @param device The device to connect to. | ||
| 65 | * @param port Destination port (usually given by lockdownd_start_service). | ||
| 66 | * @param client Pointer that will point to a newly allocated | ||
| 67 | * webinspector_client_t upon successful return. Must be freed using | ||
| 68 | * webinspector_client_free() after use. | ||
| 69 | * | ||
| 70 | * @return WEBINSPECTOR_E_SUCCESS on success, WEBINSPECTOR_E_INVALID_ARG when | ||
| 71 | * client is NULL, or an WEBINSPECTOR_E_* error code otherwise. | ||
| 72 | */ | ||
| 73 | webinspector_error_t webinspector_client_new(idevice_t device, uint16_t port, webinspector_client_t * client) | ||
| 74 | { | ||
| 75 | *client = NULL; | ||
| 76 | |||
| 77 | debug_info("Creating webinspector_client, port = %d.\n", port); | ||
| 78 | |||
| 79 | if (!device || port == 0 || !client || *client) { | ||
| 80 | debug_info("Incorrect parameter passed to webinspector_client_new.\n"); | ||
| 81 | return WEBINSPECTOR_E_INVALID_ARG; | ||
| 82 | } | ||
| 83 | |||
| 84 | property_list_service_client_t plclient = NULL; | ||
| 85 | webinspector_error_t ret = webinspector_error(property_list_service_client_new(device, port, &plclient)); | ||
| 86 | if (ret != WEBINSPECTOR_E_SUCCESS) { | ||
| 87 | debug_info("Creating a property list client failed. Error: %i", ret); | ||
| 88 | return ret; | ||
| 89 | } | ||
| 90 | |||
| 91 | webinspector_client_t client_loc = (webinspector_client_t) malloc(sizeof(struct webinspector_client_private)); | ||
| 92 | client_loc->parent = plclient; | ||
| 93 | |||
| 94 | *client = client_loc; | ||
| 95 | |||
| 96 | debug_info("webinspector_client successfully created."); | ||
| 97 | return 0; | ||
| 98 | } | ||
| 99 | |||
| 100 | /** | ||
| 101 | * Starts a new webinspector service on the specified device and connects to it. | ||
| 102 | * | ||
| 103 | * @param device The device to connect to. | ||
| 104 | * @param client Pointer that will point to a newly allocated | ||
| 105 | * webinspector_client_t upon successful return. Must be freed using | ||
| 106 | * webinspector_client_free() after use. | ||
| 107 | * | ||
| 108 | * @return WEBINSPECTOR_E_SUCCESS on success, or an WEBINSPECTOR_E_* error | ||
| 109 | * code otherwise. | ||
| 110 | */ | ||
| 111 | webinspector_error_t webinspector_start_service(idevice_t device, webinspector_client_t * client) | ||
| 112 | { | ||
| 113 | *client = NULL; | ||
| 114 | |||
| 115 | lockdownd_client_t lckd = NULL; | ||
| 116 | if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &lckd, NULL)) { | ||
| 117 | idevice_free(device); | ||
| 118 | debug_info("Could not create a lockdown client.\n"); | ||
| 119 | return WEBINSPECTOR_E_UNKNOWN_ERROR; | ||
| 120 | } | ||
| 121 | |||
| 122 | uint16_t port = 0; | ||
| 123 | lockdownd_start_service(lckd, WEBINSPECTOR_SERVICE_NAME, &port); | ||
| 124 | lockdownd_client_free(lckd); | ||
| 125 | |||
| 126 | if (port <= 0) { | ||
| 127 | debug_info("Could not start webinspector service!\n"); | ||
| 128 | return WEBINSPECTOR_E_UNKNOWN_ERROR; | ||
| 129 | } | ||
| 130 | |||
| 131 | webinspector_error_t res = webinspector_client_new(device, port, client); | ||
| 132 | if (res != WEBINSPECTOR_E_SUCCESS) { | ||
| 133 | debug_info("Could not connect to webinspector! Port: %i, error: %i\n", port, res); | ||
| 134 | return res; | ||
| 135 | } | ||
| 136 | |||
| 137 | return WEBINSPECTOR_E_SUCCESS; | ||
| 138 | } | ||
| 139 | |||
| 140 | /** | ||
| 141 | * Disconnects a webinspector client from the device and frees up the | ||
| 142 | * webinspector client data. | ||
| 143 | * | ||
| 144 | * @param client The webinspector client to disconnect and free. | ||
| 145 | * | ||
| 146 | * @return WEBINSPECTOR_E_SUCCESS on success, WEBINSPECTOR_E_INVALID_ARG when | ||
| 147 | * client is NULL, or an WEBINSPECTOR_E_* error code otherwise. | ||
| 148 | */ | ||
| 149 | webinspector_error_t webinspector_client_free(webinspector_client_t client) | ||
| 150 | { | ||
| 151 | if (!client) | ||
| 152 | return WEBINSPECTOR_E_INVALID_ARG; | ||
| 153 | |||
| 154 | webinspector_error_t err = webinspector_error(property_list_service_client_free(client->parent)); | ||
| 155 | free(client); | ||
| 156 | |||
| 157 | return err; | ||
| 158 | } | ||
| 159 | |||
| 160 | /** | ||
| 161 | * Sends a plist to the service. | ||
| 162 | * | ||
| 163 | * @param client The webinspector client | ||
| 164 | * @param plist The plist to send | ||
| 165 | * | ||
| 166 | * @return DIAGNOSTICS_RELAY_E_SUCCESS on success, | ||
| 167 | * DIAGNOSTICS_RELAY_E_INVALID_ARG when client or plist is NULL | ||
| 168 | */ | ||
| 169 | webinspector_error_t webinspector_send(webinspector_client_t client, plist_t plist) | ||
| 170 | { | ||
| 171 | webinspector_error_t res = WEBINSPECTOR_E_UNKNOWN_ERROR; | ||
| 172 | char * buf = NULL; | ||
| 173 | uint32_t length = 0; | ||
| 174 | |||
| 175 | plist_to_bin(plist, &buf, &length); | ||
| 176 | if (!buf || length == 0) { | ||
| 177 | debug_info("Error converting plist to binary."); | ||
| 178 | return res; | ||
| 179 | } | ||
| 180 | |||
| 181 | plist_t outplist = plist_new_dict(); | ||
| 182 | plist_dict_insert_item(outplist, "WIRFinalMessageKey", plist_new_data(buf, length)); | ||
| 183 | free(buf); | ||
| 184 | |||
| 185 | res = webinspector_error(property_list_service_send_binary_plist(client->parent, outplist)); | ||
| 186 | plist_free(outplist); | ||
| 187 | if (res != WEBINSPECTOR_E_SUCCESS) { | ||
| 188 | debug_info("Sending plist failed with error %d", res); | ||
| 189 | return res; | ||
| 190 | } | ||
| 191 | |||
| 192 | return res; | ||
| 193 | } | ||
| 194 | |||
| 195 | /** | ||
| 196 | * Receives a plist from the service. | ||
| 197 | * | ||
| 198 | * @param client The webinspector client | ||
| 199 | * @param plist The plist to store the received data | ||
| 200 | * | ||
| 201 | * @return DIAGNOSTICS_RELAY_E_SUCCESS on success, | ||
| 202 | * DIAGNOSTICS_RELAY_E_INVALID_ARG when client or plist is NULL | ||
| 203 | */ | ||
| 204 | webinspector_error_t webinspector_receive(webinspector_client_t client, plist_t * plist) | ||
| 205 | { | ||
| 206 | return webinspector_receive_with_timeout(client, plist, 5000); | ||
| 207 | } | ||
| 208 | |||
| 209 | /** | ||
| 210 | * Receives a plist using the given webinspector client. | ||
| 211 | * | ||
| 212 | * @param client The webinspector client to use for receiving | ||
| 213 | * @param plist pointer to a plist_t that will point to the received plist | ||
| 214 | * upon successful return | ||
| 215 | * @param timeout Maximum time in milliseconds to wait for data. | ||
| 216 | * | ||
| 217 | * @return WEBINSPECTOR_E_SUCCESS on success, | ||
| 218 | * WEBINSPECTOR_E_INVALID_ARG when client or *plist is NULL, | ||
| 219 | * WEBINSPECTOR_E_PLIST_ERROR when the received data cannot be | ||
| 220 | * converted to a plist, WEBINSPECTOR_E_MUX_ERROR when a | ||
| 221 | * communication error occurs, or WEBINSPECTOR_E_UNKNOWN_ERROR | ||
| 222 | * when an unspecified error occurs. | ||
| 223 | */ | ||
| 224 | webinspector_error_t webinspector_receive_with_timeout(webinspector_client_t client, plist_t * plist, uint32_t timeout_ms) | ||
| 225 | { | ||
| 226 | webinspector_error_t res = WEBINSPECTOR_E_UNKNOWN_ERROR; | ||
| 227 | plist_t outplist = NULL; | ||
| 228 | |||
| 229 | res = webinspector_error(property_list_service_receive_plist_with_timeout(client->parent, &outplist, timeout_ms)); | ||
| 230 | if (res != WEBINSPECTOR_E_SUCCESS || !outplist) { | ||
| 231 | debug_info("Could not receive plist, error %d", res); | ||
| 232 | plist_free(outplist); | ||
| 233 | return WEBINSPECTOR_E_MUX_ERROR; | ||
| 234 | } | ||
| 235 | |||
| 236 | plist_t inplistdata = plist_dict_get_item(outplist, "WIRFinalMessageKey"); | ||
| 237 | if (!inplistdata) { | ||
| 238 | debug_info("Could not find the internal message plist."); | ||
| 239 | plist_free(outplist); | ||
| 240 | return WEBINSPECTOR_E_PLIST_ERROR; | ||
| 241 | } | ||
| 242 | |||
| 243 | char * buf; | ||
| 244 | uint64_t length64; | ||
| 245 | plist_get_data_val(inplistdata, &buf, &length64); | ||
| 246 | plist_free(outplist); | ||
| 247 | if (!buf || length64 == 0 || length64 > 0xFFFFFFFF) { | ||
| 248 | debug_info("Error getting the inner plist binary data."); | ||
| 249 | free(buf); | ||
| 250 | return WEBINSPECTOR_E_PLIST_ERROR; | ||
| 251 | } | ||
| 252 | |||
| 253 | plist_from_bin(buf, (uint32_t) length64, plist); | ||
| 254 | free(buf); | ||
| 255 | if (!*plist) { | ||
| 256 | debug_info("Error restoring the inner plist."); | ||
| 257 | return WEBINSPECTOR_E_PLIST_ERROR; | ||
| 258 | } | ||
| 259 | |||
| 260 | return res; | ||
| 261 | } | ||
