Fix sender hash's wrong value destory func
[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         returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
784         fd = returned_fds[0];
785
786         LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
787         if (fd > 0) {
788
789                 callback_info->gio_read = g_io_channel_unix_new(fd);
790                 if (!callback_info->gio_read) {
791                         _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
792                         __callback_info_free(callback_info);
793                         return -1;
794                 }
795
796                 callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
797                                 __socket_request_handler, (gpointer)callback_info);
798                 if (callback_info->g_src_id == 0) {
799                         _LOGE("fail to add watch on socket");
800                         __callback_info_free(callback_info);
801                         return -1;
802                 }
803
804         }
805
806         data = bundle_decode(raw, len);
807         if (!data) {
808                 _LOGE("Invalid argument : message");
809                 goto out;
810         }
811
812         LOGI("call calback %s", local_appid);
813         if (bi_dir)
814                 mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
815         else
816                 mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
817         bundle_free(data);
818 out:
819         if (returned_fds)
820                 free(returned_fds);
821
822         return true;
823 }
824
825 static int __check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
826 {
827         _LOGI("Check a remote port : [%s:%s]", remote_app_id, remote_port);
828
829         GVariant *result = NULL;
830         GError *err = NULL;
831         int ret_val = MESSAGEPORT_ERROR_NONE;
832         char *bus_name = NULL;
833         message_port_remote_app_info_s *remote_app_info = NULL;
834         port_list_info_s *port_info = NULL;
835         int local_reg_id = 0;
836         message_port_local_port_info_s *mi = NULL;
837         gboolean name_exist = false;
838
839         _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
840
841         ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
842         if (ret_val != MESSAGEPORT_ERROR_NONE)
843                 return ret_val;
844
845         /* self check */
846         if (strcmp(remote_app_id, __app_id) == 0) {
847
848                 _LOGI("__is_local_port_registed ");
849                 if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
850                         *exist = false;
851                 else
852                         *exist = true;
853
854                 _LOGI("__is_local_port_registed : %d ", *exist);
855                 return MESSAGEPORT_ERROR_NONE;
856         }
857
858         port_info->exist = false;
859         bus_name = port_info->encoded_bus_name;
860
861         result = g_dbus_connection_call_sync(
862                         __gdbus_conn,
863                         DBUS_SERVICE_DBUS,
864                         DBUS_PATH_DBUS,
865                         DBUS_INTERFACE_DBUS,
866                         "NameHasOwner",
867                         g_variant_new("(s)", bus_name),
868                         G_VARIANT_TYPE("(b)"),
869                         G_DBUS_CALL_FLAGS_NONE,
870                         -1,
871                         NULL,
872                         &err);
873
874         if (err || (result == NULL)) {
875                 if (err) {
876                         _LOGE("No reply. error = %s", err->message);
877                         g_error_free(err);
878                 }
879                 ret_val = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
880         } else {
881                 g_variant_get(result, "(b)", &name_exist);
882
883                 if (!name_exist) {
884                         LOGE("Name not exist %s", bus_name);
885                         *exist = false;
886                         ret_val = MESSAGEPORT_ERROR_NONE;
887                 } else {
888
889                         if (is_trusted) {
890                                 if (remote_app_info->certificate_info != CERTIFICATE_MATCH) {
891                                         if (!__is_preloaded(__app_id, remote_app_id)) {
892                                                 if (__check_certificate(__app_id, remote_app_id) != MESSAGEPORT_ERROR_NONE) {
893                                                         ret_val = MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
894                                                         goto out;
895                                                 }
896                                         }
897                                         remote_app_info->certificate_info = CERTIFICATE_MATCH;
898                                 }
899                         }
900                         port_info->exist = true;
901                         *exist = true;
902                         ret_val = MESSAGEPORT_ERROR_NONE;
903                 }
904         }
905 out:
906         if (result)
907                 g_variant_unref(result);
908
909         return ret_val;
910 }
911
912 static void __on_sender_name_appeared(GDBusConnection *connection,
913                 const gchar     *name,
914                 const gchar     *name_owner,
915                 gpointer         user_data)
916 {
917         _LOGI("sender name appeared : %s", name);
918 }
919
920 static void __on_sender_name_vanished(GDBusConnection *connection,
921                 const gchar     *name,
922                 gpointer         user_data)
923 {
924         gboolean remove_result = FALSE;
925         int *watcher_id = (int *)user_data;
926         remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
927         if (!remove_result)
928                 _LOGE("Fail to remove sender appid from hash : %s", name);
929
930         g_bus_unwatch_name(*watcher_id);
931         free(watcher_id);
932 }
933
934 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
935 {
936         int ret = 0;
937         char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
938         char *local_appid = NULL;
939         int pid = __get_sender_pid(conn, sender);
940         int *watcher_id = (int *)calloc(1, sizeof(int));
941
942         ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
943         if (ret != AUL_R_OK) {
944                 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
945                 free(watcher_id);
946                 return false;
947         }
948
949         g_variant_get_child(parameters, 0, "&s", &local_appid);
950         if (local_appid == NULL) {
951                 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
952                 free(watcher_id);
953                 return false;
954         }
955
956         if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
957                 _LOGI("insert sender !!!!! %s", sender);
958                 g_hash_table_insert(__sender_appid_hash, (gpointer)strdup(sender), GINT_TO_POINTER(pid));
959                 *watcher_id = g_bus_watch_name_on_connection(
960                                         __gdbus_conn,
961                                         sender,
962                                         G_BUS_NAME_WATCHER_FLAGS_NONE,
963                                         __on_sender_name_appeared,
964                                         __on_sender_name_vanished,
965                                         watcher_id,
966                                         NULL);
967         } else {
968                 free(watcher_id);
969                 return false;
970         }
971         return true;
972 }
973
974 static void __dbus_method_call_handler(GDBusConnection *conn,
975                                 const gchar *sender, const gchar *object_path,
976                                 const gchar *iface_name, const gchar *method_name,
977                                 GVariant *parameters, GDBusMethodInvocation *invocation,
978                                 gpointer user_data)
979 {
980         _LOGI("method_name: %s, sender: %s", method_name, sender);
981         gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender);
982         if (sender_pid == NULL) {
983                 if (!__check_sender_validation(parameters, sender, conn))
984                         goto out;
985         }
986         if (g_strcmp0(method_name, "send_message") == 0)
987                 send_message(parameters, invocation);
988 out:
989         g_dbus_method_invocation_return_value(invocation, NULL);
990 }
991
992 static const GDBusInterfaceVTable interface_vtable = {
993         __dbus_method_call_handler,
994         NULL,
995         NULL
996 };
997
998 static int __dbus_init(void)
999 {
1000         bool ret = false;
1001         GError *error = NULL;
1002
1003         __gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
1004         if (__gdbus_conn == NULL) {
1005                 if (error != NULL) {
1006                         _LOGE("Failed to get dbus [%s]", error->message);
1007                         g_error_free(error);
1008                 }
1009                 goto out;
1010         }
1011
1012         ret = true;
1013
1014 out:
1015         if (!__gdbus_conn)
1016                 g_object_unref(__gdbus_conn);
1017
1018         return ret;
1019
1020 }
1021
1022 int __register_dbus_interface(const char *port_name, bool is_trusted)
1023 {
1024
1025         GDBusNodeInfo *introspection_data = NULL;
1026         int registration_id = 0;
1027
1028         static gchar introspection_prefix[] =
1029                 "<node>"
1030                 "  <interface name='";
1031
1032         static gchar introspection_postfix[] =
1033                 "'>"
1034                 "        <method name='send_message'>"
1035                 "          <arg type='s' name='local_appid' direction='in'/>"
1036                 "          <arg type='s' name='local_port' direction='in'/>"
1037                 "          <arg type='b' name='local_trusted' direction='in'/>"
1038                 "          <arg type='b' name='bi_dir' direction='in'/>"
1039                 "          <arg type='s' name='remote_appid' direction='in'/>"
1040                 "          <arg type='s' name='remote_port' direction='in'/>"
1041                 "          <arg type='b' name='remote_trusted' direction='in'/>"
1042                 "          <arg type='u' name='data_len' direction='in'/>"
1043                 "          <arg type='s' name='data' direction='in'/>"
1044                 "        </method>"
1045                 "  </interface>"
1046                 "</node>";
1047
1048         char *introspection_xml = NULL;
1049         int introspection_xml_len = 0;
1050
1051
1052         int owner_id = 0;
1053         GError *error = NULL;
1054         char *bus_name = NULL;
1055         char *interface_name = NULL;
1056         GVariant *result = NULL;
1057
1058         bus_name = __get_encoded_name(__app_id, port_name, is_trusted);
1059         if (!bus_name) {
1060                 _LOGE("Fail to get bus name");
1061                 goto out;
1062         }
1063         interface_name = bus_name;
1064
1065         introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
1066                 strlen(introspection_postfix) + 1;
1067
1068         introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
1069         if (!introspection_xml) {
1070                 _LOGE("out of memory");
1071                 goto out;
1072         }
1073
1074
1075         result = g_dbus_connection_call_sync(
1076                         __gdbus_conn,
1077                         DBUS_SERVICE_DBUS,
1078                         DBUS_PATH_DBUS,
1079                         DBUS_INTERFACE_DBUS,
1080                         "RequestName",
1081                         g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
1082                         G_VARIANT_TYPE("(u)"),
1083                         G_DBUS_CALL_FLAGS_NONE,
1084                         -1,
1085                         NULL,
1086                         &error);
1087         if (error) {
1088                 _LOGE("RequestName fail : %s", error->message);
1089                 goto out;
1090         }
1091         if (result == NULL) {
1092                 _LOGE("fail to get name NULL");
1093                 goto out;
1094         }
1095         g_variant_get(result, "(u)", &owner_id);
1096         if (owner_id == 0) {
1097                 _LOGE("Acquiring the own name is failed");
1098                 goto out;
1099         }
1100
1101         _LOGI("Acquiring the own name : %d", owner_id);
1102
1103         snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
1104
1105         introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
1106         if (!introspection_data) {
1107                 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
1108                 goto out;
1109         }
1110
1111         registration_id = g_dbus_connection_register_object(__gdbus_conn,
1112                                                 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
1113                                                 &interface_vtable, NULL, NULL, NULL);
1114
1115         _LOGI("registration_id %d", registration_id);
1116
1117         if (registration_id == 0) {
1118                 _LOGE("Failed to g_dbus_connection_register_object");
1119                 goto out;
1120         }
1121
1122 out:
1123         if (introspection_data)
1124                 g_dbus_node_info_unref(introspection_data);
1125         if (introspection_xml)
1126                 free(introspection_xml);
1127         if (bus_name)
1128                 free(bus_name);
1129         if (result)
1130                 g_variant_unref(result);
1131
1132
1133         return registration_id;
1134 }
1135
1136
1137 void __list_free_port_list(gpointer data)
1138 {
1139         port_list_info_s *n = (port_list_info_s *)data;
1140
1141         FREE_AND_NULL(n->encoded_bus_name);
1142         FREE_AND_NULL(n->port_name);
1143         FREE_AND_NULL(n);
1144 }
1145
1146 static void __hash_destory_local_value(gpointer data)
1147 {
1148         message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
1149         if (mli) {
1150                 if (mli->port_name)
1151                         free(mli->port_name);
1152                 free(mli);
1153         }
1154 }
1155
1156 static void __hash_destory_remote_value(gpointer data)
1157 {
1158         message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
1159         if (mri) {
1160                 FREE_AND_NULL(mri->sender_id);
1161                 FREE_AND_NULL(mri->remote_app_id);
1162                 if (mri->port_list)
1163                         g_list_free_full(mri->port_list, __list_free_port_list);
1164
1165                 free(mri);
1166         }
1167 }
1168
1169 static bool __initialize(void)
1170 {
1171
1172 #if !GLIB_CHECK_VERSION(2, 35, 0)
1173         g_type_init();
1174 #endif
1175
1176         int pid = getpid();
1177         int ret = 0;
1178         char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
1179
1180         _LOGI("initialize");
1181         ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
1182         retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
1183
1184         __app_id = strdup(buffer);
1185         retvm_if(!__app_id, false, "Malloc failed");
1186         _LOGI("init : %s", __app_id);
1187
1188         if (__local_port_info == NULL) {
1189                 __local_port_info = g_hash_table_new_full(g_direct_hash,  g_direct_equal, NULL, __hash_destory_local_value);
1190                 retvm_if(!__local_port_info, false, "fail to create __local_port_info");
1191         }
1192
1193         if (__remote_app_info == NULL) {
1194                 __remote_app_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value);
1195                 retvm_if(!__remote_app_info, false, "fail to create __remote_app_info");
1196         }
1197
1198         if (__sender_appid_hash == NULL) {
1199                 __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
1200                 retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
1201         }
1202
1203         if (__trusted_app_list_hash == NULL) {
1204                 __trusted_app_list_hash = g_hash_table_new(g_str_hash, g_str_equal);
1205                 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
1206         }
1207
1208         if (!__dbus_init())
1209                 return false;
1210         _initialized = true;
1211
1212         return true;
1213 }
1214
1215
1216 static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, messageport_message_cb callback)
1217 {
1218         message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
1219         retvm_if(!mi, false, "Malloc failed");
1220
1221         mi->callback = callback;
1222         mi->is_trusted = is_trusted;
1223         mi->port_name = strdup(local_port);
1224         if (mi->port_name == NULL) {
1225                 _LOGE("Malloc failed (%s)", local_port);
1226                 free(mi);
1227                 return false;
1228         }
1229         mi->local_id = local_id;
1230
1231         g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
1232         return true;
1233 }
1234
1235 static int __register_message_port(const char *local_port, bool is_trusted, messageport_message_cb callback)
1236 {
1237         _SECURE_LOGI("Register a message port : [%s:%s]", __app_id, local_port);
1238
1239         int local_id = 0;
1240
1241         /* Check the message port is already registed */
1242         if (__is_local_port_registed(local_port, is_trusted, &local_id, NULL))
1243                 return local_id;
1244
1245         local_id = __register_dbus_interface(local_port, is_trusted);
1246         if (local_id < 1) {
1247                 _LOGE("register_dbus_interface fail !!");
1248                 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1249         }
1250
1251         if (!__message_port_register_port(local_id, local_port, is_trusted, callback))
1252                 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1253
1254         return local_id;
1255 }
1256
1257 int __message_port_send_async(int sockfd, bundle *kb, const char *local_port,
1258                 bool local_trusted, bool is_bidirection)
1259 {
1260         int ret = 0;
1261         int data_len;
1262         int local_port_len = 0;
1263         unsigned int nb;
1264         bundle_raw *kb_data = NULL;
1265
1266         if (local_port != NULL)
1267                 local_port_len = strlen(local_port) + 1;
1268
1269         if (__write_string_to_socket(sockfd, local_port, local_port_len) != MESSAGEPORT_ERROR_NONE) {
1270                 _LOGE("write local_port fail");
1271                 return MESSAGEPORT_ERROR_IO_ERROR;
1272         }
1273
1274         if (__write_socket(sockfd, (char *)&is_bidirection, sizeof(is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
1275                 _LOGE("write is_bidirection fail");
1276                 return MESSAGEPORT_ERROR_IO_ERROR;
1277         }
1278
1279         if (__write_socket(sockfd, (char *)&local_trusted, sizeof(local_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
1280                 _LOGE("write local_trusted fail");
1281                 return MESSAGEPORT_ERROR_IO_ERROR;
1282         }
1283
1284         bundle_encode(kb, &kb_data, &data_len);
1285         if (kb_data == NULL) {
1286                 _LOGE("bundle encode fail");
1287                 ret = MESSAGEPORT_ERROR_IO_ERROR;
1288                 goto out;
1289         }
1290
1291         if (data_len > MAX_MESSAGE_SIZE) {
1292                 _LOGE("bigger than max size\n");
1293                 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1294                 goto out;
1295         }
1296
1297         if (__write_string_to_socket(sockfd, (void *)kb_data, data_len) != MESSAGEPORT_ERROR_NONE) {
1298                 _LOGE("write kb_data fail");
1299                 ret = MESSAGEPORT_ERROR_IO_ERROR;
1300         }
1301 out:
1302         if (kb_data)
1303                 free(kb_data);
1304
1305         return ret;
1306 }
1307
1308 static int __message_port_send_message(const char *remote_appid, const char *remote_port,
1309                 const char *local_port, bool trusted_message, bool local_trusted, bool bi_dir, bundle *message)
1310 {
1311
1312         int ret = MESSAGEPORT_ERROR_NONE;
1313         GUnixFDList *fd_list = NULL;
1314         GError *error = NULL;
1315
1316         int len = 0;
1317         bundle_raw *raw = NULL;
1318         char *bus_name = NULL;
1319         char *interface_name = NULL;
1320
1321         message_port_remote_app_info_s *remote_app_info = NULL;
1322         port_list_info_s *port_info = NULL;
1323         GDBusMessage *msg = NULL;
1324         GError *err = NULL;
1325         GVariant *body = NULL;
1326         int sock_pair[2] = {0,};
1327
1328         ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
1329         if (ret != MESSAGEPORT_ERROR_NONE)
1330                 return ret;
1331
1332         if (port_info->exist == false) {
1333                 bool exist = false;
1334                 _LOGI("port exist check !!");
1335                 ret =  __check_remote_port(remote_appid, remote_port, trusted_message, &exist);
1336                 if (ret != MESSAGEPORT_ERROR_NONE) {
1337                         goto out;
1338                 } else if (!exist) {
1339                         ret = MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1340                         goto out;
1341                 }
1342         }
1343
1344         if (port_info->send_sock_fd > 0) {
1345                 ret = __message_port_send_async(port_info->send_sock_fd, message,
1346                                 (local_port) ? local_port : "", local_trusted, bi_dir);
1347         } else {
1348
1349                 bus_name = port_info->encoded_bus_name;
1350                 interface_name = bus_name;
1351
1352                 if (bundle_encode(message, &raw, &len) != BUNDLE_ERROR_NONE) {
1353                         ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
1354                         goto out;
1355                 }
1356
1357                 if (MAX_MESSAGE_SIZE < len) {
1358                         _LOGE("The size of message (%d) has exceeded the maximum limit.", len);
1359                         ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1360                         goto out;
1361                 }
1362
1363                 body = g_variant_new("(ssbbssbus)", __app_id, (local_port) ? local_port : "", local_trusted, bi_dir,
1364                                 remote_appid, remote_port, trusted_message, len, raw);
1365                 if (strcmp(remote_appid, __app_id) != 0) { /* self send */
1366
1367                         /*  if message-port fail to get socket pair, communicate using GDBus */
1368                         if (aul_request_message_port_socket_pair(sock_pair) != AUL_R_OK) {
1369                                 _LOGE("error create socket pair");
1370                         } else {
1371
1372                                 _LOGI("sock pair : %d, %d",
1373                                                 sock_pair[SOCK_PAIR_SENDER], sock_pair[SOCK_PAIR_RECEIVER]);
1374                                 fd_list = g_unix_fd_list_new();
1375                                 g_unix_fd_list_append(fd_list, sock_pair[SOCK_PAIR_RECEIVER], &err);
1376                                 if (err != NULL) {
1377                                         _LOGE("g_unix_fd_list_append [%s]", error->message);
1378                                         ret = MESSAGEPORT_ERROR_IO_ERROR;
1379                                         g_error_free(err);
1380                                         goto out;
1381                                 }
1382                                 port_info->send_sock_fd = sock_pair[SOCK_PAIR_SENDER];
1383                                 close(sock_pair[SOCK_PAIR_RECEIVER]);
1384                         }
1385                 }
1386
1387                 msg = g_dbus_message_new_method_call(bus_name, MESSAGEPORT_OBJECT_PATH, interface_name, "send_message");
1388                 if (!msg) {
1389                         _LOGE("Can't allocate new method call");
1390                         ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1391                         goto out;
1392                 }
1393
1394                 g_dbus_message_set_unix_fd_list(msg, fd_list);
1395                 g_dbus_message_set_body(msg, body);
1396                 g_dbus_message_set_flags(msg, G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED);
1397                 g_dbus_connection_send_message(__gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
1398                 if (err != NULL) {
1399                         _LOGE("No reply. error = %s", err->message);
1400                         g_error_free(err);
1401                         ret = MESSAGEPORT_ERROR_IO_ERROR;
1402                         goto out;
1403                 }
1404
1405
1406         }
1407
1408 out:
1409         if (msg)
1410                 g_object_unref(msg);
1411         if (raw)
1412                 bundle_free_encoded_rawdata(&raw);
1413         if (fd_list)
1414                 g_object_unref(fd_list);
1415
1416
1417         return ret;
1418 }
1419
1420 int __message_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,  bool trusted_message, bundle *message)
1421 {
1422         message_port_local_port_info_s *local_info;
1423         int ret = __get_local_port_info(id, &local_info);
1424         if (ret != MESSAGEPORT_ERROR_NONE)
1425                 return ret;
1426
1427         _LOGI("bidirectional_message %s", local_info->port_name);
1428         return __message_port_send_message(remote_app_id, remote_port,
1429                         local_info->port_name, trusted_message, local_info->is_trusted, true, message);
1430 }
1431
1432 int messageport_unregister_local_port(int local_port_id, bool trusted_port)
1433 {
1434
1435         GVariant *result;
1436         char *bus_name = NULL;
1437         GError *err = NULL;
1438         int ret = 0;
1439
1440         _LOGI("unregister : %d", local_port_id);
1441
1442         message_port_local_port_info_s *mi =
1443                 (message_port_local_port_info_s *)
1444                 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
1445         if (mi == NULL)
1446                 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1447
1448         if (mi->is_trusted != trusted_port)
1449                 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
1450
1451         bus_name = __get_encoded_name(__app_id, mi->port_name, mi->is_trusted);
1452         if (bus_name == NULL)
1453                 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1454
1455         g_dbus_connection_unregister_object(__gdbus_conn, local_port_id);
1456
1457         result = g_dbus_connection_call_sync(
1458                         __gdbus_conn,
1459                         DBUS_SERVICE_DBUS,
1460                         DBUS_PATH_DBUS,
1461                         DBUS_INTERFACE_DBUS,
1462                         "ReleaseName",
1463                         g_variant_new("(s)", bus_name),
1464                         G_VARIANT_TYPE("(u)"),
1465                         G_DBUS_CALL_FLAGS_NONE,
1466                         -1,
1467                         NULL,
1468                         &err);
1469
1470         if (bus_name)
1471                 free(bus_name);
1472
1473         if (err) {
1474                 _LOGE("RequestName fail : %s", err->message);
1475                 g_error_free(err);
1476                 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1477         }
1478         g_variant_get(result, "(u)", &ret);
1479
1480         if (result)
1481                 g_variant_unref(result);
1482
1483         if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
1484
1485                 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
1486                         _LOGE("Port Not exist");
1487                         return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1488                 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
1489                         _LOGE("Try to release not owned name. MESSAGEPORT_ERROR_INVALID_PARAMETER");
1490                         return MESSAGEPORT_ERROR_INVALID_PARAMETER;
1491                 }
1492         }
1493
1494
1495         g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
1496
1497         return MESSAGEPORT_ERROR_NONE;
1498 }
1499
1500 int messageport_register_local_port(const char *local_port, messageport_message_cb callback)
1501 {
1502         if (!_initialized) {
1503                 if (!__initialize())
1504                         return MESSAGEPORT_ERROR_IO_ERROR;
1505         }
1506
1507         return __register_message_port(local_port, false, callback);
1508 }
1509
1510 int messageport_register_trusted_local_port(const char *local_port, messageport_message_cb callback)
1511 {
1512         if (!_initialized) {
1513                 if (!__initialize())
1514                         return MESSAGEPORT_ERROR_IO_ERROR;
1515         }
1516
1517         return __register_message_port(local_port, true, callback);
1518
1519 }
1520
1521 int messageport_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1522 {
1523         if (!_initialized) {
1524                 if (!__initialize())
1525                         return MESSAGEPORT_ERROR_IO_ERROR;
1526         }
1527
1528         int ret = __check_remote_port(remote_app_id, remote_port, false, exist);
1529         if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1530                 *exist = false;
1531                 ret = MESSAGEPORT_ERROR_NONE;
1532         }
1533
1534         return ret;
1535 }
1536
1537 int messageport_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1538 {
1539         if (!_initialized) {
1540                 if (!__initialize())
1541                         return MESSAGEPORT_ERROR_IO_ERROR;
1542         }
1543
1544         int ret = __check_remote_port(remote_app_id, remote_port, true, exist);
1545         if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1546                 *exist = false;
1547                 ret = MESSAGEPORT_ERROR_NONE;
1548         }
1549
1550         return ret;
1551 }
1552
1553 int messageport_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
1554 {
1555         if (!_initialized) {
1556                 if (!__initialize())
1557                         return MESSAGEPORT_ERROR_IO_ERROR;
1558         }
1559
1560         return __message_port_send_message(remote_app_id, remote_port, NULL, false, false, false, message);
1561 }
1562
1563 int messageport_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
1564 {
1565         if (!_initialized) {
1566                 if (!__initialize())
1567                         return MESSAGEPORT_ERROR_IO_ERROR;
1568         }
1569
1570         return __message_port_send_message(remote_app_id, remote_port, NULL, true, false, false, message);
1571 }
1572
1573 int messageport_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,
1574                 bundle *message)
1575 {
1576         if (!_initialized) {
1577                 if (!__initialize())
1578                         return MESSAGEPORT_ERROR_IO_ERROR;
1579         }
1580
1581         return __message_send_bidirectional_message(id, remote_app_id, remote_port, false, message);
1582 }
1583
1584 int messageport_send_bidirectional_trusted_message(int id, const char *remote_app_id, const char *remote_port,
1585                 bundle *message)
1586 {
1587         if (!_initialized) {
1588                 if (!__initialize())
1589                         return MESSAGEPORT_ERROR_IO_ERROR;
1590         }
1591         return __message_send_bidirectional_message(id, remote_app_id, remote_port, true, message);
1592 }
1593
1594 int messageport_get_local_port_name(int id, char **name)
1595 {
1596         message_port_local_port_info_s *local_info;
1597         int ret = __get_local_port_info(id, &local_info);
1598
1599         if (ret != MESSAGEPORT_ERROR_NONE)
1600                 return ret;
1601
1602         *name = strdup(local_info->port_name);
1603
1604         if (*name == NULL)
1605                 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1606
1607         return MESSAGEPORT_ERROR_NONE;
1608 }
1609
1610 int messageport_check_trusted_local_port(int id, bool *trusted)
1611 {
1612         message_port_local_port_info_s *local_info;
1613         int ret = __get_local_port_info(id, &local_info);
1614
1615         if (ret != MESSAGEPORT_ERROR_NONE)
1616                 return ret;
1617
1618         *trusted = local_info->is_trusted;
1619
1620         return MESSAGEPORT_ERROR_NONE;;
1621 }
1622