Added dbus methods to flush existing chains. 92/163292/1
authorNishant Chaprana <n.chaprana@samsung.com>
Fri, 8 Dec 2017 08:49:36 +0000 (14:19 +0530)
committerNishant Chaprana <n.chaprana@samsung.com>
Fri, 8 Dec 2017 08:51:22 +0000 (14:21 +0530)
Below methods are added on dbus interface.
i)  IptFlushChain
ii) Ip6tFlushChain

Change-Id: I29361263c8d1badb8785524b6b55ba4c5c28f4a7
Signed-off-by: Nishant Chaprana <n.chaprana@samsung.com>
include/stc-iptables-util.h
interfaces/stc-iptables-iface.xml
packaging/stc-iptables.spec
src/helper/helper-ip6tables.c
src/helper/helper-ip6tables.h
src/helper/helper-iptables.c
src/helper/helper-iptables.h
src/stc-iptables-gdbus.c
src/stc-iptables-util.c

index 55de31d457d93e5ceb5e0a96c689517ff3108526..0f3d9199f42a4fab476477557798374d86bc7fd8 100755 (executable)
@@ -83,6 +83,11 @@ gboolean handle_iptables_remove_chain(StcChain *object,
                               const gchar *chain,
                               void *user_data);
 
+gboolean handle_iptables_flush_chain(StcChain *object,
+                              GDBusMethodInvocation *invocation,
+                              const gchar *chain,
+                              void *user_data);
+
 /* ip6tables */
 gboolean handle_ip6tables_add_rule(StcRule *object,
                               GDBusMethodInvocation *invocation,
@@ -104,4 +109,9 @@ gboolean handle_ip6tables_remove_chain(StcChain *object,
                               const gchar *chain,
                               void *user_data);
 
+gboolean handle_ip6tables_flush_chain(StcChain *object,
+                              GDBusMethodInvocation *invocation,
+                              const gchar *chain,
+                              void *user_data);
+
 #endif /* __STC_IPTABLES_UTIL_H__ */
index 7996ceac96576cf1c56c4ac5dd844d4522a37cf5..9f07458b4a0e125f865e9ac02e120a07ea113dad 100755 (executable)
                        <arg type='s' name='chain' direction='in'/>
                        <arg type='i' name='error_code' direction='out'/>
                </method>
+               <method name='IptFlushChain'>
+                       <arg type='s' name='chain' direction='in'/>
+                       <arg type='i' name='error_code' direction='out'/>
+               </method>
                <method name='Ip6tAddChain'>
                        <arg type='s' name='chain' direction='in'/>
                        <arg type='i' name='error_code' direction='out'/>
@@ -38,5 +42,9 @@
                        <arg type='s' name='chain' direction='in'/>
                        <arg type='i' name='error_code' direction='out'/>
                </method>
+               <method name='Ip6tFlushChain'>
+                       <arg type='s' name='chain' direction='in'/>
+                       <arg type='i' name='error_code' direction='out'/>
+               </method>
        </interface>
 </node>
index 2a00b7cba5f49386036914a015d0a540140ef5b8..c1df6d8c77ce88903b9606210a37d02473867a9e 100644 (file)
@@ -1,6 +1,6 @@
 Name:       stc-iptables
 Summary:    STC(Smart Traffic Control) iptables
-Version:    0.0.3
+Version:    0.0.4
 Release:    0
 Group:      Network & Connectivity/Other
 License:    GPL-2.0+
index 128787fcd86feea9e168086de75c264ed61bd55b..d5fb40824db04e8b0242857b52f847eeac5c3e98 100755 (executable)
@@ -292,3 +292,37 @@ int ip6tables_remove_chain(const char *chain)
        STC_LOGI("Success removing chain");
        return STC_ERROR_NONE;
 }
+
+int ip6tables_flush_chain(const char *chain)
+{
+       ip6t_handle_t *handle;
+
+       handle = ip6tc_init(IP6TC_TABLE);
+       if (handle == NULL) {
+               STC_LOGE("ip6tc_init failed [%s]", ip6tc_strerror(errno));
+               return STC_ERROR_OPERATION_FAILED;
+       }
+
+       if (!ip6tc_is_chain(chain, handle)) {
+               STC_LOGW("chain not present");
+               ip6tc_free(handle);
+               return STC_ERROR_NONE;
+       }
+
+       if(!ip6tc_flush_entries(chain, handle)) {
+               STC_LOGE("Failed to flush chain [%s]", ip6tc_strerror(errno));
+               ip6tc_free(handle);
+               return STC_ERROR_OPERATION_FAILED;
+       }
+
+       if (!ip6tc_commit(handle)) {
+               STC_LOGE("Failed to ip6tc_commit [%s]", ip6tc_strerror(errno));
+               ip6tc_free(handle);
+               return STC_ERROR_OPERATION_FAILED;
+       }
+
+       ip6tc_free(handle);
+
+       STC_LOGI("Success removing chain");
+       return STC_ERROR_NONE;
+}
index 4ca0438b125ba79f4b72b50de84348bc4c39780f..ff9e8735b3f72ca11b37a7951aacce5e3e0fece9 100755 (executable)
@@ -73,4 +73,10 @@ int ip6tables_add_chain(const char *chain);
  */
 int ip6tables_remove_chain(const char *chain);
 
+/**
+ * @desc This function flushes all ip6tables rules in chain.
+ * @return 0 on success and negative value if error.
+ */
+int ip6tables_flush_chain(const char *chain);
+
 #endif /*__STC_HELPER_IP6TABLES_H__*/
index 1c3cc007693a53a358631254a2eefc4acc3e9f00..9d45d7027649eb77574bf661178f068e385a232d 100755 (executable)
@@ -292,3 +292,37 @@ int iptables_remove_chain(const char *chain)
        STC_LOGI("Success removing chain");
        return STC_ERROR_NONE;
 }
+
+int iptables_flush_chain(const char *chain)
+{
+       ipt_handle_t *handle;
+
+       handle = iptc_init(IPTC_TABLE);
+       if (handle == NULL) {
+               STC_LOGE("iptc_init failed [%s]", iptc_strerror(errno));
+               return STC_ERROR_OPERATION_FAILED;
+       }
+
+       if (!iptc_is_chain(chain, handle)) {
+               STC_LOGW("chain not present");
+               iptc_free(handle);
+               return STC_ERROR_NONE;
+       }
+
+       if (!iptc_flush_entries(chain, handle)) {
+               STC_LOGE("Failed to flush chain [%s]", iptc_strerror(errno));
+               iptc_free(handle);
+               return STC_ERROR_OPERATION_FAILED;
+       }
+
+       if (!iptc_commit(handle)) {
+               STC_LOGE("Failed to iptc_commit [%s]", iptc_strerror(errno));
+               iptc_free(handle);
+               return STC_ERROR_OPERATION_FAILED;
+       }
+
+       iptc_free(handle);
+
+       STC_LOGI("Success flushing chain");
+       return STC_ERROR_NONE;
+}
index a306b9fd86f0067b03f0681b28381497eb87dd75..c74ecb3e328b72ee9e4c5918876e93c513916203 100755 (executable)
@@ -73,4 +73,10 @@ int iptables_add_chain(const char *chain);
  */
 int iptables_remove_chain(const char *chain);
 
+/**
+ * @desc This function flushes all iptables rules in chain.
+ * @return 0 on success and negative value if error.
+ */
+int iptables_flush_chain(const char *chain);
+
 #endif /*__STC_HELPER_IPTABLES_H__*/
index 525262a976996590707dc5fb5f37fc5b25fc3b73..9b7b2d89032eb16614b13580dc15e00953aa08cb 100755 (executable)
@@ -44,6 +44,10 @@ static gboolean __stc_iptables_gdbus_chain_init(stc_iptables_s *stc_iptables)
                         G_CALLBACK(handle_iptables_remove_chain),
                         stc_iptables);
 
+       g_signal_connect(chain, "handle-ipt-flush-chain",
+                        G_CALLBACK(handle_iptables_flush_chain),
+                        stc_iptables);
+
        g_signal_connect(chain, "handle-ip6t-add-chain",
                         G_CALLBACK(handle_ip6tables_add_chain),
                         stc_iptables);
@@ -52,6 +56,10 @@ static gboolean __stc_iptables_gdbus_chain_init(stc_iptables_s *stc_iptables)
                         G_CALLBACK(handle_ip6tables_remove_chain),
                         stc_iptables);
 
+       g_signal_connect(chain, "handle-ip6t-flush-chain",
+                        G_CALLBACK(handle_ip6tables_flush_chain),
+                        stc_iptables);
+
        g_dbus_object_manager_server_export(stc_iptables->obj_mgr,
                                            G_DBUS_OBJECT_SKELETON(object));
        g_object_unref(object);
index 82cfefdb602e1cc805d8f6138b74abdc2cb7f7ec..7dd163852cc7d3dd9fb7e1de745d5d4dec298a7d 100755 (executable)
@@ -275,6 +275,31 @@ gboolean handle_iptables_remove_chain(StcChain *object,
        return TRUE;
 }
 
+gboolean handle_iptables_flush_chain(StcChain *object,
+                              GDBusMethodInvocation *invocation,
+                              const gchar *chain,
+                              void *user_data)
+{
+       __STC_LOG_FUNC_ENTER__;
+       stc_error_e ret = STC_ERROR_NONE;
+       GVariant *return_parameters = NULL;
+
+       ret = iptables_flush_chain(chain);
+       if (ret < STC_ERROR_NONE) {
+               STC_IPTABLES_DBUS_REPLY_ERROR(invocation, ret);
+               __STC_LOG_FUNC_EXIT__;
+               return TRUE;
+       }
+
+       return_parameters = g_variant_new("(i)", STC_ERROR_NONE);
+
+       STC_DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
+       STC_IPTABLES_DBUS_REPLY(invocation, return_parameters);
+
+       __STC_LOG_FUNC_EXIT__;
+       return TRUE;
+}
+
 gboolean handle_ip6tables_add_rule(StcRule *object,
                               GDBusMethodInvocation *invocation,
                               GVariant *rules,
@@ -427,3 +452,28 @@ gboolean handle_ip6tables_remove_chain(StcChain *object,
        __STC_LOG_FUNC_EXIT__;
        return TRUE;
 }
+
+gboolean handle_ip6tables_flush_chain(StcChain *object,
+                              GDBusMethodInvocation *invocation,
+                              const gchar *chain,
+                              void *user_data)
+{
+       __STC_LOG_FUNC_ENTER__;
+       stc_error_e ret = STC_ERROR_NONE;
+       GVariant *return_parameters = NULL;
+
+       ret = ip6tables_flush_chain(chain);
+       if (ret < STC_ERROR_NONE) {
+               STC_IPTABLES_DBUS_REPLY_ERROR(invocation, ret);
+               __STC_LOG_FUNC_EXIT__;
+               return TRUE;
+       }
+
+       return_parameters = g_variant_new("(i)", STC_ERROR_NONE);
+
+       STC_DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
+       STC_IPTABLES_DBUS_REPLY(invocation, return_parameters);
+
+       __STC_LOG_FUNC_EXIT__;
+       return TRUE;
+}