From: hyunuktak Date: Thu, 5 Apr 2018 06:25:56 +0000 (+0900) Subject: Added firewall feature X-Git-Tag: submit/tizen/20180416.224349~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ac34a08fa96ac2c270920ff71afa489d5f83b031;p=platform%2Fcore%2Fapi%2Fsmart-traffic-control.git Added firewall feature Change-Id: Ide1848c670d33f445050c1adba71ce35fae34f1a Signed-off-by: hyunuktak --- diff --git a/include/stc_internal.h b/include/stc_internal.h index d529a4d..9f4a552 100755 --- a/include/stc_internal.h +++ b/include/stc_internal.h @@ -51,6 +51,27 @@ extern "C" { typedef void *stc_reset_rule_h; +/** +* @} +*/ + +/** + * @addtogroup CAPI_NETWORK_STC_FIREWALL_MODULE + * @{ + */ + +/** + * @brief The firewall chain handle. + * @since_tizen 5.0 + */ +typedef void *stc_firewall_chain_h; + +/** + * @brief The firewall rule handle. + * @since_tizen 5.0 + */ +typedef void *stc_firewall_rule_h; + /** * @} */ @@ -102,6 +123,96 @@ typedef enum { * @} */ +/** + * @addtogroup CAPI_NETWORK_STC_FIREWALL_MODULE + * @{ + */ + +typedef enum { + STC_FIREWALL_UNKNOWN, + STC_FIREWALL_UNLOCKED, + STC_FIREWALL_LOCKED +} stc_firewall_lock_e; + +/** + * @brief Enumeration for firewall direction. + * @since_tizen 5.0 + */ +typedef enum { + STC_FIREWALL_DIRECTION_NONE, + STC_FIREWALL_DIRECTION_IN, + STC_FIREWALL_DIRECTION_OUT +} stc_firewall_direction_e; + +/** + * @brief Enumeration for firewall IP type. + * @since_tizen 5.0 + */ +typedef enum { + STC_FIREWALL_IP_NONE, + STC_FIREWALL_IP_SINGLE, + STC_FIREWALL_IP_MASK, + STC_FIREWALL_IP_RANGE +} stc_firewall_ip_type_e; + +/** + * @brief Enumeration for firewall port type. + * @since_tizen 5.0 + */ +typedef enum { + STC_FIREWALL_PORT_NONE, + STC_FIREWALL_PORT_SINGLE, + STC_FIREWALL_PORT_RANGE +} stc_firewall_port_type_e; + +/** + * @brief Enumeration for firewall protocol type. + * @since_tizen 5.0 + */ +typedef enum { + STC_FIREWALL_PROTOCOL_NONE, + STC_FIREWALL_PROTOCOL_TCP, + STC_FIREWALL_PROTOCOL_UDP, + STC_FIREWALL_PROTOCOL_ICMP, + STC_FIREWALL_PROTOCOL_ALL, +} stc_firewall_protocol_type_e; + +/** + * @brief Enumeration for firewall family type. + * @since_tizen 5.0 + */ +typedef enum { + STC_FIREWALL_FAMILY_NONE, + STC_FIREWALL_FAMILY_V4, + STC_FIREWALL_FAMILY_V6 +} stc_firewall_family_type_e; + +/** + * @brief Enumeration for firewall chain target. + * @since_tizen 5.0 + */ +typedef enum { + STC_FIREWALL_CHAIN_TARGET_NONE, + STC_FIREWALL_CHAIN_TARGET_INPUT, + STC_FIREWALL_CHAIN_TARGET_OUTPUT +} stc_firewall_chain_target_e; + +/** + * @brief Enumeration for firewall rule target. + * @since_tizen 5.0 + */ +typedef enum { + STC_FIREWALL_RULE_TARGET_NONE, + STC_FIREWALL_RULE_TARGET_ACCEPT, + STC_FIREWALL_RULE_TARGET_DROP, + STC_FIREWALL_RULE_TARGET_LOG, +} stc_firewall_rule_target_e; + +/** +* @} +*/ + + /***************************************************************************** * Callback functions *****************************************************************************/ @@ -144,6 +255,61 @@ typedef void (*stc_threshold_crossed_cb)(stc_restriction_info_h info, * @} */ +/** + * @addtogroup CAPI_NETWORK_STC_FIREWALL_MODULE + * @{ + */ + +/** + * @brief Called for enumerate firewall chain information. + * @since_tizen 5.0 + * + * @param[in] info The firewall chain handle + * @param[in] user_data The user data passed from the callback firewall chain function + * @return @c STC_CALLBACK_CONTINUE to continue with the next iteration of the loop, + * otherwise @c STC_CALLBACK_CANCEL to break out of the loop + * + * @pre stc_firewall_chain_foreach() will invoke this callback. + * @see stc_firewall_chain_get_name() + * @see stc_firewall_chain_get_target() + * @see stc_firewall_chain_get_priority() + * @see stc_firewall_chain_foreach() + */ +typedef stc_callback_ret_e (*stc_firewall_chain_info_cb)(stc_firewall_chain_h info_h, + void *user_data); + +/** + * @brief Called for enumerate firewall rule information. + * @since_tizen 5.0 + * + * @param[in] info The firewall rule handle + * @param[in] user_data The user data passed from the callback firewall rule function + * @return @c STC_CALLBACK_CONTINUE to continue with the next iteration of the loop, + * otherwise @c STC_CALLBACK_CANCEL to break out of the loop + * + * @pre stc_firewall_rule_foreach() will invoke this callback. + * @see stc_firewall_rule_get_chain() + * @see stc_firewall_rule_get_direction() + * @see stc_firewall_rule_get_src_ip_type() + * @see stc_firewall_rule_get_dst_ip_type() + * @see stc_firewall_rule_get_src_port_type() + * @see stc_firewall_rule_get_dst_port_type() + * @see stc_firewall_rule_get_protocol_type() + * @see stc_firewall_rule_get_family_type() + * @see stc_firewall_rule_get_src_ip() + * @see stc_firewall_rule_get_dst_ip() + * @see stc_firewall_rule_get_src_port() + * @see stc_firewall_rule_get_dst_port() + * @see stc_firewall_rule_get_ifname() + * @see stc_firewall_rule_get_target() + * @see stc_firewall_rule_foreach() + */ +typedef stc_callback_ret_e (*stc_firewall_rule_info_cb)(stc_firewall_rule_h info, + void *user_data); + +/** +* @} +*/ /***************************************************************************** * Core API functions @@ -1440,6 +1606,1270 @@ int stc_restriction_info_get_roaming_type(stc_restriction_info_h info, * @} */ +/** + * @addtogroup CAPI_NETWORK_STC_FIREWALL_MODULE + * @{ + */ + +/** + * @brief Locks the firewall. + * @since_tizen 5.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/firewall.admin + * + * @param[in] stc The stc 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_initialize() + * @see stc_firewall_unlock() + * @see stc_firewall_get_lock() + */ +int stc_firewall_lock(stc_h stc); + +/** + * @brief Unlocks the firewall. + * @since_tizen 5.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/firewall.admin + * + * @param[in] stc The stc 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_initialize() + * @see stc_firewall_lock() + * @see stc_firewall_get_lock() + */ +int stc_firewall_unlock(stc_h stc); + +/** + * @brief Gets the firewall lock state. + * @since_tizen 5.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/firewall.admin + * + * @param[in] stc The stc handle + * @param[out] state The firewall lock state + * + * @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_initialize() + * @see stc_firewall_lock() + * @see stc_firewall_unlock() + */ +int stc_firewall_get_lock(stc_h stc, stc_firewall_lock_e *state); + +/** + * @brief Creates the firewall chain handle. + * @since_tizen 5.0 + * @remarks You must release @a handle using stc_firewall_chain_destroy(). + * + * @param[in] stc The stc handle + * @param[in] name The chain name + * @param[out] chain The firewall chain 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_firewall_chain_h + * @see stc_initialize() + * @see stc_firewall_chain_destroy() + */ +int stc_firewall_chain_create(stc_h stc, const char *name, + stc_firewall_chain_h *chain_h); + +/** + * @brief Clones the firewall chain handle. + * @since_tizen 5.0 + * @remarks You must release @a handle using stc_firewall_chain_destroy(). + * + * @param[in] origin The origin firewall chain handle + * @param[out] cloned_chain The cloned firewall chain 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_firewall_chain_h + * @see stc_initialize() + * @see stc_firewall_chain_create() + * @see stc_firewall_chain_destroy() + */ +int stc_firewall_chain_clone(stc_firewall_chain_h origin_h, + stc_firewall_chain_h *cloned_chain_h); + +/** + * @brief Destroys the firewall chain handle. + * @since_tizen 5.0 + * + * @param[in] chain The firewall chain 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_firewall_chain_h + * @see stc_firewall_chain_create() + */ +int stc_firewall_chain_destroy(stc_firewall_chain_h chain_h); + +/** + * @brief Gets the name for chain. + * @since_tizen 5.0 + * + * @param[in] chain The firewall chain handle + * @param[out] name The chain 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_firewall_chain_h + * @see stc_firewall_chain_create() + * @see stc_firewall_chain_destroy() + */ +int stc_firewall_chain_get_name(stc_firewall_chain_h chain, char **name); + +/** + * @brief Gets the target type for chain. + * @since_tizen 5.0 + * + * @param[in] chain The firewall chain handle + * @param[out] target The target type + * + * @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_firewall_chain_h + * @see stc_firewall_chain_create() + * @see stc_firewall_chain_destroy() + */ +int stc_firewall_chain_get_target(stc_firewall_chain_h chain_h, + stc_firewall_chain_target_e *target); + +/** + * @brief Gets the priority for chain. + * @since_tizen 5.0 + * + * @param[in] chain The firewall chain handle + * @param[out] priority The priority + * + * @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_firewall_chain_h + * @see stc_firewall_chain_create() + * @see stc_firewall_chain_destroy() + */ +int stc_firewall_chain_get_priority(stc_firewall_chain_h chain_h, + unsigned int *priority); + +/** + * @brief Adds the chain. + * @since_tizen 5.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/firewall.common + * + * @param[in] chain The firewall chain 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_firewall_chain_h + * @see stc_firewall_chain_create() + * @see stc_firewall_chain_destroy() + */ +int stc_firewall_chain_add(stc_firewall_chain_h chain_h); + +/** + * @brief Removes the chain. + * @since_tizen 5.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/firewall.common + * + * @param[in] chain The firewall chain 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_firewall_chain_h + * @see stc_firewall_chain_create() + * @see stc_firewall_chain_destroy() + */ +int stc_firewall_chain_remove(stc_firewall_chain_h chain_h); + +/** + * @brief Flushs the chain. + * @since_tizen 5.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/firewall.common + * + * @param[in] chain The firewall chain 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_firewall_chain_h + * @see stc_firewall_chain_create() + * @see stc_firewall_chain_destroy() + */ +int stc_firewall_chain_flush(stc_firewall_chain_h chain_h); + +/** + * @brief Gets all chains. + * @since_tizen 5.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/firewall.common + * + * @param[in] stc The stc handle + * @param[in] callback The callback to be called + * @param[in] user_data The user data passed to the callback function + * + * @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_firewall_chain_h + * @see stc_firewall_chain_create() + * @see stc_firewall_chain_destroy() + */ +int stc_firewall_chain_foreach(stc_h stc, + stc_firewall_chain_info_cb callback, void *user_data); + +/** + * @brief Sets the chain to target. + * @since_tizen 5.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/firewall.common + * + * @param[in] chain The firewall chain handle + * @param[in] target The target type + * + * @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_firewall_chain_h + * @see stc_firewall_chain_create() + * @see stc_firewall_chain_destroy() + */ +int stc_firewall_chain_set(stc_firewall_chain_h chain_h, + stc_firewall_chain_target_e target); + +/** + * @brief Unsets the chain. + * @since_tizen 5.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/firewall.common + * + * @param[in] chain The firewall chain 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_firewall_chain_h + * @see stc_firewall_chain_create() + * @see stc_firewall_chain_destroy() + */ +int stc_firewall_chain_unset(stc_firewall_chain_h chain_h); + +/** + * @brief Creates the firewall rule handle. + * @since_tizen 5.0 + * @remarks You must release @a handle using stc_firewall_rule_destroy(). + * + * @param[in] stc The stc handle + * @param[out] rule The firewall rule 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_firewall_rule_h + * @see stc_initialize() + * @see stc_firewall_rule_destroy() + */ +int stc_firewall_rule_create(stc_h stc, stc_firewall_rule_h *rule_h); + +/** + * @brief Clones the firewall rule handle. + * @since_tizen 5.0 + * @remarks You must release @a handle using stc_firewall_rule_destroy(). + * + * @param[in] origin The origin firewall rule handle + * @param[out] cloned_rule The cloned firewall rule 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_firewall_rule_h + * @see stc_initialize() + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + */ +int stc_firewall_rule_clone(stc_firewall_rule_h origin_h, + stc_firewall_rule_h *cloned_rule_h); + +/** + * @brief Destroys the firewall rule handle. + * @since_tizen 5.0 + * + * @param[in] rule The firewall chain 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_firewall_rule_h + * @see stc_firewall_rule_create() + */ +int stc_firewall_rule_destroy(stc_firewall_rule_h rule_h); + +/** + * @brief Sets the chain name to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[in] chain The chain 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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_chain() + */ +int stc_firewall_rule_set_chain(stc_firewall_rule_h rule_h, char *chain); + +/** + * @brief Sets the direction to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[in] direction The direction + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_direction() + */ +int stc_firewall_rule_set_direction(stc_firewall_rule_h rule_h, + stc_firewall_direction_e direction); + +/** + * @brief Sets the source IP type to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[in] type The IP type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_src_ip_type() + */ +int stc_firewall_rule_set_src_ip_type(stc_firewall_rule_h rule_h, + stc_firewall_ip_type_e type); + +/** + * @brief Sets the destination IP type to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[in] type The IP type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_dst_ip_type() + */ +int stc_firewall_rule_set_dst_ip_type(stc_firewall_rule_h rule_h, + stc_firewall_ip_type_e type); + +/** + * @brief Sets the source port type to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[in] type The port type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_src_port_type() + */ +int stc_firewall_rule_set_src_port_type(stc_firewall_rule_h rule_h, + stc_firewall_port_type_e type); + +/** + * @brief Sets the destination port type to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[in] type The port type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_dst_port_type() + */ +int stc_firewall_rule_set_dst_port_type(stc_firewall_rule_h rule_h, + stc_firewall_port_type_e type); + +/** + * @brief Sets the protocol type to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[in] type The protocol type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_protocol_type() + */ +int stc_firewall_rule_set_protocol_type(stc_firewall_rule_h rule_h, + stc_firewall_protocol_type_e type); + +/** + * @brief Sets the address family type to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[in] type The address family type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_family_type() + */ +int stc_firewall_rule_set_family_type(stc_firewall_rule_h rule_h, + stc_firewall_family_type_e type); + +/** + * @brief Sets source IP addresses to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[in] ip1 The address 1 + * @param[in] ip2 The address 2 which will be IP address or mask + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_src_ip_type() + * @see stc_firewall_rule_get_src_ip() + */ +int stc_firewall_rule_set_src_ip(stc_firewall_rule_h rule_h, + char *ip1, char *ip2); + +/** + * @brief Sets destination IP addresses to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[in] ip1 The address 1 + * @param[in] ip2 The address 2 which will be IP address or mask + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_dst_ip_type() + * @see stc_firewall_rule_get_dst_ip() + */ +int stc_firewall_rule_set_dst_ip(stc_firewall_rule_h rule_h, + char *ip1, char *ip2); + +/** + * @brief Sets source ports to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[in] port1 The port 1 + * @param[in] port2 The port 2 + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_src_port_type() + * @see stc_firewall_rule_get_src_port() + */ +int stc_firewall_rule_set_src_port(stc_firewall_rule_h rule_h, + unsigned int port1, unsigned int port2); + +/** + * @brief Sets destination ports to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[in] port1 The port 1 + * @param[in] port2 The port 2 + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_dst_port_type() + * @see stc_firewall_rule_get_dst_port() + */ +int stc_firewall_rule_set_dst_port(stc_firewall_rule_h rule_h, + unsigned int port1, unsigned int port2); + +/** + * @brief Sets interface name to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule 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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_ifname() + */ +int stc_firewall_rule_set_ifname(stc_firewall_rule_h rule_h, char *ifname); + +/** + * @brief Sets target type to rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[in] target The target type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_target() + */ +int stc_firewall_rule_set_target(stc_firewall_rule_h rule_h, + stc_firewall_rule_target_e target); + +/** + * @brief Gets the chain name from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[out] chain The chain 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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_chain() + */ +int stc_firewall_rule_get_chain(stc_firewall_rule_h rule_h, char **chain); + +/** + * @brief Gets the direction from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[out] direction The direction + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_direction() + */ +int stc_firewall_rule_get_direction(stc_firewall_rule_h rule_h, + stc_firewall_direction_e *direction); + +/** + * @brief Gets the source IP type from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[out] type The IP type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_src_ip_type() + */ +int stc_firewall_rule_get_src_ip_type(stc_firewall_rule_h rule_h, + stc_firewall_ip_type_e *type); + +/** + * @brief Gets the destination IP type from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[out] type The IP type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_dst_ip_type() + */ +int stc_firewall_rule_get_dst_ip_type(stc_firewall_rule_h rule_h, + stc_firewall_ip_type_e *type); + +/** + * @brief Gets the source port type from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[out] type The port type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_src_port_type() + */ +int stc_firewall_rule_get_src_port_type(stc_firewall_rule_h rule_h, + stc_firewall_port_type_e *type); + +/** + * @brief Gets the destination port type from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[out] type The port type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_dst_port_type() + */ +int stc_firewall_rule_get_dst_port_type(stc_firewall_rule_h rule_h, + stc_firewall_port_type_e *type); + +/** + * @brief Gets the protocol type from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[out] type The protocol type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_protocol_type() + */ +int stc_firewall_rule_get_protocol_type(stc_firewall_rule_h rule_h, + stc_firewall_protocol_type_e *type); + +/** + * @brief Gets the address family type from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[out] type The address family type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_family_type() + */ +int stc_firewall_rule_get_family_type(stc_firewall_rule_h rule_h, + stc_firewall_family_type_e *type); + +/** + * @brief Gets the source IP addresses from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[out] ip1 The address 1 + * @param[out] ip2 The address 2 which will be IP address or mask + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_src_ip_type() + * @see stc_firewall_rule_set_src_ip() + */ +int stc_firewall_rule_get_src_ip(stc_firewall_rule_h rule_h, + char **ip1, char **ip2); + +/** + * @brief Gets the destination IP addresses from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[out] ip1 The address 1 + * @param[out] ip2 The address 2 which will be IP address or mask + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_dst_ip_type() + * @see stc_firewall_rule_set_dst_ip() + */ +int stc_firewall_rule_get_dst_ip(stc_firewall_rule_h rule_h, + char **ip1, char **ip2); + +/** + * @brief Gets the source ports from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[out] port1 The port 1 + * @param[out] port2 The port 2 + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_src_port_type() + * @see stc_firewall_rule_set_src_port() + */ +int stc_firewall_rule_get_src_port(stc_firewall_rule_h rule_h, + unsigned int *port1, unsigned int *port2); + +/** + * @brief Gets the destination ports from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[out] port1 The port 1 + * @param[out] port2 The port 2 + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_get_dst_port_type() + * @see stc_firewall_rule_set_dst_port() + */ +int stc_firewall_rule_get_dst_port(stc_firewall_rule_h rule_h, + unsigned int *port1, unsigned int *port2); + +/** + * @brief Gets the interface name from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule 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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_ifname() + */ +int stc_firewall_rule_get_ifname(stc_firewall_rule_h rule_h, char **ifname); + +/** + * @brief Gets the target type from rule. + * @since_tizen 5.0 + * + * @param[in] rule The firewall rule handle + * @param[out] target The target type + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + * @see stc_firewall_rule_set_target() + */ +int stc_firewall_rule_get_target(stc_firewall_rule_h rule_h, + stc_firewall_rule_target_e *target); + +/** + * @brief Adds the rule. + * @since_tizen 5.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/firewall.common + * + * @param[in] rule The firewall rule 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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + */ +int stc_firewall_rule_add(stc_firewall_rule_h rule_h); + +/** + * @brief Removes the rule. + * @since_tizen 5.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/firewall.common + * + * @param[in] rule The firewall rule 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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + */ +int stc_firewall_rule_remove(stc_firewall_rule_h rule_h); + +/** + * @brief Updates the rule. + * @since_tizen 5.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/firewall.common + * + * @param[in] rule The firewall rule 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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + */ +int stc_firewall_rule_update(stc_firewall_rule_h rule_h); + +/** + * @brief Gets all rules. + * @since_tizen 5.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/firewall.common + * + * @param[in] stc The stc handle + * @param[in] callback The callback to be called + * @param[in] user_data The user data passed to the callback function + * + * @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_firewall_rule_h + * @see stc_firewall_rule_create() + * @see stc_firewall_rule_destroy() + */ +int stc_firewall_rule_foreach(stc_h stc, + stc_firewall_rule_info_cb callback, void *user_data); + +/** +* @} +*/ + #ifdef __cplusplus } #endif diff --git a/packaging/capi-network-stc.spec b/packaging/capi-network-stc.spec index 443629b..5767c55 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.23 +Version: 0.0.24 Release: 1 Group: Network & Connectivity/API License: Apache-2.0 diff --git a/src/include/stc-firewall.h b/src/include/stc-firewall.h new file mode 100755 index 0000000..60c1b63 --- /dev/null +++ b/src/include/stc-firewall.h @@ -0,0 +1,142 @@ +/* + * 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-firewall.h + * @author Tak hyunuk (hyunuk.tak@samsung.com) + * @version 0.1 + */ + +#ifndef __TIZEN_STC_FIREWALL_H__ +#define __TIZEN_STC_FIREWALL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/***************************************************************************** + * Standard headers + *****************************************************************************/ + +/***************************************************************************** + * System headers + *****************************************************************************/ + +/***************************************************************************** + * Smart Traffic Control (STC) library headers + *****************************************************************************/ +#include "stc.h" + +/***************************************************************************** + * Macros and Typedefs + *****************************************************************************/ + +#define TIZEN_FEATURE_STC_FW "tizen.org/feature/network.firewall" + +#define STC_MANAGER_FW_PATH "/net/stc/firewall" +#define STC_MANAGER_FW_INTERFACE STC_MANAGER_SERVICE ".firewall" + +#define STC_MANAGER_FW_METHOD_LOCK "Lock" +#define STC_MANAGER_FW_METHOD_UNLOCK "Unlock" +#define STC_MANAGER_FW_METHOD_GET_LOCK "GetLock" + +#define STC_MANAGER_FW_METHOD_ADD_CHAIN "AddChain" +#define STC_MANAGER_FW_METHOD_REMOVE_CHAIN "RemoveChain" +#define STC_MANAGER_FW_METHOD_FLUSH_CHAIN "FlushChain" +#define STC_MANAGER_FW_METHOD_GET_ALL_CHAIN "GetAllChain" +#define STC_MANAGER_FW_METHOD_SET_CHAIN "SetChain" +#define STC_MANAGER_FW_METHOD_UNSET_CHAIN "UnsetChain" + +#define STC_MANAGER_FW_METHOD_ADD_RULE "AddRule" +#define STC_MANAGER_FW_METHOD_REMOVE_RULE "RemoveRule" +#define STC_MANAGER_FW_METHOD_UPDATE_RULE "UpdateRule" +#define STC_MANAGER_FW_METHOD_GET_ALL_RULE "GetAllRule" + +#define STC_FW_STR_LEN 128 + +#define STC_FW_CHAIN_NAME "chain" +#define STC_FW_CHAIN_TARGET "target" +#define STC_FW_CHAIN_PRIORITY "priority" + +#define STC_FW_RULE_CHAIN "chain" +#define STC_FW_RULE_DIRECTION "direction" +#define STC_FW_RULE_SIPTYPE "s_ip_type" +#define STC_FW_RULE_DIPTYPE "d_ip_type" +#define STC_FW_RULE_SPORTTYPE "s_port_type" +#define STC_FW_RULE_DPORTTYPE "d_port_type" +#define STC_FW_RULE_PROTOCOL "protocol" +#define STC_FW_RULE_FAMILY "family" +#define STC_FW_RULE_SIP1 "s_ip1" +#define STC_FW_RULE_SIP2 "s_ip2" +#define STC_FW_RULE_DIP1 "d_ip1" +#define STC_FW_RULE_DIP2 "d_ip2" +#define STC_FW_RULE_SPORT1 "s_port1" +#define STC_FW_RULE_SPORT2 "s_port2" +#define STC_FW_RULE_DPORT1 "d_port1" +#define STC_FW_RULE_DPORT2 "d_port2" +#define STC_FW_RULE_IFNAME "ifname" +#define STC_FW_RULE_TARGET "target" +#define STC_FW_RULE_IDENTIFIER "identifier" +#define STC_FW_RULE_KEY "key" + +typedef struct { + char *chain; + guint priority; + bool created; + stc_firewall_chain_target_e target; +} stc_fw_chain_s; + +typedef struct { + char *chain; + stc_firewall_direction_e direction; + stc_firewall_ip_type_e s_ip_type; + stc_firewall_ip_type_e d_ip_type; + stc_firewall_port_type_e s_port_type; + stc_firewall_port_type_e d_port_type; + stc_firewall_protocol_type_e protocol; + stc_firewall_family_type_e family; + char *s_ip1; + char *s_ip2; + char *d_ip1; + char *d_ip2; + guint s_port1; + guint s_port2; + guint d_port1; + guint d_port2; + char *ifname; + stc_firewall_rule_target_e target; + char *identifier; + guint key; +} stc_fw_rule_s; + +/***************************************************************************** + * Global Variables + *****************************************************************************/ + +/***************************************************************************** + * Local Functions Definition + *****************************************************************************/ + +#ifdef __cplusplus +} +#endif + +#endif /** __TIZEN_STC_RESTRICTION_H__ */ diff --git a/src/internal/include/stc-private.h b/src/internal/include/stc-private.h index a284b47..9ab8b6a 100755 --- a/src/internal/include/stc-private.h +++ b/src/internal/include/stc-private.h @@ -105,6 +105,21 @@ const char *_stc_convert_connection_period_to_string( const char *_stc_convert_time_period_to_string( stc_time_period_e period); +const char *_stc_convert_fw_direction_to_string( + stc_firewall_direction_e direction); +const char *_stc_convert_fw_ip_type_to_string( + stc_firewall_ip_type_e type); +const char *_stc_convert_fw_port_type_to_string( + stc_firewall_port_type_e type); +const char *_stc_convert_fw_protocol_type_to_string( + stc_firewall_protocol_type_e type); +const char *_stc_convert_fw_family_type_to_string( + stc_firewall_family_type_e type); +const char *_stc_convert_fw_chain_target_to_string( + stc_firewall_chain_target_e target); +const char *_stc_convert_fw_rule_target_to_string( + stc_firewall_rule_target_e target); + int _stc_initialize(void); int _stc_deinitialize(void); int _stc_handle_create(stc_h *stc); diff --git a/src/internal/include/stc-util.h b/src/internal/include/stc-util.h index 3c26d70..f66f913 100755 --- a/src/internal/include/stc-util.h +++ b/src/internal/include/stc-util.h @@ -49,6 +49,14 @@ #define STC_IFNAME_LEN 16 #define STC_SUBSCRIBER_ID_LEN 128 +#define MALLOC0(t, n) ((t*) g_try_malloc0((n) * sizeof(t))) +#define FREE(p) do { \ + if (p) { \ + g_free(p); \ + p = NULL; \ + } \ +} while (0) + #define STC_RETURN_VAL_IF(expr, val, fmt, arg...) do { \ if (expr) { \ STC_LOGE(fmt, ##arg); \ @@ -58,7 +66,7 @@ #define TIZEN_FEATURE_STC "tizen.org/feature/network.traffic_control" -#if 1 +#if 0 #define CHECK_FEATURE_SUPPORTED(feature_name)\ do {\ bool feature_supported = FALSE;\ diff --git a/src/internal/stc-private.c b/src/internal/stc-private.c index 1c10a19..0793721 100755 --- a/src/internal/stc-private.c +++ b/src/internal/stc-private.c @@ -255,6 +255,119 @@ const char *_stc_convert_time_period_to_string( return "Unknown"; } } + +const char *_stc_convert_fw_direction_to_string( + stc_firewall_direction_e direction) +{ + switch (direction) { + case STC_FIREWALL_DIRECTION_NONE: + return "None"; + case STC_FIREWALL_DIRECTION_IN: + return "In"; + case STC_FIREWALL_DIRECTION_OUT: + return "Out"; + default: + return "Unknown"; + } +} + +const char *_stc_convert_fw_ip_type_to_string( + stc_firewall_ip_type_e type) +{ + switch (type) { + case STC_FIREWALL_IP_NONE: + return "None"; + case STC_FIREWALL_IP_SINGLE: + return "Single"; + case STC_FIREWALL_IP_MASK: + return "Mask"; + case STC_FIREWALL_IP_RANGE: + return "Range"; + default: + return "Unknown"; + } +} + +const char *_stc_convert_fw_port_type_to_string( + stc_firewall_port_type_e type) +{ + switch (type) { + case STC_FIREWALL_PORT_NONE: + return "None"; + case STC_FIREWALL_PORT_SINGLE: + return "Single"; + case STC_FIREWALL_PORT_RANGE: + return "Range"; + default: + return "Unknown"; + } +} + +const char *_stc_convert_fw_protocol_type_to_string( + stc_firewall_protocol_type_e type) +{ + switch (type) { + case STC_FIREWALL_PROTOCOL_NONE: + return "None"; + case STC_FIREWALL_PROTOCOL_TCP: + return "TCP"; + case STC_FIREWALL_PROTOCOL_UDP: + return "UDP"; + case STC_FIREWALL_PROTOCOL_ICMP: + return "ICMP"; + case STC_FIREWALL_PROTOCOL_ALL: + return "All"; + default: + return "Unknown"; + } +} + +const char *_stc_convert_fw_family_type_to_string( + stc_firewall_family_type_e type) +{ + switch (type) { + case STC_FIREWALL_FAMILY_NONE: + return "None"; + case STC_FIREWALL_FAMILY_V4: + return "IPv4"; + case STC_FIREWALL_FAMILY_V6: + return "IPv6"; + default: + return "Unknown"; + } +} + +const char *_stc_convert_fw_chain_target_to_string( + stc_firewall_chain_target_e target) +{ + switch (target) { + case STC_FIREWALL_CHAIN_TARGET_NONE: + return "NONE"; + case STC_FIREWALL_CHAIN_TARGET_INPUT: + return "INPUT"; + case STC_FIREWALL_CHAIN_TARGET_OUTPUT: + return "OUTPUT"; + default: + return "Unknown"; + } +} + +const char *_stc_convert_fw_rule_target_to_string( + stc_firewall_rule_target_e target) +{ + switch (target) { + case STC_FIREWALL_RULE_TARGET_NONE: + return "NONE"; + case STC_FIREWALL_RULE_TARGET_ACCEPT: + return "ACCEPT"; + case STC_FIREWALL_RULE_TARGET_DROP: + return "DROP"; + case STC_FIREWALL_RULE_TARGET_LOG: + return "LOG"; + default: + return "Unknown"; + } +} //LCOV_EXCL_STOP static bool __stc_handle_find(stc_h stc) @@ -391,7 +504,7 @@ static void __stc_callback_foreach(stc_event_e e, } break; //LCOV_EXCL_STOP - default : + default: break; } } @@ -450,7 +563,7 @@ static void __stc_event_cb(stc_event_info_s *event_info, void *user_data) } break; //LCOV_EXCL_STOP - default : + default: break; } } diff --git a/src/stc-firewall.c b/src/stc-firewall.c new file mode 100755 index 0000000..a548fc6 --- /dev/null +++ b/src/stc-firewall.c @@ -0,0 +1,2093 @@ +/* + * 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-firewall.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-event.h" + +#include "stc-info.h" +#include "stc-firewall.h" + +/***************************************************************************** + * Macros and Typedefs + *****************************************************************************/ + +/***************************************************************************** + * Global Variables + *****************************************************************************/ + +static GSList *g_fw_chain_list = NULL; +static GSList *g_fw_rule_list = NULL; + +/***************************************************************************** + * Local Functions Definition + *****************************************************************************/ + +//LCOV_EXCL_START +static void __stc_fw_init_chain(void *data, const char *name) +{ + stc_fw_chain_s *chain = (stc_fw_chain_s *)data; + + memset(chain, 0, sizeof(stc_fw_chain_s)); + chain->chain = g_strdup(name); + chain->created = true; +} + +static void __stc_fw_set_chain(void *data, + stc_firewall_chain_target_e target) +{ + stc_fw_chain_s *chain = (stc_fw_chain_s *)data; + chain->target = target; +} + +static void __stc_fw_clone_chain(void *a, void *b) +{ + stc_fw_chain_s *origin = (stc_fw_chain_s *)a; + stc_fw_chain_s *cloned = (stc_fw_chain_s *)b; + + cloned->chain = g_strdup(origin->chain); + cloned->priority = origin->priority; + cloned->target = origin->target; + cloned->created = origin->created; +} + +static void __stc_fw_deinit_chain(void *data) +{ + stc_fw_chain_s *chain = (stc_fw_chain_s *)data; + FREE(chain->chain); + FREE(chain); + chain = NULL; +} + +static void __stc_fw_init_rule(void *data) +{ + stc_fw_rule_s *rule = (stc_fw_rule_s *)data; + + memset(rule, 0, sizeof(stc_fw_rule_s)); +} + +static void __stc_fw_clone_rule(void *a, void *b) +{ + stc_fw_rule_s *origin = (stc_fw_rule_s *)a; + stc_fw_rule_s *cloned = (stc_fw_rule_s *)b; + + cloned->chain = g_strdup(origin->chain); + cloned->direction = origin->direction; + cloned->s_ip_type = origin->s_ip_type; + cloned->d_ip_type = origin->d_ip_type; + cloned->s_port_type = origin->s_port_type; + cloned->d_port_type = origin->d_port_type; + cloned->protocol = origin->protocol; + cloned->family = origin->family; + cloned->s_ip1 = g_strdup(origin->s_ip1); + cloned->s_ip2 = g_strdup(origin->s_ip2); + cloned->d_ip1 = g_strdup(origin->d_ip1); + cloned->d_ip2 = g_strdup(origin->d_ip2); + cloned->s_port1 = origin->s_port1; + cloned->s_port2 = origin->s_port2; + cloned->d_port1 = origin->d_port1; + cloned->d_port2 = origin->d_port2; + cloned->ifname = g_strdup(origin->ifname); + cloned->target = origin->target; + cloned->identifier = g_strdup(origin->identifier); + cloned->key = origin->key; +} + +static void __stc_fw_deinit_rule(void *data) +{ + stc_fw_rule_s *rule = (stc_fw_rule_s *)data; + + FREE(rule->chain); + FREE(rule->s_ip1); + FREE(rule->s_ip2); + FREE(rule->d_ip1); + FREE(rule->d_ip2); + FREE(rule->ifname); + FREE(rule->identifier); + FREE(rule); + rule = NULL; +} + +static void __stc_fw_add_to_chain_list(stc_firewall_chain_h chain_h) +{ + g_fw_chain_list = g_slist_append(g_fw_chain_list, chain_h); +} + +static bool __stc_fw_check_chain_validity(stc_firewall_chain_h chain_h) +{ + if (!chain_h) + return false; + + if (g_slist_find(g_fw_chain_list, chain_h)) + return true; + else + return false; +} + +static bool __stc_fw_check_chain_created(stc_firewall_chain_h chain_h) +{ + stc_fw_chain_s *chain = (stc_fw_chain_s *)chain_h; + + return chain->created; +} + +static void __stc_fw_remove_from_chain_list(stc_firewall_chain_h chain_h) +{ + g_fw_chain_list = g_slist_remove(g_fw_chain_list, chain_h); +} + +static void __stc_fw_add_to_rule_list(stc_firewall_rule_h rule_h) +{ + g_fw_rule_list = g_slist_append(g_fw_rule_list, rule_h); +} + +static bool __stc_fw_check_rule_validity(stc_firewall_rule_h rule_h) +{ + if (!rule_h) + return false; + + if (g_slist_find(g_fw_rule_list, rule_h)) + return true; + else + return false; +} + +static bool __stc_fw_check_rule_created(stc_firewall_rule_h rule_h) +{ + stc_fw_rule_s *rule = (stc_fw_rule_s *)rule_h; + + if (rule->key) + return false; + else + return true; +} + +static void __stc_fw_remove_from_rule_list(stc_firewall_rule_h rule_h) +{ + g_fw_rule_list = g_slist_remove(g_fw_rule_list, rule_h); +} + +static void __stc_fw_make_rule_params( + stc_fw_rule_s *rule, GVariant **params) +{ + GVariantBuilder *builder; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); + + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_CHAIN, + g_variant_new_string(rule->chain)); + + if (rule->direction != STC_FIREWALL_DIRECTION_NONE) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_DIRECTION, + g_variant_new_uint16(rule->direction)); + + if (rule->s_ip_type != STC_FIREWALL_IP_NONE) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_SIPTYPE, + g_variant_new_uint16(rule->s_ip_type)); + + if (rule->d_ip_type != STC_FIREWALL_IP_NONE) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_DIPTYPE, + g_variant_new_uint16(rule->d_ip_type)); + + if (rule->s_port_type != STC_FIREWALL_PORT_NONE) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_SPORTTYPE, + g_variant_new_uint16(rule->s_port_type)); + + if (rule->d_port_type != STC_FIREWALL_PORT_NONE) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_DPORTTYPE, + g_variant_new_uint16(rule->d_port_type)); + + if (rule->protocol != STC_FIREWALL_PROTOCOL_NONE) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_PROTOCOL, + g_variant_new_uint16(rule->protocol)); + + if (rule->family != STC_FIREWALL_FAMILY_NONE) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_FAMILY, + g_variant_new_uint16(rule->family)); + + if (rule->s_ip1) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_SIP1, + g_variant_new_string(rule->s_ip1)); + + if (rule->s_ip2) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_SIP2, + g_variant_new_string(rule->s_ip2)); + + if (rule->d_ip1) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_DIP1, + g_variant_new_string(rule->d_ip1)); + + if (rule->d_ip2) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_DIP2, + g_variant_new_string(rule->d_ip2)); + + if (rule->s_port1) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_SPORT1, + g_variant_new_uint32(rule->s_port1)); + + if (rule->s_port2) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_SPORT2, + g_variant_new_uint32(rule->s_port2)); + + if (rule->d_port1) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_DPORT1, + g_variant_new_uint32(rule->d_port1)); + + if (rule->d_port2) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_DPORT2, + g_variant_new_uint32(rule->d_port2)); + + if (rule->ifname) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_IFNAME, + g_variant_new_string(rule->ifname)); + + if (rule->target) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_TARGET, + g_variant_new_uint16(rule->target)); + + if (rule->identifier) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_IDENTIFIER, + g_variant_new_string(rule->identifier)); + + if (rule->key > 0) + g_variant_builder_add(builder, "{sv}", + STC_FW_RULE_KEY, + g_variant_new_uint32(rule->key)); + + *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder)); + g_variant_builder_unref(builder); +} + +static void __stc_fw_extract_chain(const char *key, + GVariant *value, void *user_data) +{ + stc_fw_chain_s *chain = (stc_fw_chain_s *)user_data; + + if (chain == NULL) + return; + + if (!g_strcmp0(key, STC_FW_CHAIN_NAME)) { + guint str_length; + const gchar *str = g_variant_get_string(value, &str_length); + chain->chain = g_strdup(str); + + } else if (!g_strcmp0(key, STC_FW_CHAIN_PRIORITY)) { + chain->priority = g_variant_get_uint32(value); + + } else if (!g_strcmp0(key, STC_FW_CHAIN_TARGET)) { + chain->target = g_variant_get_uint16(value); + + } + +} + +static void __stc_fw_extract_rule(const char *key, + GVariant *value, void *user_data) +{ + stc_fw_rule_s *rule = (stc_fw_rule_s *)user_data; + + if (rule == NULL) + return; + + if (!g_strcmp0(key, STC_FW_RULE_CHAIN)) { + guint str_length; + const gchar *str = g_variant_get_string(value, &str_length); + rule->chain = g_strdup(str); + + } else if (!g_strcmp0(key, STC_FW_RULE_DIRECTION)) { + rule->direction = g_variant_get_uint16(value); + + } else if (!g_strcmp0(key, STC_FW_RULE_SIPTYPE)) { + rule->s_ip_type = g_variant_get_uint16(value); + + } else if (!g_strcmp0(key, STC_FW_RULE_DIPTYPE)) { + rule->d_ip_type = g_variant_get_uint16(value); + + } else if (!g_strcmp0(key, STC_FW_RULE_SPORTTYPE)) { + rule->s_port_type = g_variant_get_uint16(value); + + } else if (!g_strcmp0(key, STC_FW_RULE_DPORTTYPE)) { + rule->d_port_type = g_variant_get_uint16(value); + + } else if (!g_strcmp0(key, STC_FW_RULE_PROTOCOL)) { + rule->protocol = g_variant_get_uint16(value); + + } else if (!g_strcmp0(key, STC_FW_RULE_FAMILY)) { + rule->family = g_variant_get_uint16(value); + + } else if (!g_strcmp0(key, STC_FW_RULE_SIP1)) { + guint str_length; + const gchar *str = g_variant_get_string(value, &str_length); + rule->s_ip1 = g_strdup(str); + + } else if (!g_strcmp0(key, STC_FW_RULE_SIP2)) { + guint str_length; + const gchar *str = g_variant_get_string(value, &str_length); + rule->s_ip2 = g_strdup(str); + + } else if (!g_strcmp0(key, STC_FW_RULE_DIP1)) { + guint str_length; + const gchar *str = g_variant_get_string(value, &str_length); + rule->d_ip1 = g_strdup(str); + + } else if (!g_strcmp0(key, STC_FW_RULE_DIP2)) { + guint str_length; + const gchar *str = g_variant_get_string(value, &str_length); + rule->d_ip2 = g_strdup(str); + + } else if (!g_strcmp0(key, STC_FW_RULE_SPORT1)) { + rule->s_port1 = g_variant_get_uint32(value); + + } else if (!g_strcmp0(key, STC_FW_RULE_SPORT2)) { + rule->s_port2 = g_variant_get_uint32(value); + + } else if (!g_strcmp0(key, STC_FW_RULE_DPORT1)) { + rule->d_port1 = g_variant_get_uint32(value); + + } else if (!g_strcmp0(key, STC_FW_RULE_DPORT2)) { + rule->d_port2 = g_variant_get_uint32(value); + + } else if (!g_strcmp0(key, STC_FW_RULE_IFNAME)) { + guint str_length; + const gchar *str = g_variant_get_string(value, &str_length); + rule->ifname = g_strdup(str); + + } else if (!g_strcmp0(key, STC_FW_RULE_TARGET)) { + rule->target = g_variant_get_uint16(value); + + } else if (!g_strcmp0(key, STC_FW_RULE_IDENTIFIER)) { + guint str_length; + const gchar *str = g_variant_get_string(value, &str_length); + rule->identifier = g_strdup(str); + + } else if (!g_strcmp0(key, STC_FW_RULE_KEY)) { + rule->key = g_variant_get_uint32(value); + + } + +} + +static void __stc_fw_print_chain(stc_fw_chain_s *chain) +{ + if (chain == NULL) + return; + + STC_LOGD(STC_HR_SINGLE); + + STC_LOGD("Chain: [%s]", (chain->chain) ? chain->chain : ""); + STC_LOGD("Target: [%s]", _stc_convert_fw_chain_target_to_string(chain->target)); + STC_LOGD("Priority: [%u]", chain->priority); + + STC_LOGD(STC_HR_SINGLE); +} + +static void __stc_fw_print_rule(stc_fw_rule_s *rule) +{ + if (rule == NULL) + return; + + STC_LOGD(STC_HR_SINGLE); + + STC_LOGD("[%s][%s][%s][%s][%s][%s][%s][%s]" + "[%s][%s][%s][%s][%d][%d][%s][%d][%d][%s]", + (rule->chain) ? rule->chain : "", + _stc_convert_fw_direction_to_string(rule->direction), + (rule->ifname) ? rule->ifname : "", + _stc_convert_fw_family_type_to_string(rule->family), + _stc_convert_fw_ip_type_to_string(rule->s_ip_type), + (rule->s_ip1) ? rule->s_ip1 : "", + (rule->s_ip2) ? rule->s_ip2 : "", + _stc_convert_fw_ip_type_to_string(rule->d_ip_type), + (rule->d_ip1) ? rule->d_ip1 : "", + (rule->d_ip2) ? rule->d_ip2 : "", + _stc_convert_fw_protocol_type_to_string(rule->protocol), + _stc_convert_fw_port_type_to_string(rule->s_port_type), + rule->s_port1, + rule->s_port2, + _stc_convert_fw_port_type_to_string(rule->d_port_type), + rule->d_port1, + rule->d_port2, + _stc_convert_fw_rule_target_to_string(rule->target) + ); + + STC_LOGD(STC_HR_SINGLE); +} + +stc_error_e _stc_fw_lock(void) +{ + GVariant *message = NULL; + stc_error_e error = STC_ERROR_NONE; + int result = 0; + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_FW_PATH, + STC_MANAGER_FW_INTERFACE, + STC_MANAGER_FW_METHOD_LOCK, + NULL, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(i)", &result); + STC_LOGI("Successfully lock firewall [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +stc_error_e _stc_fw_unlock(void) +{ + GVariant *message = NULL; + stc_error_e error = STC_ERROR_NONE; + int result = 0; + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_FW_PATH, + STC_MANAGER_FW_INTERFACE, + STC_MANAGER_FW_METHOD_UNLOCK, + NULL, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(i)", &result); + STC_LOGI("Successfully lock firewall [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +stc_error_e _stc_fw_get_lock(stc_firewall_lock_e *state) +{ + GVariant *message = NULL; + stc_error_e error = STC_ERROR_NONE; + int result = 0; + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_FW_PATH, + STC_MANAGER_FW_INTERFACE, + STC_MANAGER_FW_METHOD_GET_LOCK, + NULL, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(i)", &result); + *state = (stc_firewall_lock_e)result; + + STC_LOGI("Successfully get firewall lock state [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + + +stc_error_e _stc_fw_chain_add(stc_fw_chain_s *chain) +{ + GVariant *message = NULL; + GVariant *params = NULL; + stc_error_e error = STC_ERROR_NONE; + int result = 0; + + params = g_variant_new("(s)", chain->chain); + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_FW_PATH, + STC_MANAGER_FW_INTERFACE, + STC_MANAGER_FW_METHOD_ADD_CHAIN, + params, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(i)", &result); + STC_LOGI("Successfully add firewall chain [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +stc_error_e _stc_fw_chain_remove(stc_fw_chain_s *chain) +{ + GVariant *message = NULL; + GVariant *params = NULL; + stc_error_e error = STC_ERROR_NONE; + int result = 0; + + params = g_variant_new("(s)", chain->chain); + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_FW_PATH, + STC_MANAGER_FW_INTERFACE, + STC_MANAGER_FW_METHOD_REMOVE_CHAIN, + params, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(i)", &result); + STC_LOGI("Successfully remove firewall chain [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +stc_error_e _stc_fw_chain_flush(stc_fw_chain_s *chain) +{ + GVariant *message = NULL; + GVariant *params = NULL; + stc_error_e error = STC_ERROR_NONE; + int result = 0; + + params = g_variant_new("(s)", chain->chain); + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_FW_PATH, + STC_MANAGER_FW_INTERFACE, + STC_MANAGER_FW_METHOD_FLUSH_CHAIN, + params, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(i)", &result); + STC_LOGI("Successfully flush firewall chain [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +stc_error_e _stc_fw_chain_foreach(stc_firewall_chain_info_cb callback, + void *user_data) +{ + GVariant *message = NULL; + stc_error_e error = STC_ERROR_NONE; + GVariantIter *iter = NULL; + GVariantIter *iter_row = NULL; + GSList *chain_list = NULL; + GSList *list = NULL; + stc_callback_ret_e rv = STC_CALLBACK_CANCEL; + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_FW_PATH, + STC_MANAGER_FW_INTERFACE, + STC_MANAGER_FW_METHOD_GET_ALL_CHAIN, + NULL, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(aa{sv})", &iter); + + while (g_variant_iter_next(iter, "a{sv}", &iter_row)) { + stc_fw_chain_s *chain = MALLOC0(stc_fw_chain_s, 1); + + if (chain) + chain->created = false; + + _stc_dbus_dictionary_foreach(iter_row, + __stc_fw_extract_chain, chain); + + __stc_fw_print_chain(chain); + + chain_list = g_slist_append(chain_list, (stc_fw_chain_s *)chain); + + g_variant_iter_free(iter_row); + } + + g_variant_iter_free(iter); + g_variant_unref(message); + + if ((int)g_slist_length(chain_list) == 0) { + STC_LOGD("There are no firewall chains"); + return STC_ERROR_NONE; + } + + for (list = chain_list; list; list = list->next) { + stc_fw_chain_s *chain = (stc_fw_chain_s *)list->data; + __stc_fw_add_to_chain_list((stc_firewall_chain_h)chain); + rv = callback((stc_firewall_chain_h)chain, user_data); + __stc_fw_remove_from_chain_list((stc_firewall_chain_h)chain); + if (rv == false) + break; + } + + g_slist_free_full(chain_list, __stc_fw_deinit_chain); + + return STC_ERROR_NONE; +} + +stc_error_e _stc_fw_chain_set(stc_fw_chain_s *chain) +{ + GVariant *message = NULL; + GVariant *params = NULL; + stc_error_e error = STC_ERROR_NONE; + int result = 0; + + params = g_variant_new("(su)", chain->chain, chain->target); + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_FW_PATH, + STC_MANAGER_FW_INTERFACE, + STC_MANAGER_FW_METHOD_SET_CHAIN, + params, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(i)", &result); + STC_LOGI("Successfully set firewall chain [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +stc_error_e _stc_fw_chain_unset(stc_fw_chain_s *chain) +{ + GVariant *message = NULL; + GVariant *params = NULL; + stc_error_e error = STC_ERROR_NONE; + int result = 0; + + params = g_variant_new("(s)", chain->chain); + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_FW_PATH, + STC_MANAGER_FW_INTERFACE, + STC_MANAGER_FW_METHOD_UNSET_CHAIN, + params, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(i)", &result); + STC_LOGI("Successfully unset firewall chain [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +stc_error_e _stc_fw_rule_add(stc_fw_rule_s *rule) +{ + GVariant *message = NULL; + GVariant *params = NULL; + stc_error_e error = STC_ERROR_NONE; + int result = 0; + + __stc_fw_make_rule_params(rule, ¶ms); + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_FW_PATH, + STC_MANAGER_FW_INTERFACE, + STC_MANAGER_FW_METHOD_ADD_RULE, + params, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(i)", &result); + STC_LOGI("Successfully add firewall rule [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +stc_error_e _stc_fw_rule_remove(stc_fw_rule_s *rule) +{ + GVariant *message = NULL; + GVariant *params = NULL; + stc_error_e error = STC_ERROR_NONE; + int result = 0; + + __stc_fw_make_rule_params(rule, ¶ms); + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_FW_PATH, + STC_MANAGER_FW_INTERFACE, + STC_MANAGER_FW_METHOD_REMOVE_RULE, + params, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(i)", &result); + STC_LOGI("Successfully remove firewall rule [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +stc_error_e _stc_fw_rule_update(stc_fw_rule_s *rule) +{ + GVariant *message = NULL; + GVariant *params = NULL; + stc_error_e error = STC_ERROR_NONE; + int result = 0; + + __stc_fw_make_rule_params(rule, ¶ms); + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_FW_PATH, + STC_MANAGER_FW_INTERFACE, + STC_MANAGER_FW_METHOD_UPDATE_RULE, + params, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(i)", &result); + STC_LOGI("Successfully update firewall rule [%d]", result); + g_variant_unref(message); + + return STC_ERROR_NONE; +} + +stc_error_e _stc_fw_rule_foreach(stc_firewall_rule_info_cb callback, + void *user_data) +{ + GVariant *message = NULL; + stc_error_e error = STC_ERROR_NONE; + GVariantIter *iter = NULL; + GVariantIter *iter_row = NULL; + GSList *rule_list = NULL; + GSList *list = NULL; + stc_callback_ret_e rv = STC_CALLBACK_CANCEL; + + message = _stc_dbus_invoke_method( + STC_MANAGER_SERVICE, + STC_MANAGER_FW_PATH, + STC_MANAGER_FW_INTERFACE, + STC_MANAGER_FW_METHOD_GET_ALL_RULE, + NULL, + &error); + + STC_RETURN_VAL_IF(message == NULL, + error, "Failed to invoke dbus method"); + + g_variant_get(message, "(aa{sv})", &iter); + + while (g_variant_iter_next(iter, "a{sv}", &iter_row)) { + stc_fw_rule_s *rule = MALLOC0(stc_fw_rule_s, 1); + + _stc_dbus_dictionary_foreach(iter_row, + __stc_fw_extract_rule, rule); + + __stc_fw_print_rule(rule); + + rule_list = g_slist_append(rule_list, (stc_fw_rule_s *)rule); + + g_variant_iter_free(iter_row); + } + + g_variant_iter_free(iter); + g_variant_unref(message); + + if ((int)g_slist_length(rule_list) == 0) { + STC_LOGD("There are no firewall rules"); + return STC_ERROR_NONE; + } + + for (list = rule_list; list; list = list->next) { + stc_fw_rule_s *rule = (stc_fw_rule_s *)list->data; + __stc_fw_add_to_rule_list((stc_firewall_rule_h)rule); + rv = callback((stc_firewall_rule_h)rule, user_data); + __stc_fw_remove_from_rule_list((stc_firewall_rule_h)rule); + if (rv == false) + break; + } + + g_slist_free_full(rule_list, __stc_fw_deinit_rule); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_lock(stc_h stc) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (!(_stc_handle_check_validity(stc))) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_fw_lock(); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to lock firewall [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + STC_LOGI("Firewall successfully locked"); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_unlock(stc_h stc) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (!(_stc_handle_check_validity(stc))) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_fw_unlock(); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to unlock firewall [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + STC_LOGI("Firewall successfully unlocked"); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_get_lock(stc_h stc, stc_firewall_lock_e *state) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (!(_stc_handle_check_validity(stc))) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_fw_get_lock(state); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to get firewall lock state [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + STC_LOGI("Firewall successfully get lock state"); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_chain_create(stc_h stc, const char *name, + stc_firewall_chain_h *chain_h) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (!(_stc_handle_check_validity(stc))) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!chain_h || !name) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_chain_s *fw_chain = MALLOC0(stc_fw_chain_s, 1); + if (!fw_chain) { + STC_LOGE("Memory allocation failed"); + return STC_ERROR_OUT_OF_MEMORY; + } + + __stc_fw_init_chain(fw_chain, name); + __stc_fw_add_to_chain_list((stc_firewall_chain_h)fw_chain); + + *chain_h = (stc_firewall_chain_h)fw_chain; + + STC_LOGI("Firewall chain successfully created"); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_chain_clone(stc_firewall_chain_h origin_h, + stc_firewall_chain_h *cloned_chain_h) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_chain_validity(origin_h) == false || + cloned_chain_h == NULL) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_chain_s *fw_chain = MALLOC0(stc_fw_chain_s, 1); + if (!fw_chain) { + STC_LOGE("Memory allocation failed"); + return STC_ERROR_OUT_OF_MEMORY; + } + + __stc_fw_clone_chain(origin_h, fw_chain); + __stc_fw_add_to_chain_list((stc_firewall_chain_h)fw_chain); + + *cloned_chain_h = (stc_firewall_chain_h)fw_chain; + + STC_LOGI("Firewall chain successfully cloned"); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_chain_destroy(stc_firewall_chain_h chain_h) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_chain_validity(chain_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + __stc_fw_remove_from_chain_list(chain_h); + __stc_fw_deinit_chain(chain_h); + + STC_LOGI("Firewall rule successfully destroyed"); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_chain_get_name(stc_firewall_chain_h chain_h, char **name) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_chain_validity(chain_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!name) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_chain_s *fw_chain = (stc_fw_chain_s *)chain_h; + + *name = g_strdup(fw_chain->chain); + + STC_LOGI("Chain [%s]", *name); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_chain_get_target(stc_firewall_chain_h chain_h, + stc_firewall_chain_target_e *target) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_chain_validity(chain_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!target) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_chain_s *fw_chain = (stc_fw_chain_s *)chain_h; + + *target = fw_chain->target; + + STC_LOGI("Target [%s]", _stc_convert_fw_chain_target_to_string(*target)); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_chain_get_priority(stc_firewall_chain_h chain_h, + unsigned int *priority) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_chain_validity(chain_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!priority) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_chain_s *fw_chain = (stc_fw_chain_s *)chain_h; + + *priority = fw_chain->priority; + + STC_LOGI("Priority [%u]", *priority); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_chain_add(stc_firewall_chain_h chain_h) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_chain_validity(chain_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (__stc_fw_check_chain_created(chain_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_fw_chain_add((stc_fw_chain_s *)chain_h); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to add firewall cahin [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_chain_remove(stc_firewall_chain_h chain_h) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_chain_validity(chain_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (__stc_fw_check_chain_created(chain_h) == true) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_fw_chain_remove((stc_fw_chain_s *)chain_h); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to remove firewall cahin [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_chain_flush(stc_firewall_chain_h chain_h) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_chain_validity(chain_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (__stc_fw_check_chain_created(chain_h) == true) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_fw_chain_flush((stc_fw_chain_s *)chain_h); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to flush firewall cahin [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_chain_foreach(stc_h stc, + stc_firewall_chain_info_cb callback, void *user_data) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (!(_stc_handle_check_validity(stc))) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (callback == NULL) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_fw_chain_foreach(callback, user_data); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to get all firewall chain [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_chain_set(stc_firewall_chain_h chain_h, + stc_firewall_chain_target_e target) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_chain_validity(chain_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (__stc_fw_check_chain_created(chain_h) == true) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + __stc_fw_set_chain(chain_h, target); + + ret = _stc_fw_chain_set((stc_fw_chain_s *)chain_h); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to set firewall cahin [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_chain_unset(stc_firewall_chain_h chain_h) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_chain_validity(chain_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (__stc_fw_check_chain_created(chain_h) == true) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_fw_chain_unset((stc_fw_chain_s *)chain_h); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to unset firewall cahin [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_create(stc_h stc, stc_firewall_rule_h *rule_h) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (!(_stc_handle_check_validity(stc))) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!rule_h) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = MALLOC0(stc_fw_rule_s, 1); + if (!fw_rule) { + STC_LOGE("Memory allocation failed"); + return STC_ERROR_OUT_OF_MEMORY; + } + + __stc_fw_init_rule(fw_rule); + __stc_fw_add_to_rule_list((stc_firewall_rule_h)fw_rule); + + *rule_h = (stc_firewall_rule_h)fw_rule; + + STC_LOGI("Firewall rule successfully created"); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_clone(stc_firewall_rule_h origin_h, + stc_firewall_rule_h *cloned_rule_h) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(origin_h) == false || + cloned_rule_h == NULL) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = MALLOC0(stc_fw_rule_s, 1); + if (!fw_rule) { + STC_LOGE("Memory allocation failed"); + return STC_ERROR_OUT_OF_MEMORY; + } + + __stc_fw_clone_rule(origin_h, fw_rule); + __stc_fw_add_to_rule_list((stc_firewall_rule_h)fw_rule); + + *cloned_rule_h = (stc_firewall_rule_h)fw_rule; + + STC_LOGI("Firewall rule successfully cloned"); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_destroy(stc_firewall_rule_h rule_h) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + __stc_fw_remove_from_rule_list(rule_h); + __stc_fw_deinit_rule(rule_h); + + STC_LOGI("Firewall rule successfully destroyed"); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_chain(stc_firewall_rule_h rule_h, char *chain) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!chain || strlen(chain) == 0) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + FREE(fw_rule->chain); + fw_rule->chain = g_strdup(chain); + + STC_LOGI("Chain [%s]", fw_rule->chain); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_direction(stc_firewall_rule_h rule_h, + stc_firewall_direction_e direction) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (direction > STC_FIREWALL_DIRECTION_OUT) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + fw_rule->direction = direction; + + STC_LOGI("Direction [%u]", fw_rule->direction); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_src_ip_type(stc_firewall_rule_h rule_h, + stc_firewall_ip_type_e type) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (type > STC_FIREWALL_IP_RANGE) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + fw_rule->s_ip_type = type; + + STC_LOGI("Source IP type [%u]", fw_rule->s_ip_type); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_dst_ip_type(stc_firewall_rule_h rule_h, + stc_firewall_ip_type_e type) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (type > STC_FIREWALL_IP_RANGE) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + fw_rule->d_ip_type = type; + + STC_LOGI("Destination IP type [%u]", fw_rule->d_ip_type); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_src_port_type(stc_firewall_rule_h rule_h, + stc_firewall_port_type_e type) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (type > STC_FIREWALL_PORT_RANGE) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + fw_rule->s_port_type = type; + + STC_LOGI("Source port type [%u]", fw_rule->s_port_type); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_dst_port_type(stc_firewall_rule_h rule_h, + stc_firewall_port_type_e type) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (type > STC_FIREWALL_PORT_RANGE) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + fw_rule->d_port_type = type; + + STC_LOGI("Destination port type [%u]", fw_rule->d_port_type); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_protocol_type(stc_firewall_rule_h rule_h, + stc_firewall_protocol_type_e type) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (type > STC_FIREWALL_PROTOCOL_ALL) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + fw_rule->protocol = type; + + STC_LOGI("Protocol type [%u]", fw_rule->protocol); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_family_type(stc_firewall_rule_h rule_h, + stc_firewall_family_type_e type) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (type > STC_FIREWALL_FAMILY_V6) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + fw_rule->family = type; + + STC_LOGI("Family type [%u]", fw_rule->family); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_src_ip(stc_firewall_rule_h rule_h, + char *ip1, char *ip2) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + FREE(fw_rule->s_ip1); + FREE(fw_rule->s_ip2); + fw_rule->s_ip1 = g_strdup(ip1); + fw_rule->s_ip2 = g_strdup(ip2); + + STC_LOGI("Source IP [%s:%s]", fw_rule->s_ip1, fw_rule->s_ip2); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_dst_ip(stc_firewall_rule_h rule_h, + char *ip1, char *ip2) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + FREE(fw_rule->d_ip1); + FREE(fw_rule->d_ip2); + fw_rule->d_ip1 = g_strdup(ip1); + fw_rule->d_ip2 = g_strdup(ip2); + + STC_LOGI("Destination IP [%s:%s]", fw_rule->d_ip1, fw_rule->d_ip2); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_src_port(stc_firewall_rule_h rule_h, + unsigned int port1, unsigned int port2) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + fw_rule->s_port1 = port1; + fw_rule->s_port2 = port2; + + STC_LOGI("Source port [%u]", fw_rule->s_port1, fw_rule->s_port2); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_dst_port(stc_firewall_rule_h rule_h, + unsigned int port1, unsigned int port2) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + fw_rule->d_port1 = port1; + fw_rule->d_port2 = port2; + + STC_LOGI("Destination port [%u]", fw_rule->d_port1, fw_rule->d_port2); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_ifname(stc_firewall_rule_h rule_h, char *ifname) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + FREE(fw_rule->ifname); + fw_rule->ifname = g_strdup(ifname); + + STC_LOGI("Interface name [%s]", fw_rule->ifname); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_set_target(stc_firewall_rule_h rule_h, + stc_firewall_rule_target_e target) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + fw_rule->target = target; + + STC_LOGI("Target [%s]", + _stc_convert_fw_rule_target_to_string(fw_rule->target)); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_chain(stc_firewall_rule_h rule_h, char **chain) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!chain) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *chain = g_strdup(fw_rule->chain); + + STC_LOGI("Chain [%s]", *chain); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_direction(stc_firewall_rule_h rule_h, + stc_firewall_direction_e *direction) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!direction) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *direction = fw_rule->direction; + + STC_LOGI("Direction [%s]", _stc_convert_fw_direction_to_string(*direction)); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_src_ip_type(stc_firewall_rule_h rule_h, + stc_firewall_ip_type_e *type) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!type) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *type = fw_rule->s_ip_type; + + STC_LOGI("Source IP type [%s]", _stc_convert_fw_ip_type_to_string(*type)); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_dst_ip_type(stc_firewall_rule_h rule_h, + stc_firewall_ip_type_e *type) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!type) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *type = fw_rule->d_ip_type; + + STC_LOGI("Destination IP type [%s]", _stc_convert_fw_ip_type_to_string(*type)); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_src_port_type(stc_firewall_rule_h rule_h, + stc_firewall_port_type_e *type) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!type) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *type = fw_rule->s_port_type; + + STC_LOGI("Source port type [%s]", _stc_convert_fw_port_type_to_string(*type)); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_dst_port_type(stc_firewall_rule_h rule_h, + stc_firewall_port_type_e *type) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!type) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *type = fw_rule->d_port_type; + + STC_LOGI("Destination port type [%s]", _stc_convert_fw_port_type_to_string(*type)); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_protocol_type(stc_firewall_rule_h rule_h, + stc_firewall_protocol_type_e *type) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!type) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *type = fw_rule->protocol; + + STC_LOGI("Protocol type [%s]", _stc_convert_fw_protocol_type_to_string(*type)); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_family_type(stc_firewall_rule_h rule_h, + stc_firewall_family_type_e *type) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!type) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *type = fw_rule->family; + + STC_LOGI("Address family type [%s]", _stc_convert_fw_family_type_to_string(*type)); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_src_ip(stc_firewall_rule_h rule_h, + char **ip1, char **ip2) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!ip1 || !ip2) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *ip1 = g_strdup(fw_rule->s_ip1); + *ip2 = g_strdup(fw_rule->s_ip2); + + STC_LOGI("Source IP [%s:%s]", *ip1, *ip2); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_dst_ip(stc_firewall_rule_h rule_h, + char **ip1, char **ip2) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!ip1 || !ip2) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *ip1 = g_strdup(fw_rule->d_ip1); + *ip2 = g_strdup(fw_rule->d_ip2); + + STC_LOGI("Destination IP [%s:%s]", *ip1, *ip2); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_src_port(stc_firewall_rule_h rule_h, + unsigned int *port1, unsigned int *port2) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!port1 || !port2) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *port1 = fw_rule->s_port1; + *port2 = fw_rule->s_port2; + + STC_LOGI("Source port [%u:%u]", *port1, *port2); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_dst_port(stc_firewall_rule_h rule_h, + unsigned int *port1, unsigned int *port2) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!port1 || !port2) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *port1 = fw_rule->d_port1; + *port2 = fw_rule->d_port2; + + STC_LOGI("Destination port [%u:%u]", *port1, *port2); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_ifname(stc_firewall_rule_h rule_h, char **ifname) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!ifname) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *ifname = g_strdup(fw_rule->ifname); + + STC_LOGI("Interface name [%s]", *ifname); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_get_target(stc_firewall_rule_h rule_h, + stc_firewall_rule_target_e *target) +{ + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (!target) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + stc_fw_rule_s *fw_rule = (stc_fw_rule_s *)rule_h; + + *target = fw_rule->target; + + STC_LOGI("Target [%s]", _stc_convert_fw_rule_target_to_string(*target)); + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_add(stc_firewall_rule_h rule_h) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (__stc_fw_check_rule_created(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_fw_rule_add((stc_fw_rule_s *)rule_h); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to add firewall rule [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_remove(stc_firewall_rule_h rule_h) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (__stc_fw_check_rule_created(rule_h) == true) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_fw_rule_remove((stc_fw_rule_s *)rule_h); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to remove firewall rule [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + return STC_ERROR_NONE; +} + +EXPORT_API int stc_firewall_rule_update(stc_firewall_rule_h rule_h) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (__stc_fw_check_rule_validity(rule_h) == false) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (__stc_fw_check_rule_created(rule_h) == true) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_fw_rule_update((stc_fw_rule_s *)rule_h); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to update firewall rule [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + return STC_ERROR_NONE; +} +EXPORT_API int stc_firewall_rule_foreach(stc_h stc, + stc_firewall_rule_info_cb callback, void *user_data) +{ + int ret = STC_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(TIZEN_FEATURE_STC_FW); + + if (!(_stc_handle_check_validity(stc))) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + if (callback == NULL) { + STC_LOGE("Invalid parameter"); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = _stc_fw_rule_foreach(callback, user_data); + if (ret != STC_ERROR_NONE) { + STC_LOGE("Failed to get all firewall rule [%s]", + _stc_convert_error_type_to_string(ret)); + return ret; + } + + return STC_ERROR_NONE; +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 48eda13..e2c487c 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -17,6 +17,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") SET(stc_test restriction.c statistics.c + firewall.c stc_menu.c stc_test.c ) diff --git a/test/firewall.c b/test/firewall.c new file mode 100755 index 0000000..e29daff --- /dev/null +++ b/test/firewall.c @@ -0,0 +1,1264 @@ +/* + * 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 restriction.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_chain_name[MENU_DATA_SIZE] = "STC_FIREWALL"; +static char g_chain_target[MENU_DATA_SIZE] = "1"; +static char g_chain_index[MENU_DATA_SIZE] = "0"; + +static char g_rule_chain[MENU_DATA_SIZE] = "STC_FIREWALL"; +static char g_rule_direction[MENU_DATA_SIZE] = "1"; +static char g_rule_s_ip_type[MENU_DATA_SIZE] = "1"; +static char g_rule_s_ip1[MENU_DATA_SIZE] = "102.132.217.5"; /* 2001:DB8::5 */ +static char g_rule_s_ip2[MENU_DATA_SIZE] = ""; +static char g_rule_d_ip_type[MENU_DATA_SIZE] = "2"; +static char g_rule_d_ip1[MENU_DATA_SIZE] = "102.132.217.10"; /* 2001:DB8::10 */ +static char g_rule_d_ip2[MENU_DATA_SIZE] = "255.255.255.0"; /* FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:0 */ +static char g_rule_s_port_type[MENU_DATA_SIZE] = "1"; +static char g_rule_s_port1[MENU_DATA_SIZE] = "80"; +static char g_rule_s_port2[MENU_DATA_SIZE] = "0"; +static char g_rule_d_port_type[MENU_DATA_SIZE] = "2"; +static char g_rule_d_port1[MENU_DATA_SIZE] = "0"; +static char g_rule_d_port2[MENU_DATA_SIZE] = "59136"; +static char g_rule_protocol[MENU_DATA_SIZE] = "1"; +static char g_rule_family[MENU_DATA_SIZE] = "1"; +static char g_rule_ifname[MENU_DATA_SIZE] = "seth_w0"; +static char g_rule_target[MENU_DATA_SIZE] = "1"; +static char g_rule_index[MENU_DATA_SIZE] = "0"; + +extern stc_h g_stc; +static stc_firewall_chain_h g_fw_chain_h = NULL; +static stc_firewall_rule_h g_fw_rule_h = NULL; +static GSList *g_chain_list = NULL; +static GSList *g_rule_list = NULL; + +static void __test_stc_fw_print_rule(stc_firewall_rule_h rule_h) +{ + int ret = STC_ERROR_NONE; + char *chain = NULL; + stc_firewall_direction_e direction = STC_FIREWALL_DIRECTION_NONE; + stc_firewall_ip_type_e s_ip_type = STC_FIREWALL_IP_NONE; + stc_firewall_ip_type_e d_ip_type = STC_FIREWALL_IP_NONE; + stc_firewall_port_type_e s_port_type = STC_FIREWALL_PORT_NONE; + stc_firewall_port_type_e d_port_type = STC_FIREWALL_PORT_NONE; + stc_firewall_protocol_type_e protocol = STC_FIREWALL_PROTOCOL_NONE; + stc_firewall_family_type_e family = STC_FIREWALL_FAMILY_NONE; + char *s_ip1 = NULL; + char *s_ip2 = NULL; + char *d_ip1 = NULL; + char *d_ip2 = NULL; + unsigned int s_port1 = 0; + unsigned int s_port2 = 0; + unsigned int d_port1 = 0; + unsigned int d_port2 = 0; + char *ifname = NULL; + stc_firewall_rule_target_e target = STC_FIREWALL_RULE_TARGET_NONE; + + ret = stc_firewall_rule_get_chain(rule_h, &chain); + if (ret == STC_ERROR_NONE) { + msg("Chain: " LOG_CYAN "[%s]" LOG_END, chain); + FREE(chain); + } + + ret = stc_firewall_rule_get_direction(rule_h, &direction); + if (ret == STC_ERROR_NONE) + msg("Direction: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_direction_to_string(direction)); + + ret = stc_firewall_rule_get_ifname(rule_h, &ifname); + if (ret == STC_ERROR_NONE) { + msg("Ifname: " LOG_CYAN "[%s]" LOG_END, ifname); + FREE(ifname); + } + + ret = stc_firewall_rule_get_family_type(rule_h, &family); + if (ret == STC_ERROR_NONE) + msg("Family: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_family_type_to_string(family)); + + ret = stc_firewall_rule_get_src_ip_type(rule_h, &s_ip_type); + if (ret == STC_ERROR_NONE) + msg("SrcIPtype: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_ip_type_to_string(s_ip_type)); + + ret = stc_firewall_rule_get_src_ip(rule_h, &s_ip1, &s_ip2); + if (ret == STC_ERROR_NONE) { + if (s_ip1 || s_ip2) + msg("SrcIP: " LOG_CYAN "[%s:%s]" LOG_END, s_ip1, s_ip2); + + FREE(s_ip1); + FREE(s_ip2); + } + + ret = stc_firewall_rule_get_dst_ip_type(rule_h, &d_ip_type); + if (ret == STC_ERROR_NONE) + msg("DstIPtype: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_ip_type_to_string(d_ip_type)); + + ret = stc_firewall_rule_get_dst_ip(rule_h, &d_ip1, &d_ip2); + if (ret == STC_ERROR_NONE) { + if (d_ip1 || d_ip2) + msg("DstIP: " LOG_CYAN "[%s:%s]" LOG_END, d_ip1, d_ip2); + + FREE(d_ip1); + FREE(d_ip2); + } + + ret = stc_firewall_rule_get_protocol_type(rule_h, &protocol); + if (ret == STC_ERROR_NONE) + msg("Protocol: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_protocol_type_to_string(protocol)); + + ret = stc_firewall_rule_get_src_port_type(rule_h, &s_port_type); + if (ret == STC_ERROR_NONE) + msg("SrcPorttype: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_port_type_to_string(s_port_type)); + + ret = stc_firewall_rule_get_src_port(rule_h, &s_port1, &s_port2); + if (ret == STC_ERROR_NONE) { + if (s_port1 || s_port2) + msg("SrcPort: " LOG_CYAN "[%u:%u]" LOG_END, s_port1, s_port2); + } + + ret = stc_firewall_rule_get_dst_port_type(rule_h, &d_port_type); + if (ret == STC_ERROR_NONE) + msg("DstPorttype: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_port_type_to_string(d_port_type)); + + ret = stc_firewall_rule_get_dst_port(rule_h, &d_port1, &d_port2); + if (ret == STC_ERROR_NONE) { + if (d_port1 || d_port2) + msg("DstPort: " LOG_CYAN "[%u:%u]" LOG_END, d_port1, d_port2); + } + + ret = stc_firewall_rule_get_target(rule_h, &target); + if (ret == STC_ERROR_NONE) + msg("Target: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_rule_target_to_string(target)); +} + +static int __test_stc_fw_print_all_rule(MManager *mm, struct menu_data *menu) +{ + GSList *list = NULL; + uint index = 0; + + msg(HR_DOUBLE); + msg(LOG_CYAN "[ Rule informations ]" LOG_END); + + for (list = g_rule_list; list; list = list->next) { + stc_firewall_rule_h rule_h = (stc_firewall_rule_h)list->data; + msg(HR_SINGLE); + msg("Index: " LOG_CYAN "[%u]" LOG_END, index); + __test_stc_fw_print_rule(rule_h); + ++index; + } + + msg(HR_DOUBLE); + return STC_ERROR_NONE; +} + +static void __test_stc_fw_clear_rule_list(void) +{ + if (g_rule_list) { + g_slist_free(g_rule_list); + g_rule_list = NULL; + } +} + +static stc_callback_ret_e __test_stc_fw_rule_info_cb(stc_firewall_rule_h info_h, + void *user_data) +{ + int ret = STC_ERROR_NONE; + stc_firewall_rule_h rule_h; + + ret = stc_firewall_rule_clone(info_h, &rule_h); + if (ret == STC_ERROR_NONE) + g_rule_list = g_slist_append(g_rule_list, rule_h); + + return TRUE; +} + +static int __test_stc_fw_set_rule_info(void) +{ + stc_firewall_direction_e direction = + (int)strtol(g_rule_direction, NULL, 10); + stc_firewall_ip_type_e s_ip_type = + (int)strtol(g_rule_s_ip_type, NULL, 10); + stc_firewall_ip_type_e d_ip_type = + (int)strtol(g_rule_d_ip_type, NULL, 10); + stc_firewall_port_type_e s_port_type = + (int)strtol(g_rule_s_port_type, NULL, 10); + stc_firewall_port_type_e d_port_type = + (int)strtol(g_rule_d_port_type, NULL, 10); + stc_firewall_protocol_type_e protocol = + (int)strtol(g_rule_protocol, NULL, 10); + stc_firewall_family_type_e family = + (int)strtol(g_rule_family, NULL, 10); + stc_firewall_rule_target_e target = + (int)strtol(g_rule_target, NULL, 10); + unsigned int s_port1 = + (unsigned int)strtol(g_rule_s_port1, NULL, 10); + unsigned int s_port2 = + (unsigned int)strtol(g_rule_s_port2, NULL, 10); + unsigned int d_port1 = + (unsigned int)strtol(g_rule_d_port1, NULL, 10); + unsigned int d_port2 = + (unsigned int)strtol(g_rule_d_port2, NULL, 10); + char *chain = NULL; + char *s_ip1 = NULL; + char *s_ip2 = NULL; + char *d_ip1 = NULL; + char *d_ip2 = NULL; + char *ifname = NULL; + int ret = STC_ERROR_NONE; + + if (strlen(g_rule_chain) == 0) { + msg(LOG_RED "Invalid parameter" LOG_END); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = stc_firewall_rule_set_chain(g_fw_rule_h, g_rule_chain); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set chain of rule" LOG_END); + else + msg("Fail to set chain of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + if (direction > STC_FIREWALL_DIRECTION_NONE && + direction <= STC_FIREWALL_DIRECTION_OUT) { + ret = stc_firewall_rule_set_direction(g_fw_rule_h, direction); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set direction of rule" LOG_END); + else + msg("Fail to set direction of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + if (s_ip_type > STC_FIREWALL_IP_NONE && + s_ip_type <= STC_FIREWALL_IP_RANGE) { + ret = stc_firewall_rule_set_src_ip_type(g_fw_rule_h, s_ip_type); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set source IP type of rule" LOG_END); + else + msg("Fail to set source IP type of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + if (d_ip_type > STC_FIREWALL_IP_NONE && + d_ip_type <= STC_FIREWALL_IP_RANGE) { + ret = stc_firewall_rule_set_dst_ip_type(g_fw_rule_h, d_ip_type); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set destination IP type of rule" LOG_END); + else + msg("Fail to set destination IP type of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + if (s_port_type > STC_FIREWALL_PORT_NONE && + s_port_type <= STC_FIREWALL_PORT_RANGE) { + ret = stc_firewall_rule_set_src_port_type(g_fw_rule_h, s_port_type); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set source port type of rule" LOG_END); + else + msg("Fail to set source port type of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + if (d_port_type > STC_FIREWALL_PORT_NONE && + d_port_type <= STC_FIREWALL_PORT_RANGE) { + ret = stc_firewall_rule_set_dst_port_type(g_fw_rule_h, d_port_type); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set destination port type of rule" LOG_END); + else + msg("Fail to set destination port type of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + if (protocol > STC_FIREWALL_PROTOCOL_NONE && + protocol <= STC_FIREWALL_PROTOCOL_ALL) { + ret = stc_firewall_rule_set_protocol_type(g_fw_rule_h, protocol); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set protocol type of rule" LOG_END); + else + msg("Fail to set protocol type of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + if (family > STC_FIREWALL_FAMILY_NONE && + family <= STC_FIREWALL_FAMILY_V6) { + ret = stc_firewall_rule_set_family_type(g_fw_rule_h, family); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set family type of rule" LOG_END); + else + msg("Fail to set family type of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + ret = stc_firewall_rule_set_src_ip(g_fw_rule_h, g_rule_s_ip1, g_rule_s_ip2); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set source IP of rule" LOG_END); + else + msg("Fail to set source IP of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + ret = stc_firewall_rule_set_dst_ip(g_fw_rule_h, g_rule_d_ip1, g_rule_d_ip2); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set destination IP of rule" LOG_END); + else + msg("Fail to set destination IP of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + ret = stc_firewall_rule_set_src_port(g_fw_rule_h, s_port1, s_port2); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set source port of rule" LOG_END); + else + msg("Fail to set source port of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + ret = stc_firewall_rule_set_dst_port(g_fw_rule_h, d_port1, d_port2); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set destination port of rule" LOG_END); + else + msg("Fail to set destination port of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + ret = stc_firewall_rule_set_ifname(g_fw_rule_h, g_rule_ifname); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set interface name of rule" LOG_END); + else + msg("Fail to set interface name of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + if (target > STC_FIREWALL_RULE_TARGET_NONE && + target <= STC_FIREWALL_RULE_TARGET_LOG) { + ret = stc_firewall_rule_set_target(g_fw_rule_h, target); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set target of rule" LOG_END); + else + msg("Fail to set target of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + msg(HR_SINGLE); + + ret = stc_firewall_rule_get_chain(g_fw_rule_h, &chain); + if (ret == STC_ERROR_NONE) { + msg("Chain: " LOG_CYAN "[%s]" LOG_END, chain); + FREE(chain); + } + + if (direction > STC_FIREWALL_DIRECTION_NONE && + direction <= STC_FIREWALL_DIRECTION_OUT) { + ret = stc_firewall_rule_get_direction(g_fw_rule_h, &direction); + if (ret == STC_ERROR_NONE) + msg("Direction: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_direction_to_string(direction)); + } + + ret = stc_firewall_rule_get_ifname(g_fw_rule_h, &ifname); + if (ret == STC_ERROR_NONE) { + if (ifname) + msg("Ifname: " LOG_CYAN "[%s]" LOG_END, ifname); + + FREE(ifname); + } + + ret = stc_firewall_rule_get_family_type(g_fw_rule_h, &family); + if (ret == STC_ERROR_NONE) + msg("Family: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_family_type_to_string(family)); + + ret = stc_firewall_rule_get_src_ip_type(g_fw_rule_h, &s_ip_type); + if (ret == STC_ERROR_NONE) + msg("SrcIPtype: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_ip_type_to_string(s_ip_type)); + + ret = stc_firewall_rule_get_src_ip(g_fw_rule_h, &s_ip1, &s_ip2); + if (ret == STC_ERROR_NONE) { + if (s_ip1 || s_ip2) + msg("SrcIP: " LOG_CYAN "[%s:%s]" LOG_END, s_ip1, s_ip2); + + FREE(s_ip1); + FREE(s_ip2); + } + + if (d_ip_type > STC_FIREWALL_IP_NONE && + d_ip_type <= STC_FIREWALL_IP_RANGE) { + ret = stc_firewall_rule_get_dst_ip_type(g_fw_rule_h, &d_ip_type); + if (ret == STC_ERROR_NONE) + msg("DstIPtype: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_ip_type_to_string(d_ip_type)); + } + + ret = stc_firewall_rule_get_dst_ip(g_fw_rule_h, &d_ip1, &d_ip2); + if (ret == STC_ERROR_NONE) { + if (d_ip1 || d_ip2) + msg("DstIP: " LOG_CYAN "[%s:%s]" LOG_END, d_ip1, d_ip2); + + FREE(d_ip1); + FREE(d_ip2); + } + + ret = stc_firewall_rule_get_protocol_type(g_fw_rule_h, &protocol); + if (ret == STC_ERROR_NONE) + msg("Protocol: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_protocol_type_to_string(protocol)); + + ret = stc_firewall_rule_get_src_port_type(g_fw_rule_h, &s_port_type); + if (ret == STC_ERROR_NONE) + msg("SrcPorttype: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_port_type_to_string(s_port_type)); + + ret = stc_firewall_rule_get_src_port(g_fw_rule_h, &s_port1, &s_port2); + if (ret == STC_ERROR_NONE) { + if (s_port1 || s_port2) + msg("SrcPort: " LOG_CYAN "[%u:%u]" LOG_END, s_port1, s_port2); + } + + ret = stc_firewall_rule_get_dst_port_type(g_fw_rule_h, &d_port_type); + if (ret == STC_ERROR_NONE) + msg("DstPorttype: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_port_type_to_string(d_port_type)); + + ret = stc_firewall_rule_get_dst_port(g_fw_rule_h, &d_port1, &d_port2); + if (ret == STC_ERROR_NONE) { + if (d_port1 || d_port2) + msg("DstPort: " LOG_CYAN "[%u:%u]" LOG_END, d_port1, d_port2); + } + + ret = stc_firewall_rule_get_target(g_fw_rule_h, &target); + if (ret == STC_ERROR_NONE) + msg("Target: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_rule_target_to_string(target)); + + msg(HR_SINGLE); + + return STC_ERROR_NONE; +} + +static int __test_stc_fw_update_rule_info(stc_firewall_rule_h rule_h) +{ + stc_firewall_direction_e direction = + (int)strtol(g_rule_direction, NULL, 10); + stc_firewall_ip_type_e s_ip_type = + (int)strtol(g_rule_s_ip_type, NULL, 10); + stc_firewall_ip_type_e d_ip_type = + (int)strtol(g_rule_d_ip_type, NULL, 10); + stc_firewall_port_type_e s_port_type = + (int)strtol(g_rule_s_port_type, NULL, 10); + stc_firewall_port_type_e d_port_type = + (int)strtol(g_rule_d_port_type, NULL, 10); + stc_firewall_protocol_type_e protocol = + (int)strtol(g_rule_protocol, NULL, 10); + stc_firewall_family_type_e family = + (int)strtol(g_rule_family, NULL, 10); + stc_firewall_rule_target_e target = + (int)strtol(g_rule_target, NULL, 10); + unsigned int s_port1 = + (unsigned int)strtol(g_rule_s_port1, NULL, 10); + unsigned int s_port2 = + (unsigned int)strtol(g_rule_s_port2, NULL, 10); + unsigned int d_port1 = + (unsigned int)strtol(g_rule_d_port1, NULL, 10); + unsigned int d_port2 = + (unsigned int)strtol(g_rule_d_port2, NULL, 10); + char *chain = NULL; + char *s_ip1 = NULL; + char *s_ip2 = NULL; + char *d_ip1 = NULL; + char *d_ip2 = NULL; + char *ifname = NULL; + int ret = STC_ERROR_NONE; + + if (direction > STC_FIREWALL_DIRECTION_NONE && + direction <= STC_FIREWALL_DIRECTION_OUT) { + ret = stc_firewall_rule_set_direction(rule_h, direction); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set direction of rule" LOG_END); + else + msg("Fail to set direction of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + if (s_ip_type > STC_FIREWALL_IP_NONE && + s_ip_type <= STC_FIREWALL_IP_RANGE) { + ret = stc_firewall_rule_set_src_ip_type(rule_h, s_ip_type); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set source IP type of rule" LOG_END); + else + msg("Fail to set source IP type of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + if (d_ip_type > STC_FIREWALL_IP_NONE && + d_ip_type <= STC_FIREWALL_IP_RANGE) { + ret = stc_firewall_rule_set_dst_ip_type(rule_h, d_ip_type); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set destination IP type of rule" LOG_END); + else + msg("Fail to set destination IP type of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + if (s_port_type > STC_FIREWALL_PORT_NONE && + s_port_type <= STC_FIREWALL_PORT_RANGE) { + ret = stc_firewall_rule_set_src_port_type(rule_h, s_port_type); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set source port type of rule" LOG_END); + else + msg("Fail to set source port type of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + if (d_port_type > STC_FIREWALL_PORT_NONE && + d_port_type <= STC_FIREWALL_PORT_RANGE) { + ret = stc_firewall_rule_set_dst_port_type(rule_h, d_port_type); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set destination port type of rule" LOG_END); + else + msg("Fail to set destination port type of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + if (protocol > STC_FIREWALL_PROTOCOL_NONE && + protocol <= STC_FIREWALL_PROTOCOL_ALL) { + ret = stc_firewall_rule_set_protocol_type(rule_h, protocol); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set protocol type of rule" LOG_END); + else + msg("Fail to set protocol type of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + if (family > STC_FIREWALL_FAMILY_NONE && + family <= STC_FIREWALL_FAMILY_V6) { + ret = stc_firewall_rule_set_family_type(rule_h, family); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set family type of rule" LOG_END); + else + msg("Fail to set family type of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + ret = stc_firewall_rule_set_src_ip(rule_h, g_rule_s_ip1, g_rule_s_ip2); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set source IP of rule" LOG_END); + else + msg("Fail to set source IP of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + ret = stc_firewall_rule_set_dst_ip(rule_h, g_rule_d_ip1, g_rule_d_ip2); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set destination IP of rule" LOG_END); + else + msg("Fail to set destination IP of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + ret = stc_firewall_rule_set_src_port(rule_h, s_port1, s_port2); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set source port of rule" LOG_END); + else + msg("Fail to set source port of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + ret = stc_firewall_rule_set_dst_port(rule_h, d_port1, d_port2); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set destination port of rule" LOG_END); + else + msg("Fail to set destination port of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + ret = stc_firewall_rule_set_ifname(rule_h, g_rule_ifname); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set interface name of rule" LOG_END); + else + msg("Fail to set interface name of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + if (target > STC_FIREWALL_RULE_TARGET_NONE && + target <= STC_FIREWALL_RULE_TARGET_LOG) { + ret = stc_firewall_rule_set_target(rule_h, target); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set target of rule" LOG_END); + else + msg("Fail to set target of rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + } + + msg(HR_SINGLE); + + ret = stc_firewall_rule_get_chain(rule_h, &chain); + if (ret == STC_ERROR_NONE) { + msg("Chain: " LOG_CYAN "[%s]" LOG_END, chain); + FREE(chain); + } + + if (direction > STC_FIREWALL_DIRECTION_NONE && + direction <= STC_FIREWALL_DIRECTION_OUT) { + ret = stc_firewall_rule_get_direction(rule_h, &direction); + if (ret == STC_ERROR_NONE) + msg("Direction: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_direction_to_string(direction)); + } + + ret = stc_firewall_rule_get_ifname(rule_h, &ifname); + if (ret == STC_ERROR_NONE) { + if (ifname) + msg("Ifname: " LOG_CYAN "[%s]" LOG_END, ifname); + + FREE(ifname); + } + + ret = stc_firewall_rule_get_family_type(rule_h, &family); + if (ret == STC_ERROR_NONE) + msg("Family: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_family_type_to_string(family)); + + ret = stc_firewall_rule_get_src_ip_type(rule_h, &s_ip_type); + if (ret == STC_ERROR_NONE) + msg("SrcIPtype: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_ip_type_to_string(s_ip_type)); + + ret = stc_firewall_rule_get_src_ip(rule_h, &s_ip1, &s_ip2); + if (ret == STC_ERROR_NONE) { + if (s_ip1 || s_ip2) + msg("SrcIP: " LOG_CYAN "[%s:%s]" LOG_END, s_ip1, s_ip2); + + FREE(s_ip1); + FREE(s_ip2); + } + + if (d_ip_type > STC_FIREWALL_IP_NONE && + d_ip_type <= STC_FIREWALL_IP_RANGE) { + ret = stc_firewall_rule_get_dst_ip_type(rule_h, &d_ip_type); + if (ret == STC_ERROR_NONE) + msg("DstIPtype: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_ip_type_to_string(d_ip_type)); + } + + ret = stc_firewall_rule_get_dst_ip(rule_h, &d_ip1, &d_ip2); + if (ret == STC_ERROR_NONE) { + if (d_ip1 || d_ip2) + msg("DstIP: " LOG_CYAN "[%s:%s]" LOG_END, d_ip1, d_ip2); + + FREE(d_ip1); + FREE(d_ip2); + } + + ret = stc_firewall_rule_get_protocol_type(rule_h, &protocol); + if (ret == STC_ERROR_NONE) + msg("Protocol: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_protocol_type_to_string(protocol)); + + ret = stc_firewall_rule_get_src_port_type(rule_h, &s_port_type); + if (ret == STC_ERROR_NONE) + msg("SrcPorttype: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_port_type_to_string(s_port_type)); + + ret = stc_firewall_rule_get_src_port(rule_h, &s_port1, &s_port2); + if (ret == STC_ERROR_NONE) { + if (s_port1 || s_port2) + msg("SrcPort: " LOG_CYAN "[%u:%u]" LOG_END, s_port1, s_port2); + } + + ret = stc_firewall_rule_get_dst_port_type(rule_h, &d_port_type); + if (ret == STC_ERROR_NONE) + msg("DstPorttype: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_port_type_to_string(d_port_type)); + + ret = stc_firewall_rule_get_dst_port(rule_h, &d_port1, &d_port2); + if (ret == STC_ERROR_NONE) { + if (d_port1 || d_port2) + msg("DstPort: " LOG_CYAN "[%u:%u]" LOG_END, d_port1, d_port2); + } + + ret = stc_firewall_rule_get_target(rule_h, &target); + if (ret == STC_ERROR_NONE) + msg("Target: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_rule_target_to_string(target)); + + msg(HR_SINGLE); + + return STC_ERROR_NONE; +} + +static int __test_stc_fw_create_rule(void) +{ + int ret = STC_ERROR_NONE; + + ret = stc_firewall_rule_create(g_stc, &g_fw_rule_h); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to create firewall rule" LOG_END); + else + msg("Fail to create firewall rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static int __test_stc_fw_destroy_rule(void) +{ + int ret = STC_ERROR_NONE; + + ret = stc_firewall_rule_destroy(g_fw_rule_h); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to destroy firewall rule" LOG_END); + else + msg("Fail to destroy firewall rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static int _test_stc_fw_add_rule(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + + ret = __test_stc_fw_create_rule(); + if (ret != STC_ERROR_NONE) + return ret; + + ret = __test_stc_fw_set_rule_info(); + if (ret != STC_ERROR_NONE) + return ret; + + ret = stc_firewall_rule_add(g_fw_rule_h); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to add firewall rule" LOG_END); + else + msg("Fail to add firewall rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + ret = __test_stc_fw_destroy_rule(); + return ret; +} + +static int _test_stc_fw_get_all_rule(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + + __test_stc_fw_clear_rule_list(); + + ret = stc_firewall_rule_foreach(g_stc, __test_stc_fw_rule_info_cb, NULL); + + __test_stc_fw_print_all_rule(mm, menu); + + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to get all firewall rule" LOG_END); + else + msg("Fail to get all firewall rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static int _test_stc_fw_update_rule(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + stc_firewall_rule_h rule_h = g_slist_nth_data(g_rule_list, + (unsigned int)strtol(g_rule_index, NULL, 10)); + + if (rule_h == NULL) { + msg(LOG_RED "Invalid index parameter" LOG_END); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = __test_stc_fw_update_rule_info(rule_h); + if (ret != STC_ERROR_NONE) + return ret; + + ret = stc_firewall_rule_update(rule_h); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to update firewall rule" LOG_END); + else + msg("Fail to update firewall rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static int _test_stc_fw_remove_rule(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + stc_firewall_rule_h rule_h = g_slist_nth_data(g_rule_list, + (unsigned int)strtol(g_rule_index, NULL, 10)); + + if (rule_h == NULL) { + msg(LOG_RED "Invalid index parameter" LOG_END); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = stc_firewall_rule_remove(rule_h); + if (ret == STC_ERROR_NONE) { + msg(LOG_GREEN "Success to remove firewall rule" LOG_END); + ret = stc_firewall_rule_destroy(rule_h); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to destroy firewall rule" LOG_END); + else + msg("Fail to destroy firewall rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + g_rule_list = g_slist_remove(g_rule_list, rule_h); + + } else + msg("Fail to remove firewall rule " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static void __test_stc_fw_print_chain(stc_firewall_chain_h chain_h) +{ + int ret = STC_ERROR_NONE; + char *chain = NULL; + stc_firewall_chain_target_e target = STC_FIREWALL_CHAIN_TARGET_NONE; + + ret = stc_firewall_chain_get_name(chain_h, &chain); + if (ret == STC_ERROR_NONE) + msg("Chain: " LOG_CYAN "[%s]" LOG_END, chain); + + ret = stc_firewall_chain_get_target(chain_h, &target); + if (ret == STC_ERROR_NONE) + msg("Target: " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_chain_target_to_string(target)); + + FREE(chain); +} + +static int __test_stc_fw_print_all_chain(MManager *mm, struct menu_data *menu) +{ + GSList *list = NULL; + uint index = 0; + + msg(HR_DOUBLE); + msg(LOG_CYAN "[ Chain informations ]" LOG_END); + + for (list = g_chain_list; list; list = list->next) { + stc_firewall_chain_h chain_h = (stc_firewall_chain_h)list->data; + msg(HR_SINGLE); + msg("Index: " LOG_CYAN "[%u]" LOG_END, index); + __test_stc_fw_print_chain(chain_h); + ++index; + } + + msg(HR_DOUBLE); + return STC_ERROR_NONE; +} + +static void __test_stc_fw_clear_chain_list(void) +{ + if (g_chain_list) { + g_slist_free(g_chain_list); + g_chain_list = NULL; + } +} + +static stc_callback_ret_e __test_stc_fw_chain_info_cb(stc_firewall_chain_h info_h, + void *user_data) +{ + int ret = STC_ERROR_NONE; + stc_firewall_chain_h chain_h; + + ret = stc_firewall_chain_clone(info_h, &chain_h); + if (ret == STC_ERROR_NONE) + g_chain_list = g_slist_append(g_chain_list, chain_h); + + return TRUE; +} + +static int __test_stc_fw_create_chain(void) +{ + int ret = STC_ERROR_NONE; + + ret = stc_firewall_chain_create(g_stc, g_chain_name, &g_fw_chain_h); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to create firewall chain" LOG_END); + else + msg("Fail to create firewall chain " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static int __test_stc_fw_destroy_chain(void) +{ + int ret = STC_ERROR_NONE; + + ret = stc_firewall_chain_destroy(g_fw_chain_h); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to destroy firewall chain" LOG_END); + else + msg("Fail to destroy firewall chain " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static void __test_stc_fw_rule_remove(gpointer data, gpointer user_data) +{ + stc_firewall_rule_h rule_h = (stc_firewall_rule_h)data; + stc_firewall_chain_h chain_h = (stc_firewall_chain_h)user_data; + char *name = NULL; + char *chain = NULL; + int ret = STC_ERROR_NONE; + + ret = stc_firewall_chain_get_name(chain_h, &name); + if (ret != STC_ERROR_NONE) + return; + + ret = stc_firewall_rule_get_chain(rule_h, &chain); + if (ret != STC_ERROR_NONE) { + FREE(name); + return; + } + + if (!g_strcmp0(name, chain)) + g_rule_list = g_slist_remove(g_rule_list, rule_h); + + FREE(name); + FREE(chain); + return; +} + +static int _test_stc_fw_add_chain(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + + ret = __test_stc_fw_create_chain(); + if (ret != STC_ERROR_NONE) + return ret; + + ret = stc_firewall_chain_add(g_fw_chain_h); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to add firewall chain" LOG_END); + else + msg("Fail to add firewall chain " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + ret = __test_stc_fw_destroy_chain(); + return ret; +} + +static int _test_stc_fw_get_all_chain(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + + __test_stc_fw_clear_chain_list(); + + ret = stc_firewall_chain_foreach(g_stc, __test_stc_fw_chain_info_cb, NULL); + + __test_stc_fw_print_all_chain(mm, menu); + + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to get all firewall chain" LOG_END); + else + msg("Fail to get all firewall chain " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static int _test_stc_fw_flush_chain(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + stc_firewall_chain_h chain_h = g_slist_nth_data(g_chain_list, + (unsigned int)strtol(g_chain_index, NULL, 10)); + if (chain_h == NULL) { + msg(LOG_RED "Invalid index parameter" LOG_END); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = stc_firewall_chain_flush(chain_h); + if (ret == STC_ERROR_NONE) { + msg(LOG_GREEN "Success to flush firewall chain" LOG_END); + + g_slist_foreach(g_rule_list, __test_stc_fw_rule_remove, chain_h); + } else + msg("Fail to flush firewall chain " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static int _test_stc_fw_remove_chain(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + stc_firewall_chain_h chain_h = g_slist_nth_data(g_chain_list, + (unsigned int)strtol(g_chain_index, NULL, 10)); + if (chain_h == NULL) { + msg(LOG_RED "Invalid index parameter" LOG_END); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = stc_firewall_chain_remove(chain_h); + if (ret == STC_ERROR_NONE) { + msg(LOG_GREEN "Success to remove firewall chain" LOG_END); + ret = stc_firewall_chain_destroy(chain_h); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to destroy firewall chain" LOG_END); + else + msg("Fail to destroy firewall chain " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + g_chain_list = g_slist_remove(g_chain_list, chain_h); + + } else + msg("Fail to remove firewall chain " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static int _test_stc_fw_set_chain(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + unsigned int target = (unsigned int)strtol(g_chain_target, NULL, 10); + stc_firewall_chain_h chain_h = g_slist_nth_data(g_chain_list, + (unsigned int)strtol(g_chain_index, NULL, 10)); + + if (chain_h == NULL) { + msg(LOG_RED "Invalid index parameter" LOG_END); + return STC_ERROR_INVALID_PARAMETER; + } + + if (target > STC_FIREWALL_CHAIN_TARGET_OUTPUT) { + msg(LOG_RED "Invalid target parameter" LOG_END); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = stc_firewall_chain_set(chain_h, target); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to set firewall chain" LOG_END); + else + msg("Fail to set firewall chain " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static int _test_stc_fw_unset_chain(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + stc_firewall_chain_h chain_h = g_slist_nth_data(g_chain_list, + (unsigned int)strtol(g_chain_index, NULL, 10)); + + if (chain_h == NULL) { + msg(LOG_RED "Invalid index parameter" LOG_END); + return STC_ERROR_INVALID_PARAMETER; + } + + ret = stc_firewall_chain_unset(chain_h); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to unset firewall chain" LOG_END); + else + msg("Fail to unset firewall chain " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static int _test_stc_fw_lock(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + + ret = stc_firewall_lock(g_stc); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to lock firewall state" LOG_END); + else + msg("Fail to lock firewall state " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static int _test_stc_fw_unlock(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + + ret = stc_firewall_unlock(g_stc); + if (ret == STC_ERROR_NONE) + msg(LOG_GREEN "Success to unlock firewall state" LOG_END); + else + msg("Fail to unlock firewall state " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static int _test_stc_fw_get_lock(MManager *mm, struct menu_data *menu) +{ + int ret = STC_ERROR_NONE; + stc_firewall_lock_e state = STC_FIREWALL_UNKNOWN; + + ret = stc_firewall_get_lock(g_stc, &state); + if (ret == STC_ERROR_NONE) { + msg("Firewall lock state " LOG_CYAN "[%s]" LOG_END, + test_stc_convert_fw_lock_to_string(state)); + msg(LOG_GREEN "Success to get firewall state" LOG_END); + } else + msg("Fail to unlock firewall state " LOG_RED "[%s]" LOG_END, + test_stc_convert_error_type_to_string(ret)); + + return ret; +} + +static struct menu_data menu_firewall_rule_add[] = { + { "1", "Chain", NULL, NULL, g_rule_chain}, + { "2", "Direction(0.None/1.IN/2.OUT)", NULL, NULL, g_rule_direction}, + { "3", "Src IP type(0.None/1.Single/2.Mask/3.Range)", NULL, NULL, g_rule_s_ip_type}, + { "31", "Src IP1", NULL, NULL, g_rule_s_ip1}, + { "32", "Src IP2", NULL, NULL, g_rule_s_ip2}, + { "4", "Dst IP type(0.None/1.Single/2.Mask/3.Range)", NULL, NULL, g_rule_d_ip_type}, + { "41", "Dst IP1", NULL, NULL, g_rule_d_ip1}, + { "42", "Dst IP2", NULL, NULL, g_rule_d_ip2}, + { "5", "Src Port type(0.None/1.Single/2.Range)", NULL, NULL, g_rule_s_port_type}, + { "51", "Src Port1", NULL, NULL, g_rule_s_port1}, + { "52", "Src Port2", NULL, NULL, g_rule_s_port2}, + { "6", "Dst Port type(0.None/1.Single/2.Range)", NULL, NULL, g_rule_d_port_type}, + { "61", "Dst Port1", NULL, NULL, g_rule_d_port1}, + { "62", "Dst Port2", NULL, NULL, g_rule_d_port2}, + { "7", "Protocol type(0.None/1.TCP/2.UDP/3.ICMP/4.ALL)", NULL, NULL, g_rule_protocol}, + { "8", "Family(0.None/1.IPv4/2.IPv6)", NULL, NULL, g_rule_family}, + { "9", "Interface name", NULL, NULL, g_rule_ifname}, + { "0", "Target(0.None/1.ACCEPT/2.DROP/3.LOG)", NULL, NULL, g_rule_target}, + { "a", LOG_LIGHTBLUE "[Add]" LOG_END " rule", NULL, _test_stc_fw_add_rule, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_firewall_rule_update[] = { + { "1", "Index", NULL, NULL, g_rule_index}, + { "2", "Direction(0.None/1.IN/2.OUT)", NULL, NULL, g_rule_direction}, + { "3", "Src IP type(0.None/1.Single/2.Mask/3.Range)", NULL, NULL, g_rule_s_ip_type}, + { "31", "Src IP1", NULL, NULL, g_rule_s_ip1}, + { "32", "Src IP2", NULL, NULL, g_rule_s_ip2}, + { "4", "Dst IP type(0.None/1.Single/2.Mask/3.Range)", NULL, NULL, g_rule_d_ip_type}, + { "41", "Dst IP1", NULL, NULL, g_rule_d_ip1}, + { "42", "Dst IP2", NULL, NULL, g_rule_d_ip2}, + { "5", "Src Port type(0.None/1.Single/2.Range)", NULL, NULL, g_rule_s_port_type}, + { "51", "Src Port1", NULL, NULL, g_rule_s_port1}, + { "52", "Src Port2", NULL, NULL, g_rule_s_port2}, + { "6", "Dst Port type(0.None/1.Single/2.Range)", NULL, NULL, g_rule_d_port_type}, + { "61", "Dst Port1", NULL, NULL, g_rule_d_port1}, + { "62", "Dst Port2", NULL, NULL, g_rule_d_port2}, + { "7", "Protocol type(0.None/1.TCP/2.UDP/3.ICMP/4.ALL)", NULL, NULL, g_rule_protocol}, + { "8", "Family(0.None/1.IPv4/2.IPv6)", NULL, NULL, g_rule_family}, + { "9", "Interface name", NULL, NULL, g_rule_ifname}, + { "0", "Target(0.None/1.ACCEPT/2.DROP/3.LOG)", NULL, NULL, g_rule_target}, + { "u", LOG_LIGHTBLUE "[Update]" LOG_END " rule", NULL, _test_stc_fw_update_rule, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_firewall_rule_remove[] = { + { "1", "Index", NULL, NULL, g_rule_index}, + { "r", LOG_LIGHTBLUE "[Remove]" LOG_END " rule", NULL, _test_stc_fw_remove_rule, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_firewall_rule[] = { + { "1", LOG_LIGHTBLUE "[Add]" LOG_END " rule", menu_firewall_rule_add, NULL, NULL}, + { "2", LOG_LIGHTBLUE "[GetAll]" LOG_END " rules", NULL, _test_stc_fw_get_all_rule, NULL}, + { "3", LOG_LIGHTMAGENTA "[Update]" LOG_END " rule", menu_firewall_rule_update, + __test_stc_fw_print_all_rule, NULL}, + { "4", LOG_LIGHTMAGENTA "[Remove]" LOG_END " rule", menu_firewall_rule_remove, + __test_stc_fw_print_all_rule, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_firewall_chain_add[] = { + { "1", "Chain", NULL, NULL, g_chain_name}, + { "a", LOG_LIGHTBLUE "[Add]" LOG_END " chain", NULL, _test_stc_fw_add_chain, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_firewall_chain_flush[] = { + { "1", "Index", NULL, NULL, g_chain_index}, + { "f", LOG_LIGHTMAGENTA "[Flush]" LOG_END, NULL, _test_stc_fw_flush_chain, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_firewall_chain_remove[] = { + { "1", "Index", NULL, NULL, g_chain_index}, + { "r", LOG_LIGHTMAGENTA "[Remove]" LOG_END, NULL, _test_stc_fw_remove_chain, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_firewall_chain_set[] = { + { "1", "Index", NULL, NULL, g_chain_index}, + { "2", "Target(0.None/1.INPUT/2.OUTPUT)", NULL, NULL, g_chain_target}, + { "s", LOG_LIGHTMAGENTA "[Set]" LOG_END, NULL, _test_stc_fw_set_chain, NULL}, + { NULL, NULL, }, +}; + +static struct menu_data menu_firewall_chain_unset[] = { + { "1", "Index", NULL, NULL, g_chain_index}, + { "u", LOG_LIGHTMAGENTA "[Unset]" LOG_END, NULL, _test_stc_fw_unset_chain, NULL}, + { NULL, NULL, }, +}; + +struct menu_data menu_firewall_chain[] = { + { "1", LOG_LIGHTBLUE "[Add]" LOG_END " chain", menu_firewall_chain_add, NULL, NULL}, + { "2", LOG_LIGHTBLUE "[GetAll]" LOG_END " chains", NULL, _test_stc_fw_get_all_chain, NULL}, + { "3", LOG_LIGHTMAGENTA "[Flush]" LOG_END " chain", menu_firewall_chain_flush, + __test_stc_fw_print_all_chain, NULL}, + { "4", LOG_LIGHTMAGENTA "[Remove]" LOG_END " chain", menu_firewall_chain_remove, + __test_stc_fw_print_all_chain, NULL}, + { "5", LOG_LIGHTMAGENTA "[Set]" LOG_END " chain", menu_firewall_chain_set, + __test_stc_fw_print_all_chain, NULL}, + { "6", LOG_LIGHTMAGENTA "[Unset]" LOG_END " chain", menu_firewall_chain_unset, + __test_stc_fw_print_all_chain, NULL}, + { NULL, NULL, }, +}; + +struct menu_data menu_firewall_lock[] = { + { "1", LOG_LIGHTMAGENTA "[Lock]" LOG_END " state", NULL, _test_stc_fw_lock, NULL}, + { "2", LOG_LIGHTBLUE "[Unlock]" LOG_END " state", NULL, _test_stc_fw_unlock, NULL}, + { "3", LOG_LIGHTBLUE "[Getlock]" LOG_END " state", NULL, _test_stc_fw_get_lock, NULL}, + { NULL, NULL, }, +}; + +struct menu_data menu_firewall[] = { + { "0", LOG_LIGHTMAGENTA "[Lock]" LOG_END, menu_firewall_lock, NULL, NULL}, + { "1", LOG_LIGHTBLUE "[Chain]" LOG_END, menu_firewall_chain, NULL, NULL}, + { "2", LOG_LIGHTBLUE "[Rule]" LOG_END, menu_firewall_rule, NULL, NULL}, + { NULL, NULL, }, +}; diff --git a/test/firewall.h b/test/firewall.h new file mode 100755 index 0000000..670b466 --- /dev/null +++ b/test/firewall.h @@ -0,0 +1,36 @@ +/* + * 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 firewall.h + * @author Tak hyunuk (hyunuk.tak@samsung.com) + * @version 0.1 + */ + +#ifndef __TEST_STC_FIREWALL_H__ +#define __TEST_STC_FIREWALL_H__ + +/***************************************************************************** + * Local Functions Definition + *****************************************************************************/ + +#endif /** __TEST_STC_FIREWALL_H__ */ + diff --git a/test/stc_test.c b/test/stc_test.c index 63ab417..ef3be40 100755 --- a/test/stc_test.c +++ b/test/stc_test.c @@ -40,6 +40,7 @@ #include "stc_menu.h" #include "statistics.h" #include "restriction.h" +#include "firewall.h" #define STC_APP_ID_LEN 128 #define STC_IFNAME_LEN 16 @@ -47,6 +48,7 @@ extern struct menu_data menu_statistics[]; extern struct menu_data menu_restriction[]; +extern struct menu_data menu_firewall[]; stc_h g_stc = NULL; @@ -213,9 +215,136 @@ const char *test_stc_convert_period_to_string( } } +const char *test_stc_convert_fw_lock_to_string( + stc_firewall_lock_e state) +{ + switch (state) { + case STC_FIREWALL_UNLOCKED: + return "Unlocked"; + case STC_FIREWALL_LOCKED: + return "Locked"; + default: + return "Unknown"; + } +} + +const char *test_stc_convert_fw_direction_to_string( + stc_firewall_direction_e direction) +{ + switch (direction) { + case STC_FIREWALL_DIRECTION_NONE: + return "None"; + case STC_FIREWALL_DIRECTION_IN: + return "In"; + case STC_FIREWALL_DIRECTION_OUT: + return "Out"; + default: + return "Unknown"; + } +} + +const char *test_stc_convert_fw_ip_type_to_string( + stc_firewall_ip_type_e type) +{ + switch (type) { + case STC_FIREWALL_IP_NONE: + return "None"; + case STC_FIREWALL_IP_SINGLE: + return "Single"; + case STC_FIREWALL_IP_MASK: + return "Mask"; + case STC_FIREWALL_IP_RANGE: + return "Range"; + default: + return "Unknown"; + } +} + +const char *test_stc_convert_fw_port_type_to_string( + stc_firewall_port_type_e type) +{ + switch (type) { + case STC_FIREWALL_PORT_NONE: + return "None"; + case STC_FIREWALL_PORT_SINGLE: + return "Single"; + case STC_FIREWALL_PORT_RANGE: + return "Range"; + default: + return "Unknown"; + } +} + +const char *test_stc_convert_fw_protocol_type_to_string( + stc_firewall_protocol_type_e type) +{ + switch (type) { + case STC_FIREWALL_PROTOCOL_NONE: + return "None"; + case STC_FIREWALL_PROTOCOL_TCP: + return "TCP"; + case STC_FIREWALL_PROTOCOL_UDP: + return "UDP"; + case STC_FIREWALL_PROTOCOL_ICMP: + return "ICMP"; + case STC_FIREWALL_PROTOCOL_ALL: + return "All"; + default: + return "Unknown"; + } +} + +const char *test_stc_convert_fw_family_type_to_string( + stc_firewall_family_type_e type) +{ + switch (type) { + case STC_FIREWALL_FAMILY_NONE: + return "None"; + case STC_FIREWALL_FAMILY_V4: + return "IPv4"; + case STC_FIREWALL_FAMILY_V6: + return "IPv6"; + default: + return "Unknown"; + } +} + +const char *test_stc_convert_fw_chain_target_to_string( + stc_firewall_chain_target_e target) +{ + switch (target) { + case STC_FIREWALL_CHAIN_TARGET_NONE: + return "NONE"; + case STC_FIREWALL_CHAIN_TARGET_INPUT: + return "INPUT"; + case STC_FIREWALL_CHAIN_TARGET_OUTPUT: + return "OUTPUT"; + default: + return "Unknown"; + } +} + +const char *test_stc_convert_fw_rule_target_to_string( + stc_firewall_rule_target_e target) +{ + switch (target) { + case STC_FIREWALL_RULE_TARGET_NONE: + return "NONE"; + case STC_FIREWALL_RULE_TARGET_ACCEPT: + return "ACCEPT"; + case STC_FIREWALL_RULE_TARGET_DROP: + return "DROP"; + case STC_FIREWALL_RULE_TARGET_LOG: + return "LOG"; + default: + return "Unknown"; + } +} + 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}, { NULL, NULL, }, }; diff --git a/test/stc_test.h b/test/stc_test.h index 140ae09..462b2f2 100755 --- a/test/stc_test.h +++ b/test/stc_test.h @@ -28,6 +28,15 @@ /***************************************************************************** * Local Functions Definition *****************************************************************************/ + +#define MALLOC0(t, n) ((t*) g_try_malloc0((n) * sizeof(t))) +#define FREE(p) do { \ + if (p) { \ + g_free(p); \ + p = NULL; \ + } \ +} while (0) + const char *test_stc_convert_error_type_to_string(stc_error_e err); const char *test_stc_convert_iface_type_to_string(stc_iface_type_e type); const char *test_stc_convert_roaming_to_string(stc_roaming_type_e type); @@ -36,4 +45,20 @@ const char *test_stc_convert_protocol_to_string(stc_protocol_type_e type); const char *test_stc_convert_process_state_to_string(stc_process_state_e state); const char *test_stc_convert_time_period_to_string(stc_time_period_e time); const char *test_stc_convert_period_to_string(stc_conn_period_e period); +const char *test_stc_convert_fw_lock_to_string( + stc_firewall_lock_e state); +const char *test_stc_convert_fw_direction_to_string( + stc_firewall_direction_e direction); +const char *test_stc_convert_fw_ip_type_to_string( + stc_firewall_ip_type_e type); +const char *test_stc_convert_fw_port_type_to_string( + stc_firewall_port_type_e type); +const char *test_stc_convert_fw_protocol_type_to_string( + stc_firewall_protocol_type_e type); +const char *test_stc_convert_fw_family_type_to_string( + stc_firewall_family_type_e type); +const char *test_stc_convert_fw_chain_target_to_string( + stc_firewall_chain_target_e target); +const char *test_stc_convert_fw_rule_target_to_string( + stc_firewall_rule_target_e target);