summaryrefslogtreecommitdiffstats
path: root/src/NotificationProxy.c
blob: bf837bf3028f00a967b090e4c8110e584d526b2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * NotificationProxy.c
 * Notification Proxy implementation.
 *
 * Copyright (c) 2009 Nikias Bassen, All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 */

#include <string.h>
#include <stdio.h>
#include <plist/plist.h>
#include "NotificationProxy.h"
#include "utils.h"

/** Locks an NP client, done for thread safety stuff.
 *
 * @param client The NP
 */
static void np_lock(iphone_np_client_t client)
{
	log_debug_msg("NP: Locked\n");
	g_mutex_lock(client->mutex);
}

/** Unlocks an NP client, done for thread safety stuff.
 * 
 * @param client The NP
 */
static void np_unlock(iphone_np_client_t client)
{
	log_debug_msg("NP: Unlocked\n");
	g_mutex_unlock(client->mutex);
}

/** Makes a connection to the NP service on the phone. 
 * 
 * @param phone The iPhone to connect on.
 * @param s_port The source port. 
 * @param d_port The destination port. 
 * 
 * @return A handle to the newly-connected client or NULL upon error.
 */
iphone_error_t iphone_np_new_client(iphone_device_t device, int src_port, int dst_port, iphone_np_client_t * client)
{
	int ret = IPHONE_E_SUCCESS;

	//makes sure thread environment is available
	if (!g_thread_supported())
		g_thread_init(NULL);
	iphone_np_client_t client_loc = (iphone_np_client_t) malloc(sizeof(struct iphone_np_client_int));

	if (!device)
		return IPHONE_E_INVALID_ARG;

	// Attempt connection
	client_loc->connection = NULL;
	ret = iphone_mux_new_client(device, src_port, dst_port, &client_loc->connection);
	if (IPHONE_E_SUCCESS != ret || !client_loc->connection) {
		free(client_loc);
		return ret;
	}

	client_loc->mutex = g_mutex_new();

	*client = client_loc;
	return IPHONE_E_SUCCESS;
}

/** Disconnects an NP client from the phone.
 * 
 * @param client The client to disconnect.
 */
iphone_error_t iphone_np_free_client(iphone_np_client_t client)
{
	if (!client || !client->connection)
		return IPHONE_E_INVALID_ARG;

	iphone_mux_free_client(client->connection);
	free(client);
	return IPHONE_E_SUCCESS;
}

/** Sends a notification to the NP client.
 *
 * notification messages seen so far:
 *   com.apple.itunes-mobdev.syncWillStart
 *   com.apple.itunes-mobdev.syncDidStart
 *
 * @param client The client to send to
 * @param notification The notification Message
 */
iphone_error_t iphone_np_post_notification(iphone_np_client_t client, const char *notification)
{
	char *XML_content = NULL;
	uint32_t length = 0;
	int bytes = 0;
	iphone_error_t ret;
	unsigned char sndbuf[4096];
	int sndlen = 0;
	int nlen = 0;
	plist_t dict = NULL;

	if (!client || !notification) {
		return IPHONE_E_INVALID_ARG;
	}
	np_lock(client);

	dict = plist_new_dict();
	plist_add_sub_key_el(dict, "Command");
	plist_add_sub_string_el(dict, "PostNotification");
	plist_add_sub_key_el(dict, "Name");
	plist_add_sub_string_el(dict, notification);
	plist_to_xml(dict, &XML_content, &length);

	nlen = htonl(length);

	memcpy(sndbuf + sndlen, &nlen, 4);
	sndlen += 4;
	memcpy(sndbuf + sndlen, XML_content, length);
	sndlen += length;

	plist_free(dict);
	dict = NULL;
	free(XML_content);
	XML_content = NULL;

	dict = plist_new_dict();
	plist_add_sub_key_el(dict, "Command");
	plist_add_sub_string_el(dict, "Shutdown");
	plist_to_xml(dict, &XML_content, &length);

	nlen = htonl(length);

	memcpy(sndbuf + sndlen, &nlen, 4);
	sndlen += 4;

	memcpy(sndbuf + sndlen, XML_content, length);
	sndlen += length;

	plist_free(dict);
	dict = NULL;
	free(XML_content);
	XML_content = NULL;

	log_debug_buffer(sndbuf, sndlen);

	iphone_mux_send(client->connection, sndbuf, sndlen, &bytes);
	if (bytes <= 0) {
		np_unlock(client);
		return bytes;
	}

	np_unlock(client);
	return bytes;
}

/** Notifies the iphone to send a notification on certain events.
 *
 * observation messages seen so far:
 *   com.apple.itunes-client.syncCancelRequest
 *   com.apple.itunes-client.syncSuspendRequest
 *   com.apple.itunes-client.syncResumeRequest
 *   com.apple.mobile.lockdown.phone_number_changed
 *   com.apple.mobile.lockdown.device_name_changed
 *   com.apple.springboard.attemptactivation
 *   com.apple.mobile.data_sync.domain_changed
 *   com.apple.mobile.application_installed
 *   com.apple.mobile.application_uninstalled
 *
 * @param client The client to send to
 */
iphone_error_t iphone_np_observe_notification(iphone_np_client_t client)
{
	plist_t dict = NULL;
	char *XML_content = NULL;
	uint32_t length = 0;
	int bytes = 0;
	iphone_error_t ret;
	unsigned char sndbuf[4096];
	int sndlen = 0;
	int nlen = 0;
	int i = 0;
	const char *notifications[10] = {
		"com.apple.itunes-client.syncCancelRequest",
		"com.apple.itunes-client.syncSuspendRequest",
		"com.apple.itunes-client.syncResumeRequest",
		"com.apple.mobile.lockdown.phone_number_changed",
		"com.apple.mobile.lockdown.device_name_changed",
		"com.apple.springboard.attemptactivation",
		"com.apple.mobile.data_sync.domain_changed",
		"com.apple.mobile.application_installed",
		"com.apple.mobile.application_uninstalled",
		NULL
	};

	sndlen = 0;

	if (!client) {
		return IPHONE_E_INVALID_ARG;
	}
	np_lock(client);

	while (notifications[i]) {

		dict = plist_new_dict();
		plist_add_sub_key_el(dict, "Command");
		plist_add_sub_string_el(dict, "ObserveNotification");
		plist_add_sub_key_el(dict, "Name");
		plist_add_sub_string_el(dict, notifications[i++]);
		plist_to_xml(dict, &XML_content, &length);

		nlen = htonl(length);
		memcpy(sndbuf + sndlen, &nlen, 4);
		sndlen += 4;
		memcpy(sndbuf + sndlen, XML_content, length);
		sndlen += length;

		plist_free(dict);
		dict = NULL;
		free(XML_content);
		XML_content = NULL;
	}

	dict = plist_new_dict();
	plist_add_sub_key_el(dict, "Command");
	plist_add_sub_string_el(dict, "Shutdown");
	plist_to_xml(dict, &XML_content, &length);

	nlen = htonl(length);

	memcpy(sndbuf + sndlen, &nlen, 4);
	sndlen += 4;

	memcpy(sndbuf + sndlen, XML_content, length);
	sndlen += length;

	plist_free(dict);
	dict = NULL;
	free(XML_content);
	XML_content = NULL;

	log_debug_buffer(sndbuf, sndlen);

	iphone_mux_send(client->connection, sndbuf, sndlen, &bytes);
	if (bytes <= 0) {
		np_unlock(client);
		return bytes;
	}

	np_unlock(client);
	return bytes;
}