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