Added a new API to commit iptables 48/175748/1 accepted/tizen/unified/20180417.173148 submit/tizen/20180416.224349
authorhyunuktak <hyunuk.tak@samsung.com>
Thu, 12 Apr 2018 06:08:11 +0000 (15:08 +0900)
committerhyunuktak <hyunuk.tak@samsung.com>
Thu, 12 Apr 2018 06:08:14 +0000 (15:08 +0900)
Change-Id: I43a60c35805c9d5994b3ba4b51aa6e1a4f560b4d
Signed-off-by: hyunuktak <hyunuk.tak@samsung.com>
include/stc_internal.h
packaging/capi-network-stc.spec
src/internal/include/stc-dbus.h
src/stc-manager.c
test/CMakeLists.txt
test/manager.c [new file with mode: 0755]
test/manager.h [new file with mode: 0755]
test/stc_test.c

index 9f4a552ba8e9657a1b0e04a421a7805e37b3f072..3923abc73be61d86a013a550ce383559be29fbad 100755 (executable)
@@ -2870,6 +2870,42 @@ int stc_firewall_rule_foreach(stc_h stc,
 * @}
 */
 
+/**
+ * @addtogroup CAPI_NETWORK_STC_IPTABLES_MODULE
+ * @{
+ */
+
+/**
+ * @brief Commits iptables rule.
+ * @since_tizen 5.0
+ * @privlevel platform
+ * @privilege %http://tizen.org/privilege/firewall.common
+ * @remarks You must release @a err_str using free().
+ *
+ * @param[in] stc        The stc handle
+ * @param[in] option     The option of iptables rule
+ * @param[out] err_num   The error number
+ * @param[out] err_str   The error string
+ *
+ * @return 0 on success, otherwise a negative error value
+ * @retval #STC_ERROR_NONE Successful
+ * @retval #STC_ERROR_OPERATION_FAILED General error
+ * @retval #STC_ERROR_OUT_OF_MEMORY Out of memory
+ * @retval #STC_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #STC_ERROR_INVALID_OPERATION Invalid operation
+ * @retval #STC_ERROR_NOT_INITIALIZED Not initialized
+ * @retval #STC_ERROR_NOT_SUPPORTED Not supported
+ * @retval #STC_ERROR_PERMISSION_DENIED Permission denied
+ *
+ * @see stc_initialize()
+ */
+int stc_iptables_commit(stc_h stc, const char *option,
+                       int *err_num, char **err_str);
+
+/**
+* @}
+*/
+
 #ifdef __cplusplus
 }
 #endif
index 5767c55fe2cc470849eebc3ba15bd1a4b78a00a7..412ef1db976a98d7c64c1d1c1b9580b3ca2b43cc 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       capi-network-stc
 Summary:    A Smart Traffic Control (STC) libraries in Native API
-Version:    0.0.24
+Version:    0.0.25
 Release:    1
 Group:      Network & Connectivity/API
 License:    Apache-2.0
index 67eeb2fa6c1e03999186694c0c869e95eb616377..293badc0ffae61e6f9abda653c5d8e79c79b30d1 100755 (executable)
@@ -52,25 +52,29 @@ extern "C" {
  *****************************************************************************/
 #define STC_DBUS_REPLY_TIMEOUT (120 * 1000)
 
-#define STC_MANAGER_SERVICE                            "net.stc"
-#define STC_MANAGER_STATS_INTERFACE    STC_MANAGER_SERVICE ".statistics"
-#define STC_MANAGER_RESTRICTION_INTERFACE      STC_MANAGER_SERVICE ".restriction"
-
-#define STC_MANAGER_STATS_PATH                 "/net/stc/statistics"
-#define STC_MANAGER_RESTRICTION_PATH   "/net/stc/restriction"
-
-#define STC_MANAGER_METHOD_STATS_INIT                          "Init"
-#define STC_MANAGER_METHOD_STATS_GET_PER_APP_ID                "Get"
-#define STC_MANAGER_METHOD_STATS_GET_ALL                       "GetAll"
-#define STC_MANAGER_METHOD_STATS_GET_TOTAL                     "Get"
-#define STC_MANAGER_METHOD_STATS_RESET                         "Reset"
-
-#define STC_MANAGER_METHOD_RESTRICTION_SET                             "Set"
-#define STC_MANAGER_METHOD_RESTRICTION_GET_STATUS              "GetState"
-#define STC_MANAGER_METHOD_RESTRICTION_GET_PER_APP_ID  "Get"
-#define STC_MANAGER_METHOD_RESTRICTION_GET_ALL                 "GetAll"
-#define STC_MANAGER_METHOD_RESTRICTION_UNSET                   "Unset"
-#define STC_MANAGER_METHOD_RESTRICTION_EXCLUDE                 "Exclude"
+#define STC_MANAGER_SERVICE                  "net.stc"
+#define STC_MANAGER_STATS_INTERFACE          STC_MANAGER_SERVICE ".statistics"
+#define STC_MANAGER_RESTRICTION_INTERFACE    STC_MANAGER_SERVICE ".restriction"
+#define STC_MANAGER_INTERFACE                STC_MANAGER_SERVICE ".manager"
+
+#define STC_MANAGER_STATS_PATH               "/net/stc/statistics"
+#define STC_MANAGER_RESTRICTION_PATH         "/net/stc/restriction"
+#define STC_MANAGER_PATH                     "/net/stc/manager"
+
+#define STC_MANAGER_METHOD_STATS_INIT                   "Init"
+#define STC_MANAGER_METHOD_STATS_GET_PER_APP_ID         "Get"
+#define STC_MANAGER_METHOD_STATS_GET_ALL                "GetAll"
+#define STC_MANAGER_METHOD_STATS_GET_TOTAL              "Get"
+#define STC_MANAGER_METHOD_STATS_RESET                  "Reset"
+
+#define STC_MANAGER_METHOD_RESTRICTION_SET              "Set"
+#define STC_MANAGER_METHOD_RESTRICTION_GET_STATUS       "GetState"
+#define STC_MANAGER_METHOD_RESTRICTION_GET_PER_APP_ID   "Get"
+#define STC_MANAGER_METHOD_RESTRICTION_GET_ALL          "GetAll"
+#define STC_MANAGER_METHOD_RESTRICTION_UNSET            "Unset"
+#define STC_MANAGER_METHOD_RESTRICTION_EXCLUDE          "Exclude"
+
+#define STC_MANAGER_METHOD_IPTABLES_COMMIT              "CommitIptables"
 
 #define STC_DEBUG_GDBUS_VARIANT(str, parameters) \
        do { \
index 65b39db2ca83dcb71e4348ffedbf6c31020ce3f5..75a03b5f3e1bb64a8b40e5094cbb4c2785914cdf 100755 (executable)
  *****************************************************************************/
 
 //LCOV_EXCL_START
+stc_error_e _stc_iptables_commit(const char *option,
+       int *err_num, char **err_str)
+{
+       GVariant *message = NULL;
+       GVariant *params = NULL;
+       stc_error_e error = STC_ERROR_NONE;
+       int result = 0;
+       int errnum = 0;
+       char *errstr = NULL;
+
+       params = g_variant_new("(s)", option);
+
+       message = _stc_dbus_invoke_method(
+               STC_MANAGER_SERVICE,
+               STC_MANAGER_PATH,
+               STC_MANAGER_INTERFACE,
+               STC_MANAGER_METHOD_IPTABLES_COMMIT,
+               params,
+               &error);
+
+       STC_RETURN_VAL_IF(message == NULL,
+               error, "Failed to invoke dbus method");
+
+       g_variant_get(message, "(iis)", &result, &errnum, &errstr);
+       STC_LOGI("Commit iptables result [%d:%d:%s]", result, errnum, errstr);
+       g_variant_unref(message);
+
+       if (err_num)
+               *err_num = errnum;
+       if (err_str)
+               *err_str = g_strdup(errstr);
+
+       if (result == STC_ERROR_NONE)
+               return STC_ERROR_NONE;
+       else
+               return STC_ERROR_OPERATION_FAILED;
+}
+
 EXPORT_API int stc_reset_stats(stc_h stc, stc_reset_rule_h rule)
 {
        int ret = STC_ERROR_NONE;
@@ -465,4 +503,31 @@ EXPORT_API int stc_get_restriction_status(stc_h stc, stc_restriction_rule_h rule
 
        return STC_ERROR_NONE;
 }
+
+EXPORT_API int stc_iptables_commit(stc_h stc, const char *option,
+               int *err_num, char **err_str)
+{
+       int ret = STC_ERROR_NONE;
+
+       CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC);
+
+       if (!(_stc_handle_check_validity(stc))) {
+               STC_LOGE("Invalid parameter");
+               return STC_ERROR_INVALID_PARAMETER;
+       }
+
+       if (option == NULL) {
+               STC_LOGE("Invalid parameter");
+               return STC_ERROR_INVALID_PARAMETER;
+       }
+
+       ret = _stc_iptables_commit(option, err_num, err_str);
+       if (ret != STC_ERROR_NONE) {
+               STC_LOGE("Failed to commit iptables [%s]",
+                       _stc_convert_error_type_to_string(ret));
+               return ret;
+       }
+
+       return STC_ERROR_NONE;
+}
 //LCOV_EXCL_STOP
index e2c487c4be099701ef94d23e2eeafe03e0a5dc86..fd9a8827d539ef4af1bf89919421b56198b4ee1c 100755 (executable)
@@ -18,6 +18,7 @@ SET(stc_test
        restriction.c
        statistics.c
        firewall.c
+       manager.c
        stc_menu.c
        stc_test.c
 )
diff --git a/test/manager.c b/test/manager.c
new file mode 100755 (executable)
index 0000000..01ccc00
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Smart Traffic Control (STC)
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * 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.
+ *
+ */
+
+/**
+ * This file declares definitions, macros and structures.
+ *
+ * @file        manager.c
+ * @author      Tak hyunuk (hyunuk.tak@samsung.com)
+ * @version     0.1
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <time.h>
+#include <assert.h>
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include <stc.h>
+#include <stc_internal.h>
+
+#include "stc_test.h"
+#include "stc_menu.h"
+
+static char g_iptables_option[MENU_DATA_SIZE] = "";
+
+extern stc_h g_stc;
+
+static int __test_stc_commit_iptables(MManager *mm, struct menu_data *menu)
+{
+       int ret = STC_ERROR_NONE;
+       int err_num = 0;
+       char *err_str = NULL;
+
+       ret = stc_iptables_commit(g_stc, g_iptables_option, &err_num, &err_str);
+
+       if (ret == STC_ERROR_NONE)
+               msg(LOG_GREEN "Success to commit iptables" LOG_END);
+       else
+               msg("Fail to commit iptables " LOG_RED "[%s:%d:%s]" LOG_END,
+                       test_stc_convert_error_type_to_string(ret), err_num, err_str);
+
+       FREE(err_str);
+
+       return ret;
+}
+
+static struct menu_data menu_iptables_commit[] = {
+       { "1", "Option", NULL, NULL, g_iptables_option},
+       { "c", LOG_LIGHTMAGENTA "[Commit]" LOG_END, NULL, __test_stc_commit_iptables, NULL},
+       { NULL, NULL, },
+};
+
+struct menu_data menu_iptables[] = {
+       { "1", LOG_LIGHTBLUE "[Commit]" LOG_END " iptables", menu_iptables_commit, NULL, NULL},
+       { "2", LOG_LIGHTMAGENTA "[Stop]" LOG_END " manager", NULL, NULL, NULL},
+       { NULL, NULL, },
+};
diff --git a/test/manager.h b/test/manager.h
new file mode 100755 (executable)
index 0000000..db361cb
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Smart Traffic Control (STC)
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * 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.
+ *
+ */
+
+/**
+ * This file declares definitions, macros and structures.
+ *
+ * @file        manager.h
+ * @author      Tak hyunuk (hyunuk.tak@samsung.com)
+ * @version     0.1
+ */
+
+#ifndef __TEST_STC_MANAGER_H__
+#define __TEST_STC_MANAGER_H__
+
+/*****************************************************************************
+ *  Local Functions Definition
+ *****************************************************************************/
+
+#endif /** __TEST_STC_MANAGER_H__ */
index ef3be400613f53ed7f9c7327d147efd394a1c16f..7e1c3ad5867a26b5fda93d21492ebda515042e89 100755 (executable)
@@ -41,6 +41,7 @@
 #include "statistics.h"
 #include "restriction.h"
 #include "firewall.h"
+#include "manager.h"
 
 #define STC_APP_ID_LEN 128
 #define STC_IFNAME_LEN 16
@@ -49,6 +50,7 @@
 extern struct menu_data menu_statistics[];
 extern struct menu_data menu_restriction[];
 extern struct menu_data menu_firewall[];
+extern struct menu_data menu_iptables[];
 
 stc_h g_stc = NULL;
 
@@ -345,6 +347,7 @@ static struct menu_data menu_main[] = {
        { "1", LOG_LIGHTBLUE "[Statistics]" LOG_END, menu_statistics, NULL, NULL},
        { "2", LOG_LIGHTMAGENTA "[Restriction]" LOG_END, menu_restriction, NULL, NULL},
        { "3", LOG_LIGHTMAGENTA "[Firewall]" LOG_END, menu_firewall, NULL, NULL},
+       { "4", LOG_LIGHTMAGENTA "[Iptables]" LOG_END, menu_iptables, NULL, NULL},
        { NULL, NULL, },
 };