Add dbus async call for add/remove ipt rule 85/237385/1
authorhyunuk.tak <hyunuk.tak@samsung.com>
Wed, 29 Apr 2020 01:10:30 +0000 (10:10 +0900)
committerCheoleun Moon <chleun.moon@samsung.com>
Tue, 30 Jun 2020 03:20:26 +0000 (12:20 +0900)
Change-Id: I30394b30614b51e259e554c3631abdbd5d17af54
Signed-off-by: hyunuk.tak <hyunuk.tak@samsung.com>
include/stc-manager-gdbus.h
src/helper/helper-iptables.c
src/stc-manager-gdbus.c

index 263077d..b535ed2 100755 (executable)
@@ -61,6 +61,12 @@ GVariant *stc_manager_gdbus_call_sync(GDBusConnection *connection,
                                      const char *dest, const char *path,
                                      const char *interface_name,
                                      const char *method, GVariant *params);
+int stc_manager_gdbus_call_async(GDBusConnection *connection,
+                                     const char *dest, const char *path,
+                                     const char *interface_name,
+                                     const char *method, GVariant *params,
+                                     GAsyncReadyCallback notify_func,
+                                     void *user_data);
 guint stc_manager_gdbus_subscribe_signal(GDBusConnection *connection,
                                         const gchar *sender,
                                         const gchar *interface_name,
index 1e140b7..519d898 100755 (executable)
@@ -97,136 +97,176 @@ static void __add_rule_info_to_builder(GVariantBuilder *builder,
                                      g_variant_new_uint32(rule->d_ip2.s_addr));
 }
 
+static void __add_rule_reply(
+                       GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+       GDBusConnection *conn = NULL;
+       GVariant *dbus_data = NULL;
+       GError *dbus_error = NULL;
+       int result = 0;
+       char *nfacct_name = user_data;
+
+       conn = G_DBUS_CONNECTION(source_object);
+       dbus_data = g_dbus_connection_call_finish(conn, res, &dbus_error);
+       if (dbus_error != NULL) {
+               STC_LOGE("Dbus reply error [%s]", dbus_error->message);
+               g_error_free(dbus_error);
+       } else {
+               g_variant_get(dbus_data, "(i)", &result);
+               STC_LOGI("Added rule [%d:%s]", result, nfacct_name);
+       }
+
+       g_free(nfacct_name);
+}
+
+static void __remove_rule_reply(
+                       GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+       GDBusConnection *conn = NULL;
+       GVariant *dbus_data = NULL;
+       GError *dbus_error = NULL;
+       int result = 0;
+       char *nfacct_name = user_data;
+
+       conn = G_DBUS_CONNECTION(source_object);
+       dbus_data = g_dbus_connection_call_finish(conn, res, &dbus_error);
+       if (dbus_error != NULL) {
+               STC_LOGE("Dbus reply error [%s]", dbus_error->message);
+               g_error_free(dbus_error);
+       } else {
+               g_variant_get(dbus_data, "(i)", &result);
+               STC_LOGI("Removed rule [%d:%s]", result, nfacct_name);
+       }
+
+       g_free(nfacct_name);
+}
+
 static int __iptables_rule_add(GDBusConnection *connection,
                               iptables_rule_s *rule)
 {
-       int result = 0;
+       int result = STC_ERROR_NONE;
        GVariantBuilder *builder = NULL;
        GVariant *params = NULL;
-       GVariant *message = NULL;
+       char *nfacct_name = NULL;
 
        builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
        __add_rule_info_to_builder(builder, rule);
        params = g_variant_new("(a{sv})", builder);
        g_variant_builder_unref(builder);
 
-       message = stc_manager_gdbus_call_sync(connection,
+       nfacct_name = g_strdup_printf("4:%s", rule->nfacct_name);
+
+       result = stc_manager_gdbus_call_async(connection,
                                              STC_IPTABLES_DBUS_SERVICE,
                                              STC_IPTABLES_DBUS_RULE_PATH,
                                              STC_IPTABLES_DBUS_RULE_INTERFACE,
                                              STC_IPTABLES_DBUS_METHOD_IPT_ADD_RULE,
-                                             params);
+                                             params,
+                                             __add_rule_reply,
+                                             nfacct_name);
 
-       if (message == NULL) {
-               STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE
-               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
+       if (result != STC_ERROR_NONE) {
+               STC_LOGE("Failed to invoke dbus method async");
+               g_free(nfacct_name);
        }
 
-       g_variant_get(message, "(i)", &result);
-       if (STC_DEBUG_LOG)
-               STC_LOGD("Successfully Add Rule [%d:%s]", result, rule->nfacct_name);
-       g_variant_unref(message);
-
-       return STC_ERROR_NONE;
+       return result;
 }
 
 static int __iptables_rule_remove(GDBusConnection *connection,
                                  iptables_rule_s *rule)
 {
-       int result = 0;
+       int result = STC_ERROR_NONE;
        GVariantBuilder *builder = NULL;
        GVariant *params = NULL;
-       GVariant *message = NULL;
+       char *nfacct_name = NULL;
 
        builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
        __add_rule_info_to_builder(builder, rule);
        params = g_variant_new("(a{sv})", builder);
        g_variant_builder_unref(builder);
 
-       message = stc_manager_gdbus_call_sync(connection,
+       nfacct_name = g_strdup_printf("4:%s", rule->nfacct_name);
+
+       result = stc_manager_gdbus_call_async(connection,
                                              STC_IPTABLES_DBUS_SERVICE,
                                              STC_IPTABLES_DBUS_RULE_PATH,
                                              STC_IPTABLES_DBUS_RULE_INTERFACE,
                                              STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_RULE,
-                                             params);
+                                             params,
+                                             __remove_rule_reply,
+                                             nfacct_name);
 
-       if (message == NULL) {
-               STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE
-               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
+       if (result != STC_ERROR_NONE) {
+               STC_LOGE("Failed to invoke dbus method async");
+               g_free(nfacct_name);
        }
 
-       g_variant_get(message, "(i)", &result);
-       if (STC_DEBUG_LOG)
-               STC_LOGD("Successfully Remove Rule [%d:%s]", result, rule->nfacct_name);
-       g_variant_unref(message);
-
-       return STC_ERROR_NONE;
+       return result;
 }
 
 static int __ip6tables_rule_add(GDBusConnection *connection,
                                iptables_rule_s *rule)
 {
-       int result = 0;
+       int result = STC_ERROR_NONE;
        GVariantBuilder *builder = NULL;
        GVariant *params = NULL;
-       GVariant *message = NULL;
+       char *nfacct_name = NULL;
 
        builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
        __add_rule_info_to_builder(builder, rule);
        params = g_variant_new("(a{sv})", builder);
        g_variant_builder_unref(builder);
 
-       message = stc_manager_gdbus_call_sync(connection,
+       nfacct_name = g_strdup_printf("6:%s", rule->nfacct_name);
+
+       result = stc_manager_gdbus_call_async(connection,
                                              STC_IPTABLES_DBUS_SERVICE,
                                              STC_IPTABLES_DBUS_RULE_PATH,
                                              STC_IPTABLES_DBUS_RULE_INTERFACE,
                                              STC_IPTABLES_DBUS_METHOD_IP6T_ADD_RULE,
-                                             params);
+                                             params,
+                                             __add_rule_reply,
+                                             nfacct_name);
 
-       if (message == NULL) {
-               STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE
-               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
+       if (result != STC_ERROR_NONE) {
+               STC_LOGE("Failed to invoke dbus method async");
+               g_free(nfacct_name);
        }
 
-       g_variant_get(message, "(i)", &result);
-       if (STC_DEBUG_LOG)
-               STC_LOGD("Successfully Add 6 Rule [%d:%s]", result, rule->nfacct_name);
-       g_variant_unref(message);
-
-       return STC_ERROR_NONE;
+       return result;
 }
 
 static int __ip6tables_rule_remove(GDBusConnection *connection,
                                   iptables_rule_s *rule)
 {
-       int result = 0;
+       int result = STC_ERROR_NONE;
        GVariantBuilder *builder = NULL;
        GVariant *params = NULL;
-       GVariant *message = NULL;
+       char *nfacct_name = NULL;
 
        builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
        __add_rule_info_to_builder(builder, rule);
        params = g_variant_new("(a{sv})", builder);
        g_variant_builder_unref(builder);
 
-       message = stc_manager_gdbus_call_sync(connection,
+       nfacct_name = g_strdup_printf("6:%s", rule->nfacct_name);
+
+       result = stc_manager_gdbus_call_async(connection,
                                              STC_IPTABLES_DBUS_SERVICE,
                                              STC_IPTABLES_DBUS_RULE_PATH,
                                              STC_IPTABLES_DBUS_RULE_INTERFACE,
                                              STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_RULE,
-                                             params);
+                                             params,
+                                             __remove_rule_reply,
+                                             nfacct_name);
 
-       if (message == NULL) {
-               STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE
-               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
+       if (result != STC_ERROR_NONE) {
+               STC_LOGE("Failed to invoke dbus method async");
+               g_free(nfacct_name);
        }
 
-       g_variant_get(message, "(i)", &result);
-       if (STC_DEBUG_LOG)
-               STC_LOGD("Successfully Remove 6 Rule [%d:%s]", result, rule->nfacct_name);
-       g_variant_unref(message);
-
-       return STC_ERROR_NONE;
+       return result;
 }
 
 static int __iptables_add_chain(GDBusConnection *connection,
index 943f466..a5cb068 100755 (executable)
@@ -466,6 +466,34 @@ API GVariant *stc_manager_gdbus_call_sync(GDBusConnection *connection,
        return reply;
 }
 
+API int stc_manager_gdbus_call_async(GDBusConnection *connection,
+                                     const char *dest, const char *path,
+                                     const char *interface_name,
+                                     const char *method, GVariant *params,
+                                     GAsyncReadyCallback notify_func,
+                                     void *user_data)
+{
+       if (connection == NULL) {
+               STC_LOGE("Failed to get GDBusconnection"); //LCOV_EXCL_LINE
+               return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+       }
+
+       g_dbus_connection_call(connection,
+                           dest,
+                           path,
+                           interface_name,
+                           method,
+                           params,
+                           NULL,
+                           G_DBUS_CALL_FLAGS_NONE,
+                           (5 * 1000),  /* 5 seconds timeout */
+                           NULL,
+                           (GAsyncReadyCallback)notify_func,
+                           (gpointer)user_data);
+
+       return STC_ERROR_NONE;
+}
+
 API guint stc_manager_gdbus_subscribe_signal(GDBusConnection *connection,
                                         const gchar *sender,
                                         const gchar *interface_name,