summaryrefslogtreecommitdiffstats
path: root/src/heartbeat.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/heartbeat.c')
-rw-r--r--src/heartbeat.c235
1 files changed, 235 insertions, 0 deletions
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 */
42static 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 */
73heartbeat_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 */
111heartbeat_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 */
154heartbeat_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 */
174heartbeat_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 */
198heartbeat_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 */
218heartbeat_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}