3 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file message-port.cpp
20 * @brief This is the implementation file for the MessagePort.
23 #include <sys/socket.h>
29 #include <openssl/md5.h>
31 #include <bundle_internal.h>
32 #include <pkgmgr-info.h>
35 #include <gio/gunixfdlist.h>
37 #include "message-port.h"
38 #include "message-port-log.h"
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._"
45 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
46 #define DBUS_PATH_DBUS "/org/freedesktop/DBus"
47 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
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 */
53 #define MAX_RETRY_CNT 10
54 #define SOCK_PAIR_SENDER 0
55 #define SOCK_PAIR_RECEIVER 1
58 #define retvm_if(expr, val, fmt, arg...) do { \
61 _LOGE("(%s) -> %s() return", #expr, __func__); \
66 #define retv_if(expr, val) do { \
68 _LOGE("(%s) -> %s() return", #expr, __func__); \
73 #define FREE_AND_NULL(ptr) do { \
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;
90 enum __certificate_info_type {
93 CERTIFICATE_NOT_MATCH,
96 typedef struct message_port_pkt {
97 int remote_port_name_len;
98 char *remote_port_name;
103 } message_port_pkt_s;
105 typedef struct message_port_callback_info {
106 messageport_message_cb callback;
109 GIOChannel *gio_read;
111 } message_port_callback_info_s;
113 typedef struct message_port_local_port_info {
114 messageport_message_cb callback;
118 } message_port_local_port_info_s;
120 typedef struct message_port_remote_port_info {
123 int certificate_info;
125 } message_port_remote_app_info_s;
127 typedef struct port_list_info {
129 char *encoded_bus_name;
134 GIOChannel *gio_read;
138 static void __callback_info_free(gpointer data)
140 message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
141 GError *error = NULL;
142 if (callback_info == NULL)
145 if (callback_info->remote_app_id)
146 FREE_AND_NULL(callback_info->remote_app_id);
148 if (callback_info->gio_read != NULL) {
149 g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
151 _LOGE("g_io_channel_shutdown error : %s", error->message);
154 g_io_channel_unref(callback_info->gio_read);
155 callback_info->gio_read = NULL;
158 if (callback_info->g_src_id != 0) {
159 g_source_remove(callback_info->g_src_id);
160 callback_info->g_src_id = 0;
163 FREE_AND_NULL(callback_info);
166 static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
168 GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
171 if (callback_info_list == NULL)
174 find_list = g_list_find(callback_info_list, callback_info);
175 if (find_list == NULL)
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);
183 static void __hash_destroy_callback_info(gpointer data)
186 GList *callback_list = (GList *)data;
187 if (callback_list != NULL)
188 g_list_free_full(callback_list, __callback_info_free);
191 static char *__get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
194 int prefix_len = strlen(MESSAGEPORT_BUS_NAME_PREFIX);
196 char *postfix = is_trusted ? "1" : "0";
198 unsigned char c[MD5_DIGEST_LENGTH] = {0};
199 char *md5_interface = NULL;
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");
211 snprintf(bus_name, bus_name_len, "%s_%s", remote_app_id, port_name);
213 MD5_Init(&mdContext);
214 MD5_Update(&mdContext, bus_name, bus_name_len);
215 MD5_Final(c, &mdContext);
217 md5_interface = (char *)calloc(encoded_bus_name_len , sizeof(char));
218 if (md5_interface == NULL) {
222 _LOGE("md5_interface calloc failed!!");
226 snprintf(md5_interface, encoded_bus_name_len, "%s", MESSAGEPORT_BUS_NAME_PREFIX);
227 temp = md5_interface;
230 for (index = 0; index < MD5_DIGEST_LENGTH; index++) {
231 snprintf(temp, 3, "%02x", c[index]);
235 if (postfix && postfix_len > 0)
236 snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
240 _LOGI("encoded_bus_name : %s ", md5_interface);
242 return md5_interface;
245 static int __remote_port_compare_cb(gconstpointer a, gconstpointer b)
247 port_list_info_s *key1 = (port_list_info_s *)a;
248 port_list_info_s *key2 = (port_list_info_s *)b;
250 if (key1->is_trusted == key2->is_trusted)
251 return strcmp(key1->port_name, key2->port_name);
257 static bool __is_preloaded(const char *local_appid, const char *remote_appid)
259 _LOGI("IsPreloaded");
261 bool preload_local = false;
262 bool preload_remote = false;
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);
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);
277 pkgmgrinfo_appinfo_destroy_appinfo(handle);
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);
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);
292 if (preload_local && preload_remote) {
293 pkgmgrinfo_appinfo_destroy_appinfo(handle);
296 pkgmgrinfo_appinfo_destroy_appinfo(handle);
300 static int __check_certificate(const char *local_appid, const char *remote_appid)
302 _LOGI("CheckCertificate");
304 pkgmgrinfo_cert_compare_result_type_e res;
305 int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
307 _LOGE(":CheckCertificate() Failed");
308 return MESSAGEPORT_ERROR_IO_ERROR;
310 if (res != PMINFO_CERT_COMPARE_MATCH) {
311 _LOGE("CheckCertificate() Failed : MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH");
312 return MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
315 return MESSAGEPORT_ERROR_NONE;
318 static void on_name_appeared(GDBusConnection *connection,
320 const gchar *name_owner,
323 _LOGI("name appeared : %s %s", __app_id, name);
326 static void on_name_vanished(GDBusConnection *connection,
330 _LOGI("name vanished : %s", name);
331 port_list_info_s *pli = (port_list_info_s *)user_data;
333 LOGE("NULL port info");
337 _LOGI("watcher_id :%d", pli->watcher_id);
338 if (pli->watcher_id > 0)
339 g_bus_unwatch_name(pli->watcher_id);
341 _LOGE("Invalid watcher_id %d", pli->watcher_id);
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;
352 static int __get_local_port_info(int id, message_port_local_port_info_s **info)
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));
357 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
360 return MESSAGEPORT_ERROR_NONE;
363 static port_list_info_s *__set_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
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));
369 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
372 port_info->port_name = strdup(remote_port);
373 if (!port_info->port_name) {
374 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
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;
383 port_info->send_sock_fd = 0;
385 if (ret_val != MESSAGEPORT_ERROR_NONE) {
387 FREE_AND_NULL(port_info->port_name);
388 FREE_AND_NULL(port_info->encoded_bus_name);
396 static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
398 message_port_remote_app_info_s *remote_app_info = NULL;
399 int ret_val = MESSAGEPORT_ERROR_NONE;
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;
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;;
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);
421 return remote_app_info;
424 static void __clear_disconnect_socket(port_list_info_s *port_info)
426 GError *error = NULL;
428 if (port_info == NULL)
430 _LOGI("__clear_disconnect_socket : fd [%d]", port_info->send_sock_fd);
432 if (port_info->gio_read != NULL) {
433 g_io_channel_shutdown(port_info->gio_read, TRUE, &error);
435 _LOGE("g_io_channel_shutdown error : %s", error->message);
438 g_io_channel_unref(port_info->gio_read);
439 port_info->gio_read = NULL;
442 if (port_info->g_src_id != 0) {
443 g_source_remove(port_info->g_src_id);
444 port_info->g_src_id = 0;
446 port_info->send_sock_fd = 0;
449 static gboolean __socket_disconnect_handler(GIOChannel *gio,
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);
460 static void __watch_remote_port_info(port_list_info_s *port_info)
462 if (port_info == NULL)
465 if (port_info->watcher_id < 1) {
466 port_info->watcher_id = g_bus_watch_name_on_connection(
468 port_info->encoded_bus_name,
469 G_BUS_NAME_WATCHER_FLAGS_NONE,
475 LOGI("Already watched port info");
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)
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;
488 remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_app_info, remote_app_id);
490 if (remote_app_info == NULL) {
491 remote_app_info = __set_remote_app_info(remote_app_id, remote_port, is_trusted);
493 if (remote_app_info == NULL) {
494 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
497 g_hash_table_insert(__remote_app_info, remote_app_info->remote_app_id, remote_app_info);
499 *mri = remote_app_info;
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);
510 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
513 remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
516 *pli = (port_list_info_s *)cb_list->data;
523 static bool __is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
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;
532 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
533 *local_id = mi->local_id;
542 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
544 GDBusMessage *msg = NULL;
545 GDBusMessage *reply = NULL;
550 msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
551 "org.freedesktop.DBus", "GetConnectionUnixProcessID");
553 _LOGE("Can't allocate new method call");
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);
563 _LOGE("Failed to get pid [%s]", err->message);
569 body = g_dbus_message_get_body(reply);
570 g_variant_get(body, "(u)", &pid);
576 g_object_unref(reply);
581 static int __write_socket(int fd,
584 unsigned int *bytes_write)
586 unsigned int left = nbytes;
591 while (left && (retry_cnt < MAX_RETRY_CNT)) {
592 nb = write(fd, buffer, left);
594 if (errno == EINTR) {
595 LOGE("__write_socket: EINTR error continue ...");
599 LOGE("__write_socket: ...error fd %d: errno %d\n", fd, errno);
600 return MESSAGEPORT_ERROR_IO_ERROR;
608 return MESSAGEPORT_ERROR_NONE;
611 static int __write_string_to_socket(int fd, const char *buffer, int string_len)
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;
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;
625 return MESSAGEPORT_ERROR_NONE;
628 static int __read_socket(int fd,
631 unsigned int *bytes_read)
633 unsigned int left = nbytes;
636 const struct timespec TRY_SLEEP_TIME = { 0, 500 * 1000 * 1000 };
639 while (left && (retry_cnt < MAX_RETRY_CNT)) {
640 nb = read(fd, buffer, left);
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);
649 nanosleep(&TRY_SLEEP_TIME, 0);
652 LOGE("__read_socket: ...error fd %d: errno %d\n", fd, errno);
653 return MESSAGEPORT_ERROR_IO_ERROR;
661 return MESSAGEPORT_ERROR_NONE;
664 static int __read_string_from_socket(int fd, char **buffer, int *string_len)
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;
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;
677 if (__read_socket(fd, *buffer, *string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
678 LOGE("read socket fail");
679 return MESSAGEPORT_ERROR_IO_ERROR;
682 LOGE("Invalid string len %d", *string_len);
683 return MESSAGEPORT_ERROR_IO_ERROR;
685 return MESSAGEPORT_ERROR_NONE;
688 message_port_pkt_s *__message_port_recv_raw(int fd)
690 message_port_pkt_s *pkt = NULL;
693 pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
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);
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);
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);
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);
730 static gboolean __socket_request_handler(GIOChannel *gio,
735 message_port_callback_info_s *mi;
736 message_port_pkt_s *pkt;
738 GError *error = NULL;
740 mi = (message_port_callback_info_s *)data;
743 g_io_channel_shutdown(gio, TRUE, &error);
745 _LOGE("g_io_channel_shutdown error : %s", error->message);
748 g_io_channel_unref(gio);
752 if (cond == G_IO_HUP) {
754 _LOGI("socket G_IO_HUP");
755 __callback_info_free_by_info(mi);
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);
766 if ((pkt = __message_port_recv_raw(fd)) == NULL) {
767 _LOGE("recv error on SOCKET");
768 __callback_info_free_by_info(mi);
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);
776 mi->callback(mi->local_id, mi->remote_app_id, NULL, pkt->is_trusted, kb, NULL);
780 if (pkt->remote_port_name)
781 free(pkt->remote_port_name);
791 static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation)
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;
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;
812 GUnixFDList *fd_list;
814 int *returned_fds = NULL;
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);
821 _LOGE("Invalid argument : remote_port is NULL");
825 _LOGE("Invalid argument : remote_appid is NULL");
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);
833 _LOGE("Invalid argument : local_appid");
837 _LOGE("Invalid argument : local_port");
840 if (strcmp(remote_appid, __app_id) != 0) {
841 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
844 if (strcmp(remote_port, mi->port_name) != 0) {
845 _LOGE("Invalid argument : remote_port (%s)", remote_port);
849 _LOGE("Invalid argument : data_len");
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");
859 _LOGE("The application (%s) is not signed with the same certificate",
867 callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
868 if (callback_info == NULL)
871 callback_info->local_id = mi->local_id;
872 callback_info->remote_app_id = strdup(local_appid);
873 callback_info->callback = mi->callback;
875 msg = g_dbus_method_invocation_get_message(invocation);
876 fd_list = g_dbus_message_get_unix_fd_list(msg);
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);
886 fd = returned_fds[0];
888 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
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);
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);
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);
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);
923 callback_info_list = g_list_append(callback_info_list, callback_info);
928 data = bundle_decode(raw, len);
930 _LOGE("Invalid argument : message");
934 LOGI("call calback %s", local_appid);
936 mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
938 mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
947 static int __check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
949 _LOGI("Check a remote port : [%s:%s]", remote_app_id, remote_port);
951 GVariant *result = 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;
961 _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
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)
968 if (strcmp(remote_app_id, __app_id) == 0) {
970 _LOGI("__is_local_port_registed ");
971 if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
976 _LOGI("__is_local_port_registed : %d ", *exist);
977 return MESSAGEPORT_ERROR_NONE;
980 port_info->exist = false;
981 bus_name = port_info->encoded_bus_name;
983 result = g_dbus_connection_call_sync(
989 g_variant_new("(s)", bus_name),
990 G_VARIANT_TYPE("(b)"),
991 G_DBUS_CALL_FLAGS_NONE,
996 if (err || (result == NULL)) {
998 _LOGE("No reply. error = %s", err->message);
1001 ret_val = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
1003 g_variant_get(result, "(b)", &name_exist);
1006 LOGE("Name not exist %s", bus_name);
1008 ret_val = MESSAGEPORT_ERROR_NONE;
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;
1019 remote_app_info->certificate_info = CERTIFICATE_MATCH;
1022 port_info->exist = true;
1024 ret_val = MESSAGEPORT_ERROR_NONE;
1025 __watch_remote_port_info(port_info);
1030 g_variant_unref(result);
1035 static void __on_sender_name_appeared(GDBusConnection *connection,
1037 const gchar *name_owner,
1040 _LOGI("sender name appeared : %s", name);
1043 static void __on_sender_name_vanished(GDBusConnection *connection,
1047 gboolean remove_result = FALSE;
1048 int *watcher_id = (int *)user_data;
1049 remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
1051 _LOGE("Fail to remove sender appid from hash : %s", name);
1054 if (*watcher_id > 0)
1055 g_bus_unwatch_name(*watcher_id);
1057 LOGE("Invalid watcher_id %d", *watcher_id);
1060 LOGE("watcher_id is NULL");
1064 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
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));
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);
1079 g_variant_get_child(parameters, 0, "&s", &local_appid);
1080 if (local_appid == NULL) {
1081 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
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(
1092 G_BUS_NAME_WATCHER_FLAGS_NONE,
1093 __on_sender_name_appeared,
1094 __on_sender_name_vanished,
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,
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))
1116 if (g_strcmp0(method_name, "send_message") == 0)
1117 send_message(parameters, invocation);
1119 g_dbus_method_invocation_return_value(invocation, NULL);
1122 static const GDBusInterfaceVTable interface_vtable = {
1123 __dbus_method_call_handler,
1128 static int __dbus_init(void)
1131 GError *error = NULL;
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);
1146 g_object_unref(__gdbus_conn);
1152 int __register_dbus_interface(const char *port_name, bool is_trusted)
1155 GDBusNodeInfo *introspection_data = NULL;
1156 int registration_id = 0;
1158 static gchar introspection_prefix[] =
1160 " <interface name='";
1162 static gchar introspection_postfix[] =
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'/>"
1178 char *introspection_xml = NULL;
1179 int introspection_xml_len = 0;
1183 GError *error = NULL;
1184 char *bus_name = NULL;
1185 char *interface_name = NULL;
1186 GVariant *result = NULL;
1188 bus_name = __get_encoded_name(__app_id, port_name, is_trusted);
1190 _LOGE("Fail to get bus name");
1193 interface_name = bus_name;
1195 introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
1196 strlen(introspection_postfix) + 1;
1198 introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
1199 if (!introspection_xml) {
1200 _LOGE("out of memory");
1205 result = g_dbus_connection_call_sync(
1209 DBUS_INTERFACE_DBUS,
1211 g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
1212 G_VARIANT_TYPE("(u)"),
1213 G_DBUS_CALL_FLAGS_NONE,
1218 _LOGE("RequestName fail : %s", error->message);
1219 g_error_free(error);
1222 if (result == NULL) {
1223 _LOGE("fail to get name NULL");
1226 g_variant_get(result, "(u)", &owner_id);
1227 if (owner_id == 0) {
1228 _LOGE("Acquiring the own name is failed");
1232 _LOGI("Acquiring the own name : %d", owner_id);
1234 snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
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.");
1242 registration_id = g_dbus_connection_register_object(__gdbus_conn,
1243 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
1244 &interface_vtable, NULL, NULL, NULL);
1246 _LOGI("registration_id %d", registration_id);
1248 if (registration_id == 0) {
1249 _LOGE("Failed to g_dbus_connection_register_object");
1254 if (introspection_data)
1255 g_dbus_node_info_unref(introspection_data);
1256 if (introspection_xml)
1257 free(introspection_xml);
1261 g_variant_unref(result);
1264 return registration_id;
1267 /* LCOV_EXCL_START */
1268 void __list_free_port_list(gpointer data)
1270 port_list_info_s *n = (port_list_info_s *)data;
1272 FREE_AND_NULL(n->encoded_bus_name);
1273 FREE_AND_NULL(n->port_name);
1276 /* LCOV_EXCL_STOP */
1278 /* LCOV_EXCL_START */
1279 static void __hash_destory_local_value(gpointer data)
1281 message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
1284 free(mli->port_name);
1288 /* LCOV_EXCL_STOP */
1290 /* LCOV_EXCL_START */
1291 static void __hash_destory_remote_value(gpointer data)
1293 message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
1295 FREE_AND_NULL(mri->sender_id);
1296 FREE_AND_NULL(mri->remote_app_id);
1298 g_list_free_full(mri->port_list, __list_free_port_list);
1303 /* LCOV_EXCL_STOP */
1305 static bool __initialize(void)
1308 #if !GLIB_CHECK_VERSION(2, 35, 0)
1314 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
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);
1320 __app_id = strdup(buffer);
1321 retvm_if(!__app_id, false, "Malloc failed");
1322 _LOGI("init : %s", __app_id);
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");
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");
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");
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");
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");
1351 _initialized = true;
1357 static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, messageport_message_cb callback)
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");
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);
1370 mi->local_id = local_id;
1372 g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
1376 static int __register_message_port(const char *local_port, bool is_trusted, messageport_message_cb callback)
1378 _SECURE_LOGI("Register a message port : [%s:%s]", __app_id, local_port);
1382 /* Check the message port is already registed */
1383 if (__is_local_port_registed(local_port, is_trusted, &local_id, NULL))
1386 local_id = __register_dbus_interface(local_port, is_trusted);
1388 _LOGE("register_dbus_interface fail !!");
1389 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1392 if (!__message_port_register_port(local_id, local_port, is_trusted, callback))
1393 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1398 int __message_port_send_async(int sockfd, bundle *kb, const char *local_port,
1399 bool local_trusted, bool is_bidirection)
1403 int local_port_len = 0;
1405 bundle_raw *kb_data = NULL;
1407 bundle_encode(kb, &kb_data, &data_len);
1408 if (kb_data == NULL) {
1409 _LOGE("bundle encode fail");
1410 ret = MESSAGEPORT_ERROR_IO_ERROR;
1414 if (data_len > MAX_MESSAGE_SIZE) {
1415 _LOGE("bigger than max size\n");
1416 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1420 if (local_port != NULL)
1421 local_port_len = strlen(local_port) + 1;
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;
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;
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;
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;
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)
1457 int ret = MESSAGEPORT_ERROR_NONE;
1458 GUnixFDList *fd_list = NULL;
1461 bundle_raw *raw = NULL;
1462 char *bus_name = NULL;
1463 char *interface_name = NULL;
1465 message_port_remote_app_info_s *remote_app_info = NULL;
1466 port_list_info_s *port_info = NULL;
1467 GDBusMessage *msg = NULL;
1469 GVariant *body = NULL;
1470 int sock_pair[2] = {0,};
1473 ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
1474 if (ret != MESSAGEPORT_ERROR_NONE)
1477 if (port_info->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) {
1483 } else if (!exist) {
1484 ret = MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
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);
1494 bus_name = port_info->encoded_bus_name;
1495 interface_name = bus_name;
1497 if (bundle_encode(message, &raw, &len) != BUNDLE_ERROR_NONE) {
1498 ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
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;
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 */
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");
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);
1522 _LOGE("g_unix_fd_list_append [%s]", err->message);
1523 ret = MESSAGEPORT_ERROR_IO_ERROR;
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;
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;
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;
1550 msg = g_dbus_message_new_method_call(bus_name, MESSAGEPORT_OBJECT_PATH, interface_name, "send_message");
1552 _LOGE("Can't allocate new method call");
1553 ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
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);
1561 _LOGE("No reply. error = %s", err->message);
1563 ret = MESSAGEPORT_ERROR_IO_ERROR;
1566 __watch_remote_port_info(port_info);
1572 g_object_unref(msg);
1574 bundle_free_encoded_rawdata(&raw);
1576 g_object_unref(fd_list);
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]);
1589 int __message_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port, bool trusted_message, bundle *message)
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)
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);
1601 int messageport_unregister_local_port(int local_port_id, bool trusted_port)
1605 char *bus_name = NULL;
1609 _LOGI("unregister : %d", local_port_id);
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));
1615 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1617 if (mi->is_trusted != trusted_port)
1618 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
1620 g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
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;
1626 g_dbus_connection_unregister_object(__gdbus_conn, local_port_id);
1628 result = g_dbus_connection_call_sync(
1632 DBUS_INTERFACE_DBUS,
1634 g_variant_new("(s)", bus_name),
1635 G_VARIANT_TYPE("(u)"),
1636 G_DBUS_CALL_FLAGS_NONE,
1645 _LOGE("RequestName fail : %s", err->message);
1647 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1649 g_variant_get(result, "(u)", &ret);
1652 g_variant_unref(result);
1654 if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
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;
1666 g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
1668 return MESSAGEPORT_ERROR_NONE;
1671 int messageport_register_local_port(const char *local_port, messageport_message_cb callback)
1673 if (!_initialized) {
1674 if (!__initialize())
1675 return MESSAGEPORT_ERROR_IO_ERROR;
1678 return __register_message_port(local_port, false, callback);
1681 int messageport_register_trusted_local_port(const char *local_port, messageport_message_cb callback)
1683 if (!_initialized) {
1684 if (!__initialize())
1685 return MESSAGEPORT_ERROR_IO_ERROR;
1688 return __register_message_port(local_port, true, callback);
1692 int messageport_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1694 if (!_initialized) {
1695 if (!__initialize())
1696 return MESSAGEPORT_ERROR_IO_ERROR;
1699 int ret = __check_remote_port(remote_app_id, remote_port, false, exist);
1700 if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1702 ret = MESSAGEPORT_ERROR_NONE;
1708 int messageport_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1710 if (!_initialized) {
1711 if (!__initialize())
1712 return MESSAGEPORT_ERROR_IO_ERROR;
1715 int ret = __check_remote_port(remote_app_id, remote_port, true, exist);
1716 if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1718 ret = MESSAGEPORT_ERROR_NONE;
1724 int messageport_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
1726 if (!_initialized) {
1727 if (!__initialize())
1728 return MESSAGEPORT_ERROR_IO_ERROR;
1731 return __message_port_send_message(remote_app_id, remote_port, NULL, false, false, false, message);
1734 int messageport_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
1736 if (!_initialized) {
1737 if (!__initialize())
1738 return MESSAGEPORT_ERROR_IO_ERROR;
1741 return __message_port_send_message(remote_app_id, remote_port, NULL, true, false, false, message);
1744 int messageport_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,
1747 if (!_initialized) {
1748 if (!__initialize())
1749 return MESSAGEPORT_ERROR_IO_ERROR;
1752 return __message_send_bidirectional_message(id, remote_app_id, remote_port, false, message);
1755 int messageport_send_bidirectional_trusted_message(int id, const char *remote_app_id, const char *remote_port,
1758 if (!_initialized) {
1759 if (!__initialize())
1760 return MESSAGEPORT_ERROR_IO_ERROR;
1762 return __message_send_bidirectional_message(id, remote_app_id, remote_port, true, message);