From: hyunuktak Date: Thu, 12 Apr 2018 06:08:11 +0000 (+0900) Subject: Added a new API to commit iptables X-Git-Tag: submit/tizen/20180416.224349^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=aab5fca8a0b3acb2c6490af43caf022a935e7f37;p=platform%2Fcore%2Fapi%2Fsmart-traffic-control.git Added a new API to commit iptables Change-Id: I43a60c35805c9d5994b3ba4b51aa6e1a4f560b4d Signed-off-by: hyunuktak --- diff --git a/include/stc_internal.h b/include/stc_internal.h index 9f4a552..3923abc 100755 --- a/include/stc_internal.h +++ b/include/stc_internal.h @@ -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 diff --git a/packaging/capi-network-stc.spec b/packaging/capi-network-stc.spec index 5767c55..412ef1d 100755 --- a/packaging/capi-network-stc.spec +++ b/packaging/capi-network-stc.spec @@ -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 diff --git a/src/internal/include/stc-dbus.h b/src/internal/include/stc-dbus.h index 67eeb2f..293badc 100755 --- a/src/internal/include/stc-dbus.h +++ b/src/internal/include/stc-dbus.h @@ -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 { \ diff --git a/src/stc-manager.c b/src/stc-manager.c index 65b39db..75a03b5 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -65,6 +65,44 @@ *****************************************************************************/ //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 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e2c487c..fd9a882 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 index 0000000..01ccc00 --- /dev/null +++ b/test/manager.c @@ -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 +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#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 index 0000000..db361cb --- /dev/null +++ b/test/manager.h @@ -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__ */ diff --git a/test/stc_test.c b/test/stc_test.c index ef3be40..7e1c3ad 100755 --- a/test/stc_test.c +++ b/test/stc_test.c @@ -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, }, };