diff options
Diffstat (limited to 'src/heartbeat.c')
| -rw-r--r-- | src/heartbeat.c | 147 | 
1 files changed, 147 insertions, 0 deletions
| diff --git a/src/heartbeat.c b/src/heartbeat.c new file mode 100644 index 0000000..3945d73 --- /dev/null +++ b/src/heartbeat.c | |||
| @@ -0,0 +1,147 @@ | |||
| 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 "common/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 | case PROPERTY_LIST_SERVICE_E_NOT_ENOUGH_DATA: | ||
| 56 | return HEARTBEAT_E_NOT_ENOUGH_DATA; | ||
| 57 | case PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT: | ||
| 58 | return HEARTBEAT_E_TIMEOUT; | ||
| 59 | default: | ||
| 60 | break; | ||
| 61 | } | ||
| 62 | return HEARTBEAT_E_UNKNOWN_ERROR; | ||
| 63 | } | ||
| 64 | |||
| 65 | heartbeat_error_t heartbeat_client_new(idevice_t device, lockdownd_service_descriptor_t service, heartbeat_client_t * client) | ||
| 66 | { | ||
| 67 | *client = NULL; | ||
| 68 | |||
| 69 | if (!device || !service || service->port == 0 || !client || *client) { | ||
| 70 | debug_info("Incorrect parameter passed to heartbeat_client_new."); | ||
| 71 | return HEARTBEAT_E_INVALID_ARG; | ||
| 72 | } | ||
| 73 | |||
| 74 | debug_info("Creating heartbeat_client, port = %d.", service->port); | ||
| 75 | |||
| 76 | property_list_service_client_t plclient = NULL; | ||
| 77 | heartbeat_error_t ret = heartbeat_error(property_list_service_client_new(device, service, &plclient)); | ||
| 78 | if (ret != HEARTBEAT_E_SUCCESS) { | ||
| 79 | debug_info("Creating a property list client failed. Error: %i", ret); | ||
| 80 | return ret; | ||
| 81 | } | ||
| 82 | |||
| 83 | heartbeat_client_t client_loc = (heartbeat_client_t) malloc(sizeof(struct heartbeat_client_private)); | ||
| 84 | client_loc->parent = plclient; | ||
| 85 | |||
| 86 | *client = client_loc; | ||
| 87 | |||
| 88 | debug_info("heartbeat_client successfully created."); | ||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | heartbeat_error_t heartbeat_client_start_service(idevice_t device, heartbeat_client_t * client, const char* label) | ||
| 93 | { | ||
| 94 | heartbeat_error_t err = HEARTBEAT_E_UNKNOWN_ERROR; | ||
| 95 | service_client_factory_start_service(device, HEARTBEAT_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(heartbeat_client_new), &err); | ||
| 96 | return err; | ||
| 97 | } | ||
| 98 | |||
| 99 | heartbeat_error_t heartbeat_client_free(heartbeat_client_t client) | ||
| 100 | { | ||
| 101 | if (!client) | ||
| 102 | return HEARTBEAT_E_INVALID_ARG; | ||
| 103 | |||
| 104 | heartbeat_error_t err = heartbeat_error(property_list_service_client_free(client->parent)); | ||
| 105 | free(client); | ||
| 106 | |||
| 107 | return err; | ||
| 108 | } | ||
| 109 | |||
| 110 | heartbeat_error_t heartbeat_send(heartbeat_client_t client, plist_t plist) | ||
| 111 | { | ||
| 112 | heartbeat_error_t res = HEARTBEAT_E_UNKNOWN_ERROR; | ||
| 113 | |||
| 114 | res = heartbeat_error(property_list_service_send_binary_plist(client->parent, plist)); | ||
| 115 | if (res != HEARTBEAT_E_SUCCESS) { | ||
| 116 | debug_info("Sending plist failed with error %d", res); | ||
| 117 | return res; | ||
| 118 | } | ||
| 119 | |||
| 120 | debug_plist(plist); | ||
| 121 | |||
| 122 | return res; | ||
| 123 | } | ||
| 124 | |||
| 125 | heartbeat_error_t heartbeat_receive(heartbeat_client_t client, plist_t * plist) | ||
| 126 | { | ||
| 127 | return heartbeat_receive_with_timeout(client, plist, 1000); | ||
| 128 | } | ||
| 129 | |||
| 130 | heartbeat_error_t heartbeat_receive_with_timeout(heartbeat_client_t client, plist_t * plist, uint32_t timeout_ms) | ||
| 131 | { | ||
| 132 | heartbeat_error_t res = HEARTBEAT_E_UNKNOWN_ERROR; | ||
| 133 | plist_t outplist = NULL; | ||
| 134 | |||
| 135 | res = heartbeat_error(property_list_service_receive_plist_with_timeout(client->parent, &outplist, timeout_ms)); | ||
| 136 | if (res != HEARTBEAT_E_SUCCESS || !outplist) { | ||
| 137 | debug_info("Could not receive plist, error %d", res); | ||
| 138 | plist_free(outplist); | ||
| 139 | return HEARTBEAT_E_MUX_ERROR; | ||
| 140 | } | ||
| 141 | |||
| 142 | *plist = outplist; | ||
| 143 | |||
| 144 | debug_plist(*plist); | ||
| 145 | |||
| 146 | return res; | ||
| 147 | } | ||
