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