2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <bundle_internal.h>
23 #include <sys/socket.h>
30 #include <gio/gunixfdlist.h>
31 #include <glib-unix.h>
33 #include "message_port_log.h"
34 #include "message_port.h"
35 #include "message_port_remote.h"
36 #include "message_port_common.h"
38 #define MAX_RETRY_CNT 10
39 #define SOCK_PAIR_SENDER 0
40 #define SOCK_PAIR_RECEIVER 1
42 static bool _initialized = false;
43 static GHashTable *__remote_app_info;
44 static GHashTable *__registered_callback_info_hash;
45 static const int MAX_MESSAGE_SIZE = 16 * 1024;
47 enum __certificate_info_type {
50 CERTIFICATE_NOT_MATCH,
53 typedef struct message_port_remote_port_info {
57 } message_port_remote_app_info_s;
59 typedef struct port_list_info {
60 message_port_remote_app_info_s *remote_app_info;
62 char *encoded_bus_name;
68 GList *delayed_message_list;
69 unsigned int delayed_message_size;
73 typedef struct port_key_info {
79 typedef struct delay_port_info {
80 port_key_info_s *key_info;
81 port_list_info_s *port_info;
84 typedef struct registered_callback_info {
90 message_port_registration_event_cb registered_cb;
91 message_port_registration_event_cb unregistered_cb;
92 } registered_callback_info_s;
94 enum transmission_sequence {
105 typedef struct delay_message {
107 unsigned int sent_bytes;
110 char *local_port_name;
115 } delay_message_info_s;
117 extern pthread_mutex_t mutex;
119 /* LCOV_EXCL_START */
120 static void __free_delay_message_info(delay_message_info_s *message)
122 if (message != NULL) {
123 FREE_AND_NULL(message->local_port_name);
124 FREE_AND_NULL(message->data);
125 FREE_AND_NULL(message);
130 /* LCOV_EXCL_START */
131 static void __free_list_delay_message_info(gpointer data)
133 delay_message_info_s *message = (delay_message_info_s *)data;
136 __free_delay_message_info(message);
140 /* LCOV_EXCL_START */
141 static void __clear_disconnect_socket(port_list_info_s *port_info)
143 GError *error = NULL;
145 if (port_info == NULL)
148 if (port_info->gio_read != NULL) {
149 g_io_channel_shutdown(port_info->gio_read, TRUE, &error);
151 _LOGE("g_io_channel_shutdown error : %s", error->message);
154 g_io_channel_unref(port_info->gio_read);
155 port_info->gio_read = NULL;
158 if (port_info->g_src_id != 0) {
159 g_source_remove(port_info->g_src_id);
160 port_info->g_src_id = 0;
163 if (port_info->delay_src_id != 0) {
164 g_source_remove(port_info->delay_src_id);
165 port_info->delay_src_id = 0;
168 if (port_info->delayed_message_list != NULL) {
169 g_list_free_full(port_info->delayed_message_list, __free_list_delay_message_info);
171 port_info->delayed_message_list = NULL;
174 port_info->delayed_message_size = 0;
175 port_info->send_sock_fd = 0;
179 /* LCOV_EXCL_START */
180 static void __free_port_info(gpointer data)
182 port_list_info_s *port_info = (port_list_info_s *)data;
183 message_port_remote_app_info_s *remote_app_info;
185 if (port_info == NULL)
188 remote_app_info = port_info->remote_app_info;
190 _LOGI("__free_port_info : remote_app_id : %s port_name : %s",
191 remote_app_info->remote_app_id,
192 port_info->port_name);
194 remote_app_info->port_list = g_list_remove(remote_app_info->port_list,
197 __clear_disconnect_socket(port_info);
199 if (port_info->encoded_bus_name)
200 free(port_info->encoded_bus_name);
201 if (port_info->port_name)
202 free(port_info->port_name);
206 if (g_list_length(remote_app_info->port_list) == 0) {
207 g_hash_table_remove(__remote_app_info,
208 remote_app_info->remote_app_id);
213 /* LCOV_EXCL_START */
214 static void __hash_destory_remote_value(gpointer data)
216 message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
218 FREE_AND_NULL(mri->remote_app_id);
220 g_list_free_full(mri->port_list, __free_port_info);
227 static void __registered_callback_info_free(gpointer data)
229 registered_callback_info_s *callback_info = (registered_callback_info_s *)data;
230 if (callback_info == NULL)
233 if (callback_info->remote_app_id)
234 free(callback_info->remote_app_id);
236 if (callback_info->remote_port)
237 free(callback_info->remote_port);
242 static bool __initialize(void)
244 if (!initialized_common) {
245 if (!initialize_common())
249 if (__remote_app_info == NULL) {
250 __remote_app_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value);
251 retvm_if(!__remote_app_info, false, "fail to create __remote_app_info");
254 if (__registered_callback_info_hash == NULL) {
255 __registered_callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __registered_callback_info_free);
256 retvm_if(!__registered_callback_info_hash, false, "fail to create __registered_callback_info_hash");
264 static int __remote_port_compare_cb(gconstpointer a, gconstpointer b)
266 port_list_info_s *key1 = (port_list_info_s *)a;
267 port_list_info_s *key2 = (port_list_info_s *)b;
269 if (key1->is_trusted == key2->is_trusted)
270 return strcmp(key1->port_name, key2->port_name);
275 static int __key_compare_cb(gconstpointer a, gconstpointer b)
277 port_list_info_s *key1 = (port_list_info_s *)a;
278 port_key_info_s *key2 = (port_key_info_s *)b;
280 if (key1->is_trusted == key2->is_trusted)
281 return strcmp(key1->port_name, key2->port_name);
286 static port_list_info_s *__set_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
288 int ret_val = MESSAGE_PORT_ERROR_NONE;
289 port_list_info_s *port_info = (port_list_info_s *)calloc(1, sizeof(port_list_info_s));
292 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
295 port_info->port_name = strdup(remote_port);
296 if (!port_info->port_name) {
297 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
300 port_info->is_trusted = is_trusted;
301 port_info->encoded_bus_name = get_encoded_name(remote_app_id, remote_port, is_trusted);
302 if (port_info->encoded_bus_name == NULL) {
303 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
306 port_info->send_sock_fd = 0;
308 if (ret_val != MESSAGE_PORT_ERROR_NONE) {
310 FREE_AND_NULL(port_info->port_name);
311 FREE_AND_NULL(port_info->encoded_bus_name);
319 static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
321 message_port_remote_app_info_s *remote_app_info = NULL;
322 int ret_val = MESSAGE_PORT_ERROR_NONE;
324 remote_app_info = (message_port_remote_app_info_s *)calloc(1, sizeof(message_port_remote_app_info_s));
325 if (!remote_app_info) {
326 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
330 remote_app_info->remote_app_id = strdup(remote_app_id);
331 if (remote_app_info->remote_app_id == NULL) {
332 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
337 if (ret_val != MESSAGE_PORT_ERROR_NONE) {
338 if (remote_app_info) {
339 FREE_AND_NULL(remote_app_info->remote_app_id);
340 FREE_AND_NULL(remote_app_info);
344 return remote_app_info;
348 static void __free_port_info_by_key(port_key_info_s *key_info)
350 port_list_info_s *found_port_info;
351 message_port_remote_app_info_s *found_remote_port_info;
354 found_remote_port_info =
355 (message_port_remote_app_info_s *)g_hash_table_lookup(
356 __remote_app_info, key_info->remote_app_id);
357 if (found_remote_port_info == NULL)
360 cb_list = g_list_find_custom(found_remote_port_info->port_list, key_info,
361 (GCompareFunc)__key_compare_cb);
365 found_port_info = (port_list_info_s *)cb_list->data;
366 __free_port_info(found_port_info);
371 _LOGE("Not found port_info");
374 static void __free_key_info(port_key_info_s *key_info)
376 FREE_AND_NULL(key_info->port_name);
377 FREE_AND_NULL(key_info->remote_app_id);
378 FREE_AND_NULL(key_info);
381 static gboolean __socket_disconnect_handler(GIOChannel *gio,
385 _LOGI("__socket_disconnect_handler %d", cond);
386 pthread_mutex_lock(&mutex);
387 __free_port_info_by_key((port_key_info_s *)data);
388 pthread_mutex_unlock(&mutex);
393 static void __socket_destroy_handler(gpointer data)
395 _LOGI("__socket_destroy_handler");
397 port_key_info_s *key_info = (port_key_info_s *)data;
398 __free_key_info(key_info);
402 static void __delay_socket_destroy_handler(gpointer data)
404 _LOGI("__delay_socket_destroy_handler");
405 delay_port_info *delay_info = (delay_port_info *)data;
407 FREE_AND_NULL(delay_info->key_info->port_name);
408 FREE_AND_NULL(delay_info->key_info->remote_app_id);
409 FREE_AND_NULL(delay_info->key_info);
413 static int __create_port_key_info(
414 port_list_info_s *port_info,
415 port_key_info_s **key_info)
417 int ret_val = MESSAGE_PORT_ERROR_NONE;
418 port_key_info_s *_key_info = (port_key_info_s *)
419 calloc(1, sizeof(port_key_info_s));
420 if (_key_info == NULL) {
421 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
422 _LOGE("out of memory");
426 _key_info->port_name = strdup(port_info->port_name);
427 if (_key_info->port_name == NULL) {
428 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
429 _LOGE("out of memory");
433 _key_info->is_trusted = port_info->is_trusted;
435 _key_info->remote_app_id = strdup(port_info->remote_app_info->remote_app_id);
436 if (_key_info->remote_app_id == NULL) {
437 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
438 _LOGE("out of memory");
443 if (ret_val == MESSAGE_PORT_ERROR_NONE) {
444 *key_info = _key_info;
447 FREE_AND_NULL(_key_info->port_name);
448 FREE_AND_NULL(_key_info->remote_app_id);
455 static int __get_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted,
456 message_port_remote_app_info_s **mri, port_list_info_s **pli)
458 message_port_remote_app_info_s *remote_app_info = NULL;
459 port_list_info_s port_info;
460 GList *cb_list = NULL;
461 int ret_val = MESSAGE_PORT_ERROR_NONE;
463 remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_app_info, remote_app_id);
464 if (remote_app_info == NULL) {
465 remote_app_info = __set_remote_app_info(remote_app_id, remote_port, is_trusted);
467 if (remote_app_info == NULL) {
468 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
471 g_hash_table_insert(__remote_app_info, remote_app_info->remote_app_id, remote_app_info);
473 *mri = remote_app_info;
475 port_info.port_name = strdup(remote_port);
476 if (port_info.port_name == NULL) {
477 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
480 port_info.is_trusted = is_trusted;
481 cb_list = g_list_find_custom(remote_app_info->port_list, &port_info,
482 (GCompareFunc)__remote_port_compare_cb);
483 if (port_info.port_name)
484 free(port_info.port_name);
485 if (cb_list == NULL) {
486 port_list_info_s *tmp = __set_remote_port_info(remote_app_id, remote_port, is_trusted);
488 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
491 remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
492 tmp->remote_app_info = remote_app_info;
495 *pli = (port_list_info_s *)cb_list->data;
502 int check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
504 _LOGD("Check a remote port : [%s:%s]", remote_app_id, remote_port);
506 GVariant *result = NULL;
508 int ret_val = MESSAGE_PORT_ERROR_NONE;
509 char *bus_name = NULL;
510 message_port_remote_app_info_s *remote_app_info = NULL;
511 port_list_info_s *port_info = NULL;
512 int local_reg_id = 0;
513 message_port_local_port_info_s *mi = NULL;
514 gboolean name_exist = false;
518 return MESSAGE_PORT_ERROR_IO_ERROR;
521 _LOGD("remote_app_id, app_id :[%s : %s] ", remote_app_id, app_id);
523 ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
524 if (ret_val != MESSAGE_PORT_ERROR_NONE)
528 if (strcmp(remote_app_id, app_id) == 0) {
530 _LOGD("__is_local_port_registed ");
531 if (!is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
536 _LOGD("__is_local_port_registed : %d ", *exist);
537 return MESSAGE_PORT_ERROR_NONE;
540 port_info->exist = false;
541 bus_name = port_info->encoded_bus_name;
543 result = g_dbus_connection_call_sync(
549 g_variant_new("(s)", bus_name),
550 G_VARIANT_TYPE("(b)"),
551 G_DBUS_CALL_FLAGS_NONE,
556 if (err || (result == NULL)) {
558 _LOGE("No reply. error = %s", err->message);
561 ret_val = MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
563 g_variant_get(result, "(b)", &name_exist);
566 _LOGI("Name not exist %s", bus_name);
568 ret_val = MESSAGE_PORT_ERROR_NONE;
572 if (remote_app_info->certificate_info != CERTIFICATE_MATCH) {
573 if (!is_preloaded(app_id, remote_app_id)) {
574 if (check_certificate(app_id, remote_app_id) != MESSAGE_PORT_ERROR_NONE) {
575 ret_val = MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH;
579 remote_app_info->certificate_info = CERTIFICATE_MATCH;
582 port_info->exist = true;
584 ret_val = MESSAGE_PORT_ERROR_NONE;
589 g_variant_unref(result);
591 if (ret_val != MESSAGE_PORT_ERROR_NONE || !name_exist)
592 __free_port_info((gpointer)port_info);
597 /* LCOV_EXCL_START */
598 static int __send_delayed_message(int sockfd, delay_message_info_s *message)
601 int sequence = message->sequence - 1;
602 int ret = MESSAGE_PORT_ERROR_NONE;
603 bool is_startline = true;
606 _LOGI("send_delayed_message : sockfd (%d) sequence(%d) sent byte(%d)",
607 sockfd, message->sequence, message->sent_bytes);
609 switch (message->sequence) {
612 is_startline = false;
614 case SEQUENCE_PORT_LEN:
616 offset = message->sent_bytes;
618 ret = write_socket(sockfd, ((char *)&message->local_port_len) + offset,
619 sizeof(message->local_port_len) - offset, &nb, &sequence);
620 if (ret != MESSAGE_PORT_ERROR_NONE) {
621 _LOGE("write local_port_len fail");
625 is_startline = false;
627 case SEQUENCE_PORT_NAME:
629 offset = message->sent_bytes;
631 if (message->local_port_len > 0)
632 ret = write_socket(sockfd, message->local_port_name + offset,
633 message->local_port_len - offset , &nb, &sequence);
637 if (ret != MESSAGE_PORT_ERROR_NONE) {
638 _LOGE("write local_port fail");
642 is_startline = false;
644 case SEQUENCE_BIDIRECTION:
646 offset = message->sent_bytes;
648 ret = write_socket(sockfd, ((char *)&message->is_bidirection) + offset,
649 sizeof(message->is_bidirection) - offset, &nb, &sequence);
650 if (ret != MESSAGE_PORT_ERROR_NONE) {
651 _LOGE("write is_bidirection fail");
655 is_startline = false;
657 case SEQUENCE_TRUSTED:
659 offset = message->sent_bytes;
661 ret = write_socket(sockfd, ((char *)&message->local_trusted) + offset,
662 sizeof(message->local_trusted) - offset, &nb, &sequence);
663 if (ret != MESSAGE_PORT_ERROR_NONE) {
664 _LOGE("write local_trusted fail");
668 is_startline = false;
670 case SEQUENCE_DTAT_LEN:
672 offset = message->sent_bytes;
674 ret = write_socket(sockfd, ((char *)&message->data_len) + offset,
675 sizeof(message->data_len) - offset, &nb, &sequence);
676 if (ret != MESSAGE_PORT_ERROR_NONE) {
677 _LOGE("write data_len fail");
681 is_startline = false;
685 offset = message->sent_bytes;
687 ret = write_socket(sockfd, (char *)message->data + offset,
688 message->data_len - offset, &nb, &sequence);
690 if (ret != MESSAGE_PORT_ERROR_NONE) {
691 _LOGE("write data fail");
695 is_startline = false;
698 ret = MESSAGE_PORT_ERROR_NONE;
703 if (ret == MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE) {
705 message->sent_bytes += nb;
707 message->sent_bytes = nb;
709 message->sequence = sequence;
710 _LOGE("send_delayed_message fail : sockfd (%d) sequence(%d) sent byte(%d)",
711 sockfd, message->sequence, message->sent_bytes);
719 static bool __validate_delay_port_info(delay_port_info *delay_info)
721 message_port_remote_app_info_s *found_remote_port_info;
724 found_remote_port_info =
725 (message_port_remote_app_info_s *)g_hash_table_lookup(
726 __remote_app_info, delay_info->key_info->remote_app_id);
727 if (found_remote_port_info == NULL)
730 cb_list = g_list_find(found_remote_port_info->port_list, delay_info->port_info);
736 /* LCOV_EXCL_START */
737 static gboolean __process_delayed_message(gint fd, GIOCondition cond, gpointer data)
739 delay_port_info *delay_info = (delay_port_info *)data;
740 port_list_info_s *port_info = delay_info->port_info;
741 delay_message_info_s *message;
744 if (port_info == NULL)
745 return G_SOURCE_REMOVE;
747 pthread_mutex_lock(&mutex);
749 if (__validate_delay_port_info(delay_info) == false) {
750 pthread_mutex_unlock(&mutex);
751 return G_SOURCE_REMOVE;
754 if (port_info->delayed_message_list == NULL) {
755 port_info->delayed_message_size = 0;
756 port_info->delay_src_id = 0;
757 pthread_mutex_unlock(&mutex);
758 return G_SOURCE_REMOVE;
760 message = g_list_nth_data(port_info->delayed_message_list, 0);
761 ret = __send_delayed_message(port_info->send_sock_fd, message);
763 if (ret == MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE) {
764 pthread_mutex_unlock(&mutex);
765 return G_SOURCE_CONTINUE;
766 } else if (ret == MESSAGE_PORT_ERROR_IO_ERROR) {
767 port_info->delay_src_id = 0;
768 pthread_mutex_unlock(&mutex);
769 return G_SOURCE_REMOVE;
772 port_info->delayed_message_size -= message->size;
774 port_info->delayed_message_list = g_list_remove(port_info->delayed_message_list, message);
775 __free_delay_message_info(message);
778 pthread_mutex_unlock(&mutex);
780 return G_SOURCE_CONTINUE;
784 /* LCOV_EXCL_START */
785 static int __insert_delayed_message(port_list_info_s *port_info,
789 unsigned int sent_bytes,
790 const char *local_port,
794 #define QUEUE_SIZE_MAX (1024 * 1024) /* 1MB per remote port (MAX) */
796 unsigned int tmp_size;
797 unsigned int message_size;
798 int ret = MESSAGE_PORT_ERROR_NONE;
799 delay_port_info *delay_info;
801 if (port_info->delayed_message_size >= QUEUE_SIZE_MAX) {
802 _LOGE("cache fail : delayed_message_size (%d), count(%d)",
803 port_info->delayed_message_size, g_list_length(port_info->delayed_message_list));
804 return MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
807 delay_message_info_s *message = (delay_message_info_s *)calloc(1, sizeof(delay_message_info_s));
808 retvm_if(!message, MESSAGE_PORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
810 message_size = sizeof(delay_message_info_s);
812 message->sequence = sequence;
813 tmp_size = strlen(local_port) + 1;
814 message_size += tmp_size;
815 message->local_port_len = tmp_size;
816 message->local_port_name = strdup(local_port);
817 if (message->local_port_name == NULL) {
818 _LOGE("local_port_name strdup fail");
819 ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
822 message->is_bidirection = is_bidirection;
823 message->local_trusted = local_trusted;
824 message_size += data_len;
825 message->data_len = data_len;
826 message->data = (bundle_raw *)strdup((const char *)kb_data);
827 if (message->data == NULL) {
828 _LOGE("data strdup fail");
829 ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
834 message->sent_bytes = sent_bytes;
835 message->size = message_size;
836 port_info->delayed_message_size += message_size;
838 port_info->delayed_message_list = g_list_append(port_info->delayed_message_list, message);
840 if (port_info->delay_src_id == 0) {
841 delay_info = (delay_port_info *)calloc(1, sizeof(delay_port_info));
842 if (delay_info == NULL) {
843 _LOGE("out of memory");
844 ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
848 ret = __create_port_key_info(port_info, &delay_info->key_info);
849 if (ret != MESSAGE_PORT_ERROR_NONE) {
854 delay_info->port_info = port_info;
856 port_info->delay_src_id = g_unix_fd_add_full(G_PRIORITY_DEFAULT,
857 port_info->send_sock_fd, G_IO_OUT, __process_delayed_message,
858 delay_info, __delay_socket_destroy_handler);
861 _LOGE("inserted : pm(%s) fd(%d) ms(%d) ds(%d) dlc(%d) sqn(%d) sb (%d)",
862 port_info->port_name, port_info->send_sock_fd, message_size,
863 port_info->delayed_message_size,
864 g_list_length(port_info->delayed_message_list), sequence, sent_bytes);
867 if (ret != MESSAGE_PORT_ERROR_NONE)
868 __free_delay_message_info(message);
874 static int __message_port_send_async(port_list_info_s *port_info, bundle *kb, const char *local_port,
875 bool local_trusted, bool is_bidirection)
880 bundle_raw *kb_data = NULL;
881 int sequence = SEQUENCE_START;
883 bundle_encode(kb, &kb_data, &data_len);
884 if (kb_data == NULL) {
885 _LOGE("bundle encode fail");
886 ret = MESSAGE_PORT_ERROR_INVALID_PARAMETER;
890 if (data_len > MAX_MESSAGE_SIZE) {
891 _LOGE("bigger than max size\n");
892 ret = MESSAGE_PORT_ERROR_MAX_EXCEEDED;
896 if (g_list_length(port_info->delayed_message_list) > 0) {
897 ret = MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
898 _LOGE("There are messages in the delayed_message_list (count %d)",
899 g_list_length(port_info->delayed_message_list));
903 ret = write_string_to_socket(port_info->send_sock_fd, local_port,
904 strlen(local_port) + 1, &nb, &sequence);
905 if (ret != MESSAGE_PORT_ERROR_NONE) {
906 _LOGE("write local_port fail");
910 ret = write_socket(port_info->send_sock_fd, (char *)&is_bidirection,
911 sizeof(is_bidirection), &nb, &sequence);
912 if (ret != MESSAGE_PORT_ERROR_NONE) {
913 _LOGE("write is_bidirection fail");
917 ret = write_socket(port_info->send_sock_fd, (char *)&local_trusted,
918 sizeof(local_trusted), &nb, &sequence);
919 if (ret != MESSAGE_PORT_ERROR_NONE) {
920 _LOGE("write local_trusted fail");
924 ret = write_string_to_socket(port_info->send_sock_fd, (void *)kb_data,
925 data_len, &nb, &sequence);
926 if (ret != MESSAGE_PORT_ERROR_NONE) {
927 _LOGE("write kb_data fail");
932 if (ret == MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE) {
933 ret = __insert_delayed_message(port_info, sequence, kb_data, data_len, nb,
934 local_port, local_trusted, is_bidirection);
935 if (ret != MESSAGE_PORT_ERROR_NONE)
936 ret = MESSAGE_PORT_ERROR_IO_ERROR;
945 int send_message(const char *remote_appid, const char *remote_port,
946 const char *local_port, bool trusted_message, bool local_trusted, bool bi_dir, bundle *message)
949 int ret = MESSAGE_PORT_ERROR_NONE;
950 GUnixFDList *fd_list = NULL;
953 bundle_raw *raw = NULL;
954 char *bus_name = NULL;
955 char *interface_name = NULL;
957 message_port_remote_app_info_s *remote_app_info = NULL;
958 port_list_info_s *port_info = NULL;
959 GDBusMessage *msg = NULL;
961 GVariant *body = NULL;
962 int sock_pair[2] = {0,};
964 port_key_info_s *__key_info;
968 return MESSAGE_PORT_ERROR_IO_ERROR;
971 ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
972 if (ret != MESSAGE_PORT_ERROR_NONE)
975 if (port_info->exist == false) {
977 _LOGD("port exist check !!");
978 ret = check_remote_port(remote_appid, remote_port, trusted_message, &exist);
979 if (ret != MESSAGE_PORT_ERROR_NONE) {
982 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
986 if (port_info->send_sock_fd > 0) {
987 ret = __message_port_send_async(port_info, message,
988 (local_port) ? local_port : "", local_trusted, bi_dir);
991 bus_name = port_info->encoded_bus_name;
992 interface_name = bus_name;
994 if (bundle_encode(message, &raw, &len) != BUNDLE_ERROR_NONE) {
995 ret = MESSAGE_PORT_ERROR_INVALID_PARAMETER;
999 if (MAX_MESSAGE_SIZE < len) {
1000 _LOGE("The size of message (%d) has exceeded the maximum limit.", len);
1001 ret = MESSAGE_PORT_ERROR_MAX_EXCEEDED;
1005 body = g_variant_new("(ssbbssbus)", app_id, (local_port) ? local_port : "", local_trusted, bi_dir,
1006 remote_appid, remote_port, trusted_message, len, raw);
1007 if (strcmp(remote_appid, app_id) != 0) { /* self send */
1009 /* if message-port fail to get socket pair, communicate using GDBus */
1010 if (aul_request_message_port_socket_pair(sock_pair) != AUL_R_OK) {
1011 _LOGE("error create socket pair");
1014 _LOGI("sock pair : %d, %d",
1015 sock_pair[SOCK_PAIR_SENDER], sock_pair[SOCK_PAIR_RECEIVER]);
1016 fd_list = g_unix_fd_list_new();
1017 g_unix_fd_list_append(fd_list, sock_pair[SOCK_PAIR_RECEIVER], &err);
1019 _LOGE("g_unix_fd_list_append [%s]", err->message);
1020 ret = MESSAGE_PORT_ERROR_IO_ERROR;
1025 port_info->send_sock_fd = sock_pair[SOCK_PAIR_SENDER];
1026 close(sock_pair[SOCK_PAIR_RECEIVER]);
1027 sock_pair[SOCK_PAIR_RECEIVER] = 0;
1029 port_info->gio_read = g_io_channel_unix_new(port_info->send_sock_fd);
1030 if (!port_info->gio_read) {
1031 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
1032 ret = MESSAGE_PORT_ERROR_IO_ERROR;
1036 ret = __create_port_key_info(port_info, &__key_info);
1037 if (ret != MESSAGE_PORT_ERROR_NONE) {
1038 _LOGE("out of memory");
1042 port_info->g_src_id = g_io_add_watch_full(
1043 port_info->gio_read,
1046 __socket_disconnect_handler,
1047 (gpointer)__key_info,
1048 __socket_destroy_handler);
1049 if (port_info->g_src_id == 0) {
1050 _LOGE("fail to add watch on socket");
1051 ret = MESSAGE_PORT_ERROR_IO_ERROR;
1052 __free_key_info(__key_info);
1059 msg = g_dbus_message_new_method_call(bus_name, MESSAGEPORT_OBJECT_PATH, interface_name, "send_message");
1061 _LOGE("Can't allocate new method call");
1062 ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1066 g_dbus_message_set_unix_fd_list(msg, fd_list);
1067 g_dbus_message_set_body(msg, body);
1068 g_dbus_connection_send_message(gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
1070 _LOGE("No reply. error = %s", err->message);
1072 ret = MESSAGE_PORT_ERROR_IO_ERROR;
1079 g_object_unref(msg);
1081 bundle_free_encoded_rawdata(&raw);
1083 g_object_unref(fd_list);
1085 if (ret != MESSAGE_PORT_ERROR_NONE) {
1087 __create_port_key_info(port_info, &__key_info);
1089 if (__key_info != NULL) {
1090 __free_port_info_by_key(__key_info);
1091 __free_key_info(__key_info);
1094 if (sock_pair[SOCK_PAIR_SENDER])
1095 close(sock_pair[SOCK_PAIR_SENDER]);
1096 if (sock_pair[SOCK_PAIR_RECEIVER])
1097 close(sock_pair[SOCK_PAIR_RECEIVER]);
1103 int send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port, bool trusted_message, bundle *message)
1105 message_port_local_port_info_s *local_info;
1106 int ret = get_local_port_info(id, &local_info);
1107 if (ret != MESSAGE_PORT_ERROR_NONE)
1110 _LOGD("bidirectional_message %s", local_info->port_name);
1111 return send_message(remote_app_id, remote_port,
1112 local_info->port_name, trusted_message, local_info->is_trusted, true, message);
1115 static void __name_registered(GDBusConnection *connection,
1117 const gchar *name_owner,
1121 registered_callback_info_s *info = (registered_callback_info_s *)user_data;
1123 LOGE("NULL registered_callback_info");
1127 _LOGI("watcher_id : %d, appeared name : %s , name_owner : %s\n", info->watcher_id, name, name_owner);
1128 if (info->registered_cb)
1129 info->registered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
1132 static void __name_unregistered(GDBusConnection *connection,
1137 registered_callback_info_s *info = (registered_callback_info_s *)user_data;
1139 LOGE("NULL registered_callback_info");
1143 _LOGI("watcher_id : %d, vanished name : %s\n", info->watcher_id, name);
1144 if (info->unregistered_cb)
1145 info->unregistered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
1148 int watch_remote_port(int *watcher_id, const char *remote_app_id, const char *remote_port, bool trusted_remote_port, message_port_registration_event_cb registered_cb, message_port_registration_event_cb unregistered_cb, void *user_data)
1150 int ret_val = MESSAGE_PORT_ERROR_NONE;
1151 message_port_remote_app_info_s *remote_app_info = NULL;
1152 port_list_info_s *port_info = NULL;
1154 if (!_initialized) {
1155 if (!__initialize())
1156 return MESSAGE_PORT_ERROR_IO_ERROR;
1158 _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, app_id);
1160 ret_val = __get_remote_port_info(remote_app_id, remote_port, trusted_remote_port, &remote_app_info, &port_info);
1161 if (ret_val != MESSAGE_PORT_ERROR_NONE) {
1162 _LOGE("Failed to get remote_port_info %d", ret_val);
1166 registered_callback_info_s *registered_cb_info = (registered_callback_info_s *)calloc(1, sizeof(registered_callback_info_s));
1167 retvm_if(!registered_cb_info, MESSAGE_PORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
1169 registered_cb_info->registered_cb = registered_cb;
1170 registered_cb_info->unregistered_cb = unregistered_cb;
1171 registered_cb_info->user_data = user_data;
1172 registered_cb_info->remote_app_id = strdup(remote_app_info->remote_app_id);
1173 if (registered_cb_info->remote_app_id == NULL) {
1174 free(registered_cb_info);
1175 _LOGE("Failed to alloc memory");
1176 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1178 registered_cb_info->remote_port = strdup(port_info->port_name);
1179 if (registered_cb_info->remote_port == NULL) {
1180 free(registered_cb_info->remote_app_id);
1181 free(registered_cb_info);
1182 _LOGE("Failed to alloc memory");
1183 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1186 registered_cb_info->watcher_id = g_bus_watch_name_on_connection(
1188 port_info->encoded_bus_name,
1189 G_BUS_NAME_WATCHER_FLAGS_NONE,
1191 __name_unregistered,
1194 if (registered_cb_info->watcher_id == 0) {
1195 free(registered_cb_info->remote_app_id);
1196 free(registered_cb_info->remote_port);
1197 free(registered_cb_info);
1198 _LOGE("Failed to watch name");
1199 return MESSAGE_PORT_ERROR_IO_ERROR;
1202 g_hash_table_insert(__registered_callback_info_hash,
1203 GINT_TO_POINTER(registered_cb_info->watcher_id), registered_cb_info);
1205 *watcher_id = registered_cb_info->watcher_id;
1206 return MESSAGE_PORT_ERROR_NONE;
1209 int remove_registration_event_cb(int watcher_id)
1211 registered_callback_info_s *registered_cb_info = NULL;
1212 gboolean remove_result = FALSE;
1215 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1217 registered_cb_info = g_hash_table_lookup(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
1218 if (registered_cb_info == NULL)
1219 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1221 remove_result = g_hash_table_remove(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
1223 return MESSAGE_PORT_ERROR_IO_ERROR;
1225 g_bus_unwatch_name(watcher_id);
1227 return MESSAGE_PORT_ERROR_NONE;