summaryrefslogtreecommitdiff
path: root/src/MessagePortProxy.cpp
blob: c203baddaad776224f4d3cd25dee17b1660cc2cb (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//
// Open Service Platform
// Copyright (c) 2012 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

/**
 * @file	MessagePortProxy.cpp
 * @brief	This is the implementation file for the MessagePortProxy class.
 *
 */


#include <sys/types.h>
#include <unistd.h>
#include <sstream>

#include <package_manager.h>

#include "message-port.h"
#include "message-port-messages.h"
#include "message-port-log.h"

#include "IpcClient.h"
#include "MessagePortProxy.h"

using namespace std;

static const char MESSAGE_TYPE[] = "MESSAGE_TYPE";

static const char LOCAL_APPID[] = "LOCAL_APPID";
static const char LOCAL_PORT[] = "LOCAL_PORT";
static const char TRUSTED_LOCAL[] = "TRUSTED_LOCAL";

static const char REMOTE_APPID[] = "REMOTE_APPID";
static const char REMOTE_PORT[] = "REMOTE_PORT";
static const char TRUSTED_REMOTE[] = "TRUSTED_REMOTE";
static const char TRUSTED_MESSAGE[] = "TRUSTED_MESSAGE";

static const int MAX_MESSAGE_SIZE = 8 * 1024;

MessagePortProxy::MessagePortProxy(void)
	: __pIpcClient(NULL)
	, __pMutex(NULL)
{
}

MessagePortProxy::~MessagePortProxy(void)
{
	pthread_mutex_destroy(__pMutex);
}

int
MessagePortProxy::Construct(void)
{
	IpcClient* pIpcClient = new (std::nothrow) IpcClient();
	if (pIpcClient == NULL)
	{
		return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
	}

	int ret = pIpcClient->Construct("message-port-server", this);
	if (ret != 0)
	{
		delete pIpcClient;

		_LOGE("Failed to create ipc client: %d.", ret);

		return MESSAGEPORT_ERROR_IO_ERROR;
	}

	pthread_mutex_t* pMutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
	if (pMutex == NULL)
	{
		return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
	}

	pthread_mutex_init(pMutex, NULL);

	__pMutex = pMutex;
	__pIpcClient = pIpcClient;
	__appId = pIpcClient->GetAppId();

	return 0;
}

void
MessagePortProxy::OnIpcResponseReceived(IpcClient& client, const IPC::Message& message)
{
	_LOGD("Message received, type %d", message.type());

	IPC_BEGIN_MESSAGE_MAP(MessagePortProxy, message)
	IPC_MESSAGE_HANDLER_EX(MessagePort_sendMessageAsync, &client, OnSendMessageInternal)
	IPC_END_MESSAGE_MAP_EX()
}

int
MessagePortProxy::RegisterMessagePort(const string& localPort, bool isTrusted,  messageport_message_cb callback)
{
	_LOGD("Register a message port : [%s:%s]", __appId.c_str(), localPort.c_str());

	int id = 0;

	// Check the message port is already registed
	if (IsLocalPortRegisted(localPort, isTrusted, id))
	{
		if (!isTrusted)
		{
			__listeners[localPort] = callback;
		}
		else
		{
			__trustedListeners[localPort] = callback;
		}

		return id;
	}

	bundle *b = bundle_create();

	if (!isTrusted)
	{
		bundle_add(b, TRUSTED_LOCAL, "FALSE");
	}
	else
	{
		bundle_add(b, TRUSTED_LOCAL, "TRUE");
	}

	bundle_add(b, LOCAL_APPID, __appId.c_str());
	bundle_add(b, LOCAL_PORT, localPort.c_str());


	// Create Bundle Buffer from bundle
	BundleBuffer buffer;
	buffer.b = b;

	int ret = 0;
	int return_value = 0;

	IPC::Message* pMsg = new MessagePort_registerPort(buffer, &return_value);
	if (pMsg == NULL)
	{
		bundle_free(b);
		return  MESSAGEPORT_ERROR_OUT_OF_MEMORY;
	}

	ret = __pIpcClient->SendRequest(pMsg);

	delete pMsg;

	bundle_free(b);

	if (ret != 0)
	{
		_LOGE("Failed to send a request: %d.", ret);

		return MESSAGEPORT_ERROR_IO_ERROR;
	}


	// Add a listener
	if (!isTrusted)
	{
		// Local port id
		id = GetNextId();

		__listeners[localPort] = callback;
		__idFromString[localPort] = id;
		__ids[id] = localPort;
	}
	else
	{
		id = GetNextId();

		__trustedListeners[localPort] = callback;
		__trustedIdFromString[localPort] = id;
		__trustedIds[id] = localPort;
	}

	return id;
}

int
MessagePortProxy::CheckRemotePort(const string& remoteAppId, const string& remotePort,	bool isTrusted, bool *exist)
{
	_LOGD("Check a remote port : [%s:%s]", remoteAppId.c_str(), remotePort.c_str());

	int ret = 0;

	// Check the certificate
	if (isTrusted)
	{
		// Check the preloaded
		if (!IsPreloaded(remoteAppId))
		{
			ret = CheckCertificate(remoteAppId);
			if (ret < 0)
			{
				return ret;
			}
		}
	}

	bundle *b = bundle_create();
	bundle_add(b, REMOTE_APPID, remoteAppId.c_str());
	bundle_add(b, REMOTE_PORT, remotePort.c_str());

	if (!isTrusted)
	{
		bundle_add(b, TRUSTED_REMOTE, "FALSE");
	}
	else
	{
		bundle_add(b, TRUSTED_REMOTE, "TRUE");
	}

	// To Bundle Buffer
	BundleBuffer buffer;
	buffer.b = b;

	int return_value = 0;
	IPC::Message* pMsg = new MessagePort_checkRemotePort(buffer, &return_value);
	if (pMsg == NULL)
	{
		bundle_free(b);

		return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
	}

	ret = __pIpcClient->SendRequest(pMsg);

	delete pMsg;

	bundle_free(b);

	if (ret < 0)
	{
		_LOGE("Failed to send a request: %d.", ret);

		return MESSAGEPORT_ERROR_IO_ERROR;
	}

	if (return_value < 0)
	{
		if (return_value == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND)
		{
			*exist = false;
			return 0;
		}
		else
		{
			_LOGE("Failed to check the remote messge port: %d.", return_value);
			return MESSAGEPORT_ERROR_IO_ERROR;
		}
	}

	*exist = true;
	return 0;
}

int
MessagePortProxy::SendMessage(const string& remoteAppId, const string& remotePort, bool trustedMessage, bundle* data)
{
	_LOGD("Send a message to : [%s:%s]", remoteAppId.c_str(), remotePort.c_str());

	int ret = 0;

	// Check the certificate
	if (trustedMessage)
	{
		// Check the preloaded
		if (!IsPreloaded(remoteAppId))
		{
			ret = CheckCertificate(remoteAppId);
			if (ret < 0)
			{
				return ret;
			}
		}
	}

	bundle_add(data, MESSAGE_TYPE, "UNI-DIR");

	bundle_add(data, REMOTE_APPID, remoteAppId.c_str());
	bundle_add(data, REMOTE_PORT, remotePort.c_str());

	if (!trustedMessage)
	{
		bundle_add(data, TRUSTED_MESSAGE, "FALSE");
	}
	else
	{
		bundle_add(data, TRUSTED_MESSAGE, "TRUE");
	}

	BundleBuffer buffer;
	buffer.b = data;

	ret = SendMessageInternal(buffer);

	return ret;
}

int
MessagePortProxy::SendMessage(const string& localPort, bool trustedPort, const string& remoteAppId, const string& remotePort, bool trustedMessage, bundle* data)
{
	_LOGD("Send a bidirectional message from [%s:%s] to [%s:%s]", __appId.c_str(), localPort.c_str(), remoteAppId.c_str(), remotePort.c_str());

	int ret = 0;

	// Check the certificate
	if (trustedMessage)
	{
		// Check the preloaded
		if (!IsPreloaded(remoteAppId))
		{
			ret = CheckCertificate(remoteAppId);
			if (ret < 0)
			{
				return ret;
			}
		}
	}

	bundle_add(data, MESSAGE_TYPE, "BI-DIR");

	bundle_add(data, LOCAL_APPID, __appId.c_str());
	bundle_add(data, LOCAL_PORT, localPort.c_str());

	if (!trustedPort)
	{
		bundle_add(data, TRUSTED_LOCAL, "FALSE");
	}
	else
	{
		bundle_add(data, TRUSTED_LOCAL, "TRUE");
	}

	bundle_add(data, REMOTE_APPID, remoteAppId.c_str());
	bundle_add(data, REMOTE_PORT, remotePort.c_str());

	if (!trustedMessage)
	{
		bundle_add(data, TRUSTED_MESSAGE, "FALSE");
	}
	else
	{
		bundle_add(data, TRUSTED_MESSAGE, "TRUE");
	}

	BundleBuffer buffer;
	buffer.b = data;

	ret = SendMessageInternal(buffer);

	return ret;
}

int
MessagePortProxy::SendMessageInternal(const BundleBuffer& buffer)
{
	int ret = 0;

	IPC::Message* pMsg = new MessagePort_sendMessage(buffer, &ret);
	if (pMsg == NULL)
	{
		return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
	}

	// Check the message size
	int len = 0;
	bundle_raw* raw = NULL;
	bundle_encode(buffer.b, &raw, &len);

	bundle_free_encoded_rawdata(&raw);

	if (len > MAX_MESSAGE_SIZE)
	{
		_LOGE("The size of message (%d) has exceeded the maximum limit.", len);
		return MESSAGEPORT_ERROR_MAX_EXCEEDED;
	}

	ret = __pIpcClient->SendRequest(pMsg);
	delete pMsg;

	if (ret != 0)
	{
		_LOGE("Failed to send a request: %d.", ret);
		return MESSAGEPORT_ERROR_IO_ERROR;
	}

	return 0;
}

char*
MessagePortProxy::GetLocalPortNameN(int id)
{
	string value;

	map<int, std::string>::iterator it;

	it = __ids.find(id);
	if (it == __ids.end())
	{
		it = __trustedIds.find(id);
		if (it == __ids.end())
		{
			return NULL;
		}
		else
		{
			value = __trustedIds[id];
			return strdup(value.c_str());
		}
	}
	else
	{
		value = __ids[id];
		return strdup(value.c_str());
	}

	return NULL;
}

int
MessagePortProxy::CheckTrustedLocalPort(int id, bool* trusted)
{
	map<int, std::string>::iterator it;

	it = __ids.find(id);
	if (it == __ids.end())
	{
		it = __trustedIds.find(id);
		if (it == __ids.end())
		{
			return MESSAGEPORT_ERROR_INVALID_PARAMETER;
		}
		else
		{
			*trusted = true;
			return 0;
		}
	}
	else
	{
		*trusted = false;
		return 0;
	}

	return MESSAGEPORT_ERROR_INVALID_PARAMETER;
}

MessagePortProxy*
MessagePortProxy::GetProxy(void)
{
	static MessagePortProxy* pProxy = NULL;

	if (pProxy == NULL)
	{
		MessagePortProxy* p = new MessagePortProxy();
		if (p == NULL)
		{
			return NULL;
		}

		int ret = p->Construct();
		if (ret < 0)
		{
			return NULL;
		}

		pProxy = p;
	}

	return pProxy;
}

int
MessagePortProxy::GetNextId(void)
{
	static int count = 0;

	pthread_mutex_lock(__pMutex);
	++count;
	pthread_mutex_unlock(__pMutex);

	return count;
}

bool
MessagePortProxy::IsLocalPortRegisted(const string& localPort, bool trusted, int &id)
{
	if (!trusted)
	{
		map<string, messageport_message_cb>::iterator port_it = __listeners.find(localPort);
		if (port_it == __listeners.end())
		{
			return false;
		}
		else
		{
			for (map<int, string>::iterator it = __ids.begin(); it != __ids.end(); ++it)
			{
				if (localPort.compare(it->second) == 0)
				{
					id = it->first;
					return true;
				}
			}
		}
	}
	else
	{
		map<string, messageport_message_cb>::iterator port_it = __trustedListeners.find(localPort);
		if (port_it == __trustedListeners.end())
		{
			return false;
		}
		else
		{
			for (map<int, string>::iterator it = __ids.begin(); it != __trustedIds.end(); ++it)
			{
				if (localPort.compare(it->second) == 0)
				{
					id = it->first;
					return true;
				}
			}
		}
	}

	return false;
}

int
MessagePortProxy::CheckCertificate(const std::string& remoteAppId)
{
	package_manager_compare_result_type_e res;
	int ret = package_manager_compare_app_cert_info(__appId.c_str(), remoteAppId.c_str(), &res);

	if (ret == 0)
	{
		if (res != PACAKGE_MANAGER_COMPARE_MATCH)
		{
			_LOGE("The remote application (%s) is not signed with the same certificate", remoteAppId.c_str());
			return MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
		}
	}
	else
	{
		_LOGE("Failed to check the certificate: %d.", ret);
		return MESSAGEPORT_ERROR_IO_ERROR;
	}

	return 0;
}

bool
MessagePortProxy::IsPreloaded(const std::string& remoteAppId)
{
	bool preload_local = false;
	bool preload_remote = false;

	if (package_manager_is_preload_package_by_app_id(__appId.c_str(), &preload_local) == 0)
	{
		if (package_manager_is_preload_package_by_app_id(remoteAppId.c_str(), &preload_remote) == 0)
		{
			if (preload_local && preload_remote)
			{
				return true;
			}
		}
		else
		{
			_LOGE("Failed to check the preloaded application.");
		}
	}
	else
	{
		_LOGE("Failed to check the preloaded application.");
	}

	return false;
}

bool
MessagePortProxy::OnSendMessageInternal(const BundleBuffer& buffer)
{
	bundle* b = buffer.b;

	const char* pRemoteAppId = bundle_get_val(b, REMOTE_APPID);
	const char* pRemotePort = bundle_get_val(b, REMOTE_PORT);
	string trustedMessage = bundle_get_val(b, TRUSTED_MESSAGE);

	string messageType = bundle_get_val(b, MESSAGE_TYPE);

	_LOGD("Message received to AppId: %s, Port: %s, Trusted: %s", pRemoteAppId, pRemotePort, trustedMessage.c_str());

	int id = 0;
	messageport_message_cb callback;

	if (trustedMessage.compare("FALSE") == 0)
	{
		callback = __listeners[pRemotePort];
		id = __idFromString[pRemotePort];
	}
	else
	{
		callback = __trustedListeners[pRemotePort];
		id = __trustedIdFromString[pRemotePort];
	}


	if (callback)
	{
		// remove system data
		bundle_del(b, REMOTE_APPID);
		bundle_del(b, REMOTE_PORT);
		bundle_del(b, TRUSTED_MESSAGE);
		bundle_del(b, MESSAGE_TYPE);

		if (messageType.compare("UNI-DIR") == 0)
		{
			callback(id, NULL, NULL, false, b);
		}
		else
		{
			string localAppId = bundle_get_val(b, LOCAL_APPID);
			string localPort = bundle_get_val(b, LOCAL_PORT);
			string trustedLocal = bundle_get_val(b, TRUSTED_LOCAL);

			_LOGD("From AppId: %s, Port: %s, TrustedLocal: %s", localAppId.c_str(), localPort.c_str(), trustedLocal.c_str());

			bool trustedPort = (trustedLocal.compare("TRUE") == 0);

			// remove system data
			bundle_del(b, LOCAL_APPID);
			bundle_del(b, LOCAL_PORT);
			bundle_del(b, TRUSTED_LOCAL);

			callback(id, localAppId.c_str(), localPort.c_str(), trustedPort, b);
		}
	}
	else
	{
		_LOGD("No callback");
	}

	return true;
}