-// Message Port
-// Copyright (c) 2015 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Apache License, Version 2.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ Message Port
+ Copyright (c) 2015 Samsung Electronics Co., Ltd.
+
+ Licensed under the Apache License, Version 2.0 (the License);
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
/**
* @file message-port.cpp
temp += 2;
}
- if (postfix && postfix_len > 0) {
+ if (postfix && postfix_len > 0)
snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
- }
if (bus_name)
free(bus_name);
port_list_info_s *key1 = (port_list_info_s *)a;
port_list_info_s *key2 = (port_list_info_s *)b;
- if (key1->is_trusted == key2->is_trusted) {
+ if (key1->is_trusted == key2->is_trusted)
return strcmp(key1->port_name, key2->port_name);
- }
return 1;
}
return MESSAGEPORT_ERROR_NONE;
}
-static void on_name_appeared (GDBusConnection *connection,
+static void on_name_appeared(GDBusConnection *connection,
const gchar *name,
const gchar *name_owner,
gpointer user_data)
_LOGI("name appeared : %s %s", __app_id, name);
}
-static void on_name_vanished (GDBusConnection *connection,
+static void on_name_vanished(GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
{
message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
- if (mi == NULL) {
+ if (mi == NULL)
return MESSAGEPORT_ERROR_INVALID_PARAMETER;
- }
*info = mi;
return MESSAGEPORT_ERROR_NONE;
port_info->sock_pair[0] = 0;
port_info->sock_pair[1] = 0;
- out:
+out:
if (ret_val != MESSAGEPORT_ERROR_NONE) {
if (port_info) {
FREE_AND_NULL(port_info->port_name);
remote_app_info->port_list = g_list_append(remote_app_info->port_list, port_info);
- out:
+out:
if (ret_val != MESSAGEPORT_ERROR_NONE) {
if (remote_app_info) {
FREE_AND_NULL(remote_app_info->remote_app_id);
if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
*local_id = mi->local_id;
- if (lpi != NULL) {
+ if (lpi != NULL)
*lpi = mi;
- }
return true;
}
}
goto out;
}
- g_dbus_message_set_body (msg, g_variant_new ("(s)", sender_name));
+ g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
int ret;
message_port_pkt_s *pkt = NULL;
- pkt = (message_port_pkt_s *) calloc(sizeof(char) * MAX_MESSAGE_SIZE, 1);
- if(pkt == NULL) {
+ pkt = (message_port_pkt_s *)calloc(sizeof(char) * MAX_MESSAGE_SIZE, 1);
+ if (pkt == NULL) {
close(fd);
return NULL;
}
close(fd);
return NULL;
}
- while( len < (pkt->len + HEADER_LEN) ) {
+ while (len < (pkt->len + HEADER_LEN)) {
retry_recv1:
ret = recv(fd, &pkt->data[len - 8], MAX_MESSAGE_SIZE, 0);
if (ret < 0) {
return TRUE;
}
- //_LOGI("__socket_request_handler fd : %d", fd);
-
mi = (message_port_callback_info_s *)data;
if ((pkt = __message_port_recv_raw(fd)) == NULL) {
kb = bundle_decode(pkt->data, pkt->len);
is_bidirection = pkt->is_bidirection;
- if (is_bidirection) {
+ if (is_bidirection)
mi->callback(mi->local_id, mi->remote_app_id, mi->remote_port, mi->is_trusted, kb, NULL);
- } else {
+ else
mi->callback(mi->local_id, mi->remote_app_id, NULL, mi->is_trusted, kb, NULL);
- }
if (pkt)
free(pkt);
if (remote_trusted) {
if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
if (!__is_preloaded(local_appid, remote_appid)) {
- // Check the certificate
+ /* Check the certificate */
int ret = __check_certificate(local_appid, remote_appid);
- if (ret == MESSAGEPORT_ERROR_NONE) {
+ if (ret == MESSAGEPORT_ERROR_NONE)
g_hash_table_insert(__trusted_app_list_hash, local_appid, "TRUE");
- } else {
+ else {
_LOGE("The application (%s) is not signed with the same certificate",
local_appid);
goto out;
}
callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
- if (callback_info == NULL) {
+ if (callback_info == NULL)
goto out;
- }
+
callback_info->local_id = mi->local_id;
callback_info->remote_app_id = strdup(local_appid);
callback_info->remote_port = strdup(local_port);
}
LOGI("call calback %s", local_appid);
- if (bi_dir) {
+ if (bi_dir)
mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
- } else {
+ else
mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
- }
+
out:
_LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
- if (ret_val != MESSAGEPORT_ERROR_NONE) {
+ if (ret_val != MESSAGEPORT_ERROR_NONE)
return ret_val;
- }
- // self check
+
+ /* self check */
if (strcmp(remote_app_id, __app_id) == 0) {
_LOGI("__is_local_port_registed ");
- if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi)) {
+ if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
*exist = false;
- } else {
+ else
*exist = true;
- }
+
_LOGI("__is_local_port_registed : %d ", *exist);
return MESSAGEPORT_ERROR_NONE;
}
LOGE("Name not exist %s", bus_name);
*exist = false;
ret_val = MESSAGEPORT_ERROR_NONE;
- }
- else {
+ } else {
if (is_trusted) {
if (remote_app_info->certificate_info != CERTIFICATE_MATCH) {
return;
}
- if (g_strcmp0(method_name, "send_message") == 0) {
+ if (g_strcmp0(method_name, "send_message") == 0)
send_message(parameters, invocation);
- }
}
}
-int __register_dbus_interface(const char *port_name, bool is_trusted) {
+int __register_dbus_interface(const char *port_name, bool is_trusted)
+{
GDBusNodeInfo *introspection_data = NULL;
int registration_id = 0;
if (mri) {
FREE_AND_NULL(mri->sender_id);
FREE_AND_NULL(mri->remote_app_id);
- if (mri->port_list) {
+ if (mri->port_list)
g_list_free_full(mri->port_list, __list_free_port_list);
- }
}
}
retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
}
-
- if (!__dbus_init()) {
+ if (!__dbus_init())
return false;
- }
_initialized = true;
return true;
int local_id = 0;
- // Check the message port is already registed
- if (__is_local_port_registed(local_port, is_trusted, &local_id, NULL)) {
+ /* Check the message port is already registed */
+ if (__is_local_port_registed(local_port, is_trusted, &local_id, NULL))
return local_id;
- }
local_id = __register_dbus_interface(local_port, is_trusted);
if (local_id < 1) {
return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
}
- if (!__message_port_register_port(local_id, local_port, is_trusted, callback)) {
+ if (!__message_port_register_port(local_id, local_port, is_trusted, callback))
return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
- }
return local_id;
}
if ((len = send(sockfd, pkt, pkt_size, 0)) != pkt_size) {
SECURE_LOGE("send() failed - len[%d] pkt_size[%d] (errno %d[%s])", len, pkt_size,
errno, strerror(errno));
- if (errno == EPIPE) {
+ if (errno == EPIPE)
_LOGE("fd:%d\n", sockfd);
- }
+
if (errno == EINTR) {
if (retry_ctr > 0) {
_LOGI("Retrying send on fd[%d]", sockfd);
GVariant *body = NULL;
ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
- if (ret != MESSAGEPORT_ERROR_NONE) {
+ if (ret != MESSAGEPORT_ERROR_NONE)
return ret;
- }
if (port_info->exist == false) {
bool exist = false;
remote_appid, remote_port, trusted_message, len, raw);
- if (strcmp(remote_appid, __app_id) != 0) { // self send
+ if (strcmp(remote_appid, __app_id) != 0) { /* self send */
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, port_info->sock_pair) != 0) {
_LOGE("error create socket pair");
{
message_port_local_port_info_s *local_info;
int ret = __get_local_port_info(id, &local_info);
- if (ret != MESSAGEPORT_ERROR_NONE) {
+ if (ret != MESSAGEPORT_ERROR_NONE)
return ret;
- }
_LOGI("bidirectional_message %s", local_info->port_name);
return __message_port_send_message(remote_app_id, remote_port,
message_port_local_port_info_s *local_info;
int ret = __get_local_port_info(id, &local_info);
- if (ret != MESSAGEPORT_ERROR_NONE) {
+ if (ret != MESSAGEPORT_ERROR_NONE)
return ret;
- }
*name = strdup(local_info->port_name);
- if (*name == NULL) {
+ if (*name == NULL)
return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
- }
return MESSAGEPORT_ERROR_NONE;
}
message_port_local_port_info_s *local_info;
int ret = __get_local_port_info(id, &local_info);
- if (ret != MESSAGEPORT_ERROR_NONE) {
+ if (ret != MESSAGEPORT_ERROR_NONE)
return ret;
- }
*trusted = local_info->is_trusted;
static void do_callback(message_port_message_cb callback, int local_port_id, const char *remote_app_id, const char *remote_port, bool trusted_remote_port, bundle *message, void *user_data)
{
- if (callback)
- {
+ if (callback) {
callback(local_port_id, remote_app_id, remote_port, trusted_remote_port, message, user_data);
bundle_free(message);
- }
- else
- {
+ } else {
_LOGI("Ignored");
}
}
int message_port_register_local_port(const char *local_port, message_port_message_cb callback, void *user_data)
{
- if (local_port == NULL || callback == NULL)
- {
+ if (local_port == NULL || callback == NULL) {
_LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
}
int local_port_id = messageport_register_local_port(local_port, message_dispatcher);
- if (local_port_id > 0)
- {
+ if (local_port_id > 0) {
_SECURE_LOGI("Register local port ID (%d).", local_port_id);
- if (__listeners == NULL) {
+ if (__listeners == NULL)
__listeners = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
- }
+
pthread_mutex_lock(&__mutex);
message_port_callback_item *item =
(message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
if (item == NULL) {
item = (message_port_callback_item *)calloc(1, sizeof(message_port_callback_item));
- if (item == NULL) {
+ if (item == NULL)
return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
- }
+
g_hash_table_insert(__listeners, GINT_TO_POINTER(local_port_id), item);
}
-
item->callback = callback;
item->user_data = user_data;
pthread_mutex_unlock(&__mutex);
} else {
_SECURE_LOGI("Register local port fail (%d).", local_port_id);
-
}
+
return convert_to_tizen_error((messageport_error_e)local_port_id);
}
int message_port_register_trusted_local_port(const char *local_port, message_port_trusted_message_cb callback, void *user_data)
{
- if (local_port == NULL || callback == NULL)
- {
+ if (local_port == NULL || callback == NULL) {
_LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
}
int trusted_local_port_id = messageport_register_trusted_local_port(local_port, trusted_message_dispatcher);
- if (trusted_local_port_id > 0)
- {
+ if (trusted_local_port_id > 0) {
_SECURE_LOGI("Register trusted local port ID (%d).", trusted_local_port_id);
- if (__trusted_listeners == NULL) {
+ if (__trusted_listeners == NULL)
__trusted_listeners = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
- }
+
pthread_mutex_lock(&__mutex);
message_port_callback_item *item =
(message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
if (item == NULL) {
item = (message_port_callback_item *)calloc(1, sizeof(message_port_callback_item));
- if (item == NULL) {
+ if (item == NULL)
return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
- }
+
g_hash_table_insert(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id), item);
}
pthread_mutex_unlock(&__mutex);
} else {
_SECURE_LOGI("Register trusted local port fail (%d).", trusted_local_port_id);
-
}
return convert_to_tizen_error((messageport_error_e)trusted_local_port_id);
int message_port_unregister_local_port(int local_port_id)
{
int res = MESSAGE_PORT_ERROR_NONE;
- if (local_port_id <= 0)
- {
+ if (local_port_id <= 0) {
_LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
- }
- else {
+ } else {
res = messageport_unregister_local_port(local_port_id, false);
g_hash_table_remove(__listeners, GINT_TO_POINTER(local_port_id));
}
{
int res = MESSAGE_PORT_ERROR_NONE;
- if (trusted_local_port_id <= 0)
- {
+ if (trusted_local_port_id <= 0) {
_LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
- }
- else {
+ } else {
res = messageport_unregister_local_port(trusted_local_port_id, true);
g_hash_table_remove(__trusted_listeners, GINT_TO_POINTER(trusted_local_port_id));
}
int message_port_check_remote_port(const char* remote_app_id, const char *remote_port, bool* exist)
{
- if (remote_app_id == NULL || remote_port == NULL)
- {
+ if (remote_app_id == NULL || remote_port == NULL) {
_LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
}
int message_port_check_trusted_remote_port(const char* remote_app_id, const char *remote_port, bool *exist)
{
- if (remote_app_id == NULL || remote_port == NULL)
- {
+ if (remote_app_id == NULL || remote_port == NULL) {
_LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
}
int message_port_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
{
- if (remote_app_id == NULL || remote_port == NULL || message == NULL)
- {
+ if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
_LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
}
int message_port_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
{
- if (remote_app_id == NULL || remote_port == NULL || message == NULL)
- {
+ if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
_LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
}
int message_port_send_message_with_local_port(const char *remote_app_id, const char *remote_port, bundle *message, int local_port_id)
{
- if (remote_app_id == NULL || remote_port == NULL || message == NULL)
- {
+ if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
_LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
- }
- else if (local_port_id <= 0)
- {
+ } else if (local_port_id <= 0) {
_LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
- }
- else {
+ } else {
message_port_callback_item *item = NULL;
message_port_callback_item *trusted_item = NULL;
- if (__listeners != NULL) {
+ if (__listeners != NULL)
item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
- }
- if (item == NULL && __trusted_listeners != NULL) {
+
+ if (item == NULL && __trusted_listeners != NULL)
trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
- }
- if (item == NULL && trusted_item == NULL)
- {
+ if (item == NULL && trusted_item == NULL) {
_LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
}
int message_port_send_trusted_message_with_local_port(const char* remote_app_id, const char *remote_port, bundle* message, int local_port_id)
{
- if (remote_app_id == NULL || remote_port == NULL || message == NULL)
- {
+ if (remote_app_id == NULL || remote_port == NULL || message == NULL) {
_LOGE("[MESSAGE_PORT_ERROR_INVALID_PARAMETER] NULL value is not allowed.");
return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
- }
- else if (local_port_id <= 0)
- {
+ } else if (local_port_id <= 0) {
_LOGE("[MESSAGEPORT_ERROR_INVALID_PARAMETER] Neither 0 nor negative value is allowed.");
return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
- }
- else {
+ } else {
message_port_callback_item *item = NULL;
message_port_callback_item *trusted_item = NULL;
- if (__listeners != NULL) {
+ if (__listeners != NULL)
item = (message_port_callback_item *)g_hash_table_lookup(__listeners, GINT_TO_POINTER(local_port_id));
- }
- if (item == NULL && __trusted_listeners != NULL) {
- trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
- }
+ if (item == NULL && __trusted_listeners != NULL)
+ trusted_item = (message_port_callback_item *)g_hash_table_lookup(__trusted_listeners, GINT_TO_POINTER(local_port_id));
- if (item == NULL && trusted_item == NULL)
- {
+ if (item == NULL && trusted_item == NULL) {
_LOGE("[MESSAGE_PORT_ERROR_PORT_NOT_FOUND] The local port ID (%d) is not registered.", local_port_id);
return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
}