diff options
| author | 2013-02-26 03:23:34 +0100 | |
|---|---|---|
| committer | 2013-02-26 03:23:34 +0100 | |
| commit | c5d46be5fb2103c6e9ecbd684f910e00fe16e4cd (patch) | |
| tree | fcf436f56e48d6b7f1241421dfed42a672114c53 | |
| parent | fcc1bb855efb6860417ed827d3b50feba24a9a8b (diff) | |
| download | libimobiledevice-c5d46be5fb2103c6e9ecbd684f910e00fe16e4cd.tar.gz libimobiledevice-c5d46be5fb2103c6e9ecbd684f910e00fe16e4cd.tar.bz2 | |
heartbeat: Implement basic "network heartbeat" service protocol
| -rw-r--r-- | include/Makefile.am | 1 | ||||
| -rw-r--r-- | include/libimobiledevice/heartbeat.h | 64 | ||||
| -rw-r--r-- | src/Makefile.am | 1 | ||||
| -rw-r--r-- | src/heartbeat.c | 235 | ||||
| -rw-r--r-- | src/heartbeat.h | 32 |
5 files changed, 333 insertions, 0 deletions
diff --git a/include/Makefile.am b/include/Makefile.am index c69ad04..a697453 100644 --- a/include/Makefile.am +++ b/include/Makefile.am | |||
| @@ -16,4 +16,5 @@ nobase_include_HEADERS = libimobiledevice/libimobiledevice.h \ | |||
| 16 | libimobiledevice/misagent.h \ | 16 | libimobiledevice/misagent.h \ |
| 17 | libimobiledevice/restore.h\ | 17 | libimobiledevice/restore.h\ |
| 18 | libimobiledevice/webinspector.h\ | 18 | libimobiledevice/webinspector.h\ |
| 19 | libimobiledevice/heartbeat.h\ | ||
| 19 | libimobiledevice/diagnostics_relay.h | 20 | libimobiledevice/diagnostics_relay.h |
diff --git a/include/libimobiledevice/heartbeat.h b/include/libimobiledevice/heartbeat.h new file mode 100644 index 0000000..e5adb0d --- /dev/null +++ b/include/libimobiledevice/heartbeat.h | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | /** | ||
| 2 | * @file libimobiledevice/heartbeat.h | ||
| 3 | * @brief Send "heartbeat" to device to allow service connections over network. | ||
| 4 | * \internal | ||
| 5 | * | ||
| 6 | * Copyright (c) 2013 Martin Szulecki All Rights Reserved. | ||
| 7 | * | ||
| 8 | * This library is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * This library is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with this library; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifndef IHEARTBEAT_H | ||
| 24 | #define IHEARTBEAT_H | ||
| 25 | |||
| 26 | #ifdef __cplusplus | ||
| 27 | extern "C" { | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #include <libimobiledevice/libimobiledevice.h> | ||
| 31 | #include <libimobiledevice/lockdown.h> | ||
| 32 | |||
| 33 | #define HEARTBEAT_SERVICE_NAME "com.apple.mobile.heartbeat" | ||
| 34 | |||
| 35 | /** @name Error Codes */ | ||
| 36 | /*@{*/ | ||
| 37 | #define HEARTBEAT_E_SUCCESS 0 | ||
| 38 | #define HEARTBEAT_E_INVALID_ARG -1 | ||
| 39 | #define HEARTBEAT_E_PLIST_ERROR -2 | ||
| 40 | #define HEARTBEAT_E_MUX_ERROR -3 | ||
| 41 | #define HEARTBEAT_E_SSL_ERROR -4 | ||
| 42 | #define HEARTBEAT_E_UNKNOWN_ERROR -256 | ||
| 43 | /*@}*/ | ||
| 44 | |||
| 45 | /** Represents an error code. */ | ||
| 46 | typedef int16_t heartbeat_error_t; | ||
| 47 | |||
| 48 | typedef struct heartbeat_client_private heartbeat_client_private; | ||
| 49 | typedef heartbeat_client_private *heartbeat_client_t; /**< The client handle. */ | ||
| 50 | |||
| 51 | heartbeat_error_t heartbeat_client_new(idevice_t device, lockdownd_service_descriptor_t service, heartbeat_client_t * client); | ||
| 52 | heartbeat_error_t heartbeat_client_free(heartbeat_client_t client); | ||
| 53 | |||
| 54 | heartbeat_error_t heartbeat_start_service(idevice_t device, heartbeat_client_t * client); | ||
| 55 | |||
| 56 | heartbeat_error_t heartbeat_send(heartbeat_client_t client, plist_t plist); | ||
| 57 | heartbeat_error_t heartbeat_receive(heartbeat_client_t client, plist_t * plist); | ||
| 58 | heartbeat_error_t heartbeat_receive_with_timeout(heartbeat_client_t client, plist_t * plist, uint32_t timeout_ms); | ||
| 59 | |||
| 60 | #ifdef __cplusplus | ||
| 61 | } | ||
| 62 | #endif | ||
| 63 | |||
| 64 | #endif | ||
diff --git a/src/Makefile.am b/src/Makefile.am index 4b1c56c..136baf4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
| @@ -26,6 +26,7 @@ libimobiledevice_la_SOURCES = idevice.c idevice.h \ | |||
| 26 | misagent.c misagent.h\ | 26 | misagent.c misagent.h\ |
| 27 | restore.c restore.h\ | 27 | restore.c restore.h\ |
| 28 | diagnostics_relay.c diagnostics_relay.h\ | 28 | diagnostics_relay.c diagnostics_relay.h\ |
| 29 | heartbeat.c heartbeat.h\ | ||
| 29 | webinspector.c webinspector.h | 30 | webinspector.c webinspector.h |
| 30 | 31 | ||
| 31 | if WIN32 | 32 | if WIN32 |
diff --git a/src/heartbeat.c b/src/heartbeat.c new file mode 100644 index 0000000..c97c178 --- /dev/null +++ b/src/heartbeat.c | |||
| @@ -0,0 +1,235 @@ | |||
| 1 | /* | ||
| 2 | * heartbeat.c | ||
| 3 | * com.apple.mobile.heartbeat service implementation. | ||
| 4 | * | ||
| 5 | * Copyright (c) 2013 Martin Szulecki 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 "heartbeat.h" | ||
| 30 | #include "lockdown.h" | ||
| 31 | #include "debug.h" | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Convert a property_list_service_error_t value to a heartbeat_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 heartbeat_error_t error code, | ||
| 40 | * HEARTBEAT_E_UNKNOWN_ERROR otherwise. | ||
| 41 | */ | ||
| 42 | static heartbeat_error_t heartbeat_error(property_list_service_error_t err) | ||
| 43 | { | ||
| 44 | switch (err) { | ||
| 45 | case PROPERTY_LIST_SERVICE_E_SUCCESS: | ||
| 46 | return HEARTBEAT_E_SUCCESS; | ||
| 47 | case PROPERTY_LIST_SERVICE_E_INVALID_ARG: | ||
| 48 | return HEARTBEAT_E_INVALID_ARG; | ||
| 49 | case PROPERTY_LIST_SERVICE_E_PLIST_ERROR: | ||
| 50 | return HEARTBEAT_E_PLIST_ERROR; | ||
| 51 | case PROPERTY_LIST_SERVICE_E_MUX_ERROR: | ||
| 52 | return HEARTBEAT_E_MUX_ERROR; | ||
| 53 | case PROPERTY_LIST_SERVICE_E_SSL_ERROR: | ||
| 54 | return HEARTBEAT_E_SSL_ERROR; | ||
| 55 | default: | ||
| 56 | break; | ||
| 57 | } | ||
| 58 | return HEARTBEAT_E_UNKNOWN_ERROR; | ||
| 59 | } | ||
| 60 | |||
| 61 | /** | ||
| 62 | * Connects to the heartbeat service on the specified device. | ||
| 63 | * | ||
| 64 | * @param device The device to connect to. | ||
| 65 | * @param service The service descriptor returned by lockdownd_start_service. | ||
| 66 | * @param client Pointer that will point to a newly allocated | ||
| 67 | * heartbeat_client_t upon successful return. Must be freed using | ||
| 68 | * heartbeat_client_free() after use. | ||
| 69 | * | ||
| 70 | * @return HEARTBEAT_E_SUCCESS on success, HEARTBEAT_E_INVALID_ARG when | ||
| 71 | * client is NULL, or an HEARTBEAT_E_* error code otherwise. | ||
| 72 | */ | ||
| 73 | heartbeat_error_t heartbeat_client_new(idevice_t device, lockdownd_service_descriptor_t service, heartbeat_client_t * client) | ||
| 74 | { | ||
| 75 | *client = NULL; | ||
| 76 | |||
| 77 | debug_info("Creating heartbeat_client, port = %d.", service->port); | ||
| 78 | |||
| 79 | if (!device || service->port == 0 || !client || *client) { | ||
| 80 | debug_info("Incorrect parameter passed to heartbeat_client_new."); | ||
| 81 | return HEARTBEAT_E_INVALID_ARG; | ||
| 82 | } | ||
| 83 | |||
| 84 | property_list_service_client_t plclient = NULL; | ||
| 85 | heartbeat_error_t ret = heartbeat_error(property_list_service_client_new(device, service, &plclient)); | ||
| 86 | if (ret != HEARTBEAT_E_SUCCESS) { | ||
| 87 | debug_info("Creating a property list client failed. Error: %i", ret); | ||
| 88 | return ret; | ||
| 89 | } | ||
| 90 | |||
| 91 | heartbeat_client_t client_loc = (heartbeat_client_t) malloc(sizeof(struct heartbeat_client_private)); | ||
| 92 | client_loc->parent = plclient; | ||
| 93 | |||
| 94 | *client = client_loc; | ||
| 95 | |||
| 96 | debug_info("heartbeat_client successfully created."); | ||
| 97 | return 0; | ||
| 98 | } | ||
| 99 | |||
| 100 | /** | ||
| 101 | * Starts a new heartbeat 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 | * heartbeat_client_t upon successful return. Must be freed using | ||
| 106 | * heartbeat_client_free() after use. | ||
| 107 | * | ||
| 108 | * @return HEARTBEAT_E_SUCCESS on success, or an HEARTBEAT_E_* error | ||
| 109 | * code otherwise. | ||
| 110 | */ | ||
| 111 | heartbeat_error_t heartbeat_start_service(idevice_t device, heartbeat_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."); | ||
| 119 | return HEARTBEAT_E_UNKNOWN_ERROR; | ||
| 120 | } | ||
| 121 | |||
| 122 | lockdownd_service_descriptor_t service = NULL; | ||
| 123 | lockdownd_start_service(lckd, HEARTBEAT_SERVICE_NAME, &service); | ||
| 124 | lockdownd_client_free(lckd); | ||
| 125 | |||
| 126 | if (service->port <= 0) { | ||
| 127 | debug_info("Could not start heartbeat service!"); | ||
| 128 | return HEARTBEAT_E_UNKNOWN_ERROR; | ||
| 129 | } | ||
| 130 | |||
| 131 | heartbeat_error_t res = heartbeat_client_new(device, service, client); | ||
| 132 | if (res != HEARTBEAT_E_SUCCESS) { | ||
| 133 | debug_info("Could not connect to heartbeat! Port: %i, error: %i", service->port, res); | ||
| 134 | return res; | ||
| 135 | } | ||
| 136 | |||
| 137 | if (service) { | ||
| 138 | lockdownd_service_descriptor_free(service); | ||
| 139 | service = NULL; | ||
| 140 | } | ||
| 141 | |||
| 142 | return HEARTBEAT_E_SUCCESS; | ||
| 143 | } | ||
| 144 | |||
| 145 | /** | ||
| 146 | * Disconnects a heartbeat client from the device and frees up the | ||
| 147 | * heartbeat client data. | ||
| 148 | * | ||
| 149 | * @param client The heartbeat client to disconnect and free. | ||
| 150 | * | ||
| 151 | * @return HEARTBEAT_E_SUCCESS on success, HEARTBEAT_E_INVALID_ARG when | ||
| 152 | * client is NULL, or an HEARTBEAT_E_* error code otherwise. | ||
| 153 | */ | ||
| 154 | heartbeat_error_t heartbeat_client_free(heartbeat_client_t client) | ||
| 155 | { | ||
| 156 | if (!client) | ||
| 157 | return HEARTBEAT_E_INVALID_ARG; | ||
| 158 | |||
| 159 | heartbeat_error_t err = heartbeat_error(property_list_service_client_free(client->parent)); | ||
| 160 | free(client); | ||
| 161 | |||
| 162 | return err; | ||
| 163 | } | ||
| 164 | |||
| 165 | /** | ||
| 166 | * Sends a plist to the service. | ||
| 167 | * | ||
| 168 | * @param client The heartbeat client | ||
| 169 | * @param plist The plist to send | ||
| 170 | * | ||
| 171 | * @return DIAGNOSTICS_RELAY_E_SUCCESS on success, | ||
| 172 | * DIAGNOSTICS_RELAY_E_INVALID_ARG when client or plist is NULL | ||
| 173 | */ | ||
| 174 | heartbeat_error_t heartbeat_send(heartbeat_client_t client, plist_t plist) | ||
| 175 | { | ||
| 176 | heartbeat_error_t res = HEARTBEAT_E_UNKNOWN_ERROR; | ||
| 177 | |||
| 178 | res = heartbeat_error(property_list_service_send_binary_plist(client->parent, plist)); | ||
| 179 | if (res != HEARTBEAT_E_SUCCESS) { | ||
| 180 | debug_info("Sending plist failed with error %d", res); | ||
| 181 | return res; | ||
| 182 | } | ||
| 183 | |||
| 184 | debug_plist(plist); | ||
| 185 | |||
| 186 | return res; | ||
| 187 | } | ||
| 188 | |||
| 189 | /** | ||
| 190 | * Receives a plist from the service. | ||
| 191 | * | ||
| 192 | * @param client The heartbeat client | ||
| 193 | * @param plist The plist to store the received data | ||
| 194 | * | ||
| 195 | * @return DIAGNOSTICS_RELAY_E_SUCCESS on success, | ||
| 196 | * DIAGNOSTICS_RELAY_E_INVALID_ARG when client or plist is NULL | ||
| 197 | */ | ||
| 198 | heartbeat_error_t heartbeat_receive(heartbeat_client_t client, plist_t * plist) | ||
| 199 | { | ||
| 200 | return heartbeat_receive_with_timeout(client, plist, 1000); | ||
| 201 | } | ||
| 202 | |||
| 203 | /** | ||
| 204 | * Receives a plist using the given heartbeat client. | ||
| 205 | * | ||
| 206 | * @param client The heartbeat client to use for receiving | ||
| 207 | * @param plist pointer to a plist_t that will point to the received plist | ||
| 208 | * upon successful return | ||
| 209 | * @param timeout Maximum time in milliseconds to wait for data. | ||
| 210 | * | ||
| 211 | * @return HEARTBEAT_E_SUCCESS on success, | ||
| 212 | * HEARTBEAT_E_INVALID_ARG when client or *plist is NULL, | ||
| 213 | * HEARTBEAT_E_PLIST_ERROR when the received data cannot be | ||
| 214 | * converted to a plist, HEARTBEAT_E_MUX_ERROR when a | ||
| 215 | * communication error occurs, or HEARTBEAT_E_UNKNOWN_ERROR | ||
| 216 | * when an unspecified error occurs. | ||
| 217 | */ | ||
| 218 | heartbeat_error_t heartbeat_receive_with_timeout(heartbeat_client_t client, plist_t * plist, uint32_t timeout_ms) | ||
| 219 | { | ||
| 220 | heartbeat_error_t res = HEARTBEAT_E_UNKNOWN_ERROR; | ||
| 221 | plist_t outplist = NULL; | ||
| 222 | |||
| 223 | res = heartbeat_error(property_list_service_receive_plist_with_timeout(client->parent, &outplist, timeout_ms)); | ||
| 224 | if (res != HEARTBEAT_E_SUCCESS || !outplist) { | ||
| 225 | debug_info("Could not receive plist, error %d", res); | ||
| 226 | plist_free(outplist); | ||
| 227 | return HEARTBEAT_E_MUX_ERROR; | ||
| 228 | } | ||
| 229 | |||
| 230 | *plist = outplist; | ||
| 231 | |||
| 232 | debug_plist(*plist); | ||
| 233 | |||
| 234 | return res; | ||
| 235 | } | ||
diff --git a/src/heartbeat.h b/src/heartbeat.h new file mode 100644 index 0000000..f2e9aa3 --- /dev/null +++ b/src/heartbeat.h | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | /* | ||
| 2 | * heartbeat.h | ||
| 3 | * com.apple.mobile.heartbeat service header file. | ||
| 4 | * | ||
| 5 | * Copyright (c) 2013 Martin Szulecki 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 | #ifndef HEARTBEAT_H | ||
| 23 | #define HEARTBEAT_H | ||
| 24 | |||
| 25 | #include "libimobiledevice/heartbeat.h" | ||
| 26 | #include "property_list_service.h" | ||
| 27 | |||
| 28 | struct heartbeat_client_private { | ||
| 29 | property_list_service_client_t parent; | ||
| 30 | }; | ||
| 31 | |||
| 32 | #endif | ||
