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