From 1a6e01b4598a3b6ff27e0c4e4e777abd765dc971 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 20 Nov 2018 17:43:21 +0900 Subject: [PATCH] Add packet capture APIs Change-Id: Iba2b3aedc0901fe6981ca515f944b72d3caa1cc3 Signed-off-by: hyunuktak --- include/stc_internal.h | 1 + include/stc_pcap_internal.h | 213 ++++++++++++++++++ packaging/capi-network-stc.spec | 2 +- src/include/stc-pcap.h | 77 +++++++ src/stc-pcap.c | 371 ++++++++++++++++++++++++++++++++ test/CMakeLists.txt | 1 + test/pcap.c | 164 ++++++++++++++ test/pcap.h | 37 ++++ test/stc_test.c | 5 + 9 files changed, 870 insertions(+), 1 deletion(-) create mode 100755 src/include/stc-pcap.h create mode 100755 src/stc-pcap.c create mode 100755 test/pcap.c create mode 100755 test/pcap.h diff --git a/include/stc_internal.h b/include/stc_internal.h index a25e757..5649af9 100755 --- a/include/stc_internal.h +++ b/include/stc_internal.h @@ -43,6 +43,7 @@ extern "C" { #include "stc_rstn_internal.h" #include "stc_fw_internal.h" #include "stc_ipt_internal.h" +#include "stc_pcap_internal.h" /***************************************************************************** * Macros and Typedefs diff --git a/include/stc_pcap_internal.h b/include/stc_pcap_internal.h index 5431a2a..a58f95a 100755 --- a/include/stc_pcap_internal.h +++ b/include/stc_pcap_internal.h @@ -27,11 +27,29 @@ extern "C" { /***************************************************************************** * Standard headers *****************************************************************************/ +#include +#include +#include /***************************************************************************** * Macros and Typedefs *****************************************************************************/ +/** + * @addtogroup CAPI_NETWORK_STC_FIREWALL_MODULE + * @{ + */ + +/** + * @brief The pcap handle. + * @since_tizen 5.0 + */ +typedef void *stc_pcap_h; + +/** +* @} +*/ + /***************************************************************************** * Enumeration *****************************************************************************/ @@ -44,6 +62,201 @@ extern "C" { * Core API functions *****************************************************************************/ +/** + * @addtogroup CAPI_NETWORK_STC_PCAP_MODULE + * @{ + */ + +/** + * @brief Creates the pcap handle. + * @since_tizen 5.0 + * @remarks You must release @a handle using stc_pcap_destroy(). + * + * @param[in] stc The stc handle + * @param[out] pcap_h The pcap handle + * + * @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_h + * @see #stc_pcap_h + * @see stc_initialize() + * @see stc_pcap_destroy() + */ +int stc_pcap_create(stc_h stc, stc_pcap_h *pcap_h); + +/** + * @brief Destroys the pcap handle. + * @since_tizen 5.0 + * + * @param[in] pcap_h The pcap handle + * + * @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_pcap_h + * @see stc_pcap_create() + */ +int stc_pcap_destroy(stc_pcap_h pcap_h); + +/** + * @brief Starts packet capture. + * @since_tizen 5.0 + * + * @param[in] pcap_h The pcap handle + * + * @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_pcap_h + * @see stc_pcap_create() + * @see stc_pcap_destroy() + */ +int stc_pcap_start(stc_pcap_h pcap_h); + +/** + * @brief Stops packet capture. + * @since_tizen 5.0 + * + * @param[in] pcap_h The pcap handle + * + * @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_pcap_h + * @see stc_pcap_create() + * @see stc_pcap_destroy() + */ +int stc_pcap_stop(stc_pcap_h pcap_h); + +/** + * @brief Sets interface name to pcap. + * @since_tizen 5.0 + * + * @param[in] pcap_h The pcap handle + * @param[in] ifname The interface name + * + * @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_pcap_h + * @see stc_pcap_create() + * @see stc_pcap_destroy() + * @see stc_pcap_get_ifname() + */ +int stc_pcap_set_ifname(stc_pcap_h pcap_h, char *ifname); + +/** + * @brief Sets group of nflog to pcap. + * @since_tizen 5.0 + * + * @param[in] pcap_h The pcap handle + * @param[in] group The group of nflog + * + * @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_pcap_h + * @see stc_pcap_create() + * @see stc_pcap_destroy() + * @see stc_pcap_get_nflog_group() + */ +int stc_pcap_set_nflog_group(stc_pcap_h pcap_h, int group); + +/** + * @brief Gets interface name to pcap. + * @since_tizen 5.0 + * + * @param[in] pcap_h The pcap handle + * @param[out] ifname The interface name + * + * @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_pcap_h + * @see stc_pcap_create() + * @see stc_pcap_destroy() + * @see stc_pcap_set_ifname() + */ +int stc_pcap_get_ifname(stc_pcap_h pcap_h, char **ifname); + +/** + * @brief Gets group of nflog to pcap. + * @since_tizen 5.0 + * + * @param[in] pcap_h The pcap handle + * @param[out] group The group of nflog + * + * @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_pcap_h + * @see stc_pcap_create() + * @see stc_pcap_destroy() + * @see stc_pcap_set_nflog_group() + */ +int stc_pcap_get_nflog_group(stc_pcap_h pcap_h, int *group); + +/** +* @} +*/ + #ifdef __cplusplus } #endif diff --git a/packaging/capi-network-stc.spec b/packaging/capi-network-stc.spec index ff56639..5f369af 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.34 +Version: 0.0.35 Release: 1 Group: Network & Connectivity/API License: Apache-2.0 diff --git a/src/include/stc-pcap.h b/src/include/stc-pcap.h new file mode 100755 index 0000000..b8631ac --- /dev/null +++ b/src/include/stc-pcap.h @@ -0,0 +1,77 @@ +/* + * 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 stc-pcap.h + * @author Tak hyunuk (hyunuk.tak@samsung.com) + * @version 0.1 + */ + +#ifndef __TIZEN_STC_PCAP_H__ +#define __TIZEN_STC_PCAP_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/***************************************************************************** + * Standard headers + *****************************************************************************/ + +/***************************************************************************** + * System headers + *****************************************************************************/ + +/***************************************************************************** + * Smart Traffic Control (STC) library headers + *****************************************************************************/ + +/***************************************************************************** + * Macros and Typedefs + *****************************************************************************/ + +#define STC_MANAGER_PCAP_PATH "/net/stc/pcap" +#define STC_MANAGER_PCAP_INTERFACE STC_MANAGER_SERVICE ".pcap" + +#define STC_MANAGER_PCAP_METHOD_START "Start" +#define STC_MANAGER_PCAP_METHOD_STOP "Stop" + +#define STC_PCAP_IFNAME "ifname" +#define STC_PCAP_NFLOG_GROUP "nflog_group" + +typedef struct { + char *ifname; + int nflog_group; +} stc_pcap_s; + +/***************************************************************************** + * Global Variables + *****************************************************************************/ + +/***************************************************************************** + * Local Functions Definition + *****************************************************************************/ + +#ifdef __cplusplus +} +#endif + +#endif /** __TIZEN_STC_PCAP_H__ */ diff --git a/src/stc-pcap.c b/src/stc-pcap.c new file mode 100755 index 0000000..8800ea7 --- /dev/null +++ b/src/stc-pcap.c @@ -0,0 +1,371 @@ +/* + * 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 implements Smart Traffic Control (STC) user library. + * + * @file stc-pcap.c + * @author Tak hyunuk (hyunuk.tak@samsung.com) + * @version 0.1 + */ + +/***************************************************************************** + * Standard headers + *****************************************************************************/ +#include +#include + +/***************************************************************************** + * System headers + *****************************************************************************/ + +/***************************************************************************** + * Smart Traffic Control (STC) library headers + *****************************************************************************/ +#include "stc.h" + +#include "stc-private.h" + +#include "stc-dbus.h" +#include "stc-log.h" +#include "stc-util.h" + +#include "stc-pcap.h" + +/***************************************************************************** + * Macros and Typedefs + *****************************************************************************/ + +/***************************************************************************** + * Global Variables + *****************************************************************************/ + +static GSList *g_pcap_list = NULL; + +/***************************************************************************** + * Local Functions Definition + *****************************************************************************/ + +//LCOV_EXCL_START +static void __stc_pcap_init(void *data) +{ + stc_pcap_s *pcap = (stc_pcap_s *)data; + + memset(pcap, 0, sizeof(stc_pcap_s)); +} + +static void __stc_pcap_deinit(void *data) +{ + stc_pcap_s *pcap = (stc_pcap_s *)data; + + FREE(pcap->ifname); + FREE(pcap); +} + +static void __stc_pcap_add_to_list(stc_pcap_h pcap_h) +{ + g_pcap_list = g_slist_append(g_pcap_list, pcap_h); +} + +static bool __stc_pcap_check_validity(stc_pcap_h pcap_h) +{ + if (!pcap_h) + return false; + + if (g_slist_find(g_pcap_list, pcap_h)) + return true; + else + return false; +} + +static bool __stc_pcap_check_param(stc_pcap_h pcap_h) +{ + stc_pcap_s *pcap = (stc_pcap_s *)pcap_h; + + if (pcap->ifname == NULL || + pcap->ifname[0] == '\0') + return false; + + if (pcap->nflog_group < 0) + return false; + + return true; +} + +static void __stc_pcap_remove_from_list(stc_pcap_h pcap_h) +{ + g_pcap_list = g_slist_remove(g_pcap_list, pcap_h); +} + +static void __stc_pcap_make_params( + stc_pcap_s *pcap, GVariant **params) +{ + GVariantBuilder *builder; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + + g_variant_builder_add(builder, "{sv}", + STC_PCAP_IFNAME, + g_variant_new_string(pcap->ifname)); + + g_variant_builder_add(builder, "{sv}", + STC_PCAP_NFLOG_GROUP, + g_variant_new_uint32(pcap->nflog_group)); + + *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder)); + g_variant_builder_unref(builder); +} + + +stc_error_e _stc_pcap_start(stc_pcap_s *pcap) +{ + GVariant *message = NULL; + GVariant *params = NULL; + stc_error_e error = STC_ERROR_NONE; + int result = 0; + + __stc_pcap_make_params(pcap, ¶ms); + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_PCAP_PATH, + STC_MANAGER_PCAP_INTERFACE, + STC_MANAGER_PCAP_METHOD_START, + params, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(i)", &result); + STC_LOGI("Successfully start pcap [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +stc_error_e _stc_pcap_stop(stc_pcap_s *pcap) +{ + GVariant *message = NULL; + GVariant *params = NULL; + stc_error_e error = STC_ERROR_NONE; + int result = 0; + + __stc_pcap_make_params(pcap, ¶ms); + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_PCAP_PATH, + STC_MANAGER_PCAP_INTERFACE, + STC_MANAGER_PCAP_METHOD_STOP, + params, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(i)", &result); + STC_LOGI("Successfully stop pcap [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_pcap_create(stc_h stc, stc_pcap_h *pcap_h) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC); + + if (!(_stc_handle_check_validity(stc))) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!pcap_h) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_pcap_s *pcap = MALLOC0(stc_pcap_s, 1); + if (!pcap) { + STC_LOGE("Memory allocation failed"); + return STC_ERROR_OUT_OF_MEMORY; + } + + __stc_pcap_init(pcap); + __stc_pcap_add_to_list((stc_pcap_h)pcap); + + *pcap_h = (stc_pcap_h)pcap; + + STC_LOGI("Successfully created pcap handle"); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_pcap_destroy(stc_pcap_h pcap_h) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC); + + if (__stc_pcap_check_validity(pcap_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + __stc_pcap_remove_from_list(pcap_h); + __stc_pcap_deinit(pcap_h); + + STC_LOGI("Successfully destroyed pcap handle"); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_pcap_start(stc_pcap_h pcap_h) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC); + + if (__stc_pcap_check_validity(pcap_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (__stc_pcap_check_param(pcap_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_pcap_start((stc_pcap_s *)pcap_h); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to start pcap [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_pcap_stop(stc_pcap_h pcap_h) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC); + + if (__stc_pcap_check_validity(pcap_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (__stc_pcap_check_param(pcap_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_pcap_stop((stc_pcap_s *)pcap_h); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to stop pcap [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_pcap_set_ifname(stc_pcap_h pcap_h, char *ifname) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC); + + if (__stc_pcap_check_validity(pcap_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_pcap_s *pcap = (stc_pcap_s *)pcap_h; + + FREE(pcap->ifname); + pcap->ifname = g_strdup(ifname); + + STC_LOGI("Interface name [%s]", pcap->ifname); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_pcap_set_nflog_group(stc_pcap_h pcap_h, int group) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC); + + if (__stc_pcap_check_validity(pcap_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_pcap_s *pcap = (stc_pcap_s *)pcap_h; + + pcap->nflog_group = group; + + STC_LOGI("Nflog group [%d]", pcap->nflog_group); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_pcap_get_ifname(stc_pcap_h pcap_h, char **ifname) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC); + + if (__stc_pcap_check_validity(pcap_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!ifname) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_pcap_s *pcap = (stc_pcap_s *)pcap_h; + + *ifname = g_strdup(pcap->ifname); + + STC_LOGI("Interface name [%s]", *ifname); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_pcap_get_nflog_group(stc_pcap_h pcap_h, int *group) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC); + + if (__stc_pcap_check_validity(pcap_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!group) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_pcap_s *pcap = (stc_pcap_s *)pcap_h; + + *group = pcap->nflog_group; + + STC_LOGI("Nflog group [%d]", *group); + + return STC_ERROR_NONE; +} +//LCOV_EXCL_STOP diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fd9a882..0a231d1 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -18,6 +18,7 @@ SET(stc_test restriction.c statistics.c firewall.c + pcap.c manager.c stc_menu.c stc_test.c diff --git a/test/pcap.c b/test/pcap.c new file mode 100755 index 0000000..d10f0ad --- /dev/null +++ b/test/pcap.c @@ -0,0 +1,164 @@ +/* + * 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 pcap.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_ifname[MENU_DATA_SIZE] = "seth_w0"; +static char g_nflog_group[MENU_DATA_SIZE] = "3"; + +extern stc_h g_stc; +static stc_pcap_h g_pcap_h = NULL; + +static int __test_stc_set_pcap_info(MManager *mm, struct menu_data *menu) +{ + int nflog_group = (int)strtol(g_nflog_group, NULL, 10); + char *ifname = NULL; + int ret = STC_ERROR_NONE; + + ret = stc_pcap_set_ifname(g_pcap_h, g_ifname); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set interface name" LOG_END); + else { + msg("Fail to set interface name " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + return ret; + } + + ret = stc_pcap_set_nflog_group(g_pcap_h, nflog_group); + if (ret == STC_ERROR_NONE) { + msg(LOG_GREEN "Success to set nflog group" LOG_END); + } else { + msg("Fail to set nflog group " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + return ret; + } + + msg(HR_SINGLE); + + ret = stc_pcap_get_ifname(g_pcap_h, &ifname); + if (ret == STC_ERROR_NONE) { + msg("Ifname: " LOG_CYAN "[%s]" LOG_END, ifname); + g_free(ifname); + } + + ret = stc_pcap_get_nflog_group(g_pcap_h, &nflog_group); + if (ret == STC_ERROR_NONE) + msg("Nflog group: " LOG_CYAN "[%d]" LOG_END, nflog_group); + + msg(HR_SINGLE); + + return STC_ERROR_NONE; +} + +static int __test_stc_pcap_start(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + + ret = stc_pcap_start(g_pcap_h); + + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to start pcap" LOG_END); + else + msg("Fail to start pcap " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static int __test_stc_pcap_stop(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + + ret = stc_pcap_stop(g_pcap_h); + + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to stop pcap" LOG_END); + else + msg("Fail to stop pcap " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +int test_stc_pcap_create(void) +{ + int ret = stc_pcap_create(g_stc, &g_pcap_h); + + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to create pcap handle" LOG_END); + else + msg("Fail to create pcap handle " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +int test_stc_pcap_destroy(void) +{ + int ret = STC_ERROR_NONE; + + if (g_pcap_h) { + ret = stc_pcap_destroy(g_pcap_h); + + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to destroy pcap handle" LOG_END); + else + msg("Fail to destroy pcap handle " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + return ret; +} + +static struct menu_data menu_pcap_info[] = { + { "1", "Interface name", NULL, NULL, g_ifname}, + { "2", "Nflog group", NULL, NULL, g_nflog_group}, + { "s", LOG_LIGHTMAGENTA "[Set]" LOG_END, NULL, __test_stc_set_pcap_info, NULL}, + { NULL, NULL, }, +}; + +struct menu_data menu_pcap[] = { + { "1", LOG_LIGHTMAGENTA "[Set]" LOG_END " pcap info", menu_pcap_info, NULL, NULL}, + { "2", LOG_LIGHTMAGENTA "[Start]" LOG_END " pcap", NULL, __test_stc_pcap_start, NULL}, + { "3", LOG_LIGHTMAGENTA "[Stop]" LOG_END " pcap", NULL, __test_stc_pcap_stop, NULL}, + { NULL, NULL, }, +}; diff --git a/test/pcap.h b/test/pcap.h new file mode 100755 index 0000000..b8ab8ba --- /dev/null +++ b/test/pcap.h @@ -0,0 +1,37 @@ +/* + * 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 pcap.h + * @author Tak hyunuk (hyunuk.tak@samsung.com) + * @version 0.1 + */ + +#ifndef __TEST_STC_PCAP_H__ +#define __TEST_STC_PCAP_H__ + +/***************************************************************************** + * Local Functions Definition + *****************************************************************************/ +int test_stc_pcap_create(void); +int test_stc_pcap_destroy(void); + +#endif /** __TEST_STC_PCAP_H__ */ diff --git a/test/stc_test.c b/test/stc_test.c index 54f01bf..dae1b37 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 "pcap.h" #include "manager.h" #define STC_APP_ID_LEN 128 @@ -50,6 +51,7 @@ extern struct menu_data menu_statistics[]; extern struct menu_data menu_restriction[]; extern struct menu_data menu_firewall[]; +extern struct menu_data menu_pcap[]; extern struct menu_data menu_iptables[]; stc_h g_stc = NULL; @@ -344,6 +346,7 @@ static struct menu_data menu_main[] = { { "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}, + { "5", LOG_LIGHTMAGENTA "[Pcap]" LOG_END, menu_pcap, NULL, NULL}, { NULL, NULL, }, }; @@ -369,6 +372,7 @@ static int __test_stc_initialize(MManager *mm, struct menu_data *menu) test_stc_stats_rule_create(); test_stc_restriction_rule_create(); test_stc_restriction_register_cb(); + test_stc_pcap_create(); menu_manager_set_user_data(mm, g_stc); @@ -383,6 +387,7 @@ static int __test_stc_deinitialize(void) test_stc_stats_rule_destroy(); test_stc_restriction_rule_destroy(); test_stc_restriction_unregister_cb(); + test_stc_pcap_destroy(); if (g_stc) { ret = stc_deinitialize(g_stc); -- 2.34.1