diff options
Diffstat (limited to 'src/webinspector.c')
| -rw-r--r-- | src/webinspector.c | 263 |
1 files changed, 263 insertions, 0 deletions
diff --git a/src/webinspector.c b/src/webinspector.c new file mode 100644 index 0000000..f960fcc --- /dev/null +++ b/src/webinspector.c | |||
| @@ -0,0 +1,263 @@ | |||
| 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 "common/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 | case PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT: | ||
| 56 | return WEBINSPECTOR_E_RECEIVE_TIMEOUT; | ||
| 57 | case PROPERTY_LIST_SERVICE_E_NOT_ENOUGH_DATA: | ||
| 58 | return WEBINSPECTOR_E_NOT_ENOUGH_DATA; | ||
| 59 | default: | ||
| 60 | break; | ||
| 61 | } | ||
| 62 | return WEBINSPECTOR_E_UNKNOWN_ERROR; | ||
| 63 | } | ||
| 64 | |||
| 65 | webinspector_error_t webinspector_client_new(idevice_t device, lockdownd_service_descriptor_t service, webinspector_client_t * client) | ||
| 66 | { | ||
| 67 | *client = NULL; | ||
| 68 | |||
| 69 | if (!device || !service || service->port == 0 || !client || *client) { | ||
| 70 | debug_info("Incorrect parameter passed to webinspector_client_new."); | ||
| 71 | return WEBINSPECTOR_E_INVALID_ARG; | ||
| 72 | } | ||
| 73 | |||
| 74 | debug_info("Creating webinspector_client, port = %d.", service->port); | ||
| 75 | |||
| 76 | property_list_service_client_t plclient = NULL; | ||
| 77 | webinspector_error_t ret = webinspector_error(property_list_service_client_new(device, service, &plclient)); | ||
| 78 | if (ret != WEBINSPECTOR_E_SUCCESS) { | ||
| 79 | debug_info("Creating a property list client failed. Error: %i", ret); | ||
| 80 | return ret; | ||
| 81 | } | ||
| 82 | |||
| 83 | webinspector_client_t client_loc = (webinspector_client_t) malloc(sizeof(struct webinspector_client_private)); | ||
| 84 | client_loc->parent = plclient; | ||
| 85 | |||
| 86 | *client = client_loc; | ||
| 87 | |||
| 88 | debug_info("webinspector_client successfully created."); | ||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | webinspector_error_t webinspector_client_start_service(idevice_t device, webinspector_client_t * client, const char* label) | ||
| 93 | { | ||
| 94 | webinspector_error_t err = WEBINSPECTOR_E_UNKNOWN_ERROR; | ||
| 95 | service_client_factory_start_service(device, WEBINSPECTOR_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(webinspector_client_new), &err); | ||
| 96 | return err; | ||
| 97 | } | ||
| 98 | |||
| 99 | webinspector_error_t webinspector_client_free(webinspector_client_t client) | ||
| 100 | { | ||
| 101 | if (!client) | ||
| 102 | return WEBINSPECTOR_E_INVALID_ARG; | ||
| 103 | |||
| 104 | webinspector_error_t err = webinspector_error(property_list_service_client_free(client->parent)); | ||
| 105 | free(client); | ||
| 106 | |||
| 107 | return err; | ||
| 108 | } | ||
| 109 | |||
| 110 | webinspector_error_t webinspector_send(webinspector_client_t client, plist_t plist) | ||
| 111 | { | ||
| 112 | webinspector_error_t res = WEBINSPECTOR_E_UNKNOWN_ERROR; | ||
| 113 | |||
| 114 | uint32_t offset = 0; | ||
| 115 | int is_final_message = 0; | ||
| 116 | |||
| 117 | char *packet = NULL; | ||
| 118 | uint32_t packet_length = 0; | ||
| 119 | |||
| 120 | debug_info("Sending webinspector message..."); | ||
| 121 | debug_plist(plist); | ||
| 122 | |||
| 123 | /* convert plist to packet */ | ||
| 124 | plist_to_bin(plist, &packet, &packet_length); | ||
| 125 | if (!packet || packet_length == 0) { | ||
| 126 | debug_info("Error converting plist to binary."); | ||
| 127 | return res; | ||
| 128 | } | ||
| 129 | |||
| 130 | do { | ||
| 131 | /* determine if we need to send partial messages */ | ||
| 132 | if (packet_length < WEBINSPECTOR_PARTIAL_PACKET_CHUNK_SIZE) { | ||
| 133 | is_final_message = 1; | ||
| 134 | } else { | ||
| 135 | /* send partial packet */ | ||
| 136 | is_final_message = 0; | ||
| 137 | } | ||
| 138 | |||
| 139 | plist_t outplist = plist_new_dict(); | ||
| 140 | if (!is_final_message) { | ||
| 141 | /* split packet into partial chunks */ | ||
| 142 | plist_dict_set_item(outplist, "WIRPartialMessageKey", plist_new_data(packet + offset, WEBINSPECTOR_PARTIAL_PACKET_CHUNK_SIZE)); | ||
| 143 | offset += WEBINSPECTOR_PARTIAL_PACKET_CHUNK_SIZE; | ||
| 144 | packet_length -= WEBINSPECTOR_PARTIAL_PACKET_CHUNK_SIZE; | ||
| 145 | } else { | ||
| 146 | /* send final chunk */ | ||
| 147 | plist_dict_set_item(outplist, "WIRFinalMessageKey", plist_new_data(packet + offset, packet_length)); | ||
| 148 | offset += packet_length; | ||
| 149 | packet_length -= packet_length; | ||
| 150 | } | ||
| 151 | |||
| 152 | res = webinspector_error(property_list_service_send_binary_plist(client->parent, outplist)); | ||
| 153 | plist_free(outplist); | ||
| 154 | outplist = NULL; | ||
| 155 | if (res != WEBINSPECTOR_E_SUCCESS) { | ||
| 156 | debug_info("Sending plist failed with error %d", res); | ||
| 157 | return res; | ||
| 158 | } | ||
| 159 | } while(packet_length > 0); | ||
| 160 | |||
| 161 | free(packet); | ||
| 162 | packet = NULL; | ||
| 163 | |||
| 164 | return res; | ||
| 165 | } | ||
| 166 | |||
| 167 | webinspector_error_t webinspector_receive(webinspector_client_t client, plist_t * plist) | ||
| 168 | { | ||
| 169 | return webinspector_receive_with_timeout(client, plist, 5000); | ||
| 170 | } | ||
| 171 | |||
| 172 | webinspector_error_t webinspector_receive_with_timeout(webinspector_client_t client, plist_t * plist, uint32_t timeout_ms) | ||
| 173 | { | ||
| 174 | webinspector_error_t res = WEBINSPECTOR_E_UNKNOWN_ERROR; | ||
| 175 | plist_t message = NULL; | ||
| 176 | plist_t key = NULL; | ||
| 177 | |||
| 178 | int is_final_message = 1; | ||
| 179 | |||
| 180 | char* buffer = NULL; | ||
| 181 | uint64_t length = 0; | ||
| 182 | |||
| 183 | char* packet = NULL; | ||
| 184 | char* newpacket = NULL; | ||
| 185 | uint64_t packet_length = 0; | ||
| 186 | |||
| 187 | debug_info("Receiving webinspector message..."); | ||
| 188 | |||
| 189 | do { | ||
| 190 | /* receive message */ | ||
| 191 | res = webinspector_error(property_list_service_receive_plist_with_timeout(client->parent, &message, timeout_ms)); | ||
| 192 | if (res != WEBINSPECTOR_E_SUCCESS || !message) { | ||
| 193 | debug_info("Could not receive message, error %d", res); | ||
| 194 | plist_free(message); | ||
| 195 | return WEBINSPECTOR_E_MUX_ERROR; | ||
| 196 | } | ||
| 197 | |||
| 198 | /* get message key */ | ||
| 199 | key = plist_dict_get_item(message, "WIRFinalMessageKey"); | ||
| 200 | if (!key) { | ||
| 201 | key = plist_dict_get_item(message, "WIRPartialMessageKey"); | ||
| 202 | if (!key) { | ||
| 203 | debug_info("ERROR: Unable to read message key."); | ||
| 204 | plist_free(message); | ||
| 205 | return WEBINSPECTOR_E_PLIST_ERROR; | ||
| 206 | } | ||
| 207 | is_final_message = 0; | ||
| 208 | } else { | ||
| 209 | is_final_message = 1; | ||
| 210 | } | ||
| 211 | |||
| 212 | /* read partial data */ | ||
| 213 | plist_get_data_val(key, &buffer, &length); | ||
| 214 | if (!buffer || length == 0 || length > 0xFFFFFFFF) { | ||
| 215 | debug_info("ERROR: Unable to get the inner plist binary data."); | ||
| 216 | free(packet); | ||
| 217 | free(buffer); | ||
| 218 | return WEBINSPECTOR_E_PLIST_ERROR; | ||
| 219 | } | ||
| 220 | |||
| 221 | /* (re)allocate packet data */ | ||
| 222 | if (!packet) { | ||
| 223 | packet = (char*)malloc(length * sizeof(char)); | ||
| 224 | } else { | ||
| 225 | newpacket = (char*)realloc(packet, (packet_length + length) * sizeof(char)); | ||
| 226 | packet = newpacket; | ||
| 227 | } | ||
| 228 | |||
| 229 | /* copy partial data into final packet data */ | ||
| 230 | memcpy(packet + packet_length, buffer, length); | ||
| 231 | |||
| 232 | /* cleanup buffer */ | ||
| 233 | free(buffer); | ||
| 234 | buffer = NULL; | ||
| 235 | |||
| 236 | if (message) { | ||
| 237 | plist_free(message); | ||
| 238 | message = NULL; | ||
| 239 | } | ||
| 240 | |||
| 241 | /* adjust packet length */ | ||
| 242 | packet_length += length; | ||
| 243 | length = 0; | ||
| 244 | } while(!is_final_message); | ||
| 245 | |||
| 246 | /* read final message */ | ||
| 247 | if (packet_length) { | ||
| 248 | plist_from_bin(packet, (uint32_t)packet_length, plist); | ||
| 249 | if (!*plist) { | ||
| 250 | debug_info("Error restoring the final plist."); | ||
| 251 | free(packet); | ||
| 252 | return WEBINSPECTOR_E_PLIST_ERROR; | ||
| 253 | } | ||
| 254 | |||
| 255 | debug_plist(*plist); | ||
| 256 | } | ||
| 257 | |||
| 258 | if (packet) { | ||
| 259 | free(packet); | ||
| 260 | } | ||
| 261 | |||
| 262 | return res; | ||
| 263 | } | ||
