2e54cffaff82f7bd58dd97fec1f7cb0377742788
[platform/core/appfw/message-port.git] / src / message-port.c
1 /*
2  * Message Port
3  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /**
19  * @file        message-port.cpp
20  * @brief       This is the implementation file for the MessagePort.
21  */
22
23 #include <sys/socket.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <glib.h>
27 #include <gio/gio.h>
28 #include <aul/aul.h>
29 #include <openssl/md5.h>
30 #include <bundle.h>
31 #include <bundle_internal.h>
32 #include <pkgmgr-info.h>
33 #include <aul.h>
34 #include <gio/gio.h>
35 #include <gio/gunixfdlist.h>
36
37 #include "message-port.h"
38 #include "message-port-log.h"
39
40 #define MAX_PACKAGE_STR_SIZE 512
41 #define MESSAGEPORT_BUS_NAME_PREFIX "org.tizen.messageport._"
42 #define MESSAGEPORT_OBJECT_PATH "/org/tizen/messageport"
43 #define MESSAGEPORT_INTERFACE_PREFIX "org.tizen.messageport._"
44
45 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
46 #define DBUS_PATH_DBUS "/org/freedesktop/DBus"
47 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
48
49 #define DBUS_RELEASE_NAME_REPLY_RELEASED        1 /* *< Service was released from the given name */
50 #define DBUS_RELEASE_NAME_REPLY_NON_EXISTENT    2 /* *< The given name does not exist on the bus */
51 #define DBUS_RELEASE_NAME_REPLY_NOT_OWNER       3 /* *< Service is not an owner of the given name */
52 #define HEADER_LEN 8
53 #define MAX_RETRY_CNT 2
54 #define SOCK_PAIR_SENDER 0
55 #define SOCK_PAIR_RECEIVER 1
56
57
58 #define retvm_if(expr, val, fmt, arg...) do { \
59         if (expr) { \
60                 _LOGE(fmt, ##arg); \
61                 _LOGE("(%s) -> %s() return", #expr, __func__); \
62                 return val; \
63         } \
64 } while (0)
65
66 #define retv_if(expr, val) do { \
67         if (expr) { \
68                 _LOGE("(%s) -> %s() return", #expr, __func__); \
69                 return val; \
70         } \
71 } while (0)
72
73 #define FREE_AND_NULL(ptr) do { \
74         if (ptr) { \
75                 free((void *)ptr); \
76                 ptr = NULL; \
77         } \
78 } while (0)
79
80 static bool _initialized = false;
81 static GDBusConnection *__gdbus_conn;
82 static char *__app_id;
83 static GHashTable *__local_port_info;
84 static GHashTable *__remote_app_info;
85 static GHashTable *__sender_appid_hash;
86 static GHashTable *__trusted_app_list_hash;
87 static GHashTable *__callback_info_hash;
88 static const int MAX_MESSAGE_SIZE = 16 * 1024;
89
90 enum __certificate_info_type {
91         UNKNOWN = 0,
92         CERTIFICATE_MATCH,
93         CERTIFICATE_NOT_MATCH,
94 };
95
96 typedef struct message_port_pkt {
97         int remote_port_name_len;
98         char *remote_port_name;
99         bool is_bidirection;
100         bool is_trusted;
101         int data_len;
102         unsigned char *data;
103 } message_port_pkt_s;
104
105 typedef struct message_port_callback_info {
106         messageport_message_cb callback;
107         int local_id;
108         char *remote_app_id;
109         GIOChannel *gio_read;
110         int g_src_id;
111 } message_port_callback_info_s;
112
113 typedef struct message_port_local_port_info {
114         messageport_message_cb callback;
115         bool is_trusted;
116         char *port_name;
117         int local_id;
118 } message_port_local_port_info_s;
119
120 typedef struct message_port_remote_port_info {
121         char *sender_id;
122         char *remote_app_id;
123         int certificate_info;
124         GList *port_list;
125 } message_port_remote_app_info_s;
126
127 typedef struct port_list_info {
128         char *port_name;
129         char *encoded_bus_name;
130         bool is_trusted;
131         int send_sock_fd;
132         int watcher_id;
133         bool exist;
134 } port_list_info_s;
135
136 static void __callback_info_free(gpointer data)
137 {
138         message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
139         GError *error = NULL;
140         if (callback_info == NULL)
141                 return;
142
143         if (callback_info->remote_app_id)
144                 FREE_AND_NULL(callback_info->remote_app_id);
145
146         if (callback_info->gio_read != NULL) {
147                 g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
148                 if (error) {
149                         _LOGE("g_io_channel_shutdown error : %s", error->message);
150                         g_error_free(error);
151                 }
152                 g_io_channel_unref(callback_info->gio_read);
153                 callback_info->gio_read = NULL;
154         }
155
156         if (callback_info->g_src_id != 0) {
157                 g_source_remove(callback_info->g_src_id);
158                 callback_info->g_src_id = 0;
159         }
160
161         FREE_AND_NULL(callback_info);
162 }
163
164 static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
165 {
166         GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
167         GList *find_list;
168
169         if (callback_info_list == NULL)
170                 return;
171
172         find_list = g_list_find(callback_info_list, callback_info);
173         if (find_list == NULL)
174                 return;
175
176         callback_info_list = g_list_remove_link(callback_info_list, find_list);
177         __callback_info_free(callback_info);
178         g_list_free(find_list);
179 }
180
181 static void __hash_destroy_callback_info(gpointer data)
182 {
183
184         GList *callback_list = (GList *)data;
185         if (callback_list != NULL)
186                 g_list_free_full(callback_list, __callback_info_free);
187 }
188
189 static char *__get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
190 {
191
192         int prefix_len = strlen(MESSAGEPORT_BUS_NAME_PREFIX);
193         int postfix_len = 1;
194         char *postfix = is_trusted ? "1" : "0";
195
196         unsigned char c[MD5_DIGEST_LENGTH] = {0};
197         char *md5_interface = NULL;
198         char *temp;
199         int index = 0;
200         MD5_CTX mdContext;
201         int encoded_bus_name_len = prefix_len + postfix_len + (MD5_DIGEST_LENGTH * 2) + 2;
202         int bus_name_len = strlen(remote_app_id) + strlen(port_name) + 2;
203         char *bus_name = (char *)calloc(bus_name_len, sizeof(char));
204         if (bus_name == NULL) {
205                 _LOGE("bus_name calloc failed");
206                 return 0;
207         }
208
209         snprintf(bus_name, bus_name_len, "%s_%s", remote_app_id, port_name);
210
211         MD5_Init(&mdContext);
212         MD5_Update(&mdContext, bus_name, bus_name_len);
213         MD5_Final(c, &mdContext);
214
215         md5_interface = (char *)calloc(encoded_bus_name_len , sizeof(char));
216         if (md5_interface == NULL) {
217                 if (bus_name)
218                         free(bus_name);
219
220                 _LOGE("md5_interface calloc failed!!");
221                 return 0;
222         }
223
224         snprintf(md5_interface, encoded_bus_name_len, "%s", MESSAGEPORT_BUS_NAME_PREFIX);
225         temp = md5_interface;
226         temp += prefix_len;
227
228         for (index = 0; index < MD5_DIGEST_LENGTH; index++) {
229                 snprintf(temp, 3, "%02x", c[index]);
230                 temp += 2;
231         }
232
233         if (postfix && postfix_len > 0)
234                 snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
235         if (bus_name)
236                 free(bus_name);
237
238         _LOGI("encoded_bus_name : %s ", md5_interface);
239
240         return md5_interface;
241 }
242
243 static int __remote_port_compare_cb(gconstpointer a, gconstpointer b)
244 {
245         port_list_info_s *key1 = (port_list_info_s *)a;
246         port_list_info_s *key2 = (port_list_info_s *)b;
247
248         if (key1->is_trusted == key2->is_trusted)
249                 return strcmp(key1->port_name, key2->port_name);
250
251         return 1;
252 }
253
254
255 static bool __is_preloaded(const char *local_appid, const char *remote_appid)
256 {
257         _LOGI("IsPreloaded");
258
259         bool preload_local = false;
260         bool preload_remote = false;
261
262         pkgmgrinfo_appinfo_h handle = NULL;
263         int ret = pkgmgrinfo_appinfo_get_usr_appinfo(local_appid, getuid(), &handle);
264         if (ret != PMINFO_R_OK) {
265                 _LOGE("Failed to get the appinfo. %d", ret);
266                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
267                 return false;
268         }
269         ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_local);
270         if (ret != PMINFO_R_OK) {
271                 _LOGE("Failed to check the preloaded application. %d", ret);
272                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
273                 return false;
274         }
275         ret = pkgmgrinfo_appinfo_get_usr_appinfo(remote_appid, getuid(), &handle);
276         if (ret != PMINFO_R_OK) {
277                 _LOGE("Failed to get the appinfo. %d", ret);
278                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
279                 return false;
280         }
281         ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_remote);
282         if (ret != PMINFO_R_OK) {
283                 _LOGE("Failed to check the preloaded application. %d", ret);
284                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
285                 return false;
286         }
287
288         if (preload_local && preload_remote) {
289                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
290                 return true;
291         }
292         pkgmgrinfo_appinfo_destroy_appinfo(handle);
293         return false;
294 }
295
296 static int __check_certificate(const char *local_appid, const char *remote_appid)
297 {
298         _LOGI("CheckCertificate");
299
300         pkgmgrinfo_cert_compare_result_type_e res;
301         int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
302         if (ret < 0) {
303                 _LOGE(":CheckCertificate() Failed");
304                 return MESSAGEPORT_ERROR_IO_ERROR;
305         }
306         if (res != PMINFO_CERT_COMPARE_MATCH) {
307                 _LOGE("CheckCertificate() Failed : MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH");
308                 return MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
309         }
310
311         return MESSAGEPORT_ERROR_NONE;
312 }
313
314 static void on_name_appeared(GDBusConnection *connection,
315                 const gchar     *name,
316                 const gchar     *name_owner,
317                 gpointer         user_data)
318 {
319         _LOGI("name appeared : %s %s", __app_id, name);
320 }
321
322 static void on_name_vanished(GDBusConnection *connection,
323                 const gchar     *name,
324                 gpointer         user_data)
325 {
326         _LOGI("name vanished : %s", name);
327         port_list_info_s *pli = (port_list_info_s *)user_data;
328         g_bus_unwatch_name(pli->watcher_id);
329         pli->exist = false;
330         pli->watcher_id = 0;
331         _LOGI("name vanished socket : %d", pli->send_sock_fd);
332         if (pli->send_sock_fd > 0) {
333                 close(pli->send_sock_fd);
334                 pli->send_sock_fd = 0;
335         }
336 }
337
338 static int __get_local_port_info(int id, message_port_local_port_info_s **info)
339 {
340         message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
341
342         if (mi == NULL)
343                 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
344         *info = mi;
345
346         return MESSAGEPORT_ERROR_NONE;
347 }
348
349 static port_list_info_s *__set_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
350 {
351         int ret_val = MESSAGEPORT_ERROR_NONE;
352         port_list_info_s *port_info = (port_list_info_s *)calloc(1, sizeof(port_list_info_s));
353
354         if (!port_info) {
355                 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
356                 goto out;
357         }
358         port_info->port_name = strdup(remote_port);
359         if (!port_info->port_name) {
360                 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
361                 goto out;
362         }
363         port_info->is_trusted = is_trusted;
364         port_info->encoded_bus_name = __get_encoded_name(remote_app_id, remote_port, is_trusted);
365         if (port_info->encoded_bus_name == NULL) {
366                 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
367                 goto out;
368         }
369         port_info->send_sock_fd = 0;
370 out:
371         if (ret_val != MESSAGEPORT_ERROR_NONE) {
372                 if (port_info) {
373                         FREE_AND_NULL(port_info->port_name);
374                         FREE_AND_NULL(port_info->encoded_bus_name);
375                         free(port_info);
376                 }
377                 return NULL;
378         }
379         return port_info;
380 }
381
382 static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
383 {
384         message_port_remote_app_info_s *remote_app_info = NULL;
385         int ret_val = MESSAGEPORT_ERROR_NONE;
386
387         remote_app_info = (message_port_remote_app_info_s *)calloc(1, sizeof(message_port_remote_app_info_s));
388         if (!remote_app_info) {
389                 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
390                 goto out;
391         }
392
393         remote_app_info->remote_app_id = strdup(remote_app_id);
394         if (remote_app_info->remote_app_id == NULL) {
395                 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;;
396                 goto out;
397         }
398
399 out:
400         if (ret_val != MESSAGEPORT_ERROR_NONE) {
401                 if (remote_app_info) {
402                         FREE_AND_NULL(remote_app_info->remote_app_id);
403                         FREE_AND_NULL(remote_app_info);
404                 }
405                 return NULL;
406         }
407         return remote_app_info;
408 }
409
410 static int __get_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted,
411                 message_port_remote_app_info_s **mri, port_list_info_s **pli)
412 {
413         message_port_remote_app_info_s *remote_app_info = NULL;
414         port_list_info_s port_info;
415         GList *cb_list = NULL;
416         int ret_val = MESSAGEPORT_ERROR_NONE;
417
418         remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_app_info, remote_app_id);
419
420         if (remote_app_info == NULL) {
421                 remote_app_info = __set_remote_app_info(remote_app_id, remote_port, is_trusted);
422
423                 if (remote_app_info == NULL) {
424                         ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
425                         goto out;
426                 }
427                 g_hash_table_insert(__remote_app_info, remote_app_info->remote_app_id, remote_app_info);
428         }
429         *mri = remote_app_info;
430
431         port_info.port_name = strdup(remote_port);
432         port_info.is_trusted = is_trusted;
433         cb_list = g_list_find_custom(remote_app_info->port_list, &port_info,
434                                         (GCompareFunc)__remote_port_compare_cb);
435         if (port_info.port_name)
436                 free(port_info.port_name);
437         if (cb_list == NULL) {
438                 port_list_info_s *tmp = __set_remote_port_info(remote_app_id, remote_port, is_trusted);
439                 if (tmp == NULL) {
440                         ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
441                         goto out;
442                 }
443                 remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
444                 *pli = tmp;
445         } else {
446                 *pli = (port_list_info_s *)cb_list->data;
447         }
448         if ((*pli)->watcher_id < 1) {
449                 LOGI("watch remote port : %s", (*pli)->encoded_bus_name);
450                 (*pli)->watcher_id = g_bus_watch_name_on_connection(
451                                         __gdbus_conn,
452                                         (*pli)->encoded_bus_name,
453                                         G_BUS_NAME_WATCHER_FLAGS_NONE,
454                                         on_name_appeared,
455                                         on_name_vanished,
456                                         *pli,
457                                         NULL);
458         }
459 out:
460
461         return ret_val;
462 }
463
464 static bool __is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
465 {
466         GHashTableIter iter;
467         gpointer key, value;
468
469         g_hash_table_iter_init(&iter, __local_port_info);
470         while (g_hash_table_iter_next(&iter, &key, &value)) {
471                 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
472
473                 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
474                         *local_id = mi->local_id;
475                         if (lpi != NULL)
476                                 *lpi = mi;
477                         return true;
478                 }
479         }
480         return false;
481 }
482
483 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
484 {
485         GDBusMessage *msg = NULL;
486         GDBusMessage *reply = NULL;
487         GError *err = NULL;
488         GVariant *body;
489         int pid = 0;
490
491         msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
492                         "org.freedesktop.DBus", "GetConnectionUnixProcessID");
493         if (!msg) {
494                 _LOGE("Can't allocate new method call");
495                 goto out;
496         }
497
498         g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
499         reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
500                                                         G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
501
502         if (!reply) {
503                 if (err != NULL) {
504                         _LOGE("Failed to get pid [%s]", err->message);
505                         g_error_free(err);
506                 }
507                 goto out;
508         }
509
510         body = g_dbus_message_get_body(reply);
511         g_variant_get(body, "(u)", &pid);
512
513 out:
514         if (msg)
515                 g_object_unref(msg);
516         if (reply)
517                 g_object_unref(reply);
518
519         return pid;
520 }
521
522 static int __write_socket(int fd,
523                 const char *buffer,
524                 unsigned int nbytes,
525                 unsigned int *bytes_write)
526 {
527         unsigned int left = nbytes;
528         ssize_t nb;
529         int retry_cnt = 0;
530
531         *bytes_write = 0;
532         while (left && (retry_cnt < MAX_RETRY_CNT)) {
533                 nb = write(fd, buffer, left);
534                 if (nb == -1) {
535                         if (errno == EINTR) {
536                                 LOGE("__write_socket: EINTR error continue ...");
537                                 retry_cnt++;
538                                 continue;
539                         }
540                         LOGE("__write_socket: ...error fd %d: errno %d\n", fd, errno);
541                         return MESSAGEPORT_ERROR_IO_ERROR;
542                 }
543
544                 left -= nb;
545                 buffer += nb;
546                 *bytes_write += nb;
547                 retry_cnt = 0;
548         }
549         return MESSAGEPORT_ERROR_NONE;
550 }
551
552 static int __write_string_to_socket(int fd, const char *buffer, int string_len)
553 {
554         unsigned int nb;
555         if (__write_socket(fd, (char *)&string_len, sizeof(string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
556                 _LOGE("write string_len fail");
557                 return MESSAGEPORT_ERROR_IO_ERROR;
558         }
559
560         if (string_len > 0) {
561                 if (__write_socket(fd, buffer, string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
562                         _LOGE("wirte buffer fail");
563                         return MESSAGEPORT_ERROR_IO_ERROR;
564                 }
565         }
566         return MESSAGEPORT_ERROR_NONE;
567 }
568
569 static int __read_socket(int fd,
570                 char *buffer,
571                 unsigned int nbytes,
572                 unsigned int *bytes_read)
573 {
574         unsigned int left = nbytes;
575         ssize_t nb;
576         int retry_cnt = 0;
577
578         *bytes_read = 0;
579         while (left && (retry_cnt < MAX_RETRY_CNT)) {
580                 nb = read(fd, buffer, left);
581                 if (nb == 0) {
582                         LOGE("__read_socket: ...read EOF, socket closed %d: nb %d\n", fd, nb);
583                         return MESSAGEPORT_ERROR_IO_ERROR;
584                 } else if (nb == -1) {
585                         if (errno == EINTR) {
586                                 LOGE("__read_socket: EINTR error continue ...");
587                                 retry_cnt++;
588                                 continue;
589                         }
590                         LOGE("__read_socket: ...error fd %d: errno %d\n", fd, errno);
591                         return MESSAGEPORT_ERROR_IO_ERROR;
592                 }
593
594                 left -= nb;
595                 buffer += nb;
596                 *bytes_read += nb;
597                 retry_cnt = 0;
598         }
599         return MESSAGEPORT_ERROR_NONE;
600 }
601
602 static int __read_string_from_socket(int fd, char **buffer, int *string_len)
603 {
604         unsigned int nb;
605         if (__read_socket(fd, (char *)string_len, sizeof(*string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
606                 LOGE("read socket fail");
607                 return MESSAGEPORT_ERROR_IO_ERROR;
608         }
609         if (*string_len > 0 && *string_len < MAX_MESSAGE_SIZE) {
610                 *buffer = (char *)calloc(*string_len, sizeof(char));
611                 if (*buffer == NULL) {
612                         LOGE("Out of memory.");
613                         return MESSAGEPORT_ERROR_IO_ERROR;
614                 }
615                 if (__read_socket(fd, *buffer, *string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
616                         LOGE("read socket fail");
617                         return MESSAGEPORT_ERROR_IO_ERROR;
618                 }
619         } else {
620                 LOGE("Invalid string len %d", &string_len);
621                 return MESSAGEPORT_ERROR_IO_ERROR;
622         }
623         return MESSAGEPORT_ERROR_NONE;
624 }
625
626 message_port_pkt_s *__message_port_recv_raw(int fd)
627 {
628         message_port_pkt_s *pkt = NULL;
629         unsigned int nb;
630
631         pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
632         if (pkt == NULL) {
633                 close(fd);
634                 return NULL;
635         }
636
637         if (__read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGEPORT_ERROR_NONE) {
638                 LOGE("read socket fail: port_name");
639                 free(pkt->remote_port_name);
640                 free(pkt);
641                 return NULL;
642         }
643
644         if (__read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
645                 LOGE("read socket fail: is_bidirection");
646                 free(pkt->remote_port_name);
647                 free(pkt);
648                 return NULL;
649         }
650
651         if (__read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
652                 LOGE("read socket fail: is_trusted");
653                 free(pkt->remote_port_name);
654                 free(pkt);
655                 return NULL;
656         }
657
658         if (__read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGEPORT_ERROR_NONE) {
659                 LOGE("read socket fail: data");
660                 free(pkt->remote_port_name);
661                 free(pkt);
662                 return NULL;
663         }
664
665         return pkt;
666 }
667
668 static gboolean __socket_request_handler(GIOChannel *gio,
669                 GIOCondition cond,
670                 gpointer data)
671 {
672         int fd = 0;
673         message_port_callback_info_s *mi;
674         message_port_pkt_s *pkt;
675         bundle *kb = NULL;
676         GError *error = NULL;
677
678         mi = (message_port_callback_info_s *)data;
679         if (mi == NULL) {
680
681                 g_io_channel_shutdown(gio, TRUE, &error);
682                 if (error) {
683                         _LOGE("g_io_channel_shutdown error : %s", error->message);
684                         g_error_free(error);
685                 }
686                 g_io_channel_unref(gio);
687                 return FALSE;
688         }
689
690         if (cond == G_IO_HUP) {
691
692                 _LOGI("socket G_IO_HUP");
693                 __callback_info_free_by_info(mi);
694                 return FALSE;
695
696         } else {
697
698                 if ((fd = g_io_channel_unix_get_fd(gio)) < 0) {
699                         _LOGE("fail to get fd from io channel");
700                         __callback_info_free_by_info(mi);
701                         return FALSE;
702                 }
703
704                 if ((pkt = __message_port_recv_raw(fd)) == NULL) {
705                         _LOGE("recv error on SOCKET");
706                         __callback_info_free_by_info(mi);
707                         return FALSE;
708                 }
709
710                 kb = bundle_decode(pkt->data, pkt->data_len);
711                 if (pkt->is_bidirection)
712                         mi->callback(mi->local_id, mi->remote_app_id, pkt->remote_port_name, pkt->is_trusted, kb, NULL);
713                 else
714                         mi->callback(mi->local_id, mi->remote_app_id, NULL, pkt->is_trusted, kb, NULL);
715
716                 bundle_free(kb);
717                 if (pkt) {
718                         if (pkt->remote_port_name)
719                                 free(pkt->remote_port_name);
720                         if (pkt->data)
721                                 free(pkt->data);
722                         free(pkt);
723                 }
724         }
725
726         return TRUE;
727 }
728
729 static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation)
730 {
731         char *local_port = NULL;
732         char *local_appid = NULL;
733         char *remote_appid = NULL;
734         char *remote_port = NULL;
735         gboolean local_trusted = false;
736         gboolean remote_trusted = false;
737         gboolean bi_dir = false;
738         int len = 0;
739
740         bundle *data = NULL;
741         bundle_raw *raw = NULL;
742         message_port_local_port_info_s *mi;
743         int local_reg_id = 0;
744         message_port_callback_info_s *callback_info;
745         message_port_callback_info_s *head_callback_info;
746         GList *callback_info_list = NULL;
747
748         char buf[1024];
749         GDBusMessage *msg;
750         GUnixFDList *fd_list;
751         int fd_len;
752         int *returned_fds = NULL;
753         int fd;
754
755         g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
756                         &remote_appid, &remote_port, &remote_trusted, &len, &raw);
757
758         if (!remote_port) {
759                 _LOGE("Invalid argument : remote_port is NULL");
760                 goto out;
761         }
762         if (!remote_appid) {
763                 _LOGE("Invalid argument : remote_appid is NULL");
764                 goto out;
765         }
766         if (!__is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
767                 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
768                 goto out;
769         }
770         if (!local_appid) {
771                 _LOGE("Invalid argument : local_appid");
772                 goto out;
773         }
774         if (!local_port) {
775                 _LOGE("Invalid argument : local_port");
776                 goto out;
777         }
778         if (strcmp(remote_appid, __app_id) != 0) {
779                 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
780                 goto out;
781         }
782         if (strcmp(remote_port, mi->port_name) != 0) {
783                 _LOGE("Invalid argument : remote_port (%s)", remote_port);
784                 goto out;
785         }
786         if (!len) {
787                 _LOGE("Invalid argument : data_len");
788                 goto out;
789         }
790         if (remote_trusted) {
791                 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
792                         if (!__is_preloaded(local_appid, remote_appid)) {
793                                 int ret = __check_certificate(local_appid, remote_appid);
794                                 if (ret == MESSAGEPORT_ERROR_NONE)
795                                         g_hash_table_insert(__trusted_app_list_hash, local_appid, "TRUE");
796                                 else {
797                                         _LOGE("The application (%s) is not signed with the same certificate",
798                                                         local_appid);
799                                         goto out;
800                                 }
801                         }
802                 }
803         }
804
805         callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
806         if (callback_info == NULL)
807                 goto out;
808
809         callback_info->local_id = mi->local_id;
810         callback_info->remote_app_id = strdup(local_appid);
811         callback_info->callback = mi->callback;
812
813         msg = g_dbus_method_invocation_get_message(invocation);
814         fd_list = g_dbus_message_get_unix_fd_list(msg);
815
816         /* When application send message to self fd_list is NULL */
817         if (fd_list != NULL) {
818                 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
819                 if (returned_fds == NULL) {
820                         _LOGE("fail to get fds");
821                         __callback_info_free(callback_info);
822                         return -1;
823                 }
824                 fd = returned_fds[0];
825
826                 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
827                 if (fd > 0) {
828
829                         callback_info->gio_read = g_io_channel_unix_new(fd);
830                         if (!callback_info->gio_read) {
831                                 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
832                                 __callback_info_free(callback_info);
833                                 return -1;
834                         }
835
836                         callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
837                                         __socket_request_handler, (gpointer)callback_info);
838                         if (callback_info->g_src_id == 0) {
839                                 _LOGE("fail to add watch on socket");
840                                 __callback_info_free(callback_info);
841                                 return -1;
842                         }
843
844                         callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
845                         if (callback_info_list == NULL) {
846                                 head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
847                                 if (head_callback_info == NULL) {
848                                         _LOGE("fail to alloc head_callback_info");
849                                         __callback_info_free(callback_info);
850                                         return -1;
851                                 }
852                                 head_callback_info->local_id = 0;
853                                 head_callback_info->remote_app_id = NULL;
854                                 head_callback_info->callback = NULL;
855                                 head_callback_info->gio_read = NULL;
856                                 head_callback_info->g_src_id = 0;
857                                 callback_info_list = g_list_append(callback_info_list, head_callback_info);
858                                 callback_info_list = g_list_append(callback_info_list, callback_info);
859                                 g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(mi->local_id), callback_info_list);
860                         } else {
861                                 callback_info_list = g_list_append(callback_info_list, callback_info);
862                         }
863                 }
864         }
865
866         data = bundle_decode(raw, len);
867         if (!data) {
868                 _LOGE("Invalid argument : message");
869                 goto out;
870         }
871
872         LOGI("call calback %s", local_appid);
873         if (bi_dir)
874                 mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
875         else
876                 mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
877         bundle_free(data);
878 out:
879         if (returned_fds)
880                 free(returned_fds);
881
882         return true;
883 }
884
885 static int __check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
886 {
887         _LOGI("Check a remote port : [%s:%s]", remote_app_id, remote_port);
888
889         GVariant *result = NULL;
890         GError *err = NULL;
891         int ret_val = MESSAGEPORT_ERROR_NONE;
892         char *bus_name = NULL;
893         message_port_remote_app_info_s *remote_app_info = NULL;
894         port_list_info_s *port_info = NULL;
895         int local_reg_id = 0;
896         message_port_local_port_info_s *mi = NULL;
897         gboolean name_exist = false;
898
899         _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
900
901         ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
902         if (ret_val != MESSAGEPORT_ERROR_NONE)
903                 return ret_val;
904
905         /* self check */
906         if (strcmp(remote_app_id, __app_id) == 0) {
907
908                 _LOGI("__is_local_port_registed ");
909                 if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
910                         *exist = false;
911                 else
912                         *exist = true;
913
914                 _LOGI("__is_local_port_registed : %d ", *exist);
915                 return MESSAGEPORT_ERROR_NONE;
916         }
917
918         port_info->exist = false;
919         bus_name = port_info->encoded_bus_name;
920
921         result = g_dbus_connection_call_sync(
922                         __gdbus_conn,
923                         DBUS_SERVICE_DBUS,
924                         DBUS_PATH_DBUS,
925                         DBUS_INTERFACE_DBUS,
926                         "NameHasOwner",
927                         g_variant_new("(s)", bus_name),
928                         G_VARIANT_TYPE("(b)"),
929                         G_DBUS_CALL_FLAGS_NONE,
930                         -1,
931                         NULL,
932                         &err);
933
934         if (err || (result == NULL)) {
935                 if (err) {
936                         _LOGE("No reply. error = %s", err->message);
937                         g_error_free(err);
938                 }
939                 ret_val = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
940         } else {
941                 g_variant_get(result, "(b)", &name_exist);
942
943                 if (!name_exist) {
944                         LOGE("Name not exist %s", bus_name);
945                         *exist = false;
946                         ret_val = MESSAGEPORT_ERROR_NONE;
947                 } else {
948
949                         if (is_trusted) {
950                                 if (remote_app_info->certificate_info != CERTIFICATE_MATCH) {
951                                         if (!__is_preloaded(__app_id, remote_app_id)) {
952                                                 if (__check_certificate(__app_id, remote_app_id) != MESSAGEPORT_ERROR_NONE) {
953                                                         ret_val = MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
954                                                         goto out;
955                                                 }
956                                         }
957                                         remote_app_info->certificate_info = CERTIFICATE_MATCH;
958                                 }
959                         }
960                         port_info->exist = true;
961                         *exist = true;
962                         ret_val = MESSAGEPORT_ERROR_NONE;
963                 }
964         }
965 out:
966         if (result)
967                 g_variant_unref(result);
968
969         return ret_val;
970 }
971
972 static void __on_sender_name_appeared(GDBusConnection *connection,
973                 const gchar     *name,
974                 const gchar     *name_owner,
975                 gpointer         user_data)
976 {
977         _LOGI("sender name appeared : %s", name);
978 }
979
980 static void __on_sender_name_vanished(GDBusConnection *connection,
981                 const gchar     *name,
982                 gpointer         user_data)
983 {
984         gboolean remove_result = FALSE;
985         int *watcher_id = (int *)user_data;
986         remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
987         if (!remove_result)
988                 _LOGE("Fail to remove sender appid from hash : %s", name);
989
990         g_bus_unwatch_name(*watcher_id);
991         free(watcher_id);
992 }
993
994 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
995 {
996         int ret = 0;
997         char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
998         char *local_appid = NULL;
999         int pid = __get_sender_pid(conn, sender);
1000         int *watcher_id = (int *)calloc(1, sizeof(int));
1001
1002         ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
1003         if (ret != AUL_R_OK) {
1004                 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
1005                 free(watcher_id);
1006                 return false;
1007         }
1008
1009         g_variant_get_child(parameters, 0, "&s", &local_appid);
1010         if (local_appid == NULL) {
1011                 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
1012                 free(watcher_id);
1013                 return false;
1014         }
1015
1016         if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
1017                 _LOGI("insert sender !!!!! %s", sender);
1018                 g_hash_table_insert(__sender_appid_hash, (gpointer)strdup(sender), GINT_TO_POINTER(pid));
1019                 *watcher_id = g_bus_watch_name_on_connection(
1020                                         __gdbus_conn,
1021                                         sender,
1022                                         G_BUS_NAME_WATCHER_FLAGS_NONE,
1023                                         __on_sender_name_appeared,
1024                                         __on_sender_name_vanished,
1025                                         watcher_id,
1026                                         NULL);
1027         } else {
1028                 free(watcher_id);
1029                 return false;
1030         }
1031         return true;
1032 }
1033
1034 static void __dbus_method_call_handler(GDBusConnection *conn,
1035                                 const gchar *sender, const gchar *object_path,
1036                                 const gchar *iface_name, const gchar *method_name,
1037                                 GVariant *parameters, GDBusMethodInvocation *invocation,
1038                                 gpointer user_data)
1039 {
1040         _LOGI("method_name: %s, sender: %s", method_name, sender);
1041         gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender);
1042         if (sender_pid == NULL) {
1043                 if (!__check_sender_validation(parameters, sender, conn))
1044                         goto out;
1045         }
1046         if (g_strcmp0(method_name, "send_message") == 0)
1047                 send_message(parameters, invocation);
1048 out:
1049         g_dbus_method_invocation_return_value(invocation, NULL);
1050 }
1051
1052 static const GDBusInterfaceVTable interface_vtable = {
1053         __dbus_method_call_handler,
1054         NULL,
1055         NULL
1056 };
1057
1058 static int __dbus_init(void)
1059 {
1060         bool ret = false;
1061         GError *error = NULL;
1062
1063         __gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
1064         if (__gdbus_conn == NULL) {
1065                 if (error != NULL) {
1066                         _LOGE("Failed to get dbus [%s]", error->message);
1067                         g_error_free(error);
1068                 }
1069                 goto out;
1070         }
1071
1072         ret = true;
1073
1074 out:
1075         if (!__gdbus_conn)
1076                 g_object_unref(__gdbus_conn);
1077
1078         return ret;
1079
1080 }
1081
1082 int __register_dbus_interface(const char *port_name, bool is_trusted)
1083 {
1084
1085         GDBusNodeInfo *introspection_data = NULL;
1086         int registration_id = 0;
1087
1088         static gchar introspection_prefix[] =
1089                 "<node>"
1090                 "  <interface name='";
1091
1092         static gchar introspection_postfix[] =
1093                 "'>"
1094                 "        <method name='send_message'>"
1095                 "          <arg type='s' name='local_appid' direction='in'/>"
1096                 "          <arg type='s' name='local_port' direction='in'/>"
1097                 "          <arg type='b' name='local_trusted' direction='in'/>"
1098                 "          <arg type='b' name='bi_dir' direction='in'/>"
1099                 "          <arg type='s' name='remote_appid' direction='in'/>"
1100                 "          <arg type='s' name='remote_port' direction='in'/>"
1101                 "          <arg type='b' name='remote_trusted' direction='in'/>"
1102                 "          <arg type='u' name='data_len' direction='in'/>"
1103                 "          <arg type='s' name='data' direction='in'/>"
1104                 "        </method>"
1105                 "  </interface>"
1106                 "</node>";
1107
1108         char *introspection_xml = NULL;
1109         int introspection_xml_len = 0;
1110
1111
1112         int owner_id = 0;
1113         GError *error = NULL;
1114         char *bus_name = NULL;
1115         char *interface_name = NULL;
1116         GVariant *result = NULL;
1117
1118         bus_name = __get_encoded_name(__app_id, port_name, is_trusted);
1119         if (!bus_name) {
1120                 _LOGE("Fail to get bus name");
1121                 goto out;
1122         }
1123         interface_name = bus_name;
1124
1125         introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
1126                 strlen(introspection_postfix) + 1;
1127
1128         introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
1129         if (!introspection_xml) {
1130                 _LOGE("out of memory");
1131                 goto out;
1132         }
1133
1134
1135         result = g_dbus_connection_call_sync(
1136                         __gdbus_conn,
1137                         DBUS_SERVICE_DBUS,
1138                         DBUS_PATH_DBUS,
1139                         DBUS_INTERFACE_DBUS,
1140                         "RequestName",
1141                         g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
1142                         G_VARIANT_TYPE("(u)"),
1143                         G_DBUS_CALL_FLAGS_NONE,
1144                         -1,
1145                         NULL,
1146                         &error);
1147         if (error) {
1148                 _LOGE("RequestName fail : %s", error->message);
1149                 goto out;
1150         }
1151         if (result == NULL) {
1152                 _LOGE("fail to get name NULL");
1153                 goto out;
1154         }
1155         g_variant_get(result, "(u)", &owner_id);
1156         if (owner_id == 0) {
1157                 _LOGE("Acquiring the own name is failed");
1158                 goto out;
1159         }
1160
1161         _LOGI("Acquiring the own name : %d", owner_id);
1162
1163         snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
1164
1165         introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
1166         if (!introspection_data) {
1167                 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
1168                 goto out;
1169         }
1170
1171         registration_id = g_dbus_connection_register_object(__gdbus_conn,
1172                                                 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
1173                                                 &interface_vtable, NULL, NULL, NULL);
1174
1175         _LOGI("registration_id %d", registration_id);
1176
1177         if (registration_id == 0) {
1178                 _LOGE("Failed to g_dbus_connection_register_object");
1179                 goto out;
1180         }
1181
1182 out:
1183         if (introspection_data)
1184                 g_dbus_node_info_unref(introspection_data);
1185         if (introspection_xml)
1186                 free(introspection_xml);
1187         if (bus_name)
1188                 free(bus_name);
1189         if (result)
1190                 g_variant_unref(result);
1191
1192
1193         return registration_id;
1194 }
1195
1196 /* LCOV_EXCL_START */
1197 void __list_free_port_list(gpointer data)
1198 {
1199         port_list_info_s *n = (port_list_info_s *)data;
1200
1201         FREE_AND_NULL(n->encoded_bus_name);
1202         FREE_AND_NULL(n->port_name);
1203         FREE_AND_NULL(n);
1204 }
1205 /* LCOV_EXCL_STOP */
1206
1207 /* LCOV_EXCL_START */
1208 static void __hash_destory_local_value(gpointer data)
1209 {
1210         message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
1211         if (mli) {
1212                 if (mli->port_name)
1213                         free(mli->port_name);
1214                 free(mli);
1215         }
1216 }
1217 /* LCOV_EXCL_STOP */
1218
1219 /* LCOV_EXCL_START */
1220 static void __hash_destory_remote_value(gpointer data)
1221 {
1222         message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
1223         if (mri) {
1224                 FREE_AND_NULL(mri->sender_id);
1225                 FREE_AND_NULL(mri->remote_app_id);
1226                 if (mri->port_list)
1227                         g_list_free_full(mri->port_list, __list_free_port_list);
1228
1229                 free(mri);
1230         }
1231 }
1232 /* LCOV_EXCL_STOP */
1233
1234 static bool __initialize(void)
1235 {
1236
1237 #if !GLIB_CHECK_VERSION(2, 35, 0)
1238         g_type_init();
1239 #endif
1240
1241         int pid = getpid();
1242         int ret = 0;
1243         char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
1244
1245         _LOGI("initialize");
1246         ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
1247         retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
1248
1249         __app_id = strdup(buffer);
1250         retvm_if(!__app_id, false, "Malloc failed");
1251         _LOGI("init : %s", __app_id);
1252
1253         if (__local_port_info == NULL) {
1254                 __local_port_info = g_hash_table_new_full(g_direct_hash,  g_direct_equal, NULL, __hash_destory_local_value);
1255                 retvm_if(!__local_port_info, false, "fail to create __local_port_info");
1256         }
1257
1258         if (__remote_app_info == NULL) {
1259                 __remote_app_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value);
1260                 retvm_if(!__remote_app_info, false, "fail to create __remote_app_info");
1261         }
1262
1263         if (__sender_appid_hash == NULL) {
1264                 __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
1265                 retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
1266         }
1267
1268         if (__trusted_app_list_hash == NULL) {
1269                 __trusted_app_list_hash = g_hash_table_new(g_str_hash, g_str_equal);
1270                 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
1271         }
1272
1273         if (__callback_info_hash == NULL) {
1274                 __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
1275                 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
1276         }
1277
1278         if (!__dbus_init())
1279                 return false;
1280         _initialized = true;
1281
1282         return true;
1283 }
1284
1285
1286 static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, messageport_message_cb callback)
1287 {
1288         message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
1289         retvm_if(!mi, false, "Malloc failed");
1290
1291         mi->callback = callback;
1292         mi->is_trusted = is_trusted;
1293         mi->port_name = strdup(local_port);
1294         if (mi->port_name == NULL) {
1295                 _LOGE("Malloc failed (%s)", local_port);
1296                 free(mi);
1297                 return false;
1298         }
1299         mi->local_id = local_id;
1300
1301         g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
1302         return true;
1303 }
1304
1305 static int __register_message_port(const char *local_port, bool is_trusted, messageport_message_cb callback)
1306 {
1307         _SECURE_LOGI("Register a message port : [%s:%s]", __app_id, local_port);
1308
1309         int local_id = 0;
1310
1311         /* Check the message port is already registed */
1312         if (__is_local_port_registed(local_port, is_trusted, &local_id, NULL))
1313                 return local_id;
1314
1315         local_id = __register_dbus_interface(local_port, is_trusted);
1316         if (local_id < 1) {
1317                 _LOGE("register_dbus_interface fail !!");
1318                 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1319         }
1320
1321         if (!__message_port_register_port(local_id, local_port, is_trusted, callback))
1322                 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1323
1324         return local_id;
1325 }
1326
1327 int __message_port_send_async(int sockfd, bundle *kb, const char *local_port,
1328                 bool local_trusted, bool is_bidirection)
1329 {
1330         int ret = 0;
1331         int data_len;
1332         int local_port_len = 0;
1333         unsigned int nb;
1334         bundle_raw *kb_data = NULL;
1335
1336         if (local_port != NULL)
1337                 local_port_len = strlen(local_port) + 1;
1338
1339         if (__write_string_to_socket(sockfd, local_port, local_port_len) != MESSAGEPORT_ERROR_NONE) {
1340                 _LOGE("write local_port fail");
1341                 return MESSAGEPORT_ERROR_IO_ERROR;
1342         }
1343
1344         if (__write_socket(sockfd, (char *)&is_bidirection, sizeof(is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
1345                 _LOGE("write is_bidirection fail");
1346                 return MESSAGEPORT_ERROR_IO_ERROR;
1347         }
1348
1349         if (__write_socket(sockfd, (char *)&local_trusted, sizeof(local_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
1350                 _LOGE("write local_trusted fail");
1351                 return MESSAGEPORT_ERROR_IO_ERROR;
1352         }
1353
1354         bundle_encode(kb, &kb_data, &data_len);
1355         if (kb_data == NULL) {
1356                 _LOGE("bundle encode fail");
1357                 ret = MESSAGEPORT_ERROR_IO_ERROR;
1358                 goto out;
1359         }
1360
1361         if (data_len > MAX_MESSAGE_SIZE) {
1362                 _LOGE("bigger than max size\n");
1363                 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1364                 goto out;
1365         }
1366
1367         if (__write_string_to_socket(sockfd, (void *)kb_data, data_len) != MESSAGEPORT_ERROR_NONE) {
1368                 _LOGE("write kb_data fail");
1369                 ret = MESSAGEPORT_ERROR_IO_ERROR;
1370         }
1371 out:
1372         if (kb_data)
1373                 free(kb_data);
1374
1375         return ret;
1376 }
1377
1378 static int __message_port_send_message(const char *remote_appid, const char *remote_port,
1379                 const char *local_port, bool trusted_message, bool local_trusted, bool bi_dir, bundle *message)
1380 {
1381
1382         int ret = MESSAGEPORT_ERROR_NONE;
1383         GUnixFDList *fd_list = NULL;
1384
1385         int len = 0;
1386         bundle_raw *raw = NULL;
1387         char *bus_name = NULL;
1388         char *interface_name = NULL;
1389
1390         message_port_remote_app_info_s *remote_app_info = NULL;
1391         port_list_info_s *port_info = NULL;
1392         GDBusMessage *msg = NULL;
1393         GError *err = NULL;
1394         GVariant *body = NULL;
1395         int sock_pair[2] = {0,};
1396
1397         ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
1398         if (ret != MESSAGEPORT_ERROR_NONE)
1399                 return ret;
1400
1401         if (port_info->exist == false) {
1402                 bool exist = false;
1403                 _LOGI("port exist check !!");
1404                 ret =  __check_remote_port(remote_appid, remote_port, trusted_message, &exist);
1405                 if (ret != MESSAGEPORT_ERROR_NONE) {
1406                         goto out;
1407                 } else if (!exist) {
1408                         ret = MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1409                         goto out;
1410                 }
1411         }
1412
1413         if (port_info->send_sock_fd > 0) {
1414                 ret = __message_port_send_async(port_info->send_sock_fd, message,
1415                                 (local_port) ? local_port : "", local_trusted, bi_dir);
1416         } else {
1417
1418                 bus_name = port_info->encoded_bus_name;
1419                 interface_name = bus_name;
1420
1421                 if (bundle_encode(message, &raw, &len) != BUNDLE_ERROR_NONE) {
1422                         ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
1423                         goto out;
1424                 }
1425
1426                 if (MAX_MESSAGE_SIZE < len) {
1427                         _LOGE("The size of message (%d) has exceeded the maximum limit.", len);
1428                         ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1429                         goto out;
1430                 }
1431
1432                 body = g_variant_new("(ssbbssbus)", __app_id, (local_port) ? local_port : "", local_trusted, bi_dir,
1433                                 remote_appid, remote_port, trusted_message, len, raw);
1434                 if (strcmp(remote_appid, __app_id) != 0) { /* self send */
1435
1436                         /*  if message-port fail to get socket pair, communicate using GDBus */
1437                         if (aul_request_message_port_socket_pair(sock_pair) != AUL_R_OK) {
1438                                 _LOGE("error create socket pair");
1439                         } else {
1440
1441                                 _LOGI("sock pair : %d, %d",
1442                                                 sock_pair[SOCK_PAIR_SENDER], sock_pair[SOCK_PAIR_RECEIVER]);
1443                                 fd_list = g_unix_fd_list_new();
1444                                 g_unix_fd_list_append(fd_list, sock_pair[SOCK_PAIR_RECEIVER], &err);
1445                                 if (err != NULL) {
1446                                         _LOGE("g_unix_fd_list_append [%s]", err->message);
1447                                         ret = MESSAGEPORT_ERROR_IO_ERROR;
1448                                         g_error_free(err);
1449                                         goto out;
1450                                 }
1451                                 port_info->send_sock_fd = sock_pair[SOCK_PAIR_SENDER];
1452                                 close(sock_pair[SOCK_PAIR_RECEIVER]);
1453                         }
1454                 }
1455
1456                 msg = g_dbus_message_new_method_call(bus_name, MESSAGEPORT_OBJECT_PATH, interface_name, "send_message");
1457                 if (!msg) {
1458                         _LOGE("Can't allocate new method call");
1459                         ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1460                         goto out;
1461                 }
1462
1463                 g_dbus_message_set_unix_fd_list(msg, fd_list);
1464                 g_dbus_message_set_body(msg, body);
1465                 g_dbus_connection_send_message(__gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
1466                 if (err != NULL) {
1467                         _LOGE("No reply. error = %s", err->message);
1468                         g_error_free(err);
1469                         ret = MESSAGEPORT_ERROR_IO_ERROR;
1470                         goto out;
1471                 }
1472
1473
1474         }
1475
1476 out:
1477         if (msg)
1478                 g_object_unref(msg);
1479         if (raw)
1480                 bundle_free_encoded_rawdata(&raw);
1481         if (fd_list)
1482                 g_object_unref(fd_list);
1483
1484
1485         return ret;
1486 }
1487
1488 int __message_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,  bool trusted_message, bundle *message)
1489 {
1490         message_port_local_port_info_s *local_info;
1491         int ret = __get_local_port_info(id, &local_info);
1492         if (ret != MESSAGEPORT_ERROR_NONE)
1493                 return ret;
1494
1495         _LOGI("bidirectional_message %s", local_info->port_name);
1496         return __message_port_send_message(remote_app_id, remote_port,
1497                         local_info->port_name, trusted_message, local_info->is_trusted, true, message);
1498 }
1499
1500 int messageport_unregister_local_port(int local_port_id, bool trusted_port)
1501 {
1502
1503         GVariant *result;
1504         char *bus_name = NULL;
1505         GError *err = NULL;
1506         int ret = 0;
1507
1508         _LOGI("unregister : %d", local_port_id);
1509
1510         message_port_local_port_info_s *mi =
1511                 (message_port_local_port_info_s *)
1512                 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
1513         if (mi == NULL)
1514                 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1515
1516         if (mi->is_trusted != trusted_port)
1517                 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
1518
1519         g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
1520
1521         bus_name = __get_encoded_name(__app_id, mi->port_name, mi->is_trusted);
1522         if (bus_name == NULL)
1523                 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1524
1525         g_dbus_connection_unregister_object(__gdbus_conn, local_port_id);
1526
1527         result = g_dbus_connection_call_sync(
1528                         __gdbus_conn,
1529                         DBUS_SERVICE_DBUS,
1530                         DBUS_PATH_DBUS,
1531                         DBUS_INTERFACE_DBUS,
1532                         "ReleaseName",
1533                         g_variant_new("(s)", bus_name),
1534                         G_VARIANT_TYPE("(u)"),
1535                         G_DBUS_CALL_FLAGS_NONE,
1536                         -1,
1537                         NULL,
1538                         &err);
1539
1540         if (bus_name)
1541                 free(bus_name);
1542
1543         if (err) {
1544                 _LOGE("RequestName fail : %s", err->message);
1545                 g_error_free(err);
1546                 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1547         }
1548         g_variant_get(result, "(u)", &ret);
1549
1550         if (result)
1551                 g_variant_unref(result);
1552
1553         if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
1554
1555                 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
1556                         _LOGE("Port Not exist");
1557                         return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1558                 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
1559                         _LOGE("Try to release not owned name. MESSAGEPORT_ERROR_INVALID_PARAMETER");
1560                         return MESSAGEPORT_ERROR_INVALID_PARAMETER;
1561                 }
1562         }
1563
1564
1565         g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
1566
1567         return MESSAGEPORT_ERROR_NONE;
1568 }
1569
1570 int messageport_register_local_port(const char *local_port, messageport_message_cb callback)
1571 {
1572         if (!_initialized) {
1573                 if (!__initialize())
1574                         return MESSAGEPORT_ERROR_IO_ERROR;
1575         }
1576
1577         return __register_message_port(local_port, false, callback);
1578 }
1579
1580 int messageport_register_trusted_local_port(const char *local_port, messageport_message_cb callback)
1581 {
1582         if (!_initialized) {
1583                 if (!__initialize())
1584                         return MESSAGEPORT_ERROR_IO_ERROR;
1585         }
1586
1587         return __register_message_port(local_port, true, callback);
1588
1589 }
1590
1591 int messageport_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1592 {
1593         if (!_initialized) {
1594                 if (!__initialize())
1595                         return MESSAGEPORT_ERROR_IO_ERROR;
1596         }
1597
1598         int ret = __check_remote_port(remote_app_id, remote_port, false, exist);
1599         if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1600                 *exist = false;
1601                 ret = MESSAGEPORT_ERROR_NONE;
1602         }
1603
1604         return ret;
1605 }
1606
1607 int messageport_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1608 {
1609         if (!_initialized) {
1610                 if (!__initialize())
1611                         return MESSAGEPORT_ERROR_IO_ERROR;
1612         }
1613
1614         int ret = __check_remote_port(remote_app_id, remote_port, true, exist);
1615         if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1616                 *exist = false;
1617                 ret = MESSAGEPORT_ERROR_NONE;
1618         }
1619
1620         return ret;
1621 }
1622
1623 int messageport_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
1624 {
1625         if (!_initialized) {
1626                 if (!__initialize())
1627                         return MESSAGEPORT_ERROR_IO_ERROR;
1628         }
1629
1630         return __message_port_send_message(remote_app_id, remote_port, NULL, false, false, false, message);
1631 }
1632
1633 int messageport_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
1634 {
1635         if (!_initialized) {
1636                 if (!__initialize())
1637                         return MESSAGEPORT_ERROR_IO_ERROR;
1638         }
1639
1640         return __message_port_send_message(remote_app_id, remote_port, NULL, true, false, false, message);
1641 }
1642
1643 int messageport_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,
1644                 bundle *message)
1645 {
1646         if (!_initialized) {
1647                 if (!__initialize())
1648                         return MESSAGEPORT_ERROR_IO_ERROR;
1649         }
1650
1651         return __message_send_bidirectional_message(id, remote_app_id, remote_port, false, message);
1652 }
1653
1654 int messageport_send_bidirectional_trusted_message(int id, const char *remote_app_id, const char *remote_port,
1655                 bundle *message)
1656 {
1657         if (!_initialized) {
1658                 if (!__initialize())
1659                         return MESSAGEPORT_ERROR_IO_ERROR;
1660         }
1661         return __message_send_bidirectional_message(id, remote_app_id, remote_port, true, message);
1662 }
1663